at_socket_esp32.c 15 KB

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