at_socket_sim76xx.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * File : at_socket_sim76xx.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-12-22 thomasonegd first version
  23. * 2019-03-06 thomasonegd fix udp connection.
  24. * 2019-03-08 thomasonegd add power_on & power_off api
  25. */
  26. #include <at.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <rtthread.h>
  30. #include <sys/socket.h>
  31. #include <at_socket.h>
  32. #if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10200
  33. #error "This AT Client version is older, please check and update latest AT Client!"
  34. #endif
  35. #define LOG_TAG "at.sim76xx"
  36. #include <at_log.h>
  37. #ifdef AT_DEVICE_SIM76XX
  38. #define SIM76XX_MODULE_SEND_MAX_SIZE 1500
  39. #define SIM76XX_WAIT_CONNECT_TIME 5000
  40. #define SIM76XX_THREAD_STACK_SIZE 1024
  41. #define SIM76XX_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  42. #define SIM76XX_MAX_CONNECTIONS 10
  43. /* set real event by current socket and current state */
  44. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  45. /* AT socket event type */
  46. #define SIM76XX_EVENT_CONN_OK (1L << 0)
  47. #define SIM76XX_EVENT_SEND_OK (1L << 1)
  48. #define SIM76XX_EVENT_RECV_OK (1L << 2)
  49. #define SIM76XX_EVNET_CLOSE_OK (1L << 3)
  50. #define SIM76XX_EVENT_CONN_FAIL (1L << 4)
  51. #define SIM76XX_EVENT_SEND_FAIL (1L << 5)
  52. uint8_t at_socket_init = 0;
  53. static int cur_socket;
  54. static int cur_send_bfsz;
  55. static rt_event_t at_socket_event;
  56. static rt_mutex_t at_event_lock;
  57. static at_evt_cb_t at_evt_cb_set[] = {
  58. [AT_SOCKET_EVT_RECV] = NULL,
  59. [AT_SOCKET_EVT_CLOSED] = NULL,
  60. };
  61. static char udp_ipstr[SIM76XX_MAX_CONNECTIONS][16];
  62. static int udp_port[SIM76XX_MAX_CONNECTIONS];
  63. static void at_tcp_ip_errcode_parse(int result)//Unsolicited TCP/IP command<err> codes
  64. {
  65. switch(result)
  66. {
  67. case 0 : LOG_D("%d : operation succeeded ", result); break;
  68. case 1 : LOG_E("%d : UNetwork failure", result); break;
  69. case 2 : LOG_E("%d : Network not opened", result); break;
  70. case 3 : LOG_E("%d : Wrong parameter", result); break;
  71. case 4 : LOG_E("%d : Operation not supported", result); break;
  72. case 5 : LOG_E("%d : Failed to create socket", result); break;
  73. case 6 : LOG_E("%d : Failed to bind socket", result); break;
  74. case 7 : LOG_E("%d : TCP server is already listening", result); break;
  75. case 8 : LOG_E("%d : Busy", result); break;
  76. case 9 : LOG_E("%d : Sockets opened", result); break;
  77. case 10 : LOG_E("%d : Timeout ", result); break;
  78. case 11 : LOG_E("%d : DNS parse failed for AT+CIPOPEN", result); break;
  79. case 255 : LOG_E("%d : Unknown error", result); break;
  80. }
  81. }
  82. static int at_socket_event_send(uint32_t event)
  83. {
  84. return (int) rt_event_send(at_socket_event, event);
  85. }
  86. static int at_socket_event_recv(uint32_t event, uint32_t timeout, rt_uint8_t option)
  87. {
  88. int result = 0;
  89. rt_uint32_t recved;
  90. result = rt_event_recv(at_socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  91. if (result != RT_EOK)
  92. {
  93. return -RT_ETIMEOUT;
  94. }
  95. return recved;
  96. }
  97. /**
  98. * close socket by AT commands.
  99. *
  100. * @param current socket
  101. *
  102. * @return 0: close socket success
  103. * -1: send AT commands error
  104. * -2: wait socket event timeout
  105. * -5: no memory
  106. */
  107. static int sim76xx_socket_close(int socket)
  108. {
  109. at_response_t resp = RT_NULL;
  110. int result = RT_EOK;
  111. int activated;
  112. uint8_t s;
  113. uint8_t lnk_stat[10];
  114. resp = at_create_resp(128, 0, rt_tick_from_millisecond(500));
  115. if (!resp)
  116. {
  117. LOG_E("No memory for response structure!");
  118. return -RT_ENOMEM;
  119. }
  120. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  121. rt_thread_delay(rt_tick_from_millisecond(100));
  122. // check socket link_state
  123. if (at_exec_cmd(resp,"AT+CIPCLOSE?") < 0)
  124. {
  125. result = -RT_ERROR;
  126. goto __exit;
  127. }
  128. if(at_resp_parse_line_args_by_kw(resp,"+CIPCLOSE:", "+CIPCLOSE: %d,%d,%d,%d,%d,%d,%d,%d,%d,%d",&lnk_stat[0],&lnk_stat[1],&lnk_stat[1]
  129. ,&lnk_stat[2],&lnk_stat[3],&lnk_stat[4],&lnk_stat[5],&lnk_stat[6],&lnk_stat[7],&lnk_stat[8],&lnk_stat[9]) < 0)
  130. {
  131. result = -RT_ERROR;
  132. goto __exit;
  133. }
  134. if (lnk_stat[socket])
  135. {
  136. // close tcp or udp socket if connected
  137. if (at_exec_cmd(resp, "AT+CIPCLOSE=%d", socket) < 0)
  138. {
  139. result = -RT_ERROR;
  140. goto __exit;
  141. }
  142. }
  143. // check the network open or not
  144. if (at_exec_cmd(resp,"AT+NETOPEN?") < 0)
  145. {
  146. result = -RT_ERROR;
  147. goto __exit;
  148. }
  149. if(at_resp_parse_line_args_by_kw(resp, "+NETOPEN:", "+NETOPEN: %d", &activated) < 0)
  150. {
  151. result = -RT_ERROR;
  152. goto __exit;
  153. }
  154. if (activated)
  155. {
  156. // if already open,then close it.
  157. if (at_exec_cmd(resp,"AT+NETCLOSE") < 0)
  158. {
  159. result = -RT_ERROR;
  160. goto __exit;
  161. }
  162. }
  163. __exit:
  164. rt_mutex_release(at_event_lock);
  165. if (resp)
  166. {
  167. at_delete_resp(resp);
  168. }
  169. return result;
  170. }
  171. /**
  172. * open packet network
  173. */
  174. static int sim76xx_network_socket_open(void)
  175. {
  176. int result = RT_EOK;
  177. at_response_t resp = RT_NULL;
  178. int activated;
  179. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  180. if (!resp)
  181. {
  182. LOG_E("No memory for response structure!");
  183. return -RT_ENOMEM;
  184. }
  185. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  186. // check the network open or not
  187. if (at_exec_cmd(resp,"AT+NETOPEN?") < 0)
  188. {
  189. result = -RT_ERROR;
  190. goto __exit;
  191. }
  192. if(at_resp_parse_line_args_by_kw(resp, "+NETOPEN:", "+NETOPEN: %d", &activated) < 0)
  193. {
  194. result = -RT_ERROR;
  195. goto __exit;
  196. }
  197. if (activated)
  198. {//network socket is already opened
  199. goto __exit;
  200. }else
  201. {
  202. // if not opened the open it.
  203. if (at_exec_cmd(resp,"AT+NETOPEN") < 0)
  204. {
  205. result = -RT_ERROR;
  206. goto __exit;
  207. }
  208. }
  209. __exit:
  210. rt_mutex_release(at_event_lock);
  211. if (resp)
  212. {
  213. at_delete_resp(resp);
  214. }
  215. return result;
  216. }
  217. /**
  218. * create TCP/UDP client or server connect by AT commands.
  219. *
  220. * @param socket current socket
  221. * @param ip server or client IP address
  222. * @param port server or client port
  223. * @param type connect socket type(tcp, udp)
  224. * @param is_client connection is client
  225. *
  226. * @return 0: connect success
  227. * -1: connect failed, send commands error or type error
  228. * -2: wait socket event timeout
  229. * -5: no memory
  230. */
  231. static int sim76xx_socket_connect(int socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  232. {
  233. at_response_t resp = RT_NULL;
  234. int result = RT_EOK,event_result = 0;
  235. rt_bool_t retryed = RT_FALSE;
  236. RT_ASSERT(ip);
  237. RT_ASSERT(port >= 0);
  238. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  239. if (!resp)
  240. {
  241. LOG_E("No memory for response structure!");
  242. return -RT_ENOMEM;
  243. }
  244. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  245. __retry:
  246. if (is_client)
  247. {
  248. //open network socket first(AT+NETOPEN)
  249. sim76xx_network_socket_open();
  250. switch (type)
  251. {
  252. case AT_SOCKET_TCP:
  253. /* send AT commands to connect TCP server */
  254. if (at_exec_cmd(resp, "AT+CIPOPEN=%d,\"TCP\",\"%s\",%d", socket, ip, port) < 0)
  255. {
  256. result = -RT_ERROR;
  257. }
  258. break;
  259. case AT_SOCKET_UDP:
  260. if (at_exec_cmd(resp, "AT+CIPOPEN=%d,\"UDP\",,,%d", socket, port) < 0)
  261. {
  262. result = -RT_ERROR;
  263. }
  264. strcpy(udp_ipstr[socket],ip);
  265. udp_port[socket] = port;
  266. break;
  267. default:
  268. LOG_E("Not supported connect type : %d.", type);
  269. result = -RT_ERROR;
  270. goto __exit;
  271. }
  272. }
  273. /* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use.*/
  274. if (at_socket_event_recv(SET_EVENT(socket, 0), rt_tick_from_millisecond(10 * 1000), RT_EVENT_FLAG_OR) < 0)
  275. {
  276. LOG_E("socket (%d) connect failed, wait connect result timeout.", socket);
  277. result = -RT_ETIMEOUT;
  278. goto __exit;
  279. }
  280. /* waiting OK or failed result */
  281. if ((event_result = at_socket_event_recv(SIM76XX_EVENT_CONN_OK | SIM76XX_EVENT_CONN_FAIL, rt_tick_from_millisecond(1 * 1000),
  282. RT_EVENT_FLAG_OR)) < 0)
  283. {
  284. LOG_E("socket (%d) connect failed, wait connect OK|FAIL timeout.", socket);
  285. result = -RT_ETIMEOUT;
  286. goto __exit;
  287. }
  288. /* check result */
  289. if (event_result & SIM76XX_EVENT_CONN_FAIL)
  290. {
  291. if (!retryed)
  292. {
  293. LOG_E("socket (%d) connect failed, maybe the socket was not be closed at the last time and now will retry.", socket);
  294. if (sim76xx_socket_close(socket) < 0)
  295. {
  296. goto __exit;
  297. }
  298. retryed = RT_TRUE;
  299. goto __retry;
  300. }
  301. LOG_E("socket (%d) connect failed, failed to establish a connection.", socket);
  302. result = -RT_ERROR;
  303. goto __exit;
  304. }
  305. if (result != RT_EOK && !retryed)
  306. {
  307. LOG_D("socket (%d) connect failed, maybe the socket was not be closed at the last time and now will retry.", socket);
  308. if (sim76xx_socket_close(socket) < 0)
  309. {
  310. goto __exit;
  311. }
  312. retryed = RT_TRUE;
  313. result = RT_EOK;
  314. goto __retry;
  315. }
  316. __exit:
  317. rt_mutex_release(at_event_lock);
  318. if (resp)
  319. {
  320. at_delete_resp(resp);
  321. }
  322. return result;
  323. }
  324. /**
  325. * send data to server or client by AT commands.
  326. *
  327. * @param socket current socket
  328. * @param buff send buffer
  329. * @param bfsz send buffer size
  330. * @param type connect socket type(tcp, udp)
  331. *
  332. * @return >=0: the size of send success
  333. * -1: send AT commands error or send data error
  334. * -2: waited socket event timeout
  335. * -5: no memory
  336. */
  337. static int sim76xx_socket_send(int socket, const char *buff, size_t bfsz, enum at_socket_type type)
  338. {
  339. int result = RT_EOK;
  340. int event_result = 0;
  341. at_response_t resp = RT_NULL;
  342. size_t cur_pkt_size = 0, sent_size = 0;
  343. RT_ASSERT(buff);
  344. RT_ASSERT(bfsz > 0);
  345. resp = at_create_resp(128, 2, rt_tick_from_millisecond(5000));
  346. if (!resp)
  347. {
  348. LOG_E("No memory for response structure!");
  349. return -RT_ENOMEM;
  350. }
  351. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  352. /* set current socket for send URC event */
  353. cur_socket = socket;
  354. /* set AT client end sign to deal with '>' sign.*/
  355. at_set_end_sign('>');
  356. while (sent_size < bfsz)
  357. {
  358. if (bfsz - sent_size < SIM76XX_MODULE_SEND_MAX_SIZE)
  359. {
  360. cur_pkt_size = bfsz - sent_size;
  361. }
  362. else
  363. {
  364. cur_pkt_size = SIM76XX_MODULE_SEND_MAX_SIZE;
  365. }
  366. switch(type)
  367. {
  368. case AT_SOCKET_TCP:
  369. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  370. if (at_exec_cmd(resp, "AT+CIPSEND=%d,%d", socket, cur_pkt_size) < 0)
  371. {
  372. result = -RT_ERROR;
  373. goto __exit;
  374. }
  375. break;
  376. case AT_SOCKET_UDP:
  377. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  378. if (at_exec_cmd(resp, "AT+CIPSEND=%d,%d,\"%s\",%d", socket, cur_pkt_size,udp_ipstr[socket],udp_port[socket]) < 0)
  379. {
  380. result = -RT_ERROR;
  381. goto __exit;
  382. }
  383. break;
  384. }
  385. /* send the real data to server or client */
  386. result = (int) at_client_send(buff + sent_size, cur_pkt_size);
  387. if (result == 0)
  388. {
  389. result = -RT_ERROR;
  390. goto __exit;
  391. }
  392. /* waiting result event from AT URC */
  393. if (at_socket_event_recv(SET_EVENT(socket, 0), rt_tick_from_millisecond(5 * 1000), RT_EVENT_FLAG_OR) < 0)
  394. {
  395. LOG_E("socket (%d) send failed, wait connect result timeout.", socket);
  396. result = -RT_ETIMEOUT;
  397. goto __exit;
  398. }
  399. /* waiting OK or failed result */
  400. if ((event_result = at_socket_event_recv(SIM76XX_EVENT_SEND_OK | SIM76XX_EVENT_SEND_FAIL, rt_tick_from_millisecond(5 * 1000),
  401. RT_EVENT_FLAG_OR)) < 0)
  402. {
  403. LOG_E("socket (%d) send failed, wait connect OK|FAIL timeout.", socket);
  404. result = -RT_ETIMEOUT;
  405. goto __exit;
  406. }
  407. /* check result */
  408. if (event_result & SIM76XX_EVENT_SEND_FAIL)
  409. {
  410. LOG_E("socket (%d) send failed, return failed.", socket);
  411. result = -RT_ERROR;
  412. goto __exit;
  413. }
  414. if (type == AT_SOCKET_TCP)
  415. {
  416. cur_pkt_size = cur_send_bfsz;
  417. }
  418. sent_size += cur_pkt_size;
  419. }
  420. __exit:
  421. /* reset the end sign for data */
  422. at_set_end_sign(0);
  423. rt_mutex_release(at_event_lock);
  424. if (resp)
  425. {
  426. at_delete_resp(resp);
  427. }
  428. return result;
  429. }
  430. /**
  431. * domain resolve by AT commands.
  432. *
  433. * @param name domain name
  434. * @param ip parsed IP address, it's length must be 16
  435. *
  436. * @return 0: domain resolve success
  437. * -2: wait socket event timeout
  438. * -5: no memory
  439. */
  440. static int sim76xx_domain_resolve(const char *name, char ip[16])
  441. {
  442. #define RESOLVE_RETRY 5
  443. int i, result = RT_EOK;
  444. char domain[32] = { 0 };
  445. char domain_ip[16] = {0};
  446. at_response_t resp = RT_NULL;
  447. RT_ASSERT(name);
  448. RT_ASSERT(ip);
  449. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  450. if (!resp)
  451. {
  452. LOG_E("No memory for response structure!");
  453. return -RT_ENOMEM;
  454. }
  455. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  456. for(i = 0; i < RESOLVE_RETRY; i++)
  457. {
  458. if (at_exec_cmd(resp, "AT+CDNSGIP=\"%s\"", name) < 0)
  459. {
  460. rt_thread_delay(rt_tick_from_millisecond(200));
  461. /* resolve failed, maybe receive an URC CRLF */
  462. continue;
  463. }
  464. /* parse the third line of response data, get the IP address */
  465. /* +CDNSGIP: 1,"www.baidu.com","14.215.177.39" */
  466. if(at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "+CDNSGIP: 1,%[^,],\"%[^\"]", domain,domain_ip) < 0)
  467. {
  468. rt_thread_delay(rt_tick_from_millisecond(200));
  469. /* resolve failed, maybe receive an URC CRLF */
  470. continue;
  471. }
  472. if (strlen(domain_ip) < 8)
  473. {
  474. rt_thread_delay(rt_tick_from_millisecond(200));
  475. /* resolve failed, maybe receive an URC CRLF */
  476. continue;
  477. }
  478. else
  479. {
  480. strncpy(ip, domain_ip, 15);
  481. ip[15] = '\0';
  482. break;
  483. }
  484. }
  485. rt_mutex_release(at_event_lock);
  486. if (i == RESOLVE_RETRY)
  487. {
  488. result = -RT_ERROR;
  489. }
  490. if (resp)
  491. {
  492. at_delete_resp(resp);
  493. }
  494. return result;
  495. }
  496. /**
  497. * set AT socket event notice callback
  498. *
  499. * @param event notice event
  500. * @param cb notice callback
  501. */
  502. static void sim76xx_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  503. {
  504. if (event < (sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1])))
  505. {
  506. at_evt_cb_set[event] = cb;
  507. }
  508. }
  509. static void urc_send_func(const char *data, rt_size_t size)
  510. {
  511. int socket = 0;
  512. int rqst_size;
  513. int cnf_size;
  514. RT_ASSERT(data && size);
  515. sscanf(data,"+CIPSEND: %d,%d,%d",&socket,&rqst_size,&cnf_size);
  516. cur_send_bfsz = cnf_size;
  517. at_socket_event_send(SET_EVENT(socket, SIM76XX_EVENT_SEND_OK));
  518. }
  519. static void urc_ping_func(const char *data, rt_size_t size)
  520. {
  521. static int icmp_seq = 0;
  522. int i, j = 0;
  523. int result, recv_len, time, ttl;
  524. int sent, rcvd, lost, min, max, avg;
  525. char dst_ip[16] = { 0 };
  526. RT_ASSERT(data);
  527. for (i=0;i<size;i++)
  528. {
  529. if(*(data+i) == '.')
  530. j++;
  531. }
  532. if (j != 0)
  533. {
  534. sscanf(data, "+CPING: %d,%[^,],%d,%d,%d", &result, dst_ip, &recv_len, &time, &ttl);
  535. if (result == 1)
  536. LOG_I("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms", recv_len, dst_ip, icmp_seq++, ttl, time);
  537. }
  538. else
  539. {
  540. sscanf(data, "+CPING: %d,%d,%d,%d,%d,%d,%d", &result, &sent, &rcvd, &lost, &min, &max, &avg);
  541. if (result == 3)
  542. LOG_I("%d sent %d received %d lost, min=%dms max=%dms average=%dms", sent, rcvd, lost, min, max, avg);
  543. if (result == 2)
  544. LOG_I("ping requst timeout");
  545. }
  546. }
  547. static void urc_connect_func(const char *data, rt_size_t size)
  548. {
  549. int socket = 0;
  550. int result = 0;
  551. RT_ASSERT(data && size);
  552. sscanf(data, "+CIPOPEN: %d,%d", &socket , &result);
  553. if (result == 0)
  554. {
  555. at_socket_event_send(SET_EVENT(socket, SIM76XX_EVENT_CONN_OK));
  556. }
  557. else
  558. {
  559. at_tcp_ip_errcode_parse(result);
  560. at_socket_event_send(SET_EVENT(socket, SIM76XX_EVENT_CONN_FAIL));
  561. }
  562. }
  563. static void urc_close_func(const char *data, rt_size_t size)
  564. {
  565. int socket = 0;
  566. int reason = 0;
  567. RT_ASSERT(data && size);
  568. sscanf(data, "+IPCLOSE %d,%d", &socket, &reason);
  569. switch(reason)
  570. {
  571. case 0:
  572. LOG_E("socket is closed by local,active");
  573. break;
  574. case 1:
  575. LOG_E("socket is closed by remote,passive");
  576. break;
  577. case 2:
  578. LOG_E("socket is closed for sending timeout");
  579. break;
  580. }
  581. /* notice the socket is disconnect by remote */
  582. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  583. {
  584. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  585. }
  586. }
  587. static void urc_recv_func(const char *data, rt_size_t size)
  588. {
  589. rt_size_t bfsz = 0, temp_size = 0;
  590. rt_int32_t timeout;
  591. char *recv_buf = RT_NULL, temp[8];
  592. RT_ASSERT(data && size);
  593. /* get the current socket and receive buffer size by receive data */
  594. sscanf(data, "+IPD%d:",(int *) &bfsz);
  595. /* get receive timeout by receive buffer length */
  596. timeout = bfsz * 10;
  597. if (bfsz == 0)
  598. return;
  599. recv_buf = rt_calloc(1, bfsz);
  600. if (!recv_buf)
  601. {
  602. LOG_E("no memory for URC receive buffer (%d)!", bfsz);
  603. /* read and clean the coming data */
  604. while (temp_size < bfsz)
  605. {
  606. if (bfsz - temp_size > sizeof(temp))
  607. {
  608. at_client_recv(temp, sizeof(temp), timeout);
  609. }
  610. else
  611. {
  612. at_client_recv(temp, bfsz - temp_size, timeout);
  613. }
  614. temp_size += sizeof(temp);
  615. }
  616. return;
  617. }
  618. /* sync receive data */
  619. if (at_client_recv(recv_buf, bfsz, timeout) != bfsz)
  620. {
  621. LOG_E("receive size(%d) data failed!", bfsz);
  622. rt_free(recv_buf);
  623. return;
  624. }
  625. /* notice the receive buffer and buffer size */
  626. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  627. {
  628. at_evt_cb_set[AT_SOCKET_EVT_RECV](cur_socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  629. }
  630. }
  631. static struct at_urc urc_table[] = {
  632. {"+CIPSEND:", "\r\n", urc_send_func},
  633. {"+CIPOPEN:", "\r\n", urc_connect_func},
  634. {"+CPING:", "\r\n", urc_ping_func},
  635. {"+IPCLOSE", "\r\n", urc_close_func},
  636. {"+IPD", "\r\n", urc_recv_func},
  637. };
  638. #define AT_SEND_CMD(resp, cmd) \
  639. do \
  640. { \
  641. if (at_exec_cmd(at_resp_set_info(resp, 256, 0, rt_tick_from_millisecond(5000)), cmd) < 0) \
  642. { \
  643. LOG_E("RT AT send commands(%s) error!", cmd); \
  644. result = -RT_ERROR; \
  645. goto __exit; \
  646. } \
  647. } while(0); \
  648. /**
  649. * power up sim76xx modem
  650. */
  651. static void sim76xx_power_on(void)
  652. {
  653. rt_pin_write(AT_DEVICE_POWER_PIN, PIN_HIGH);
  654. rt_thread_delay(rt_tick_from_millisecond(300));
  655. rt_pin_write(AT_DEVICE_POWER_PIN, PIN_LOW);
  656. while (rt_pin_read(AT_DEVICE_STATUS_PIN) == PIN_LOW)
  657. {
  658. rt_thread_delay(rt_tick_from_millisecond(10));
  659. }
  660. }
  661. static void sim76xx_power_off(void)
  662. {
  663. rt_pin_write(AT_DEVICE_POWER_PIN, PIN_HIGH);
  664. rt_thread_delay(rt_tick_from_millisecond(3000));
  665. rt_pin_write(AT_DEVICE_POWER_PIN, PIN_LOW);
  666. while (rt_pin_read(AT_DEVICE_STATUS_PIN) == PIN_HIGH)
  667. {
  668. rt_thread_delay(rt_tick_from_millisecond(10));
  669. }
  670. rt_pin_write(AT_DEVICE_POWER_PIN, PIN_LOW);
  671. }
  672. static void sim76xx_init_thread_entry(void *parameter)
  673. {
  674. #define CSQ_RETRY 20
  675. #define CREG_RETRY 10
  676. #define CGREG_RETRY 20
  677. #define CGATT_RETRY 10
  678. #define CCLK_RETRY 10
  679. at_response_t resp = RT_NULL;
  680. rt_err_t result = RT_EOK;
  681. rt_size_t i, qi_arg[3];
  682. char parsed_data[20];
  683. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  684. if (!resp)
  685. {
  686. LOG_E("No memory for response structure!");
  687. result = -RT_ENOMEM;
  688. goto __exit;
  689. }
  690. /* power-up sim76xx */
  691. sim76xx_power_on();
  692. LOG_D("Start initializing the SIM76XXE module");
  693. /* wait SIM76XX startup finish, Send AT every 5s, if receive OK, SYNC success*/
  694. if (at_client_wait_connect(SIM76XX_WAIT_CONNECT_TIME))
  695. {
  696. result = -RT_ETIMEOUT;
  697. goto __exit;
  698. }
  699. /* disable echo */
  700. AT_SEND_CMD(resp, "ATE0");
  701. /* get module version */
  702. AT_SEND_CMD(resp, "ATI");
  703. /* show module version */
  704. for (i = 0; i < (int) resp->line_counts - 1; i++)
  705. {
  706. LOG_D("%s", at_resp_get_line(resp, i + 1));
  707. }
  708. /* check SIM card */
  709. AT_SEND_CMD(resp, "AT+CPIN?");
  710. if (!at_resp_get_line_by_kw(resp, "READY"))
  711. {
  712. LOG_E("SIM card detection failed");
  713. result = -RT_ERROR;
  714. goto __exit;
  715. }
  716. /* waiting for dirty data to be digested */
  717. rt_thread_delay(rt_tick_from_millisecond(10));
  718. /* check signal strength */
  719. for (i = 0; i < CSQ_RETRY; i++)
  720. {
  721. AT_SEND_CMD(resp, "AT+CSQ");
  722. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &qi_arg[0], &qi_arg[1]);
  723. if (qi_arg[0] != 99)
  724. {
  725. LOG_D("Signal strength: %d Channel bit error rate: %d", qi_arg[0], qi_arg[1]);
  726. break;
  727. }
  728. rt_thread_delay(rt_tick_from_millisecond(1000));
  729. }
  730. if (i == CSQ_RETRY)
  731. {
  732. LOG_E("Signal strength check failed (%s)", parsed_data);
  733. result = -RT_ERROR;
  734. goto __exit;
  735. }
  736. //do not show the prompt when receiving data
  737. AT_SEND_CMD(resp, "AT+CIPSRIP=0");
  738. /* check the GSM network is registered */
  739. for (i = 0; i < CREG_RETRY; i++)
  740. {
  741. AT_SEND_CMD(resp,"AT+CREG?");
  742. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  743. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) || !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  744. {
  745. LOG_D("GSM network is registered (%s)", parsed_data);
  746. break;
  747. }
  748. rt_thread_delay(rt_tick_from_millisecond(1000));
  749. }
  750. if (i == CREG_RETRY)
  751. {
  752. LOG_E("The GSM network is register failed (%s)", parsed_data);
  753. result = -RT_ERROR;
  754. goto __exit;
  755. }
  756. /* check the GPRS network is registered */
  757. for (i = 0; i < CGREG_RETRY; i++)
  758. {
  759. AT_SEND_CMD(resp,"AT+CGREG?");
  760. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %s", &parsed_data);
  761. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) || !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  762. {
  763. LOG_D("GPRS network is registered (%s)", parsed_data);
  764. break;
  765. }
  766. rt_thread_delay(rt_tick_from_millisecond(1000));
  767. }
  768. if (i == CGREG_RETRY)
  769. {
  770. LOG_E("The GPRS network is register failed (%s)", parsed_data);
  771. result = -RT_ERROR;
  772. goto __exit;
  773. }
  774. /* check packet domain attach or detach */
  775. for (i = 0;i < CGATT_RETRY; i++)
  776. {
  777. AT_SEND_CMD(resp,"AT+CGATT?")
  778. at_resp_parse_line_args_by_kw(resp,"+CGATT:","+CGATT: %s",&parsed_data);
  779. if (!strncmp(parsed_data,"1",1))
  780. {
  781. LOG_I("Packet domain attach");
  782. break;
  783. }
  784. rt_thread_delay(rt_tick_from_millisecond(1000));
  785. }
  786. if (i == CGATT_RETRY)
  787. {
  788. LOG_E("The GPRS network attach failed");
  789. result = -RT_ERROR;
  790. goto __exit;
  791. }
  792. /* get real time */
  793. int year,month,day,hour,min,sec;
  794. for (i = 0;i < CCLK_RETRY;i++)
  795. {
  796. if (at_exec_cmd(at_resp_set_info(resp, 256, 0, rt_tick_from_millisecond(5000)), "AT+CCLK?") < 0)
  797. {
  798. rt_thread_delay(rt_tick_from_millisecond(500));
  799. continue;
  800. }
  801. //+CCLK: "18/12/22,18:33:12+32"
  802. if (at_resp_parse_line_args_by_kw(resp,"+CCLK:","+CCLK: \"%d/%d/%d,%d:%d:%d",&year,&month,&day,&hour,&min,&sec) < 0)
  803. {
  804. rt_thread_delay(rt_tick_from_millisecond(500));
  805. continue;
  806. }
  807. set_date(year + 2000,month,day);
  808. set_time(hour,min,sec);
  809. break;
  810. }
  811. if (i == CCLK_RETRY)
  812. {
  813. LOG_E("The GPRS network attach failed");
  814. result = -RT_ERROR;
  815. goto __exit;
  816. }
  817. /* set active PDP context's profile number */
  818. AT_SEND_CMD(resp, "AT+CSOCKSETPN=1");
  819. __exit:
  820. if (resp)
  821. {
  822. at_delete_resp(resp);
  823. }
  824. if (!result)
  825. {
  826. LOG_I("AT network initialize success!");
  827. at_socket_init = 1;
  828. }
  829. else
  830. {
  831. LOG_E("AT network initialize failed (%d)!", result);
  832. }
  833. }
  834. int sim76xx_net_init(void)
  835. {
  836. #ifdef PKG_AT_INIT_BY_THREAD
  837. rt_thread_t tid;
  838. tid = rt_thread_create("sim76xx_net_init", sim76xx_init_thread_entry, RT_NULL,SIM76XX_THREAD_STACK_SIZE, SIM76XX_THREAD_PRIORITY, 20);
  839. if (tid)
  840. {
  841. rt_thread_startup(tid);
  842. }
  843. else
  844. {
  845. LOG_E("Create AT initialization thread fail!");
  846. }
  847. #else
  848. sim76xx_init_thread_entry(RT_NULL);
  849. #endif
  850. return RT_EOK;
  851. }
  852. int sim76xx_ping(int argc, char **argv)
  853. {
  854. at_response_t resp = RT_NULL;
  855. if (argc != 2)
  856. {
  857. rt_kprintf("Please input: at_ping <host address>\n");
  858. return -RT_ERROR;
  859. }
  860. resp = at_create_resp(64, 0, rt_tick_from_millisecond(5000));
  861. if (!resp)
  862. {
  863. rt_kprintf("No memory for response structure!\n");
  864. return -RT_ENOMEM;
  865. }
  866. if (at_exec_cmd(resp, "AT+CPING=\"%s\",1,4,64,1000,10000,255", argv[1]) < 0)
  867. {
  868. if (resp)
  869. {
  870. at_delete_resp(resp);
  871. }
  872. rt_kprintf("AT send ping commands error!\n");
  873. return -RT_ERROR;
  874. }
  875. if (resp)
  876. {
  877. at_delete_resp(resp);
  878. }
  879. return RT_EOK;
  880. }
  881. int sim76xx_connect(int argc, char **argv)
  882. {
  883. int32_t port;
  884. if (argc != 3)
  885. {
  886. rt_kprintf("Please input: at_connect <host address>\n");
  887. return -RT_ERROR;
  888. }
  889. sscanf(argv[2],"%d",&port);
  890. sim76xx_socket_connect(0, argv[1], port, AT_SOCKET_TCP, 1);
  891. return RT_EOK;
  892. }
  893. int sim76xx_close(int argc, char **argv)
  894. {
  895. if (sim76xx_socket_close(0) < 0)
  896. {
  897. rt_kprintf("sim76xx_socket_close fail\n");
  898. }
  899. else
  900. {
  901. rt_kprintf("sim76xx_socket_closeed\n");
  902. }
  903. return RT_EOK;
  904. }
  905. int sim76xx_send(int argc, char **argv)
  906. {
  907. const char *buff = "1234567890\n";
  908. if (sim76xx_socket_send(0, buff, 11, AT_SOCKET_TCP) < 0)
  909. {
  910. rt_kprintf("sim76xx_socket_send fail\n");
  911. }
  912. return RT_EOK;
  913. }
  914. int sim76xx_domain(int argc, char **argv)
  915. {
  916. char ip[16];
  917. if (sim76xx_domain_resolve("www.baidu.com", ip) < 0)
  918. {
  919. rt_kprintf("sim76xx_socket_send fail\n");
  920. }
  921. else
  922. {
  923. rt_kprintf("baidu.com : %s\n", ip);
  924. }
  925. return RT_EOK;
  926. }
  927. int sim76xx_ifconfig(void)
  928. {
  929. at_response_t resp = RT_NULL;
  930. char resp_arg[AT_CMD_MAX_LEN] = { 0 };
  931. rt_err_t result = RT_EOK;
  932. resp = at_create_resp(128, 2, rt_tick_from_millisecond(300));
  933. if (!resp)
  934. {
  935. rt_kprintf("No memory for response structure!\n");
  936. return -RT_ENOMEM;
  937. }
  938. /* Show PDP address */
  939. AT_SEND_CMD(resp, "AT+CGPADDR");
  940. at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %s", &resp_arg);
  941. rt_kprintf("IP adress : %s\n", resp_arg);
  942. __exit:
  943. if (resp)
  944. {
  945. at_delete_resp(resp);
  946. }
  947. return result;
  948. }
  949. #ifdef FINSH_USING_MSH
  950. #include <finsh.h>
  951. MSH_CMD_EXPORT_ALIAS(sim76xx_net_init, at_net_init, initialize AT network);
  952. MSH_CMD_EXPORT_ALIAS(sim76xx_ping, at_ping, AT ping network host);
  953. MSH_CMD_EXPORT_ALIAS(sim76xx_ifconfig, at_ifconfig, list the information of network interfaces);
  954. MSH_CMD_EXPORT_ALIAS(sim76xx_connect, at_connect, AT connect network host);
  955. MSH_CMD_EXPORT_ALIAS(sim76xx_close, at_close, AT close a socket);
  956. MSH_CMD_EXPORT_ALIAS(sim76xx_send, at_send, AT send a pack);
  957. MSH_CMD_EXPORT_ALIAS(sim76xx_domain, at_domain, AT domain resolve);
  958. #endif
  959. static const struct at_device_ops sim76xx_socket_ops = {
  960. sim76xx_socket_connect,
  961. sim76xx_socket_close,
  962. sim76xx_socket_send,
  963. sim76xx_domain_resolve,
  964. sim76xx_socket_set_event_cb,
  965. };
  966. static int at_socket_device_init(void)
  967. {
  968. /* create current AT socket event */
  969. at_socket_event = rt_event_create("at_se", RT_IPC_FLAG_FIFO);
  970. if (at_socket_event == RT_NULL)
  971. {
  972. LOG_E("RT AT client port initialize failed! at_sock_event create failed!");
  973. return -RT_ENOMEM;
  974. }
  975. /* create current AT socket event lock */
  976. at_event_lock = rt_mutex_create("at_se", RT_IPC_FLAG_FIFO);
  977. if (at_event_lock == RT_NULL)
  978. {
  979. LOG_E("RT AT client port initialize failed! at_sock_lock create failed!");
  980. rt_event_delete(at_socket_event);
  981. return -RT_ENOMEM;
  982. }
  983. /* initialize AT client */
  984. at_client_init(AT_DEVICE_NAME, AT_DEVICE_RECV_BUFF_LEN);
  985. /* register URC data execution function */
  986. at_set_urc_table(urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  987. /* initialize sim76xx pin config */
  988. rt_pin_mode(AT_DEVICE_POWER_PIN, PIN_MODE_OUTPUT);
  989. rt_pin_mode(AT_DEVICE_STATUS_PIN, PIN_MODE_INPUT);
  990. /* initialize sim76xx network */
  991. sim76xx_net_init();
  992. /* set sim76xx AT Socket options */
  993. at_socket_device_register(&sim76xx_socket_ops);
  994. return RT_EOK;
  995. }
  996. INIT_APP_EXPORT(at_socket_device_init);
  997. #endif /* AT_DEVICE_SIM76XX */