Checkpoint/switch/source/configuration.cpp

284 lines
8.4 KiB
C++
Raw Normal View History

2018-07-14 15:05:30 +00:00
/*
2019-05-06 20:51:09 +00:00
* This file is part of Checkpoint
* Copyright (C) 2017-2019 Bernardo Giordano, FlagBrew
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
2018-07-14 15:05:30 +00:00
#include "configuration.hpp"
2019-04-12 21:39:55 +00:00
static struct mg_mgr mgr;
2019-05-06 20:51:09 +00:00
static struct mg_connection* nc;
2019-04-12 21:39:55 +00:00
static struct mg_serve_http_opts s_http_server_opts;
2019-05-06 20:51:09 +00:00
static const char* s_http_port = "8000";
2019-04-12 21:39:55 +00:00
2019-05-06 20:51:09 +00:00
static void handle_populate(struct mg_connection* nc, struct http_message* hm)
2019-04-12 21:39:55 +00:00
{
2019-05-04 12:35:16 +00:00
// populate gets called at startup, assume a new connection has been started
blinkLed(2);
2019-05-06 20:51:09 +00:00
auto json = Configuration::getInstance().getJson();
auto map = getCompleteTitleList();
2019-04-12 21:39:55 +00:00
json["title_list"] = map;
2019-05-06 20:51:09 +00:00
std::string body = json.dump();
mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%.*s", (unsigned long)body.length(), (int)body.length(), body.c_str());
2019-04-12 21:39:55 +00:00
}
2019-05-06 20:51:09 +00:00
static void handle_save(struct mg_connection* nc, struct http_message* hm)
2019-04-12 21:39:55 +00:00
{
FILE* f = fopen(Configuration::getInstance().BASEPATH.c_str(), "w");
2019-05-06 20:51:09 +00:00
if (f != NULL) {
fwrite(hm->body.p, 1, hm->body.len, f);
fclose(f);
}
2019-04-12 21:39:55 +00:00
Configuration::getInstance().load();
Configuration::getInstance().parse();
// Send response
2019-05-06 20:51:09 +00:00
mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%.*s", (unsigned long)hm->body.len, (int)hm->body.len, hm->body.p);
2019-04-12 21:39:55 +00:00
}
2019-05-06 20:51:09 +00:00
static void ev_handler(struct mg_connection* nc, int ev, void* ev_data)
2019-04-12 21:39:55 +00:00
{
2019-05-06 20:51:09 +00:00
struct http_message* hm = (struct http_message*)ev_data;
2019-04-12 21:39:55 +00:00
switch (ev) {
case MG_EV_HTTP_REQUEST:
if (mg_vcmp(&hm->uri, "/save") == 0) {
handle_save(nc, hm);
2019-05-06 20:51:09 +00:00
}
else if (mg_vcmp(&hm->uri, "/populate") == 0) {
2019-04-12 21:39:55 +00:00
handle_populate(nc, hm);
2019-05-06 20:51:09 +00:00
}
else {
2019-04-12 21:39:55 +00:00
mg_serve_http(nc, hm, s_http_server_opts);
}
break;
default:
break;
}
}
2018-07-14 15:05:30 +00:00
Configuration::Configuration(void)
{
// check for existing config.json files on the sd card, BASEPATH
2019-05-06 20:51:09 +00:00
if (!io::fileExists(BASEPATH)) {
2018-07-14 15:05:30 +00:00
store();
}
2019-04-12 21:39:55 +00:00
load();
2018-07-14 15:05:30 +00:00
2019-07-09 20:08:48 +00:00
// check if json is valid
if (!mJson.is_object()) {
store();
}
bool updateJson = false;
2019-07-09 20:08:48 +00:00
if (mJson.find("version") == mJson.end()) {
// if config is present but is < 3.4.2, override it
2018-07-14 15:05:30 +00:00
store();
}
2019-05-06 20:51:09 +00:00
else {
2019-07-05 19:27:39 +00:00
if (mJson["version"] < CONFIG_VERSION) {
mJson["version"] = CONFIG_VERSION;
2019-07-05 19:58:49 +00:00
updateJson = true;
}
2019-07-05 19:27:39 +00:00
if (!(mJson.contains("pksm-bridge") && mJson["pksm-bridge"].is_boolean())) {
2019-07-05 19:08:15 +00:00
mJson["pksm-bridge"] = false;
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:08:15 +00:00
}
2019-07-05 19:27:39 +00:00
if (!(mJson.contains("ftp-enabled") && mJson["ftp-enabled"].is_boolean())) {
2019-07-05 19:08:15 +00:00
mJson["ftp-enabled"] = false;
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:27:39 +00:00
}
if (!(mJson.contains("filter") && mJson["filter"].is_array())) {
mJson["filter"] = nlohmann::json::array();
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:27:39 +00:00
}
if (!(mJson.contains("favorites") && mJson["favorites"].is_array())) {
mJson["favorites"] = nlohmann::json::array();
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:27:39 +00:00
}
if (!(mJson.contains("additional_save_folders") && mJson["additional_save_folders"].is_array())) {
mJson["additional_save_folders"] = nlohmann::json::array();
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:27:39 +00:00
}
// check every single entry in the arrays...
for (auto& obj : mJson["filter"]) {
if (!obj.is_string()) {
mJson["filter"] = nlohmann::json::array();
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:27:39 +00:00
break;
}
}
for (auto& obj : mJson["favorites"]) {
if (!obj.is_string()) {
mJson["favorites"] = nlohmann::json::array();
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:27:39 +00:00
break;
}
}
for (auto& obj : mJson["additional_save_folders"]) {
if (!obj.is_string()) {
mJson["additional_save_folders"] = nlohmann::json::array();
2019-07-05 19:58:49 +00:00
updateJson = true;
2019-07-05 19:27:39 +00:00
break;
}
2018-12-16 14:27:48 +00:00
}
}
2019-05-06 20:51:09 +00:00
if (updateJson) {
mJson["version"] = CONFIG_VERSION;
2019-04-12 21:39:55 +00:00
save();
}
2018-07-14 15:05:30 +00:00
2019-04-12 21:39:55 +00:00
parse();
// load server
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_set_protocol_http_websocket(nc);
s_http_server_opts.document_root = "romfs:/web_root";
2019-05-06 20:51:09 +00:00
s_http_server_opts.auth_domain = "flagbrew.org";
2019-04-12 21:39:55 +00:00
}
Configuration::~Configuration(void)
{
mg_mgr_free(&mgr);
}
void Configuration::store(void)
{
FILE* in = fopen("romfs:/config.json", "rt");
2019-05-06 20:51:09 +00:00
if (in != NULL) {
nlohmann::json src = nlohmann::json::parse(in, nullptr, false);
fclose(in);
std::string writeData = src.dump(2);
writeData.shrink_to_fit();
size_t size = writeData.size();
FILE* out = fopen(BASEPATH.c_str(), "wt");
2019-05-06 20:51:09 +00:00
if (out != NULL) {
fwrite(writeData.c_str(), 1, size, out);
fclose(out);
}
}
2019-04-12 21:39:55 +00:00
}
bool Configuration::filter(u64 id)
{
return mFilterIds.find(id) != mFilterIds.end();
}
bool Configuration::favorite(u64 id)
{
return mFavoriteIds.find(id) != mFavoriteIds.end();
}
std::vector<std::string> Configuration::additionalSaveFolders(u64 id)
{
std::vector<std::string> emptyvec;
auto folders = mAdditionalSaveFolders.find(id);
return folders == mAdditionalSaveFolders.end() ? emptyvec : folders->second;
}
bool Configuration::isPKSMBridgeEnabled(void)
{
return PKSMBridgeEnabled;
}
void Configuration::pollServer(void)
{
2019-05-06 20:51:09 +00:00
mg_mgr_poll(&mgr, 1000 / 60);
2019-04-12 21:39:55 +00:00
}
void Configuration::save(void)
{
std::string writeData = mJson.dump(2);
writeData.shrink_to_fit();
size_t size = writeData.size();
FILE* out = fopen(BASEPATH.c_str(), "wt");
2019-05-06 20:51:09 +00:00
if (out != NULL) {
fwrite(writeData.c_str(), 1, size, out);
fclose(out);
}
2019-04-12 21:39:55 +00:00
}
void Configuration::load(void)
{
FILE* in = fopen(BASEPATH.c_str(), "rt");
2019-05-06 20:51:09 +00:00
if (in != NULL) {
mJson = nlohmann::json::parse(in, nullptr, false);
fclose(in);
}
2019-04-12 21:39:55 +00:00
}
void Configuration::parse(void)
{
mFilterIds.clear();
mFavoriteIds.clear();
mAdditionalSaveFolders.clear();
2018-07-14 15:05:30 +00:00
// parse filters
std::vector<std::string> filter = mJson["filter"];
2019-05-06 20:51:09 +00:00
for (auto& id : filter) {
2018-07-14 15:05:30 +00:00
mFilterIds.emplace(strtoull(id.c_str(), NULL, 16));
}
2018-09-16 14:34:12 +00:00
// parse favorites
std::vector<std::string> favorites = mJson["favorites"];
2019-05-06 20:51:09 +00:00
for (auto& id : favorites) {
2018-09-16 14:34:12 +00:00
mFavoriteIds.emplace(strtoull(id.c_str(), NULL, 16));
}
2018-07-14 15:05:30 +00:00
// parse additional save folders
auto js = mJson["additional_save_folders"];
2019-05-06 20:51:09 +00:00
for (auto it = js.begin(); it != js.end(); ++it) {
2018-07-14 15:05:30 +00:00
std::vector<std::string> folders = it.value()["folders"];
std::vector<std::string> sfolders;
2019-05-06 20:51:09 +00:00
for (auto& folder : folders) {
2018-07-14 15:05:30 +00:00
sfolders.push_back(folder);
}
mAdditionalSaveFolders.emplace(strtoull(it.key().c_str(), NULL, 16), sfolders);
}
2018-12-16 14:27:48 +00:00
// parse PKSM Bridge flag
PKSMBridgeEnabled = mJson["pksm-bridge"];
2019-07-05 19:08:15 +00:00
// parse FTP flag
FTPEnabled = mJson["ftp-enabled"];
2018-07-14 15:05:30 +00:00
}
2019-04-12 21:39:55 +00:00
const char* Configuration::c_str(void)
2018-07-14 15:05:30 +00:00
{
2019-04-12 21:39:55 +00:00
return mJson.dump().c_str();
2018-12-16 14:27:48 +00:00
}
2019-04-12 21:39:55 +00:00
nlohmann::json Configuration::getJson(void)
2018-12-16 14:27:48 +00:00
{
2019-04-12 21:39:55 +00:00
return mJson;
2019-07-05 19:08:15 +00:00
}
bool Configuration::isFTPEnabled(void)
{
return FTPEnabled;
2018-07-14 15:05:30 +00:00
}