at_device_bc26.c 27 KB

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