at_device_m26.c 24 KB

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