at_socket_a9g.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. * File : at_socket_a9g.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-11-23 luliang first version
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <at_device_a9g.h>
  27. #define LOG_TAG "at.skt.a9g"
  28. #include <at_log.h>
  29. #if defined(AT_DEVICE_USING_A9G) && defined(AT_USING_SOCKET)
  30. #define A9G_MODULE_SEND_MAX_SIZE 1000
  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 A9G_EVENT_CONN_OK (1L << 0)
  35. #define A9G_EVENT_SEND_OK (1L << 1)
  36. #define A9G_EVENT_RECV_OK (1L << 2)
  37. #define A9G_EVENT_CLOSE_OK (1L << 3)
  38. #define A9G_EVENT_CONN_FAIL (1L << 4)
  39. #define A9G_EVENT_SEND_FAIL (1L << 5)
  40. static at_evt_cb_t at_evt_cb_set[] = {
  41. [AT_SOCKET_EVT_RECV] = NULL,
  42. [AT_SOCKET_EVT_CLOSED] = NULL,
  43. };
  44. static int a9g_socket_event_send(struct at_device *device, uint32_t event)
  45. {
  46. return (int) rt_event_send(device->socket_event, event);
  47. }
  48. static int a9g_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  49. {
  50. int result = RT_EOK;
  51. rt_uint32_t recved;
  52. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  53. if (result != RT_EOK)
  54. {
  55. return -RT_ETIMEOUT;
  56. }
  57. return recved;
  58. }
  59. /**
  60. * close socket by AT commands.
  61. *
  62. * @param current socket
  63. *
  64. * @return 0: close socket success
  65. * -1: send AT commands error
  66. * -2: wait socket event timeout
  67. * -5: no memory
  68. */
  69. static int a9g_socket_close(struct at_socket *socket)
  70. {
  71. int result = RT_EOK;
  72. at_response_t resp = RT_NULL;
  73. int device_socket = (int) socket->user_data;
  74. struct at_device *device = (struct at_device *) socket->device;
  75. resp = at_create_resp(64, 0, rt_tick_from_millisecond(600));
  76. if (resp == RT_NULL)
  77. {
  78. LOG_E("no memory for a9g device(%s) response creat.", device->name);
  79. return -RT_ENOMEM;
  80. }
  81. if (at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE=%d", device_socket) < 0)
  82. {
  83. result = -RT_ERROR;
  84. goto __exit;
  85. }
  86. __exit:
  87. if (resp)
  88. {
  89. at_delete_resp(resp);
  90. }
  91. return result;
  92. }
  93. /**
  94. * create TCP/UDP client or server connect by AT commands.
  95. *
  96. * @param socket current socket
  97. * @param ip server or client IP address
  98. * @param port server or client port
  99. * @param type connect socket type(tcp, udp)
  100. * @param is_client connection is client
  101. *
  102. * @return 0: connect success
  103. * -1: connect failed, send commands error or type error
  104. * -2: wait socket event timeout
  105. * -5: no memory
  106. */
  107. static int a9g_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  108. {
  109. uint32_t event = 0;
  110. rt_bool_t retryed = RT_FALSE;
  111. at_response_t resp = RT_NULL;
  112. int result = RT_EOK, event_result = 0;
  113. int device_socket = (int) socket->user_data;
  114. struct at_device *device = (struct at_device *) socket->device;
  115. RT_ASSERT(ip);
  116. RT_ASSERT(port >= 0);
  117. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  118. if (resp == RT_NULL)
  119. {
  120. LOG_E("no memory for a9g device(%s) response structure.", device->name);
  121. return -RT_ENOMEM;
  122. }
  123. __retry:
  124. /* clear socket connect event */
  125. event = SET_EVENT(device_socket, A9G_EVENT_CONN_OK | A9G_EVENT_CONN_FAIL);
  126. a9g_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  127. if (is_client)
  128. {
  129. switch (type)
  130. {
  131. case AT_SOCKET_TCP:
  132. /* send AT commands(eg: AT+QIOPEN=0,"TCP","x.x.x.x", 1234) to connect TCP server */
  133. if (at_obj_exec_cmd(device->client, RT_NULL, "AT+CIPSTART=\"TCP\",\"%s\",%d", ip, port) < 0)
  134. {
  135. result = -RT_ERROR;
  136. goto __exit;
  137. }
  138. break;
  139. case AT_SOCKET_UDP:
  140. if (at_obj_exec_cmd(device->client, RT_NULL, "AT+CIPSTART=\"UDP\",\"%s\",%d", ip, port) < 0)
  141. {
  142. result = -RT_ERROR;
  143. goto __exit;
  144. }
  145. break;
  146. default:
  147. LOG_E("a9g device(%s) not supported connect type : %d.", device->name, type);
  148. result = -RT_ERROR;
  149. goto __exit;
  150. }
  151. }
  152. /* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use */
  153. if (a9g_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  154. {
  155. LOG_E("a9g device(%s) socket(%d) connect failed, wait connect result timeout.",device->name, device_socket);
  156. result = -RT_ETIMEOUT;
  157. goto __exit;
  158. }
  159. /* waiting OK or failed result */
  160. event_result = a9g_socket_event_recv(device,
  161. A9G_EVENT_CONN_OK | A9G_EVENT_CONN_FAIL, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  162. if (event_result < 0)
  163. {
  164. LOG_E("a9g device(%s) socket(%d) connect failed, wait connect OK|FAIL timeout.", device->name, device_socket);
  165. result = -RT_ETIMEOUT;
  166. goto __exit;
  167. }
  168. /* check result */
  169. if (event_result & A9G_EVENT_CONN_FAIL)
  170. {
  171. if (retryed == RT_FALSE)
  172. {
  173. LOG_D("a9g device(%s) socket(%d) connect failed, maybe the socket was not be closed at the last time and now will retry.",
  174. device->name, device_socket);
  175. if (a9g_socket_close(socket) < 0)
  176. {
  177. result = -RT_ERROR;
  178. goto __exit;
  179. }
  180. retryed = RT_TRUE;
  181. goto __retry;
  182. }
  183. LOG_E("a9g device(%s) socket(%d) connect failed.", device->name, device_socket);
  184. result = -RT_ERROR;
  185. goto __exit;
  186. }
  187. __exit:
  188. if (resp)
  189. {
  190. at_delete_resp(resp);
  191. }
  192. return result;
  193. }
  194. /**
  195. * send data to server or client by AT commands.
  196. *
  197. * @param socket current socket
  198. * @param buff send buffer
  199. * @param bfsz send buffer size
  200. * @param type connect socket type(tcp, udp)
  201. *
  202. * @return >=0: the size of send success
  203. * -1: send AT commands error or send data error
  204. * -2: waited socket event timeout
  205. * -5: no memory
  206. */
  207. static int a9g_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  208. {
  209. int result = RT_EOK;
  210. size_t cur_pkt_size = 0, sent_size = 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. rt_mutex_t lock = device->client->lock;
  215. RT_ASSERT(buff);
  216. resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
  217. if (resp == RT_NULL)
  218. {
  219. LOG_E("no memory for a9g device(%s) response structure.", device->name);
  220. return -RT_ENOMEM;
  221. }
  222. rt_mutex_take(lock, RT_WAITING_FOREVER);
  223. /* set AT client end sign to deal with '>' sign.*/
  224. at_obj_set_end_sign(device->client, '>');
  225. while (sent_size < bfsz)
  226. {
  227. if (bfsz - sent_size < A9G_MODULE_SEND_MAX_SIZE)
  228. {
  229. cur_pkt_size = bfsz - sent_size;
  230. }
  231. else
  232. {
  233. cur_pkt_size = A9G_MODULE_SEND_MAX_SIZE;
  234. }
  235. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  236. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
  237. {
  238. result = -RT_ERROR;
  239. goto __exit;
  240. }
  241. /* send the real data to server or client */
  242. result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  243. if (result == 0)
  244. {
  245. result = -RT_ERROR;
  246. goto __exit;
  247. }
  248. /* waiting OK or failed result */
  249. at_resp_set_info(resp, 128, 0, 30 * RT_TICK_PER_SECOND);
  250. if (at_obj_exec_cmd(device->client, resp, "") < 0)
  251. {
  252. result = -RT_ERROR;
  253. goto __exit;
  254. }
  255. at_resp_set_info(resp, 128, 2, 5 * RT_TICK_PER_SECOND);
  256. sent_size += cur_pkt_size;
  257. }
  258. __exit:
  259. /* reset the end sign for data conflict */
  260. at_obj_set_end_sign(device->client, 0);
  261. rt_mutex_release(lock);
  262. if (resp)
  263. {
  264. at_delete_resp(resp);
  265. }
  266. return result;
  267. }
  268. /**
  269. * domain resolve by AT commands.
  270. *
  271. * @param name domain name
  272. * @param ip parsed IP address, it's length must be 16
  273. *
  274. * @return 0: domain resolve success
  275. * -1: send AT commands error or response error
  276. * -2: wait socket event timeout
  277. * -5: no memory
  278. */
  279. static int a9g_domain_resolve(const char *name, char ip[16])
  280. {
  281. #define RESOLVE_RETRY 5
  282. int i, result = RT_EOK;
  283. char recv_ip[16] = { 0 };
  284. at_response_t resp = RT_NULL;
  285. struct at_device *device = RT_NULL;
  286. RT_ASSERT(name);
  287. RT_ASSERT(ip);
  288. device = at_device_get_first_initialized();
  289. if (device == RT_NULL)
  290. {
  291. LOG_E("get first initialization a9g device failed.");
  292. return -RT_ERROR;
  293. }
  294. /* The maximum response time is 14 seconds, affected by network status */
  295. resp = at_create_resp(1024, 3, 14 * RT_TICK_PER_SECOND);
  296. if (resp == RT_NULL)
  297. {
  298. LOG_E("no memory for a9g device(%s) response structure.", device->name);
  299. return -RT_ENOMEM;
  300. }
  301. for (i = 0; i < RESOLVE_RETRY; i++)
  302. {
  303. int err_code = 0;
  304. if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%s\"", name) < 0)
  305. {
  306. result = -RT_ERROR;
  307. goto __exit;
  308. }
  309. /* domain name prase error options */
  310. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "+CDNSGIP: 0,%d", &err_code) > 0)
  311. {
  312. /* 3 - network error, 8 - dns common error */
  313. if (err_code == 3 || err_code == 8)
  314. {
  315. result = -RT_ERROR;
  316. goto __exit;
  317. }
  318. }
  319. /* parse the third line of response data, get the IP address */
  320. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", recv_ip) < 0)
  321. {
  322. rt_thread_mdelay(100);
  323. /* resolve failed, maybe receive an URC CRLF */
  324. continue;
  325. }
  326. if (rt_strlen(recv_ip) < 8)
  327. {
  328. rt_thread_mdelay(100);
  329. /* resolve failed, maybe receive an URC CRLF */
  330. continue;
  331. }
  332. else
  333. {
  334. rt_thread_mdelay(10);
  335. rt_strncpy(ip, recv_ip, 15);
  336. ip[15] = '\0';
  337. break;
  338. }
  339. }
  340. __exit:
  341. if (resp)
  342. {
  343. at_delete_resp(resp);
  344. }
  345. return result;
  346. }
  347. /**
  348. * set AT socket event notice callback
  349. *
  350. * @param event notice event
  351. * @param cb notice callback
  352. */
  353. static void a9g_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  354. {
  355. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  356. {
  357. at_evt_cb_set[event] = cb;
  358. }
  359. }
  360. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  361. {
  362. int device_socket = 0;
  363. struct at_device *device = RT_NULL;
  364. char *client_name = client->device->parent.name;
  365. RT_ASSERT(data && size);
  366. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  367. if (device == RT_NULL)
  368. {
  369. LOG_E("get a9g device by client name(%s) failed.", client_name);
  370. return;
  371. }
  372. /* get the current socket by receive data */
  373. sscanf(data, "%d,%*s", &device_socket);
  374. if (strstr(data, "CONNECT OK"))
  375. {
  376. a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_CONN_OK));
  377. }
  378. else if (strstr(data, "CONNECT FAIL"))
  379. {
  380. a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_CONN_FAIL));
  381. }
  382. }
  383. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  384. {
  385. int device_socket = 0;
  386. struct at_device *device = RT_NULL;
  387. char *client_name = client->device->parent.name;
  388. RT_ASSERT(data && size);
  389. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  390. if (device == RT_NULL)
  391. {
  392. LOG_E("get a9g device by client name(%s) failed.", client_name);
  393. return;
  394. }
  395. /* get the current socket by receive data */
  396. sscanf(data, "%d,%*s", &device_socket);
  397. if (rt_strstr(data, "SEND OK"))
  398. {
  399. a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_SEND_OK));
  400. }
  401. else if (rt_strstr(data, "SEND FAIL"))
  402. {
  403. a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_SEND_FAIL));
  404. }
  405. }
  406. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  407. {
  408. int device_socket = 0;
  409. struct at_device *device = RT_NULL;
  410. char *client_name = client->device->parent.name;
  411. RT_ASSERT(data && size);
  412. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  413. if (device == RT_NULL)
  414. {
  415. LOG_E("get a9g device by client name(%s) failed.", client_name);
  416. return;
  417. }
  418. /* get the current socket by receive data */
  419. sscanf(data, "%d,%*s", &device_socket);
  420. if (rt_strstr(data, "CLOSE OK"))
  421. {
  422. a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_CLOSE_OK));
  423. }
  424. else if (rt_strstr(data, "CLOSED"))
  425. {
  426. struct at_socket *socket = RT_NULL;
  427. /* get AT socket object by device socket descriptor */
  428. socket = &(device->sockets[device_socket]);
  429. /* notice the socket is disconnect by remote */
  430. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  431. {
  432. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  433. }
  434. }
  435. }
  436. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  437. {
  438. int device_socket = 0;
  439. rt_int32_t timeout;
  440. rt_size_t temp_size = 0;
  441. char temp[8] = {0};
  442. rt_size_t bfsz = 0;
  443. char *recv_buf = RT_NULL;
  444. struct at_socket *socket = RT_NULL;
  445. struct at_device *device = RT_NULL;
  446. char *client_name = client->device->parent.name;
  447. RT_ASSERT(data && size);
  448. /* get the current socket and receive buffer size by receive data */
  449. sscanf(data, "+CIPRCV,%d,%d:", &device_socket, (int *) &bfsz);
  450. /* get receive timeout by receive buffer length */
  451. timeout = bfsz;
  452. if (device_socket < 0 || bfsz == 0)
  453. {
  454. return;
  455. }
  456. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  457. if (device == RT_NULL)
  458. {
  459. LOG_E("get a9g device by client name(%s) failed.", client_name);
  460. return;
  461. }
  462. recv_buf = (char *) rt_calloc(1, bfsz);
  463. if (recv_buf == RT_NULL)
  464. {
  465. LOG_E("no memory for a9g device(%s) URC receive buffer (%d).", device->name, bfsz);
  466. temp_size = (size-(rt_strstr(data,":")+1-data)-2);
  467. /* read and clean the coming data */
  468. while (temp_size < bfsz)
  469. {
  470. if (bfsz - temp_size > sizeof(temp))
  471. {
  472. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  473. }
  474. else
  475. {
  476. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  477. }
  478. temp_size += sizeof(temp);
  479. }
  480. return;
  481. }
  482. /* sync receive data */
  483. temp_size = (size-(rt_strstr(data,":")+1-data)-2);
  484. rt_memcpy(recv_buf,rt_strstr(data,":")+1,temp_size);
  485. if (at_client_obj_recv(client, recv_buf+temp_size, bfsz-temp_size, timeout) != (bfsz-temp_size))
  486. {
  487. LOG_E("a9g device(%s) receive size(%d) data failed.", device->name, bfsz);
  488. rt_free(recv_buf);
  489. return;
  490. }
  491. /* get AT socket object by device socket descriptor */
  492. socket = &(device->sockets[device_socket]);
  493. /* notice the receive buffer and buffer size */
  494. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  495. {
  496. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  497. }
  498. }
  499. /* A9G device URC table for the socket data */
  500. static const struct at_urc urc_table[] =
  501. {
  502. {"", "CONNECT OK\r\n", urc_connect_func},
  503. {"", ",CONNECT FAIL\r\n", urc_connect_func},
  504. {"", ",SEND FAIL\r\n", urc_send_func},
  505. {"CLOSED:", "\r\n", urc_close_func},
  506. {"+CIPRCV,", "\r\n", urc_recv_func},
  507. };
  508. static const struct at_socket_ops a9g_socket_ops =
  509. {
  510. a9g_socket_connect,
  511. a9g_socket_close,
  512. a9g_socket_send,
  513. a9g_domain_resolve,
  514. a9g_socket_set_event_cb,
  515. };
  516. int a9g_socket_init(struct at_device *device)
  517. {
  518. RT_ASSERT(device);
  519. /* register URC data execution function */
  520. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  521. return RT_EOK;
  522. }
  523. int a9g_socket_class_register(struct at_device_class *class)
  524. {
  525. RT_ASSERT(class);
  526. class->socket_num = AT_DEVICE_A9G_SOCKETS_NUM;
  527. class->socket_ops = &a9g_socket_ops;
  528. return RT_EOK;
  529. }
  530. #endif /* AT_DEVICE_USING_A9G && AT_USING_SOCKET */