at_device_sim76xx.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * File : at_socket_sim76xx.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-12-22 thomasonegd first version
  23. * 2019-03-06 thomasonegd fix udp connection.
  24. * 2019-03-08 thomasonegd add power_on & power_off api
  25. * 2019-05-14 chenyong multi AT socket client support
  26. * 2019-08-24 chenyong add netdev support
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <at_device_sim76xx.h>
  31. #define LOG_TAG "at.dev.sim76"
  32. #include <at_log.h>
  33. #ifdef AT_DEVICE_USING_SIM76XX
  34. #define SIM76XX_WAIT_CONNECT_TIME 5000
  35. #define SIM76XX_THREAD_STACK_SIZE 2048
  36. #define SIM76XX_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
  37. /* power up sim76xx modem */
  38. static void sim76xx_power_on(struct at_device *device)
  39. {
  40. struct at_device_sim76xx *sim76xx = RT_NULL;
  41. sim76xx = (struct at_device_sim76xx *) device->user_data;
  42. /* not nead to set pin configuration for m26 device power on */
  43. if (sim76xx->power_pin == -1 || sim76xx->power_status_pin == -1)
  44. {
  45. return;
  46. }
  47. if (rt_pin_read(sim76xx->power_status_pin) == PIN_HIGH)
  48. {
  49. return;
  50. }
  51. rt_pin_write(sim76xx->power_pin, PIN_HIGH);
  52. while (rt_pin_read(sim76xx->power_status_pin) == PIN_LOW)
  53. {
  54. rt_thread_mdelay(10);
  55. }
  56. rt_pin_write(sim76xx->power_pin, PIN_LOW);
  57. }
  58. /* power off sim76xx modem */
  59. static void sim76xx_power_off(struct at_device *device)
  60. {
  61. struct at_device_sim76xx *sim76xx = RT_NULL;
  62. sim76xx = (struct at_device_sim76xx *) device->user_data;
  63. /* not nead to set pin configuration for m26 device power on */
  64. if (sim76xx->power_pin == -1 || sim76xx->power_status_pin == -1)
  65. {
  66. return;
  67. }
  68. if (rt_pin_read(sim76xx->power_status_pin) == PIN_LOW)
  69. {
  70. return;
  71. }
  72. rt_pin_write(sim76xx->power_pin, PIN_HIGH);
  73. while (rt_pin_read(sim76xx->power_status_pin) == PIN_HIGH)
  74. {
  75. rt_thread_mdelay(10);
  76. }
  77. rt_pin_write(sim76xx->power_pin, PIN_LOW);
  78. }
  79. /* ============================= sim76xx network interface operations ============================= */
  80. /* set sim76xx network interface device status and address information */
  81. static int sim76xx_netdev_set_info(struct netdev *netdev)
  82. {
  83. #define SIM76XX_IMEI_RESP_SIZE 256
  84. #define SIM76XX_IPADDR_RESP_SIZE 64
  85. #define SIM76XX_DNS_RESP_SIZE 96
  86. #define SIM76XX_INFO_RESP_TIMO rt_tick_from_millisecond(300)
  87. int result = RT_EOK;
  88. ip_addr_t addr;
  89. at_response_t resp = RT_NULL;
  90. struct at_device *device = RT_NULL;
  91. RT_ASSERT(netdev);
  92. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  93. if (device == RT_NULL)
  94. {
  95. LOG_E("get device(%s) failed.", netdev->name);
  96. return -RT_ERROR;
  97. }
  98. /* set network interface device status */
  99. netdev_low_level_set_status(netdev, RT_TRUE);
  100. netdev_low_level_set_link_status(netdev, RT_TRUE);
  101. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  102. resp = at_create_resp(SIM76XX_IMEI_RESP_SIZE, 0, SIM76XX_INFO_RESP_TIMO);
  103. if (resp == RT_NULL)
  104. {
  105. LOG_E("no memory for resp create.");
  106. result = -RT_ENOMEM;
  107. goto __exit;
  108. }
  109. /* set network interface device hardware address(IMEI) */
  110. {
  111. #define SIM76XX_NETDEV_HWADDR_LEN 8
  112. #define SIM76XX_IMEI_LEN 15
  113. char imei[SIM76XX_IMEI_LEN] = {0};
  114. int i = 0, j = 0;
  115. /* send "ATI" commond to get device IMEI */
  116. if (at_obj_exec_cmd(device->client, resp, "ATI") < 0)
  117. {
  118. result = -RT_ERROR;
  119. goto __exit;
  120. }
  121. if (at_resp_parse_line_args_by_kw(resp, "IMEI:", "IMEI: %s", imei) <= 0)
  122. {
  123. LOG_E("%s device prase \"ATI\" cmd error.", device->name);
  124. result = -RT_ERROR;
  125. goto __exit;
  126. }
  127. LOG_D("%s device IMEI number: %s", device->name, imei);
  128. netdev->hwaddr_len = SIM76XX_NETDEV_HWADDR_LEN;
  129. /* get hardware address by IMEI */
  130. for (i = 0, j = 0; i < SIM76XX_NETDEV_HWADDR_LEN && j < SIM76XX_IMEI_LEN; i++, j += 2)
  131. {
  132. if (j != SIM76XX_IMEI_LEN - 1)
  133. {
  134. netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
  135. }
  136. else
  137. {
  138. netdev->hwaddr[i] = (imei[j] - '0');
  139. }
  140. }
  141. }
  142. /* set network interface device IP address */
  143. {
  144. #define IP_ADDR_SIZE_MAX 16
  145. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  146. at_resp_set_info(resp, SIM76XX_IPADDR_RESP_SIZE, 2, SIM76XX_INFO_RESP_TIMO);
  147. /* send "AT+IPADDR" commond to get IP address */
  148. if (at_obj_exec_cmd(device->client, resp, "AT+IPADDR") < 0)
  149. {
  150. result = -RT_ERROR;
  151. goto __exit;
  152. }
  153. if (at_resp_parse_line_args_by_kw(resp, "+IPADDR:", "+IPADDR: %s", ipaddr) <= 0)
  154. {
  155. LOG_E("%s device prase \"AT+IPADDR\" cmd error!", device->name);
  156. result = -RT_ERROR;
  157. goto __exit;
  158. }
  159. LOG_D("%s device IP address: %s", device->name, ipaddr);
  160. /* set network interface address information */
  161. inet_aton(ipaddr, &addr);
  162. netdev_low_level_set_ipaddr(netdev, &addr);
  163. }
  164. /* set network interface device dns server */
  165. {
  166. #define DEF_DNS_ADDR "114.114.114.114"
  167. const char *dns_server = DEF_DNS_ADDR;
  168. ip_addr_t addr;
  169. /* not support get dns server address, using default dns server address */
  170. inet_aton(dns_server, &addr);
  171. netdev_low_level_set_dns_server(netdev, 0, &addr);
  172. }
  173. __exit:
  174. if (resp)
  175. {
  176. at_delete_resp(resp);
  177. }
  178. return result;
  179. }
  180. /* check sim76xx device link_up status */
  181. static void check_link_status_entry(void *parameter)
  182. {
  183. #define SIM76XX_LINK_STATUS_OK 1
  184. #define SIM76XX_LINK_RESP_SIZE 64
  185. #define SIM76XX_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
  186. #define SIM76XX_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
  187. at_response_t resp = RT_NULL;
  188. int result_code, link_status;
  189. struct at_device *device = RT_NULL;
  190. struct netdev *netdev = (struct netdev *)parameter;
  191. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  192. if (device == RT_NULL)
  193. {
  194. LOG_E("get device(%s) failed.", netdev->name);
  195. return;
  196. }
  197. resp = at_create_resp(SIM76XX_LINK_RESP_SIZE, 0, SIM76XX_LINK_RESP_TIMO);
  198. if (resp == RT_NULL)
  199. {
  200. LOG_E("no memory for resp create.");
  201. return;
  202. }
  203. while (1)
  204. {
  205. /* send "AT+CGREG?" commond to check netweork interface device link status */
  206. if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") < 0)
  207. {
  208. rt_thread_mdelay(SIM76XX_LINK_DELAY_TIME);
  209. continue;
  210. }
  211. link_status = -1;
  212. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %d,%d", &result_code, &link_status);
  213. /* check the network interface device link status */
  214. if ((SIM76XX_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
  215. {
  216. netdev_low_level_set_link_status(netdev, (SIM76XX_LINK_STATUS_OK == link_status));
  217. }
  218. rt_thread_mdelay(SIM76XX_LINK_DELAY_TIME);
  219. }
  220. }
  221. static int sim76xx_netdev_check_link_status(struct netdev *netdev)
  222. {
  223. #define SIM76XX_LINK_THREAD_TICK 20
  224. #define SIM76XX_LINK_THREAD_STACK_SIZE (1024 + 512)
  225. #define SIM76XX_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  226. rt_thread_t tid;
  227. char tname[RT_NAME_MAX] = {0};
  228. RT_ASSERT(netdev);
  229. rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
  230. tid = rt_thread_create(tname, check_link_status_entry, (void *)netdev,
  231. SIM76XX_LINK_THREAD_STACK_SIZE, SIM76XX_LINK_THREAD_PRIORITY, SIM76XX_LINK_THREAD_TICK);
  232. if (tid)
  233. {
  234. rt_thread_startup(tid);
  235. }
  236. return RT_EOK;
  237. }
  238. static int sim76xx_net_init(struct at_device *device);
  239. /* sim76xx network interface device set up status */
  240. static int sim76xx_netdev_set_up(struct netdev *netdev)
  241. {
  242. struct at_device *device = RT_NULL;
  243. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  244. if (device == RT_NULL)
  245. {
  246. LOG_E("get device(%s) failed.", netdev->name);
  247. return -RT_ERROR;
  248. }
  249. if (device->is_init == RT_FALSE)
  250. {
  251. sim76xx_net_init(device);
  252. device->is_init = RT_TRUE;
  253. netdev_low_level_set_status(netdev, RT_TRUE);
  254. LOG_D("network intterface device(%s) set up status.", netdev->name);
  255. }
  256. return RT_EOK;
  257. }
  258. /* sim76xx network interface device set down status */
  259. static int sim76xx_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. sim76xx_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. #ifdef NETDEV_USING_PING
  278. /* sim76xx network interface device ping feature */
  279. static int sim76xx_netdev_ping(struct netdev *netdev, const char *host,
  280. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
  281. {
  282. #define SIM76XX_PING_RESP_SIZE 128
  283. #define SIM76XX_PING_IP_SIZE 16
  284. #define SIM76XX_PING_TIMEO (12 * RT_TICK_PER_SECOND)
  285. int result = RT_EOK;
  286. int response, pkg_size, time, ttl;
  287. char ip_addr[SIM76XX_PING_IP_SIZE] = {0};
  288. at_response_t resp = RT_NULL;
  289. struct at_device *device = RT_NULL;
  290. RT_ASSERT(netdev);
  291. RT_ASSERT(host);
  292. RT_ASSERT(ping_resp);
  293. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  294. if (device == RT_NULL)
  295. {
  296. LOG_E("get device(%s) failed.", netdev->name);
  297. return -RT_ERROR;
  298. }
  299. resp = at_create_resp(SIM76XX_PING_RESP_SIZE, 6, SIM76XX_PING_TIMEO);
  300. if (resp == RT_NULL)
  301. {
  302. LOG_E("no memory for resp create.");
  303. result = -RT_ERROR;
  304. goto __exit;
  305. }
  306. /* send "AT+CIPPING=<dest_addr>,<dest_addr_type>[,<num_pings>[,<package_size>[,<interval_time>[,<wait_timer>[,<TTL>]]]]]"
  307. commond to send ping request */
  308. if (at_obj_exec_cmd(device->client, resp, "AT+CPING=\"%s\",1,1,%d,,,64", host, data_len) < 0)
  309. {
  310. result = -RT_ERROR;
  311. goto __exit;
  312. }
  313. if (at_resp_parse_line_args_by_kw(resp, "+CPING:", "+CPING:%d,%[^,],%d,%d,%d",
  314. &response, ip_addr, &pkg_size, &time, &ttl) <= 0)
  315. {
  316. result = -RT_ERROR;
  317. goto __exit;
  318. }
  319. /* 2 - ping timeout */
  320. if (response == 2)
  321. {
  322. result = -RT_ETIMEOUT;
  323. goto __exit;
  324. }
  325. inet_aton(ip_addr, &(ping_resp->ip_addr));
  326. ping_resp->data_len = pkg_size;
  327. /* reply time, in units of ms */
  328. ping_resp->ticks = rt_tick_from_millisecond(time);
  329. ping_resp->ttl = ttl;
  330. __exit:
  331. if (resp)
  332. {
  333. at_delete_resp(resp);
  334. }
  335. return result;
  336. }
  337. #endif /* NETDEV_USING_PING */
  338. /* sim76xx network interface device operations */
  339. const struct netdev_ops sim76xx_netdev_ops =
  340. {
  341. sim76xx_netdev_set_up,
  342. sim76xx_netdev_set_down,
  343. RT_NULL, /* not support set ip, netmask, gatway address */
  344. RT_NULL, /* not support set DNS server address */
  345. RT_NULL, /* not support set DHCP status */
  346. #ifdef NETDEV_USING_PING
  347. sim76xx_netdev_ping,
  348. #endif
  349. RT_NULL, /* not support netstat feature */
  350. };
  351. /* register sim76xx network interface */
  352. static struct netdev *sim76xx_netdev_add(const char *netdev_name)
  353. {
  354. #define ETHERNET_MTU 1500
  355. #define HWADDR_LEN 8
  356. struct netdev *netdev = RT_NULL;
  357. RT_ASSERT(netdev_name);
  358. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  359. if (netdev == RT_NULL)
  360. {
  361. LOG_E("no memory for netdev create.");
  362. return RT_NULL;
  363. }
  364. netdev->mtu = ETHERNET_MTU;
  365. netdev->hwaddr_len = HWADDR_LEN;
  366. netdev->ops = &sim76xx_netdev_ops;
  367. #ifdef SAL_USING_AT
  368. extern int sal_at_netdev_set_pf_info(struct netdev * netdev);
  369. /* set the network interface socket/netdb operations */
  370. sal_at_netdev_set_pf_info(netdev);
  371. #endif
  372. netdev_register(netdev, netdev_name, RT_NULL);
  373. return netdev;
  374. }
  375. /* ============================= sim76xx device operations ============================= */
  376. #define AT_SEND_CMD(client, resp, cmd) \
  377. do { \
  378. (resp) = at_resp_set_info((resp), 256, 0, 5 * RT_TICK_PER_SECOND); \
  379. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  380. { \
  381. result = -RT_ERROR; \
  382. goto __exit; \
  383. } \
  384. } while(0) \
  385. /* initialize the sim76xx device network connection by command */
  386. static void sim76xx_init_thread_entry(void *parameter)
  387. {
  388. #define INIT_RETRY 5
  389. #define CPIN_RETRY 5
  390. #define CSQ_RETRY 10
  391. #define CREG_RETRY 10
  392. #define CGREG_RETRY 10
  393. #define CGATT_RETRY 10
  394. #define CCLK_RETRY 10
  395. at_response_t resp = RT_NULL;
  396. rt_err_t result = RT_EOK;
  397. rt_size_t i, qi_arg[3] = {0};
  398. int retry_num = INIT_RETRY;
  399. char parsed_data[20] = {0};
  400. struct at_device *device = (struct at_device *)parameter;
  401. struct at_client *client = device->client;
  402. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  403. if (resp == RT_NULL)
  404. {
  405. LOG_E("no memory for resp create.");
  406. return;
  407. }
  408. LOG_D("start init %s device.", device->name);
  409. while (retry_num--)
  410. {
  411. /* power-up sim76xx */
  412. sim76xx_power_on(device);
  413. /* wait SIM76XX startup finish, Send AT every 5s, if receive OK, SYNC success*/
  414. if (at_client_wait_connect(SIM76XX_WAIT_CONNECT_TIME))
  415. {
  416. result = -RT_ETIMEOUT;
  417. goto __exit;
  418. }
  419. /* disable echo */
  420. AT_SEND_CMD(client, resp, "ATE0");
  421. /* get module version */
  422. AT_SEND_CMD(client, resp, "ATI");
  423. /* show module version */
  424. for (i = 0; i < (int)resp->line_counts - 1; i++)
  425. {
  426. LOG_D("%s", at_resp_get_line(resp, i + 1));
  427. }
  428. /* check SIM card */
  429. rt_thread_mdelay(1000);
  430. for (i = 0; i < CPIN_RETRY; i++)
  431. {
  432. at_obj_exec_cmd(client, resp, "AT+CPIN?");
  433. if (at_resp_get_line_by_kw(resp, "READY"))
  434. {
  435. LOG_D("%s device SIM card detection success.", device->name);
  436. break;
  437. }
  438. LOG_I("\"AT+CPIN\" commands send retry...");
  439. rt_thread_mdelay(1000);
  440. }
  441. if (i == CPIN_RETRY)
  442. {
  443. result = -RT_ERROR;
  444. goto __exit;
  445. }
  446. /* waiting for dirty data to be digested */
  447. rt_thread_mdelay(10);
  448. /* check signal strength */
  449. for (i = 0; i < CSQ_RETRY; i++)
  450. {
  451. AT_SEND_CMD(client, resp, "AT+CSQ");
  452. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &qi_arg[0], &qi_arg[1]);
  453. if (qi_arg[0] != 99)
  454. {
  455. LOG_D("%s device signal strength: %d Channel bit error rate: %d", device->name, qi_arg[0], qi_arg[1]);
  456. break;
  457. }
  458. rt_thread_mdelay(1000);
  459. }
  460. if (i == CSQ_RETRY)
  461. {
  462. LOG_E("%s device signal strength check failed (%s).", device->name, parsed_data);
  463. result = -RT_ERROR;
  464. goto __exit;
  465. }
  466. /* do not show the prompt when receiving data */
  467. AT_SEND_CMD(client, resp, "AT+CIPSRIP=0");
  468. /* check the GSM network is registered */
  469. for (i = 0; i < CREG_RETRY; i++)
  470. {
  471. AT_SEND_CMD(client, resp, "AT+CREG?");
  472. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  473. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) || !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  474. {
  475. LOG_D("%s device GSM is registered(%s).", device->name, parsed_data);
  476. break;
  477. }
  478. rt_thread_mdelay(1000);
  479. }
  480. if (i == CREG_RETRY)
  481. {
  482. LOG_E("%s device GSM is register failed(%s).", device->name, parsed_data);
  483. result = -RT_ERROR;
  484. goto __exit;
  485. }
  486. /* check the GPRS network is registered */
  487. for (i = 0; i < CGREG_RETRY; i++)
  488. {
  489. AT_SEND_CMD(client, resp, "AT+CGREG?");
  490. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %s", &parsed_data);
  491. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) || !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  492. {
  493. LOG_D("%s device GPRS is registered(%s).", device->name, parsed_data);
  494. break;
  495. }
  496. rt_thread_mdelay(1000);
  497. }
  498. if (i == CGREG_RETRY)
  499. {
  500. LOG_E("%s device GPRS is register failed(%s).", device->name, parsed_data);
  501. result = -RT_ERROR;
  502. goto __exit;
  503. }
  504. /* check packet domain attach or detach */
  505. for (i = 0; i < CGATT_RETRY; i++)
  506. {
  507. AT_SEND_CMD(client, resp, "AT+CGATT?");
  508. at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT: %s", &parsed_data);
  509. if (!strncmp(parsed_data, "1", 1))
  510. {
  511. LOG_D("%s device Packet domain attach.", device->name);
  512. break;
  513. }
  514. rt_thread_mdelay(1000);
  515. }
  516. if (i == CGATT_RETRY)
  517. {
  518. LOG_E("%s device GPRS attach failed.", device->name);
  519. result = -RT_ERROR;
  520. goto __exit;
  521. }
  522. /* configure context */
  523. AT_SEND_CMD(client, resp, "AT+CGDCONT=1,\"IP\",\"CMNET\"");
  524. /* activate context */
  525. {
  526. int net_status = 0;
  527. AT_SEND_CMD(client, resp, "AT+NETOPEN?");
  528. at_resp_parse_line_args_by_kw(resp, "+NETOPEN:", "+NETOPEN: %d", &net_status);
  529. /* 0 - netwoek close, 1 - network open */
  530. if (net_status == 0)
  531. {
  532. AT_SEND_CMD(client, resp, "AT+NETOPEN");
  533. }
  534. }
  535. /* set active PDP context's profile number */
  536. AT_SEND_CMD(client, resp, "AT+CSOCKSETPN=1");
  537. #ifdef RT_USING_RTC
  538. /* get real time */
  539. int year, month, day, hour, min, sec;
  540. for (i = 0; i < CCLK_RETRY; i++)
  541. {
  542. if (at_obj_exec_cmd(device->client, at_resp_set_info(resp, 256, 0, 5 * RT_TICK_PER_SECOND), "AT+CCLK?") < 0)
  543. {
  544. rt_thread_mdelay(500);
  545. continue;
  546. }
  547. /* +CCLK: "18/12/22,18:33:12+32" */
  548. if (at_resp_parse_line_args_by_kw(resp, "+CCLK:", "+CCLK: \"%d/%d/%d,%d:%d:%d",
  549. &year, &month, &day, &hour, &min, &sec) < 0)
  550. {
  551. rt_thread_mdelay(500);
  552. continue;
  553. }
  554. set_date(year + 2000, month, day);
  555. set_time(hour, min, sec);
  556. break;
  557. }
  558. if (i == CCLK_RETRY)
  559. {
  560. LOG_E("%s device GPRS attach failed.", device->name);
  561. result = -RT_ERROR;
  562. goto __exit;
  563. }
  564. #endif /* RT_USING_RTC */
  565. /* initialize successfully */
  566. result = RT_EOK;
  567. break;
  568. __exit:
  569. if (result != RT_EOK)
  570. {
  571. /* power off the sim76xx device */
  572. sim76xx_power_off(device);
  573. rt_thread_mdelay(1000);
  574. LOG_I("%s device initialize retry...", device->name);
  575. }
  576. }
  577. if (resp)
  578. {
  579. at_delete_resp(resp);
  580. }
  581. if (result == RT_EOK)
  582. {
  583. /* set network interface device status and address information */
  584. sim76xx_netdev_set_info(device->netdev);
  585. /* check and create link staus sync thread */
  586. if (rt_thread_find(device->netdev->name) == RT_NULL)
  587. {
  588. sim76xx_netdev_check_link_status(device->netdev);
  589. }
  590. LOG_I("%s device network initialize success!", device->name);
  591. }
  592. else
  593. {
  594. LOG_E("%s device network initialize failed(%d)!", device->name, result);
  595. }
  596. }
  597. int sim76xx_net_init(struct at_device *device)
  598. {
  599. #ifdef AT_DEVICE_SIM76XX_INIT_ASYN
  600. rt_thread_t tid;
  601. tid = rt_thread_create("sim76_net", sim76xx_init_thread_entry, (void *)device,
  602. SIM76XX_THREAD_STACK_SIZE, SIM76XX_THREAD_PRIORITY, 20);
  603. if (tid)
  604. {
  605. rt_thread_startup(tid);
  606. }
  607. else
  608. {
  609. LOG_E("create %s device init thread failed.", device->name);
  610. return -RT_ERROR;
  611. }
  612. #else
  613. sim76xx_init_thread_entry(device);
  614. #endif /* AT_DEVICE_SIM76XX_INIT_ASYN */
  615. return RT_EOK;
  616. }
  617. #ifdef FINSH_USING_MSH
  618. #include <finsh.h>
  619. MSH_CMD_EXPORT_ALIAS(sim76xx_net_init, at_net_init, initialize AT network);
  620. #endif
  621. /* initialize the sim76xx device network connection and register network interface device */
  622. static int sim76xx_init(struct at_device *device)
  623. {
  624. struct at_device_sim76xx *sim76xx = (struct at_device_sim76xx *) device->user_data;
  625. /* initialize AT client */
  626. at_client_init(sim76xx->client_name, sim76xx->recv_line_num);
  627. device->client = at_client_get(sim76xx->client_name);
  628. if (device->client == RT_NULL)
  629. {
  630. LOG_E("get AT client(%s) failed.", sim76xx->client_name);
  631. return -RT_ERROR;
  632. }
  633. #ifdef AT_USING_SOCKET
  634. sim76xx_socket_init(device);
  635. #endif
  636. /* add sim76xx device to the netdev list */
  637. device->netdev = sim76xx_netdev_add(sim76xx->device_name);
  638. if (device->netdev == RT_NULL)
  639. {
  640. LOG_E("get netdev(%s) failed.", sim76xx->device_name);
  641. return -RT_ERROR;
  642. }
  643. /* initialize sim76xx pin configuration */
  644. if (sim76xx->power_pin != -1 && sim76xx->power_status_pin != -1)
  645. {
  646. rt_pin_mode(sim76xx->power_pin, PIN_MODE_OUTPUT);
  647. rt_pin_mode(sim76xx->power_status_pin, PIN_MODE_INPUT);
  648. }
  649. /* initialize sim76xx device network */
  650. return sim76xx_netdev_set_up(device->netdev);
  651. }
  652. /* deinit sim76xx device network connect */
  653. static int sim76xx_deinit(struct at_device *device)
  654. {
  655. return sim76xx_netdev_set_down(device->netdev);
  656. }
  657. /* custom device control operations */
  658. static int sim76xx_control(struct at_device *device, int cmd, void *arg)
  659. {
  660. int result = -RT_ERROR;
  661. RT_ASSERT(device);
  662. switch (cmd)
  663. {
  664. case AT_DEVICE_CTRL_POWER_ON:
  665. case AT_DEVICE_CTRL_POWER_OFF:
  666. case AT_DEVICE_CTRL_RESET:
  667. case AT_DEVICE_CTRL_LOW_POWER:
  668. case AT_DEVICE_CTRL_SLEEP:
  669. case AT_DEVICE_CTRL_WAKEUP:
  670. case AT_DEVICE_CTRL_NET_CONN:
  671. case AT_DEVICE_CTRL_NET_DISCONN:
  672. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  673. case AT_DEVICE_CTRL_GET_SIGNAL:
  674. case AT_DEVICE_CTRL_GET_GPS:
  675. case AT_DEVICE_CTRL_GET_VER:
  676. LOG_W("not support the control command(%d).", cmd);
  677. break;
  678. default:
  679. LOG_E("input error control command(%d).", cmd);
  680. break;
  681. }
  682. return result;
  683. }
  684. const struct at_device_ops sim76xx_device_ops =
  685. {
  686. sim76xx_init,
  687. sim76xx_deinit,
  688. sim76xx_control,
  689. };
  690. /* register sim76xx class to the global at_device class list */
  691. static int sim76xx_device_class_register(void)
  692. {
  693. struct at_device_class *class = RT_NULL;
  694. class = (struct at_device_class *)rt_calloc(1, sizeof(struct at_device_class));
  695. if (class == RT_NULL)
  696. {
  697. LOG_E("no memory for device class create.");
  698. return -RT_ENOMEM;
  699. }
  700. /* fill sim76xx device class object */
  701. #ifdef AT_USING_SOCKET
  702. sim76xx_socket_class_register(class);
  703. #endif
  704. class->device_ops = &sim76xx_device_ops;
  705. return at_device_class_register(class, AT_DEVICE_CLASS_SIM76XX);
  706. }
  707. INIT_DEVICE_EXPORT(sim76xx_device_class_register);
  708. #endif /* AT_DEVICE_SIM76XX */