Skip to content

Commit

Permalink
#38: Configurable MTU size
Browse files Browse the repository at this point in the history
  • Loading branch information
thankthemaker committed May 22, 2022
1 parent 91ea5ca commit e672ba8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ default_envs = wemos_d1_mini32

[common_env_data]
lib_deps_external =
adafruit/Adafruit NeoPixel @ ^1.7.0
adafruit/Adafruit NeoPixel @ ^1.10.4
bakercp/Logger @ 1.0.3
h2zero/NimBLE-Arduino@1.2.0
h2zero/NimBLE-Arduino@1.3.6
fabianoriccardi/Melody Player @ ^2.2.2
bblanchon/ArduinoJson @ ^6.17.3
paulo-raca/Buffered Streams @ ^1.0.6
Expand Down
2 changes: 2 additions & 0 deletions src/AppConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void AppConfiguration::readPreferences() {
config.ledFrequency = doc["ledFrequency"] | "KHZ800";
config.idleLightTimeout = doc["idleLightTimeout"] | 60000;
config.logLevel = doc["logLevel"] | Logger::WARNING;
config.mtuSize = doc["mtuSize"] | 512;
config.saveConfig = false;
config.sendConfig = false;
preferences.end();
Expand Down Expand Up @@ -95,6 +96,7 @@ void AppConfiguration::savePreferences() {
doc["ledFrequency"] = config.ledFrequency;
doc["idleLightTimeout"] = config.idleLightTimeout;
doc["logLevel"] = config.logLevel;
doc["mtuSize"] = config.mtuSize;
String json = "";
serializeJson(doc, json);
log_n("savePreferences: %s", json.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/AppConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct Config {
VISITABLE(String , ledType);
VISITABLE(String , ledFrequency);
VISITABLE(int , idleLightTimeout);
VISITABLE(int , mtuSize);
END_VISITABLES;
};

Expand Down
35 changes: 24 additions & 11 deletions src/BleServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,9 @@ void activateWiFiAp(const char *password) {
inline
void BleServer::onConnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) {
char buf[128];
snprintf(buf, 128, "Client connected: %s", NimBLEAddress(desc->peer_ota_addr).toString().c_str());
snprintf(buf, 128, "Client connected: %s", NimBLEAddress(desc->peer_ota_addr).toString().c_str());
Logger::notice(LOG_TAG_BLESERVER, buf);
Logger::notice(LOG_TAG_BLESERVER, "Multi-connect support: start advertising");
uint16_t mtu = pServer->getPeerMTU(NimBLEAddress(desc->peer_ota_addr));
if (mtu != 0 && mtu < MTU_SIZE) {
MTU_SIZE = mtu;
BLE_PACKET_SIZE = MTU_SIZE - 3;
NimBLEDevice::setMTU(mtu);
snprintf(buf, 128, "New MTU-size: %d", mtu);
Logger::warning(LOG_TAG_BLESERVER, buf);
}
deviceConnected = true;
NimBLEDevice::startAdvertising();
};
Expand All @@ -134,13 +126,26 @@ void BleServer::onDisconnect(NimBLEServer *pServer) {
NimBLEDevice::startAdvertising();
}

// NimBLEServerCallbacks::onMTUChange
inline
void BleServer::onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc) {
char buf[128];
snprintf(buf, 128, "MTU changed - new size %d, peer %s", MTU, NimBLEAddress(desc->peer_ota_addr).toString().c_str());
Logger::notice(LOG_TAG_BLESERVER, buf);
MTU_SIZE = MTU;
BLE_PACKET_SIZE = MTU_SIZE - 3;
}

void BleServer::init(Stream *vesc, CanBus *canbus) {
vescSerial = vesc;

// Create the BLE Device
NimBLEDevice::init(AppConfiguration::getInstance()->config.deviceName.c_str());
NimBLEDevice::setMTU(MTU_SIZE);

int mtu_size = AppConfiguration::getInstance()->config.mtuSize;
NimBLEDevice::setMTU(mtu_size);
char buf[128];
snprintf(buf, 128, "Initial MTU size %d", mtu_size);
Logger::notice(LOG_TAG_BLESERVER, buf);
this->canbus = canbus;

// Create the BLE Server
Expand Down Expand Up @@ -382,6 +387,14 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
AppConfiguration::getInstance()->config.ledFrequency = value.c_str();
} else if (key == "logLevel") {
AppConfiguration::getInstance()->config.logLevel = static_cast<Logger::Level>(atoi(value.c_str()));
} else if (key == "mtuSize") {
uint16_t mtu = atoi(value.c_str());
AppConfiguration::getInstance()->config.mtuSize = mtu;
if (mtu != 0 && mtu < MTU_SIZE) {
NimBLEDevice::setMTU(mtu);
snprintf(buf, 128, "New MTU-size: %d", mtu);
Logger::warning(LOG_TAG_BLESERVER, buf);
}
} else if (key == "wifiActive") {
if (value.compare("true") != -1) {
activateWiFiAp(wifiPassword);
Expand Down
1 change: 1 addition & 0 deletions src/BleServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BleServer :
// NimBLEServerCallbacks
void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
void onDisconnect(NimBLEServer* pServer);
void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc);

// NimBLECharacteristicCallbacks
void onWrite(NimBLECharacteristic* pCharacteristic);
Expand Down

0 comments on commit e672ba8

Please sign in to comment.