at_socket_ml307.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-12-16 Jonas first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <at_device_ml307.h>
  13. #define LOG_TAG "at.skt.ml307"
  14. #include <at_log.h>
  15. #if defined(AT_DEVICE_USING_ML307) && defined(AT_USING_SOCKET)
  16. #if !defined (ML307_MODULE_SEND_MAX_SIZE)
  17. #define ML307_MODULE_SEND_MAX_SIZE 4096
  18. #endif
  19. /* set real event by current socket and current state */
  20. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  21. //#define SET_EVENT(socket, event) ((1 << (socket + 16)) | (event))
  22. /* AT socket event type */
  23. #define ML307_EVENT_CONN_OK (1L << 0)
  24. #define ML307_EVENT_SEND_OK (1L << 1)
  25. #define ML307_EVENT_RECV_OK (1L << 2)
  26. #define ML307_EVENT_CLOSE_OK (1L << 3)
  27. #define ML307_EVENT_CONN_FAIL (1L << 4)
  28. #define ML307_EVENT_SEND_FAIL (1L << 5)
  29. static at_evt_cb_t at_evt_cb_set[] = {
  30. [AT_SOCKET_EVT_RECV] = NULL,
  31. [AT_SOCKET_EVT_CLOSED] = NULL,
  32. };
  33. static int ml307_socket_event_send(struct at_device *device, uint32_t event)
  34. {
  35. return (int) rt_event_send(device->socket_event, event);
  36. }
  37. static int ml307_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  38. {
  39. int result = RT_EOK;
  40. rt_uint32_t recved;
  41. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  42. if (result != RT_EOK)
  43. {
  44. return -RT_ETIMEOUT;
  45. }
  46. return recved;
  47. }
  48. /**
  49. * close socket by AT commands.
  50. *
  51. * @param current socket
  52. *
  53. * @return 0: close socket success
  54. * -1: send AT commands error
  55. * -2: wait socket event timeout
  56. * -5: no memory
  57. */
  58. static int ml307_socket_close(struct at_socket *socket)
  59. {
  60. uint32_t event = 0;
  61. int result = RT_EOK;
  62. int device_socket = (int) socket->user_data;
  63. struct at_device *device = (struct at_device *) socket->device;
  64. /* clear socket close event */
  65. event = SET_EVENT(device_socket, ML307_EVENT_CLOSE_OK);
  66. ml307_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  67. if (at_obj_exec_cmd(device->client, NULL, "AT+MIPCLOSE=%d", device_socket) < 0)
  68. {
  69. result = -RT_ERROR;
  70. goto __exit;
  71. }
  72. if (ml307_socket_event_recv(device, event, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  73. {
  74. LOG_E("ml307 device(%s) socket(%d) close failed, wait close OK timeout.", device->name, device_socket);
  75. result = -RT_ETIMEOUT;
  76. goto __exit;
  77. }
  78. __exit:
  79. return result;
  80. }
  81. /**
  82. * create TCP/UDP client or server connect by AT commands.
  83. *
  84. * @param socket current socket
  85. * @param ip server or client IP address
  86. * @param port server or client port
  87. * @param type connect socket type(tcp, udp)
  88. * @param is_client connection is client
  89. *
  90. * @return 0: connect success
  91. * -1: connect failed, send commands error or type error
  92. * -2: wait socket event timeout
  93. * -5: no memory
  94. */
  95. static int ml307_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  96. {
  97. uint32_t event = 0;
  98. rt_bool_t retryed = RT_FALSE;
  99. at_response_t resp = RT_NULL;
  100. int result = RT_EOK, event_result = 0;
  101. int device_socket = (int) socket->user_data;
  102. struct at_device *device = (struct at_device *) socket->device;
  103. RT_ASSERT(ip);
  104. RT_ASSERT(port >= 0);
  105. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  106. if (resp == RT_NULL)
  107. {
  108. LOG_E("no memory for ml307 device(%s) response structure.", device->name);
  109. return -RT_ENOMEM;
  110. }
  111. __retry:
  112. /* clear socket connect event */
  113. event = SET_EVENT(device_socket, ML307_EVENT_CONN_OK | ML307_EVENT_CONN_FAIL);
  114. ml307_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  115. if (is_client)
  116. {
  117. switch (type)
  118. {
  119. case AT_SOCKET_TCP:
  120. /* send AT commands(eg: AT+MIPOPEN=0,"TCP","x.x.x.x", 1234) to connect TCP server */
  121. if (at_obj_exec_cmd(device->client, RT_NULL, "AT+MIPOPEN=%d,\"TCP\",\"%s\",%d", device_socket, ip, port) < 0)
  122. {
  123. result = -RT_ERROR;
  124. goto __exit;
  125. }
  126. break;
  127. case AT_SOCKET_UDP:
  128. if (at_obj_exec_cmd(device->client, RT_NULL, "AT+MIPOPEN=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
  129. {
  130. result = -RT_ERROR;
  131. goto __exit;
  132. }
  133. break;
  134. default:
  135. LOG_E("ml307 device(%s) not supported connect type : %d.", device->name, type);
  136. result = -RT_ERROR;
  137. goto __exit;
  138. }
  139. }
  140. /* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use */
  141. if (ml307_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  142. {
  143. LOG_E("ml307 device(%s) socket(%d) connect failed, wait connect result timeout.",device->name, device_socket);
  144. result = -RT_ETIMEOUT;
  145. goto __exit;
  146. }
  147. /* waiting OK or failed result */
  148. event_result = ml307_socket_event_recv(device,
  149. ML307_EVENT_CONN_OK | ML307_EVENT_CONN_FAIL, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  150. if (event_result < 0)
  151. {
  152. LOG_E("ml307 device(%s) socket(%d) connect failed, wait connect OK|FAIL timeout.", device->name, device_socket);
  153. result = -RT_ETIMEOUT;
  154. goto __exit;
  155. }
  156. /* check result */
  157. if (event_result & ML307_EVENT_CONN_FAIL)
  158. {
  159. if (retryed == RT_FALSE)
  160. {
  161. LOG_D("ml307 device(%s) socket(%d) connect failed, maybe the socket was not be closed at the last time and now will retry.",
  162. device->name, device_socket);
  163. if (ml307_socket_close(socket) < 0)
  164. {
  165. result = -RT_ERROR;
  166. goto __exit;
  167. }
  168. retryed = RT_TRUE;
  169. goto __retry;
  170. }
  171. LOG_E("ml307 device(%s) socket(%d) connect failed.", device->name, device_socket);
  172. result = -RT_ERROR;
  173. goto __exit;
  174. }
  175. __exit:
  176. if (resp)
  177. {
  178. at_delete_resp(resp);
  179. }
  180. return result;
  181. }
  182. /**
  183. * send data to server or client by AT commands.
  184. *
  185. * @param socket current socket
  186. * @param buff send buffer
  187. * @param bfsz send buffer size
  188. * @param type connect socket type(tcp, udp)
  189. *
  190. * @return >=0: the size of send success
  191. * -1: send AT commands error or send data error
  192. * -2: waited socket event timeout
  193. * -5: no memory
  194. */
  195. static int ml307_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  196. {
  197. uint32_t event = 0;
  198. int result = RT_EOK, event_result = 0;
  199. size_t cur_pkt_size = 0, sent_size = 0;
  200. at_response_t resp = RT_NULL;
  201. int device_socket = (int) socket->user_data;
  202. struct at_device *device = (struct at_device *) socket->device;
  203. rt_mutex_t lock = at_device_get_client_lock(device);
  204. RT_ASSERT(buff);
  205. resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
  206. if (resp == RT_NULL)
  207. {
  208. LOG_E("no memory for ml307 device(%s) response structure.", device->name);
  209. return -RT_ENOMEM;
  210. }
  211. rt_mutex_take(lock, RT_WAITING_FOREVER);
  212. /* clear socket connect event */
  213. event = SET_EVENT(device_socket, ML307_EVENT_SEND_OK | ML307_EVENT_SEND_FAIL);
  214. ml307_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  215. /* set AT client end sign to deal with '>' sign.*/
  216. at_obj_set_end_sign(device->client, '>');
  217. while (sent_size < bfsz)
  218. {
  219. if (bfsz - sent_size < ML307_MODULE_SEND_MAX_SIZE)
  220. {
  221. cur_pkt_size = bfsz - sent_size;
  222. }
  223. else
  224. {
  225. cur_pkt_size = ML307_MODULE_SEND_MAX_SIZE;
  226. }
  227. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  228. if (at_obj_exec_cmd(device->client, resp, "AT+MIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
  229. {
  230. result = -RT_ERROR;
  231. goto __exit;
  232. }
  233. /* send the real data to server or client */
  234. result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  235. if (result == 0)
  236. {
  237. result = -RT_ERROR;
  238. goto __exit;
  239. }
  240. /* waiting result event from AT URC */
  241. if (ml307_socket_event_recv(device, SET_EVENT(device_socket, 0), 15 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  242. {
  243. LOG_E("ml307 device(%s) socket(%d) send failed, wait connect result timeout.", device->name, device_socket);
  244. result = -RT_ETIMEOUT;
  245. goto __exit;
  246. }
  247. /* waiting OK or failed result */
  248. event_result = ml307_socket_event_recv(device,
  249. ML307_EVENT_SEND_OK | ML307_EVENT_SEND_FAIL, 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  250. if (event_result < 0)
  251. {
  252. LOG_E("ml307 device(%s) socket(%d) send failed, wait connect OK|FAIL timeout.", device->name, device_socket);
  253. result = -RT_ETIMEOUT;
  254. goto __exit;
  255. }
  256. /* check result */
  257. if (event_result & ML307_EVENT_SEND_FAIL)
  258. {
  259. LOG_E("ml307 device(%s) socket(%d) send failed.", device->name, device_socket);
  260. result = -RT_ERROR;
  261. goto __exit;
  262. }
  263. sent_size += cur_pkt_size;
  264. }
  265. __exit:
  266. /* reset the end sign for data conflict */
  267. at_obj_set_end_sign(device->client, 0);
  268. rt_mutex_release(lock);
  269. if (resp)
  270. {
  271. at_delete_resp(resp);
  272. }
  273. return result;
  274. }
  275. /**
  276. * domain resolve by AT commands.
  277. *
  278. * @param name domain name
  279. * @param ip parsed IP address, it's length must be 16
  280. *
  281. * @return 0: domain resolve success
  282. * -1: send AT commands error or response error
  283. * -2: wait socket event timeout
  284. * -5: no memory
  285. */
  286. static int ml307_domain_resolve(const char *name, char ip[16])
  287. {
  288. #define RESOLVE_RETRY 5
  289. int i, result = RT_EOK;
  290. char recv_ip[16] = { 0 };
  291. at_response_t resp = RT_NULL;
  292. struct at_device *device = RT_NULL;
  293. RT_ASSERT(name);
  294. RT_ASSERT(ip);
  295. device = at_device_get_first_initialized();
  296. if (device == RT_NULL)
  297. {
  298. LOG_E("get first initialization ml307 device failed.");
  299. return -RT_ERROR;
  300. }
  301. /* The maximum response time is 14 seconds, affected by network status */
  302. resp = at_create_resp(1024, 4, 14 * RT_TICK_PER_SECOND);
  303. if (resp == RT_NULL)
  304. {
  305. LOG_E("no memory for ml307 device(%s) response structure.", device->name);
  306. return -RT_ENOMEM;
  307. }
  308. for (i = 0; i < RESOLVE_RETRY; i++)
  309. {
  310. int err_code = 0;
  311. if (at_obj_exec_cmd(device->client, resp, "AT+MDNSGIP=\"%s\"", name) < 0)
  312. {
  313. result = -RT_ERROR;
  314. goto __exit;
  315. }
  316. /* domain name prase error options */
  317. if (at_resp_parse_line_args_by_kw(resp, "+CME ERROR: ", "+CME ERROR: %d", &err_code) > 0)
  318. {
  319. /* 580 - DNS解析失败或错误的IP格式, 581 - DNS忙碌 */
  320. if (err_code == 580 || err_code == 581)
  321. {
  322. result = -RT_ERROR;
  323. goto __exit;
  324. }
  325. }
  326. /* parse the third line of response data, get the IP address */
  327. if (at_resp_parse_line_args_by_kw(resp, "+MDNSGIP: ", "%*[^,],\"%[^\"]", recv_ip) < 0)
  328. {
  329. rt_thread_mdelay(100);
  330. /* resolve failed, maybe receive an URC CRLF */
  331. continue;
  332. }
  333. if (rt_strlen(recv_ip) < 8)
  334. {
  335. rt_thread_mdelay(100);
  336. /* resolve failed, maybe receive an URC CRLF */
  337. continue;
  338. }
  339. else
  340. {
  341. rt_thread_mdelay(10);
  342. rt_strncpy(ip, recv_ip, 15);
  343. ip[15] = '\0';
  344. break;
  345. }
  346. }
  347. __exit:
  348. if (resp)
  349. {
  350. at_delete_resp(resp);
  351. }
  352. return result;
  353. }
  354. /**
  355. * set AT socket event notice callback
  356. *
  357. * @param event notice event
  358. * @param cb notice callback
  359. */
  360. static void ml307_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  361. {
  362. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  363. {
  364. at_evt_cb_set[event] = cb;
  365. }
  366. }
  367. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  368. {
  369. int device_socket = 0;
  370. struct at_device *device = RT_NULL;
  371. char *client_name = client->device->parent.name;
  372. int connect_result = 0;
  373. RT_ASSERT(data && size);
  374. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  375. if (device == RT_NULL)
  376. {
  377. LOG_E("get ml307 device by client name(%s) failed.", client_name);
  378. return;
  379. }
  380. /* get the current socket by receive data */
  381. rt_sscanf(data, "%*s %d,%d", &device_socket, &connect_result);
  382. if(connect_result == 0)
  383. {
  384. ml307_socket_event_send(device, SET_EVENT(device_socket, ML307_EVENT_CONN_OK));
  385. }
  386. else
  387. {
  388. ml307_socket_event_send(device, SET_EVENT(device_socket, ML307_EVENT_CONN_FAIL));
  389. }
  390. }
  391. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  392. {
  393. int device_socket = 0;
  394. struct at_device *device = RT_NULL;
  395. char *client_name = client->device->parent.name;
  396. int send_len = 0;
  397. RT_ASSERT(data && size);
  398. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  399. if (device == RT_NULL)
  400. {
  401. LOG_E("get ml307 device by client name(%s) failed.", client_name);
  402. return;
  403. }
  404. /* get the current socket by receive data */
  405. rt_sscanf(data, "+MIPSEND: %d,%d", &device_socket, &send_len);
  406. if (send_len >= 0)
  407. {
  408. ml307_socket_event_send(device, SET_EVENT(device_socket, ML307_EVENT_SEND_OK));
  409. }
  410. else
  411. {
  412. ml307_socket_event_send(device, SET_EVENT(device_socket, ML307_EVENT_SEND_FAIL));
  413. }
  414. }
  415. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  416. {
  417. int device_socket = 0;
  418. struct at_device *device = RT_NULL;
  419. char *client_name = client->device->parent.name;
  420. int close_result = 0;
  421. RT_ASSERT(data && size);
  422. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  423. if (device == RT_NULL)
  424. {
  425. LOG_E("get ml307 device by client name(%s) failed.", client_name);
  426. return;
  427. }
  428. /* get the current socket by receive data */
  429. rt_sscanf(data, "%d", &device_socket);
  430. if (close_result == 0)
  431. {
  432. ml307_socket_event_send(device, SET_EVENT(device_socket, ML307_EVENT_CLOSE_OK));
  433. }
  434. else
  435. {
  436. struct at_socket *socket = RT_NULL;
  437. /* get AT socket object by device socket descriptor */
  438. socket = &(device->sockets[device_socket]);
  439. /* notice the socket is disconnect by remote */
  440. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  441. {
  442. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  443. }
  444. }
  445. }
  446. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  447. {
  448. int device_socket = 0;
  449. rt_int32_t timeout;
  450. rt_size_t temp_size = 0;
  451. char temp[8] = {0};
  452. rt_size_t bfsz = 0;
  453. char *recv_buf = RT_NULL;
  454. struct at_socket *socket = RT_NULL;
  455. struct at_device *device = RT_NULL;
  456. char *client_name = client->device->parent.name;
  457. char *ptr = RT_NULL, *data_ptr = RT_NULL, *ptr2 = RT_NULL;
  458. RT_ASSERT(data && size);
  459. /* get the current socket and receive buffer size by receive data */
  460. rt_sscanf(data, "+MIPURC:%*[^,],%d,%d", &device_socket, (int *) &bfsz);
  461. /* get receive timeout by receive buffer length */
  462. timeout = bfsz;
  463. if (device_socket < 0 || bfsz == 0)
  464. {
  465. return;
  466. }
  467. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  468. if (device == RT_NULL)
  469. {
  470. LOG_E("get ml307 device by client name(%s) failed.", client_name);
  471. return;
  472. }
  473. //ML307 AT指令, TCP和UDP普通模式接收数据格式如下:
  474. //+MIPURC: "rtcp",<connect_id>,<recv_length>,<data>
  475. //+MIPURC: "rudp",<connect_id>,<recv_length>,<data>
  476. ptr = rt_strstr(data,",");
  477. data_ptr = ptr + 1;
  478. ptr = rt_strstr(data_ptr,",");
  479. data_ptr = ptr + 1;
  480. ptr = rt_strstr(data_ptr,",");
  481. ptr++; //ptr指向<data>数据起始位置
  482. temp_size = size - (ptr - data); //temp_size是接收缓冲区中接收到的<data>实际长度
  483. recv_buf = (char *) rt_calloc(1, bfsz);
  484. if (recv_buf == RT_NULL)
  485. {
  486. LOG_E("no memory for ml307 device(%s) URC receive buffer (%d).", device->name, bfsz);
  487. if(bfsz <= temp_size)
  488. {
  489. return;
  490. }
  491. /* read and clean the coming data */
  492. while (temp_size < bfsz)
  493. {
  494. if (bfsz - temp_size > sizeof(temp))
  495. {
  496. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  497. }
  498. else
  499. {
  500. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  501. }
  502. temp_size += sizeof(temp);
  503. }
  504. return;
  505. }
  506. if (temp_size - 2 == bfsz)
  507. {
  508. rt_memcpy(recv_buf, ptr, bfsz);
  509. }
  510. else if(temp_size < bfsz)
  511. {
  512. rt_memcpy(recv_buf, ptr, temp_size);
  513. ptr2 = recv_buf + temp_size;
  514. int recv_len = at_client_obj_recv(client, ptr2, bfsz - temp_size, timeout);
  515. /* sync receive data */
  516. if (recv_len != (bfsz - temp_size))
  517. {
  518. LOG_E("ml307 device(%s) receive size(%d) data failed.", device->name, bfsz);
  519. rt_free(recv_buf);
  520. return;
  521. }
  522. }
  523. else
  524. {
  525. LOG_E("ml307 device(%s) receive size(%d) data failed.", device->name, bfsz);
  526. rt_free(recv_buf);
  527. return;
  528. }
  529. /* get AT socket object by device socket descriptor */
  530. socket = &(device->sockets[device_socket]);
  531. /* notice the receive buffer and buffer size */
  532. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  533. {
  534. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  535. }
  536. }
  537. static void urc_state_func(struct at_client *client, const char *data, rt_size_t size)
  538. {
  539. RT_ASSERT(data);
  540. LOG_I("URC state data : %.*s", size, data);
  541. }
  542. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  543. {
  544. RT_ASSERT(data);
  545. LOG_I("URC data : %.*s", size, data);
  546. }
  547. static void urc_disc_func(struct at_client *client, const char *data, rt_size_t size)
  548. {
  549. int device_socket = 0;
  550. struct at_device *device = RT_NULL;
  551. char *client_name = client->device->parent.name;
  552. int connect_state = 0;
  553. RT_ASSERT(data && size);
  554. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  555. if (device == RT_NULL)
  556. {
  557. LOG_E("get ml307 device by client name(%s) failed.", client_name);
  558. return;
  559. }
  560. /* get the current socket & connect_state by receive data */
  561. rt_sscanf(data, "%*[^ ] \"%*[^\"]\",%d,%d", &device_socket, &connect_state);
  562. // LOG_I("device_socket = %d, connect_state = %d", device_socket, connect_state);
  563. //connect_state: 1. The servr close connection; 2. Connection exception; 3. Deactivate PDP context.
  564. if (connect_state > 0)
  565. {
  566. struct at_socket *socket = RT_NULL;
  567. /* get AT socket object by device socket descriptor */
  568. socket = &(device->sockets[device_socket]);
  569. /* notice the socket is disconnect by remote */
  570. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  571. {
  572. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  573. }
  574. }
  575. }
  576. static void urc_drop_func(struct at_client *client, const char *data, rt_size_t size)
  577. {
  578. RT_ASSERT(data);
  579. LOG_I("URC data : %.*s", size, data);
  580. }
  581. static void urc_disc_drop_func(struct at_client *client, const char *data, rt_size_t size)
  582. {
  583. RT_ASSERT(data && size);
  584. switch(*(data + 11))
  585. {
  586. case 'i' : urc_disc_func(client, data, size); break; //+MIPURC: "disconn",<connect_id>,<connect_state>
  587. case 'r' : urc_drop_func(client, data, size); break; //+MIPURC: "drop",<connect_id>,<drop_length>
  588. default : urc_func(client, data, size); break;
  589. }
  590. }
  591. static void urc_mipurc_func(struct at_client *client, const char *data, rt_size_t size)
  592. {
  593. RT_ASSERT(data && size);
  594. switch(*(data + 10))
  595. {
  596. case 's' : urc_state_func(client, data, size); break; //+MIPURC: "state",<connect_id>,<connect_state>
  597. case 'r' : urc_recv_func(client, data, size); break; //+MIPURC: "recv",<connect_id>,<recv_length>
  598. case 'd' : urc_disc_drop_func(client, data, size); break; //+MIPURC: "disconn",<connect_id>,<connect_state>或+MIPURC: "drop",<connect_id>,<drop_length>
  599. default : urc_func(client, data, size); break;
  600. }
  601. }
  602. static void urc_cme_err_func(struct at_client *client, const char *data, rt_size_t size)
  603. {
  604. RT_ASSERT(data);
  605. LOG_I("URC CME ERROR Code : %.*s", size, data);
  606. }
  607. /* ML307 device URC table for the socket data */
  608. static const struct at_urc urc_table[] =
  609. {
  610. {"+MIPOPEN:", "\r\n", urc_connect_func},
  611. {"", "CONNECT\r\n", urc_connect_func},
  612. {"+MIPSEND:", "\r\n", urc_send_func},
  613. {"+MIPCLOSE:", "\r\n", urc_close_func},
  614. {"+MIPURC:", "\r\n", urc_mipurc_func},
  615. {"+CME ERROR:", "\r\n", urc_cme_err_func},
  616. };
  617. static struct at_socket_ops ml307_socket_ops =
  618. {
  619. ml307_socket_connect,
  620. ml307_socket_close,
  621. ml307_socket_send,
  622. ml307_domain_resolve,
  623. ml307_socket_set_event_cb,
  624. #if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
  625. RT_NULL,
  626. #endif
  627. };
  628. int ml307_socket_init(struct at_device *device)
  629. {
  630. RT_ASSERT(device);
  631. /* register URC data execution function */
  632. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  633. return RT_EOK;
  634. }
  635. int ml307_socket_class_register(struct at_device_class *class)
  636. {
  637. RT_ASSERT(class);
  638. class->socket_num = AT_DEVICE_ML307_SOCKETS_NUM;
  639. class->socket_ops = &ml307_socket_ops;
  640. return RT_EOK;
  641. }
  642. #endif /* AT_DEVICE_USING_ML307 && AT_USING_SOCKET */