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

@@ -93,7 +93,7 @@ typedef struct at_echo_st {
size_t __w_idx;
int __is_expecting;
k_sem_t __expect_notify;
int fuzzy_matching;
int __is_fuzzy_match;
} at_echo_t;
typedef void (*at_event_callback_t)(void);
@@ -269,7 +269,7 @@ __API__ int tos_at_init(hal_uart_port_t uart_port, at_event_t *event_table, size
*
* @attention None
*
* @return
* @return
None
*/
__API__ void tos_at_deinit(void);

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* 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_UTILS_H_
#define _TOS_AT_UTILS_H_

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* 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.
*---------------------------------------------------------------------------*/
#include "tos_at.h"
__STATIC__ at_agent_t at_agent;
@@ -161,18 +178,16 @@ __STATIC__ int at_is_echo_expect(void)
return 0;
}
if(at_echo->fuzzy_matching){
if(strstr(recv_buffer, expect)!=NULL){
return 0;
}
return -1;
if (at_echo->__is_fuzzy_match) {
if (strstr(recv_buffer, expect) != NULL) {
return 0;
}
return -1;
}
if (strncmp(expect, recv_buffer, expect_len) == 0) {
return 1;
}
return 0;
}
@@ -346,7 +361,7 @@ __API__ int tos_at_echo_create(at_echo_t *echo, char *buffer, size_t buffer_size
echo->status = AT_ECHO_STATUS_NONE;
echo->__w_idx = 0;
echo->__is_expecting = K_FALSE;
echo->fuzzy_matching = K_FALSE;
echo->__is_fuzzy_match = K_FALSE;
return 0;
}
@@ -367,7 +382,7 @@ __API__ int tos_at_echo_fuzzy_matching_create(at_echo_t *echo, char *buffer, siz
echo->status = AT_ECHO_STATUS_NONE;
echo->__w_idx = 0;
echo->__is_expecting = K_FALSE;
echo->fuzzy_matching = K_TRUE;
echo->__is_fuzzy_match = K_TRUE;
return 0;
}

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* 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.
*---------------------------------------------------------------------------*/
#include "tos_k.h"
#include "tos_at.h"

View File

@@ -21,7 +21,7 @@
#include <stdint.h>
#include <stdio.h>
typedef enum sal_proto_en {
typedef enum sal_protocol_en {
TOS_SAL_PROTO_TCP,
TOS_SAL_PROTO_UDP,
} sal_proto_t;

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_ */

View File

@@ -0,0 +1,217 @@
/*----------------------------------------------------------------------------
* 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.
*---------------------------------------------------------------------------*/
#include "tos_at_socket.h"
#include "tos_at_socket_prv.h"
#include "sal_module_wrapper.h"
int socket(int domain, int type, int protocol)
{
int pub_fd;
sal_proto_t proto;
protocol = protocol;
if (domain != AF_INET) {
return -1;
}
if (type == SOCK_STREAM) {
proto = TOS_SAL_PROTO_TCP;
} else if (type == SOCK_DGRAM) {
proto = TOS_SAL_PROTO_UDP;
} else {
return -1;
}
pub_fd = socket_id_alloc();
if (pub_fd < 0) {
return -1;
}
socket_set_protocol(pub_fd, proto);
return pub_fd;
}
static int sockaddr2ip(const struct sockaddr_in *addr_in, char ip[], int ip_len)
{
snprintf(ip, 16, "%u.%u.%u.%u", ((uint8_t *)(&addr_in->sin_addr.s_addr))[0],
((uint8_t *)(&addr_in->sin_addr.s_addr))[1],
((uint8_t *)(&addr_in->sin_addr.s_addr))[2],
((uint8_t *)(&addr_in->sin_addr.s_addr))[3]);
return 0;
}
static int sockaddr2port(const struct sockaddr_in *addr_in, char port[], int port_len)
{
snprintf(port, port_len, "%d", htons(addr_in->sin_port));
return 0;
}
int connect(int socket, const struct sockaddr *address, socklen_t address_len)
{
int prv_fd;
char ip[16], port[8];
const struct sockaddr_in *addr_in = (const struct sockaddr_in *)address;
SOCKET_ID_SANITY_CHECK(socket);
if (sockaddr2ip(addr_in, ip, sizeof(ip)) != 0) {
return -1;
}
if (sockaddr2port(addr_in, port, sizeof(port)) != 0) {
return -1;
}
prv_fd = tos_sal_module_connect(ip, port, socket_get_protocol(socket));
if (prv_fd < 0) {
return -1;
}
socket_set_fd(socket, prv_fd);
return 0;
}
int recv(int socket, void *buffer, size_t length, int flags)
{
int total_len = 0, remain_len = length, recv_len;
SOCKET_ID_SET_SANITY_CHECK(socket);
if (!(flags & MSG_WAITALL)) {
return tos_sal_module_recv(socket_get_fd(socket), buffer, length);
}
while (total_len < length) {
recv_len = tos_sal_module_recv(socket_get_fd(socket),
(uint8_t *)buffer + total_len,
remain_len);
if (recv_len <= 0) {
continue;
}
remain_len -= recv_len;
total_len += recv_len;
}
return length;
}
int recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len)
{
int total_len = 0, remain_len = length, recv_len;
(void)address;
(void)address_len;
SOCKET_ID_SET_SANITY_CHECK(socket);
if (!(flags & MSG_WAITALL)) {
return tos_sal_module_recvfrom(socket_get_fd(socket), buffer, length);
}
while (total_len < length) {
recv_len = tos_sal_module_recvfrom(socket_get_fd(socket),
(uint8_t *)buffer + total_len,
remain_len);
if (recv_len <= 0) {
continue;
}
remain_len -= recv_len;
total_len += recv_len;
}
return length;
}
int send(int socket, const void *buffer, size_t length, int flags)
{
SOCKET_ID_SET_SANITY_CHECK(socket);
return tos_sal_module_send(socket_get_fd(socket), buffer, length);
}
int sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
{
(void)dest_addr;
(void)dest_len;
SOCKET_ID_SET_SANITY_CHECK(socket);
return tos_sal_module_send(socket_get_fd(socket), message, length);
}
int shutdown(int socket, int how)
{
int rc;
how = how;
SOCKET_ID_SET_SANITY_CHECK(socket);
if (socket_id_set_sanity_check(socket)) {
rc = tos_sal_module_close(socket_get_fd(socket));
if (rc != 0) {
return rc;
}
}
socket_id_free(socket);
return 0;
}
int read(int socket, void *buffer, size_t length)
{
SOCKET_ID_SET_SANITY_CHECK(socket);
return tos_sal_module_recv(socket_get_fd(socket), buffer, length);
}
int close(int socket)
{
int rc;
SOCKET_ID_SANITY_CHECK(socket);
if (socket_id_set_sanity_check(socket)) {
rc = tos_sal_module_close(socket_get_fd(socket));
if (rc != 0) {
return rc;
}
}
socket_id_free(socket);
return 0;
}
int write(int socket, const void *buffer, size_t length)
{
SOCKET_ID_SET_SANITY_CHECK(socket);
return tos_sal_module_send(socket_get_fd(socket), buffer, length);
}

View File

@@ -0,0 +1,122 @@
/*----------------------------------------------------------------------------
* 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.
*---------------------------------------------------------------------------*/
#include "tos_at_socket_lib.h"
s8_t
inet_aton(const char *cp, struct in_addr *addr)
{
u32_t val;
s32_t base, n;
char c;
u32_t parts[4];
u32_t* pp = parts;
c = *cp;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, isdigit=decimal.
*/
if (!isdigit(c))
return (0);
val = 0; base = 10;
if (c == '0') {
c = *++cp;
if (c == 'x' || c == 'X')
base = 16, c = *++cp;
else
base = 8;
}
for (;;) {
if (isdigit(c)) {
val = (val * base) + (s16_t)(c - '0');
c = *++cp;
} else if (base == 16 && isxdigit(c)) {
val = (val << 4) |
(s16_t)(c + 10 - (islower(c) ? 'a' : 'A'));
c = *++cp;
} else
break;
}
if (c == '.') {
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16 bits)
* a.b (with b treated as 24 bits)
*/
if (pp >= parts + 3)
return (0);
*pp++ = val;
c = *++cp;
} else
break;
}
/*
* Check for trailing characters.
*/
if (c != '\0' && (!isascii(c) || !isspace(c)))
return (0);
/*
* Concoct the address according to
* the number of parts specified.
*/
n = pp - parts + 1;
switch (n) {
case 0:
return (0); /* initial nondigit */
case 1: /* a -- 32 bits */
break;
case 2: /* a.b -- 8.24 bits */
if (val > 0xffffff)
return (0);
val |= parts[0] << 24;
break;
case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16);
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
if (addr)
addr->s_addr = htonl(val);
return (1);
}
u32_t inet_addr(const char *cp)
{
struct in_addr val;
if (inet_aton(cp, &val)) {
return (val.s_addr);
}
return INADDR_NONE;
}

View File

@@ -0,0 +1,90 @@
/*----------------------------------------------------------------------------
* 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.
*---------------------------------------------------------------------------*/
#include "tos_k.h"
#include "tos_at_socket_prv.h"
#include "sal_module_wrapper.h"
static socket_ctl_t socket_ctl_table[SOCKET_CTL_MAX] = { 0 };
int socket_id_sanity_check(int id)
{
if (id < 0 ||
id >= TOS_COUNT_OF(socket_ctl_table) ||
!socket_ctl_table[id].is_taken) {
return K_FALSE;
}
return K_TRUE;
}
int socket_id_set_sanity_check(int id)
{
if (!socket_id_sanity_check(id)) {
return K_FALSE;
}
if (socket_ctl_table[id].fd == -1) {
return K_FALSE;
}
return K_TRUE;
}
int socket_id_alloc(void)
{
int i = 0;
socket_ctl_t *the_ctl;
for (i = 0; i < TOS_COUNT_OF(socket_ctl_table); ++i) {
the_ctl = &socket_ctl_table[i];
if (!the_ctl->is_taken) {
the_ctl->is_taken = K_TRUE;
the_ctl->fd = -1;
return i;
}
}
return -1;
}
void socket_id_free(int id)
{
socket_ctl_table[id].is_taken = K_FALSE;
socket_ctl_table[id].fd = -1;
}
void socket_set_protocol(int id, sal_proto_t protocol)
{
socket_ctl_table[id].protocol = protocol;
}
sal_proto_t socket_get_protocol(int id)
{
return socket_ctl_table[id].protocol;
}
void socket_set_fd(int id, int fd)
{
socket_ctl_table[id].fd = fd;
}
int socket_get_fd(int id)
{
return socket_ctl_table[id].fd;
}