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

Refactor OTA update manager #316

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
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
23 changes: 17 additions & 6 deletions include/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include <cstdint>
#include <string_view>

#define DISABLE_COPY(TypeName) \
TypeName(const TypeName&) = delete; \
#define DISABLE_COPY(TypeName) \
TypeName(const TypeName&) = delete; \
void operator=(const TypeName&) = delete
#define DISABLE_MOVE(TypeName) \
TypeName(TypeName&&) = delete; \
#define DISABLE_MOVE(TypeName) \
TypeName(TypeName&&) = delete; \
void operator=(TypeName&&) = delete

#ifndef OPENSHOCK_API_DOMAIN
Expand All @@ -20,7 +20,17 @@
#error "OPENSHOCK_FW_VERSION must be defined"
#endif

#define OPENSHOCK_FW_CDN_URL(path) "https://" OPENSHOCK_FW_CDN_DOMAIN path
#define OPENSHOCK_FW_CDN_URL(path) "https://" OPENSHOCK_FW_CDN_DOMAIN path
#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")
#define OPENSHOCK_FW_CDN_BETA_URL OPENSHOCK_FW_CDN_CHANNEL_URL("beta")
#define OPENSHOCK_FW_CDN_DEVELOP_URL OPENSHOCK_FW_CDN_CHANNEL_URL("develop")
#define OPENSHOCK_FW_CDN_BOARDS_BASE_URL_FORMAT OPENSHOCK_FW_CDN_URL("/%s")
#define OPENSHOCK_FW_CDN_BOARDS_INDEX_URL_FORMAT OPENSHOCK_FW_CDN_BOARDS_BASE_URL_FORMAT "/boards.txt"
#define OPENSHOCK_FW_CDN_VERSION_BASE_URL_FORMAT OPENSHOCK_FW_CDN_BOARDS_BASE_URL_FORMAT "/" OPENSHOCK_FW_BOARD
#define OPENSHOCK_FW_CDN_APP_URL_FORMAT OPENSHOCK_FW_CDN_VERSION_BASE_URL_FORMAT "/app.bin"
#define OPENSHOCK_FW_CDN_FILESYSTEM_URL_FORMAT OPENSHOCK_FW_CDN_VERSION_BASE_URL_FORMAT "/staticfs.bin"
#define OPENSHOCK_FW_CDN_SHA256_HASHES_URL_FORMAT OPENSHOCK_FW_CDN_VERSION_BASE_URL_FORMAT "/hashes.sha256.txt"

#define OPENSHOCK_GPIO_INVALID -1

Expand All @@ -40,7 +50,8 @@

// Check if Arduino.h exists, if not instruct the developer to remove "arduino-esp32" from the useragent and replace it with "ESP-IDF", after which the developer may remove this warning.
#if defined(__has_include) && !__has_include("Arduino.h")
#warning "Let it be known that Arduino hath finally been cast aside in favor of the noble ESP-IDF! I beseech thee, kind sir or madam, wouldst thou kindly partake in the honors of expunging 'arduino-esp32' from yonder useragent aloft, and in its stead, bestow the illustrious 'ESP-IDF'?"
#warning \
"Let it be known that Arduino hath finally been cast aside in favor of the noble ESP-IDF! I beseech thee, kind sir or madam, wouldst thou kindly partake in the honors of expunging 'arduino-esp32' from yonder useragent aloft, and in its stead, bestow the illustrious 'ESP-IDF'?"
#endif

#if __cplusplus >= 202'302L
Expand Down
15 changes: 3 additions & 12 deletions include/config/OtaUpdateConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@

#include "config/ConfigBase.h"
#include "FirmwareBootType.h"
#include "OtaUpdateChannel.h"
#include "OtaUpdateStep.h"
#include "ota/OtaUpdateChannel.h"
#include "ota/OtaUpdateStep.h"

#include <string>

namespace OpenShock::Config {
struct OtaUpdateConfig : public ConfigBase<Serialization::Configuration::OtaUpdateConfig> {
OtaUpdateConfig();
OtaUpdateConfig(
bool isEnabled,
std::string cdnDomain,
OtaUpdateChannel updateChannel,
bool checkOnStartup,
bool checkPeriodically,
uint16_t checkInterval,
bool allowBackendManagement,
bool requireManualApproval,
int32_t updateId,
OtaUpdateStep updateStep
bool isEnabled, std::string cdnDomain, OtaUpdateChannel updateChannel, bool checkOnStartup, bool checkPeriodically, uint16_t checkInterval, bool allowBackendManagement, bool requireManualApproval, int32_t updateId, OtaUpdateStep updateStep
);

bool isEnabled;
Expand Down
35 changes: 35 additions & 0 deletions include/http/FirmwareCDN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "http/HTTPRequestManager.h"
#include "ota/FirmwareBinaryHash.h"
#include "ota/FirmwareReleaseInfo.h"
#include "ota/OtaUpdateChannel.h"
#include "SemVer.h"

#include <string_view>

namespace OpenShock::HTTP::FirmwareCDN {
/// @brief Fetches the firmware version for the given channel from the firmware CDN.
/// Valid response codes: 200, 304
/// @param channel The channel to fetch the firmware version for.
/// @return The firmware version or an error response.
HTTP::Response<OpenShock::SemVer> GetFirmwareVersion(OtaUpdateChannel channel);

/// @brief Fetches the list of available boards for the given firmware version from the firmware CDN.
/// Valid response codes: 200, 304
/// @param version The firmware version to fetch the boards for.
/// @return The list of available boards or an error response.
HTTP::Response<std::vector<std::string>> GetFirmwareBoards(const OpenShock::SemVer& version);

/// @brief Fetches the binary hashes for the given firmware version from the firmware CDN.
/// Valid response codes: 200, 304
/// @param version The firmware version to fetch the binary hashes for.
/// @return The binary hashes or an error response.
HTTP::Response<std::vector<FirmwareBinaryHash>> GetFirmwareBinaryHashes(const OpenShock::SemVer& version);

/// @brief Fetches the firmware release information for the given firmware version from the firmware CDN.
/// Valid response codes: 200, 304
/// @param version The firmware version to fetch the release information for.
/// @return The firmware release information or an error response.
HTTP::Response<FirmwareReleaseInfo> GetFirmwareReleaseInfo(const OpenShock::SemVer& version);
} // namespace OpenShock::HTTP::FirmwareCDN
11 changes: 11 additions & 0 deletions include/ota/FirmwareBinaryHash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <cstdint>
#include <string>

namespace OpenShock {
struct FirmwareBinaryHash {
std::string name;
uint8_t hash[32];
};
} // namespace OpenShock
13 changes: 13 additions & 0 deletions include/ota/FirmwareReleaseInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <cstdint>
#include <string>

namespace OpenShock {
struct FirmwareReleaseInfo {
std::string appBinaryUrl;
uint8_t appBinaryHash[32];
std::string filesystemBinaryUrl;
uint8_t filesystemBinaryHash[32];
};
} // namespace OpenShock
5 changes: 3 additions & 2 deletions include/OtaUpdateChannel.h → include/ota/OtaUpdateChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

#include "serialization/_fbs/HubConfig_generated.h"

#include <cstring>
#include <cstdint>
#include <cstring>

namespace OpenShock {
typedef OpenShock::Serialization::Configuration::OtaUpdateChannel OtaUpdateChannel;

inline bool TryParseOtaUpdateChannel(OtaUpdateChannel& channel, const char* str) {
inline bool TryParseOtaUpdateChannel(OtaUpdateChannel& channel, const char* str)
{
if (strcasecmp(str, "stable") == 0) {
channel = OtaUpdateChannel::Stable;
return true;
Expand Down
21 changes: 21 additions & 0 deletions include/ota/OtaUpdateClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "SemVer.h"

#include <freertos/task.h>

namespace OpenShock {
class OtaUpdateClient {
public:
OtaUpdateClient(const OpenShock::SemVer& version);
~OtaUpdateClient();

bool Start();

private:
void _task();

OpenShock::SemVer m_version;
TaskHandle_t m_taskHandle;
};
} // namespace OpenShock
12 changes: 1 addition & 11 deletions include/OtaUpdateManager.h → include/ota/OtaUpdateManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "FirmwareBootType.h"
#include "FirmwareReleaseInfo.h"
#include "OtaUpdateChannel.h"
#include "SemVer.h"

Expand All @@ -12,17 +13,6 @@
namespace OpenShock::OtaUpdateManager {
[[nodiscard]] bool Init();

struct FirmwareRelease {
std::string appBinaryUrl;
uint8_t appBinaryHash[32];
std::string filesystemBinaryUrl;
uint8_t filesystemBinaryHash[32];
};

bool TryGetFirmwareVersion(OtaUpdateChannel channel, OpenShock::SemVer& version);
bool TryGetFirmwareBoards(const OpenShock::SemVer& version, std::vector<std::string>& boards);
bool TryGetFirmwareRelease(const OpenShock::SemVer& version, FirmwareRelease& release);

bool TryStartFirmwareUpdate(const OpenShock::SemVer& version);

FirmwareBootType GetFirmwareBootType();
Expand Down
3 changes: 2 additions & 1 deletion include/OtaUpdateStep.h → include/ota/OtaUpdateStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
namespace OpenShock {
typedef OpenShock::Serialization::Configuration::OtaUpdateStep OtaUpdateStep;

inline bool TryParseOtaUpdateStep(OtaUpdateStep& channel, const char* str) {
inline bool TryParseOtaUpdateStep(OtaUpdateStep& channel, const char* str)
{
if (strcasecmp(str, "none") == 0) {
channel = OtaUpdateStep::None;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/GatewayClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const char* const TAG = "GatewayClient";
#include "events/Events.h"
#include "Logging.h"
#include "message_handlers/WebSocket.h"
#include "OtaUpdateManager.h"
#include "ota/OtaUpdateManager.h"
#include "serialization/WSGateway.h"
#include "Time.h"
#include "util/CertificateUtils.h"
Expand Down
Loading
Loading