at_device_bc28.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. #define LOG_TAG "at.dev.bc28"
  28. #include <at_log.h>
  29. #ifdef AT_DEVICE_USING_BC28
  30. #define BC28_WAIT_CONNECT_TIME 5000
  31. #define BC28_THREAD_STACK_SIZE 2048
  32. #define BC28_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  33. static int bc28_sleep(struct at_device *device)
  34. {
  35. return(RT_EOK);
  36. }
  37. static int bc28_wakeup(struct at_device *device)
  38. {
  39. return(RT_EOK);
  40. }
  41. static int bc28_reset(struct at_device *device)
  42. {
  43. struct at_device_bc28 *bc28 = (struct at_device_bc28 *)device->user_data;
  44. rt_pin_mode(bc28->power_pin, PIN_MODE_OUTPUT);
  45. rt_pin_write(bc28->power_pin, PIN_HIGH);
  46. rt_thread_mdelay(300);
  47. rt_pin_write(bc28->power_pin, PIN_LOW);
  48. bc28->power_status = 1;
  49. return(RT_EOK);
  50. }
  51. static int bc28_check_link_status(struct at_device *device)
  52. {
  53. at_response_t resp = RT_NULL;
  54. struct at_device_bc28 *bc28 = RT_NULL;
  55. int result = -RT_ERROR;
  56. bc28 = (struct at_device_bc28 *)device->user_data;
  57. if ( ! bc28->power_status) // power off
  58. {
  59. LOG_E("the power is off.");
  60. return(-RT_ERROR);
  61. }
  62. if (bc28->sleep_status) // is sleep status
  63. {
  64. if (bc28->power_pin != -1)
  65. {
  66. rt_pin_write(bc28->power_pin, PIN_HIGH);
  67. rt_thread_mdelay(100);
  68. rt_pin_write(bc28->power_pin, PIN_LOW);
  69. rt_thread_mdelay(200);
  70. }
  71. }
  72. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  73. if (resp == RT_NULL)
  74. {
  75. LOG_E("no memory for resp create.");
  76. return(-RT_ERROR);
  77. }
  78. result = -RT_ERROR;
  79. if (at_obj_exec_cmd(device->client, resp, "AT+CGATT?") == RT_EOK)
  80. {
  81. int link_stat = 0;
  82. if (at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT:%d", &link_stat) > 0)
  83. {
  84. if (link_stat == 1)
  85. {
  86. result = RT_EOK;
  87. }
  88. }
  89. }
  90. at_delete_resp(resp);
  91. return(result);
  92. }
  93. /* ============================= bc28 network interface operations ============================= */
  94. /* set bc28 network interface device status and address information */
  95. static int bc28_netdev_set_info(struct netdev *netdev)
  96. {
  97. #define BC28_INFO_RESP_SIZE 128
  98. #define BC28_INFO_RESP_TIMOUT rt_tick_from_millisecond(5000)
  99. int result = RT_EOK;
  100. ip_addr_t addr;
  101. at_response_t resp = RT_NULL;
  102. struct at_device *device = RT_NULL;
  103. RT_ASSERT(netdev);
  104. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  105. if (device == RT_NULL)
  106. {
  107. LOG_E("get device(%s) failed.", netdev->name);
  108. return -RT_ERROR;
  109. }
  110. /* set network interface device status */
  111. netdev_low_level_set_status(netdev, RT_TRUE);
  112. netdev_low_level_set_link_status(netdev, RT_TRUE);
  113. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  114. resp = at_create_resp(BC28_INFO_RESP_SIZE, 0, BC28_INFO_RESP_TIMOUT);
  115. if (resp == RT_NULL)
  116. {
  117. LOG_E("no memory for resp create.");
  118. result = -RT_ENOMEM;
  119. goto __exit;
  120. }
  121. /* set network interface device hardware address(IMEI) */
  122. {
  123. #define BC28_NETDEV_HWADDR_LEN 8
  124. #define BC28_IMEI_LEN 15
  125. char imei[BC28_IMEI_LEN] = {0};
  126. int i = 0, j = 0;
  127. /* send "AT+CGSN=1" commond to get device IMEI */
  128. if (at_obj_exec_cmd(device->client, resp, "AT+CGSN=1") != RT_EOK)
  129. {
  130. result = -RT_ERROR;
  131. goto __exit;
  132. }
  133. if (at_resp_parse_line_args(resp, 2, "+CGSN:%s", imei) <= 0)
  134. {
  135. LOG_E("%s device prase \"AT+CGSN=1\" cmd error.", device->name);
  136. result = -RT_ERROR;
  137. goto __exit;
  138. }
  139. LOG_D("%s device IMEI number: %s", device->name, imei);
  140. netdev->hwaddr_len = BC28_NETDEV_HWADDR_LEN;
  141. /* get hardware address by IMEI */
  142. for (i = 0, j = 0; i < BC28_NETDEV_HWADDR_LEN && j < BC28_IMEI_LEN; i++, j+=2)
  143. {
  144. if (j != BC28_IMEI_LEN - 1)
  145. {
  146. netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
  147. }
  148. else
  149. {
  150. netdev->hwaddr[i] = (imei[j] - '0');
  151. }
  152. }
  153. }
  154. /* set network interface device IP address */
  155. {
  156. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  157. /* send "AT+CGPADDR" commond to get IP address */
  158. if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR") != RT_EOK)
  159. {
  160. result = -RT_ERROR;
  161. goto __exit;
  162. }
  163. /* parse response data "+CGPADDR: 0,<IP_address>" */
  164. if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR:%*d,%s", ipaddr) <= 0)
  165. {
  166. LOG_E("%s device \"AT+CGPADDR\" cmd error.", device->name);
  167. result = -RT_ERROR;
  168. goto __exit;
  169. }
  170. LOG_D("%s device IP address: %s", device->name, ipaddr);
  171. /* set network interface address information */
  172. inet_aton(ipaddr, &addr);
  173. netdev_low_level_set_ipaddr(netdev, &addr);
  174. }
  175. /* set network interface device dns server */
  176. {
  177. #define DNS_ADDR_SIZE_MAX 16
  178. char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
  179. /* send "AT+QIDNSCFG?" commond to get DNS servers address */
  180. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG?") != RT_EOK)
  181. {
  182. result = -RT_ERROR;
  183. goto __exit;
  184. }
  185. /* parse response data "PrimaryDns:<pri_dns>"
  186. * "SecondaryDns:<sec_dns>"
  187. */
  188. if (at_resp_parse_line_args_by_kw(resp, "PrimaryDns:", "PrimaryDns: %s", dns_server1) <= 0)
  189. {
  190. LOG_E("%s device prase \"AT+QIDNSCFG?\" cmd error.", device->name);
  191. result = -RT_ERROR;
  192. goto __exit;
  193. }
  194. if (at_resp_parse_line_args_by_kw(resp, "SecondaryDns:", "SecondaryDns: %s", dns_server2) <= 0)
  195. {
  196. LOG_E("%s device prase \"AT+QIDNSCFG?\" cmd error.", device->name);
  197. result = -RT_ERROR;
  198. goto __exit;
  199. }
  200. LOG_D("%s device primary DNS server address: %s", device->name, dns_server1);
  201. LOG_D("%s device secondary DNS server address: %s", device->name, dns_server2);
  202. inet_aton(dns_server1, &addr);
  203. netdev_low_level_set_dns_server(netdev, 0, &addr);
  204. inet_aton(dns_server2, &addr);
  205. netdev_low_level_set_dns_server(netdev, 1, &addr);
  206. }
  207. __exit:
  208. if (resp)
  209. {
  210. at_delete_resp(resp);
  211. }
  212. return result;
  213. }
  214. static void bc28_check_link_status_entry(void *parameter)
  215. {
  216. #define BC28_LINK_DELAY_TIME (60 * RT_TICK_PER_SECOND)
  217. rt_bool_t is_link_up;
  218. struct at_device *device = RT_NULL;
  219. struct netdev *netdev = (struct netdev *) parameter;
  220. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  221. if (device == RT_NULL)
  222. {
  223. LOG_E("get device(%s) failed.", netdev->name);
  224. return;
  225. }
  226. while (1)
  227. {
  228. is_link_up = (bc28_check_link_status(device) == RT_EOK);
  229. netdev_low_level_set_link_status(netdev, is_link_up);
  230. rt_thread_delay(BC28_LINK_DELAY_TIME);
  231. }
  232. }
  233. static int bc28_netdev_check_link_status(struct netdev *netdev)
  234. {
  235. #define BC28_LINK_THREAD_STACK_SIZE (1024 + 512)
  236. #define BC28_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  237. rt_thread_t tid;
  238. char tname[RT_NAME_MAX] = {0};
  239. RT_ASSERT(netdev);
  240. rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
  241. /* create bc28 link status polling thread */
  242. tid = rt_thread_create(tname, bc28_check_link_status_entry, (void *)netdev,
  243. BC28_LINK_THREAD_STACK_SIZE, BC28_LINK_THREAD_PRIORITY, 20);
  244. if (tid != RT_NULL)
  245. {
  246. rt_thread_startup(tid);
  247. }
  248. return RT_EOK;
  249. }
  250. static int bc28_net_init(struct at_device *device);
  251. static int bc28_netdev_set_up(struct netdev *netdev)
  252. {
  253. struct at_device *device = RT_NULL;
  254. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  255. if (device == RT_NULL)
  256. {
  257. LOG_E("get device(%s) failed.", netdev->name);
  258. return -RT_ERROR;
  259. }
  260. if (device->is_init == RT_FALSE)
  261. {
  262. bc28_net_init(device);
  263. device->is_init = RT_TRUE;
  264. netdev_low_level_set_status(netdev, RT_TRUE);
  265. LOG_D("network interface device(%s) set up status.", netdev->name);
  266. }
  267. return RT_EOK;
  268. }
  269. static int bc28_netdev_set_down(struct netdev *netdev)
  270. {
  271. struct at_device *device = RT_NULL;
  272. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  273. if (device == RT_NULL)
  274. {
  275. LOG_E("get device(%s) failed.", netdev->name);
  276. return -RT_ERROR;
  277. }
  278. if (device->is_init == RT_TRUE)
  279. {
  280. //bc28_power_off(device);
  281. device->is_init = RT_FALSE;
  282. netdev_low_level_set_status(netdev, RT_FALSE);
  283. LOG_D("network interface device(%s) set down status.", netdev->name);
  284. }
  285. return RT_EOK;
  286. }
  287. static int bc28_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  288. {
  289. #define BC28_DNS_RESP_LEN 64
  290. #define BC28_DNS_RESP_TIMEOUT rt_tick_from_millisecond(300)
  291. int result = RT_EOK;
  292. at_response_t resp = RT_NULL;
  293. struct at_device *device = RT_NULL;
  294. RT_ASSERT(netdev);
  295. RT_ASSERT(dns_server);
  296. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  297. if (device == RT_NULL)
  298. {
  299. LOG_E("get device(%s) failed.", netdev->name);
  300. return -RT_ERROR;
  301. }
  302. resp = at_create_resp(BC28_DNS_RESP_LEN, 0, BC28_DNS_RESP_TIMEOUT);
  303. if (resp == RT_NULL)
  304. {
  305. LOG_E("no memory for resp create.");
  306. result = -RT_ENOMEM;
  307. goto __exit;
  308. }
  309. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=%s", inet_ntoa(*dns_server)) != RT_EOK)
  310. {
  311. result = -RT_ERROR;
  312. goto __exit;
  313. }
  314. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  315. __exit:
  316. if (resp)
  317. {
  318. at_delete_resp(resp);
  319. }
  320. return result;
  321. }
  322. #ifdef AT_USING_SOCKET
  323. int bc28_domain_resolve(const char *name, char ip[16]);
  324. #endif
  325. #ifdef NETDEV_USING_PING
  326. static int bc28_netdev_ping(struct netdev *netdev, const char *host,
  327. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
  328. {
  329. #define BC28_PING_RESP_SIZE 128
  330. #define BC28_PING_IP_SIZE 16
  331. #define BC28_PING_TIMEOUT (10 * RT_TICK_PER_SECOND)
  332. rt_err_t result = RT_EOK;
  333. int response = -1, recv_data_len, ping_time, ttl;
  334. char ip_addr[BC28_PING_IP_SIZE] = {0};
  335. at_response_t resp = RT_NULL;
  336. struct at_device *device = RT_NULL;
  337. RT_ASSERT(netdev);
  338. RT_ASSERT(host);
  339. RT_ASSERT(ping_resp);
  340. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  341. if (device == RT_NULL)
  342. {
  343. LOG_E("get device(%s) failed.", netdev->name);
  344. return -RT_ERROR;
  345. }
  346. resp = at_create_resp(BC28_PING_RESP_SIZE, 4, BC28_PING_TIMEOUT);
  347. if (resp == RT_NULL)
  348. {
  349. LOG_E("no memory for resp create");
  350. return -RT_ENOMEM;
  351. }
  352. /* DNS resolve */
  353. struct in_addr inp;
  354. if (inet_aton(host, &inp) > 0)
  355. {
  356. rt_strncpy(ip_addr, host, BC28_PING_IP_SIZE);
  357. }
  358. #ifdef AT_USING_SOCKET
  359. else
  360. {
  361. if(0 > bc28_domain_resolve(host, ip_addr))
  362. {
  363. LOG_E("can not resolve domain");
  364. goto __exit;
  365. }
  366. }
  367. #endif
  368. if (at_obj_exec_cmd(device->client, resp, "AT+NPING=%s,%d,%d",
  369. ip_addr, data_len, timeout*1000/RT_TICK_PER_SECOND) < 0)
  370. {
  371. result = -RT_ERROR;
  372. goto __exit;
  373. }
  374. if (at_resp_parse_line_args_by_kw(resp, "+NPINGERR:", "+NPINGERR:%d", &response) > 0)
  375. {
  376. switch (response)
  377. {
  378. case 1:
  379. result = -RT_ETIMEOUT;
  380. break;
  381. case 2:
  382. default:
  383. result = -RT_ERROR;
  384. break;
  385. }
  386. }
  387. else if (at_resp_parse_line_args_by_kw(resp, "+NPING:", "+NPING:%[^,],%d,%d",
  388. ip_addr, &ttl, &ping_time) > 0 )
  389. {
  390. inet_aton(ip_addr, &(ping_resp->ip_addr));
  391. ping_resp->data_len = data_len;
  392. ping_resp->ticks = rt_tick_from_millisecond(ping_time);
  393. ping_resp->ttl = ttl;
  394. result = RT_EOK;
  395. }
  396. else
  397. {
  398. result = -RT_ERROR;
  399. goto __exit;
  400. }
  401. __exit:
  402. if (resp)
  403. {
  404. at_delete_resp(resp);
  405. }
  406. return result;
  407. }
  408. #endif /* NETDEV_USING_PING */
  409. const struct netdev_ops bc28_netdev_ops =
  410. {
  411. bc28_netdev_set_up,
  412. bc28_netdev_set_down,
  413. RT_NULL,
  414. bc28_netdev_set_dns_server,
  415. RT_NULL,
  416. #ifdef NETDEV_USING_PING
  417. bc28_netdev_ping,
  418. #endif
  419. RT_NULL,
  420. };
  421. static struct netdev *bc28_netdev_add(const char *netdev_name)
  422. {
  423. #define ETHERNET_MTU 1500
  424. #define HWADDR_LEN 8
  425. struct netdev *netdev = RT_NULL;
  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. at_client_init(bc28->client_name, bc28->recv_bufsz);
  733. device->client = at_client_get(bc28->client_name);
  734. if (device->client == RT_NULL)
  735. {
  736. LOG_E("get AT client(%s) failed.", bc28->client_name);
  737. return -RT_ERROR;
  738. }
  739. /* register URC data execution function */
  740. #ifdef AT_USING_SOCKET
  741. bc28_socket_init(device);
  742. #endif
  743. /* add bc28 device to the netdev list */
  744. device->netdev = bc28_netdev_add(bc28->device_name);
  745. if (device->netdev == RT_NULL)
  746. {
  747. LOG_E("add netdev(%s) failed.", bc28->device_name);
  748. return -RT_ERROR;
  749. }
  750. /* initialize bc28 pin configuration */
  751. if (bc28->power_pin != -1)
  752. {
  753. rt_pin_mode(bc28->power_pin, PIN_MODE_OUTPUT);
  754. rt_pin_write(bc28->power_pin, PIN_LOW);
  755. }
  756. /* initialize bc28 device network */
  757. return bc28_netdev_set_up(device->netdev);
  758. }
  759. static int bc28_deinit(struct at_device *device)
  760. {
  761. RT_ASSERT(device);
  762. return bc28_netdev_set_down(device->netdev);
  763. }
  764. static int bc28_control(struct at_device *device, int cmd, void *arg)
  765. {
  766. int result = -RT_ERROR;
  767. RT_ASSERT(device);
  768. switch (cmd)
  769. {
  770. case AT_DEVICE_CTRL_RESET:
  771. result = bc28_reset(device);
  772. break;
  773. case AT_DEVICE_CTRL_SLEEP:
  774. case AT_DEVICE_CTRL_WAKEUP:
  775. case AT_DEVICE_CTRL_POWER_ON:
  776. case AT_DEVICE_CTRL_POWER_OFF:
  777. case AT_DEVICE_CTRL_LOW_POWER:
  778. case AT_DEVICE_CTRL_NET_CONN:
  779. case AT_DEVICE_CTRL_NET_DISCONN:
  780. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  781. case AT_DEVICE_CTRL_GET_SIGNAL:
  782. case AT_DEVICE_CTRL_GET_GPS:
  783. case AT_DEVICE_CTRL_GET_VER:
  784. LOG_W("not support the control command(%d).", cmd);
  785. break;
  786. default:
  787. LOG_E("input error control command(%d).", cmd);
  788. break;
  789. }
  790. return result;
  791. }
  792. const struct at_device_ops bc28_device_ops =
  793. {
  794. bc28_init,
  795. bc28_deinit,
  796. bc28_control,
  797. };
  798. static int bc28_device_class_register(void)
  799. {
  800. struct at_device_class *class = RT_NULL;
  801. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  802. if (class == RT_NULL)
  803. {
  804. LOG_E("no memory for device class create.");
  805. return -RT_ENOMEM;
  806. }
  807. /* fill bc28 device class object */
  808. #ifdef AT_USING_SOCKET
  809. bc28_socket_class_register(class);
  810. #endif
  811. class->device_ops = &bc28_device_ops;
  812. return at_device_class_register(class, AT_DEVICE_CLASS_BC28);
  813. }
  814. INIT_DEVICE_EXPORT(bc28_device_class_register);
  815. #endif /* AT_DEVICE_USING_BC28 */