Skip to content

Commit 067106f

Browse files
committed
[at_socket] support alloc socket dynamically with at device
1 parent 8f4333d commit 067106f

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

components/net/at/at_socket/at_socket.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ static int alloc_empty_socket(rt_slist_t *l)
302302
return idx;
303303
}
304304

305-
static struct at_socket *alloc_socket_by_device(struct at_device *device)
305+
static struct at_socket *alloc_socket_by_device(struct at_device *device, enum at_socket_type type)
306306
{
307307
static rt_mutex_t at_slock = RT_NULL;
308308
struct at_socket *sock = RT_NULL;
@@ -323,14 +323,21 @@ static struct at_socket *alloc_socket_by_device(struct at_device *device)
323323
rt_mutex_take(at_slock, RT_WAITING_FOREVER);
324324

325325
/* find an empty at socket entry */
326-
for (idx = 0; idx < device->class->socket_num && device->sockets[idx].magic; idx++);
326+
if (device->class->socket_ops->at_socket != RT_NULL)
327+
{
328+
idx = device->class->socket_ops->at_socket(device, type);
329+
}
330+
else
331+
{
332+
for (idx = 0; idx < device->class->socket_num && device->sockets[idx].magic; idx++);
333+
}
327334

328335
/* can't find an empty protocol family entry */
329-
if (idx == device->class->socket_num)
336+
if (idx < 0 || idx >= device->class->socket_num)
330337
{
331338
goto __err;
332339
}
333-
340+
334341
sock = &(device->sockets[idx]);
335342
/* the socket descriptor is the number of sockte lists */
336343
sock->socket = alloc_empty_socket(&(sock->list));
@@ -374,7 +381,7 @@ static struct at_socket *alloc_socket_by_device(struct at_device *device)
374381
return RT_NULL;
375382
}
376383

377-
static struct at_socket *alloc_socket(void)
384+
static struct at_socket *alloc_socket(enum at_socket_type type)
378385
{
379386
extern struct netdev *netdev_default;
380387
struct netdev *netdev = RT_NULL;
@@ -401,7 +408,7 @@ static struct at_socket *alloc_socket(void)
401408
return RT_NULL;
402409
}
403410

404-
return alloc_socket_by_device(device);
411+
return alloc_socket_by_device(device, type);
405412
}
406413

407414
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)
433440
}
434441

435442
/* allocate and initialize a new AT socket */
436-
sock = alloc_socket();
443+
sock = alloc_socket(socket_type);
437444
if (sock == RT_NULL)
438445
{
439446
return -1;
@@ -615,7 +622,7 @@ int at_bind(int socket, const struct sockaddr *name, socklen_t namelen)
615622
}
616623

617624
/* allocate new socket */
618-
new_sock = alloc_socket_by_device(new_device);
625+
new_sock = alloc_socket_by_device(new_device, type);
619626
if (new_sock == RT_NULL)
620627
{
621628
return -1;

components/net/at/at_socket/at_socket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef enum
5656
} at_socket_evt_t;
5757

5858
struct at_socket;
59+
struct at_device;
5960

6061
typedef void (*at_evt_cb_t)(struct at_socket *socket, at_socket_evt_t event, const char *buff, size_t bfsz);
6162

@@ -65,6 +66,7 @@ typedef void (* at_socket_callback)(struct at_socket *conn, int event, uint16_t
6566
/* AT socket operations function */
6667
struct at_socket_ops
6768
{
69+
int (*at_socket)(struct at_device *device, enum at_socket_type type);
6870
int (*at_connect)(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client);
6971
int (*at_closesocket)(struct at_socket *socket);
7072
int (*at_send)(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type);

0 commit comments

Comments
 (0)