void criarTabela(char *linha,int i){ int pos = 1; strcpy(tabelas[i].nome,""); char *temp; strcpy(temp,""); int count = 0; while(linha[pos] != '}'){ if(linha[pos] == ':'){ pos++; while(linha[pos] != ','){ sprintf(tabelas[i].nome,"%s%c",tabelas[i].nome,linha[pos]); count++; pos++; } } pos++; } while(count < 29){ tabelas[i].nome[count]=' '; count++; } linha = strstr(linha,"qtd_campos="); pos=10; while(linha[pos] != ','){ pos++; if(linha[pos] != ','){ sprintf(temp,"%s%c",temp,linha[pos]); }else{ tabelas[i].quantidadeCampos = atoi(temp); strcpy(temp,""); } } tabelas[i].dados= (campo*)malloc(sizeof(campo)*tabelas[i].quantidadeCampos); int j,k; char *auxiliar; strcpy(temp,""); for(j=0;j<tabelas[i].quantidadeCampos;j++){ k=0; auxiliar = strstr(linha,"campos=["); pos = 8; while(k != j){ if(auxiliar[pos] == ';'){k++;} pos++; }count=0; while(auxiliar[pos] != ';'){ if(auxiliar[pos] != ';'){ sprintf(temp,"%s%c",temp,auxiliar[pos]); count++; }if(auxiliar[pos+1] == ';'){ strcpy(tabelas[i].dados[j].nome,temp); strcpy(temp,""); while(count < 29){ tabelas[i].dados[j].nome[count]=' '; count++; } count = 0; } pos++; } strcpy(temp,""); k=0; auxiliar = strstr(linha,"tamanho=["); pos = 9; while(k != j){ if(auxiliar[pos] == ','){k++;} pos++; } while(auxiliar[pos] != ','){ if(auxiliar[pos] != ','){ sprintf(temp,"%s%c",temp,auxiliar[pos]); }if(auxiliar[pos+1] == ','){ tabelas[i].dados[j].tamanho = atoi(temp); strcpy(temp,""); } pos++; } strcpy(temp,""); k=0; auxiliar = strstr(linha,"tipo=["); pos = 6; while(k != j){ if(auxiliar[pos] == ','){k++;} pos++; } count = 0; while(auxiliar[pos] != ','){ if(auxiliar[pos] != ','){ sprintf(temp,"%s%c",temp,auxiliar[pos]); count++; }if(auxiliar[pos+1] == ','){ strcpy(tabelas[i].dados[j].tipo,temp); strcpy(temp,""); while(count < 29){ tabelas[i].dados[j].tipo[count]=' '; count++; } count = 0; } pos++; } } uint16_t tamanhoTabela = 0; for(j=0;j<tabelas[i].quantidadeCampos;j++){ tamanhoTabela += tabelas[i].dados[i].tamanho; } tabelas[i].crc = gen_crc16(&tabelas[i], tamanhoTabela); printf("crc:%x\n",tabelas[i].crc); system("PAUSE"); system("cls"); }
int main ( int argc, char** argv ) { char* hexfile = NULL; char* devname = NULL; ISP_EID eid = EID_NOK; IspMessage ispmsg; /* * TODO: enhance the argument parsing */ if (argc < 5) { printf("[Error Usage]:\n" "at892prog -f <hex file name>\n" " -d <serial device>\n"); return -1; } else { hexfile = argv[2]; devname = argv[4]; } // init memset(&serial_dev, 0, sizeof(SerialDevice)); // prepare message header ispmsg.hdr.typ = MSGT_MEM_WRITE; ispmsg.hdr.len = DATA_SIZE; ispmsg.hdr.crc = 0x0000; // message data content eid = read_hexfile(hexfile, ispmsg.msg.data, &ispmsg.hdr.len); if (eid != EID_OK) { printf("Could not read hex file: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } printf("Read hex file successfully: %d bytes\n", ispmsg.hdr.len); // data crc ispmsg.hdr.crc = gen_crc16(ispmsg.msg.data, ispmsg.hdr.len); printf("Data CRC: %04x\n", ispmsg.hdr.crc); // encode message eid = encode_message(&ispmsg, send_buffer); if (eid != EID_OK) { printf("Encode message failed: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } printf("Message was encoded successfully\n"); // open connection to TTY device eid = serial_open(devname, &serial_dev, &serial_cfg); if (eid != EID_OK) { printf("Open TTY device failed: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } printf("Connect to %s successfully\n", devname); // create TTY receiving thread if (pthread_create(&tid_receiving, NULL, get_data_routine, NULL) < 0) { printf("Cannot create thread: %s\n", strerror(errno)); goto EXIT_PROGRAM; } // send message to device eid = serial_send(&serial_dev, send_buffer, MESSAGE_SIZE); if (eid != EID_OK) { printf("Message sent failed: eid=%d errno=%s\n", eid, strerror(errno)); goto EXIT_PROGRAM; } else printf("Sent %d bytes\n", MESSAGE_SIZE); EXIT_PROGRAM: // waiting for thread done pthread_join(tid_receiving, NULL); if (eid != EID_OK) { printf("Received serial data failed: eid=%d errno=%s\n", eid, strerror(errno)); } eid = serial_close(&serial_dev); if (eid != EID_OK) { printf("Device closed fail: eid=%d errno=%s\n", eid, strerror(errno)); } printf("Device closed\n"); return 0; }