|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <assert.h> |
| 21 | +#include <string.h> |
| 22 | +#include <syscfg/syscfg.h> |
| 23 | +#include <sysinit/sysinit.h> |
| 24 | +#include <nimble/ble.h> |
| 25 | +#include <ipc_icbmsg/ipc_icbmsg.h> |
| 26 | +#include <nimble/transport.h> |
| 27 | +#include <nimble/transport/hci_ipc.h> |
| 28 | + |
| 29 | +#define BLE_HCI_IPC_ID (0) |
| 30 | + |
| 31 | +static struct hci_ipc_sm g_hci_ipc_sm; |
| 32 | + |
| 33 | +static void ble_hci_trans_rx(const void *data, size_t len, void *user_data); |
| 34 | +static struct ipc_ept_cfg hci_ept_cfg = { |
| 35 | + .name = "nrf_bt_hci", |
| 36 | + .cb = { |
| 37 | + // .bound = hci_ept_bound, |
| 38 | + .received = ble_hci_trans_rx, |
| 39 | + }, |
| 40 | +}; |
| 41 | +static uint8_t hci_ept_local_addr; |
| 42 | + |
| 43 | +static int |
| 44 | +icbmsg_ble_hci_send_mbuf(struct hci_ipc_hdr *hdr, struct os_mbuf *om) |
| 45 | +{ |
| 46 | + int rc; |
| 47 | + struct os_mbuf *x; |
| 48 | + struct ipc_icmsg_buf buf; |
| 49 | + |
| 50 | + rc = ipc_icbmsg_alloc_tx_buf(BLE_HCI_IPC_ID, &buf, |
| 51 | + sizeof(*hdr) + OS_MBUF_PKTHDR(om)->omp_len); |
| 52 | + assert(rc == 0); |
| 53 | + if (rc != 0) { |
| 54 | + return BLE_ERR_MEM_CAPACITY; |
| 55 | + } |
| 56 | + |
| 57 | + memcpy(buf.data, hdr, sizeof(*hdr)); |
| 58 | + buf.len = sizeof(*hdr); |
| 59 | + |
| 60 | + x = om; |
| 61 | + while (x) { |
| 62 | + memcpy(buf.data + buf.len, x->om_data, x->om_len); |
| 63 | + buf.len += x->om_len; |
| 64 | + x = SLIST_NEXT(x, om_next); |
| 65 | + } |
| 66 | + |
| 67 | + rc = ipc_icbmsg_send_buf(BLE_HCI_IPC_ID, hci_ept_local_addr, &buf); |
| 68 | + |
| 69 | + os_mbuf_free_chain(om); |
| 70 | + |
| 71 | + return (rc < 0) ? BLE_ERR_MEM_CAPACITY : 0; |
| 72 | +} |
| 73 | + |
| 74 | +static int |
| 75 | +icbmsg_ble_hci_acl_tx(struct os_mbuf *om) |
| 76 | +{ |
| 77 | + struct hci_ipc_hdr hdr; |
| 78 | + |
| 79 | + hdr.type = HCI_IPC_TYPE_ACL; |
| 80 | + hdr.length = 4 + get_le16(&om->om_data[2]); |
| 81 | + |
| 82 | + return icbmsg_ble_hci_send_mbuf(&hdr, om); |
| 83 | +} |
| 84 | + |
| 85 | +#if !MYNEWT_VAL(BLE_CONTROLLER) |
| 86 | +static int |
| 87 | +icbmsg_ble_hci_iso_tx(struct os_mbuf *om) |
| 88 | +{ |
| 89 | + struct hci_ipc_hdr hdr; |
| 90 | + |
| 91 | + hdr.type = HCI_IPC_TYPE_ISO; |
| 92 | + hdr.length = 4 + get_le16(&om->om_data[2]); |
| 93 | + |
| 94 | + return icbmsg_ble_hci_send_mbuf(&hdr, om); |
| 95 | +} |
| 96 | +#endif |
| 97 | + |
| 98 | +static void |
| 99 | +ble_hci_trans_rx(const void *data, size_t len, void *user_data) |
| 100 | +{ |
| 101 | + // assert(channel == IPC_RX_CHANNEL); |
| 102 | + |
| 103 | + hci_ipc_rx(&g_hci_ipc_sm, data, len); |
| 104 | +} |
| 105 | + |
| 106 | +static void |
| 107 | +icbmsg_ble_hci_init(void) |
| 108 | +{ |
| 109 | + os_sr_t sr; |
| 110 | + |
| 111 | + SYSINIT_ASSERT_ACTIVE(); |
| 112 | + |
| 113 | + OS_ENTER_CRITICAL(sr); |
| 114 | + hci_ept_local_addr = ipc_icmsg_register_ept(BLE_HCI_IPC_ID, &hci_ept_cfg); |
| 115 | + OS_EXIT_CRITICAL(sr); |
| 116 | + |
| 117 | + // OS_ENTER_CRITICAL(sr); |
| 118 | + // ble_hci_trans_rx(IPC_RX_CHANNEL, NULL); |
| 119 | + // OS_EXIT_CRITICAL(sr); |
| 120 | +} |
| 121 | + |
| 122 | +#if MYNEWT_VAL(BLE_CONTROLLER) |
| 123 | +int |
| 124 | +ble_transport_to_hs_evt_impl(void *ev_buf) |
| 125 | +{ |
| 126 | + int rc; |
| 127 | + uint8_t *hci_ev = ev_buf; |
| 128 | + struct ipc_icmsg_buf buf; |
| 129 | + struct hci_ipc_hdr hdr; |
| 130 | + |
| 131 | + hdr.type = ble_transport_ipc_buf_evt_type_get(ev_buf); |
| 132 | + hdr.length = 2 + hci_ev[1]; |
| 133 | + |
| 134 | + rc = ipc_icbmsg_alloc_tx_buf(BLE_HCI_IPC_ID, &buf, |
| 135 | + sizeof(hdr) + hdr.length); |
| 136 | + assert(rc == 0); |
| 137 | + if (rc != 0) { |
| 138 | + return BLE_ERR_MEM_CAPACITY; |
| 139 | + } |
| 140 | + |
| 141 | + memcpy(buf.data, &hdr, sizeof(hdr)); |
| 142 | + buf.len = sizeof(hdr); |
| 143 | + |
| 144 | + memcpy(buf.data + buf.len, hci_ev, hdr.length); |
| 145 | + buf.len += hdr.length; |
| 146 | + |
| 147 | + rc = ipc_icbmsg_send_buf(BLE_HCI_IPC_ID, hci_ept_local_addr, &buf); |
| 148 | + |
| 149 | + ble_transport_ipc_free(ev_buf); |
| 150 | + |
| 151 | + return (rc < 0) ? BLE_ERR_MEM_CAPACITY : 0; |
| 152 | +} |
| 153 | + |
| 154 | +int |
| 155 | +ble_transport_to_hs_acl_impl(struct os_mbuf *om) |
| 156 | +{ |
| 157 | + return icbmsg_ble_hci_acl_tx(om); |
| 158 | +} |
| 159 | + |
| 160 | +void |
| 161 | +ble_transport_hs_init(void) |
| 162 | +{ |
| 163 | + hci_ipc_init(NULL, &g_hci_ipc_sm); |
| 164 | + icbmsg_ble_hci_init(); |
| 165 | +} |
| 166 | +#endif /* BLE_CONTROLLER */ |
| 167 | + |
| 168 | +#if !MYNEWT_VAL(BLE_CONTROLLER) |
| 169 | +int |
| 170 | +ble_transport_to_ll_cmd_impl(void *ev_buf) |
| 171 | +{ |
| 172 | + int rc; |
| 173 | + uint8_t *cmd = ev_buf; |
| 174 | + struct ipc_icmsg_buf buf; |
| 175 | + struct hci_ipc_hdr hdr; |
| 176 | + |
| 177 | + hdr.type = HCI_IPC_TYPE_CMD; |
| 178 | + hdr.length = 3 + cmd[2]; |
| 179 | + |
| 180 | + rc = ipc_icbmsg_alloc_tx_buf(BLE_HCI_IPC_ID, &buf, |
| 181 | + sizeof(hdr) + hdr.length); |
| 182 | + assert(rc == 0); |
| 183 | + if (rc != 0) { |
| 184 | + return BLE_ERR_MEM_CAPACITY; |
| 185 | + } |
| 186 | + |
| 187 | + memcpy(buf.data, &hdr, sizeof(hdr)); |
| 188 | + buf.len = sizeof(hdr); |
| 189 | + |
| 190 | + memcpy(buf.data + buf.len, cmd, hdr.length); |
| 191 | + buf.len += hdr.length; |
| 192 | + |
| 193 | + ble_transport_ipc_free(ev_buf); |
| 194 | + |
| 195 | + return (rc < 0) ? BLE_ERR_MEM_CAPACITY : 0; |
| 196 | +} |
| 197 | + |
| 198 | +int |
| 199 | +ble_transport_to_ll_acl_impl(struct os_mbuf *om) |
| 200 | +{ |
| 201 | + return icbmsg_ble_hci_acl_tx(om); |
| 202 | +} |
| 203 | + |
| 204 | +int |
| 205 | +ble_transport_to_ll_iso_impl(struct os_mbuf *om) |
| 206 | +{ |
| 207 | + return icbmsg_ble_hci_iso_tx(om); |
| 208 | +} |
| 209 | + |
| 210 | +void |
| 211 | +ble_transport_ll_init(void) |
| 212 | +{ |
| 213 | + hci_ipc_init(NULL, &g_hci_ipc_sm); |
| 214 | + icbmsg_ble_hci_init(); |
| 215 | +} |
| 216 | +#endif /* !BLE_CONTROLLER */ |
0 commit comments