at_socket_rw007.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * File : at_socket_rw007.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-12 chenyong multi AT socket client support
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <at_device_rw007.h>
  28. #define LOG_TAG "at.skt.rw007"
  29. #include <at_log.h>
  30. #if defined(AT_DEVICE_USING_RW007) && defined(AT_USING_SOCKET)
  31. #define RW007_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 RW007_EVENT_CONN_OK (1L << 0)
  36. #define RW007_EVENT_SEND_OK (1L << 1)
  37. #define RW007_EVENT_RECV_OK (1L << 2)
  38. #define RW007_EVNET_CLOSE_OK (1L << 3)
  39. #define RW007_EVENT_CONN_FAIL (1L << 4)
  40. #define RW007_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 rw007_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 rw007_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  50. {
  51. int result = RT_EOK;
  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 rw007_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_PER_SECOND);
  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 rw007_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, socket);
  148. if (rw007_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 rw007_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_rw007 *rw007 = (struct at_device_rw007 *) 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. rw007->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 < RW007_MODULE_SEND_MAX_SIZE)
  202. {
  203. cur_pkt_size = bfsz - sent_size;
  204. }
  205. else
  206. {
  207. cur_pkt_size = RW007_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 (rw007_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  224. {
  225. LOG_E("%s device socket(%d) wait send result timeout.", device->name, device_socket);
  226. result = -RT_ETIMEOUT;
  227. goto __exit;
  228. }
  229. /* waiting OK or failed result */
  230. event_result = rw007_socket_event_recv(device, RW007_EVENT_SEND_OK | RW007_EVENT_SEND_FAIL,
  231. 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  232. if (event_result < 0)
  233. {
  234. LOG_E("%s device socket(%d) wait send OK|FAIL timeout.", device->name, device_socket);
  235. result = -RT_ETIMEOUT;
  236. goto __exit;
  237. }
  238. /* check result */
  239. if (event_result & RW007_EVENT_SEND_FAIL)
  240. {
  241. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  242. result = -RT_ERROR;
  243. goto __exit;
  244. }
  245. sent_size += cur_pkt_size;
  246. }
  247. __exit:
  248. /* reset the end sign for data */
  249. at_obj_set_end_sign(device->client, 0);
  250. rt_mutex_release(lock);
  251. if (resp)
  252. {
  253. at_delete_resp(resp);
  254. }
  255. return result > 0 ? sent_size : result;
  256. }
  257. /**
  258. * domain resolve by AT commands.
  259. *
  260. * @param name domain name
  261. * @param ip parsed IP address, it's length must be 16
  262. *
  263. * @return 0: domain resolve success
  264. * -2: wait socket event timeout
  265. * -5: no memory
  266. */
  267. static int rw007_domain_resolve(const char *name, char ip[16])
  268. {
  269. #define RESOLVE_RETRY 5
  270. int i, result = RT_EOK;
  271. char recv_ip[16] = { 0 };
  272. at_response_t resp = RT_NULL;
  273. struct at_device *device = RT_NULL;
  274. RT_ASSERT(name);
  275. RT_ASSERT(ip);
  276. device = at_device_get_first_initialized();
  277. if (device == RT_NULL)
  278. {
  279. LOG_E("get first init device failed.");
  280. return -RT_ERROR;
  281. }
  282. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  283. if (resp == RT_NULL)
  284. {
  285. LOG_E("no memory for resp create.");
  286. return -RT_ENOMEM;
  287. }
  288. for (i = 0; i < RESOLVE_RETRY; i++)
  289. {
  290. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", name) < 0)
  291. {
  292. result = -RT_ERROR;
  293. goto __exit;
  294. }
  295. /* parse the third line of response data, get the IP address */
  296. if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%s", recv_ip) < 0)
  297. {
  298. rt_thread_mdelay(100);
  299. /* resolve failed, maybe receive an URC CRLF */
  300. continue;
  301. }
  302. if (rt_strlen(recv_ip) < 8)
  303. {
  304. rt_thread_mdelay(100);
  305. /* resolve failed, maybe receive an URC CRLF */
  306. continue;
  307. }
  308. else
  309. {
  310. rt_strncpy(ip, recv_ip, 15);
  311. ip[15] = '\0';
  312. break;
  313. }
  314. }
  315. __exit:
  316. if (resp)
  317. {
  318. at_delete_resp(resp);
  319. }
  320. return result;
  321. }
  322. /**
  323. * set AT socket event notice callback
  324. *
  325. * @param event notice event
  326. * @param cb notice callback
  327. */
  328. static void rw007_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  329. {
  330. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  331. {
  332. at_evt_cb_set[event] = cb;
  333. }
  334. }
  335. static const struct at_socket_ops rw007_socket_ops =
  336. {
  337. rw007_socket_connect,
  338. rw007_socket_close,
  339. rw007_socket_send,
  340. rw007_domain_resolve,
  341. rw007_socket_set_event_cb,
  342. };
  343. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  344. {
  345. int device_socket = 0;
  346. struct at_device *device = RT_NULL;
  347. struct at_device_rw007 *rw007 = RT_NULL;
  348. char *client_name = client->device->parent.name;
  349. RT_ASSERT(data && size);
  350. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  351. if (device == RT_NULL)
  352. {
  353. LOG_E("get device(%s) failed.", client_name);
  354. return;
  355. }
  356. rw007 = (struct at_device_rw007 *) device->user_data;
  357. device_socket = (int) rw007->user_data;
  358. if (rt_strstr(data, "SEND OK"))
  359. {
  360. rw007_socket_event_send(device, SET_EVENT(device_socket, RW007_EVENT_SEND_OK));
  361. }
  362. else if (rt_strstr(data, "SEND FAIL"))
  363. {
  364. rw007_socket_event_send(device, SET_EVENT(device_socket, RW007_EVENT_SEND_FAIL));
  365. }
  366. }
  367. static void urc_send_bfsz_func(struct at_client *client, const char *data, rt_size_t size)
  368. {
  369. static int cur_send_bfsz = 0;
  370. RT_ASSERT(data && size);
  371. sscanf(data, "Recv %d bytes", &cur_send_bfsz);
  372. }
  373. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  374. {
  375. int device_socket = 0;
  376. struct at_socket *socket = RT_NULL;
  377. struct at_device *device = RT_NULL;
  378. char *client_name = client->device->parent.name;
  379. RT_ASSERT(data && size);
  380. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  381. if (device == RT_NULL)
  382. {
  383. LOG_E("get device(%s) failed.", client_name);
  384. return;
  385. }
  386. sscanf(data, "%d,CLOSED", &device_socket);
  387. /* get at socket object by device socket descriptor */
  388. socket = &(device->sockets[device_socket]);
  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 current 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 10 ms */
  414. timeout = bfsz > 10 ? bfsz : 10;
  415. if (device_socket < 0 || bfsz == 0)
  416. {
  417. return;
  418. }
  419. recv_buf = (char *) rt_calloc(1, bfsz);
  420. if (recv_buf == RT_NULL)
  421. {
  422. LOG_E("no memory for receive buffer (%d).", bfsz);
  423. /* read and clean the coming data */
  424. while (temp_size < bfsz)
  425. {
  426. if (bfsz - temp_size > sizeof(temp))
  427. {
  428. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  429. }
  430. else
  431. {
  432. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  433. }
  434. temp_size += sizeof(temp);
  435. }
  436. return;
  437. }
  438. /* sync receive data */
  439. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  440. {
  441. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  442. rt_free(recv_buf);
  443. return;
  444. }
  445. /* get at socket object by device socket descriptor */
  446. socket = &(device->sockets[device_socket]);
  447. /* notice the receive buffer and buffer size */
  448. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  449. {
  450. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  451. }
  452. }
  453. static struct at_urc urc_table[] =
  454. {
  455. {"SEND OK", "\r\n", urc_send_func},
  456. {"SEND FAIL", "\r\n", urc_send_func},
  457. {"Recv", "bytes\r\n", urc_send_bfsz_func},
  458. {"", ",CLOSED\r\n", urc_close_func},
  459. {"+IPD", ":", urc_recv_func},
  460. };
  461. int rw007_socket_init(struct at_device *device)
  462. {
  463. RT_ASSERT(device);
  464. /* register URC data execution function */
  465. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  466. return RT_EOK;
  467. }
  468. int rw007_socket_class_register(struct at_device_class *class)
  469. {
  470. RT_ASSERT(class);
  471. class->socket_num = AT_DEVICE_RW007_SOCKETS_NUM;
  472. class->socket_ops = &rw007_socket_ops;
  473. return RT_EOK;
  474. }
  475. #endif /* AT_DEVICE_USING_RW007 && AT_USING_SOCKET */