Skip to content

Commit 02d1305

Browse files
committed
Static API fixes and some refactoring
1 parent 3a0e42e commit 02d1305

8 files changed

+49
-57
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333

3434
.pio
3535
.vscode
36-
src/config.h
36+
src/config.cpp

src/ble_api.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ BLEAdvertisedDeviceCallbacks *BLEApi::_advertisedDeviceCallback = new myAdvertis
1515

1616
void BLEApi::init()
1717
{
18-
BLEDevice::init("ESP32BLEGW");
19-
bleScan = BLEDevice::getScan();
20-
bleScan->setAdvertisedDeviceCallbacks(BLEApi::_advertisedDeviceCallback);
21-
bleScan->setInterval(1349);
22-
bleScan->setWindow(449);
23-
bleScan->setActiveScan(true);
24-
_isReady = true;
18+
if (!_isReady) {
19+
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
20+
BLEDevice::init("ESP32BLEGW");
21+
bleScan = BLEDevice::getScan();
22+
bleScan->setAdvertisedDeviceCallbacks(BLEApi::_advertisedDeviceCallback);
23+
bleScan->setInterval(1349);
24+
bleScan->setWindow(449);
25+
bleScan->setActiveScan(true);
26+
_isReady = true;
27+
}
2528
}
2629

2730
bool BLEApi::isReady() {

src/config.cpp.example

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "config.h"
2+
3+
const char *aesKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // 128 bits hex representation (32 characters)
4+
const char *ssid = "YOUR_WIFI_SSID";
5+
const char *password = "YOUR_WIFI_PASSWORD";

src/config.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef ESP_GW_CONFIG_H
2+
#define ESP_GW_CONFIG_H
3+
4+
extern const char *aesKey;
5+
extern const char *ssid;
6+
extern const char *password;
7+
8+
#endif

src/config.h.example

-8
This file was deleted.

src/main.cpp

+2-23
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33

44
#include <WiFi.h>
55

6-
#include <WebSocketsServer.h>
7-
WebSocketsServer webSocket = WebSocketsServer(80);
8-
96
#include <ArduinoJson.h>
107

11-
#include "security.h"
12-
Security sec = Security();
13-
148
#include "noble_api.h"
159

1610
#include <esp_bt.h>
@@ -35,38 +29,23 @@ bool setupWifi()
3529
return true;
3630
}
3731

38-
void setupServer()
39-
{
40-
NobleApi::init(&sec);
41-
}
42-
43-
44-
void setupBLE() {
45-
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
46-
BLEApi::init();
47-
}
48-
4932
void setup()
5033
{
5134
Serial.begin(115200);
5235
esp_log_level_set("*", ESP_LOG_INFO);
5336

54-
sec.setKey(aesKey);
55-
5637
do
5738
{
5839
delay(200);
5940
} while (!setupWifi());
6041

61-
setupBLE();
62-
63-
setupServer();
42+
NobleApi::init();
6443

6544
Serial.println("Setup complete");
6645
meminfo("After full setup");
6746
}
6847

6948
void loop()
7049
{
71-
webSocket.loop();
50+
NobleApi::loop();
7251
}

src/noble_api.cpp

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#include "noble_api.h"
2-
#include "ble_api.h"
32

43
Security *NobleApi::sec = nullptr;
54
WebSocketsServer *NobleApi::ws = nullptr;
65
std::map<uint32_t, std::string> *NobleApi::challenges = nullptr;
76

8-
void NobleApi::init(Security *security)
7+
void NobleApi::init()
98
{
10-
sec = security;
9+
sec = new Security(aesKey);
1110
challenges = new std::map<uint32_t, std::string>();
11+
BLEApi::init();
1212
ws = new WebSocketsServer(ESP_GW_WEBSOCKET_PORT);
1313
ws->begin();
14-
ws->onEvent(std::bind(onWsEvent, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
15-
Serial.println(ESP_GW_WEBSOCKET_PORT);
14+
ws->onEvent(onWsEvent);
15+
}
16+
17+
void NobleApi::loop() {
18+
ws->loop();
1619
}
1720

1821
void NobleApi::onWsEvent(uint8_t client, WStype_t type, uint8_t *payload, size_t length)
@@ -41,20 +44,19 @@ void NobleApi::onWsEvent(uint8_t client, WStype_t type, uint8_t *payload, size_t
4144
StaticJsonDocument<1024> command;
4245
DeserializationError error = deserializeJson(command, payload, length);
4346

44-
if (error)
47+
if (error != DeserializationError::Ok)
4548
{ //Check for errors in parsing
46-
Serial.println("Parsing failed");
49+
Serial.printf("Message parsing failed: ");
4750
Serial.println(error.f_str());
4851
return;
4952
}
5053
else
5154
{
5255
const char *action = command["action"];
53-
if (action)
56+
if (action && strlen(action) > 0)
5457
{
5558
bool authenticated = false;
56-
std::string stringIV = challenges->at(client);
57-
if (stringIV.length() == 0)
59+
if (challenges->find(client) == challenges->end())
5860
{
5961
authenticated = true;
6062
}
@@ -109,11 +111,11 @@ void NobleApi::checkAuth(uint8_t client, const char *response)
109111
const size_t responseLength = strlen(response);
110112
if (responseLength % BLOCK_SIZE == 0)
111113
{
112-
std::string stringIV = challenges->at(client);
113-
if (stringIV.length() > 0)
114+
std::map<uint32_t, std::string>::iterator it = challenges->find(client);
115+
if (it != challenges->end())
114116
{
115117
uint8_t iv[BLOCK_SIZE];
116-
sec->fromHex(stringIV.c_str(), stringIV.length(), iv);
118+
sec->fromHex(it->second.c_str(), it->second.length(), iv);
117119

118120
uint8_t encryptedResponse[responseLength / 2];
119121
size_t encryptedResponseLength = sec->fromHex(response, responseLength, encryptedResponse);
@@ -152,12 +154,12 @@ void NobleApi::sendJsonMessage(uint8_t client, JsonDocument &command)
152154

153155
void NobleApi::sendAuthMessage(uint8_t client)
154156
{
155-
std::string stringIV = challenges->at(client);
156-
if (stringIV.length() > 0)
157+
std::map<uint32_t, std::string>::iterator it = challenges->find(client);
158+
if (it != challenges->end())
157159
{
158160
StaticJsonDocument<128> command;
159161
command["type"] = "auth";
160-
command["challenge"] = stringIV;
162+
command["challenge"] = it->second;
161163
sendJsonMessage(client, command);
162164
command.clear();
163165
}

src/noble_api.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
#include <WebSocketsServer.h>
1111
#include <ArduinoJson.h>
1212
#include <map>
13+
#include "config.h"
1314
#include "security.h"
15+
#include "ble_api.h"
1416

1517
class NobleApi {
1618
public:
17-
static void init(Security *security);
18-
static void onWsEvent(uint8_t client, WStype_t type, uint8_t * payload, size_t length);
19+
static void init();
20+
static void loop();
1921
private:
2022
static Security *sec;
2123
static WebSocketsServer *ws;
@@ -25,6 +27,7 @@ class NobleApi {
2527
static void sendJsonMessage(uint8_t client, JsonDocument &command);
2628
static void sendAuthMessage(uint8_t client);
2729
static void sendState(uint8_t client);
30+
static void onWsEvent(uint8_t client, WStype_t type, uint8_t * payload, size_t length);
2831
};
2932

3033
#endif

0 commit comments

Comments
 (0)