2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/bluetooth/bluetooth_input.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-19 12:45:37 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Handle incoming Bluetooth frame input
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/mm/iob.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 "devif/devif.h"
|
|
|
|
#include "bluetooth/bluetooth.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_BLUETOOTH
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public 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_queue_frame
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add one frame to the connection's RX queue.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The socket connection structure.
|
|
|
|
* framel - A single frame to add to the RX queue.
|
2020-02-23 09:50:23 +01:00
|
|
|
* meta - Meta data characterizing the received frame.
|
2018-03-31 22:55:03 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; A negated errno value is returned on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int bluetooth_queue_frame(FAR struct bluetooth_conn_s *conn,
|
|
|
|
FAR struct iob_s *frame,
|
|
|
|
FAR struct bluetooth_frame_meta_s *meta)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_container_s *container;
|
|
|
|
|
|
|
|
/* Allocate a container for the frame */
|
|
|
|
|
|
|
|
container = bluetooth_container_allocate();
|
|
|
|
if (container == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to allocate a container\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the container */
|
|
|
|
|
|
|
|
memset(&container->bn_raddr, 0, sizeof(bt_addr_t));
|
|
|
|
container->bn_channel = meta->bm_channel;
|
|
|
|
BLUETOOTH_ADDRCOPY(&container->bn_raddr, &meta->bm_raddr);
|
|
|
|
|
|
|
|
DEBUGASSERT(frame != NULL);
|
|
|
|
container->bn_iob = frame;
|
|
|
|
|
|
|
|
/* Add the container to the tail of the list of incoming frames */
|
|
|
|
|
|
|
|
container->bn_flink = NULL;
|
|
|
|
if (conn->bc_rxtail == NULL)
|
|
|
|
{
|
|
|
|
conn->bc_rxhead = container;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn->bc_rxtail->bn_flink = container;
|
|
|
|
}
|
|
|
|
|
2022-07-13 08:02:04 +02:00
|
|
|
conn->bc_rxtail = container;
|
|
|
|
|
2018-03-31 22:55:03 +02:00
|
|
|
#if CONFIG_NET_BLUETOOTH_BACKLOG > 0
|
2021-02-19 15:01:46 +01:00
|
|
|
/* If incrementing the count would exceed the maximum bc_backlog value,
|
|
|
|
* then delete the oldest frame from the head of the RX queue.
|
2019-03-12 21:22:52 +01:00
|
|
|
*/
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-03-12 21:22:52 +01:00
|
|
|
if (conn->bc_backlog >= CONFIG_NET_BLUETOOTH_BACKLOG)
|
|
|
|
{
|
2019-07-03 02:02:23 +02:00
|
|
|
DEBUGASSERT(conn->bc_backlog == CONFIG_NET_BLUETOOTH_BACKLOG);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Remove the container from the tail RX input queue. */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
container = conn->bc_rxhead;
|
|
|
|
DEBUGASSERT(container != NULL);
|
|
|
|
conn->bc_rxhead = container->bn_flink;
|
|
|
|
container->bn_flink = NULL;
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Did the RX queue become empty? */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
if (conn->bc_rxhead == NULL)
|
|
|
|
{
|
|
|
|
conn->bc_rxtail = NULL;
|
|
|
|
}
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
DEBUGASSERT(container != NULL && container->bn_iob != NULL);
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
/* Free both the IOB and the container */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2022-08-08 04:21:03 +02:00
|
|
|
iob_free(container->bn_iob);
|
2019-07-03 02:02:23 +02:00
|
|
|
bluetooth_container_free(container);
|
2019-03-12 21:22:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Increment the count of frames in the queue. */
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-03-12 21:22:52 +01:00
|
|
|
conn->bc_backlog++;
|
|
|
|
}
|
2018-03-31 22:55:03 +02:00
|
|
|
|
2019-03-12 21:22:52 +01:00
|
|
|
DEBUGASSERT((int)conn->bc_backlog == bluetooth_count_frames(conn));
|
2018-03-31 22:55:03 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bluetooth_input
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle incoming Bluetooth input
|
|
|
|
*
|
|
|
|
* This function is called when the radio device driver has received an
|
|
|
|
* frame from the network. The frame from the device driver must be
|
|
|
|
* provided in by the IOB frame argument of the function call:
|
|
|
|
*
|
|
|
|
* - The frame data is in the IOB io_data[] buffer,
|
|
|
|
* - The length of the frame is in the IOB io_len field, and
|
|
|
|
* - The offset past and radio MAC header is provided in the io_offset
|
|
|
|
* field.
|
|
|
|
*
|
|
|
|
* The frame argument may refer to a single frame (a list of length one)
|
|
|
|
* or may it be the head of a list of multiple frames.
|
|
|
|
*
|
|
|
|
* - The io_flink field points to the next frame in the list (if enable)
|
|
|
|
* - The last frame in the list will have io_flink == NULL.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* radio The radio network driver interface.
|
|
|
|
* framelist - The head of an incoming list of frames. Normally this
|
|
|
|
* would be a single frame. A list may be provided if
|
|
|
|
* appropriate, however.
|
|
|
|
* meta - Meta data characterizing the received frame.
|
|
|
|
*
|
|
|
|
* If there are multiple frames in the list, this metadata
|
|
|
|
* must apply to all of the frames in the list.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK The Bluetooth has been processed and can be deleted
|
|
|
|
* ERROR Hold the Bluetooth and try again later. There is a listening
|
|
|
|
* socket but no recv in place to catch the Bluetooth yet.
|
|
|
|
* Useful when a packet arrives before a recv call is in place.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from the network diver with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int bluetooth_input(FAR struct radio_driver_s *radio,
|
|
|
|
FAR struct iob_s *framelist,
|
|
|
|
FAR struct bluetooth_frame_meta_s *meta)
|
|
|
|
{
|
|
|
|
FAR struct bluetooth_conn_s *conn;
|
|
|
|
FAR struct iob_s *frame;
|
|
|
|
FAR struct iob_s *next;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Check if there is a connection that will accept this packet */
|
|
|
|
|
|
|
|
conn = bluetooth_conn_active(meta);
|
|
|
|
if (conn != NULL)
|
|
|
|
{
|
|
|
|
/* Setup for the application callback (NOTE: These should not be
|
|
|
|
* used by PF_BLUETOOTH sockets).
|
|
|
|
*/
|
|
|
|
|
|
|
|
radio->r_dev.d_appdata = radio->r_dev.d_buf;
|
|
|
|
radio->r_dev.d_len = 0;
|
|
|
|
radio->r_dev.d_sndlen = 0;
|
|
|
|
|
|
|
|
/* The framelist probably contains only a single frame, but we will
|
|
|
|
* process it as a list of frames.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (frame = framelist; frame != NULL; frame = next)
|
|
|
|
{
|
|
|
|
/* Remove the frame from the list */
|
|
|
|
|
2022-12-16 04:56:40 +01:00
|
|
|
next = frame->io_flink;
|
2018-03-31 22:55:03 +02:00
|
|
|
frame->io_flink = NULL;
|
|
|
|
|
|
|
|
/* Add the frame to the RX queue */
|
|
|
|
|
|
|
|
ret = bluetooth_queue_frame(conn, frame, meta);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to queue frame: %d\n", ret);
|
2022-08-08 04:21:03 +02:00
|
|
|
iob_free(frame);
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the application callback. The frame may be processed now
|
|
|
|
* if there is a user wait for an incoming frame. Or it may pend in
|
|
|
|
* the RX queue until some user process reads the frame. NOTE: The
|
|
|
|
* return value from bluetooth_callback would distinguish these
|
|
|
|
* cases: BLUETOOTH_NEWDATA will still be processed if the frame
|
|
|
|
* was not consumed.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
bluetooth_callback(radio, conn, BLUETOOTH_NEWDATA);
|
2018-03-31 22:55:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nwarn("WARNING: No listener\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_BLUETOOTH */
|