@@ -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
407414static 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 ;
0 commit comments