at_device_sim800c.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * File : at_socket_sim800c.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-06-12 malongwei first version
  23. * 2019-05-13 chenyong multi AT socket client support
  24. * 2020-07-24 awenchen fix the stack overflow when parse cops
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <at_device_sim800c.h>
  30. #define LOG_TAG "at.dev.sim800"
  31. #include <at_log.h>
  32. #ifdef AT_DEVICE_USING_SIM800C
  33. #define SIM800C_WAIT_CONNECT_TIME 5000
  34. #define SIM800C_THREAD_STACK_SIZE 2048
  35. #define SIM800C_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  36. /* AT+CSTT command default*/
  37. static char *CSTT_CHINA_MOBILE = "AT+CSTT=\"CMNET\"";
  38. static char *CSTT_CHINA_UNICOM = "AT+CSTT=\"UNINET\"";
  39. static char *CSTT_CHINA_TELECOM = "AT+CSTT=\"CTNET\"";
  40. static void sim800c_power_on(struct at_device *device)
  41. {
  42. struct at_device_sim800c *sim800c = RT_NULL;
  43. sim800c = (struct at_device_sim800c *) device->user_data;
  44. /* not nead to set pin configuration for m26 device power on */
  45. if (sim800c->power_pin == -1 || sim800c->power_status_pin == -1)
  46. {
  47. return;
  48. }
  49. if (rt_pin_read(sim800c->power_status_pin) == PIN_HIGH)
  50. {
  51. return;
  52. }
  53. rt_pin_write(sim800c->power_pin, PIN_HIGH);
  54. while (rt_pin_read(sim800c->power_status_pin) == PIN_LOW)
  55. {
  56. rt_thread_mdelay(10);
  57. }
  58. rt_pin_write(sim800c->power_pin, PIN_LOW);
  59. }
  60. static void sim800c_power_off(struct at_device *device)
  61. {
  62. struct at_device_sim800c *sim800c = RT_NULL;
  63. sim800c = (struct at_device_sim800c *) device->user_data;
  64. /* not nead to set pin configuration for m26 device power on */
  65. if (sim800c->power_pin == -1 || sim800c->power_status_pin == -1)
  66. {
  67. return;
  68. }
  69. if (rt_pin_read(sim800c->power_status_pin) == PIN_LOW)
  70. {
  71. return;
  72. }
  73. rt_pin_write(sim800c->power_pin, PIN_HIGH);
  74. while (rt_pin_read(sim800c->power_status_pin) == PIN_HIGH)
  75. {
  76. rt_thread_mdelay(10);
  77. }
  78. rt_pin_write(sim800c->power_pin, PIN_LOW);
  79. }
  80. /* ============================= sim76xx network interface operations ============================= */
  81. /* set sim800c network interface device status and address information */
  82. static int sim800c_netdev_set_info(struct netdev *netdev)
  83. {
  84. #define SIM800C_IMEI_RESP_SIZE 32
  85. #define SIM800C_IPADDR_RESP_SIZE 32
  86. #define SIM800C_DNS_RESP_SIZE 96
  87. #define SIM800C_INFO_RESP_TIMO rt_tick_from_millisecond(300)
  88. int result = RT_EOK;
  89. ip_addr_t addr;
  90. at_response_t resp = RT_NULL;
  91. struct at_device *device = RT_NULL;
  92. RT_ASSERT(netdev);
  93. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  94. if (device == RT_NULL)
  95. {
  96. LOG_E("get device(%s) failed.");
  97. return -RT_ERROR;
  98. }
  99. /* set network interface device status */
  100. netdev_low_level_set_status(netdev, RT_TRUE);
  101. netdev_low_level_set_link_status(netdev, RT_TRUE);
  102. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  103. resp = at_create_resp(SIM800C_IMEI_RESP_SIZE, 0, SIM800C_INFO_RESP_TIMO);
  104. if (resp == RT_NULL)
  105. {
  106. LOG_E("no memory for resp create.");
  107. result = -RT_ENOMEM;
  108. goto __exit;
  109. }
  110. /* set network interface device hardware address(IMEI) */
  111. {
  112. #define SIM800C_NETDEV_HWADDR_LEN 8
  113. #define SIM800C_IMEI_LEN 15
  114. char imei[SIM800C_IMEI_LEN] = {0};
  115. int i = 0, j = 0;
  116. /* send "AT+GSN" commond to get device IMEI */
  117. if (at_obj_exec_cmd(device->client, resp, "AT+GSN") < 0)
  118. {
  119. result = -RT_ERROR;
  120. goto __exit;
  121. }
  122. if (at_resp_parse_line_args(resp, 2, "%s", imei) <= 0)
  123. {
  124. LOG_E("%s device prase \"AT+GSN\" cmd error.", device->name);
  125. result = -RT_ERROR;
  126. goto __exit;
  127. }
  128. LOG_D("%s device IMEI number: %s", device->name, imei);
  129. netdev->hwaddr_len = SIM800C_NETDEV_HWADDR_LEN;
  130. /* get hardware address by IMEI */
  131. for (i = 0, j = 0; i < SIM800C_NETDEV_HWADDR_LEN && j < SIM800C_IMEI_LEN; i++, j += 2)
  132. {
  133. if (j != SIM800C_IMEI_LEN - 1)
  134. {
  135. netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
  136. }
  137. else
  138. {
  139. netdev->hwaddr[i] = (imei[j] - '0');
  140. }
  141. }
  142. }
  143. /* set network interface device IP address */
  144. {
  145. #define IP_ADDR_SIZE_MAX 16
  146. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  147. at_resp_set_info(resp, SIM800C_IPADDR_RESP_SIZE, 2, SIM800C_INFO_RESP_TIMO);
  148. /* send "AT+CIFSR" commond to get IP address */
  149. if (at_obj_exec_cmd(device->client, resp, "AT+CIFSR") < 0)
  150. {
  151. result = -RT_ERROR;
  152. goto __exit;
  153. }
  154. if (at_resp_parse_line_args_by_kw(resp, ".", "%s", ipaddr) <= 0)
  155. {
  156. LOG_E("%s device prase \"AT+CIFSR\" cmd error.", device->name);
  157. result = -RT_ERROR;
  158. goto __exit;
  159. }
  160. LOG_D("%s device IP address: %s", device->name, ipaddr);
  161. /* set network interface address information */
  162. inet_aton(ipaddr, &addr);
  163. netdev_low_level_set_ipaddr(netdev, &addr);
  164. }
  165. /* set network interface device dns server */
  166. {
  167. #define DNS_ADDR_SIZE_MAX 16
  168. char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
  169. at_resp_set_info(resp, SIM800C_DNS_RESP_SIZE, 0, SIM800C_INFO_RESP_TIMO);
  170. /* send "AT+CDNSCFG?" commond to get DNS servers address */
  171. if (at_obj_exec_cmd(device->client, resp, "AT+CDNSCFG?") < 0)
  172. {
  173. result = -RT_ERROR;
  174. goto __exit;
  175. }
  176. if (at_resp_parse_line_args_by_kw(resp, "PrimaryDns:", "PrimaryDns:%s", dns_server1) <= 0 ||
  177. at_resp_parse_line_args_by_kw(resp, "SecondaryDns:", "SecondaryDns:%s", dns_server2) <= 0)
  178. {
  179. LOG_E("%s device prase \"AT+CDNSCFG?\" cmd error.", device->name);
  180. result = -RT_ERROR;
  181. goto __exit;
  182. }
  183. LOG_D("%s device primary DNS server address: %s", device->name, dns_server1);
  184. LOG_D("%s device secondary DNS server address: %s", device->name, dns_server2);
  185. inet_aton(dns_server1, &addr);
  186. netdev_low_level_set_dns_server(netdev, 0, &addr);
  187. inet_aton(dns_server2, &addr);
  188. netdev_low_level_set_dns_server(netdev, 1, &addr);
  189. }
  190. __exit:
  191. if (resp)
  192. {
  193. at_delete_resp(resp);
  194. }
  195. return result;
  196. }
  197. static void check_link_status_entry(void *parameter)
  198. {
  199. #define SIM800C_LINK_STATUS_OK 1
  200. #define SIM800C_LINK_RESP_SIZE 64
  201. #define SIM800C_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
  202. #define SIM800C_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
  203. at_response_t resp = RT_NULL;
  204. int result_code, link_status;
  205. struct at_device *device = RT_NULL;
  206. struct netdev *netdev = (struct netdev *)parameter;
  207. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  208. if (device == RT_NULL)
  209. {
  210. LOG_E("get device(%s) failed.", netdev->name);
  211. return;
  212. }
  213. resp = at_create_resp(SIM800C_LINK_RESP_SIZE, 0, SIM800C_LINK_RESP_TIMO);
  214. if (resp == RT_NULL)
  215. {
  216. LOG_E("no memory for resp create.");
  217. return;
  218. }
  219. while (1)
  220. {
  221. /* send "AT+CGREG?" commond to check netweork interface device link status */
  222. if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") < 0)
  223. {
  224. rt_thread_mdelay(SIM800C_LINK_DELAY_TIME);
  225. continue;
  226. }
  227. link_status = -1;
  228. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %d,%d", &result_code, &link_status);
  229. /* check the network interface device link status */
  230. if ((SIM800C_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
  231. {
  232. netdev_low_level_set_link_status(netdev, (SIM800C_LINK_STATUS_OK == link_status));
  233. }
  234. rt_thread_mdelay(SIM800C_LINK_DELAY_TIME);
  235. }
  236. }
  237. static int sim800c_netdev_check_link_status(struct netdev *netdev)
  238. {
  239. #define SIM800C_LINK_THREAD_TICK 20
  240. #define SIM800C_LINK_THREAD_STACK_SIZE (1024 + 512)
  241. #define SIM800C_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  242. rt_thread_t tid;
  243. char tname[RT_NAME_MAX] = {0};
  244. RT_ASSERT(netdev);
  245. rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
  246. tid = rt_thread_create(tname, check_link_status_entry, (void *) netdev,
  247. SIM800C_LINK_THREAD_STACK_SIZE, SIM800C_LINK_THREAD_PRIORITY, SIM800C_LINK_THREAD_TICK);
  248. if (tid)
  249. {
  250. rt_thread_startup(tid);
  251. }
  252. return RT_EOK;
  253. }
  254. static int sim800c_net_init(struct at_device *device);
  255. static int sim800c_netdev_set_up(struct netdev *netdev)
  256. {
  257. struct at_device *device = RT_NULL;
  258. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  259. if (device == RT_NULL)
  260. {
  261. LOG_E("get device(%s) failed.", netdev->name);
  262. return -RT_ERROR;
  263. }
  264. if (device->is_init == RT_FALSE)
  265. {
  266. sim800c_net_init(device);
  267. device->is_init = RT_TRUE;
  268. netdev_low_level_set_status(netdev, RT_TRUE);
  269. LOG_D("network interface device(%s) set up status.", netdev->name);
  270. }
  271. return RT_EOK;
  272. }
  273. static int sim800c_netdev_set_down(struct netdev *netdev)
  274. {
  275. struct at_device *device = RT_NULL;
  276. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  277. if (device == RT_NULL)
  278. {
  279. LOG_E("get device(%s) failed.", netdev->name);
  280. return -RT_ERROR;
  281. }
  282. if (device->is_init == RT_TRUE)
  283. {
  284. sim800c_power_off(device);
  285. device->is_init = RT_FALSE;
  286. netdev_low_level_set_status(netdev, RT_FALSE);
  287. LOG_D("network interface device(%s) set down status.", netdev->name);
  288. }
  289. return RT_EOK;
  290. }
  291. static int sim800c_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  292. {
  293. #define SIM800C_DNS_RESP_LEN 8
  294. #define SIM800C_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
  295. int result = RT_EOK;
  296. at_response_t resp = RT_NULL;
  297. struct at_device *device = RT_NULL;
  298. RT_ASSERT(netdev);
  299. RT_ASSERT(dns_server);
  300. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  301. if (device == RT_NULL)
  302. {
  303. LOG_E("get device(%s) failed.", netdev->name);
  304. return -RT_ERROR;
  305. }
  306. resp = at_create_resp(SIM800C_DNS_RESP_LEN, 0, SIM800C_DNS_RESP_TIMEO);
  307. if (resp == RT_NULL)
  308. {
  309. LOG_D("no memory for resp create.");
  310. result = -RT_ENOMEM;
  311. goto __exit;
  312. }
  313. /* send "AT+CDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
  314. if (at_obj_exec_cmd(device->client, resp, "AT+CDNSCFG=\"%s\"", inet_ntoa(*dns_server)) < 0)
  315. {
  316. result = -RT_ERROR;
  317. goto __exit;
  318. }
  319. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  320. __exit:
  321. if (resp)
  322. {
  323. at_delete_resp(resp);
  324. }
  325. return result;
  326. }
  327. static int sim800c_ping_domain_resolve(struct at_device *device, const char *name, char ip[16])
  328. {
  329. int result = RT_EOK;
  330. char recv_ip[16] = { 0 };
  331. at_response_t resp = RT_NULL;
  332. /* The maximum response time is 14 seconds, affected by network status */
  333. resp = at_create_resp(128, 4, 14 * RT_TICK_PER_SECOND);
  334. if (resp == RT_NULL)
  335. {
  336. LOG_E("no memory for resp create.");
  337. return -RT_ENOMEM;
  338. }
  339. if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%s\"", name) < 0)
  340. {
  341. result = -RT_ERROR;
  342. goto __exit;
  343. }
  344. /* parse the third line of response data, get the IP address */
  345. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", recv_ip) < 0)
  346. {
  347. rt_thread_mdelay(100);
  348. /* resolve failed, maybe receive an URC CRLF */
  349. }
  350. if (rt_strlen(recv_ip) < 8)
  351. {
  352. rt_thread_mdelay(100);
  353. /* resolve failed, maybe receive an URC CRLF */
  354. }
  355. else
  356. {
  357. rt_strncpy(ip, recv_ip, 15);
  358. ip[15] = '\0';
  359. }
  360. __exit:
  361. if (resp)
  362. {
  363. at_delete_resp(resp);
  364. }
  365. return result;
  366. }
  367. #ifdef NETDEV_USING_PING
  368. static int sim800c_netdev_ping(struct netdev *netdev, const char *host,
  369. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
  370. {
  371. #define SIM800C_PING_RESP_SIZE 128
  372. #define SIM800C_PING_IP_SIZE 16
  373. #define SIM800C_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  374. #define SIM800C_PING_ERR_TIME 600
  375. #define SIM800C_PING_ERR_TTL 255
  376. int result = RT_EOK;
  377. int response, time, ttl, i, err_code = 0;
  378. char ip_addr[SIM800C_PING_IP_SIZE] = {0};
  379. at_response_t resp = RT_NULL;
  380. struct at_device *device = RT_NULL;
  381. RT_ASSERT(netdev);
  382. RT_ASSERT(host);
  383. RT_ASSERT(ping_resp);
  384. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  385. if (device == RT_NULL)
  386. {
  387. LOG_E("get device(%s) failed.", netdev->name);
  388. return -RT_ERROR;
  389. }
  390. for (i = 0; i < rt_strlen(host) && !isalpha(host[i]); i++);
  391. if (i < strlen(host))
  392. {
  393. /* check domain name is usable */
  394. if (sim800c_ping_domain_resolve(device, host, ip_addr) < 0)
  395. {
  396. return -RT_ERROR;
  397. }
  398. rt_memset(ip_addr, 0x00, SIM800C_PING_IP_SIZE);
  399. }
  400. resp = at_create_resp(SIM800C_PING_RESP_SIZE, 0, SIM800C_PING_TIMEO);
  401. if (resp == RT_NULL)
  402. {
  403. LOG_E("no memory for resp create.");
  404. result = -RT_ERROR;
  405. goto __exit;
  406. }
  407. /* domain name prase error options */
  408. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP: 0", "+CDNSGIP: 0,%d", &err_code) > 0)
  409. {
  410. /* 3 - network error, 8 - dns common error */
  411. if (err_code == 3 || err_code == 8)
  412. {
  413. result = -RT_ERROR;
  414. goto __exit;
  415. }
  416. }
  417. /* send "AT+CIPPING=<IP addr>[,<retryNum>[,<dataLen>[,<timeout>[,<ttl>]]]]" commond to send ping request */
  418. if (at_obj_exec_cmd(device->client, resp, "AT+CIPPING=%s,1,%d,%d,64",
  419. host, data_len, SIM800C_PING_TIMEO / (RT_TICK_PER_SECOND / 10)) < 0)
  420. {
  421. result = -RT_ERROR;
  422. goto __exit;
  423. }
  424. if (at_resp_parse_line_args_by_kw(resp, "+CIPPING:", "+CIPPING:%d,\"%[^\"]\",%d,%d",
  425. &response, ip_addr, &time, &ttl) <= 0)
  426. {
  427. result = -RT_ERROR;
  428. goto __exit;
  429. }
  430. /* the ping request timeout expires, the response time settting to 600 and ttl setting to 255 */
  431. if (time == SIM800C_PING_ERR_TIME && ttl == SIM800C_PING_ERR_TTL)
  432. {
  433. result = -RT_ETIMEOUT;
  434. goto __exit;
  435. }
  436. inet_aton(ip_addr, &(ping_resp->ip_addr));
  437. ping_resp->data_len = data_len;
  438. /* reply time, in units of 100 ms */
  439. ping_resp->ticks = time * 100;
  440. ping_resp->ttl = ttl;
  441. __exit:
  442. if (resp)
  443. {
  444. at_delete_resp(resp);
  445. }
  446. return result;
  447. }
  448. #endif /* NETDEV_USING_PING */
  449. const struct netdev_ops sim800c_netdev_ops =
  450. {
  451. sim800c_netdev_set_up,
  452. sim800c_netdev_set_down,
  453. RT_NULL, /* not support set ip, netmask, gatway address */
  454. sim800c_netdev_set_dns_server,
  455. RT_NULL, /* not support set DHCP status */
  456. #ifdef NETDEV_USING_PING
  457. sim800c_netdev_ping,
  458. #endif
  459. RT_NULL,
  460. };
  461. static struct netdev *sim800c_netdev_add(const char *netdev_name)
  462. {
  463. #define SIM800C_NETDEV_MTU 1500
  464. struct netdev *netdev = RT_NULL;
  465. RT_ASSERT(netdev_name);
  466. netdev = netdev_get_by_name(netdev_name);
  467. if (netdev != RT_NULL)
  468. {
  469. return (netdev);
  470. }
  471. netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
  472. if (netdev == RT_NULL)
  473. {
  474. LOG_E("no memory for netdev create.");
  475. return RT_NULL;
  476. }
  477. netdev->mtu = SIM800C_NETDEV_MTU;
  478. netdev->ops = &sim800c_netdev_ops;
  479. #ifdef SAL_USING_AT
  480. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  481. /* set the network interface socket/netdb operations */
  482. sal_at_netdev_set_pf_info(netdev);
  483. #endif
  484. netdev_register(netdev, netdev_name, RT_NULL);
  485. return netdev;
  486. }
  487. /* ============================= sim76xx device operations ============================= */
  488. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  489. do { \
  490. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  491. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  492. { \
  493. result = -RT_ERROR; \
  494. goto __exit; \
  495. } \
  496. } while(0) \
  497. /* init for sim800c */
  498. static void sim800c_init_thread_entry(void *parameter)
  499. {
  500. #define INIT_RETRY 5
  501. #define CPIN_RETRY 10
  502. #define CSQ_RETRY 10
  503. #define CREG_RETRY 10
  504. #define CGREG_RETRY 20
  505. int i, qimux, retry_num = INIT_RETRY;
  506. char parsed_data[32] = {0};
  507. rt_err_t result = RT_EOK;
  508. at_response_t resp = RT_NULL;
  509. struct at_device *device = (struct at_device *)parameter;
  510. struct at_client *client = device->client;
  511. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  512. if (resp == RT_NULL)
  513. {
  514. LOG_E("no memory for resp create.");
  515. return;
  516. }
  517. LOG_D("start init %s device", device->name);
  518. while (retry_num--)
  519. {
  520. rt_memset(parsed_data, 0, sizeof(parsed_data));
  521. rt_thread_mdelay(500);
  522. sim800c_power_on(device);
  523. rt_thread_mdelay(1000);
  524. /* wait sim800c startup finish */
  525. if (at_client_obj_wait_connect(client, SIM800C_WAIT_CONNECT_TIME))
  526. {
  527. result = -RT_ETIMEOUT;
  528. goto __exit;
  529. }
  530. /* disable echo */
  531. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  532. /* get module version */
  533. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  534. /* show module version */
  535. for (i = 0; i < (int)resp->line_counts - 1; i++)
  536. {
  537. LOG_D("%s", at_resp_get_line(resp, i + 1));
  538. }
  539. /* check SIM card */
  540. for (i = 0; i < CPIN_RETRY; i++)
  541. {
  542. AT_SEND_CMD(client, resp, 2, 5 * RT_TICK_PER_SECOND, "AT+CPIN?");
  543. if (at_resp_get_line_by_kw(resp, "READY"))
  544. {
  545. LOG_D("%s device SIM card detection success.", device->name);
  546. break;
  547. }
  548. rt_thread_mdelay(1000);
  549. }
  550. if (i == CPIN_RETRY)
  551. {
  552. LOG_E("%s device SIM card detection failed.", device->name);
  553. result = -RT_ERROR;
  554. goto __exit;
  555. }
  556. /* waiting for dirty data to be digested */
  557. rt_thread_mdelay(10);
  558. /* check the GSM network is registered */
  559. for (i = 0; i < CREG_RETRY; i++)
  560. {
  561. AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
  562. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  563. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  564. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  565. {
  566. LOG_D("%s device GSM is registered(%s),", device->name, parsed_data);
  567. break;
  568. }
  569. rt_thread_mdelay(1000);
  570. }
  571. if (i == CREG_RETRY)
  572. {
  573. LOG_E("%s device GSM is register failed(%s).", device->name, parsed_data);
  574. result = -RT_ERROR;
  575. goto __exit;
  576. }
  577. /* check the GPRS network is registered */
  578. for (i = 0; i < CGREG_RETRY; i++)
  579. {
  580. AT_SEND_CMD(client, resp, 0, 300, "AT+CGREG?");
  581. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %s", &parsed_data);
  582. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  583. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  584. {
  585. LOG_D("%s device GPRS is registered(%s).", device->name, parsed_data);
  586. break;
  587. }
  588. rt_thread_mdelay(1000);
  589. }
  590. if (i == CGREG_RETRY)
  591. {
  592. LOG_E("%s device GPRS is register failed(%s).", device->name, parsed_data);
  593. result = -RT_ERROR;
  594. goto __exit;
  595. }
  596. /* check signal strength */
  597. for (i = 0; i < CSQ_RETRY; i++)
  598. {
  599. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  600. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  601. if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  602. {
  603. LOG_D("%s device signal strength: %s", device->name, parsed_data);
  604. break;
  605. }
  606. rt_thread_mdelay(1000);
  607. }
  608. if (i == CSQ_RETRY)
  609. {
  610. LOG_E("%s device signal strength check failed (%s)", device->name, parsed_data);
  611. result = -RT_ERROR;
  612. goto __exit;
  613. }
  614. /* the device default response timeout is 40 seconds, but it set to 15 seconds is convenient to use. */
  615. AT_SEND_CMD(client, resp, 2, 20 * 1000, "AT+CIPSHUT");
  616. /* Set to multiple connections */
  617. AT_SEND_CMD(client, resp, 0, 300, "AT+CIPMUX?");
  618. at_resp_parse_line_args_by_kw(resp, "+CIPMUX:", "+CIPMUX: %d", &qimux);
  619. if (qimux == 0)
  620. {
  621. AT_SEND_CMD(client, resp, 0, 300, "AT+CIPMUX=1");
  622. }
  623. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
  624. at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
  625. if (rt_strcmp(parsed_data, "CHINA MOBILE") == 0)
  626. {
  627. /* "CMCC" */
  628. LOG_I("%s device network operator: %s", device->name, parsed_data);
  629. AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_MOBILE);
  630. }
  631. else if (rt_strcmp(parsed_data, "CHN-UNICOM") == 0)
  632. {
  633. /* "UNICOM" */
  634. LOG_I("%s device network operator: %s", device->name, parsed_data);
  635. AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_UNICOM);
  636. }
  637. else if (rt_strcmp(parsed_data, "CHN-CT") == 0)
  638. {
  639. AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_TELECOM);
  640. /* "CT" */
  641. LOG_I("%s device network operator: %s", device->name, parsed_data);
  642. }
  643. else
  644. {
  645. AT_SEND_CMD(client, resp, 0, 300, "AT+CSTT");
  646. LOG_I("%s device network operator: %s", device->name, parsed_data);
  647. }
  648. /* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */
  649. AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+CIICR");
  650. AT_SEND_CMD(client, resp, 2, 300, "AT+CIFSR");
  651. if (at_resp_get_line_by_kw(resp, "ERROR") != RT_NULL)
  652. {
  653. LOG_E("%s device get the local address failed.", device->name);
  654. result = -RT_ERROR;
  655. goto __exit;
  656. }
  657. /* initialize successfully */
  658. result = RT_EOK;
  659. break;
  660. __exit:
  661. if (result != RT_EOK)
  662. {
  663. /* power off the sim800c device */
  664. sim800c_power_off(device);
  665. rt_thread_mdelay(1000);
  666. LOG_I("%s device initialize retry...", device->name);
  667. }
  668. }
  669. if (resp)
  670. {
  671. at_delete_resp(resp);
  672. }
  673. if (result == RT_EOK)
  674. {
  675. /* set network interface device status and address information */
  676. sim800c_netdev_set_info(device->netdev);
  677. /* check and create link staus sync thread */
  678. if (rt_thread_find(device->netdev->name) == RT_NULL)
  679. {
  680. sim800c_netdev_check_link_status(device->netdev);
  681. }
  682. LOG_I("%s device network initialize success!", device->name);
  683. }
  684. else
  685. {
  686. LOG_E("%s device network initialize failed(%d)!", device->name, result);
  687. }
  688. }
  689. static int sim800c_net_init(struct at_device *device)
  690. {
  691. #ifdef AT_DEVICE_SIM800C_INIT_ASYN
  692. rt_thread_t tid;
  693. tid = rt_thread_create("sim800c_net", sim800c_init_thread_entry, (void *)device,
  694. SIM800C_THREAD_STACK_SIZE, SIM800C_THREAD_PRIORITY, 20);
  695. if (tid)
  696. {
  697. rt_thread_startup(tid);
  698. }
  699. else
  700. {
  701. LOG_E("create %s device init thread failed.", device->name);
  702. return -RT_ERROR;
  703. }
  704. #else
  705. sim800c_init_thread_entry(device);
  706. #endif /* AT_DEVICE_SIM800C_INIT_ASYN */
  707. return RT_EOK;
  708. }
  709. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  710. {
  711. RT_ASSERT(data);
  712. LOG_I("URC data : %.*s", size, data);
  713. }
  714. /* sim800c device URC table for the device control */
  715. static const struct at_urc urc_table[] =
  716. {
  717. {"RDY", "\r\n", urc_func},
  718. };
  719. static int sim800c_init(struct at_device *device)
  720. {
  721. struct at_device_sim800c *sim800c = (struct at_device_sim800c *) device->user_data;
  722. /* initialize AT client */
  723. at_client_init(sim800c->client_name, sim800c->recv_line_num);
  724. device->client = at_client_get(sim800c->client_name);
  725. if (device->client == RT_NULL)
  726. {
  727. LOG_E("get AT client(%s) failed.", sim800c->client_name);
  728. return -RT_ERROR;
  729. }
  730. /* register URC data execution function */
  731. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  732. #ifdef AT_USING_SOCKET
  733. sim800c_socket_init(device);
  734. #endif
  735. /* add sim800c device to the netdev list */
  736. device->netdev = sim800c_netdev_add(sim800c->device_name);
  737. if (device->netdev == RT_NULL)
  738. {
  739. LOG_E("get netdev(%s) failed.", sim800c->device_name);
  740. return -RT_ERROR;
  741. }
  742. /* initialize sim800c pin configuration */
  743. if (sim800c->power_pin != -1 && sim800c->power_status_pin != -1)
  744. {
  745. rt_pin_mode(sim800c->power_pin, PIN_MODE_OUTPUT);
  746. rt_pin_mode(sim800c->power_status_pin, PIN_MODE_INPUT);
  747. }
  748. /* initialize sim800c device network */
  749. return sim800c_netdev_set_up(device->netdev);
  750. }
  751. static int sim800c_deinit(struct at_device *device)
  752. {
  753. return sim800c_netdev_set_down(device->netdev);
  754. }
  755. static int sim800c_control(struct at_device *device, int cmd, void *arg)
  756. {
  757. int result = -RT_ERROR;
  758. RT_ASSERT(device);
  759. switch (cmd)
  760. {
  761. case AT_DEVICE_CTRL_POWER_ON:
  762. case AT_DEVICE_CTRL_POWER_OFF:
  763. case AT_DEVICE_CTRL_RESET:
  764. case AT_DEVICE_CTRL_LOW_POWER:
  765. case AT_DEVICE_CTRL_SLEEP:
  766. case AT_DEVICE_CTRL_WAKEUP:
  767. case AT_DEVICE_CTRL_NET_CONN:
  768. case AT_DEVICE_CTRL_NET_DISCONN:
  769. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  770. case AT_DEVICE_CTRL_GET_SIGNAL:
  771. case AT_DEVICE_CTRL_GET_GPS:
  772. case AT_DEVICE_CTRL_GET_VER:
  773. LOG_W("not support the control command(%d).", cmd);
  774. break;
  775. default:
  776. LOG_E("input error control command(%d).", cmd);
  777. break;
  778. }
  779. return result;
  780. }
  781. const struct at_device_ops sim800c_device_ops =
  782. {
  783. sim800c_init,
  784. sim800c_deinit,
  785. sim800c_control,
  786. };
  787. static int sim800c_device_class_register(void)
  788. {
  789. struct at_device_class *class = RT_NULL;
  790. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  791. if (class == RT_NULL)
  792. {
  793. LOG_E("no memory for device class create.");
  794. return -RT_ENOMEM;
  795. }
  796. /* fill sim800c device class object */
  797. #ifdef AT_USING_SOCKET
  798. sim800c_socket_class_register(class);
  799. #endif
  800. class->device_ops = &sim800c_device_ops;
  801. return at_device_class_register(class, AT_DEVICE_CLASS_SIM800C);
  802. }
  803. INIT_DEVICE_EXPORT(sim800c_device_class_register);
  804. #endif /* AT_DEVICE_USING_SIM800C */