at_socket_n58.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * File : at_socket_n58.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2020, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2020-05-22 shuobatian first version
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <at_device_n58.h>
  27. #if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10300
  28. #error "This AT Client version is older, please check and update latest AT Client!"
  29. #endif
  30. #define LOG_TAG "at.skt"
  31. #include <at_log.h>
  32. #if defined(AT_DEVICE_USING_N58) && defined(AT_USING_SOCKET)
  33. #define N58_MODULE_SEND_MAX_SIZE 1000
  34. /* set real event by current socket and current state */
  35. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  36. /* AT socket event type */
  37. #define N58_EVENT_CONN_OK (1L << 0)
  38. #define N58_EVENT_SEND_OK (1L << 1)
  39. #define N58_EVENT_RECV_OK (1L << 2)
  40. #define N58_EVNET_CLOSE_OK (1L << 3)
  41. #define N58_EVENT_CONN_FAIL (1L << 4)
  42. #define N58_EVENT_SEND_FAIL (1L << 5)
  43. static at_evt_cb_t at_evt_cb_set[] = {
  44. [AT_SOCKET_EVT_RECV] = NULL,
  45. [AT_SOCKET_EVT_CLOSED] = NULL,
  46. };
  47. static int n58_socket_event_send(struct at_device *device, uint32_t event)
  48. {
  49. return (int)rt_event_send(device->socket_event, event);
  50. }
  51. static int n58_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  52. {
  53. int result = RT_EOK;
  54. rt_uint32_t recved;
  55. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  56. if (result != RT_EOK)
  57. {
  58. return -RT_ETIMEOUT;
  59. }
  60. return recved;
  61. }
  62. /**
  63. * close socket by AT commands.
  64. *
  65. * @param current socket
  66. *
  67. * @return 0: close socket success
  68. * -1: send AT commands error
  69. * -2: wait socket event timeout
  70. * -5: no memory
  71. */
  72. static int n58_socket_close(struct at_socket *socket)
  73. {
  74. uint32_t event = 0;
  75. int result = RT_EOK;
  76. int device_socket = (int)socket->user_data;
  77. enum at_socket_type type_socket = socket->type;
  78. struct at_device *device = (struct at_device *)socket->device;
  79. /* clear socket close event */
  80. event = SET_EVENT(device_socket, N58_EVNET_CLOSE_OK);
  81. n58_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  82. if (type_socket == AT_SOCKET_TCP)
  83. {
  84. if (at_obj_exec_cmd(device->client, NULL, "AT+TCPCLOSE=%d", device_socket) < 0)
  85. {
  86. result = -RT_ERROR;
  87. goto __exit;
  88. }
  89. }
  90. else if (type_socket == AT_SOCKET_UDP)
  91. {
  92. if (at_obj_exec_cmd(device->client, NULL, "AT+UDPCLOSE=%d", device_socket) < 0)
  93. {
  94. result = -RT_ERROR;
  95. goto __exit;
  96. }
  97. }
  98. if (n58_socket_event_recv(device, event, rt_tick_from_millisecond(300 * 3), RT_EVENT_FLAG_AND) < 0)
  99. {
  100. LOG_E("n58 device(%s) socket(%d) close failed, wait close OK timeout.", device->name, device_socket);
  101. result = -RT_ETIMEOUT;
  102. goto __exit;
  103. }
  104. __exit:
  105. return result;
  106. }
  107. /**
  108. * create TCP/UDP client or server connect by AT commands.
  109. *
  110. * @param socket current socket
  111. * @param ip server or client IP address
  112. * @param port server or client port
  113. * @param type connect socket type(tcp, udp)
  114. * @param is_client connection is client
  115. *
  116. * @return 0: connect success
  117. * -1: connect failed, send commands error or type error
  118. * -2: wait socket event timeout
  119. * -5: no memory
  120. */
  121. static int n58_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  122. {
  123. uint32_t event = 0;
  124. rt_bool_t retryed = RT_FALSE;
  125. at_response_t resp = RT_NULL;
  126. int result = RT_EOK, event_result = 0;
  127. int device_socket = (int)socket->user_data;
  128. struct at_device *device = (struct at_device *)socket->device;
  129. RT_ASSERT(ip);
  130. RT_ASSERT(port >= 0);
  131. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  132. if (resp == RT_NULL)
  133. {
  134. LOG_E("no memory for n58 device(%s) response structure.", device->name);
  135. return -RT_ENOMEM;
  136. }
  137. __retry:
  138. /* clear socket connect event */
  139. event = SET_EVENT(device_socket, N58_EVENT_CONN_OK | N58_EVENT_CONN_FAIL);
  140. n58_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  141. if (is_client)
  142. {
  143. switch (type)
  144. {
  145. case AT_SOCKET_TCP:
  146. /* send AT commands(eg: AT+TCPSETUP=<n>,<ip>,<port>) to connect TCP server */
  147. if (at_obj_exec_cmd(device->client, RT_NULL,
  148. "AT+TCPSETUP=%d,%s,%d", device_socket, ip, port) < 0)
  149. {
  150. result = -RT_ERROR;
  151. goto __exit;
  152. }
  153. break;
  154. /* send AT commands(eg: AT+UDPSETUP=<n>,<ip>,<port>) to connect TCP server */
  155. case AT_SOCKET_UDP:
  156. if (at_obj_exec_cmd(device->client, RT_NULL,
  157. "AT+UDPSETUP=%d,%s,%d", device_socket, ip, port) < 0)
  158. {
  159. result = -RT_ERROR;
  160. goto __exit;
  161. }
  162. break;
  163. default:
  164. LOG_E("n58 device(%s) not supported connect type : %d.", device->name, type);
  165. result = -RT_ERROR;
  166. goto __exit;
  167. }
  168. }
  169. /* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use */
  170. if (n58_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  171. {
  172. LOG_E("n58 device(%s) socket(%d) connect failed, wait connect result timeout.", device->name, device_socket);
  173. result = -RT_ETIMEOUT;
  174. goto __exit;
  175. }
  176. /* waiting OK or failed result */
  177. event_result = n58_socket_event_recv(device,
  178. N58_EVENT_CONN_OK | N58_EVENT_CONN_FAIL, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  179. if (event_result < 0)
  180. {
  181. LOG_E("n58 device(%s) socket(%d) connect failed, wait connect OK|FAIL timeout.", device->name, device_socket);
  182. result = -RT_ETIMEOUT;
  183. goto __exit;
  184. }
  185. /* check result */
  186. if (event_result & N58_EVENT_CONN_FAIL)
  187. {
  188. if (retryed == RT_FALSE)
  189. {
  190. LOG_D("n58 device(%s) socket(%d) connect failed, maybe the socket was not be closed at the last time and now will retry.",
  191. device->name, device_socket);
  192. if (n58_socket_close(socket) < 0)
  193. {
  194. result = -RT_ERROR;
  195. goto __exit;
  196. }
  197. retryed = RT_TRUE;
  198. goto __retry;
  199. }
  200. LOG_E("n58 device(%s) socket(%d) connect failed.", device->name, device_socket);
  201. result = -RT_ERROR;
  202. goto __exit;
  203. }
  204. __exit:
  205. if (resp)
  206. {
  207. at_delete_resp(resp);
  208. }
  209. return result;
  210. }
  211. /**
  212. * send data to server or client by AT commands.
  213. *
  214. * @param socket current socket
  215. * @param buff send buffer
  216. * @param bfsz send buffer size
  217. * @param type connect socket type(tcp, udp)
  218. *
  219. * @return >=0: the size of send success
  220. * -1: send AT commands error or send data error
  221. * -2: waited socket event timeout
  222. * -5: no memory
  223. */
  224. static int n58_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  225. {
  226. uint32_t event = 0;
  227. int result = RT_EOK, event_result = 0;
  228. size_t cur_pkt_size = 0, sent_size = 0;
  229. at_response_t resp = RT_NULL;
  230. int device_socket = (int)socket->user_data;
  231. struct at_device *device = (struct at_device *)socket->device;
  232. rt_mutex_t lock = device->client->lock;
  233. RT_ASSERT(buff);
  234. resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
  235. if (resp == RT_NULL)
  236. {
  237. LOG_E("no memory for n58 device(%s) response structure.", device->name);
  238. return -RT_ENOMEM;
  239. }
  240. rt_mutex_take(lock, RT_WAITING_FOREVER);
  241. /* clear socket connect event */
  242. event = SET_EVENT(device_socket, N58_EVENT_SEND_OK | N58_EVENT_SEND_FAIL);
  243. n58_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  244. /* set AT client end sign to deal with '>' sign.*/
  245. at_obj_set_end_sign(device->client, '>');
  246. while (sent_size < bfsz)
  247. {
  248. if (bfsz - sent_size < N58_MODULE_SEND_MAX_SIZE)
  249. {
  250. cur_pkt_size = bfsz - sent_size;
  251. }
  252. else
  253. {
  254. cur_pkt_size = N58_MODULE_SEND_MAX_SIZE;
  255. }
  256. /* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
  257. if (type == AT_SOCKET_TCP)
  258. {
  259. if (at_obj_exec_cmd(device->client, resp, "AT+TCPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
  260. {
  261. result = -RT_ERROR;
  262. goto __exit;
  263. }
  264. }
  265. else if (type == AT_SOCKET_UDP)
  266. {
  267. if (at_obj_exec_cmd(device->client, resp, "AT+UDPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
  268. {
  269. result = -RT_ERROR;
  270. goto __exit;
  271. }
  272. }
  273. /* send the real data to server or client */
  274. result = (int)at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  275. if (result == 0)
  276. {
  277. result = -RT_ERROR;
  278. goto __exit;
  279. }
  280. /* waiting result event from AT URC */
  281. if (n58_socket_event_recv(device, SET_EVENT(device_socket, 0), 15 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  282. {
  283. LOG_E("n58 device(%s) socket(%d) send failed, wait connect result timeout.", device->name, device_socket);
  284. result = -RT_ETIMEOUT;
  285. goto __exit;
  286. }
  287. /* waiting OK or failed result */
  288. event_result = n58_socket_event_recv(device,
  289. N58_EVENT_SEND_OK | N58_EVENT_SEND_FAIL, 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  290. if (event_result < 0)
  291. {
  292. LOG_E("n58 device(%s) socket(%d) send failed, wait connect OK|FAIL timeout.", device->name, device_socket);
  293. result = -RT_ETIMEOUT;
  294. goto __exit;
  295. }
  296. /* check result */
  297. if (event_result & N58_EVENT_SEND_FAIL)
  298. {
  299. LOG_E("n58 device(%s) socket(%d) send failed.", device->name, device_socket);
  300. result = -RT_ERROR;
  301. goto __exit;
  302. }
  303. sent_size += cur_pkt_size;
  304. }
  305. __exit:
  306. /* reset the end sign for data conflict */
  307. at_obj_set_end_sign(device->client, 0);
  308. rt_mutex_release(lock);
  309. if (resp)
  310. {
  311. at_delete_resp(resp);
  312. }
  313. return result;
  314. }
  315. /**
  316. * domain resolve by AT commands.
  317. *
  318. * @param name domain name
  319. * @param ip parsed IP address, it's length must be 16
  320. *
  321. * @return 0: domain resolve success
  322. * -1: send AT commands error or response error
  323. * -2: wait socket event timeout
  324. * -5: no memory
  325. */
  326. static int n58_domain_resolve(const char *name, char ip[16])
  327. {
  328. #define RESOLVE_RETRY 5
  329. int i, result = RT_EOK;
  330. char recv_ip[16] = {0};
  331. at_response_t resp = RT_NULL;
  332. struct at_device *device = RT_NULL;
  333. RT_ASSERT(name);
  334. RT_ASSERT(ip);
  335. device = at_device_get_first_initialized();
  336. if (device == RT_NULL)
  337. {
  338. LOG_E("get first initialization n58 device failed.");
  339. return -RT_ERROR;
  340. }
  341. /* The maximum response time is 14 seconds, affected by network status */
  342. resp = at_create_resp(128, 4, 14 * RT_TICK_PER_SECOND);
  343. if (resp == RT_NULL)
  344. {
  345. LOG_E("no memory for n58 device(%s) response structure.", device->name);
  346. return -RT_ENOMEM;
  347. }
  348. for (i = 0; i < RESOLVE_RETRY; i++)
  349. {
  350. int err_code = 0;
  351. if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%s\"", name) < 0)
  352. {
  353. result = -RT_ERROR;
  354. goto __exit;
  355. }
  356. /* domain name prase error options */
  357. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP: 0", "+CDNSGIP: 0,%d", &err_code) > 0)
  358. {
  359. /* 3 - network error, 8 - dns common error */
  360. if (err_code == 3 || err_code == 8)
  361. {
  362. result = -RT_ERROR;
  363. goto __exit;
  364. }
  365. }
  366. /* parse the third line of response data, get the IP address */
  367. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", recv_ip) < 0)
  368. {
  369. rt_thread_mdelay(100);
  370. /* resolve failed, maybe receive an URC CRLF */
  371. continue;
  372. }
  373. if (rt_strlen(recv_ip) < 8)
  374. {
  375. rt_thread_mdelay(100);
  376. /* resolve failed, maybe receive an URC CRLF */
  377. continue;
  378. }
  379. else
  380. {
  381. rt_thread_mdelay(10);
  382. rt_strncpy(ip, recv_ip, 15);
  383. ip[15] = '\0';
  384. break;
  385. }
  386. }
  387. __exit:
  388. if (resp)
  389. {
  390. at_delete_resp(resp);
  391. }
  392. return result;
  393. }
  394. /**
  395. * set AT socket event notice callback
  396. *
  397. * @param event notice event
  398. * @param cb notice callback
  399. */
  400. static void n58_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  401. {
  402. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  403. {
  404. at_evt_cb_set[event] = cb;
  405. }
  406. }
  407. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  408. {
  409. int device_socket = 0;
  410. struct at_device *device = RT_NULL;
  411. char *client_name = client->device->parent.name;
  412. char constat[16] = {0};
  413. RT_ASSERT(data && size);
  414. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  415. if (device == RT_NULL)
  416. {
  417. LOG_E("get n58 device by client name(%s) failed.", client_name);
  418. return;
  419. }
  420. /* get the current socket by receive data */
  421. sscanf(data, "%*[^ ]%d,%s", &device_socket, constat);
  422. LOG_D("data:%s", data);
  423. LOG_D("socket:%d,constat:%s", device_socket, constat);
  424. if (strstr(constat, "OK"))
  425. {
  426. LOG_D("socket %d:connect ok!", device_socket);
  427. n58_socket_event_send(device, SET_EVENT(device_socket, N58_EVENT_CONN_OK));
  428. }
  429. else if (strstr(constat, "FAIL"))
  430. {
  431. LOG_D("socket %d:connect fail!", device_socket);
  432. n58_socket_event_send(device, SET_EVENT(device_socket, N58_EVENT_CONN_FAIL));
  433. }
  434. }
  435. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  436. {
  437. int device_socket = 0;
  438. struct at_device *device = RT_NULL;
  439. char *client_name = client->device->parent.name;
  440. RT_ASSERT(data && size);
  441. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  442. if (device == RT_NULL)
  443. {
  444. LOG_E("get n58 device by client name(%s) failed.", client_name);
  445. return;
  446. }
  447. /* get the current socket by receive data */
  448. sscanf(data, "%*[^ ] %d,%*d\r\n", &device_socket);
  449. if (rt_strstr(data, "OPERATION"))
  450. {
  451. LOG_E("input data timeout!");
  452. n58_socket_event_send(device, SET_EVENT(device_socket, N58_EVENT_SEND_FAIL));
  453. }
  454. else if (rt_strstr(data, "ERROR")) //链路号错误
  455. {
  456. n58_socket_event_send(device, SET_EVENT(device_socket, N58_EVENT_SEND_FAIL));
  457. }
  458. else //没有错误就是成功
  459. {
  460. n58_socket_event_send(device, SET_EVENT(device_socket, N58_EVENT_SEND_OK));
  461. }
  462. }
  463. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  464. {
  465. int device_socket = 0;
  466. struct at_device *device = RT_NULL;
  467. char *client_name = client->device->parent.name;
  468. RT_ASSERT(data && size);
  469. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  470. if (device == RT_NULL)
  471. {
  472. LOG_E("get n58 device by client name(%s) failed.", client_name);
  473. return;
  474. }
  475. /* get the current socket by receive data */
  476. /* +TCPCLOSE: 1,OK */
  477. sscanf(data, "%*[^ ]%d,%*s", &device_socket);
  478. if (rt_strstr(data, "OK"))
  479. {
  480. n58_socket_event_send(device, SET_EVENT(device_socket, N58_EVNET_CLOSE_OK));
  481. }
  482. else if (rt_strstr(data, "Link Closed"))
  483. {
  484. struct at_socket *socket = RT_NULL;
  485. /* get AT socket object by device socket descriptor */
  486. socket = &(device->sockets[device_socket]);
  487. /* notice the socket is disconnect by remote */
  488. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  489. {
  490. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  491. }
  492. }
  493. }
  494. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  495. {
  496. int device_socket = 0;
  497. rt_size_t bfsz = 0;
  498. int data_index = size - 1;
  499. char *recv_buf = RT_NULL;
  500. struct at_socket *socket = RT_NULL;
  501. struct at_device *device = RT_NULL;
  502. char *client_name = client->device->parent.name;
  503. RT_ASSERT(data && size);
  504. /* get the current socket and receive buffer size by receive data */
  505. sscanf(data, "%*[^ ] %d,%d,", &device_socket, (int *)&bfsz);
  506. recv_buf = (char *)rt_calloc(1, bfsz + 1);
  507. if (recv_buf == RT_NULL)
  508. {
  509. LOG_E("no memory for n58 device(%s) URC receive buffer (%d).", device->name, bfsz);
  510. return;
  511. }
  512. data_index -= 2; //"\r\n"移除
  513. recv_buf[bfsz] = '\0';
  514. for (int i = bfsz - 1; i >= 0; i--)
  515. {
  516. recv_buf[i] = data[data_index];
  517. data_index--;
  518. }
  519. /* get receive timeout by receive buffer length */
  520. LOG_D("recv socket:%d", device_socket);
  521. if (device_socket < 0 || bfsz == 0)
  522. {
  523. return;
  524. }
  525. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  526. if (device == RT_NULL)
  527. {
  528. LOG_E("get n58 device by client name(%s) failed.", client_name);
  529. return;
  530. }
  531. /* get AT socket object by device socket descriptor */
  532. socket = &(device->sockets[device_socket]);
  533. /* notice the receive buffer and buffer size */
  534. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  535. {
  536. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  537. }
  538. }
  539. /* n58 device URC table for the socket data */
  540. static const struct at_urc urc_table[] =
  541. {
  542. {"+TCPSETUP:", "\r\n", urc_connect_func},
  543. {"+UDPSETUP:", "\r\n", urc_connect_func},
  544. {"+TCPSEND:", "\r\n", urc_send_func},
  545. {"+UDPSEND:", "\r\n", urc_send_func},
  546. {"+TCPCLOSE:", "\r\n", urc_close_func},
  547. {"+UDPCLOSE:", "\r\n", urc_close_func},
  548. {"+TCPRECV:", "\r\n", urc_recv_func},
  549. {"+UDPRECV:", "\r\n", urc_recv_func},
  550. };
  551. static const struct at_socket_ops n58_socket_ops =
  552. {
  553. n58_socket_connect,
  554. n58_socket_close,
  555. n58_socket_send,
  556. n58_domain_resolve,
  557. n58_socket_set_event_cb,
  558. #if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
  559. RT_NULL,
  560. #endif
  561. };
  562. int n58_socket_init(struct at_device *device)
  563. {
  564. RT_ASSERT(device);
  565. /* register URC data execution function */
  566. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  567. return RT_EOK;
  568. }
  569. int n58_socket_class_register(struct at_device_class *class)
  570. {
  571. RT_ASSERT(class);
  572. class->socket_num = AT_DEVICE_N58_SOCKETS_NUM;
  573. class->socket_ops = &n58_socket_ops;
  574. return RT_EOK;
  575. }
  576. #endif /* AT_DEVICE_USING_n58 && AT_USING_SOCKET */