at_device_bc28.c 26 KB

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