at_socket.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * File : at_socket.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-06 chenyong first version
  23. */
  24. #include <at.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <sys/time.h>
  29. #include <at_socket.h>
  30. #ifdef SAL_USING_POSIX
  31. #include <dfs_poll.h>
  32. #endif
  33. #ifdef DBG_SECTION_NAME
  34. #undef DBG_SECTION_NAME
  35. #define DBG_SECTION_NAME "AT_SOC"
  36. #endif
  37. #define HTONS_PORT(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
  38. #define NIPQUAD(addr) \
  39. ((unsigned char *)&addr)[0], \
  40. ((unsigned char *)&addr)[1], \
  41. ((unsigned char *)&addr)[2], \
  42. ((unsigned char *)&addr)[3]
  43. #if !defined(AT_DEVICE_SOCKETS_NUM) || defined(AT_DEVICE_NOT_SELECTED)
  44. #error The AT socket device is not selected, please select it through the env menuconfig.
  45. #endif
  46. /* The maximum number of sockets structure */
  47. #ifndef AT_SOCKETS_NUM
  48. #define AT_SOCKETS_NUM AT_DEVICE_SOCKETS_NUM
  49. #endif
  50. typedef enum {
  51. AT_EVENT_SEND,
  52. AT_EVENT_RECV,
  53. AT_EVENT_ERROR,
  54. } at_event_t;
  55. /* the global array of available sockets */
  56. static struct at_socket sockets[AT_SOCKETS_NUM] = { 0 };
  57. /* AT device socket options */
  58. static struct at_device_ops *at_dev_ops = RT_NULL;
  59. struct at_socket *at_get_socket(int socket)
  60. {
  61. if (socket < 0 || socket >= AT_SOCKETS_NUM)
  62. {
  63. return RT_NULL;
  64. }
  65. /* check socket structure valid or not */
  66. if (sockets[socket].magic != AT_SOCKET_MAGIC)
  67. {
  68. return RT_NULL;
  69. }
  70. return &sockets[socket];
  71. }
  72. /* get a block to the AT socket receive list*/
  73. static size_t at_recvpkt_put(rt_slist_t *rlist, const char *ptr, size_t length)
  74. {
  75. at_recv_pkt_t pkt;
  76. pkt = (at_recv_pkt_t) rt_calloc(1, sizeof(struct at_recv_pkt));
  77. if (pkt == RT_NULL)
  78. {
  79. LOG_E("No memory for receive packet table!");
  80. return 0;
  81. }
  82. pkt->bfsz_totle = length;
  83. pkt->bfsz_index = 0;
  84. pkt->buff = (char *) ptr;
  85. rt_slist_append(rlist, &pkt->list);
  86. return length;
  87. }
  88. /* delete and free all receive buffer list */
  89. static int at_recvpkt_all_delete(rt_slist_t *rlist)
  90. {
  91. at_recv_pkt_t pkt;
  92. rt_slist_t *node;
  93. if(rt_slist_isempty(rlist))
  94. return 0;
  95. for(node = rt_slist_first(rlist); node; node = rt_slist_next(node))
  96. {
  97. pkt = rt_slist_entry(node, struct at_recv_pkt, list);
  98. if (pkt->buff)
  99. {
  100. rt_free(pkt->buff);
  101. }
  102. if(pkt)
  103. {
  104. rt_free(pkt);
  105. pkt = RT_NULL;
  106. }
  107. }
  108. return 0;
  109. }
  110. /* delete and free specified list block */
  111. static int at_recvpkt_node_delete(rt_slist_t *rlist, rt_slist_t *node)
  112. {
  113. at_recv_pkt_t pkt;
  114. if(rt_slist_isempty(rlist))
  115. return 0;
  116. rt_slist_remove(rlist, node);
  117. pkt= rt_slist_entry(node, struct at_recv_pkt, list);
  118. if (pkt->buff)
  119. {
  120. rt_free(pkt->buff);
  121. }
  122. if (pkt)
  123. {
  124. rt_free(pkt);
  125. pkt = RT_NULL;
  126. }
  127. return 0;
  128. }
  129. /* get a block from AT socket receive list */
  130. static size_t at_recvpkt_get(rt_slist_t *rlist, char *mem, size_t len)
  131. {
  132. rt_slist_t *node;
  133. at_recv_pkt_t pkt;
  134. size_t content_pos = 0, page_pos = 0;
  135. if(rt_slist_isempty(rlist))
  136. return 0;
  137. for (node = rt_slist_first(rlist); node; node = rt_slist_next(node))
  138. {
  139. pkt = rt_slist_entry(node, struct at_recv_pkt, list);
  140. page_pos = pkt->bfsz_totle - pkt->bfsz_index;
  141. if (page_pos >= len - content_pos)
  142. {
  143. memcpy((char *) mem + content_pos, pkt->buff + pkt->bfsz_index, len - content_pos);
  144. pkt->bfsz_index += len - content_pos;
  145. if (pkt->bfsz_index == pkt->bfsz_totle)
  146. {
  147. at_recvpkt_node_delete(rlist, node);
  148. }
  149. content_pos = len;
  150. break;
  151. }
  152. else
  153. {
  154. memcpy((char *) mem + content_pos, pkt->buff + pkt->bfsz_index, page_pos);
  155. content_pos += page_pos;
  156. pkt->bfsz_index += page_pos;
  157. at_recvpkt_node_delete(rlist, node);
  158. }
  159. }
  160. return content_pos;
  161. }
  162. static void at_do_event_changes(struct at_socket *sock, at_event_t event, rt_bool_t is_plus)
  163. {
  164. switch (event)
  165. {
  166. case AT_EVENT_SEND:
  167. {
  168. if (is_plus)
  169. {
  170. sock->sendevent++;
  171. #ifdef SAL_USING_POSIX
  172. rt_wqueue_wakeup(&sock->wait_head, (void*) POLLOUT);
  173. #endif
  174. }
  175. else if (sock->sendevent)
  176. {
  177. sock->sendevent --;
  178. }
  179. break;
  180. }
  181. case AT_EVENT_RECV:
  182. {
  183. if (is_plus)
  184. {
  185. sock->rcvevent++;
  186. #ifdef SAL_USING_POSIX
  187. rt_wqueue_wakeup(&sock->wait_head, (void*) POLLIN);
  188. #endif
  189. }
  190. else if (sock->rcvevent)
  191. {
  192. sock->rcvevent --;
  193. }
  194. break;
  195. }
  196. case AT_EVENT_ERROR:
  197. {
  198. if (is_plus)
  199. {
  200. sock->errevent++;
  201. #ifdef SAL_USING_POSIX
  202. rt_wqueue_wakeup(&sock->wait_head, (void*) POLLERR);
  203. #endif
  204. }
  205. else if (sock->errevent)
  206. {
  207. sock->errevent --;
  208. }
  209. break;
  210. }
  211. default:
  212. LOG_E("Not supported event (%d)", event);
  213. }
  214. }
  215. static struct at_socket *alloc_socket(void)
  216. {
  217. static rt_mutex_t at_slock = RT_NULL;
  218. char name[RT_NAME_MAX];
  219. struct at_socket *sock;
  220. int idx;
  221. if(at_slock == RT_NULL)
  222. {
  223. /* create AT socket lock */
  224. at_slock = rt_mutex_create("at_s", RT_IPC_FLAG_FIFO);
  225. if (at_slock == RT_NULL)
  226. {
  227. LOG_E("No memory for AT socket lock!");
  228. return RT_NULL;
  229. }
  230. }
  231. rt_mutex_take(at_slock, RT_WAITING_FOREVER);
  232. /* find an empty at socket entry */
  233. for (idx = 0; idx < AT_SOCKETS_NUM && sockets[idx].magic; idx++);
  234. /* can't find an empty protocol family entry */
  235. if (idx == AT_SOCKETS_NUM)
  236. {
  237. goto __err;
  238. }
  239. sock = &(sockets[idx]);
  240. sock->magic = AT_SOCKET_MAGIC;
  241. sock->socket = idx;
  242. sock->state = AT_SOCKET_NONE;
  243. sock->rcvevent = RT_NULL;
  244. sock->sendevent = RT_NULL;
  245. sock->errevent = RT_NULL;
  246. rt_slist_init(&sock->recvpkt_list);
  247. rt_snprintf(name, RT_NAME_MAX, "%s%d", "at_sr", idx);
  248. /* create AT socket receive mailbox */
  249. if ((sock->recv_notice = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO)) == RT_NULL)
  250. {
  251. goto __err;
  252. }
  253. rt_snprintf(name, RT_NAME_MAX, "%s%d", "at_sr", idx);
  254. /* create AT socket receive ring buffer lock */
  255. if((sock->recv_lock = rt_mutex_create(name, RT_IPC_FLAG_FIFO)) == RT_NULL)
  256. {
  257. goto __err;
  258. }
  259. rt_mutex_release(at_slock);
  260. return sock;
  261. __err:
  262. rt_mutex_release(at_slock);
  263. return RT_NULL;
  264. }
  265. int at_socket(int domain, int type, int protocol)
  266. {
  267. struct at_socket *sock;
  268. enum at_socket_type socket_type;
  269. /* check socket family protocol */
  270. RT_ASSERT(domain == AF_AT||domain == AF_INET);
  271. //TODO check protocol
  272. switch(type)
  273. {
  274. case SOCK_STREAM:
  275. socket_type = AT_SOCKET_TCP;
  276. break;
  277. case SOCK_DGRAM:
  278. socket_type = AT_SOCKET_UDP;
  279. break;
  280. default :
  281. LOG_E("Don't support socket type (%d)!", type);
  282. return -1;
  283. }
  284. /* allocate and initialize a new AT socket */
  285. sock = alloc_socket();
  286. if(sock == RT_NULL)
  287. {
  288. LOG_E("Allocate a new AT socket failed!");
  289. return RT_NULL;
  290. }
  291. sock->type = socket_type;
  292. #ifdef SAL_USING_POSIX
  293. rt_wqueue_init(&sock->wait_head);
  294. #endif
  295. return sock->socket;
  296. }
  297. static int free_socket(struct at_socket *sock)
  298. {
  299. if (sock->recv_notice)
  300. {
  301. rt_sem_delete(sock->recv_notice);
  302. }
  303. if (sock->recv_lock)
  304. {
  305. rt_mutex_delete(sock->recv_lock);
  306. }
  307. if (!rt_slist_isempty(&sock->recvpkt_list))
  308. {
  309. at_recvpkt_all_delete(&sock->recvpkt_list);
  310. }
  311. memset(sock, 0x00, sizeof(struct at_socket));
  312. return 0;
  313. }
  314. int at_closesocket(int socket)
  315. {
  316. struct at_socket *sock;
  317. enum at_socket_state last_state;
  318. if (at_dev_ops == RT_NULL)
  319. {
  320. return -1;
  321. }
  322. /* deal with TCP server actively disconnect */
  323. rt_thread_delay(rt_tick_from_millisecond(100));
  324. sock = at_get_socket(socket);
  325. if (sock == RT_NULL)
  326. {
  327. return -1;
  328. }
  329. last_state = sock->state;
  330. /* the rt_at_socket_close is need some time, so change state in advance */
  331. sock->state = AT_SOCKET_CLOSED;
  332. if (last_state == AT_SOCKET_CONNECT)
  333. {
  334. if (at_dev_ops->at_closesocket(socket) != 0)
  335. {
  336. LOG_E("AT socket (%d) closesocket failed!", socket);
  337. free_socket(sock);
  338. return -1;
  339. }
  340. }
  341. free_socket(sock);
  342. return 0;
  343. }
  344. int at_shutdown(int socket, int how)
  345. {
  346. struct at_socket *sock;
  347. if (at_dev_ops == RT_NULL)
  348. {
  349. return -1;
  350. }
  351. sock = at_get_socket(socket);
  352. if (sock == RT_NULL)
  353. {
  354. return -1;
  355. }
  356. if (sock->state == AT_SOCKET_CONNECT)
  357. {
  358. if (at_dev_ops->at_closesocket(socket) != 0)
  359. {
  360. LOG_E("AT socket (%d) shutdown failed!", socket);
  361. free_socket(sock);
  362. return -1;
  363. }
  364. }
  365. free_socket(sock);
  366. return 0;
  367. }
  368. int at_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  369. {
  370. if (at_get_socket(socket) == RT_NULL)
  371. {
  372. return -1;
  373. }
  374. return 0;
  375. }
  376. /* get IP address and port by socketaddr structure information */
  377. static int socketaddr_to_ipaddr_port(const struct sockaddr *sockaddr, ip_addr_t *addr, uint16_t *port)
  378. {
  379. const struct sockaddr_in* sin = (const struct sockaddr_in*) (const void *) sockaddr;
  380. (*addr).u_addr.ip4.addr = sin->sin_addr.s_addr;
  381. *port = (uint16_t) HTONS_PORT(sin->sin_port);
  382. return 0;
  383. }
  384. /* ipaddr structure change to IP address */
  385. static int ipaddr_to_ipstr(const struct sockaddr *sockaddr, char *ipstr)
  386. {
  387. struct sockaddr_in *sin = (struct sockaddr_in *) sockaddr;
  388. /* change network ip_addr to ip string */
  389. rt_snprintf(ipstr, 16, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
  390. return 0;
  391. }
  392. static void at_recv_notice_cb(int socket, at_socket_evt_t event, const char *buff, size_t bfsz)
  393. {
  394. struct at_socket *sock;
  395. RT_ASSERT(buff);
  396. RT_ASSERT(bfsz);
  397. RT_ASSERT(event == AT_SOCKET_EVT_RECV);
  398. sock = at_get_socket(socket);
  399. if (sock == RT_NULL)
  400. return ;
  401. /* put receive buffer to receiver packet list */
  402. rt_mutex_take(sock->recv_lock, RT_WAITING_FOREVER);
  403. at_recvpkt_put(&(sock->recvpkt_list), buff, bfsz);
  404. rt_mutex_release(sock->recv_lock);
  405. rt_sem_release(sock->recv_notice);
  406. at_do_event_changes(sock, AT_EVENT_RECV, RT_TRUE);
  407. }
  408. static void at_closed_notice_cb(int socket, at_socket_evt_t event, const char *buff, size_t bfsz)
  409. {
  410. struct at_socket *sock;
  411. RT_ASSERT(event == AT_SOCKET_EVT_CLOSED);
  412. if ((sock = at_get_socket(socket)) == RT_NULL)
  413. return ;
  414. at_do_event_changes(sock, AT_EVENT_RECV, RT_TRUE);
  415. at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
  416. sock->state = AT_SOCKET_CLOSED;
  417. rt_sem_release(sock->recv_notice);
  418. }
  419. int at_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  420. {
  421. struct at_socket *sock;
  422. ip_addr_t remote_addr;
  423. uint16_t remote_port;
  424. char ipstr[16] = { 0 };
  425. int result = 0;
  426. if (at_dev_ops == RT_NULL)
  427. {
  428. return -1;
  429. }
  430. sock = at_get_socket(socket);
  431. if (sock == RT_NULL)
  432. {
  433. result = -1;
  434. goto __exit;
  435. }
  436. if (sock->state != AT_SOCKET_NONE)
  437. {
  438. LOG_E("Socket %d connect state is %d.", sock->socket, sock->state);
  439. result = -1;
  440. goto __exit;
  441. }
  442. /* get IP address and port by socketaddr structure */
  443. socketaddr_to_ipaddr_port(name, &remote_addr, &remote_port);
  444. ipaddr_to_ipstr(name, ipstr);
  445. if (at_dev_ops->at_connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0)
  446. {
  447. LOG_E("AT socket(%d) connect failed!", socket);
  448. result = -1;
  449. goto __exit;
  450. }
  451. sock->state = AT_SOCKET_CONNECT;
  452. /* set AT socket receive data callback function */
  453. at_dev_ops->at_set_event_cb(AT_SOCKET_EVT_RECV, at_recv_notice_cb);
  454. at_dev_ops->at_set_event_cb(AT_SOCKET_EVT_CLOSED, at_closed_notice_cb);
  455. __exit:
  456. if (result < 0)
  457. {
  458. at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
  459. }
  460. return result;
  461. }
  462. int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
  463. {
  464. struct at_socket *sock;
  465. int timeout;
  466. int result = 0;
  467. size_t recv_len = 0;
  468. if (mem == RT_NULL || len == 0)
  469. {
  470. LOG_E("AT recvfrom input data or length error!");
  471. return -1;
  472. }
  473. if (at_dev_ops == RT_NULL)
  474. {
  475. return -1;
  476. }
  477. sock = at_get_socket(socket);
  478. if (sock == RT_NULL)
  479. {
  480. result = -1;
  481. goto __exit;
  482. }
  483. /* if the socket type is UDP, nead to connect socket first */
  484. if (from && sock->type == AT_SOCKET_UDP && sock->state == AT_SOCKET_NONE)
  485. {
  486. ip_addr_t remote_addr;
  487. uint16_t remote_port;
  488. char ipstr[16] = { 0 };
  489. socketaddr_to_ipaddr_port(from, &remote_addr, &remote_port);
  490. ipaddr_to_ipstr(from, ipstr);
  491. if (at_dev_ops->at_connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0)
  492. {
  493. LOG_E("AT socket UDP connect failed!");
  494. result = -1;
  495. goto __exit;
  496. }
  497. sock->state = AT_SOCKET_CONNECT;
  498. }
  499. if (sock->state != AT_SOCKET_CONNECT)
  500. {
  501. LOG_E("received data error, current socket (%d) state (%d) is error.", socket, sock->state);
  502. result = -1;
  503. goto __exit;
  504. }
  505. /* receive packet list last transmission of remaining data */
  506. rt_mutex_take(sock->recv_lock, RT_WAITING_FOREVER);
  507. if((recv_len = at_recvpkt_get(&(sock->recvpkt_list), (char *)mem, len)) > 0)
  508. {
  509. rt_mutex_release(sock->recv_lock);
  510. goto __exit;
  511. }
  512. rt_mutex_release(sock->recv_lock);
  513. /* non-blocking sockets receive data */
  514. if (flags & MSG_DONTWAIT)
  515. {
  516. goto __exit;
  517. }
  518. /* set AT socket receive timeout */
  519. if((timeout = sock->recv_timeout) == 0)
  520. {
  521. timeout = RT_WAITING_FOREVER;
  522. }
  523. while (1)
  524. {
  525. /* wait the receive semaphore */
  526. if (rt_sem_take(sock->recv_notice, timeout) < 0)
  527. {
  528. LOG_E("AT socket (%d) receive timeout (%d)!", socket, timeout);
  529. result = -1;
  530. goto __exit;
  531. }
  532. else
  533. {
  534. if (sock->state == AT_SOCKET_CONNECT)
  535. {
  536. /* get receive buffer to receiver ring buffer */
  537. rt_mutex_take(sock->recv_lock, RT_WAITING_FOREVER);
  538. recv_len = at_recvpkt_get(&(sock->recvpkt_list), (char *) mem, len);
  539. rt_mutex_release(sock->recv_lock);
  540. if (recv_len > 0)
  541. {
  542. break;
  543. }
  544. }
  545. else
  546. {
  547. LOG_D("received data exit, current socket (%d) is closed by remote.", socket);
  548. result = -1;
  549. goto __exit;
  550. }
  551. }
  552. }
  553. __exit:
  554. if (result < 0)
  555. {
  556. at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
  557. }
  558. else
  559. {
  560. result = recv_len;
  561. if (recv_len)
  562. {
  563. at_do_event_changes(sock, AT_EVENT_RECV, RT_FALSE);
  564. }
  565. }
  566. return result;
  567. }
  568. int at_recv(int s, void *mem, size_t len, int flags)
  569. {
  570. return at_recvfrom(s, mem, len, flags, RT_NULL, RT_NULL);
  571. }
  572. int at_sendto(int socket, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen)
  573. {
  574. struct at_socket *sock;
  575. int len, result = 0;
  576. if (at_dev_ops == RT_NULL)
  577. {
  578. result = -1;
  579. goto __exit;
  580. }
  581. if (data == RT_NULL || size == 0)
  582. {
  583. LOG_E("AT sendto input data or size error!");
  584. result = -1;
  585. goto __exit;
  586. }
  587. sock = at_get_socket(socket);
  588. if (sock == RT_NULL)
  589. {
  590. result = -1;
  591. goto __exit;
  592. }
  593. switch (sock->type)
  594. {
  595. case AT_SOCKET_TCP:
  596. if (sock->state != AT_SOCKET_CONNECT)
  597. {
  598. LOG_E("send data error, current socket (%d) state (%d) is error.", socket, sock->state);
  599. result = -1;
  600. goto __exit;
  601. }
  602. if ((len = at_dev_ops->at_send(sock->socket, (const char *) data, size, sock->type)) < 0)
  603. {
  604. result = -1;
  605. goto __exit;
  606. }
  607. break;
  608. case AT_SOCKET_UDP:
  609. if (to && sock->state == AT_SOCKET_NONE)
  610. {
  611. ip_addr_t remote_addr;
  612. uint16_t remote_port;
  613. char ipstr[16] = { 0 };
  614. socketaddr_to_ipaddr_port(to, &remote_addr, &remote_port);
  615. ipaddr_to_ipstr(to, ipstr);
  616. if (at_dev_ops->at_connect(socket, ipstr, remote_port, sock->type, RT_TRUE) < 0)
  617. {
  618. LOG_E("AT socket (%d) UDP connect failed!", socket);
  619. result = -1;
  620. goto __exit;
  621. }
  622. sock->state = AT_SOCKET_CONNECT;
  623. }
  624. if ((len = at_dev_ops->at_send(sock->socket, (char *) data, size, sock->type)) < 0)
  625. {
  626. result = -1;
  627. goto __exit;
  628. }
  629. break;
  630. default:
  631. LOG_E("Socket (%d) type %d is not support.", socket, sock->type);
  632. result = -1;
  633. goto __exit;
  634. }
  635. __exit:
  636. if (result < 0)
  637. {
  638. at_do_event_changes(sock, AT_EVENT_ERROR, RT_TRUE);
  639. }
  640. else
  641. {
  642. result = len;
  643. }
  644. return result;
  645. }
  646. int at_send(int socket, const void *data, size_t size, int flags)
  647. {
  648. return at_sendto(socket, data, size, flags, RT_NULL, 0);
  649. }
  650. int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  651. {
  652. struct at_socket *sock;
  653. int32_t timeout;
  654. if (optval == RT_NULL || optlen == RT_NULL)
  655. {
  656. LOG_E("AT getsocketopt input option value or option length error!");
  657. return -1;
  658. }
  659. sock = at_get_socket(socket);
  660. if (sock == RT_NULL)
  661. {
  662. return -1;
  663. }
  664. switch (level)
  665. {
  666. case SOL_SOCKET:
  667. switch (optname)
  668. {
  669. case SO_RCVTIMEO:
  670. timeout = sock->recv_timeout;
  671. ((struct timeval *)(optval))->tv_sec = (timeout) / 1000U;
  672. ((struct timeval *)(optval))->tv_usec = (timeout % 1000U) * 1000U;
  673. break;
  674. case SO_SNDTIMEO:
  675. timeout = sock->send_timeout;
  676. ((struct timeval *) optval)->tv_sec = timeout / 1000U;
  677. ((struct timeval *) optval)->tv_usec = (timeout % 1000U) * 1000U;
  678. break;
  679. default:
  680. LOG_E("AT socket (%d) not support option name : %d.", socket, optname);
  681. return -1;
  682. }
  683. break;
  684. default:
  685. LOG_E("AT socket (%d) not support option level : %d.", socket, level);
  686. return -1;
  687. }
  688. return 0;
  689. }
  690. int at_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  691. {
  692. struct at_socket *sock;
  693. if (optval == RT_NULL)
  694. {
  695. LOG_E("AT setsockopt input option value error!");
  696. return -1;
  697. }
  698. sock = at_get_socket(socket);
  699. if (sock == RT_NULL)
  700. {
  701. return -1;
  702. }
  703. switch (level)
  704. {
  705. case SOL_SOCKET:
  706. switch (optname)
  707. {
  708. case SO_RCVTIMEO:
  709. sock->recv_timeout = ((const struct timeval *) optval)->tv_sec * 1000
  710. + ((const struct timeval *) optval)->tv_usec / 1000;
  711. break;
  712. case SO_SNDTIMEO:
  713. sock->send_timeout = ((const struct timeval *) optval)->tv_sec * 1000
  714. + ((const struct timeval *) optval)->tv_usec / 1000;
  715. break;
  716. default:
  717. LOG_E("AT socket (%d) not support option name : %d.", socket, optname);
  718. return -1;
  719. }
  720. break;
  721. case IPPROTO_TCP:
  722. switch (optname)
  723. {
  724. case TCP_NODELAY:
  725. break;
  726. }
  727. break;
  728. default:
  729. LOG_E("AT socket (%d) not support option level : %d.", socket, level);
  730. return -1;
  731. }
  732. return 0;
  733. }
  734. static uint32_t ipstr_atol(const char* nptr)
  735. {
  736. uint32_t total = 0;
  737. char sign = '+';
  738. /* jump space */
  739. while (isspace(*nptr))
  740. {
  741. ++nptr;
  742. }
  743. if (*nptr == '-' || *nptr == '+')
  744. {
  745. sign = *nptr++;
  746. }
  747. while (isdigit(*nptr))
  748. {
  749. total = 10 * total + ((*nptr++) - '0');
  750. }
  751. return (sign == '-') ? -total : total;
  752. }
  753. /* IP address to unsigned int type */
  754. static uint32_t ipstr_to_u32(char *ipstr)
  755. {
  756. char ipBytes[4] = { 0 };
  757. uint32_t i;
  758. for (i = 0; i < 4; i++, ipstr++)
  759. {
  760. ipBytes[i] = (char) ipstr_atol(ipstr);
  761. if ((ipstr = strchr(ipstr, '.')) == RT_NULL)
  762. {
  763. break;
  764. }
  765. }
  766. return *(uint32_t *) ipBytes;
  767. }
  768. struct hostent *at_gethostbyname(const char *name)
  769. {
  770. ip_addr_t addr;
  771. char ipstr[16] = { 0 };
  772. /* buffer variables for at_gethostbyname() */
  773. static struct hostent s_hostent;
  774. static char *s_aliases;
  775. static ip_addr_t s_hostent_addr;
  776. static ip_addr_t *s_phostent_addr[2];
  777. static char s_hostname[DNS_MAX_NAME_LENGTH + 1];
  778. size_t idx = 0;
  779. if (name == RT_NULL)
  780. {
  781. LOG_E("AT gethostbyname input name error!");
  782. return RT_NULL;
  783. }
  784. if (at_dev_ops == RT_NULL)
  785. {
  786. return RT_NULL;
  787. }
  788. for (idx = 0; idx < strlen(name) && !isalpha(name[idx]); idx++);
  789. if (idx < strlen(name))
  790. {
  791. if (at_dev_ops->at_domain_resolve(name, ipstr) < 0)
  792. {
  793. LOG_E("AT domain (%s) resolve error!", name);
  794. return RT_NULL;
  795. }
  796. }
  797. else
  798. {
  799. strncpy(ipstr, name, strlen(name));
  800. }
  801. addr.u_addr.ip4.addr = ipstr_to_u32(ipstr);
  802. /* fill hostent structure */
  803. s_hostent_addr = addr;
  804. s_phostent_addr[0] = &s_hostent_addr;
  805. s_phostent_addr[1] = RT_NULL;
  806. strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
  807. s_hostname[DNS_MAX_NAME_LENGTH] = 0;
  808. s_hostent.h_name = s_hostname;
  809. s_aliases = RT_NULL;
  810. s_hostent.h_aliases = &s_aliases;
  811. s_hostent.h_addrtype = AF_AT;
  812. s_hostent.h_length = sizeof(ip_addr_t);
  813. s_hostent.h_addr_list = (char**) &s_phostent_addr;
  814. return &s_hostent;
  815. }
  816. int at_getaddrinfo(const char *nodename, const char *servname,
  817. const struct addrinfo *hints, struct addrinfo **res)
  818. {
  819. int port_nr = 0;
  820. ip_addr_t addr;
  821. struct addrinfo *ai;
  822. struct sockaddr_storage *sa;
  823. size_t total_size = 0;
  824. size_t namelen = 0;
  825. int ai_family = 0;
  826. if (res == RT_NULL)
  827. {
  828. return EAI_FAIL;
  829. }
  830. *res = RT_NULL;
  831. if (at_dev_ops == RT_NULL)
  832. {
  833. return EAI_FAIL;
  834. }
  835. if ((nodename == RT_NULL) && (servname == RT_NULL))
  836. {
  837. return EAI_NONAME;
  838. }
  839. if (hints != RT_NULL)
  840. {
  841. ai_family = hints->ai_family;
  842. if (hints->ai_family != AF_AT && hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC)
  843. {
  844. return EAI_FAMILY;
  845. }
  846. }
  847. if (servname != RT_NULL)
  848. {
  849. /* service name specified: convert to port number */
  850. port_nr = atoi(servname);
  851. if ((port_nr <= 0) || (port_nr > 0xffff))
  852. {
  853. return EAI_SERVICE;
  854. }
  855. }
  856. if (nodename != RT_NULL)
  857. {
  858. /* service location specified, try to resolve */
  859. if ((hints != RT_NULL) && (hints->ai_flags & AI_NUMERICHOST))
  860. {
  861. /* no DNS lookup, just parse for an address string */
  862. if (!inet_aton(nodename, (ip4_addr_t * )&addr))
  863. {
  864. return EAI_NONAME;
  865. }
  866. if (ai_family == AF_AT || ai_family == AF_INET)
  867. {
  868. return EAI_NONAME;
  869. }
  870. }
  871. else
  872. {
  873. char ip_str[16] = { 0 };
  874. size_t idx = 0;
  875. for (idx = 0; idx < strlen(nodename) && !isalpha(nodename[idx]); idx++);
  876. if(idx < strlen(nodename))
  877. {
  878. if (at_dev_ops->at_domain_resolve((char *) nodename, ip_str) != 0)
  879. {
  880. return EAI_FAIL;
  881. }
  882. }
  883. else
  884. {
  885. strncpy(ip_str, nodename, strlen(nodename));
  886. }
  887. addr.type = IPADDR_TYPE_V4;
  888. if ((addr.u_addr.ip4.addr = ipstr_to_u32(ip_str)) == 0)
  889. {
  890. return EAI_FAIL;
  891. }
  892. }
  893. }
  894. else
  895. {
  896. /* to do service location specified, use loopback address */
  897. }
  898. total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
  899. if (nodename != RT_NULL)
  900. {
  901. namelen = strlen(nodename);
  902. if (namelen > DNS_MAX_NAME_LENGTH)
  903. {
  904. /* invalid name length */
  905. return EAI_FAIL;
  906. }
  907. RT_ASSERT(total_size + namelen + 1 > total_size);
  908. total_size += namelen + 1;
  909. }
  910. /* If this fails, please report to lwip-devel! :-) */
  911. RT_ASSERT(total_size <= sizeof(struct addrinfo) + sizeof(struct sockaddr_storage) + DNS_MAX_NAME_LENGTH + 1);
  912. ai = (struct addrinfo *) rt_malloc(total_size);
  913. if (ai == RT_NULL)
  914. {
  915. return EAI_MEMORY;
  916. }
  917. memset(ai, 0, total_size);
  918. /* cast through void* to get rid of alignment warnings */
  919. sa = (struct sockaddr_storage *) (void *) ((uint8_t *) ai + sizeof(struct addrinfo));
  920. struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
  921. /* set up sockaddr */
  922. sa4->sin_addr.s_addr = addr.u_addr.ip4.addr;
  923. sa4->sin_family = AF_INET;
  924. sa4->sin_len = sizeof(struct sockaddr_in);
  925. sa4->sin_port = htons((u16_t )port_nr);
  926. ai->ai_family = AF_INET;
  927. /* set up addrinfo */
  928. if (hints != RT_NULL)
  929. {
  930. /* copy socktype & protocol from hints if specified */
  931. ai->ai_socktype = hints->ai_socktype;
  932. ai->ai_protocol = hints->ai_protocol;
  933. }
  934. if (nodename != RT_NULL)
  935. {
  936. /* copy nodename to canonname if specified */
  937. ai->ai_canonname = ((char *) ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
  938. memcpy(ai->ai_canonname, nodename, namelen);
  939. ai->ai_canonname[namelen] = 0;
  940. }
  941. ai->ai_addrlen = sizeof(struct sockaddr_storage);
  942. ai->ai_addr = (struct sockaddr *) sa;
  943. *res = ai;
  944. return 0;
  945. }
  946. void at_freeaddrinfo(struct addrinfo *ai)
  947. {
  948. struct addrinfo *next;
  949. while (ai != NULL)
  950. {
  951. next = ai->ai_next;
  952. rt_free(ai);
  953. ai = next;
  954. }
  955. }
  956. void at_scoket_device_register(const struct at_device_ops *ops)
  957. {
  958. RT_ASSERT(ops);
  959. RT_ASSERT(ops->at_connect);
  960. RT_ASSERT(ops->at_closesocket);
  961. RT_ASSERT(ops->at_send);
  962. RT_ASSERT(ops->at_domain_resolve);
  963. RT_ASSERT(ops->at_set_event_cb);
  964. at_dev_ops = (struct at_device_ops *) ops;
  965. }