Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better checking for shocker types, fix rmt sending causing crashes #149

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions include/ShockerModelType.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,27 @@
#include "serialization/_fbs/ShockerModelType_generated.h"

#include <cstdint>
#include <cstring>

namespace OpenShock {
typedef OpenShock::Serialization::Types::ShockerModelType ShockerModelType;

inline bool ShockerModelTypeFromString(const char* str, ShockerModelType& out, bool allowTypo = false) {
if (strcasecmp(str, "caixianlin") == 0 || strcasecmp(str, "cai-xianlin") == 0) {
out = ShockerModelType::CaiXianlin;
return true;
}

if (strcasecmp(str, "petrainer") == 0) {
out = ShockerModelType::Petrainer;
return true;
}

if (allowTypo && strcasecmp(str, "pettrainer") == 0) {
out = ShockerModelType::Petrainer;
return true;
}

return false;
}
} // namespace OpenShock
2 changes: 1 addition & 1 deletion include/radio/rmt/internal/Shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace OpenShock::Rmt::Internal {
static_assert(N > 0, "N must be greater than 0");
static_assert(N < std::numeric_limits<T>::digits, "N must be less or equal to the number of bits in T");

for (std::size_t bit_pos = N - 1; bit_pos >= 0; --bit_pos) {
for (std::int64_t bit_pos = N - 1; bit_pos >= 0; --bit_pos) {
pulses.push_back((data >> bit_pos) & 1 ? rmtOne : rmtZero);
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/serialization/JsonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ bool JsonAPI::ParseDeviceInfoJsonResponse(int code, const cJSON* root, JsonAPI::
}

OpenShock::ShockerModelType shockerModelType;
if (strcmp(shockerModelStr, "caixianlin") == 0 || strcmp(shockerModelStr, "cai-xianlin") == 0 || strcmp(shockerModelStr, "CaiXianlin") == 0) {
shockerModelType = OpenShock::ShockerModelType::CaiXianlin;
} else if (strcmp(shockerModelStr, "petrainer") == 0 || strcmp(shockerModelStr, "Petrainer") == 0) {
shockerModelType = OpenShock::ShockerModelType::Petrainer;
} else {
if (!OpenShock::ShockerModelTypeFromString(shockerModelStr, shockerModelType, true)) { // PetTrainer is a typo in the API, we pass true to allow it
ESP_LOGJSONE("value at 'shocker.model' is not a valid shocker model", shocker);
return false;
}
Expand Down
6 changes: 1 addition & 5 deletions src/serialization/JsonSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ bool JsonSerial::ParseShockerCommand(const cJSON* root, JsonSerial::ShockerComma
return false;
}
ShockerModelType modelType;
if (strcmp(model->valuestring, "caixianlin") == 0) {
modelType = ShockerModelType::CaiXianlin;
} else if (strcmp(model->valuestring, "petrainer") == 0) {
modelType = ShockerModelType::Petrainer;
} else {
if (!ShockerModelTypeFromString(model->valuestring, modelType)) {
ESP_LOGE(TAG, "value at 'model' is not a valid shocker model (caixianlin, petrainer)");
return false;
}
Expand Down