2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* net/bluetooth/bluetooth_recvmsg.c
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* 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
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* 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.
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2018-04-29 20:43:09 +02:00
|
|
|
#include <nuttx/wireless/bluetooth/bt_hci.h>
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "bluetooth/bluetooth.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_BLUETOOTH
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct bluetooth_recvfrom_s
|
|
|
|
{
|
|
|
|
FAR struct socket *ir_sock; /* Points to the parent socket structure */
|
|
|
|
FAR struct devif_callback_s *ir_cb; /* Reference to callback instance */
|
|
|
|
FAR struct sockaddr *ir_from; /* Location to return the from address */
|
|
|
|
FAR uint8_t *ir_buffer; /* Pointer to receive buffer */
|
|
|
|
size_t ir_buflen; /* Length of receive buffer */
|
|
|
|
sem_t ir_sem; /* Semaphore signals recv completion */
|
|
|
|
ssize_t ir_result; /* Success:size, failure:negated errno */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-29 08:00:56 +02:00
|
|
|
#if CONFIG_NET_BLUETOOTH_BACKLOG > 0
|
2018-03-31 22:55:03 +02:00
|
|
|
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:
|
2020-08-29 21:28:52 +02:00
|
|
|
* The network is locked
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
static ssize_t
|
|
|
|
bluetooth_recvfrom_rxqueue(FAR struct radio_driver_s *radio,
|
|
|
|
FAR struct bluetooth_recvfrom_s *pstate)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
|
|
|
FAR struct bluetooth_container_s *container;
|
2020-08-29 21:28:52 +02:00
|
|
|
FAR struct sockaddr_l2 *iaddr;
|
2018-03-31 22:55:03 +02:00
|
|
|
FAR struct bluetooth_conn_s *conn;
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
size_t copylen;
|
|
|
|
int ret = -EAGAIN;
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Check if there is anything in in the RX input queue */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* Remove the container from the RX input queue. */
|
|
|
|
|
|
|
|
container = conn->bc_rxhead;
|
|
|
|
DEBUGASSERT(container != NULL);
|
|
|
|
conn->bc_rxhead = container->bn_flink;
|
|
|
|
container->bn_flink = NULL;
|
|
|
|
|
|
|
|
/* Did the RX queue become empty? */
|
|
|
|
|
|
|
|
if (conn->bc_rxhead == NULL)
|
|
|
|
{
|
|
|
|
conn->bc_rxtail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_NET_BLUETOOTH_BACKLOG > 0
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Decrement the count of frames in the queue. */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
DEBUGASSERT(conn->bc_backlog > 0);
|
|
|
|
conn->bc_backlog--;
|
|
|
|
DEBUGASSERT((int)conn->bc_backlog == bluetooth_count_frames(conn));
|
2018-03-31 22:55:03 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract the IOB containing the frame from the container */
|
|
|
|
|
2022-12-16 04:56:40 +01:00
|
|
|
iob = container->bn_iob;
|
2018-03-31 22:55:03 +02:00
|
|
|
container->bn_iob = NULL;
|
|
|
|
DEBUGASSERT(iob != NULL);
|
|
|
|
|
|
|
|
/* Copy the new packet data into the user buffer */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* If a 'from' address pointer was supplied, copy the source address
|
2018-03-31 22:55:03 +02:00
|
|
|
* in the container there.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pstate->ir_from != NULL)
|
|
|
|
{
|
2022-12-16 04:56:40 +01:00
|
|
|
iaddr = (FAR struct sockaddr_l2 *)pstate->ir_from;
|
|
|
|
iaddr->l2_family = AF_BLUETOOTH;
|
2020-08-29 21:28:52 +02:00
|
|
|
BLUETOOTH_ADDRCOPY(&iaddr->l2_bdaddr, &container->bn_raddr);
|
|
|
|
iaddr->l2_cid = container->bn_channel;
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free both the IOB and the container */
|
|
|
|
|
2022-08-08 04:21:03 +02:00
|
|
|
iob_free(iob);
|
2018-03-31 22:55:03 +02:00
|
|
|
bluetooth_container_free(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_recvfrom_eventhandler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint16_t bluetooth_recvfrom_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvpriv,
|
|
|
|
uint16_t flags)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_recvfrom_s *pstate;
|
|
|
|
FAR struct radio_driver_s *radio;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ninfo("flags: %04x\n", flags);
|
|
|
|
|
2022-08-25 15:17:57 +02:00
|
|
|
DEBUGASSERT(pvpriv != NULL && dev != NULL);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
/* Ignore polls from non Bluetooth network drivers */
|
|
|
|
|
|
|
|
if (dev->d_lltype != NET_LL_BLUETOOTH)
|
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that this is the driver to which the socket is bound. */
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2023-05-01 16:15:05 +02:00
|
|
|
/* #warning Missing logic */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2022-08-25 15:17:57 +02:00
|
|
|
pstate = pvpriv;
|
2018-03-31 22:55:03 +02:00
|
|
|
radio = (FAR struct radio_driver_s *)dev;
|
|
|
|
|
|
|
|
/* 'pstate' might be null in some race conditions (?) */
|
|
|
|
|
|
|
|
if (pstate != NULL)
|
|
|
|
{
|
|
|
|
/* If a new packet is available, then complete the read action. */
|
|
|
|
|
|
|
|
if ((flags & BLUETOOTH_NEWDATA) != 0)
|
|
|
|
{
|
|
|
|
/* Attempt to receive the frame */
|
|
|
|
|
|
|
|
ret = bluetooth_recvfrom_rxqueue(radio, pstate);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
/* Don't allow any further call backs. */
|
|
|
|
|
2022-12-16 04:56:40 +01:00
|
|
|
pstate->ir_cb->flags = 0;
|
|
|
|
pstate->ir_cb->priv = NULL;
|
|
|
|
pstate->ir_cb->event = NULL;
|
|
|
|
pstate->ir_result = ret;
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
/* indicate that the data has been consumed */
|
|
|
|
|
|
|
|
flags &= ~BLUETOOTH_NEWDATA;
|
|
|
|
|
|
|
|
/* Wake up the waiting thread, returning the number of bytes
|
|
|
|
* actually read.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsem_post(&pstate->ir_sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Name: bluetooth_recvmsg
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Implements the socket recvfrom interface for the case of the AF_INET
|
2020-12-01 07:55:16 +01:00
|
|
|
* and AF_INET6 address families. bluetooth_recvmsg() receives messages
|
2020-05-17 16:47:40 +02:00
|
|
|
* from a socket, and may be used to receive data on a socket whether or
|
|
|
|
* not it is connection-oriented.
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
2020-12-01 07:55:16 +01:00
|
|
|
* 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
|
2018-03-31 22:55:03 +02:00
|
|
|
* 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
|
2020-12-01 07:55:16 +01:00
|
|
|
* msg Buffer to receive data
|
2018-03-31 22:55:03 +02:00
|
|
|
* flags Receive flags
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-12-01 07:55:16 +01:00
|
|
|
* On success, returns the number of characters received. If no data is
|
2018-03-31 22:55:03 +02:00
|
|
|
* available to be received and the peer has performed an orderly shutdown,
|
2020-12-01 07:55:16 +01:00
|
|
|
* recvmsg() will return 0. Otherwise, on errors, a negated errno value is
|
|
|
|
* returned (see recvmsg() for the list of appropriate error values).
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
|
|
|
|
int flags)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
2020-12-01 07:55:16 +01:00
|
|
|
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;
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct bluetooth_conn_s *conn = psock->s_conn;
|
2018-03-31 22:55:03 +02:00
|
|
|
FAR struct radio_driver_s *radio;
|
|
|
|
struct bluetooth_recvfrom_s state;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* If a 'from' address has been provided, verify that it is large
|
|
|
|
* enough to hold this address family.
|
|
|
|
*/
|
|
|
|
|
2020-08-29 21:28:52 +02:00
|
|
|
if (from != NULL && *fromlen < sizeof(struct sockaddr_l2))
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-04-01 00:31:51 +02:00
|
|
|
if (psock->s_type != SOCK_RAW)
|
2018-03-31 22:55:03 +02:00
|
|
|
{
|
|
|
|
nerr("ERROR: Unsupported socket type: %d\n", psock->s_type);
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Perform the packet recvmsg() operation */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
/* Initialize the state structure. This is done with the network
|
|
|
|
* locked because we don't want anything to happen until we are ready.
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
memset(&state, 0, sizeof(struct bluetooth_recvfrom_s));
|
|
|
|
|
|
|
|
state.ir_buflen = len;
|
|
|
|
state.ir_buffer = buf;
|
|
|
|
state.ir_sock = psock;
|
|
|
|
state.ir_from = from;
|
|
|
|
|
|
|
|
/* Get the device driver that will service this transfer */
|
|
|
|
|
|
|
|
radio = bluetooth_find_device(conn, &conn->bc_laddr);
|
|
|
|
if (radio == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Before we wait for data, let's check if there are already frame(s)
|
|
|
|
* waiting in the RX queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = bluetooth_recvfrom_rxqueue(radio, &state);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
/* Good newe! We have a frame and we are done. */
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&state.ir_sem, 0, 0); /* Doesn't really fail */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
|
|
|
state.ir_cb = bluetooth_callback_alloc(&radio->r_dev, conn);
|
|
|
|
if (state.ir_cb)
|
|
|
|
{
|
2022-12-16 04:56:40 +01:00
|
|
|
state.ir_cb->flags = (BLUETOOTH_NEWDATA | BLUETOOTH_POLL);
|
|
|
|
state.ir_cb->priv = (FAR void *)&state;
|
|
|
|
state.ir_cb->event = bluetooth_recvfrom_eventhandler;
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
/* Wait for either the receive to complete or for an error/timeout to
|
2023-01-13 07:51:38 +01:00
|
|
|
* occur. NOTES: (1) net_sem_wait will also terminate if a signal
|
2018-03-31 22:55:03 +02:00
|
|
|
* is received, (2) the network is locked! It will be un-locked while
|
|
|
|
* the task sleeps and automatically re-locked when the task restarts.
|
|
|
|
*/
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
net_sem_wait(&state.ir_sem);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
|
|
|
/* Make sure that no further events are processed */
|
|
|
|
|
|
|
|
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:
|
|
|
|
net_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_BLUETOOTH */
|