at_device_me3616.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. * 2019-12-30 qiyongzhong first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <at_device_me3616.h>
  13. #define LOG_TAG "at.dev.me3616"
  14. #include <at_log.h>
  15. #ifdef AT_DEVICE_USING_ME3616
  16. #ifndef ME3616_DEEP_SLEEP_EN
  17. #define ME3616_DEEP_SLEEP_EN 0//module support deep sleep mode
  18. #endif
  19. #define ME3616_WAIT_CONNECT_TIME 5000
  20. #define ME3616_THREAD_STACK_SIZE 2048
  21. #define ME3616_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  22. static int me3616_power_on(struct at_device *device)
  23. {
  24. struct at_device_me3616 *me3616 = RT_NULL;
  25. me3616 = (struct at_device_me3616 *)device->user_data;
  26. me3616->power_status = RT_TRUE;
  27. /* not nead to set pin configuration for me3616 device power on */
  28. if (me3616->power_pin == -1)
  29. {
  30. return(RT_EOK);
  31. }
  32. rt_pin_write(me3616->power_pin, PIN_HIGH);
  33. rt_thread_mdelay(500);
  34. rt_pin_write(me3616->power_pin, PIN_LOW);
  35. LOG_D("power on success.");
  36. return(RT_EOK);
  37. }
  38. static int me3616_power_off(struct at_device *device)
  39. {
  40. at_response_t resp = RT_NULL;
  41. struct at_device_me3616 *me3616 = RT_NULL;
  42. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  43. if (resp == RT_NULL)
  44. {
  45. LOG_E("no memory for resp create.");
  46. return(-RT_ERROR);
  47. }
  48. at_obj_exec_cmd(device->client, resp, "AT+ZTURNOFF");//command response does not have "\r\n" after "OK"
  49. /*if (at_obj_exec_cmd(device->client, resp, "AT+ZTURNOFF") != RT_EOK)
  50. {
  51. LOG_D("power off fail.");
  52. at_delete_resp(resp);
  53. return(-RT_ERROR);
  54. }*/
  55. at_delete_resp(resp);
  56. me3616 = (struct at_device_me3616 *)device->user_data;
  57. me3616->power_status = RT_FALSE;
  58. LOG_D("power off success.");
  59. return(RT_EOK);
  60. }
  61. static int me3616_sleep(struct at_device *device)
  62. {
  63. at_response_t resp = RT_NULL;
  64. struct at_device_me3616 *me3616 = RT_NULL;
  65. me3616 = (struct at_device_me3616 *)device->user_data;
  66. if ( ! me3616->power_status)//power off
  67. {
  68. return(RT_EOK);
  69. }
  70. if (me3616->sleep_status)//is sleep status
  71. {
  72. return(RT_EOK);
  73. }
  74. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  75. if (resp == RT_NULL)
  76. {
  77. LOG_E("no memory for resp create.");
  78. return(-RT_ERROR);
  79. }
  80. if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=1,,,\"00111110\",\"00000001\"") != RT_EOK)
  81. {
  82. LOG_D("enable sleep fail.");
  83. at_delete_resp(resp);
  84. return(-RT_ERROR);
  85. }
  86. #if ME3616_DEEP_SLEEP_EN
  87. if (at_obj_exec_cmd(device->client, resp, "AT+ZSLR") != RT_EOK)
  88. {
  89. LOG_D("startup entry into sleep fail.");
  90. at_delete_resp(resp);
  91. return(-RT_ERROR);
  92. }
  93. #endif
  94. at_delete_resp(resp);
  95. me3616->sleep_status = RT_TRUE;
  96. LOG_D("sleep success.");
  97. return(RT_EOK);
  98. }
  99. static int me3616_wakeup(struct at_device *device)
  100. {
  101. at_response_t resp = RT_NULL;
  102. struct at_device_me3616 *me3616 = RT_NULL;
  103. me3616 = (struct at_device_me3616 *)device->user_data;
  104. if ( ! me3616->power_status)//power off
  105. {
  106. LOG_E("the power is off and the wake-up cannot be performed");
  107. return(-RT_ERROR);
  108. }
  109. if ( ! me3616->sleep_status)//no sleep status
  110. {
  111. return(RT_EOK);
  112. }
  113. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  114. if (resp == RT_NULL)
  115. {
  116. LOG_E("no memory for resp create.");
  117. return(-RT_ERROR);
  118. }
  119. #if ME3616_DEEP_SLEEP_EN
  120. if (me3616->power_pin != -1)
  121. {
  122. rt_pin_write(me3616->power_pin, PIN_HIGH);
  123. rt_thread_mdelay(100);
  124. rt_pin_write(me3616->power_pin, PIN_LOW);
  125. rt_thread_mdelay(200);
  126. }
  127. #endif
  128. if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK)
  129. {
  130. LOG_D("wake up fail.");
  131. at_delete_resp(resp);
  132. return(-RT_ERROR);
  133. }
  134. at_delete_resp(resp);
  135. me3616->sleep_status = RT_FALSE;
  136. LOG_D("wake up success.");
  137. return(RT_EOK);
  138. }
  139. static int me3616_check_link_status(struct at_device *device)
  140. {
  141. at_response_t resp = RT_NULL;
  142. struct at_device_me3616 *me3616 = RT_NULL;
  143. int result = -RT_ERROR;
  144. RT_ASSERT(device);
  145. me3616 = (struct at_device_me3616 *)device->user_data;
  146. if ( ! me3616->power_status)//power off
  147. {
  148. LOG_D("the power is off.");
  149. return(-RT_ERROR);
  150. }
  151. #if ME3616_DEEP_SLEEP_EN
  152. if (me3616->sleep_status)//is sleep status
  153. {
  154. if (me3616->power_pin != -1)
  155. {
  156. rt_pin_write(me3616->power_pin, PIN_HIGH);
  157. rt_thread_mdelay(100);
  158. rt_pin_write(me3616->power_pin, PIN_LOW);
  159. rt_thread_mdelay(200);
  160. }
  161. }
  162. #endif
  163. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  164. if (resp == RT_NULL)
  165. {
  166. LOG_E("no memory for resp create.");
  167. return(-RT_ERROR);
  168. }
  169. result = -RT_ERROR;
  170. if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") == RT_EOK)
  171. {
  172. int link_stat = 0;
  173. if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
  174. {
  175. if (link_stat == 1 || link_stat == 5)
  176. {
  177. result = RT_EOK;
  178. }
  179. }
  180. }
  181. #if ME3616_DEEP_SLEEP_EN
  182. if (me3616->sleep_status)//is sleep status
  183. {
  184. if (at_obj_exec_cmd(device->client, resp, "AT+ZSLR") != RT_EOK)
  185. {
  186. LOG_D("startup entry into sleep fail.");
  187. }
  188. }
  189. #endif
  190. at_delete_resp(resp);
  191. return(result);
  192. }
  193. /* ============================= me3616 network interface operations ============================= */
  194. /* set me3616 network interface device status and address information */
  195. static int me3616_netdev_set_info(struct netdev *netdev)
  196. {
  197. #define ME3616_INFO_RESP_SIZE 128
  198. #define ME3616_INFO_RESP_TIMO rt_tick_from_millisecond(300)
  199. int result = RT_EOK;
  200. ip_addr_t addr;
  201. at_response_t resp = RT_NULL;
  202. struct at_device *device = RT_NULL;
  203. RT_ASSERT(netdev);
  204. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  205. if (device == RT_NULL)
  206. {
  207. LOG_E("get device(%s) failed.", netdev->name);
  208. return -RT_ERROR;
  209. }
  210. /* set network interface device status */
  211. netdev_low_level_set_status(netdev, RT_TRUE);
  212. netdev_low_level_set_link_status(netdev, RT_TRUE);
  213. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  214. resp = at_create_resp(ME3616_INFO_RESP_SIZE, 0, ME3616_INFO_RESP_TIMO);
  215. if (resp == RT_NULL)
  216. {
  217. LOG_E("no memory for resp create.");
  218. result = -RT_ENOMEM;
  219. goto __exit;
  220. }
  221. /* set network interface device hardware address(IMEI) */
  222. {
  223. #define ME3616_NETDEV_HWADDR_LEN 8
  224. #define ME3616_IMEI_LEN 15
  225. char imei[ME3616_IMEI_LEN] = {0};
  226. int i = 0, j = 0;
  227. /* send "AT+GSN" commond to get device IMEI */
  228. if (at_obj_exec_cmd(device->client, resp, "AT+GSN") != RT_EOK)
  229. {
  230. result = -RT_ERROR;
  231. goto __exit;
  232. }
  233. if (at_resp_parse_line_args(resp, 2, "%s", imei) <= 0)
  234. {
  235. LOG_E("%s device prase \"AT+GSN\" cmd error.", device->name);
  236. result = -RT_ERROR;
  237. goto __exit;
  238. }
  239. LOG_D("%s device IMEI number: %s", device->name, imei);
  240. netdev->hwaddr_len = ME3616_NETDEV_HWADDR_LEN;
  241. /* get hardware address by IMEI */
  242. for (i = 0, j = 0; i < ME3616_NETDEV_HWADDR_LEN && j < ME3616_IMEI_LEN; i++, j+=2)
  243. {
  244. if (j != ME3616_IMEI_LEN - 1)
  245. {
  246. netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
  247. }
  248. else
  249. {
  250. netdev->hwaddr[i] = (imei[j] - '0');
  251. }
  252. }
  253. }
  254. /* set network interface device IP address */
  255. {
  256. #define IP_ADDR_SIZE_MAX 16
  257. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  258. /* send "AT+CGPADDR=1" commond to get IP address */
  259. if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") != RT_EOK)
  260. {
  261. result = -RT_ERROR;
  262. goto __exit;
  263. }
  264. /* parse response data "+CGPADDR: 1,<IP_address>" */
  265. if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*[^\"]\"%[^\"]", ipaddr) <= 0)
  266. {
  267. LOG_E("%s device \"AT+CGPADDR=1\" cmd error.", device->name);
  268. result = -RT_ERROR;
  269. goto __exit;
  270. }
  271. LOG_D("%s device IP address: %s", device->name, ipaddr);
  272. /* set network interface address information */
  273. inet_aton(ipaddr, &addr);
  274. netdev_low_level_set_ipaddr(netdev, &addr);
  275. }
  276. __exit:
  277. if (resp)
  278. {
  279. at_delete_resp(resp);
  280. }
  281. return result;
  282. }
  283. static void me3616_check_link_status_entry(void *parameter)
  284. {
  285. #define ME3616_LINK_DELAY_TIME (60 * RT_TICK_PER_SECOND)
  286. rt_bool_t is_link_up;
  287. struct at_device *device = RT_NULL;
  288. struct netdev *netdev = (struct netdev *) parameter;
  289. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  290. if (device == RT_NULL)
  291. {
  292. LOG_E("get device(%s) failed.", netdev->name);
  293. return;
  294. }
  295. while (1)
  296. {
  297. is_link_up = (me3616_check_link_status(device) == RT_EOK);
  298. netdev_low_level_set_link_status(netdev, is_link_up);
  299. rt_thread_delay(ME3616_LINK_DELAY_TIME);
  300. }
  301. }
  302. static int me3616_netdev_check_link_status(struct netdev *netdev)
  303. {
  304. #define ME3616_LINK_THREAD_TICK 20
  305. #define ME3616_LINK_THREAD_STACK_SIZE (1024 + 512)
  306. #define ME3616_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  307. rt_thread_t tid;
  308. char tname[RT_NAME_MAX] = {0};
  309. RT_ASSERT(netdev);
  310. rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
  311. /* create me3616 link status polling thread */
  312. tid = rt_thread_create(tname, me3616_check_link_status_entry, (void *)netdev,
  313. ME3616_LINK_THREAD_STACK_SIZE, ME3616_LINK_THREAD_PRIORITY, ME3616_LINK_THREAD_TICK);
  314. if (tid != RT_NULL)
  315. {
  316. rt_thread_startup(tid);
  317. }
  318. return RT_EOK;
  319. }
  320. static int me3616_net_init(struct at_device *device);
  321. static int me3616_netdev_set_up(struct netdev *netdev)
  322. {
  323. struct at_device *device = RT_NULL;
  324. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  325. if (device == RT_NULL)
  326. {
  327. LOG_E("get device(%s) failed.", netdev->name);
  328. return -RT_ERROR;
  329. }
  330. if (device->is_init == RT_FALSE)
  331. {
  332. me3616_net_init(device);
  333. device->is_init = RT_TRUE;
  334. netdev_low_level_set_status(netdev, RT_TRUE);
  335. LOG_D("network interface device(%s) set up status.", netdev->name);
  336. }
  337. return RT_EOK;
  338. }
  339. static int me3616_netdev_set_down(struct netdev *netdev)
  340. {
  341. struct at_device *device = RT_NULL;
  342. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  343. if (device == RT_NULL)
  344. {
  345. LOG_E("get device(%s) failed.", netdev->name);
  346. return -RT_ERROR;
  347. }
  348. if (device->is_init == RT_TRUE)
  349. {
  350. me3616_power_off(device);
  351. device->is_init = RT_FALSE;
  352. netdev_low_level_set_status(netdev, RT_FALSE);
  353. LOG_D("network interface device(%s) set down status.", netdev->name);
  354. }
  355. return RT_EOK;
  356. }
  357. #ifdef NETDEV_USING_PING
  358. static int me3616_netdev_ping(struct netdev *netdev, const char *host,
  359. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
  360. #if RT_VER_NUM >= 0x50100
  361. , rt_bool_t is_bind
  362. #endif
  363. )
  364. {
  365. #define ME3616_PING_RESP_SIZE 256
  366. #define ME3616_PING_IP_SIZE 16
  367. #define ME3616_PING_TIMEO (10 * RT_TICK_PER_SECOND)
  368. rt_err_t result = RT_EOK;
  369. int response = -1, recv_data_len, ping_time, ttl;
  370. char ip_addr[ME3616_PING_IP_SIZE] = {0};
  371. at_response_t resp = RT_NULL;
  372. struct at_device *device = RT_NULL;
  373. RT_ASSERT(netdev);
  374. RT_ASSERT(host);
  375. RT_ASSERT(ping_resp);
  376. #if RT_VER_NUM >= 0x50100
  377. RT_UNUSED(is_bind);
  378. #endif
  379. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  380. if (device == RT_NULL)
  381. {
  382. LOG_E("get device(%s) failed.", netdev->name);
  383. return -RT_ERROR;
  384. }
  385. resp = at_create_resp(ME3616_PING_RESP_SIZE, 0, ME3616_PING_TIMEO);
  386. if (resp == RT_NULL)
  387. {
  388. LOG_E("no memory for resp create");
  389. return -RT_ENOMEM;
  390. }
  391. if (at_obj_exec_cmd(device->client, resp, "AT+EDNS=\"%s\"", host) != RT_EOK)
  392. {
  393. result = -RT_ERROR;
  394. goto __exit;
  395. }
  396. if (at_resp_parse_line_args_by_kw(resp, "IPV4:", "IPV4:%s\r", ip_addr) <= 0)
  397. {
  398. result = -RT_ERROR;
  399. goto __exit;
  400. }
  401. at_resp_set_info(resp, ME3616_PING_RESP_SIZE, 8, timeout);
  402. /* send "AT+PING=<host>[-l/L <p_size>] [-n/N <count>][-w/W <time>][-6][-i <value>][-d<value>] */
  403. if (at_obj_exec_cmd(device->client, resp, "AT+PING=%s -d 1 -n 1 -w %d", ip_addr, timeout) < 0)
  404. {
  405. result = -RT_ERROR;
  406. goto __exit;
  407. }
  408. if (at_resp_parse_line_args_by_kw(resp, "received=", "%*[^=]=%d%*[^=]=%d%*[^=]=%d",
  409. &recv_data_len, &ping_time, &ttl) <= 0)
  410. {
  411. result = -RT_ERROR;
  412. goto __exit;
  413. }
  414. inet_aton(ip_addr, &(ping_resp->ip_addr));
  415. ping_resp->data_len = recv_data_len;
  416. ping_resp->ticks = ping_time;
  417. ping_resp->ttl = ttl;
  418. __exit:
  419. if (resp)
  420. {
  421. at_delete_resp(resp);
  422. }
  423. return result;
  424. }
  425. #endif /* NETDEV_USING_PING */
  426. const struct netdev_ops me3616_netdev_ops =
  427. {
  428. me3616_netdev_set_up,
  429. me3616_netdev_set_down,
  430. RT_NULL,
  431. RT_NULL,
  432. RT_NULL,
  433. #ifdef NETDEV_USING_PING
  434. me3616_netdev_ping,
  435. RT_NULL,
  436. #endif
  437. RT_NULL,
  438. };
  439. static struct netdev *me3616_netdev_add(const char *netdev_name)
  440. {
  441. #define ETHERNET_MTU 1500
  442. #define HWADDR_LEN 8
  443. struct netdev *netdev = RT_NULL;
  444. netdev = netdev_get_by_name(netdev_name);
  445. if(netdev != RT_NULL)
  446. {
  447. return(netdev);
  448. }
  449. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  450. if (netdev == RT_NULL)
  451. {
  452. LOG_E("no memory for netdev create.");
  453. return RT_NULL;
  454. }
  455. netdev->mtu = ETHERNET_MTU;
  456. netdev->ops = &me3616_netdev_ops;
  457. netdev->hwaddr_len = HWADDR_LEN;
  458. #ifdef SAL_USING_AT
  459. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  460. /* set the network interface socket/netdb operations */
  461. sal_at_netdev_set_pf_info(netdev);
  462. #endif
  463. netdev_register(netdev, netdev_name, RT_NULL);
  464. return netdev;
  465. }
  466. /* ============================= me3616 device operations ============================= */
  467. /* initialize for me3616 */
  468. static void me3616_init_thread_entry(void *parameter)
  469. {
  470. #define INIT_RETRY 5
  471. #define CPIN_RETRY 5
  472. #define CSQ_RETRY 20
  473. #define CGREG_RETRY 60
  474. #define IPADDR_RETRY 10
  475. int i;
  476. int retry_num = INIT_RETRY;
  477. rt_err_t result = RT_EOK;
  478. at_response_t resp = RT_NULL;
  479. struct at_device *device = (struct at_device *) parameter;
  480. struct at_client *client = device->client;
  481. resp = at_create_resp(256, 0, rt_tick_from_millisecond(500));
  482. if (resp == RT_NULL)
  483. {
  484. LOG_E("no memory for resp create.");
  485. return;
  486. }
  487. LOG_D("start init %s device.", device->name);
  488. while (retry_num--)
  489. {
  490. /* power on the me3616 device */
  491. me3616_power_on(device);
  492. rt_thread_mdelay(2000);
  493. /* wait me3616 startup finish, send AT every 500ms, if receive OK, SYNC success*/
  494. if (at_client_obj_wait_connect(client, ME3616_WAIT_CONNECT_TIME))
  495. {
  496. result = -RT_ETIMEOUT;
  497. goto __exit;
  498. }
  499. /* disable echo */
  500. if (at_obj_exec_cmd(device->client, resp, "ATE0") != RT_EOK)
  501. {
  502. result = -RT_ERROR;
  503. goto __exit;
  504. }
  505. /* disable PSM mode */
  506. if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK)
  507. {
  508. result = -RT_ERROR;
  509. goto __exit;
  510. }
  511. /* disable eDRX mode */
  512. if (at_obj_exec_cmd(device->client, resp, "AT+CEDRXS=0") != RT_EOK)
  513. {
  514. result = -RT_ERROR;
  515. goto __exit;
  516. }
  517. /* disable low power mode */
  518. if (at_obj_exec_cmd(device->client, resp, "AT+ESOSETRPT=1") != RT_EOK)
  519. {
  520. result = -RT_ERROR;
  521. goto __exit;
  522. }
  523. /* disable sleep function */
  524. if (at_obj_exec_cmd(device->client, resp, "AT+ZSLR=0") != RT_EOK)
  525. {
  526. result = -RT_ERROR;
  527. goto __exit;
  528. }
  529. /* Get the baudrate */
  530. if (at_obj_exec_cmd(device->client, resp, "AT+IPR?") != RT_EOK)
  531. {
  532. result = -RT_ERROR;
  533. goto __exit;
  534. }
  535. at_resp_parse_line_args_by_kw(resp, "+IPR:", "+IPR: %d\r", &i);
  536. LOG_D("%s device baudrate %d", device->name, i);
  537. /* get module version */
  538. if (at_obj_exec_cmd(device->client, resp, "ATI") != RT_EOK)
  539. {
  540. result = -RT_ERROR;
  541. goto __exit;
  542. }
  543. for (i = 0; i < (int) resp->line_counts - 1; i++)
  544. {
  545. LOG_D("%s", at_resp_get_line(resp, i + 1));
  546. }
  547. /* check SIM card */
  548. for (i = 0; i < CPIN_RETRY; i++)
  549. {
  550. rt_thread_mdelay(1000);
  551. if (at_obj_exec_cmd(device->client, resp, "AT+CPIN?") == RT_EOK)
  552. {
  553. if (at_resp_get_line_by_kw(resp, "READY") != RT_NULL)
  554. break;
  555. }
  556. }
  557. if (i == CPIN_RETRY)
  558. {
  559. LOG_E("%s device SIM card detection failed.", device->name);
  560. result = -RT_ERROR;
  561. goto __exit;
  562. }
  563. /* check signal strength */
  564. for (i = 0; i < CSQ_RETRY; i++)
  565. {
  566. rt_thread_mdelay(1000);
  567. if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK)
  568. {
  569. int signal_strength = 0, err_rate = 0;
  570. if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &signal_strength, &err_rate) > 0)
  571. {
  572. if ((signal_strength != 99) && (signal_strength != 0))
  573. {
  574. LOG_D("%s device signal strength: %d, channel bit error rate: %d",
  575. device->name, signal_strength, err_rate);
  576. break;
  577. }
  578. }
  579. }
  580. }
  581. if (i == CSQ_RETRY)
  582. {
  583. LOG_E("%s device signal strength check failed", device->name);
  584. result = -RT_ERROR;
  585. goto __exit;
  586. }
  587. /* check the GPRS network is registered */
  588. for (i = 0; i < CGREG_RETRY; i++)
  589. {
  590. rt_thread_mdelay(1000);
  591. if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") == RT_EOK)
  592. {
  593. int link_stat = 0;
  594. if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
  595. {
  596. if ((link_stat == 1) || (link_stat == 5))
  597. {
  598. LOG_D("%s device GPRS is registered", device->name);
  599. break;
  600. }
  601. }
  602. }
  603. }
  604. if (i == CGREG_RETRY)
  605. {
  606. LOG_E("%s device GPRS is register failed", device->name);
  607. result = -RT_ERROR;
  608. goto __exit;
  609. }
  610. /* check the GPRS network IP address */
  611. for (i = 0; i < IPADDR_RETRY; i++)
  612. {
  613. rt_thread_mdelay(1000);
  614. if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") == RT_EOK)
  615. {
  616. #define IP_ADDR_SIZE_MAX 16
  617. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  618. /* parse response data "+CGPADDR: 1,<IP_address>" */
  619. if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*[^\"]\"%[^\"]", ipaddr) > 0)
  620. {
  621. LOG_D("%s device IP address: %s", device->name, ipaddr);
  622. break;
  623. }
  624. }
  625. }
  626. if (i == IPADDR_RETRY)
  627. {
  628. LOG_E("%s device GPRS is get IP address failed", device->name);
  629. result = -RT_ERROR;
  630. goto __exit;
  631. }
  632. /* initialize successfully */
  633. result = RT_EOK;
  634. break;
  635. __exit:
  636. if (result != RT_EOK)
  637. {
  638. /* power off the me3616 device */
  639. me3616_power_off(device);
  640. rt_thread_mdelay(1000);
  641. LOG_I("%s device initialize retry...", device->name);
  642. }
  643. }
  644. if (resp)
  645. {
  646. at_delete_resp(resp);
  647. }
  648. if (result == RT_EOK)
  649. {
  650. /* set network interface device status and address information */
  651. me3616_netdev_set_info(device->netdev);
  652. /* check and create link staus sync thread */
  653. if (rt_thread_find(device->netdev->name) == RT_NULL)
  654. {
  655. me3616_netdev_check_link_status(device->netdev);
  656. }
  657. LOG_I("%s device network initialize success.", device->name);
  658. }
  659. else
  660. {
  661. LOG_E("%s device network initialize failed(%d).", device->name, result);
  662. }
  663. }
  664. /* me3616 device network initialize */
  665. static int me3616_net_init(struct at_device *device)
  666. {
  667. #ifdef AT_DEVICE_ME3616_INIT_ASYN
  668. rt_thread_t tid;
  669. tid = rt_thread_create("me3616_net", me3616_init_thread_entry, (void *)device,
  670. ME3616_THREAD_STACK_SIZE, ME3616_THREAD_PRIORITY, 20);
  671. if (tid)
  672. {
  673. rt_thread_startup(tid);
  674. }
  675. else
  676. {
  677. LOG_E("create %s device init thread failed.", device->name);
  678. return -RT_ERROR;
  679. }
  680. #else
  681. me3616_init_thread_entry(device);
  682. #endif /* AT_DEVICE_ME3616_INIT_ASYN */
  683. return RT_EOK;
  684. }
  685. static int me3616_init(struct at_device *device)
  686. {
  687. struct at_device_me3616 *me3616 = RT_NULL;
  688. RT_ASSERT(device);
  689. me3616 = (struct at_device_me3616 *) device->user_data;
  690. me3616->power_status = RT_FALSE;//default power is off.
  691. me3616->sleep_status = RT_FALSE;//default sleep is disabled.
  692. /* initialize AT client */
  693. #if RT_VER_NUM >= 0x50100
  694. at_client_init(me3616->client_name, me3616->recv_line_num, me3616->recv_line_num);
  695. #else
  696. at_client_init(me3616->client_name, me3616->recv_line_num);
  697. #endif
  698. device->client = at_client_get(me3616->client_name);
  699. if (device->client == RT_NULL)
  700. {
  701. LOG_E("get AT client(%s) failed.", me3616->client_name);
  702. return -RT_ERROR;
  703. }
  704. /* register URC data execution function */
  705. #ifdef AT_USING_SOCKET
  706. me3616_socket_init(device);
  707. #endif
  708. /* add me3616 device to the netdev list */
  709. device->netdev = me3616_netdev_add(me3616->device_name);
  710. if (device->netdev == RT_NULL)
  711. {
  712. LOG_E("add netdev(%s) failed.", me3616->device_name);
  713. return -RT_ERROR;
  714. }
  715. /* initialize me3616 pin configuration */
  716. if (me3616->power_pin != -1)
  717. {
  718. rt_pin_write(me3616->power_pin, PIN_LOW);
  719. rt_pin_mode(me3616->power_pin, PIN_MODE_OUTPUT);
  720. }
  721. /* initialize me3616 device network */
  722. return me3616_netdev_set_up(device->netdev);
  723. }
  724. static int me3616_deinit(struct at_device *device)
  725. {
  726. RT_ASSERT(device);
  727. return me3616_netdev_set_down(device->netdev);
  728. }
  729. static int me3616_control(struct at_device *device, int cmd, void *arg)
  730. {
  731. int result = -RT_ERROR;
  732. RT_ASSERT(device);
  733. switch (cmd)
  734. {
  735. case AT_DEVICE_CTRL_SLEEP:
  736. result = me3616_sleep(device);
  737. break;
  738. case AT_DEVICE_CTRL_WAKEUP:
  739. result = me3616_wakeup(device);
  740. break;
  741. case AT_DEVICE_CTRL_POWER_ON:
  742. case AT_DEVICE_CTRL_POWER_OFF:
  743. case AT_DEVICE_CTRL_RESET:
  744. case AT_DEVICE_CTRL_LOW_POWER:
  745. case AT_DEVICE_CTRL_NET_CONN:
  746. case AT_DEVICE_CTRL_NET_DISCONN:
  747. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  748. case AT_DEVICE_CTRL_GET_SIGNAL:
  749. case AT_DEVICE_CTRL_GET_GPS:
  750. case AT_DEVICE_CTRL_GET_VER:
  751. LOG_W("not support the control command(%d).", cmd);
  752. break;
  753. default:
  754. LOG_E("input error control command(%d).", cmd);
  755. break;
  756. }
  757. return result;
  758. }
  759. const struct at_device_ops me3616_device_ops =
  760. {
  761. me3616_init,
  762. me3616_deinit,
  763. me3616_control,
  764. };
  765. static int me3616_device_class_register(void)
  766. {
  767. struct at_device_class *class = RT_NULL;
  768. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  769. if (class == RT_NULL)
  770. {
  771. LOG_E("no memory for device class create.");
  772. return -RT_ENOMEM;
  773. }
  774. /* fill me3616 device class object */
  775. #ifdef AT_USING_SOCKET
  776. me3616_socket_class_register(class);
  777. #endif
  778. class->device_ops = &me3616_device_ops;
  779. return at_device_class_register(class, AT_DEVICE_CLASS_ME3616);
  780. }
  781. INIT_DEVICE_EXPORT(me3616_device_class_register);
  782. #endif /* AT_DEVICE_USING_ME3616 */