c语言转变java语言求帮助
#include <stdint.h>
#include <stdio.h>
typedef enum {
SABO_NO_ERR,
SABO_SYS_ERR,
SABO_POINTER_ERR,
SABO_OVERFLOW_ERR,
SABO_DATA_ERR,
} ErrCode;
void lib_debug(char *text,uint8_t *data,uint16_t length){
uint16_t i = 0;
printf("%s;length:%u;data:",text,length);
for(i = 0; i < length; i++){
printf(" %02X",data[ i ]);
}
printf(" END ");
}
uint8_t get_bcc(uint8_t *buff,uint16_t length){
uint8_t bcc = 0;
uint16_t i = 0;
for(i = 0;i < length;i++){
bcc ^= *(buff+i);
}
return bcc;
}
ErrCode analysis_tlv(uint8_t data,uint8_t head_code,uint8_t *buff,uint16_t max_len,void func(uint8_t*,uint16_t)){
static uint8_t star = 0,step = 0,dlen,bcc;
static uint16_t length = 0;
if(!buff)return SABO_POINTER_ERR;
if(star == 0){
if(data == head_code){
star = 1;
length = 0;
dlen = bcc = 0;
buff[length++] = data;
}
}else{
if(length < max_len){
buff[length++] = data;
if(length == 2){
dlen = buff[1];
if(dlen == 0){
star = 0;
return SABO_DATA_ERR;
}
}
if(dlen > 0){
bcc ^= data;
if((length-2) == dlen){
if(bcc == 0){
if(func){
func(buff,length);
}else{
star = 0;
return SABO_POINTER_ERR;
}
}star = 0;
}
}
}else{
star = 0;
return SABO_OVERFLOW_ERR;
}
}
return SABO_NO_ERR;
}
void recv_event(uint8_t *buff,uint16_t length){
lib_debug((char*)__func__,buff,length);
}
uint8_t rbuf[100];
void recv(uint8_t value){
ErrCode Err = analysis_tlv(value,0x7F,rbuf,100,recv_event);
if(Err > 0){
printf("ERROR:%u ",Err);
}
}
int main(){
uint8_t buf[1024] = {0x7F,0x16,0xA4,0x00,0x30,0x38,0x38,0x38,0x38,0x39,0x32,0x36,0x36,0xD0,0xBB,0xC9,0xFA,0x00,0x00,0x00,0x00,0x00,0x00,0xD1};
//uint8_t buf[1024] = {0x7F,0x16,0xA4,0x00,0x30,0x38,0x38,0x38,0x38,0x39,0x32,0x36,0x36,0xD0,0xBB,0xC9,0xFA,0x00,0x00,0x00,0x00,0x00,0x00,0xD1,0,1,2,3,4,5,6,0x7F,0x16,0xA4,0x00,0x30,0x38,0x38,0x38,0x38,0x39,0x32,0x36,0x36,0xD0,0xBB,0xC9,0xFA,0x00,0x00,0x00,0x00,0x00,0x00,0xD1,0x7F,0x16,0xA4,0x00,0x30,0x38,0x38,0x38,0x38,0x39,0x32,0x36,0x36,0xD0,0xBB,0xC9,0xFA,0x00,0x00,0x00,0x00,0x00,0x00,0xD1},i;
uint16_t i;
for(i = 0;i < 100;i++){
recv(*(buf+i));
}
while(1);
return 0;
}