/******************************************************************* * FUNCTION: CommandParse * AUTHOR = TRAMPAS STERN * FILE = command.c * DATE = 1/25/2004 4:03:03 PM * * PARAMETERS: Takes the command line string * * DESCRIPTION: parses the command line and returns Command ID * * RETURNS: Command ID, and * * *******************************************************************/ UINT CommandParse(CHAR *str) { CHAR *ptr; CHAR *ptr2; UINT i; CHAR cmd[MAX_STRING]; CHAR buff[MAX_CMD_LENGTH]; CHAR argv[MAX_ARGS][MAX_ARG_LENGTH]; //CHAR *ptrArgv[MAX_ARGS]; UINT numArgs; //first we need find command and arguments ptr=strchr(str,' '); //find first char if (ptr==0) { //we have two options, frist whole thing is command //second bad command if(strlen(str)>0) ptr=str+strlen(str); else return 0; //bad command } //copy string to command buffer. i=0; ptr2=str; while(ptr!=0 && ptr!=ptr2 && i<(MAX_CMD_LENGTH-1)) { buff[i++]=*ptr2++; } buff[i]=0; //now buff contains the command let's get the args numArgs=0; while(*ptr!=0 && *ptr==' ') ptr++; //increment pointer past ' ' if (*ptr!=0) { ptr2=strchr(ptr,' '); if (ptr2==0) { //we have two options, frist whole thing is command //second bad command if(strlen(ptr)>0) ptr2=ptr+strlen(ptr); } while(ptr2!=0 && numArgs<MAX_ARGS) { int j; j=0; while (ptr2!=ptr && j<(MAX_ARG_LENGTH-1)) { argv[numArgs][j++]=*ptr++; } argv[numArgs][j++]=0; numArgs++; ptr2=0; if (*ptr!=0) { while(*ptr!=0 && *ptr==' ') ptr++; //increment pointer past ' ' ptr2=strchr(ptr,' '); if (ptr2==0) { //we have two options, frist whole thing is command //second bad command if(strlen(ptr)>0) ptr2=ptr+strlen(ptr); } } } } /* for(i=0; i<MAX_ARGS; i++) { ptrArgv[i]=argv[i]; } //now let's parse the command for(i=1; i<NUM_COMMANDS; i++) { if (strcmp((const char *)buff,(const char *)strCommands[i])==0) return (*ptrCommands[i])(numArgs,ptrArgv); } */ sprintf(cmd,"exit"); if (strcmp(buff,cmd)==0) { CommandExit=1; } sprintf(cmd,"ver"); if (strcmp(buff,cmd)==0) { printf("software v%d.%02d",VER_MAJOR,VER_MINOR); } sprintf(cmd,"hwver"); if (strcmp(buff,cmd)==0) { printf("hardware v%d",HW_VER); } sprintf(cmd,"logb"); if (strcmp(buff,cmd)==0) { datalog_bin(); } sprintf(cmd,"vpwmon"); if (strcmp(buff,cmd)==0) { vpw_montior(); } sprintf(cmd,"log"); if (strcmp(buff,cmd)==0) { datalog(); } sprintf(cmd,"pwmhigh"); if (strcmp(buff,cmd)==0) { output_high(PIN_F5); } sprintf(cmd,"pwmlow"); if (strcmp(buff,cmd)==0) { output_low(PIN_F5); } sprintf(cmd,"pwmmon"); if (strcmp(buff,cmd)==0) { pwm_monitor(); } sprintf(cmd,"pwmread"); if (strcmp(buff,cmd)==0) { while(!kbhit()) { printf("PWM bus=%d\n\r",PWM_IN); } getch2(); } sprintf(cmd,"5baud"); if (strcmp(buff,cmd)==0) { UBYTE keys[2]; printf("%lu ",iso_5baud(keys,2,0x33)); printf("%X %X\n\r",keys[0],keys[1]); } sprintf(cmd,"crc"); if (strcmp(buff,cmd)==0) { UBYTE data[20]; UBYTE temp; for (i=0; i<numArgs; i++) { //we need to parse ASCII temp=ascii2hex(argv[i]); data[i]=temp; } for (temp=0; temp<numArgs; temp++) { printf("%X ",data[temp]); } printf("\n\r"); fprintf(STDIN,"CRC %X\n\r",crc(data,numArgs)); } sprintf(cmd,"csum"); if (strcmp(buff,cmd)==0) { UBYTE data[20]; UBYTE temp; for (i=0; i<numArgs; i++) { //we need to parse ASCII temp=ascii2hex(argv[i]); data[i]=temp; } for (temp=0; temp<numArgs; temp++) { printf("%X ",data[temp]); } printf("\n\r"); fprintf(STDIN,"CheckSum %X\n\r",checksum(data,numArgs)); } sprintf(cmd, "vagcom"); if (strcmp(buff,cmd)==0) { //we need to monitor the serial port // when port goes high drive k-line accordingly } sprintf(cmd, "vag"); if (strcmp(buff,cmd)==0) { UBYTE data[60]; UBYTE resp[20]; UBYTE ret; UBYTE i; UBYTE block; //first lets init communications ret=iso_5baud(data,2,0x01); data[2]=0; while(data[2]!=0x09) { //get block from ECM ret=vagGetBlock(data,60); for(i=0; i<ret; i++) { printf("%X ",data[i]); } printf("\n\r"); //now build ack block=data[1]+1; resp[0]=data[1]+1; resp[1]=0x09; vagPutBlock(resp,2); block++; } ret=vagGetBlock(data,60); for(i=0; i<ret; i++) { printf("%X ",data[i]); } printf("\n\r"); block=data[1]+1; printf("Test block "); for(i=0; i<numArgs; i++) { resp[i+1]=ascii2hex(argv[i]); printf("%X ",resp[i+1]); } printf("\n\r"); //now lets see what happens if we send a block request for block 02 resp[0]=block; //resp[1]=code; vagPutBlock(resp,numArgs+1); data[2]=0; while(data[2]!=0x09) { //get block from ECM ret=vagGetBlock(data,60); for(i=0; i<ret; i++) { printf("%X ",data[i]); } printf("\n\r"); //now build ack block=data[1]+1; resp[0]=data[1]+1; resp[1]=0x09; vagPutBlock(resp,2); block++; } } sprintf(cmd,"isosend"); if (strcmp(buff,cmd)==0) { UBYTE data[60]; UBYTE resp[20]; UBYTE i; UBYTE temp; printf("hit key to stop\n\r"); //we need to copy args to data for (i=0; i<numArgs; i++) { //we need to parse ASCII temp=ascii2hex(argv[i]); data[i]=temp; } //printf("\n\r"); while(!kbhit()) { temp=iso_send(resp, 60, data, numArgs,0x33); printf("response %u\n\r",temp); for (i=0; i<temp; i++) { printf("%X ",resp[i]); } printf("\n\r"); } while(kbhit()) getch2(); } sprintf(cmd,"pwmsend"); if (strcmp(buff,cmd)==0) { UBYTE data[60]; UBYTE resp[20]; UBYTE i; UBYTE temp; printf("hit key to stop\n\r"); //we need to copy args to data for (i=0; i<numArgs; i++) { //we need to parse ASCII temp=ascii2hex(argv[i]); data[i]=temp; //printf("args %x %s\n\r",temp,argv[i]); } //printf("\n\r"); while(!kbhit()) { temp=pwm_send(resp, 60, data, numArgs); printf("response %u\n\r",temp); for (i=0; i<temp; i++) { printf("%X ",resp[i]); } printf("\n\r"); } while(kbhit()) getch2(); } /* sprintf(cmd,"flashread"); if (strcmp(buff,cmd)==0) { UDWORD addr; addr=atoi32(argv[0]); printf("*%lu=%X\n\r",addr,flash_read(addr)); } sprintf(cmd,"flashbufread"); if (strcmp(buff,cmd)==0) { UDWORD addr; addr=atoi32(argv[0]); printf("*%lu=%X\n\r",addr,flash_buf_read(addr)); } */ sprintf(cmd,"flasherase"); if (strcmp(buff,cmd)==0) { flash_erase(); printf("Done\n\r"); } /* sprintf(cmd,"flashput"); if (strcmp(buff,cmd)==0) { UDWORD addr; UBYTE data; addr=atoi32(argv[0]); data=atoi(argv[1]); printf("Putting *%lu=%X\n\r",addr,data); data=flash_put(addr,data); printf("returned %x\n\r",data); } */ sprintf(cmd,"vpwsend"); if (strcmp(buff,cmd)==0) { UBYTE data[60]; UBYTE resp[20]; UBYTE i; UBYTE temp; printf("hit key to stop\n\r"); //we need to copy args to data for (i=0; i<numArgs; i++) { //we need to parse ASCII temp=ascii2hex(argv[i]); data[i]=temp; } //printf("\n\r"); while(!kbhit()) { temp=vpw_send(resp, 60, data, numArgs); printf("response %u\n\r",temp); for (i=0; i<temp; i++) { printf("%X ",resp[i]); } printf("\n\r"); } while(kbhit()) getch2(); } sprintf(cmd,"isomon"); if (strcmp(buff,cmd)==0) { while (!kbhit()) iso_monitor(); while(kbhit()) getch2(); } sprintf(cmd,"isoput"); if (strcmp(buff,cmd)==0) { UBYTE temp; printf("ISO Put test\n\r"); printf("press any key to stop\n\r"); temp=0x03; while (!kbhit()) iso_put(&temp,1,20); while(kbhit()) getch2(); } sprintf(cmd,"vpwhigh"); if (strcmp(buff,cmd)==0) { UBYTE temp; printf("VPW high test\n\r"); printf("press any key to stop\n\r"); temp=0x03; output_high(VPW_OUT); while (!kbhit()){ printf("VPW_IN=%u\n\r",VPW_IN); } while(kbhit()) getch2(); output_low(VPW_OUT); } sprintf(cmd,"isohigh"); if (strcmp(buff,cmd)==0) { UBYTE temp; printf("ISO high test\n\r"); printf("press any key to stop\n\r"); temp=0x03; K_HIGH; while (!kbhit()){ printf("ISO_IN=%u\n\r",K_IN); } while(kbhit()) getch2(); K_LOW; } sprintf(cmd,"isolow"); if (strcmp(buff,cmd)==0) { UBYTE temp; printf("ISO low test\n\r"); printf("press any key to stop\n\r"); temp=0x03; K_LOW; while (!kbhit()){ printf("ISO_IN=%u\n\r",K_IN); } while(kbhit()) getch2(); K_HIGH; } sprintf(cmd,"vpwlow"); if (strcmp(buff,cmd)==0) { UBYTE temp; printf("VPW low test\n\r"); printf("press any key to stop\n\r"); temp=0x03; output_low(VPW_OUT); while (!kbhit()){ printf("VPW_IN=%u\n\r",VPW_IN); } while(kbhit()) getch2(); output_low(VPW_OUT); } sprintf(cmd,"flashpgm"); if (strcmp(buff,cmd)==0) { //UBYTE ret; printf("Start Xmodem download to unit of flash"); delay_ms(250); Flash_serial_program(0); } sprintf(cmd,"flashpcodes"); if (strcmp(buff,cmd)==0) { //UBYTE ret; printf("Start Xmodem download to unit of P codes"); delay_ms(250); Flash_serial_program(PCODE_FLASH_START); } sprintf(cmd,"flashccodes"); if (strcmp(buff,cmd)==0) { //UBYTE ret; printf("Start Xmodem download to unit of C codes"); delay_ms(250); Flash_serial_program(CCODE_FLASH_START); } sprintf(cmd,"flashbcodes"); if (strcmp(buff,cmd)==0) { //UBYTE ret; printf("Start Xmodem download to unit of B codes"); delay_ms(250); Flash_serial_program(BCODE_FLASH_START); } sprintf(cmd,"flashucodes"); if (strcmp(buff,cmd)==0) { //UBYTE ret; printf("Start Xmodem download to unit of U codes"); delay_ms(250); Flash_serial_program(UCODE_FLASH_START); } sprintf(cmd,"flashfirm"); if (strcmp(buff,cmd)==0) { UWORD i; printf("Erasing Firmware\n\r"); for(i=(FIRM_FLASH_START>>8); i<(FLASH_MAX_PAGES>>8); i++) { //sprintf(temp,"%lu",i); //LCD_print2(temp,2); //printf("Erasing Page %lu\n\r",i); if (flash_erase_page(i)!=0) return 1; } printf("Start Xmodem download to unit of Firmware"); delay_ms(250); Flash_serial_program(FIRM_FLASH_START); delay_ms(250); printf("Checking Firmware\n\r"); if (FirmwareCheck(FIRM_FLASH_START)==0) { printf("Updating Firmware\n\r"); FirmwareUpdate(FIRM_FLASH_START); }else { printf("Firmware not correct, try reloading\n\r"); } }
int main(int argc, char **argv) { int fd; int i, res, desc_size = 0; char buf[256], *dev; struct hidraw_report_descriptor rpt_desc; struct hidraw_devinfo info; /* Usage check */ if (argc != 3) { fprintf(stderr, "Usage: %s <hidraw-dev> <SITL IP>\n", argv[0]); return 1; } /* Device is argv[1] */ dev = argv[1]; /* IP addr is argv[2] */ pwm_init(argv[2]); /* Open the Device with non-blocking reads. In real life, don't use a hard coded path; use libudev instead. */ fd = open(dev, O_RDWR|O_NONBLOCK); if (fd < 0) { perror("Unable to open device"); return 1; } memset(&rpt_desc, 0x0, sizeof(rpt_desc)); memset(&info, 0x0, sizeof(info)); memset(buf, 0x0, sizeof(buf)); /* Get Report Descriptor Size */ res = ioctl(fd, HIDIOCGRDESCSIZE, &desc_size); if (res < 0) { perror("HIDIOCGRDESCSIZE"); } else { printf("Report Descriptor Size: %d\n", desc_size); } /* Get Report Descriptor */ rpt_desc.size = desc_size; res = ioctl(fd, HIDIOCGRDESC, &rpt_desc); if (res < 0) { perror("HIDIOCGRDESC"); } else { printf("Report Descriptor:\n"); for (i = 0; i < rpt_desc.size; i++) { printf("%hhx ", rpt_desc.value[i]); } puts("\n"); } /* Get Raw Name */ res = ioctl(fd, HIDIOCGRAWNAME(256), buf); if (res < 0) { perror("HIDIOCGRAWNAME"); } else { printf("Raw Name: %s\n", buf); } /* Get Physical Location */ res = ioctl(fd, HIDIOCGRAWPHYS(256), buf); if (res < 0) { perror("HIDIOCGRAWPHYS"); } else { printf("Raw Phys: %s\n", buf); } /* Get Raw Info */ res = ioctl(fd, HIDIOCGRAWINFO, &info); if (res < 0) { perror("HIDIOCGRAWINFO"); } else { printf("Raw Info:\n"); printf("\tbustype: %d (%s)\n", info.bustype, bus_str(info.bustype)); printf("\tvendor: 0x%04hx\n", info.vendor); printf("\tproduct: 0x%04hx\n", info.product); } /* Set Feature */ buf[0] = 0x9; /* Report Number */ buf[1] = 0xff; buf[2] = 0xff; buf[3] = 0xff; res = ioctl(fd, HIDIOCSFEATURE(4), buf); if (res < 0) { perror("HIDIOCSFEATURE"); } else { printf("ioctl HIDIOCGFEATURE returned: %d\n", res); } /* Get Feature */ buf[0] = 0x9; /* Report Number */ res = ioctl(fd, HIDIOCGFEATURE(256), buf); if (res < 0) { perror("HIDIOCGFEATURE"); } else { printf("ioctl HIDIOCGFEATURE returned: %d\n", res); printf("Report data (not containing the report number):\n\t"); for (i = 0; i < res; i++) { printf("%hhx ", buf[i]); } puts("\n"); } /* Send a Report to the Device */ buf[0] = 0x1; /* Report Number */ buf[1] = 0x77; res = write(fd, buf, 2); if (res < 0) { printf("Error: %d\n", errno); perror("write"); } else { printf("write() wrote %d bytes\n", res); } /* Get a report from the device */ res = read(fd, buf, 16); if (res < 0) { perror("read"); } else { printf("read() read %d bytes:\n\t", res); for (i = 0; i < res; i++) { printf("%hhx ", buf[i]); } puts("\n"); } /* OP loop */ while (1) { /* Send a Report to the Device */ buf[0] = 0x0; /* Report Number */ buf[1] = 0x0; res = write(fd, buf, 2); if (res < 0) { printf("Error: %d\n", errno); perror("write"); } /* Get a report from the device */ res = read(fd, buf, 16); if (res < 0) { perror("read"); } else { pwm_send(buf, res); } usleep(20 * 1000); } close(fd); return 0; }