Skip to content

[at_socket] support alloc socket dynamically with at device #3755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/net/at/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ if RT_USING_AT

config AT_SW_VERSION_NUM
hex
default 0x10300
default 0x10301
help
software module version number

Expand Down
23 changes: 15 additions & 8 deletions components/net/at/at_socket/at_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -374,7 +381,7 @@ static struct at_socket *alloc_socket_by_device(struct at_device *device)
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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions components/net/at/at_socket/at_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -70,6 +71,7 @@ struct at_socket_ops
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 */
Expand Down
2 changes: 1 addition & 1 deletion components/net/at/include/at.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down