at_device_n21.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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_n21.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_N21
  20. #define N21_WAIT_CONNECT_TIME 10000
  21. #define N21_THREAD_STACK_SIZE 2048
  22. #define N21_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
  23. // #define N21_THREAD_PRIORITY 6
  24. static void n21_power_on(struct at_device *device)
  25. {
  26. struct at_device_n21 *n21 = RT_NULL;
  27. n21 = (struct at_device_n21 *)device->user_data;
  28. /* not nead to set pin configuration for n21 device power on */
  29. #if (N21_SAMPLE_POWER_PIN != -1)
  30. #if (N21_SAMPLE_STATUS_PIN != -1)
  31. if (n21->power_pin == -1 || n21->power_status_pin == -1)
  32. {
  33. return;
  34. }
  35. if (rt_pin_read(n21->power_status_pin) == PIN_HIGH)
  36. {
  37. return;
  38. }
  39. rt_pin_write(n21->power_pin, PIN_HIGH);
  40. while (rt_pin_read(n21->power_status_pin) == PIN_LOW)
  41. {
  42. rt_thread_mdelay(10);
  43. }
  44. rt_pin_write(n21->power_pin, PIN_LOW);
  45. #else
  46. if (n21->power_pin == -1)
  47. {
  48. return;
  49. }
  50. rt_pin_write(n21->power_pin, PIN_HIGH);
  51. #endif
  52. #endif
  53. }
  54. static void n21_power_off(struct at_device *device)
  55. {
  56. struct at_device_n21 *n21 = RT_NULL;
  57. n21 = (struct at_device_n21 *)device->user_data;
  58. #if (N21_SAMPLE_POWER_PIN != -1)
  59. #if (N21_SAMPLE_STATUS_PIN != -1)
  60. /* not nead to set pin configuration for m26 device power on */
  61. if (n21->power_pin == -1 || n21->power_status_pin == -1)
  62. {
  63. return;
  64. }
  65. if (rt_pin_read(n21->power_status_pin) == PIN_LOW)
  66. {
  67. return;
  68. }
  69. rt_pin_write(n21->power_pin, PIN_HIGH);
  70. while (rt_pin_read(n21->power_status_pin) == PIN_HIGH)
  71. {
  72. rt_thread_mdelay(10);
  73. }
  74. rt_pin_write(n21->power_pin, PIN_LOW);
  75. #else
  76. if (n21->power_pin == -1)
  77. {
  78. return;
  79. }
  80. rt_pin_write(n21->power_pin, PIN_LOW);
  81. #endif
  82. #endif
  83. }
  84. /* ============================= neoway n21 network interface operations ============================= */
  85. /* set n21 network interface device status and address information */
  86. static int n21_netdev_set_info(struct netdev *netdev)
  87. {
  88. #define N21_SET_INFO_RESP_SIZE 128
  89. #define N21_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 n21 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(N21_SET_INFO_RESP_SIZE, 0, N21_INFO_RESP_TIMO);
  112. if (resp == RT_NULL)
  113. {
  114. LOG_E("n21 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 N21_NETDEV_HWADDR_LEN 8
  121. #define N21_IEMI_LEN 15
  122. char iemi[N21_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("n21 device(%s) IEMI get fail", device->name);
  133. result = -RT_ERROR;
  134. goto __exit;
  135. }
  136. netdev->hwaddr_len = N21_NETDEV_HWADDR_LEN;
  137. /* get hardware address by IEMI */
  138. for (i = 0, j = 0; i < N21_NETDEV_HWADDR_LEN && j < N21_IEMI_LEN; i++, j += 2)
  139. {
  140. if (j != N21_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("n21 device(%s) prase \"AT+XIIC?\" commands resposne data error!", device->name);
  167. result = -RT_ERROR;
  168. goto __exit;
  169. }
  170. LOG_I("n21 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. //n21 not support inquire dns
  176. __exit:
  177. if (resp)
  178. {
  179. at_delete_resp(resp);
  180. }
  181. return result;
  182. }
  183. static void check_link_status_entry(void *parameter)
  184. {
  185. #define N21_LINK_STATUS_OK 1
  186. #define N21_LINK_RESP_SIZE 64
  187. #define N21_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
  188. #define N21_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
  189. at_response_t resp = RT_NULL;
  190. int result_code, link_status;
  191. struct at_device *device = RT_NULL;
  192. #if (N21_SAMPLE_STATUS_PIN != -1)
  193. struct at_device_n21 *n21 = RT_NULL;
  194. #endif
  195. char parsed_data[10] = {0};
  196. struct netdev *netdev = (struct netdev *)parameter;
  197. LOG_D("statrt n21 device(%s) link status check \n");
  198. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  199. if (device == RT_NULL)
  200. {
  201. LOG_E("get n21 device by netdev name(%s) failed.", netdev->name);
  202. return;
  203. }
  204. #if (N21_SAMPLE_STATUS_PIN != -1)
  205. n21 = (struct at_device_n21 *)device->user_data;
  206. #endif
  207. resp = at_create_resp(N21_LINK_RESP_SIZE, 0, N21_LINK_RESP_TIMO);
  208. if (resp == RT_NULL)
  209. {
  210. LOG_E("n21 device(%s) set check link status failed, no memory for response object.", device->name);
  211. return;
  212. }
  213. while (1)
  214. {
  215. /* send "AT+CEREG?" commond to check netweork interface device link status */
  216. if (at_obj_exec_cmd(device->client, resp, "AT+CEREG?") < 0)
  217. {
  218. rt_thread_mdelay(N21_LINK_DELAY_TIME);
  219. LOG_E("n21 device(%s) send cgreg failed", device->name);
  220. continue;
  221. }
  222. link_status = -1;
  223. at_resp_parse_line_args_by_kw(resp, "+CEREG:", "+CEREG: %d,%d\r\n", &result_code, &link_status);
  224. /* check the network interface device link status */
  225. if ((N21_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
  226. {
  227. netdev_low_level_set_link_status(netdev, (N21_LINK_STATUS_OK == link_status));
  228. }
  229. #if (N21_SAMPLE_STATUS_PIN != -1)
  230. if (rt_pin_read(n21->power_status_pin) == PIN_HIGH) //check the module_status , if moduble_status is Low, user can do your logic here
  231. {
  232. #endif
  233. if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == 0)
  234. {
  235. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  236. if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  237. {
  238. LOG_W("n21 device(%s) signal strength: %s", device->name, parsed_data);
  239. }
  240. }
  241. #if (N21_SAMPLE_STATUS_PIN != -1)
  242. }
  243. else
  244. {
  245. LOG_E("netdev name(%s) status pin is low", device->name);
  246. netdev_low_level_set_link_status(netdev, RT_FALSE);
  247. return;
  248. }
  249. #endif
  250. rt_thread_mdelay(N21_LINK_DELAY_TIME);
  251. }
  252. }
  253. static int n21_netdev_check_link_status(struct netdev *netdev)
  254. {
  255. #define N21_LINK_THREAD_TICK 20
  256. #define N21_LINK_THREAD_STACK_SIZE 1024
  257. #define N21_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 22)
  258. rt_thread_t tid;
  259. char tname[RT_NAME_MAX] = {0};
  260. if (netdev == RT_NULL)
  261. {
  262. LOG_E("input network interface device is NULL.\n");
  263. return -RT_ERROR;
  264. }
  265. rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
  266. tid = rt_thread_create(tname, check_link_status_entry, (void *)netdev,
  267. N21_LINK_THREAD_STACK_SIZE, N21_LINK_THREAD_PRIORITY, N21_LINK_THREAD_TICK);
  268. if (tid)
  269. {
  270. rt_thread_startup(tid);
  271. }
  272. return RT_EOK;
  273. }
  274. static int n21_net_init(struct at_device *device);
  275. static int n21_netdev_set_up(struct netdev *netdev)
  276. {
  277. struct at_device *device = RT_NULL;
  278. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  279. if (device == RT_NULL)
  280. {
  281. LOG_E("get n21 device by netdev name(%s) failed.", netdev->name);
  282. return -RT_ERROR;
  283. }
  284. if (device->is_init == RT_FALSE)
  285. {
  286. n21_net_init(device);
  287. device->is_init = RT_TRUE;
  288. netdev_low_level_set_status(netdev, RT_TRUE);
  289. LOG_D("the network interface device(%s) set up status.", netdev->name);
  290. }
  291. return RT_EOK;
  292. }
  293. static int n21_netdev_set_down(struct netdev *netdev)
  294. {
  295. struct at_device *device = RT_NULL;
  296. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  297. if (device == RT_NULL)
  298. {
  299. LOG_E("get n21 device by netdev name(%s) failed.", netdev->name);
  300. return -RT_ERROR;
  301. }
  302. if (device->is_init == RT_TRUE)
  303. {
  304. n21_power_off(device);
  305. device->is_init = RT_FALSE;
  306. netdev_low_level_set_status(netdev, RT_FALSE);
  307. LOG_D("the network interface device(%s) set down status.", netdev->name);
  308. }
  309. return RT_EOK;
  310. }
  311. #ifdef NETDEV_USING_PING
  312. static int n21_netdev_ping(struct netdev *netdev, const char *host,
  313. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
  314. #if RT_VER_NUM >= 0x50100
  315. , rt_bool_t is_bind
  316. #endif
  317. )
  318. {
  319. #define N21_PING_RESP_SIZE 512
  320. #define N21_PING_IP_SIZE 16
  321. #define N21_PING_TIMEO (5 * RT_TICK_PER_SECOND)
  322. #define N21_PING_ERR_TIME 600
  323. #define N21_PING_ERR_TTL 255
  324. int result = RT_EOK;
  325. int bytes, time, ttl, i;
  326. char ip_addr[N21_PING_IP_SIZE] = {0};
  327. at_response_t resp = RT_NULL;
  328. struct at_device *device = RT_NULL;
  329. #if RT_VER_NUM >= 0x50100
  330. RT_UNUSED(is_bind);
  331. #endif
  332. RT_ASSERT(netdev);
  333. RT_ASSERT(host);
  334. RT_ASSERT(ping_resp);
  335. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  336. if (device == RT_NULL)
  337. {
  338. LOG_E("get n21 device by netdev name(%s) failed.", netdev->name);
  339. return -RT_ERROR;
  340. }
  341. for (i = 0; i < rt_strlen(host) && !isalpha(host[i]); i++)
  342. ;
  343. resp = at_create_resp(N21_PING_RESP_SIZE, 7, N21_PING_TIMEO);
  344. if (resp == RT_NULL)
  345. {
  346. LOG_E("n21 device(%s) set dns server failed, no memory for response object.", device->name);
  347. result = -RT_ERROR;
  348. goto __exit;
  349. }
  350. /* send "AT+PING=<ip>[,<timeout>,<size>,<num>]" commond to send ping request */
  351. /* n21 ping <size> ranges of ipv4(36-1500), ipv6(56-1500) */
  352. if (at_obj_exec_cmd(device->client, resp, "AT+PING=%s,%d,%d,1",
  353. host, N21_PING_TIMEO / (RT_TICK_PER_SECOND / 10), data_len + 4) < 0)
  354. {
  355. result = -RT_ERROR;
  356. goto __exit;
  357. }
  358. //Reply from 39.156.69.79: bytes= 36 time = 148(ms), TTL = 255
  359. //Reply from %[^:]: bytes= %d time = %d(ms), TTL = %d
  360. if (at_resp_parse_line_args_by_kw(resp, "Reply from ", "Reply from %[^:]: bytes= %d time = %d(ms), TTL = %d",
  361. ip_addr, &bytes, &time, &ttl) <= 0)
  362. {
  363. result = -RT_ERROR;
  364. goto __exit;
  365. }
  366. /* the ping request timeout expires, the response time settting to 600 and ttl setting to 255 */
  367. if (time == N21_PING_ERR_TIME && ttl == N21_PING_ERR_TTL)
  368. {
  369. result = -RT_ETIMEOUT;
  370. goto __exit;
  371. }
  372. inet_aton(ip_addr, &(ping_resp->ip_addr));
  373. ping_resp->data_len = data_len;
  374. ping_resp->ticks = time;
  375. ping_resp->ttl = ttl;
  376. __exit:
  377. if (resp)
  378. {
  379. at_delete_resp(resp);
  380. }
  381. return result;
  382. }
  383. #endif /* NETDEV_USING_PING */
  384. #ifdef NETDEV_USING_NETSTAT
  385. void n21_netdev_netstat(struct netdev *netdev)
  386. {
  387. // TODO netstat support
  388. }
  389. #endif /* NETDEV_USING_NETSTAT */
  390. const struct netdev_ops n21_netdev_ops =
  391. {
  392. n21_netdev_set_up,
  393. n21_netdev_set_down,
  394. RT_NULL, /* not support set ip, netmask, gatway address */
  395. RT_NULL, /* not support set dns server */
  396. RT_NULL, /* not support set DHCP status */
  397. #ifdef NETDEV_USING_PING
  398. n21_netdev_ping,
  399. #endif
  400. #ifdef NETDEV_USING_NETSTAT
  401. n21_netdev_netstat,
  402. #endif
  403. };
  404. static struct netdev *n21_netdev_add(const char *netdev_name)
  405. {
  406. #define n21_NETDEV_MTU 1500
  407. struct netdev *netdev = RT_NULL;
  408. RT_ASSERT(netdev_name);
  409. netdev = netdev_get_by_name(netdev_name);
  410. if (netdev != RT_NULL)
  411. {
  412. return (netdev);
  413. }
  414. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  415. if (netdev == RT_NULL)
  416. {
  417. LOG_E("no memory for n21 device(%s) netdev structure.", netdev_name);
  418. return RT_NULL;
  419. }
  420. netdev->mtu = n21_NETDEV_MTU;
  421. netdev->ops = &n21_netdev_ops;
  422. #ifdef SAL_USING_AT
  423. extern int sal_at_netdev_set_pf_info(struct netdev * netdev);
  424. /* set the network interface socket/netdb operations */
  425. sal_at_netdev_set_pf_info(netdev);
  426. #endif
  427. netdev_register(netdev, netdev_name, RT_NULL);
  428. return netdev;
  429. }
  430. /* ============================= n21 device operations ============================= */
  431. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  432. do \
  433. { \
  434. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  435. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  436. { \
  437. result = -RT_ERROR; \
  438. goto __exit; \
  439. } \
  440. } while (0)
  441. /* init for n21 */
  442. static void n21_init_thread_entry(void *parameter)
  443. {
  444. #define INIT_RETRY 5
  445. #define CPIN_RETRY 10
  446. #define CSQ_RETRY 10
  447. #define CREG_RETRY 10
  448. #define CEREG_RETRY 30
  449. #define CCID_SIZE 20
  450. int i, retry_num = INIT_RETRY;
  451. char ccid[CCID_SIZE] = {0};
  452. char parsed_data[10] = {0};
  453. rt_err_t result = RT_EOK;
  454. at_response_t resp = RT_NULL;
  455. struct at_device *device = (struct at_device *)parameter;
  456. struct at_client *client = device->client;
  457. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  458. if (resp == RT_NULL)
  459. {
  460. LOG_E("no memory for n21 device(%s) response structure.", device->name);
  461. return;
  462. }
  463. while (retry_num--)
  464. {
  465. rt_memset(parsed_data, 0, sizeof(parsed_data));
  466. n21_power_on(device);
  467. rt_thread_mdelay(5000); //check the n21 hardware manual, when we use the pow_key to start n21, it takes about 20s,so we put 25s here to ensure starting n21 normally.
  468. LOG_I("start initializing the n21 device(%s)", device->name);
  469. /* wait n21 startup finish */
  470. if (at_client_obj_wait_connect(client, N21_WAIT_CONNECT_TIME))
  471. {
  472. result = -RT_ETIMEOUT;
  473. goto __exit;
  474. }
  475. /* disable echo */
  476. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  477. /* get module version */
  478. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  479. /* show module version */
  480. for (i = 0; i < (int)resp->line_counts - 1; i++)
  481. {
  482. LOG_I("%s", at_resp_get_line(resp, i + 1));
  483. }
  484. /* check SIM card */
  485. for (i = 0; i < CPIN_RETRY; i++)
  486. {
  487. at_resp_set_info(resp, 128, 2, 5 * RT_TICK_PER_SECOND);
  488. if (at_obj_exec_cmd(client, resp, "AT+CCID") < 0)
  489. {
  490. LOG_E("AT+CCID ERROR! retry:%d.", i);
  491. rt_thread_mdelay(1000);
  492. continue;
  493. }
  494. if (at_resp_parse_line_args_by_kw(resp, "+CCID:", "+CCID: %s", ccid))
  495. {
  496. LOG_I("n21 device(%s) SIM card detection success.", device->name);
  497. LOG_I("CCID: %s", ccid);
  498. break;
  499. }
  500. rt_thread_mdelay(1000);
  501. }
  502. if (i == CPIN_RETRY)
  503. {
  504. LOG_E("n21 device(%s) SIM card detection failed.", device->name);
  505. result = -RT_ERROR;
  506. goto __exit;
  507. }
  508. /* waiting for dirty data to be digested */
  509. rt_thread_mdelay(10);
  510. LOG_I("register network!");
  511. /* check the GSM network is registered */
  512. for (i = 0; i < CREG_RETRY; i++)
  513. {
  514. AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
  515. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  516. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  517. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  518. {
  519. LOG_I("n21 device(%s) network is registered(%s),", device->name, parsed_data);
  520. break;
  521. }
  522. rt_thread_mdelay(1000);
  523. }
  524. if (i == CREG_RETRY)
  525. {
  526. LOG_E("n21 device(%s) network is register failed(%s).", device->name, parsed_data);
  527. rt_thread_mdelay(10);
  528. result = -RT_ERROR;
  529. goto __exit;
  530. }
  531. /* check the GPRS network is registered */
  532. for (i = 0; i < CEREG_RETRY; i++)
  533. {
  534. AT_SEND_CMD(client, resp, 0, 300, "AT+CEREG?");
  535. at_resp_parse_line_args_by_kw(resp, "+CEREG:", "+CEREG: %s", &parsed_data);
  536. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  537. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  538. {
  539. LOG_I("n21 device(%s) GPRS network is registered(%s).", device->name, parsed_data);
  540. break;
  541. }
  542. rt_thread_mdelay(1000);
  543. }
  544. if (i == CEREG_RETRY)
  545. {
  546. LOG_E("n21 device(%s) GPRS network is register failed(%s).", device->name, parsed_data);
  547. result = -RT_ERROR;
  548. goto __exit;
  549. }
  550. /* check signal strength */
  551. for (i = 0; i < CSQ_RETRY; i++)
  552. {
  553. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  554. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  555. if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  556. {
  557. LOG_I("n21 device(%s) signal strength: %s", device->name, parsed_data);
  558. break;
  559. }
  560. rt_thread_mdelay(1000);
  561. }
  562. if (i == CSQ_RETRY)
  563. {
  564. LOG_E("n21 device(%s) signal strength check failed (%s)", device->name, parsed_data);
  565. result = -RT_ERROR;
  566. goto __exit;
  567. }
  568. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS=3,0");
  569. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
  570. at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
  571. if (rt_strcmp(parsed_data, "ChinaMobile") == 0)
  572. {
  573. /* "CMCC" */
  574. LOG_I("n21 device(%s) network operator: %s", device->name, parsed_data);
  575. }
  576. else if (rt_strcmp(parsed_data, "ChinaUnicom") == 0)
  577. {
  578. /* "UNICOM" */
  579. LOG_I("n21 device(%s) network operator: %s", device->name, parsed_data);
  580. }
  581. else if (rt_strcmp(parsed_data, "ChinaTelecom") == 0)
  582. {
  583. /* "CT" */
  584. LOG_I("n21 device(%s) network operator: %s", device->name, parsed_data);
  585. }
  586. else
  587. {
  588. LOG_E("n21 device(%s) Unknown carrier:%s", device->name, parsed_data);
  589. }
  590. /* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */
  591. AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+XIIC=1");
  592. AT_SEND_CMD(client, resp, 0, 300, "AT+XIIC?");
  593. if (at_resp_get_line_by_kw(resp, "ERROR") != RT_NULL)
  594. {
  595. LOG_E("n21 device(%s) get the local address failed.", device->name);
  596. result = -RT_ERROR;
  597. goto __exit;
  598. }
  599. result = RT_EOK;
  600. AT_SEND_CMD(client, resp, 0, 300, "AT+RECVMODE=1"); //set direct receive
  601. __exit:
  602. if (result == RT_EOK)
  603. {
  604. break;
  605. }
  606. else
  607. {
  608. /* power off the n21 device */
  609. n21_power_off(device);
  610. rt_thread_mdelay(1000);
  611. LOG_I("n21 device(%s) initialize retry...", device->name);
  612. }
  613. }
  614. if (resp)
  615. {
  616. at_delete_resp(resp);
  617. }
  618. if (result == RT_EOK)
  619. {
  620. /* set network interface device status and address information */
  621. n21_netdev_set_info(device->netdev);
  622. /* check and create link staus sync thread */
  623. if (rt_thread_find(device->netdev->name) == RT_NULL)
  624. {
  625. n21_netdev_check_link_status(device->netdev);
  626. }
  627. LOG_I("n21 device(%s) network initialize success!", device->name);
  628. }
  629. else
  630. {
  631. LOG_E("n21 device(%s) network initialize failed(%d)!", device->name, result);
  632. }
  633. }
  634. static int n21_net_init(struct at_device *device)
  635. {
  636. #ifdef AT_DEVICE_N21_INIT_ASYN
  637. rt_thread_t tid;
  638. tid = rt_thread_create("n21_net_init", n21_init_thread_entry, (void *)device,
  639. N21_THREAD_STACK_SIZE, N21_THREAD_PRIORITY, 20);
  640. if (tid)
  641. {
  642. rt_thread_startup(tid);
  643. }
  644. else
  645. {
  646. LOG_E("create n21 device(%s) initialization thread failed.", device->name);
  647. return -RT_ERROR;
  648. }
  649. #else
  650. n21_init_thread_entry(device);
  651. #endif /* AT_DEVICE_N21_INIT_ASYN */
  652. return RT_EOK;
  653. }
  654. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  655. {
  656. RT_ASSERT(data);
  657. LOG_I("URC data : %.*s", size, data);
  658. }
  659. /* n21 device URC table for the device control */
  660. static const struct at_urc urc_table[] =
  661. {
  662. {"+PBREADY", "\r\n", urc_func},
  663. {"CLOSED", "\r\n", urc_func},
  664. };
  665. static int n21_init(struct at_device *device)
  666. {
  667. struct at_device_n21 *n21 = (struct at_device_n21 *)device->user_data;
  668. /* initialize AT client */
  669. #if RT_VER_NUM >= 0x50100
  670. at_client_init(n21->client_name, n21->recv_line_num, n21->recv_line_num);
  671. #else
  672. at_client_init(n21->client_name, n21->recv_line_num);
  673. #endif
  674. device->client = at_client_get(n21->client_name);
  675. if (device->client == RT_NULL)
  676. {
  677. LOG_E("n21 device(%s) initialize failed, get AT client(%s) failed.", n21->device_name, n21->client_name);
  678. return -RT_ERROR;
  679. }
  680. /* register URC data execution function */
  681. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  682. #ifdef AT_USING_SOCKET
  683. n21_socket_init(device);
  684. #endif
  685. /* add n21 device to the netdev list */
  686. device->netdev = n21_netdev_add(n21->device_name);
  687. if (device->netdev == RT_NULL)
  688. {
  689. LOG_E("n21 device(%s) initialize failed, get network interface device failed.", n21->device_name);
  690. return -RT_ERROR;
  691. }
  692. /* initialize n21 pin configuration */
  693. if (n21->power_pin != -1)
  694. {
  695. rt_pin_mode(n21->power_pin, PIN_MODE_OUTPUT);
  696. }
  697. if (n21->power_status_pin != -1)
  698. {
  699. rt_pin_mode(n21->power_status_pin, PIN_MODE_INPUT);
  700. }
  701. /* initialize n21 device network */
  702. return n21_netdev_set_up(device->netdev);
  703. }
  704. static int n21_deinit(struct at_device *device)
  705. {
  706. return n21_netdev_set_down(device->netdev);
  707. }
  708. static int n21_reset(struct at_device *device)
  709. {
  710. int result = RT_EOK;
  711. struct at_client *client = device->client;
  712. /* n21 only poweroff cmd,not support reboot.*/
  713. /* use power pin reboot */
  714. #if (N21_SAMPLE_POWER_PIN != -1)
  715. rt_pin_write(N21_SAMPLE_POWER_PIN, 0);
  716. rt_thread_mdelay(500);
  717. rt_pin_write(N21_SAMPLE_POWER_PIN, 1);
  718. rt_thread_mdelay(1000);
  719. /* waiting 10 seconds for n21 device reset */
  720. device->is_init = RT_FALSE;
  721. if (at_client_obj_wait_connect(client, N21_WAIT_CONNECT_TIME))
  722. {
  723. return -RT_ETIMEOUT;
  724. }
  725. /* initialize n21 device network */
  726. n21_net_init(device);
  727. device->is_init = RT_TRUE;
  728. #else
  729. result = -RT_ERROR;
  730. #endif
  731. return result;
  732. }
  733. static int n21_control(struct at_device *device, int cmd, void *arg)
  734. {
  735. int result = -RT_ERROR;
  736. RT_ASSERT(device);
  737. switch (cmd)
  738. {
  739. case AT_DEVICE_CTRL_POWER_ON:
  740. case AT_DEVICE_CTRL_POWER_OFF:
  741. case AT_DEVICE_CTRL_LOW_POWER:
  742. case AT_DEVICE_CTRL_SLEEP:
  743. case AT_DEVICE_CTRL_WAKEUP:
  744. case AT_DEVICE_CTRL_NET_CONN:
  745. case AT_DEVICE_CTRL_NET_DISCONN:
  746. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  747. case AT_DEVICE_CTRL_GET_SIGNAL:
  748. case AT_DEVICE_CTRL_GET_GPS:
  749. case AT_DEVICE_CTRL_GET_VER:
  750. LOG_W("n21 not support the control command(%d).", cmd);
  751. break;
  752. case AT_DEVICE_CTRL_RESET:
  753. result = n21_reset(device);
  754. break;
  755. default:
  756. LOG_E("input error control command(%d).", cmd);
  757. break;
  758. }
  759. return result;
  760. }
  761. const struct at_device_ops n21_device_ops =
  762. {
  763. n21_init,
  764. n21_deinit,
  765. n21_control,
  766. };
  767. static int n21_device_class_register(void)
  768. {
  769. struct at_device_class *class = RT_NULL;
  770. class = (struct at_device_class *)rt_calloc(1, sizeof(struct at_device_class));
  771. if (class == RT_NULL)
  772. {
  773. LOG_E("no memory for n21 device class create.");
  774. return -RT_ENOMEM;
  775. }
  776. /* fill n21 device class object */
  777. #ifdef AT_USING_SOCKET
  778. n21_socket_class_register(class);
  779. #endif
  780. class->device_ops = &n21_device_ops;
  781. return at_device_class_register(class, AT_DEVICE_CLASS_N21);
  782. }
  783. INIT_DEVICE_EXPORT(n21_device_class_register);
  784. #endif /* AT_DEVICE_USING_N21 */