at_socket_esp32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * File : at_socket_esp32.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. * 2018-06-20 chenyong first version
  23. * 2019-05-09 chenyong multi AT socket client support
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <at_device_esp32.h>
  28. #define LOG_TAG "at.skt.esp32"
  29. #include <at_log.h>
  30. #if defined(AT_DEVICE_USING_ESP32) && defined(AT_USING_SOCKET)
  31. #define ESP32_MODULE_SEND_MAX_SIZE 2048
  32. /* set real event by current socket and current state */
  33. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  34. /* AT socket event type */
  35. #define ESP32_EVENT_CONN_OK (1L << 0)
  36. #define ESP32_EVENT_SEND_OK (1L << 1)
  37. #define ESP32_EVENT_RECV_OK (1L << 2)
  38. #define ESP32_EVNET_CLOSE_OK (1L << 3)
  39. #define ESP32_EVENT_CONN_FAIL (1L << 4)
  40. #define ESP32_EVENT_SEND_FAIL (1L << 5)
  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 int esp32_socket_event_send(struct at_device *device, uint32_t event)
  46. {
  47. return (int) rt_event_send(device->socket_event, event);
  48. }
  49. static int esp32_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  50. {
  51. int result = 0;
  52. rt_uint32_t recved;
  53. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  54. if (result != RT_EOK)
  55. {
  56. return -RT_ETIMEOUT;
  57. }
  58. return recved;
  59. }
  60. /**
  61. * close socket by AT commands.
  62. *
  63. * @param current socket
  64. *
  65. * @return 0: close socket success
  66. * -1: send AT commands error
  67. * -2: wait socket event timeout
  68. * -5: no memory
  69. */
  70. static int esp32_socket_close(struct at_socket *socket)
  71. {
  72. int result = RT_EOK;
  73. at_response_t resp = RT_NULL;
  74. int device_socket = (int) socket->user_data;
  75. struct at_device *device = (struct at_device *) socket->device;
  76. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  77. if (resp == RT_NULL)
  78. {
  79. LOG_E("no memory for resp create.");
  80. return -RT_ENOMEM;
  81. }
  82. result = at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE=%d", device_socket);
  83. if (resp)
  84. {
  85. at_delete_resp(resp);
  86. }
  87. return result;
  88. }
  89. /**
  90. * create TCP/UDP client or server connect by AT commands.
  91. *
  92. * @param socket current socket
  93. * @param ip server or client IP address
  94. * @param port server or client port
  95. * @param type connect socket type(tcp, udp)
  96. * @param is_client connection is client
  97. *
  98. * @return 0: connect success
  99. * -1: connect failed, send commands error or type error
  100. * -2: wait socket event timeout
  101. * -5: no memory
  102. */
  103. static int esp32_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  104. {
  105. int result = RT_EOK;
  106. rt_bool_t retryed = RT_FALSE;
  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. RT_ASSERT(ip);
  111. RT_ASSERT(port >= 0);
  112. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  113. if (resp == RT_NULL)
  114. {
  115. LOG_E("no memory for resp create.");
  116. return -RT_ENOMEM;
  117. }
  118. __retry:
  119. if (is_client)
  120. {
  121. switch (type)
  122. {
  123. case AT_SOCKET_TCP:
  124. /* send AT commands to connect TCP server */
  125. if (at_obj_exec_cmd(device->client, resp,
  126. "AT+CIPSTART=%d,\"TCP\",\"%s\",%d,60", device_socket, ip, port) < 0)
  127. {
  128. result = -RT_ERROR;
  129. }
  130. break;
  131. case AT_SOCKET_UDP:
  132. if (at_obj_exec_cmd(device->client, resp,
  133. "AT+CIPSTART=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
  134. {
  135. result = -RT_ERROR;
  136. }
  137. break;
  138. default:
  139. LOG_E("not supported connect type %d.", type);
  140. result = -RT_ERROR;
  141. goto __exit;
  142. }
  143. }
  144. if (result != RT_EOK && retryed == RT_FALSE)
  145. {
  146. LOG_D("%s device socket (%d) connect failed, the socket was not be closed and now will connect retry.",
  147. device->name, device_socket);
  148. if (esp32_socket_close(socket) < 0)
  149. {
  150. goto __exit;
  151. }
  152. retryed = RT_TRUE;
  153. result = RT_EOK;
  154. goto __retry;
  155. }
  156. __exit:
  157. if (resp)
  158. {
  159. at_delete_resp(resp);
  160. }
  161. return result;
  162. }
  163. /**
  164. * send data to server or client by AT commands.
  165. *
  166. * @param socket current socket
  167. * @param buff send buffer
  168. * @param bfsz send buffer size
  169. * @param type connect socket type(tcp, udp)
  170. *
  171. * @return >=0: the size of send success
  172. * -1: send AT commands error or send data error
  173. * -2: waited socket event timeout
  174. * -5: no memory
  175. */
  176. static int esp32_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  177. {
  178. int result = RT_EOK;
  179. int event_result = 0;
  180. size_t cur_pkt_size = 0, sent_size = 0;
  181. at_response_t resp = RT_NULL;
  182. int device_socket = (int) socket->user_data;
  183. struct at_device *device = (struct at_device *) socket->device;
  184. struct at_device_esp32 *esp32 = (struct at_device_esp32 *) device->user_data;
  185. rt_mutex_t lock = device->client->lock;
  186. RT_ASSERT(buff);
  187. RT_ASSERT(bfsz > 0);
  188. resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
  189. if (resp == RT_NULL)
  190. {
  191. LOG_E("no memory for resp create.");
  192. return -RT_ENOMEM;
  193. }
  194. rt_mutex_take(lock, RT_WAITING_FOREVER);
  195. /* set current socket for send URC event */
  196. esp32->user_data = (void *) device_socket;
  197. /* set AT client end sign to deal with '>' sign */
  198. at_obj_set_end_sign(device->client, '>');
  199. while (sent_size < bfsz)
  200. {
  201. if (bfsz - sent_size < ESP32_MODULE_SEND_MAX_SIZE)
  202. {
  203. cur_pkt_size = bfsz - sent_size;
  204. }
  205. else
  206. {
  207. cur_pkt_size = ESP32_MODULE_SEND_MAX_SIZE;
  208. }
  209. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line */
  210. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
  211. {
  212. result = -RT_ERROR;
  213. goto __exit;
  214. }
  215. /* send the real data to server or client */
  216. result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  217. if (result == 0)
  218. {
  219. result = -RT_ERROR;
  220. goto __exit;
  221. }
  222. /* waiting result event from AT URC */
  223. if (esp32_socket_event_recv(device, SET_EVENT(device_socket, 0),
  224. 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  225. {
  226. LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
  227. result = -RT_ETIMEOUT;
  228. goto __exit;
  229. }
  230. /* waiting OK or failed result */
  231. event_result = esp32_socket_event_recv(device, ESP32_EVENT_SEND_OK | ESP32_EVENT_SEND_FAIL,
  232. 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  233. if (event_result < 0)
  234. {
  235. LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
  236. result = -RT_ETIMEOUT;
  237. goto __exit;
  238. }
  239. /* check result */
  240. if (event_result & ESP32_EVENT_SEND_FAIL)
  241. {
  242. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  243. result = -RT_ERROR;
  244. goto __exit;
  245. }
  246. sent_size += cur_pkt_size;
  247. }
  248. __exit:
  249. /* reset the end sign for data */
  250. at_obj_set_end_sign(device->client, 0);
  251. rt_mutex_release(lock);
  252. if (resp)
  253. {
  254. at_delete_resp(resp);
  255. }
  256. return result > 0 ? sent_size : result;
  257. }
  258. /**
  259. * domain resolve by AT commands.
  260. *
  261. * @param name domain name
  262. * @param ip parsed IP address, it's length must be 16
  263. *
  264. * @return 0: domain resolve success
  265. * -2: wait socket event timeout
  266. * -5: no memory
  267. */
  268. static int esp32_domain_resolve(const char *name, char ip[16])
  269. {
  270. #define RESOLVE_RETRY 5
  271. int i, result = RT_EOK;
  272. char recv_ip[16] = { 0 };
  273. at_response_t resp = RT_NULL;
  274. struct at_device *device = RT_NULL;
  275. RT_ASSERT(name);
  276. RT_ASSERT(ip);
  277. device = at_device_get_first_initialized();
  278. if (device == RT_NULL)
  279. {
  280. LOG_E("get first init device failed.");
  281. return -RT_ERROR;
  282. }
  283. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  284. if (resp == RT_NULL)
  285. {
  286. LOG_E("no memory for resp create.");
  287. return -RT_ENOMEM;
  288. }
  289. for (i = 0; i < RESOLVE_RETRY; i++)
  290. {
  291. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", name) < 0)
  292. {
  293. result = -RT_ERROR;
  294. goto __exit;
  295. }
  296. /* parse the third line of response data, get the IP address */
  297. if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%s", recv_ip) < 0)
  298. {
  299. rt_thread_mdelay(100);
  300. /* resolve failed, maybe receive an URC CRLF */
  301. continue;
  302. }
  303. if (rt_strlen(recv_ip) < 8)
  304. {
  305. rt_thread_mdelay(100);
  306. /* resolve failed, maybe receive an URC CRLF */
  307. continue;
  308. }
  309. else
  310. {
  311. rt_strncpy(ip, recv_ip, 15);
  312. ip[15] = '\0';
  313. break;
  314. }
  315. }
  316. __exit:
  317. if (resp)
  318. {
  319. at_delete_resp(resp);
  320. }
  321. return result;
  322. }
  323. /**
  324. * set AT socket event notice callback
  325. *
  326. * @param event notice event
  327. * @param cb notice callback
  328. */
  329. static void esp32_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  330. {
  331. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  332. {
  333. at_evt_cb_set[event] = cb;
  334. }
  335. }
  336. static const struct at_socket_ops esp32_socket_ops =
  337. {
  338. esp32_socket_connect,
  339. esp32_socket_close,
  340. esp32_socket_send,
  341. esp32_domain_resolve,
  342. esp32_socket_set_event_cb,
  343. };
  344. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  345. {
  346. int device_socket = 0;
  347. struct at_device *device = RT_NULL;
  348. struct at_device_esp32 *esp32 = RT_NULL;
  349. char *client_name = client->device->parent.name;
  350. RT_ASSERT(data && size);
  351. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  352. if (device == RT_NULL)
  353. {
  354. LOG_E("get device(%s) failed.", client_name);
  355. return;
  356. }
  357. esp32 = (struct at_device_esp32 *) device->user_data;
  358. device_socket = (int) esp32->user_data;
  359. if (rt_strstr(data, "SEND OK"))
  360. {
  361. esp32_socket_event_send(device, SET_EVENT(device_socket, ESP32_EVENT_SEND_OK));
  362. }
  363. else if (rt_strstr(data, "SEND FAIL"))
  364. {
  365. esp32_socket_event_send(device, SET_EVENT(device_socket, ESP32_EVENT_SEND_FAIL));
  366. }
  367. }
  368. static void urc_send_bfsz_func(struct at_client *client, const char *data, rt_size_t size)
  369. {
  370. static int cur_send_bfsz = 0;
  371. RT_ASSERT(data && size);
  372. sscanf(data, "Recv %d bytes", &cur_send_bfsz);
  373. }
  374. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  375. {
  376. int index = 0;
  377. struct at_socket *socket = RT_NULL;
  378. struct at_device *device = RT_NULL;
  379. char *client_name = client->device->parent.name;
  380. RT_ASSERT(data && size);
  381. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  382. if (device == RT_NULL)
  383. {
  384. LOG_E("get device(%s) failed.", client_name);
  385. return;
  386. }
  387. sscanf(data, "%d,CLOSED", &index);
  388. socket = &(device->sockets[index]);
  389. /* notice the socket is disconnect by remote */
  390. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  391. {
  392. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  393. }
  394. }
  395. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  396. {
  397. int device_socket = 0;
  398. rt_int32_t timeout = 0;
  399. rt_size_t bfsz = 0, temp_size = 0;
  400. char *recv_buf = RT_NULL, temp[8] = {0};
  401. struct at_socket *socket = RT_NULL;
  402. struct at_device *device = RT_NULL;
  403. char *client_name = client->device->parent.name;
  404. RT_ASSERT(data && size);
  405. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  406. if (device == RT_NULL)
  407. {
  408. LOG_E("get device(%s) failed.", client_name);
  409. return;
  410. }
  411. /* get the at deveice socket and receive buffer size by receive data */
  412. sscanf(data, "+IPD,%d,%d:", &device_socket, (int *) &bfsz);
  413. /* set receive timeout by receive buffer length, not less than 10ms */
  414. timeout = bfsz > 10 ? bfsz : 10;
  415. if (device_socket < 0 || bfsz == 0)
  416. return;
  417. recv_buf = (char *) rt_calloc(1, bfsz);
  418. if (recv_buf == RT_NULL)
  419. {
  420. LOG_E("no memory receive buffer(%d).", bfsz);
  421. /* read and clean the coming data */
  422. while (temp_size < bfsz)
  423. {
  424. if (bfsz - temp_size > sizeof(temp))
  425. {
  426. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  427. }
  428. else
  429. {
  430. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  431. }
  432. temp_size += sizeof(temp);
  433. }
  434. return;
  435. }
  436. /* sync receive data */
  437. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  438. {
  439. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  440. rt_free(recv_buf);
  441. return;
  442. }
  443. /* get at socket object by device socket descriptor */
  444. socket = &(device->sockets[device_socket]);
  445. /* notice the receive buffer and buffer size */
  446. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  447. {
  448. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  449. }
  450. }
  451. static const struct at_urc urc_table[] =
  452. {
  453. {"SEND OK", "\r\n", urc_send_func},
  454. {"SEND FAIL", "\r\n", urc_send_func},
  455. {"Recv", "bytes\r\n", urc_send_bfsz_func},
  456. {"", ",CLOSED\r\n", urc_close_func},
  457. {"+IPD", ":", urc_recv_func},
  458. };
  459. int esp32_socket_init(struct at_device *device)
  460. {
  461. RT_ASSERT(device);
  462. /* register URC data execution function */
  463. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  464. return RT_EOK;
  465. }
  466. int esp32_socket_class_register(struct at_device_class *class)
  467. {
  468. RT_ASSERT(class);
  469. class->socket_num = AT_DEVICE_ESP32_SOCKETS_NUM;
  470. class->socket_ops = &esp32_socket_ops;
  471. return RT_EOK;
  472. }
  473. #endif /* AT_DEVICE_USING_ESP32 && AT_USING_SOCKET */