123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125 |
- /****************************************************************************
- *
- * 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 "BCUDisp.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
- slpManWakeSrc_e Wakeup_source;
- Wakeup_source = slpManGetWakeupSrc(); //获取唤醒源
- #ifdef DEBUGLOG
- Debug_printf("[%d,%d]\n", Wakeup_source, AppDataInfo.SysReStartCount);
- #endif
- 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;
- UINT8 csq = 0;
- INT8 snr = 0;
- INT8 rsnr = 0;
- UINT16 DataLen = 0;
- UINT8 BattSendFreq = 30;
- UINT8 GpsSendFreq = 30;
- UINT8 DebugFeq = 10;
- UINT16 year;
- UINT8 month, day, hour, minute, sec;
- UTC8TimeType UTC8TimeTcp;
- INT16 len = -1;
- UINT8 *SendBuffer = NULL;
- //电池工作模式的延迟转换由Maintask负责,发送频率调节由tcp负责
- switch (BattWorkStateDelay)
- {
- case BATT_IDLE_SYM:
- {
- BattSendFreq = AppDataInfo.BattInfoSendFreqLow;
- GpsSendFreq = AppDataInfo.PosInfoSendFreqLow;
- break;
- }
- case BATT_CHARGE_SYM:
- {
- BattSendFreq = AppDataInfo.BattInfoSendFreqHigh;
- GpsSendFreq = AppDataInfo.PosInfoSendFreqLow;
- break;
- }
- case BATT_DISCHARGE_SYM:
- {
- BattSendFreq = AppDataInfo.BattInfoSendFreqNomal;
- GpsSendFreq = AppDataInfo.PosInfoSendFreqHigh;
- break;
- }
- default:
- {
- BattSendFreq = AppDataInfo.BattInfoSendFreqNomal;
- GpsSendFreq = AppDataInfo.PosInfoSendFreqNormal;
- break;
- }
- }
- if (getbit(PadInterrupt, 3) == 1 || getbit(PadInterrupt, 4) == 1) //有震动产生,提高位置信息发送频率
- {
- GpsSendFreq = AppDataInfo.PosInfoSendFreqHigh;
- if (send_counter % 20 == 0)
- {
- clrbit(PadInterrupt, 3);
- clrbit(PadInterrupt, 4);
- }
- }
- DebugFeq = BattSendFreq;
- if (send_counter % BattSendFreq == 0 && send_counter != 0)
- {
- UINT16 ErrorTemp = 0x00;
- UINT8 ProtocolHeaderLen = 25; //电池信息协议头部加校验码长度,此长度不更改
- UINT8 ProtocolFixedLen = 60; //电池信息协议固定总长度 如协议新增,需要更改此长度
- UINT8 ProtocolFluctedLen = AppDataInfo.BattCellCount * 2 + AppDataInfo.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) = sfmd_st_fltLevel; //故障等级-39
- if (osOK == osMutexAcquire(Error_Mutex, 100))
- {
- ErrorTemp = GetErrorNum((UINT16 *)ErrorNum, ErrorNumLength);
- }
- 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) = (UINT8)(socd_pct_bcuSoc / 10); //soc-49
- *(SendBuffer + 49) = (UINT8)(sohd_pct_bcuSoh / 10); //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) = AppDataInfo.BattCellCount; //电压个数-55
- for (UINT8 i = 0; i < AppDataInfo.BattCellCount; i++) //单体电压
- {
- *(SendBuffer + 54 + i * 2 + 1) = (battCellU[i] >> 8) & 0xFF;
- *(SendBuffer + 54 + i * 2 + 2) = battCellU[i] & 0xFF;
- }
- *(SendBuffer + 55 + AppDataInfo.BattCellCount * 2) = AppDataInfo.BattTempCount; //模组温度个数
- memcpy((SendBuffer + 55 + AppDataInfo.BattCellCount * 2 + 1), &battCellTemp, AppDataInfo.BattTempCount); //模组温度
- *(SendBuffer + 56 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = battWorkState; //电池工作状态
- *(SendBuffer + 57 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = battHeatEnableState; //电池加热使能
- *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = BMS_OTHER_TEMP + NB_OTHER_TEMP_NUM; //其他温度个数
- *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 1) = MOSTemp; //mos温度
- *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 2) = packTemp; //环境温度
- *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 3) = fastChargeTemp; //快充温度
- *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 4) = normalChargeTemp; //慢充温度
- *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 5) = heatTemp1; //加热温度1
- *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 6) = heatTemp2; //加热温度2
- *(SendBuffer + 59 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + BMS_OTHER_TEMP + NB_OTHER_TEMP_NUM) = bcc_chk(SendBuffer, ProtocolFixedLen + ProtocolFluctedLen - 1); //校验码
- if (1)
- {
- 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;
- }
- if (send_counter % GpsSendFreq == 0 && send_counter != 0)
- {
- GPSInfo GpsRecvData;
- 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, (UINT8 *)&GpsRecvData, 0, 1000);
- if (ret == 0)
- {
- memcpy((UINT8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, (UINT8 *)&GpsRecvData, sizeof(GPSInfo));
- }
- else
- {
- memset((UINT8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, 0x00, 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;
- UINT16 xyzDatacahce[3] = {0};
- memcpy(xyzDatacahce, xyzData, 3);
- for (UINT8 i = 0; i < 3; i++)
- {
- if (xyzDatacahce[i] > 0x8000) // 数据为负
- {
- xyzDatacahce[i] = (UINT16)((UINT16)(~(xyzDatacahce[i])) + 1);
- xyzDatacahce[i] = 20000U - xyzDatacahce[i];
- }
- else
- {
- xyzDatacahce[i] = xyzDatacahce[i] + 20000U;
- }
- }
- GpsToTcpInfo.gpsInfo.xData[0] = xyzDatacahce[0] >> 8;
- GpsToTcpInfo.gpsInfo.xData[1] = xyzDatacahce[0];
- GpsToTcpInfo.gpsInfo.yData[0] = xyzDatacahce[1] >> 8;
- GpsToTcpInfo.gpsInfo.yData[1] = xyzDatacahce[1];
- GpsToTcpInfo.gpsInfo.zData[0] = xyzDatacahce[2] >> 8;
- GpsToTcpInfo.gpsInfo.zData[1] = xyzDatacahce[2];
- GpsToTcpInfo.CRC = bcc_chk((UINT8 *)&GpsToTcpInfo, sizeof(GPSMsgtoTcpType) - 1);
- 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 (send_counter == 1 || (BMSupdatestatus != 0xFF && UartRecvFlag == 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] = bmsHwVersion / 10;
- VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = bmsHwVersion % 10;
- VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = 0;
- VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = 0;
- VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = bmsSwVersion / 10;
- VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = 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 && BMS_Fota_update_flag == FALSE)
- {
- len = tcpipConnectionSend(socContext.id, (UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo), 0, 0, 0);
- if (len > 0)
- {
- TcpSendLen = 0x01 | TcpSendLen;
- BMSupdatestatus = 0xFF;
- }
- else
- {
- TcpSendLen = 0xFE & TcpSendLen;
- sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0);
- }
- }
- }
- if (send_counter % DebugFeq == 0)
- {
- DebugMsgtoTcpType DebugMsgInfo;
- UINT16 BufferLen = 0;
- UINT8 rbuf[1000];
- static UINT8 Debugcounter = 0;
- if (Debugcounter % 5 == 0)
- {
- sprintf((char *)rbuf, "A-%x,%d,%d,%x,%x,,%d,%d",
- battProtectState, AppDataInfo.RelayControl, chargerConnectState, BMSupdatestatus, PadInterrupt,
- AppDataInfo.ExpiryTimeArray[1], AppDataInfo.ExpiryTimeArray[2]);
- }
- else
- {
- INT16 temp = cdmv_ohm_deltR[0];
- for (UINT8 i = 0; i < cmnc_num_cellUNum; i++)
- {
- temp = max(temp, cdmv_ohm_deltR[i]);
- }
- sprintf((char *)rbuf, "B-%d,%d,%d,%d,%d,%d,%d,%d,%d,,%d,%d,,\
- %d,%d,%d",
- socd_pct_ahSoc, socd_pct_ekfSoc, socd_pct_estSoc, socd_pct_battSoc, socd_pct_bcuSoc, battSOC * 10, socd_pct_cellBattSoc, sohv_Q_packCapArrEo[9], temp, test_curr, test_U,
- battI - 10000, maxCellVol, minCellVol);
- }
- Debugcounter++;
- if (Debugcounter > 100)
- {
- Debugcounter = 0;
- }
- BufferLen = strlen(rbuf);
- 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 (SendBuffer != NULL)
- {
- free(SendBuffer);
- }
- SendBuffer = NULL;
- }
- if (send_counter == 0) //调试信息暂时不发,需调整之后发送
- {
- DebugMsgtoTcpType DebugMsgInfo;
- UINT16 BufferLen = 0;
- BufferLen = Debug_GetSize();
- if (BufferLen == 0)
- {
- send_counter++;
- return;
- }
- else if (BufferLen > 200U && BufferLen != 0xFFFFU)
- {
- Debug_Del_Logfile();
- #ifdef DEBUGLOG
- Debug_printf("oversize-Sys:%d,%d\n", AppDataInfo.SysReStartCount, BufferLen);
- #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 > 4000000000U)
- {
- 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 if (*(Ptr + 30) == 0x80) //远程锁定命令
- {
- TcpCmdAnswer[3] = 0x01;
- 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);
- if (*(Ptr + 31) == 0x01) //0x01代表锁定
- {
- AppDataInfo.appDataModify = TRUE;
- AppDataInfo.UserLock = TRUE;
- tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
- }
- else if (*(Ptr + 31) == 0x02) //0x02代表解锁
- {
- AppDataInfo.appDataModify = TRUE;
- AppDataInfo.UserLock = FALSE;
- tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
- }
- BattLockFunc();
- }
- else if (*(Ptr + 30) == 0x8A) //继电器控制
- {
- TcpCmdAnswer[3] = 0x01;
- 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);
- if (*(Ptr + 31) == 0x01) //0x01代表断开
- {
- AppDataInfo.appDataModify = TRUE;
- if (RelayForceControl == 0)
- {
- AppDataInfo.RelayControl = TRUE;
- }
- AppDataInfo.BattForceLock = TRUE;
- tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
- }
- else if (*(Ptr + 31) == 0x00) //0x00代表关闭
- {
- AppDataInfo.appDataModify = TRUE;
- if (RelayForceControl == 0)
- {
- AppDataInfo.RelayControl = FALSE;
- }
- AppDataInfo.BattForceLock = FALSE;
- tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
- }
- BattLockFunc();
- }
- else if (*(Ptr + 30) == 0x89) //蜂鸣器控制
- {
- TcpCmdAnswer[3] = 0x01;
- 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);
- if (*(Ptr + 31) == 0x01) //0x01代表断开
- {
- BuzzerControl = TRUE;
- tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
- }
- else if (*(Ptr + 31) == 0x00) //0x00代表关闭
- {
- BuzzerControl = FALSE;
- tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
- }
- }
- else if (*(Ptr + 30) == 0x85) //租期设置
- {
- UINT8 rentalStartArray[6] = {0}, ExpiryUTC8TimeArray[6] = {0};
- UINT16 rentaldays = 0;
- TcpCmdAnswer[3] = 0x01;
- 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);
- AppDataInfo.RentalType = *(Ptr + 31);
- MEMCPY(rentalStartArray, (Ptr + 32), 6);
- rentaldays = *(Ptr + 38) << 8 | *(Ptr + 39);
- ExpiryTimeCal(rentalStartArray, rentaldays, ExpiryUTC8TimeArray);
- MEMCPY(AppDataInfo.ExpiryTimeArray, ExpiryUTC8TimeArray, 6);
- AppDataInfo.appDataModify = TRUE;
- tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
- #ifdef USING_PRINTF
- printf("rental!\n");
- for (UINT8 i = 0; i < 6; i++)
- printf("%d ", ExpiryUTC8TimeArray[i]);
- #endif
- BattLockFunc();
- }
- 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;i<rcvInd->length;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;
- }
- }
|