at_device_bc26.c 27 KB

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