AppTaskTcp.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /****************************************************************************
  2. *
  3. * Copy right: Qx.Chen jie
  4. * File name: TcpTask.c
  5. * Description: 主要负责:1.数据发送、定时心跳上报,2.数据接收、命令接收及处理,3.Fota函数调用,4.驻网及失败状态监测与上报
  6. * History: 2021-07-05
  7. * Version: V3.0
  8. ****************************************************************************/
  9. #include "AppTaskTcp.h"
  10. #include "BCUDisp.h"
  11. #include "numeric.h"
  12. //局部变量申请
  13. static StaticTask_t gProcess_Tcp_Task_t;
  14. static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
  15. static QueueHandle_t psEventQueueHandle; //状态转换队列句柄
  16. static osThreadId_t TcpTaskId = NULL;
  17. static eventCallbackMessage_t *queueItem = NULL;
  18. static UINT8 gImsi[16] = {0};
  19. static UINT32 gCellID = 0;
  20. static QueueHandle_t TcpRecvHandle = NULL;
  21. static UINT8 TcpRecvEnd = 0;
  22. static UINT32 send_counter = 0;
  23. static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait);
  24. static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen);
  25. static void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent);
  26. static void TcpDataInfoRecvHandle();
  27. static void TcpDataInfoAssembleSend();
  28. static void TcpHeartDataSend();
  29. static void TcpTask(void *arg)
  30. {
  31. appSetEDRXSettingSync(0, 5, 1800000);
  32. appSetPSMSettingSync(0, 3 * 60 * 60, 10);
  33. appSetCFUN(1);
  34. psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t *));
  35. if (psEventQueueHandle == NULL)
  36. {
  37. return;
  38. }
  39. if (TcpRecvHandle == NULL)
  40. {
  41. TcpRecvHandle = osMessageQueueNew(3, sizeof(TcpipConnectionRecvDataInd *), NULL);
  42. }
  43. osDelay(5000); //等待驻网延时5s
  44. slpManWakeSrc_e Wakeup_source;
  45. Wakeup_source = slpManGetWakeupSrc(); //获取唤醒源
  46. #ifdef DEBUGLOG
  47. Debug_printf("[%d,%d]\n", Wakeup_source, AppDataInfo.SysReStartCount);
  48. #endif
  49. while (true)
  50. {
  51. osDelay(100);
  52. if (gProcess_app == WORK && TCPWorkState == 0x00 && AppNVMData.EOLState == 1 && TimeCounter % 10 == 0)
  53. {
  54. sendQueueMsg(QMSG_ID_SOCK_SENDPKG, 0);
  55. }
  56. else if (gProcess_app == LISTEN && TimeCounter % (10 * 60 * 3) == 0)
  57. {
  58. sendQueueMsg(QMSG_ID_SOCK_LISTEN, 0);
  59. send_counter = 0;
  60. }
  61. if (TCPWorkState != 0 && TimeCounter % 10 == 0)
  62. {
  63. TcpRecvEnd++;
  64. if (TcpRecvEnd > 10)
  65. {
  66. TcpRecvEnd = 0;
  67. TCPWorkState = 0;
  68. }
  69. }
  70. if (xQueueReceive(psEventQueueHandle, &queueItem, 0))
  71. {
  72. switch (queueItem->messageId)
  73. {
  74. case QMSG_ID_NW_IP_READY:
  75. if (socContext.id < 0)
  76. {
  77. struct addrinfo hints;
  78. struct addrinfo *servinfo = NULL;
  79. char *Tcp_ip_des;
  80. memset(&hints, 0, sizeof(hints));
  81. hints.ai_family = AF_INET;
  82. hints.ai_socktype = SOCK_STREAM;
  83. getaddrinfo(TCP_ADD, NULL, &hints, &servinfo);
  84. Tcp_ip_des = ip4addr_ntoa((const ip4_addr_t *)(&(servinfo->ai_addr->sa_data[2])));
  85. #ifdef USING_PRINTF1
  86. printf("\nservinfo-%d:\n", servinfo->ai_addr->sa_len);
  87. for (int i = 0; i < 4; i++)
  88. printf("%d ", servinfo->ai_addr->sa_data[2 + i]);
  89. printf("\nTcp_ip_des-%d:\n", sizeof(Tcp_ip_des));
  90. for (int i = 0; i < 16; i++)
  91. printf("%c", *(Tcp_ip_des + i));
  92. #endif
  93. socContext.id = tcpipConnectionCreate(TCPIP_CONNECTION_PROTOCOL_TCP, PNULL, 0, Tcp_ip_des, TCP_PORT, socketAppConnectionCallBack);
  94. if (NULL != servinfo)
  95. {
  96. freeaddrinfo(servinfo);
  97. }
  98. }
  99. if (socContext.id >= 0)
  100. {
  101. TcpErrorcount = 0;
  102. socContext.status = APP_SOCKET_CONNECTION_CONNECTED;
  103. #ifdef USING_PRINTF
  104. printf("create connection %u success\n", socContext.id);
  105. #endif
  106. ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_2, P_INFO, 1, "create connection %u success", socContext.id);
  107. }
  108. else
  109. {
  110. sendQueueMsg(QMSG_ID_NW_IP_NOREACHABLE, 0);
  111. #ifdef USING_PRINTF
  112. printf("create connection fail\n");
  113. #endif
  114. ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_3, P_ERROR, 0, "create connection fail");
  115. }
  116. break;
  117. case QMSG_ID_NW_IP_NOREACHABLE:
  118. {
  119. TcpErrorcount++;
  120. socContext.id = -1;
  121. appSetCFUN(0);
  122. osDelay(5000);
  123. appSetCFUN(1);
  124. osDelay(10000);
  125. #ifdef USING_PRINTF
  126. printf("Network restart\n");
  127. #endif
  128. }
  129. break;
  130. case QMSG_ID_SOCK_SENDPKG:
  131. if (socContext.id >= 0 && socContext.status == APP_SOCKET_CONNECTION_CONNECTED)
  132. {
  133. NmAtiSyncRet netstatus1;
  134. appGetNetInfoSync(0, &netstatus1);
  135. if (netstatus1.body.netInfoRet.netifInfo.netStatus == NM_NETIF_ACTIVATED || netstatus1.body.netInfoRet.netifInfo.netStatus == NM_NETIF_ACTIVATED_INFO_CHNAGED)
  136. {
  137. TcpDataInfoAssembleSend();
  138. }
  139. else
  140. {
  141. sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0);
  142. }
  143. }
  144. else
  145. {
  146. sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
  147. #ifdef USING_PRINTF
  148. printf("connection %u or status invalid", socContext.id, socContext.status);
  149. #endif
  150. ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_9, P_ERROR, 2, "connection %u or status invalid", socContext.id, socContext.status);
  151. }
  152. break;
  153. case QMSG_ID_SOCK_RECVPKG:
  154. {
  155. TcpDataInfoRecvHandle();
  156. osDelay(100);
  157. TcpRecvEnd = 0;
  158. break;
  159. }
  160. case QMSG_ID_NW_IP_SUSPEND:
  161. if (socContext.id >= 0 && socContext.status != APP_SOCKET_CONNECTION_CLOSED)
  162. {
  163. if (tcpipConnectionClose(socContext.id) < 0)
  164. {
  165. #ifdef USING_PRINTF
  166. printf("close connection %u success", socContext.id);
  167. #endif
  168. socContext.id = -1;
  169. socContext.status = APP_SOCKET_CONNECTION_CLOSED;
  170. ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_4, P_INFO, 1, "close connection %u success", socContext.id);
  171. }
  172. else
  173. {
  174. #ifdef USING_PRINTF
  175. printf("close connection %u fail", socContext.id);
  176. #endif
  177. ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_5, P_ERROR, 1, "close connection %u fail", socContext.id);
  178. }
  179. }
  180. else
  181. {
  182. sendQueueMsg(QMSG_ID_NW_IP_NOREACHABLE, 0);
  183. #ifdef USING_PRINTF
  184. printf("connection %u or status invalid", socContext.id, socContext.status);
  185. #endif
  186. ECOMM_TRACE(UNILOG_PLA_APP, ecTestCaseTcpClient_6, P_ERROR, 2, "connection %u or status invalid", socContext.id, socContext.status);
  187. }
  188. break;
  189. case QMSG_ID_SOCK_LISTEN:
  190. {
  191. if (socContext.id >= 0 && socContext.status == APP_SOCKET_CONNECTION_CONNECTED)
  192. {
  193. #ifdef USING_PRINTF
  194. printf("[%d]QMSG_ID_SOCK_LISTEN\n", __LINE__);
  195. #endif
  196. TcpHeartDataSend();
  197. }
  198. else
  199. {
  200. sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
  201. #ifdef USING_PRINTF
  202. printf("connection %u or status invalid", socContext.id, socContext.status);
  203. #endif
  204. }
  205. break;
  206. }
  207. case QMSG_ID_SOCK_EXIT:
  208. #ifdef USING_PRINTF
  209. printf("QMSG_ID_SOCK_EXIT queueItem->messageId:%x\n", queueItem->messageId);
  210. #endif
  211. #ifdef USING_PRINTF
  212. printf("socket exit\n");
  213. #endif
  214. appSetCFUN(0);
  215. osDelay(1000);
  216. break;
  217. }
  218. free(queueItem);
  219. } //if
  220. } //while 循环
  221. }
  222. /***********************************************************************************************************************
  223. * 网络组包发送函数
  224. * 输入:空
  225. * 输出:空
  226. * 处理:将相关信息组包之后进行按一定频率发送,每次调用间隔为1s
  227. ***********************************************************************************************************************/
  228. static void TcpDataInfoAssembleSend()
  229. {
  230. OsaUtcTimeTValue TimeStracture;
  231. UINT8 csq = 0;
  232. INT8 snr = 0;
  233. INT8 rsnr = 0;
  234. UINT16 DataLen = 0;
  235. UINT8 BattSendFreq = 30;
  236. UINT8 GpsSendFreq = 30;
  237. UINT8 DebugFeq = 10;
  238. UINT16 year;
  239. UINT8 month, day, hour, minute, sec;
  240. UTC8TimeType UTC8TimeTcp;
  241. INT16 len = -1;
  242. UINT8 *SendBuffer = NULL;
  243. //电池工作模式的延迟转换由Maintask负责,发送频率调节由tcp负责
  244. switch (BattWorkStateDelay)
  245. {
  246. case BATT_IDLE_SYM:
  247. {
  248. BattSendFreq = AppDataInfo.BattInfoSendFreqLow;
  249. GpsSendFreq = AppDataInfo.PosInfoSendFreqLow;
  250. break;
  251. }
  252. case BATT_CHARGE_SYM:
  253. {
  254. BattSendFreq = AppDataInfo.BattInfoSendFreqHigh;
  255. GpsSendFreq = AppDataInfo.PosInfoSendFreqLow;
  256. break;
  257. }
  258. case BATT_DISCHARGE_SYM:
  259. {
  260. BattSendFreq = AppDataInfo.BattInfoSendFreqNomal;
  261. GpsSendFreq = AppDataInfo.PosInfoSendFreqHigh;
  262. break;
  263. }
  264. default:
  265. {
  266. BattSendFreq = AppDataInfo.BattInfoSendFreqNomal;
  267. GpsSendFreq = AppDataInfo.PosInfoSendFreqNormal;
  268. break;
  269. }
  270. }
  271. if (getbit(PadInterrupt, 3) == 1 || getbit(PadInterrupt, 4) == 1) //有震动产生,提高位置信息发送频率
  272. {
  273. GpsSendFreq = AppDataInfo.PosInfoSendFreqHigh;
  274. if (send_counter % 20 == 0)
  275. {
  276. clrbit(PadInterrupt, 3);
  277. clrbit(PadInterrupt, 4);
  278. }
  279. }
  280. DebugFeq = BattSendFreq;
  281. if (send_counter % BattSendFreq == 0 && send_counter != 0)
  282. {
  283. UINT16 ErrorTemp = 0x00;
  284. UINT8 ProtocolHeaderLen = 25; //电池信息协议头部加校验码长度,此长度不更改
  285. UINT8 ProtocolFixedLen = 60; //电池信息协议固定总长度 如协议新增,需要更改此长度
  286. UINT8 ProtocolFluctedLen = AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + BMS_OTHER_TEMP + NB_OTHER_TEMP_NUM; //电池信息协议变动长度
  287. SendBuffer = malloc(ProtocolFixedLen + ProtocolFluctedLen);
  288. if (SendBuffer == NULL)
  289. {
  290. return;
  291. }
  292. appGetSystemTimeUtcSync(&TimeStracture);
  293. year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16;
  294. month = (TimeStracture.UTCtimer1 & 0xff00) >> 8;
  295. day = TimeStracture.UTCtimer1 & 0xff;
  296. hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24;
  297. minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16;
  298. sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8;
  299. UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec);
  300. appGetSignalInfoSync(&csq, &snr, &rsnr);
  301. *(SendBuffer + 0) = TCP_START_SYM1; //起始码-1
  302. *(SendBuffer + 1) = TCP_START_SYM2; //起始码-2
  303. *(SendBuffer + 2) = TCP_CMD_SYM; //命令标识-3
  304. *(SendBuffer + 3) = TCP_ANS_SYM; //应答标识-4
  305. memcpy(SendBuffer + 4, AppNVMData.battSN, BATT_SN_LEN); //SN码 5-21
  306. *(SendBuffer + 21) = TCP_ENCPT_DISABLE; //加密方式-22
  307. DataLen = ProtocolFixedLen + ProtocolFluctedLen - ProtocolHeaderLen; //电池信息单元协议固定长度
  308. *(SendBuffer + 22) = (DataLen >> 8) & 0xFF; //数据长度H-23
  309. *(SendBuffer + 23) = DataLen & 0xFF; //数据长度L-24
  310. *(SendBuffer + 24) = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year-25
  311. *(SendBuffer + 25) = UTC8TimeTcp.month & 0xFF; //month-26
  312. *(SendBuffer + 26) = UTC8TimeTcp.day & 0xFF; //day-27
  313. *(SendBuffer + 27) = UTC8TimeTcp.hour & 0xFF; //hour-28
  314. *(SendBuffer + 28) = UTC8TimeTcp.minute & 0xFF; //mins-29
  315. *(SendBuffer + 29) = UTC8TimeTcp.second & 0xFF; //sec-30
  316. *(SendBuffer + 30) = BATTMSG; //电池信息发送-31
  317. *(SendBuffer + 31) = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year-32
  318. *(SendBuffer + 32) = UTC8TimeTcp.month & 0xFF; //month-33
  319. *(SendBuffer + 33) = UTC8TimeTcp.day & 0xFF; //day-34
  320. *(SendBuffer + 34) = UTC8TimeTcp.hour & 0xFF; //hour-35
  321. *(SendBuffer + 35) = UTC8TimeTcp.minute & 0xFF; //mins-36
  322. *(SendBuffer + 36) = UTC8TimeTcp.second & 0xFF; //sec-37
  323. *(SendBuffer + 37) = csq; //信号强度-38
  324. *(SendBuffer + 38) = sfmd_st_fltLevel; //故障等级-39
  325. if (osOK == osMutexAcquire(Error_Mutex, 100))
  326. {
  327. ErrorTemp = GetErrorNum((UINT16 *)ErrorNum, ErrorNumLength);
  328. }
  329. else
  330. {
  331. ErrorTemp = 0x0000;
  332. }
  333. osMutexRelease(Error_Mutex);
  334. *(SendBuffer + 39) = ErrorTemp >> 8; //故障代码H-40
  335. *(SendBuffer + 40) = ErrorTemp & 0xFF; //故障代码L-41
  336. *(SendBuffer + 41) = battI >> 8; //电流-42
  337. *(SendBuffer + 42) = battI & 0xFF; //电流-43
  338. *(SendBuffer + 43) = battPackVol >> 8; //电压-44
  339. *(SendBuffer + 44) = battPackVol & 0xFF; //电压-45
  340. *(SendBuffer + 45) = battPackVol >> 8; //电压-46
  341. *(SendBuffer + 46) = battPackVol & 0xFF; //电压-47
  342. *(SendBuffer + 47) = battMOSSwitchState; //mos状态-48
  343. *(SendBuffer + 48) = (UINT8)(socd_pct_bcuSoc / 10); //soc-49
  344. *(SendBuffer + 49) = (UINT8)(sohd_pct_bcuSoh / 10); //soh-50
  345. *(SendBuffer + 50) = (battBalanceoInfo >> 24) & 0xFF;
  346. *(SendBuffer + 51) = (battBalanceoInfo >> 16) & 0xFF;
  347. *(SendBuffer + 52) = (battBalanceoInfo >> 8) & 0xFF;
  348. *(SendBuffer + 53) = battBalanceoInfo & 0xFF; //均衡状态-51-54
  349. *(SendBuffer + 54) = AppDataInfo.BattCellCount; //电压个数-55
  350. for (UINT8 i = 0; i < AppDataInfo.BattCellCount; i++) //单体电压
  351. {
  352. *(SendBuffer + 54 + i * 2 + 1) = (battCellU[i] >> 8) & 0xFF;
  353. *(SendBuffer + 54 + i * 2 + 2) = battCellU[i] & 0xFF;
  354. }
  355. *(SendBuffer + 55 + AppDataInfo.BattCellCount * 2) = AppDataInfo.BattTempCount; //模组温度个数
  356. memcpy((SendBuffer + 55 + AppDataInfo.BattCellCount * 2 + 1), &battCellTemp, AppDataInfo.BattTempCount); //模组温度
  357. *(SendBuffer + 56 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = battWorkState; //电池工作状态
  358. *(SendBuffer + 57 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = battHeatEnableState; //电池加热使能
  359. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount) = BMS_OTHER_TEMP + NB_OTHER_TEMP_NUM; //其他温度个数
  360. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 1) = MOSTemp; //mos温度
  361. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 2) = packTemp; //环境温度
  362. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 3) = fastChargeTemp; //快充温度
  363. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 4) = normalChargeTemp; //慢充温度
  364. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 5) = heatTemp1; //加热温度1
  365. *(SendBuffer + 58 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + 6) = heatTemp2; //加热温度2
  366. *(SendBuffer + 59 + AppDataInfo.BattCellCount * 2 + AppDataInfo.BattTempCount + BMS_OTHER_TEMP + NB_OTHER_TEMP_NUM) = bcc_chk(SendBuffer, ProtocolFixedLen + ProtocolFluctedLen - 1); //校验码
  367. if (1)
  368. {
  369. len = tcpipConnectionSend(socContext.id, SendBuffer, ProtocolFixedLen + ProtocolFluctedLen, 0, 0, 0);
  370. #ifdef USING_PRINTF1
  371. printf("Batt-[%d]:\n", len);
  372. #endif
  373. if (len > 0)
  374. {
  375. TcpSendLen = 0x02 | TcpSendLen;
  376. }
  377. else
  378. {
  379. TcpSendLen = 0xFD & TcpSendLen;
  380. sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0);
  381. }
  382. }
  383. if (SendBuffer != NULL)
  384. {
  385. free(SendBuffer);
  386. }
  387. SendBuffer = NULL;
  388. }
  389. if (send_counter % GpsSendFreq == 0 && send_counter != 0)
  390. {
  391. GPSInfo GpsRecvData;
  392. GPSMsgtoTcpType GpsToTcpInfo;
  393. UINT16 tac = 0;
  394. UINT32 cellId = 0;
  395. appGetLocationInfoSync(&tac, &cellId);
  396. appGetSystemTimeUtcSync(&TimeStracture);
  397. year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16;
  398. month = (TimeStracture.UTCtimer1 & 0xff00) >> 8;
  399. day = TimeStracture.UTCtimer1 & 0xff;
  400. hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24;
  401. minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16;
  402. sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8;
  403. UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec);
  404. DataLen = (UINT16)sizeof(GpsToTcpInfo.gpsInfo);
  405. GpsToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  406. GpsToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  407. GpsToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  408. GpsToTcpInfo.ansSymbol = TCP_ANS_SYM;
  409. memcpy(GpsToTcpInfo.SN, AppNVMData.battSN, BATT_SN_LEN);
  410. GpsToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt
  411. GpsToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  412. GpsToTcpInfo.dataLength[1] = DataLen & 0xFF;
  413. GpsToTcpInfo.gpsInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year
  414. GpsToTcpInfo.gpsInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month
  415. GpsToTcpInfo.gpsInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day
  416. GpsToTcpInfo.gpsInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour
  417. GpsToTcpInfo.gpsInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins
  418. GpsToTcpInfo.gpsInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec
  419. GpsToTcpInfo.gpsInfo.msgMark = GPSMSG;
  420. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year
  421. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month
  422. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day
  423. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour
  424. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins
  425. GpsToTcpInfo.gpsInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  426. osStatus_t ret = osMessageQueueGet(GpsRecvHandle, (UINT8 *)&GpsRecvData, 0, 1000);
  427. if (ret == 0)
  428. {
  429. memcpy((UINT8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, (UINT8 *)&GpsRecvData, sizeof(GPSInfo));
  430. }
  431. else
  432. {
  433. memset((UINT8 *)&GpsToTcpInfo.gpsInfo.GpsInfoData, 0x00, sizeof(GPSInfo));
  434. }
  435. GpsToTcpInfo.gpsInfo.Tac[0] = tac >> 8;
  436. GpsToTcpInfo.gpsInfo.Tac[1] = tac & 0xFF;
  437. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 24;
  438. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 16;
  439. GpsToTcpInfo.gpsInfo.CellID[0] = cellId >> 8;
  440. GpsToTcpInfo.gpsInfo.CellID[0] = cellId;
  441. UINT16 xyzDatacahce[3] = {0};
  442. memcpy(xyzDatacahce, xyzData, 3);
  443. for (UINT8 i = 0; i < 3; i++)
  444. {
  445. if (xyzDatacahce[i] > 0x8000) // 数据为负
  446. {
  447. xyzDatacahce[i] = (UINT16)((UINT16)(~(xyzDatacahce[i])) + 1);
  448. xyzDatacahce[i] = 20000U - xyzDatacahce[i];
  449. }
  450. else
  451. {
  452. xyzDatacahce[i] = xyzDatacahce[i] + 20000U;
  453. }
  454. }
  455. GpsToTcpInfo.gpsInfo.xData[0] = xyzDatacahce[0] >> 8;
  456. GpsToTcpInfo.gpsInfo.xData[1] = xyzDatacahce[0];
  457. GpsToTcpInfo.gpsInfo.yData[0] = xyzDatacahce[1] >> 8;
  458. GpsToTcpInfo.gpsInfo.yData[1] = xyzDatacahce[1];
  459. GpsToTcpInfo.gpsInfo.zData[0] = xyzDatacahce[2] >> 8;
  460. GpsToTcpInfo.gpsInfo.zData[1] = xyzDatacahce[2];
  461. GpsToTcpInfo.CRC = bcc_chk((UINT8 *)&GpsToTcpInfo, sizeof(GPSMsgtoTcpType) - 1);
  462. if (1)
  463. {
  464. len = tcpipConnectionSend(socContext.id, (UINT8 *)&GpsToTcpInfo, sizeof(GpsToTcpInfo), 0, 0, 0);
  465. #ifdef USING_PRINTF1
  466. for (int i = 0; i < sizeof(GpsToTcpInfo); i++)
  467. {
  468. printf("%02x ", *((UINT8 *)&GpsToTcpInfo + i));
  469. }
  470. printf("-[%d]-Gpslen:%d\n", __LINE__, len);
  471. #endif
  472. if (len > 0)
  473. {
  474. TcpSendLen = 0x04 | TcpSendLen;
  475. }
  476. else
  477. {
  478. TcpSendLen = 0xFB & TcpSendLen;
  479. sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0);
  480. }
  481. }
  482. }
  483. if (send_counter == 1 || (BMSupdatestatus != 0xFF && UartRecvFlag == 1)) //版本信息上报 上报频率为每次重启后上报或者每次更新后上报或者每天上报一次均可
  484. {
  485. VersionMsgtoTcpType VerMsgToTcpInfo;
  486. CHAR iccid[20];
  487. CHAR imei[15];
  488. appGetIccidNumSync(iccid);
  489. appGetImeiNumSync(imei);
  490. // CHAR *verData = NULL;
  491. // verData = appGetNBVersionInfo();
  492. // #ifdef USING_PRINTF
  493. // printf("verData:%s\n",verData);
  494. // #endif
  495. appGetSystemTimeUtcSync(&TimeStracture);
  496. year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16;
  497. month = (TimeStracture.UTCtimer1 & 0xff00) >> 8;
  498. day = TimeStracture.UTCtimer1 & 0xff;
  499. hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24;
  500. minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16;
  501. sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8;
  502. UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec);
  503. DataLen = (UINT16)sizeof(VerMsgToTcpInfo.VerInfo);
  504. VerMsgToTcpInfo.startSymbol[0] = TCP_START_SYM1;
  505. VerMsgToTcpInfo.startSymbol[1] = TCP_START_SYM2;
  506. VerMsgToTcpInfo.cmdSymbol = TCP_CMD_SYM;
  507. VerMsgToTcpInfo.ansSymbol = TCP_ANS_SYM;
  508. memcpy(VerMsgToTcpInfo.SN, AppNVMData.battSN, BATT_SN_LEN);
  509. VerMsgToTcpInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt
  510. VerMsgToTcpInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  511. VerMsgToTcpInfo.dataLength[1] = DataLen & 0xFF;
  512. VerMsgToTcpInfo.VerInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year
  513. VerMsgToTcpInfo.VerInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month
  514. VerMsgToTcpInfo.VerInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day
  515. VerMsgToTcpInfo.VerInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour
  516. VerMsgToTcpInfo.VerInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins
  517. VerMsgToTcpInfo.VerInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec
  518. VerMsgToTcpInfo.VerInfo.msgMark = VERSIONMSG;
  519. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year
  520. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month
  521. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day
  522. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour
  523. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins
  524. VerMsgToTcpInfo.VerInfo.msgCollectionTimeUTC[5] = UTC8TimeTcp.second & 0xFF;
  525. memcpy(VerMsgToTcpInfo.VerInfo.ICCID, iccid, 20);
  526. memcpy(VerMsgToTcpInfo.VerInfo.IMEI, imei, 15);
  527. VerMsgToTcpInfo.VerInfo.BMSHwVersion[0] = bmsHwVersion / 10;
  528. VerMsgToTcpInfo.VerInfo.BMSHwVersion[1] = bmsHwVersion % 10;
  529. VerMsgToTcpInfo.VerInfo.BMSSwVersion[0] = 0;
  530. VerMsgToTcpInfo.VerInfo.BMSSwVersion[1] = 0;
  531. VerMsgToTcpInfo.VerInfo.BMSSwVersion[2] = bmsSwVersion / 10;
  532. VerMsgToTcpInfo.VerInfo.BMSSwVersion[3] = bmsSwVersion % 10;
  533. VerMsgToTcpInfo.VerInfo.NBHwVersion[0] = (HWVERSION >> 8) & 0xFF;
  534. VerMsgToTcpInfo.VerInfo.NBHwVersion[1] = (HWVERSION)&0xFF;
  535. VerMsgToTcpInfo.VerInfo.BLVersion[0] = (BLSWVERSION >> 24) & 0xFF;
  536. VerMsgToTcpInfo.VerInfo.BLVersion[1] = (BLSWVERSION >> 16) & 0xFF;
  537. VerMsgToTcpInfo.VerInfo.BLVersion[2] = (BLSWVERSION >> 8) & 0xFF;
  538. VerMsgToTcpInfo.VerInfo.BLVersion[3] = (BLSWVERSION)&0xFF;
  539. VerMsgToTcpInfo.VerInfo.DRVVersion[0] = (DRVSWVERSION >> 24) & 0xFF;
  540. VerMsgToTcpInfo.VerInfo.DRVVersion[1] = (DRVSWVERSION >> 16) & 0xFF;
  541. VerMsgToTcpInfo.VerInfo.DRVVersion[2] = (DRVSWVERSION >> 8) & 0xFF;
  542. VerMsgToTcpInfo.VerInfo.DRVVersion[3] = (DRVSWVERSION)&0xFF;
  543. VerMsgToTcpInfo.VerInfo.APPVersion[0] = (APPSWVERSION >> 24) & 0xFF;
  544. VerMsgToTcpInfo.VerInfo.APPVersion[1] = (APPSWVERSION >> 16) & 0xFF;
  545. VerMsgToTcpInfo.VerInfo.APPVersion[2] = (APPSWVERSION >> 8) & 0xFF;
  546. VerMsgToTcpInfo.VerInfo.APPVersion[3] = (APPSWVERSION)&0xFF;
  547. VerMsgToTcpInfo.VerInfo.BmsType = BMS_MANUFACTURE;
  548. VerMsgToTcpInfo.VerInfo.BmsInfo = BMS_INFO;
  549. VerMsgToTcpInfo.VerInfo.DataModuleType = DATA_MODULE_TYPE;
  550. VerMsgToTcpInfo.CRC = bcc_chk((UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo) - 1);
  551. if (NB_Fota_update_flag == FALSE && BMS_Fota_update_flag == FALSE)
  552. {
  553. len = tcpipConnectionSend(socContext.id, (UINT8 *)&VerMsgToTcpInfo, sizeof(VerMsgToTcpInfo), 0, 0, 0);
  554. if (len > 0)
  555. {
  556. TcpSendLen = 0x01 | TcpSendLen;
  557. BMSupdatestatus = 0xFF;
  558. }
  559. else
  560. {
  561. TcpSendLen = 0xFE & TcpSendLen;
  562. sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0);
  563. }
  564. }
  565. }
  566. if (send_counter % DebugFeq == 0)
  567. {
  568. DebugMsgtoTcpType DebugMsgInfo;
  569. UINT16 BufferLen = 0;
  570. UINT8 rbuf[1000];
  571. static UINT8 Debugcounter = 0;
  572. if (Debugcounter % 5 == 0)
  573. {
  574. sprintf((char *)rbuf, "A-%x,%d,%d,%x,%x,,%d,%d",
  575. battProtectState, AppDataInfo.RelayControl, chargerConnectState, BMSupdatestatus, PadInterrupt,
  576. AppDataInfo.ExpiryTimeArray[1], AppDataInfo.ExpiryTimeArray[2]);
  577. }
  578. else
  579. {
  580. INT16 temp = cdmv_ohm_deltR[0];
  581. for (UINT8 i = 0; i < cmnc_num_cellUNum; i++)
  582. {
  583. temp = max(temp, cdmv_ohm_deltR[i]);
  584. }
  585. sprintf((char *)rbuf, "B-%d,%d,%d,%d,%d,%d,%d,%d,%d,,%d,%d,,\
  586. %d,%d,%d",
  587. 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,
  588. battI - 10000, maxCellVol, minCellVol);
  589. }
  590. Debugcounter++;
  591. if (Debugcounter > 100)
  592. {
  593. Debugcounter = 0;
  594. }
  595. BufferLen = strlen(rbuf);
  596. SendBuffer = malloc(BufferLen + sizeof(DebugMsgInfo));
  597. memcpy(SendBuffer + sizeof(DebugMsgInfo) - 1, rbuf, BufferLen);
  598. appGetSystemTimeUtcSync(&TimeStracture);
  599. year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16;
  600. month = (TimeStracture.UTCtimer1 & 0xff00) >> 8;
  601. day = TimeStracture.UTCtimer1 & 0xff;
  602. hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24;
  603. minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16;
  604. sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8;
  605. UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec);
  606. DataLen = sizeof(DebugMsgInfo.DebugInfo) + BufferLen;
  607. DebugMsgInfo.startSymbol[0] = TCP_START_SYM1;
  608. DebugMsgInfo.startSymbol[1] = TCP_START_SYM2;
  609. DebugMsgInfo.cmdSymbol = TCP_CMD_SYM;
  610. DebugMsgInfo.ansSymbol = TCP_ANS_SYM;
  611. memcpy(DebugMsgInfo.SN, AppNVMData.battSN, BATT_SN_LEN);
  612. DebugMsgInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt
  613. DebugMsgInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  614. DebugMsgInfo.dataLength[1] = DataLen & 0xFF;
  615. DebugMsgInfo.DebugInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year
  616. DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month
  617. DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day
  618. DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour
  619. DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins
  620. DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec
  621. DebugMsgInfo.DebugInfo.msgMark = DEBUGMSG;
  622. DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen >> 8;
  623. DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen;
  624. memcpy(SendBuffer, (UINT8 *)&DebugMsgInfo, sizeof(DebugMsgInfo) - 1);
  625. DebugMsgInfo.CRC = bcc_chk(SendBuffer, BufferLen + sizeof(DebugMsgInfo) - 1);
  626. memcpy(SendBuffer + BufferLen + sizeof(DebugMsgInfo) - 1, &DebugMsgInfo.CRC, 1);
  627. len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen + sizeof(DebugMsgInfo), 0, 0, 0);
  628. #ifdef USING_PRINTF1
  629. printf("DebugMsg-[%d]:\n", len);
  630. // for (int i = 0; i < BufferLen+sizeof(DebugMsgInfo); i++)
  631. // {
  632. // printf("%02x ",*(SendBuffer + i));
  633. // }
  634. #endif
  635. if (SendBuffer != NULL)
  636. {
  637. free(SendBuffer);
  638. }
  639. SendBuffer = NULL;
  640. }
  641. if (send_counter == 0) //调试信息暂时不发,需调整之后发送
  642. {
  643. DebugMsgtoTcpType DebugMsgInfo;
  644. UINT16 BufferLen = 0;
  645. BufferLen = Debug_GetSize();
  646. if (BufferLen == 0)
  647. {
  648. send_counter++;
  649. return;
  650. }
  651. else if (BufferLen > 200U && BufferLen != 0xFFFFU)
  652. {
  653. Debug_Del_Logfile();
  654. #ifdef DEBUGLOG
  655. Debug_printf("oversize-Sys:%d,%d\n", AppDataInfo.SysReStartCount, BufferLen);
  656. #endif
  657. BufferLen = Debug_GetSize();
  658. }
  659. UINT8 rbuf[BufferLen];
  660. Debug_Read_Logfile(rbuf, BufferLen);
  661. SendBuffer = malloc(BufferLen + sizeof(DebugMsgInfo));
  662. memcpy(SendBuffer + sizeof(DebugMsgInfo) - 1, rbuf, BufferLen);
  663. appGetSystemTimeUtcSync(&TimeStracture);
  664. year = (TimeStracture.UTCtimer1 & 0xffff0000) >> 16;
  665. month = (TimeStracture.UTCtimer1 & 0xff00) >> 8;
  666. day = TimeStracture.UTCtimer1 & 0xff;
  667. hour = (TimeStracture.UTCtimer2 & 0xff000000) >> 24;
  668. minute = (TimeStracture.UTCtimer2 & 0xff0000) >> 16;
  669. sec = (TimeStracture.UTCtimer2 & 0xff00) >> 8;
  670. UTCToBeijing((UTC8TimeType *)&UTC8TimeTcp, year, month, day, hour, minute, sec);
  671. DataLen = sizeof(DebugMsgInfo.DebugInfo) + BufferLen;
  672. DebugMsgInfo.startSymbol[0] = TCP_START_SYM1;
  673. DebugMsgInfo.startSymbol[1] = TCP_START_SYM2;
  674. DebugMsgInfo.cmdSymbol = TCP_CMD_SYM;
  675. DebugMsgInfo.ansSymbol = TCP_ANS_SYM;
  676. memcpy(DebugMsgInfo.SN, AppNVMData.battSN, BATT_SN_LEN);
  677. DebugMsgInfo.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt
  678. DebugMsgInfo.dataLength[0] = (DataLen >> 8) & 0xFF;
  679. DebugMsgInfo.dataLength[1] = DataLen & 0xFF;
  680. DebugMsgInfo.DebugInfo.sendTimeUTC[0] = (UTC8TimeTcp.year - 0x07D0) & 0xFF; //year
  681. DebugMsgInfo.DebugInfo.sendTimeUTC[1] = UTC8TimeTcp.month & 0xFF; //month
  682. DebugMsgInfo.DebugInfo.sendTimeUTC[2] = UTC8TimeTcp.day & 0xFF; //day
  683. DebugMsgInfo.DebugInfo.sendTimeUTC[3] = UTC8TimeTcp.hour & 0xFF; //hour
  684. DebugMsgInfo.DebugInfo.sendTimeUTC[4] = UTC8TimeTcp.minute & 0xFF; //mins
  685. DebugMsgInfo.DebugInfo.sendTimeUTC[5] = UTC8TimeTcp.second & 0xFF; //sec
  686. DebugMsgInfo.DebugInfo.msgMark = DEBUGMSG;
  687. DebugMsgInfo.DebugInfo.DebugLen[0] = BufferLen >> 8;
  688. DebugMsgInfo.DebugInfo.DebugLen[1] = BufferLen;
  689. memcpy(SendBuffer, (UINT8 *)&DebugMsgInfo, sizeof(DebugMsgInfo) - 1);
  690. DebugMsgInfo.CRC = bcc_chk(SendBuffer, BufferLen + sizeof(DebugMsgInfo) - 1);
  691. memcpy(SendBuffer + BufferLen + sizeof(DebugMsgInfo) - 1, &DebugMsgInfo.CRC, 1);
  692. len = tcpipConnectionSend(socContext.id, SendBuffer, BufferLen + sizeof(DebugMsgInfo), 0, 0, 0);
  693. #ifdef USING_PRINTF1
  694. printf("DebugMsg-[%d]:\n", len);
  695. // for (int i = 0; i < BufferLen+sizeof(DebugMsgInfo); i++)
  696. // {
  697. // printf("%02x ",*(SendBuffer + i));
  698. // }
  699. #endif
  700. if (len > 0)
  701. {
  702. Debug_Del_Logfile();
  703. }
  704. if (SendBuffer != NULL)
  705. {
  706. free(SendBuffer);
  707. }
  708. SendBuffer = NULL;
  709. }
  710. if (send_counter > 4000000000U)
  711. {
  712. send_counter = 0;
  713. }
  714. else
  715. {
  716. send_counter++;
  717. }
  718. }
  719. static void TcpHeartDataSend()
  720. {
  721. UINT8 len = 0;
  722. HeartMsgtoTcpType HeartMsgToTcp;
  723. HeartMsgToTcp.startSymbol[0] = TCP_START_SYM1;
  724. HeartMsgToTcp.startSymbol[1] = TCP_START_SYM2;
  725. HeartMsgToTcp.cmdSymbol = TCP_HEART_SYM;
  726. HeartMsgToTcp.ansSymbol = TCP_ANS_SYM;
  727. memcpy(HeartMsgToTcp.SN, AppNVMData.battSN, BATT_SN_LEN);
  728. HeartMsgToTcp.encryptMethod = TCP_ENCPT_DISABLE; //not encrypt
  729. HeartMsgToTcp.dataLength[0] = 0;
  730. HeartMsgToTcp.dataLength[1] = 0;
  731. HeartMsgToTcp.CRC = bcc_chk((UINT8 *)&HeartMsgToTcp, sizeof(HeartMsgToTcp) - 1);
  732. len = tcpipConnectionSend(socContext.id, (UINT8 *)&HeartMsgToTcp, sizeof(HeartMsgToTcp), 0, 0, 0);
  733. if (len < 0)
  734. {
  735. sendQueueMsg(QMSG_ID_NW_IP_SUSPEND, 0);
  736. }
  737. #ifdef USING_PRINTF1
  738. printf("HeartMsg-[%d]:\n", len);
  739. for (int i = 0; i < sizeof(HeartMsgToTcp); i++)
  740. printf("%02x ", *((UINT8 *)&HeartMsgToTcp + i));
  741. #endif
  742. }
  743. static void TcpDataInfoRecvHandle()
  744. {
  745. TcpipConnectionRecvDataInd *TcpRecvData = NULL;
  746. osMessageQueueGet(TcpRecvHandle, &TcpRecvData, 0, 0);
  747. UINT8 Tcp_Cmd;
  748. UINT8 *Ptr;
  749. UINT8 TcpCmdAnswer[31];
  750. TcpCmdAnswer[0] = TCP_START_SYM1;
  751. TcpCmdAnswer[1] = TCP_START_SYM1;
  752. if (TcpRecvData != NULL && TcpRecvData->length != 0)
  753. {
  754. Ptr = TcpRecvData->data;
  755. if ((*(Ptr + 0) == TCP_START_SYM1) && (*(Ptr + 1) == TCP_START_SYM2)) //服务器起始信息
  756. {
  757. Tcp_Cmd = *(Ptr + 2); //命令标志
  758. if (*(Ptr + 3) == 0xFE)
  759. {
  760. TCPWorkState = 0x01;
  761. }
  762. switch (Tcp_Cmd)
  763. {
  764. case TCP_QUERY_SYM:
  765. break;
  766. case TCP_SETCMD_SYM:
  767. break;
  768. case TCP_CONCMD_SYM:
  769. {
  770. TcpCmdAnswer[2] = TCP_CONCMD_SYM;
  771. if (*(Ptr + 30) == 0x01) //远程升级命令
  772. {
  773. Fota_Func(Ptr, socContext.id);
  774. }
  775. else if (*(Ptr + 30) == 0x88) //BMS远程升级数据传输命令
  776. {
  777. Fota_Func(Ptr, socContext.id);
  778. }
  779. else if (*(Ptr + 30) == 0x80) //远程锁定命令
  780. {
  781. TcpCmdAnswer[3] = 0x01;
  782. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  783. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  784. TcpCmdAnswer[22] = 0x00;
  785. TcpCmdAnswer[23] = 0x06;
  786. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  787. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  788. if (*(Ptr + 31) == 0x01) //0x01代表锁定
  789. {
  790. AppDataInfo.appDataModify = TRUE;
  791. AppDataInfo.UserLock = TRUE;
  792. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  793. }
  794. else if (*(Ptr + 31) == 0x02) //0x02代表解锁
  795. {
  796. AppDataInfo.appDataModify = TRUE;
  797. AppDataInfo.UserLock = FALSE;
  798. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  799. }
  800. BattLockFunc();
  801. }
  802. else if (*(Ptr + 30) == 0x8A) //继电器控制
  803. {
  804. TcpCmdAnswer[3] = 0x01;
  805. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  806. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  807. TcpCmdAnswer[22] = 0x00;
  808. TcpCmdAnswer[23] = 0x06;
  809. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  810. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  811. if (*(Ptr + 31) == 0x01) //0x01代表断开
  812. {
  813. AppDataInfo.appDataModify = TRUE;
  814. if (RelayForceControl == 0)
  815. {
  816. AppDataInfo.RelayControl = TRUE;
  817. }
  818. AppDataInfo.BattForceLock = TRUE;
  819. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  820. }
  821. else if (*(Ptr + 31) == 0x00) //0x00代表关闭
  822. {
  823. AppDataInfo.appDataModify = TRUE;
  824. if (RelayForceControl == 0)
  825. {
  826. AppDataInfo.RelayControl = FALSE;
  827. }
  828. AppDataInfo.BattForceLock = FALSE;
  829. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  830. }
  831. BattLockFunc();
  832. }
  833. else if (*(Ptr + 30) == 0x89) //蜂鸣器控制
  834. {
  835. TcpCmdAnswer[3] = 0x01;
  836. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  837. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  838. TcpCmdAnswer[22] = 0x00;
  839. TcpCmdAnswer[23] = 0x06;
  840. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  841. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  842. if (*(Ptr + 31) == 0x01) //0x01代表断开
  843. {
  844. BuzzerControl = TRUE;
  845. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  846. }
  847. else if (*(Ptr + 31) == 0x00) //0x00代表关闭
  848. {
  849. BuzzerControl = FALSE;
  850. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  851. }
  852. }
  853. else if (*(Ptr + 30) == 0x85) //租期设置
  854. {
  855. UINT8 rentalStartArray[6] = {0}, ExpiryUTC8TimeArray[6] = {0};
  856. UINT16 rentaldays = 0;
  857. TcpCmdAnswer[3] = 0x01;
  858. MEMCPY(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  859. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  860. TcpCmdAnswer[22] = 0x00;
  861. TcpCmdAnswer[23] = 0x06;
  862. MEMCPY(&TcpCmdAnswer[24], (Ptr + 24), 6);
  863. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  864. AppDataInfo.RentalType = *(Ptr + 31);
  865. MEMCPY(rentalStartArray, (Ptr + 32), 6);
  866. rentaldays = *(Ptr + 38) << 8 | *(Ptr + 39);
  867. ExpiryTimeCal(rentalStartArray, rentaldays, ExpiryUTC8TimeArray);
  868. MEMCPY(AppDataInfo.ExpiryTimeArray, ExpiryUTC8TimeArray, 6);
  869. AppDataInfo.appDataModify = TRUE;
  870. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  871. #ifdef USING_PRINTF
  872. printf("rental!\n");
  873. for (UINT8 i = 0; i < 6; i++)
  874. printf("%d ", ExpiryUTC8TimeArray[i]);
  875. #endif
  876. BattLockFunc();
  877. }
  878. else
  879. {
  880. TcpCmdAnswer[3] = 0x0f;
  881. memcpy(&TcpCmdAnswer[4], (Ptr + 4), BATT_SN_LEN);
  882. TcpCmdAnswer[21] = TCP_ENCPT_DISABLE;
  883. TcpCmdAnswer[22] = 0x00;
  884. TcpCmdAnswer[23] = 0x06;
  885. memcpy(&TcpCmdAnswer[24], (Ptr + 24), 6);
  886. TcpCmdAnswer[30] = bcc_chk(TcpCmdAnswer, 30);
  887. tcpipConnectionSend(socContext.id, TcpCmdAnswer, 31, 0, 0, 0);
  888. }
  889. break;
  890. }
  891. default:
  892. {
  893. break;
  894. }
  895. }
  896. }
  897. }
  898. if (TcpRecvData != NULL)
  899. {
  900. free(TcpRecvData);
  901. }
  902. TcpRecvData = NULL;
  903. }
  904. //Tcp线程初始化
  905. void AppTaskTcpInit(void *arg)
  906. {
  907. osThreadAttr_t task_attr;
  908. registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback);
  909. memset(&task_attr, 0, sizeof(task_attr));
  910. memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE);
  911. task_attr.name = "Tcp_Task";
  912. task_attr.stack_mem = gProcess_Tcp_TaskStack;
  913. task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE;
  914. task_attr.priority = osPriorityBelowNormal7;
  915. task_attr.cb_mem = &gProcess_Tcp_Task_t;
  916. task_attr.cb_size = sizeof(StaticTask_t);
  917. TcpTaskId = osThreadNew(TcpTask, NULL, &task_attr);
  918. }
  919. void AppTaskTcpDeInit(void *arg)
  920. {
  921. osThreadTerminate(TcpTaskId);
  922. TcpTaskId = NULL;
  923. }
  924. //Tcp 状态转换函数
  925. static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait)
  926. {
  927. eventCallbackMessage_t *queueMsg = NULL;
  928. queueMsg = malloc(sizeof(eventCallbackMessage_t));
  929. queueMsg->messageId = msgId;
  930. if (psEventQueueHandle)
  931. {
  932. if (pdTRUE != xQueueSend(psEventQueueHandle, &queueMsg, xTickstoWait))
  933. {
  934. ECOMM_TRACE(UNILOG_PLA_APP, sendQueueMsg_1, P_ERROR, 0, "xQueueSend error");
  935. }
  936. }
  937. }
  938. //Tcp状态注册函数
  939. static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen)
  940. {
  941. CmiSimImsiStr *imsi = NULL;
  942. CmiPsCeregInd *cereg = NULL;
  943. NmAtiNetifInfo *netif = NULL;
  944. switch (eventID)
  945. {
  946. case NB_URC_ID_SIM_READY:
  947. {
  948. imsi = (CmiSimImsiStr *)param;
  949. memcpy(gImsi, imsi->contents, imsi->length);
  950. #ifdef USING_PRINTF
  951. printf("SIM ready(imsi=%s)\n", (UINT8 *)imsi->contents);
  952. #endif
  953. break;
  954. }
  955. case NB_URC_ID_MM_SIGQ:
  956. {
  957. //rssi = *(UINT8 *)param;
  958. #ifdef USING_PRINTF1
  959. printf("RSSI signal=%d\n", rssi);
  960. #endif
  961. break;
  962. }
  963. case NB_URC_ID_PS_BEARER_ACTED:
  964. {
  965. #ifdef USING_PRINTF
  966. printf("Default bearer activated\n");
  967. #endif
  968. break;
  969. }
  970. case NB_URC_ID_PS_BEARER_DEACTED:
  971. {
  972. #ifdef USING_PRINTF
  973. printf("Default bearer Deactivated\n");
  974. #endif
  975. break;
  976. }
  977. case NB_URC_ID_PS_CEREG_CHANGED:
  978. {
  979. cereg = (CmiPsCeregInd *)param;
  980. gCellID = cereg->celId;
  981. #ifdef USING_PRINTF
  982. printf("URCCallBack:CEREG changed act:%d celId:%d locPresent:%d tac:%d\n", cereg->act, cereg->celId, cereg->locPresent, cereg->tac);
  983. #endif
  984. break;
  985. }
  986. case NB_URC_ID_PS_NETINFO:
  987. {
  988. netif = (NmAtiNetifInfo *)param;
  989. if (netif->netStatus == NM_NETIF_ACTIVATED)
  990. sendQueueMsg(QMSG_ID_NW_IP_READY, 0);
  991. break;
  992. }
  993. }
  994. return 0;
  995. }
  996. //Tcpz连接状态回调函数
  997. static void socketAppConnectionCallBack(UINT8 connectionEventType, void *bodyEvent)
  998. {
  999. switch (connectionEventType)
  1000. {
  1001. case TCPIP_CONNECTION_STATUS_EVENT:
  1002. {
  1003. TcpipConnectionStatusInd *statusInd;
  1004. statusInd = (TcpipConnectionStatusInd *)bodyEvent;
  1005. if (statusInd != PNULL)
  1006. {
  1007. if (statusInd->status == TCPIP_CONNECTION_STATUS_CLOSED)
  1008. {
  1009. #ifdef USING_PRINTF
  1010. printf("socketAppConnectionCallBack socket connection %u closed,cause %u", statusInd->connectionId, statusInd->cause);
  1011. #endif
  1012. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_1, P_ERROR, 2, "socketAppConnectionCallBack socket connection %u closed,cause %u", statusInd->connectionId, statusInd->cause);
  1013. if (statusInd->connectionId == socContext.id)
  1014. {
  1015. socContext.id = -1;
  1016. socContext.status = APP_SOCKET_CONNECTION_CLOSED;
  1017. }
  1018. }
  1019. else if (statusInd->status == TCPIP_CONNECTION_STATUS_CONNECTING)
  1020. {
  1021. #ifdef USING_PRINTF
  1022. printf("socketAppConnectionCallBack socket connection %u is connecting", statusInd->connectionId);
  1023. #endif
  1024. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_2, P_INFO, 1, "socketAppConnectionCallBack socket connection %u is connecting", statusInd->connectionId);
  1025. if (statusInd->connectionId == socContext.id)
  1026. {
  1027. socContext.status = APP_SOCKET_CONNECTION_CONNECTING;
  1028. }
  1029. }
  1030. else if (statusInd->status == TCPIP_CONNECTION_STATUS_CONNECTED)
  1031. {
  1032. #ifdef USING_PRINTF
  1033. printf("socketAppConnectionCallBack socket connection %u is connected", statusInd->connectionId);
  1034. #endif
  1035. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_3, P_ERROR, 1, "socketAppConnectionCallBack socket connection %u is connected", statusInd->connectionId);
  1036. if (statusInd->connectionId == socContext.id)
  1037. {
  1038. socContext.status = APP_SOCKET_CONNECTION_CONNECTED;
  1039. }
  1040. }
  1041. }
  1042. else
  1043. {
  1044. #ifdef USING_PRINTF
  1045. printf("socketAppConnectionCallBack invalid connection status event");
  1046. #endif
  1047. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_4, P_ERROR, 0, "socketAppConnectionCallBack invalid connection status event");
  1048. }
  1049. break;
  1050. }
  1051. case TCPIP_CONNECTION_RECEIVE_EVENT:
  1052. {
  1053. TcpipConnectionRecvDataInd *rcvInd = PNULL;
  1054. TcpipConnectionRecvDataInd *rcvbuffer = NULL;
  1055. rcvInd = (TcpipConnectionRecvDataInd *)bodyEvent;
  1056. if (rcvInd != PNULL)
  1057. {
  1058. rcvbuffer = malloc(sizeof(TcpipConnectionRecvDataInd));
  1059. memcpy(rcvbuffer, rcvInd, sizeof(TcpipConnectionRecvDataInd));
  1060. osMessageQueuePut(TcpRecvHandle, &rcvbuffer, 0, 1000);
  1061. sendQueueMsg(QMSG_ID_SOCK_RECVPKG, 0);
  1062. #ifdef USING_PRINTF
  1063. // uint8_t* Ptr;
  1064. // Ptr=rcvInd->data;
  1065. printf("socket connection %u receive length %u data:\n", rcvInd->connectionId, rcvInd->length);
  1066. // for(int i = 0;i<rcvInd->length;i++)
  1067. // printf("%x ",*(Ptr+i));
  1068. // printf("\n");
  1069. #endif
  1070. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_5, P_INFO, 2, "socketAppConnectionCallBack socket connection %u receive length %u data", rcvInd->connectionId, rcvInd->length);
  1071. }
  1072. else
  1073. {
  1074. #ifdef USING_PRINTF
  1075. printf("socketAppConnectionCallBack invalid connection rcv event");
  1076. #endif
  1077. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_6, P_ERROR, 0, "socketAppConnectionCallBack invalid connection rcv event");
  1078. }
  1079. break;
  1080. }
  1081. case TCPIP_CONNECTION_UL_STATUS_EVENT:
  1082. {
  1083. TcpipConnectionUlDataStatusInd *ulStatusInd;
  1084. ulStatusInd = (TcpipConnectionUlDataStatusInd *)bodyEvent;
  1085. if (ulStatusInd != PNULL)
  1086. {
  1087. if (ulStatusInd->status == Tcpip_Connection_UL_DATA_SUCCESS)
  1088. {
  1089. #ifdef USING_PRINTF
  1090. printf("socketAppConnectionCallBack socket connection %u sequence %u data has sent success", ulStatusInd->connectionId, ulStatusInd->sequence);
  1091. #endif
  1092. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_7, P_INFO, 2, "socketAppConnectionCallBack socket connection %u sequence %u data has sent success", ulStatusInd->connectionId, ulStatusInd->sequence);
  1093. }
  1094. else if (ulStatusInd->status == Tcpip_Connection_UL_DATA_FAIL)
  1095. {
  1096. #ifdef USING_PRINTF
  1097. printf("socketAppConnectionCallBack socket connection %u sequence %u data has sent fail", ulStatusInd->connectionId, ulStatusInd->sequence);
  1098. #endif
  1099. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_8, P_WARNING, 2, "socketAppConnectionCallBack socket connection %u sequence %u data has sent fail", ulStatusInd->connectionId, ulStatusInd->sequence);
  1100. }
  1101. }
  1102. else
  1103. {
  1104. #ifdef USING_PRINTF
  1105. printf("socketAppConnectionCallBack invalid connection ul status event");
  1106. #endif
  1107. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_9, P_ERROR, 0, "socketAppConnectionCallBack invalid connection ul status event");
  1108. }
  1109. break;
  1110. }
  1111. default:
  1112. #ifdef USING_PRINTF
  1113. printf("socketAppConnectionCallBack invalid event type %u", connectionEventType);
  1114. #endif
  1115. ECOMM_TRACE(UNILOG_PLA_APP, socketAppConnectionCallBack_10, P_ERROR, 1, "socketAppConnectionCallBack invalid event type %u", connectionEventType);
  1116. break;
  1117. }
  1118. }