at_device_ec20.c 34 KB

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