at_device_n21.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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 = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  417. if (netdev == RT_NULL)
  418. {
  419. LOG_E("no memory for n21 device(%s) netdev structure.", netdev_name);
  420. return RT_NULL;
  421. }
  422. netdev->mtu = n21_NETDEV_MTU;
  423. netdev->ops = &n21_netdev_ops;
  424. #ifdef SAL_USING_AT
  425. extern int sal_at_netdev_set_pf_info(struct netdev * netdev);
  426. /* set the network interface socket/netdb operations */
  427. sal_at_netdev_set_pf_info(netdev);
  428. #endif
  429. netdev_register(netdev, netdev_name, RT_NULL);
  430. return netdev;
  431. }
  432. /* ============================= n21 device operations ============================= */
  433. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  434. do \
  435. { \
  436. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  437. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  438. { \
  439. result = -RT_ERROR; \
  440. goto __exit; \
  441. } \
  442. } while (0)
  443. /* init for n21 */
  444. static void n21_init_thread_entry(void *parameter)
  445. {
  446. #define INIT_RETRY 5
  447. #define CPIN_RETRY 10
  448. #define CSQ_RETRY 10
  449. #define CREG_RETRY 10
  450. #define CEREG_RETRY 30
  451. #define CCID_SIZE 20
  452. int i, retry_num = INIT_RETRY;
  453. char ccid[CCID_SIZE] = {0};
  454. char parsed_data[10] = {0};
  455. rt_err_t result = RT_EOK;
  456. at_response_t resp = RT_NULL;
  457. struct at_device *device = (struct at_device *)parameter;
  458. struct at_client *client = device->client;
  459. resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
  460. if (resp == RT_NULL)
  461. {
  462. LOG_E("no memory for n21 device(%s) response structure.", device->name);
  463. return;
  464. }
  465. while (retry_num--)
  466. {
  467. rt_memset(parsed_data, 0, sizeof(parsed_data));
  468. n21_power_on(device);
  469. 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.
  470. LOG_I("start initializing the n21 device(%s)", device->name);
  471. /* wait n21 startup finish */
  472. if (at_client_obj_wait_connect(client, N21_WAIT_CONNECT_TIME))
  473. {
  474. result = -RT_ETIMEOUT;
  475. goto __exit;
  476. }
  477. /* disable echo */
  478. AT_SEND_CMD(client, resp, 0, 300, "ATE0");
  479. /* get module version */
  480. AT_SEND_CMD(client, resp, 0, 300, "ATI");
  481. /* show module version */
  482. for (i = 0; i < (int)resp->line_counts - 1; i++)
  483. {
  484. LOG_I("%s", at_resp_get_line(resp, i + 1));
  485. }
  486. /* check SIM card */
  487. for (i = 0; i < CPIN_RETRY; i++)
  488. {
  489. at_resp_set_info(resp, 128, 2, 5 * RT_TICK_PER_SECOND);
  490. if (at_obj_exec_cmd(client, resp, "AT+CCID") < 0)
  491. {
  492. LOG_E("AT+CCID ERROR! retry:%d.", i);
  493. rt_thread_mdelay(1000);
  494. continue;
  495. }
  496. if (at_resp_parse_line_args_by_kw(resp, "+CCID:", "+CCID: %s", ccid))
  497. {
  498. LOG_I("n21 device(%s) SIM card detection success.", device->name);
  499. LOG_I("CCID: %s", ccid);
  500. break;
  501. }
  502. rt_thread_mdelay(1000);
  503. }
  504. if (i == CPIN_RETRY)
  505. {
  506. LOG_E("n21 device(%s) SIM card detection failed.", device->name);
  507. result = -RT_ERROR;
  508. goto __exit;
  509. }
  510. /* waiting for dirty data to be digested */
  511. rt_thread_mdelay(10);
  512. LOG_I("register network!");
  513. /* check the GSM network is registered */
  514. for (i = 0; i < CREG_RETRY; i++)
  515. {
  516. AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
  517. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  518. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  519. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  520. {
  521. LOG_I("n21 device(%s) network is registered(%s),", device->name, parsed_data);
  522. break;
  523. }
  524. rt_thread_mdelay(1000);
  525. }
  526. if (i == CREG_RETRY)
  527. {
  528. LOG_E("n21 device(%s) network is register failed(%s).", device->name, parsed_data);
  529. rt_thread_mdelay(10);
  530. result = -RT_ERROR;
  531. goto __exit;
  532. }
  533. /* check the GPRS network is registered */
  534. for (i = 0; i < CEREG_RETRY; i++)
  535. {
  536. AT_SEND_CMD(client, resp, 0, 300, "AT+CEREG?");
  537. at_resp_parse_line_args_by_kw(resp, "+CEREG:", "+CEREG: %s", &parsed_data);
  538. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  539. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  540. {
  541. LOG_I("n21 device(%s) GPRS network is registered(%s).", device->name, parsed_data);
  542. break;
  543. }
  544. rt_thread_mdelay(1000);
  545. }
  546. if (i == CEREG_RETRY)
  547. {
  548. LOG_E("n21 device(%s) GPRS network is register failed(%s).", device->name, parsed_data);
  549. result = -RT_ERROR;
  550. goto __exit;
  551. }
  552. /* check signal strength */
  553. for (i = 0; i < CSQ_RETRY; i++)
  554. {
  555. AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
  556. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  557. if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  558. {
  559. LOG_I("n21 device(%s) signal strength: %s", device->name, parsed_data);
  560. break;
  561. }
  562. rt_thread_mdelay(1000);
  563. }
  564. if (i == CSQ_RETRY)
  565. {
  566. LOG_E("n21 device(%s) signal strength check failed (%s)", device->name, parsed_data);
  567. result = -RT_ERROR;
  568. goto __exit;
  569. }
  570. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS=3,0");
  571. AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
  572. at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
  573. if (rt_strcmp(parsed_data, "ChinaMobile") == 0)
  574. {
  575. /* "CMCC" */
  576. LOG_I("n21 device(%s) network operator: %s", device->name, parsed_data);
  577. }
  578. else if (rt_strcmp(parsed_data, "ChinaUnicom") == 0)
  579. {
  580. /* "UNICOM" */
  581. LOG_I("n21 device(%s) network operator: %s", device->name, parsed_data);
  582. }
  583. else if (rt_strcmp(parsed_data, "ChinaTelecom") == 0)
  584. {
  585. /* "CT" */
  586. LOG_I("n21 device(%s) network operator: %s", device->name, parsed_data);
  587. }
  588. else
  589. {
  590. LOG_E("n21 device(%s) Unknown carrier:%s", device->name, parsed_data);
  591. }
  592. /* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */
  593. AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+XIIC=1");
  594. AT_SEND_CMD(client, resp, 0, 300, "AT+XIIC?");
  595. if (at_resp_get_line_by_kw(resp, "ERROR") != RT_NULL)
  596. {
  597. LOG_E("n21 device(%s) get the local address failed.", device->name);
  598. result = -RT_ERROR;
  599. goto __exit;
  600. }
  601. result = RT_EOK;
  602. AT_SEND_CMD(client, resp, 0, 300, "AT+RECVMODE=1"); //set direct receive
  603. __exit:
  604. if (result == RT_EOK)
  605. {
  606. break;
  607. }
  608. else
  609. {
  610. /* power off the n21 device */
  611. n21_power_off(device);
  612. rt_thread_mdelay(1000);
  613. LOG_I("n21 device(%s) initialize retry...", device->name);
  614. }
  615. }
  616. if (resp)
  617. {
  618. at_delete_resp(resp);
  619. }
  620. if (result == RT_EOK)
  621. {
  622. /* set network interface device status and address information */
  623. n21_netdev_set_info(device->netdev);
  624. /* check and create link staus sync thread */
  625. if (rt_thread_find(device->netdev->name) == RT_NULL)
  626. {
  627. n21_netdev_check_link_status(device->netdev);
  628. }
  629. LOG_I("n21 device(%s) network initialize success!", device->name);
  630. }
  631. else
  632. {
  633. LOG_E("n21 device(%s) network initialize failed(%d)!", device->name, result);
  634. }
  635. }
  636. static int n21_net_init(struct at_device *device)
  637. {
  638. #ifdef AT_DEVICE_N21_INIT_ASYN
  639. rt_thread_t tid;
  640. tid = rt_thread_create("n21_net_init", n21_init_thread_entry, (void *)device,
  641. N21_THREAD_STACK_SIZE, N21_THREAD_PRIORITY, 20);
  642. if (tid)
  643. {
  644. rt_thread_startup(tid);
  645. }
  646. else
  647. {
  648. LOG_E("create n21 device(%s) initialization thread failed.", device->name);
  649. return -RT_ERROR;
  650. }
  651. #else
  652. n21_init_thread_entry(device);
  653. #endif /* AT_DEVICE_N21_INIT_ASYN */
  654. return RT_EOK;
  655. }
  656. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  657. {
  658. RT_ASSERT(data);
  659. LOG_I("URC data : %.*s", size, data);
  660. }
  661. /* n21 device URC table for the device control */
  662. static const struct at_urc urc_table[] =
  663. {
  664. {"+PBREADY", "\r\n", urc_func},
  665. {"CLOSED", "\r\n", urc_func},
  666. };
  667. static int n21_init(struct at_device *device)
  668. {
  669. struct at_device_n21 *n21 = (struct at_device_n21 *)device->user_data;
  670. /* initialize AT client */
  671. at_client_init(n21->client_name, n21->recv_line_num);
  672. device->client = at_client_get(n21->client_name);
  673. if (device->client == RT_NULL)
  674. {
  675. LOG_E("n21 device(%s) initialize failed, get AT client(%s) failed.", n21->device_name, n21->client_name);
  676. return -RT_ERROR;
  677. }
  678. /* register URC data execution function */
  679. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  680. #ifdef AT_USING_SOCKET
  681. n21_socket_init(device);
  682. #endif
  683. /* add n21 device to the netdev list */
  684. device->netdev = n21_netdev_add(n21->device_name);
  685. if (device->netdev == RT_NULL)
  686. {
  687. LOG_E("n21 device(%s) initialize failed, get network interface device failed.", n21->device_name);
  688. return -RT_ERROR;
  689. }
  690. /* initialize n21 pin configuration */
  691. if (n21->power_pin != -1)
  692. {
  693. rt_pin_mode(n21->power_pin, PIN_MODE_OUTPUT);
  694. }
  695. if (n21->power_status_pin != -1)
  696. {
  697. rt_pin_mode(n21->power_status_pin, PIN_MODE_INPUT);
  698. }
  699. /* initialize n21 device network */
  700. return n21_netdev_set_up(device->netdev);
  701. }
  702. static int n21_deinit(struct at_device *device)
  703. {
  704. return n21_netdev_set_down(device->netdev);
  705. }
  706. static int n21_reset(struct at_device *device)
  707. {
  708. int result = RT_EOK;
  709. struct at_client *client = device->client;
  710. /* n21 only poweroff cmd,not support reboot.*/
  711. /* use power pin reboot */
  712. #if (N21_SAMPLE_POWER_PIN != -1)
  713. rt_pin_write(N21_SAMPLE_POWER_PIN, 0);
  714. rt_thread_mdelay(500);
  715. rt_pin_write(N21_SAMPLE_POWER_PIN, 1);
  716. rt_thread_mdelay(1000);
  717. /* waiting 10 seconds for n21 device reset */
  718. device->is_init = RT_FALSE;
  719. if (at_client_obj_wait_connect(client, N21_WAIT_CONNECT_TIME))
  720. {
  721. return -RT_ETIMEOUT;
  722. }
  723. /* initialize n21 device network */
  724. n21_net_init(device);
  725. device->is_init = RT_TRUE;
  726. #else
  727. result = -RT_ERROR;
  728. #endif
  729. return result;
  730. }
  731. static int n21_control(struct at_device *device, int cmd, void *arg)
  732. {
  733. int result = -RT_ERROR;
  734. RT_ASSERT(device);
  735. switch (cmd)
  736. {
  737. case AT_DEVICE_CTRL_POWER_ON:
  738. case AT_DEVICE_CTRL_POWER_OFF:
  739. case AT_DEVICE_CTRL_LOW_POWER:
  740. case AT_DEVICE_CTRL_SLEEP:
  741. case AT_DEVICE_CTRL_WAKEUP:
  742. case AT_DEVICE_CTRL_NET_CONN:
  743. case AT_DEVICE_CTRL_NET_DISCONN:
  744. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  745. case AT_DEVICE_CTRL_GET_SIGNAL:
  746. case AT_DEVICE_CTRL_GET_GPS:
  747. case AT_DEVICE_CTRL_GET_VER:
  748. LOG_W("n21 not support the control command(%d).", cmd);
  749. break;
  750. case AT_DEVICE_CTRL_RESET:
  751. result = n21_reset(device);
  752. break;
  753. default:
  754. LOG_E("input error control command(%d).", cmd);
  755. break;
  756. }
  757. return result;
  758. }
  759. const struct at_device_ops n21_device_ops =
  760. {
  761. n21_init,
  762. n21_deinit,
  763. n21_control,
  764. };
  765. static int n21_device_class_register(void)
  766. {
  767. struct at_device_class *class = RT_NULL;
  768. class = (struct at_device_class *)rt_calloc(1, sizeof(struct at_device_class));
  769. if (class == RT_NULL)
  770. {
  771. LOG_E("no memory for n21 device class create.");
  772. return -RT_ENOMEM;
  773. }
  774. /* fill n21 device class object */
  775. #ifdef AT_USING_SOCKET
  776. n21_socket_class_register(class);
  777. #endif
  778. class->device_ops = &n21_device_ops;
  779. return at_device_class_register(class, AT_DEVICE_CLASS_N21);
  780. }
  781. INIT_DEVICE_EXPORT(n21_device_class_register);
  782. #endif /* AT_DEVICE_USING_N21 */