Skip to content

Commit 9fdd46f

Browse files
authored
Merge pull request #1737 from chenyong111/master
Add query and unregistered protocol family functions,format code
2 parents 6bde1c5 + fc73538 commit 9fdd46f

File tree

15 files changed

+261
-72
lines changed

15 files changed

+261
-72
lines changed

components/net/at/at_socket/at_socket.c

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636

3737
#ifdef DBG_SECTION_NAME
3838
#undef DBG_SECTION_NAME
39-
#define DBG_SECTION_NAME "[AT_SOC] "
39+
#define DBG_SECTION_NAME "AT_SOC"
4040
#endif
4141

42-
4342
#define HTONS_PORT(x) ((((x) & 0x00ffUL) << 8) | (((x) & 0xff00UL) >> 8))
4443
#define NIPQUAD(addr) \
4544
((unsigned char *)&addr)[0], \
@@ -89,7 +88,7 @@ static size_t at_recvpkt_put(rt_slist_t *rlist, const char *ptr, size_t length)
8988
at_recv_pkt_t pkt;
9089

9190
pkt = (at_recv_pkt_t) rt_calloc(1, sizeof(struct at_recv_pkt));
92-
if (!pkt)
91+
if (pkt == RT_NULL)
9392
{
9493
LOG_E("No memory for receive packet table!");
9594
return 0;
@@ -340,7 +339,7 @@ int at_socket(int domain, int type, int protocol)
340339

341340
/* allocate and initialize a new AT socket */
342341
sock = alloc_socket();
343-
if(!sock)
342+
if(sock == RT_NULL)
344343
{
345344
LOG_E("Allocate a new AT socket failed!");
346345
return RT_NULL;
@@ -381,14 +380,16 @@ int at_closesocket(int socket)
381380
struct at_socket *sock;
382381
enum at_socket_state last_state;
383382

384-
if (!at_dev_ops)
383+
if (at_dev_ops == RT_NULL)
385384
{
386-
LOG_E("Please register AT device socket options first!");
387385
return -1;
388386
}
389387

390-
if ((sock = at_get_socket(socket)) == RT_NULL)
388+
sock = at_get_socket(socket);
389+
if (sock == RT_NULL)
390+
{
391391
return -1;
392+
}
392393

393394
last_state = sock->state;
394395

@@ -410,14 +411,16 @@ int at_shutdown(int socket, int how)
410411
{
411412
struct at_socket *sock;
412413

413-
if (!at_dev_ops)
414+
if (at_dev_ops == RT_NULL)
414415
{
415-
LOG_E("Please register AT device socket options first!");
416416
return -1;
417417
}
418418

419-
if ((sock = at_get_socket(socket)) == RT_NULL)
419+
sock = at_get_socket(socket);
420+
if (sock == RT_NULL)
421+
{
420422
return -1;
423+
}
421424

422425
if (sock->state == AT_SOCKET_CONNECT)
423426
{
@@ -434,7 +437,9 @@ int at_bind(int socket, const struct sockaddr *name, socklen_t namelen)
434437
{
435438

436439
if (at_get_socket(socket) == RT_NULL)
440+
{
437441
return -1;
442+
}
438443

439444
return 0;
440445
}
@@ -470,7 +475,8 @@ static void at_recv_notice_cb(int socket, at_socket_evt_t event, const char *buf
470475
RT_ASSERT(bfsz);
471476
RT_ASSERT(event == AT_SOCKET_EVT_RECV);
472477

473-
if ((sock = at_get_socket(socket)) == RT_NULL)
478+
sock = at_get_socket(socket);
479+
if (sock == RT_NULL)
474480
return ;
475481

476482
/* put receive buffer to receiver packet list */
@@ -506,14 +512,13 @@ int at_connect(int socket, const struct sockaddr *name, socklen_t namelen)
506512
char ipstr[16] = { 0 };
507513
int result = 0;
508514

509-
if (!at_dev_ops)
515+
if (at_dev_ops == RT_NULL)
510516
{
511-
LOG_E("Please register AT device socket options first!");
512517
return -1;
513518
}
514519

515520
sock = at_get_socket(socket);
516-
if (!sock)
521+
if (sock == RT_NULL)
517522
{
518523
result = -1;
519524
goto __exit;
@@ -560,21 +565,19 @@ int at_recvfrom(int socket, void *mem, size_t len, int flags, struct sockaddr *f
560565
int result = 0;
561566
size_t recv_len = 0;
562567

563-
if (!mem || len == 0)
568+
if (mem == RT_NULL || len == 0)
564569
{
565570
LOG_E("AT recvfrom input data or length error!");
566-
result = -1;
567-
goto __exit;
571+
return -1;
568572
}
569573

570-
if (!at_dev_ops)
574+
if (at_dev_ops == RT_NULL)
571575
{
572-
LOG_E("Please register AT device socket options first!");
573576
return -1;
574577
}
575578

576579
sock = at_get_socket(socket);
577-
if (!sock)
580+
if (sock == RT_NULL)
578581
{
579582
result = -1;
580583
goto __exit;
@@ -686,22 +689,21 @@ int at_sendto(int socket, const void *data, size_t size, int flags, const struct
686689
struct at_socket *sock;
687690
int len, result = 0;
688691

689-
if (!at_dev_ops)
692+
if (at_dev_ops == RT_NULL)
690693
{
691-
LOG_E("Please register AT device socket options first!");
692694
result = -1;
693695
goto __exit;
694696
}
695697

696-
if (!data || size == 0)
698+
if (data == RT_NULL || size == 0)
697699
{
698700
LOG_E("AT sendto input data or size error!");
699701
result = -1;
700702
goto __exit;
701703
}
702704

703705
sock = at_get_socket(socket);
704-
if (!sock)
706+
if (sock == RT_NULL)
705707
{
706708
result = -1;
707709
goto __exit;
@@ -780,14 +782,14 @@ int at_getsockopt(int socket, int level, int optname, void *optval, socklen_t *o
780782
struct at_socket *sock;
781783
int32_t timeout;
782784

783-
if (!optval || !optlen)
785+
if (optval == RT_NULL || optlen == RT_NULL)
784786
{
785787
LOG_E("AT getsocketopt input option value or option length error!");
786788
return -1;
787789
}
788790

789791
sock = at_get_socket(socket);
790-
if (!sock)
792+
if (sock == RT_NULL)
791793
{
792794
return -1;
793795
}
@@ -827,14 +829,14 @@ int at_setsockopt(int socket, int level, int optname, const void *optval, sockle
827829
{
828830
struct at_socket *sock;
829831

830-
if (!optval)
832+
if (optval == RT_NULL)
831833
{
832834
LOG_E("AT setsockopt input option value error!");
833835
return -1;
834836
}
835837

836838
sock = at_get_socket(socket);
837-
if (!sock)
839+
if (sock == RT_NULL)
838840
{
839841
return -1;
840842
}
@@ -923,15 +925,14 @@ struct hostent *at_gethostbyname(const char *name)
923925
static char s_hostname[DNS_MAX_NAME_LENGTH + 1];
924926
size_t idx = 0;
925927

926-
if (!name)
928+
if (name == RT_NULL)
927929
{
928930
LOG_E("AT gethostbyname input name error!");
929931
return RT_NULL;
930932
}
931933

932-
if (!at_dev_ops)
934+
if (at_dev_ops == RT_NULL)
933935
{
934-
LOG_E("Please register AT device socket options first!");
935936
return RT_NULL;
936937
}
937938

@@ -983,12 +984,13 @@ int at_getaddrinfo(const char *nodename, const char *servname,
983984
{
984985
return EAI_FAIL;
985986
}
986-
if (!at_dev_ops)
987+
*res = RT_NULL;
988+
989+
if (at_dev_ops == RT_NULL)
987990
{
988-
LOG_E("Please register AT device socket options first!");
989991
return EAI_FAIL;
990992
}
991-
*res = RT_NULL;
993+
992994
if ((nodename == RT_NULL) && (servname == RT_NULL))
993995
{
994996
return EAI_NONAME;
@@ -1085,10 +1087,10 @@ int at_getaddrinfo(const char *nodename, const char *servname,
10851087
struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
10861088
/* set up sockaddr */
10871089
sa4->sin_addr.s_addr = addr.u_addr.ip4.addr;
1088-
sa4->sin_family = AF_AT;
1090+
sa4->sin_family = AF_INET;
10891091
sa4->sin_len = sizeof(struct sockaddr_in);
10901092
sa4->sin_port = htons((u16_t )port_nr);
1091-
ai->ai_family = AF_AT;
1093+
ai->ai_family = AF_INET;
10921094

10931095
/* set up addrinfo */
10941096
if (hints != RT_NULL)
@@ -1114,9 +1116,13 @@ int at_getaddrinfo(const char *nodename, const char *servname,
11141116

11151117
void at_freeaddrinfo(struct addrinfo *ai)
11161118
{
1117-
if (ai != RT_NULL)
1119+
struct addrinfo *next;
1120+
1121+
while (ai != NULL)
11181122
{
1123+
next = ai->ai_next;
11191124
rt_free(ai);
1125+
ai = next;
11201126
}
11211127
}
11221128

components/net/at/at_socket/at_socket.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include <netdb.h>
3333
#include <sys/socket.h>
3434

35+
#ifdef __cplusplus
36+
extern "C" {
37+
#endif
38+
3539
#ifndef AT_SOCKET_RECV_BFSZ
3640
#define AT_SOCKET_RECV_BFSZ 512
3741
#endif
@@ -144,7 +148,7 @@ void at_scoket_device_register(const struct at_device_ops *ops);
144148
#ifndef RT_USING_SAL
145149

146150
#define socket(domain, type, protocol) at_socket(domain, type, protocol)
147-
#define closescoket(socket) at_closesocket(socket)
151+
#define closesocket(socket) at_closesocket(socket)
148152
#define shutdown(socket, how) at_shutdown(socket, how)
149153
#define bind(socket, name, namelen) at_bind(socket, name, namelen)
150154
#define connect(socket, name, namelen) at_connect(socket, name, namelen)
@@ -160,4 +164,8 @@ void at_scoket_device_register(const struct at_device_ops *ops);
160164

161165
#endif /* RT_USING_SAL */
162166

167+
#ifdef __cplusplus
168+
}
169+
#endif
170+
163171
#endif /* AT_SOCKET_H__ */

components/net/at/include/at.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
#include <rtthread.h>
2929

30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
3033
#define AT_SW_VERSION "0.3.0"
3134

3235
#define DBG_ENABLE
@@ -251,4 +254,8 @@ void at_port_reset(void);
251254
void at_port_factory_reset(void);
252255
#endif
253256

257+
#ifdef __cplusplus
258+
}
259+
#endif
260+
254261
#endif /* __AT_H__ */

components/net/sal_socket/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ if GetDepend('SAL_USING_LWIP'):
1616

1717
if GetDepend('SAL_USING_AT'):
1818
src += Glob('impl/af_inet_at.c')
19+
20+
if GetDepend('SAL_USING_LWIP') or GetDepend('SAL_USING_AT'):
21+
CPPPATH += [cwd + '/impl']
1922

2023
if GetDepend('SAL_USING_POSIX'):
2124
src += Glob('dfs_net/*.c')
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* File : af_inet.h
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-08-25 ChenYong First version
23+
*/
24+
25+
#ifndef __AF_INET_H__
26+
#define __AF_INET_H__
27+
28+
#include <rtthread.h>
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
#ifdef SAL_USING_LWIP
35+
/* lwIP protocol family register */
36+
int lwip_inet_init(void);
37+
#endif
38+
39+
#ifdef SAL_USING_AT
40+
/* AT protocol family register */
41+
int at_inet_init(void);
42+
#endif
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif /* __AF_INET_H__ */

components/net/sal_socket/impl/af_inet_at.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <sal.h>
2727

2828
#include <at_socket.h>
29+
#include <af_inet.h>
2930

3031
#ifdef SAL_USING_POSIX
3132
#include <dfs_poll.h>
@@ -107,6 +108,7 @@ static int at_create(struct sal_socket *socket, int type, int protocol)
107108
}
108109

109110
static const struct proto_family at_inet_family_ops = {
111+
"at",
110112
AF_AT,
111113
AF_INET,
112114
at_create,

components/net/sal_socket/impl/af_inet_lwip.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#endif
3535

3636
#include <sal.h>
37+
#include <af_inet.h>
3738

3839
#if LWIP_VERSION < 0x2000000
3940
#define SELWAIT_T int
@@ -284,6 +285,7 @@ static int inet_create(struct sal_socket *socket, int type, int protocol)
284285
}
285286

286287
static const struct proto_family lwip_inet_family_ops = {
288+
"lwip",
287289
AF_INET,
288290
AF_INET,
289291
inet_create,

0 commit comments

Comments
 (0)