/**************************************************************************** * * Copy right: Qx.Chen jie * File name: TcpTask.c * Description: 主要负责:1.数据发送、定时心跳上报,2.数据接收、命令接收及处理,3.Fota函数调用,4.驻网及失败状态监测与上报 * History: 2021-07-05 * Version: V3.0 ****************************************************************************/ #include "AppTaskTcp.h" #include "numeric.h" //局部变量申请 static StaticTask_t gProcess_Tcp_Task_t; static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE]; static QueueHandle_t psEventQueueHandle; //状态转换队列句柄 static osThreadId_t TcpTaskId = NULL; static eventCallbackMessage_t *queueItem = NULL; static UINT8 gImsi[16] = {0}; static UINT32 gCellID = 0; static QueueHandle_t TcpRecvHandle = NULL; static UINT8 TcpRecvEnd = 0; static UINT32 send_counter = 0; static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait); static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen); static void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent); static void TcpDataInfoRecvHandle(); static void TcpDataInfoAssembleSend(); static void TcpHeartDataSend(); static void TcpTask(void *arg) { appSetEDRXSettingSync(0, 5, 1800000); appSetPSMSettingSync(0, 3 * 60 * 60, 10); appSetCFUN(1); psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t *)); if (psEventQueueHandle == NULL) { return; } if (TcpRecvHandle == NULL) { TcpRecvHandle = osMessageQueueNew(3, sizeof(TcpipConnectionRecvDataInd *), NULL); } osDelay(5000); //等待驻网延时5s while (true) { osDelay(100); if (gProcess_app == WORK && TCPWorkState == 0x00 && AppNVMData.EOLState == 1 && TimeCounter % 10 == 0) { sendQueueMsg(QMSG_ID_SOCK_SENDPKG, 0); } else if (gProcess_app == LISTEN && TimeCounter % (10 * 60 * 3) == 0) { sendQueueMsg(QMSG_ID_SOCK_LISTEN, 0); send_counter = 0; } if (TCPWorkState != 0 && TimeCounter % 10 == 0) { TcpRecvEnd++; if (TcpRecvEnd > 10) { TcpRecvEnd = 0; TCPWorkState = 0; } } if (xQueueReceive(psEventQueueHandle, &queueItem, 0)) { switch (queueItem->messageId) { case QMSG_ID_NW_IP_READY: if (socContext.id < 0) { struct addrinfo hints; struct addrinfo *servinfo = NULL; char *Tcp_ip_des; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; getaddrinfo(TCP_ADD, NULL, &hints, &servinfo); Tcp_ip_des = ip4addr_ntoa((const ip4_addr_t *)(&(servinfo->ai_addr->sa_data[2]))); #ifdef USING_PRINTF1 printf("\nservinfo-%d:\n", servinfo->ai_addr->sa_len); for (int i = 0; i < 4; i++) printf("%d ", servinfo->ai_addr->sa_data[2 + i]); printf("\nTcp_ip_des-%d:\n", sizeof(Tcp_ip_des)); for (int i = 0; i < 16; i++) printf("%c", *(Tcp_ip_des + i)); #endif socContext.id = tcpipConnectionCreate(TCPIP_CONNECTION_PROTOCOL_TCP, PNULL, 0, Tcp_ip_des, TCP_PORT, socketAppConnectionCallBack); if (NULL != servinfo) { freeaddrinfo(servinfo); } } if (socContext.id >= 0) { TcpErrorcount = 0; socContext.status = APP_SOCKET_CONNECTION_CONNECTED; #ifdef USING_PRINTF printf("create connection %u success\n", socContext.id); #endif ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_2, P_INFO, 1, "create connection %u success", socContext.id); } else { sendQueueMsg(QMSG_ID_NW_IP_NOREACHABLE, 0); #ifdef USING_PRINTF printf("create connection fail\n"); #endif ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_3, P_ERROR, 0, "create connection fail"); } break; case QMSG_ID_NW_IP_NOREACHABLE: { TcpErrorcount++; socContext.id = -1; appSetCFUN(0); osDelay(5000); appSetCFUN(1); osDelay(10000); #ifdef USING_PRINTF printf("Network restart\n"); #endif } break; case QMSG_ID_SOCK_SENDPKG: if (socContext.id >= 0 && socContext.status == APP_SOCKET_CONNECTION_CONNECTED) { NmAtiSyncRet netstatus1; appGetNetInfoSync(0, &netstatus1); if (netstatus1.body.netInfoRet.netifInfo.netStatus == NM_NETIF_ACTIVATED || netstatus1.body.netInfoRet.netifInfo.netStatus == NM_NETIF_ACTIVATED_INFO_CHNAGED) { TcpDataInfoAssembleSend(); } else { sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } } else { sendQueueMsg(QMSG_ID_NW_IP_READY, 0); #ifdef USING_PRINTF printf("connection %u or status invalid", socContext.id, socContext.status); #endif ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_9, P_ERROR, 2, "connection %u or status invalid", socContext.id, socContext.status); } break; case QMSG_ID_SOCK_RECVPKG: { TcpDataInfoRecvHandle(); osDelay(100); TcpRecvEnd = 0; break; } case QMSG_ID_NW_IP_SUSPEND: if (socContext.id >= 0 && socContext.status != APP_SOCKET_CONNECTION_CLOSED) { if (tcpipConnectionClose(socContext.id) < 0) { #ifdef USING_PRINTF printf("close connection %u success", socContext.id); #endif socContext.id = -1; socContext.status = APP_SOCKET_CONNECTION_CLOSED; ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_4, P_INFO, 1, "close connection %u success", socContext.id); } else { #ifdef USING_PRINTF printf("close connection %u fail", socContext.id); #endif ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_5, P_ERROR, 1, "close connection %u fail", socContext.id); } } else { sendQueueMsg(QMSG_ID_NW_IP_NOREACHABLE, 0); #ifdef USING_PRINTF printf("connection %u or status invalid", socContext.id, socContext.status); #endif ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_6, P_ERROR, 2, "connection %u or status invalid", socContext.id, socContext.status); } break; case QMSG_ID_SOCK_LISTEN: { if (socContext.id >= 0 && socContext.status == APP_SOCKET_CONNECTION_CONNECTED) { #ifdef USING_PRINTF printf("[%d]QMSG_ID_SOCK_LISTEN\n", __LINE__); #endif TcpHeartDataSend(); } else { sendQueueMsg(QMSG_ID_NW_IP_READY, 0); #ifdef USING_PRINTF printf("connection %u or status invalid", socContext.id, socContext.status); #endif } break; } case QMSG_ID_SOCK_EXIT: #ifdef USING_PRINTF printf("QMSG_ID_SOCK_EXIT queueItem->messageId:%x\n", queueItem->messageId); #endif #ifdef USING_PRINTF printf("socket exit\n"); #endif appSetCFUN(0); osDelay(1000); break; } free(queueItem); } //if } //while 循环 } /*********************************************************************************************************************** * 网络组包发送函数 * 输入:空 * 输出:空 * 处理:将相关信息组包之后进行按一定频率发送,每次调用间隔为1s ***********************************************************************************************************************/ static void TcpDataInfoAssembleSend() { OsaUtcTimeTValue TimeStracture; INT8 snr = 0; INT8 rsnr = 0; UINT16 DataLen = 0; UINT16 BattSendFreq = 30; UINT8 GpsSendFreq = 30; UINT16 year; UINT8 month, day, hour, minute, sec; UTC8TimeType UTC8TimeTcp; UINT8 i = 0; INT16 len = -1; UINT8 *SendBuffer = NULL; appGetSignalInfoSync(&csq, &snr, &rsnr); BattSendFreq = AppDataInfo.BattInfoSendFreqNomal; GpsSendFreq = AppDataInfo.PosInfoSendFreqNormal; if (getbit(PadInterrupt, 3) == 1 || getbit(PadInterrupt, 4) == 1) //有震动产生,提高位置信息发送频率 { GpsSendFreq = AppDataInfo.PosInfoSendFreqHigh; if (send_counter % 10 == 0) { clrbit(PadInterrupt, 3); clrbit(PadInterrupt, 4); } } if (BattWorkStateDelay == BATT_IDLE_SYM) { BattSendFreq = 10 * 60; } else { BattSendFreq = 10; } /* if(FALSE) { UINT16 ErrorTemp = 0x00; UINT8 ProtocolHeaderLen = 25; //电池信息协议头部加校验码长度,此长度不更改 UINT8 ProtocolFixedLen = 60; //电池信息协议固定总长度 如协议新增,需要更改此长度 UINT8 ProtocolFluctedLen = AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + BMS_OTHER_TEMP + NB_OTHER_TEMP_NUM;//电池信息协议变动长度 SendBuffer = malloc(ProtocolFixedLen + ProtocolFluctedLen); if(SendBuffer==NULL) { return; } appGetSystemTimeUtcSync(&TimeStracture); year=(TimeStracture.UTCtimer1&0xffff0000)>>16; month=(TimeStracture.UTCtimer1&0xff00)>>8; day=TimeStracture.UTCtimer1&0xff; hour=(TimeStracture.UTCtimer2&0xff000000)>>24; minute=(TimeStracture.UTCtimer2&0xff0000)>>16; sec=(TimeStracture.UTCtimer2&0xff00)>>8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp,year,month,day,hour,minute,sec); appGetSignalInfoSync(&csq,&snr,&rsnr); *(SendBuffer + 0 ) = TCP_START_SYM1; //起始码-1 *(SendBuffer + 1 ) = TCP_START_SYM2; //起始码-2 *(SendBuffer + 2 ) = TCP_CMD_SYM; //命令标识-3 *(SendBuffer + 3 ) = TCP_ANS_SYM; //应答标识-4 memcpy(SendBuffer + 4,AppNVMData.battSN,BATT_SN_LEN); //SN码 5-21 *(SendBuffer + 21 ) = TCP_ENCPT_DISABLE; //加密方式-22 DataLen = ProtocolFixedLen + ProtocolFluctedLen -ProtocolHeaderLen; //电池信息单元协议固定长度 *(SendBuffer + 22 ) = (DataLen>>8) & 0xFF; //数据长度H-23 *(SendBuffer + 23 ) = DataLen & 0xFF; //数据长度L-24 *(SendBuffer + 24 ) = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year-25 *(SendBuffer + 25 ) = UTC8TimeTcp.month & 0xFF; //month-26 *(SendBuffer + 26 ) = UTC8TimeTcp.day & 0xFF; //day-27 *(SendBuffer + 27 ) = UTC8TimeTcp.hour & 0xFF; //hour-28 *(SendBuffer + 28 ) = UTC8TimeTcp.minute & 0xFF; //mins-29 *(SendBuffer + 29 ) = UTC8TimeTcp.second & 0xFF; //sec-30 *(SendBuffer + 30 ) = BATTMSG; //电池信息发送-31 *(SendBuffer + 31 ) = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year-32 *(SendBuffer + 32 ) = UTC8TimeTcp.month & 0xFF; //month-33 *(SendBuffer + 33 ) = UTC8TimeTcp.day & 0xFF; //day-34 *(SendBuffer + 34 ) = UTC8TimeTcp.hour & 0xFF; //hour-35 *(SendBuffer + 35 ) = UTC8TimeTcp.minute & 0xFF; //mins-36 *(SendBuffer + 36 ) = UTC8TimeTcp.second & 0xFF; //sec-37 *(SendBuffer + 37 ) = csq; //信号强度-38 *(SendBuffer + 38 ) = 0x00; //故障等级-39 if(osOK==osMutexAcquire(Error_Mutex, 100)) { ErrorTemp = GetErrorNum((UINT16 *)ErrorNum,sizeof(ErrorNum)); } else { ErrorTemp = 0x0000; } osMutexRelease(Error_Mutex); *(SendBuffer + 39 ) = ErrorTemp>>8; //故障代码H-40 *(SendBuffer + 40 ) = ErrorTemp & 0xFF; //故障代码L-41 *(SendBuffer + 41 ) = battI>>8; //电流-42 *(SendBuffer + 42 ) = battI & 0xFF; //电流-43 *(SendBuffer + 43 ) = battPackVol >> 8; //电压-44 *(SendBuffer + 44 ) = battPackVol & 0xFF; //电压-45 *(SendBuffer + 45 ) = battPackVol >> 8; //电压-46 *(SendBuffer + 46 ) = battPackVol & 0xFF; //电压-47 *(SendBuffer + 47 ) = battMOSSwitchState; //mos状态-48 *(SendBuffer + 48 ) = battSOC; //soc-49 *(SendBuffer + 49 ) = battSOH; //soh-50 *(SendBuffer + 50 ) = (battBalanceoInfo>>24)&0xFF; *(SendBuffer + 51 ) = (battBalanceoInfo>>16)&0xFF; *(SendBuffer + 52 ) = (battBalanceoInfo>>8)&0xFF; *(SendBuffer + 53 ) = battBalanceoInfo&0xFF; //均衡状态-51-54 *(SendBuffer + 54 ) = AppNVMData.BattCellCount; //电压个数-55 for(i=0;i>8) & 0xFF; *(SendBuffer + 54 + i*2 + 2) = battCellU[i] & 0xFF; } *(SendBuffer + 55 + AppNVMData.BattCellCount*2) = AppNVMData.BattTempCount; //模组温度个数 memcpy((SendBuffer + 55 + AppNVMData.BattCellCount*2 + 1),&battCellTemp,AppNVMData.BattTempCount); //模组温度 *(SendBuffer + 56 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount) = battWorkState; //电池工作状态 *(SendBuffer + 57 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount) = battHeatEnableState; //电池加热使能 *(SendBuffer + 58 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount) = BMS_OTHER_TEMP+NB_OTHER_TEMP_NUM; //其他温度个数 *(SendBuffer + 58 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + 1 ) = MOSTemp; //mos温度 *(SendBuffer + 58 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + 2 ) = packTemp; //环境温度 *(SendBuffer + 58 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + 3 ) = fastChargeTemp; //快充温度 *(SendBuffer + 58 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + 4) = normalChargeTemp; //慢充温度 *(SendBuffer + 58 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + 5) = heatTemp1; //加热温度1 *(SendBuffer + 58 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + 6) = heatTemp2; //加热温度2 *(SendBuffer + 59 + AppNVMData.BattCellCount*2 + AppNVMData.BattTempCount + BMS_OTHER_TEMP+NB_OTHER_TEMP_NUM) = bcc_chk(SendBuffer, ProtocolFixedLen + ProtocolFluctedLen - 1 );//校验码 if(UartRecvFlag) { len = tcpipConnectionSend(socContext.id, SendBuffer, ProtocolFixedLen + ProtocolFluctedLen, 0, 0, 0); #ifdef USING_PRINTF1 printf("Batt-[%d]:\n",len); #endif if(len>0) { TcpSendLen = 0x02 | TcpSendLen; } else { TcpSendLen = 0xFD & TcpSendLen; sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } } if(SendBuffer!=NULL) { free(SendBuffer); } SendBuffer=NULL; } */ GpsSendFreq = min(3 * 60, BattSendFreq); if (send_counter % GpsSendFreq == 0 && send_counter != 0) { osMessageQueueReset(GpsRecvHandle); GPSInfo *GpsRecvData = NULL; GPSMsgtoTcpType GpsToTcpInfo; UINT16 tac = 0; UINT32 cellId = 0; appGetLocationInfoSync(&tac, &cellId); appGetSystemTimeUtcSync(&TimeStracture); year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16; month = (TimeStracture.UTCtimer1 & 0xff00) >> 8; day = TimeStracture.UTCtimer1 & 0xff; hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24; minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16; sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec); DataLen = (UINT16)sizeof(GpsToTcpInfo.gpsInfo); GpsToTcpInfo.startSymbol[0] = TCP_START_SYM1; GpsToTcpInfo.startSymbol[1] = TCP_START_SYM2; GpsToTcpInfo.cmdSymbol = TCP_CMD_SYM; GpsToTcpInfo.ansSymbol = TCP_ANS_SYM; memcpy(GpsToTcpInfo.SN, AppNVMData.battSN, BATT_SN_LEN); GpsToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt GpsToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF; GpsToTcpInfo.dataLength[1] = DataLen & 0xFF; GpsToTcpInfo.gpsInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year GpsToTcpInfo.gpsInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month GpsToTcpInfo.gpsInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day GpsToTcpInfo.gpsInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour GpsToTcpInfo.gpsInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins GpsToTcpInfo.gpsInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec GpsToTcpInfo.gpsInfo.msgMark = GPSMSG; GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF; osStatus_t ret = osMessageQueueGet(GpsRecvHandle, &GpsRecvData, 0, 2000); if (ret == 0) { memcpy((UINT8 *)&GpsToTcpInfo.gpsInfo.locateMark, GpsRecvData, sizeof(GPSInfo)); GpsToTcpInfo.gpsInfo.Tac[0] = tac >> 8; GpsToTcpInfo.gpsInfo.Tac[1] = tac & 0xFF; GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 24; GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 16; GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 8; GpsToTcpInfo.gpsInfo.CellID[0] = cellId; GpsToTcpInfo.CRC = bcc_chk((UINT8 *)&GpsToTcpInfo, sizeof(GPSMsgtoTcpType) - 1); //if(GpsToTcpInfo.gpsInfo.locateMark==0x01) if (1) { len = tcpipConnectionSend(socContext.id, (UINT8 *)&GpsToTcpInfo, sizeof(GpsToTcpInfo), 0, 0, 0); #ifdef USING_PRINTF1 for (int i = 0; i < sizeof(GpsToTcpInfo); i++) { printf("%02x ", *((UINT8 *)&GpsToTcpInfo + i)); } printf("-[%d]-Gpslen:%d\n", __LINE__, len); #endif if (len > 0) { TcpSendLen = 0x04 | TcpSendLen; } else { TcpSendLen = 0xFB & TcpSendLen; sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } } } if (GpsRecvData != NULL) free(GpsRecvData); GpsRecvData = NULL; } if (send_counter == 1) //版本信息上报 上报频率为每次重启后上报或者每次更新后上报或者每天上报一次均可 { VersionMsgtoTcpType VerMsgToTcpInfo; CHAR iccid[20]; CHAR imei[15]; appGetIccidNumSync(iccid); appGetImeiNumSync(imei); // CHAR *verData = NULL; // verData = appGetNBVersionInfo(); // #ifdef USING_PRINTF // printf("verData:%s\n",verData); // #endif appGetSystemTimeUtcSync(&TimeStracture); year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16; month = (TimeStracture.UTCtimer1 & 0xff00) >> 8; day = TimeStracture.UTCtimer1 & 0xff; hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24; minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16; sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec); DataLen = (UINT16)sizeof(VerMsgToTcpInfo.VerInfo); VerMsgToTcpInfo.startSymbol[0] = TCP_START_SYM1; VerMsgToTcpInfo.startSymbol[1] = TCP_START_SYM2; VerMsgToTcpInfo.cmdSymbol = TCP_CMD_SYM; VerMsgToTcpInfo.ansSymbol = TCP_ANS_SYM; memcpy(VerMsgToTcpInfo.SN, AppNVMData.battSN, BATT_SN_LEN); VerMsgToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt VerMsgToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF; VerMsgToTcpInfo.dataLength[1] = DataLen & 0xFF; VerMsgToTcpInfo.VerInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year VerMsgToTcpInfo.VerInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month VerMsgToTcpInfo.VerInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day VerMsgToTcpInfo.VerInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour VerMsgToTcpInfo.VerInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins VerMsgToTcpInfo.VerInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec VerMsgToTcpInfo.VerInfo.msgMark = VERSIONMSG; VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF; memcpy(VerMsgToTcpInfo.VerInfo.ICCID, iccid, 20); memcpy(VerMsgToTcpInfo.VerInfo.IMEI, imei, 15); VerMsgToTcpInfo.VerInfo.BMSHwVersion[0] = AppNVMData.BmsHwVersion / 10; VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = AppNVMData.BmsHwVersion % 10; VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = 0; VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = 0; VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = AppNVMData.BmsSwVersion / 10; VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = AppNVMData.BmsSwVersion % 10; VerMsgToTcpInfo.VerInfo.NBHwVersion[0] = (HWVERSION >> 8) & 0xFF; VerMsgToTcpInfo.VerInfo.NBHwVersion[1] = (HWVERSION)&0xFF; VerMsgToTcpInfo.VerInfo.BLVersion[0] = (BLSWVERSION >> 24) & 0xFF; VerMsgToTcpInfo.VerInfo.BLVersion[1] = (BLSWVERSION >> 16) & 0xFF; VerMsgToTcpInfo.VerInfo.BLVersion[2] = (BLSWVERSION >> 8) & 0xFF; VerMsgToTcpInfo.VerInfo.BLVersion[3] = (BLSWVERSION)&0xFF; VerMsgToTcpInfo.VerInfo.DRVVersion[0] = (DRVSWVERSION >> 24) & 0xFF; VerMsgToTcpInfo.VerInfo.DRVVersion[1] = (DRVSWVERSION >> 16) & 0xFF; VerMsgToTcpInfo.VerInfo.DRVVersion[2] = (DRVSWVERSION >> 8) & 0xFF; VerMsgToTcpInfo.VerInfo.DRVVersion[3] = (DRVSWVERSION)&0xFF; VerMsgToTcpInfo.VerInfo.APPVersion[0] = (APPSWVERSION >> 24) & 0xFF; VerMsgToTcpInfo.VerInfo.APPVersion[1] = (APPSWVERSION >> 16) & 0xFF; VerMsgToTcpInfo.VerInfo.APPVersion[2] = (APPSWVERSION >> 8) & 0xFF; VerMsgToTcpInfo.VerInfo.APPVersion[3] = (APPSWVERSION)&0xFF; VerMsgToTcpInfo.VerInfo.BmsType = BMS_MANUFACTURE; VerMsgToTcpInfo.VerInfo.BmsInfo = BMS_INFO; VerMsgToTcpInfo.VerInfo.DataModuleType = DATA_MODULE_TYPE; VerMsgToTcpInfo.CRC = bcc_chk((UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo) - 1); if (NB_Fota_update_flag == FALSE) { len = tcpipConnectionSend(socContext.id, (UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo), 0, 0, 0); if (len > 0) { TcpSendLen = 0x01 | TcpSendLen; } else { TcpSendLen = 0xFE & TcpSendLen; sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } } } if ((send_counter) % (BattSendFreq * 3) == 0) //90发送 { appGetSystemTimeUtcSync(&TimeStracture); year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16; month = (TimeStracture.UTCtimer1 & 0xff00) >> 8; day = TimeStracture.UTCtimer1 & 0xff; hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24; minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16; sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec); UINT8 Tcptype = 0x90; UINT16 BufferLen = 0x56; SendBuffer = malloc(BufferLen); TcpDataAssemble(Tcptype, SendBuffer, UTC8TimeTcp); len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen, 0, 0, 0); #ifdef USING_PRINTF1 printf("[%d]DebugMsg-[%d]:\n", __LINE__, len); for (UINT16 i = 0; i < BufferLen; i++) { printf("%02x ", *(SendBuffer + i)); } printf("[%d]\n", __LINE__); #endif if (SendBuffer != NULL) { free(SendBuffer); } SendBuffer = NULL; if (len > 0) { ; } else { sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } } if ((send_counter - 1) % BattSendFreq == 0) //91发送 { appGetSystemTimeUtcSync(&TimeStracture); year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16; month = (TimeStracture.UTCtimer1 & 0xff00) >> 8; day = TimeStracture.UTCtimer1 & 0xff; hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24; minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16; sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec); UINT8 Tcptype = 0x91; UINT16 BufferLen = 0x54 + CELL_NUM * 2 + CELL_TEMP_NUM; SendBuffer = malloc(BufferLen); TcpDataAssemble(Tcptype, SendBuffer, UTC8TimeTcp); len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen, 0, 0, 0); #ifdef USING_PRINTF1 printf("[%d]0x91-[%d]:\n", __LINE__, len); for (UINT16 i = 0; i < BufferLen; i++) { printf("%02x ", *(SendBuffer + i)); } printf("[%d]\n", __LINE__); #endif if (SendBuffer != NULL) { free(SendBuffer); } SendBuffer = NULL; if (len > 0) { ; } else { sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } } if ((send_counter - 2) % (BattSendFreq * 4) == 0) //92发送 { appGetSystemTimeUtcSync(&TimeStracture); year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16; month = (TimeStracture.UTCtimer1 & 0xff00) >> 8; day = TimeStracture.UTCtimer1 & 0xff; hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24; minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16; sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec); UINT8 Tcptype = 0x92; UINT16 BufferLen = 0x4E; SendBuffer = malloc(BufferLen); TcpDataAssemble(Tcptype, SendBuffer, UTC8TimeTcp); len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen, 0, 0, 0); #ifdef USING_PRINTF1 printf("[%d]DebugMsg-[%d]:\n", __LINE__, len); for (UINT16 i = 0; i < BufferLen; i++) { printf("%02x ", *(SendBuffer + i)); } printf("[%d]\n", __LINE__); #endif if (SendBuffer != NULL) { free(SendBuffer); } SendBuffer = NULL; if (len > 0) { ; } else { sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } } /*调试信息字符发送形式,但是发送流量会大2倍 if((send_counter)%10==0)//90发送 { appGetSystemTimeUtcSync(&TimeStracture); year=(TimeStracture.UTCtimer1&0xffff0000)>>16; month=(TimeStracture.UTCtimer1&0xff00)>>8; day=TimeStracture.UTCtimer1&0xff; hour=(TimeStracture.UTCtimer2&0xff000000)>>24; minute=(TimeStracture.UTCtimer2&0xff0000)>>16; sec=(TimeStracture.UTCtimer2&0xff00)>>8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp,year,month,day,hour,minute,sec); DebugMsgtoTcpType DebugMsgInfo; UINT8 Tcptype =0x90; UINT16 BufferLen1 = 0x55,BufferLen = 0; UINT8 *rbuf = NULL; rbuf = malloc(BufferLen1); BufferLen = BufferLen1*2; SendBuffer = malloc(BufferLen+sizeof(DebugMsgInfo)); TcpDataAssemble(Tcptype,rbuf,UTC8TimeTcp); for(UINT16 i=0;i>8) & 0xFF; DebugMsgInfo.dataLength[1] = DataLen & 0xFF; DebugMsgInfo.DebugInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec DebugMsgInfo.DebugInfo.msgMark = DEBUGMSG; DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen>>8; DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen; memcpy(SendBuffer,(UINT8 *)&DebugMsgInfo,sizeof(DebugMsgInfo)-1); DebugMsgInfo.CRC = bcc_chk(SendBuffer, BufferLen+sizeof(DebugMsgInfo)-1); memcpy(SendBuffer+BufferLen+sizeof(DebugMsgInfo)-1,&DebugMsgInfo.CRC,1); len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen+sizeof(DebugMsgInfo), 0, 0, 0); #ifdef USING_PRINTF printf("[%d]DebugMsg-[%d]-[%d]:\n",__LINE__,BufferLen,len); #endif #ifdef USING_PRINTF1 printf("[%d]DebugMsg-[%d]:\n",__LINE__,len); for (UINT16 i = 0; i < BufferLen*2+sizeof(DebugMsgInfo); i++) { printf("%02x ",*(SendBuffer + i)); } #endif if(SendBuffer!=NULL) { free(SendBuffer); } SendBuffer=NULL; } if((send_counter+1)%10==0)//91发送 { appGetSystemTimeUtcSync(&TimeStracture); year=(TimeStracture.UTCtimer1&0xffff0000)>>16; month=(TimeStracture.UTCtimer1&0xff00)>>8; day=TimeStracture.UTCtimer1&0xff; hour=(TimeStracture.UTCtimer2&0xff000000)>>24; minute=(TimeStracture.UTCtimer2&0xff0000)>>16; sec=(TimeStracture.UTCtimer2&0xff00)>>8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp,year,month,day,hour,minute,sec); DebugMsgtoTcpType DebugMsgInfo; UINT8 Tcptype =0x91; UINT16 BufferLen1 = 0x53 + numOfCells*2 + numOfCellTemp,BufferLen = 0; UINT8 *rbuf = NULL; BufferLen = BufferLen1*2; rbuf = malloc(BufferLen1); SendBuffer = malloc(BufferLen+sizeof(DebugMsgInfo)); TcpDataAssemble(Tcptype,rbuf,UTC8TimeTcp); for(UINT16 i=0;i>8) & 0xFF; DebugMsgInfo.dataLength[1] = DataLen & 0xFF; DebugMsgInfo.DebugInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec DebugMsgInfo.DebugInfo.msgMark = DEBUGMSG; DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen>>8; DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen; memcpy(SendBuffer,(UINT8 *)&DebugMsgInfo,sizeof(DebugMsgInfo)-1); DebugMsgInfo.CRC = bcc_chk(SendBuffer, BufferLen+sizeof(DebugMsgInfo)-1); memcpy(SendBuffer+BufferLen+sizeof(DebugMsgInfo)-1,&DebugMsgInfo.CRC,1); len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen+sizeof(DebugMsgInfo), 0, 0, 0); #ifdef USING_PRINTF printf("[%d]DebugMsg-[%d]-[%d]:\n",__LINE__,BufferLen,len); #endif #ifdef USING_PRINTF1 printf("[%d]DebugMsg-[%d]:\n",__LINE__,len); for (UINT16 i = 0; i < BufferLen*2+sizeof(DebugMsgInfo); i++) { printf("%c ",*(SendBuffer + i)); } #endif if(SendBuffer!=NULL) { free(SendBuffer); } SendBuffer=NULL; } if((send_counter+2)%60==0)//92发送 { appGetSystemTimeUtcSync(&TimeStracture); year=(TimeStracture.UTCtimer1&0xffff0000)>>16; month=(TimeStracture.UTCtimer1&0xff00)>>8; day=TimeStracture.UTCtimer1&0xff; hour=(TimeStracture.UTCtimer2&0xff000000)>>24; minute=(TimeStracture.UTCtimer2&0xff0000)>>16; sec=(TimeStracture.UTCtimer2&0xff00)>>8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp,year,month,day,hour,minute,sec); DebugMsgtoTcpType DebugMsgInfo; UINT8 Tcptype =0x92; UINT16 BufferLen1 = 0x4D,BufferLen = 0; UINT8 *rbuf = NULL; rbuf = malloc(BufferLen1); BufferLen = BufferLen1*2; SendBuffer = malloc(BufferLen+sizeof(DebugMsgInfo)); TcpDataAssemble(Tcptype,rbuf,UTC8TimeTcp); for(UINT16 i=0;i>8) & 0xFF; DebugMsgInfo.dataLength[1] = DataLen & 0xFF; DebugMsgInfo.DebugInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec DebugMsgInfo.DebugInfo.msgMark = DEBUGMSG; DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen>>8; DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen; memcpy(SendBuffer,(UINT8 *)&DebugMsgInfo,sizeof(DebugMsgInfo)-1); DebugMsgInfo.CRC = bcc_chk(SendBuffer, BufferLen+sizeof(DebugMsgInfo)-1); memcpy(SendBuffer+BufferLen+sizeof(DebugMsgInfo)-1,&DebugMsgInfo.CRC,1); len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen+sizeof(DebugMsgInfo), 0, 0, 0); #ifdef USING_PRINTF1 printf("[%d]DebugMsg-[%d]:\n",__LINE__,len); for (UINT16 i = 0; i < BufferLen*2+sizeof(DebugMsgInfo); i++) { printf("%c ",*(SendBuffer + i)); } #endif if(SendBuffer!=NULL) { free(SendBuffer); } SendBuffer=NULL; } */ /* if(FALSE)//调试信息暂时不发,需调整之后发送 { DebugMsgtoTcpType DebugMsgInfo; UINT16 BufferLen = 0; slpManWakeSrc_e Wakeup_source; Wakeup_source = slpManGetWakeupSrc();//获取唤醒源 #ifdef DEBUGLOG Debug_printf("[%d]\n",Wakeup_source); #endif BufferLen = Debug_GetSize(); if(BufferLen==0) { send_counter++; return; } else if(BufferLen>200) { Debug_Del_Logfile(); #ifdef DEBUGLOG Debug_printf("oversize-Sys:%d\n",AppDataInfo.SysReStartCount); #endif BufferLen = Debug_GetSize(); } UINT8 rbuf[BufferLen]; Debug_Read_Logfile(rbuf,BufferLen); SendBuffer = malloc(BufferLen+sizeof(DebugMsgInfo)); memcpy(SendBuffer+sizeof(DebugMsgInfo)-1, rbuf,BufferLen); appGetSystemTimeUtcSync(&TimeStracture); year=(TimeStracture.UTCtimer1&0xffff0000)>>16; month=(TimeStracture.UTCtimer1&0xff00)>>8; day=TimeStracture.UTCtimer1&0xff; hour=(TimeStracture.UTCtimer2&0xff000000)>>24; minute=(TimeStracture.UTCtimer2&0xff0000)>>16; sec=(TimeStracture.UTCtimer2&0xff00)>>8; UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp,year,month,day,hour,minute,sec); DataLen=sizeof(DebugMsgInfo.DebugInfo) + BufferLen; DebugMsgInfo.startSymbol[0] = TCP_START_SYM1; DebugMsgInfo.startSymbol[1] = TCP_START_SYM2; DebugMsgInfo.cmdSymbol = TCP_CMD_SYM; DebugMsgInfo.ansSymbol = TCP_ANS_SYM; memcpy(DebugMsgInfo.SN, AppNVMData.battSN,BATT_SN_LEN); DebugMsgInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt DebugMsgInfo.dataLength[0] = (DataLen>>8) & 0xFF; DebugMsgInfo.dataLength[1] = DataLen & 0xFF; DebugMsgInfo.DebugInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec DebugMsgInfo.DebugInfo.msgMark = DEBUGMSG; DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen>>8; DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen; memcpy(SendBuffer,(UINT8 *)&DebugMsgInfo,sizeof(DebugMsgInfo)-1); DebugMsgInfo.CRC = bcc_chk(SendBuffer, BufferLen+sizeof(DebugMsgInfo)-1); memcpy(SendBuffer+BufferLen+sizeof(DebugMsgInfo)-1,&DebugMsgInfo.CRC,1); len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen+sizeof(DebugMsgInfo), 0, 0, 0); #ifdef USING_PRINTF1 printf("DebugMsg-[%d]:\n",len); // for (int i = 0; i < BufferLen+sizeof(DebugMsgInfo); i++) // { // printf("%02x ",*(SendBuffer + i)); // } #endif if(len>0) { Debug_Del_Logfile(); } if(SendBuffer!=NULL) { free(SendBuffer); } SendBuffer=NULL; } */ if (send_counter > 0xffff) { send_counter = 0; } else { send_counter++; } } static void TcpHeartDataSend() { UINT8 len = 0; HeartMsgtoTcpType HeartMsgToTcp; HeartMsgToTcp.startSymbol[0] = TCP_START_SYM1; HeartMsgToTcp.startSymbol[1] = TCP_START_SYM2; HeartMsgToTcp.cmdSymbol = TCP_HEART_SYM; HeartMsgToTcp.ansSymbol = TCP_ANS_SYM; memcpy(HeartMsgToTcp.SN, AppNVMData.battSN, BATT_SN_LEN); HeartMsgToTcp.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt HeartMsgToTcp.dataLength[0] = 0; HeartMsgToTcp.dataLength[1] = 0; HeartMsgToTcp.CRC = bcc_chk((UINT8 *)&HeartMsgToTcp, sizeof(HeartMsgToTcp) - 1); len = tcpipConnectionSend(socContext.id, (UINT8 *)&HeartMsgToTcp, sizeof(HeartMsgToTcp), 0, 0, 0); if (len < 0) { sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0); } #ifdef USING_PRINTF1 printf("HeartMsg-[%d]:\n", len); for (int i = 0; i < sizeof(HeartMsgToTcp); i++) printf("%02x ", *((UINT8 *)&HeartMsgToTcp + i)); #endif } static void TcpDataInfoRecvHandle() { TcpipConnectionRecvDataInd *TcpRecvData = NULL; osMessageQueueGet(TcpRecvHandle, &TcpRecvData, 0, 0); UINT8 Tcp_Cmd; UINT8 *Ptr; UINT8 TcpCmdAnswer[31]; TcpCmdAnswer[0] = TCP_START_SYM1; TcpCmdAnswer[1] = TCP_START_SYM1; if (TcpRecvData != NULL && TcpRecvData->length != 0) { Ptr = TcpRecvData->data; if ((*(Ptr + 0) == TCP_START_SYM1) && (*(Ptr + 1) == TCP_START_SYM2)) //服务器起始信息 { Tcp_Cmd = *(Ptr + 2); //命令标志 if (*(Ptr + 3) == 0xFE) { TCPWorkState = 0x01; } switch (Tcp_Cmd) { case TCP_QUERY_SYM: break; case TCP_SETCMD_SYM: break; case TCP_CONCMD_SYM: { TcpCmdAnswer[2] = TCP_CONCMD_SYM; if (*(Ptr + 30) == 0x01) //远程升级命令 { Fota_Func(Ptr, socContext.id); } else if (*(Ptr + 30) == 0x88) //BMS远程升级数据传输命令 { Fota_Func(Ptr, socContext.id); } else { TcpCmdAnswer[3] = 0x0f; memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN); TcpCmdAnswer[21] = TCP_ENCPT_DISABLE; TcpCmdAnswer[22] = 0x00; TcpCmdAnswer[23] = 0x06; memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6); TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30); tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0); } break; } default: { break; } } } if (TcpRecvData != NULL) { free(TcpRecvData); } TcpRecvData = NULL; } } //Tcp线程初始化 void AppTaskTcpInit(void *arg) { osThreadAttr_t task_attr; registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback); memset(&task_attr, 0, sizeof(task_attr)); memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE); task_attr.name = "Tcp_Task"; task_attr.stack_mem = gProcess_Tcp_TaskStack; task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE; task_attr.priority = osPriorityBelowNormal7; task_attr.cb_mem = &gProcess_Tcp_Task_t; task_attr.cb_size = sizeof(StaticTask_t); TcpTaskId = osThreadNew(TcpTask, NULL, &task_attr); } void AppTaskTcpDeInit(void *arg) { osThreadTerminate(TcpTaskId); TcpTaskId = NULL; } //Tcp 状态转换函数 static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait) { eventCallbackMessage_t *queueMsg = NULL; queueMsg = malloc(sizeof(eventCallbackMessage_t)); queueMsg->messageId = msgId; if (psEventQueueHandle) { if (pdTRUE != xQueueSend(psEventQueueHandle, &queueMsg, xTickstoWait)) { ECOMM_TRACE(UNILOG_PLA_APP, sendQueueMsg_1, P_ERROR, 0, "xQueueSend error"); } } } //Tcp状态注册函数 static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen) { CmiSimImsiStr *imsi = NULL; CmiPsCeregInd *cereg = NULL; NmAtiNetifInfo *netif = NULL; switch (eventID) { case NB_URC_ID_SIM_READY: { imsi = (CmiSimImsiStr *)param; memcpy(gImsi, imsi->contents, imsi->length); #ifdef USING_PRINTF printf("SIM ready(imsi=%s)\n", (UINT8 *)imsi->contents); #endif break; } case NB_URC_ID_MM_SIGQ: { //rssi = *(UINT8 *)param; #ifdef USING_PRINTF1 printf("RSSI signal=%d\n", rssi); #endif break; } case NB_URC_ID_PS_BEARER_ACTED: { #ifdef USING_PRINTF printf("Default bearer activated\n"); #endif break; } case NB_URC_ID_PS_BEARER_DEACTED: { #ifdef USING_PRINTF printf("Default bearer Deactivated\n"); #endif break; } case NB_URC_ID_PS_CEREG_CHANGED: { cereg = (CmiPsCeregInd *)param; gCellID = cereg->celId; #ifdef USING_PRINTF printf("URCCallBack:CEREG changed act:%d celId:%d locPresent:%d tac:%d\n", cereg->act, cereg->celId, cereg->locPresent, cereg->tac); #endif break; } case NB_URC_ID_PS_NETINFO: { netif = (NmAtiNetifInfo *)param; if (netif->netStatus == NM_NETIF_ACTIVATED) sendQueueMsg(QMSG_ID_NW_IP_READY, 0); break; } } return 0; } //Tcpz连接状态回调函数 static void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent) { switch (connectionEventType) { case TCPIP_CONNECTION_STATUS_EVENT: { TcpipConnectionStatusInd *statusInd; statusInd = (TcpipConnectionStatusInd *)bodyEvent; if (statusInd != PNULL) { if (statusInd->status == TCPIP_CONNECTION_STATUS_CLOSED) { #ifdef USING_PRINTF printf("socketAppConnectionCallBack socket connection %u closed,cause %u", statusInd->connectionId, statusInd->cause); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_1, P_ERROR, 2, "socketAppConnectionCallBack socket connection %u closed,cause %u", statusInd->connectionId, statusInd->cause); if (statusInd->connectionId == socContext.id) { socContext.id = -1; socContext.status = APP_SOCKET_CONNECTION_CLOSED; } } else if (statusInd->status == TCPIP_CONNECTION_STATUS_CONNECTING) { #ifdef USING_PRINTF printf("socketAppConnectionCallBack socket connection %u is connecting", statusInd->connectionId); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_2, P_INFO, 1, "socketAppConnectionCallBack socket connection %u is connecting", statusInd->connectionId); if (statusInd->connectionId == socContext.id) { socContext.status = APP_SOCKET_CONNECTION_CONNECTING; } } else if (statusInd->status == TCPIP_CONNECTION_STATUS_CONNECTED) { #ifdef USING_PRINTF printf("socketAppConnectionCallBack socket connection %u is connected", statusInd->connectionId); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_3, P_ERROR, 1, "socketAppConnectionCallBack socket connection %u is connected", statusInd->connectionId); if (statusInd->connectionId == socContext.id) { socContext.status = APP_SOCKET_CONNECTION_CONNECTED; } } } else { #ifdef USING_PRINTF printf("socketAppConnectionCallBack invalid connection status event"); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_4, P_ERROR, 0, "socketAppConnectionCallBack invalid connection status event"); } break; } case TCPIP_CONNECTION_RECEIVE_EVENT: { TcpipConnectionRecvDataInd *rcvInd = PNULL; TcpipConnectionRecvDataInd *rcvbuffer = NULL; rcvInd = (TcpipConnectionRecvDataInd *)bodyEvent; if (rcvInd != PNULL) { rcvbuffer = malloc(sizeof(TcpipConnectionRecvDataInd)); memcpy(rcvbuffer, rcvInd, sizeof(TcpipConnectionRecvDataInd)); osMessageQueuePut(TcpRecvHandle, &rcvbuffer, 0, 1000); sendQueueMsg(QMSG_ID_SOCK_RECVPKG, 0); #ifdef USING_PRINTF // uint8_t* Ptr; // Ptr=rcvInd->data; printf("socket connection %u receive length %u data:\n", rcvInd->connectionId, rcvInd->length); // for(int i = 0;ilength;i++) // printf("%x ",*(Ptr+i)); // printf("\n"); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_5, P_INFO, 2, "socketAppConnectionCallBack socket connection %u receive length %u data", rcvInd->connectionId, rcvInd->length); } else { #ifdef USING_PRINTF printf("socketAppConnectionCallBack invalid connection rcv event"); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_6, P_ERROR, 0, "socketAppConnectionCallBack invalid connection rcv event"); } break; } case TCPIP_CONNECTION_UL_STATUS_EVENT: { TcpipConnectionUlDataStatusInd *ulStatusInd; ulStatusInd = (TcpipConnectionUlDataStatusInd *)bodyEvent; if (ulStatusInd != PNULL) { if (ulStatusInd->status == Tcpip_Connection_UL_DATA_SUCCESS) { #ifdef USING_PRINTF printf("socketAppConnectionCallBack socket connection %u sequence %u data has sent success", ulStatusInd->connectionId, ulStatusInd->sequence); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_7, P_INFO, 2, "socketAppConnectionCallBack socket connection %u sequence %u data has sent success", ulStatusInd->connectionId, ulStatusInd->sequence); } else if (ulStatusInd->status == Tcpip_Connection_UL_DATA_FAIL) { #ifdef USING_PRINTF printf("socketAppConnectionCallBack socket connection %u sequence %u data has sent fail", ulStatusInd->connectionId, ulStatusInd->sequence); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_8, P_WARNING, 2, "socketAppConnectionCallBack socket connection %u sequence %u data has sent fail", ulStatusInd->connectionId, ulStatusInd->sequence); } } else { #ifdef USING_PRINTF printf("socketAppConnectionCallBack invalid connection ul status event"); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_9, P_ERROR, 0, "socketAppConnectionCallBack invalid connection ul status event"); } break; } default: #ifdef USING_PRINTF printf("socketAppConnectionCallBack invalid event type %u", connectionEventType); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_10, P_ERROR, 1, "socketAppConnectionCallBack invalid event type %u", connectionEventType); break; } }