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