Skip to content

Commit

Permalink
[wip] nimble/transport: Add ipc_icbmsg
Browse files Browse the repository at this point in the history
  • Loading branch information
mkasenberg committed Sep 30, 2024
1 parent caecb2c commit b5fda2a
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ struct hci_ipc_shm {
void hci_ipc_init(volatile struct hci_ipc_shm *shm, struct hci_ipc_sm *sm);
int hci_ipc_rx(struct hci_ipc_sm *sm, const uint8_t *buf, uint16_t len);

#if 0
extern void hci_ipc_atomic_put(volatile uint16_t *num);
extern uint16_t hci_ipc_atomic_get(volatile uint16_t *num);

/* Just to optimize static inlines below, do not use directly! */
extern volatile struct hci_ipc_shm *g_ipc_shm;
#endif

static inline int
hci_ipc_get(uint8_t type)
{
return 1;
#if 0
volatile struct hci_ipc_shm *shm = g_ipc_shm;

switch (type) {
Expand All @@ -76,11 +80,13 @@ hci_ipc_get(uint8_t type)
}

return 0;
#endif
}

static inline void
hci_ipc_put(uint8_t type)
{
#if 0
volatile struct hci_ipc_shm *shm = g_ipc_shm;

switch (type) {
Expand All @@ -94,6 +100,7 @@ hci_ipc_put(uint8_t type)
hci_ipc_atomic_put(&shm->n2a_num_evt_disc);
break;
}
#endif
}

#endif /* _HCI_IPC_H_ */
4 changes: 3 additions & 1 deletion nimble/transport/common/hci_ipc/src/hci_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,11 @@ hci_ipc_rx(struct hci_ipc_sm *sm, const uint8_t *buf, uint16_t len)
void
hci_ipc_init(volatile struct hci_ipc_shm *shm, struct hci_ipc_sm *sm)
{
memset(sm, 0, sizeof(*sm));
#if 0
assert(g_ipc_shm == NULL);

g_ipc_shm = shm;
memset(sm, 0, sizeof(*sm));

#if MYNEWT_VAL(BLE_CONTROLLER)
while (shm->n2a_num_evt_disc == 0) {
Expand All @@ -239,4 +240,5 @@ hci_ipc_init(volatile struct hci_ipc_shm *shm, struct hci_ipc_sm *sm)
shm->n2a_num_evt = MYNEWT_VAL(BLE_TRANSPORT_EVT_COUNT);
shm->n2a_num_evt_disc = MYNEWT_VAL(BLE_TRANSPORT_EVT_DISCARDABLE_COUNT);
#endif
#endif
}
36 changes: 36 additions & 0 deletions nimble/transport/ipc_icbmsg/pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# 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.
#

pkg.name: nimble/transport/ipc_icbmsg
pkg.description: HCI transport via IPC
pkg.author: "Apache Mynewt <[email protected]>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
- ble
- bluetooth
- ipc_icbmsg

pkg.deps:
- nimble
- nimble/transport/common/hci_ipc
- "@apache-mynewt-core/kernel/os"
- "@apache-mynewt-core/hw/drivers/ipc_icbmsg"

pkg.apis:
- ble_transport
216 changes: 216 additions & 0 deletions nimble/transport/ipc_icbmsg/src/icbmsg_ble_hci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* 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 <string.h>
#include <syscfg/syscfg.h>
#include <sysinit/sysinit.h>
#include <nimble/ble.h>
#include <ipc_icbmsg/ipc_icbmsg.h>
#include <nimble/transport.h>
#include <nimble/transport/hci_ipc.h>

#define BLE_HCI_IPC_ID (0)

static struct hci_ipc_sm g_hci_ipc_sm;

static void ble_hci_trans_rx(const void *data, size_t len, void *user_data);
static struct ipc_ept_cfg hci_ept_cfg = {
.name = "nrf_bt_hci",
.cb = {
// .bound = hci_ept_bound,
.received = ble_hci_trans_rx,
},
};
static uint8_t hci_ept_local_addr;

static int
icbmsg_ble_hci_send_mbuf(struct hci_ipc_hdr *hdr, struct os_mbuf *om)
{
int rc;
struct os_mbuf *x;
struct ipc_icmsg_buf buf;

rc = ipc_icbmsg_alloc_tx_buf(BLE_HCI_IPC_ID, &buf,
sizeof(*hdr) + OS_MBUF_PKTHDR(om)->omp_len);
assert(rc == 0);
if (rc != 0) {
return BLE_ERR_MEM_CAPACITY;
}

memcpy(buf.data, hdr, sizeof(*hdr));
buf.len = sizeof(*hdr);

x = om;
while (x) {
memcpy(buf.data + buf.len, x->om_data, x->om_len);
buf.len += x->om_len;
x = SLIST_NEXT(x, om_next);
}

rc = ipc_icbmsg_send_buf(BLE_HCI_IPC_ID, hci_ept_local_addr, &buf);

os_mbuf_free_chain(om);

return (rc < 0) ? BLE_ERR_MEM_CAPACITY : 0;
}

static int
icbmsg_ble_hci_acl_tx(struct os_mbuf *om)
{
struct hci_ipc_hdr hdr;

hdr.type = HCI_IPC_TYPE_ACL;
hdr.length = 4 + get_le16(&om->om_data[2]);

return icbmsg_ble_hci_send_mbuf(&hdr, om);
}

#if !MYNEWT_VAL(BLE_CONTROLLER)
static int
icbmsg_ble_hci_iso_tx(struct os_mbuf *om)
{
struct hci_ipc_hdr hdr;

hdr.type = HCI_IPC_TYPE_ISO;
hdr.length = 4 + get_le16(&om->om_data[2]);

return icbmsg_ble_hci_send_mbuf(&hdr, om);
}
#endif

static void
ble_hci_trans_rx(const void *data, size_t len, void *user_data)
{
// assert(channel == IPC_RX_CHANNEL);

hci_ipc_rx(&g_hci_ipc_sm, data, len);
}

static void
icbmsg_ble_hci_init(void)
{
os_sr_t sr;

SYSINIT_ASSERT_ACTIVE();

OS_ENTER_CRITICAL(sr);
hci_ept_local_addr = ipc_icmsg_register_ept(BLE_HCI_IPC_ID, &hci_ept_cfg);
OS_EXIT_CRITICAL(sr);

// OS_ENTER_CRITICAL(sr);
// ble_hci_trans_rx(IPC_RX_CHANNEL, NULL);
// OS_EXIT_CRITICAL(sr);
}

#if MYNEWT_VAL(BLE_CONTROLLER)
int
ble_transport_to_hs_evt_impl(void *ev_buf)
{
int rc;
uint8_t *hci_ev = ev_buf;
struct ipc_icmsg_buf buf;
struct hci_ipc_hdr hdr;

hdr.type = ble_transport_ipc_buf_evt_type_get(ev_buf);
hdr.length = 2 + hci_ev[1];

rc = ipc_icbmsg_alloc_tx_buf(BLE_HCI_IPC_ID, &buf,
sizeof(hdr) + hdr.length);
assert(rc == 0);
if (rc != 0) {
return BLE_ERR_MEM_CAPACITY;
}

memcpy(buf.data, &hdr, sizeof(hdr));
buf.len = sizeof(hdr);

memcpy(buf.data + buf.len, hci_ev, hdr.length);
buf.len += hdr.length;

rc = ipc_icbmsg_send_buf(BLE_HCI_IPC_ID, hci_ept_local_addr, &buf);

ble_transport_ipc_free(ev_buf);

return (rc < 0) ? BLE_ERR_MEM_CAPACITY : 0;
}

int
ble_transport_to_hs_acl_impl(struct os_mbuf *om)
{
return icbmsg_ble_hci_acl_tx(om);
}

void
ble_transport_hs_init(void)
{
hci_ipc_init(NULL, &g_hci_ipc_sm);
icbmsg_ble_hci_init();
}
#endif /* BLE_CONTROLLER */

#if !MYNEWT_VAL(BLE_CONTROLLER)
int
ble_transport_to_ll_cmd_impl(void *ev_buf)
{
int rc;
uint8_t *cmd = ev_buf;
struct ipc_icmsg_buf buf;
struct hci_ipc_hdr hdr;

hdr.type = HCI_IPC_TYPE_CMD;
hdr.length = 3 + cmd[2];

rc = ipc_icbmsg_alloc_tx_buf(BLE_HCI_IPC_ID, &buf,
sizeof(hdr) + hdr.length);
assert(rc == 0);
if (rc != 0) {
return BLE_ERR_MEM_CAPACITY;
}

memcpy(buf.data, &hdr, sizeof(hdr));
buf.len = sizeof(hdr);

memcpy(buf.data + buf.len, cmd, hdr.length);
buf.len += hdr.length;

ble_transport_ipc_free(ev_buf);

return (rc < 0) ? BLE_ERR_MEM_CAPACITY : 0;
}

int
ble_transport_to_ll_acl_impl(struct os_mbuf *om)
{
return icbmsg_ble_hci_acl_tx(om);
}

int
ble_transport_to_ll_iso_impl(struct os_mbuf *om)
{
return icbmsg_ble_hci_iso_tx(om);
}

void
ble_transport_ll_init(void)
{
hci_ipc_init(NULL, &g_hci_ipc_sm);
icbmsg_ble_hci_init();
}
#endif /* !BLE_CONTROLLER */
20 changes: 20 additions & 0 deletions nimble/transport/ipc_icbmsg/syscfg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.vals.BLE_MONITOR:
BLE_TRANSPORT_RX_TASK_STACK_SIZE: 120
BLE_TRANSPORT_RX_TASK_PRIO: 1
2 changes: 1 addition & 1 deletion nimble/transport/nrf5340/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pkg.deps:
- nimble
- nimble/transport/common/hci_ipc
- "@apache-mynewt-core/kernel/os"
- "@apache-mynewt-core/hw/drivers/ipc_nrf5340"
- "@apache-mynewt-core/hw/drivers/ipc_icbmsg"

pkg.apis:
- ble_transport
7 changes: 5 additions & 2 deletions nimble/transport/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pkg.keywords:
pkg.apis:
- ble_transport

pkg.deps:
- nimble/transport/ipc_icbmsg
pkg.deps.'BLE_TRANSPORT_HS == "native"':
- nimble/host
pkg.deps.'BLE_TRANSPORT_LL == "native"':
Expand All @@ -36,8 +38,9 @@ pkg.deps.'BLE_TRANSPORT_LL == "emspi"':
- nimble/transport/emspi
pkg.deps.'BLE_TRANSPORT_HS == "dialog_cmac" || BLE_TRANSPORT_LL == "dialog_cmac"':
- nimble/transport/dialog_cmac
pkg.deps.'BLE_TRANSPORT_HS == "nrf5340" || BLE_TRANSPORT_LL == "nrf5340"':
- nimble/transport/nrf5340
pkg.deps.'BLE_TRANSPORT_HS == "ipc_icbmsg" || BLE_TRANSPORT_LL == "ipc_icbmsg"':
- nimble/transport/ipc_icbmsg
# - nimble/transport/nrf5340
pkg.deps.'BLE_TRANSPORT_LL == "socket"':
- nimble/transport/socket
pkg.deps.'BLE_TRANSPORT_HS == "uart"':
Expand Down
2 changes: 2 additions & 0 deletions nimble/transport/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ syscfg.defs:
- native
- dialog_cmac
- nrf5340
- ipc_icbmsg
- uart
- usb
- cdc
Expand All @@ -44,6 +45,7 @@ syscfg.defs:
- emspi
- dialog_cmac
- nrf5340
- ipc_icbmsg
- socket
- apollo3
- uart_ll
Expand Down

0 comments on commit b5fda2a

Please sign in to comment.