int8 dealPacket( pgcontext pgc, ppacket pTxBuf ) { GAgent_Printf(GAGENT_DEBUG,"\r\n"); GAgent_Printf(GAGENT_DEBUG,"IN %s packet type : %04x",__FUNCTION__ ,pTxBuf->type ); if( ((pTxBuf->type)&(LOCAL_DATA_OUT)) == LOCAL_DATA_OUT ) { GAgent_Printf( GAGENT_DEBUG,"packet Type : LOCAL_DATA_OUT "); GAgent_LocalDataWriteP0( pgc,pgc->rtinfo.local.uart_fd, pTxBuf,MCU_CTRL_CMD ); GAgent_Printf( GAGENT_DEBUG,"ReSetpacket Type : LOCAL_DATA_OUT "); pTxBuf->type = SetPacketType(pTxBuf->type, LOCAL_DATA_OUT, 0); } if( ((pTxBuf->type)&(CLOUD_DATA_OUT)) == CLOUD_DATA_OUT ) { if( ((pgc->rtinfo.GAgentStatus)&WIFI_CLOUD_CONNECTED) == WIFI_CLOUD_CONNECTED ) { GAgent_Printf( GAGENT_DEBUG,"packet Type : CLOUD_DATA_OUT "); GAgent_Cloud_SendData( pgc,pTxBuf,(pTxBuf->pend)-(pTxBuf->ppayload) ); GAgent_Printf( GAGENT_DEBUG,"ReSetpacket Type : CLOUD_DATA_OUT "); } pTxBuf->type = SetPacketType(pTxBuf->type, CLOUD_DATA_OUT, 0); } if( ((pTxBuf->type)&(LAN_TCP_DATA_OUT)) == LAN_TCP_DATA_OUT ) { GAgent_Lan_SendTcpData(pgc, pTxBuf); pTxBuf->type = SetPacketType(pTxBuf->type, LAN_TCP_DATA_OUT, 0); } GAgent_Printf( GAGENT_DEBUG,"OUT packet type : %04X\r\n",pTxBuf->type ); return 0; }
//--------------------------------------------------------------------------- void PacketFile::atClose(void) { if(isOpen() && fileMode != READ) // update filesize { int32_t endPtr = getLength(); //seek(sizeof(int32_t)); //Move Past Version Marker //writeLong(endPtr); //Write File length int32_t tableEntry; currentPacket = numPackets; if(!seekTable) { while(--currentPacket >= 0) { seek(TABLE_ENTRY(currentPacket)); tableEntry = readLong(); if(GetPacketType(tableEntry) == STORAGE_TYPE_NUL) { seek(TABLE_ENTRY(currentPacket)); writeLong(SetPacketType(endPtr, STORAGE_TYPE_NUL)); } else { endPtr = GetPacketOffset(tableEntry); } } } else { while(--currentPacket >= 0) { tableEntry = seekTable[currentPacket]; if(GetPacketType(tableEntry) == STORAGE_TYPE_NUL) { seekTable[currentPacket] = SetPacketType(endPtr, STORAGE_TYPE_NUL); } else { endPtr = GetPacketOffset(tableEntry); } } } //----------------------------------------------------- // If seekTable was being used, write it back to file if(seekTable) { seek(sizeof(int32_t) * 2); //File Version & File Length write(puint8_t(seekTable), (numPackets * sizeof(int32_t))); } //------------------------------------------------------ // Is we were using a checkSum, calc it and write it to // the beginning of the file. if(usesCheckSum) { int32_t checkSum = checkSumFile(); seek(0); writeLong(checkSum); } } clear(); }
bool JackNetSlaveInterface::Init() { jack_log("JackNetSlaveInterface::Init()"); // set the parameters to send strcpy(fParams.fPacketType, "params"); fParams.fProtocolVersion = NETWORK_PROTOCOL; SetPacketType(&fParams, SLAVE_AVAILABLE); // init loop : get a master and start, do it until connection is ok net_status_t status; do { // first, get a master, do it until a valid connection is running do { status = SendAvailableToMaster(); if (status == NET_SOCKET_ERROR) { return false; } } while (status != NET_CONNECTED); // then tell the master we are ready jack_info("Initializing connection with %s...", fParams.fMasterNetName); status = SendStartToMaster(); if (status == NET_ERROR) { return false; } } while (status != NET_ROLLING); return true; }
void JackNetMasterInterface::Exit() { jack_log("JackNetMasterInterface::Exit, ID %u", fParams.fID); // stop process fRunning = false; // send a 'multicast euthanasia request' - new socket is required on macosx jack_info("Exiting '%s' %s", fParams.fName, fMulticastIP); SetPacketType(&fParams, KILL_MASTER); JackNetSocket mcast_socket(fMulticastIP, fSocket.GetPort()); session_params_t net_params; memset(&net_params, 0, sizeof(session_params_t)); SessionParamsHToN(&fParams, &net_params); if (mcast_socket.NewSocket() == SOCKET_ERROR) { jack_error("Can't create socket : %s", StrError(NET_ERROR_CODE)); } if (mcast_socket.SendTo(&net_params, sizeof(session_params_t), 0, fMulticastIP) == SOCKET_ERROR) { jack_error("Can't send suicide request : %s", StrError(NET_ERROR_CODE)); } mcast_socket.Close(); }
//--------------------------------------------------------------------------- void PacketFile::reserve(int32_t count, bool useCheckSum) { //--------------------------------------------------- // If we already have packets, reserve does nothing. // Otherwise, reserve sets up the file. Must be // called before any writing to a newly created file. if(numPackets) { return; } usesCheckSum = useCheckSum; numPackets = count; int32_t firstPacketOffset = TABLE_ENTRY(numPackets); writeLong(PACKET_FILE_VERSION); writeLong(firstPacketOffset); //---------------------------- // initialize the seek table while(count-- > 0) writeLong(SetPacketType(firstPacketOffset, STORAGE_TYPE_NUL)); //------------------------------------------------------------- // If we called this, chances are we are writing a packet file // from start to finish. It is MUCH faster if this table is // updated in memory and flushed when the file is closed. if(!seekTable) { seekTable = (int32_t*)systemHeap->Malloc(numPackets * sizeof(int32_t)); if(seekTable != nullptr) { seek(sizeof(int32_t) * 2); //File Version & File Length read(puint8_t(seekTable), (numPackets * sizeof(int32_t))); } } }
bool JackNetMasterInterface::Init() { jack_log("JackNetMasterInterface::Init : ID %u", fParams.fID); session_params_t host_params; uint attempt = 0; int rx_bytes = 0; // socket if (fSocket.NewSocket() == SOCKET_ERROR) { jack_error("Can't create socket : %s", StrError(NET_ERROR_CODE)); return false; } // timeout on receive (for init) if (fSocket.SetTimeOut(MASTER_INIT_TIMEOUT) < 0) { jack_error("Can't set init timeout : %s", StrError(NET_ERROR_CODE)); } // connect if (fSocket.Connect() == SOCKET_ERROR) { jack_error("Can't connect : %s", StrError(NET_ERROR_CODE)); return false; } // send 'SLAVE_SETUP' until 'START_MASTER' received jack_info("Sending parameters to %s...", fParams.fSlaveNetName); do { session_params_t net_params; memset(&net_params, 0, sizeof(session_params_t)); SetPacketType(&fParams, SLAVE_SETUP); SessionParamsHToN(&fParams, &net_params); if (fSocket.Send(&net_params, sizeof(session_params_t), 0) == SOCKET_ERROR) { jack_error("Error in send : %s", StrError(NET_ERROR_CODE)); } memset(&net_params, 0, sizeof(session_params_t)); if (((rx_bytes = fSocket.Recv(&net_params, sizeof(session_params_t), 0)) == SOCKET_ERROR) && (fSocket.GetError() != NET_NO_DATA)) { jack_error("Problem with network"); return false; } SessionParamsNToH(&net_params, &host_params); } while ((GetPacketType(&host_params) != START_MASTER) && (++attempt < SLAVE_SETUP_RETRY)); if (attempt == SLAVE_SETUP_RETRY) { jack_error("Slave doesn't respond, exiting"); return false; } return true; }
// Separate the connection protocol into two separated step bool JackNetSlaveInterface::InitConnection(int time_out_sec) { jack_log("JackNetSlaveInterface::InitConnection time_out_sec = %d", time_out_sec); int try_count = (time_out_sec > 0) ? int((1000000.f * float(time_out_sec)) / float(SLAVE_INIT_TIMEOUT)) : INT_MAX; // set the parameters to send strcpy(fParams.fPacketType, "params"); fParams.fProtocolVersion = NETWORK_PROTOCOL; SetPacketType(&fParams, SLAVE_AVAILABLE); return (SendAvailableToMaster(try_count) == NET_CONNECTED); }
/**************************************************************** FunctionName : GAgent_Cloud_SendData Description : send buf data to M2M server. return : 0-ok other fail. Add by Alex.lin --2015-03-17 ****************************************************************/ uint32 GAgent_Cloud_SendData( pgcontext pgc,ppacket pbuf,int32 buflen ) { int8 ret = 0; if( isPacketTypeSet( pbuf->type,CLOUD_DATA_OUT ) == 1); { ret = MQTT_SenData( pgc->gc.DID,pbuf,buflen ); GAgent_Printf(GAGENT_INFO,"Send date to cloud :len =%d ,ret =%d",buflen,ret ); pbuf->type = SetPacketType( pbuf->type,CLOUD_DATA_OUT,0 ); } return ret; }
net_status_t JackNetSlaveInterface::SendStartToMaster() { jack_log("JackNetSlaveInterface::SendStartToMaster"); // tell the master to start session_params_t net_params; memset(&net_params, 0, sizeof(session_params_t)); SetPacketType(&fParams, START_MASTER); SessionParamsHToN(&fParams, &net_params); if (fSocket.Send(&net_params, sizeof(session_params_t), 0) == SOCKET_ERROR) { jack_error("Error in send : %s", StrError(NET_ERROR_CODE)); return (fSocket.GetError() == NET_CONN_ERROR) ? NET_ERROR : NET_SEND_ERROR; } return NET_ROLLING; }
bool JackNetSlaveInterface::InitConnection() { jack_log ( "JackNetSlaveInterface::InitConnection()" ); //set the parameters to send strcpy (fParams.fPacketType, "params"); fParams.fProtocolVersion = SLAVE_PROTOCOL; SetPacketType (&fParams, SLAVE_AVAILABLE); net_status_t status; do { //get a master status = SendAvailableToMaster(); if (status == NET_SOCKET_ERROR) return false; } while (status != NET_CONNECTED); return true; }
/**************************************************************** FunctionName : Cloud_M2MDataHandle. Description : Receive cloud business data . xpg : global context. Rxbuf : global buf struct. buflen : receive max len. return : >0 have business data,and need to handle. other,no business data. Add by Alex.lin --2015-03-10 ****************************************************************/ int32 Cloud_M2MDataHandle( pgcontext pgc,ppacket pbuf /*, ppacket poutBuf*/, uint32 buflen) { uint32 dTime=0,ret=0,dataLen=0; uint32 packetLen=0; pgcontext pGlobalVar=NULL; pgconfig pConfigData=NULL; int8 *username=NULL; int8 *password=NULL; uint8* pMqttBuf=NULL; fd_set readfd; int32 mqtt_fd=0; uint16 mqttstatus=0; uint8 mqttpackType=0; pConfigData = &(pgc->gc); pGlobalVar = pgc; pMqttBuf = pbuf->phead; mqttstatus = pGlobalVar->rtinfo.waninfo.mqttstatus; mqtt_fd = pGlobalVar->rtinfo.waninfo.m2m_socketid; readfd = pGlobalVar->rtinfo.readfd; username = pConfigData->DID; password = pConfigData->wifipasscode; if( strlen(pConfigData->m2m_ip)==0 ) { //GAgent_Printf( GAGENT_INFO,"M2M IP =0 IP TIME 1 %d 2%d ",pgc->rtinfo.waninfo.RefreshIPLastTime,pgc->rtinfo.waninfo.RefreshIPTime); return 0; } if( MQTT_STATUS_START==mqttstatus ) { GAgent_Printf(GAGENT_INFO,"Req to connect m2m !"); GAgent_Printf(GAGENT_INFO,"username: %s password: %s",username,password); Cloud_ReqConnect( pgc,username,password ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGIN ); GAgent_Printf(GAGENT_INFO," MQTT_STATUS_START "); pgc->rtinfo.waninfo.send2MqttLastTime = GAgent_GetDevTime_MS(); return 0; } dTime = abs( GAgent_GetDevTime_MS()-pGlobalVar->rtinfo.waninfo.send2MqttLastTime ); if( FD_ISSET( mqtt_fd,&readfd )||( mqttstatus!=MQTT_STATUS_RUNNING && dTime>GAGENT_MQTT_TIMEOUT)) { if( FD_ISSET( mqtt_fd,&readfd ) ) { GAgent_Printf(GAGENT_DEBUG,"Data form M2M!!!"); resetPacket( pbuf ); packetLen = MQTT_readPacket(mqtt_fd,pbuf,GAGENT_BUF_LEN ); if( packetLen==-1 ) { mqtt_fd=-1; pGlobalVar->rtinfo.waninfo.m2m_socketid=-1; GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_START ); GAgent_Printf(GAGENT_DEBUG,"MQTT fd was closed!!"); GAgent_Printf(GAGENT_DEBUG,"GAgent go to MQTT_STATUS_START"); return -1; } else if(packetLen>0) { mqttpackType = MQTTParseMessageType( pMqttBuf ); GAgent_Printf( GAGENT_DEBUG,"MQTT message type %d",mqttpackType ); } else { return -1; } } /*create mqtt connect to m2m.*/ if( MQTT_STATUS_RUNNING!=mqttstatus && (MQTT_MSG_CONNACK==mqttpackType||MQTT_MSG_SUBACK==mqttpackType||dTime>GAGENT_MQTT_TIMEOUT)) { switch( mqttstatus) { case MQTT_STATUS_RES_LOGIN: ret = Cloud_ResConnect( pMqttBuf ); if( ret!=0 ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_REQ_LOGIN Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf(GAGENT_DEBUG,"MQTT req connetc m2m again!"); Cloud_ReqConnect( pgc,username,password ); } } else { GAgent_Printf(GAGENT_DEBUG,"GAgent do req connect m2m OK !"); GAgent_Printf(GAGENT_DEBUG,"Go to MQTT_STATUS_REQ_LOGINTOPIC1. "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC1 ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGINTOPIC1 ); } break; case MQTT_STATUS_RES_LOGINTOPIC1: ret = Cloud_ResSubTopic(pMqttBuf,pgc->rtinfo.waninfo.mqttMsgsubid ); if( 0!=ret ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_RES_LOGINTOPIC1 Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf( GAGENT_DEBUG,"GAgent req sub LOGINTOPIC1 again "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC1 ); } } else { GAgent_Printf(GAGENT_DEBUG,"Go to MQTT_STATUS_RES_LOGINTOPIC2. "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC2 ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGINTOPIC2 ); } break; case MQTT_STATUS_RES_LOGINTOPIC2: ret = Cloud_ResSubTopic(pMqttBuf,pgc->rtinfo.waninfo.mqttMsgsubid ); if( 0 != ret ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_RES_LOGINTOPIC2 Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf( GAGENT_INFO,"GAgent req sub LOGINTOPIC2 again."); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC2 ); } } else { GAgent_Printf(GAGENT_DEBUG," Go to MQTT_STATUS_RES_LOGINTOPIC3. "); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC3 ); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RES_LOGINTOPIC3 ); } break; case MQTT_STATUS_RES_LOGINTOPIC3: ret = Cloud_ResSubTopic(pMqttBuf,pgc->rtinfo.waninfo.mqttMsgsubid ); if( ret != 0 ) { GAgent_Printf(GAGENT_DEBUG," MQTT_STATUS_RES_LOGINTOPIC3 Fail "); if( dTime > GAGENT_MQTT_TIMEOUT ) { GAgent_Printf(GAGENT_DEBUG,"GAgent req sub LOGINTOPIC3 again." ); Cloud_ReqSubTopic( pgc,MQTT_STATUS_REQ_LOGINTOPIC3 ); } } else { GAgent_Printf(GAGENT_CRITICAL,"GAgent Cloud Working..."); GAgent_SetCloudServerStatus( pgc,MQTT_STATUS_RUNNING ); GAgent_SetWiFiStatus( pgc,WIFI_CLOUD_STATUS,0 ); } break; default: break; } pgc->rtinfo.waninfo.send2MqttLastTime = GAgent_GetDevTime_MS(); } else if( packetLen>0 && ( mqttstatus == MQTT_STATUS_RUNNING ) ) { int varlen=0,p0datalen=0; switch( mqttpackType ) { case MQTT_MSG_PINGRESP: pgc->rtinfo.waninfo.cloudPingTime=0; GAgent_Printf(GAGENT_INFO,"GAgent MQTT Pong ... \r\n"); break; case MQTT_MSG_PUBLISH: dataLen = Mqtt_DispatchPublishPacket( pgc,pMqttBuf,packetLen ); if( dataLen>0 ) { pbuf->type = SetPacketType( pbuf->type,CLOUD_DATA_IN,1 ); ParsePacket( pbuf ); GAgent_Printf(GAGENT_INFO,"%s %d type : %04X len :%d",__FUNCTION__,__LINE__,pbuf->type,dataLen ); } break; default: GAgent_Printf(GAGENT_WARNING," data form m2m but msg type is %d",mqttpackType ); break; } } } return dataLen; }
//--------------------------------------------------------------------------- int32_t PacketFile::writePacket(int32_t packet, puint8_t buffer, int32_t nbytes, uint8_t pType) { //-------------------------------------------------------- // This function writes the packet to the current end // of file and stores the packet address in the seek // table. NOTE that this cannot be used to replace // a packet. That function is writePacket which takes // a packet number and a buffer. The size cannot change // and, as such, is irrelevant. You must write the // same sized packet each time, if the packet already // exists. In theory, it could be smaller but the check // right now doesn't allow anything but same size. int32_t result = 0; puint8_t workBuffer = nullptr; if(pType == ANY_PACKET_TYPE || pType == STORAGE_TYPE_LZD || pType == STORAGE_TYPE_ZLIB) { if((nbytes << 1) < 4096) workBuffer = (puint8_t)malloc(4096); else workBuffer = (puint8_t)malloc(nbytes << 1); gosASSERT(workBuffer != nullptr); } gosASSERT((packet > 0) || (packet < numPackets)); packetBase = getLength(); currentPacket = packet; packetSize = packetUnpackedSize = nbytes; //----------------------------------------------- // Code goes in here to pick the best compressed // version of the packet. Otherwise, default // to RAW. if((pType == ANY_PACKET_TYPE) || (pType == STORAGE_TYPE_LZD) || (pType == STORAGE_TYPE_ZLIB)) { if(pType == ANY_PACKET_TYPE) pType = STORAGE_TYPE_RAW; //----------------------------- // Find best compression here. // This USED to use LZ. Use ZLib from now on. // Game will ALWAYS be able to READ LZ Packets!! uint32_t actualSize = nbytes << 1; if(actualSize < 4096) actualSize = 4096; uint32_t workBufferSize = actualSize; uint32_t oldBufferSize = nbytes; int32_t compressedResult = compress2(workBuffer, &workBufferSize, buffer, nbytes, Z_DEFAULT_COMPRESSION); if(compressedResult != Z_OK) STOP(("Unable to write packet %d to file %s. Error %d", packet, fileName, compressedResult)); compressedResult = uncompress(buffer, &oldBufferSize, workBuffer, nbytes); if((int32_t)oldBufferSize != nbytes) STOP(("Packet size changed after compression. Was %d is now %d", nbytes, oldBufferSize)); if((pType == STORAGE_TYPE_LZD) || (pType == STORAGE_TYPE_ZLIB) || ((int32_t)workBufferSize < nbytes)) { pType = STORAGE_TYPE_ZLIB; packetSize = workBufferSize; } } packetType = pType; seek(packetBase); if(packetType == STORAGE_TYPE_ZLIB) { writeLong(packetUnpackedSize); result = write(workBuffer, packetSize); } else { result = write(buffer, packetSize); } if(!seekTable) { seek(TABLE_ENTRY(packet)); writeLong(SetPacketType(packetBase, packetType)); } else { seekTable[packet] = SetPacketType(packetBase, packetType); } int32_t* currentEntry = nullptr; if(seekTable) { packet++; currentEntry = &(seekTable[packet]); } int32_t tableData = SetPacketType(getLength(), STORAGE_TYPE_NUL); while(packet < (numPackets - 1)) { if(!seekTable) { writeLong(tableData); } else { *currentEntry = tableData; currentEntry++; } packet++; } if(workBuffer) free(workBuffer); return result; }
uint32 GAgent_LocalDataHandle( pgcontext pgc,ppacket Rxbuf,int32 RxLen /*,ppacket Txbuf*/ ) { int8 cmd=0; uint8 sn=0,checksum=0; uint8 *localRxbuf=NULL; uint32 ret = 0; uint8 configType=0; _tm tm; if( RxLen>0 ) { localRxbuf = Rxbuf->phead; cmd = localRxbuf[4]; sn = localRxbuf[5]; checksum = GAgent_SetCheckSum( localRxbuf,RxLen-1 ); if( checksum!=localRxbuf[RxLen-1] ) { GAgent_Printf( GAGENT_ERROR,"local data cmd=%02x checksum error !",cmd ); GAgent_DebugPacket(Rxbuf->phead, RxLen); return 0; } switch( cmd ) { case MCU_REPORT: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); Rxbuf->type = SetPacketType( Rxbuf->type,LOCAL_DATA_IN,1 ); ParsePacket( Rxbuf ); ret = 1; break; case MCU_CONFIG_WIFI: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); configType = localRxbuf[8]; GAgent_Config( configType,pgc ); ret = 0; break; case MCU_RESET_WIFI: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); GAgent_Clean_Config(pgc); sleep(2); GAgent_DevReset(); ret = 0; break; case WIFI_PING2MCU_ACK: pgc->rtinfo.local.timeoutCnt=0; GAgent_Printf( GAGENT_CRITICAL,"Local pong..."); ret = 0 ; break; case MCU_CTRL_CMD_ACK: ret = 0; break; case WIFI_TEST: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); GAgent_EnterTest( pgc ); ret = 0; break; case MCU_ENABLE_BIND: Local_Ack2MCU( pgc->rtinfo.local.uart_fd,sn,cmd+1 ); pgc->mcu.passcodeTimeout = pgc->mcu.passcodeEnableTime; GAgent_SetWiFiStatus( pgc,WIFI_MODE_BINDING,1 ); ret = 0; break; case MCU_REQ_GSERVER_TIME: tm = GAgent_GetLocalTimeForm(pgc->rtinfo.clock); *(uint16 *)(Rxbuf->ppayload) = htons(tm.year); Rxbuf->ppayload[2] = tm.month; Rxbuf->ppayload[3] = tm.day; Rxbuf->ppayload[4] = tm.hour; Rxbuf->ppayload[5] = tm.minute; Rxbuf->ppayload[6] = tm.second; Rxbuf->pend = (Rxbuf->ppayload) + 7; Local_Ack2MCUwithP0( Rxbuf, pgc->rtinfo.local.uart_fd, sn, MCU_REQ_GSERVER_TIME_ACK ); ret = 0; break; default: ret = 0; break; } //... } return ret; }
/*************************************************** FunctionName : ParsePacket. Description : set the source phead ppayload pend. pbug : data source struct. return : 0 ok other fail. Add by Alex.lin --2015-03-21 ***************************************************/ uint32 ParsePacket( ppacket pRxBuf ) { int32 varlen=0; int32 datalen=0; uint16 cmd=0; uint16 *pcmd=NULL; GAgent_Printf(GAGENT_DEBUG,"\r\n"); GAgent_Printf(GAGENT_DEBUG,"IN %s packet type : %04x",__FUNCTION__ ,pRxBuf->type ); if( ((pRxBuf->type)&(CLOUD_DATA_IN)) == CLOUD_DATA_IN ) { datalen = mqtt_parse_rem_len( pRxBuf->phead+3 ); varlen = mqtt_num_rem_len_bytes( pRxBuf->phead+3 ); pcmd = (u16*)&(pRxBuf->phead[4+varlen+1]); cmd = ntohs( *pcmd ); GAgent_Printf( GAGENT_INFO,"CLOUD_DATA_IN cmd : %04X", cmd ); if( cmd == 0x0090 ) { pRxBuf->ppayload = pRxBuf->phead+4+varlen+1+2; } if( cmd ==0x0093 ) {//with sn. pRxBuf->ppayload = pRxBuf->phead+4+varlen+1+2+4; } pRxBuf->pend = pRxBuf->phead+4+varlen+datalen; GAgent_Printf( GAGENT_DEBUG," ReSet Data Type : %04X - CLOUD_DATA_IN", pRxBuf->type ); pRxBuf->type = SetPacketType( pRxBuf->type,CLOUD_DATA_IN,0 ); pRxBuf->type = SetPacketType( pRxBuf->type,LOCAL_DATA_OUT,1 ); GAgent_Printf( GAGENT_DEBUG," Set Data Type : %04X - LOCAL_DATA_OUT", pRxBuf->type ); } else if( ((pRxBuf->type)&(LOCAL_DATA_IN)) == LOCAL_DATA_IN ) { /* head(0xffff)| len(2B) | cmd(1B) | sn(1B) | flag(2B) | payload(xB) | checksum(1B) */ pRxBuf->ppayload = pRxBuf->phead+8; /* head + len + cmd + sn + flag */ datalen = ( (int32)ntohs( *(uint16 *)(pRxBuf->phead + 2) ) ) & 0xffff; pRxBuf->pend = (pRxBuf->phead )+( datalen+4 ); /* datalen + head + len */ GAgent_Printf( GAGENT_DEBUG," ReSet Data Type : %04X - LOCAL_DATA_IN", pRxBuf->type ); pRxBuf->type = SetPacketType( pRxBuf->type,LOCAL_DATA_IN,0 ); pRxBuf->type = SetPacketType( pRxBuf->type,CLOUD_DATA_OUT,1 ); pRxBuf->type = SetPacketType( pRxBuf->type,LAN_TCP_DATA_OUT,1 ); GAgent_Printf( GAGENT_DEBUG," Set Data Type : %04X - CLOUD_DATA_OUT & LAN_TCP_DATA_OUT ",pRxBuf->type ); } else if( ((pRxBuf->type)&(LAN_TCP_DATA_IN)) == LAN_TCP_DATA_IN ) { datalen = mqtt_parse_rem_len( pRxBuf->phead+3 ); varlen = mqtt_num_rem_len_bytes( pRxBuf->phead+3 ); pRxBuf->ppayload = pRxBuf->phead + LAN_PROTOCOL_HEAD_LEN + varlen + LAN_PROTOCOL_FLAG_LEN + LAN_PROTOCOL_CMD_LEN; pRxBuf->pend = pRxBuf->phead + LAN_PROTOCOL_HEAD_LEN + varlen + datalen; GAgent_Printf( GAGENT_DEBUG," ReSet Data Type : %04X - LAN_TCP_DATA_IN", pRxBuf->type ); pRxBuf->type = SetPacketType( pRxBuf->type,LAN_TCP_DATA_IN,0 ); pRxBuf->type = SetPacketType( pRxBuf->type,LOCAL_DATA_OUT,1 ); GAgent_Printf( GAGENT_DEBUG," Set Data Type : %04X - LOCAL_DATA_OUT", pRxBuf->type ); } else { GAgent_Printf( GAGENT_DEBUG,"Data Type error,wite :%04X ", pRxBuf->type ); return 1; } GAgent_Printf( GAGENT_DEBUG,"OUT packet type : %04X\r\n",pRxBuf->type ); return 0; }