Added authentication on ESP side

This commit is contained in:
Dorian Zedler 2020-10-17 01:08:43 +02:00
parent 458eba1f8f
commit 2edc225c35
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
6 changed files with 32 additions and 23 deletions

View file

@ -62,6 +62,8 @@ public:
bool getDeviceConnected();
bool disconnectCurrentDevice();
String getDeviceAddress();
protected:
// callbacks for BLEServer
void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override;

View file

@ -27,25 +27,22 @@ private:
template <typename T>
const T &writeToEeprom(int address, size_t size, const T &t)
{
Serial.println("Writing at: " + String(address) + " size: " + String(sizeof(T)));
if (sizeof(T) > size) {
Serial.println("Error writing: Size should be: " + String(size));
Serial.println("[Error][EepromManager] writing: Size should be: " + String(size) + " but was: " + String(sizeof(T)));
return t;
}
const T &res = EEPROM.put(address, t);
Serial.println("Eeprom commit returned: " + String(EEPROM.commit()));
EEPROM.commit();
return res;
}
template <typename T>
T &readFromEeprom(int address, size_t size, T &t)
{
Serial.println("Reading at: " + String(address) + " size: " + String(sizeof(T)));
if (sizeof(T) > size) {
Serial.println("Error reading: Size should be: " + String(size));
Serial.println("[Error][EepromManager] reading: Size should be: " + String(size) + " but was: " + String(sizeof(T)));
return t;
}

View file

@ -56,6 +56,27 @@ void BluetoothLeUartServer::sendData(String data)
txCharacteristic->notify();
}
bool BluetoothLeUartServer::getDeviceConnected()
{
return this->deviceConnected;
}
bool BluetoothLeUartServer::disconnectCurrentDevice() {
if(!this->getDeviceConnected())
return false;
this->bleServer->disconnect(this->deviceConnectionId);
return true;
}
String BluetoothLeUartServer::getDeviceAddress() {
String address = BLEDevice::getAddress().toString().c_str();
address.toUpperCase();
return address;
}
void BluetoothLeUartServer::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param)
{
// only allow one device
@ -85,16 +106,3 @@ void BluetoothLeUartServer::onWrite(BLECharacteristic *rxCharacteristic)
if (this->callbacks != nullptr)
this->callbacks->onDataReceived(rxCharacteristic->getValue().c_str());
}
bool BluetoothLeUartServer::getDeviceConnected()
{
return this->deviceConnected;
}
bool BluetoothLeUartServer::disconnectCurrentDevice() {
if(!this->getDeviceConnected())
return false;
this->bleServer->disconnect(this->deviceConnectionId);
return true;
}

View file

@ -8,7 +8,6 @@ EepromManager::EepromManager()
EepromUnit *EepromManager::registerEempromUnit(size_t size)
{
Serial.println("Registering new EepromUnit with size: " + String(size) + " at " + String(this->currentAddressEnding));
// create a new Unit at the current address ending
EepromUnit *newUnit = new EepromUnit(this, this->currentAddressEnding, size);
// move the new address ending

View file

@ -65,18 +65,21 @@ void OmobiLedDisplay::onDataReceived(String dataString)
OmobiDisplayStatusCode replyStatus = InternalError;
JsonObject replyData = replyDoc.createNestedObject("data");
if (requestHeader != AuthorizeSessionCommand && !this->sessionAuthorized)
if (requestHeader > KeepAliveCommand && !this->sessionAuthorized)
replyStatus = Unauthorized;
else
switch (requestHeader)
{
case AuthorizeSessionCommand:
{
String combinedCode = this->bleServer->getDeviceAddress() + String(this->properties.deviceCode);
String secret = this->sha256(combinedCode);
if (this->sessionAuthorized)
{
replyStatus = Success;
}
else if (requestData["secret"] == this->sha256(this->properties.deviceCode))
else if (requestData["secret"] == secret)
{
replyStatus = Success;
this->sessionAuthorized = true;

View file

@ -13,7 +13,7 @@ void setup()
Adafruit_NeoMatrix *displayMatrix = new Adafruit_NeoMatrix(
8,
8,
1,
2,
1,
PIN,
NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_ROWS + NEO_TILE_PROGRESSIVE +