AppTaskTcp.c 51 KB

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