Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Mar 16, 2021
1 parent 1a4a7c1 commit 70d3818
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 40 deletions.
12 changes: 7 additions & 5 deletions Sming/Components/rboot/include/Data/Stream/RbootOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class RbootOutputStream : public ReadWriteStream
{
public:
/**
* @brief Construct a stream using raw flash address/size
* @param startAddress the start address on the storage media
* @param maxLength the maximum allowed length of the rom. Use 0 if unlimited.
* @note This should be avoided, use partition where possible
*/
RbootOutputStream(uint32_t startAddress, size_t maxLength = 0) : startAddress(startAddress), maxLength(maxLength)
{
Expand Down Expand Up @@ -86,11 +88,11 @@ class RbootOutputStream : public ReadWriteStream
}

protected:
bool initialized = false;
rboot_write_status rBootWriteStatus = {};
size_t written = 0; // << the number of written bytes
uint32_t startAddress = 0; // << the start address on the storage
size_t maxLength = 0; // << maximum allowed length
bool initialized{false};
rboot_write_status rBootWriteStatus{};
size_t written{0}; // << the number of written bytes
uint32_t startAddress{0}; // << the start address on the storage
size_t maxLength{0}; // << maximum allowed length

protected:
virtual bool init();
Expand Down
62 changes: 48 additions & 14 deletions Sming/Components/rboot/include/Network/RbootHttpUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
#include <Network/HttpClient.h>
#include "../Data/Stream/RbootOutputStream.h"

#define NO_ROM_SWITCH 0xff
/**
* @brief Magic value for ROM slot indicating slot won't change after successful OTA
*/
constexpr uint8_t NO_ROM_SWITCH{0xff};

class RbootHttpUpdater;

typedef Delegate<void(RbootHttpUpdater& client, bool result)> OtaUpdateDelegate;
using OtaUpdateDelegate = Delegate<void(RbootHttpUpdater& client, bool result)>;

struct RbootHttpUpdaterItem {
String url;
Expand All @@ -39,11 +42,41 @@ class RbootHttpUpdater : protected HttpClient
cleanup();
}

/**
* @brief Add an item to update
* @param offset
* @param firmwareFileUrl
* @param maxSize
* @retval bool
* @note Use the `Partition` overload where possible
*/
bool addItem(uint32_t offset, const String& firmwareFileUrl, size_t maxSize = 0);
bool addItem(const String& firmwareFileUrl, RbootOutputStream* stream = nullptr);

/**
* @brief Add an item to update
* @param firmwareFileUrl
* @param partition Target partition to write
* @retval bool
*/
bool addItem(const String& firmwareFileUrl, Partition partition)
{
return addItem(partition.address(), firmwareFileUrl, partition.size());
}

/**
* @brief Add an item to update use a pre-constructed stream
* @param firmwareFileUrl
* @param stream
* @retval bool
*/
bool addItem(const String& firmwareFileUrl, RbootOutputStream* stream);

void start();

/**
* @brief On completion, switch to the given ROM slot
* @param romSlot specify NO_ROM_SWITCH (the default) to cancel any previously set switch
*/
void switchToRom(uint8_t romSlot)
{
this->romSlot = romSlot;
Expand All @@ -59,11 +92,13 @@ class RbootHttpUpdater : protected HttpClient
this->updateDelegate = reqUpdateDelegate;
}

/* Sets the base request that can be used to pass
* - default request parameters, like request headers...
* - default SSL options
* - default SSL fingeprints
* - default SSL client certificates
/**
* @brief Sets the base request that can be used to pass
*
* - default request parameters, like request headers...
* - default SSL options
* - default SSL fingeprints
* - default SSL client certificates
*
* @param request
*/
Expand All @@ -87,12 +122,11 @@ class RbootHttpUpdater : protected HttpClient

protected:
Vector<RbootHttpUpdaterItem> items;
int currentItem = 0;
rboot_write_status rbootWriteStatus;
uint8_t romSlot = NO_ROM_SWITCH;
OtaUpdateDelegate updateDelegate = nullptr;

HttpRequest* baseRequest = nullptr;
OtaUpdateDelegate updateDelegate;
HttpRequest* baseRequest{nullptr};
uint8_t romSlot{NO_ROM_SWITCH};
uint8_t currentItem{0};
rboot_write_status rbootWriteStatus{};

private:
void cleanup()
Expand Down
18 changes: 10 additions & 8 deletions Sming/Components/rboot/src/RbootHttpUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ bool RbootHttpUpdater::addItem(const String& firmwareFileUrl, RbootOutputStream*
void RbootHttpUpdater::start()
{
for(unsigned i = 0; i < items.count(); i++) {
RbootHttpUpdaterItem& it = items[i];
debug_d("Download file:\r\n (%d) %s -> %X", currentItem, it.url.c_str(), it.targetOffset);
auto& it = items[i];
debug_d("Download file:\r\n"
" (%u) %s -> %X",
currentItem, it.url.c_str(), it.targetOffset);

HttpRequest* request;
if(baseRequest != nullptr) {
Expand Down Expand Up @@ -82,8 +84,8 @@ int RbootHttpUpdater::itemComplete(HttpConnection& client, bool success)
return -1;
}

RbootHttpUpdaterItem& it = items[currentItem];
debug_d("Finished: URL: %s, Offset: %d, Length: %d", it.url.c_str(), it.stream->getStartAddress(),
auto& it = items[currentItem];
debug_d("Finished: URL: %s, Offset: 0x%X, Length: %u", it.url.c_str(), it.stream->getStartAddress(),
it.stream->available());

it.stream = nullptr; // the actual deletion will happen outside of this class
Expand All @@ -94,14 +96,14 @@ int RbootHttpUpdater::itemComplete(HttpConnection& client, bool success)

int RbootHttpUpdater::updateComplete(HttpConnection& client, bool success)
{
auto hasError = itemComplete(client, success);
if(hasError) {
int hasError = itemComplete(client, success);
if(hasError != 0) {
return hasError;
}

debug_d("\r\nFirmware download finished!");
for(unsigned i = 0; i < items.count(); i++) {
debug_d(" - item: %d, addr: %X, url: %s", i, items[i].targetOffset, items[i].url.c_str());
debug_d(" - item: %u, addr: 0x%X, url: %s", i, items[i].targetOffset, items[i].url.c_str());
}

if(!success) {
Expand Down Expand Up @@ -136,7 +138,7 @@ void RbootHttpUpdater::applyUpdate()
}

// set to boot new rom and then reboot
debug_d("Firmware updated, rebooting to rom %d...\r\n", romSlot);
debug_d("Firmware updated, rebooting to rom %u...\r\n", romSlot);
rboot_set_current_rom(romSlot);
System.restart();
}
19 changes: 10 additions & 9 deletions Sming/Libraries/OtaUpgrade/OtaUpgrade/BasicStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ class BasicStream : public ReadWriteStream
uint32_t address;
uint32_t size;
uint8_t index;
bool updated = false;
} slot;
bool updated{false};
};
Slot slot;

// Instead of RbootOutputStream, the rboot write API is used directly because in a future extension the OTA file may contain data for multiple FLASH regions.
rboot_write_status rbootWriteStatus = {};
rboot_write_status rbootWriteStatus{};

enum class State {
Error,
Expand All @@ -129,14 +130,14 @@ class BasicStream : public ReadWriteStream
VerifyRoms,
RomsComplete,
};
State state = State::Header;
State state{State::Header};

#ifdef ENABLE_OTA_SIGNING
using Verifier = SignatureVerifier;
static const uint32_t expectedHeaderMagic = OTA_HEADER_MAGIC_SIGNED;
static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_SIGNED};
#else
using Verifier = ChecksumVerifier;
static const uint32_t expectedHeaderMagic = OTA_HEADER_MAGIC_NOT_SIGNED;
static const uint32_t expectedHeaderMagic{OTA_HEADER_MAGIC_NOT_SIGNED};
#endif
Verifier verifier;

Expand All @@ -145,9 +146,9 @@ class BasicStream : public ReadWriteStream

Verifier::VerificationData verificationData;

size_t remainingBytes = 0;
uint8_t* destinationPtr = nullptr;
uint8_t romIndex = 0;
size_t remainingBytes{0};
uint8_t* destinationPtr{nullptr};
uint8_t romIndex{0};

void setupChunk(State nextState, size_t size, void* destination = nullptr)
{
Expand Down
8 changes: 4 additions & 4 deletions Sming/Libraries/OtaUpgrade/OtaUpgrade/EncryptedStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class EncryptedStream : public BasicStream
Chunk,
None,
};
Fragment fragment = Fragment::Header;

size_t remainingBytes = sizeof(header);
uint8_t* fragmentPtr = header;
Fragment fragment{Fragment::Header};
size_t remainingBytes{sizeof header};
uint8_t* fragmentPtr{header};
std::unique_ptr<uint8_t[]> buffer;
size_t bufferSize = 0;
size_t bufferSize{0};
};

} // namespace OtaUpgrade

0 comments on commit 70d3818

Please sign in to comment.