at_device_n58.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. * 2020-05-22 shuobatian first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <at_device_n58.h>
  14. #if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10300
  15. #error "This AT Client version is older, please check and update latest AT Client!"
  16. #endif
  17. #define LOG_TAG "at.dev"
  18. #include <at_log.h>
  19. #ifdef AT_DEVICE_USING_N58
  20. #define N58_WAIT_CONNECT_TIME 10000
  21. #define N58_THREAD_STACK_SIZE 2048
  22. #define N58_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
  23. // #define N58_THREAD_PRIORITY 6
  24. static void n58_power_on(struct at_device *device)
  25. {
  26. struct at_device_n58 *n58 = RT_NULL;
  27. n58 = (struct at_device_n58 *)device->user_data;
  28. /* not nead to set pin configuration for n58 device power on */
  29. #if (N58_SAMPLE_POWER_PIN != -1)
  30. #if (N58_SAMPLE_STATUS_PIN != -1)
  31. if (n58->power_pin == -1 || n58->power_status_pin == -1)
  32. {
  33. return;
  34. }
  35. if (rt_pin_read(n58->power_status_pin) == PIN_HIGH)
  36. {
  37. return;
  38. }
  39. rt_pin_write(n58->power_pin, PIN_HIGH);
  40. while (rt_pin_read(n58->power_status_pin) == PIN_LOW)
  41. {
  42. rt_thread_mdelay(10);
  43. }
  44. rt_pin_write(n58->power_pin, PIN_LOW);
  45. #else
  46. if (n58->power_pin == -1)
  47. {
  48. return;
  49. }
  50. rt_pin_write(n58->power_pin, PIN_HIGH);
  51. #endif
  52. #endif
  53. }
  54. static void n58_power_off(struct at_device *device)
  55. {
  56. struct at_device_n58 *n58 = RT_NULL;
  57. n58 = (struct at_device_n58 *)device->user_data;
  58. #if (N58_SAMPLE_POWER_PIN != -1)
  59. #if (N58_SAMPLE_STATUS_PIN != -1)
  60. /* not nead to set pin configuration for n58 device power on */
  61. if (n58->power_pin == -1 || n58->power_status_pin == -1)
  62. {
  63. return;
  64. }
  65. if (rt_pin_read(n58->power_status_pin) == PIN_LOW)
  66. {
  67. return;
  68. }
  69. rt_pin_write(n58->power_pin, PIN_HIGH);
  70. while (rt_pin_read(n58->power_status_pin) == PIN_HIGH)
  71. {
  72. rt_thread_mdelay(10);
  73. }
  74. rt_pin_write(n58->power_pin, PIN_LOW);
  75. #else
  76. if (n58->power_pin == -1)
  77. {
  78. return;
  79. }
  80. rt_pin_write(n58->power_pin, PIN_LOW);
  81. #endif
  82. #endif
  83. }
  84. /* ============================= neoway n58 network interface operations ============================= */
  85. /* set n58 network interface device status and address information */
  86. static int n58_netdev_set_info(struct netdev *netdev)
  87. {
  88. #define N58_SET_INFO_RESP_SIZE 128
  89. #define N58_INFO_RESP_TIMO rt_tick_from_millisecond(300)
  90. int result = RT_EOK;
  91. //int at_result = 0;
  92. ip_addr_t addr;
  93. at_response_t resp = RT_NULL;
  94. struct at_device *device = RT_NULL;
  95. if (netdev == RT_NULL)
  96. {
  97. LOG_E("input network interface device is NULL.");
  98. return -RT_ERROR;
  99. }
  100. LOG_D("netdev set info");
  101. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  102. if (device == RT_NULL)
  103. {
  104. LOG_E("get n58 device by netdev name(%s) failed.", netdev->name);
  105. return -RT_ERROR;
  106. }
  107. /* set network interface device status */
  108. netdev_low_level_set_status(netdev, RT_TRUE);
  109. netdev_low_level_set_link_status(netdev, RT_TRUE);
  110. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  111. resp = at_create_resp(N58_SET_INFO_RESP_SIZE, 0, N58_INFO_RESP_TIMO);
  112. if (resp == RT_NULL)
  113. {
  114. LOG_E("n58 device(%s) set IP address failed, no memory for response object.", device->name);
  115. result = -RT_ENOMEM;
  116. goto __exit;
  117. }
  118. /* set network interface device hardware address(IEMI) */
  119. {
  120. #define N58_NETDEV_HWADDR_LEN 8
  121. #define N58_IEMI_LEN 15
  122. char iemi[N58_IEMI_LEN] = {0};
  123. int i = 0, j = 0;
  124. /* send "AT+GSN" commond to get device IEMI */
  125. if (at_obj_exec_cmd(device->client, resp, "AT+CGSN") < 0)
  126. {
  127. result = -RT_ERROR;
  128. goto __exit;
  129. }
  130. if (at_resp_parse_line_args_by_kw(resp, "+CGSN:", "+CGSN: %s", iemi) <= 0)
  131. {
  132. LOG_E("n58 device(%s) IEMI get fail", device->name);
  133. result = -RT_ERROR;
  134. goto __exit;
  135. }
  136. netdev->hwaddr_len = N58_NETDEV_HWADDR_LEN;
  137. /* get hardware address by IEMI */
  138. for (i = 0, j = 0; i < N58_NETDEV_HWADDR_LEN && j < N58_IEMI_LEN; i++, j += 2)
  139. {
  140. if (j != N58_IEMI_LEN - 1)
  141. {
  142. netdev->hwaddr[i] = (iemi[j] - '0') * 10 + (iemi[j + 1] - '0');
  143. }
  144. else
  145. {
  146. netdev->hwaddr[i] = (iemi[j] - '0');
  147. }
  148. }
  149. LOG_D("hwaddr:%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X,IEMI:%s", netdev->hwaddr[0],
  150. netdev->hwaddr[1], netdev->hwaddr[2], netdev->hwaddr[3], netdev->hwaddr[4],
  151. netdev->hwaddr[5], netdev->hwaddr[6], netdev->hwaddr[7], iemi);
  152. }
  153. LOG_D("get IP address");
  154. /* get network interface device IP address */
  155. {
  156. #define IP_ADDR_SIZE_MAX 32
  157. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  158. /* send "AT+XIIC?" commond to get IP address */
  159. if (at_obj_exec_cmd(device->client, resp, "AT+XIIC?") < 0)
  160. {
  161. result = -RT_ERROR;
  162. goto __exit;
  163. }
  164. if (at_resp_parse_line_args_by_kw(resp, "+XIIC: ", "+XIIC:%*[^,],%s", ipaddr) <= 0)
  165. {
  166. LOG_E("n58 device(%s) prase \"AT+XIIC?\" commands resposne data error!", device->name);
  167. result = -RT_ERROR;
  168. goto __exit;
  169. }
  170. LOG_I("n58 device(%s) IP address: %s", device->name, ipaddr);
  171. /* set network interface address information */
  172. inet_aton(ipaddr, &addr);
  173. netdev_low_level_set_ipaddr(netdev, &addr);
  174. }
  175. /* set network interface device dns server */
  176. {
  177. #define DNS_ADDR_SIZE_MAX 16
  178. char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
  179. /* send "AT+DNSSERVER?" commond to get DNS servers address */
  180. if (at_obj_exec_cmd(device->client, resp, "AT+DNSSERVER?") < 0)
  181. {
  182. result = -RT_ERROR;
  183. goto __exit;
  184. }
  185. if (at_resp_parse_line_args_by_kw(resp, "+DNSSERVER: ", "+DNSSERVER: dns1:%[^,],dns2:%s\r\n", dns_server1, dns_server2) <= 0)
  186. {
  187. LOG_E("Prase \"AT+DNSSERVER?\" commands resposne data error!");
  188. result = -RT_ERROR;
  189. goto __exit;
  190. }
  191. LOG_D("n58 device(%s) primary DNS server address: %s", device->name, dns_server1);
  192. LOG_D("n58 device(%s) secondary DNS server address: %s", device->name, dns_server2);
  193. inet_aton(dns_server1, &addr);
  194. netdev_low_level_set_dns_server(netdev, 0, &addr);
  195. inet_aton(dns_server2, &addr);
  196. netdev_low_level_set_dns_server(netdev, 1, &addr);
  197. }
  198. __exit:
  199. if (resp)
  200. {
  201. at_delete_resp(resp);
  202. }
  203. return result;
  204. }
  205. static void check_link_status_entry(void *parameter)
  206. {
  207. #define N58_LINK_STATUS_OK 1
  208. #define N58_LINK_RESP_SIZE 64
  209. #define N58_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
  210. #define N58_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
  211. at_response_t resp = RT_NULL;
  212. int result_code, link_status;
  213. struct at_device *device = RT_NULL;
  214. #if (N58_SAMPLE_STATUS_PIN != -1)
  215. struct at_device_n58 *n58 = RT_NULL;
  216. #endif
  217. char parsed_data[10] = {0};
  218. struct netdev *netdev = (struct netdev *)parameter;
  219. LOG_D("statrt n58 device(%s) link status check \n");
  220. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  221. if (device == RT_NULL)
  222. {
  223. LOG_E("get n58 device by netdev name(%s) failed.", netdev->name);
  224. return;
  225. }
  226. #if (N58_SAMPLE_STATUS_PIN != -1)
  227. n58 = (struct at_device_n58 *)device->user_data;
  228. #endif
  229. resp = at_create_resp(N58_LINK_RESP_SIZE, 0, N58_LINK_RESP_TIMO);
  230. if (resp == RT_NULL)
  231. {
  232. LOG_E("n58 device(%s) set check link status failed, no memory for response object.", device->name);
  233. return;
  234. }
  235. while (1)
  236. {
  237. /* send "AT+CEREG?" commond to check netweork interface device link status */
  238. if (at_obj_exec_cmd(device->client, resp, "AT+CEREG?") < 0)
  239. {
  240. rt_thread_mdelay(N58_LINK_DELAY_TIME);
  241. LOG_E("n58 device(%s) send cgreg failed", device->name);
  242. continue;
  243. }
  244. link_status = -1;
  245. at_resp_parse_line_args_by_kw(resp, "+CEREG:", "+CEREG: %d,%d\r\n", &result_code, &link_status);
  246. /* check the network interface device link status */
  247. if ((N58_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
  248. {
  249. netdev_low_level_set_link_status(netdev, (N58_LINK_STATUS_OK == link_status));
  250. }
  251. #if (N58_SAMPLE_STATUS_PIN != -1)
  252. if (rt_pin_read(n58->power_status_pin) == PIN_HIGH) //check the module_status , if moduble_status is Low, user can do your logic here
  253. {
  254. #endif
  255. if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == 0)
  256. {
  257. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  258. if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  259. {
  260. LOG_D("n58 device(%s) signal strength: %s", device->name, parsed_data);
  261. }
  262. }
  263. #if (N58_SAMPLE_STATUS_PIN != -1)
  264. }
  265. else
  266. {
  267. LOG_E("netdev name(%s) status pin is low", device->name);
  268. netdev_low_level_set_link_status(netdev, RT_FALSE);
  269. return;
  270. }
  271. #endif
  272. rt_thread_mdelay(N58_LINK_DELAY_TIME);
  273. }
  274. }
  275. static int n58_netdev_check_link_status(struct netdev *netdev)
  276. {
  277. #define N58_LINK_THREAD_TICK 20
  278. #define N58_LINK_THREAD_STACK_SIZE 1024
  279. #define N58_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 22)
  280. rt_thread_t tid;
  281. char tname[RT_NAME_MAX] = {0};
  282. if (netdev == RT_NULL)
  283. {
  284. LOG_E("input network interface device is NULL.\n");
  285. return -RT_ERROR;
  286. }
  287. rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
  288. tid = rt_thread_create(tname, check_link_status_entry, (void *)netdev,
  289. N58_LINK_THREAD_STACK_SIZE, N58_LINK_THREAD_PRIORITY, N58_LINK_THREAD_TICK);
  290. if (tid)
  291. {
  292. rt_thread_startup(tid);
  293. }
  294. return RT_EOK;
  295. }
  296. static int n58_net_init(struct at_device *device);
  297. static int n58_netdev_set_up(struct netdev *netdev)
  298. {
  299. struct at_device *device = RT_NULL;
  300. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  301. if (device == RT_NULL)
  302. {
  303. LOG_E("get n58 device by netdev name(%s) failed.", netdev->name);
  304. return -RT_ERROR;
  305. }
  306. if (device->is_init == RT_FALSE)
  307. {
  308. n58_net_init(device);
  309. device->is_init = RT_TRUE;
  310. netdev_low_level_set_status(netdev, RT_TRUE);
  311. LOG_D("the network interface device(%s) set up status.", netdev->name);
  312. }
  313. return RT_EOK;
  314. }
  315. static int n58_netdev_set_down(struct netdev *netdev)
  316. {
  317. struct at_device *device = RT_NULL;
  318. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  319. if (device == RT_NULL)
  320. {
  321. LOG_E("get n58 device by netdev name(%s) failed.", netdev->name);
  322. return -RT_ERROR;
  323. }
  324. if (device->is_init == RT_TRUE)
  325. {
  326. n58_power_off(device);
  327. device->is_init = RT_FALSE;
  328. netdev_low_level_set_status(netdev, RT_FALSE);
  329. LOG_D("the network interface device(%s) set down status.", netdev->name);
  330. }
  331. return RT_EOK;
  332. }
  333. static int n58_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  334. {
  335. #define N58_DNS_RESP_LEN 128
  336. #define N58_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
  337. int result = RT_EOK;
  338. at_response_t resp = RT_NULL;
  339. struct at_device *device = RT_NULL;
  340. RT_ASSERT(netdev);
  341. RT_ASSERT(dns_server);
  342. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  343. if (device == RT_NULL)
  344. {
  345. LOG_E("get n58 device by netdev name(%s) failed.", netdev->name);
  346. }
  347. resp = at_create_resp(N58_DNS_RESP_LEN, 0, N58_DNS_RESP_TIMEO);
  348. if (resp == RT_NULL)
  349. {
  350. LOG_D("n58 set dns server failed, no memory for response object.");
  351. result = -RT_ENOMEM;
  352. goto __exit;
  353. }
  354. LOG_D("dns_num:%d,dns_server:%s", dns_num, inet_ntoa(*dns_server));
  355. /* send "AT+CDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
  356. if (at_obj_exec_cmd(device->client, resp, "AT+DNSSERVER=%d,%s", dns_num + 1, inet_ntoa(*dns_server)) < 0)
  357. {
  358. result = -RT_ERROR;
  359. goto __exit;
  360. }
  361. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  362. __exit:
  363. if (resp)
  364. {
  365. at_delete_resp(resp);
  366. }
  367. result = -RT_ERROR;
  368. return result;
  369. }
  370. #ifdef NETDEV_USING_PING
  371. static int n58_netdev_ping(struct netdev *netdev, const char *host,
  372. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
  373. #if RT_VER_NUM >= 0x50100
  374. , rt_bool_t is_bind
  375. #endif
  376. )
  377. {
  378. #define N58_PING_RESP_SIZE 512
  379. #define N58_PING_IP_SIZE 16
  380. #define N58_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  381. #define N58_PING_ERR_TIME 600
  382. #define N58_PING_ERR_TTL 255
  383. int result = RT_EOK;
  384. int bytes, time, ttl, i;
  385. char ip_addr[N58_PING_IP_SIZE] = {0};
  386. at_response_t resp = RT_NULL;
  387. struct at_device *device = RT_NULL;
  388. #if RT_VER_NUM >= 0x50100
  389. RT_UNUSED(is_bind);
  390. #endif
  391. RT_ASSERT(netdev);
  392. RT_ASSERT(host);
  393. RT_ASSERT(ping_resp);
  394. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  395. if (device == RT_NULL)
  396. {
  397. LOG_E("get n58 device by netdev name(%s) failed.", netdev->name);
  398. return -RT_ERROR;
  399. }
  400. for (i = 0; i < rt_strlen(host) && !isalpha(host[i]); i++)
  401. ;
  402. resp = at_create_resp(N58_PING_RESP_SIZE, 7, N58_PING_TIMEO);
  403. if (resp == RT_NULL)
  404. {
  405. LOG_E("n58 device(%s) set dns server failed, no memory for response object.", device->name);
  406. result = -RT_ERROR;
  407. goto __exit;
  408. }
  409. /* send "AT+PING=<ip>[,<timeout>,<size>,<num>]" commond to send ping request */
  410. /* n58 ping <size> ranges of ipv4(36-1500), ipv6(56-1500) */
  411. if (at_obj_exec_cmd(device->client, resp, "AT+PING=%s,%d,%d,1",
  412. host, N58_PING_TIMEO / (RT_TICK_PER_SECOND / 10), data_len + 4) < 0)
  413. {
  414. result = -RT_ERROR;
  415. goto __exit;
  416. }
  417. //Reply from 39.156.69.79: bytes= 36 time = 148(ms), TTL = 255
  418. //Reply from %[^:]: bytes= %d time = %d(ms), TTL = %d
  419. if (at_resp_parse_line_args_by_kw(resp, "Reply from ", "Reply from %[^:]: bytes= %d time = %d(ms), TTL = %d",
  420. ip_addr, &bytes, &time, &ttl) <= 0)
  421. {
  422. result = -RT_ERROR;
  423. goto __exit;
  424. }
  425. /* the ping request timeout expires, the response time settting to 600 and ttl setting to 255 */
  426. if (time == N58_PING_ERR_TIME && ttl == N58_PING_ERR_TTL)
  427. {
  428. result = -RT_ETIMEOUT;
  429. goto __exit;
  430. }
  431. inet_aton(ip_addr, &(ping_resp->ip_addr));
  432. ping_resp->data_len = data_len;
  433. ping_resp->ticks = time;
  434. ping_resp->ttl = ttl;
  435. __exit:
  436. if (resp)
  437. {
  438. at_delete_resp(resp);
  439. }
  440. return result;
  441. }
  442. #endif /* NETDEV_USING_PING */
  443. #ifdef NETDEV_USING_NETSTAT
  444. void n58_netdev_netstat(struct netdev *netdev)
  445. {
  446. // TODO netstat support
  447. }
  448. #endif /* NETDEV_USING_NETSTAT */
  449. const struct netdev_ops n58_netdev_ops =
  450. {
  451. n58_netdev_set_up,
  452. n58_netdev_set_down,
  453. RT_NULL, /* not support set ip, netmask, gatway address */
  454. n58_netdev_set_dns_server, //n58_netdev_set_dns_server,
  455. RT_NULL, /* not support set DHCP status */
  456. #ifdef NETDEV_USING_PING
  457. n58_netdev_ping,
  458. #endif
  459. #ifdef NETDEV_USING_NETSTAT
  460. n58_netdev_netstat,
  461. #endif
  462. };
  463. static struct netdev *n58_netdev_add(const char *netdev_name)
  464. {
  465. #define n58_NETDEV_MTU 1500
  466. struct netdev *netdev = RT_NULL;
  467. RT_ASSERT(netdev_name);
  468. netdev = netdev_get_by_name(netdev_name);
  469. if (netdev != RT_NULL)
  470. {
  471. return (netdev);
  472. }
  473. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  474. if (netdev == RT_NULL)
  475. {
  476. LOG_E("no memory for n58 device(%s) netdev structure.", netdev_name);
  477. return RT_NULL;
  478. }
  479. netdev->mtu = n58_NETDEV_MTU;
  480. netdev->ops = &n58_netdev_ops;
  481. #ifdef SAL_USING_AT
  482. extern int sal_at_netdev_set_pf_info(struct netdev * netdev);
  483. /* set the network interface socket/netdb operations */
  484. sal_at_netdev_set_pf_info(netdev);
  485. #endif
  486. netdev_register(netdev, netdev_name, RT_NULL);
  487. return netdev;
  488. }
  489. /* ============================= n58 device operations ============================= */
  490. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  491. do \
  492. { \
  493. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  494. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  495. { \
  496. result = -RT_ERROR; \
  497. goto __exit; \
  498. } \
  499. } while (0)
  500. /* init for n58 */
  501. static void n58_init_thread_entry(void *parameter)
  502. {
  503. #define INIT_RETRY 5
  504. #define CPIN_RETRY 10
  505. #define CSQ_RETRY 10
  506. #define CREG_RETRY 60
  507. #define CEREG_RETRY 30
  508. #define CCID_SIZE 20
  509. int i, retry_num = INIT_RETRY;
  510. char ccid[CCID_SIZE] = {0};
  511. char parsed_data[10] = {0};
  512. rt_err_t result = RT_EOK;
  513. at_response_t resp = RT_NULL;
  514. struct at_device *device = (struct at_device *)parameter;
  515. struct at_client *client = device->client;
  516. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  517. if (resp == RT_NULL)
  518. {
  519. LOG_E("no memory for n58 device(%s) response structure.", device->name);
  520. return;
  521. }
  522. while (retry_num--)
  523. {
  524. rt_memset(parsed_data, 0, sizeof(parsed_data));
  525. rt_thread_mdelay(1000);
  526. n58_power_on(device);
  527. rt_thread_mdelay(5000); //check the n58 hardware manual, when we use the pow_key to start n58, it takes about 20s,so we put 25s here to ensure starting n58 normally.
  528. LOG_I("start initializing the n58 device(%s)", device->name);
  529. /* wait n58 startup finish */
  530. if (at_client_obj_wait_connect(client, N58_WAIT_CONNECT_TIME))
  531. {
  532. result = -RT_ETIMEOUT;
  533. goto __exit;
  534. }
  535. /* disable echo */
  536. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  537. /* get module version */
  538. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  539. /* show module version */
  540. for (i = 0; i < (int)resp->line_counts - 1; i++)
  541. {
  542. LOG_I("%s", at_resp_get_line(resp, i + 1));
  543. }
  544. /* check SIM card */
  545. for (i = 0; i < CPIN_RETRY; i++)
  546. {
  547. at_resp_set_info(resp, 128, 2, 5 * RT_TICK_PER_SECOND);
  548. if (at_obj_exec_cmd(client, resp, "AT+CCID") < 0)
  549. {
  550. LOG_E("AT+CCID ERROR! retry:%d.", i);
  551. rt_thread_mdelay(1000);
  552. continue;
  553. }
  554. if (at_resp_parse_line_args_by_kw(resp, "+CCID:", "+CCID: %s", ccid))
  555. {
  556. LOG_I("n58 device(%s) SIM card detection success.", device->name);
  557. LOG_I("CCID: %s", ccid);
  558. break;
  559. }
  560. rt_thread_mdelay(1000);
  561. }
  562. if (i == CPIN_RETRY)
  563. {
  564. LOG_E("n58 device(%s) SIM card detection failed.", device->name);
  565. result = -RT_ERROR;
  566. goto __exit;
  567. }
  568. /* waiting for dirty data to be digested */
  569. rt_thread_mdelay(500);
  570. LOG_I("register network!");
  571. /* check the GSM network is registered */
  572. for (i = 0; i < CREG_RETRY; i++)
  573. {
  574. AT_SEND_CMD(client, resp, 0, 1000, "AT+CREG?");
  575. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  576. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  577. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  578. {
  579. LOG_I("n58 device(%s) network is registered(%s),", device->name, parsed_data);
  580. break;
  581. }
  582. rt_thread_mdelay(1000);
  583. LOG_W("AT+CREG? Retry:%d", i);
  584. }
  585. if (i == CREG_RETRY)
  586. {
  587. LOG_E("n58 device(%s) network is register failed(%s).", device->name, parsed_data);
  588. rt_thread_mdelay(10);
  589. result = -RT_ERROR;
  590. goto __exit;
  591. }
  592. /* check the GPRS network is registered */
  593. for (i = 0; i < CEREG_RETRY; i++)
  594. {
  595. AT_SEND_CMD(client, resp, 0, 300, "AT+CEREG?");
  596. at_resp_parse_line_args_by_kw(resp, "+CEREG:", "+CEREG: %s", &parsed_data);
  597. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  598. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  599. {
  600. LOG_I("n58 device(%s) GPRS network is registered(%s).", device->name, parsed_data);
  601. break;
  602. }
  603. rt_thread_mdelay(1000);
  604. }
  605. if (i == CEREG_RETRY)
  606. {
  607. LOG_E("n58 device(%s) GPRS network is register failed(%s).", device->name, parsed_data);
  608. result = -RT_ERROR;
  609. goto __exit;
  610. }
  611. /* check signal strength */
  612. for (i = 0; i < CSQ_RETRY; i++)
  613. {
  614. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  615. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  616. if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  617. {
  618. LOG_I("n58 device(%s) signal strength: %s", device->name, parsed_data);
  619. break;
  620. }
  621. rt_thread_mdelay(1000);
  622. }
  623. if (i == CSQ_RETRY)
  624. {
  625. LOG_E("n58 device(%s) signal strength check failed (%s)", device->name, parsed_data);
  626. result = -RT_ERROR;
  627. goto __exit;
  628. }
  629. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS=3,0");
  630. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
  631. at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
  632. if (rt_strcmp(parsed_data, "ChinaMobile") == 0)
  633. {
  634. /* "CMCC" */
  635. LOG_I("n58 device(%s) network operator: %s", device->name, parsed_data);
  636. }
  637. else if (rt_strcmp(parsed_data, "ChinaUnicom") == 0)
  638. {
  639. /* "UNICOM" */
  640. LOG_I("n58 device(%s) network operator: %s", device->name, parsed_data);
  641. }
  642. else if (rt_strcmp(parsed_data, "ChinaTelecom") == 0)
  643. {
  644. /* "CT" */
  645. LOG_I("n58 device(%s) network operator: %s", device->name, parsed_data);
  646. }
  647. else
  648. {
  649. LOG_E("n58 device(%s) Unknown carrier:%s", device->name, parsed_data);
  650. }
  651. /* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */
  652. AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+XIIC=1");
  653. AT_SEND_CMD(client, resp, 0, 300, "AT+XIIC?");
  654. if (at_resp_get_line_by_kw(resp, "ERROR") != RT_NULL)
  655. {
  656. LOG_E("n58 device(%s) get the local address failed.", device->name);
  657. result = -RT_ERROR;
  658. goto __exit;
  659. }
  660. result = RT_EOK;
  661. AT_SEND_CMD(client, resp, 0, 300, "AT+RECVMODE=1"); //set direct receive
  662. rt_thread_mdelay(2000); //wait for the module to process
  663. __exit:
  664. if (result == RT_EOK)
  665. {
  666. break;
  667. }
  668. else
  669. {
  670. /* power off the n58 device */
  671. n58_power_off(device);
  672. rt_thread_mdelay(1000);
  673. LOG_I("n58 device(%s) initialize retry...", device->name);
  674. }
  675. }
  676. if (resp)
  677. {
  678. at_delete_resp(resp);
  679. }
  680. if (result == RT_EOK)
  681. {
  682. /* set network interface device status and address information */
  683. n58_netdev_set_info(device->netdev);
  684. n58_netdev_check_link_status(device->netdev);
  685. LOG_I("n58 device(%s) network initialize success!", device->name);
  686. }
  687. else
  688. {
  689. LOG_E("n58 device(%s) network initialize failed(%d)!", device->name, result);
  690. }
  691. }
  692. static int n58_net_init(struct at_device *device)
  693. {
  694. #ifdef AT_DEVICE_N58_INIT_ASYN
  695. rt_thread_t tid;
  696. tid = rt_thread_create("n58_net_init", n58_init_thread_entry, (void *)device,
  697. N58_THREAD_STACK_SIZE, N58_THREAD_PRIORITY, 20);
  698. if (tid)
  699. {
  700. rt_thread_startup(tid);
  701. }
  702. else
  703. {
  704. LOG_E("create n58 device(%s) initialization thread failed.", device->name);
  705. return -RT_ERROR;
  706. }
  707. #else
  708. n58_init_thread_entry(device);
  709. #endif /* AT_DEVICE_N58_INIT_ASYN */
  710. return RT_EOK;
  711. }
  712. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  713. {
  714. RT_ASSERT(data);
  715. LOG_I("URC data : %.*s", size, data);
  716. }
  717. /* n58 device URC table for the device control */
  718. static const struct at_urc urc_table[] =
  719. {
  720. {"+PBREADY", "\r\n", urc_func},
  721. {"CLOSED", "\r\n", urc_func},
  722. };
  723. static int n58_init(struct at_device *device)
  724. {
  725. struct at_device_n58 *n58 = (struct at_device_n58 *)device->user_data;
  726. /* initialize AT client */
  727. #if RT_VER_NUM >= 0x50100
  728. at_client_init(n58->client_name, n58->recv_line_num, n58->recv_line_num);
  729. #else
  730. at_client_init(n58->client_name, n58->recv_line_num);
  731. #endif
  732. device->client = at_client_get(n58->client_name);
  733. if (device->client == RT_NULL)
  734. {
  735. LOG_E("n58 device(%s) initialize failed, get AT client(%s) failed.", n58->device_name, n58->client_name);
  736. return -RT_ERROR;
  737. }
  738. /* register URC data execution function */
  739. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  740. #ifdef AT_USING_SOCKET
  741. n58_socket_init(device);
  742. #endif
  743. /* add n58 device to the netdev list */
  744. device->netdev = n58_netdev_add(n58->device_name);
  745. if (device->netdev == RT_NULL)
  746. {
  747. LOG_E("n58 device(%s) initialize failed, get network interface device failed.", n58->device_name);
  748. return -RT_ERROR;
  749. }
  750. /* initialize n58 pin configuration */
  751. if (n58->power_pin != -1)
  752. {
  753. rt_pin_mode(n58->power_pin, PIN_MODE_OUTPUT);
  754. }
  755. if (n58->power_status_pin != -1)
  756. {
  757. rt_pin_mode(n58->power_status_pin, PIN_MODE_INPUT);
  758. }
  759. /* initialize n58 device network */
  760. return n58_netdev_set_up(device->netdev);
  761. }
  762. static int n58_deinit(struct at_device *device)
  763. {
  764. return n58_netdev_set_down(device->netdev);
  765. }
  766. static int n58_reset(struct at_device *device)
  767. {
  768. int result = RT_EOK;
  769. struct at_client *client = device->client;
  770. /* n58 only poweroff cmd,not support reboot.*/
  771. /* use power pin reboot */
  772. #if (N58_SAMPLE_POWER_PIN != -1)
  773. rt_pin_write(N58_SAMPLE_POWER_PIN, 0);
  774. rt_thread_mdelay(500);
  775. rt_pin_write(N58_SAMPLE_POWER_PIN, 1);
  776. rt_thread_mdelay(1000);
  777. /* waiting 10 seconds for n58 device reset */
  778. device->is_init = RT_FALSE;
  779. if (at_client_obj_wait_connect(client, N58_WAIT_CONNECT_TIME))
  780. {
  781. return -RT_ETIMEOUT;
  782. }
  783. /* initialize n58 device network */
  784. n58_net_init(device);
  785. device->is_init = RT_TRUE;
  786. #else
  787. result = -RT_ERROR;
  788. #endif
  789. return result;
  790. }
  791. static int n58_control(struct at_device *device, int cmd, void *arg)
  792. {
  793. int result = -RT_ERROR;
  794. RT_ASSERT(device);
  795. switch (cmd)
  796. {
  797. case AT_DEVICE_CTRL_POWER_ON:
  798. case AT_DEVICE_CTRL_POWER_OFF:
  799. case AT_DEVICE_CTRL_LOW_POWER:
  800. case AT_DEVICE_CTRL_SLEEP:
  801. case AT_DEVICE_CTRL_WAKEUP:
  802. case AT_DEVICE_CTRL_NET_CONN:
  803. case AT_DEVICE_CTRL_NET_DISCONN:
  804. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  805. case AT_DEVICE_CTRL_GET_SIGNAL:
  806. case AT_DEVICE_CTRL_GET_GPS:
  807. case AT_DEVICE_CTRL_GET_VER:
  808. LOG_W("n58 not support the control command(%d).", cmd);
  809. break;
  810. case AT_DEVICE_CTRL_RESET:
  811. result = n58_reset(device);
  812. break;
  813. default:
  814. LOG_E("input error control command(%d).", cmd);
  815. break;
  816. }
  817. return result;
  818. }
  819. const struct at_device_ops n58_device_ops =
  820. {
  821. n58_init,
  822. n58_deinit,
  823. n58_control,
  824. };
  825. static int n58_device_class_register(void)
  826. {
  827. struct at_device_class *class = RT_NULL;
  828. class = (struct at_device_class *)rt_calloc(1, sizeof(struct at_device_class));
  829. if (class == RT_NULL)
  830. {
  831. LOG_E("no memory for n58 device class create.");
  832. return -RT_ENOMEM;
  833. }
  834. /* fill n58 device class object */
  835. #ifdef AT_USING_SOCKET
  836. n58_socket_class_register(class);
  837. #endif
  838. class->device_ops = &n58_device_ops;
  839. return at_device_class_register(class, AT_DEVICE_CLASS_N58);
  840. }
  841. INIT_DEVICE_EXPORT(n58_device_class_register);
  842. #endif /* AT_DEVICE_USING_N58 */