| 378 | } |
| 379 | |
| 380 | static struct at_socket *alloc_socket_by_device(struct at_device *device, enum at_socket_type type) |
| 381 | { |
| 382 | rt_base_t level; |
| 383 | struct at_socket *sock = RT_NULL; |
| 384 | char name[RT_NAME_MAX] = {0}; |
| 385 | int idx = 0; |
| 386 | |
| 387 | if (at_slock == RT_NULL) |
| 388 | { |
| 389 | /* create AT socket lock */ |
| 390 | at_slock = rt_mutex_create("at_slock", RT_IPC_FLAG_PRIO); |
| 391 | if (at_slock == RT_NULL) |
| 392 | { |
| 393 | LOG_E("No memory for socket allocation lock!"); |
| 394 | return RT_NULL; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | rt_mutex_take(at_slock, RT_WAITING_FOREVER); |
| 399 | |
| 400 | /* find an empty at socket entry */ |
| 401 | if (device->class->socket_ops->at_socket != RT_NULL) |
| 402 | { |
| 403 | idx = device->class->socket_ops->at_socket(device, type); |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | for (idx = 0; idx < device->class->socket_num && device->sockets[idx].magic == AT_SOCKET_MAGIC; idx++); |
| 408 | } |
| 409 | |
| 410 | /* can't find an empty protocol family entry */ |
| 411 | if (idx < 0 || idx >= device->class->socket_num) |
| 412 | { |
| 413 | LOG_E("can't find an empty protocol family entry."); |
| 414 | goto __err; |
| 415 | } |
| 416 | |
| 417 | sock = &(device->sockets[idx]); |
| 418 | /* the socket descriptor is the number of sockte lists */ |
| 419 | sock->socket = idx; |
| 420 | /* the socket operations is the specify operations of the device */ |
| 421 | sock->ops = device->class->socket_ops; |
| 422 | /* the user-data is the at device socket descriptor */ |
| 423 | sock->user_data = (void *) idx; |
| 424 | sock->device = (void *) device; |
| 425 | sock->magic = AT_SOCKET_MAGIC; |
| 426 | sock->state = AT_SOCKET_NONE; |
| 427 | sock->rcvevent = RT_NULL; |
| 428 | sock->sendevent = RT_NULL; |
| 429 | sock->errevent = RT_NULL; |
| 430 | |
| 431 | rt_slist_init(&(sock->list)); |
| 432 | level = rt_hw_interrupt_disable(); |
| 433 | rt_slist_insert(&_socket_list, &(sock->list)); |
| 434 | rt_hw_interrupt_enable(level); |
| 435 | |
| 436 | rt_slist_init(&sock->recvpkt_list); |
| 437 | #ifdef SAL_USING_POSIX |
no test coverage detected