at_socket_bc26.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * File : at_socket_bc26.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. * 2019-12-10 qiyongzhong first version
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <at_device_bc26.h>
  27. #define LOG_TAG "at.skt.bc26"
  28. #include <at_log.h>
  29. #if defined(AT_DEVICE_USING_BC26) && defined(AT_USING_SOCKET)
  30. #define BC26_MODULE_SEND_MAX_SIZE 1024
  31. /* set real event by current socket and current state */
  32. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  33. /* AT socket event type */
  34. #define BC26_EVENT_CONN_OK (1L << 0)
  35. #define BC26_EVENT_SEND_OK (1L << 1)
  36. #define BC26_EVENT_RECV_OK (1L << 2)
  37. #define BC26_EVNET_CLOSE_OK (1L << 3)
  38. #define BC26_EVENT_CONN_FAIL (1L << 4)
  39. #define BC26_EVENT_SEND_FAIL (1L << 5)
  40. #define BC26_EVENT_DOMAIN_OK (1L << 6)
  41. static at_evt_cb_t at_evt_cb_set[] = {
  42. [AT_SOCKET_EVT_RECV] = NULL,
  43. [AT_SOCKET_EVT_CLOSED] = NULL,
  44. };
  45. static void at_tcp_ip_errcode_parse(int result)//TCP/IP_QIGETERROR
  46. {
  47. switch(result)
  48. {
  49. case 0 : LOG_D("%d : Operation successful", result); break;
  50. case 550 : LOG_E("%d : Unknown error", result); break;
  51. case 551 : LOG_E("%d : Operation blocked", result); break;
  52. case 552 : LOG_E("%d : Invalid parameters", result); break;
  53. case 553 : LOG_E("%d : Memory not enough", result); break;
  54. case 554 : LOG_E("%d : Create socket failed", result); break;
  55. case 555 : LOG_E("%d : Operation not supported", result); break;
  56. case 556 : LOG_E("%d : Socket bind failed", result); break;
  57. case 557 : LOG_E("%d : Socket listen failed", result); break;
  58. case 558 : LOG_E("%d : Socket write failed", result); break;
  59. case 559 : LOG_E("%d : Socket read failed", result); break;
  60. case 560 : LOG_E("%d : Socket accept failed", result); break;
  61. case 561 : LOG_E("%d : Open PDP context failed", result); break;
  62. case 562 : LOG_E("%d : Close PDP context failed", result); break;
  63. case 563 : LOG_W("%d : Socket identity has been used", result); break;
  64. case 564 : LOG_E("%d : DNS busy", result); break;
  65. case 565 : LOG_E("%d : DNS parse failed", result); break;
  66. case 566 : LOG_E("%d : Socket connect failed", result); break;
  67. // case 567 : LOG_W("%d : Socket has been closed", result); break;
  68. case 567 : break;
  69. case 568 : LOG_E("%d : Operation busy", result); break;
  70. case 569 : LOG_E("%d : Operation timeout", result); break;
  71. case 570 : LOG_E("%d : PDP context broken down", result); break;
  72. case 571 : LOG_E("%d : Cancel send", result); break;
  73. case 572 : LOG_E("%d : Operation not allowed", result); break;
  74. case 573 : LOG_E("%d : APN not configured", result); break;
  75. case 574 : LOG_E("%d : Port busy", result); break;
  76. default : LOG_E("%d : Unknown err code", result); break;
  77. }
  78. }
  79. static int bc26_socket_event_send(struct at_device *device, uint32_t event)
  80. {
  81. return (int) rt_event_send(device->socket_event, event);
  82. }
  83. static int bc26_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  84. {
  85. int result = RT_EOK;
  86. rt_uint32_t recved;
  87. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  88. if (result != RT_EOK)
  89. {
  90. return -RT_ETIMEOUT;
  91. }
  92. return recved;
  93. }
  94. /**
  95. * close socket by AT commands.
  96. *
  97. * @param current socket
  98. *
  99. * @return 0: close socket success
  100. * -1: send AT commands error
  101. * -2: wait socket event timeout
  102. * -5: no memory
  103. */
  104. static int bc26_socket_close(struct at_socket *socket)
  105. {
  106. int result = RT_EOK;
  107. at_response_t resp = RT_NULL;
  108. int device_socket = (int) socket->user_data;
  109. struct at_device *device = (struct at_device *) socket->device;
  110. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  111. if (resp == RT_NULL)
  112. {
  113. LOG_E("no memory for resp create.");
  114. return -RT_ENOMEM;
  115. }
  116. result = at_obj_exec_cmd(device->client, resp, "AT+QICLOSE=%d", device_socket);
  117. at_delete_resp(resp);
  118. return result;
  119. }
  120. /**
  121. * create TCP/UDP client or server connect by AT commands.
  122. *
  123. * @param socket current socket
  124. * @param ip server or client IP address
  125. * @param port server or client port
  126. * @param type connect socket type(tcp, udp)
  127. * @param is_client connection is client
  128. *
  129. * @return 0: connect success
  130. * -1: connect failed, send commands error or type error
  131. * -2: wait socket event timeout
  132. * -5: no memory
  133. */
  134. static int bc26_socket_connect(struct at_socket *socket, char *ip, int32_t port,
  135. enum at_socket_type type, rt_bool_t is_client)
  136. {
  137. #define CONN_RETRY 2
  138. int i = 0;
  139. const char *type_str = RT_NULL;
  140. uint32_t event = 0;
  141. at_response_t resp = RT_NULL;
  142. int result = 0, event_result = 0;
  143. int device_socket = (int) socket->user_data;
  144. struct at_device *device = (struct at_device *) socket->device;
  145. RT_ASSERT(ip);
  146. RT_ASSERT(port >= 0);
  147. if ( ! is_client)
  148. {
  149. return -RT_ERROR;
  150. }
  151. switch(type)
  152. {
  153. case AT_SOCKET_TCP:
  154. type_str = "TCP";
  155. break;
  156. case AT_SOCKET_UDP:
  157. type_str = "UDP";
  158. break;
  159. default:
  160. LOG_E("%s device socket(%d) connect type error.", device->name, device_socket);
  161. return -RT_ERROR;
  162. }
  163. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  164. if (resp == RT_NULL)
  165. {
  166. LOG_E("no memory for resp create.");
  167. return -RT_ENOMEM;
  168. }
  169. for(i=0; i<CONN_RETRY; i++)
  170. {
  171. /* clear socket connect event */
  172. event = SET_EVENT(device_socket, BC26_EVENT_CONN_OK | BC26_EVENT_CONN_FAIL);
  173. bc26_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  174. if (at_obj_exec_cmd(device->client, resp, "AT+QIOPEN=1,%d,\"%s\",\"%s\",%d,0,1",
  175. device_socket, type_str, ip, port) < 0)
  176. {
  177. result = -RT_ERROR;
  178. break;
  179. }
  180. /* waiting result event from AT URC, the device default connection timeout is 60 seconds*/
  181. if (bc26_socket_event_recv(device, SET_EVENT(device_socket, 0),
  182. 60 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  183. {
  184. LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
  185. result = -RT_ETIMEOUT;
  186. break;
  187. }
  188. /* waiting OK or failed result */
  189. event_result = bc26_socket_event_recv(device, BC26_EVENT_CONN_OK | BC26_EVENT_CONN_FAIL,
  190. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  191. if (event_result < 0)
  192. {
  193. LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
  194. result = -RT_ETIMEOUT;
  195. break;
  196. }
  197. /* check result */
  198. if (event_result & BC26_EVENT_CONN_OK)
  199. {
  200. result = RT_EOK;
  201. break;
  202. }
  203. LOG_D("%s device socket(%d) connect failed, the socket was not be closed and now will connect retry.",
  204. device->name, device_socket);
  205. if (bc26_socket_close(socket) < 0)
  206. {
  207. result = -RT_ERROR;
  208. break;
  209. }
  210. }
  211. if (i == CONN_RETRY)
  212. {
  213. LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
  214. result = -RT_ERROR;
  215. }
  216. if (resp)
  217. {
  218. at_delete_resp(resp);
  219. }
  220. return result;
  221. }
  222. static int at_get_send_size(struct at_socket *socket, size_t *size, size_t *acked, size_t *nacked)
  223. {
  224. int result = 0;
  225. at_response_t resp = RT_NULL;
  226. int device_socket = (int) socket->user_data;
  227. struct at_device *device = (struct at_device *) socket->device;
  228. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  229. if (resp == RT_NULL)
  230. {
  231. LOG_E("no memory for resp create.", device->name);
  232. return -RT_ENOMEM;
  233. }
  234. if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,0", device_socket) < 0)
  235. {
  236. result = -RT_ERROR;
  237. goto __exit;
  238. }
  239. if (at_resp_parse_line_args_by_kw(resp, "+QISEND:", "+QISEND: %d,%d,%d", size, acked, nacked) <= 0)
  240. {
  241. result = -RT_ERROR;
  242. goto __exit;
  243. }
  244. __exit:
  245. if (resp)
  246. {
  247. at_delete_resp(resp);
  248. }
  249. return result;
  250. }
  251. static int at_wait_send_finish(struct at_socket *socket, size_t settings_size)
  252. {
  253. /* get the timeout by the input data size */
  254. rt_tick_t timeout = rt_tick_from_millisecond(settings_size);
  255. rt_tick_t last_time = rt_tick_get();
  256. size_t size = 0, acked = 0, nacked = 0xFFFF;
  257. while (rt_tick_get() - last_time <= timeout)
  258. {
  259. at_get_send_size(socket, &size, &acked, &nacked);
  260. if (nacked == 0)
  261. {
  262. return RT_EOK;
  263. }
  264. rt_thread_mdelay(100);
  265. }
  266. return -RT_ETIMEOUT;
  267. }
  268. /**
  269. * send data to server or client by AT commands.
  270. *
  271. * @param socket current socket
  272. * @param buff send buffer
  273. * @param bfsz send buffer size
  274. * @param type connect socket type(tcp, udp)
  275. *
  276. * @return >=0: the size of send success
  277. * -1: send AT commands error or send data error
  278. * -2: waited socket event timeout
  279. * -5: no memory
  280. */
  281. static int bc26_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  282. {
  283. uint32_t event = 0;
  284. int result = 0, event_result = 0;
  285. size_t cur_pkt_size = 0, sent_size = 0;
  286. at_response_t resp = RT_NULL;
  287. int device_socket = (int) socket->user_data;
  288. struct at_device *device = (struct at_device *) socket->device;
  289. struct at_device_bc26 *bc26 = (struct at_device_bc26 *) device->user_data;
  290. rt_mutex_t lock = device->client->lock;
  291. RT_ASSERT(buff);
  292. resp = at_create_resp(128, 2, rt_tick_from_millisecond(300));
  293. if (resp == RT_NULL)
  294. {
  295. LOG_E("no memory for resp create.");
  296. return -RT_ENOMEM;
  297. }
  298. rt_mutex_take(lock, RT_WAITING_FOREVER);
  299. /* set current socket for send URC event */
  300. bc26->user_data = (void *) device_socket;
  301. /* clear socket send event */
  302. event = SET_EVENT(device_socket, BC26_EVENT_SEND_OK | BC26_EVENT_SEND_FAIL);
  303. bc26_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  304. /* set AT client end sign to deal with '>' sign.*/
  305. at_obj_set_end_sign(device->client, '>');
  306. while (sent_size < bfsz)
  307. {
  308. if (bfsz - sent_size < BC26_MODULE_SEND_MAX_SIZE)
  309. {
  310. cur_pkt_size = bfsz - sent_size;
  311. }
  312. else
  313. {
  314. cur_pkt_size = BC26_MODULE_SEND_MAX_SIZE;
  315. }
  316. /* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
  317. if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,%d", device_socket, (int)cur_pkt_size) < 0)
  318. {
  319. result = -RT_ERROR;
  320. goto __exit;
  321. }
  322. rt_thread_mdelay(5);//delay at least 4ms
  323. /* send the real data to server or client */
  324. result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  325. if (result == 0)
  326. {
  327. result = -RT_ERROR;
  328. goto __exit;
  329. }
  330. /* waiting result event from AT URC */
  331. event = SET_EVENT(device_socket, 0);
  332. event_result = bc26_socket_event_recv(device, event, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  333. if (event_result < 0)
  334. {
  335. LOG_E("%s device socket(%d) wait event timeout.", device->name, device_socket);
  336. result = -RT_ETIMEOUT;
  337. goto __exit;
  338. }
  339. /* waiting OK or failed result */
  340. event = BC26_EVENT_SEND_OK | BC26_EVENT_SEND_FAIL;
  341. event_result = bc26_socket_event_recv(device, event, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  342. if (event_result < 0)
  343. {
  344. LOG_E("%s device socket(%d) wait sned OK|FAIL timeout.", device->name, device_socket);
  345. result = -RT_ETIMEOUT;
  346. goto __exit;
  347. }
  348. /* check result */
  349. if (event_result & BC26_EVENT_SEND_FAIL)
  350. {
  351. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  352. result = -RT_ERROR;
  353. goto __exit;
  354. }
  355. if (type == AT_SOCKET_TCP)
  356. {
  357. at_wait_send_finish(socket, 2*cur_pkt_size);
  358. }
  359. sent_size += cur_pkt_size;
  360. }
  361. __exit:
  362. /* reset the end sign for data conflict */
  363. at_obj_set_end_sign(device->client, 0);
  364. rt_mutex_release(lock);
  365. if (resp)
  366. {
  367. at_delete_resp(resp);
  368. }
  369. return result > 0 ? sent_size : result;
  370. }
  371. /**
  372. * domain resolve by AT commands.
  373. *
  374. * @param name domain name
  375. * @param ip parsed IP address, it's length must be 16
  376. *
  377. * @return 0: domain resolve success
  378. * -1: send AT commands error or response error
  379. * -2: wait socket event timeout
  380. * -5: no memory
  381. */
  382. static int bc26_domain_resolve(const char *name, char ip[16])
  383. {
  384. #define RESOLVE_RETRY 3
  385. int i, result;
  386. at_response_t resp = RT_NULL;
  387. struct at_device *device = RT_NULL;
  388. struct at_device_bc26 *bc26 = RT_NULL;
  389. RT_ASSERT(name);
  390. RT_ASSERT(ip);
  391. device = at_device_get_first_initialized();
  392. if (device == RT_NULL)
  393. {
  394. LOG_E("get first init device failed.");
  395. return -RT_ERROR;
  396. }
  397. /* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
  398. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  399. if (!resp)
  400. {
  401. LOG_E("no memory for resp create.");
  402. return -RT_ENOMEM;
  403. }
  404. /* clear BC26_EVENT_DOMAIN_OK */
  405. bc26_socket_event_recv(device, BC26_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
  406. bc26 = (struct at_device_bc26 *) device->user_data;
  407. bc26->socket_data = ip;
  408. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSGIP=1,\"%s\"", name) != RT_EOK)
  409. {
  410. result = -RT_ERROR;
  411. goto __exit;
  412. }
  413. for(i = 0; i < RESOLVE_RETRY; i++)
  414. {
  415. /* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
  416. if (bc26_socket_event_recv(device, BC26_EVENT_DOMAIN_OK, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  417. {
  418. result = -RT_ETIMEOUT;
  419. continue;
  420. }
  421. else
  422. {
  423. if (rt_strlen(ip) < 8)
  424. {
  425. rt_thread_mdelay(100);
  426. /* resolve failed, maybe receive an URC CRLF */
  427. result = -RT_ERROR;
  428. continue;
  429. }
  430. else
  431. {
  432. result = RT_EOK;
  433. break;
  434. }
  435. }
  436. }
  437. __exit:
  438. bc26->socket_data = RT_NULL;
  439. if (resp)
  440. {
  441. at_delete_resp(resp);
  442. }
  443. return result;
  444. }
  445. /**
  446. * set AT socket event notice callback
  447. *
  448. * @param event notice event
  449. * @param cb notice callback
  450. */
  451. static void bc26_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  452. {
  453. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  454. {
  455. at_evt_cb_set[event] = cb;
  456. }
  457. }
  458. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  459. {
  460. int device_socket = 0, result = 0;
  461. struct at_device *device = RT_NULL;
  462. char *client_name = client->device->parent.name;
  463. RT_ASSERT(data && size);
  464. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  465. if (device == RT_NULL)
  466. {
  467. LOG_E("get device(%s) failed.", client_name);
  468. return;
  469. }
  470. sscanf(data, "+QIOPEN: %d,%d", &device_socket , &result);
  471. if (result == 0)
  472. {
  473. bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_CONN_OK));
  474. }
  475. else
  476. {
  477. at_tcp_ip_errcode_parse(result);
  478. bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_CONN_FAIL));
  479. }
  480. }
  481. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  482. {
  483. int device_socket = 0;
  484. struct at_device *device = RT_NULL;
  485. struct at_device_bc26 *bc26 = RT_NULL;
  486. char *client_name = client->device->parent.name;
  487. RT_ASSERT(data && size);
  488. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  489. if (device == RT_NULL)
  490. {
  491. LOG_E("get device(%s) failed.", client_name);
  492. return;
  493. }
  494. bc26 = (struct at_device_bc26 *) device->user_data;
  495. device_socket = (int) bc26->user_data;
  496. if (rt_strstr(data, "SEND OK"))
  497. {
  498. bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_SEND_OK));
  499. }
  500. else if (rt_strstr(data, "SEND FAIL"))
  501. {
  502. bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_SEND_FAIL));
  503. }
  504. }
  505. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  506. {
  507. int device_socket = 0;
  508. struct at_socket *socket = RT_NULL;
  509. struct at_device *device = RT_NULL;
  510. char *client_name = client->device->parent.name;
  511. RT_ASSERT(data && size);
  512. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  513. if (device == RT_NULL)
  514. {
  515. LOG_E("get device(%s) failed.", client_name);
  516. return;
  517. }
  518. sscanf(data, "+QIURC: \"closed\",%d", &device_socket);
  519. /* get at socket object by device socket descriptor */
  520. socket = &(device->sockets[device_socket]);
  521. /* notice the socket is disconnect by remote */
  522. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  523. {
  524. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  525. }
  526. }
  527. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  528. {
  529. int device_socket = 0;
  530. rt_int32_t timeout;
  531. rt_size_t bfsz = 0, temp_size = 0;
  532. char *recv_buf = RT_NULL, temp[8] = {0};
  533. struct at_socket *socket = RT_NULL;
  534. struct at_device *device = RT_NULL;
  535. char *client_name = client->device->parent.name;
  536. RT_ASSERT(data && size);
  537. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  538. if (device == RT_NULL)
  539. {
  540. LOG_E("get device(%s) failed.", client_name);
  541. return;
  542. }
  543. /* get the current socket and receive buffer size by receive data */
  544. sscanf(data, "+QIURC: \"recv\",%d,%d", &device_socket, (int *) &bfsz);
  545. /* set receive timeout by receive buffer length, not less than 10 ms */
  546. timeout = bfsz > 10 ? bfsz : 10;
  547. if (device_socket < 0 || bfsz == 0)
  548. {
  549. return;
  550. }
  551. recv_buf = (char *) rt_calloc(1, bfsz);
  552. if (recv_buf == RT_NULL)
  553. {
  554. LOG_E("no memory for URC receive buffer(%d).", bfsz);
  555. /* read and clean the coming data */
  556. while (temp_size < bfsz)
  557. {
  558. if (bfsz - temp_size > sizeof(temp))
  559. {
  560. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  561. }
  562. else
  563. {
  564. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  565. }
  566. temp_size += sizeof(temp);
  567. }
  568. return;
  569. }
  570. /* sync receive data */
  571. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  572. {
  573. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  574. rt_free(recv_buf);
  575. return;
  576. }
  577. /* get at socket object by device socket descriptor */
  578. socket = &(device->sockets[device_socket]);
  579. /* notice the receive buffer and buffer size */
  580. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  581. {
  582. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  583. }
  584. }
  585. static void urc_dnsqip_func(struct at_client *client, const char *data, rt_size_t size)
  586. {
  587. int i = 0, j = 0;
  588. char recv_ip[16] = {0};
  589. int result, ip_count, dns_ttl;
  590. struct at_device *device = RT_NULL;
  591. struct at_device_bc26 *bc26 = RT_NULL;
  592. char *client_name = client->device->parent.name;
  593. RT_ASSERT(data && size);
  594. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  595. if (device == RT_NULL)
  596. {
  597. LOG_E("get device(%s) failed.", client_name);
  598. return;
  599. }
  600. bc26 = (struct at_device_bc26 *) device->user_data;
  601. if (bc26->socket_data == RT_NULL)
  602. {
  603. LOG_D("%s device socket_data no config.", bc26->device_name);
  604. return;
  605. }
  606. for (i = 0; i < size; i++)
  607. {
  608. if (*(data + i) == '.')
  609. j++;
  610. }
  611. /* There would be several dns result, we just pickup one */
  612. if (j == 3)
  613. {
  614. sscanf(data, "+QIURC: \"dnsgip\",\"%[^\"]", recv_ip);
  615. recv_ip[15] = '\0';
  616. rt_memcpy(bc26->socket_data, recv_ip, sizeof(recv_ip));
  617. bc26_socket_event_send(device, BC26_EVENT_DOMAIN_OK);
  618. }
  619. else
  620. {
  621. sscanf(data, "+QIURC: \"dnsgip\",%d,%d,%d", &result, &ip_count, &dns_ttl);
  622. if (result)
  623. {
  624. at_tcp_ip_errcode_parse(result);
  625. }
  626. }
  627. }
  628. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  629. {
  630. RT_ASSERT(data);
  631. LOG_I("URC data : %.*s", size, data);
  632. }
  633. static void urc_qiurc_func(struct at_client *client, const char *data, rt_size_t size)
  634. {
  635. RT_ASSERT(data && size);
  636. switch(*(data + 9))
  637. {
  638. case 'c' : urc_close_func(client, data, size); break;//+QIURC: "closed"
  639. case 'r' : urc_recv_func(client, data, size); break;//+QIURC: "recv"
  640. case 'd' : urc_dnsqip_func(client, data, size); break;//+QIURC: "dnsgip"
  641. default : urc_func(client, data, size); break;
  642. }
  643. }
  644. static const struct at_urc urc_table[] =
  645. {
  646. {"SEND OK", "\r\n", urc_send_func},
  647. {"SEND FAIL", "\r\n", urc_send_func},
  648. {"+QIOPEN:", "\r\n", urc_connect_func},
  649. {"+QIURC:", "\r\n", urc_qiurc_func},
  650. };
  651. static const struct at_socket_ops bc26_socket_ops =
  652. {
  653. bc26_socket_connect,
  654. bc26_socket_close,
  655. bc26_socket_send,
  656. bc26_domain_resolve,
  657. bc26_socket_set_event_cb,
  658. };
  659. int bc26_socket_init(struct at_device *device)
  660. {
  661. RT_ASSERT(device);
  662. /* register URC data execution function */
  663. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  664. return RT_EOK;
  665. }
  666. int bc26_socket_class_register(struct at_device_class *class)
  667. {
  668. RT_ASSERT(class);
  669. class->socket_num = AT_DEVICE_BC26_SOCKETS_NUM;
  670. class->socket_ops = &bc26_socket_ops;
  671. return RT_EOK;
  672. }
  673. #endif /* AT_DEVICE_USING_BC26 && AT_USING_SOCKET */