Persist all configuration settings across upgrades. Restart wifi-p2p if neccessary.

This commit is contained in:
Robin Davies
2024-08-27 14:55:48 -04:00
parent 722ef456bc
commit 06ebfb0bce
15 changed files with 340 additions and 201 deletions
+30 -23
View File
@@ -1,18 +1,18 @@
/*
* MIT License
*
*
* Copyright (c) 2022 Robin E. R. Davies
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -23,6 +23,7 @@
*/
#include "ServiceConfiguration.hpp"
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <grp.h>
@@ -35,43 +36,49 @@ using namespace pipedal;
using namespace std;
using namespace config_serializer;
const char ServiceConfiguration::DEVICEID_FILE_NAME[] = "/etc/pipedal/config/service.conf";
const char ServiceConfiguration::DEVICEID_FILE_NAME[] = "/var/pipedal/config/service.conf";
#define SERIALIZER_ENTRY(MEMBER_NAME) \
new ConfigSerializer<ServiceConfiguration,decltype(ServiceConfiguration::MEMBER_NAME)>(#MEMBER_NAME, &ServiceConfiguration::MEMBER_NAME, "")
new ConfigSerializer<ServiceConfiguration, decltype(ServiceConfiguration::MEMBER_NAME)>(#MEMBER_NAME, &ServiceConfiguration::MEMBER_NAME, "")
static p2p::autoptr_vector<ConfigSerializerBase<ServiceConfiguration>>
deviceIdSerializers
{
SERIALIZER_ENTRY(uuid),
SERIALIZER_ENTRY(deviceName),
SERIALIZER_ENTRY(server_port)
};
deviceIdSerializers{
SERIALIZER_ENTRY(uuid),
SERIALIZER_ENTRY(deviceName),
SERIALIZER_ENTRY(server_port)};
ServiceConfiguration::ServiceConfiguration()
: base(deviceIdSerializers)
: base(deviceIdSerializers)
{
filename = "/var/pipedal/config/service.conf";
}
void ServiceConfiguration::Load()
void ServiceConfiguration::Load(std::filesystem::path filename)
{
base::Load(DEVICEID_FILE_NAME,false);
this->filename = filename;
base::Load(filename, false);
}
void ServiceConfiguration::Save()
{
base::Save(DEVICEID_FILE_NAME);
{
struct group *group_;
auto directory = filename.parent_path();
std::filesystem::create_directories(directory);
// make sure the file has correct permissions.
std::ofstream t;
t.open(filename);
t.close();
struct group *group_;
group_ = getgrnam("pipedal_d");
if (group_ == nullptr)
{
throw logic_error("Group not found: pipedal_d");
}
std::ignore = chown(DEVICEID_FILE_NAME,-1,group_->gr_gid);
std::ignore = chmod(DEVICEID_FILE_NAME, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
std::ignore = chown(DEVICEID_FILE_NAME, -1, group_->gr_gid);
std::ignore = chmod(DEVICEID_FILE_NAME, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH);
}
base::Save(filename);
}
@@ -158,12 +158,12 @@ void WifiDirectConfigSettings::Load()
{
this->enable_ = false;
std::string strEnable;
if (ConfigUtil::GetConfigLine("/etc/pipedal/config/pipedal_p2pd.conf","enabled",&strEnable))
if (ConfigUtil::GetConfigLine("/var/pipedal/config/pipedal_p2pd.conf","enabled",&strEnable))
{
this->enable_ = (strEnable == "true" || strEnable == "1");
}
std::string strWlan;
if (ConfigUtil::GetConfigLine("/etc/pipedal/config/pipedal_p2pd.conf","wlan",&strWlan))
if (ConfigUtil::GetConfigLine("/var/pipedal/config/pipedal_p2pd.conf","wlan",&strWlan))
{
this->wlan_ = strWlan;
} else {
@@ -172,20 +172,20 @@ void WifiDirectConfigSettings::Load()
if (!ConfigUtil::GetConfigLine("/etc/pipedal/config/pipedal_p2pd.conf","p2p_device_name",&this->hotspotName_))
if (!ConfigUtil::GetConfigLine("/var/pipedal/config/pipedal_p2pd.conf","p2p_device_name",&this->hotspotName_))
{
this->hotspotName_ = "PiPedal";
}
if (!ConfigUtil::GetConfigLine("/etc/pipedal/config/pipedal_p2pd.conf","country_code",&this->countryCode_))
if (!ConfigUtil::GetConfigLine("/var/pipedal/config/pipedal_p2pd.conf","country_code",&this->countryCode_))
{
this->countryCode_ = "US";
}
if (!ConfigUtil::GetConfigLine("/etc/pipedal/config/pipedal_p2pd.conf","p2p_pin",&this->pin_))
if (!ConfigUtil::GetConfigLine("/var/pipedal/config/pipedal_p2pd.conf","p2p_pin",&this->pin_))
{
this->pin_ = "12345678";
}
if (!ConfigUtil::GetConfigLine("/etc/pipedal/config/pipedal_p2pd.conf","wifiChannel",&this->channel_))
if (!ConfigUtil::GetConfigLine("/var/pipedal/config/pipedal_p2pd.conf","wifiChannel",&this->channel_))
{
this->channel_ = "6";
}
@@ -26,6 +26,7 @@
#include <string>
#include "ConfigSerializer.hpp"
#include <filesystem>
namespace pipedal {
using namespace config_serializer;
@@ -38,12 +39,16 @@ namespace pipedal {
static const char DEVICEID_FILE_NAME[];
void Load();
void Load(
std::filesystem::path path = "/var/pipedal/config/service.conf"
);
void Save();
std::string uuid;
std::string deviceName = "PiPedal";
uint32_t server_port = 80;
private:
std::filesystem::path filename;
};
}