at_device_n21.c 27 KB

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