Skip to content

Commit

Permalink
Check for unhandled arguments in REST requests, and print them.
Browse files Browse the repository at this point in the history
Changed all getFromRequest<bool> to getFromRequest<int> because rduinoJson considers a bool and int but not the other way around.
  • Loading branch information
jackjansen committed Jan 12, 2025
1 parent bd31888 commit 3c5a6d7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 35 deletions.
12 changes: 11 additions & 1 deletion src/iotsaApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,24 @@ class IotsaApiMod : public IotsaMod, public IotsaApiProvider {
virtual bool getHandler(const char *path, JsonObject& reply) override { return false; }
virtual bool putHandler(const char *path, const JsonVariant& request, JsonObject& reply) override { return false; }
virtual bool postHandler(const char *path, const JsonVariant& request, JsonObject& reply) override { return false; }
protected:
template <typename JT, typename CT> bool getFromRequest(const JsonObject& reqObj, const char *name, CT& var) {
if (reqObj[name].is<JT>()) {
var = reqObj[name].as<CT>();
reqObj.remove(name);
return true;
}
// IFDEBUG IotsaSerial.printf("xxxjack IotsaApi parameter %s not found\n", name);
return false;
}
protected:
bool checkUnhandled(const JsonObject& reqObj) {
bool rv = false;
for (JsonPair kv : reqObj) {
rv = true;
IFDEBUG IotsaSerial.printf("Unhandled IotsaApi parameter: %s\n", kv.key().c_str());
}
return rv;
}
IotsaApiService api;
};

Expand Down
15 changes: 9 additions & 6 deletions src/iotsaBLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,19 @@ bool IotsaBLEServerMod::getHandler(const char *path, JsonObject& reply) {

bool IotsaBLEServerMod::putHandler(const char *path, const JsonVariant& request, JsonObject& reply) {
JsonObject reqObj = request.as<JsonObject>();
bool anyChanged = false;
bool newEnabled = isEnabled;
if (getFromRequest<bool>(reqObj, "isEnabled", newEnabled) && newEnabled != isEnabled) {
if (getFromRequest<int>(reqObj, "isEnabled", newEnabled) && newEnabled != isEnabled) {
anyChanged = true;
isEnabled = request["isEnabled"];
iotsaConfig.requestReboot(4000);
}
adv_min = request["adv_min"]|adv_min;
adv_max = request["adv_max"]|adv_max;
tx_power = request["tx_power"]|tx_power;
configSave();
return true;
if (getFromRequest<int>(reqObj, "adv_min", adv_min)) anyChanged = true;
if (getFromRequest<int>(reqObj, "adv_max", adv_max)) anyChanged = true;
if (getFromRequest<int>(reqObj, "tx_power", tx_power)) anyChanged = true;
if (anyChanged) configSave();
checkUnhandled(reqObj);
return anyChanged;
}
#endif // IOTSA_WITH_API

Expand Down
27 changes: 14 additions & 13 deletions src/iotsaButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ bool IotsaButtonMod::getHandler(const char *path, JsonObject& reply) {
}

bool IotsaButtonMod::putHandler(const char *path, const JsonVariant& request, JsonObject& reply) {
bool any = false;
bool anyChanged = false;
if (strcmp(path, "/api/buttons") == 0) {
if (!request.is<JsonArray>()) {
return false;
Expand All @@ -182,14 +182,14 @@ bool IotsaButtonMod::putHandler(const char *path, const JsonVariant& request, Js
for (int i=0; i<nButton; i++) {
const JsonVariant r = all[i];
if (buttons[i].req.putHandler(r)) {
any = true;
anyChanged = true;
}
const JsonObject reqObj = r.as<JsonObject>();
if (getFromRequest<bool>(reqObj, "onPress", buttons[i].sendOnPress)) {
any = true;
if (getFromRequest<int>(reqObj, "onPress", buttons[i].sendOnPress)) {
anyChanged = true;
}
if (getFromRequest<bool>(reqObj, "onRelease", buttons[i].sendOnRelease)) {
any = true;
if (getFromRequest<int>(reqObj, "onRelease", buttons[i].sendOnRelease)) {
anyChanged = true;
}
}
} else {
Expand All @@ -198,18 +198,19 @@ bool IotsaButtonMod::putHandler(const char *path, const JsonVariant& request, Js
int idx = num.toInt();
Button *b = buttons + idx;
if (b->req.putHandler(request)) {
any = true;
anyChanged = true;
}
const JsonObject reqObj = request.as<JsonObject>();
if (getFromRequest<bool>(reqObj, "onPress", buttons[idx].sendOnPress)) {
any = true;
if (getFromRequest<int>(reqObj, "onPress", buttons[idx].sendOnPress)) {
anyChanged = true;
}
if (getFromRequest<bool>(reqObj, "onRelease", buttons[idx].sendOnRelease)) {
any = true;
if (getFromRequest<int>(reqObj, "onRelease", buttons[idx].sendOnRelease)) {
anyChanged = true;
}
}
if (any) configSave();
return any;
// xxxjack cannot use checkUnhandled() because of the array of buttons
if (anyChanged) configSave();
return anyChanged;
}
#endif // IOTSA_WITH_API

Expand Down
15 changes: 10 additions & 5 deletions src/iotsaConfigMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ bool IotsaConfigMod::putHandler(const char *path, const JsonVariant& request, Js
JsonObject reqObj = request.as<JsonObject>();
// First look for arguments that are also valid in normal mode.
bool wifiDisabled;
if (getFromRequest<bool>(reqObj, "wifiDisabled", wifiDisabled)) {
if (getFromRequest<int>(reqObj, "wifiDisabled", wifiDisabled)) {
iotsa_wifi_mode newMode = wifiDisabled ? iotsa_wifi_mode::IOTSA_WIFI_DISABLED : iotsa_wifi_mode::IOTSA_WIFI_NORMAL;
iotsaConfig.wifiMode = newMode;
iotsaConfig.wantWifiModeSwitchAtMillis = millis()+1000;
radioModeChanged = true;
}
#ifdef IOTSA_WITH_BLE
bool bleDisabled;
if (getFromRequest<bool>(reqObj, "bleDisabled", bleDisabled)) {
if (getFromRequest<int>(reqObj, "bleDisabled", bleDisabled)) {
iotsa_ble_mode newMode = bleDisabled ? iotsa_ble_mode::IOTSA_BLE_DISABLED : iotsa_ble_mode::IOTSA_BLE_ENABLED;
iotsaConfig.bleMode = newMode;
iotsaConfig.wantBleModeSwitchAtMillis = millis()+1000;
Expand All @@ -450,7 +450,9 @@ bool IotsaConfigMod::putHandler(const char *path, const JsonVariant& request, Js
}
}
if (!iotsaConfig.inConfigurationOrFactoryMode()) {
IFDEBUG IotsaSerial.println("Not in config mode");
if (checkUnhandled(reqObj)) {
IotsaSerial.println("Unhandled IotsaApi parameters, not in config mode");
}
if (reqObj["reboot"]) {
iotsaConfig.requestReboot(2000);
anyChanged = true;
Expand All @@ -461,11 +463,11 @@ bool IotsaConfigMod::putHandler(const char *path, const JsonVariant& request, Js
anyChanged = true;
reply["needsReboot"] = true;
}
if (getFromRequest<bool>(reqObj, "wifiDisabledOnBoot", iotsaConfig.wifiDisabledOnBoot)) {
if (getFromRequest<int>(reqObj, "wifiDisabledOnBoot", iotsaConfig.wifiDisabledOnBoot)) {
anyChanged = true;
}
#ifdef IOTSA_WITH_BLE
if (getFromRequest<bool>(reqObj, "bleDisabledOnBoot", iotsaConfig.bleDisabledOnBoot)) {
if (getFromRequest<int>(reqObj, "bleDisabledOnBoot", iotsaConfig.bleDisabledOnBoot)) {
anyChanged = true;
}
#endif
Expand Down Expand Up @@ -549,6 +551,9 @@ bool IotsaConfigMod::putHandler(const char *path, const JsonVariant& request, Js
iotsaConfig.requestReboot(2000);
anyChanged = true;
}
if (checkUnhandled(reqObj)) {
IotsaSerial.println("Unhandled IotsaApi parameters");
}
return anyChanged||radioModeChanged;
}
#endif // IOTSA_WITH_API
Expand Down
1 change: 1 addition & 0 deletions src/iotsaNothing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ bool IotsaNothingMod::putHandler(const char *path, const JsonVariant& request, J
anyChanged = true;
}
if (anyChanged) configSave();
checkUnhandled(reqObj);
return anyChanged;
}
#endif // IOTSA_WITH_API
Expand Down
1 change: 1 addition & 0 deletions src/iotsaNtp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ bool IotsaNtpMod::putHandler(const char *path, const JsonVariant& request, JsonO
}
#endif
if (anyChanged) configSave();
checkUnhandled(reqObj);
return anyChanged;
}
#endif // IOTSA_WITH_API
Expand Down
1 change: 1 addition & 0 deletions src/iotsaRtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ bool IotsaRtcMod::putHandler(const char *path, const JsonVariant& request, JsonO
if (getFromRequest<const char *>(reqObj, "isoTime", time)) {
anyChanged = setIsoTime(time);
}
checkUnhandled(reqObj);
return anyChanged;
}
#endif // IOTSA_WITH_API
Expand Down
12 changes: 2 additions & 10 deletions src/iotsaWifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,12 @@ bool IotsaWifiMod::putHandler(const char *path, const JsonVariant& request, Json
if (getFromRequest<const char *>(reqObj, "ssidPassword", ssidPassword)) {
anyChanged = true;
}
#if 0
if (reqObj["ssid"].is<const char *>()) {
ssid = reqObj["ssid"].as<String>();
anyChanged = true;
}
if (reqObj["ssidPassword"].is<const char *>()) {
ssidPassword = reqObj["ssidPassword"].as<String>();
anyChanged = true;
}
#endif

if (anyChanged) configSave();
if (reqObj["reboot"]) {
iotsaConfig.requestReboot(2000);
}
checkUnhandled(reqObj);
return anyChanged;
}
#endif // IOTSA_WITH_API
Expand Down

0 comments on commit 3c5a6d7

Please sign in to comment.