at_device_ec20.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * File : at_device_ec20.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. * 2018-06-12 chenyong first version
  23. * 2018-08-12 Marcus port to ec20
  24. * 2019-05-13 chenyong multi AT socket client support
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <at_device_ec20.h>
  29. #define LOG_TAG "at.dev"
  30. #include <at_log.h>
  31. #ifdef AT_DEVICE_USING_EC20
  32. #define EC20_WAIT_CONNECT_TIME 5000
  33. #define EC20_THREAD_STACK_SIZE 1024
  34. #define EC20_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  35. /* AT+QICSGP command default*/
  36. static char *QICSGP_CHINA_MOBILE = "AT+QICSGP=1,1,\"CMNET\",\"\",\"\",0";
  37. static char *QICSGP_CHINA_UNICOM = "AT+QICSGP=1,1,\"UNINET\",\"\",\"\",0";
  38. static char *QICSGP_CHINA_TELECOM = "AT+QICSGP=1,1,\"CTNET\",\"\",\"\",0";
  39. static void at_cme_errcode_parse(int result)
  40. {
  41. switch(result)
  42. {
  43. case 0 : LOG_E("%d : Phone failure", result); break;
  44. case 1 : LOG_E("%d : No connection to phone", result); break;
  45. case 2 : LOG_E("%d : Phone-adaptor link reserved", result); break;
  46. case 3 : LOG_E("%d : Operation not allowed", result); break;
  47. case 4 : LOG_E("%d : Operation not supported", result); break;
  48. case 5 : LOG_E("%d : PH-SIM PIN required", result); break;
  49. case 6 : LOG_E("%d : PH-FSIM PIN required", result); break;
  50. case 7 : LOG_E("%d : PH-FSIM PUK required", result); break;
  51. case 10 : LOG_E("%d : SIM not inserted", result); break;
  52. case 11 : LOG_E("%d : SIM PIN required", result); break;
  53. case 12 : LOG_E("%d : SIM PUK required", result); break;
  54. case 13 : LOG_E("%d : SIM failure", result); break;
  55. case 14 : LOG_E("%d : SIM busy", result); break;
  56. case 15 : LOG_E("%d : SIM wrong", result); break;
  57. case 16 : LOG_E("%d : Incorrect password", result); break;
  58. case 17 : LOG_E("%d : SIM PIN2 required", result); break;
  59. case 18 : LOG_E("%d : SIM PUK2 required", result); break;
  60. case 20 : LOG_E("%d : Memory full", result); break;
  61. case 21 : LOG_E("%d : Invalid index", result); break;
  62. case 22 : LOG_E("%d : Not found", result); break;
  63. case 23 : LOG_E("%d : Memory failure", result); break;
  64. case 24 : LOG_E("%d : Text string too long", result); break;
  65. case 25 : LOG_E("%d : Invalid characters in text string", result); break;
  66. case 26 : LOG_E("%d : Dial string too long", result); break;
  67. case 27 : LOG_E("%d : Invalid characters in dial string", result); break;
  68. case 30 : LOG_E("%d : No network service", result); break;
  69. case 31 : LOG_E("%d : Network timeout", result); break;
  70. case 32 : LOG_E("%d : Network not allowed - emergency calls only", result); break;
  71. case 40 : LOG_E("%d : Network personalization PIN required", result); break;
  72. case 41 : LOG_E("%d : Network personalization PUK required", result); break;
  73. case 42 : LOG_E("%d : Network subset personalization PIN required", result); break;
  74. case 43 : LOG_E("%d : Network subset personalization PUK required", result); break;
  75. case 44 : LOG_E("%d : Service provider personalization PIN required", result); break;
  76. case 45 : LOG_E("%d : Service provider personalization PUK required", result); break;
  77. case 46 : LOG_E("%d : Corporate personalization PIN required", result); break;
  78. case 47 : LOG_E("%d : Corporate personalization PUK required", result); break;
  79. case 901 : LOG_E("%d : Audio unknown error", result); break;
  80. case 902 : LOG_E("%d : Audio invalid parameters", result); break;
  81. case 903 : LOG_E("%d : Audio operation not supported", result); break;
  82. case 904 : LOG_E("%d : Audio device busy", result); break;
  83. default : LOG_E("%d : Unknown err code", result); break;
  84. }
  85. }
  86. static void at_cms_errcode_parse(int result)
  87. {
  88. switch(result)
  89. {
  90. case 300 : LOG_E("%d : ME failure", result); break;
  91. case 301 : LOG_E("%d : SMS ME reserved", result); break;
  92. case 302 : LOG_E("%d : Operation not allowed", result); break;
  93. case 303 : LOG_E("%d : Operation not supported", result); break;
  94. case 304 : LOG_E("%d : Invalid PDU mode", result); break;
  95. case 305 : LOG_E("%d : Invalid text mode", result); break;
  96. case 310 : LOG_E("%d : SIM not inserted", result); break;
  97. case 311 : LOG_E("%d : SIM pin necessary", result); break;
  98. case 312 : LOG_E("%d : PH SIM pin necessary", result); break;
  99. case 313 : LOG_E("%d : SIM failure", result); break;
  100. case 314 : LOG_E("%d : SIM busy", result); break;
  101. case 315 : LOG_E("%d : SIM wrong", result); break;
  102. case 316 : LOG_E("%d : SIM PUK required", result); break;
  103. case 317 : LOG_E("%d : SIM PIN2 required", result); break;
  104. case 318 : LOG_E("%d : SIM PUK2 required", result); break;
  105. case 320 : LOG_E("%d : Memory failure", result); break;
  106. case 321 : LOG_E("%d : Invalid memory index", result); break;
  107. case 322 : LOG_E("%d : Memory full", result); break;
  108. case 330 : LOG_E("%d : SMSC address unknown", result); break;
  109. case 331 : LOG_E("%d : No network", result); break;
  110. case 332 : LOG_E("%d : Network timeout", result); break;
  111. case 500 : LOG_E("%d : Unknown", result); break;
  112. case 512 : LOG_E("%d : SIM not ready", result); break;
  113. case 513 : LOG_E("%d : Message length exceeds", result); break;
  114. case 514 : LOG_E("%d : Invalid request parameters", result); break;
  115. case 515 : LOG_E("%d : ME storage failure", result); break;
  116. case 517 : LOG_E("%d : Invalid service mode", result); break;
  117. case 528 : LOG_E("%d : More message to send state error", result); break;
  118. case 529 : LOG_E("%d : MO SMS is not allow", result); break;
  119. case 530 : LOG_E("%d : GPRS is suspended", result); break;
  120. case 531 : LOG_E("%d : ME storage full", result); break;
  121. default : LOG_E("%d : Unknown err code", result); break;
  122. }
  123. }
  124. static void at_mms_errcode_parse(int result)//MMS
  125. {
  126. switch(result)
  127. {
  128. case 751 : LOG_E("%d : Unknown error", result); break;
  129. case 752 : LOG_E("%d : URL length error", result); break;
  130. case 753 : LOG_E("%d : URL error", result); break;
  131. case 754 : LOG_E("%d : Invalid proxy type", result); break;
  132. case 755 : LOG_E("%d : Proxy address error", result); break;
  133. case 756 : LOG_E("%d : Invalid parameter", result); break;
  134. case 757 : LOG_E("%d : Recipient address full", result); break;
  135. case 758 : LOG_E("%d : CC recipient address full", result); break;
  136. case 759 : LOG_E("%d : BCC recipient address full", result); break;
  137. case 760 : LOG_E("%d : Attachments full", result); break;
  138. case 761 : LOG_E("%d : File error", result); break;
  139. case 762 : LOG_E("%d : No recipient", result); break;
  140. case 763 : LOG_E("%d : File not found", result); break;
  141. case 764 : LOG_E("%d : MMS busy", result); break;
  142. case 765 : LOG_E("%d : Server response failed", result); break;
  143. case 766 : LOG_E("%d : Error response of HTTP(S) post", result); break;
  144. case 767 : LOG_E("%d : Invalid report of HTTP(S) post", result); break;
  145. case 768 : LOG_E("%d : PDP activation failed", result); break;
  146. case 769 : LOG_E("%d : PDP deactivated", result); break;
  147. case 770 : LOG_E("%d : Socket creation failed", result); break;
  148. case 771 : LOG_E("%d : Socket connection failed", result); break;
  149. case 772 : LOG_E("%d : Socket read failed", result); break;
  150. case 773 : LOG_E("%d : Socket write failed", result); break;
  151. case 774 : LOG_E("%d : Socket closed", result); break;
  152. case 775 : LOG_E("%d : Timeout", result); break;
  153. case 776 : LOG_E("%d : Encode data error", result); break;
  154. case 777 : LOG_E("%d : HTTP(S) decode data error", result); break;
  155. default : LOG_E("%d : Unknown err code", result); break;
  156. }
  157. }
  158. static void ec20_power_on(struct at_device *device)
  159. {
  160. struct at_device_ec20 *ec20 = RT_NULL;
  161. ec20 = (struct at_device_ec20 *)device->user_data;
  162. /* not nead to set pin configuration for ec20 device power on */
  163. if (ec20->power_pin == -1 || ec20->power_status_pin == -1)
  164. {
  165. return;
  166. }
  167. if (rt_pin_read(ec20->power_status_pin) == PIN_HIGH)
  168. {
  169. return;
  170. }
  171. rt_pin_write(ec20->power_pin, PIN_HIGH);
  172. while (rt_pin_read(ec20->power_status_pin) == PIN_LOW)
  173. {
  174. rt_thread_mdelay(10);
  175. }
  176. rt_pin_write(ec20->power_pin, PIN_LOW);
  177. }
  178. static void ec20_power_off(struct at_device *device)
  179. {
  180. struct at_device_ec20 *ec20 = RT_NULL;
  181. ec20 = (struct at_device_ec20 *)device->user_data;
  182. /* not nead to set pin configuration for ec20 device power on */
  183. if (ec20->power_pin == -1 || ec20->power_status_pin == -1)
  184. {
  185. return;
  186. }
  187. if (rt_pin_read(ec20->power_status_pin) == PIN_LOW)
  188. {
  189. return;
  190. }
  191. rt_pin_write(ec20->power_pin, PIN_HIGH);
  192. while (rt_pin_read(ec20->power_status_pin) == PIN_HIGH)
  193. {
  194. rt_thread_mdelay(10);
  195. }
  196. rt_pin_write(ec20->power_pin, PIN_LOW);
  197. }
  198. /* ============================= ec20 network interface operations ============================= */
  199. /* set ec20 network interface device status and address information */
  200. static int ec20_netdev_set_info(struct netdev *netdev)
  201. {
  202. #define EC20_IEMI_RESP_SIZE 32
  203. #define EC20_IPADDR_RESP_SIZE 64
  204. #define EC20_DNS_RESP_SIZE 96
  205. #define EC20_INFO_RESP_TIMO rt_tick_from_millisecond(300)
  206. int result = RT_EOK;
  207. ip_addr_t addr;
  208. at_response_t resp = RT_NULL;
  209. struct at_device *device = RT_NULL;
  210. if (netdev == RT_NULL)
  211. {
  212. LOG_E("input network interface device is NULL.");
  213. return -RT_ERROR;
  214. }
  215. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  216. if (device == RT_NULL)
  217. {
  218. LOG_E("get ec20 device by netdev name(%s) failed.", netdev->name);
  219. return -RT_ERROR;
  220. }
  221. /* set network interface device status */
  222. netdev_low_level_set_status(netdev, RT_TRUE);
  223. netdev_low_level_set_link_status(netdev, RT_TRUE);
  224. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  225. resp = at_create_resp(EC20_IEMI_RESP_SIZE, 0, EC20_INFO_RESP_TIMO);
  226. if (resp == RT_NULL)
  227. {
  228. LOG_E("no memory for ec20 device(%s) response structure.", device->name);
  229. result = -RT_ENOMEM;
  230. goto __exit;
  231. }
  232. /* set network interface device hardware address(IEMI) */
  233. {
  234. #define EC20_NETDEV_HWADDR_LEN 8
  235. #define EC20_IEMI_LEN 15
  236. char iemi[EC20_IEMI_LEN] = {0};
  237. int i = 0, j = 0;
  238. /* send "AT+GSN" commond to get device IEMI */
  239. if (at_obj_exec_cmd(device->client, resp, "AT+GSN") < 0)
  240. {
  241. result = -RT_ERROR;
  242. goto __exit;
  243. }
  244. if (at_resp_parse_line_args(resp, 2, "%s", iemi) <= 0)
  245. {
  246. LOG_E("ec20 device(%s) prase \"AT+GSN\" commands resposne data error.", device->name);
  247. result = -RT_ERROR;
  248. goto __exit;
  249. }
  250. LOG_D("ec20 deevice(%s) IEMI number: %s", device->name, iemi);
  251. netdev->hwaddr_len = EC20_NETDEV_HWADDR_LEN;
  252. /* get hardware address by IEMI */
  253. for (i = 0, j = 0; i < EC20_NETDEV_HWADDR_LEN && j < EC20_IEMI_LEN; i++, j+=2)
  254. {
  255. if (j != EC20_IEMI_LEN - 1)
  256. {
  257. netdev->hwaddr[i] = (iemi[j] - '0') * 10 + (iemi[j + 1] - '0');
  258. }
  259. else
  260. {
  261. netdev->hwaddr[i] = (iemi[j] - '0');
  262. }
  263. }
  264. }
  265. /* set network interface device IP address */
  266. {
  267. #define IP_ADDR_SIZE_MAX 16
  268. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  269. resp = at_resp_set_info(resp, EC20_IPADDR_RESP_SIZE, 0, EC20_INFO_RESP_TIMO);
  270. /* send "AT+QIACT?" commond to get IP address */
  271. if (at_obj_exec_cmd(device->client, resp, "AT+QIACT?") < 0)
  272. {
  273. result = -RT_ERROR;
  274. goto __exit;
  275. }
  276. /* parse response data "+QIACT: 1,<context_state>,<context_type>[,<IP_address>]" */
  277. if (at_resp_parse_line_args_by_kw(resp, "+QIACT:", "+QIACT: %*[^\"]\"%[^\"]", ipaddr) <= 0)
  278. {
  279. LOG_E("Prase \"AT+QIACT?\" commands resposne data error!");
  280. result = -RT_ERROR;
  281. goto __exit;
  282. }
  283. LOG_D("ec20 device(%s) IP address: %s", device->name, ipaddr);
  284. /* set network interface address information */
  285. inet_aton(ipaddr, &addr);
  286. netdev_low_level_set_ipaddr(netdev, &addr);
  287. }
  288. /* set network interface device dns server */
  289. {
  290. #define DNS_ADDR_SIZE_MAX 16
  291. char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
  292. resp = at_resp_set_info(resp, EC20_DNS_RESP_SIZE, 0, EC20_INFO_RESP_TIMO);
  293. /* send "AT+QIDNSCFG=1" commond to get DNS servers address */
  294. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=1") < 0)
  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("ec20 device(%s) prase \"AT+QIDNSCFG=1\" commands resposne data error.", device->name);
  304. result = -RT_ERROR;
  305. goto __exit;
  306. }
  307. LOG_D("ec20 device(%s) primary DNS server address: %s", device->name, dns_server1);
  308. LOG_D("ec20 devcie(%s) 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 ec20_check_link_status_entry(void *parameter)
  322. {
  323. #define EC20_LINK_RESP_SIZE 64
  324. #define EC20_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
  325. #define EC20_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
  326. int link_stat = 0;
  327. at_response_t resp = RT_NULL;
  328. struct at_device *device = RT_NULL;
  329. struct netdev *netdev = (struct netdev *) parameter;
  330. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  331. if (device == RT_NULL)
  332. {
  333. LOG_E("get ec20 device by netdev name(%s) failed.", netdev->name);
  334. return;
  335. }
  336. resp = at_create_resp(EC20_LINK_RESP_SIZE, 0, EC20_LINK_RESP_TIMO);
  337. if (resp == RT_NULL)
  338. {
  339. LOG_E("no memory for ec20 device(%s) response structure.", device->name);
  340. return;
  341. }
  342. while (1)
  343. {
  344. /* send "AT+CGREG" commond to check netweork interface device link status */
  345. if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") < 0)
  346. {
  347. if (netdev_is_link_up(netdev))
  348. {
  349. netdev_low_level_set_link_status(netdev, RT_FALSE);
  350. }
  351. rt_thread_mdelay(EC20_LINK_DELAY_TIME);
  352. continue;
  353. }
  354. else
  355. {
  356. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat);
  357. /* 1 Registered, home network,5 Registered, roaming */
  358. if (link_stat == 1 || link_stat == 5)
  359. {
  360. if (netdev_is_link_up(netdev) == RT_FALSE)
  361. {
  362. netdev_low_level_set_link_status(netdev, RT_TRUE);
  363. }
  364. }
  365. else
  366. {
  367. if (netdev_is_link_up(netdev))
  368. {
  369. netdev_low_level_set_link_status(netdev, RT_FALSE);
  370. }
  371. }
  372. }
  373. rt_thread_mdelay(EC20_LINK_DELAY_TIME);
  374. }
  375. }
  376. static int ec20_netdev_check_link_status(struct netdev *netdev)
  377. {
  378. #define EC20_LINK_THREAD_TICK 20
  379. #define EC20_LINK_THREAD_STACK_SIZE 1024
  380. #define EC20_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  381. rt_thread_t tid;
  382. if (netdev == RT_NULL)
  383. {
  384. LOG_E("input network interface device is NULL.\n");
  385. return -RT_ERROR;
  386. }
  387. /* create ec20 link status polling thread */
  388. tid = rt_thread_create("ec20_link", ec20_check_link_status_entry, (void *) netdev,
  389. EC20_LINK_THREAD_STACK_SIZE, EC20_LINK_THREAD_PRIORITY, EC20_LINK_THREAD_TICK);
  390. if (tid != RT_NULL)
  391. {
  392. rt_thread_startup(tid);
  393. }
  394. return RT_EOK;
  395. }
  396. static int ec20_net_init(struct at_device *device);
  397. static int ec20_netdev_set_up(struct netdev *netdev)
  398. {
  399. struct at_device *device = RT_NULL;
  400. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  401. if (device == RT_NULL)
  402. {
  403. LOG_E("get ec20 device by netdev name(%s) failed.", netdev->name);
  404. return -RT_ERROR;
  405. }
  406. if (device->is_init == RT_FALSE)
  407. {
  408. ec20_net_init(device);
  409. device->is_init = RT_TRUE;
  410. netdev_low_level_set_status(netdev, RT_TRUE);
  411. LOG_D("the network interface device(%s) set up status.", netdev->name);
  412. }
  413. return RT_EOK;
  414. }
  415. static int ec20_netdev_set_down(struct netdev *netdev)
  416. {
  417. struct at_device *device = RT_NULL;
  418. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  419. if (device == RT_NULL)
  420. {
  421. LOG_E("get ec20 device by netdev name(%s) failed.", netdev->name);
  422. return -RT_ERROR;
  423. }
  424. if (device->is_init == RT_TRUE)
  425. {
  426. ec20_power_off(device);
  427. device->is_init = RT_FALSE;
  428. netdev_low_level_set_status(netdev, RT_FALSE);
  429. LOG_D("the network interface device(%s) set down status.", netdev->name);
  430. }
  431. return RT_EOK;
  432. }
  433. static int ec20_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  434. {
  435. #define EC20_DNS_RESP_LEN 8
  436. #define EC20_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
  437. int result = RT_EOK;
  438. at_response_t resp = RT_NULL;
  439. struct at_device *device = RT_NULL;
  440. RT_ASSERT(netdev);
  441. RT_ASSERT(dns_server);
  442. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  443. if (device == RT_NULL)
  444. {
  445. LOG_E("get ec20 device by netdev name(%s) failed.", netdev->name);
  446. return -RT_ERROR;
  447. }
  448. resp = at_create_resp(EC20_DNS_RESP_LEN, 0, EC20_DNS_RESP_TIMEO);
  449. if (resp == RT_NULL)
  450. {
  451. LOG_D("no memory for ec20 device(%s) response structure.", device->name);
  452. result = -RT_ENOMEM;
  453. goto __exit;
  454. }
  455. /* send "AT+QIDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
  456. if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=1,\"%s\"", inet_ntoa(*dns_server)) < 0)
  457. {
  458. result = -RT_ERROR;
  459. goto __exit;
  460. }
  461. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  462. __exit:
  463. if (resp)
  464. {
  465. at_delete_resp(resp);
  466. }
  467. return result;
  468. }
  469. #ifdef NETDEV_USING_PING
  470. static int ec20_netdev_ping(struct netdev *netdev, const char *host,
  471. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
  472. {
  473. #define EC20_PING_RESP_SIZE 128
  474. #define EC20_PING_IP_SIZE 16
  475. #define EC20_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  476. rt_err_t result = RT_EOK;
  477. int response = -1, recv_data_len, ping_time, ttl;
  478. char ip_addr[EC20_PING_IP_SIZE] = {0};
  479. at_response_t resp = RT_NULL;
  480. struct at_device *device = RT_NULL;
  481. RT_ASSERT(netdev);
  482. RT_ASSERT(host);
  483. RT_ASSERT(ping_resp);
  484. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  485. if (device == RT_NULL)
  486. {
  487. LOG_E("get ec20 device by netdev name(%s) failed.", netdev->name);
  488. return -RT_ERROR;
  489. }
  490. resp = at_create_resp(EC20_PING_RESP_SIZE, 4, EC20_PING_TIMEO);
  491. if (resp == RT_NULL)
  492. {
  493. LOG_E("no memory for ec20 device(%s) response structure.", device->name);
  494. return -RT_ENOMEM;
  495. }
  496. /* send "AT+QPING="<host>"[,[<timeout>][,<pingnum>]]" commond to send ping request */
  497. if (at_obj_exec_cmd(device->client, resp, "AT+QPING=1,\"%s\",%d,1", host, timeout / RT_TICK_PER_SECOND) < 0)
  498. {
  499. result = -RT_ERROR;
  500. goto __exit;
  501. }
  502. at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d", &response);
  503. /* Received the ping response from the server */
  504. if (response == 0)
  505. {
  506. if (at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d,\"%[^\"]\",%d,%d,%d",
  507. &response, ip_addr, &recv_data_len, &ping_time, &ttl) <= 0)
  508. {
  509. result = -RT_ERROR;
  510. goto __exit;
  511. }
  512. }
  513. /* prase response number */
  514. switch (response)
  515. {
  516. case 0:
  517. inet_aton(ip_addr, &(ping_resp->ip_addr));
  518. ping_resp->data_len = recv_data_len;
  519. ping_resp->ticks = ping_time;
  520. ping_resp->ttl = ttl;
  521. result = RT_EOK;
  522. break;
  523. case 569:
  524. result = -RT_ETIMEOUT;
  525. break;
  526. default:
  527. result = -RT_ERROR;
  528. break;
  529. }
  530. __exit:
  531. if (resp)
  532. {
  533. at_delete_resp(resp);
  534. }
  535. return result;
  536. }
  537. #endif /* NETDEV_USING_PING */
  538. #ifdef NETDEV_USING_NETSTAT
  539. static void ec20_netdev_netstat(struct netdev *netdev)
  540. {
  541. // TODO
  542. }
  543. #endif /* NETDEV_USING_NETSTAT */
  544. const struct netdev_ops ec20_netdev_ops =
  545. {
  546. ec20_netdev_set_up,
  547. ec20_netdev_set_down,
  548. RT_NULL,
  549. ec20_netdev_set_dns_server,
  550. RT_NULL,
  551. #ifdef NETDEV_USING_PING
  552. ec20_netdev_ping,
  553. #endif
  554. #ifdef NETDEV_USING_NETSTAT
  555. ec20_netdev_netstat,
  556. #endif
  557. };
  558. static struct netdev *ec20_netdev_add(const char *netdev_name)
  559. {
  560. #define ETHERNET_MTU 1500
  561. #define HWADDR_LEN 8
  562. struct netdev *netdev = RT_NULL;
  563. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  564. if (netdev == RT_NULL)
  565. {
  566. LOG_E("no memory for ec20 device(%s) netdev structure.", netdev->name);
  567. return RT_NULL;
  568. }
  569. netdev->mtu = ETHERNET_MTU;
  570. netdev->ops = &ec20_netdev_ops;
  571. netdev->hwaddr_len = HWADDR_LEN;
  572. #ifdef SAL_USING_AT
  573. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  574. /* set the network interface socket/netdb operations */
  575. sal_at_netdev_set_pf_info(netdev);
  576. #endif
  577. netdev_register(netdev, netdev_name, RT_NULL);
  578. return netdev;
  579. }
  580. /* ============================= ec20 device operations ============================= */
  581. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  582. do { \
  583. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  584. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  585. { \
  586. result = -RT_ERROR; \
  587. goto __exit; \
  588. } \
  589. } while(0) \
  590. /* initialize for ec20 */
  591. static void ec20_init_thread_entry(void *parameter)
  592. {
  593. #define INIT_RETRY 5
  594. #define CIMI_RETRY 10
  595. #define CSQ_RETRY 20
  596. #define CREG_RETRY 10
  597. #define CGREG_RETRY 20
  598. int i, qi_arg[3] = {0};
  599. int retry_num = INIT_RETRY;
  600. char parsed_data[20] = {0};
  601. rt_err_t result = RT_EOK;
  602. at_response_t resp = RT_NULL;
  603. struct at_device *device = (struct at_device *) parameter;
  604. struct at_client *client = device->client;
  605. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  606. if (resp == RT_NULL)
  607. {
  608. LOG_E("no memory for ec20 device(%s) response structure.", device->name);
  609. return;
  610. }
  611. LOG_D("start initializing the ec20 device(%s).", device->name);
  612. while (retry_num--)
  613. {
  614. /* power on the ec20 device */
  615. ec20_power_on(device);
  616. rt_thread_mdelay(1000);
  617. /* wait ec20 startup finish, send AT every 500ms, if receive OK, SYNC success*/
  618. if (at_client_obj_wait_connect(client, EC20_WAIT_CONNECT_TIME))
  619. {
  620. result = -RT_ETIMEOUT;
  621. goto __exit;
  622. }
  623. /* set response format to ATV1 */
  624. AT_SEND_CMD(client, resp, 0, 300, "ATV1");
  625. /* disable echo */
  626. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  627. /* Use AT+CMEE=2 to enable result code and use verbose values */
  628. AT_SEND_CMD(client, resp, 0, 300, "AT+CMEE=2");
  629. /* Get the baudrate */
  630. AT_SEND_CMD(client, resp, 0, 300, "AT+IPR?");
  631. at_resp_parse_line_args_by_kw(resp, "+IPR:", "+IPR: %d", &i);
  632. LOG_D("ec20 device(%s) baudrate %d", device->name, i);
  633. /* get module version */
  634. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  635. /* show module version */
  636. for (i = 0; i < (int) resp->line_counts - 1; i++)
  637. {
  638. LOG_D("%s", at_resp_get_line(resp, i + 1));
  639. }
  640. /* Use AT+GSN to query the IMEI of module */
  641. AT_SEND_CMD(client, resp, 0, 300, "AT+GSN");
  642. /* check SIM card */
  643. AT_SEND_CMD(client, resp, 2, 5 * 1000, "AT+CPIN?");
  644. if (!at_resp_get_line_by_kw(resp, "READY"))
  645. {
  646. LOG_E("ec20 device(%s) SIM card detection failed.", device->name);
  647. result = -RT_ERROR;
  648. goto __exit;
  649. }
  650. /* waiting for dirty data to be digested */
  651. rt_thread_mdelay(10);
  652. /* Use AT+CIMI to query the IMSI of SIM card */
  653. // AT_SEND_CMD(client, resp, 2, 300, "AT+CIMI");
  654. i = 0;
  655. resp = at_resp_set_info(resp, 128, 0, rt_tick_from_millisecond(300));
  656. while(at_obj_exec_cmd(device->client, resp, "AT+CIMI") < 0)
  657. {
  658. i++;
  659. if(i > CIMI_RETRY)
  660. {
  661. LOG_E("ec20 device(%s) read CIMI failed.", device->name);
  662. result = -RT_ERROR;
  663. goto __exit;
  664. }
  665. rt_thread_mdelay(1000);
  666. }
  667. /* Use AT+QCCID to query ICCID number of SIM card */
  668. AT_SEND_CMD(client, resp, 0, 300, "AT+QCCID");
  669. /* check signal strength */
  670. for (i = 0; i < CSQ_RETRY; i++)
  671. {
  672. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  673. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &qi_arg[0], &qi_arg[1]);
  674. if (qi_arg[0] != 99)
  675. {
  676. LOG_D("ec20 device(%s) signal strength: %d, channel bit error rate: %d",
  677. device->name, qi_arg[0], qi_arg[1]);
  678. break;
  679. }
  680. rt_thread_mdelay(1000);
  681. }
  682. if (i == CSQ_RETRY)
  683. {
  684. LOG_E("ec20 device(%s) signal strength check failed (%s)", device->name, parsed_data);
  685. result = -RT_ERROR;
  686. goto __exit;
  687. }
  688. /* check the GSM network is registered */
  689. for (i = 0; i < CREG_RETRY; i++)
  690. {
  691. AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
  692. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  693. if (!rt_strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  694. !rt_strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  695. {
  696. LOG_D("ec20 device(%s) GSM network is registered(%s)", device->name, parsed_data);
  697. break;
  698. }
  699. rt_thread_mdelay(1000);
  700. }
  701. if (i == CREG_RETRY)
  702. {
  703. LOG_E("ec20 device(%s) GSM network is register failed (%s)", device->name, parsed_data);
  704. result = -RT_ERROR;
  705. goto __exit;
  706. }
  707. /* check the GPRS network is registered */
  708. for (i = 0; i < CGREG_RETRY; i++)
  709. {
  710. AT_SEND_CMD(client, resp, 0, 300, "AT+CGREG?");
  711. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %s", &parsed_data);
  712. if (!rt_strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  713. !rt_strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  714. {
  715. LOG_D("ec20 device(%s) GPRS network is registered(%s)", device->name, parsed_data);
  716. break;
  717. }
  718. rt_thread_mdelay(1000);
  719. }
  720. if (i == CGREG_RETRY)
  721. {
  722. LOG_E("ec20 device(%s) GPRS network is register failed (%s)", device->name, parsed_data);
  723. result = -RT_ERROR;
  724. goto __exit;
  725. }
  726. /*Use AT+CEREG? to query current EPS Network Registration Status*/
  727. AT_SEND_CMD(client, resp, 0, 300, "AT+CEREG?");
  728. /* Use AT+COPS? to query current Network Operator */
  729. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
  730. at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
  731. if(rt_strcmp(parsed_data,"CHINA MOBILE") == 0)
  732. {
  733. /* "CMCC" */
  734. LOG_I("ec20 device(%s) network operator: %s", device->name, parsed_data);
  735. AT_SEND_CMD(client, resp, 0, 300, QICSGP_CHINA_MOBILE);
  736. }
  737. else if(strcmp(parsed_data,"CHN-UNICOM") == 0)
  738. {
  739. /* "UNICOM" */
  740. LOG_I("ec20 device(%s) network operator: %s", device->name, parsed_data);
  741. AT_SEND_CMD(client, resp, 0, 300, QICSGP_CHINA_UNICOM);
  742. }
  743. else if(rt_strcmp(parsed_data,"CHN-CT") == 0)
  744. {
  745. /* "CT" */
  746. LOG_I("ec20 device(%s) network operator: %s", device->name, parsed_data);
  747. AT_SEND_CMD(client, resp, 0, 300, QICSGP_CHINA_TELECOM);
  748. }
  749. /* Enable automatic time zone update via NITZ and update LOCAL time to RTC */
  750. AT_SEND_CMD(client, resp, 0, 300, "AT+CTZU=3");
  751. /* Get RTC time */
  752. AT_SEND_CMD(client, resp, 0, 300, "AT+CCLK?");
  753. /* Deactivate context profile */
  754. AT_SEND_CMD(client, resp, 0, 40 * 1000, "AT+QIDEACT=1");
  755. /* Activate context profile */
  756. AT_SEND_CMD(client, resp, 0, 150 * 1000, "AT+QIACT=1");
  757. /* Query the status of the context profile */
  758. AT_SEND_CMD(client, resp, 0, 150 * 1000, "AT+QIACT?");
  759. at_resp_parse_line_args_by_kw(resp, "+QIACT:", "+QIACT: %*[^\"]\"%[^\"]", &parsed_data);
  760. LOG_I("ec20 device(%s) IP address: %s", device->name, parsed_data);
  761. result = RT_EOK;
  762. __exit:
  763. if (result == RT_EOK)
  764. {
  765. break;
  766. }
  767. else
  768. {
  769. /* power off the ec20 device */
  770. ec20_power_off(device);
  771. rt_thread_mdelay(1000);
  772. LOG_I("ec20 device(%s) initialize retry...", device->name);
  773. }
  774. }
  775. if (resp)
  776. {
  777. at_delete_resp(resp);
  778. }
  779. if (result == RT_EOK)
  780. {
  781. /* set network interface device status and address information */
  782. ec20_netdev_set_info(device->netdev);
  783. ec20_netdev_check_link_status(device->netdev);
  784. LOG_I("ec20 device(%s) network initialize success.", device->name);
  785. }
  786. else
  787. {
  788. LOG_E("ec20 device(%s) network initialize failed(%d).", device->name, result);
  789. }
  790. }
  791. /* ec20 device network initialize */
  792. static int ec20_net_init(struct at_device *device)
  793. {
  794. #ifdef AT_DEVICE_EC20_INIT_ASYN
  795. rt_thread_t tid;
  796. tid = rt_thread_create("ec20_net_init", ec20_init_thread_entry, (void *) device,
  797. EC20_THREAD_STACK_SIZE, EC20_THREAD_PRIORITY, 20);
  798. if (tid)
  799. {
  800. rt_thread_startup(tid);
  801. }
  802. else
  803. {
  804. LOG_E("create ec20 device(%s) initialization thread failed.", device->name);
  805. return -RT_ERROR;
  806. }
  807. #else
  808. ec20_init_thread_entry(device);
  809. #endif /* AT_DEVICE_EC20_INIT_ASYN */
  810. return RT_EOK;
  811. }
  812. static int ec20_init(struct at_device *device)
  813. {
  814. struct at_device_ec20 *ec20 = (struct at_device_ec20 *) device->user_data;
  815. /* initialize AT client */
  816. at_client_init(ec20->client_name, ec20->recv_line_num);
  817. device->client = at_client_get(ec20->client_name);
  818. if (device->client == RT_NULL)
  819. {
  820. LOG_E("ec20 device(%s) initialize failed, get AT client(%s) failed.", ec20->device_name, ec20->client_name);
  821. return -RT_ERROR;
  822. }
  823. /* register URC data execution function */
  824. #ifdef AT_USING_SOCKET
  825. ec20_socket_init(device);
  826. #endif
  827. /* add ec20 device to the netdev list */
  828. device->netdev = ec20_netdev_add(ec20->device_name);
  829. if (device->netdev == RT_NULL)
  830. {
  831. LOG_E("ec20 device(%s) initialize failed, get network interface device failed.", ec20->device_name);
  832. return -RT_ERROR;
  833. }
  834. /* initialize ec20 pin configuration */
  835. if (ec20->power_pin != -1 && ec20->power_status_pin != -1)
  836. {
  837. rt_pin_mode(ec20->power_pin, PIN_MODE_OUTPUT);
  838. rt_pin_mode(ec20->power_status_pin, PIN_MODE_INPUT);
  839. }
  840. /* initialize ec20 device network */
  841. return ec20_netdev_set_up(device->netdev);
  842. }
  843. static int ec20_deinit(struct at_device *device)
  844. {
  845. return ec20_netdev_set_down(device->netdev);
  846. }
  847. static int ec20_control(struct at_device *device, int cmd, void *arg)
  848. {
  849. int result = -RT_ERROR;
  850. RT_ASSERT(device);
  851. switch (cmd)
  852. {
  853. case AT_DEVICE_CTRL_POWER_ON:
  854. case AT_DEVICE_CTRL_POWER_OFF:
  855. case AT_DEVICE_CTRL_RESET:
  856. case AT_DEVICE_CTRL_LOW_POWER:
  857. case AT_DEVICE_CTRL_SLEEP:
  858. case AT_DEVICE_CTRL_WAKEUP:
  859. case AT_DEVICE_CTRL_NET_CONN:
  860. case AT_DEVICE_CTRL_NET_DISCONN:
  861. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  862. case AT_DEVICE_CTRL_GET_SIGNAL:
  863. case AT_DEVICE_CTRL_GET_GPS:
  864. case AT_DEVICE_CTRL_GET_VER:
  865. LOG_W("ec20 not support the control command(%d).", cmd);
  866. break;
  867. default:
  868. LOG_E("input error control command(%d).", cmd);
  869. break;
  870. }
  871. return result;
  872. }
  873. const struct at_device_ops ec20_device_ops =
  874. {
  875. ec20_init,
  876. ec20_deinit,
  877. ec20_control,
  878. };
  879. static int ec20_device_class_register(void)
  880. {
  881. struct at_device_class *class = RT_NULL;
  882. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  883. if (class == RT_NULL)
  884. {
  885. LOG_E("no memory for ec20 device class create.");
  886. return -RT_ENOMEM;
  887. }
  888. /* fill ec20 device class object */
  889. #ifdef AT_USING_SOCKET
  890. ec20_socket_class_register(class);
  891. #endif
  892. class->device_ops = &ec20_device_ops;
  893. return at_device_class_register(class, AT_DEVICE_CLASS_EC20);
  894. }
  895. INIT_DEVICE_EXPORT(ec20_device_class_register);
  896. #endif /* AT_DEVICE_USING_EC20 */