at_device_sim800c.c 27 KB

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