Skip to content

Commit

Permalink
Add EmergencyMode class, add it to ctx
Browse files Browse the repository at this point in the history
This will be used for a fallback mechanism if the filesystem gets corrupted.
  • Loading branch information
alufers committed Apr 12, 2024
1 parent efb388c commit d2a3de7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/core/main/app/EmergencyMode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "EmergencyMode.h"
#include <string>
#include "EuphLogger.h"

using namespace euph;

EmergencyMode::EmergencyMode() {}

bool EmergencyMode::isActive() const {
return this->reason != EmergencyModeReason::NOT_ACTIVE;
}

void EmergencyMode::trip(EmergencyModeReason reason) {
EUPH_LOG(error, "EmergencyMode", "Tripped emergency mode with reason: %s",
getReasonString(reason).c_str());
this->reason = reason;
}

bool EmergencyMode::tryServe(struct mg_connection* conn) {
if (!this->isActive()) {
return false;
}

// Serve the emergency mode page
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n\r\n"
"Emergency mode!");

return true;
}

std::string EmergencyMode::getReasonString(EmergencyModeReason reason) {

#define CASE_RETURN_STR(x) \
case EmergencyModeReason::x: \
return #x
switch (reason) {
CASE_RETURN_STR(NOT_ACTIVE);
CASE_RETURN_STR(BERRY_INIT_ERROR);
}

return "__FIXME_UNKNOWN_ENUM_VALUE__";
}
4 changes: 4 additions & 0 deletions src/core/main/app/HTTPDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ void HTTPDispatcher::serveWeb(struct mg_connection* conn) {
}
}

if (this->ctx->emergencyMode->tryServe(conn)) {
return;
}

auto reqInfo = mg_get_request_info(conn);
std::string_view uri = std::string_view(reqInfo->local_uri);

Expand Down
64 changes: 64 additions & 0 deletions src/core/main/app/include/EmergencyMode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include <memory>
#include <string>

#include "civetweb.h"

namespace euph {

/**
* @brief Enumerates the reasons why emergency mode can be active.
*/
enum class EmergencyModeReason {

/**
* @brief Emergency mode is not actually active.
*/
NOT_ACTIVE = 0,

BERRY_INIT_ERROR = 1,
};

/**
* @brief Emergency mode provides a way to recover a device which suffered major filesystem corruption.
*
* It serves a very simple one-file web page that can be used to connect to Wi-Fi,
* wipe the filesystem, download new filesystem contents (or upload them from a local PC).
*
* The web page should not depend on any files stored on the FS.
*/
class EmergencyMode {
public:
EmergencyMode();

bool isActive() const;

/**
* @brief Enable emergency mode with a specific reason.
*
* @param reason The reason why emergency mode has been activated.
*/
void trip(EmergencyModeReason reason);

/**
* @brief Serve the emergency mode page, if emergency mode is active.
*
* @param conn The connection to serve the page to.
* @return true If the page was served, caller should return from the request handler.
* @return false If the page was not served, caller should continue processing the request.
*/
bool tryServe(struct mg_connection* conn);

/**
* @brief Returns a human-readable string for the given emergency mode reason.
*
* @param reason The reason to get a string for.
* @return std::string The human-readable string.
*/
static std::string getReasonString(EmergencyModeReason reason);

private:
EmergencyModeReason reason = EmergencyModeReason::NOT_ACTIVE;
};
} // namespace euph
5 changes: 5 additions & 0 deletions src/core/main/app/include/EuphContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// forward declaration of Context
#include "CoreEvents.h"
#include "EmergencyMode.h"
#include "WrappedSemaphore.h"
namespace euph {
struct Context;
Expand All @@ -15,6 +16,7 @@ class Connectivity;
#include "BerryBind.h"
#include "Connectivity.h"
#include "EventBus.h"
#include "EmergencyMode.h"

/**
* @brief The main context of the application.
Expand Down Expand Up @@ -103,6 +105,7 @@ struct Context {
std::shared_ptr<bell::CentralAudioBuffer> audioBuffer;
std::shared_ptr<euph::PlaybackController> playbackController;
std::shared_ptr<euph::Connectivity> connectivity;
std::shared_ptr<euph::EmergencyMode> emergencyMode;

// Display name of the device, gets replaced by user setting later on
std::string displayName = "Euphonium";
Expand All @@ -120,6 +123,7 @@ struct Context {
ctx->playbackController->eventBus = ctx->eventBus;
ctx->playbackController->playbackAccessSemaphore = std::make_unique<bell::WrappedSemaphore>(1);
ctx->playbackController->playbackAccessSemaphore->give();
ctx->emergencyMode = std::make_shared<euph::EmergencyMode>();
return ctx;
}

Expand All @@ -133,6 +137,7 @@ struct Context {
ctx->playbackController->playbackAccessSemaphore = std::make_unique<bell::WrappedSemaphore>(1);
ctx->playbackController->playbackAccessSemaphore->give();
ctx->eventBus = bus;
ctx->emergencyMode = std::make_shared<euph::EmergencyMode>();

#ifdef ESP_PLATFORM
ctx->rootPath = "/fs";
Expand Down

0 comments on commit d2a3de7

Please sign in to comment.