at_socket_rw007.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * File : at_socket_rw007.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-06-20 chenyong first version
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <rtthread.h>
  27. #include <sys/socket.h>
  28. #include <at.h>
  29. #include <at_socket.h>
  30. #if !defined(RT_USING_NETDEV)
  31. #error "This RT-Thread version is older, please check and updata laster RT-Thread!"
  32. #else
  33. #include <arpa/inet.h>
  34. #include <netdev.h>
  35. #endif /* RT_USING_NETDEV */
  36. #if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10200
  37. #error "This AT Client version is older, please check and update latest AT Client!"
  38. #endif
  39. #define LOG_TAG "at.rw007"
  40. #include <at_log.h>
  41. #ifdef AT_DEVICE_RW007
  42. #define RW007_NETDEV_NAME "rw007"
  43. #define RW007_MODULE_SEND_MAX_SIZE 2048
  44. #define RW007_WAIT_CONNECT_TIME 5000
  45. #define RW007_THREAD_STACK_SIZE 1024
  46. #define RW007_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  47. /* set real event by current socket and current state */
  48. #define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
  49. /* AT socket event type */
  50. #define RW007_EVENT_CONN_OK (1L << 0)
  51. #define RW007_EVENT_SEND_OK (1L << 1)
  52. #define RW007_EVENT_RECV_OK (1L << 2)
  53. #define RW007_EVNET_CLOSE_OK (1L << 3)
  54. #define RW007_EVENT_CONN_FAIL (1L << 4)
  55. #define RW007_EVENT_SEND_FAIL (1L << 5)
  56. static int cur_socket;
  57. static int cur_send_bfsz;
  58. static rt_event_t at_socket_event;
  59. static rt_mutex_t at_event_lock;
  60. static at_evt_cb_t at_evt_cb_set[] = {
  61. [AT_SOCKET_EVT_RECV] = NULL,
  62. [AT_SOCKET_EVT_CLOSED] = NULL,
  63. };
  64. static int at_socket_event_send(uint32_t event)
  65. {
  66. return (int) rt_event_send(at_socket_event, event);
  67. }
  68. static int at_socket_event_recv(uint32_t event, uint32_t timeout, rt_uint8_t option)
  69. {
  70. int result = 0;
  71. rt_uint32_t recved;
  72. result = rt_event_recv(at_socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
  73. if (result != RT_EOK)
  74. {
  75. return -RT_ETIMEOUT;
  76. }
  77. return recved;
  78. }
  79. /**
  80. * close socket by AT commands.
  81. *
  82. * @param current socket
  83. *
  84. * @return 0: close socket success
  85. * -1: send AT commands error
  86. * -2: wait socket event timeout
  87. * -5: no memory
  88. */
  89. static int rw007_socket_close(int socket)
  90. {
  91. at_response_t resp = RT_NULL;
  92. int result = RT_EOK;
  93. resp = at_create_resp(64, 0, rt_tick_from_millisecond(5000));
  94. if (!resp)
  95. {
  96. LOG_E("No memory for response structure!");
  97. return -RT_ENOMEM;
  98. }
  99. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  100. if (at_exec_cmd(resp, "AT+CIPCLOSE=%d", socket) < 0)
  101. {
  102. result = -RT_ERROR;
  103. goto __exit;
  104. }
  105. __exit:
  106. rt_mutex_release(at_event_lock);
  107. if (resp)
  108. {
  109. at_delete_resp(resp);
  110. }
  111. return result;
  112. }
  113. /**
  114. * create TCP/UDP client or server connect by AT commands.
  115. *
  116. * @param socket current socket
  117. * @param ip server or client IP address
  118. * @param port server or client port
  119. * @param type connect socket type(tcp, udp)
  120. * @param is_client connection is client
  121. *
  122. * @return 0: connect success
  123. * -1: connect failed, send commands error or type error
  124. * -2: wait socket event timeout
  125. * -5: no memory
  126. */
  127. static int rw007_socket_connect(int socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
  128. {
  129. at_response_t resp = RT_NULL;
  130. int result = RT_EOK;
  131. rt_bool_t retryed = RT_FALSE;
  132. RT_ASSERT(ip);
  133. RT_ASSERT(port >= 0);
  134. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  135. if (!resp)
  136. {
  137. LOG_E("No memory for response structure!");
  138. return -RT_ENOMEM;
  139. }
  140. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  141. __retry:
  142. if (is_client)
  143. {
  144. switch (type)
  145. {
  146. case AT_SOCKET_TCP:
  147. /* send AT commands to connect TCP server */
  148. if (at_exec_cmd(resp, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d,60", socket, ip, port) < 0)
  149. {
  150. result = -RT_ERROR;
  151. }
  152. break;
  153. case AT_SOCKET_UDP:
  154. if (at_exec_cmd(resp, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d", socket, ip, port) < 0)
  155. {
  156. result = -RT_ERROR;
  157. }
  158. break;
  159. default:
  160. LOG_E("Not supported connect type : %d.", type);
  161. result = -RT_ERROR;
  162. goto __exit;
  163. }
  164. }
  165. if (result != RT_EOK && !retryed)
  166. {
  167. LOG_D("socket (%d) connect failed, maybe the socket was not be closed at the last time and now will retry.", socket);
  168. if (rw007_socket_close(socket) < 0)
  169. {
  170. goto __exit;
  171. }
  172. retryed = RT_TRUE;
  173. result = RT_EOK;
  174. goto __retry;
  175. }
  176. __exit:
  177. rt_mutex_release(at_event_lock);
  178. if (resp)
  179. {
  180. at_delete_resp(resp);
  181. }
  182. return result;
  183. }
  184. /**
  185. * send data to server or client by AT commands.
  186. *
  187. * @param socket current socket
  188. * @param buff send buffer
  189. * @param bfsz send buffer size
  190. * @param type connect socket type(tcp, udp)
  191. *
  192. * @return >=0: the size of send success
  193. * -1: send AT commands error or send data error
  194. * -2: waited socket event timeout
  195. * -5: no memory
  196. */
  197. static int rw007_socket_send(int socket, const char *buff, size_t bfsz, enum at_socket_type type)
  198. {
  199. int result = RT_EOK;
  200. int event_result = 0;
  201. at_response_t resp = RT_NULL;
  202. size_t cur_pkt_size = 0, sent_size = 0;
  203. RT_ASSERT(buff);
  204. RT_ASSERT(bfsz > 0);
  205. resp = at_create_resp(128, 2, rt_tick_from_millisecond(5000));
  206. if (!resp)
  207. {
  208. LOG_E("No memory for response structure!");
  209. return -RT_ENOMEM;
  210. }
  211. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  212. /* set current socket for send URC event */
  213. cur_socket = socket;
  214. /* set AT client end sign to deal with '>' sign.*/
  215. at_set_end_sign('>');
  216. while (sent_size < bfsz)
  217. {
  218. if (bfsz - sent_size < RW007_MODULE_SEND_MAX_SIZE)
  219. {
  220. cur_pkt_size = bfsz - sent_size;
  221. }
  222. else
  223. {
  224. cur_pkt_size = RW007_MODULE_SEND_MAX_SIZE;
  225. }
  226. /* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
  227. if (at_exec_cmd(resp, "AT+CIPSEND=%d,%d", socket, cur_pkt_size) < 0)
  228. {
  229. result = -RT_ERROR;
  230. goto __exit;
  231. }
  232. /* send the real data to server or client */
  233. result = (int) at_client_send(buff + sent_size, cur_pkt_size);
  234. if (result == 0)
  235. {
  236. result = -RT_ERROR;
  237. goto __exit;
  238. }
  239. /* waiting result event from AT URC */
  240. if (at_socket_event_recv(SET_EVENT(socket, 0), rt_tick_from_millisecond(5 * 1000), RT_EVENT_FLAG_OR) < 0)
  241. {
  242. LOG_E("socket (%d) send failed, wait connect result timeout.", socket);
  243. result = -RT_ETIMEOUT;
  244. goto __exit;
  245. }
  246. /* waiting OK or failed result */
  247. if ((event_result = at_socket_event_recv(RW007_EVENT_SEND_OK | RW007_EVENT_SEND_FAIL, rt_tick_from_millisecond(5 * 1000),
  248. RT_EVENT_FLAG_OR)) < 0)
  249. {
  250. LOG_E("socket (%d) send failed, wait connect OK|FAIL timeout.", socket);
  251. result = -RT_ETIMEOUT;
  252. goto __exit;
  253. }
  254. /* check result */
  255. if (event_result & RW007_EVENT_SEND_FAIL)
  256. {
  257. LOG_E("socket (%d) send failed, return failed.", socket);
  258. result = -RT_ERROR;
  259. goto __exit;
  260. }
  261. if (type == AT_SOCKET_TCP)
  262. {
  263. cur_pkt_size = cur_send_bfsz;
  264. }
  265. sent_size += cur_pkt_size;
  266. }
  267. __exit:
  268. /* reset the end sign for data */
  269. at_set_end_sign(0);
  270. rt_mutex_release(at_event_lock);
  271. if (resp)
  272. {
  273. at_delete_resp(resp);
  274. }
  275. return result;
  276. }
  277. /**
  278. * domain resolve by AT commands.
  279. *
  280. * @param name domain name
  281. * @param ip parsed IP address, it's length must be 16
  282. *
  283. * @return 0: domain resolve success
  284. * -2: wait socket event timeout
  285. * -5: no memory
  286. */
  287. static int rw007_domain_resolve(const char *name, char ip[16])
  288. {
  289. #define RESOLVE_RETRY 5
  290. int i, result = RT_EOK;
  291. char recv_ip[16] = { 0 };
  292. at_response_t resp = RT_NULL;
  293. RT_ASSERT(name);
  294. RT_ASSERT(ip);
  295. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  296. if (!resp)
  297. {
  298. LOG_E("No memory for response structure!");
  299. return -RT_ENOMEM;
  300. }
  301. rt_mutex_take(at_event_lock, RT_WAITING_FOREVER);
  302. for(i = 0; i < RESOLVE_RETRY; i++)
  303. {
  304. if (at_exec_cmd(resp, "AT+CIPDOMAIN=\"%s\"", name) < 0)
  305. {
  306. result = -RT_ERROR;
  307. goto __exit;
  308. }
  309. /* parse the third line of response data, get the IP address */
  310. if(at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%s", recv_ip) < 0)
  311. {
  312. rt_thread_delay(rt_tick_from_millisecond(100));
  313. /* resolve failed, maybe receive an URC CRLF */
  314. continue;
  315. }
  316. if (strlen(recv_ip) < 8)
  317. {
  318. rt_thread_delay(rt_tick_from_millisecond(100));
  319. /* resolve failed, maybe receive an URC CRLF */
  320. continue;
  321. }
  322. else
  323. {
  324. strncpy(ip, recv_ip, 15);
  325. ip[15] = '\0';
  326. break;
  327. }
  328. }
  329. __exit:
  330. rt_mutex_release(at_event_lock);
  331. if (resp)
  332. {
  333. at_delete_resp(resp);
  334. }
  335. return result;
  336. }
  337. /**
  338. * set AT socket event notice callback
  339. *
  340. * @param event notice event
  341. * @param cb notice callback
  342. */
  343. static void rw007_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
  344. {
  345. if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
  346. {
  347. at_evt_cb_set[event] = cb;
  348. }
  349. }
  350. static void urc_send_func(const char *data, rt_size_t size)
  351. {
  352. RT_ASSERT(data && size);
  353. if (strstr(data, "SEND OK"))
  354. {
  355. at_socket_event_send(SET_EVENT(cur_socket, RW007_EVENT_SEND_OK));
  356. }
  357. else if (strstr(data, "SEND FAIL"))
  358. {
  359. at_socket_event_send(SET_EVENT(cur_socket, RW007_EVENT_SEND_FAIL));
  360. }
  361. }
  362. static void urc_send_bfsz_func(const char *data, rt_size_t size)
  363. {
  364. int send_bfsz = 0;
  365. RT_ASSERT(data && size);
  366. sscanf(data, "Recv %d bytes", &send_bfsz);
  367. cur_send_bfsz = send_bfsz;
  368. }
  369. static void urc_close_func(const char *data, rt_size_t size)
  370. {
  371. int socket = 0;
  372. RT_ASSERT(data && size);
  373. sscanf(data, "%d,CLOSED", &socket);
  374. /* notice the socket is disconnect by remote */
  375. if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
  376. {
  377. at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
  378. }
  379. }
  380. static void urc_recv_func(const char *data, rt_size_t size)
  381. {
  382. int socket = 0;
  383. rt_size_t bfsz = 0, temp_size = 0;
  384. rt_int32_t timeout;
  385. char *recv_buf = RT_NULL, temp[8];
  386. RT_ASSERT(data && size);
  387. /* get the current socket and receive buffer size by receive data */
  388. sscanf(data, "+IPD,%d,%d:", &socket, (int *) &bfsz);
  389. /* get receive timeout by receive buffer length */
  390. timeout = bfsz;
  391. if (socket < 0 || bfsz == 0)
  392. return;
  393. recv_buf = rt_calloc(1, bfsz);
  394. if (!recv_buf)
  395. {
  396. LOG_E("no memory for URC receive buffer (%d)!", bfsz);
  397. /* read and clean the coming data */
  398. while (temp_size < bfsz)
  399. {
  400. if (bfsz - temp_size > sizeof(temp))
  401. {
  402. at_client_recv(temp, sizeof(temp), timeout);
  403. }
  404. else
  405. {
  406. at_client_recv(temp, bfsz - temp_size, timeout);
  407. }
  408. temp_size += sizeof(temp);
  409. }
  410. return;
  411. }
  412. /* sync receive data */
  413. if (at_client_recv(recv_buf, bfsz, timeout) != bfsz)
  414. {
  415. LOG_E("receive size(%d) data failed!", bfsz);
  416. rt_free(recv_buf);
  417. return;
  418. }
  419. /* notice the receive buffer and buffer size */
  420. if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
  421. {
  422. at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
  423. }
  424. }
  425. static void urc_busy_p_func(const char *data, rt_size_t size)
  426. {
  427. RT_ASSERT(data && size);
  428. LOG_D("system is processing a commands and it cannot respond to the current commands.");
  429. }
  430. static void urc_busy_s_func(const char *data, rt_size_t size)
  431. {
  432. RT_ASSERT(data && size);
  433. LOG_D("system is sending data and it cannot respond to the current commands.");
  434. }
  435. static void urc_func(const char *data, rt_size_t size)
  436. {
  437. RT_ASSERT(data && size);
  438. if(strstr(data, "WIFI CONNECTED"))
  439. {
  440. LOG_I("RW007 WIFI is connected.");
  441. }
  442. else if(strstr(data, "WIFI DISCONNECT"))
  443. {
  444. LOG_I("RW007 WIFI is disconnect.");
  445. }
  446. }
  447. static struct at_urc urc_table[] = {
  448. {"SEND OK", "\r\n", urc_send_func},
  449. {"SEND FAIL", "\r\n", urc_send_func},
  450. {"Recv", "bytes\r\n", urc_send_bfsz_func},
  451. {"", ",CLOSED\r\n", urc_close_func},
  452. {"+IPD", ":", urc_recv_func},
  453. {"busy p", "\r\n", urc_busy_p_func},
  454. {"busy s", "\r\n", urc_busy_s_func},
  455. {"WIFI CONNECTED", "\r\n", urc_func},
  456. {"WIFI DISCONNECT", "\r\n", urc_func},
  457. };
  458. #define AT_SEND_CMD(resp, cmd) \
  459. do \
  460. { \
  461. if (at_exec_cmd(at_resp_set_info(resp, 256, 0, rt_tick_from_millisecond(5000)), cmd) < 0) \
  462. { \
  463. LOG_E("RT AT send commands(%s) error!", cmd); \
  464. result = -RT_ERROR; \
  465. goto __exit; \
  466. } \
  467. } while(0); \
  468. static void rw007_init_thread_entry(void *parameter)
  469. {
  470. at_response_t resp = RT_NULL;
  471. rt_err_t result = RT_EOK;
  472. rt_size_t i;
  473. resp = at_create_resp(128, 0, rt_tick_from_millisecond(5000));
  474. if (!resp)
  475. {
  476. LOG_E("No memory for response structure!");
  477. result = -RT_ENOMEM;
  478. goto __exit;
  479. }
  480. rt_thread_delay(rt_tick_from_millisecond(5000));
  481. /* reset module */
  482. AT_SEND_CMD(resp, "AT+RST");
  483. /* reset waiting delay */
  484. rt_thread_delay(rt_tick_from_millisecond(1000));
  485. /* disable echo */
  486. AT_SEND_CMD(resp, "ATE0");
  487. /* set current mode to Wi-Fi station */
  488. AT_SEND_CMD(resp, "AT+CWMODE=1");
  489. /* get module version */
  490. AT_SEND_CMD(resp, "AT+GMR");
  491. /* show module version */
  492. for (i = 0; i < resp->line_counts - 1; i++)
  493. {
  494. LOG_D("%s", at_resp_get_line(resp, i + 1));
  495. }
  496. /* connect to WiFi AP */
  497. if (at_exec_cmd(at_resp_set_info(resp, 128, 0, 20 * RT_TICK_PER_SECOND), "AT+CWJAP=\"%s\",\"%s\"",
  498. AT_DEVICE_WIFI_SSID, AT_DEVICE_WIFI_PASSWORD) != RT_EOK)
  499. {
  500. LOG_E("AT network initialize failed, check ssid(%s) and password(%s).", AT_DEVICE_WIFI_SSID, AT_DEVICE_WIFI_PASSWORD);
  501. result = -RT_ERROR;
  502. goto __exit;
  503. }
  504. AT_SEND_CMD(resp, "AT+CIPMUX=1");
  505. __exit:
  506. if (resp)
  507. {
  508. at_delete_resp(resp);
  509. }
  510. if (!result)
  511. {
  512. LOG_I("AT network initialize success!");
  513. }
  514. else
  515. {
  516. LOG_E("AT network initialize failed (%d)!", result);
  517. }
  518. }
  519. int rw007_net_init(void)
  520. {
  521. #ifdef PKG_AT_INIT_BY_THREAD
  522. rt_thread_t tid;
  523. tid = rt_thread_create("rw007_net_init", rw007_init_thread_entry, RT_NULL,RW007_THREAD_STACK_SIZE, RW007_THREAD_PRIORITY, 20);
  524. if (tid)
  525. {
  526. rt_thread_startup(tid);
  527. }
  528. else
  529. {
  530. LOG_E("Create AT initialization thread fail!");
  531. }
  532. #else
  533. rw007_init_thread_entry(RT_NULL);
  534. #endif
  535. return RT_EOK;
  536. }
  537. int rw007_ping(int argc, char **argv)
  538. {
  539. at_response_t resp = RT_NULL;
  540. static int icmp_seq;
  541. int req_time;
  542. if (argc != 2)
  543. {
  544. rt_kprintf("Please input: at_ping <host address>\n");
  545. return -RT_ERROR;
  546. }
  547. resp = at_create_resp(64, 0, rt_tick_from_millisecond(5000));
  548. if (!resp)
  549. {
  550. rt_kprintf("No memory for response structure!\n");
  551. return -RT_ENOMEM;
  552. }
  553. for(icmp_seq = 1; icmp_seq <= 4; icmp_seq++)
  554. {
  555. if (at_exec_cmd(resp, "AT+PING=\"%s\"", argv[1]) < 0)
  556. {
  557. rt_kprintf("ping: unknown remote server host\n");
  558. at_delete_resp(resp);
  559. return -RT_ERROR;
  560. }
  561. if(at_resp_parse_line_args_by_kw(resp, "+", "+%d", &req_time) < 0)
  562. {
  563. continue;
  564. }
  565. if (req_time)
  566. {
  567. rt_kprintf("32 bytes from %s icmp_seq=%d time=%d ms\n", argv[1], icmp_seq, req_time);
  568. }
  569. }
  570. if (resp)
  571. {
  572. at_delete_resp(resp);
  573. }
  574. return RT_EOK;
  575. }
  576. #ifdef FINSH_USING_MSH
  577. #include <finsh.h>
  578. MSH_CMD_EXPORT_ALIAS(rw007_net_init, at_net_init, initialize AT network);
  579. MSH_CMD_EXPORT_ALIAS(rw007_ping, at_ping, AT ping network host);
  580. #endif
  581. static const struct at_device_ops rw007_socket_ops = {
  582. rw007_socket_connect,
  583. rw007_socket_close,
  584. rw007_socket_send,
  585. rw007_domain_resolve,
  586. rw007_socket_set_event_cb,
  587. };
  588. static int rw007_netdev_add(const char *netdev_name)
  589. {
  590. #define ETHERNET_MTU 1500
  591. #define HWADDR_LEN 8
  592. struct netdev *netdev = RT_NULL;
  593. int result = 0;
  594. RT_ASSERT(netdev_name);
  595. netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
  596. if (netdev == RT_NULL)
  597. {
  598. return RT_NULL;
  599. }
  600. /* TODO: improve netdev adaptation */
  601. netdev->mtu = ETHERNET_MTU;
  602. netdev->hwaddr_len = HWADDR_LEN;
  603. netdev->ops = RT_NULL;
  604. #ifdef SAL_USING_AT
  605. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  606. /* set the network interface socket/netdb operations */
  607. sal_at_netdev_set_pf_info(netdev);
  608. #endif
  609. result = netdev_register(netdev, netdev_name, RT_NULL);
  610. /*TODO: improve netdev adaptation */
  611. netdev_low_level_set_status(netdev, RT_TRUE);
  612. netdev_low_level_set_link_status(netdev, RT_TRUE);
  613. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  614. netdev->flags |= NETDEV_FLAG_INTERNET_UP;
  615. return result;
  616. }
  617. static int at_socket_device_init(void)
  618. {
  619. /* create current AT socket event */
  620. at_socket_event = rt_event_create("at_se", RT_IPC_FLAG_FIFO);
  621. if (at_socket_event == RT_NULL)
  622. {
  623. LOG_E("RT AT client port initialize failed! at_sock_event create failed!");
  624. return -RT_ENOMEM;
  625. }
  626. /* create current AT socket event lock */
  627. at_event_lock = rt_mutex_create("at_se", RT_IPC_FLAG_FIFO);
  628. if (at_event_lock == RT_NULL)
  629. {
  630. LOG_E("RT AT client port initialize failed! at_sock_lock create failed!");
  631. rt_event_delete(at_socket_event);
  632. return -RT_ENOMEM;
  633. }
  634. /* initialize AT client */
  635. at_client_init(AT_DEVICE_NAME, AT_DEVICE_RECV_BUFF_LEN);
  636. /* register URC data execution function */
  637. at_set_urc_table(urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  638. /* add network interface device to netdev list */
  639. if (rw007_netdev_add(RW007_NETDEV_NAME) < 0)
  640. {
  641. LOG_E("RW007 network interface device(%d) add failed.", RW007_NETDEV_NAME);
  642. return -RT_ENOMEM;
  643. }
  644. /* initialize rw007 network */
  645. rw007_net_init();
  646. /* set rw007 AT Socket options */
  647. at_socket_device_register(&rw007_socket_ops);
  648. return RT_EOK;
  649. }
  650. INIT_APP_EXPORT(at_socket_device_init);
  651. #endif /* AT_DEVICE_RW007 */