From 821809047b7ab568e378734f79576a4b07dee3b5 Mon Sep 17 00:00:00 2001 From: Jeff Winn <6961614+jeff-winn@users.noreply.github.com> Date: Mon, 13 Dec 2021 13:33:51 -0500 Subject: [PATCH] Relocating the PIN value to the settings. (#100) --- sketches/Bluetooth/Bluetooth/Bluetooth.ino | 5 +--- .../Bluetooth/Bluetooth/src/BLEController.cpp | 11 +++++--- .../Bluetooth/Bluetooth/src/BLEController.h | 3 ++- .../src/commands/I2cCommandFactory.cpp | 12 ++++++--- .../src/commands/I2cCommandFactory.h | 2 +- .../src/commands/i2c/SetPinCommand.cpp | 19 +++++++++++++ .../src/commands/i2c/SetPinCommand.h | 15 +++++++++++ .../Mainboard/src/ConfigurationSettings.cpp | 27 ++++--------------- .../Mainboard/src/ConfigurationSettings.h | 4 +-- .../Mainboard/src/commands/BTReadyCommand.cpp | 1 + .../Mainboard/src/hardware/NRF52.cpp | 9 +++++++ .../Mainboard/Mainboard/src/hardware/NRF52.h | 1 + sketches/shared/BitConverter.cpp | 20 ++++++++++++++ sketches/shared/BitConverter.h | 6 +++++ sketches/shared/Constants.h | 1 + 15 files changed, 99 insertions(+), 37 deletions(-) create mode 100644 sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.cpp create mode 100644 sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.h diff --git a/sketches/Bluetooth/Bluetooth/Bluetooth.ino b/sketches/Bluetooth/Bluetooth/Bluetooth.ino index 019400c5..84b77477 100644 --- a/sketches/Bluetooth/Bluetooth/Bluetooth.ino +++ b/sketches/Bluetooth/Bluetooth/Bluetooth.ino @@ -2,10 +2,8 @@ #include "src/commands/I2cCommandFactory.h" #include "src/I2cController.h" -#define PAIRING_PIN "000000" - void setup() { - BLE.init(OnBluetoothCommandReceived, PAIRING_PIN); + BLE.init(OnBluetoothCommandReceived); I2CBus.init(OnI2cPacketReceived); I2CBus.notifyReady(); @@ -13,7 +11,6 @@ void setup() { void loop() { I2CBus.checkForAsyncCommands(); - delay(10); } void OnI2cPacketReceived(uint8_t type, uint8_t subtype, uint8_t *data, uint8_t len) { diff --git a/sketches/Bluetooth/Bluetooth/src/BLEController.cpp b/sketches/Bluetooth/Bluetooth/src/BLEController.cpp index f43c5497..dbed0857 100644 --- a/sketches/Bluetooth/Bluetooth/src/BLEController.cpp +++ b/sketches/Bluetooth/Bluetooth/src/BLEController.cpp @@ -18,7 +18,7 @@ BLEController::BLEController() { BLEController::~BLEController() { } -void BLEController::init(RemoteCommandReceivedCallback callback, const char *pin) { +void BLEController::init(RemoteCommandReceivedCallback callback) { SetBluetoothCommandReceivedCallback(callback); Bluefruit.begin(); @@ -48,9 +48,14 @@ void BLEController::init(RemoteCommandReceivedCallback callback, const char *pin Bluefruit.Advertising.setFastTimeout(30); Bluefruit.Security.setIOCaps(false, false, false); - if (pin != NULL) { - Bluefruit.Security.setPIN(pin); +} + +void BLEController::setPin(const char* pin) { + if (pin == NULL) { + return; } + + Bluefruit.Security.setPIN(pin); } void BLEController::startAdvertising() { diff --git a/sketches/Bluetooth/Bluetooth/src/BLEController.h b/sketches/Bluetooth/Bluetooth/src/BLEController.h index 209d49f8..95b31fc8 100644 --- a/sketches/Bluetooth/Bluetooth/src/BLEController.h +++ b/sketches/Bluetooth/Bluetooth/src/BLEController.h @@ -12,12 +12,13 @@ class BLEController { BLEController(); ~BLEController(); - void init(RemoteCommandReceivedCallback callback, const char *pin); + void init(RemoteCommandReceivedCallback callback); void setCharacteristic(uint8_t characteristicId, uint8_t *data, uint8_t len); void startAdvertising(); void clearBonds(); + void setPin(const char* pin); private: BlasterService m_blasterService; diff --git a/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.cpp b/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.cpp index 7e5696e4..29d035d3 100644 --- a/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.cpp +++ b/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.cpp @@ -4,6 +4,7 @@ #include "i2c/ResetCommand.h" #include "i2c/StartAdvertisingCommand.h" #include "i2c/SetCharacteristicCommand.h" +#include "i2c/SetPinCommand.h" #include "i2c/SetTransmitCountCommand.h" #include "../shared/Constants.h" @@ -15,7 +16,7 @@ I2cCommandFactory::I2cCommandFactory() { I2cCommandFactory::~I2cCommandFactory() { } -Command *I2cCommandFactory::create(uint8_t type, uint8_t subtype) { +Command* I2cCommandFactory::create(uint8_t type, uint8_t subtype) { switch (type) { case NRF52_CID_START_ADVERTISING: { return new StartAdvertisingCommand(); @@ -23,12 +24,15 @@ Command *I2cCommandFactory::create(uint8_t type, uint8_t subtype) { case NRF52_CID_RESET: { return new ResetCommand(); } - case NRF52_CID_SET_TRANSMIT_COUNT: { - return new SetTransmitCountCommand(); - } case NRF52_CID_SET_CHARACTERISTIC: { return new SetCharacteristicCommand(subtype); } + case NRF52_CID_SET_PIN: { + return new SetPinCommand(); + } + case NRF52_CID_SET_TRANSMIT_COUNT: { + return new SetTransmitCountCommand(); + } } return NULL; diff --git a/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.h b/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.h index 2a6660d1..1054adab 100644 --- a/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.h +++ b/sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.h @@ -8,7 +8,7 @@ class I2cCommandFactory { I2cCommandFactory(); ~I2cCommandFactory(); - Command *create(uint8_t type, uint8_t subtype); + Command* create(uint8_t type, uint8_t subtype); }; extern I2cCommandFactory I2cCommands; diff --git a/sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.cpp b/sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.cpp new file mode 100644 index 00000000..863b363e --- /dev/null +++ b/sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.cpp @@ -0,0 +1,19 @@ +#include "SetPinCommand.h" + +#include "../../BLEController.h" +#include "../../shared/BitConverter.h" + +SetPinCommand::SetPinCommand() { +} + +SetPinCommand::~SetPinCommand() { +} + +void SetPinCommand::executeImpl(uint8_t *data, uint8_t len) { + auto pin = Convert.toCharArray(data, len); + if (pin != NULL) { + BLE.setPin(pin); + } + + delete[] pin; +} \ No newline at end of file diff --git a/sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.h b/sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.h new file mode 100644 index 00000000..c57e4701 --- /dev/null +++ b/sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.h @@ -0,0 +1,15 @@ +#ifndef SET_PIN_COMMAND_H +#define SET_PIN_COMMAND_H + +#include "../Command.h" + +class SetPinCommand : public Command { + public: + SetPinCommand(); + ~SetPinCommand() override; + + protected: + void executeImpl(uint8_t *data, uint8_t len) override; +}; + +#endif /* SET_PIN_COMMAND_H */ \ No newline at end of file diff --git a/sketches/Mainboard/Mainboard/src/ConfigurationSettings.cpp b/sketches/Mainboard/Mainboard/src/ConfigurationSettings.cpp index 93146241..9850fea8 100644 --- a/sketches/Mainboard/Mainboard/src/ConfigurationSettings.cpp +++ b/sketches/Mainboard/Mainboard/src/ConfigurationSettings.cpp @@ -2,6 +2,8 @@ #include "shared/BitConverter.h" #include "ConfigurationSettings.h" +const char* DEFAULT_PAIRING_PIN = "000000"; + const short HAS_EXISTING_PAIRING_ADDR = 0x02; const short OPERATOR_TOKEN_LENGTH_ADDR = 0x10; const short OPERATOR_TOKEN_ADDR = 0x11; @@ -62,28 +64,9 @@ void ConfigurationSettings::clear() { } } -// AuthenticationToken_t ConfigurationSettings::getAuthenticationToken() { -// AuthenticationToken_t result; - -// result.length = framDriver.read(OPERATOR_TOKEN_LENGTH_ADDR); -// if (result.length > 0) { -// result.data = new byte[result.length]; - -// for (byte index = 0; index < result.length; index++) { -// result.data[index] = framDriver.read(OPERATOR_TOKEN_ADDR + index); -// } -// } - -// return result; -// } - -// void ConfigurationSettings::setAuthenticationToken(AuthenticationToken_t token) { -// framDriver.write(OPERATOR_TOKEN_ADDR, token.length); - -// for (byte index = 0; index < token.length; index++) { -// framDriver.write(OPERATOR_TOKEN_ADDR + index, token.data[index]); -// } -// } +const char* ConfigurationSettings::getPairingPin() { + return DEFAULT_PAIRING_PIN; +} bool ConfigurationSettings::hasExistingPairing() { return framDriver.read(HAS_EXISTING_PAIRING_ADDR) != 0x00; diff --git a/sketches/Mainboard/Mainboard/src/ConfigurationSettings.h b/sketches/Mainboard/Mainboard/src/ConfigurationSettings.h index 3dd42fec..2b3b41cd 100644 --- a/sketches/Mainboard/Mainboard/src/ConfigurationSettings.h +++ b/sketches/Mainboard/Mainboard/src/ConfigurationSettings.h @@ -6,13 +6,13 @@ // Provides access to the configuration settings. class ConfigurationSettings { public: - // AuthenticationToken_t getAuthenticationToken(); - // void setAuthenticationToken(AuthenticationToken_t token); void resetAuthenticationToken(); void setExistingPairing(bool value); bool hasExistingPairing(); + const char* getPairingPin(); + int getFeedNormalSpeed(); void setFeedNormalSpeed(int value); diff --git a/sketches/Mainboard/Mainboard/src/commands/BTReadyCommand.cpp b/sketches/Mainboard/Mainboard/src/commands/BTReadyCommand.cpp index 4aa4621b..34269067 100644 --- a/sketches/Mainboard/Mainboard/src/commands/BTReadyCommand.cpp +++ b/sketches/Mainboard/Mainboard/src/commands/BTReadyCommand.cpp @@ -24,5 +24,6 @@ void BTReadyCommand::handleImpl(uint8_t* data, uint16_t len) { BT.setCharacteristic(NRF52_CHR_BELT_MAX_SPEED, Settings.getFeedMaxSpeed()); BT.setCharacteristic(NRF52_CHR_HOPPER_LOCK_ENABLED, Settings.isHopperLockEnabled()); + BT.setPin(Settings.getPairingPin()); BT.startAdvertising(); } \ No newline at end of file diff --git a/sketches/Mainboard/Mainboard/src/hardware/NRF52.cpp b/sketches/Mainboard/Mainboard/src/hardware/NRF52.cpp index 694e24af..e92396c1 100644 --- a/sketches/Mainboard/Mainboard/src/hardware/NRF52.cpp +++ b/sketches/Mainboard/Mainboard/src/hardware/NRF52.cpp @@ -27,6 +27,15 @@ bool NRF52::hasPendingPackets() { return m_signal->isSet(); } +void NRF52::setPin(const char* pin) { + auto len = strlen(pin); + auto bytes = Convert.toByteArray(pin, len); + + sendPacket(NRF52_CID_SET_PIN, 0, bytes, len); + + delete[] bytes; +} + void NRF52::startAdvertising() { sendPacket(NRF52_CID_START_ADVERTISING, 0, NULL, 0); } diff --git a/sketches/Mainboard/Mainboard/src/hardware/NRF52.h b/sketches/Mainboard/Mainboard/src/hardware/NRF52.h index 7ef4c5da..24af4393 100644 --- a/sketches/Mainboard/Mainboard/src/hardware/NRF52.h +++ b/sketches/Mainboard/Mainboard/src/hardware/NRF52.h @@ -19,6 +19,7 @@ class NRF52 { void setCharacteristic(uint8_t characteristicId, int value); void setCharacteristic(uint8_t characteristicId, uint8_t *data, uint8_t len); + void setPin(const char* pin); void startAdvertising(); void readPacket(ReadPacketCallback callback); diff --git a/sketches/shared/BitConverter.cpp b/sketches/shared/BitConverter.cpp index 94e9132c..19addeba 100644 --- a/sketches/shared/BitConverter.cpp +++ b/sketches/shared/BitConverter.cpp @@ -58,4 +58,24 @@ byte* BitConverter::toFloatArray(const float value) { }; return result; +} + +char* BitConverter::toCharArray(const byte* bytes, size_t len) { + char* chars = new char[len]; + + for (int index = 0; index < len; index++) { + chars[index] = (byte)bytes[index]; + } + + return chars; +} + +byte* BitConverter::toByteArray(const char* str, size_t len) { + byte* bytes = new byte[len]; + + for (int index = 0; index < len; index++) { + bytes[index] = (byte)str[index]; + } + + return bytes; } \ No newline at end of file diff --git a/sketches/shared/BitConverter.h b/sketches/shared/BitConverter.h index ca0689ec..5f3fb2c1 100644 --- a/sketches/shared/BitConverter.h +++ b/sketches/shared/BitConverter.h @@ -17,6 +17,12 @@ class BitConverter { // Converts the value to a byte array. byte* toFloatArray(const float value); + + // Converts the string to a byte array. + byte* toByteArray(const char* str, size_t len); + + // Converts the byte array to a string. + char* toCharArray(const byte* bytes, size_t len); }; // Performs conversions between data types. diff --git a/sketches/shared/Constants.h b/sketches/shared/Constants.h index 5256d695..0bf60464 100644 --- a/sketches/shared/Constants.h +++ b/sketches/shared/Constants.h @@ -9,6 +9,7 @@ const uint8_t NRF52_CID_SET_TRANSMIT_COUNT = 0x0B; const uint8_t NRF52_CID_START_ADVERTISING = 0x01; const uint8_t NRF52_CID_RESET = 0x02; const uint8_t NRF52_CID_SET_CHARACTERISTIC = 0x03; +const uint8_t NRF52_CID_SET_PIN = 0x04; const uint8_t NRF52_CID_BELT_SPEED = 0x64; const uint8_t NRF52_CID_FLYWHEEL_SPEED = 0xC8; const uint8_t NRF52_CID_FLYWHEEL_TRIM = 0xC9;