1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132 |
- /****************************************************************************
- *
- * 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;
- BattSendFreq = 1;
- 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,%d,%d,%d,%d,%d,%d,%d,%d,,\
- %d,%d,%d,%d,%d,%d,%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,
- battI - 10000, maxCellVol, minCellVol,
- test_curr[0], test_curr[1], test_curr[2], test_curr[3], test_curr[4], test_curr[5], test_curr[6], test_curr[7], test_curr[8], test_curr[9],
- test_U1[0], test_U1[1], test_U1[2], test_U1[3], test_U1[4], test_U1[5], test_U1[6], test_U1[7], test_U1[8], test_U1[9],
- test_U2[0], test_U2[1], test_U2[2], test_U2[3], test_U2[4], test_U2[5], test_U2[6], test_U2[7], test_U2[8], test_U2[9]);
- }
- 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;
- }
- }
|