Skip to content

Commit fd7b168

Browse files
authored
fix(hci): fixing issue with l2 connections queue and error handling (#5)
* fix(hci): fixing issue with l2 connections queue * fix(example): cleanup warnings * fix(hci): fix error handling * chore(funding): added sponsor button * chore(docs): update readme * chore(docs): readme update
1 parent 67869a7 commit fd7b168

7 files changed

+289
-165
lines changed

FUNDING.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# These are supported funding model platforms
2+
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
12+
polar: # Replace with a single Polar username
13+
buy_me_a_coffee: stoprocent # Replace with a single Buy Me a Coffee username
14+
thanks_dev: # Replace with a single thanks.dev username
15+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

README.md

+25-23
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,34 @@ https://img.shields.io/npm/v/@stoprocent/bluetooth-hci-socket.svg
1313
)](
1414
https://www.npmjs.com/package/@stoprocent/bluetooth-hci-socket
1515
)
16-
[![Fediverse](
17-
https://img.shields.io/mastodon/follow/279303?domain=https%3A%2F%2Fmastodon.social&style=social#rzr
18-
)](
19-
https://mastodon.social/@rzr/103886495590566533#bluetooth-hci-socket
20-
)
21-
[![IRC Channel](
22-
https://img.shields.io/badge/chat-on%20libera-brightgreen.svg
23-
)](
24-
https://kiwiirc.com/client/irc.libera.chat/#iot
25-
)
2616

2717
Bluetooth HCI socket binding for Node.js
2818

29-
__NOTE:__ Currently only supports __Linux__, __FreeBSD__, __Windows__ or any operating systems when using HCI over uart.
19+
__NOTE:__ Currently only supports __Linux__, __FreeBSD__, __Windows__ or **any operating systems when using HCI over uart**.
20+
21+
## About This Fork
22+
23+
This fork of `node-bluetooth-hci-socket` exists to introduce several important improvements and address compatibility issues across different operating systems.
24+
25+
1. **System-independent UART HCI driver**: I have introduced a driver that allows UART HCI dongles to be used seamlessly across any operating system, making the library much more flexible and portable.
26+
27+
2. **Rewriting Native Code**: The C++ native binding code has been fully rewritten from **Nan** to **N-API**, ensuring long-term compatibility with modern versions of Node.js. Along with this, I have also resolved numerous issues that plagued the original code, improving both stability and performance.
28+
29+
If you value these contributions and the ongoing maintenance of this project, please consider supporting my work.
30+
31+
[![Buy me a coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/stoprocent)
32+
33+
## Install
34+
35+
```sh
36+
npm install @stoprocent/bluetooth-hci-socket
37+
```
38+
39+
## Usage
40+
41+
```javascript
42+
var BluetoothHciSocket = require('@stoprocent/bluetooth-hci-socket');
43+
```
3044

3145
## Prerequisites
3246

@@ -85,18 +99,6 @@ Note:
8599
| BCM2045A0 Bluetooth 4.1 | 0x0a5c | 0x6412 |
86100
| Marvell AVASTAR | 0x1286 | 0x204C |
87101

88-
## Install
89-
90-
```sh
91-
npm install @stoprocent/bluetooth-hci-socket
92-
```
93-
94-
## Usage
95-
96-
```javascript
97-
var BluetoothHciSocket = require('@stoprocent/bluetooth-hci-socket');
98-
```
99-
100102
### Actions
101103

102104
#### Create

examples/le-connection-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,4 @@ function disconnectConnection (handle, reason) {
189189
bluetoothHciSocket.write(cmd);
190190
}
191191

192-
createConnection('dc:0b:86:95:e8:a5', 'random');
192+
createConnection('fc:d1:75:16:7b:fc', 'random');

include/BluetoothHciSocket.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class BluetoothHciSocket : public Napi::ObjectWrap<BluetoothHciSocket> {
132132
* @param data Pointer to the data buffer.
133133
* @return Result code.
134134
*/
135-
int kernelDisconnectWorkArounds(int length, char* data);
135+
void kernelDisconnectWorkArounds(char* data, int length);
136136

137137
/**
138138
* @brief Workaround for kernel connect issues.
@@ -168,6 +168,7 @@ class BluetoothHciSocket : public Napi::ObjectWrap<BluetoothHciSocket> {
168168
uint8_t _addressType; ///< Address type (public or random)
169169

170170
// Maps to manage connected and connecting L2CAP sockets
171+
std::mutex _mapMutex;
171172
std::map<bdaddr_t, std::weak_ptr<BluetoothHciL2Socket>> _l2sockets_connected; ///< Connected L2CAP sockets
172173
std::map<bdaddr_t, std::shared_ptr<BluetoothHciL2Socket>> _l2sockets_connecting; ///< Connecting L2CAP sockets
173174
std::map<uint16_t, std::shared_ptr<BluetoothHciL2Socket>> _l2sockets_handles; ///< L2CAP sockets by handle

include/BluetoothStructs.h

+19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@
2929
#define HCI_DEV_NONE 0xFFFF ///< No HCI device
3030
#define HCI_MAX_DEV 16 ///< Maximum number of HCI devices
3131

32+
// HCI Event Packet Type
33+
#define HCI_EVENT_PKT 0x04
34+
35+
// HCI Event Codes
36+
#define HCI_EV_LE_META 0x3E
37+
#define HCI_EV_DISCONN_COMPLETE 0x05
38+
39+
// HCI LE Meta Event Subevent Codes
40+
#define HCI_EV_LE_CONN_COMPLETE 0x01
41+
#define HCI_EV_LE_ENH_CONN_COMPLETE 0x0A
42+
43+
// Status Codes
44+
#define HCI_SUCCESS 0x00
45+
46+
//
47+
#define HCI_COMMAND_PKT 0x01
48+
#define HCI_LE_CREATE_CONN 0x200D
49+
#define HCI_LE_EXT_CREATE_CONN 0x2043
50+
3251
// L2CAP constants
3352
#define ATT_CID 4 ///< Attribute Protocol CID (Channel Identifier)
3453

src/BluetoothHciL2Socket.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ BluetoothHciL2Socket::BluetoothHciL2Socket(BluetoothHciSocket* parent,
1414
const bdaddr_t* bdaddr_dst,
1515
uint8_t dst_type,
1616
uint64_t expires)
17-
: _parent(parent), _expires(expires), _socket(-1)
17+
: _socket(-1), _parent(parent), _expires(expires)
1818
{
1919
uint16_t l2cid;
2020

@@ -40,9 +40,6 @@ BluetoothHciL2Socket::BluetoothHciL2Socket(BluetoothHciSocket* parent,
4040
_l2_dst.l2_cid = l2cid;
4141
memcpy(&_l2_dst.l2_bdaddr, bdaddr_dst, sizeof(bdaddr_t));
4242
_l2_dst.l2_bdaddr_type = dst_type; // BDADDR_LE_PUBLIC (0x01), BDADDR_LE_RANDOM (0x02)
43-
44-
// Attempt to connect
45-
this->connect();
4643
}
4744

4845
BluetoothHciL2Socket::~BluetoothHciL2Socket() {
@@ -53,12 +50,12 @@ BluetoothHciL2Socket::~BluetoothHciL2Socket() {
5350
}
5451

5552
void BluetoothHciL2Socket::connect() {
56-
_socket = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
57-
if(_socket < 0) return;
53+
this->_socket = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
54+
if(this->_socket < 0) return;
5855

59-
if (bind(_socket, (struct sockaddr*)&_l2_src, sizeof(_l2_src)) < 0) {
60-
close(_socket);
61-
_socket = -1;
56+
if (bind(this->_socket, (struct sockaddr*)&_l2_src, sizeof(_l2_src)) < 0) {
57+
close(this->_socket);
58+
this->_socket = -1;
6259
return;
6360
}
6461

0 commit comments

Comments
 (0)