at_socket_ec200x.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * File : at_socket_ec200x.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_ec200x.h>
  27. #define LOG_TAG "at.skt.ec200x"
  28. #include <at_log.h>
  29. #if defined(AT_DEVICE_USING_EC200X) && defined(AT_USING_SOCKET)
  30. #define EC200X_MODULE_SEND_MAX_SIZE 1460
  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 EC200X_EVENT_CONN_OK (1L << 0)
  35. #define EC200X_EVENT_SEND_OK (1L << 1)
  36. #define EC200X_EVENT_RECV_OK (1L << 2)
  37. #define EC200X_EVNET_CLOSE_OK (1L << 3)
  38. #define EC200X_EVENT_CONN_FAIL (1L << 4)
  39. #define EC200X_EVENT_SEND_FAIL (1L << 5)
  40. #define EC200X_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 ec200x_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 ec200x_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 ec200x_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 ec200x_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, EC200X_EVENT_CONN_OK | EC200X_EVENT_CONN_FAIL);
  173. ec200x_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 (ec200x_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 = ec200x_socket_event_recv(device, EC200X_EVENT_CONN_OK | EC200X_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 & EC200X_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 (ec200x_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 ec200x_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_ec200x *ec200x = (struct at_device_ec200x *) 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. ec200x->user_data = (void *) device_socket;
  301. /* clear socket send event */
  302. event = SET_EVENT(device_socket, EC200X_EVENT_SEND_OK | EC200X_EVENT_SEND_FAIL);
  303. ec200x_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 < EC200X_MODULE_SEND_MAX_SIZE)
  309. {
  310. cur_pkt_size = bfsz - sent_size;
  311. }
  312. else
  313. {
  314. cur_pkt_size = EC200X_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 = ec200x_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 = EC200X_EVENT_SEND_OK | EC200X_EVENT_SEND_FAIL;
  341. event_result = ec200x_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 & EC200X_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, cur_pkt_size);
  358. rt_thread_mdelay(10);
  359. }
  360. sent_size += cur_pkt_size;
  361. }
  362. __exit:
  363. /* reset the end sign for data conflict */
  364. at_obj_set_end_sign(device->client, 0);
  365. rt_mutex_release(lock);
  366. if (resp)
  367. {
  368. at_delete_resp(resp);
  369. }
  370. return result > 0 ? sent_size : result;
  371. }
  372. /**
  373. * domain resolve by AT commands.
  374. *
  375. * @param name domain name
  376. * @param ip parsed IP address, it's length must be 16
  377. *
  378. * @return 0: domain resolve success
  379. * -1: send AT commands error or response error
  380. * -2: wait socket event timeout
  381. * -5: no memory
  382. */
  383. static int ec200x_domain_resolve(const char *name, char ip[16])
  384. {
  385. #define RESOLVE_RETRY 3
  386. int i, result;
  387. at_response_t resp = RT_NULL;
  388. struct at_device *device = RT_NULL;
  389. struct at_device_ec200x *ec200x = RT_NULL;
  390. RT_ASSERT(name);
  391. RT_ASSERT(ip);
  392. device = at_device_get_first_initialized();
  393. if (device == RT_NULL)
  394. {
  395. LOG_E("get first init device failed.");
  396. return -RT_ERROR;
  397. }
  398. /* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
  399. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  400. if (!resp)
  401. {
  402. LOG_E("no memory for resp create.");
  403. return -RT_ENOMEM;
  404. }
  405. /* clear EC200X_EVENT_DOMAIN_OK */
  406. ec200x_socket_event_recv(device, EC200X_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
  407. ec200x = (struct at_device_ec200x *) device->user_data;
  408. ec200x->socket_data = ip;
  409. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSGIP=1,\"%s\"", name) != RT_EOK)
  410. {
  411. result = -RT_ERROR;
  412. goto __exit;
  413. }
  414. for(i = 0; i < RESOLVE_RETRY; i++)
  415. {
  416. /* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
  417. if (ec200x_socket_event_recv(device, EC200X_EVENT_DOMAIN_OK, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  418. {
  419. result = -RT_ETIMEOUT;
  420. continue;
  421. }
  422. else
  423. {
  424. if (rt_strlen(ip) < 8)
  425. {
  426. rt_thread_mdelay(100);
  427. /* resolve failed, maybe receive an URC CRLF */
  428. result = -RT_ERROR;
  429. continue;
  430. }
  431. else
  432. {
  433. result = RT_EOK;
  434. break;
  435. }
  436. }
  437. }
  438. __exit:
  439. ec200x->socket_data = RT_NULL;
  440. if (resp)
  441. {
  442. at_delete_resp(resp);
  443. }
  444. return result;
  445. }
  446. /**
  447. * set AT socket event notice callback
  448. *
  449. * @param event notice event
  450. * @param cb notice callback
  451. */
  452. static void ec200x_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  453. {
  454. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  455. {
  456. at_evt_cb_set[event] = cb;
  457. }
  458. }
  459. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  460. {
  461. int device_socket = 0, result = 0;
  462. struct at_device *device = RT_NULL;
  463. char *client_name = client->device->parent.name;
  464. RT_ASSERT(data && size);
  465. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  466. if (device == RT_NULL)
  467. {
  468. LOG_E("get device(%s) failed.", client_name);
  469. return;
  470. }
  471. sscanf(data, "+QIOPEN: %d,%d", &device_socket , &result);
  472. if (result == 0)
  473. {
  474. ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_CONN_OK));
  475. }
  476. else
  477. {
  478. at_tcp_ip_errcode_parse(result);
  479. ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_CONN_FAIL));
  480. }
  481. }
  482. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  483. {
  484. int device_socket = 0;
  485. struct at_device *device = RT_NULL;
  486. struct at_device_ec200x *ec200x = RT_NULL;
  487. char *client_name = client->device->parent.name;
  488. RT_ASSERT(data && size);
  489. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  490. if (device == RT_NULL)
  491. {
  492. LOG_E("get device(%s) failed.", client_name);
  493. return;
  494. }
  495. ec200x = (struct at_device_ec200x *) device->user_data;
  496. device_socket = (int) ec200x->user_data;
  497. if (rt_strstr(data, "SEND OK"))
  498. {
  499. ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_SEND_OK));
  500. }
  501. else if (rt_strstr(data, "SEND FAIL"))
  502. {
  503. ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_SEND_FAIL));
  504. }
  505. }
  506. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  507. {
  508. int device_socket = 0;
  509. struct at_socket *socket = RT_NULL;
  510. struct at_device *device = RT_NULL;
  511. char *client_name = client->device->parent.name;
  512. RT_ASSERT(data && size);
  513. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  514. if (device == RT_NULL)
  515. {
  516. LOG_E("get device(%s) failed.", client_name);
  517. return;
  518. }
  519. sscanf(data, "+QIURC: \"closed\",%d", &device_socket);
  520. /* get at socket object by device socket descriptor */
  521. socket = &(device->sockets[device_socket]);
  522. /* notice the socket is disconnect by remote */
  523. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  524. {
  525. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  526. }
  527. }
  528. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  529. {
  530. int device_socket = 0;
  531. rt_int32_t timeout;
  532. rt_size_t bfsz = 0, temp_size = 0;
  533. char *recv_buf = RT_NULL, temp[8] = {0};
  534. struct at_socket *socket = RT_NULL;
  535. struct at_device *device = RT_NULL;
  536. char *client_name = client->device->parent.name;
  537. RT_ASSERT(data && size);
  538. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  539. if (device == RT_NULL)
  540. {
  541. LOG_E("get device(%s) failed.", client_name);
  542. return;
  543. }
  544. /* get the current socket and receive buffer size by receive data */
  545. sscanf(data, "+QIURC: \"recv\",%d,%d", &device_socket, (int *) &bfsz);
  546. /* set receive timeout by receive buffer length, not less than 10 ms */
  547. timeout = bfsz > 10 ? bfsz : 10;
  548. if (device_socket < 0 || bfsz == 0)
  549. {
  550. return;
  551. }
  552. recv_buf = (char *) rt_calloc(1, bfsz);
  553. if (recv_buf == RT_NULL)
  554. {
  555. LOG_E("no memory for URC receive buffer(%d).", bfsz);
  556. /* read and clean the coming data */
  557. while (temp_size < bfsz)
  558. {
  559. if (bfsz - temp_size > sizeof(temp))
  560. {
  561. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  562. }
  563. else
  564. {
  565. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  566. }
  567. temp_size += sizeof(temp);
  568. }
  569. return;
  570. }
  571. /* sync receive data */
  572. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  573. {
  574. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  575. rt_free(recv_buf);
  576. return;
  577. }
  578. /* get at socket object by device socket descriptor */
  579. socket = &(device->sockets[device_socket]);
  580. /* notice the receive buffer and buffer size */
  581. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  582. {
  583. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  584. }
  585. }
  586. static void urc_pdpdeact_func(struct at_client *client, const char *data, rt_size_t size)
  587. {
  588. int connectID = 0;
  589. RT_ASSERT(data && size);
  590. sscanf(data, "+QIURC: \"pdpdeact\",%d", &connectID);
  591. LOG_E("context (%d) is deactivated.", connectID);
  592. }
  593. static void urc_dnsqip_func(struct at_client *client, const char *data, rt_size_t size)
  594. {
  595. int i = 0, j = 0;
  596. char recv_ip[16] = {0};
  597. int result, ip_count, dns_ttl;
  598. struct at_device *device = RT_NULL;
  599. struct at_device_ec200x *ec200x = RT_NULL;
  600. char *client_name = client->device->parent.name;
  601. RT_ASSERT(data && size);
  602. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  603. if (device == RT_NULL)
  604. {
  605. LOG_E("get device(%s) failed.", client_name);
  606. return;
  607. }
  608. ec200x = (struct at_device_ec200x *) device->user_data;
  609. if (ec200x->socket_data == RT_NULL)
  610. {
  611. LOG_D("%s device socket_data no config.", ec200x->device_name);
  612. return;
  613. }
  614. for (i = 0; i < size; i++)
  615. {
  616. if (*(data + i) == '.')
  617. j++;
  618. }
  619. /* There would be several dns result, we just pickup one */
  620. if (j == 3)
  621. {
  622. sscanf(data, "+QIURC: \"dnsgip\",\"%[^\"]", recv_ip);
  623. recv_ip[15] = '\0';
  624. rt_memcpy(ec200x->socket_data, recv_ip, sizeof(recv_ip));
  625. ec200x_socket_event_send(device, EC200X_EVENT_DOMAIN_OK);
  626. }
  627. else
  628. {
  629. sscanf(data, "+QIURC: \"dnsgip\",%d,%d,%d", &result, &ip_count, &dns_ttl);
  630. if (result)
  631. {
  632. at_tcp_ip_errcode_parse(result);
  633. }
  634. }
  635. }
  636. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  637. {
  638. RT_ASSERT(data);
  639. LOG_I("URC data : %.*s", size, data);
  640. }
  641. static void urc_qiurc_func(struct at_client *client, const char *data, rt_size_t size)
  642. {
  643. RT_ASSERT(data && size);
  644. switch(*(data + 9))
  645. {
  646. case 'c' : urc_close_func(client, data, size); break;//+QIURC: "closed"
  647. case 'r' : urc_recv_func(client, data, size); break;//+QIURC: "recv"
  648. case 'p' : urc_pdpdeact_func(client, data, size); break;//+QIURC: "pdpdeact"
  649. case 'd' : urc_dnsqip_func(client, data, size); break;//+QIURC: "dnsgip"
  650. default : urc_func(client, data, size); break;
  651. }
  652. }
  653. static const struct at_urc urc_table[] =
  654. {
  655. {"SEND OK", "\r\n", urc_send_func},
  656. {"SEND FAIL", "\r\n", urc_send_func},
  657. {"+QIOPEN:", "\r\n", urc_connect_func},
  658. {"+QIURC:", "\r\n", urc_qiurc_func},
  659. };
  660. static const struct at_socket_ops ec200x_socket_ops =
  661. {
  662. ec200x_socket_connect,
  663. ec200x_socket_close,
  664. ec200x_socket_send,
  665. ec200x_domain_resolve,
  666. ec200x_socket_set_event_cb,
  667. };
  668. int ec200x_socket_init(struct at_device *device)
  669. {
  670. RT_ASSERT(device);
  671. /* register URC data execution function */
  672. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  673. return RT_EOK;
  674. }
  675. int ec200x_socket_class_register(struct at_device_class *class)
  676. {
  677. RT_ASSERT(class);
  678. class->socket_num = AT_DEVICE_EC200X_SOCKETS_NUM;
  679. class->socket_ops = &ec200x_socket_ops;
  680. return RT_EOK;
  681. }
  682. #endif /* AT_DEVICE_USING_EC200X && AT_USING_SOCKET */