Skip to content

Commit

Permalink
Relocating the PIN value to the settings. (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-winn committed Dec 13, 2021
1 parent 1785e27 commit 8218090
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 37 deletions.
5 changes: 1 addition & 4 deletions sketches/Bluetooth/Bluetooth/Bluetooth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
#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();
}

void loop() {
I2CBus.checkForAsyncCommands();
delay(10);
}

void OnI2cPacketReceived(uint8_t type, uint8_t subtype, uint8_t *data, uint8_t len) {
Expand Down
11 changes: 8 additions & 3 deletions sketches/Bluetooth/Bluetooth/src/BLEController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion sketches/Bluetooth/Bluetooth/src/BLEController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions sketches/Bluetooth/Bluetooth/src/commands/I2cCommandFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -15,20 +16,23 @@ 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();
}
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
15 changes: 15 additions & 0 deletions sketches/Bluetooth/Bluetooth/src/commands/i2c/SetPinCommand.h
Original file line number Diff line number Diff line change
@@ -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 */
27 changes: 5 additions & 22 deletions sketches/Mainboard/Mainboard/src/ConfigurationSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions sketches/Mainboard/Mainboard/src/ConfigurationSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
9 changes: 9 additions & 0 deletions sketches/Mainboard/Mainboard/src/hardware/NRF52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions sketches/Mainboard/Mainboard/src/hardware/NRF52.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions sketches/shared/BitConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions sketches/shared/BitConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions sketches/shared/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8218090

Please sign in to comment.