Skip to content

Commit

Permalink
Fix the rest of the problems
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Sep 6, 2024
1 parent e1ce415 commit 8767bfb
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 104 deletions.
4 changes: 1 addition & 3 deletions include/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
TypeName(TypeName&&) = delete; \

Check warning on line 10 in include/Common.h

View workflow job for this annotation

GitHub Actions / cpp-linter

include/Common.h:10:12 [bugprone-macro-parentheses]

macro argument should be enclosed in parentheses
void operator=(TypeName&&) = delete

Check warning on line 11 in include/Common.h

View workflow job for this annotation

GitHub Actions / cpp-linter

include/Common.h:11:18 [bugprone-macro-parentheses]

macro argument should be enclosed in parentheses


#ifndef OPENSHOCK_API_DOMAIN
#error "OPENSHOCK_API_DOMAIN must be defined"
#endif
Expand Down Expand Up @@ -59,6 +58,5 @@
#endif

namespace OpenShock::Constants {

Check warning on line 60 in include/Common.h

View workflow job for this annotation

GitHub Actions / cpp-linter

include/Common.h:60:11 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'OpenShock' is non-const and globally accessible, consider making it const
const char* const FW_USERAGENT = OPENSHOCK_FW_USERAGENT;
const std::string_view FW_USERAGENT_sv = OPENSHOCK_FW_USERAGENT ""_sv;
const char* const FW_USERAGENT = OPENSHOCK_FW_USERAGENT;
} // namespace OpenShock::Constants
4 changes: 2 additions & 2 deletions include/SemVer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace OpenShock {
SemVer(uint16_t major, uint16_t minor, uint16_t patch)
: major(major), minor(minor), patch(patch), prerelease(), build()
{}
SemVer(uint16_t major, uint16_t minor, uint16_t patch, StringView prerelease, StringView build)
: major(major), minor(minor), patch(patch), prerelease(prerelease.toString()), build(build.toString())
SemVer(uint16_t major, uint16_t minor, uint16_t patch, std::string_view prerelease, std::string_view build)
: major(major), minor(minor), patch(patch), prerelease(std::string(prerelease)), build(std::string(build))
{}

bool operator==(const SemVer& other) const {
Expand Down
104 changes: 102 additions & 2 deletions include/util/StringUtils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,108 @@
#pragma once

Check notice on line 1 in include/util/StringUtils.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on include/util/StringUtils.h

File include/util/StringUtils.h does not conform to Custom style guidelines. (lines 66, 67, 71, 72)

#include <string>
#include <WString.h>

Check failure on line 3 in include/util/StringUtils.h

View workflow job for this annotation

GitHub Actions / cpp-linter

include/util/StringUtils.h:3:10 [clang-diagnostic-error]

'WString.h' file not found

#include <cstdint>
#include <limits>
#include <string>
#include <string_view>
#include <vector>

namespace OpenShock {

Check warning on line 11 in include/util/StringUtils.h

View workflow job for this annotation

GitHub Actions / cpp-linter

include/util/StringUtils.h:11:11 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'OpenShock' is non-const and globally accessible, consider making it const
bool FormatToString(std::string& out, const char* format, ...);
} // namespace OpenShock

inline std::vector<std::string_view> StringSplit(const std::string_view view, char delimiter, std::size_t maxSplits = std::numeric_limits<std::size_t>::max()) {
if (view.empty()) {
return {};
}

std::vector<std::string_view> result = {};

std::size_t pos = 0;
std::size_t splits = 0;
while (pos < view.size() && splits < maxSplits) {
std::size_t nextPos = view.find(delimiter, pos);
if (nextPos == std::string_view::npos) {
nextPos = view.size();
}

result.push_back(view.substr(pos, nextPos - pos));
pos = nextPos + 1;
++splits;
}

if (pos < view.size()) {
result.push_back(view.substr(pos));
}

return result;
}
inline std::vector<std::string_view> StringSplit(const std::string_view view, bool (*predicate)(char delimiter), std::size_t maxSplits = std::numeric_limits<std::size_t>::max()) {
if (view.empty()) {
return {};
}

std::vector<std::string_view> result = {};

const char* start = nullptr;
for (const char* ptr = view.begin(); ptr < view.end(); ++ptr) {
if (predicate(*ptr)) {
if (start != nullptr) {
result.emplace_back(std::string_view(start, ptr - start));
start = nullptr;
}
} else if (start == nullptr) {
start = ptr;
}
}

if (start != nullptr) {
result.emplace_back(std::string_view(start, view.end() - start));
}

return result;
}
inline std::vector<std::string_view> StringSplitNewLines(const std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t>::max()) {
return StringSplit(
view, [](char c) { return c == '\r' || c == '\n'; }, maxSplits
);
}
inline std::vector<std::string_view> StringSplitWhiteSpace(const std::string_view view, std::size_t maxSplits = std::numeric_limits<std::size_t>::max()) {
return StringSplit(
view, [](char c) { return isspace(c) != 0; }, maxSplits
);
}
constexpr std::string_view StringTrimLeft(std::string_view view) {
if (view.empty()) {
return view;
}

std::size_t pos = 0;
while (pos < view.size() && isspace(view[pos])) {
++pos;
}

return view.substr(pos);
}
constexpr std::string_view StringTrimRight(std::string_view view) {
if (view.empty()) {
return view;
}

std::size_t pos = view.size() - 1;
while (pos > 0 && isspace(view[pos])) {
--pos;
}

return view.substr(0, pos + 1);
}
constexpr std::string_view StringTrim(std::string_view view) {
return StringTrimLeft(StringTrimRight(view));
}
constexpr bool StringStartsWith(std::string_view view, std::string_view prefix) {
return view.size() >= prefix.size() && view.substr(0, prefix.size()) == prefix;
}
inline String StringToArduinoString(std::string_view view) {
return String(view.data(), view.size());
}
} // namespace OpenShock
1 change: 1 addition & 0 deletions src/EStopManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const char* const TAG = "EStopManager";
#include "VisualStateManager.h"

#include <driver/gpio.h>
#include <freertos/queue.h>
#include <freertos/timers.h>

using namespace OpenShock;
Expand Down
36 changes: 19 additions & 17 deletions src/OtaUpdateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const char* const TAG = "OtaUpdateManager";
#include <sstream>
#include <string_view>

using namespace std::string_view_literals;

#define OPENSHOCK_FW_CDN_CHANNEL_URL(ch) OPENSHOCK_FW_CDN_URL("/version-" ch ".txt")

#define OPENSHOCK_FW_CDN_STABLE_URL OPENSHOCK_FW_CDN_CHANNEL_URL("stable")
Expand Down Expand Up @@ -148,7 +150,7 @@ bool _flashAppPartition(const esp_partition_t* partition, std::string_view remot

if (!OpenShock::FlashPartitionFromUrl(partition, remoteUrl, remoteHash, onProgress)) {
ESP_LOGE(TAG, "Failed to flash app partition");
_sendFailureMessage("Failed to flash app partition"_sv);
_sendFailureMessage("Failed to flash app partition"sv);
return false;
}

Expand All @@ -159,7 +161,7 @@ bool _flashAppPartition(const esp_partition_t* partition, std::string_view remot
// Set app partition bootable.
if (esp_ota_set_boot_partition(partition) != ESP_OK) {
ESP_LOGE(TAG, "Failed to set app partition bootable");
_sendFailureMessage("Failed to set app partition bootable"_sv);
_sendFailureMessage("Failed to set app partition bootable"sv);
return false;
}

Expand All @@ -174,7 +176,7 @@ bool _flashFilesystemPartition(const esp_partition_t* parition, std::string_view
// Make sure captive portal is stopped, timeout after 5 seconds.
if (!CaptivePortal::ForceClose(5000U)) {
ESP_LOGE(TAG, "Failed to force close captive portal (timed out)");
_sendFailureMessage("Failed to force close captive portal (timed out)"_sv);
_sendFailureMessage("Failed to force close captive portal (timed out)"sv);
return false;
}

Expand All @@ -194,7 +196,7 @@ bool _flashFilesystemPartition(const esp_partition_t* parition, std::string_view

if (!OpenShock::FlashPartitionFromUrl(parition, remoteUrl, remoteHash, onProgress)) {
ESP_LOGE(TAG, "Failed to flash filesystem partition");
_sendFailureMessage("Failed to flash filesystem partition"_sv);
_sendFailureMessage("Failed to flash filesystem partition"sv);
return false;
}

Expand All @@ -206,7 +208,7 @@ bool _flashFilesystemPartition(const esp_partition_t* parition, std::string_view
fs::LittleFSFS test;
if (!test.begin(false, "/static", 10, "static0")) {
ESP_LOGE(TAG, "Failed to mount filesystem");
_sendFailureMessage("Failed to mount filesystem"_sv);
_sendFailureMessage("Failed to mount filesystem"sv);
return false;
}
test.end();
Expand Down Expand Up @@ -334,7 +336,7 @@ void _otaUpdateTask(void* arg) {
OtaUpdateManager::FirmwareRelease release;
if (!OtaUpdateManager::TryGetFirmwareRelease(version, release)) {
ESP_LOGE(TAG, "Failed to fetch firmware release"); // TODO: Send error message to server
_sendFailureMessage("Failed to fetch firmware release"_sv);
_sendFailureMessage("Failed to fetch firmware release"sv);
continue;
}

Expand All @@ -350,15 +352,15 @@ void _otaUpdateTask(void* arg) {
const esp_partition_t* appPartition = esp_ota_get_next_update_partition(nullptr);
if (appPartition == nullptr) {
ESP_LOGE(TAG, "Failed to get app update partition"); // TODO: Send error message to server
_sendFailureMessage("Failed to get app update partition"_sv);
_sendFailureMessage("Failed to get app update partition"sv);
continue;
}

// Get filesystem partition.
const esp_partition_t* filesystemPartition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, "static0");
if (filesystemPartition == nullptr) {
ESP_LOGE(TAG, "Failed to find filesystem partition"); // TODO: Send error message to server
_sendFailureMessage("Failed to find filesystem partition"_sv);
_sendFailureMessage("Failed to find filesystem partition"sv);
continue;
}

Expand All @@ -373,7 +375,7 @@ void _otaUpdateTask(void* arg) {
// Set OTA boot type in config.
if (!Config::SetOtaUpdateStep(OpenShock::OtaUpdateStep::Updated)) {
ESP_LOGE(TAG, "Failed to set OTA update step");
_sendFailureMessage("Failed to set OTA update step"_sv);
_sendFailureMessage("Failed to set OTA update step"sv);
continue;
}

Expand Down Expand Up @@ -410,17 +412,17 @@ bool _tryGetStringList(std::string_view url, std::vector<std::string>& list) {

std::string_view data = response.data;

auto lines = data.splitLines();
auto lines = OpenShock::StringSplitNewLines(data);
list.reserve(lines.size());

for (auto line : lines) {
line = line.trim();
line = OpenShock::StringTrim(line);

if (line.empty()) {
continue;
}

list.push_back(line.toString());
list.push_back(std::string(line));
}

return true;
Expand Down Expand Up @@ -580,21 +582,21 @@ bool OtaUpdateManager::TryGetFirmwareRelease(const OpenShock::SemVer& version, F
return false;
}

auto hashesLines = std::string_view(sha256HashesResponse.data).splitLines();
auto hashesLines = OpenShock::StringSplitNewLines(sha256HashesResponse.data);

// Parse hashes.
bool foundAppHash = false, foundFilesystemHash = false;
for (std::string_view line : hashesLines) {
auto parts = line.splitWhitespace();
auto parts = OpenShock::StringSplitWhiteSpace(line);
if (parts.size() != 2) {
ESP_LOGE(TAG, "Invalid hashes entry: %.*s", line.size(), line.data());
return false;
}

auto hash = parts[0].trim();
auto file = parts[1].trim();
auto hash = OpenShock::StringTrim(parts[0]);
auto file = OpenShock::StringTrim(parts[1]);

if (file.startsWith("./"_sv)) {
if (OpenShock::StringStartsWith(file, "./"sv)) {
file = file.substr(2);
}

Expand Down
5 changes: 3 additions & 2 deletions src/SemVer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const char* const TAG = "SemVer";

#include "Logging.h"
#include "util/StringUtils.h"

using namespace OpenShock;

Expand Down Expand Up @@ -145,7 +146,7 @@ bool _semverIsVersionCore(std::string_view str) {
return false;
}

auto parts = str.split('.');
auto parts = OpenShock::StringSplit(str, '.');
if (parts.size() != 3) {
return false;
}
Expand Down Expand Up @@ -253,7 +254,7 @@ std::string SemVer::toString() const {
}

bool OpenShock::TryParseSemVer(std::string_view semverStr, SemVer& semver) {
auto parts = semverStr.split('.');
auto parts = OpenShock::StringSplit(semverStr, '.');
if (parts.size() < 3) {
ESP_LOGE(TAG, "Must have at least 3 parts: %.*s", semverStr.length(), semverStr.data());
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/config/WiFiConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace OpenShock::Config;

WiFiConfig::WiFiConfig() : accessPointSSID(OPENSHOCK_FW_AP_PREFIX), hostname(OPENSHOCK_FW_HOSTNAME), credentialsList() { }

WiFiConfig::WiFiConfig(std::string_view accessPointSSID, std::string_view hostname, const std::vector<WiFiCredentials>& credentialsList) : accessPointSSID(accessPointSSID.toString()), hostname(hostname.toString()), credentialsList(credentialsList) { }
WiFiConfig::WiFiConfig(std::string_view accessPointSSID, std::string_view hostname, const std::vector<WiFiCredentials>& credentialsList) : accessPointSSID(std::string(accessPointSSID)), hostname(std::string(hostname)), credentialsList(credentialsList) { }

void WiFiConfig::ToDefault() {
accessPointSSID = OPENSHOCK_FW_AP_PREFIX;
Expand Down
2 changes: 1 addition & 1 deletion src/config/WiFiCredentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace OpenShock::Config;

WiFiCredentials::WiFiCredentials() : id(0), ssid(), password() { }

WiFiCredentials::WiFiCredentials(uint8_t id, std::string_view ssid, std::string_view password) : id(id), ssid(ssid.toString()), password(password.toString()) { }
WiFiCredentials::WiFiCredentials(uint8_t id, std::string_view ssid, std::string_view password) : id(id), ssid(std::string(ssid)), password(std::string(password)) { }

void WiFiCredentials::ToDefault() {
id = 0;
Expand Down
Loading

0 comments on commit 8767bfb

Please sign in to comment.