at_socket_bc26.c 22 KB

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