at_socket_sim800c.c 18 KB

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