at_device_ec20.c 35 KB

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