at_device_ec200x.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-12-13 qiyongzhong first version
  9. * 2023-06-03 CX support netstat command
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <at_device_ec200x.h>
  14. #define LOG_TAG "at.dev.ec200x"
  15. #include <at_log.h>
  16. #ifdef AT_DEVICE_USING_EC200X
  17. #define EC200X_WAIT_CONNECT_TIME 10000
  18. #define EC200X_THREAD_STACK_SIZE 2048
  19. #define EC200X_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  20. static int ec200x_power_on(struct at_device *device)
  21. {
  22. struct at_device_ec200x *ec200x = RT_NULL;
  23. ec200x = (struct at_device_ec200x *)device->user_data;
  24. if (ec200x->power_ctrl)
  25. {
  26. (ec200x->power_ctrl)(1);
  27. rt_thread_mdelay(2000);
  28. }
  29. if (ec200x->power_pin == -1)//no power on pin
  30. {
  31. ec200x->power_status = RT_TRUE;
  32. return(RT_EOK);
  33. }
  34. if (ec200x->power_status_pin != -1)//use power status pin
  35. {
  36. ec200x->power_status = rt_pin_read(ec200x->power_status_pin);//read power status
  37. }
  38. if (ec200x->power_status)//power is on
  39. {
  40. return(RT_EOK);
  41. }
  42. rt_pin_write(ec200x->power_pin, PIN_HIGH);
  43. if (ec200x->power_status_pin != -1)//use power status pin
  44. {
  45. while (rt_pin_read(ec200x->power_status_pin) == PIN_LOW)
  46. {
  47. rt_thread_mdelay(10);
  48. }
  49. }
  50. rt_thread_mdelay(500);
  51. rt_pin_write(ec200x->power_pin, PIN_LOW);
  52. ec200x->power_status = RT_TRUE;
  53. return(RT_EOK);
  54. }
  55. static int ec200x_power_off(struct at_device *device)
  56. {
  57. struct at_device_ec200x *ec200x = RT_NULL;
  58. ec200x = (struct at_device_ec200x *)device->user_data;
  59. if (ec200x->power_ctrl)
  60. {
  61. ec200x->power_status = RT_FALSE;
  62. (ec200x->power_ctrl)(0);
  63. rt_thread_mdelay(2*1000);
  64. return(RT_EOK);
  65. }
  66. if (ec200x->power_pin == -1)//no power on pin
  67. {
  68. return(RT_EOK);
  69. }
  70. if (ec200x->power_status_pin != -1)//use power status pin
  71. {
  72. ec200x->power_status = rt_pin_read(ec200x->power_status_pin);//read power status
  73. }
  74. if ( ! ec200x->power_status)//power is off
  75. {
  76. return(RT_EOK);
  77. }
  78. if (ec200x->power_status_pin != -1)//use power status pin
  79. {
  80. rt_pin_write(ec200x->power_pin, PIN_HIGH);
  81. rt_thread_mdelay(1000);
  82. rt_pin_write(ec200x->power_pin, PIN_LOW);
  83. while (rt_pin_read(ec200x->power_status_pin) == PIN_HIGH)//wait power down
  84. {
  85. rt_thread_mdelay(100);
  86. }
  87. }
  88. else
  89. {
  90. at_obj_exec_cmd(device->client, RT_NULL, "AT+QPOWD=0");
  91. rt_thread_mdelay(5*1000);
  92. }
  93. ec200x->power_status = RT_FALSE;
  94. return(RT_EOK);
  95. }
  96. static int ec200x_sleep(struct at_device *device)
  97. {
  98. //at_response_t resp = RT_NULL;
  99. struct at_device_ec200x *ec200x = RT_NULL;
  100. ec200x = (struct at_device_ec200x *)device->user_data;
  101. if ( ! ec200x->power_status)//power off
  102. {
  103. return(RT_EOK);
  104. }
  105. if (ec200x->sleep_status)//is sleep status
  106. {
  107. return(RT_EOK);
  108. }
  109. if (ec200x->wakeup_pin == -1)//use wakeup pin
  110. {
  111. LOG_E("no config wakeup pin, can not entry into sleep mode.");
  112. return(-RT_ERROR);
  113. }
  114. /*
  115. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  116. if (resp == RT_NULL)
  117. {
  118. LOG_D("no memory for resp create.");
  119. return(-RT_ERROR);
  120. }
  121. if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=1") != RT_EOK)//enable sleep mode
  122. {
  123. LOG_D("enable sleep fail.\"AT+QSCLK=1\" execute fail.");
  124. at_delete_resp(resp);
  125. return(-RT_ERROR);
  126. }
  127. at_delete_resp(resp);
  128. */
  129. rt_pin_write(ec200x->wakeup_pin, PIN_HIGH);
  130. ec200x->sleep_status = RT_TRUE;
  131. return(RT_EOK);
  132. }
  133. static int ec200x_wakeup(struct at_device *device)
  134. {
  135. //at_response_t resp = RT_NULL;
  136. struct at_device_ec200x *ec200x = RT_NULL;
  137. ec200x = (struct at_device_ec200x *)device->user_data;
  138. if ( ! ec200x->power_status)//power off
  139. {
  140. LOG_E("the power is off and the wake-up cannot be performed");
  141. return(-RT_ERROR);
  142. }
  143. if ( ! ec200x->sleep_status)//no sleep status
  144. {
  145. return(RT_EOK);
  146. }
  147. rt_pin_write(ec200x->wakeup_pin, PIN_LOW);
  148. rt_thread_mdelay(200);
  149. /*
  150. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  151. if (resp == RT_NULL)
  152. {
  153. LOG_D("no memory for resp create.");
  154. return(-RT_ERROR);
  155. }
  156. if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=0") != RT_EOK)//disable sleep mode
  157. {
  158. LOG_D("wake up fail. \"AT+QSCLK=0\" execute fail.");
  159. at_delete_resp(resp);
  160. return(-RT_ERROR);
  161. }
  162. at_delete_resp(resp);
  163. */
  164. ec200x->sleep_status = RT_FALSE;
  165. return(RT_EOK);
  166. }
  167. static int ec200x_check_link_status(struct at_device *device)
  168. {
  169. at_response_t resp = RT_NULL;
  170. struct at_device_ec200x *ec200x = RT_NULL;
  171. int result = -RT_ERROR;
  172. ec200x = (struct at_device_ec200x *)device->user_data;
  173. if ( ! ec200x->power_status)//power off
  174. {
  175. LOG_D("the power is off.");
  176. return(-RT_ERROR);
  177. }
  178. if (ec200x->sleep_status)//is sleep status
  179. {
  180. rt_pin_write(ec200x->wakeup_pin, PIN_LOW);
  181. rt_thread_mdelay(200);
  182. }
  183. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  184. if (resp == RT_NULL)
  185. {
  186. LOG_D("no memory for resp create.");
  187. return(-RT_ERROR);
  188. }
  189. result = -RT_ERROR;
  190. if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") == RT_EOK)
  191. {
  192. int link_stat = 0;
  193. if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
  194. {
  195. if (link_stat == 1 || link_stat == 5)
  196. {
  197. result = RT_EOK;
  198. }
  199. }
  200. }
  201. at_delete_resp(resp);
  202. if (ec200x->sleep_status)//is sleep status
  203. {
  204. rt_pin_write(ec200x->wakeup_pin, PIN_HIGH);
  205. }
  206. return(result);
  207. }
  208. static int ec200x_read_rssi(struct at_device *device)
  209. {
  210. int result = -RT_ERROR;
  211. at_response_t resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  212. if (resp == RT_NULL)
  213. {
  214. LOG_D("no memory for resp create.");
  215. return(result);
  216. }
  217. if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK)
  218. {
  219. int rssi = 0;
  220. if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d", &rssi) > 0)
  221. {
  222. struct at_device_ec200x *ec200x = (struct at_device_ec200x *)device->user_data;
  223. if (rssi < 99)
  224. {
  225. ec200x->rssi = rssi * 2 - 113;
  226. }
  227. else if(rssi >= 100 && rssi < 199)
  228. {
  229. ec200x->rssi = rssi - 216;
  230. }
  231. result = RT_EOK;
  232. }
  233. }
  234. at_delete_resp(resp);
  235. return(result);
  236. }
  237. /* ============================= ec200x network interface operations ============================= */
  238. /* set ec200x network interface device status and address information */
  239. static int ec200x_netdev_set_info(struct netdev *netdev)
  240. {
  241. #define EC200X_INFO_RESP_SIZE 256
  242. #define EC200X_INFO_RESP_TIMO rt_tick_from_millisecond(1000)
  243. int result = RT_EOK;
  244. ip_addr_t addr;
  245. at_response_t resp = RT_NULL;
  246. struct at_device *device = RT_NULL;
  247. RT_ASSERT(netdev);
  248. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  249. if (device == RT_NULL)
  250. {
  251. LOG_E("get device(%s) failed.", netdev->name);
  252. return -RT_ERROR;
  253. }
  254. /* set network interface device status */
  255. netdev_low_level_set_status(netdev, RT_TRUE);
  256. netdev_low_level_set_link_status(netdev, RT_TRUE);
  257. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  258. resp = at_create_resp(EC200X_INFO_RESP_SIZE, 0, EC200X_INFO_RESP_TIMO);
  259. if (resp == RT_NULL)
  260. {
  261. LOG_E("no memory for resp create.");
  262. result = -RT_ENOMEM;
  263. goto __exit;
  264. }
  265. /* set network interface device hardware address(IMEI) */
  266. {
  267. #define EC200X_NETDEV_HWADDR_LEN 8
  268. #define EC200X_IMEI_LEN 15
  269. char imei[32] = {0};
  270. int i = 0, j = 0;
  271. /* send "AT+GSN" commond to get device IMEI */
  272. if (at_obj_exec_cmd(device->client, resp, "AT+GSN") != RT_EOK)
  273. {
  274. result = -RT_ERROR;
  275. goto __exit;
  276. }
  277. if (at_resp_parse_line_args(resp, 2, "%s", imei) <= 0)
  278. {
  279. LOG_E("%s device prase \"AT+GSN\" cmd error.", device->name);
  280. result = -RT_ERROR;
  281. goto __exit;
  282. }
  283. LOG_D("%s device IMEI number: %s", device->name, imei);
  284. netdev->hwaddr_len = EC200X_NETDEV_HWADDR_LEN;
  285. /* get hardware address by IMEI */
  286. for (i = 0, j = 0; i < EC200X_NETDEV_HWADDR_LEN && j < EC200X_IMEI_LEN; i++, j+=2)
  287. {
  288. if (j != EC200X_IMEI_LEN - 1)
  289. {
  290. netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
  291. }
  292. else
  293. {
  294. netdev->hwaddr[i] = (imei[j] - '0');
  295. }
  296. }
  297. }
  298. /* read number of SIM card */
  299. {
  300. if (at_obj_exec_cmd(device->client, resp, "AT+QCCID") == RT_EOK)
  301. {
  302. char str[32];
  303. if (at_resp_parse_line_args_by_kw(resp, "+QCCID:", "+QCCID:%s\r", str) > 0)
  304. {
  305. LOG_D("QCCID of SIM card : %s", str);
  306. }
  307. }
  308. }
  309. /* set network interface device IP address */
  310. {
  311. #define IP_ADDR_SIZE_MAX 128
  312. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  313. /* send "AT+CGPADDR=1" commond to get IP address */
  314. if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") != RT_EOK)
  315. {
  316. result = -RT_ERROR;
  317. goto __exit;
  318. }
  319. /* parse response data "+CGPADDR: 1,<IP_address>" */
  320. if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*[^\"]\"%[^\"]", ipaddr) <= 0)
  321. {
  322. LOG_E("%s device \"AT+CGPADDR=1\" cmd error.", device->name);
  323. result = -RT_ERROR;
  324. goto __exit;
  325. }
  326. LOG_D("%s device IP address: %s", device->name, ipaddr);
  327. /* set network interface address information */
  328. inet_aton(ipaddr, &addr);
  329. netdev_low_level_set_ipaddr(netdev, &addr);
  330. }
  331. /* set network interface device dns server */
  332. {
  333. #define DNS_ADDR_SIZE_MAX 16
  334. char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
  335. /* send "AT+QIDNSCFG=1" commond to get DNS servers address */
  336. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=1") != RT_EOK)
  337. {
  338. result = -RT_ERROR;
  339. goto __exit;
  340. }
  341. /* parse response data "+QIDNSCFG: <contextID>,<pridnsaddr>,<secdnsaddr>" */
  342. if (at_resp_parse_line_args_by_kw(resp, "+QIDNSCFG:", "+QIDNSCFG: 1,\"%[^\"]\",\"%[^\"]\"",
  343. dns_server1, dns_server2) <= 0)
  344. {
  345. LOG_E("%s device prase \"AT+QIDNSCFG=1\" cmd error.", device->name);
  346. result = -RT_ERROR;
  347. goto __exit;
  348. }
  349. LOG_D("%s device primary DNS server address: %s", device->name, dns_server1);
  350. LOG_D("%s device secondary DNS server address: %s", device->name, dns_server2);
  351. inet_aton(dns_server1, &addr);
  352. netdev_low_level_set_dns_server(netdev, 0, &addr);
  353. inet_aton(dns_server2, &addr);
  354. netdev_low_level_set_dns_server(netdev, 1, &addr);
  355. }
  356. __exit:
  357. if (resp)
  358. {
  359. at_delete_resp(resp);
  360. }
  361. return result;
  362. }
  363. static void ec200x_check_link_status_entry(void *parameter)
  364. {
  365. #define EC200X_LINK_DELAY_TIME (60 * RT_TICK_PER_SECOND)
  366. rt_bool_t is_link_up;
  367. struct at_device *device = RT_NULL;
  368. struct netdev *netdev = (struct netdev *) parameter;
  369. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  370. if (device == RT_NULL)
  371. {
  372. LOG_E("get device(%s) failed.", netdev->name);
  373. return;
  374. }
  375. while (1)
  376. {
  377. ec200x_read_rssi(device);
  378. rt_thread_delay(EC200X_LINK_DELAY_TIME);
  379. is_link_up = (ec200x_check_link_status(device) == RT_EOK);
  380. netdev_low_level_set_link_status(netdev, is_link_up);
  381. }
  382. }
  383. static int ec200x_netdev_check_link_status(struct netdev *netdev)
  384. {
  385. #define EC200X_LINK_THREAD_TICK 20
  386. #define EC200X_LINK_THREAD_STACK_SIZE (1024 + 512)
  387. #define EC200X_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  388. rt_thread_t tid;
  389. char tname[RT_NAME_MAX] = {0};
  390. RT_ASSERT(netdev);
  391. rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
  392. /* create ec200x link status polling thread */
  393. tid = rt_thread_create(tname, ec200x_check_link_status_entry, (void *)netdev,
  394. EC200X_LINK_THREAD_STACK_SIZE, EC200X_LINK_THREAD_PRIORITY, EC200X_LINK_THREAD_TICK);
  395. if (tid != RT_NULL)
  396. {
  397. rt_thread_startup(tid);
  398. }
  399. return RT_EOK;
  400. }
  401. static int ec200x_net_init(struct at_device *device);
  402. static int ec200x_netdev_set_up(struct netdev *netdev)
  403. {
  404. struct at_device *device = RT_NULL;
  405. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  406. if (device == RT_NULL)
  407. {
  408. LOG_E("get device(%s) failed.", netdev->name);
  409. return -RT_ERROR;
  410. }
  411. if (device->is_init == RT_FALSE)
  412. {
  413. ec200x_net_init(device);
  414. device->is_init = RT_TRUE;
  415. netdev_low_level_set_status(netdev, RT_TRUE);
  416. LOG_D("network interface device(%s) set up status.", netdev->name);
  417. }
  418. return RT_EOK;
  419. }
  420. static int ec200x_netdev_set_down(struct netdev *netdev)
  421. {
  422. struct at_device *device = RT_NULL;
  423. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  424. if (device == RT_NULL)
  425. {
  426. LOG_E("get device(%s) failed.", netdev->name);
  427. return -RT_ERROR;
  428. }
  429. if (device->is_init == RT_TRUE)
  430. {
  431. ec200x_power_off(device);
  432. device->is_init = RT_FALSE;
  433. netdev_low_level_set_status(netdev, RT_FALSE);
  434. LOG_D("network interface device(%s) set down status.", netdev->name);
  435. }
  436. return RT_EOK;
  437. }
  438. static int ec200x_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  439. {
  440. #define EC200X_DNS_RESP_LEN 64
  441. #define EC200X_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
  442. int result = RT_EOK;
  443. at_response_t resp = RT_NULL;
  444. struct at_device *device = RT_NULL;
  445. RT_ASSERT(netdev);
  446. RT_ASSERT(dns_server);
  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(EC200X_DNS_RESP_LEN, 0, EC200X_DNS_RESP_TIMEO);
  454. if (resp == RT_NULL)
  455. {
  456. LOG_D("no memory for resp create.");
  457. result = -RT_ENOMEM;
  458. goto __exit;
  459. }
  460. /* send "AT+QIDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
  461. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=%d,%s",
  462. dns_num, inet_ntoa(*dns_server)) != RT_EOK)
  463. {
  464. result = -RT_ERROR;
  465. goto __exit;
  466. }
  467. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  468. __exit:
  469. if (resp)
  470. {
  471. at_delete_resp(resp);
  472. }
  473. return result;
  474. }
  475. #ifdef NETDEV_USING_PING
  476. static int ec200x_netdev_ping(struct netdev *netdev, const char *host,
  477. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
  478. #if RT_VER_NUM >= 0x50100
  479. , rt_bool_t is_bind
  480. #endif
  481. )
  482. {
  483. #define EC200X_PING_RESP_SIZE 128
  484. #define EC200X_PING_IP_SIZE 16
  485. #define EC200X_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  486. rt_err_t result = RT_EOK;
  487. int response = -1, recv_data_len, ping_time, ttl;
  488. char ip_addr[EC200X_PING_IP_SIZE] = {0};
  489. at_response_t resp = RT_NULL;
  490. struct at_device *device = RT_NULL;
  491. #if RT_VER_NUM >= 0x50100
  492. RT_UNUSED(is_bind);
  493. #endif
  494. RT_ASSERT(netdev);
  495. RT_ASSERT(host);
  496. RT_ASSERT(ping_resp);
  497. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  498. if (device == RT_NULL)
  499. {
  500. LOG_E("get device(%s) failed.", netdev->name);
  501. return -RT_ERROR;
  502. }
  503. resp = at_create_resp(EC200X_PING_RESP_SIZE, 4, EC200X_PING_TIMEO);
  504. if (resp == RT_NULL)
  505. {
  506. LOG_E("no memory for resp create");
  507. return -RT_ENOMEM;
  508. }
  509. /* send "AT+QPING=<contextID>"<host>"[,[<timeout>][,<pingnum>]]" commond to send ping request */
  510. if (at_obj_exec_cmd(device->client, resp, "AT+QPING=1,%s,%d,1", host, timeout / RT_TICK_PER_SECOND) < 0)
  511. {
  512. result = -RT_ERROR;
  513. goto __exit;
  514. }
  515. at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d", &response);
  516. /* Received the ping response from the server */
  517. if (response == 0)
  518. {
  519. if (at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d,\"%[^\"]\",%d,%d,%d",
  520. &response, ip_addr, &recv_data_len, &ping_time, &ttl) <= 0)
  521. {
  522. result = -RT_ERROR;
  523. goto __exit;
  524. }
  525. }
  526. /* prase response number */
  527. switch (response)
  528. {
  529. case 0:
  530. inet_aton(ip_addr, &(ping_resp->ip_addr));
  531. ping_resp->data_len = recv_data_len;
  532. ping_resp->ticks = ping_time;
  533. ping_resp->ttl = ttl;
  534. result = RT_EOK;
  535. break;
  536. case 569:
  537. result = -RT_ETIMEOUT;
  538. break;
  539. default:
  540. result = -RT_ERROR;
  541. break;
  542. }
  543. __exit:
  544. if (resp)
  545. {
  546. at_delete_resp(resp);
  547. }
  548. return result;
  549. }
  550. #endif /* NETDEV_USING_PING */
  551. #ifdef NETDEV_USING_NETSTAT
  552. void ec200x_netdev_netstat(struct netdev *netdev)
  553. {
  554. #define EC200X_NETSTAT_RESP_SIZE 320
  555. #define EC200X_NETSTAT_TYPE_SIZE 4
  556. #define EC200X_NETSTAT_IPADDR_SIZE 17
  557. #define EC200X_NETSTAT_EXPRESSION "+QISTATE:%*d,\"%[^\"]\",\"%[^\"]\",%d,%*d,%d"
  558. at_response_t resp = RT_NULL;
  559. struct at_device *device = RT_NULL;
  560. int remote_port, link_sta, i;
  561. char *type = RT_NULL;
  562. char *ipaddr = RT_NULL;
  563. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  564. if (device == RT_NULL)
  565. {
  566. LOG_E("get device(%s) failed.", netdev->name);
  567. return;
  568. }
  569. type = (char *) rt_calloc(1, EC200X_NETSTAT_TYPE_SIZE);
  570. ipaddr = (char *) rt_calloc(1, EC200X_NETSTAT_IPADDR_SIZE);
  571. if ((type && ipaddr) == RT_NULL)
  572. {
  573. LOG_E("no memory for ipaddr create.");
  574. goto __exit;
  575. }
  576. resp = at_create_resp(EC200X_NETSTAT_RESP_SIZE, 0, 5 * RT_TICK_PER_SECOND);
  577. if (resp == RT_NULL)
  578. {
  579. LOG_E("no memory for resp create.", device->name);
  580. goto __exit;
  581. }
  582. /* send network connection information commond "AT+QISTATE?" and wait response */
  583. if (at_obj_exec_cmd(device->client, resp, "AT+QISTATE?") < 0)
  584. {
  585. goto __exit;
  586. }
  587. for (i = 1; i <= resp->line_counts; i++)
  588. {
  589. if (strstr(at_resp_get_line(resp, i), "+QISTATE:"))
  590. {
  591. /* parse the third line of response data, get the network connection information */
  592. if (at_resp_parse_line_args(resp, i, EC200X_NETSTAT_EXPRESSION, type, ipaddr, &remote_port, &link_sta) < 0)
  593. {
  594. goto __exit;
  595. }
  596. else
  597. {
  598. /* link_sta==2?"LINK_INTNET_UP":"LINK_INTNET_DOWN" */
  599. LOG_RAW("%s: %s ==> %s:%d\n", type, inet_ntoa(netdev->ip_addr), ipaddr, remote_port);
  600. }
  601. }
  602. }
  603. __exit:
  604. if (resp)
  605. {
  606. at_delete_resp(resp);
  607. }
  608. if (type)
  609. {
  610. rt_free(type);
  611. }
  612. if (ipaddr)
  613. {
  614. rt_free(ipaddr);
  615. }
  616. }
  617. #endif
  618. const struct netdev_ops ec200x_netdev_ops =
  619. {
  620. ec200x_netdev_set_up,
  621. ec200x_netdev_set_down,
  622. RT_NULL,
  623. ec200x_netdev_set_dns_server,
  624. RT_NULL,
  625. #ifdef NETDEV_USING_PING
  626. ec200x_netdev_ping,
  627. #endif
  628. #ifdef NETDEV_USING_NETSTAT
  629. ec200x_netdev_netstat,
  630. #endif
  631. };
  632. static struct netdev *ec200x_netdev_add(const char *netdev_name)
  633. {
  634. #define ETHERNET_MTU 1500
  635. #define HWADDR_LEN 8
  636. struct netdev *netdev = RT_NULL;
  637. netdev = netdev_get_by_name(netdev_name);
  638. if(netdev != RT_NULL)
  639. {
  640. return netdev;
  641. }
  642. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  643. if (netdev == RT_NULL)
  644. {
  645. LOG_E("no memory for netdev create.");
  646. return RT_NULL;
  647. }
  648. netdev->mtu = ETHERNET_MTU;
  649. netdev->ops = &ec200x_netdev_ops;
  650. netdev->hwaddr_len = HWADDR_LEN;
  651. #ifdef SAL_USING_AT
  652. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  653. /* set the network interface socket/netdb operations */
  654. sal_at_netdev_set_pf_info(netdev);
  655. #endif
  656. netdev_register(netdev, netdev_name, RT_NULL);
  657. return netdev;
  658. }
  659. /* ============================= ec200x device operations ============================= */
  660. /* initialize for ec200x */
  661. static void ec200x_init_thread_entry(void *parameter)
  662. {
  663. #define RESP_SIZE 128
  664. #define INIT_RETRY 5
  665. #define CPIN_RETRY 10
  666. #define CSQ_RETRY 20
  667. #define CGREG_RETRY 50
  668. #define IPADDR_RETRY 10
  669. int i;
  670. int retry_num = INIT_RETRY;
  671. rt_err_t result = RT_EOK;
  672. at_response_t resp = RT_NULL;
  673. struct at_device *device = (struct at_device *) parameter;
  674. struct at_client *client = device->client;
  675. resp = at_create_resp(RESP_SIZE, 0, rt_tick_from_millisecond(300));
  676. if (resp == RT_NULL)
  677. {
  678. LOG_E("no memory for resp create.");
  679. return;
  680. }
  681. LOG_D("start init %s device.", device->name);
  682. while (retry_num--)
  683. {
  684. /* power on the ec200x device */
  685. ec200x_power_on(device);
  686. rt_thread_mdelay(1000);
  687. /* wait ec200x startup finish, send AT every 500ms, if receive OK, SYNC success*/
  688. if (at_client_obj_wait_connect(client, EC200X_WAIT_CONNECT_TIME))
  689. {
  690. result = -RT_ETIMEOUT;
  691. goto __exit;
  692. }
  693. /* disable echo */
  694. if (at_obj_exec_cmd(device->client, resp, "ATE0") != RT_EOK)
  695. {
  696. result = -RT_ERROR;
  697. goto __exit;
  698. }
  699. /* Get the baudrate */
  700. if (at_obj_exec_cmd(device->client, resp, "AT+IPR?") != RT_EOK)
  701. {
  702. result = -RT_ERROR;
  703. goto __exit;
  704. }
  705. at_resp_parse_line_args_by_kw(resp, "+IPR:", "+IPR: %d", &i);
  706. LOG_D("%s device baudrate %d", device->name, i);
  707. /* get module version */
  708. if (at_obj_exec_cmd(device->client, resp, "ATI") != RT_EOK)
  709. {
  710. result = -RT_ERROR;
  711. goto __exit;
  712. }
  713. for (i = 0; i < (int) resp->line_counts - 1; i++)
  714. {
  715. LOG_D("%s", at_resp_get_line(resp, i + 1));
  716. }
  717. /* check SIM card */
  718. for (i = 0; i < CPIN_RETRY; i++)
  719. {
  720. rt_thread_mdelay(1000);
  721. if (at_obj_exec_cmd(device->client, resp, "AT+CPIN?") == RT_EOK)
  722. {
  723. if (at_resp_get_line_by_kw(resp, "READY") != RT_NULL)
  724. break;
  725. }
  726. }
  727. if (i == CPIN_RETRY)
  728. {
  729. LOG_E("%s device SIM card detection failed.", device->name);
  730. result = -RT_ERROR;
  731. goto __exit;
  732. }
  733. /* check signal strength */
  734. for (i = 0; i < CSQ_RETRY; i++)
  735. {
  736. rt_thread_mdelay(1000);
  737. if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK)
  738. {
  739. int signal_strength = 0, err_rate = 0;
  740. if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &signal_strength, &err_rate) > 0)
  741. {
  742. if ((signal_strength != 99) && (signal_strength != 0))
  743. {
  744. LOG_D("%s device signal strength: %d, channel bit error rate: %d",
  745. device->name, signal_strength, err_rate);
  746. break;
  747. }
  748. }
  749. }
  750. }
  751. if (i == CSQ_RETRY)
  752. {
  753. LOG_E("%s device signal strength check failed", device->name);
  754. result = -RT_ERROR;
  755. goto __exit;
  756. }
  757. /* check the GPRS network is registered */
  758. for (i = 0; i < CGREG_RETRY; i++)
  759. {
  760. rt_thread_mdelay(1000);
  761. if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") == RT_EOK)
  762. {
  763. int link_stat = 0;
  764. if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
  765. {
  766. if ((link_stat == 1) || (link_stat == 5))
  767. {
  768. LOG_D("%s device GPRS is registered", device->name);
  769. break;
  770. }
  771. }
  772. }
  773. }
  774. if (i == CGREG_RETRY)
  775. {
  776. LOG_E("%s device GPRS is register failed", device->name);
  777. result = -RT_ERROR;
  778. goto __exit;
  779. }
  780. if (((struct at_device_ec200x *)(device->user_data))->wakeup_pin != -1)//use wakeup pin
  781. {
  782. if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=1") != RT_EOK)// enable sleep mode fail
  783. {
  784. result = -RT_ERROR;
  785. goto __exit;
  786. }
  787. }
  788. /* Close Echo the Data */
  789. if (at_obj_exec_cmd(device->client, resp, "AT+QISDE=0") != RT_EOK)
  790. {
  791. result = -RT_ERROR;
  792. goto __exit;
  793. }
  794. /* Deactivate context profile */
  795. resp = at_resp_set_info(resp, RESP_SIZE, 0, rt_tick_from_millisecond(40*1000));
  796. if (at_obj_exec_cmd(device->client, resp, "AT+QIDEACT=1") != RT_EOK)
  797. {
  798. result = -RT_ERROR;
  799. goto __exit;
  800. }
  801. /* Activate context profile */
  802. resp = at_resp_set_info(resp, RESP_SIZE, 0, rt_tick_from_millisecond(150*1000));
  803. if (at_obj_exec_cmd(device->client, resp, "AT+QIACT=1") != RT_EOK)
  804. {
  805. result = -RT_ERROR;
  806. goto __exit;
  807. }
  808. /* initialize successfully */
  809. result = RT_EOK;
  810. break;
  811. __exit:
  812. if (result != RT_EOK)
  813. {
  814. /* power off the ec200x device */
  815. ec200x_power_off(device);
  816. rt_thread_mdelay(3000);
  817. LOG_I("%s device initialize retry...", device->name);
  818. }
  819. }
  820. if (resp)
  821. {
  822. at_delete_resp(resp);
  823. }
  824. if (result == RT_EOK)
  825. {
  826. /* set network interface device status and address information */
  827. ec200x_netdev_set_info(device->netdev);
  828. /* check and create link staus sync thread */
  829. if (rt_thread_find(device->netdev->name) == RT_NULL)
  830. {
  831. ec200x_netdev_check_link_status(device->netdev);
  832. }
  833. LOG_I("%s device network initialize success.", device->name);
  834. }
  835. else
  836. {
  837. LOG_E("%s device network initialize failed(%d).", device->name, result);
  838. }
  839. }
  840. /* ec200x device network initialize */
  841. static int ec200x_net_init(struct at_device *device)
  842. {
  843. #ifdef AT_DEVICE_EC200X_INIT_ASYN
  844. rt_thread_t tid;
  845. tid = rt_thread_create("ec200x_net", ec200x_init_thread_entry, (void *)device,
  846. EC200X_THREAD_STACK_SIZE, EC200X_THREAD_PRIORITY, 20);
  847. if (tid)
  848. {
  849. rt_thread_startup(tid);
  850. }
  851. else
  852. {
  853. LOG_E("create %s device init thread failed.", device->name);
  854. return -RT_ERROR;
  855. }
  856. #else
  857. ec200x_init_thread_entry(device);
  858. #endif /* AT_DEVICE_EC200X_INIT_ASYN */
  859. return RT_EOK;
  860. }
  861. static int ec200x_init(struct at_device *device)
  862. {
  863. struct at_device_ec200x *ec200x = RT_NULL;
  864. RT_ASSERT(device);
  865. ec200x = (struct at_device_ec200x *) device->user_data;
  866. ec200x->power_status = RT_FALSE;//default power is off.
  867. ec200x->sleep_status = RT_FALSE;//default sleep is disabled.
  868. /* initialize AT client */
  869. #if RT_VER_NUM >= 0x50100
  870. at_client_init(ec200x->client_name, ec200x->recv_line_num, ec200x->recv_line_num);
  871. #else
  872. at_client_init(ec200x->client_name, ec200x->recv_line_num);
  873. #endif
  874. device->client = at_client_get(ec200x->client_name);
  875. if (device->client == RT_NULL)
  876. {
  877. LOG_E("get AT client(%s) failed.", ec200x->client_name);
  878. return -RT_ERROR;
  879. }
  880. /* register URC data execution function */
  881. #ifdef AT_USING_SOCKET
  882. ec200x_socket_init(device);
  883. #endif
  884. /* add ec200x device to the netdev list */
  885. device->netdev = ec200x_netdev_add(ec200x->device_name);
  886. if (device->netdev == RT_NULL)
  887. {
  888. LOG_E("add netdev(%s) failed.", ec200x->device_name);
  889. return -RT_ERROR;
  890. }
  891. /* initialize ec200x pin configuration */
  892. if (ec200x->power_pin != -1)
  893. {
  894. rt_pin_write(ec200x->power_pin, PIN_LOW);
  895. rt_pin_mode(ec200x->power_pin, PIN_MODE_OUTPUT);
  896. }
  897. if (ec200x->power_status_pin != -1)
  898. {
  899. rt_pin_mode(ec200x->power_status_pin, PIN_MODE_INPUT);
  900. }
  901. if (ec200x->wakeup_pin != -1)
  902. {
  903. rt_pin_write(ec200x->wakeup_pin, PIN_LOW);
  904. rt_pin_mode(ec200x->wakeup_pin, PIN_MODE_OUTPUT);
  905. }
  906. /* initialize ec200x device network */
  907. return ec200x_netdev_set_up(device->netdev);
  908. }
  909. static int ec200x_deinit(struct at_device *device)
  910. {
  911. RT_ASSERT(device);
  912. return ec200x_netdev_set_down(device->netdev);
  913. }
  914. static int ec200x_control(struct at_device *device, int cmd, void *arg)
  915. {
  916. int result = -RT_ERROR;
  917. RT_ASSERT(device);
  918. switch (cmd)
  919. {
  920. case AT_DEVICE_CTRL_SLEEP:
  921. result = ec200x_sleep(device);
  922. break;
  923. case AT_DEVICE_CTRL_WAKEUP:
  924. result = ec200x_wakeup(device);
  925. break;
  926. case AT_DEVICE_CTRL_POWER_ON:
  927. case AT_DEVICE_CTRL_POWER_OFF:
  928. case AT_DEVICE_CTRL_RESET:
  929. case AT_DEVICE_CTRL_LOW_POWER:
  930. case AT_DEVICE_CTRL_NET_CONN:
  931. case AT_DEVICE_CTRL_NET_DISCONN:
  932. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  933. case AT_DEVICE_CTRL_GET_SIGNAL:
  934. case AT_DEVICE_CTRL_GET_GPS:
  935. case AT_DEVICE_CTRL_GET_VER:
  936. LOG_W("not support the control command(%d).", cmd);
  937. break;
  938. default:
  939. LOG_E("input error control command(%d).", cmd);
  940. break;
  941. }
  942. return result;
  943. }
  944. const struct at_device_ops ec200x_device_ops =
  945. {
  946. ec200x_init,
  947. ec200x_deinit,
  948. ec200x_control,
  949. };
  950. static int ec200x_device_class_register(void)
  951. {
  952. struct at_device_class *class = RT_NULL;
  953. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  954. if (class == RT_NULL)
  955. {
  956. LOG_E("no memory for device class create.");
  957. return -RT_ENOMEM;
  958. }
  959. /* fill ec200x device class object */
  960. #ifdef AT_USING_SOCKET
  961. ec200x_socket_class_register(class);
  962. #endif
  963. class->device_ops = &ec200x_device_ops;
  964. return at_device_class_register(class, AT_DEVICE_CLASS_EC200X);
  965. }
  966. INIT_DEVICE_EXPORT(ec200x_device_class_register);
  967. #endif /* AT_DEVICE_USING_EC200X */