* net/bluetooth/bluetooth_recvmsg.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <assert.h>
#include <netpacket/bluetooth.h>
#include <arch/irq.h>
#include <nuttx/semaphore.h>
#include <nuttx/mm/iob.h>
#include <nuttx/net/net.h>
#include <nuttx/net/radiodev.h>
#include <nuttx/net/bluetooth.h>
#include <nuttx/wireless/bluetooth/bt_hci.h>
#include "netdev/netdev.h"
#include "devif/devif.h"
#include "socket/socket.h"
#include "utils/utils.h"
#include "bluetooth/bluetooth.h"
#ifdef CONFIG_NET_BLUETOOTH
* Private Types
****************************************************************************/
struct bluetooth_recvfrom_s
{
FAR struct socket *ir_sock;
FAR struct devif_callback_s *ir_cb;
FAR struct sockaddr *ir_from;
FAR uint8_t *ir_buffer;
size_t ir_buflen;
sem_t ir_sem;
ssize_t ir_result;
};
* Private Functions
****************************************************************************/
* Name: bluetooth_count_frames
*
* Description:
* Return the number of frames in the RX queue.
*
* Input Parameters:
* conn - The socket connection structure.
*
* Returned Value:
* The number of frames in the queue.
*
****************************************************************************/
#if CONFIG_NET_BLUETOOTH_BACKLOG > 0
static int bluetooth_count_frames(FAR struct bluetooth_conn_s *conn)
{
FAR struct bluetooth_container_s *container;
int count;
for (count = 0, container = conn->bc_rxhead;
container != NULL;
count++, container = container->bn_flink)
{
}
return count;
}
#endif
* Name: bluetooth_recvfrom_sender
*
* Description:
* Perform the reception operation if there are any queued frames in the
* RX frame queue.
*
* Input Parameters:
*
* Returned Value:
*
* Assumptions:
* The network is locked
*
****************************************************************************/
static ssize_t
bluetooth_recvfrom_rxqueue(FAR struct radio_driver_s *radio,
FAR struct bluetooth_recvfrom_s *pstate)
{
FAR struct bluetooth_container_s *container;
FAR struct sockaddr_l2 *iaddr;
FAR struct bluetooth_conn_s *conn;
FAR struct iob_s *iob;
size_t copylen;
int ret = -EAGAIN;
DEBUGASSERT(pstate != NULL && pstate->ir_sock != NULL);
conn = (FAR struct bluetooth_conn_s *)pstate->ir_sock->s_conn;
DEBUGASSERT(conn != NULL);
if (conn->bc_rxhead != NULL)
{
container = conn->bc_rxhead;
DEBUGASSERT(container != NULL);
conn->bc_rxhead = container->bn_flink;
container->bn_flink = NULL;
if (conn->bc_rxhead == NULL)
{
conn->bc_rxtail = NULL;
}
#if CONFIG_NET_BLUETOOTH_BACKLOG > 0
DEBUGASSERT(conn->bc_backlog > 0);
conn->bc_backlog--;
DEBUGASSERT((int)conn->bc_backlog == bluetooth_count_frames(conn));
#endif
iob = container->bn_iob;
container->bn_iob = NULL;
DEBUGASSERT(iob != NULL);
copylen = iob->io_len - iob->io_offset;
memcpy(pstate->ir_buffer, &iob->io_data[iob->io_offset], copylen);
ninfo("Received %d bytes\n", (int)copylen);
ret = copylen;
* in the container there.
*/
if (pstate->ir_from != NULL)
{
iaddr = (FAR struct sockaddr_l2 *)pstate->ir_from;
iaddr->l2_family = AF_BLUETOOTH;
BLUETOOTH_ADDRCOPY(&iaddr->l2_bdaddr, &container->bn_raddr);
iaddr->l2_cid = container->bn_channel;
}
iob_free(iob);
bluetooth_container_free(container);
}
return ret;
}
* Name: bluetooth_recvfrom_eventhandler
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static uint32_t bluetooth_recvfrom_eventhandler(FAR struct net_driver_s *dev,
FAR void *pvpriv,
uint32_t flags)
{
FAR struct bluetooth_recvfrom_s *pstate;
FAR struct radio_driver_s *radio;
ssize_t ret;
ninfo("flags: %" PRIx32 "\n", flags);
DEBUGASSERT(pvpriv != NULL && dev != NULL);
if (dev->d_lltype != NET_LL_BLUETOOTH)
{
return flags;
}
pstate = pvpriv;
radio = (FAR struct radio_driver_s *)dev;
if (pstate != NULL)
{
if ((flags & BLUETOOTH_NEWDATA) != 0)
{
ret = bluetooth_recvfrom_rxqueue(radio, pstate);
if (ret > 0)
{
pstate->ir_cb->flags = 0;
pstate->ir_cb->priv = NULL;
pstate->ir_cb->event = NULL;
pstate->ir_result = ret;
flags &= ~BLUETOOTH_NEWDATA;
* actually read.
*/
nxsem_post(&pstate->ir_sem);
}
}
}
return flags;
}
* Public Functions
****************************************************************************/
* Name: bluetooth_recvmsg
*
* Description:
* Implements the socket recvfrom interface for the case of the AF_INET
* and AF_INET6 address families. bluetooth_recvmsg() receives messages
* from a socket, and may be used to receive data on a socket whether or
* not it is connection-oriented.
*
* If msg_name is not NULL, and the underlying protocol provides the source
* address, this source address is filled in. The argument 'msg_namelen' is
* initialized to the size of the buffer associated with msg_name, and
* modified on return to indicate the actual size of the address stored
* there.
*
* Input Parameters:
* psock A pointer to a NuttX-specific, internal socket structure
* msg Buffer to receive data
* flags Receive flags
*
* Returned Value:
* On success, returns the number of characters received. If no data is
* available to be received and the peer has performed an orderly shutdown,
* recvmsg() will return 0. Otherwise, on errors, a negated errno value is
* returned (see recvmsg() for the list of appropriate error values).
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
int flags)
{
FAR void *buf = msg->msg_iov->iov_base;
size_t len = msg->msg_iov->iov_len;
FAR struct sockaddr *from = msg->msg_name;
FAR socklen_t *fromlen = &msg->msg_namelen;
FAR struct bluetooth_conn_s *conn = psock->s_conn;
FAR struct radio_driver_s *radio;
struct bluetooth_recvfrom_s state;
ssize_t ret;
* enough to hold this address family.
*/
if (from != NULL && *fromlen < sizeof(struct sockaddr_l2))
{
return -EINVAL;
}
if (psock->s_type != SOCK_RAW)
{
nerr("ERROR: Unsupported socket type: %d\n", psock->s_type);
return -EPROTONOSUPPORT;
}
if (msg->msg_iovlen != 1)
{
return -ENOTSUP;
}
* locked because we don't want anything to happen until we are ready.
*/
memset(&state, 0, sizeof(struct bluetooth_recvfrom_s));
state.ir_buflen = len;
state.ir_buffer = buf;
state.ir_sock = psock;
state.ir_from = from;
radio = bluetooth_find_device(conn, &conn->bc_laddr);
if (radio == NULL)
{
ret = -ENODEV;
goto errout_with_lock;
}
conn_dev_lock(&conn->bc_conn, &radio->r_dev);
* waiting in the RX queue.
*/
ret = bluetooth_recvfrom_rxqueue(radio, &state);
if (ret > 0)
{
conn_dev_unlock(&conn->bc_conn, &radio->r_dev);
return ret;
}
nxsem_init(&state.ir_sem, 0, 0);
state.ir_cb = bluetooth_callback_alloc(&radio->r_dev, conn);
if (state.ir_cb)
{
state.ir_cb->flags = (BLUETOOTH_NEWDATA | BLUETOOTH_POLL);
state.ir_cb->priv = (FAR void *)&state;
state.ir_cb->event = bluetooth_recvfrom_eventhandler;
* occur. NOTES: (1) conn_dev_sem_timedwait will also terminate if a
* signal is received, (2) the network is locked! It will be un-locked
* while the task sleeps and automatically re-locked when the task
* restarts.
*/
conn_dev_sem_timedwait(&state.ir_sem, true, UINT_MAX,
&conn->bc_conn, &radio->r_dev);
bluetooth_callback_free(&radio->r_dev, conn, state.ir_cb);
ret = state.ir_result;
}
else
{
ret = -EBUSY;
}
nxsem_destroy(&state.ir_sem);
errout_with_lock:
conn_dev_unlock(&conn->bc_conn, &radio->r_dev);
return ret;
}
#endif