add socket wrapper for at framework

you can use at framework to send/recv data in socket API
example: see examples\tcp_through_module_based_at_socket
project: see board\TencentOS_tiny_EVB_MX_Plus\KEIL\tcp_through_module_based_at_socket
This commit is contained in:
daishengdong
2020-04-07 16:39:13 +08:00
parent f14e7b1753
commit 032af66467
60 changed files with 1127 additions and 217 deletions

View File

@@ -0,0 +1,66 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_AT_SOCKET_H_
#define _TOS_AT_SOCKET_H_
#include "tos_at_socket_lib.h"
#include "tos_at_socket_types.h"
#define AF_INET 0
#define AF_INET6 1
#define AF_UNIX 2
/* Provides sequenced, reliable, bidirectional, connection-mode byte streams, and may provide a transmission mechanism for out-of-band data. */
#define SOCK_STREAM 0
/* Provides datagrams, which are connectionless-mode, unreliable messages of fixed maximum length. */
#define SOCK_DGRAM 1
/* Peeks at an incoming message. The data is treated as unread and the next recv() or similar function shall still return this data. */
#define MSG_PEEK 0x01
/* Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific. */
#define MSG_OOB 0x02
/* On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function may return the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket. */
#define MSG_WAITALL 0x04
int socket(int domain, int type, int protocol);
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
int recv(int socket, void *buffer, size_t length, int flags);
int recvfrom(int socket, void * buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len);
int send(int socket, const void *buffer, size_t length, int flags);
int sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
int shutdown(int socket, int how);
int read(int socket, void *buffer, size_t length);
int close(int socket);
int write(int socket, const void *buffer, size_t length);
#endif /* _TOS_AT_SOCKET_H_ */

View File

@@ -0,0 +1,78 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_AT_SOCKET_LIB_H_
#define _TOS_AT_SOCKET_LIB_H_
#include "tos_k.h"
#include "tos_at_socket_types.h"
#if (TOS_CFG_CPU_BYTE_ORDER == CPU_BYTE_ORDER_BIG_ENDIAN)
#ifndef htons
#define htons(x) (x)
#endif
#ifndef ntohs
#define ntohs(x) (x)
#endif
#ifndef htonl
#define htonl(x) (x)
#endif
#ifndef ntohl
#define ntohl(x) (x)
#endif
#elif (TOS_CFG_CPU_BYTE_ORDER == CPU_BYTE_ORDER_LITTLE_ENDIAN)
#ifndef htons
#define htons(x) ((u16_t)((((x) & (u16_t)0x00ffU) << 8) | (((x) & (u16_t)0xff00U) >> 8)))
#endif
#ifndef ntohs
#define ntohs(x) htons(x)
#endif
#ifndef htonl
#define htonl(x) ((((x) & (u32_t)0x000000ffUL) << 24) | \
(((x) & (u32_t)0x0000ff00UL) << 8) | \
(((x) & (u32_t)0x00ff0000UL) >> 8) | \
(((x) & (u32_t)0xff000000UL) >> 24))
#endif
#ifndef ntohl
#define ntohl(x) htonl(x)
#endif
#endif
#define bzero(s, n) memset(s, 0, n)
#define INADDR_NONE ((u32_t) 0xffffffff) /* 255.255.255.255 */
#define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
#define isascii(c) in_range(c, 0x20, 0x7f)
#define isdigit(c) in_range(c, '0', '9')
#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
#define islower(c) in_range(c, 'a', 'z')
#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
u32_t inet_addr(const char *cp);
#endif /* _TOS_AT_SOCKET_LIB_H_ */

View File

@@ -0,0 +1,60 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_AT_SOCKET_PRV_H_
#define _TOS_AT_SOCKET_PRV_H_
#include "sal_module_wrapper.h"
#define SOCKET_CTL_MAX 6 /* should consistent with AT_DATA_CHANNEL_NUM */
typedef struct socket_control_st {
int fd; /* the real channel id allocated by at framework.
for some module like NBIot(bc35), you only know the channel id until you really do the
connect to the server */
int is_taken; /* is this control usable?(if is_taken is FALSE then it's not usable) */
sal_proto_t protocol;
} socket_ctl_t;
int socket_id_sanity_check(int id);
int socket_id_set_sanity_check(int id);
int socket_id_alloc(void);
void socket_id_free(int id);
void socket_set_protocol(int id, sal_proto_t protocol);
sal_proto_t socket_get_protocol(int id);
void socket_set_fd(int id, int fd);
int socket_get_fd(int id);
#define SOCKET_ID_SANITY_CHECK(id) \
if (!socket_id_sanity_check(id)) { \
return -1; \
}
#define SOCKET_ID_SET_SANITY_CHECK(id) \
if (!socket_id_set_sanity_check(id)) { \
return -1; \
}
#endif /* _TOS_AT_SOCKET_PRV_H_ */

View File

@@ -0,0 +1,58 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_AT_SOCKET_TYPES_H_
#define _TOS_AT_SOCKET_TYPES_H_
#include "stdint.h"
typedef uint8_t u8_t;
typedef uint16_t u16_t;
typedef uint32_t u32_t;
typedef int8_t s8_t;
typedef int16_t s16_t;
typedef int32_t s32_t;
#if !defined(in_addr_t)
typedef u32_t in_addr_t;
#endif
struct in_addr {
in_addr_t s_addr;
};
struct sockaddr_in {
u8_t sin_len;
u8_t sin_family;
u16_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct sockaddr {
u8_t sa_len;
u8_t sa_family;
char sa_data[14];
};
#ifndef socklen_t
# define socklen_t int
#endif
#endif /* _TOS_AT_SOCKET_TYPES_H_ */