at_socket_m6315.c 18 KB

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