at_socket_bc28.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-02-13 luhuadong first version
  9. * 2020-07-19 luhuadong support alloc socket
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <at_device_bc28.h>
  14. #if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10301
  15. #error "This AT Client version is older, please check and update latest AT Client!"
  16. #endif
  17. #define LOG_TAG "at.skt.bc28"
  18. #include <at_log.h>
  19. #if defined(AT_DEVICE_USING_BC28) && defined(AT_USING_SOCKET)
  20. #define BC28_MODULE_SEND_MAX_SIZE 1358
  21. #define BC28_MODULE_RECV_MAX_SIZE 1358
  22. /* set real event by current socket and current state */
  23. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  24. /* AT socket event type */
  25. #define BC28_EVENT_CONN_OK (1L << 0)
  26. #define BC28_EVENT_SEND_OK (1L << 1)
  27. #define BC28_EVENT_RECV_OK (1L << 2)
  28. #define BC28_EVNET_CLOSE_OK (1L << 3)
  29. #define BC28_EVENT_CONN_FAIL (1L << 4)
  30. #define BC28_EVENT_SEND_FAIL (1L << 5)
  31. #define BC28_EVENT_DOMAIN_OK (1L << 6)
  32. #define BC28_EVENT_DOMAIN_FAIL (1L << 7)
  33. static at_evt_cb_t at_evt_cb_set[] = {
  34. [AT_SOCKET_EVT_RECV] = NULL,
  35. [AT_SOCKET_EVT_CLOSED] = NULL,
  36. };
  37. static struct at_socket_ip_info
  38. {
  39. char ip_addr[IP_ADDR_SIZE_MAX];
  40. int port;
  41. } bc28_sock_info[AT_DEVICE_BC28_SOCKETS_NUM];
  42. /**
  43. * convert data from ASCII string to Hex string.
  44. *
  45. * @param str input ASCII string
  46. * @param hex output Hex string
  47. * @param len length of str, or the size you want to convert
  48. *
  49. * @return =0: convert failed, or no data need to convert
  50. * >0: the size of convert success
  51. */
  52. static int string_to_hex(const char *str, char *hex, const rt_size_t len)
  53. {
  54. RT_ASSERT(str && hex);
  55. int str_len = rt_strlen(str);
  56. int pos = 0, i;
  57. if (len < 1 || str_len < len)
  58. {
  59. return 0;
  60. }
  61. for (i = 0; i < len; i++, pos += 2)
  62. {
  63. rt_sprintf(&hex[pos], "%02X", str[i]);
  64. }
  65. return i;
  66. }
  67. /**
  68. * convert data from Hex string to ASCII string.
  69. *
  70. * @param hex input Hex string
  71. * @param str output ASCII string
  72. * @param len length of str, or the size you want to convert
  73. *
  74. * @return =0: convert failed, or no data need to convert
  75. * >0: the size of convert success
  76. */
  77. static int hex_to_string(const char *hex, char *str, const rt_size_t len)
  78. {
  79. RT_ASSERT(hex && str);
  80. int hex_len = rt_strlen(hex);
  81. int pos = 0, left, right, i;
  82. if (len < 1 || hex_len/2 < len)
  83. {
  84. return 0;
  85. }
  86. for (i = 0; i < len*2; i++, pos++)
  87. {
  88. left = hex[i++];
  89. right = hex[i];
  90. left = (left < 58) ? (left - 48) : (left - 55);
  91. right = (right < 58) ? (right - 48) : (right - 55);
  92. str[pos] = (left << 4) | right;
  93. }
  94. return pos;
  95. }
  96. static void at_tcp_ip_errcode_parse(int result)//TCP/IP_QIGETERROR
  97. {
  98. switch(result)
  99. {
  100. case 0 : LOG_D("%d : Operation successful", result); break;
  101. case 550 : LOG_E("%d : Unknown error", result); break;
  102. case 551 : LOG_E("%d : Operation blocked", result); break;
  103. case 552 : LOG_E("%d : Invalid parameters", result); break;
  104. case 553 : LOG_E("%d : Memory not enough", result); break;
  105. case 554 : LOG_E("%d : Create socket failed", result); break;
  106. case 555 : LOG_E("%d : Operation not supported", result); break;
  107. case 556 : LOG_E("%d : Socket bind failed", result); break;
  108. case 557 : LOG_E("%d : Socket listen failed", result); break;
  109. case 558 : LOG_E("%d : Socket write failed", result); break;
  110. case 559 : LOG_E("%d : Socket read failed", result); break;
  111. case 560 : LOG_E("%d : Socket accept failed", result); break;
  112. case 561 : LOG_E("%d : Open PDP context failed", result); break;
  113. case 562 : LOG_E("%d : Close PDP context failed", result); break;
  114. case 563 : LOG_W("%d : Socket identity has been used", result); break;
  115. case 564 : LOG_E("%d : DNS busy", result); break;
  116. case 565 : LOG_E("%d : DNS parse failed", result); break;
  117. case 566 : LOG_E("%d : Socket connect failed", result); break;
  118. // case 567 : LOG_W("%d : Socket has been closed", result); break;
  119. case 567 : break;
  120. case 568 : LOG_E("%d : Operation busy", result); break;
  121. case 569 : LOG_E("%d : Operation timeout", result); break;
  122. case 570 : LOG_E("%d : PDP context broken down", result); break;
  123. case 571 : LOG_E("%d : Cancel send", result); break;
  124. case 572 : LOG_E("%d : Operation not allowed", result); break;
  125. case 573 : LOG_E("%d : APN not configured", result); break;
  126. case 574 : LOG_E("%d : Port busy", result); break;
  127. default : LOG_E("%d : Unknown err code", result); break;
  128. }
  129. }
  130. static int bc28_socket_event_send(struct at_device *device, uint32_t event)
  131. {
  132. return (int) rt_event_send(device->socket_event, event);
  133. }
  134. static int bc28_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
  135. {
  136. int result = RT_EOK;
  137. rt_uint32_t recved;
  138. result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  139. if (result != RT_EOK)
  140. {
  141. return -RT_ETIMEOUT;
  142. }
  143. return recved;
  144. }
  145. /**
  146. * close socket by AT commands.
  147. *
  148. * @param current socket
  149. *
  150. * @return 0: close socket success
  151. * -1: send AT commands error
  152. * -2: wait socket event timeout
  153. * -5: no memory
  154. */
  155. static int bc28_socket_close(struct at_socket *socket)
  156. {
  157. int result = RT_EOK;
  158. at_response_t resp = RT_NULL;
  159. int device_socket = (int) socket->user_data;
  160. struct at_device *device = (struct at_device *) socket->device;
  161. resp = at_create_resp(64, 0, rt_tick_from_millisecond(3000));
  162. if (resp == RT_NULL)
  163. {
  164. LOG_E("no memory for resp create.");
  165. return -RT_ENOMEM;
  166. }
  167. result = at_obj_exec_cmd(device->client, resp, "AT+NSOCL=%d", device_socket);
  168. if (result < 0)
  169. {
  170. LOG_E("%s device close socket(%d) failed [%d].", device->name, device_socket, result);
  171. }
  172. else
  173. {
  174. LOG_D("%s device close socket(%d).", device->name, device_socket);
  175. }
  176. at_delete_resp(resp);
  177. return result;
  178. }
  179. /**
  180. * create socket by AT commands.
  181. *
  182. * @param type connect socket type(tcp, udp)
  183. *
  184. * @return >=0: create socket success, return the socket id (0-6)
  185. * -1: send or exec AT commands error
  186. * -5: no memory
  187. */
  188. static int bc28_socket_create(struct at_device *device, enum at_socket_type type)
  189. {
  190. const char *type_str = RT_NULL;
  191. uint32_t protocol = 0;
  192. uint32_t port = 0; /* range: 0-65535, if 0 means get a random port */
  193. at_response_t resp = RT_NULL;
  194. int socket = -1, result = 0;
  195. switch(type)
  196. {
  197. case AT_SOCKET_TCP:
  198. type_str = "STREAM";
  199. protocol = 6;
  200. break;
  201. case AT_SOCKET_UDP:
  202. type_str = "DGRAM";
  203. protocol = 17;
  204. break;
  205. default:
  206. return -RT_ERROR;
  207. }
  208. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  209. if (resp == RT_NULL)
  210. {
  211. LOG_E("no memory for resp create.");
  212. return -RT_ENOMEM;
  213. }
  214. /* create socket */
  215. if (at_obj_exec_cmd(device->client, resp, "AT+NSOCR=%s,%d,%d,1", type_str, protocol, port) < 0)
  216. {
  217. result = -RT_ERROR;
  218. goto __exit;
  219. }
  220. /* check socket */
  221. if (at_resp_parse_line_args(resp, 2, "%d", &socket) <= 0)
  222. {
  223. LOG_E("%s device create %s socket failed.", device->name, type_str);
  224. result = -RT_ERROR;
  225. goto __exit;
  226. }
  227. else
  228. {
  229. LOG_D("%s device create a %s socket(%d).", device->name, type_str, socket);
  230. result = socket;
  231. }
  232. __exit:
  233. if (resp)
  234. {
  235. at_delete_resp(resp);
  236. }
  237. return result;
  238. }
  239. /**
  240. * create TCP/UDP client or server connect by AT commands.
  241. *
  242. * @param socket current socket
  243. * @param ip server or client IP address
  244. * @param port server or client port
  245. * @param type connect socket type(tcp, udp)
  246. * @param is_client connection is client
  247. *
  248. * @return 0: connect success
  249. * -1: connect failed, send commands error or type error
  250. * -2: wait socket event timeout
  251. * -5: no memory
  252. */
  253. static int bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
  254. enum at_socket_type type, rt_bool_t is_client)
  255. {
  256. #define CONN_RETRY 3
  257. int i = 0;
  258. uint32_t event = 0;
  259. at_response_t resp = RT_NULL;
  260. int result = 0, event_result = 0;
  261. int device_socket = (int) socket->user_data;
  262. int return_socket = -1;
  263. struct at_device *device = (struct at_device *) socket->device;
  264. RT_ASSERT(ip);
  265. RT_ASSERT(port >= 0);
  266. if (!is_client)
  267. {
  268. return -RT_ERROR;
  269. }
  270. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  271. if (resp == RT_NULL)
  272. {
  273. LOG_E("no memory for resp create.");
  274. return -RT_ENOMEM;
  275. }
  276. /* if the protocol is not tcp, no need connect to server */
  277. if (type != AT_SOCKET_TCP)
  278. {
  279. if (type == AT_SOCKET_UDP)
  280. {
  281. rt_strncpy(bc28_sock_info[device_socket].ip_addr, ip, IP_ADDR_SIZE_MAX);
  282. bc28_sock_info[device_socket].port = port;
  283. }
  284. return RT_EOK;
  285. }
  286. for(i=0; i<CONN_RETRY; i++)
  287. {
  288. /* clear socket connect event */
  289. event = SET_EVENT(device_socket, BC28_EVENT_CONN_OK | BC28_EVENT_CONN_FAIL);
  290. bc28_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  291. if (at_obj_exec_cmd(device->client, resp, "AT+NSOCO=%d,%s,%d", device_socket, ip, port) < 0)
  292. {
  293. result = -RT_ERROR;
  294. continue;
  295. }
  296. LOG_D("%s device socket(%d) try connect to %s:%d.", device->name, device_socket, ip, port);
  297. if(!at_resp_get_line_by_kw(resp, "OK"))
  298. {
  299. result = -RT_ERROR;
  300. continue;
  301. }
  302. /* waiting result event from AT URC, the device default connection timeout is 30 seconds*/
  303. if (bc28_socket_event_recv(device, SET_EVENT(device_socket, 0),
  304. 30 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  305. {
  306. LOG_D("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
  307. /* No news is good news */
  308. result = RT_EOK;
  309. break;
  310. }
  311. /* waiting OK or failed result */
  312. event_result = bc28_socket_event_recv(device, BC28_EVENT_CONN_OK | BC28_EVENT_CONN_FAIL,
  313. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  314. if (event_result & BC28_EVENT_CONN_FAIL)
  315. {
  316. LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
  317. result = -RT_ERROR;
  318. continue;
  319. }
  320. else
  321. {
  322. result = RT_EOK;
  323. break;
  324. }
  325. }
  326. __exit:
  327. if (resp)
  328. {
  329. at_delete_resp(resp);
  330. }
  331. return result;
  332. }
  333. /**
  334. * send data to server or client by AT commands.
  335. *
  336. * @param socket current socket
  337. * @param buff send buffer
  338. * @param bfsz send buffer size
  339. * @param type connect socket type(tcp, udp)
  340. *
  341. * @return >=0: the size of send success
  342. * -1: send AT commands error or send data error
  343. * -2: waited socket event timeout
  344. * -5: no memory
  345. */
  346. static int bc28_socket_send(struct at_socket *socket, const char *buff,
  347. size_t bfsz, enum at_socket_type type)
  348. {
  349. uint32_t event = 0;
  350. int result = 0, event_result = 0;
  351. size_t cur_pkt_size = 0, sent_size = 0;
  352. at_response_t resp = RT_NULL;
  353. int device_socket = (int) socket->user_data;
  354. struct at_device *device = (struct at_device *) socket->device;
  355. struct at_device_bc28 *bc28 = (struct at_device_bc28 *) device->user_data;
  356. rt_mutex_t lock = device->client->lock;
  357. RT_ASSERT(buff);
  358. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  359. if (resp == RT_NULL)
  360. {
  361. LOG_E("no memory for resp create.");
  362. return -RT_ENOMEM;
  363. }
  364. rt_mutex_take(lock, RT_WAITING_FOREVER);
  365. /* clear socket send event */
  366. event = SET_EVENT(device_socket, BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL);
  367. bc28_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
  368. /* only use for UDP socket */
  369. const char *ip = bc28_sock_info[device_socket].ip_addr;
  370. const int port = bc28_sock_info[device_socket].port;
  371. while (sent_size < bfsz)
  372. {
  373. if (bfsz - sent_size < BC28_MODULE_SEND_MAX_SIZE)
  374. {
  375. cur_pkt_size = bfsz - sent_size;
  376. }
  377. else
  378. {
  379. cur_pkt_size = BC28_MODULE_SEND_MAX_SIZE;
  380. }
  381. size_t i = 0, ind = 0;
  382. char hex_data[cur_pkt_size * 2 + 1];
  383. rt_memset(hex_data, 0, sizeof(hex_data));
  384. for (i=0, ind=0; i<cur_pkt_size; i++, ind+=2)
  385. {
  386. rt_sprintf(&hex_data[ind], "%02X", buff[sent_size + i]);
  387. }
  388. switch (type)
  389. {
  390. case AT_SOCKET_TCP:
  391. /* AT+NSOSD=<socket>,<length>,<data>[,<flag>[,<sequence>]] */
  392. if (at_obj_exec_cmd(device->client, resp, "AT+NSOSD=%d,%d,%s,0x100,1", device_socket,
  393. (int)cur_pkt_size, hex_data) < 0)
  394. {
  395. result = -RT_ERROR;
  396. goto __exit;
  397. }
  398. LOG_D("%s device tcp socket(%d) send %d bytes.\n>> %s", device->name, device_socket, (int)cur_pkt_size, hex_data);
  399. break;
  400. case AT_SOCKET_UDP:
  401. /* AT+NSOST=<socket>,<remote_addr>,<remote_port>,<length>,<data>[,<sequence>] */
  402. if (at_obj_exec_cmd(device->client, resp, "AT+NSOST=%d,%s,%d,%d,%s,1", device_socket,
  403. ip, port, (int)cur_pkt_size, hex_data) < 0)
  404. {
  405. result = -RT_ERROR;
  406. goto __exit;
  407. }
  408. LOG_D("%s device udp socket(%d) send %d bytes to %s:%d.\n>> %s", device->name, device_socket, ip, port, (int)cur_pkt_size, hex_data);
  409. break;
  410. default:
  411. LOG_E("not supported send type %d.", type);
  412. result = -RT_ERROR;
  413. goto __exit;
  414. }
  415. /* check if sent ok */
  416. if (!at_resp_get_line_by_kw(resp, "OK"))
  417. {
  418. LOG_E("%s device socket(%d) send data failed.", device->name, device_socket);
  419. result = -RT_ERROR;
  420. goto __exit;
  421. }
  422. int return_socket = -1, return_size = -1;
  423. if (at_resp_parse_line_args(resp, 2, "%d,%d", &return_socket, &return_size) <= 0)
  424. {
  425. LOG_E("%s device socket(%d) send data failed.", device->name, device_socket);
  426. result = -RT_ERROR;
  427. goto __exit;
  428. }
  429. if (return_socket != device_socket || return_size != cur_pkt_size)
  430. {
  431. LOG_E("%s device socket(%d) send data incompletely.", device->name, device_socket);
  432. result = -RT_ERROR;
  433. goto __exit;
  434. }
  435. /* waiting result event from AT URC, the device default timeout is 60 seconds*/
  436. if (bc28_socket_event_recv(device, SET_EVENT(device_socket, 0),
  437. 60 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
  438. {
  439. LOG_E("%s device socket(%d) wait send result timeout.", device->name, device_socket);
  440. result = -RT_ETIMEOUT;
  441. goto __exit;
  442. }
  443. /* waiting OK or failed result */
  444. event_result = bc28_socket_event_recv(device, BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL,
  445. 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  446. if (event_result & BC28_EVENT_SEND_FAIL)
  447. {
  448. LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
  449. result = -RT_ERROR;
  450. goto __exit;
  451. }
  452. else
  453. {
  454. LOG_D("%s device socket(%d) send success.", device->name, device_socket);
  455. sent_size += cur_pkt_size;
  456. result = sent_size;
  457. }
  458. }
  459. __exit:
  460. rt_mutex_release(lock);
  461. if (resp)
  462. {
  463. at_delete_resp(resp);
  464. }
  465. return result > 0 ? sent_size : result;
  466. }
  467. /**
  468. * domain resolve by AT commands.
  469. *
  470. * @param name domain name
  471. * @param ip parsed IP address, it's length must be 16
  472. *
  473. * @return 0: domain resolve success
  474. * -1: send AT commands error or response error
  475. * -2: wait socket event timeout
  476. * -5: no memory
  477. */
  478. int bc28_domain_resolve(const char *name, char ip[16])
  479. {
  480. #define RESOLVE_RETRY 1
  481. int i, result, event_result = 0;
  482. at_response_t resp = RT_NULL;
  483. struct at_device *device = RT_NULL;
  484. struct at_device_bc28 *bc28 = RT_NULL;
  485. RT_ASSERT(name);
  486. RT_ASSERT(ip);
  487. device = at_device_get_first_initialized();
  488. if (device == RT_NULL)
  489. {
  490. LOG_E("get first init device failed.");
  491. return -RT_ERROR;
  492. }
  493. /* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
  494. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  495. if (!resp)
  496. {
  497. LOG_E("no memory for resp create.");
  498. return -RT_ENOMEM;
  499. }
  500. /* clear BC28_EVENT_DOMAIN_OK */
  501. bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
  502. bc28 = (struct at_device_bc28 *) device->user_data;
  503. bc28->socket_data = ip;
  504. if (at_obj_exec_cmd(device->client, resp, "AT+QDNS=0,%s", name) < 0)
  505. {
  506. result = -RT_ERROR;
  507. goto __exit;
  508. }
  509. for(i = 0; i < RESOLVE_RETRY; i++)
  510. {
  511. /* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
  512. event_result = bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK | BC28_EVENT_DOMAIN_FAIL,
  513. 30 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
  514. if (event_result < 0)
  515. {
  516. result = -RT_ETIMEOUT;
  517. continue;
  518. }
  519. else if (event_result & BC28_EVENT_DOMAIN_FAIL)
  520. {
  521. LOG_E("%d device resolve domain name failed.", device->name);
  522. result = -RT_ERROR;
  523. continue;
  524. }
  525. else
  526. {
  527. result = RT_EOK;
  528. break;
  529. }
  530. }
  531. __exit:
  532. bc28->socket_data = RT_NULL;
  533. if (resp)
  534. {
  535. at_delete_resp(resp);
  536. }
  537. return result;
  538. }
  539. /**
  540. * set AT socket event notice callback
  541. *
  542. * @param event notice event
  543. * @param cb notice callback
  544. */
  545. static void bc28_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  546. {
  547. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  548. {
  549. at_evt_cb_set[event] = cb;
  550. }
  551. }
  552. static void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
  553. {
  554. int device_socket = 0, result = 0;
  555. struct at_device *device = RT_NULL;
  556. char *client_name = client->device->parent.name;
  557. RT_ASSERT(data && size);
  558. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  559. if (device == RT_NULL)
  560. {
  561. LOG_E("get device(%s) failed.", client_name);
  562. return;
  563. }
  564. /* only for firmware version base BC28JAR02xxx */
  565. sscanf(data, "+QTCPIND: %d,%d", &device_socket , &result);
  566. if (result == 0)
  567. {
  568. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_OK));
  569. }
  570. else
  571. {
  572. at_tcp_ip_errcode_parse(result);
  573. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));
  574. }
  575. }
  576. static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
  577. {
  578. int device_socket = 0, sequence = 0, status = 0;
  579. struct at_device *device = RT_NULL;
  580. struct at_device_bc28 *bc28 = RT_NULL;
  581. char *client_name = client->device->parent.name;
  582. RT_ASSERT(data && size);
  583. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  584. if (device == RT_NULL)
  585. {
  586. LOG_E("get device(%s) failed.", client_name);
  587. return;
  588. }
  589. sscanf(data, "+NSOSTR:%d,%d,%d", &device_socket, &sequence, &status);
  590. if (1 == status)
  591. {
  592. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_OK));
  593. }
  594. else
  595. {
  596. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_FAIL));
  597. }
  598. }
  599. static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
  600. {
  601. int device_socket = 0;
  602. struct at_socket *socket = RT_NULL;
  603. struct at_device *device = RT_NULL;
  604. char *client_name = client->device->parent.name;
  605. RT_ASSERT(data && size);
  606. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  607. if (device == RT_NULL)
  608. {
  609. LOG_E("get device(%s) failed.", client_name);
  610. return;
  611. }
  612. sscanf(data, "+NSOCLI: %d", &device_socket);
  613. bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));
  614. if (device_socket >= 0)
  615. {
  616. /* get at socket object by device socket descriptor */
  617. socket = &(device->sockets[device_socket]);
  618. /* notice the socket is disconnect by remote */
  619. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  620. {
  621. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
  622. }
  623. }
  624. }
  625. static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
  626. {
  627. int device_socket = 0;
  628. rt_int32_t timeout;
  629. rt_size_t bfsz = 0, temp_size = 0;
  630. char *recv_buf = RT_NULL, *hex_buf = RT_NULL, temp[8] = {0};
  631. char remote_addr[IP_ADDR_SIZE_MAX] = {0};
  632. int remote_port = -1, return_socket = -1, data_length = 0, remaining_length = 0;
  633. struct at_socket *socket = RT_NULL;
  634. struct at_device *device = RT_NULL;
  635. char *client_name = client->device->parent.name;
  636. RT_ASSERT(data && size);
  637. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  638. if (device == RT_NULL)
  639. {
  640. LOG_E("get device(%s) failed.", client_name);
  641. return;
  642. }
  643. hex_buf = (char *) rt_calloc(1, BC28_MODULE_RECV_MAX_SIZE * 2 + 1);
  644. /* get the current socket and receive buffer size by receive data */
  645. /* mode 2 => +NSONMI:<socket>,<remote_addr>, <remote_port>,<length>,<data> */
  646. sscanf(data, "+NSONMI:%d,%[0-9.],%d,%d,%s", &device_socket, remote_addr, &remote_port, (int *) &bfsz, hex_buf);
  647. LOG_D("%s device socket(%d) recv %d bytes from %s:%d\n>> %s", device_socket, bfsz, remote_addr, remote_port, hex_buf);
  648. /* set receive timeout by receive buffer length, not less than 10 ms */
  649. timeout = bfsz > 10 ? bfsz : 10;
  650. if (device_socket < 0 || bfsz == 0)
  651. {
  652. return;
  653. }
  654. recv_buf = (char *) rt_calloc(1, bfsz + 1);
  655. if (recv_buf == RT_NULL || hex_buf == RT_NULL)
  656. {
  657. LOG_E("no memory for URC receive buffer(%d).", bfsz);
  658. /* read and clean the coming data */
  659. while (temp_size < bfsz)
  660. {
  661. if (bfsz - temp_size > sizeof(temp))
  662. {
  663. at_client_obj_recv(client, temp, sizeof(temp), timeout);
  664. }
  665. else
  666. {
  667. at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
  668. }
  669. temp_size += sizeof(temp);
  670. }
  671. if (recv_buf) rt_free(recv_buf);
  672. if (hex_buf) rt_free(hex_buf);
  673. return;
  674. }
  675. /* convert receive data */
  676. hex_to_string(hex_buf, recv_buf, bfsz);
  677. rt_free(hex_buf);
  678. /* get at socket object by device socket descriptor */
  679. socket = &(device->sockets[device_socket]);
  680. /* notice the receive buffer and buffer size */
  681. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  682. {
  683. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  684. }
  685. }
  686. static void urc_dns_func(struct at_client *client, const char *data, rt_size_t size)
  687. {
  688. int i = 0, j = 0;
  689. char recv_ip[16] = {0};
  690. int result, ip_count, dns_ttl;
  691. struct at_device *device = RT_NULL;
  692. struct at_device_bc28 *bc28 = RT_NULL;
  693. char *client_name = client->device->parent.name;
  694. RT_ASSERT(data && size);
  695. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  696. if (device == RT_NULL)
  697. {
  698. LOG_E("get device(%s) failed.", client_name);
  699. return;
  700. }
  701. bc28 = (struct at_device_bc28 *) device->user_data;
  702. if (bc28->socket_data == RT_NULL)
  703. {
  704. LOG_D("%s device socket_data no config.", bc28->device_name);
  705. return;
  706. }
  707. sscanf(data, "+QDNS:%s", recv_ip);
  708. recv_ip[15] = '\0';
  709. if (rt_strstr(recv_ip, "FAIL"))
  710. {
  711. bc28_socket_event_send(device, BC28_EVENT_DOMAIN_FAIL);
  712. }
  713. else
  714. {
  715. rt_memcpy(bc28->socket_data, recv_ip, sizeof(recv_ip));
  716. bc28_socket_event_send(device, BC28_EVENT_DOMAIN_OK);
  717. }
  718. }
  719. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  720. {
  721. RT_ASSERT(data);
  722. LOG_I("URC data : %.*s", size, data);
  723. }
  724. /* +NSOSTR:<socket>,<sequence>,<status> */
  725. static const struct at_urc urc_table[] =
  726. {
  727. {"+QDNS:", "\r\n", urc_dns_func},
  728. {"+QTCPIND:", "\r\n", urc_connect_func},
  729. {"+NSOSTR:", "\r\n", urc_send_func},
  730. {"+NSONMI:", "\r\n", urc_recv_func},
  731. {"+NSOCLI:", "\r\n", urc_close_func},
  732. };
  733. static const struct at_socket_ops bc28_socket_ops =
  734. {
  735. bc28_socket_connect,
  736. bc28_socket_close,
  737. bc28_socket_send,
  738. bc28_domain_resolve,
  739. bc28_socket_set_event_cb,
  740. #if defined(AT_SW_VERSION_NUM) && AT_SW_VERSION_NUM > 0x10300
  741. bc28_socket_create,
  742. #endif
  743. };
  744. int bc28_socket_init(struct at_device *device)
  745. {
  746. RT_ASSERT(device);
  747. /* register URC data execution function */
  748. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  749. return RT_EOK;
  750. }
  751. int bc28_socket_class_register(struct at_device_class *class)
  752. {
  753. RT_ASSERT(class);
  754. class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM;
  755. class->socket_ops = &bc28_socket_ops;
  756. return RT_EOK;
  757. }
  758. #endif /* AT_DEVICE_USING_BC28 && AT_USING_SOCKET */