at_device_bc26.c 26 KB

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