Skip to content

Commit b7438b4

Browse files
committed
zephyr raw hci porting
1 parent 9263b3a commit b7438b4

File tree

4 files changed

+204
-1
lines changed

4 files changed

+204
-1
lines changed

src/utility/HCIUartTransport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) && !defined(ARDUINO_SILABS) && !defined(ARDUINO_UNOR4_WIFI) || defined(TARGET_NANO_RP2040_CONNECT) //|| defined(CORE_CM4)
20+
#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) && !defined(ARDUINO_SILABS) && !defined(ARDUINO_UNOR4_WIFI) && !defined(__ZEPHYR__) || defined(TARGET_NANO_RP2040_CONNECT) //|| defined(CORE_CM4)
2121

2222
#include "HCIUartTransport.h"
2323

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#if defined(__ZEPHYR__)
21+
22+
#include "HCIVirtualTransportZephyr.h"
23+
24+
extern "C" struct k_fifo* __rx_queue;
25+
26+
HCIVirtualTransportZephyrClass::HCIVirtualTransportZephyrClass()
27+
{
28+
}
29+
30+
HCIVirtualTransportZephyrClass::~HCIVirtualTransportZephyrClass()
31+
{
32+
}
33+
34+
extern "C" void set_public_address(struct k_fifo *rx_queue);
35+
int HCIVirtualTransportZephyrClass::begin()
36+
{
37+
bt_enable_raw(__rx_queue);
38+
//bt_hci_raw_set_mode(BT_HCI_RAW_MODE_PASSTHROUGH);
39+
set_public_address(__rx_queue);
40+
bt_hci_raw_set_mode(BT_HCI_RAW_MODE_H4);
41+
rxbuf.clear();
42+
return 1;
43+
}
44+
45+
void HCIVirtualTransportZephyrClass::end()
46+
{
47+
}
48+
49+
void HCIVirtualTransportZephyrClass::wait(unsigned long timeout)
50+
{
51+
delay(timeout);
52+
}
53+
54+
int HCIVirtualTransportZephyrClass::available()
55+
{
56+
if (rxbuf.available()) {
57+
return rxbuf.available();
58+
}
59+
60+
static struct net_buf *buf;
61+
buf = net_buf_get(__rx_queue, K_MSEC(10));
62+
if (!buf) {
63+
return 0;
64+
}
65+
66+
for (int i = 0; i < buf->len; i++) {
67+
rxbuf.store_char((uint8_t)buf->data[i]);
68+
}
69+
net_buf_pull(buf, buf->len);
70+
if (!buf->len) {
71+
net_buf_unref(buf);
72+
buf = NULL;
73+
}
74+
75+
return rxbuf.available();
76+
}
77+
78+
// never called
79+
int HCIVirtualTransportZephyrClass::peek()
80+
{
81+
return -1;
82+
}
83+
84+
int HCIVirtualTransportZephyrClass::read()
85+
{
86+
87+
if (rxbuf.available()) {
88+
return rxbuf.read_char();
89+
}
90+
91+
return -1;
92+
}
93+
94+
size_t HCIVirtualTransportZephyrClass::write(const uint8_t* data, size_t length)
95+
{
96+
struct net_buf *__net_buf = bt_buf_get_tx(BT_BUF_H4, K_MSEC(10), data, length);
97+
if (__net_buf) {
98+
auto err = bt_send(__net_buf);
99+
if (err) {
100+
net_buf_unref(__net_buf);
101+
}
102+
return length;
103+
}
104+
return 0;
105+
}
106+
107+
HCIVirtualTransportZephyrClass HCIVirtualTransportZephyr;
108+
109+
HCITransportInterface& HCITransport = HCIVirtualTransportZephyr;
110+
111+
#endif
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "HCITransport.h"
21+
#include "api/RingBuffer.h"
22+
#include <stdint.h>
23+
#include <stdio.h>
24+
#include <string.h>
25+
26+
extern "C" {
27+
#include <zephyr/bluetooth/hci_raw.h>
28+
#include <zephyr/bluetooth/buf.h>
29+
}
30+
31+
class HCIVirtualTransportZephyrClass : public HCITransportInterface {
32+
public:
33+
HCIVirtualTransportZephyrClass();
34+
virtual ~HCIVirtualTransportZephyrClass();
35+
36+
virtual int begin();
37+
virtual void end();
38+
39+
virtual void wait(unsigned long timeout);
40+
41+
virtual int available();
42+
virtual int peek();
43+
virtual int read();
44+
45+
virtual size_t write(const uint8_t* data, size_t length);
46+
47+
private:
48+
RingBufferN<258> rxbuf;
49+
};

src/utility/HCIVirtualZephyrMacros.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#if defined(__ZEPHYR__)
2+
3+
#include <zephyr/bluetooth/hci.h>
4+
#include <zephyr/bluetooth/buf.h>
5+
#include <zephyr/bluetooth/hci_vs.h>
6+
7+
static K_FIFO_DEFINE(rx_queue);
8+
struct k_fifo* __rx_queue = &rx_queue;
9+
10+
11+
// from https://github.com/thomasstenersen/sdk-zephyr/commit/df546a0eb9b96b453373f38483959d2363a83cae#diff-217778ef1d0638c89207786f06425396891f987713120e83db724d26b9813eebR331
12+
// referenced by https://devzone.nordicsemi.com/f/nordic-q-a/73310/incompatibility-between-mcumgr-cli-and-hci_usb-when-running-with-bt_ll_softdevice-enabled
13+
14+
void set_public_address(struct k_fifo *rx_queue)
15+
{
16+
const struct bt_hci_cp_vs_write_bd_addr cmd = {
17+
.bdaddr.val = {0xFA, 0xFA, 0xFA, 0xFA, 0xFA, 0xFA}
18+
};
19+
const struct bt_hci_cmd_hdr cmd_header = {
20+
.opcode = BT_HCI_OP_VS_WRITE_BD_ADDR,
21+
.param_len = sizeof(cmd)
22+
};
23+
struct net_buf *buf;
24+
int err;
25+
buf = bt_buf_get_tx(BT_BUF_CMD, K_NO_WAIT,
26+
&cmd_header, sizeof(cmd_header));
27+
net_buf_add_mem(buf, &cmd, sizeof(cmd));
28+
err = bt_send(buf);
29+
__ASSERT_NO_MSG(err == 0);
30+
/* Pull out the command complete. */
31+
buf = net_buf_get(rx_queue, K_SECONDS(10)); /* 10s == HCI_CMD_TIMEOUT */
32+
struct bt_hci_evt_hdr *hdr;
33+
__ASSERT_NO_MSG(buf != NULL);
34+
__ASSERT_NO_MSG(buf->len >= sizeof(*hdr));
35+
hdr = net_buf_pull_mem(buf, sizeof(*hdr));
36+
__ASSERT_NO_MSG(hdr->evt == BT_HCI_EVT_CMD_COMPLETE);
37+
struct bt_hci_evt_cmd_complete *evt;
38+
evt = net_buf_pull_mem(buf, sizeof(*evt));
39+
__ASSERT_NO_MSG(sys_le16_to_cpu(evt->opcode) == BT_HCI_OP_VS_WRITE_BD_ADDR);
40+
net_buf_unref(buf);
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)