at_device_bc28.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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, size_t data_len,
  315. uint32_t timeout, struct netdev_ping_resp *ping_resp
  316. #if RT_VER_NUM >= 0x50100
  317. , rt_bool_t is_bind
  318. #endif
  319. )
  320. {
  321. #define BC28_PING_RESP_SIZE 128
  322. #define BC28_PING_IP_SIZE 16
  323. #define BC28_PING_TIMEOUT (10 * RT_TICK_PER_SECOND)
  324. rt_err_t result = RT_EOK;
  325. int response = -1, ping_time, ttl;
  326. char ip_addr[BC28_PING_IP_SIZE] = {0};
  327. at_response_t resp = RT_NULL;
  328. struct at_device *device = RT_NULL;
  329. #if RT_VER_NUM >= 0x50100
  330. RT_UNUSED(is_bind);
  331. #endif
  332. RT_ASSERT(netdev);
  333. RT_ASSERT(host);
  334. RT_ASSERT(ping_resp);
  335. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  336. if (device == RT_NULL)
  337. {
  338. LOG_E("get device(%s) failed.", netdev->name);
  339. return -RT_ERROR;
  340. }
  341. resp = at_create_resp(BC28_PING_RESP_SIZE, 4, BC28_PING_TIMEOUT);
  342. if (resp == RT_NULL)
  343. {
  344. LOG_E("no memory for resp create");
  345. return -RT_ENOMEM;
  346. }
  347. /* DNS resolve */
  348. struct in_addr inp;
  349. if (inet_aton(host, &inp) > 0)
  350. {
  351. rt_strncpy(ip_addr, host, BC28_PING_IP_SIZE);
  352. }
  353. #ifdef AT_USING_SOCKET
  354. else
  355. {
  356. if(0 > bc28_domain_resolve(host, ip_addr))
  357. {
  358. LOG_E("can not resolve domain");
  359. goto __exit;
  360. }
  361. }
  362. #endif
  363. if (at_obj_exec_cmd(device->client, resp, "AT+NPING=%s,%d,%d",
  364. ip_addr, data_len, timeout*1000/RT_TICK_PER_SECOND) < 0)
  365. {
  366. result = -RT_ERROR;
  367. goto __exit;
  368. }
  369. if (at_resp_parse_line_args_by_kw(resp, "+NPINGERR:", "+NPINGERR:%d", &response) > 0)
  370. {
  371. switch (response)
  372. {
  373. case 1:
  374. result = -RT_ETIMEOUT;
  375. break;
  376. case 2:
  377. default:
  378. result = -RT_ERROR;
  379. break;
  380. }
  381. }
  382. else if (at_resp_parse_line_args_by_kw(resp, "+NPING:", "+NPING:%[^,],%d,%d",
  383. ip_addr, &ttl, &ping_time) > 0 )
  384. {
  385. inet_aton(ip_addr, &(ping_resp->ip_addr));
  386. ping_resp->data_len = data_len;
  387. ping_resp->ticks = rt_tick_from_millisecond(ping_time);
  388. ping_resp->ttl = ttl;
  389. result = RT_EOK;
  390. }
  391. else
  392. {
  393. result = -RT_ERROR;
  394. goto __exit;
  395. }
  396. __exit:
  397. if (resp)
  398. {
  399. at_delete_resp(resp);
  400. }
  401. return result;
  402. }
  403. #endif /* NETDEV_USING_PING */
  404. const struct netdev_ops bc28_netdev_ops =
  405. {
  406. bc28_netdev_set_up,
  407. bc28_netdev_set_down,
  408. RT_NULL,
  409. bc28_netdev_set_dns_server,
  410. RT_NULL,
  411. #ifdef NETDEV_USING_PING
  412. bc28_netdev_ping,
  413. #endif
  414. RT_NULL,
  415. };
  416. static struct netdev *bc28_netdev_add(const char *netdev_name)
  417. {
  418. #define ETHERNET_MTU 1500
  419. #define HWADDR_LEN 8
  420. struct netdev *netdev = RT_NULL;
  421. netdev = netdev_get_by_name(netdev_name);
  422. if (netdev != RT_NULL)
  423. {
  424. return (netdev);
  425. }
  426. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  427. if (netdev == RT_NULL)
  428. {
  429. LOG_E("no memory for netdev create.");
  430. return RT_NULL;
  431. }
  432. netdev->mtu = ETHERNET_MTU;
  433. netdev->ops = &bc28_netdev_ops;
  434. netdev->hwaddr_len = HWADDR_LEN;
  435. #ifdef SAL_USING_AT
  436. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  437. /* set the network interface socket/netdb operations */
  438. sal_at_netdev_set_pf_info(netdev);
  439. #endif
  440. netdev_register(netdev, netdev_name, RT_NULL);
  441. return netdev;
  442. }
  443. /* ============================= bc28 device operations ============================= */
  444. /* initialize for bc28 */
  445. static void bc28_init_thread_entry(void *parameter)
  446. {
  447. #define INIT_RETRY 5
  448. #define CPIN_RETRY 5
  449. #define CSQ_RETRY 20
  450. #define CGREG_RETRY 50
  451. #define IPADDR_RETRY 10
  452. #define AT_DEFAULT_TIMEOUT 5000
  453. int i;
  454. int retry_num = INIT_RETRY;
  455. rt_err_t result = RT_EOK;
  456. at_response_t resp = RT_NULL;
  457. struct at_device *device = (struct at_device *) parameter;
  458. struct at_client *client = device->client;
  459. resp = at_create_resp(128, 0, rt_tick_from_millisecond(AT_DEFAULT_TIMEOUT));
  460. if (resp == RT_NULL)
  461. {
  462. LOG_E("no memory for resp create.");
  463. return;
  464. }
  465. LOG_D("start init %s device.", device->name);
  466. while (retry_num--)
  467. {
  468. /* reset the bc28 device */
  469. bc28_reset(device);
  470. rt_thread_mdelay(1000);
  471. /* wait bc28 startup finish, send AT every 500ms, if receive OK, SYNC success*/
  472. if (at_client_obj_wait_connect(client, BC28_WAIT_CONNECT_TIME))
  473. {
  474. result = -RT_ETIMEOUT;
  475. goto __exit;
  476. }
  477. /* disable echo */
  478. if (at_obj_exec_cmd(device->client, resp, "ATE0") != RT_EOK)
  479. {
  480. result = -RT_ERROR;
  481. LOG_E("ATE0");
  482. goto __exit;
  483. }
  484. /* disable auto register */
  485. if (at_obj_exec_cmd(device->client, resp, "AT+QREGSWT=2") != RT_EOK)
  486. {
  487. result = -RT_ERROR;
  488. LOG_E(">> AT+QREGSWT=2");
  489. goto __exit;
  490. }
  491. /* disable auto connect */
  492. if (at_obj_exec_cmd(device->client, resp, "AT+NCONFIG=AUTOCONNECT,FALSE") != RT_EOK)
  493. {
  494. result = -RT_ERROR;
  495. LOG_E(">> AT+NCONFIG=AUTOCONNECT,FALSE");
  496. goto __exit;
  497. }
  498. /* reboot */
  499. at_obj_exec_cmd(device->client, resp, "AT+NRB");
  500. rt_thread_mdelay(5000);
  501. while (at_obj_exec_cmd(device->client, resp, "AT") != RT_EOK)
  502. {
  503. rt_thread_mdelay(1000);
  504. }
  505. /* check IMEI */
  506. if (at_obj_exec_cmd(device->client, resp, "AT+CGSN=1") != RT_EOK)
  507. {
  508. result = -RT_ERROR;
  509. LOG_E(">> AT+CGSN=1");
  510. goto __exit;
  511. }
  512. /* search band */
  513. if (at_obj_exec_cmd(device->client, resp, "AT+NBAND=%d", AT_DEVICE_BC28_OP_BAND) != RT_EOK)
  514. {
  515. result = -RT_ERROR;
  516. LOG_E(">> AT+NBAND=8");
  517. goto __exit;
  518. }
  519. /* set max function */
  520. if (at_obj_exec_cmd(device->client, resp, "AT+CFUN=1") != RT_EOK)
  521. {
  522. result = -RT_ERROR;
  523. LOG_E(">> AT+CFUN=1");
  524. goto __exit;
  525. }
  526. /* auto report recv from tcp */
  527. if (at_obj_exec_cmd(device->client, resp, "AT+NSONMI=2") != RT_EOK)
  528. {
  529. result = -RT_ERROR;
  530. LOG_E(">> AT+NSONMI=2");
  531. goto __exit;
  532. }
  533. /* disable eDRX mode */
  534. if (at_obj_exec_cmd(device->client, resp, "AT+CEDRXS=0,5") != RT_EOK)
  535. {
  536. result = -RT_ERROR;
  537. LOG_E(">> AT+CEDRXS=0,5");
  538. goto __exit;
  539. }
  540. /* disable PSM mode */
  541. if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK)
  542. {
  543. result = -RT_ERROR;
  544. LOG_E(">> AT+CPSMS=0");
  545. goto __exit;
  546. }
  547. /* check IMSI */
  548. if (at_obj_exec_cmd(device->client, resp, "AT+CIMI") != RT_EOK)
  549. {
  550. result = -RT_ERROR;
  551. LOG_E(">> AT+CIMI");
  552. goto __exit;
  553. }
  554. /* attach */
  555. if (at_obj_exec_cmd(device->client, resp, "AT+CGATT=1") != RT_EOK)
  556. {
  557. result = -RT_ERROR;
  558. LOG_E(">> AT+CGATT=1");
  559. goto __exit;
  560. }
  561. /* Get the baudrate */
  562. if (at_obj_exec_cmd(device->client, resp, "AT+NATSPEED?") != RT_EOK)
  563. {
  564. result = -RT_ERROR;
  565. LOG_E(">> AT+NATSPEED?");
  566. goto __exit;
  567. }
  568. at_resp_parse_line_args_by_kw(resp, "+NATSPEED:", "+NATSPEED:%d", &i);
  569. LOG_D("%s device baudrate %d", device->name, i);
  570. /* get module version */
  571. if (at_obj_exec_cmd(device->client, resp, "ATI") != RT_EOK)
  572. {
  573. result = -RT_ERROR;
  574. LOG_E(">> ATI");
  575. goto __exit;
  576. }
  577. for (i = 0; i < (int) resp->line_counts - 1; i++)
  578. {
  579. LOG_D("%s", at_resp_get_line(resp, i + 1));
  580. }
  581. /* check SIM card */
  582. for (i = 0; i < CPIN_RETRY; i++)
  583. {
  584. rt_thread_mdelay(1000);
  585. if (at_obj_exec_cmd(device->client, resp, "AT+CPIN?") == RT_EOK)
  586. {
  587. if (at_resp_get_line_by_kw(resp, "READY") != RT_NULL)
  588. break;
  589. }
  590. }
  591. if (i == CPIN_RETRY)
  592. {
  593. LOG_E("%s device SIM card detection failed.", device->name);
  594. result = -RT_ERROR;
  595. goto __exit;
  596. }
  597. /* check signal strength */
  598. for (i = 0; i < CSQ_RETRY; i++)
  599. {
  600. rt_thread_mdelay(1000);
  601. if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK)
  602. {
  603. int signal_strength = 0, err_rate = 0;
  604. if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ:%d,%d", &signal_strength, &err_rate) > 0)
  605. {
  606. if ((signal_strength != 99) && (signal_strength != 0))
  607. {
  608. LOG_D("%s device signal strength: %d, channel bit error rate: %d",
  609. device->name, signal_strength, err_rate);
  610. break;
  611. }
  612. }
  613. }
  614. }
  615. if (i == CSQ_RETRY)
  616. {
  617. LOG_E("%s device signal strength check failed", device->name);
  618. result = -RT_ERROR;
  619. goto __exit;
  620. }
  621. /* check the GPRS network is registered */
  622. for (i = 0; i < CGREG_RETRY; i++)
  623. {
  624. rt_thread_mdelay(1000);
  625. if (at_obj_exec_cmd(device->client, resp, "AT+CGATT?") == RT_EOK)
  626. {
  627. int link_stat = 0;
  628. if (at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT:%d", &link_stat) > 0)
  629. {
  630. if (link_stat == 1)
  631. {
  632. LOG_D("%s device GPRS is registered", device->name);
  633. break;
  634. }
  635. }
  636. }
  637. }
  638. if (i == CGREG_RETRY)
  639. {
  640. LOG_E("%s device GPRS is register failed", device->name);
  641. result = -RT_ERROR;
  642. goto __exit;
  643. }
  644. /* check the GPRS network IP address */
  645. for (i = 0; i < IPADDR_RETRY; i++)
  646. {
  647. rt_thread_mdelay(1000);
  648. if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR") == RT_EOK)
  649. {
  650. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  651. /* parse response data "+CGPADDR: 0,<IP_address>" */
  652. if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR:%*d,%s", ipaddr) > 0)
  653. {
  654. LOG_D("%s device IP address: %s", device->name, ipaddr);
  655. break;
  656. }
  657. }
  658. }
  659. if (i == IPADDR_RETRY)
  660. {
  661. LOG_E("%s device GPRS is get IP address failed", device->name);
  662. result = -RT_ERROR;
  663. goto __exit;
  664. }
  665. /* initialize successfully */
  666. result = RT_EOK;
  667. break;
  668. __exit:
  669. if (result != RT_EOK)
  670. {
  671. /* power off the bc28 device */
  672. //bc28_power_off(device);
  673. rt_thread_mdelay(1000);
  674. LOG_I("%s device initialize retry...", device->name);
  675. }
  676. }
  677. if (resp)
  678. {
  679. at_delete_resp(resp);
  680. }
  681. if (result == RT_EOK)
  682. {
  683. /* set network interface device status and address information */
  684. bc28_netdev_set_info(device->netdev);
  685. /* check and create link staus sync thread */
  686. if (rt_thread_find(device->netdev->name) == RT_NULL)
  687. {
  688. bc28_netdev_check_link_status(device->netdev);
  689. }
  690. LOG_I("%s device network initialize success.", device->name);
  691. }
  692. else
  693. {
  694. LOG_E("%s device network initialize failed(%d).", device->name, result);
  695. }
  696. }
  697. /* bc28 device network initialize */
  698. static int bc28_net_init(struct at_device *device)
  699. {
  700. #ifdef AT_DEVICE_BC28_INIT_ASYN
  701. rt_thread_t tid;
  702. tid = rt_thread_create("bc28_net", bc28_init_thread_entry, (void *)device,
  703. BC28_THREAD_STACK_SIZE, BC28_THREAD_PRIORITY, 20);
  704. if (tid)
  705. {
  706. rt_thread_startup(tid);
  707. }
  708. else
  709. {
  710. LOG_E("create %s device init thread failed.", device->name);
  711. return -RT_ERROR;
  712. }
  713. #else
  714. bc28_init_thread_entry(device);
  715. #endif /* AT_DEVICE_BC28_INIT_ASYN */
  716. return RT_EOK;
  717. }
  718. static int bc28_init(struct at_device *device)
  719. {
  720. struct at_device_bc28 *bc28 = (struct at_device_bc28 *)device->user_data;
  721. /* configure AT client */
  722. rt_device_t serial = rt_device_find(bc28->client_name);
  723. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  724. config.baud_rate = BC28_AT_CLIENT_BAUD_RATE;
  725. config.data_bits = DATA_BITS_8;
  726. config.stop_bits = STOP_BITS_1;
  727. config.bufsz = bc28->recv_bufsz;
  728. config.parity = PARITY_NONE;
  729. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
  730. rt_device_close(serial);
  731. /* initialize AT client */
  732. #if RT_VER_NUM >= 0x50100
  733. at_client_init(bc28->client_name, bc28->recv_bufsz, bc28->recv_bufsz);
  734. #else
  735. at_client_init(bc28->client_name, bc28->recv_bufsz);
  736. #endif
  737. device->client = at_client_get(bc28->client_name);
  738. if (device->client == RT_NULL)
  739. {
  740. LOG_E("get AT client(%s) failed.", bc28->client_name);
  741. return -RT_ERROR;
  742. }
  743. /* register URC data execution function */
  744. #ifdef AT_USING_SOCKET
  745. bc28_socket_init(device);
  746. #endif
  747. /* add bc28 device to the netdev list */
  748. device->netdev = bc28_netdev_add(bc28->device_name);
  749. if (device->netdev == RT_NULL)
  750. {
  751. LOG_E("add netdev(%s) failed.", bc28->device_name);
  752. return -RT_ERROR;
  753. }
  754. /* initialize bc28 pin configuration */
  755. if (bc28->power_pin != -1)
  756. {
  757. rt_pin_mode(bc28->power_pin, PIN_MODE_OUTPUT);
  758. rt_pin_write(bc28->power_pin, PIN_LOW);
  759. }
  760. /* initialize bc28 device network */
  761. return bc28_netdev_set_up(device->netdev);
  762. }
  763. static int bc28_deinit(struct at_device *device)
  764. {
  765. RT_ASSERT(device);
  766. return bc28_netdev_set_down(device->netdev);
  767. }
  768. static int bc28_control(struct at_device *device, int cmd, void *arg)
  769. {
  770. int result = -RT_ERROR;
  771. RT_ASSERT(device);
  772. switch (cmd)
  773. {
  774. case AT_DEVICE_CTRL_RESET:
  775. result = bc28_reset(device);
  776. break;
  777. case AT_DEVICE_CTRL_SLEEP:
  778. case AT_DEVICE_CTRL_WAKEUP:
  779. case AT_DEVICE_CTRL_POWER_ON:
  780. case AT_DEVICE_CTRL_POWER_OFF:
  781. case AT_DEVICE_CTRL_LOW_POWER:
  782. case AT_DEVICE_CTRL_NET_CONN:
  783. case AT_DEVICE_CTRL_NET_DISCONN:
  784. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  785. case AT_DEVICE_CTRL_GET_SIGNAL:
  786. case AT_DEVICE_CTRL_GET_GPS:
  787. case AT_DEVICE_CTRL_GET_VER:
  788. LOG_W("not support the control command(%d).", cmd);
  789. break;
  790. default:
  791. LOG_E("input error control command(%d).", cmd);
  792. break;
  793. }
  794. return result;
  795. }
  796. const struct at_device_ops bc28_device_ops =
  797. {
  798. bc28_init,
  799. bc28_deinit,
  800. bc28_control,
  801. };
  802. static int bc28_device_class_register(void)
  803. {
  804. struct at_device_class *class = RT_NULL;
  805. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  806. if (class == RT_NULL)
  807. {
  808. LOG_E("no memory for device class create.");
  809. return -RT_ENOMEM;
  810. }
  811. /* fill bc28 device class object */
  812. #ifdef AT_USING_SOCKET
  813. bc28_socket_class_register(class);
  814. #endif
  815. class->device_ops = &bc28_device_ops;
  816. return at_device_class_register(class, AT_DEVICE_CLASS_BC28);
  817. }
  818. INIT_DEVICE_EXPORT(bc28_device_class_register);
  819. #endif /* AT_DEVICE_USING_BC28 */