Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | /* * Copyright (c) 2019 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <init.h> #include <sys/byteorder.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <drivers/bluetooth/hci_driver.h> #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER) #define LOG_MODULE_NAME bt_hci_driver #include "common/log.h" #define RPMSG_CMD 0x01 #define RPMSG_ACL 0x02 #define RPMSG_SCO 0x03 #define RPMSG_EVT 0x04 int bt_rpmsg_platform_init(void); int bt_rpmsg_platform_send(struct net_buf *buf); static bool is_hci_event_discardable(const u8_t *evt_data) { u8_t evt_type = evt_data[0]; switch (evt_type) { #if defined(CONFIG_BT_BREDR) case BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI: case BT_HCI_EVT_EXTENDED_INQUIRY_RESULT: return true; #endif case BT_HCI_EVT_LE_META_EVENT: { u8_t subevt_type = evt_data[sizeof(struct bt_hci_evt_hdr)]; switch (subevt_type) { case BT_HCI_EVT_LE_ADVERTISING_REPORT: return true; default: return false; } } default: return false; } } static struct net_buf *bt_rpmsg_evt_recv(u8_t *data, size_t remaining, bool *prio) { bool discardable; struct bt_hci_evt_hdr hdr; struct net_buf *buf; if (remaining < sizeof(hdr)) { BT_ERR("Not enough data for event header"); return NULL; } discardable = is_hci_event_discardable(data); memcpy((void *)&hdr, data, sizeof(hdr)); data += sizeof(hdr); remaining -= sizeof(hdr); if (remaining != hdr.len) { BT_ERR("Event payload length is not correct"); return NULL; } BT_DBG("len %u", hdr.len); buf = bt_buf_get_evt(hdr.evt, discardable, K_NO_WAIT); if (!buf) { if (discardable) { BT_DBG("Discardable buffer pool full, ignoring event"); } else { BT_ERR("No available event buffers!"); } return buf; } net_buf_add_mem(buf, &hdr, sizeof(hdr)); *prio = bt_hci_evt_is_prio(hdr.evt); net_buf_add_mem(buf, data, remaining); return buf; } static struct net_buf *bt_rpmsg_acl_recv(u8_t *data, size_t remaining) { struct bt_hci_acl_hdr hdr; struct net_buf *buf; if (remaining < sizeof(hdr)) { BT_ERR("Not enough data for ACL header"); return NULL; } buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT); if (buf) { memcpy((void *)&hdr, data, sizeof(hdr)); data += sizeof(hdr); remaining -= sizeof(hdr); net_buf_add_mem(buf, &hdr, sizeof(hdr)); } else { BT_ERR("No available ACL buffers!"); return NULL; } if (remaining != sys_le16_to_cpu(hdr.len)) { BT_ERR("ACL payload length is not correct"); net_buf_unref(buf); return NULL; } BT_DBG("len %u", remaining); net_buf_add_mem(buf, data, remaining); return buf; } void bt_rpmsg_rx(u8_t *data, size_t len) { u8_t pkt_indicator; bool prio = false; struct net_buf *buf = NULL; size_t remaining = len; BT_HEXDUMP_DBG(data, len, "RPMsg data:"); pkt_indicator = *data++; remaining -= sizeof(pkt_indicator); switch (pkt_indicator) { case RPMSG_EVT: buf = bt_rpmsg_evt_recv(data, remaining, &prio); break; case RPMSG_ACL: buf = bt_rpmsg_acl_recv(data, remaining); break; default: BT_ERR("Unknown HCI type %u", pkt_indicator); return; } if (buf) { BT_DBG("Calling bt_recv(%p)", buf); if (prio) { bt_recv_prio(buf); } else { bt_recv(buf); } BT_HEXDUMP_DBG(buf->data, buf->len, "RX buf payload:"); } } static int bt_rpmsg_send(struct net_buf *buf) { int err; u8_t pkt_indicator; BT_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len); switch (bt_buf_get_type(buf)) { case BT_BUF_ACL_OUT: pkt_indicator = RPMSG_ACL; break; case BT_BUF_CMD: pkt_indicator = RPMSG_CMD; break; default: BT_ERR("Unknown type %u", bt_buf_get_type(buf)); goto done; } net_buf_push_u8(buf, pkt_indicator); BT_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:"); err = bt_rpmsg_platform_send(buf); if (err < 0) { BT_ERR("Failed to send (err %d)", err); } done: net_buf_unref(buf); return 0; } static int bt_rpmsg_open(void) { BT_DBG(""); return bt_rpmsg_platform_init(); } static const struct bt_hci_driver drv = { .name = "RPMsg", .open = bt_rpmsg_open, .send = bt_rpmsg_send, .bus = BT_HCI_DRIVER_BUS_IPM, }; static int bt_rpmsg_init(struct device *unused) { ARG_UNUSED(unused); return bt_hci_driver_register(&drv); } SYS_INIT(bt_rpmsg_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); |