From 067106f164d7ace094bdf4a08279eddfc0e4a5b7 Mon Sep 17 00:00:00 2001 From: luhuadong Date: Tue, 21 Jul 2020 00:29:12 +0800 Subject: [PATCH 1/2] [at_socket] support alloc socket dynamically with at device --- components/net/at/at_socket/at_socket.c | 23 +++++++++++++++-------- components/net/at/at_socket/at_socket.h | 2 ++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/components/net/at/at_socket/at_socket.c b/components/net/at/at_socket/at_socket.c index ca57677daf..666b0f509e 100644 --- a/components/net/at/at_socket/at_socket.c +++ b/components/net/at/at_socket/at_socket.c @@ -302,7 +302,7 @@ static int alloc_empty_socket(rt_slist_t *l) return idx; } -static struct at_socket *alloc_socket_by_device(struct at_device *device) +static struct at_socket *alloc_socket_by_device(struct at_device *device, enum at_socket_type type) { static rt_mutex_t at_slock = RT_NULL; struct at_socket *sock = RT_NULL; @@ -323,14 +323,21 @@ static struct at_socket *alloc_socket_by_device(struct at_device *device) rt_mutex_take(at_slock, RT_WAITING_FOREVER); /* find an empty at socket entry */ - for (idx = 0; idx < device->class->socket_num && device->sockets[idx].magic; idx++); + if (device->class->socket_ops->at_socket != RT_NULL) + { + idx = device->class->socket_ops->at_socket(device, type); + } + else + { + for (idx = 0; idx < device->class->socket_num && device->sockets[idx].magic; idx++); + } /* can't find an empty protocol family entry */ - if (idx == device->class->socket_num) + if (idx < 0 || idx >= device->class->socket_num) { goto __err; } - + sock = &(device->sockets[idx]); /* the socket descriptor is the number of sockte lists */ sock->socket = alloc_empty_socket(&(sock->list)); @@ -374,7 +381,7 @@ __err: return RT_NULL; } -static struct at_socket *alloc_socket(void) +static struct at_socket *alloc_socket(enum at_socket_type type) { extern struct netdev *netdev_default; struct netdev *netdev = RT_NULL; @@ -401,7 +408,7 @@ static struct at_socket *alloc_socket(void) return RT_NULL; } - return alloc_socket_by_device(device); + return alloc_socket_by_device(device, type); } static void at_recv_notice_cb(struct at_socket *sock, at_socket_evt_t event, const char *buff, size_t bfsz); @@ -433,7 +440,7 @@ int at_socket(int domain, int type, int protocol) } /* allocate and initialize a new AT socket */ - sock = alloc_socket(); + sock = alloc_socket(socket_type); if (sock == RT_NULL) { return -1; @@ -615,7 +622,7 @@ int at_bind(int socket, const struct sockaddr *name, socklen_t namelen) } /* allocate new socket */ - new_sock = alloc_socket_by_device(new_device); + new_sock = alloc_socket_by_device(new_device, type); if (new_sock == RT_NULL) { return -1; diff --git a/components/net/at/at_socket/at_socket.h b/components/net/at/at_socket/at_socket.h index d98fa05f24..07e3de345c 100644 --- a/components/net/at/at_socket/at_socket.h +++ b/components/net/at/at_socket/at_socket.h @@ -56,6 +56,7 @@ typedef enum } at_socket_evt_t; struct at_socket; +struct at_device; typedef void (*at_evt_cb_t)(struct at_socket *socket, at_socket_evt_t event, const char *buff, size_t bfsz); @@ -65,6 +66,7 @@ typedef void (* at_socket_callback)(struct at_socket *conn, int event, uint16_t /* AT socket operations function */ struct at_socket_ops { + int (*at_socket)(struct at_device *device, enum at_socket_type type); int (*at_connect)(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client); int (*at_closesocket)(struct at_socket *socket); int (*at_send)(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type); From f56af401cc4f41b80b4039fd9aabbf42ffa2ed4b Mon Sep 17 00:00:00 2001 From: luhuadong Date: Thu, 23 Jul 2020 01:13:48 +0800 Subject: [PATCH 2/2] [at_socket] update AT_SW_VERSION and adjust at_socket_ops --- components/net/at/Kconfig | 2 +- components/net/at/at_socket/at_socket.h | 2 +- components/net/at/include/at.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/net/at/Kconfig b/components/net/at/Kconfig index 92818331af..4aedbb888b 100644 --- a/components/net/at/Kconfig +++ b/components/net/at/Kconfig @@ -82,7 +82,7 @@ if RT_USING_AT config AT_SW_VERSION_NUM hex - default 0x10300 + default 0x10301 help software module version number diff --git a/components/net/at/at_socket/at_socket.h b/components/net/at/at_socket/at_socket.h index 07e3de345c..3317579f05 100644 --- a/components/net/at/at_socket/at_socket.h +++ b/components/net/at/at_socket/at_socket.h @@ -66,12 +66,12 @@ typedef void (* at_socket_callback)(struct at_socket *conn, int event, uint16_t /* AT socket operations function */ struct at_socket_ops { - int (*at_socket)(struct at_device *device, enum at_socket_type type); int (*at_connect)(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client); int (*at_closesocket)(struct at_socket *socket); int (*at_send)(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type); int (*at_domain_resolve)(const char *name, char ip[16]); void (*at_set_event_cb)(at_socket_evt_t event, at_evt_cb_t cb); + int (*at_socket)(struct at_device *device, enum at_socket_type type); }; /* AT receive package list structure */ diff --git a/components/net/at/include/at.h b/components/net/at/include/at.h index a7a6aeca7a..2d0e270f6a 100644 --- a/components/net/at/include/at.h +++ b/components/net/at/include/at.h @@ -18,7 +18,7 @@ extern "C" { #endif -#define AT_SW_VERSION "1.3.0" +#define AT_SW_VERSION "1.3.1" #define AT_CMD_NAME_LEN 16 #define AT_END_MARK_LEN 4