at_socket_bc28.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * File : at_socket_bc28.c
  3. * This file is part of RT-Thread RTOS
  4. * Copyright (c) 2020, RudyLo <luhuadong@163.com>
  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-02-13 luhuadong first version
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <at_device_bc28.h>
  27. #define LOG_TAG "at.skt.bc28"
  28. #include <at_log.h>
  29. #if defined(AT_DEVICE_USING_BC28) && defined(AT_USING_SOCKET)
  30. #define BC28_MODULE_SEND_MAX_SIZE 1358
  31. #define BC28_MODULE_RECV_MAX_SIZE 1358
  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 BC28_EVENT_CONN_OK (1L << 0)
  36. #define BC28_EVENT_SEND_OK (1L << 1)
  37. #define BC28_EVENT_RECV_OK (1L << 2)
  38. #define BC28_EVNET_CLOSE_OK (1L << 3)
  39. #define BC28_EVENT_CONN_FAIL (1L << 4)
  40. #define BC28_EVENT_SEND_FAIL (1L << 5)
  41. #define BC28_EVENT_DOMAIN_OK (1L << 6)
  42. #define BC28_EVENT_DOMAIN_FAIL (1L << 7)
  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 struct at_socket_ip_info
  48. {
  49. char ip_addr[IP_ADDR_SIZE_MAX];
  50. int port;
  51. } bc28_sock_info[AT_DEVICE_BC28_SOCKETS_NUM];
  52. /**
  53. * convert data from ASCII string to Hex string.
  54. *
  55. * @param str input ASCII string
  56. * @param hex output Hex string
  57. * @param len length of str, or the size you want to convert
  58. *
  59. * @return =0: convert failed, or no data need to convert
  60. * >0: the size of convert success
  61. */
  62. static int string_to_hex(const char *str, char *hex, const rt_size_t len)
  63. {
  64. RT_ASSERT(str && hex);
  65. int str_len = rt_strlen(str);
  66. int pos = 0, i;
  67. if (len < 1 || str_len < len)
  68. {
  69. return 0;
  70. }
  71. for (i = 0; i < len; i++, pos += 2)
  72. {
  73. rt_sprintf(&hex[pos], "%02X", str[i]);
  74. }
  75. return i;
  76. }
  77. /**
  78. * convert data from Hex string to ASCII string.
  79. *
  80. * @param hex input Hex string
  81. * @param str output ASCII string
  82. * @param len length of str, or the size you want to convert
  83. *
  84. * @return =0: convert failed, or no data need to convert
  85. * >0: the size of convert success
  86. */
  87. static int hex_to_string(const char *hex, char *str, const rt_size_t len)
  88. {
  89. RT_ASSERT(hex && str);
  90. int hex_len = rt_strlen(hex);
  91. int pos = 0, left, right, i;
  92. if (len < 1 || hex_len/2 < len)
  93. {
  94. return 0;
  95. }
  96. for (i = 0; i < len*2; i++, pos++)
  97. {
  98. left = hex[i++];
  99. right = hex[i];
  100. left = (left < 58) ? (left - 48) : (left - 55);
  101. right = (right < 58) ? (right - 48) : (right - 55);
  102. str[pos] = (left << 4) | right;
  103. }
  104. return pos;
  105. }
  106. static void at_tcp_ip_errcode_parse(int result)//TCP/IP_QIGETERROR
  107. {
  108. switch(result)
  109. {
  110. case 0 : LOG_D("%d : Operation successful", result); break;
  111. case 550 : LOG_E("%d : Unknown error", result); break;
  112. case 551 : LOG_E("%d : Operation blocked", result); break;
  113. case 552 : LOG_E("%d : Invalid parameters", result); break;
  114. case 553 : LOG_E("%d : Memory not enough", result); break;
  115. case 554 : LOG_E("%d : Create socket failed", result); break;
  116. case 555 : LOG_E("%d : Operation not supported", result); break;
  117. case 556 : LOG_E("%d : Socket bind failed", result); break;
  118. case 557 : LOG_E("%d : Socket listen failed", result); break;
  119. case 558 : LOG_E("%d : Socket write failed", result); break;
  120. case 559 : LOG_E("%d : Socket read failed", result); break;
  121. case 560 : LOG_E("%d : Socket accept failed", result); break;
  122. case 561 : LOG_E("%d : Open PDP context failed", result); break;
  123. case 562 : LOG_E("%d : Close PDP context failed", result); break;
  124. case 563 : LOG_W("%d : Socket identity has been used", result); break;
  125. case 564 : LOG_E("%d : DNS busy", result); break;
  126. case 565 : LOG_E("%d : DNS parse failed", result); break;
  127. case 566 : LOG_E("%d : Socket connect failed", result); break;
  128. // case 567 : LOG_W("%d : Socket has been closed", result); break;
  129. case 567 : break;
  130. case 568 : LOG_E("%d : Operation busy", result); break;
  131. case 569 : LOG_E("%d : Operation timeout", result); break;
  132. case 570 : LOG_E("%d : PDP context broken down", result); break;
  133. case 571 : LOG_E("%d : Cancel send", result); break;
  134. case 572 : LOG_E("%d : Operation not allowed", result); break;
  135. case 573 : LOG_E("%d : APN not configured", result); break;
  136. case 574 : LOG_E("%d : Port busy", result); break;
  137. default : LOG_E("%d : Unknown err code", result); break;
  138. }
  139. }
  140. static int bc28_socket_event_send(struct at_device *device, uint32_t event)
  141. {
  142. return (int) rt_event_send(device->socket_event, event);
  143. }
  144. static int bc28_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  145. {
  146. int result = RT_EOK;
  147. rt_uint32_t recved;
  148. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  149. if (result != RT_EOK)
  150. {
  151. return -RT_ETIMEOUT;
  152. }
  153. return recved;
  154. }
  155. /**
  156. * close socket by AT commands.
  157. *
  158. * @param current socket
  159. *
  160. * @return 0: close socket success
  161. * -1: send AT commands error
  162. * -2: wait socket event timeout
  163. * -5: no memory
  164. */
  165. static int bc28_socket_close(struct at_socket *socket)
  166. {
  167. int result = RT_EOK;
  168. at_response_t resp = RT_NULL;
  169. int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
  170. struct at_device *device = (struct at_device *) socket->device;
  171. resp = at_create_resp(64, 0, rt_tick_from_millisecond(3000));
  172. if (resp == RT_NULL)
  173. {
  174. LOG_E("no memory for resp create.");
  175. return -RT_ENOMEM;
  176. }
  177. result = at_obj_exec_cmd(device->client, resp, "AT+NSOCL=%d", device_socket);
  178. if (result < 0)
  179. {
  180. LOG_E("%s device close socket(%d) failed [%d].", device->name, device_socket, result);
  181. }
  182. else
  183. {
  184. LOG_D("%s device close socket(%d).", device->name, device_socket);
  185. }
  186. at_delete_resp(resp);
  187. return result;
  188. }
  189. /**
  190. * create socket by AT commands.
  191. *
  192. * @param type connect socket type(tcp, udp)
  193. * @param port listen port (range: 0-65535), if 0 means get a random port
  194. *
  195. * @return >=0: create socket success, return the socket id (0-6)
  196. * -1: send or exec AT commands error
  197. * -5: no memory
  198. */
  199. static int bc28_socket_create(struct at_device *device, enum at_socket_type type, uint32_t port)
  200. {
  201. const char *type_str = RT_NULL;
  202. uint32_t protocol = 0;
  203. at_response_t resp = RT_NULL;
  204. int socket = -1, result = 0;
  205. switch(type)
  206. {
  207. case AT_SOCKET_TCP:
  208. type_str = "STREAM";
  209. protocol = 6;
  210. break;
  211. case AT_SOCKET_UDP:
  212. type_str = "DGRAM";
  213. protocol = 17;
  214. break;
  215. default:
  216. return -RT_ERROR;
  217. }
  218. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  219. if (resp == RT_NULL)
  220. {
  221. LOG_E("no memory for resp create.");
  222. return -RT_ENOMEM;
  223. }
  224. /* create socket */
  225. if (at_obj_exec_cmd(device->client, resp, "AT+NSOCR=%s,%d,%d,1", type_str, protocol, port) < 0)
  226. {
  227. result = -RT_ERROR;
  228. goto __exit;
  229. }
  230. /* check socket */
  231. if (at_resp_parse_line_args(resp, 2, "%d", &socket) <= 0)
  232. {
  233. LOG_E("%s device create %s socket failed.", device->name, type_str);
  234. result = -RT_ERROR;
  235. goto __exit;
  236. }
  237. else
  238. {
  239. LOG_D("%s device create a %s socket(%d).", device->name, type_str, socket);
  240. result = socket;
  241. }
  242. __exit:
  243. if (resp)
  244. {
  245. at_delete_resp(resp);
  246. }
  247. return result;
  248. }
  249. /**
  250. * create TCP/UDP client or server connect by AT commands.
  251. *
  252. * @param socket current socket
  253. * @param ip server or client IP address
  254. * @param port server or client port
  255. * @param type connect socket type(tcp, udp)
  256. * @param is_client connection is client
  257. *
  258. * @return 0: connect success
  259. * -1: connect failed, send commands error or type error
  260. * -2: wait socket event timeout
  261. * -5: no memory
  262. */
  263. static int bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
  264. enum at_socket_type type, rt_bool_t is_client)
  265. {
  266. #define CONN_RETRY 3
  267. int i = 0;
  268. uint32_t event = 0;
  269. at_response_t resp = RT_NULL;
  270. int result = 0, event_result = 0;
  271. int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
  272. int return_socket = -1;
  273. struct at_device *device = (struct at_device *) socket->device;
  274. RT_ASSERT(ip);
  275. RT_ASSERT(port >= 0);
  276. if (!is_client)
  277. {
  278. return -RT_ERROR;
  279. }
  280. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  281. if (resp == RT_NULL)
  282. {
  283. LOG_E("no memory for resp create.");
  284. return -RT_ENOMEM;
  285. }
  286. return_socket = bc28_socket_create(device, type, 0);
  287. if (return_socket != device_socket)
  288. {
  289. LOG_E("socket not match (request %d, return %d).", device_socket, return_socket);
  290. result = at_obj_exec_cmd(device->client, resp, "AT+NSOCL=%d", return_socket);
  291. if (result < 0)
  292. {
  293. LOG_E("%s device close socket(%d) failed [%d].", device->name, return_socket, result);
  294. }
  295. else
  296. {
  297. LOG_D("%s device close socket(%d).", device->name, return_socket);
  298. /* notice the socket is disconnect by remote */
  299. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  300. {
  301. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  302. }
  303. }
  304. result = -RT_ERROR;
  305. goto __exit;
  306. }
  307. /* if the protocol is not tcp, no need connect to server */
  308. if (type != AT_SOCKET_TCP)
  309. {
  310. if (type == AT_SOCKET_UDP)
  311. {
  312. rt_strncpy(bc28_sock_info[device_socket].ip_addr, ip, IP_ADDR_SIZE_MAX);
  313. bc28_sock_info[device_socket].port = port;
  314. }
  315. return RT_EOK;
  316. }
  317. for(i=0; i<CONN_RETRY; i++)
  318. {
  319. /* clear socket connect event */
  320. event = SET_EVENT(device_socket, BC28_EVENT_CONN_OK | BC28_EVENT_CONN_FAIL);
  321. bc28_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  322. if (at_obj_exec_cmd(device->client, resp, "AT+NSOCO=%d,%s,%d", device_socket, ip, port) < 0)
  323. {
  324. result = -RT_ERROR;
  325. continue;
  326. }
  327. LOG_D("%s device socket(%d) try connect to %s:%d.", device->name, device_socket, ip, port);
  328. if(!at_resp_get_line_by_kw(resp, "OK"))
  329. {
  330. result = -RT_ERROR;
  331. continue;
  332. }
  333. /* waiting result event from AT URC, the device default connection timeout is 30 seconds*/
  334. if (bc28_socket_event_recv(device, SET_EVENT(device_socket, 0),
  335. 30 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  336. {
  337. LOG_D("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
  338. /* No news is good news */
  339. result = RT_EOK;
  340. break;
  341. }
  342. /* waiting OK or failed result */
  343. event_result = bc28_socket_event_recv(device, BC28_EVENT_CONN_OK | BC28_EVENT_CONN_FAIL,
  344. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  345. if (event_result & BC28_EVENT_CONN_FAIL)
  346. {
  347. LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
  348. result = -RT_ERROR;
  349. continue;
  350. }
  351. else
  352. {
  353. result = RT_EOK;
  354. break;
  355. }
  356. }
  357. __exit:
  358. if (resp)
  359. {
  360. at_delete_resp(resp);
  361. }
  362. return result;
  363. }
  364. /**
  365. * send data to server or client by AT commands.
  366. *
  367. * @param socket current socket
  368. * @param buff send buffer
  369. * @param bfsz send buffer size
  370. * @param type connect socket type(tcp, udp)
  371. *
  372. * @return >=0: the size of send success
  373. * -1: send AT commands error or send data error
  374. * -2: waited socket event timeout
  375. * -5: no memory
  376. */
  377. static int bc28_socket_send(struct at_socket *socket, const char *buff,
  378. size_t bfsz, enum at_socket_type type)
  379. {
  380. uint32_t event = 0;
  381. int result = 0, event_result = 0;
  382. size_t cur_pkt_size = 0, sent_size = 0;
  383. at_response_t resp = RT_NULL;
  384. int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
  385. struct at_device *device = (struct at_device *) socket->device;
  386. struct at_device_bc28 *bc28 = (struct at_device_bc28 *) device->user_data;
  387. rt_mutex_t lock = device->client->lock;
  388. RT_ASSERT(buff);
  389. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  390. if (resp == RT_NULL)
  391. {
  392. LOG_E("no memory for resp create.");
  393. return -RT_ENOMEM;
  394. }
  395. rt_mutex_take(lock, RT_WAITING_FOREVER);
  396. /* clear socket send event */
  397. event = SET_EVENT(device_socket, BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL);
  398. bc28_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  399. /* only use for UDP socket */
  400. const char *ip = bc28_sock_info[device_socket].ip_addr;
  401. const int port = bc28_sock_info[device_socket].port;
  402. while (sent_size < bfsz)
  403. {
  404. if (bfsz - sent_size < BC28_MODULE_SEND_MAX_SIZE)
  405. {
  406. cur_pkt_size = bfsz - sent_size;
  407. }
  408. else
  409. {
  410. cur_pkt_size = BC28_MODULE_SEND_MAX_SIZE;
  411. }
  412. size_t i = 0, ind = 0;
  413. char hex_data[cur_pkt_size * 2 + 1];
  414. rt_memset(hex_data, 0, sizeof(hex_data));
  415. for (i=0, ind=0; i<cur_pkt_size; i++, ind+=2)
  416. {
  417. sprintf(&hex_data[ind], "%02X", buff[sent_size + i]);
  418. }
  419. switch (type)
  420. {
  421. case AT_SOCKET_TCP:
  422. /* AT+NSOSD=<socket>,<length>,<data>[,<flag>[,<sequence>]] */
  423. if (at_obj_exec_cmd(device->client, resp, "AT+NSOSD=%d,%d,%s,0x100,1", device_socket,
  424. (int)cur_pkt_size, hex_data) < 0)
  425. {
  426. result = -RT_ERROR;
  427. goto __exit;
  428. }
  429. LOG_D("%s device tcp socket(%d) send %d bytes.\n>> %s", device->name, device_socket, (int)cur_pkt_size, hex_data);
  430. break;
  431. case AT_SOCKET_UDP:
  432. /* AT+NSOST=<socket>,<remote_addr>,<remote_port>,<length>,<data>[,<sequence>] */
  433. if (at_obj_exec_cmd(device->client, resp, "AT+NSOST=%d,%s,%d,%d,%s,1", device_socket,
  434. ip, port, (int)cur_pkt_size, hex_data) < 0)
  435. {
  436. result = -RT_ERROR;
  437. goto __exit;
  438. }
  439. 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);
  440. break;
  441. default:
  442. LOG_E("not supported send type %d.", type);
  443. result = -RT_ERROR;
  444. goto __exit;
  445. }
  446. /* check if sent ok */
  447. if (!at_resp_get_line_by_kw(resp, "OK"))
  448. {
  449. LOG_E("%s device socket(%d) send data failed.", device->name, device_socket);
  450. result = -RT_ERROR;
  451. goto __exit;
  452. }
  453. int return_socket = -1, return_size = -1;
  454. if (at_resp_parse_line_args(resp, 2, "%d,%d", &return_socket, &return_size) <= 0)
  455. {
  456. LOG_E("%s device socket(%d) send data failed.", device->name, device_socket);
  457. result = -RT_ERROR;
  458. goto __exit;
  459. }
  460. if (return_socket != device_socket || return_size != cur_pkt_size)
  461. {
  462. LOG_E("%s device socket(%d) send data incompletely.", device->name, device_socket);
  463. result = -RT_ERROR;
  464. goto __exit;
  465. }
  466. /* waiting result event from AT URC, the device default timeout is 60 seconds*/
  467. if (bc28_socket_event_recv(device, SET_EVENT(device_socket, 0),
  468. 60 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  469. {
  470. LOG_E("%s device socket(%d) wait send result timeout.", device->name, device_socket);
  471. result = -RT_ETIMEOUT;
  472. goto __exit;
  473. }
  474. /* waiting OK or failed result */
  475. event_result = bc28_socket_event_recv(device, BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL,
  476. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  477. if (event_result & BC28_EVENT_SEND_FAIL)
  478. {
  479. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  480. result = -RT_ERROR;
  481. goto __exit;
  482. }
  483. else
  484. {
  485. LOG_D("%s device socket(%d) send success.", device->name, device_socket);
  486. sent_size += cur_pkt_size;
  487. result = sent_size;
  488. }
  489. }
  490. __exit:
  491. rt_mutex_release(lock);
  492. if (resp)
  493. {
  494. at_delete_resp(resp);
  495. }
  496. return result > 0 ? sent_size : result;
  497. }
  498. /**
  499. * domain resolve by AT commands.
  500. *
  501. * @param name domain name
  502. * @param ip parsed IP address, it's length must be 16
  503. *
  504. * @return 0: domain resolve success
  505. * -1: send AT commands error or response error
  506. * -2: wait socket event timeout
  507. * -5: no memory
  508. */
  509. int bc28_domain_resolve(const char *name, char ip[16])
  510. {
  511. #define RESOLVE_RETRY 1
  512. int i, result, event_result = 0;
  513. at_response_t resp = RT_NULL;
  514. struct at_device *device = RT_NULL;
  515. struct at_device_bc28 *bc28 = RT_NULL;
  516. RT_ASSERT(name);
  517. RT_ASSERT(ip);
  518. device = at_device_get_first_initialized();
  519. if (device == RT_NULL)
  520. {
  521. LOG_E("get first init device failed.");
  522. return -RT_ERROR;
  523. }
  524. /* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
  525. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  526. if (!resp)
  527. {
  528. LOG_E("no memory for resp create.");
  529. return -RT_ENOMEM;
  530. }
  531. /* clear BC28_EVENT_DOMAIN_OK */
  532. bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
  533. bc28 = (struct at_device_bc28 *) device->user_data;
  534. bc28->socket_data = ip;
  535. if (at_obj_exec_cmd(device->client, resp, "AT+QDNS=0,%s", name) < 0)
  536. {
  537. result = -RT_ERROR;
  538. goto __exit;
  539. }
  540. for(i = 0; i < RESOLVE_RETRY; i++)
  541. {
  542. /* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
  543. event_result = bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK | BC28_EVENT_DOMAIN_FAIL,
  544. 30 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  545. if (event_result < 0)
  546. {
  547. result = -RT_ETIMEOUT;
  548. continue;
  549. }
  550. else if (event_result & BC28_EVENT_DOMAIN_FAIL)
  551. {
  552. LOG_E("%d device resolve domain name failed.", device->name);
  553. result = -RT_ERROR;
  554. continue;
  555. }
  556. else
  557. {
  558. result = RT_EOK;
  559. break;
  560. }
  561. }
  562. __exit:
  563. bc28->socket_data = RT_NULL;
  564. if (resp)
  565. {
  566. at_delete_resp(resp);
  567. }
  568. return result;
  569. }
  570. /**
  571. * set AT socket event notice callback
  572. *
  573. * @param event notice event
  574. * @param cb notice callback
  575. */
  576. static void bc28_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  577. {
  578. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  579. {
  580. at_evt_cb_set[event] = cb;
  581. }
  582. }
  583. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  584. {
  585. int device_socket = 0, result = 0;
  586. struct at_device *device = RT_NULL;
  587. char *client_name = client->device->parent.name;
  588. RT_ASSERT(data && size);
  589. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  590. if (device == RT_NULL)
  591. {
  592. LOG_E("get device(%s) failed.", client_name);
  593. return;
  594. }
  595. /* only for firmware version base BC28JAR02xxx */
  596. sscanf(data, "+QTCPIND: %d,%d", &device_socket , &result);
  597. if (result == 0)
  598. {
  599. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_OK));
  600. }
  601. else
  602. {
  603. at_tcp_ip_errcode_parse(result);
  604. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));
  605. }
  606. }
  607. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  608. {
  609. int device_socket = 0, sequence = 0, status = 0;
  610. struct at_device *device = RT_NULL;
  611. struct at_device_bc28 *bc28 = RT_NULL;
  612. char *client_name = client->device->parent.name;
  613. RT_ASSERT(data && size);
  614. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  615. if (device == RT_NULL)
  616. {
  617. LOG_E("get device(%s) failed.", client_name);
  618. return;
  619. }
  620. sscanf(data, "+NSOSTR:%d,%d,%d", &device_socket, &sequence, &status);
  621. if (1 == status)
  622. {
  623. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_OK));
  624. }
  625. else
  626. {
  627. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_FAIL));
  628. }
  629. }
  630. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  631. {
  632. int device_socket = 0;
  633. struct at_socket *socket = RT_NULL;
  634. struct at_device *device = RT_NULL;
  635. char *client_name = client->device->parent.name;
  636. RT_ASSERT(data && size);
  637. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  638. if (device == RT_NULL)
  639. {
  640. LOG_E("get device(%s) failed.", client_name);
  641. return;
  642. }
  643. sscanf(data, "+NSOCLI: %d", &device_socket);
  644. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));
  645. if (device_socket - AT_DEVICE_BC28_MIN_SOCKET >= 0)
  646. {
  647. /* get at socket object by device socket descriptor */
  648. socket = &(device->sockets[device_socket - AT_DEVICE_BC28_MIN_SOCKET]);
  649. /* notice the socket is disconnect by remote */
  650. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  651. {
  652. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  653. }
  654. }
  655. }
  656. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  657. {
  658. int device_socket = 0;
  659. rt_int32_t timeout;
  660. rt_size_t bfsz = 0, temp_size = 0;
  661. char *recv_buf = RT_NULL, *hex_buf = RT_NULL, temp[8] = {0};
  662. char remote_addr[IP_ADDR_SIZE_MAX] = {0};
  663. int remote_port = -1, return_socket = -1, data_length = 0, remaining_length = 0;
  664. struct at_socket *socket = RT_NULL;
  665. struct at_device *device = RT_NULL;
  666. char *client_name = client->device->parent.name;
  667. RT_ASSERT(data && size);
  668. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  669. if (device == RT_NULL)
  670. {
  671. LOG_E("get device(%s) failed.", client_name);
  672. return;
  673. }
  674. hex_buf = (char *) rt_calloc(1, BC28_MODULE_RECV_MAX_SIZE * 2 + 1);
  675. /* get the current socket and receive buffer size by receive data */
  676. /* mode 2 => +NSONMI:<socket>,<remote_addr>, <remote_port>,<length>,<data> */
  677. sscanf(data, "+NSONMI:%d,%[0-9.],%d,%d,%s", &device_socket, remote_addr, &remote_port, (int *) &bfsz, hex_buf);
  678. LOG_D("%s device socket(%d) recv %d bytes from %s:%d\n>> %s", device_socket, bfsz, remote_addr, remote_port, hex_buf);
  679. /* set receive timeout by receive buffer length, not less than 10 ms */
  680. timeout = bfsz > 10 ? bfsz : 10;
  681. if (device_socket < 0 || bfsz == 0)
  682. {
  683. return;
  684. }
  685. recv_buf = (char *) rt_calloc(1, bfsz + 1);
  686. if (recv_buf == RT_NULL || hex_buf == RT_NULL)
  687. {
  688. LOG_E("no memory for URC receive buffer(%d).", bfsz);
  689. /* read and clean the coming data */
  690. while (temp_size < bfsz)
  691. {
  692. if (bfsz - temp_size > sizeof(temp))
  693. {
  694. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  695. }
  696. else
  697. {
  698. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  699. }
  700. temp_size += sizeof(temp);
  701. }
  702. if (recv_buf) rt_free(recv_buf);
  703. if (hex_buf) rt_free(hex_buf);
  704. return;
  705. }
  706. /* convert receive data */
  707. hex_to_string(hex_buf, recv_buf, bfsz);
  708. rt_free(hex_buf);
  709. /* get at socket object by device socket descriptor */
  710. socket = &(device->sockets[device_socket - AT_DEVICE_BC28_MIN_SOCKET]);
  711. /* notice the receive buffer and buffer size */
  712. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  713. {
  714. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  715. }
  716. }
  717. static void urc_dns_func(struct at_client *client, const char *data, rt_size_t size)
  718. {
  719. int i = 0, j = 0;
  720. char recv_ip[16] = {0};
  721. int result, ip_count, dns_ttl;
  722. struct at_device *device = RT_NULL;
  723. struct at_device_bc28 *bc28 = RT_NULL;
  724. char *client_name = client->device->parent.name;
  725. RT_ASSERT(data && size);
  726. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  727. if (device == RT_NULL)
  728. {
  729. LOG_E("get device(%s) failed.", client_name);
  730. return;
  731. }
  732. bc28 = (struct at_device_bc28 *) device->user_data;
  733. if (bc28->socket_data == RT_NULL)
  734. {
  735. LOG_D("%s device socket_data no config.", bc28->device_name);
  736. return;
  737. }
  738. sscanf(data, "+QDNS:%s", recv_ip);
  739. recv_ip[15] = '\0';
  740. if (rt_strstr(recv_ip, "FAIL"))
  741. {
  742. bc28_socket_event_send(device, BC28_EVENT_DOMAIN_FAIL);
  743. }
  744. else
  745. {
  746. rt_memcpy(bc28->socket_data, recv_ip, sizeof(recv_ip));
  747. bc28_socket_event_send(device, BC28_EVENT_DOMAIN_OK);
  748. }
  749. }
  750. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  751. {
  752. RT_ASSERT(data);
  753. LOG_I("URC data : %.*s", size, data);
  754. }
  755. /* +NSOSTR:<socket>,<sequence>,<status> */
  756. static const struct at_urc urc_table[] =
  757. {
  758. {"+QDNS:", "\r\n", urc_dns_func},
  759. {"+QTCPIND:", "\r\n", urc_connect_func},
  760. {"+NSOSTR:", "\r\n", urc_send_func},
  761. {"+NSONMI:", "\r\n", urc_recv_func},
  762. {"+NSOCLI:", "\r\n", urc_close_func},
  763. };
  764. static const struct at_socket_ops bc28_socket_ops =
  765. {
  766. bc28_socket_connect,
  767. bc28_socket_close,
  768. bc28_socket_send,
  769. bc28_domain_resolve,
  770. bc28_socket_set_event_cb,
  771. };
  772. int bc28_socket_init(struct at_device *device)
  773. {
  774. RT_ASSERT(device);
  775. /* register URC data execution function */
  776. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  777. return RT_EOK;
  778. }
  779. int bc28_socket_class_register(struct at_device_class *class)
  780. {
  781. RT_ASSERT(class);
  782. class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM - AT_DEVICE_BC28_MIN_SOCKET;
  783. class->socket_ops = &bc28_socket_ops;
  784. return RT_EOK;
  785. }
  786. #endif /* AT_DEVICE_USING_BC28 && AT_USING_SOCKET */