int32_t ProcessCommand(uint8_t *pu8Buffer, uint32_t u32BufferLen) { uint32_t u32sum; printf("ProcessCommand\n"); //shanchun20121116 my_memcpy((uint8_t *)&gCmd, pu8Buffer, u32BufferLen); /* Check size */ if((gCmd.u8Size > sizeof(gCmd)) || (gCmd.u8Size > u32BufferLen)) return -1; /* Check signature */ if(gCmd.u32Signature != HID_CMD_SIGNATURE) return -1; /* Calculate checksum & check it*/ u32sum = CalCheckSum((uint8_t *)&gCmd, gCmd.u8Size); if(u32sum != gCmd.u32Checksum) return -1; switch(gCmd.u8Cmd) { case HID_CMD_ERASE: { HID_CmdEraseSectors(&gCmd); break; } case HID_CMD_READ: { HID_CmdReadPages(&gCmd); break; } case HID_CMD_WRITE: { HID_CmdWritePages(&gCmd); break; } case HID_CMD_TEST: { HID_CmdTest(&gCmd); break; } case HID_CMD_TKWRITE: { HID_CmdTKWrite(&gCmd); break; } case HID_CMD_TKREAD: { HID_CmdTKRead(&gCmd); break; } default: return -1; } return 0; }
int encodeFrame(Prn_Frame_t *pFrame) { BYTE EvenChk,OddChk; int len; int CmdBufLen; CalCheckSum(pFrame->pFlagBuf+1,pFrame->FlagBufLen-1,&EvenChk,&OddChk); CmdBufLen=pFrame->FlagBufLen-1; memmove(pFrame->pFlagBuf+7,pFrame->pFlagBuf+1,CmdBufLen); pFrame->pFlagBuf[1] = pFrame->pFlagBuf[0]; pFrame->pFlagBuf[0]=0xc0; pFrame->pFlagBuf[2] = pFrame->i8DataId; pFrame->pFlagBuf[3] = (BYTE)(CmdBufLen/1000+0x30); pFrame->pFlagBuf[4] = (BYTE)(CmdBufLen%1000/100)+0x30; pFrame->pFlagBuf[5] = (BYTE)(CmdBufLen%100/10)+0x30; pFrame->pFlagBuf[6] = (BYTE)(CmdBufLen%10)+0x30; pFrame->pFlagBuf[CmdBufLen+7] = EvenChk; pFrame->pFlagBuf[CmdBufLen+8] = OddChk; len=CmdEncode(pFrame->pFlagBuf+1,CmdBufLen+8); pFrame->pFlagBuf[len+1]=0xc1; return len+2; }
int encodeFrame(BYTE *buf,Prn_Frame_t *pFrame) { static BYTE TempBuf[10000]; BYTE EvenChk,OddChk; int len; TempBuf[0] = pFrame->i8TypeOfFrame; TempBuf[1] = pFrame->i8DataId; TempBuf[2] = (BYTE)(pFrame->CmdBufLen/1000+0x30); TempBuf[3] = (BYTE)(pFrame->CmdBufLen%1000)/100+0x30; TempBuf[4] = (BYTE)(pFrame->CmdBufLen%100)/10+0x30; TempBuf[5] = (BYTE)pFrame->CmdBufLen%10+0x30; memcpy(TempBuf+6,pFrame->pCmdBuf,pFrame->CmdBufLen); CalCheckSum(pFrame->pCmdBuf,pFrame->CmdBufLen,&EvenChk,&OddChk); TempBuf[pFrame->CmdBufLen+6] = EvenChk; TempBuf[pFrame->CmdBufLen+7] = OddChk; buf[0]=0xc0; len=CmdEncode(buf+1,TempBuf,pFrame->CmdBufLen+8); // ASSERT_MINE(len>0); buf[len+1]=0xc1; return len+2; }
int CmdGetIoName(TCPSOCKET * sock,CmdHead * cmd,int datasize) { const uint8_t outsize = sizeof(CmdIoName); uint8_t buffer[sizeof(CmdHead)+outsize]; CmdHead * tcmd = (CmdHead *)buffer; CmdIoName * tio = (CmdIoName *)GET_CMD_DATA(tcmd); CmdIoName * rio = (CmdIoName *)GET_CMD_DATA(cmd); // datasize = datasize; // if(!BspReadIoName(rio->io_addr,tio)) { //sprintf(tio->io_name,"INPUT%d",rio->io_addr[0]); tcmd->cmd_option = CMD_ACK_OK; } else { if(THISINFO)printf("CmdGetIoName Failed!!!\r\n"); tcmd->cmd_option = CMD_ACK_KO; } tio->io_addr[0] = rio->io_addr[0]; tio->io_addr[1] = rio->io_addr[1]; // tcmd->cmd = CMD_GET_IO_NAME; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = outsize; tcmd->data_checksum = CalCheckSum(tio,outsize); // NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdGetIoName()\r\n"); return 0; }
int CmdRevertIoOutIndex(TCPSOCKET * sock,CmdHead * cmd,int datasize) { int rc = -1; const uint8_t outsize = sizeof(CmdIobitmap); uint8_t buffer[sizeof(CmdHead)+outsize]; CmdHead * tcmd = (CmdHead *)buffer; CmdIobitmap * sio = (CmdIobitmap *)GET_CMD_DATA(tcmd); CmdIobitmap * io = (CmdIobitmap *)GET_CMD_DATA(cmd); // if(datasize < sizeof(CmdIobitmap)) { if(THISERROR)printf("ERROR:Cmd CmdSetIoOutBit Datasize ERROR\r\n"); tcmd->cmd_option = CMD_ACK_KO; goto error; } // tcmd->cmd_option = CMD_ACK_OK; //rc = _ioctl(_fileno(sys_varient.iofile), IO_SIG_BITMAP, io->io_msk); //rc = _ioctl(_fileno(sys_varient.iofile), IO_OUT_GET, sio->io_msk); io_out_convert_bits(0,io->io_msk,32); memset(sio->io_msk,0,sizeof(sio->io_msk)); io_out_get_bits(0,sio->io_msk,32); rc = 0; error: tcmd->cmd = CMD_REV_IO_SOME_BIT; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = outsize; tcmd->data_checksum = CalCheckSum(sio,outsize); // NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdSetIoOutBit()!\r\n"); return rc; }
int CmdGetInputCtlModeIndex(TCPSOCKET * sock,CmdHead * cmd,int datasize) { const uint8_t outsize = sizeof(CmdInputModeIndex); uint8_t buffer[sizeof(CmdHead)+outsize]; CmdHead * tcmd = (CmdHead *)buffer; CmdInputModeIndex * sio = (CmdInputModeIndex *)GET_CMD_DATA(tcmd); CmdInputModeIndex * io = (CmdInputModeIndex *)GET_CMD_DATA(cmd); // datasize = datasize; // sio->index = io->index; if(!BspReadManualCtlModeIndex(io->index,&sio->mode)) { tcmd->cmd_option = CMD_ACK_OK; } else { if(THISINFO)printf("BspReadManualCtlModeIndex Failed!!!\r\n"); tcmd->cmd_option = CMD_ACK_KO; } tcmd->cmd = CMD_GET_INPUT_CTL_MODE_INDEX; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = sizeof(CmdInputModeIndex); tcmd->data_checksum = CalCheckSum(sio,sizeof(CmdInputModeIndex)); // NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdGetInputCtlModeIndex()\r\n"); return 0; }
int CmdIoFinish(TCPSOCKET * sock,CmdHead * tcmd,int outsize) { tcmd->cmd_len = outsize; tcmd->data_checksum = CalCheckSum(GET_CMD_DATA(tcmd),outsize); NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); //if(THISINFO)printf("CmdIoFinish()\r\n"); return 0; }
int CmdSetIoOutValue(TCPSOCKET * sock,CmdHead * cmd,int datasize) { int rc = -1; const uint8_t outsize = sizeof(CmdIoValue); uint8_t buffer[sizeof(CmdHead)+outsize]; CmdHead * tcmd = (CmdHead *)buffer; CmdIoValue * sio = (CmdIoValue *)GET_CMD_DATA(tcmd); // CmdIoValue * io = (CmdIoValue *)GET_CMD_DATA(cmd); // if(datasize < sizeof(CmdIoValue)) { if(THISERROR)printf("ERROR:Cmd Set Io Value Datasize ERROR\r\n"); return -1; } if(io->io_count <= 32) { //uint32_t tmp; //SetRelayWithDelay(group_arry4_to_uint32(io->io_value)); //rc = _ioctl(_fileno(sys_varient.iofile), IO_OUT_SET, io->io_value); //rc = _ioctl(_fileno(sys_varient.iofile), IO_OUT_GET, sio->io_value); io_out_set_bits(0,io->io_value,32); memset(sio->io_value,0,sizeof(sio->io_value)); sio->io_count = io_out_get_bits(0,sio->io_value,32); rc = 0; tcmd->cmd_option = CMD_ACK_OK; sio->io_count = io->io_count; //tmp = GetIoOut(); //sio->io_value[0] = (uint8_t)((tmp >> 0) & 0xFF); //sio->io_value[1] = (uint8_t)((tmp >> 8) & 0xFF); //sio->io_value[2] = (uint8_t)((tmp >> 16) & 0xFF); //sio->io_value[3] = (uint8_t)((tmp >> 24) & 0xFF); } else { if(THISERROR)printf("Cmd Set Io Io Count Error!!\r\n"); tcmd->cmd_option = CMD_ACK_KO; sio->io_count = 0; } tcmd->cmd = CMD_SET_IO_OUT_VALUE; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = outsize; tcmd->data_checksum = CalCheckSum(sio,outsize); // NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdSetIoOutValue()\r\n"); return rc; }
int CmdRevIoOutOneBit(TCPSOCKET * sock,CmdHead * cmd,int datasize) { int rc = -1; const uint8_t outsize = sizeof(CmdIoValue); uint8_t buffer[sizeof(CmdHead)+outsize]; unsigned int num; unsigned char reg; CmdIoOneBit * io = (CmdIoOneBit *)GET_CMD_DATA(cmd); CmdHead * tcmd = (CmdHead *)buffer; CmdIoValue * sio = (CmdIoValue *)GET_CMD_DATA(tcmd); // if(datasize < sizeof(CmdIoOneBit)) { if(THISERROR)printf("ERROR:Cmd CmdSetIoOutOneBit Datasize ERROR\r\n"); tcmd->cmd_option = CMD_ACK_KO; goto error; } // tcmd->cmd_option = CMD_ACK_OK; num = io->io_bitnum[1]; num <<= 8; num += io->io_bitnum[0]; reg = code_msk[0]; printf("rev io one bit ,num = %d\r\n",num); io_out_convert_bits(num,®,1); memset(sio->io_value,0,sizeof(sio->io_value)); sio->io_count = io_out_get_bits(0,sio->io_value,32); rc = 0; error: tcmd->cmd = CMD_REV_IO_ONE_BIT; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = outsize; tcmd->data_checksum = CalCheckSum(sio,outsize); // NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdSetIoOutBit()!\r\n"); return rc; }
int CmdGetIoInValue(TCPSOCKET * sock,CmdHead * cmd,int datasize) { int rc = -1; //uint32_t tmp; const uint8_t outsize = sizeof(CmdIoValue); uint8_t buffer[sizeof(CmdHead)+outsize]; CmdHead * tcmd = (CmdHead *)buffer; CmdIoValue * sio = (CmdIoValue *)GET_CMD_DATA(tcmd); // datasize = datasize; // //rc = _ioctl(_fileno(sys_varient.iofile), GET_IN_NUM, &tmp); //sio->io_count = (unsigned char)tmp; //if(tmp) { //rc = _ioctl(_fileno(sys_varient.iofile), IO_IN_GET, sio->io_value); //} memset(sio->io_value,0,sizeof(sio->io_value)); sio->io_count = io_in_get_bits(0,sio->io_value,32); rc = 0; //sio->io_count = 8; //sio->io_value[0] = (uint32_t)((GetFilterInput() >> 0) & 0xFF); //sio->io_value[1] = (uint32_t)((GetFilterInput() >> 8) & 0xFF); //sio->io_value[2] = (uint32_t)((GetFilterInput() >> 16) & 0xFF); //sio->io_value[3] = (uint32_t)((GetFilterInput() >> 24) & 0xFF); // tcmd->cmd_option = CMD_ACK_OK; tcmd->cmd = CMD_GET_IO_IN_VALUE; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = outsize; tcmd->data_checksum = CalCheckSum(sio,outsize); // NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdGetIoInValue()!\r\n"); return rc; }
int CmdGetIpConfig(TCPSOCKET * sock,CmdHead * cmd,int datasize) { const uint8_t outsize = sizeof(CmdIpConfigData); uint8_t buffer[sizeof(CmdHead)+outsize]; CmdHead * tcmd = (CmdHead *)buffer; CmdIpConfigData * sio = (CmdIpConfigData *)GET_CMD_DATA(tcmd); // datasize = datasize; // if(!BspReadIpConfig(sio)) { tcmd->cmd_option = CMD_ACK_OK; } else { tcmd->cmd_option = CMD_ACK_KO; } // tcmd->cmd = CMD_GET_IP_CONFIG; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = sizeof(CmdIpConfigData); tcmd->data_checksum = CalCheckSum(sio,sizeof(CmdIpConfigData)); // NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdGetIpConfig()\r\n"); return 0; }
//-------------------------------------------------------------------- int CmdGetIoOutValue(TCPSOCKET * sock,CmdHead * cmd,int datasize) { int rc = -1; const uint8_t outsize = sizeof(CmdIoValue); uint8_t buffer[sizeof(CmdHead)+outsize]; CmdHead * tcmd = (CmdHead *)buffer; CmdIoValue * sio = (CmdIoValue *)GET_CMD_DATA(tcmd); // datasize = datasize; // memset(sio->io_value,0,sizeof(sio->io_value)); sio->io_count = io_out_get_bits(0,sio->io_value,32); rc = 0; // tcmd->cmd_option = CMD_ACK_OK; tcmd->cmd = CMD_GET_IO_OUT_VALUE; tcmd->cmd_index = cmd->cmd_index; tcmd->cmd_len = outsize; tcmd->data_checksum = CalCheckSum(sio,outsize); NutTcpSend(sock,tcmd,(int)(sizeof(CmdHead)+outsize)); if(THISINFO)printf("CmdGetIoOutValue OK\r\n"); return rc; }