at_socket_m26.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * File : at_socket_m26.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, 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. * 2018-06-12 chenyong first version
  23. * 2019-05-12 chenyong multi AT socket client support
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <at_device_m26.h>
  28. #define LOG_TAG "at.skt.m26"
  29. #include <at_log.h>
  30. #if defined(AT_DEVICE_USING_M26) && defined(AT_USING_SOCKET)
  31. #define M26_MODULE_SEND_MAX_SIZE 1460
  32. /* set real event by current socket and current state */
  33. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  34. /* AT socket event type */
  35. #define M26_EVENT_CONN_OK (1L << 0)
  36. #define M26_EVENT_SEND_OK (1L << 1)
  37. #define M26_EVENT_RECV_OK (1L << 2)
  38. #define M26_EVNET_CLOSE_OK (1L << 3)
  39. #define M26_EVENT_CONN_FAIL (1L << 4)
  40. #define M26_EVENT_SEND_FAIL (1L << 5)
  41. static at_evt_cb_t at_evt_cb_set[] =
  42. {
  43. [AT_SOCKET_EVT_RECV] = NULL,
  44. [AT_SOCKET_EVT_CLOSED] = NULL,
  45. };
  46. static int m26_socket_event_send(struct at_device *device, uint32_t event)
  47. {
  48. return (int) rt_event_send(device->socket_event, event);
  49. }
  50. static int m26_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  51. {
  52. int result = 0;
  53. rt_uint32_t recved = 0;
  54. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  55. if (result != RT_EOK)
  56. {
  57. return -RT_ETIMEOUT;
  58. }
  59. return recved;
  60. }
  61. /**
  62. * close socket by AT commands.
  63. *
  64. * @param current socket
  65. *
  66. * @return 0: close socket success
  67. * -1: send AT commands error
  68. * -2: wait socket event timeout
  69. * -5: no memory
  70. */
  71. static int m26_socket_close(struct at_socket *socket)
  72. {
  73. int result = 0;
  74. at_response_t resp = RT_NULL;
  75. int device_socke = (int) socket->user_data;
  76. struct at_device *device = (struct at_device *) socket->device;
  77. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  78. if (resp == RT_NULL)
  79. {
  80. LOG_E("no memory for resp create.", device->name);
  81. return -RT_ENOMEM;
  82. }
  83. /* clear socket close event */
  84. m26_socket_event_recv(device, SET_EVENT(device_socke, M26_EVNET_CLOSE_OK), 0, RT_EVENT_FLAG_OR);
  85. if (at_obj_exec_cmd(device->client, resp, "AT+QICLOSE=%d", device_socke) < 0)
  86. {
  87. result = -RT_ERROR;
  88. goto __exit;
  89. }
  90. if (m26_socket_event_recv(device, SET_EVENT(device_socke, M26_EVNET_CLOSE_OK),
  91. rt_tick_from_millisecond(300 * 3), RT_EVENT_FLAG_AND) < 0)
  92. {
  93. LOG_E("%s device socket(%d) close failed, wait close OK timeout.", device->name, device_socke);
  94. result = -RT_ETIMEOUT;
  95. goto __exit;
  96. }
  97. __exit:
  98. if (resp)
  99. {
  100. at_delete_resp(resp);
  101. }
  102. return result;
  103. }
  104. /**
  105. * create TCP/UDP client or server connect by AT commands.
  106. *
  107. * @param socket current socket
  108. * @param ip server or client IP address
  109. * @param port server or client port
  110. * @param type connect socket type(tcp, udp)
  111. * @param is_client connection is client
  112. *
  113. * @return 0: connect success
  114. * -1: connect failed, send commands error or type error
  115. * -2: wait socket event timeout
  116. * -5: no memory
  117. */
  118. static int m26_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  119. {
  120. rt_bool_t retryed = RT_FALSE;
  121. at_response_t resp = RT_NULL;
  122. int result = 0, event_result = 0;
  123. int device_socket = (int) socket->user_data;
  124. struct at_device *device = (struct at_device *) socket->device;
  125. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  126. if (resp == RT_NULL)
  127. {
  128. LOG_E("no memory for resp create.");
  129. return -RT_ENOMEM;
  130. }
  131. RT_ASSERT(ip);
  132. RT_ASSERT(port >= 0);
  133. __retry:
  134. /* clear socket connect event */
  135. event_result = SET_EVENT(device_socket, M26_EVENT_CONN_OK | M26_EVENT_CONN_FAIL);
  136. m26_socket_event_recv(device, event_result, 0, RT_EVENT_FLAG_OR);
  137. if (is_client)
  138. {
  139. switch (type)
  140. {
  141. case AT_SOCKET_TCP:
  142. /* send AT commands(eg: AT+QIOPEN=0,"TCP","x.x.x.x", 1234) to connect TCP server */
  143. if (at_obj_exec_cmd(device->client, resp,
  144. "AT+QIOPEN=%d,\"TCP\",\"%s\",%d", device_socket, ip, port) < 0)
  145. {
  146. result = -RT_ERROR;
  147. goto __exit;
  148. }
  149. break;
  150. case AT_SOCKET_UDP:
  151. if (at_obj_exec_cmd(device->client, resp,
  152. "AT+QIOPEN=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
  153. {
  154. result = -RT_ERROR;
  155. goto __exit;
  156. }
  157. break;
  158. default:
  159. LOG_E("%s device not supported connect type : %d.", device->name, type);
  160. return -RT_ERROR;
  161. }
  162. }
  163. /* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use.*/
  164. if (m26_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  165. {
  166. LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
  167. result = -RT_ETIMEOUT;
  168. goto __exit;
  169. }
  170. /* waiting OK or failed result */
  171. if ((event_result = m26_socket_event_recv(device, M26_EVENT_CONN_OK | M26_EVENT_CONN_FAIL,
  172. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR)) < 0)
  173. {
  174. LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
  175. result = -RT_ETIMEOUT;
  176. goto __exit;
  177. }
  178. /* check result */
  179. if (event_result & M26_EVENT_CONN_FAIL)
  180. {
  181. if (retryed == RT_FALSE)
  182. {
  183. LOG_D("%s device socket(%d) connect failed, the socket was not be closed and now will retry.",
  184. device->name, device_socket);
  185. if (m26_socket_close(socket) < 0)
  186. {
  187. goto __exit;
  188. }
  189. retryed = RT_TRUE;
  190. goto __retry;
  191. }
  192. LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
  193. result = -RT_ERROR;
  194. goto __exit;
  195. }
  196. __exit:
  197. if (resp)
  198. {
  199. at_delete_resp(resp);
  200. }
  201. return result;
  202. }
  203. static int at_get_send_size(struct at_socket *socket, size_t *size, size_t *acked, size_t *nacked)
  204. {
  205. int result = 0;
  206. at_response_t resp = RT_NULL;
  207. int device_socket = (int) socket->user_data;
  208. struct at_device *device = (struct at_device *) socket->device;
  209. resp = at_create_resp(64, 0, 5 * RT_TICK_PER_SECOND);
  210. if (resp == RT_NULL)
  211. {
  212. LOG_E("no memory for resp create.");
  213. result = -RT_ENOMEM;
  214. goto __exit;
  215. }
  216. if (at_obj_exec_cmd(device->client, resp, "AT+QISACK=%d", device_socket) < 0)
  217. {
  218. result = -RT_ERROR;
  219. goto __exit;
  220. }
  221. if (at_resp_parse_line_args_by_kw(resp, "+QISACK:", "+QISACK: %d, %d, %d", size, acked, nacked) <= 0)
  222. {
  223. LOG_E("%s device prase \"AT+QISACK\" cmd error.", device->name);
  224. result = -RT_ERROR;
  225. goto __exit;
  226. }
  227. __exit:
  228. if (resp)
  229. {
  230. at_delete_resp(resp);
  231. }
  232. return result;
  233. }
  234. static int at_wait_send_finish(struct at_socket *socket, size_t settings_size)
  235. {
  236. /* get the timeout by the input data size */
  237. rt_tick_t timeout = rt_tick_from_millisecond(settings_size);
  238. rt_tick_t last_time = rt_tick_get();
  239. size_t size = 0, acked = 0, nacked = 0xFFFF;
  240. while (rt_tick_get() - last_time <= timeout)
  241. {
  242. at_get_send_size(socket, &size, &acked, &nacked);
  243. if (nacked == 0)
  244. {
  245. return RT_EOK;
  246. }
  247. rt_thread_mdelay(50);
  248. }
  249. return -RT_ETIMEOUT;
  250. }
  251. /**
  252. * send data to server or client by AT commands.
  253. *
  254. * @param socket current socket
  255. * @param buff send buffer
  256. * @param bfsz send buffer size
  257. * @param type connect socket type(tcp, udp)
  258. *
  259. * @return >=0: the size of send success
  260. * -1: send AT commands error or send data error
  261. * -2: waited socket event timeout
  262. * -5: no memory
  263. */
  264. static int m26_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  265. {
  266. int result = 0, event_result = 0;
  267. size_t pkt_size = 0, sent_size = 0;
  268. at_response_t resp = RT_NULL;
  269. int device_socket = (int) socket->user_data;
  270. struct at_device *device = (struct at_device *) socket->device;
  271. struct at_device_m26 *m26 = (struct at_device_m26 *) device->user_data;
  272. rt_mutex_t lock = device->client->lock;
  273. RT_ASSERT(buff);
  274. resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
  275. if (resp == RT_NULL)
  276. {
  277. LOG_E("no memory for resp create.");
  278. return -RT_ENOMEM;
  279. }
  280. rt_mutex_take(lock, RT_WAITING_FOREVER);
  281. /* Clear socket send event */
  282. event_result = SET_EVENT(device_socket, M26_EVENT_SEND_OK | M26_EVENT_SEND_FAIL);
  283. m26_socket_event_recv(device, event_result, 0, RT_EVENT_FLAG_OR);
  284. /* set current socket for send URC event */
  285. m26->user_data = (void *) device_socket;
  286. /* set AT client end sign to deal with '>' sign.*/
  287. at_obj_set_end_sign(device->client, '>');
  288. while (sent_size < bfsz)
  289. {
  290. if (bfsz - sent_size < M26_MODULE_SEND_MAX_SIZE)
  291. {
  292. pkt_size = bfsz - sent_size;
  293. }
  294. else
  295. {
  296. pkt_size = M26_MODULE_SEND_MAX_SIZE;
  297. }
  298. /* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
  299. if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,%d", device_socket, pkt_size) < 0)
  300. {
  301. result = -RT_ERROR;
  302. goto __exit;
  303. }
  304. /* send the real data to server or client */
  305. result = (int) at_client_obj_send(device->client, buff + sent_size, pkt_size);
  306. if (result == 0)
  307. {
  308. result = -RT_ERROR;
  309. goto __exit;
  310. }
  311. /* waiting result event from AT URC */
  312. if (m26_socket_event_recv(device, SET_EVENT(device_socket, 0), 15 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  313. {
  314. LOG_E("%s device socket(%d) wait send result timeout.", device->name, device_socket);
  315. result = -RT_ETIMEOUT;
  316. goto __exit;
  317. }
  318. /* waiting OK or failed result */
  319. if ((event_result = m26_socket_event_recv(device, M26_EVENT_SEND_OK | M26_EVENT_SEND_FAIL,
  320. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR)) < 0)
  321. {
  322. LOG_E("%s device socket(%d) wait send OK|FAIL timeout.", device->name, device_socket);
  323. result = -RT_ETIMEOUT;
  324. goto __exit;
  325. }
  326. /* check result */
  327. if (event_result & M26_EVENT_SEND_FAIL)
  328. {
  329. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  330. result = -RT_ERROR;
  331. goto __exit;
  332. }
  333. if (type == AT_SOCKET_TCP)
  334. {
  335. at_wait_send_finish(socket, pkt_size);
  336. }
  337. sent_size += pkt_size;
  338. }
  339. __exit:
  340. /* reset the end sign for data conflict */
  341. at_obj_set_end_sign(device->client, 0);
  342. rt_mutex_release(lock);
  343. if (resp)
  344. {
  345. at_delete_resp(resp);
  346. }
  347. return result > 0 ? sent_size : result;
  348. }
  349. /**
  350. * domain resolve by AT commands.
  351. *
  352. * @param name domain name
  353. * @param ip parsed IP address, it's length must be 16
  354. *
  355. * @return 0: domain resolve success
  356. * -1: send AT commands error or response error
  357. * -2: wait socket event timeout
  358. * -5: no memory
  359. */
  360. static int m26_domain_resolve(const char *name, char ip[16])
  361. {
  362. #define RESOLVE_RETRY 5
  363. int i, result = RT_EOK;
  364. char recv_ip[16] = { 0 };
  365. at_response_t resp = RT_NULL;
  366. struct at_device *device = RT_NULL;
  367. RT_ASSERT(name);
  368. RT_ASSERT(ip);
  369. device = at_device_get_first_initialized();
  370. if (device == RT_NULL)
  371. {
  372. LOG_E("get first init device failed.");
  373. return -RT_ERROR;
  374. }
  375. /* The maximum response time is 14 seconds, affected by network status */
  376. resp = at_create_resp(128, 4, 14 * RT_TICK_PER_SECOND);
  377. if (resp == RT_NULL)
  378. {
  379. LOG_E("no memory for resp create.");
  380. return -RT_ENOMEM;
  381. }
  382. for(i = 0; i < RESOLVE_RETRY; i++)
  383. {
  384. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSGIP=\"%s\"", name) < 0)
  385. {
  386. result = -RT_ERROR;
  387. goto __exit;
  388. }
  389. /* parse the third line of response data, get the IP address */
  390. if(at_resp_parse_line_args_by_kw(resp, ".", "%s", recv_ip) < 0)
  391. {
  392. rt_thread_mdelay(100);
  393. /* resolve failed, maybe receive an URC CRLF */
  394. continue;
  395. }
  396. if (rt_strlen(recv_ip) < 8)
  397. {
  398. rt_thread_mdelay(100);
  399. /* resolve failed, maybe receive an URC CRLF */
  400. continue;
  401. }
  402. else
  403. {
  404. rt_thread_mdelay(10);
  405. rt_strncpy(ip, recv_ip, 15);
  406. ip[15] = '\0';
  407. break;
  408. }
  409. }
  410. __exit:
  411. if (resp)
  412. {
  413. at_delete_resp(resp);
  414. }
  415. return result;
  416. }
  417. /**
  418. * set AT socket event notice callback
  419. *
  420. * @param event notice event
  421. * @param cb notice callback
  422. */
  423. static void m26_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  424. {
  425. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  426. {
  427. at_evt_cb_set[event] = cb;
  428. }
  429. }
  430. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  431. {
  432. int device_socket = 0;
  433. struct at_device *device = RT_NULL;
  434. char *client_name = client->device->parent.name;
  435. RT_ASSERT(data && size);
  436. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  437. if (device == RT_NULL)
  438. {
  439. LOG_E("get device(%s) failed.", client_name);
  440. return;
  441. }
  442. sscanf(data, "%d%*[^0-9]", &device_socket);
  443. if (rt_strstr(data, "CONNECT OK"))
  444. {
  445. m26_socket_event_send(device, SET_EVENT(device_socket, M26_EVENT_CONN_OK));
  446. }
  447. else
  448. {
  449. m26_socket_event_send(device, SET_EVENT(device_socket, M26_EVENT_CONN_FAIL));
  450. }
  451. }
  452. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  453. {
  454. int device_socket = 0;
  455. struct at_device *device = RT_NULL;
  456. struct at_device_m26 *m26 = RT_NULL;
  457. char *client_name = client->device->parent.name;
  458. RT_ASSERT(data && size);
  459. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  460. if (device == RT_NULL)
  461. {
  462. LOG_E("get device(%s) failed.", client_name);
  463. return;
  464. }
  465. m26 = (struct at_device_m26 *) device->user_data;
  466. device_socket = (int) m26->user_data;
  467. if (rt_strstr(data, "SEND OK"))
  468. {
  469. m26_socket_event_send(device, SET_EVENT(device_socket, M26_EVENT_SEND_OK));
  470. }
  471. else if (rt_strstr(data, "SEND FAIL"))
  472. {
  473. m26_socket_event_send(device, SET_EVENT(device_socket, M26_EVENT_SEND_FAIL));
  474. }
  475. }
  476. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  477. {
  478. int device_socket = 0;
  479. struct at_device *device = RT_NULL;
  480. char *client_name = client->device->parent.name;
  481. RT_ASSERT(data && size);
  482. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  483. if (device == RT_NULL)
  484. {
  485. LOG_E("get device(%s) failed.", client_name);
  486. return;
  487. }
  488. sscanf(data, "%d%*s", &device_socket);
  489. if (rt_strstr(data, "CLOSE OK"))
  490. {
  491. m26_socket_event_send(device, SET_EVENT(device_socket, M26_EVNET_CLOSE_OK));
  492. }
  493. else if (rt_strstr(data, "CLOSED"))
  494. {
  495. struct at_socket *socket = RT_NULL;
  496. /* get at socket object by device socket descriptor */
  497. socket = &(device->sockets[device_socket]);
  498. /* notice the socket is disconnect by remote */
  499. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  500. {
  501. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  502. }
  503. }
  504. }
  505. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  506. {
  507. int device_socket = 0;
  508. rt_int32_t timeout;
  509. rt_size_t bfsz = 0, temp_size = 0;
  510. char *recv_buf = RT_NULL, temp[8] = {0};
  511. struct at_socket *socket = RT_NULL;
  512. struct at_device *device = RT_NULL;
  513. char *client_name = client->device->parent.name;
  514. RT_ASSERT(data && size);
  515. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  516. if (device == RT_NULL)
  517. {
  518. LOG_E("get device(%s) failed.", client_name);
  519. return;
  520. }
  521. /* get the current socket and receive buffer size by receive data */
  522. sscanf(data, "+RECEIVE: %d, %d", &device_socket, (int *) &bfsz);
  523. /* set receive timeout by receive buffer length, not less than 10 ms */
  524. timeout = bfsz > 10 ? bfsz : 10;
  525. if (device_socket < 0 || bfsz == 0)
  526. {
  527. return;
  528. }
  529. recv_buf = (char *) rt_calloc(1, bfsz);
  530. if (recv_buf == RT_NULL)
  531. {
  532. LOG_E("no memory for receive buffer (%d).", device->name, bfsz);
  533. /* read and clean the coming data */
  534. while (temp_size < bfsz)
  535. {
  536. if (bfsz - temp_size > sizeof(temp))
  537. {
  538. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  539. }
  540. else
  541. {
  542. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  543. }
  544. temp_size += sizeof(temp);
  545. }
  546. return;
  547. }
  548. /* sync receive data */
  549. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  550. {
  551. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  552. rt_free(recv_buf);
  553. return;
  554. }
  555. /* get at socket object by device socket descriptor */
  556. socket = &(device->sockets[device_socket]);
  557. /* notice the receive buffer and buffer size */
  558. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  559. {
  560. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  561. }
  562. }
  563. static const struct at_urc urc_table[] =
  564. {
  565. {"", ", CONNECT OK\r\n", urc_connect_func},
  566. {"", ", CONNECT FAIL\r\n", urc_connect_func},
  567. {"SEND OK", "\r\n", urc_send_func},
  568. {"SEND FAIL", "\r\n", urc_send_func},
  569. {"", ", CLOSE OK\r\n", urc_close_func},
  570. {"", ", CLOSED\r\n", urc_close_func},
  571. {"+RECEIVE:", "\r\n", urc_recv_func},
  572. };
  573. static const struct at_socket_ops m26_socket_ops =
  574. {
  575. m26_socket_connect,
  576. m26_socket_close,
  577. m26_socket_send,
  578. m26_domain_resolve,
  579. m26_socket_set_event_cb,
  580. };
  581. int m26_socket_init(struct at_device *device)
  582. {
  583. RT_ASSERT(device);
  584. /* register URC data execution function */
  585. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  586. return RT_EOK;
  587. }
  588. int m26_socket_class_register(struct at_device_class *class)
  589. {
  590. RT_ASSERT(class);
  591. class->socket_num = AT_DEVICE_M26_SOCKETS_NUM;
  592. class->socket_ops = &m26_socket_ops;
  593. return RT_EOK;
  594. }
  595. #endif /* AT_DEVICE_USING_M26 && AT_USING_SOCKET */