at_socket_mw31.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. * 2019-06-23 flybreak first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <at_device_mw31.h>
  13. #define LOG_TAG "at.skt.mw31"
  14. #include <at_log.h>
  15. #if defined(AT_DEVICE_USING_MW31) && defined(AT_USING_SOCKET)
  16. #define MW31_MODULE_SEND_MAX_SIZE 1024
  17. /* set real event by current socket and current state */
  18. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  19. /* AT socket event type */
  20. #define MW31_EVENT_CONN_OK (1L << 0)
  21. #define MW31_EVENT_SEND_OK (1L << 1)
  22. #define MW31_EVENT_RECV_OK (1L << 2)
  23. #define MW31_EVNET_CLOSE_OK (1L << 3)
  24. #define MW31_EVENT_CONN_FAIL (1L << 4)
  25. #define MW31_EVENT_SEND_FAIL (1L << 5)
  26. static at_evt_cb_t at_evt_cb_set[] =
  27. {
  28. [AT_SOCKET_EVT_RECV] = NULL,
  29. [AT_SOCKET_EVT_CLOSED] = NULL,
  30. };
  31. /**
  32. * close socket by AT commands.
  33. *
  34. * @param current socket
  35. *
  36. * @return 0: close socket success
  37. * -1: send AT commands error
  38. * -2: wait socket event timeout
  39. * -5: no memory
  40. */
  41. static int mw31_socket_close(struct at_socket *socket)
  42. {
  43. int result = RT_EOK;
  44. at_response_t resp = RT_NULL;
  45. int device_socket = (int) socket->user_data;
  46. struct at_device *device = (struct at_device *) socket->device;
  47. char type[15] = {0}, status[15] = {0};
  48. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  49. if (resp == RT_NULL)
  50. {
  51. LOG_E("no memory for resp create.");
  52. return -RT_ENOMEM;
  53. }
  54. at_obj_exec_cmd(device->client, resp, "AT+CIPSTATUS=%d", device_socket);
  55. if (at_resp_parse_line_args_by_kw(resp, "+CIPSTATU:", "+CIPSTATU:%[^,],%s", type, status) > 0)
  56. {
  57. LOG_D("%s\n%s\n", type, status);
  58. }
  59. if (status[0] == 'd' || status[2] == 'o')
  60. {
  61. goto __exit;
  62. }
  63. result = at_obj_exec_cmd(device->client, resp, "AT+CIPSTOP=%d", device_socket);
  64. __exit:
  65. if (resp)
  66. {
  67. at_delete_resp(resp);
  68. }
  69. return result;
  70. }
  71. /**
  72. * create TCP/UDP client or server connect by AT commands.
  73. *
  74. * @param socket current socket
  75. * @param ip server or client IP address
  76. * @param port server or client port
  77. * @param type connect socket type(tcp, udp)
  78. * @param is_client connection is client
  79. *
  80. * @return 0: connect success
  81. * -1: connect failed, send commands error or type error
  82. * -2: wait socket event timeout
  83. * -5: no memory
  84. */
  85. static int mw31_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  86. {
  87. int result = RT_EOK;
  88. rt_bool_t retryed = RT_FALSE;
  89. at_response_t resp = RT_NULL;
  90. int device_socket = (int) socket->user_data;
  91. struct at_device *device = (struct at_device *) socket->device;
  92. RT_ASSERT(ip);
  93. RT_ASSERT(port >= 0);
  94. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  95. if (resp == RT_NULL)
  96. {
  97. LOG_E("no memory for resp create.");
  98. return -RT_ENOMEM;
  99. }
  100. __retry:
  101. if (is_client)
  102. {
  103. switch (type)
  104. {
  105. case AT_SOCKET_TCP:
  106. /* send AT commands to connect TCP server */
  107. if (at_obj_exec_cmd(device->client, resp,
  108. "AT+CIPSTART=%d,tcp_client,%s,%d,%d", device_socket, ip, port, device_socket) < 0)
  109. {
  110. result = -RT_ERROR;
  111. }
  112. break;
  113. case AT_SOCKET_UDP:
  114. if (at_obj_exec_cmd(device->client, resp,
  115. "AT+CIPSTART=%d,udp_unicast,%s,%d,%d", device_socket, ip, port, device_socket) < 0)
  116. {
  117. result = -RT_ERROR;
  118. }
  119. break;
  120. default:
  121. LOG_E("not supported connect type %d.", type);
  122. result = -RT_ERROR;
  123. goto __exit;
  124. }
  125. }
  126. if (result != RT_EOK && retryed == RT_FALSE)
  127. {
  128. LOG_D("%s device socket (%d) connect failed, the socket was not be closed and now will connect retry.",
  129. device->name, device_socket);
  130. if (mw31_socket_close(socket) < 0)
  131. {
  132. goto __exit;
  133. }
  134. retryed = RT_TRUE;
  135. result = RT_EOK;
  136. goto __retry;
  137. }
  138. __exit:
  139. if (resp)
  140. {
  141. at_delete_resp(resp);
  142. }
  143. return result;
  144. }
  145. /**
  146. * send data to server or client by AT commands.
  147. *
  148. * @param socket current socket
  149. * @param buff send buffer
  150. * @param bfsz send buffer size
  151. * @param type connect socket type(tcp, udp)
  152. *
  153. * @return >=0: the size of send success
  154. * -1: send AT commands error or send data error
  155. * -2: waited socket event timeout
  156. * -5: no memory
  157. */
  158. static int mw31_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  159. {
  160. int result = RT_EOK;
  161. size_t cur_pkt_size = 0, sent_size = 0;
  162. at_response_t resp = RT_NULL;
  163. int device_socket = (int) socket->user_data;
  164. struct at_device *device = (struct at_device *) socket->device;
  165. struct at_device_mw31 *mw31 = (struct at_device_mw31 *) device->user_data;
  166. rt_mutex_t lock = at_device_get_client_lock(device);
  167. char send_buf[20] = {0};
  168. RT_ASSERT(buff);
  169. RT_ASSERT(bfsz > 0);
  170. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  171. if (resp == RT_NULL)
  172. {
  173. LOG_E("no memory for resp create.");
  174. return -RT_ENOMEM;
  175. }
  176. rt_mutex_take(lock, RT_WAITING_FOREVER);
  177. /* set current socket for send URC event */
  178. mw31->user_data = (void *) device_socket;
  179. /* set AT client end sign to deal with '>' sign */
  180. at_obj_set_end_sign(device->client, '>');
  181. while (sent_size < bfsz)
  182. {
  183. if (bfsz - sent_size < MW31_MODULE_SEND_MAX_SIZE)
  184. {
  185. cur_pkt_size = bfsz - sent_size;
  186. }
  187. else
  188. {
  189. cur_pkt_size = MW31_MODULE_SEND_MAX_SIZE;
  190. }
  191. rt_sprintf(send_buf, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size);
  192. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line */
  193. at_client_obj_send(device->client, send_buf, strlen(send_buf));
  194. at_client_obj_send(device->client, "\r", 1);
  195. /* send the real data to server or client */
  196. result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  197. if (result == 0)
  198. {
  199. result = -RT_ERROR;
  200. goto __exit;
  201. }
  202. sent_size += cur_pkt_size;
  203. }
  204. __exit:
  205. /* reset the end sign for data */
  206. at_obj_set_end_sign(device->client, 0);
  207. rt_mutex_release(lock);
  208. if (resp)
  209. {
  210. at_delete_resp(resp);
  211. }
  212. return result > 0 ? sent_size : result;
  213. }
  214. /**
  215. * domain resolve by AT commands.
  216. *
  217. * @param name domain name
  218. * @param ip parsed IP address, it's length must be 16
  219. *
  220. * @return 0: domain resolve success
  221. * -2: wait socket event timeout
  222. * -5: no memory
  223. */
  224. static int mw31_domain_resolve(const char *name, char ip[16])
  225. {
  226. #define RESOLVE_RETRY 5
  227. int i, result = RT_EOK;
  228. rt_uint8_t recv_ip_num = 0;
  229. char recv_ip[16] = { 0 };
  230. at_response_t resp = RT_NULL;
  231. struct at_device *device = RT_NULL;
  232. RT_ASSERT(name);
  233. RT_ASSERT(ip);
  234. device = at_device_get_first_initialized();
  235. if (device == RT_NULL)
  236. {
  237. LOG_E("get first init device failed.");
  238. return -RT_ERROR;
  239. }
  240. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  241. if (resp == RT_NULL)
  242. {
  243. LOG_E("no memory for resp create.");
  244. return -RT_ENOMEM;
  245. }
  246. for (i = 0; i < RESOLVE_RETRY; i++)
  247. {
  248. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=%s", name) < 0)
  249. {
  250. result = -RT_ERROR;
  251. goto __exit;
  252. }
  253. /* parse the third line of response data, get the IP address */
  254. if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%d", &recv_ip_num) < 0 ||
  255. recv_ip_num == 0 ||
  256. at_resp_parse_line_args(resp, 3, "%s", recv_ip) < 0)
  257. {
  258. rt_thread_mdelay(100);
  259. /* resolve failed, maybe receive an URC CRLF */
  260. continue;
  261. }
  262. if (rt_strlen(recv_ip) < 8)
  263. {
  264. rt_thread_mdelay(100);
  265. /* resolve failed, maybe receive an URC CRLF */
  266. continue;
  267. }
  268. else
  269. {
  270. rt_strncpy(ip, recv_ip, 15);
  271. ip[15] = '\0';
  272. break;
  273. }
  274. }
  275. __exit:
  276. if (resp)
  277. {
  278. at_delete_resp(resp);
  279. }
  280. return result;
  281. }
  282. /**
  283. * set AT socket event notice callback
  284. *
  285. * @param event notice event
  286. * @param cb notice callback
  287. */
  288. static void mw31_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  289. {
  290. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  291. {
  292. at_evt_cb_set[event] = cb;
  293. }
  294. }
  295. static const struct at_socket_ops mw31_socket_ops =
  296. {
  297. mw31_socket_connect,
  298. mw31_socket_close,
  299. mw31_socket_send,
  300. mw31_domain_resolve,
  301. mw31_socket_set_event_cb,
  302. #if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
  303. RT_NULL,
  304. #endif
  305. };
  306. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  307. {
  308. int device_socket = 0;
  309. rt_int32_t timeout = 0;
  310. rt_size_t bfsz = 0, temp_size = 0;
  311. char *recv_buf = RT_NULL, temp[8] = {0};
  312. struct at_socket *socket = RT_NULL;
  313. struct at_device *device = RT_NULL;
  314. char *client_name = client->device->parent.name;
  315. rt_uint8_t i;
  316. RT_ASSERT(data && size);
  317. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  318. if (device == RT_NULL)
  319. {
  320. LOG_E("get device(%s) failed.", client_name);
  321. return;
  322. }
  323. at_client_obj_recv(client, temp, 2, 1000);
  324. /* get the at deveice socket and receive buffer size by receive data */
  325. rt_sscanf(temp, "%d,", &device_socket);
  326. temp[0] = 0;
  327. temp[1] = 0;
  328. for (i = 0; i < 6; i++)
  329. {
  330. if (i > 0 && temp[i - 1] == ',')
  331. {
  332. break;
  333. }
  334. at_client_obj_recv(client, &temp[i], 1, 1000);
  335. }
  336. rt_sscanf(temp, "%ld,", &bfsz);
  337. LOG_D("socket:%d, size:%ld\n", device_socket, bfsz);
  338. /* set receive timeout by receive buffer length, not less than 10 ms */
  339. timeout = bfsz > 10 ? bfsz : 10;
  340. if (device_socket < 0 || bfsz == 0)
  341. return;
  342. recv_buf = (char *) rt_calloc(1, bfsz);
  343. if (recv_buf == RT_NULL)
  344. {
  345. LOG_E("no memory for receive buffer(%d).", bfsz);
  346. /* read and clean the coming data */
  347. while (temp_size < bfsz)
  348. {
  349. if (bfsz - temp_size > sizeof(temp))
  350. {
  351. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  352. }
  353. else
  354. {
  355. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  356. }
  357. temp_size += sizeof(temp);
  358. }
  359. return;
  360. }
  361. /* sync receive data */
  362. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  363. {
  364. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  365. rt_free(recv_buf);
  366. return;
  367. }
  368. /* get at socket object by device socket descriptor */
  369. socket = &(device->sockets[device_socket]);
  370. /* notice the receive buffer and buffer size */
  371. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  372. {
  373. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  374. }
  375. }
  376. static const struct at_urc urc_table[] =
  377. {
  378. {"+CIPEVENT:SOCKET", ",", urc_recv_func},
  379. {":SOCKET", ",", urc_recv_func},
  380. };
  381. int mw31_socket_init(struct at_device *device)
  382. {
  383. RT_ASSERT(device);
  384. /* register URC data execution function */
  385. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  386. return RT_EOK;
  387. }
  388. int mw31_socket_class_register(struct at_device_class *class)
  389. {
  390. RT_ASSERT(class);
  391. class->socket_num = AT_DEVICE_MW31_SOCKETS_NUM;
  392. class->socket_ops = &mw31_socket_ops;
  393. return RT_EOK;
  394. }
  395. #endif /* AT_DEVICE_USING_MW31 && AT_USING_SOCKET */