-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EmergencyMode class, add it to ctx
This will be used for a fallback mechanism if the filesystem gets corrupted.
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters