at_device_bc28.c 26 KB

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