at_device_ec20.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * Copyright (c) 2006-2022, 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. {
  457. #define EC20_PING_RESP_SIZE 128
  458. #define EC20_PING_IP_SIZE 16
  459. #define EC20_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  460. rt_err_t result = RT_EOK;
  461. int response = -1, recv_data_len, ping_time, ttl;
  462. char ip_addr[EC20_PING_IP_SIZE] = {0};
  463. at_response_t resp = RT_NULL;
  464. struct at_device *device = RT_NULL;
  465. RT_ASSERT(netdev);
  466. RT_ASSERT(host);
  467. RT_ASSERT(ping_resp);
  468. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  469. if (device == RT_NULL)
  470. {
  471. LOG_E("get device(%s) failed.", netdev->name);
  472. return -RT_ERROR;
  473. }
  474. resp = at_create_resp(EC20_PING_RESP_SIZE, 4, EC20_PING_TIMEO);
  475. if (resp == RT_NULL)
  476. {
  477. LOG_E("no memory for resp create");
  478. return -RT_ENOMEM;
  479. }
  480. /* send "AT+QPING="<host>"[,[<timeout>][,<pingnum>]]" commond to send ping request */
  481. if (at_obj_exec_cmd(device->client, resp, "AT+QPING=1,\"%s\",%d,1", host, timeout / RT_TICK_PER_SECOND) < 0)
  482. {
  483. result = -RT_ERROR;
  484. goto __exit;
  485. }
  486. at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d", &response);
  487. /* Received the ping response from the server */
  488. if (response == 0)
  489. {
  490. if (at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d,\"%[^\"]\",%d,%d,%d",
  491. &response, ip_addr, &recv_data_len, &ping_time, &ttl) <= 0)
  492. {
  493. result = -RT_ERROR;
  494. goto __exit;
  495. }
  496. }
  497. /* prase response number */
  498. switch (response)
  499. {
  500. case 0:
  501. inet_aton(ip_addr, &(ping_resp->ip_addr));
  502. ping_resp->data_len = recv_data_len;
  503. ping_resp->ticks = ping_time;
  504. ping_resp->ttl = ttl;
  505. result = RT_EOK;
  506. break;
  507. case 569:
  508. result = -RT_ETIMEOUT;
  509. break;
  510. default:
  511. result = -RT_ERROR;
  512. break;
  513. }
  514. __exit:
  515. if (resp)
  516. {
  517. at_delete_resp(resp);
  518. }
  519. return result;
  520. }
  521. #endif /* NETDEV_USING_PING */
  522. const struct netdev_ops ec20_netdev_ops =
  523. {
  524. ec20_netdev_set_up,
  525. ec20_netdev_set_down,
  526. RT_NULL,
  527. ec20_netdev_set_dns_server,
  528. RT_NULL,
  529. #ifdef NETDEV_USING_PING
  530. ec20_netdev_ping,
  531. #endif
  532. RT_NULL,
  533. };
  534. static struct netdev *ec20_netdev_add(const char *netdev_name)
  535. {
  536. #define ETHERNET_MTU 1500
  537. #define HWADDR_LEN 8
  538. struct netdev *netdev = RT_NULL;
  539. netdev = netdev_get_by_name(netdev_name);
  540. if (netdev != RT_NULL)
  541. {
  542. return (netdev);
  543. }
  544. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  545. if (netdev == RT_NULL)
  546. {
  547. LOG_E("no memory for netdev create.");
  548. return RT_NULL;
  549. }
  550. netdev->mtu = ETHERNET_MTU;
  551. netdev->ops = &ec20_netdev_ops;
  552. netdev->hwaddr_len = HWADDR_LEN;
  553. #ifdef SAL_USING_AT
  554. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  555. /* set the network interface socket/netdb operations */
  556. sal_at_netdev_set_pf_info(netdev);
  557. #endif
  558. netdev_register(netdev, netdev_name, RT_NULL);
  559. return netdev;
  560. }
  561. /* ============================= ec20 device operations ============================= */
  562. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  563. do { \
  564. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  565. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  566. { \
  567. result = -RT_ERROR; \
  568. goto __exit; \
  569. } \
  570. } while(0) \
  571. /* initialize for ec20 */
  572. static void ec20_init_thread_entry(void *parameter)
  573. {
  574. #define INIT_RETRY 5
  575. #define CIMI_RETRY 10
  576. #define CSQ_RETRY 20
  577. #define CREG_RETRY 10
  578. #define CGREG_RETRY 20
  579. int i, qi_arg[3] = {0};
  580. int retry_num = INIT_RETRY;
  581. char parsed_data[20] = {0};
  582. rt_err_t result = RT_EOK;
  583. at_response_t resp = RT_NULL;
  584. struct at_device *device = (struct at_device *) parameter;
  585. struct at_client *client = device->client;
  586. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  587. if (resp == RT_NULL)
  588. {
  589. LOG_E("no memory for resp create.");
  590. return;
  591. }
  592. LOG_D("start init %s device.", device->name);
  593. while (retry_num--)
  594. {
  595. /* power on the ec20 device */
  596. ec20_power_on(device);
  597. rt_thread_mdelay(1000);
  598. /* wait ec20 startup finish, send AT every 500ms, if receive OK, SYNC success*/
  599. if (at_client_obj_wait_connect(client, EC20_WAIT_CONNECT_TIME))
  600. {
  601. result = -RT_ETIMEOUT;
  602. goto __exit;
  603. }
  604. /* set response format to ATV1 */
  605. AT_SEND_CMD(client, resp, 0, 300, "ATV1");
  606. /* disable echo */
  607. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  608. /* Use AT+CMEE=2 to enable result code and use verbose values */
  609. AT_SEND_CMD(client, resp, 0, 300, "AT+CMEE=2");
  610. /* Get the baudrate */
  611. AT_SEND_CMD(client, resp, 0, 300, "AT+IPR?");
  612. at_resp_parse_line_args_by_kw(resp, "+IPR:", "+IPR: %d", &i);
  613. LOG_D("%s device baudrate %d", device->name, i);
  614. /* get module version */
  615. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  616. /* show module version */
  617. for (i = 0; i < (int) resp->line_counts - 1; i++)
  618. {
  619. LOG_D("%s", at_resp_get_line(resp, i + 1));
  620. }
  621. /* Use AT+GSN to query the IMEI of module */
  622. AT_SEND_CMD(client, resp, 0, 300, "AT+GSN");
  623. /* check SIM card */
  624. AT_SEND_CMD(client, resp, 2, 5 * 1000, "AT+CPIN?");
  625. if (!at_resp_get_line_by_kw(resp, "READY"))
  626. {
  627. LOG_E("%s device SIM card detection failed.", device->name);
  628. result = -RT_ERROR;
  629. goto __exit;
  630. }
  631. /* waiting for dirty data to be digested */
  632. rt_thread_mdelay(10);
  633. /* Use AT+CIMI to query the IMSI of SIM card */
  634. // AT_SEND_CMD(client, resp, 2, 300, "AT+CIMI");
  635. i = 0;
  636. resp = at_resp_set_info(resp, 128, 0, rt_tick_from_millisecond(300));
  637. while(at_obj_exec_cmd(device->client, resp, "AT+CIMI") < 0)
  638. {
  639. i++;
  640. if(i > CIMI_RETRY)
  641. {
  642. LOG_E("%s device read CIMI failed.", device->name);
  643. result = -RT_ERROR;
  644. goto __exit;
  645. }
  646. rt_thread_mdelay(1000);
  647. }
  648. /* Use AT+QCCID to query ICCID number of SIM card */
  649. AT_SEND_CMD(client, resp, 0, 300, "AT+QCCID");
  650. /* check signal strength */
  651. for (i = 0; i < CSQ_RETRY; i++)
  652. {
  653. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  654. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &qi_arg[0], &qi_arg[1]);
  655. if (qi_arg[0] != 99)
  656. {
  657. LOG_D("%s device signal strength: %d, channel bit error rate: %d",
  658. device->name, qi_arg[0], qi_arg[1]);
  659. break;
  660. }
  661. rt_thread_mdelay(1000);
  662. }
  663. if (i == CSQ_RETRY)
  664. {
  665. LOG_E("%s device signal strength check failed (%s)", device->name, parsed_data);
  666. result = -RT_ERROR;
  667. goto __exit;
  668. }
  669. /* check the GSM network is registered */
  670. for (i = 0; i < CREG_RETRY; i++)
  671. {
  672. AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
  673. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  674. if (!rt_strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  675. !rt_strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  676. {
  677. LOG_D("%s device GSM is registered(%s)", device->name, parsed_data);
  678. break;
  679. }
  680. rt_thread_mdelay(1000);
  681. }
  682. if (i == CREG_RETRY)
  683. {
  684. LOG_E("%s device GSM is register failed (%s)", device->name, parsed_data);
  685. result = -RT_ERROR;
  686. goto __exit;
  687. }
  688. /* check the GPRS network is registered */
  689. for (i = 0; i < CGREG_RETRY; i++)
  690. {
  691. AT_SEND_CMD(client, resp, 0, 300, "AT+CGREG?");
  692. at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %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("%s device GPRS is registered(%s)", device->name, parsed_data);
  697. break;
  698. }
  699. rt_thread_mdelay(1000);
  700. }
  701. if (i == CGREG_RETRY)
  702. {
  703. LOG_E("%s device GPRS is register failed (%s)", device->name, parsed_data);
  704. result = -RT_ERROR;
  705. goto __exit;
  706. }
  707. /*Use AT+CEREG? to query current EPS Network Registration Status*/
  708. AT_SEND_CMD(client, resp, 0, 300, "AT+CEREG?");
  709. /* Use AT+COPS? to query current Network Operator */
  710. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
  711. at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
  712. if(rt_strcmp(parsed_data,"CHINA MOBILE") == 0)
  713. {
  714. /* "CMCC" */
  715. LOG_I("%s device network operator: %s", device->name, parsed_data);
  716. AT_SEND_CMD(client, resp, 0, 300, QICSGP_CHINA_MOBILE);
  717. }
  718. else if(strcmp(parsed_data,"CHN-UNICOM") == 0)
  719. {
  720. /* "UNICOM" */
  721. LOG_I("%s device network operator: %s", device->name, parsed_data);
  722. AT_SEND_CMD(client, resp, 0, 300, QICSGP_CHINA_UNICOM);
  723. }
  724. else if(rt_strcmp(parsed_data,"CHN-CT") == 0)
  725. {
  726. /* "CT" */
  727. LOG_I("%s device network operator: %s", device->name, parsed_data);
  728. AT_SEND_CMD(client, resp, 0, 300, QICSGP_CHINA_TELECOM);
  729. }
  730. /* Enable automatic time zone update via NITZ and update LOCAL time to RTC */
  731. AT_SEND_CMD(client, resp, 0, 300, "AT+CTZU=3");
  732. /* Get RTC time */
  733. AT_SEND_CMD(client, resp, 0, 300, "AT+CCLK?");
  734. /* Deactivate context profile */
  735. AT_SEND_CMD(client, resp, 0, 40 * 1000, "AT+QIDEACT=1");
  736. /* Activate context profile */
  737. AT_SEND_CMD(client, resp, 0, 150 * 1000, "AT+QIACT=1");
  738. /* Query the status of the context profile */
  739. AT_SEND_CMD(client, resp, 0, 150 * 1000, "AT+QIACT?");
  740. at_resp_parse_line_args_by_kw(resp, "+QIACT:", "+QIACT: %*[^\"]\"%[^\"]", &parsed_data);
  741. LOG_I("%s device IP address: %s", device->name, parsed_data);
  742. /* initialize successfully */
  743. result = RT_EOK;
  744. break;
  745. __exit:
  746. if (result != RT_EOK)
  747. {
  748. /* power off the ec20 device */
  749. ec20_power_off(device);
  750. rt_thread_mdelay(1000);
  751. LOG_I("%s device initialize retry...", device->name);
  752. }
  753. }
  754. if (resp)
  755. {
  756. at_delete_resp(resp);
  757. }
  758. if (result == RT_EOK)
  759. {
  760. /* set network interface device status and address information */
  761. ec20_netdev_set_info(device->netdev);
  762. /* check and create link staus sync thread */
  763. if (rt_thread_find(device->netdev->name) == RT_NULL)
  764. {
  765. ec20_netdev_check_link_status(device->netdev);
  766. }
  767. LOG_I("%s device network initialize success.", device->name);
  768. }
  769. else
  770. {
  771. LOG_E("%s device network initialize failed(%d).", device->name, result);
  772. }
  773. }
  774. /* ec20 device network initialize */
  775. static int ec20_net_init(struct at_device *device)
  776. {
  777. #ifdef AT_DEVICE_EC20_INIT_ASYN
  778. rt_thread_t tid;
  779. tid = rt_thread_create("ec20_net", ec20_init_thread_entry, (void *)device,
  780. EC20_THREAD_STACK_SIZE, EC20_THREAD_PRIORITY, 20);
  781. if (tid)
  782. {
  783. rt_thread_startup(tid);
  784. }
  785. else
  786. {
  787. LOG_E("create %s device init thread failed.", device->name);
  788. return -RT_ERROR;
  789. }
  790. #else
  791. ec20_init_thread_entry(device);
  792. #endif /* AT_DEVICE_EC20_INIT_ASYN */
  793. return RT_EOK;
  794. }
  795. static int ec20_init(struct at_device *device)
  796. {
  797. struct at_device_ec20 *ec20 = (struct at_device_ec20 *) device->user_data;
  798. /* initialize AT client */
  799. at_client_init(ec20->client_name, ec20->recv_line_num);
  800. device->client = at_client_get(ec20->client_name);
  801. if (device->client == RT_NULL)
  802. {
  803. LOG_E("get AT client(%s) failed.", ec20->client_name);
  804. return -RT_ERROR;
  805. }
  806. /* register URC data execution function */
  807. #ifdef AT_USING_SOCKET
  808. ec20_socket_init(device);
  809. #endif
  810. /* add ec20 device to the netdev list */
  811. device->netdev = ec20_netdev_add(ec20->device_name);
  812. if (device->netdev == RT_NULL)
  813. {
  814. LOG_E("add netdev(%s) failed.", ec20->device_name);
  815. return -RT_ERROR;
  816. }
  817. /* initialize ec20 pin configuration */
  818. if (ec20->power_pin != -1 && ec20->power_status_pin != -1)
  819. {
  820. rt_pin_mode(ec20->power_pin, PIN_MODE_OUTPUT);
  821. rt_pin_mode(ec20->power_status_pin, PIN_MODE_INPUT);
  822. }
  823. /* initialize ec20 device network */
  824. return ec20_netdev_set_up(device->netdev);
  825. }
  826. static int ec20_deinit(struct at_device *device)
  827. {
  828. return ec20_netdev_set_down(device->netdev);
  829. }
  830. static int ec20_control(struct at_device *device, int cmd, void *arg)
  831. {
  832. int result = -RT_ERROR;
  833. RT_ASSERT(device);
  834. switch (cmd)
  835. {
  836. case AT_DEVICE_CTRL_POWER_ON:
  837. case AT_DEVICE_CTRL_POWER_OFF:
  838. case AT_DEVICE_CTRL_RESET:
  839. case AT_DEVICE_CTRL_LOW_POWER:
  840. case AT_DEVICE_CTRL_SLEEP:
  841. case AT_DEVICE_CTRL_WAKEUP:
  842. case AT_DEVICE_CTRL_NET_CONN:
  843. case AT_DEVICE_CTRL_NET_DISCONN:
  844. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  845. case AT_DEVICE_CTRL_GET_SIGNAL:
  846. case AT_DEVICE_CTRL_GET_GPS:
  847. case AT_DEVICE_CTRL_GET_VER:
  848. LOG_W("not support the control command(%d).", cmd);
  849. break;
  850. default:
  851. LOG_E("input error control command(%d).", cmd);
  852. break;
  853. }
  854. return result;
  855. }
  856. const struct at_device_ops ec20_device_ops =
  857. {
  858. ec20_init,
  859. ec20_deinit,
  860. ec20_control,
  861. };
  862. static int ec20_device_class_register(void)
  863. {
  864. struct at_device_class *class = RT_NULL;
  865. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  866. if (class == RT_NULL)
  867. {
  868. LOG_E("no memory for device class create.");
  869. return -RT_ENOMEM;
  870. }
  871. /* fill ec20 device class object */
  872. #ifdef AT_USING_SOCKET
  873. ec20_socket_class_register(class);
  874. #endif
  875. class->device_ops = &ec20_device_ops;
  876. return at_device_class_register(class, AT_DEVICE_CLASS_EC20);
  877. }
  878. INIT_DEVICE_EXPORT(ec20_device_class_register);
  879. #endif /* AT_DEVICE_USING_EC20 */