// Looks for defined commands and takes appropriate action. // read_buffer - the data to examine. void onCommand(char *read_buffer, int chars_read, int serial_port) { // Terminate the data read with a null to enable using string functions // on the data read. if (strstr(read_buffer, "led1on") != NULL) { led_control(0, 1); serial_port_write("led1 is on\r\n",serial_port); printf("led1 is on\r\n"); } else if (strstr(read_buffer, "led1off") != NULL) { led_control(0, 0); serial_port_write("led1 is off\r\n",serial_port); printf("led1 is off\r\n"); } else if (strstr(read_buffer, "closeapp") != NULL) { applicationStop(serial_port); } else { //print everything else. printf("%s\n",read_buffer); } }
//=========================================== int main (int argc, char * argv[]) { //------------------ //debugging option char dump,showlastonly; //------------------- int i,j,k,len,cl; unsigned int c; unsigned char data[1024]; char buffer[1024]; char * pb; //evaluate cmdline line argument if(argc < 4 ){ printf("usage %s <device> <baudrate> <ussdcommand>\n",argv[0]); return 1; } //dump verbose option if (!memcmp("-vvv",argv[1],4)){cl=1;showlastonly=1;dump=1;verbose=1;} else if(!memcmp("-vv" ,argv[1],3)){cl=1;showlastonly=1;dump=1;verbose=0;} else if(!memcmp("-v" ,argv[1],2)){cl=1;showlastonly=1;dump=0;verbose=0;} else {cl=0;showlastonly=0,dump=0,verbose=0;} //setting up baudrate serial.baudrate=atoi(argv[cl+2]); if(getbaudrate(serial.baudrate)==1){ fprintf(stderr,"Fatal Error, cannot set baudrate: %d\n",serial.baudrate); return 1; } serial.port=strdup(argv[cl+1]); //opening the port if (openport()){ fprintf(stderr,"Fatal Error, cannot open device: %s\n",serial.port); return 1; } ///* if(dump)printf("AT\n"); serial_port_write("AT\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } //if(len)printhex(data,len); if(dump)printf("ATE0\n"); serial_port_write("ATE0\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } //if(len)printhex(data,len); if(dump)printf("AT^CURC=0\n"); serial_port_write("AT^CURC=0\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } //if(len)printhex(data,len); if(dump)printf("AT^USSDMODE=0\n"); serial_port_write("AT^USSDMODE=0\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } //if(len)printhex(data,len); //sending the ussd sequence start at argv[3]; for (i=cl+3;i<argc;i++){ memset(buffer,0,sizeof(buffer)); strcpy(buffer,"AT+CUSD=1,\""); strcat(buffer,argv[i]); strcat(buffer,"\",15\r\n"); //strcat(buffer,"\"\r\n"); if(dump){printf(buffer);} serial_port_write(buffer); len=getser(data,timeoutussd); //see if this is not ascii //skip respon daOKda+CUSSD: 1,", start at 18 //end ,68"da at len-6 if(len>22){ //data received nicely, dump to stdout if only last ussd sequence if( showlastonly || i == (argc-1)) { //check wheter ascii transparent or encoded 7bit ascii //if(dump)fwrite(&data[18],1,len-18-6,stdout); k=0; for (j=18;j<22;j++){ if (((data[j] >= '0') && (data[j] <='9')) || ((data[j] >= 'A') && (data[j] <='F')) || ((data[j] >= 'a') && (data[j] <='f')) ) {k++;} else {k=0;break;} } if(k){ //data not ascii encoded 7bit ascii integer (2byte char) //printf("not ascii\n"); //printf("length %d bagi 4 %d sisa %d\n",(len-18-6),((len-18-6)/4),(len-18-6) % 4 ); //printf("\n"); for(k=18;k<(len-6);k+=4){ c=0; for(j=0;j<4;j++){ c=c<<4; if ((data[k+j] >= '0') && (data[k+j] <='9'))c+=data[k+j]-'0'; else if ((data[k+j] >= 'A') && (data[k+j] <='F'))c+=data[k+j]-'A'+0x0a; else if ((data[k+j] >= 'a') && (data[k+j] <='f'))c+=data[k+j]-'a'+0x0a; else {perror ("failed 7bit decode\n");return 1;} } putchar(c);//putchar(0x20); } printf("\n"); } //data is ascii transparent, continue else {fwrite(&data[18],1,len-18-6,stdout);printf("\n");} }//if(i==argc-1) }//if(len>22) else{ perror("ERROR data!\n"); fwrite(data,1,len,stdout); break; //goto endsession; //return 1; } }//for (i=3;i<argc;i++) endsession: //end ussd session if(dump)printf("AT+CUSD=2\r\n"); serial_port_write("AT+CUSD=2\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } closeport(); return 0; }
int main(int argc, char *argv[]){ write_uart_error_ptr = &write_uart_error; //initialize the function pointer to write error write_udp_error_ptr = &write_udp_error; write_decode_error_ptr = &write_decode_error; write_log_error_ptr = &write_log_error; //parse arguments if(argc == 4){ //first argument is always name of program or empty string connection.server_ip=argv[1]; connection.port_number_lisa_to_pc=atoi(argv[2]); connection.port_number_pc_to_lisa=atoi(argv[3]); }else{ printf("wrong parameters: server ip - send port number - receive port number\n"); exit(EXIT_FAILURE); } //init log (mount sd card if necessary) int err = init_log(); LOG_err_handler(err,write_log_error_ptr); if(err != LOG_ERR_NONE){ exit(EXIT_FAILURE); //mounting SD card failed } enable_ptp(); #if LOGGING > 0 //init circular data log buffers cbInit(cb_read_lisa, CBSIZE); cbInit(cb_write_lisa, CBSIZE); cbInit(cb_read_ground, CBSIZE); cbInit(cb_write_ground, CBSIZE); #endif err = serial_port_setup(); UART_err_handler(err,write_uart_error_ptr); if(err != UART_ERR_NONE){ exit(EXIT_FAILURE); } //thread variables pthread_t thread_lisa_to_pc,thread_data_logging_lisa,thread_data_logging_ground; //create a second thread which executes lisa_to_pc if(pthread_create(&thread_lisa_to_pc, NULL, lisa_to_pc,NULL)) { error_write(FILENAME,"error creating lisa thread"); exit(EXIT_FAILURE); } #if LOGGING > 0 //create a third thread which executes data_logging_lisa if(pthread_create(&thread_data_logging_lisa, NULL, data_logging_lisa,NULL)) { error_write(FILENAME,"error creating lisa logging thread"); exit(EXIT_FAILURE); } //create a fourth thread which executes data_logging_groundstation if(pthread_create(&thread_data_logging_ground, NULL, data_logging_groundstation,NULL)) { error_write(FILENAME,"error creating groundstation logging thread"); exit(EXIT_FAILURE); } #endif /*-------------------------START OF FIRST THREAD: PC TO LISA------------------------*/ static UDP udp_server; uint8_t input_stream[OUTPUT_BUFFER]; ElemType cb_elem = {0}; //init the data decode pointers init_decoding(); UDP_err_handler(openUDPServerSocket(&udp_server,connection.port_number_pc_to_lisa,UDP_SOCKET_TIMEOUT),write_udp_error_ptr); while(1){ //1. retreive UDP data form PC from ethernet port. err=receiveUDPServerData(&udp_server,(void *)&input_stream,sizeof(input_stream)); //blocking !!! UDP_err_handler(err,write_udp_error_ptr); if(err==UDP_ERR_NONE){ #if LOGGING > 0 if(!cbIsFull(cb_write_ground)){ memcpy (&cb_elem.value, &input_stream, sizeof(input_stream)); cbWrite(cb_write_ground, &cb_elem); }else{ if(reading_flag_ground==0){ switch_cb_ground_pointers(); }else{ printf("GROUND WRITE WAS NOT READY \n"); } } #endif int new_length = strip_timestamp(input_stream); //lisa expects a package without a timestamp UART_err_handler(serial_port_write(input_stream,new_length),write_uart_error_ptr); } } UART_err_handler(serial_port_close(),write_uart_error_ptr); UDP_err_handler(closeUDPServerSocket(&udp_server),write_udp_error_ptr); /*------------------------END OF FIRST THREAD------------------------*/ //wait for the second thread to finish if(pthread_join(thread_lisa_to_pc, NULL)) { error_write(FILENAME,"error joining thread_lisa_to_pc"); } #if LOGGING > 0 //wait for the third thread to finish if(pthread_join(thread_data_logging_lisa, NULL)) { error_write(FILENAME,"error joining thread_data_logging_lisa"); } //wait for the fourth thread to finish if(pthread_join(thread_data_logging_ground, NULL)) { error_write(FILENAME,"error joining thread_data_logging_ground"); } //free circular buffers cbFree(cb_read_lisa); cbFree(cb_write_lisa); cbFree(cb_read_ground); cbFree(cb_write_ground); #endif return 0; }
/** Helper for printing to the serial port. * @param ch Character to display. * @param data Port to print to. * @param total Pointer to total character count. */ static void serial_port_printf_helper(char ch, void *data, int *total) { serial_port_t *port = data; serial_port_write(port, ch); *total = *total + 1; }
//=========================================== int main (int argc, char * argv[]) { //------------------ //debugging option char dump,showlastonly; //------------------- int i,j,k,len,cl; unsigned int c; unsigned char data[1024]; char buffer[1024]; char * pb; //evaluate cmdline line argument if(argc < 3 ){ printf("usage %s <device> <baudrate>\n",argv[0]); return 1; } //dump verbose option if (!memcmp("-vvv",argv[1],4)){cl=1;showlastonly=1;dump=1;verbose=1;} else if(!memcmp("-vv" ,argv[1],3)){cl=1;showlastonly=1;dump=1;verbose=0;} else if(!memcmp("-v" ,argv[1],2)){cl=1;showlastonly=1;dump=0;verbose=0;} else {cl=0;showlastonly=0,dump=0,verbose=0;} //setting up baudrate serial.baudrate=atoi(argv[cl+2]); if(getbaudrate(serial.baudrate)==1){ fprintf(stderr,"Fatal Error, cannot set baudrate: %d\n",serial.baudrate); return 1; } serial.port=strdup(argv[cl+1]); //opening the port if (openport()){ fprintf(stderr,"Fatal Error, cannot open device: %s\n",serial.port); return 1; } ///* if(dump)printf("AT\n"); serial_port_write("AT\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } //if(len)printhex(data,len); if(dump)printf("ATE0\n"); serial_port_write("ATE0\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } //if(len)printhex(data,len); if(dump)printf("AT^CURC=0\n"); serial_port_write("AT^CURC=0\r\n"); len=getser(data,timeout); if(len){ if(dump)fwrite(data,1,len,stdout); pb=(char *) buffer; pb=strstr((char *)data,"OK"); if(memcmp("OK",pb,2)){ perror("ERROR data!\n"); fwrite(data,1,len,stdout); return 1; } }else{ perror("ERROR timeout!\n"); return 1; } //if(len)printhex(data,len); closeport(); return 0; }