at_device_sim76xx.c 24 KB

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