at_device_sim800c.c 28 KB

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