Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional NimBLE library #28

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/codeql/codeql-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
paths:
- firmware
- include
paths-ignore:
- .pio/libdeps
5 changes: 5 additions & 0 deletions .github/workflows/build-bhaptics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ jobs:
serial_plotter_flag:
- SERIAL_PLOTTER=false
- SERIAL_PLOTTER=true
nimble_flag:
- BLUETOOTH_USE_NIMBLE=true
- BLUETOOTH_USE_NIMBLE=false

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -128,12 +131,14 @@ jobs:
run: |
sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini
sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini
sed -i '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini

- name: Update build flags (macOS)
if: runner.os == 'macOS'
run: |
sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.battery_flag }}/' platformio.ini
sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.serial_plotter_flag }}/' platformio.ini
sed -i '' '/__OH_FIRMWARE__/p; s/__OH_FIRMWARE__/${{ matrix.nimble_flag }}/' platformio.ini

- name: Install PlatformIO
run: |
Expand Down
12 changes: 1 addition & 11 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
Expand Down Expand Up @@ -58,6 +47,7 @@ jobs:
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
config-file: ./.github/codeql/codeql-config.yml
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
Expand Down
24 changes: 14 additions & 10 deletions include/config/bluetooth.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#pragma once

#ifndef BLUETOOTH_NAME
#define BLUETOOTH_NAME "OpenHaptics"
#endif

#ifndef BLUETOOTH_ADDRESS
#define BLUETOOTH_ADDRESS \
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB }
#endif
#pragma once

#ifndef BLUETOOTH_NAME
#define BLUETOOTH_NAME "OpenHaptics"
#endif

#ifndef BLUETOOTH_ADDRESS
#define BLUETOOTH_ADDRESS \
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB }
#endif

#ifndef BLUETOOTH_USE_NIMBLE
#define BLUETOOTH_USE_NIMBLE false
#endif
110 changes: 60 additions & 50 deletions lib/bh/connection_bhble.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
#include <connection_bhble.hpp>
#include "connection_bhble.hpp"

#include <output.hpp>
#include <events.hpp>

#include <Arduino.h>
#include <BLE2902.h>
#include <HardwareSerial.h>

#if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true
// BLE2902 not needed: https://github.com/h2zero/NimBLE-Arduino/blob/release/1.4/docs/Migration_guide.md#descriptors

#define PROPERTY_READ NIMBLE_PROPERTY::READ
#define PROPERTY_WRITE NIMBLE_PROPERTY::WRITE
#define PROPERTY_WRITE_NR NIMBLE_PROPERTY::WRITE_NR
#define PROPERTY_BROADCAST NIMBLE_PROPERTY::BROADCAST
#define PROPERTY_NOTIFY NIMBLE_PROPERTY::NOTIFY
#define PROPERTY_INDICATE NIMBLE_PROPERTY::INDICATE
#else
#include <BLE2902.h>

#define PROPERTY_READ BLECharacteristic::PROPERTY_READ
#define PROPERTY_WRITE BLECharacteristic::PROPERTY_WRITE
#define PROPERTY_WRITE_NR BLECharacteristic::PROPERTY_WRITE_NR
#define PROPERTY_BROADCAST BLECharacteristic::PROPERTY_BROADCAST
#define PROPERTY_NOTIFY BLECharacteristic::PROPERTY_NOTIFY
#define PROPERTY_INDICATE BLECharacteristic::PROPERTY_INDICATE
#endif

class BHServerCallbacks final : public BLEServerCallbacks {
public:
BHServerCallbacks() {}
Expand Down Expand Up @@ -41,9 +60,12 @@ class SerialOutputCharCallbacks : public BLECharacteristicCallbacks {
pCharacteristic->getValue().length());
};

void onStatus(BLECharacteristic* pCharacteristic,
Status s,
uint32_t code) override {
#if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true
void onStatus(BLECharacteristic* pCharacteristic, Status s, int code) override
#else
void onStatus(BLECharacteristic* pCharacteristic, Status s, uint32_t code) override
#endif
{
Serial.printf(">>\tonStatus (UUID: %s) \n",
pCharacteristic->getUUID().toString().c_str());
Serial.printf("\tstatus: %d, code: %u \n", s, code);
Expand All @@ -58,11 +80,10 @@ class MotorCharCallbacks : public BLECharacteristicCallbacks {
bh_motor_handler_t motorTransformer;

public:
MotorCharCallbacks(bh_motor_handler_t motorTransformer)
: motorTransformer(motorTransformer) {}
MotorCharCallbacks(bh_motor_handler_t motorTransformer) : motorTransformer(motorTransformer) {}

void onWrite(BLECharacteristic* pCharacteristic) override {
auto value = pCharacteristic->getValue();
std::string value = pCharacteristic->getValue();

this->motorTransformer(value);
};
Expand Down Expand Up @@ -100,63 +121,67 @@ void BH::ConnectionBHBLE::begin() {

// Each characteristic needs 2 handles and descriptor 1 handle.
this->motorService =
this->bleServer->createService(BH_BLE_SERVICE_MOTOR_UUID, 25);
this->bleServer->createService(BH_BLE_SERVICE_MOTOR_UUID);

{
MotorCharCallbacks* motorCallbacks =
new MotorCharCallbacks(this->motorHandler);
MotorCharCallbacks* motorCallbacks = new MotorCharCallbacks(this->motorHandler);

auto* motorChar = this->motorService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_MOTOR_UUID,
BLECharacteristic::PROPERTY_WRITE_NR);
PROPERTY_WRITE_NR
);
motorChar->setCallbacks(motorCallbacks);

auto* motorCharStable = this->motorService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_MOTOR_STABLE_UUID,
BLECharacteristic::PROPERTY_WRITE);
PROPERTY_WRITE
);
motorCharStable->setCallbacks(motorCallbacks);
}

{
auto* configChar = this->motorService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_CONFIG_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
PROPERTY_READ | PROPERTY_WRITE
);
configChar->setCallbacks(new ConfigCharCallbacks());
}

{
auto* serialNumberChar = this->motorService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_SERIAL_KEY_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
PROPERTY_READ | PROPERTY_WRITE
);
serialNumberChar->setValue(this->config.serialNumber, BH_SERIAL_NUMBER_LENGTH);
serialNumberChar->setCallbacks(new SerialOutputCharCallbacks());
}

{
this->batteryChar = this->motorService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_BATTERY_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::
PROPERTY_WRITE_NR // for whatever reason, it have to be
// writable, otherwise Desktop app crashes
PROPERTY_READ | PROPERTY_WRITE_NR // for whatever reason, it have to bewritable, otherwise Desktop app crashes
);

#if !defined(BLUETOOTH_USE_NIMBLE) || BLUETOOTH_USE_NIMBLE != true
batteryChar->addDescriptor(new BLE2902());
#endif

// original bHaptics Player require non-null value for battery level, otherwise it crashes
#if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true
uint16_t defaultLevel = 0;
uint16_t defaultLevel = 0;
#else
uint16_t defaultLevel = 100;
uint16_t defaultLevel = 100;
#endif

this->batteryChar->setValue(defaultLevel);
// this->batteryChar->notify();
this->batteryChar->setValue(defaultLevel);
// this->batteryChar->notify();
}

{
auto* versionChar = this->motorService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_VERSION_UUID,
BLECharacteristic::PROPERTY_READ);
PROPERTY_READ
);
versionChar->setCallbacks(new SerialOutputCharCallbacks());
uint16_t firmwareVersion = BH_FIRMWARE_VERSION;
versionChar->setValue(firmwareVersion);
Expand All @@ -165,54 +190,39 @@ void BH::ConnectionBHBLE::begin() {
{
auto* monitorChar = this->motorService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_TACTSUIT_MONITOR_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_BROADCAST |
BLECharacteristic::PROPERTY_INDICATE |
BLECharacteristic::PROPERTY_WRITE_NR);
PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR
);
monitorChar->setCallbacks(new SerialOutputCharCallbacks());

#if !defined(BLUETOOTH_USE_NIMBLE) || BLUETOOTH_USE_NIMBLE != true
monitorChar->addDescriptor(new BLE2902());
#endif

uint16_t audioCableState = NO_AUDIO_CABLE;
monitorChar->setValue(audioCableState);
}

// auto* athGlobalChar = this->motorService->createCharacteristic(
// BH_BLE_SERVICE_MOTOR_CHAR_ATH_GLOBAL_CONF_UUID,
// BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE |
// BLECharacteristic::PROPERTY_NOTIFY |
// BLECharacteristic::PROPERTY_BROADCAST|
// BLECharacteristic::PROPERTY_INDICATE|
// BLECharacteristic::PROPERTY_WRITE_NR
// PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR
// );
// athGlobalChar->setCallbacks(new SerialOutputCharCallbacks());

// auto* athThemeChar = this->motorService->createCharacteristic(
// BH_BLE_SERVICE_MOTOR_CHAR_ATH_THEME_UUID,
// BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE |
// BLECharacteristic::PROPERTY_NOTIFY |
// BLECharacteristic::PROPERTY_BROADCAST|
// BLECharacteristic::PROPERTY_INDICATE|
// BLECharacteristic::PROPERTY_WRITE_NR
// PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR
// );
// athThemeChar->setCallbacks(new SerialOutputCharCallbacks());

// auto* motorMappingChar = this->motorService->createCharacteristic(
// BH_BLE_SERVICE_MOTOR_CHAR_MOTTOR_MAPPING_UUID,
// BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE |
// BLECharacteristic::PROPERTY_NOTIFY |
// BLECharacteristic::PROPERTY_BROADCAST|
// BLECharacteristic::PROPERTY_INDICATE|
// BLECharacteristic::PROPERTY_WRITE_NR
// PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR
// );
// motorMappingChar->setCallbacks(new SerialOutputCharCallbacks());

// auto* signatureMappingChar = this->motorService->createCharacteristic(
// BH_BLE_SERVICE_MOTOR_CHAR_SIGNATURE_PATTERN_UUID,
// BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE |
// BLECharacteristic::PROPERTY_NOTIFY |
// BLECharacteristic::PROPERTY_BROADCAST|
// BLECharacteristic::PROPERTY_INDICATE|
// BLECharacteristic::PROPERTY_WRITE_NR
// PROPERTY_READ | PROPERTY_WRITE | PROPERTY_NOTIFY | PROPERTY_BROADCAST | PROPERTY_INDICATE | PROPERTY_WRITE_NR
// );
// signatureMappingChar->setCallbacks(new SerialOutputCharCallbacks());

Expand All @@ -223,7 +233,7 @@ void BH::ConnectionBHBLE::begin() {

auto* dfuControlChar = dfuService->createCharacteristic(
BH_BLE_SERVICE_MOTOR_CHAR_SIGNATURE_PATTERN_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
PROPERTY_READ | PROPERTY_WRITE);
dfuService->start();
}

Expand Down
8 changes: 6 additions & 2 deletions lib/bh/connection_bhble.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
#include <bh_types.hpp>

#include <Arduino.h>
#include <BLEDevice.h>
#include <esp_wifi.h>

#if defined(BLUETOOTH_USE_NIMBLE) && BLUETOOTH_USE_NIMBLE == true
#include <NimBLEDevice.h>
#else
#include <BLEDevice.h>
#endif

#if defined(BATTERY_ENABLED) && BATTERY_ENABLED == true
#include "abstract_battery.hpp"
#include <BLE2902.h>
#endif

// typedef void (*bh_motor_handler_t)(std::string&);
Expand Down
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ build_flags =
; -D DEBUG_ESP_PORT=Serial
; -D SERIAL_PLOTTER=true
-D BATTERY_ENABLED=true
-D BLUETOOTH_USE_NIMBLE=true
build_unflags =
-std=gnu++11
build_src_filter =
Expand All @@ -36,6 +37,7 @@ lib_deps =
adafruit/Adafruit PWM Servo Driver Library@^2.4.0
adafruit/Adafruit INA219@^1.2.1
sparkfun/SparkFun MAX1704x Fuel Gauge Arduino Library@^1.0.4
h2zero/NimBLE-Arduino@^1.4.0

[env]
framework = arduino
Expand Down