at_device_m26.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * File : at_device_m26.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 chenyong first version
  23. * 2019-05-12 chenyong multi AT socket client support
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <at_device_m26.h>
  28. #define LOG_TAG "at.dev.m26"
  29. #include <at_log.h>
  30. #ifdef AT_DEVICE_USING_M26
  31. #define M26_WAIT_CONNECT_TIME 5000
  32. #define M26_THREAD_STACK_SIZE 2048
  33. #define M26_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  34. static void m26_power_on(struct at_device *device)
  35. {
  36. struct at_device_m26 *m26 = RT_NULL;
  37. m26 = (struct at_device_m26 *) device->user_data;
  38. /* not nead to set pin configuration for m26 device power on */
  39. if (m26->power_pin == -1 || m26->power_status_pin == -1)
  40. {
  41. return;
  42. }
  43. if (rt_pin_read(m26->power_status_pin) == PIN_HIGH)
  44. {
  45. return;
  46. }
  47. rt_pin_write(m26->power_pin, PIN_HIGH);
  48. while (rt_pin_read(m26->power_status_pin) == PIN_LOW)
  49. {
  50. rt_thread_mdelay(10);
  51. }
  52. rt_pin_write(m26->power_pin, PIN_LOW);
  53. }
  54. static void m26_power_off(struct at_device *device)
  55. {
  56. struct at_device_m26 *m26 = RT_NULL;
  57. m26 = (struct at_device_m26 *) device->user_data;
  58. /* not nead to set pin configuration for m26 device power on */
  59. if (m26->power_pin == -1 || m26->power_status_pin == -1)
  60. {
  61. return;
  62. }
  63. if (rt_pin_read(m26->power_status_pin) == PIN_LOW)
  64. {
  65. return;
  66. }
  67. rt_pin_write(m26->power_pin, PIN_HIGH);
  68. while (rt_pin_read(m26->power_status_pin) == PIN_HIGH)
  69. {
  70. rt_thread_mdelay(10);
  71. }
  72. rt_pin_write(m26->power_pin, PIN_LOW);
  73. }
  74. /* ============================= m26 network interface operations ============================= */
  75. /* set m26 network interface device status and address information */
  76. static int m26_netdev_set_info(struct netdev *netdev)
  77. {
  78. #define M26_IMEI_RESP_SIZE 32
  79. #define M26_IPADDR_RESP_SIZE 32
  80. #define M26_DNS_RESP_SIZE 96
  81. #define M26_INFO_RESP_TIMO rt_tick_from_millisecond(300)
  82. int result = RT_EOK;
  83. ip_addr_t addr;
  84. at_response_t resp = RT_NULL;
  85. struct at_device *device = RT_NULL;
  86. struct at_client *client = RT_NULL;
  87. RT_ASSERT(netdev);
  88. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  89. if (device == RT_NULL)
  90. {
  91. LOG_E("get device(%s) failed.", netdev->name);
  92. return -RT_ERROR;
  93. }
  94. client = device->client;
  95. /* set network interface device up status */
  96. netdev_low_level_set_status(netdev, RT_TRUE);
  97. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  98. resp = at_create_resp(M26_IMEI_RESP_SIZE, 0, M26_INFO_RESP_TIMO);
  99. if (resp == RT_NULL)
  100. {
  101. LOG_E("no memory for resp create.");
  102. result = -RT_ENOMEM;
  103. goto __exit;
  104. }
  105. /* set network interface device hardware address(IMEI) */
  106. {
  107. #define M26_NETDEV_HWADDR_LEN 8
  108. #define M26_IMEI_LEN 15
  109. char imei[M26_IMEI_LEN] = {0};
  110. int i = 0, j = 0;
  111. /* send "AT+GSN" commond to get device IMEI */
  112. if (at_obj_exec_cmd(client, resp, "AT+GSN") < 0)
  113. {
  114. result = -RT_ERROR;
  115. goto __exit;
  116. }
  117. if (at_resp_parse_line_args(resp, 2, "%s", imei) <= 0)
  118. {
  119. LOG_E("%s device prase \"AT+GSN\" cmd error.", device->name);
  120. result = -RT_ERROR;
  121. goto __exit;
  122. }
  123. LOG_D("%s device IMEI number: %s", device->name, imei);
  124. netdev->hwaddr_len = M26_NETDEV_HWADDR_LEN;
  125. /* get hardware address by IMEI */
  126. for (i = 0, j = 0; i < M26_NETDEV_HWADDR_LEN && j < M26_IMEI_LEN; i++, j+=2)
  127. {
  128. if (j != M26_IMEI_LEN - 1)
  129. {
  130. netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
  131. }
  132. else
  133. {
  134. netdev->hwaddr[i] = (imei[j] - '0');
  135. }
  136. }
  137. }
  138. /* set network interface device IP address */
  139. {
  140. #define IP_ADDR_SIZE_MAX 16
  141. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  142. at_resp_set_info(resp, M26_IPADDR_RESP_SIZE, 2, M26_INFO_RESP_TIMO);
  143. /* send "AT+QILOCIP" commond to get IP address */
  144. if (at_obj_exec_cmd(client, resp, "AT+QILOCIP") < 0)
  145. {
  146. result = -RT_ERROR;
  147. goto __exit;
  148. }
  149. if (at_resp_parse_line_args_by_kw(resp, ".", "%s", ipaddr) <= 0)
  150. {
  151. LOG_E("%s device prase \"AT+QILOCIP\" cmd error.", device->name);
  152. result = -RT_ERROR;
  153. goto __exit;
  154. }
  155. LOG_D("%s device IP address: %s", device->name, ipaddr);
  156. /* set network interface address information */
  157. inet_aton(ipaddr, &addr);
  158. netdev_low_level_set_ipaddr(netdev, &addr);
  159. }
  160. /* set network interface device dns server */
  161. {
  162. #define DNS_ADDR_SIZE_MAX 16
  163. char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
  164. at_resp_set_info(resp, M26_DNS_RESP_SIZE, 0, M26_INFO_RESP_TIMO);
  165. /* send "AT+QIDNSCFG?" commond to get DNS servers address */
  166. if (at_obj_exec_cmd(client, resp, "AT+QIDNSCFG?") < 0)
  167. {
  168. result = -RT_ERROR;
  169. goto __exit;
  170. }
  171. if (at_resp_parse_line_args_by_kw(resp, "PrimaryDns:", "PrimaryDns:%s", dns_server1) <= 0 ||
  172. at_resp_parse_line_args_by_kw(resp, "SecondaryDns:", "SecondaryDns:%s", dns_server2) <= 0)
  173. {
  174. LOG_E("%s device prase \"AT+QIDNSCFG?\" cmd error.", device->name);
  175. result = -RT_ERROR;
  176. goto __exit;
  177. }
  178. LOG_D("%s device primary DNS server address: %s", device->name, dns_server1);
  179. LOG_D("%s device secondary DNS server address: %s", device->name, dns_server2);
  180. inet_aton(dns_server1, &addr);
  181. netdev_low_level_set_dns_server(netdev, 0, &addr);
  182. inet_aton(dns_server2, &addr);
  183. netdev_low_level_set_dns_server(netdev, 1, &addr);
  184. }
  185. __exit:
  186. if (resp)
  187. {
  188. at_delete_resp(resp);
  189. }
  190. return result;
  191. }
  192. static void check_link_status_entry(void *parameter)
  193. {
  194. #define M26_LINK_STATUS_OK 0
  195. #define M26_LINK_RESP_SIZE 64
  196. #define M26_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
  197. #define M26_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
  198. struct netdev *netdev = (struct netdev *)parameter;
  199. struct at_device *device = RT_NULL;
  200. at_response_t resp = RT_NULL;
  201. int link_status;
  202. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  203. if (device == RT_NULL)
  204. {
  205. LOG_E("get device(%s) failed.", netdev->name);
  206. return;
  207. }
  208. resp = at_create_resp(M26_LINK_RESP_SIZE, 0, M26_LINK_RESP_TIMO);
  209. if (resp == RT_NULL)
  210. {
  211. LOG_E("no memory for resp create.");
  212. return;
  213. }
  214. while (1)
  215. {
  216. /* send "AT+QNSTATUS" commond to check netweork interface device link status */
  217. if (at_obj_exec_cmd(device->client, resp, "AT+QNSTATUS") < 0)
  218. {
  219. rt_thread_mdelay(M26_LINK_DELAY_TIME);
  220. continue;
  221. }
  222. link_status = -1;
  223. at_resp_parse_line_args_by_kw(resp, "+QNSTATUS:", "+QNSTATUS: %d", &link_status);
  224. /* check the network interface device link status */
  225. if ((M26_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
  226. {
  227. netdev_low_level_set_link_status(netdev, (M26_LINK_STATUS_OK == link_status));
  228. }
  229. rt_thread_mdelay(M26_LINK_DELAY_TIME);
  230. }
  231. }
  232. static int m26_netdev_check_link_status(struct netdev *netdev)
  233. {
  234. #define M26_LINK_THREAD_TICK 20
  235. #define M26_LINK_THREAD_STACK_SIZE (1024 + 512)
  236. #define M26_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  237. rt_thread_t tid;
  238. char tname[RT_NAME_MAX] = {0};
  239. RT_ASSERT(netdev);
  240. rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
  241. tid = rt_thread_create(tname, check_link_status_entry, (void *)netdev,
  242. M26_LINK_THREAD_STACK_SIZE, M26_LINK_THREAD_PRIORITY, M26_LINK_THREAD_TICK);
  243. if (tid)
  244. {
  245. rt_thread_startup(tid);
  246. }
  247. return RT_EOK;
  248. }
  249. static int m26_net_init(struct at_device *device);
  250. static int m26_netdev_set_up(struct netdev *netdev)
  251. {
  252. struct at_device *device = RT_NULL;
  253. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  254. if (device == RT_NULL)
  255. {
  256. LOG_E("get device(%s) failed.", netdev->name);
  257. return -RT_ERROR;
  258. }
  259. if (device->is_init == RT_FALSE)
  260. {
  261. m26_net_init(device);
  262. device->is_init = RT_TRUE;
  263. netdev_low_level_set_status(netdev, RT_TRUE);
  264. LOG_D("network interface device(%s) set up status.", netdev->name);
  265. }
  266. return RT_EOK;
  267. }
  268. static int m26_netdev_set_down(struct netdev *netdev)
  269. {
  270. struct at_device *device = RT_NULL;
  271. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  272. if (device == RT_NULL)
  273. {
  274. LOG_E("get device(%s) failed.", netdev->name);
  275. return -RT_ERROR;
  276. }
  277. if (device->is_init == RT_TRUE)
  278. {
  279. m26_power_off(device);
  280. device->is_init = RT_FALSE;
  281. netdev_low_level_set_status(netdev, RT_FALSE);
  282. LOG_D("network interface device(%s) set down status.", netdev->name);
  283. }
  284. return RT_EOK;
  285. }
  286. static int m26_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  287. {
  288. #define M26_DNS_RESP_LEN 8
  289. #define M26_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
  290. int result = RT_EOK;
  291. at_response_t resp = RT_NULL;
  292. struct at_device *device = RT_NULL;
  293. RT_ASSERT(netdev);
  294. RT_ASSERT(dns_server);
  295. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  296. if (device == RT_NULL)
  297. {
  298. LOG_E("get device(%s) failed.", netdev->name);
  299. return - RT_ERROR;
  300. }
  301. resp = at_create_resp(M26_DNS_RESP_LEN, 0, M26_DNS_RESP_TIMEO);
  302. if (resp == RT_NULL)
  303. {
  304. LOG_E("no memory for resp create.");
  305. return -RT_ENOMEM;
  306. }
  307. /* send "AT+QIDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
  308. if (at_exec_cmd(resp, "AT+QIDNSCFG=\"%s\"", inet_ntoa(*dns_server)) < 0)
  309. {
  310. result = -RT_ERROR;
  311. goto __exit;
  312. }
  313. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  314. __exit:
  315. if (resp)
  316. {
  317. at_delete_resp(resp);
  318. }
  319. return result;
  320. }
  321. #ifdef NETDEV_USING_PING
  322. static int m26_netdev_ping(struct netdev *netdev, const char *host,
  323. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
  324. {
  325. #define M26_PING_RESP_SIZE 128
  326. #define M26_PING_IP_SIZE 16
  327. #define M26_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  328. int result = RT_EOK;
  329. at_response_t resp = RT_NULL;
  330. char ip_addr[M26_PING_IP_SIZE] = {0};
  331. int response, recv_data_len, time, ttl;
  332. struct at_device *device = RT_NULL;
  333. RT_ASSERT(netdev);
  334. RT_ASSERT(host);
  335. RT_ASSERT(ping_resp);
  336. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  337. if (device == RT_NULL)
  338. {
  339. LOG_E("get device(%s) failed.", netdev->name);
  340. return - RT_ERROR;
  341. }
  342. resp = at_create_resp(M26_PING_RESP_SIZE, 5, M26_PING_TIMEO);
  343. if (resp == RT_NULL)
  344. {
  345. LOG_E("no memory for resp create.");
  346. return -RT_ENOMEM;
  347. }
  348. /* send "AT+QPING="<host>"[,[<timeout>][,<pingnum>]]" commond to send ping request */
  349. if (at_obj_exec_cmd(device->client, resp, "AT+QPING=\"%s\",%d,1", host, M26_PING_TIMEO / RT_TICK_PER_SECOND) < 0)
  350. {
  351. result = -RT_ERROR;
  352. goto __exit;
  353. }
  354. at_resp_parse_line_args_by_kw(resp, "+QPING:","+QPING:%d", &response);
  355. /* Received the ping response from the server */
  356. if (response == 0)
  357. {
  358. if (at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d,%[^,],%d,%d,%d",
  359. &response, ip_addr, &recv_data_len, &time, &ttl) <= 0)
  360. {
  361. result = -RT_ERROR;
  362. goto __exit;
  363. }
  364. }
  365. /* prase response number */
  366. switch (response)
  367. {
  368. case 0:
  369. inet_aton(ip_addr, &(ping_resp->ip_addr));
  370. ping_resp->data_len = recv_data_len;
  371. ping_resp->ticks = time;
  372. ping_resp->ttl = ttl;
  373. result = RT_EOK;
  374. break;
  375. case 1:
  376. result = -RT_ETIMEOUT;
  377. break;
  378. default:
  379. result = -RT_ERROR;
  380. break;
  381. }
  382. __exit:
  383. if (resp)
  384. {
  385. at_delete_resp(resp);
  386. }
  387. return result;
  388. }
  389. #endif /* NETDEV_USING_PING */
  390. const struct netdev_ops m26_netdev_ops =
  391. {
  392. m26_netdev_set_up,
  393. m26_netdev_set_down,
  394. RT_NULL, /* not support set ip, netmask, gatway address */
  395. m26_netdev_set_dns_server,
  396. RT_NULL, /* not support set DHCP status */
  397. #ifdef NETDEV_USING_PING
  398. m26_netdev_ping,
  399. #endif
  400. RT_NULL,
  401. };
  402. static struct netdev *m26_netdev_add(const char *netdev_name)
  403. {
  404. #define M26_NETDEV_MTU 1500
  405. struct netdev *netdev = RT_NULL;
  406. RT_ASSERT(netdev_name);
  407. netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
  408. if (netdev == RT_NULL)
  409. {
  410. LOG_E("no memory for netdev ceate.");
  411. return RT_NULL;
  412. }
  413. netdev->mtu = M26_NETDEV_MTU;
  414. netdev->ops = &m26_netdev_ops;
  415. #ifdef SAL_USING_AT
  416. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  417. /* set the network interface socket/netdb operations */
  418. sal_at_netdev_set_pf_info(netdev);
  419. #endif
  420. netdev_register(netdev, netdev_name, RT_NULL);
  421. return netdev;
  422. }
  423. /* ============================= m26 device operations ============================= */
  424. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  425. do { \
  426. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  427. if (at_obj_exec_cmd((client),(resp), (cmd)) < 0) \
  428. { \
  429. result = -RT_ERROR; \
  430. goto __exit; \
  431. } \
  432. } while(0); \
  433. /* init for m26 or mc20 */
  434. static void m26_init_thread_entry(void *parameter)
  435. {
  436. #define INIT_RETRY 5
  437. #define CPIN_RETRY 10
  438. #define CSQ_RETRY 10
  439. #define CREG_RETRY 10
  440. #define CGREG_RETRY 20
  441. at_response_t resp = RT_NULL;
  442. int i, qimux, qimode;
  443. int retry_num = INIT_RETRY;
  444. char parsed_data[10];
  445. rt_err_t result = RT_EOK;
  446. struct at_device *device = (struct at_device *)parameter;
  447. struct at_client *client = device->client;
  448. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  449. if (resp == RT_NULL)
  450. {
  451. LOG_E("no memory for resp create.");
  452. return;
  453. }
  454. LOG_D("start init m26/mc20 device(%s)", device->name);
  455. while (retry_num--)
  456. {
  457. /* power on the m26 device */
  458. m26_power_on(device);
  459. rt_thread_mdelay(1000);
  460. /* wait m26|mc20 startup finish */
  461. if (at_client_obj_wait_connect(client, M26_WAIT_CONNECT_TIME))
  462. {
  463. result = -RT_ETIMEOUT;
  464. goto __exit;
  465. }
  466. /* disable echo */
  467. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  468. /* get module version */
  469. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  470. /* show module version */
  471. for (i = 0; i < (int) resp->line_counts - 1; i++)
  472. {
  473. LOG_D("%s", at_resp_get_line(resp, i + 1));
  474. }
  475. /* check SIM card */
  476. for (i = 0; i < CPIN_RETRY; i++)
  477. {
  478. AT_SEND_CMD(client, resp, 2, 5 * RT_TICK_PER_SECOND, "AT+CPIN?");
  479. if (at_resp_get_line_by_kw(resp, "READY"))
  480. {
  481. LOG_D("%s device SIM card detection success.", device->name);
  482. break;
  483. }
  484. rt_thread_mdelay(1000);
  485. }
  486. if (i == CPIN_RETRY)
  487. {
  488. LOG_E("SIM card detection failed!");
  489. result = -RT_ERROR;
  490. goto __exit;
  491. }
  492. /* waiting for dirty data to be digested */
  493. rt_thread_mdelay(10);
  494. /* check signal strength */
  495. for (i = 0; i < CSQ_RETRY; i++)
  496. {
  497. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  498. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  499. if (rt_strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  500. {
  501. LOG_D("%s device signal strength: %s", device->name, parsed_data);
  502. break;
  503. }
  504. rt_thread_mdelay(1000);
  505. }
  506. if (i == CSQ_RETRY)
  507. {
  508. LOG_E("%s device signal strength check failed(%s).", device->name, parsed_data);
  509. result = -RT_ERROR;
  510. goto __exit;
  511. }
  512. /* check the GSM network is registered */
  513. for (i = 0; i < CREG_RETRY; i++)
  514. {
  515. AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
  516. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  517. if (!rt_strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  518. !rt_strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  519. {
  520. LOG_D("%s device GSM is registered(%s).", device->name, parsed_data);
  521. break;
  522. }
  523. rt_thread_mdelay(1000);
  524. }
  525. if (i == CREG_RETRY)
  526. {
  527. LOG_E("%s device GSM is register failed(%s)", device->name, parsed_data);
  528. result = -RT_ERROR;
  529. goto __exit;
  530. }
  531. /* check the GPRS network is registered */
  532. for (i = 0; i < CGREG_RETRY; i++)
  533. {
  534. AT_SEND_CMD(client, resp, 0, 300, "AT+CGREG?");
  535. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %s", &parsed_data);
  536. if (!rt_strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  537. !rt_strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  538. {
  539. LOG_D("%s device GPRS is registered(%s).", device->name, parsed_data);
  540. break;
  541. }
  542. rt_thread_mdelay(1000);
  543. }
  544. if (i == CGREG_RETRY)
  545. {
  546. LOG_E("%s device GPRS is register failed(%s).", device->name, parsed_data);
  547. result = -RT_ERROR;
  548. goto __exit;
  549. }
  550. AT_SEND_CMD(client, resp, 0, 300, "AT+QIFGCNT=0");
  551. AT_SEND_CMD(client, resp, 0, 300, "AT+QICSGP=1, \"CMNET\"");
  552. AT_SEND_CMD(client, resp, 0, 300, "AT+QIMODE?");
  553. at_resp_parse_line_args_by_kw(resp, "+QIMODE:", "+QIMODE: %d", &qimode);
  554. if (qimode == 1)
  555. {
  556. AT_SEND_CMD(client, resp, 0, 300, "AT+QIMODE=0");
  557. }
  558. /* the device default response timeout is 40 seconds, but it set to 15 seconds is convenient to use. */
  559. AT_SEND_CMD(client, resp, 2, 20 * 1000, "AT+QIDEACT");
  560. /* Set to multiple connections */
  561. AT_SEND_CMD(client, resp, 0, 300, "AT+QIMUX?");
  562. at_resp_parse_line_args_by_kw(resp, "+QIMUX:", "+QIMUX: %d", &qimux);
  563. if (qimux == 0)
  564. {
  565. AT_SEND_CMD(client, resp, 0, 300, "AT+QIMUX=1");
  566. }
  567. AT_SEND_CMD(client, resp, 0, 300, "AT+QIREGAPP");
  568. /* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */
  569. AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+QIACT");
  570. AT_SEND_CMD(client, resp, 2, 300, "AT+QILOCIP");
  571. /* initialize successfully */
  572. result = RT_EOK;
  573. break;
  574. __exit:
  575. if (result != RT_EOK)
  576. {
  577. /* power off the m26 device */
  578. m26_power_off(device);
  579. rt_thread_mdelay(1000);
  580. LOG_I("%s device initialize retry...", device->name);
  581. }
  582. }
  583. if (resp)
  584. {
  585. at_delete_resp(resp);
  586. }
  587. if (result == RT_EOK)
  588. {
  589. m26_netdev_set_info(device->netdev);
  590. /* check and create link staus sync thread */
  591. if (rt_thread_find(device->netdev->name) == RT_NULL)
  592. {
  593. m26_netdev_check_link_status(device->netdev);
  594. }
  595. LOG_I("%s device network initialize success.", device->name);
  596. }
  597. else
  598. {
  599. LOG_E("%s device network initialize failed(%d).", device->name, result);
  600. }
  601. }
  602. static int m26_net_init(struct at_device *device)
  603. {
  604. #ifdef AT_DEVICE_M26_INIT_ASYN
  605. rt_thread_t tid;
  606. tid = rt_thread_create("m26_net", m26_init_thread_entry, (void *)device,
  607. M26_THREAD_STACK_SIZE, M26_THREAD_PRIORITY, 20);
  608. if (tid)
  609. {
  610. rt_thread_startup(tid);
  611. }
  612. else
  613. {
  614. LOG_E("create %s device init thread failed.", device->name);
  615. return -RT_ERROR;
  616. }
  617. #else
  618. m26_init_thread_entry(device);
  619. #endif /* AT_DEVICE_M26_INIT_ASYN */
  620. return RT_EOK;
  621. }
  622. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  623. {
  624. RT_ASSERT(data);
  625. LOG_I("URC data : %.*s", size, data);
  626. }
  627. static const struct at_urc urc_table[] = {
  628. {"RING", "\r\n", urc_func},
  629. {"Call Ready", "\r\n", urc_func},
  630. {"RDY", "\r\n", urc_func},
  631. {"NO CARRIER", "\r\n", urc_func},
  632. };
  633. static int m26_init(struct at_device *device)
  634. {
  635. struct at_device_m26 *m26 = (struct at_device_m26 *) device->user_data;
  636. /* initialize AT client */
  637. at_client_init(m26->client_name, m26->recv_line_num);
  638. device->client = at_client_get(m26->client_name);
  639. if (device->client == RT_NULL)
  640. {
  641. LOG_E("get AT client(%s) failed.", m26->client_name);
  642. return -RT_ERROR;
  643. }
  644. /* register URC data execution function */
  645. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  646. #ifdef AT_USING_SOCKET
  647. m26_socket_init(device);
  648. #endif
  649. /* add m26 netdev to the netdev list */
  650. device->netdev = m26_netdev_add(m26->device_name);
  651. if (device->netdev == RT_NULL)
  652. {
  653. LOG_E("get netdev(%s) failed.", m26->device_name);
  654. return -RT_ERROR;
  655. }
  656. /* initialize m26 pin configuration */
  657. if (m26->power_pin != -1 && m26->power_status_pin != -1)
  658. {
  659. rt_pin_mode(m26->power_pin, PIN_MODE_OUTPUT);
  660. rt_pin_mode(m26->power_status_pin, PIN_MODE_INPUT);
  661. }
  662. /* initialize m26 device network */
  663. return m26_netdev_set_up(device->netdev);
  664. }
  665. static int m26_deinit(struct at_device *device)
  666. {
  667. return m26_netdev_set_down(device->netdev);
  668. }
  669. static int m26_control(struct at_device *device, int cmd, void *arg)
  670. {
  671. int result = -RT_ERROR;
  672. RT_ASSERT(device);
  673. switch (cmd)
  674. {
  675. case AT_DEVICE_CTRL_POWER_ON:
  676. case AT_DEVICE_CTRL_POWER_OFF:
  677. case AT_DEVICE_CTRL_RESET:
  678. case AT_DEVICE_CTRL_LOW_POWER:
  679. case AT_DEVICE_CTRL_SLEEP:
  680. case AT_DEVICE_CTRL_WAKEUP:
  681. case AT_DEVICE_CTRL_NET_CONN:
  682. case AT_DEVICE_CTRL_NET_DISCONN:
  683. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  684. case AT_DEVICE_CTRL_GET_SIGNAL:
  685. case AT_DEVICE_CTRL_GET_GPS:
  686. case AT_DEVICE_CTRL_GET_VER:
  687. LOG_W("not support the control command(%d).", cmd);
  688. break;
  689. default:
  690. LOG_E("input error control command(%d).", cmd);
  691. break;
  692. }
  693. return result;
  694. }
  695. static const struct at_device_ops m26_device_ops =
  696. {
  697. m26_init,
  698. m26_deinit,
  699. m26_control,
  700. };
  701. static int m26_device_class_register(void)
  702. {
  703. struct at_device_class *class = RT_NULL;
  704. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  705. if (class == RT_NULL)
  706. {
  707. LOG_E("no memory for device class create.");
  708. return -RT_ENOMEM;
  709. }
  710. /* fill m26 device class object */
  711. #ifdef AT_USING_SOCKET
  712. m26_socket_class_register(class);
  713. #endif
  714. class->device_ops = &m26_device_ops;
  715. return at_device_class_register(class, AT_DEVICE_CLASS_M26_MC20);
  716. }
  717. INIT_DEVICE_EXPORT(m26_device_class_register);
  718. #endif /* AT_DEVICE_USING_M26 */