at_device_sim800c.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. #if RT_VER_NUM >= 0x50100
  357. , rt_bool_t is_bind
  358. #endif
  359. )
  360. {
  361. #define SIM800C_PING_RESP_SIZE 128
  362. #define SIM800C_PING_IP_SIZE 16
  363. #define SIM800C_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  364. #define SIM800C_PING_ERR_TIME 600
  365. #define SIM800C_PING_ERR_TTL 255
  366. int result = RT_EOK;
  367. int response, time, ttl, i, err_code = 0;
  368. char ip_addr[SIM800C_PING_IP_SIZE] = {0};
  369. at_response_t resp = RT_NULL;
  370. struct at_device *device = RT_NULL;
  371. #if RT_VER_NUM >= 0x50100
  372. RT_UNUSED(is_bind);
  373. #endif
  374. RT_ASSERT(netdev);
  375. RT_ASSERT(host);
  376. RT_ASSERT(ping_resp);
  377. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  378. if (device == RT_NULL)
  379. {
  380. LOG_E("get device(%s) failed.", netdev->name);
  381. return -RT_ERROR;
  382. }
  383. for (i = 0; i < rt_strlen(host) && !isalpha(host[i]); i++);
  384. if (i < strlen(host))
  385. {
  386. /* check domain name is usable */
  387. if (sim800c_ping_domain_resolve(device, host, ip_addr) < 0)
  388. {
  389. return -RT_ERROR;
  390. }
  391. rt_memset(ip_addr, 0x00, SIM800C_PING_IP_SIZE);
  392. }
  393. resp = at_create_resp(SIM800C_PING_RESP_SIZE, 0, SIM800C_PING_TIMEO);
  394. if (resp == RT_NULL)
  395. {
  396. LOG_E("no memory for resp create.");
  397. result = -RT_ERROR;
  398. goto __exit;
  399. }
  400. /* domain name prase error options */
  401. if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP: 0", "+CDNSGIP: 0,%d", &err_code) > 0)
  402. {
  403. /* 3 - network error, 8 - dns common error */
  404. if (err_code == 3 || err_code == 8)
  405. {
  406. result = -RT_ERROR;
  407. goto __exit;
  408. }
  409. }
  410. /* send "AT+CIPPING=<IP addr>[,<retryNum>[,<dataLen>[,<timeout>[,<ttl>]]]]" commond to send ping request */
  411. if (at_obj_exec_cmd(device->client, resp, "AT+CIPPING=%s,1,%d,%d,64",
  412. host, data_len, SIM800C_PING_TIMEO / (RT_TICK_PER_SECOND / 10)) < 0)
  413. {
  414. result = -RT_ERROR;
  415. goto __exit;
  416. }
  417. if (at_resp_parse_line_args_by_kw(resp, "+CIPPING:", "+CIPPING:%d,\"%[^\"]\",%d,%d",
  418. &response, ip_addr, &time, &ttl) <= 0)
  419. {
  420. result = -RT_ERROR;
  421. goto __exit;
  422. }
  423. /* the ping request timeout expires, the response time settting to 600 and ttl setting to 255 */
  424. if (time == SIM800C_PING_ERR_TIME && ttl == SIM800C_PING_ERR_TTL)
  425. {
  426. result = -RT_ETIMEOUT;
  427. goto __exit;
  428. }
  429. inet_aton(ip_addr, &(ping_resp->ip_addr));
  430. ping_resp->data_len = data_len;
  431. /* reply time, in units of 100 ms */
  432. ping_resp->ticks = time * 100;
  433. ping_resp->ttl = ttl;
  434. __exit:
  435. if (resp)
  436. {
  437. at_delete_resp(resp);
  438. }
  439. return result;
  440. }
  441. #endif /* NETDEV_USING_PING */
  442. const struct netdev_ops sim800c_netdev_ops =
  443. {
  444. sim800c_netdev_set_up,
  445. sim800c_netdev_set_down,
  446. RT_NULL, /* not support set ip, netmask, gatway address */
  447. sim800c_netdev_set_dns_server,
  448. RT_NULL, /* not support set DHCP status */
  449. #ifdef NETDEV_USING_PING
  450. sim800c_netdev_ping,
  451. #endif
  452. RT_NULL,
  453. };
  454. static struct netdev *sim800c_netdev_add(const char *netdev_name)
  455. {
  456. #define SIM800C_NETDEV_MTU 1500
  457. struct netdev *netdev = RT_NULL;
  458. RT_ASSERT(netdev_name);
  459. netdev = netdev_get_by_name(netdev_name);
  460. if (netdev != RT_NULL)
  461. {
  462. return (netdev);
  463. }
  464. netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
  465. if (netdev == RT_NULL)
  466. {
  467. LOG_E("no memory for netdev create.");
  468. return RT_NULL;
  469. }
  470. netdev->mtu = SIM800C_NETDEV_MTU;
  471. netdev->ops = &sim800c_netdev_ops;
  472. #ifdef SAL_USING_AT
  473. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  474. /* set the network interface socket/netdb operations */
  475. sal_at_netdev_set_pf_info(netdev);
  476. #endif
  477. netdev_register(netdev, netdev_name, RT_NULL);
  478. return netdev;
  479. }
  480. /* ============================= sim76xx device operations ============================= */
  481. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  482. do { \
  483. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  484. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  485. { \
  486. result = -RT_ERROR; \
  487. goto __exit; \
  488. } \
  489. } while(0) \
  490. /* init for sim800c */
  491. static void sim800c_init_thread_entry(void *parameter)
  492. {
  493. #define INIT_RETRY 5
  494. #define CPIN_RETRY 10
  495. #define CSQ_RETRY 10
  496. #define CREG_RETRY 10
  497. #define CGREG_RETRY 20
  498. int i, qimux, retry_num = INIT_RETRY;
  499. char parsed_data[32] = {0};
  500. rt_err_t result = RT_EOK;
  501. at_response_t resp = RT_NULL;
  502. struct at_device *device = (struct at_device *)parameter;
  503. struct at_client *client = device->client;
  504. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  505. if (resp == RT_NULL)
  506. {
  507. LOG_E("no memory for resp create.");
  508. return;
  509. }
  510. LOG_D("start init %s device", device->name);
  511. while (retry_num--)
  512. {
  513. rt_memset(parsed_data, 0, sizeof(parsed_data));
  514. rt_thread_mdelay(500);
  515. sim800c_power_on(device);
  516. rt_thread_mdelay(1000);
  517. /* wait sim800c startup finish */
  518. if (at_client_obj_wait_connect(client, SIM800C_WAIT_CONNECT_TIME))
  519. {
  520. result = -RT_ETIMEOUT;
  521. goto __exit;
  522. }
  523. /* disable echo */
  524. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  525. /* get module version */
  526. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  527. /* show module version */
  528. for (i = 0; i < (int)resp->line_counts - 1; i++)
  529. {
  530. LOG_D("%s", at_resp_get_line(resp, i + 1));
  531. }
  532. /* check SIM card */
  533. for (i = 0; i < CPIN_RETRY; i++)
  534. {
  535. AT_SEND_CMD(client, resp, 2, 5 * RT_TICK_PER_SECOND, "AT+CPIN?");
  536. if (at_resp_get_line_by_kw(resp, "READY"))
  537. {
  538. LOG_D("%s device SIM card detection success.", device->name);
  539. break;
  540. }
  541. rt_thread_mdelay(1000);
  542. }
  543. if (i == CPIN_RETRY)
  544. {
  545. LOG_E("%s device SIM card detection failed.", device->name);
  546. result = -RT_ERROR;
  547. goto __exit;
  548. }
  549. /* waiting for dirty data to be digested */
  550. rt_thread_mdelay(10);
  551. /* check the GSM network is registered */
  552. for (i = 0; i < CREG_RETRY; i++)
  553. {
  554. AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
  555. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  556. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  557. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  558. {
  559. LOG_D("%s device GSM is registered(%s),", device->name, parsed_data);
  560. break;
  561. }
  562. rt_thread_mdelay(1000);
  563. }
  564. if (i == CREG_RETRY)
  565. {
  566. LOG_E("%s device GSM is register failed(%s).", device->name, parsed_data);
  567. result = -RT_ERROR;
  568. goto __exit;
  569. }
  570. /* check the GPRS network is registered */
  571. for (i = 0; i < CGREG_RETRY; i++)
  572. {
  573. AT_SEND_CMD(client, resp, 0, 300, "AT+CGREG?");
  574. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %s", &parsed_data);
  575. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  576. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  577. {
  578. LOG_D("%s device GPRS is registered(%s).", device->name, parsed_data);
  579. break;
  580. }
  581. rt_thread_mdelay(1000);
  582. }
  583. if (i == CGREG_RETRY)
  584. {
  585. LOG_E("%s device GPRS is register failed(%s).", device->name, parsed_data);
  586. result = -RT_ERROR;
  587. goto __exit;
  588. }
  589. /* check signal strength */
  590. for (i = 0; i < CSQ_RETRY; i++)
  591. {
  592. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  593. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  594. if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  595. {
  596. LOG_D("%s device signal strength: %s", device->name, parsed_data);
  597. break;
  598. }
  599. rt_thread_mdelay(1000);
  600. }
  601. if (i == CSQ_RETRY)
  602. {
  603. LOG_E("%s device signal strength check failed (%s)", device->name, parsed_data);
  604. result = -RT_ERROR;
  605. goto __exit;
  606. }
  607. /* the device default response timeout is 40 seconds, but it set to 15 seconds is convenient to use. */
  608. AT_SEND_CMD(client, resp, 2, 20 * 1000, "AT+CIPSHUT");
  609. /* Set to multiple connections */
  610. AT_SEND_CMD(client, resp, 0, 300, "AT+CIPMUX?");
  611. at_resp_parse_line_args_by_kw(resp, "+CIPMUX:", "+CIPMUX: %d", &qimux);
  612. if (qimux == 0)
  613. {
  614. AT_SEND_CMD(client, resp, 0, 300, "AT+CIPMUX=1");
  615. }
  616. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
  617. at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
  618. if (rt_strcmp(parsed_data, "CHINA MOBILE") == 0)
  619. {
  620. /* "CMCC" */
  621. LOG_I("%s device network operator: %s", device->name, parsed_data);
  622. AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_MOBILE);
  623. }
  624. else if (rt_strcmp(parsed_data, "CHN-UNICOM") == 0)
  625. {
  626. /* "UNICOM" */
  627. LOG_I("%s device network operator: %s", device->name, parsed_data);
  628. AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_UNICOM);
  629. }
  630. else if (rt_strcmp(parsed_data, "CHN-CT") == 0)
  631. {
  632. AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_TELECOM);
  633. /* "CT" */
  634. LOG_I("%s device network operator: %s", device->name, parsed_data);
  635. }
  636. else
  637. {
  638. AT_SEND_CMD(client, resp, 0, 300, "AT+CSTT");
  639. LOG_I("%s device network operator: %s", device->name, parsed_data);
  640. }
  641. /* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */
  642. AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+CIICR");
  643. AT_SEND_CMD(client, resp, 2, 300, "AT+CIFSR");
  644. if (at_resp_get_line_by_kw(resp, "ERROR") != RT_NULL)
  645. {
  646. LOG_E("%s device get the local address failed.", device->name);
  647. result = -RT_ERROR;
  648. goto __exit;
  649. }
  650. /* initialize successfully */
  651. result = RT_EOK;
  652. break;
  653. __exit:
  654. if (result != RT_EOK)
  655. {
  656. /* power off the sim800c device */
  657. sim800c_power_off(device);
  658. rt_thread_mdelay(1000);
  659. LOG_I("%s device initialize retry...", device->name);
  660. }
  661. }
  662. if (resp)
  663. {
  664. at_delete_resp(resp);
  665. }
  666. if (result == RT_EOK)
  667. {
  668. /* set network interface device status and address information */
  669. sim800c_netdev_set_info(device->netdev);
  670. /* check and create link staus sync thread */
  671. if (rt_thread_find(device->netdev->name) == RT_NULL)
  672. {
  673. sim800c_netdev_check_link_status(device->netdev);
  674. }
  675. LOG_I("%s device network initialize success!", device->name);
  676. }
  677. else
  678. {
  679. LOG_E("%s device network initialize failed(%d)!", device->name, result);
  680. }
  681. }
  682. static int sim800c_net_init(struct at_device *device)
  683. {
  684. #ifdef AT_DEVICE_SIM800C_INIT_ASYN
  685. rt_thread_t tid;
  686. tid = rt_thread_create("sim800c_net", sim800c_init_thread_entry, (void *)device,
  687. SIM800C_THREAD_STACK_SIZE, SIM800C_THREAD_PRIORITY, 20);
  688. if (tid)
  689. {
  690. rt_thread_startup(tid);
  691. }
  692. else
  693. {
  694. LOG_E("create %s device init thread failed.", device->name);
  695. return -RT_ERROR;
  696. }
  697. #else
  698. sim800c_init_thread_entry(device);
  699. #endif /* AT_DEVICE_SIM800C_INIT_ASYN */
  700. return RT_EOK;
  701. }
  702. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  703. {
  704. RT_ASSERT(data);
  705. LOG_I("URC data : %.*s", size, data);
  706. }
  707. /* sim800c device URC table for the device control */
  708. static const struct at_urc urc_table[] =
  709. {
  710. {"RDY", "\r\n", urc_func},
  711. };
  712. static int sim800c_init(struct at_device *device)
  713. {
  714. struct at_device_sim800c *sim800c = (struct at_device_sim800c *) device->user_data;
  715. /* initialize AT client */
  716. #if RT_VER_NUM >= 0x50100
  717. at_client_init(sim800c->client_name, sim800c->recv_line_num, sim800c->recv_line_num);
  718. #else
  719. at_client_init(sim800c->client_name, sim800c->recv_line_num);
  720. #endif
  721. device->client = at_client_get(sim800c->client_name);
  722. if (device->client == RT_NULL)
  723. {
  724. LOG_E("get AT client(%s) failed.", sim800c->client_name);
  725. return -RT_ERROR;
  726. }
  727. /* register URC data execution function */
  728. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  729. #ifdef AT_USING_SOCKET
  730. sim800c_socket_init(device);
  731. #endif
  732. /* add sim800c device to the netdev list */
  733. device->netdev = sim800c_netdev_add(sim800c->device_name);
  734. if (device->netdev == RT_NULL)
  735. {
  736. LOG_E("get netdev(%s) failed.", sim800c->device_name);
  737. return -RT_ERROR;
  738. }
  739. /* initialize sim800c pin configuration */
  740. if (sim800c->power_pin != -1 && sim800c->power_status_pin != -1)
  741. {
  742. rt_pin_mode(sim800c->power_pin, PIN_MODE_OUTPUT);
  743. rt_pin_mode(sim800c->power_status_pin, PIN_MODE_INPUT);
  744. }
  745. /* initialize sim800c device network */
  746. return sim800c_netdev_set_up(device->netdev);
  747. }
  748. static int sim800c_deinit(struct at_device *device)
  749. {
  750. return sim800c_netdev_set_down(device->netdev);
  751. }
  752. static int sim800c_control(struct at_device *device, int cmd, void *arg)
  753. {
  754. int result = -RT_ERROR;
  755. RT_ASSERT(device);
  756. switch (cmd)
  757. {
  758. case AT_DEVICE_CTRL_POWER_ON:
  759. case AT_DEVICE_CTRL_POWER_OFF:
  760. case AT_DEVICE_CTRL_RESET:
  761. case AT_DEVICE_CTRL_LOW_POWER:
  762. case AT_DEVICE_CTRL_SLEEP:
  763. case AT_DEVICE_CTRL_WAKEUP:
  764. case AT_DEVICE_CTRL_NET_CONN:
  765. case AT_DEVICE_CTRL_NET_DISCONN:
  766. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  767. case AT_DEVICE_CTRL_GET_SIGNAL:
  768. case AT_DEVICE_CTRL_GET_GPS:
  769. case AT_DEVICE_CTRL_GET_VER:
  770. LOG_W("not support the control command(%d).", cmd);
  771. break;
  772. default:
  773. LOG_E("input error control command(%d).", cmd);
  774. break;
  775. }
  776. return result;
  777. }
  778. const struct at_device_ops sim800c_device_ops =
  779. {
  780. sim800c_init,
  781. sim800c_deinit,
  782. sim800c_control,
  783. };
  784. static int sim800c_device_class_register(void)
  785. {
  786. struct at_device_class *class = RT_NULL;
  787. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  788. if (class == RT_NULL)
  789. {
  790. LOG_E("no memory for device class create.");
  791. return -RT_ENOMEM;
  792. }
  793. /* fill sim800c device class object */
  794. #ifdef AT_USING_SOCKET
  795. sim800c_socket_class_register(class);
  796. #endif
  797. class->device_ops = &sim800c_device_ops;
  798. return at_device_class_register(class, AT_DEVICE_CLASS_SIM800C);
  799. }
  800. INIT_DEVICE_EXPORT(sim800c_device_class_register);
  801. #endif /* AT_DEVICE_USING_SIM800C */