Skip to content

Commit

Permalink
fix(hci): fixing issue with l2 connections queue and error handling (#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stoprocent authored Oct 1, 2024
1 parent 67869a7 commit fd7b168
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 165 deletions.
15 changes: 15 additions & 0 deletions FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
polar: # Replace with a single Polar username
buy_me_a_coffee: stoprocent # Replace with a single Buy Me a Coffee username
thanks_dev: # Replace with a single thanks.dev username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,34 @@ https://img.shields.io/npm/v/@stoprocent/bluetooth-hci-socket.svg
)](
https://www.npmjs.com/package/@stoprocent/bluetooth-hci-socket
)
[![Fediverse](
https://img.shields.io/mastodon/follow/279303?domain=https%3A%2F%2Fmastodon.social&style=social#rzr
)](
https://mastodon.social/@rzr/103886495590566533#bluetooth-hci-socket
)
[![IRC Channel](
https://img.shields.io/badge/chat-on%20libera-brightgreen.svg
)](
https://kiwiirc.com/client/irc.libera.chat/#iot
)

Bluetooth HCI socket binding for Node.js

__NOTE:__ Currently only supports __Linux__, __FreeBSD__, __Windows__ or any operating systems when using HCI over uart.
__NOTE:__ Currently only supports __Linux__, __FreeBSD__, __Windows__ or **any operating systems when using HCI over uart**.

## About This Fork

This fork of `node-bluetooth-hci-socket` exists to introduce several important improvements and address compatibility issues across different operating systems.

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.

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.

If you value these contributions and the ongoing maintenance of this project, please consider supporting my work.

[![Buy me a coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/stoprocent)

## Install

```sh
npm install @stoprocent/bluetooth-hci-socket
```

## Usage

```javascript
var BluetoothHciSocket = require('@stoprocent/bluetooth-hci-socket');
```

## Prerequisites

Expand Down Expand Up @@ -85,18 +99,6 @@ Note:
| BCM2045A0 Bluetooth 4.1 | 0x0a5c | 0x6412 |
| Marvell AVASTAR | 0x1286 | 0x204C |

## Install

```sh
npm install @stoprocent/bluetooth-hci-socket
```

## Usage

```javascript
var BluetoothHciSocket = require('@stoprocent/bluetooth-hci-socket');
```

### Actions

#### Create
Expand Down
2 changes: 1 addition & 1 deletion examples/le-connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ function disconnectConnection (handle, reason) {
bluetoothHciSocket.write(cmd);
}

createConnection('dc:0b:86:95:e8:a5', 'random');
createConnection('fc:d1:75:16:7b:fc', 'random');
3 changes: 2 additions & 1 deletion include/BluetoothHciSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class BluetoothHciSocket : public Napi::ObjectWrap<BluetoothHciSocket> {
* @param data Pointer to the data buffer.
* @return Result code.
*/
int kernelDisconnectWorkArounds(int length, char* data);
void kernelDisconnectWorkArounds(char* data, int length);

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

// Maps to manage connected and connecting L2CAP sockets
std::mutex _mapMutex;
std::map<bdaddr_t, std::weak_ptr<BluetoothHciL2Socket>> _l2sockets_connected; ///< Connected L2CAP sockets
std::map<bdaddr_t, std::shared_ptr<BluetoothHciL2Socket>> _l2sockets_connecting; ///< Connecting L2CAP sockets
std::map<uint16_t, std::shared_ptr<BluetoothHciL2Socket>> _l2sockets_handles; ///< L2CAP sockets by handle
Expand Down
19 changes: 19 additions & 0 deletions include/BluetoothStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@
#define HCI_DEV_NONE 0xFFFF ///< No HCI device
#define HCI_MAX_DEV 16 ///< Maximum number of HCI devices

// HCI Event Packet Type
#define HCI_EVENT_PKT 0x04

// HCI Event Codes
#define HCI_EV_LE_META 0x3E
#define HCI_EV_DISCONN_COMPLETE 0x05

// HCI LE Meta Event Subevent Codes
#define HCI_EV_LE_CONN_COMPLETE 0x01
#define HCI_EV_LE_ENH_CONN_COMPLETE 0x0A

// Status Codes
#define HCI_SUCCESS 0x00

//
#define HCI_COMMAND_PKT 0x01
#define HCI_LE_CREATE_CONN 0x200D
#define HCI_LE_EXT_CREATE_CONN 0x2043

// L2CAP constants
#define ATT_CID 4 ///< Attribute Protocol CID (Channel Identifier)

Expand Down
15 changes: 6 additions & 9 deletions src/BluetoothHciL2Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ BluetoothHciL2Socket::BluetoothHciL2Socket(BluetoothHciSocket* parent,
const bdaddr_t* bdaddr_dst,
uint8_t dst_type,
uint64_t expires)
: _parent(parent), _expires(expires), _socket(-1)
: _socket(-1), _parent(parent), _expires(expires)
{
uint16_t l2cid;

Expand All @@ -40,9 +40,6 @@ BluetoothHciL2Socket::BluetoothHciL2Socket(BluetoothHciSocket* parent,
_l2_dst.l2_cid = l2cid;
memcpy(&_l2_dst.l2_bdaddr, bdaddr_dst, sizeof(bdaddr_t));
_l2_dst.l2_bdaddr_type = dst_type; // BDADDR_LE_PUBLIC (0x01), BDADDR_LE_RANDOM (0x02)

// Attempt to connect
this->connect();
}

BluetoothHciL2Socket::~BluetoothHciL2Socket() {
Expand All @@ -53,12 +50,12 @@ BluetoothHciL2Socket::~BluetoothHciL2Socket() {
}

void BluetoothHciL2Socket::connect() {
_socket = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if(_socket < 0) return;
this->_socket = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if(this->_socket < 0) return;

if (bind(_socket, (struct sockaddr*)&_l2_src, sizeof(_l2_src)) < 0) {
close(_socket);
_socket = -1;
if (bind(this->_socket, (struct sockaddr*)&_l2_src, sizeof(_l2_src)) < 0) {
close(this->_socket);
this->_socket = -1;
return;
}

Expand Down
Loading

0 comments on commit fd7b168

Please sign in to comment.