at_socket_bc28.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. * 2020-02-13 luhuadong first version
  9. * 2020-07-19 luhuadong support alloc socket
  10. * 2022-11-9 wangcheng support bc28 low version firmware
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <at_device_bc28.h>
  15. #if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10301
  16. #error "This AT Client version is older, please check and update latest AT Client!"
  17. #endif
  18. #define LOG_TAG "at.skt.bc28"
  19. #include <at_log.h>
  20. #if defined(AT_DEVICE_USING_BC28) && defined(AT_USING_SOCKET)
  21. #define BC28_MODULE_SEND_MAX_SIZE 1358
  22. #define BC28_MODULE_RECV_MAX_SIZE 1358
  23. /* set real event by current socket and current state */
  24. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  25. /* AT socket event type */
  26. #define BC28_EVENT_CONN_OK (1L << 0)
  27. #define BC28_EVENT_SEND_OK (1L << 1)
  28. #define BC28_EVENT_RECV_OK (1L << 2)
  29. #define BC28_EVNET_CLOSE_OK (1L << 3)
  30. #define BC28_EVENT_CONN_FAIL (1L << 4)
  31. #define BC28_EVENT_SEND_FAIL (1L << 5)
  32. #define BC28_EVENT_DOMAIN_OK (1L << 6)
  33. #define BC28_EVENT_DOMAIN_FAIL (1L << 7)
  34. static at_evt_cb_t at_evt_cb_set[] = {
  35. [AT_SOCKET_EVT_RECV] = NULL,
  36. [AT_SOCKET_EVT_CLOSED] = NULL,
  37. };
  38. static struct at_socket_ip_info
  39. {
  40. char ip_addr[IP_ADDR_SIZE_MAX];
  41. int port;
  42. } bc28_sock_info[AT_DEVICE_BC28_SOCKETS_NUM];
  43. /**
  44. * convert data from Hex string to ASCII string.
  45. *
  46. * @param hex input Hex string
  47. * @param str output ASCII string
  48. * @param len length of str, or the size you want to convert
  49. *
  50. * @return =0: convert failed, or no data need to convert
  51. * >0: the size of convert success
  52. */
  53. static int hex_to_string(const char *hex, char *str, const rt_size_t len)
  54. {
  55. RT_ASSERT(hex && str);
  56. int hex_len = rt_strlen(hex);
  57. int pos = 0, left, right, i;
  58. if (len < 1 || hex_len/2 < len)
  59. {
  60. return 0;
  61. }
  62. for (i = 0; i < len*2; i++, pos++)
  63. {
  64. left = hex[i++];
  65. right = hex[i];
  66. left = (left < 58) ? (left - 48) : (left - 55);
  67. right = (right < 58) ? (right - 48) : (right - 55);
  68. str[pos] = (left << 4) | right;
  69. }
  70. return pos;
  71. }
  72. static int bc28_socket_event_send(struct at_device *device, uint32_t event)
  73. {
  74. return (int) rt_event_send(device->socket_event, event);
  75. }
  76. static int bc28_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  77. {
  78. int result = RT_EOK;
  79. rt_uint32_t recved;
  80. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  81. if (result != RT_EOK)
  82. {
  83. return -RT_ETIMEOUT;
  84. }
  85. return recved;
  86. }
  87. /**
  88. * close socket by AT commands.
  89. *
  90. * @param current socket
  91. *
  92. * @return 0: close socket success
  93. * -1: send AT commands error
  94. * -2: wait socket event timeout
  95. * -5: no memory
  96. */
  97. static int bc28_socket_close(struct at_socket *socket)
  98. {
  99. int result = RT_EOK;
  100. at_response_t resp = RT_NULL;
  101. int device_socket = (int) socket->user_data;
  102. struct at_device *device = (struct at_device *) socket->device;
  103. resp = at_create_resp(64, 0, rt_tick_from_millisecond(3000));
  104. if (resp == RT_NULL)
  105. {
  106. LOG_E("no memory for resp create.");
  107. return -RT_ENOMEM;
  108. }
  109. result = at_obj_exec_cmd(device->client, resp, "AT+NSOCL=%d", device_socket);
  110. if (result < 0)
  111. {
  112. LOG_E("%s device close socket(%d) failed [%d].", device->name, device_socket, result);
  113. }
  114. else
  115. {
  116. LOG_D("%s device close socket(%d).", device->name, device_socket);
  117. }
  118. at_delete_resp(resp);
  119. return result;
  120. }
  121. /**
  122. * create socket by AT commands.
  123. *
  124. * @param type connect socket type(tcp, udp)
  125. *
  126. * @return >=0: create socket success, return the socket id (0-6)
  127. * -1: send or exec AT commands error
  128. * -5: no memory
  129. */
  130. static int bc28_socket_create(struct at_device *device, enum at_socket_type type)
  131. {
  132. const char *type_str = RT_NULL;
  133. uint32_t protocol = 0;
  134. uint32_t port = 0; /* range: 0-65535, if 0 means get a random port */
  135. at_response_t resp = RT_NULL;
  136. int socket = -1, result = 0;
  137. switch(type)
  138. {
  139. case AT_SOCKET_TCP:
  140. type_str = "STREAM";
  141. protocol = 6;
  142. break;
  143. case AT_SOCKET_UDP:
  144. type_str = "DGRAM";
  145. protocol = 17;
  146. break;
  147. default:
  148. return -RT_ERROR;
  149. }
  150. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  151. if (resp == RT_NULL)
  152. {
  153. LOG_E("no memory for resp create.");
  154. return -RT_ENOMEM;
  155. }
  156. /* create socket */
  157. if (at_obj_exec_cmd(device->client, resp, "AT+NSOCR=%s,%d,%d,1", type_str, protocol, port) < 0)
  158. {
  159. result = -RT_ERROR;
  160. goto __exit;
  161. }
  162. /* check socket */
  163. if (at_resp_parse_line_args(resp, 2, "%d", &socket) <= 0)
  164. {
  165. LOG_E("%s device create %s socket failed.", device->name, type_str);
  166. result = -RT_ERROR;
  167. goto __exit;
  168. }
  169. else
  170. {
  171. LOG_D("%s device create a %s socket(%d).", device->name, type_str, socket);
  172. result = socket;
  173. }
  174. __exit:
  175. if (resp)
  176. {
  177. at_delete_resp(resp);
  178. }
  179. return result;
  180. }
  181. /**
  182. * create TCP/UDP client or server connect by AT commands.
  183. *
  184. * @param socket current socket
  185. * @param ip server or client IP address
  186. * @param port server or client port
  187. * @param type connect socket type(tcp, udp)
  188. * @param is_client connection is client
  189. *
  190. * @return 0: connect success
  191. * -1: connect failed, send commands error or type error
  192. * -2: wait socket event timeout
  193. * -5: no memory
  194. */
  195. static int bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
  196. enum at_socket_type type, rt_bool_t is_client)
  197. {
  198. #define CONN_RETRY 3
  199. int i = 0;
  200. at_response_t resp = RT_NULL;
  201. int result = 0;
  202. int device_socket = (int) socket->user_data;
  203. struct at_device *device = (struct at_device *) socket->device;
  204. RT_ASSERT(ip);
  205. RT_ASSERT(port >= 0);
  206. if (!is_client)
  207. {
  208. return -RT_ERROR;
  209. }
  210. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  211. if (resp == RT_NULL)
  212. {
  213. LOG_E("no memory for resp create.");
  214. return -RT_ENOMEM;
  215. }
  216. /* if the protocol is not tcp, no need connect to server */
  217. if (type != AT_SOCKET_TCP)
  218. {
  219. if (type == AT_SOCKET_UDP)
  220. {
  221. rt_strncpy(bc28_sock_info[device_socket].ip_addr, ip, IP_ADDR_SIZE_MAX);
  222. bc28_sock_info[device_socket].port = port;
  223. }
  224. return RT_EOK;
  225. }
  226. for(i=0; i<CONN_RETRY; i++)
  227. {
  228. if (at_obj_exec_cmd(device->client, resp, "AT+NSOCO=%d,%s,%d", device_socket, ip, port) < 0)
  229. {
  230. result = -RT_ERROR;
  231. continue;
  232. }
  233. LOG_D("%s device socket(%d) try connect to %s:%d.", device->name, device_socket, ip, port);
  234. if(!at_resp_get_line_by_kw(resp, "OK"))
  235. {
  236. result = -RT_ERROR;
  237. continue;
  238. }
  239. break;
  240. }
  241. if (resp)
  242. {
  243. at_delete_resp(resp);
  244. }
  245. return result;
  246. }
  247. /**
  248. * send data to server or client by AT commands.
  249. *
  250. * @param socket current socket
  251. * @param buff send buffer
  252. * @param bfsz send buffer size
  253. * @param type connect socket type(tcp, udp)
  254. *
  255. * @return >=0: the size of send success
  256. * -1: send AT commands error or send data error
  257. * -2: waited socket event timeout
  258. * -5: no memory
  259. */
  260. static int bc28_socket_send(struct at_socket *socket, const char *buff,
  261. size_t bfsz, enum at_socket_type type)
  262. {
  263. uint32_t event = 0;
  264. int result = 0, event_result = 0;
  265. size_t cur_pkt_size = 0, sent_size = 0;
  266. at_response_t resp = RT_NULL;
  267. int device_socket = (int) socket->user_data;
  268. struct at_device *device = (struct at_device *) socket->device;
  269. rt_mutex_t lock = device->client->lock;
  270. RT_ASSERT(buff);
  271. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  272. if (resp == RT_NULL)
  273. {
  274. LOG_E("no memory for resp create.");
  275. return -RT_ENOMEM;
  276. }
  277. rt_mutex_take(lock, RT_WAITING_FOREVER);
  278. /* clear socket send event */
  279. event = SET_EVENT(device_socket, BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL);
  280. bc28_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  281. /* only use for UDP socket */
  282. const char *ip = bc28_sock_info[device_socket].ip_addr;
  283. const int port = bc28_sock_info[device_socket].port;
  284. while (sent_size < bfsz)
  285. {
  286. if (bfsz - sent_size < BC28_MODULE_SEND_MAX_SIZE)
  287. {
  288. cur_pkt_size = bfsz - sent_size;
  289. }
  290. else
  291. {
  292. cur_pkt_size = BC28_MODULE_SEND_MAX_SIZE;
  293. }
  294. size_t i = 0, ind = 0;
  295. char hex_data[cur_pkt_size * 2 + 1];
  296. rt_memset(hex_data, 0, sizeof(hex_data));
  297. for (i=0, ind=0; i<cur_pkt_size; i++, ind+=2)
  298. {
  299. rt_sprintf(&hex_data[ind], "%02X", buff[sent_size + i]);
  300. }
  301. switch (type)
  302. {
  303. case AT_SOCKET_TCP:
  304. /* AT+NSOSD=<socket>,<length>,<data>[,<flag>[,<sequence>]] */
  305. if (at_obj_exec_cmd(device->client, resp, "AT+NSOSD=%d,%d,%s,0x100,1", device_socket,
  306. (int)cur_pkt_size, hex_data) < 0)
  307. {
  308. result = -RT_ERROR;
  309. goto __exit;
  310. }
  311. LOG_D("%s device tcp socket(%d) send %d bytes.\n>> %s", device->name, device_socket, (int)cur_pkt_size, hex_data);
  312. break;
  313. case AT_SOCKET_UDP:
  314. /* AT+NSOST=<socket>,<remote_addr>,<remote_port>,<length>,<data>[,<sequence>] */
  315. if (at_obj_exec_cmd(device->client, resp, "AT+NSOST=%d,%s,%d,%d,%s,1", device_socket,
  316. ip, port, (int)cur_pkt_size, hex_data) < 0)
  317. {
  318. result = -RT_ERROR;
  319. goto __exit;
  320. }
  321. LOG_D("%s device udp socket(%d) send %d bytes to %s:%d.\n>> %s", device->name, device_socket, ip, port, (int)cur_pkt_size, hex_data);
  322. break;
  323. default:
  324. LOG_E("not supported send type %d.", type);
  325. result = -RT_ERROR;
  326. goto __exit;
  327. }
  328. /* check if sent ok */
  329. if (!at_resp_get_line_by_kw(resp, "OK"))
  330. {
  331. LOG_E("%s device socket(%d) send data failed.", device->name, device_socket);
  332. result = -RT_ERROR;
  333. goto __exit;
  334. }
  335. /* waiting result event from AT URC, the device default timeout is 60 seconds*/
  336. if (bc28_socket_event_recv(device, SET_EVENT(device_socket, 0),
  337. 60 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  338. {
  339. LOG_E("%s device socket(%d) wait send result timeout.", device->name, device_socket);
  340. result = -RT_ETIMEOUT;
  341. goto __exit;
  342. }
  343. /* waiting OK or failed result */
  344. event_result = bc28_socket_event_recv(device, BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL,
  345. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  346. if (event_result & BC28_EVENT_SEND_FAIL)
  347. {
  348. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  349. result = -RT_ERROR;
  350. goto __exit;
  351. }
  352. else
  353. {
  354. LOG_D("%s device socket(%d) send success.", device->name, device_socket);
  355. sent_size += cur_pkt_size;
  356. result = sent_size;
  357. }
  358. }
  359. __exit:
  360. rt_mutex_release(lock);
  361. if (resp)
  362. {
  363. at_delete_resp(resp);
  364. }
  365. return result > 0 ? sent_size : result;
  366. }
  367. static struct rt_mutex dns_mutex;
  368. /**
  369. * domain resolve by AT commands.
  370. *
  371. * @param name domain name
  372. * @param ip parsed IP address, it's length must be 16
  373. *
  374. * @return 0: domain resolve success
  375. * -1: send AT commands error or response error
  376. * -2: wait socket event timeout
  377. * -5: no memory
  378. */
  379. int bc28_domain_resolve(const char *name, char ip[16])
  380. {
  381. #define RESOLVE_RETRY 1
  382. int i, result, event_result = 0;
  383. at_response_t resp = RT_NULL;
  384. struct at_device *device = RT_NULL;
  385. struct at_device_bc28 *bc28 = RT_NULL;
  386. RT_ASSERT(name);
  387. RT_ASSERT(ip);
  388. device = at_device_get_first_initialized();
  389. if (device == RT_NULL)
  390. {
  391. LOG_E("get first init device failed.");
  392. return -RT_ERROR;
  393. }
  394. /* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
  395. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  396. if (!resp)
  397. {
  398. LOG_E("no memory for resp create.");
  399. return -RT_ENOMEM;
  400. }
  401. rt_mutex_take(&dns_mutex, RT_WAITING_FOREVER);
  402. /* clear BC28_EVENT_DOMAIN_OK */
  403. bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
  404. bc28 = (struct at_device_bc28 *) device->user_data;
  405. bc28->socket_data = ip;
  406. if (at_obj_exec_cmd(device->client, resp, "AT+QDNS=0,%s", name) < 0)
  407. {
  408. result = -RT_ERROR;
  409. goto __exit;
  410. }
  411. for(i = 0; i < RESOLVE_RETRY; i++)
  412. {
  413. /* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
  414. event_result = bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK | BC28_EVENT_DOMAIN_FAIL,
  415. 30 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  416. if (event_result < 0)
  417. {
  418. result = -RT_ETIMEOUT;
  419. continue;
  420. }
  421. else if (event_result & BC28_EVENT_DOMAIN_FAIL)
  422. {
  423. LOG_E("%d device resolve domain name failed.", device->name);
  424. result = -RT_ERROR;
  425. continue;
  426. }
  427. else
  428. {
  429. result = RT_EOK;
  430. break;
  431. }
  432. }
  433. __exit:
  434. rt_mutex_release(&dns_mutex);
  435. bc28->socket_data = RT_NULL;
  436. if (resp)
  437. {
  438. at_delete_resp(resp);
  439. }
  440. return result;
  441. }
  442. /**
  443. * set AT socket event notice callback
  444. *
  445. * @param event notice event
  446. * @param cb notice callback
  447. */
  448. static void bc28_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  449. {
  450. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  451. {
  452. at_evt_cb_set[event] = cb;
  453. }
  454. }
  455. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  456. {
  457. int device_socket = 0, sequence = 0, status = 0;
  458. struct at_device *device = RT_NULL;
  459. char *client_name = client->device->parent.name;
  460. RT_ASSERT(data && size);
  461. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  462. if (device == RT_NULL)
  463. {
  464. LOG_E("get device(%s) failed.", client_name);
  465. return;
  466. }
  467. sscanf(data, "+NSOSTR:%d,%d,%d", &device_socket, &sequence, &status);
  468. if (1 == status)
  469. {
  470. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_OK));
  471. }
  472. else
  473. {
  474. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_FAIL));
  475. }
  476. }
  477. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  478. {
  479. int device_socket = 0;
  480. struct at_socket *socket = RT_NULL;
  481. struct at_device *device = RT_NULL;
  482. char *client_name = client->device->parent.name;
  483. RT_ASSERT(data && size);
  484. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  485. if (device == RT_NULL)
  486. {
  487. LOG_E("get device(%s) failed.", client_name);
  488. return;
  489. }
  490. sscanf(data, "+NSOCLI: %d", &device_socket);
  491. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));
  492. if (device_socket >= 0)
  493. {
  494. /* get at socket object by device socket descriptor */
  495. socket = &(device->sockets[device_socket]);
  496. /* notice the socket is disconnect by remote */
  497. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  498. {
  499. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  500. }
  501. }
  502. }
  503. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  504. {
  505. int device_socket = 0;
  506. rt_int32_t timeout;
  507. rt_size_t bfsz = 0, temp_size = 0;
  508. char *recv_buf = RT_NULL, *hex_buf = RT_NULL, temp[8] = {0};
  509. char remote_addr[IP_ADDR_SIZE_MAX] = {0};
  510. int remote_port = -1;
  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. hex_buf = (char *) rt_calloc(1, BC28_MODULE_RECV_MAX_SIZE * 2 + 1);
  522. /* get the current socket and receive buffer size by receive data */
  523. /* mode 2 => +NSONMI:<socket>,<remote_addr>, <remote_port>,<length>,<data> */
  524. sscanf(data, "+NSONMI:%d,%[0123456789.],%d,%d,%s", &device_socket, remote_addr, &remote_port, (int *) &bfsz, hex_buf);
  525. LOG_D("device socket(%d) recv %d bytes from %s:%d\n>> %s", device_socket, bfsz, remote_addr, remote_port, hex_buf);
  526. /* set receive timeout by receive buffer length, not less than 10 ms */
  527. timeout = bfsz > 10 ? bfsz : 10;
  528. if (device_socket < 0 || bfsz == 0)
  529. {
  530. return;
  531. }
  532. recv_buf = (char *) rt_calloc(1, bfsz + 1);
  533. if (recv_buf == RT_NULL || hex_buf == RT_NULL)
  534. {
  535. LOG_E("no memory for URC receive buffer(%d).", bfsz);
  536. /* read and clean the coming data */
  537. while (temp_size < bfsz)
  538. {
  539. if (bfsz - temp_size > sizeof(temp))
  540. {
  541. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  542. }
  543. else
  544. {
  545. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  546. }
  547. temp_size += sizeof(temp);
  548. }
  549. if (recv_buf) rt_free(recv_buf);
  550. if (hex_buf) rt_free(hex_buf);
  551. return;
  552. }
  553. /* convert receive data */
  554. hex_to_string(hex_buf, recv_buf, bfsz);
  555. rt_free(hex_buf);
  556. /* get at socket object by device socket descriptor */
  557. socket = &(device->sockets[device_socket]);
  558. /* notice the receive buffer and buffer size */
  559. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  560. {
  561. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  562. }
  563. }
  564. static void urc_dns_func(struct at_client *client, const char *data, rt_size_t size)
  565. {
  566. char recv_ip[16] = {0};
  567. struct at_device *device = RT_NULL;
  568. struct at_device_bc28 *bc28 = RT_NULL;
  569. char *client_name = client->device->parent.name;
  570. RT_ASSERT(data && size);
  571. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  572. if (device == RT_NULL)
  573. {
  574. LOG_E("get device(%s) failed.", client_name);
  575. return;
  576. }
  577. bc28 = (struct at_device_bc28 *) device->user_data;
  578. if (bc28->socket_data == RT_NULL)
  579. {
  580. LOG_D("%s device socket_data no config.", bc28->device_name);
  581. return;
  582. }
  583. sscanf(data, "+QDNS:%s", recv_ip);
  584. recv_ip[15] = '\0';
  585. if (rt_strstr(recv_ip, "FAIL"))
  586. {
  587. bc28_socket_event_send(device, BC28_EVENT_DOMAIN_FAIL);
  588. }
  589. else
  590. {
  591. rt_memcpy(bc28->socket_data, recv_ip, sizeof(recv_ip));
  592. bc28_socket_event_send(device, BC28_EVENT_DOMAIN_OK);
  593. }
  594. }
  595. /* +NSOSTR:<socket>,<sequence>,<status> */
  596. static const struct at_urc urc_table[] =
  597. {
  598. {"+QDNS:", "\r\n", urc_dns_func},
  599. {"+NSOSTR:", "\r\n", urc_send_func},
  600. {"+NSONMI:", "\r\n", urc_recv_func},
  601. {"+NSOCLI:", "\r\n", urc_close_func},
  602. };
  603. static const struct at_socket_ops bc28_socket_ops =
  604. {
  605. bc28_socket_connect,
  606. bc28_socket_close,
  607. bc28_socket_send,
  608. bc28_domain_resolve,
  609. bc28_socket_set_event_cb,
  610. #if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
  611. bc28_socket_create,
  612. #endif
  613. };
  614. int bc28_socket_init(struct at_device *device)
  615. {
  616. RT_ASSERT(device);
  617. /* register URC data execution function */
  618. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  619. return RT_EOK;
  620. }
  621. int bc28_socket_class_register(struct at_device_class *class)
  622. {
  623. RT_ASSERT(class);
  624. rt_mutex_init(&dns_mutex, "dns", RT_IPC_FLAG_PRIO);
  625. class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM;
  626. class->socket_ops = &bc28_socket_ops;
  627. return RT_EOK;
  628. }
  629. #endif /* AT_DEVICE_USING_BC28 && AT_USING_SOCKET */