net_log.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include "queue.h"
  6. #include "board.h"
  7. #include "chip.h"
  8. #include "animation.h"
  9. #include "sfud.h"
  10. #include "romfile.h"
  11. #include "sysinfo.h"
  12. #include "mmcsd_core.h"
  13. #include "ff_stdio.h"
  14. #include "ota_update.h"
  15. #include "md5.h"
  16. #include "unzip.h"
  17. #include "zip.h"
  18. #include "ulog.h"
  19. #include "easyflash.h"
  20. #include "include/crc.h"
  21. #include "ff_sfdisk.h"
  22. #include "ark_dwc2.h"
  23. #include "FreeRTOS_Sockets.h"
  24. #include "FreeRTOS_IP.h"
  25. #include "FreeRTOS_DHCP.h"
  26. #include "iperf_task.h"
  27. #include "iot_wifi.h"
  28. #include "FreeRTOS_DHCP_Server.h"
  29. #ifdef NCM_LOG_SUPPORT
  30. #define LOG_FRAME_HEADER_MAGIC 0xFF5AFFA5
  31. #define LOG_FRAME_TAIL_MAGIC 0xFFA5FF5A
  32. #define FILENAME_LEN 128
  33. #define MD5_LEN 32
  34. #define LOG_BUF_SIZE 0x100000
  35. #pragma pack(push, 1)
  36. typedef struct {
  37. uint32_t header;
  38. uint32_t type;
  39. uint32_t sub_type;
  40. uint32_t sub_type_1;
  41. uint32_t len;
  42. uint8_t checksum;
  43. } LogFrame;
  44. typedef struct {
  45. uint8_t request_type;
  46. uint32_t log_size;
  47. uint8_t log_md5[MD5_LEN];
  48. uint8_t log_filename[FILENAME_LEN];
  49. } LogTfrReq;
  50. #pragma pack(pop)
  51. static Socket_t log_req_socket;
  52. static Socket_t log_tfr_socket;
  53. #define FRAME_MAX_LEN 0x100000
  54. #define LOG_TFR_MAX_SIZE 128000
  55. #define OTA_LOG_FILENAME "log.zip"
  56. static int log_rev_state = 0;
  57. static int log_state_len = 0;
  58. static int log_rev_len = 0;
  59. static uint32_t log_frame_type = 0;
  60. static uint32_t log_frame_subtype = 0;
  61. static uint32_t log_frame_subtype_1 = 0;
  62. static uint32_t log_frame_len = 0;
  63. static uint8_t log_frame_checksum = 0;
  64. static uint8_t *log_frame_buf;
  65. static uint32_t log_file_size;
  66. static uint32_t log_file_offset;
  67. static uint32_t log_file_framenum;
  68. static uint8_t net_log_timeout_flag = 0;
  69. #define LOG_CHECKSUM(x) ((((x) >> 24) & 0xff) + (((x) >> 16) & 0xff) + (((x) >> 8) & 0xff) + ((x) & 0xff))
  70. static uint8_t calc_log_checksum(uint32_t type, uint32_t subtype, uint32_t subtype_1, uint32_t len)
  71. {
  72. uint32_t checksum = 0;
  73. checksum = LOG_CHECKSUM(type) + LOG_CHECKSUM(subtype) + LOG_CHECKSUM(subtype_1) + LOG_CHECKSUM(len);
  74. return 0x100 - (uint8_t)checksum;
  75. }
  76. static void logSendFrame(Socket_t socket, uint32_t type, uint32_t subtype,
  77. uint32_t subtype_1, void *data, uint32_t len)
  78. {
  79. LogFrame *frame = NULL;
  80. uint8_t *tmp;
  81. int32_t leftsize;
  82. BaseType_t ret;
  83. u32 errcnt = 0;
  84. printf("send frame type=0x%x, subtype=0x%x, len=%d.\n", type, subtype, len);
  85. frame = (LogFrame*)pvPortMalloc(sizeof(LogFrame) + len + 4);
  86. if (frame) {
  87. frame->header = LOG_FRAME_HEADER_MAGIC;
  88. frame->type = type;
  89. frame->sub_type = subtype;
  90. frame->sub_type_1 = subtype_1;
  91. frame->len = len;
  92. frame->checksum = calc_log_checksum(type, subtype, subtype_1, len);
  93. tmp = (uint8_t*)frame + sizeof(LogFrame);
  94. if (len) {
  95. memcpy(tmp, data, len);
  96. tmp += len;
  97. }
  98. *(uint32_t *)tmp = LOG_FRAME_TAIL_MAGIC;
  99. }
  100. leftsize = sizeof(LogFrame) + len + 4;
  101. tmp = (uint8_t *)frame;
  102. while (leftsize > 0) {
  103. ret = FreeRTOS_send(socket, tmp, leftsize, 0);
  104. if (ret > 0) {
  105. leftsize -= ret;
  106. tmp += ret;
  107. errcnt = 0;
  108. } else {
  109. //printf("FreeRTOS_send err %d.\n", ret);
  110. TRACE_INFO("FreeRTOS_send err %d.\n", ret);
  111. if (errcnt++ > 100) {
  112. //printf("FreeRTOS_send fail.\n");
  113. TRACE_INFO("FreeRTOS_send fail.\n");
  114. break;
  115. }
  116. vTaskDelay(pdMS_TO_TICKS(10));
  117. }
  118. }
  119. net_log_timeout_flag = 1;
  120. vPortFree(frame);
  121. }
  122. static int logTransferData(void)
  123. {
  124. char filename[FILENAME_LEN + 16] = OTA_MOUNT_PATH "/" OTA_LOG_FILENAME;
  125. FF_FILE *fp;
  126. uint32_t size;
  127. uint8_t *buf;
  128. fp = ff_fopen(filename, "rb");
  129. if (!fp) {
  130. printf("open log file fail.\n");
  131. return -1;
  132. }
  133. if (log_file_offset == 0) {
  134. log_file_framenum = 1;
  135. log_file_size = ff_filelength(fp);
  136. }
  137. size = log_file_size - log_file_offset;
  138. size = size > LOG_TFR_MAX_SIZE ? LOG_TFR_MAX_SIZE : size;
  139. ff_fseek(fp, log_file_offset, FF_SEEK_SET);
  140. log_file_offset += size;
  141. buf = pvPortMalloc(size + 6);
  142. if (!buf) {
  143. printf("tfrlog buf malloc fail.\n");
  144. ff_fclose(fp);
  145. return -1;
  146. }
  147. if (size > 0) {
  148. *(uint32_t*)buf = size;
  149. if (log_file_offset == log_file_size)
  150. *(uint16_t*)(buf + 4) = 0;
  151. else
  152. *(uint16_t*)(buf + 4) = log_file_framenum;
  153. log_file_framenum++;
  154. if (ff_fread(buf + 6, 1, size, fp) != size) {
  155. printf("read tfrlog fail.\n");
  156. vPortFree(buf);
  157. ff_fclose(fp);
  158. return -1;
  159. }
  160. } else {
  161. memset(buf, 0, 6);
  162. }
  163. logSendFrame(log_tfr_socket, 0x1f, 0x01, 0, buf, size + 6);
  164. vPortFree(buf);
  165. ff_fclose(fp);
  166. return 0;
  167. }
  168. static void RevLogFrameHandler(uint32_t type, uint32_t subtype, uint32_t subtype_1,
  169. uint8_t *data, uint32_t len)
  170. {
  171. printf("rev frame type=0x%x, subtype=0x%x, len=%d.\n", type, subtype, len);
  172. switch (type) {
  173. case 0x0a:
  174. if (subtype == 0x10) {
  175. if (data[0] == 0x01) {
  176. LogTfrReq req = {0};
  177. uint8_t *logbuf;
  178. size_t logsize;
  179. SystemTime_t tm;
  180. char filename[FILENAME_LEN + 16] = OTA_MOUNT_PATH "/" OTA_LOG_FILENAME;
  181. zipFile zf = {0};
  182. zip_fileinfo zi = {0};
  183. FF_FILE *fp;
  184. size_t filesize;
  185. size_t leftsize;
  186. size_t size;
  187. size_t logindex;
  188. unsigned char md5out[16];
  189. char md5string[MD5_LEN + 1];
  190. md5_context md5ctx;
  191. uint32_t logcrc;
  192. #if DEVICE_TYPE_SELECT == SPI_NAND_FLASH
  193. int readsize;
  194. #endif
  195. int i;
  196. logbuf = pvPortMalloc(LOG_BUF_SIZE);
  197. if (!logbuf) {
  198. printf("logbuf malloc fail.\n");
  199. logSendFrame(log_req_socket, 0x1b, 0x06, 0, &req, sizeof(req));
  200. break;
  201. }
  202. iGetLocalTime(&tm);
  203. snprintf((char*)req.log_filename, FILENAME_LEN, "IPLog%.4d%.2d%.2d%.2d%.2d%.2d.zip", tm.tm_year,
  204. tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  205. zf = zipOpen(filename, 0);
  206. if (!zf) {
  207. printf("create log file fail.\n");
  208. vPortFree(logbuf);
  209. logSendFrame(log_req_socket, 0x1b, 0x06, 0, &req, sizeof(req));
  210. break;
  211. }
  212. if (zipOpenNewFileInZip(zf, "log", &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 9)) {
  213. printf("zipOpenNewFileInZip fail.\n");
  214. zipClose(zf, NULL);
  215. vPortFree(logbuf);
  216. logSendFrame(log_req_socket, 0x1b, 0x06, 0, &req, sizeof(req));
  217. break;
  218. }
  219. leftsize = logsize = ef_log_get_used_size();
  220. logcrc = 0xffffffff;
  221. logindex = 0;
  222. while (leftsize > 0) {
  223. size = leftsize > LOG_BUF_SIZE ? LOG_BUF_SIZE : leftsize;
  224. #if DEVICE_TYPE_SELECT == SPI_NAND_FLASH
  225. readsize = read_flash_log(logbuf, logindex, size);
  226. if (readsize > 0) {
  227. if (size > readsize) {
  228. logsize -= (size - readsize);
  229. }
  230. if (zipWriteInFileInZip(zf, logbuf, readsize)) {
  231. printf("zipWriteInFileInZip fail.\n");
  232. break;
  233. }
  234. logcrc = xcrc32(logbuf, readsize, logcrc, HARD_CALC_CRC);
  235. }
  236. #else
  237. read_flash_log(logbuf, logindex, size);
  238. if (zipWriteInFileInZip(zf, logbuf, size)) {
  239. printf("zipWriteInFileInZip fail.\n");
  240. break;
  241. }
  242. logcrc = xcrc32(logbuf, size, logcrc, HARD_CALC_CRC);
  243. #endif
  244. leftsize -= size;
  245. logindex += size;
  246. }
  247. if (leftsize > 0) {
  248. zipClose(zf, NULL);
  249. vPortFree(logbuf);
  250. logSendFrame(log_req_socket, 0x1b, 0x06, 0, &req, sizeof(req));
  251. break;
  252. }
  253. sprintf((char*)logbuf, "\nlog size is 0x%x, crc is 0x%x.\n", logsize, logcrc);
  254. zipWriteInFileInZip(zf, logbuf, strlen((char*)logbuf));
  255. zipCloseFileInZip(zf);
  256. zipClose(zf, NULL);
  257. fp = ff_fopen(filename, "rb");
  258. if (!fp) {
  259. printf("open log zip file fail.\n");
  260. vPortFree(logbuf);
  261. logSendFrame(log_req_socket, 0x1b, 0x06, 0, &req, sizeof(req));
  262. break;
  263. }
  264. req.log_size = filesize = ff_filelength(fp);
  265. leftsize = filesize;
  266. memset(&md5ctx, 0, sizeof(md5_context));
  267. md5_starts(&md5ctx);
  268. while (leftsize > 0) {
  269. size = leftsize > LOG_BUF_SIZE ? LOG_BUF_SIZE : leftsize;
  270. size = ff_fread(logbuf, 1, size, fp);
  271. md5_update(&md5ctx, logbuf, size);
  272. leftsize -= size;
  273. }
  274. md5_finish(&md5ctx, md5out);
  275. ff_fclose(fp);
  276. vPortFree(logbuf);
  277. for (i = 0; i < 16; i++)
  278. sprintf(md5string + 2 * i, "%.2x", md5out[i]);
  279. memcpy(req.log_md5, md5string, MD5_LEN);
  280. req.request_type = 1;
  281. logSendFrame(log_req_socket, 0x1b, 0x06, 0, &req, sizeof(req));
  282. }
  283. }
  284. break;
  285. case 0x0b:
  286. if (subtype == 0x06) {
  287. if (data[0] == 0x01 && data[1] == 0x00) {
  288. uint8_t errdata[6] = {0};
  289. log_file_offset = 0;
  290. if (logTransferData())
  291. logSendFrame(log_tfr_socket, 0x1f, 0x01, 0, errdata, sizeof(errdata));
  292. }
  293. }
  294. break;
  295. case 0x0f:
  296. if (subtype == 0x02) {
  297. if (data[0] == 0x00) {
  298. uint8_t errdata[6] = {0};
  299. if (logTransferData())
  300. logSendFrame(log_tfr_socket, 0x1f, 0x01, 0, errdata, sizeof(errdata));
  301. }
  302. } else if (subtype == 0x03) {
  303. if (data[128] == 0x00) {
  304. printf("log transfer ok.\n");
  305. } else if (data[128] == 0x01 && data[129] == 0x01) {
  306. uint8_t errdata[6] = {0};
  307. printf("log transfer fail, resend.\n");
  308. log_file_offset = 0;
  309. if (logTransferData())
  310. logSendFrame(log_tfr_socket, 0x1f, 0x01, 0, errdata, sizeof(errdata));
  311. }
  312. }
  313. }
  314. }
  315. static void RevLogDataHandler(uint8_t *buf, int32_t len)
  316. {
  317. int i;
  318. for (i = 0; i < len; i++) {
  319. switch (log_rev_state) {
  320. case 0: //head receive
  321. if (log_state_len == 0 && buf[i] == 0xa5) {
  322. log_state_len++;
  323. } else if (log_state_len == 1 && buf[i] == 0xff) {
  324. log_state_len++;
  325. } else if (log_state_len == 2 && buf[i] == 0x5a) {
  326. log_state_len++;
  327. } else if (log_state_len == 3 && buf[i] == 0xff) {
  328. log_rev_state++;
  329. log_state_len = 0;
  330. log_frame_type = 0;
  331. log_frame_subtype = 0;
  332. log_frame_subtype_1 = 0;
  333. log_frame_len = 0;
  334. } else if (buf[i] == 0xff) {
  335. log_state_len = 1;
  336. } else {
  337. log_state_len = 0;
  338. }
  339. break;
  340. case 1: //type receive
  341. log_frame_type |= buf[i] << (8 * log_state_len);
  342. if (++log_state_len == 4) {
  343. log_rev_state++;
  344. log_state_len = 0;
  345. }
  346. break;
  347. case 2: //sub_type receive
  348. log_frame_subtype |= buf[i] << (8 * log_state_len);
  349. if (++log_state_len == 4) {
  350. log_rev_state++;
  351. log_state_len = 0;
  352. }
  353. break;
  354. case 3: //sub_type_1 receive
  355. log_frame_subtype_1 |= buf[i] << (8 * log_state_len);
  356. if (++log_state_len == 4) {
  357. log_rev_state++;
  358. log_state_len = 0;
  359. }
  360. break;
  361. case 4: //data len receive
  362. log_frame_len |= buf[i] << (8 * log_state_len);
  363. if (++log_state_len == 4) {
  364. if (log_frame_len > FRAME_MAX_LEN) {
  365. printf("Invalid NCM frame len 0x%x.\n", log_frame_len);
  366. }
  367. if (log_frame_len == 0)
  368. log_rev_state += 2;
  369. else
  370. log_rev_state++;
  371. log_rev_len = 0;
  372. log_state_len = 0;
  373. }
  374. break;
  375. case 5: //checksum receive
  376. log_frame_checksum = buf[i];
  377. if (log_frame_checksum != calc_log_checksum(log_frame_type, log_frame_subtype,
  378. log_frame_subtype_1, log_frame_len)) {
  379. printf("log frame checksum fail.\n");
  380. log_rev_state = 0;
  381. } else {
  382. log_rev_state++;
  383. }
  384. break;
  385. case 6: //data receive
  386. if (log_rev_len < FRAME_MAX_LEN)
  387. log_frame_buf[log_rev_len] = buf[i];
  388. if (++log_rev_len == log_frame_len)
  389. log_rev_state++;
  390. break;
  391. case 7: //tail receive
  392. if (log_state_len == 0 && buf[i] == 0x5a) {
  393. log_state_len++;
  394. } else if (log_state_len == 1 && buf[i] == 0xff) {
  395. log_state_len++;
  396. } else if (log_state_len == 2 && buf[i] == 0xa5) {
  397. log_state_len++;
  398. } else if (log_state_len == 3 && buf[i] == 0xff) {
  399. RevLogFrameHandler(log_frame_type, log_frame_subtype, log_frame_subtype_1,
  400. log_frame_buf, log_frame_len);
  401. log_rev_state = 0;
  402. log_state_len = 0;
  403. } else {
  404. log_state_len = 0;
  405. }
  406. break;
  407. default:
  408. break;
  409. }
  410. }
  411. }
  412. static void set_socket_win_net(Socket_t xSockets)
  413. {
  414. WinProperties_t xWinProperties;
  415. memset(&xWinProperties, '\0', sizeof xWinProperties);
  416. xWinProperties.lTxBufSize = ipconfigIPERF_TX_BUFSIZE; /* Units of bytes. */
  417. xWinProperties.lTxWinSize = ipconfigIPERF_TX_WINSIZE; /* Size in units of MSS */
  418. xWinProperties.lRxBufSize = ipconfigIPERF_RX_BUFSIZE; /* Units of bytes. */
  419. xWinProperties.lRxWinSize = ipconfigIPERF_RX_WINSIZE; /* Size in units of MSS */
  420. FreeRTOS_setsockopt( xSockets, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProperties, sizeof( xWinProperties ) );
  421. }
  422. static uint8_t ncm_data_buffer[65536] = {0};
  423. static int NCMLogConnectServer(Socket_t socket, int port)
  424. {
  425. struct freertos_sockaddr xAddress;
  426. int retries = 3;
  427. xAddress.sin_port = FreeRTOS_htons( port );
  428. xAddress.sin_addr = FreeRTOS_inet_addr_quick(192, 168, 2, 99);
  429. while (retries-- > 0) {
  430. if(!FreeRTOS_connect(socket, &xAddress, sizeof(xAddress)))
  431. return 0;
  432. vTaskDelay(pdMS_TO_TICKS(100));
  433. }
  434. return -1;
  435. }
  436. static int vCreateNCMLogClientSocket( void )
  437. {
  438. SocketSet_t xFD_Set;
  439. Socket_t xReqSockets = FREERTOS_INVALID_SOCKET, xTfrSockets = FREERTOS_INVALID_SOCKET;
  440. static const TickType_t xNoTimeOut = pdMS_TO_TICKS( 10000 );
  441. BaseType_t ret = -1;
  442. EventBits_t events;
  443. xFD_Set = FreeRTOS_CreateSocketSet();
  444. xReqSockets = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
  445. configASSERT( xReqSockets != FREERTOS_INVALID_SOCKET );
  446. FreeRTOS_setsockopt( xReqSockets,
  447. 0,
  448. FREERTOS_SO_RCVTIMEO,
  449. &xNoTimeOut,
  450. sizeof( xNoTimeOut ) );
  451. set_socket_win_net(xReqSockets);
  452. if (!NCMLogConnectServer(xReqSockets, 10001)) {
  453. printf("Connect to 192.168.2.99:10001 ok.\n");
  454. } else {
  455. printf("Connect to 192.168.2.99:10001 fail.\n");
  456. FreeRTOS_closesocket(xReqSockets);
  457. FreeRTOS_DeleteSocketSet(xFD_Set);
  458. return -1;
  459. }
  460. log_req_socket = xReqSockets;
  461. xTfrSockets = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
  462. configASSERT( xTfrSockets != FREERTOS_INVALID_SOCKET );
  463. FreeRTOS_setsockopt( xTfrSockets,
  464. 0,
  465. FREERTOS_SO_RCVTIMEO,
  466. &xNoTimeOut,
  467. sizeof( xNoTimeOut ) );
  468. set_socket_win_net(xTfrSockets);
  469. if (!NCMLogConnectServer(xTfrSockets, 10002)) {
  470. printf("Connect to 192.168.2.99:10002 ok.\n");
  471. } else {
  472. printf("Connect to 192.168.2.99:10002 fail.\n");
  473. FreeRTOS_closesocket(xReqSockets);
  474. FreeRTOS_closesocket(xTfrSockets);
  475. FreeRTOS_DeleteSocketSet(xFD_Set);
  476. return -1;
  477. }
  478. log_tfr_socket = xTfrSockets;
  479. while (1) {
  480. FreeRTOS_FD_CLR(xReqSockets, xFD_Set, eSELECT_ALL);
  481. FreeRTOS_FD_SET(xReqSockets, xFD_Set, eSELECT_READ | eSELECT_EXCEPT);
  482. FreeRTOS_FD_CLR(xTfrSockets, xFD_Set, eSELECT_ALL);
  483. FreeRTOS_FD_SET(xTfrSockets, xFD_Set, eSELECT_READ | eSELECT_EXCEPT);
  484. ret = FreeRTOS_select( xFD_Set, pdMS_TO_TICKS(3000));//portMAX_DELAY
  485. if (ret < 0) {
  486. break;
  487. } else if(ret == 0) {
  488. if(net_log_timeout_flag) {
  489. printf("%s, net_log timeout: %d\r\n", __func__, __LINE__);
  490. usb_dwc2_reset(0, 1);
  491. net_log_timeout_flag = 0;
  492. break;
  493. }
  494. continue;
  495. }
  496. net_log_timeout_flag = 0;
  497. if( events = FreeRTOS_FD_ISSET ( xReqSockets, xFD_Set ) ) {
  498. if (events & eSELECT_READ) {
  499. ret = FreeRTOS_recv(xReqSockets, ncm_data_buffer, sizeof(ncm_data_buffer), 0);
  500. if (ret > 0) {
  501. printf("@recv buf size:%d\r\n", ret);
  502. //print_hex(ncm_data_buffer, ret, NULL);
  503. RevLogDataHandler(ncm_data_buffer, ret);
  504. } else {
  505. printf("FreeRTOS_recv err:%d\r\n", ret);
  506. break;
  507. }
  508. }
  509. if (events & eSELECT_EXCEPT) {
  510. printf("xReqSockets exception\r\n");
  511. break;
  512. }
  513. }
  514. if( events = FreeRTOS_FD_ISSET ( xTfrSockets, xFD_Set ) ) {
  515. if (events & eSELECT_READ) {
  516. ret = FreeRTOS_recv(xTfrSockets, ncm_data_buffer, sizeof(ncm_data_buffer), 0);
  517. if (ret > 0) {
  518. printf("@@recv buf size:%d\r\n", ret);
  519. //print_hex(ncm_data_buffer, ret, NULL);
  520. RevLogDataHandler(ncm_data_buffer, ret);
  521. } else {
  522. printf("FreeRTOS_recv err:%d\r\n", ret);
  523. break;
  524. }
  525. }
  526. if (events & eSELECT_EXCEPT) {
  527. printf("xTfrSockets exception\r\n");
  528. break;
  529. }
  530. }
  531. }
  532. FreeRTOS_closesocket(xReqSockets);
  533. FreeRTOS_closesocket(xTfrSockets);
  534. FreeRTOS_DeleteSocketSet(xFD_Set);
  535. log_req_socket = NULL;
  536. log_tfr_socket = NULL;
  537. return 0;
  538. }
  539. void ncm_log_demo_thread(void *param)
  540. {
  541. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  542. FF_Disk_t *sfdisk = FF_SFDiskInit(SF_MOUNT_PATH);
  543. if (!sfdisk) {
  544. printf("FF_SFDiskInit fail.\n");
  545. return;
  546. }
  547. #endif
  548. log_frame_buf = pvPortMalloc(FRAME_MAX_LEN);
  549. if (!log_frame_buf) {
  550. printf("log_frame_buf malloc fail.\n");
  551. return;
  552. }
  553. while(1) {
  554. vCreateNCMLogClientSocket();
  555. vTaskDelay(pdMS_TO_TICKS(3000));
  556. }
  557. }
  558. void ncm_log_demo(void)
  559. {
  560. if (xTaskCreate(ncm_log_demo_thread, "ncmlog", 32768, NULL,
  561. 1, NULL) != pdPASS) {
  562. printf("create ncm log demo task fail.\n");
  563. }
  564. }
  565. #endif