AppTaskTcp.c 51 KB

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