at_socket_n720.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * File : at_socket_n720.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. * 2020-10-28 qiyongzhong first version
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <at_device_n720.h>
  27. #define LOG_TAG "at.skt.n720"
  28. #include <at_log.h>
  29. #if defined(AT_DEVICE_USING_N720) && defined(AT_USING_SOCKET)
  30. #define N720_MODULE_SEND_MAX_SIZE 2000
  31. /* set real event by current socket and current state */
  32. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  33. /* AT socket event type */
  34. #define N720_EVENT_CONN_OK (1L << 0)
  35. #define N720_EVENT_SEND_OK (1L << 1)
  36. #define N720_EVENT_RECV_OK (1L << 2)
  37. #define N720_EVNET_CLOSE_OK (1L << 3)
  38. #define N720_EVENT_CONN_FAIL (1L << 4)
  39. #define N720_EVENT_SEND_FAIL (1L << 5)
  40. #define N720_EVENT_DOMAIN_OK (1L << 6)
  41. static at_evt_cb_t at_evt_cb_set[] = {
  42. [AT_SOCKET_EVT_RECV] = NULL,
  43. [AT_SOCKET_EVT_CLOSED] = NULL,
  44. };
  45. /**
  46. * close socket by AT commands.
  47. *
  48. * @param current socket
  49. *
  50. * @return 0: close socket success
  51. * -1: send AT commands error
  52. * -2: wait socket event timeout
  53. * -5: no memory
  54. */
  55. static int n720_socket_close(struct at_socket *socket)
  56. {
  57. int result = RT_EOK;
  58. at_response_t resp = RT_NULL;
  59. int device_socket = (int) socket->user_data;
  60. struct at_device *device = (struct at_device *) socket->device;
  61. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  62. if (resp == RT_NULL)
  63. {
  64. LOG_E("no memory for resp create.");
  65. return -RT_ENOMEM;
  66. }
  67. result = at_obj_exec_cmd(device->client, resp, "AT$MYNETCLOSE=%d", device_socket);
  68. at_delete_resp(resp);
  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 n720_socket_connect(struct at_socket *socket, char *ip, int32_t port,
  86. enum at_socket_type type, rt_bool_t is_client)
  87. {
  88. int type_val = 0;
  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. if ( ! is_client)
  95. {
  96. return -RT_ERROR;
  97. }
  98. switch(type)
  99. {
  100. case AT_SOCKET_TCP:
  101. type_val = 0;
  102. break;
  103. case AT_SOCKET_UDP:
  104. type_val = 2;
  105. break;
  106. default:
  107. LOG_E("%s device socket(%d) connect type error.", device->name, device_socket);
  108. return -RT_ERROR;
  109. }
  110. resp = at_create_resp(128, 0, rt_tick_from_millisecond(15*1000));
  111. if (resp == RT_NULL)
  112. {
  113. LOG_E("no memory for resp create.");
  114. return -RT_ENOMEM;
  115. }
  116. if (at_obj_exec_cmd(device->client, resp, "AT$MYNETSRV=0,%d,%d,0,\"%s:%d\"", device_socket, type_val, ip, port) < 0)
  117. {
  118. at_delete_resp(resp);
  119. LOG_E("%s device socket(%d) config params fail.", device->name, device_socket);
  120. return -RT_ERROR;
  121. }
  122. if (at_obj_exec_cmd(device->client, resp, "AT$MYNETOPEN=%d", device_socket) < 0)
  123. {
  124. at_delete_resp(resp);
  125. LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
  126. return -RT_ERROR;
  127. }
  128. at_delete_resp(resp);
  129. return RT_EOK;
  130. }
  131. static int at_get_send_size(struct at_socket *socket, size_t *nacked)
  132. {
  133. int result = 0;
  134. at_response_t resp = RT_NULL;
  135. int device_socket = (int) socket->user_data;
  136. struct at_device *device = (struct at_device *) socket->device;
  137. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  138. if (resp == RT_NULL)
  139. {
  140. LOG_E("no memory for resp create.", device->name);
  141. return -RT_ENOMEM;
  142. }
  143. if (at_obj_exec_cmd(device->client, resp, "AT$MYNETACK=%d", device_socket) < 0)
  144. {
  145. result = -RT_ERROR;
  146. goto __exit;
  147. }
  148. if (at_resp_parse_line_args_by_kw(resp, "$MYNETACK:", "$MYNETACK:%*d,%d,%*d", nacked) <= 0)
  149. {
  150. result = -RT_ERROR;
  151. goto __exit;
  152. }
  153. __exit:
  154. if (resp)
  155. {
  156. at_delete_resp(resp);
  157. }
  158. return result;
  159. }
  160. static int at_wait_send_finish(struct at_socket *socket, size_t settings_size)
  161. {
  162. /* get the timeout by the input data size */
  163. rt_tick_t timeout = rt_tick_from_millisecond(settings_size);
  164. rt_tick_t last_time = rt_tick_get();
  165. size_t nacked = 0xFFFF;
  166. while (rt_tick_get() - last_time <= timeout)
  167. {
  168. at_get_send_size(socket, &nacked);
  169. if (nacked == 0)
  170. {
  171. return RT_EOK;
  172. }
  173. rt_thread_mdelay(100);
  174. }
  175. return -RT_ETIMEOUT;
  176. }
  177. /**
  178. * send data to server or client by AT commands.
  179. *
  180. * @param socket current socket
  181. * @param buff send buffer
  182. * @param bfsz send buffer size
  183. * @param type connect socket type(tcp, udp)
  184. *
  185. * @return >=0: the size of send success
  186. * -1: send AT commands error or send data error
  187. * -2: waited socket event timeout
  188. * -5: no memory
  189. */
  190. static int n720_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
  191. {
  192. int result = 0;
  193. size_t cur_pkt_size = 0, sent_size = 0;
  194. at_response_t resp = RT_NULL;
  195. int device_socket = (int) socket->user_data;
  196. struct at_device *device = (struct at_device *) socket->device;
  197. struct at_device_n720 *n720 = (struct at_device_n720 *) device->user_data;
  198. rt_mutex_t lock = device->client->lock;
  199. RT_ASSERT(buff);
  200. resp = at_create_resp(128, 2, rt_tick_from_millisecond(300));
  201. if (resp == RT_NULL)
  202. {
  203. LOG_E("no memory for resp create.");
  204. return -RT_ENOMEM;
  205. }
  206. rt_mutex_take(lock, RT_WAITING_FOREVER);
  207. /* set current socket for send URC event */
  208. n720->user_data = (void *) device_socket;
  209. while (sent_size < bfsz)
  210. {
  211. if (bfsz - sent_size < N720_MODULE_SEND_MAX_SIZE)
  212. {
  213. cur_pkt_size = bfsz - sent_size;
  214. }
  215. else
  216. {
  217. cur_pkt_size = N720_MODULE_SEND_MAX_SIZE;
  218. }
  219. if (at_obj_exec_cmd(device->client, resp, "AT$MYNETWRITE=%d,%d", device_socket, (int)cur_pkt_size) < 0)
  220. {
  221. result = -RT_ERROR;
  222. goto __exit;
  223. }
  224. if (at_resp_get_line_by_kw(resp, "$MYNETWRITE:") == RT_NULL)
  225. {
  226. result = -RT_ERROR;
  227. goto __exit;
  228. }
  229. /* send the real data to server or client */
  230. result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
  231. if (result == 0)
  232. {
  233. result = -RT_ERROR;
  234. goto __exit;
  235. }
  236. if (type == AT_SOCKET_TCP)
  237. {
  238. at_wait_send_finish(socket, cur_pkt_size);
  239. //rt_thread_mdelay(10);
  240. }
  241. sent_size += cur_pkt_size;
  242. }
  243. __exit:
  244. rt_mutex_release(lock);
  245. if (resp)
  246. {
  247. at_delete_resp(resp);
  248. }
  249. return result > 0 ? sent_size : result;
  250. }
  251. /**
  252. * domain resolve by AT commands.
  253. *
  254. * @param name domain name
  255. * @param ip parsed IP address, it's length must be 16
  256. *
  257. * @return 0: domain resolve success
  258. * -1: send AT commands error or response error
  259. * -2: wait socket event timeout
  260. * -5: no memory
  261. */
  262. static int n720_domain_resolve(const char *name, char ip[16])
  263. {
  264. #define RESOLVE_RETRY 3
  265. int i, result = RT_EOK;
  266. char recv_ip[20] = {0};
  267. at_response_t resp = RT_NULL;
  268. struct at_device *device = RT_NULL;
  269. RT_ASSERT(name);
  270. RT_ASSERT(ip);
  271. device = at_device_get_first_initialized();
  272. if (device == RT_NULL)
  273. {
  274. LOG_E("get first initialization n58 device failed.");
  275. return -RT_ERROR;
  276. }
  277. /* The maximum response time is 14 seconds, affected by network status */
  278. resp = at_create_resp(128, 0, 15 * RT_TICK_PER_SECOND);
  279. if (resp == RT_NULL)
  280. {
  281. LOG_E("no memory for n58 device(%s) response structure.", device->name);
  282. return -RT_ENOMEM;
  283. }
  284. for (i = 0; i < RESOLVE_RETRY; i++)
  285. {
  286. if (at_obj_exec_cmd(device->client, resp, "AT+DNS=%s", name) < 0)
  287. {
  288. result = -RT_ERROR;
  289. goto __exit;
  290. }
  291. /* parse the third line of response data, get the IP address */
  292. if (at_resp_parse_line_args_by_kw(resp, "+DNS:", "+DNS: %s", recv_ip) < 0)
  293. {
  294. rt_thread_mdelay(100);
  295. /* resolve failed, maybe receive an URC CRLF */
  296. continue;
  297. }
  298. if (rt_strlen(recv_ip) < 8)
  299. {
  300. rt_thread_mdelay(100);
  301. /* resolve failed, maybe receive an URC CRLF */
  302. continue;
  303. }
  304. else
  305. {
  306. rt_thread_mdelay(10);
  307. rt_strncpy(ip, recv_ip, 15);
  308. ip[15] = '\0';
  309. break;
  310. }
  311. }
  312. __exit:
  313. if (resp)
  314. {
  315. at_delete_resp(resp);
  316. }
  317. return result;
  318. }
  319. /**
  320. * set AT socket event notice callback
  321. *
  322. * @param event notice event
  323. * @param cb notice callback
  324. */
  325. static void n720_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  326. {
  327. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  328. {
  329. at_evt_cb_set[event] = cb;
  330. }
  331. }
  332. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  333. {
  334. int device_socket = 0;
  335. struct at_socket *socket = RT_NULL;
  336. struct at_device *device = RT_NULL;
  337. char *client_name = client->device->parent.name;
  338. RT_ASSERT(data && size);
  339. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  340. if (device == RT_NULL)
  341. {
  342. LOG_E("get device(%s) failed.", client_name);
  343. return;
  344. }
  345. sscanf(data, "$MYURCCLOSE: %d", &device_socket);
  346. /* get at socket object by device socket descriptor */
  347. socket = &(device->sockets[device_socket]);
  348. /* notice the socket is disconnect by remote */
  349. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  350. {
  351. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  352. }
  353. }
  354. static void send_net_read(struct at_client *client, int device_socket)
  355. {
  356. char *send_buf = (char *) rt_malloc(128);
  357. if (send_buf == RT_NULL)
  358. {
  359. return;
  360. }
  361. rt_sprintf(send_buf, "AT$MYNETREAD=%d,%d\r\n", device_socket, N720_MODULE_SEND_MAX_SIZE);
  362. at_client_obj_send(client, send_buf, strlen(send_buf));
  363. rt_free(send_buf);
  364. }
  365. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  366. {
  367. int device_socket = 0;
  368. struct at_device *device = RT_NULL;
  369. char *client_name = client->device->parent.name;
  370. RT_ASSERT(data && size);
  371. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  372. if (device == RT_NULL)
  373. {
  374. LOG_E("get device(%s) failed.", client_name);
  375. return;
  376. }
  377. if (sscanf(data, "$MYURCREAD: %d", &device_socket) <= 0)
  378. {
  379. return;
  380. }
  381. send_net_read(client, device_socket);
  382. }
  383. static void read_ack_func(struct at_client *client, const char *data, rt_size_t size)
  384. {
  385. int device_socket = 0;
  386. rt_int32_t timeout;
  387. int bfsz = 0, temp_size = 0;
  388. char *recv_buf = RT_NULL, temp[8] = {0};
  389. struct at_socket *socket = RT_NULL;
  390. struct at_device *device = RT_NULL;
  391. char *client_name = client->device->parent.name;
  392. RT_ASSERT(data && size);
  393. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  394. if (device == RT_NULL)
  395. {
  396. LOG_E("get device(%s) failed.", client_name);
  397. return;
  398. }
  399. /* get the current socket and receive buffer size by receive data */
  400. sscanf(data, "$MYNETREAD: %d,%d", &device_socket, &bfsz);
  401. /* set receive timeout by receive buffer length, not less than 10 ms */
  402. timeout = bfsz > 10 ? bfsz : 10;
  403. if (device_socket < 0 || bfsz == 0)
  404. {
  405. return;
  406. }
  407. recv_buf = (char *) rt_calloc(1, bfsz);
  408. if (recv_buf == RT_NULL)
  409. {
  410. LOG_E("no memory for URC receive buffer(%d).", bfsz);
  411. /* read and clean the coming data */
  412. while (temp_size < bfsz)
  413. {
  414. if (bfsz - temp_size > sizeof(temp))
  415. {
  416. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  417. }
  418. else
  419. {
  420. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  421. }
  422. temp_size += sizeof(temp);
  423. }
  424. return;
  425. }
  426. /* sync receive data */
  427. if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
  428. {
  429. LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
  430. rt_free(recv_buf);
  431. return;
  432. }
  433. /* get at socket object by device socket descriptor */
  434. socket = &(device->sockets[device_socket]);
  435. /* notice the receive buffer and buffer size */
  436. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  437. {
  438. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  439. }
  440. send_net_read(client, device_socket);
  441. }
  442. static const struct at_urc urc_table[] =
  443. {
  444. {"$MYURCCLOSE:", "\r\n", urc_close_func},
  445. {"$MYURCREAD:", "\r\n", urc_recv_func},
  446. {"$MYNETREAD:", "\r\n", read_ack_func},
  447. };
  448. static const struct at_socket_ops n720_socket_ops =
  449. {
  450. n720_socket_connect,
  451. n720_socket_close,
  452. n720_socket_send,
  453. n720_domain_resolve,
  454. n720_socket_set_event_cb,
  455. };
  456. int n720_socket_init(struct at_device *device)
  457. {
  458. RT_ASSERT(device);
  459. /* register URC data execution function */
  460. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  461. return RT_EOK;
  462. }
  463. int n720_socket_class_register(struct at_device_class *class)
  464. {
  465. RT_ASSERT(class);
  466. class->socket_num = AT_DEVICE_N720_SOCKETS_NUM;
  467. class->socket_ops = &n720_socket_ops;
  468. return RT_EOK;
  469. }
  470. #endif /* AT_DEVICE_USING_N720 && AT_USING_SOCKET */