Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nimble/transport: Add common HCI RX task #1647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nimble/transport/include/nimble/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ struct os_mbuf;
/* Initialization */
void ble_transport_init(void);

/* Common HCI RX task functions */
typedef void ble_transport_rx_func_t(void *arg);
void ble_transport_rx_register(ble_transport_rx_func_t *func, void *arg);
void ble_transport_rx(void);

/* Allocators for supported data types */
void *ble_transport_alloc_cmd(void);
void *ble_transport_alloc_evt(int discardable);
Expand Down
31 changes: 23 additions & 8 deletions nimble/transport/nrf5340/src/nrf5340_ble_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@

static struct hci_ipc_sm g_hci_ipc_sm;

static void
rx_func(void *arg)
{
uint8_t *buf;
int len;

len = ipc_nrf5340_available_buf(IPC_RX_CHANNEL, (void **)&buf);
while (len > 0) {
len = hci_ipc_rx(&g_hci_ipc_sm, buf, len);
ipc_nrf5340_consume(IPC_RX_CHANNEL, len);
len = ipc_nrf5340_available_buf(IPC_RX_CHANNEL, (void **)&buf);
}
}

static int
nrf5340_ble_hci_acl_tx(struct os_mbuf *om)
{
Expand Down Expand Up @@ -95,22 +109,23 @@ nrf5340_ble_hci_iso_tx(struct os_mbuf *om)
static void
nrf5340_ble_hci_trans_rx(int channel, void *user_data)
{
uint8_t *buf;
int len;
assert(channel == IPC_RX_CHANNEL);

len = ipc_nrf5340_available_buf(channel, (void **)&buf);
while (len > 0) {
len = hci_ipc_rx(&g_hci_ipc_sm, buf, len);
ipc_nrf5340_consume(channel, len);
len = ipc_nrf5340_available_buf(channel, (void **)&buf);
}
#if MYNEWT_VAL(BLE_TRANSPORT_NRF5340_RX_TASK)
ble_transport_rx();
#else
rx_func(NULL);
#endif
}

static void
nrf5340_ble_hci_init(void)
{
SYSINIT_ASSERT_ACTIVE();

#if MYNEWT_VAL(BLE_TRANSPORT_NRF5340_RX_TASK)
ble_transport_rx_register(rx_func, NULL);
#endif
ipc_nrf5340_recv(IPC_RX_CHANNEL, nrf5340_ble_hci_trans_rx, NULL);
}

Expand Down
28 changes: 28 additions & 0 deletions nimble/transport/nrf5340/syscfg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.

syscfg.defs:
BLE_TRANSPORT_NRF5340_RX_TASK:
description: >
Use separate task for RX. This is required and enabled by default
if monitor is used, optional otherwise.
value: 0

syscfg.vals.BLE_MONITOR:
BLE_TRANSPORT_NRF5340_RX_TASK: 1
BLE_TRANSPORT_RX_PRIO: 1
BLE_TRANSPORT_RX_STACK_SIZE: 120
84 changes: 84 additions & 0 deletions nimble/transport/src/rx_task.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.
*/

#include <assert.h>
#include <syscfg/syscfg.h>
#if MYNEWT
#include <os/os_task.h>
#endif
#include <os/os_mbuf.h>
#include <nimble/transport.h>
#include <nimble/nimble_npl.h>

#if !defined(MYNEWT) || MYNEWT_VAL(BLE_TRANSPORT_RX_STACK_SIZE) > 0

static ble_transport_rx_func_t *rx_func;
static void *rx_func_arg;

#if MYNEWT
OS_TASK_STACK_DEFINE(rx_stack, MYNEWT_VAL(BLE_TRANSPORT_RX_STACK_SIZE));
static struct os_task rx_task;
#endif

static struct ble_npl_eventq rx_eventq;
static struct ble_npl_event rx_event;
Fixed Show fixed Hide fixed

static void
rx_event_func(struct ble_npl_event *ev)
Fixed Show fixed Hide fixed
{
rx_func(rx_func_arg);
}

static void
rx_task_func(void *arg)

Check notice

Code scanning / CodeQL

Unused static function Note

Static function rx_task_func is unreachable
{
struct ble_npl_event *ev;

ble_npl_eventq_init(&rx_eventq);

while (1) {
ev = ble_npl_eventq_get(&rx_eventq, BLE_NPL_TIME_FOREVER);
ble_npl_event_run(ev);
}
}

void
ble_transport_rx_register(ble_transport_rx_func_t *func, void *arg)
{
assert(func && !rx_func);

rx_func = func;
rx_func_arg = arg;

ble_npl_event_init(&rx_event, rx_event_func, NULL);

#ifdef MYNEWT
os_task_init(&rx_task, "hci_rx_task", rx_task_func, NULL,
MYNEWT_VAL(BLE_TRANSPORT_RX_PRIO), OS_WAIT_FOREVER, rx_stack,
MYNEWT_VAL(BLE_TRANSPORT_RX_STACK_SIZE));
#endif
}

void
ble_transport_rx(void)
{
ble_npl_eventq_put(&rx_eventq, &rx_event);
}

#endif
3 changes: 3 additions & 0 deletions nimble/transport/syscfg.monitor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@ syscfg.defs:
length value will be split.
value: 128

syscfg.defs.'BLE_MONITOR_UART || BLE_MONITOR_RTT':
BLE_MONITOR: 1

syscfg.restrictions:
- '!(BLE_MONITOR_UART && BLE_MONITOR_RTT)'
8 changes: 8 additions & 0 deletions nimble/transport/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ syscfg.defs:
of ISO buffers available for controller.
value: MYNEWT_VAL(BLE_TRANSPORT_ISO_COUNT)

BLE_TRANSPORT_RX_PRIO:
description: Priority of RX task
type: task_priority
value: 126
BLE_TRANSPORT_RX_STACK_SIZE:
description: Stack size of RX task
value:

# import monitor and defunct settings from separate file to reduce clutter in main file
$import:
- "@apache-mynewt-nimble/nimble/transport/syscfg.monitor.yml"
Expand Down
Loading