at_device_sim800c.c 29 KB

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