Skip to content

Commit dad506a

Browse files
author
Ubuntu
committed
Update Config Shadow to store config file updates persistently
1 parent 8ca0ddd commit dad506a

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

source/config/Config.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,7 @@ constexpr char Config::TAG[];
24912491
constexpr char Config::DEFAULT_CONFIG_DIR[];
24922492
constexpr char Config::DEFAULT_KEY_DIR[];
24932493
constexpr char Config::DEFAULT_CONFIG_FILE[];
2494+
constexpr char Config::DEFAULT_ROOT_CONFIG_FILE[];
24942495
constexpr char Config::CLI_HELP[];
24952496
constexpr char Config::CLI_VERSION[];
24962497
constexpr char Config::CLI_EXPORT_DEFAULT_SETTINGS[];
@@ -2884,7 +2885,7 @@ bool Config::ParseConfigFile(const string &file, ConfigFileType configFileType)
28842885
}
28852886

28862887
#if !defined(DISABLE_MQTT)
2887-
LOGM_INFO(TAG, "Successfully fetched JSON config file: %s", Sanitize(contents).c_str());
2888+
LOGM_INFO(TAG, "Successfully fetched JSON config file from %s : %s", expandedPath.c_str(), Sanitize(contents).c_str());
28882889
#endif
28892890
setting.close();
28902891

source/config/Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ namespace Aws
486486
static constexpr char DEFAULT_CONFIG_DIR[] = "~/.aws-iot-device-client/";
487487
static constexpr char DEFAULT_KEY_DIR[] = "~/.aws-iot-device-client/keys/";
488488
static constexpr char DEFAULT_CONFIG_FILE[] = "~/.aws-iot-device-client/aws-iot-device-client.conf";
489+
static constexpr char DEFAULT_ROOT_CONFIG_FILE[] = "/etc/.aws-iot-device-client/aws-iot-device-client.conf";
489490
static constexpr char DEFAULT_FLEET_PROVISIONING_RUNTIME_CONFIG_FILE[] =
490491
"~/.aws-iot-device-client/aws-iot-device-client-runtime.conf";
491492
static constexpr char DEFAULT_HTTP_PROXY_CONFIG_FILE[] = "~/.aws-iot-device-client/http-proxy.conf";

source/shadow/ConfigShadow.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,31 @@ using namespace Aws::Iotshadow;
1919
using namespace Aws::Iot::DeviceClient::Shadow;
2020
using namespace Aws::Iot::DeviceClient::Util;
2121
using namespace Aws::Iot::DeviceClient::Logging;
22+
using namespace Aws::Iot::DeviceClient;
2223

2324
constexpr char ConfigShadow::TAG[];
2425
constexpr char ConfigShadow::DEFAULT_CONFIG_SHADOW_NAME[];
2526
constexpr int ConfigShadow::DEFAULT_WAIT_TIME_SECONDS;
27+
28+
void ConfigShadow::updateLocalConfigFile(PlainConfig &config, const char* configFilePath)const
29+
{
30+
ofstream configFile(configFilePath);
31+
JsonObject jsonObj;
32+
// Convert config to JSON
33+
config.SerializeToObject(jsonObj);
34+
35+
if (configFile.is_open())
36+
{
37+
configFile << jsonObj.View().WriteReadable();
38+
configFile.close();
39+
LOGM_INFO(TAG, "Updated configuration saved locally to disk at: %s", configFilePath);
40+
}
41+
else
42+
{
43+
LOGM_WARN(TAG, "Failed to open config file: %s, Error: %s", configFilePath, strerror(errno));
44+
}
45+
}
46+
2647
void ConfigShadow::getNamedShadowRejectedHandler(Iotshadow::ErrorResponse *errorResponse, int ioError)
2748
{
2849
if (ioError)
@@ -352,6 +373,17 @@ void ConfigShadow::resetClientConfigWithJSON(
352373
PlainConfig::JSON_KEY_SAMPLE_SHADOW);
353374
}
354375
}
376+
377+
// Save the updated configuration to a local file for persistence
378+
JsonObject jsonObj;
379+
config.SerializeToObject(jsonObj);
380+
381+
const char* jsonStr = jsonObj.View().WriteReadable().c_str();
382+
LOGM_INFO(TAG, "Updating device configuration files with the following: %s", jsonStr);
383+
384+
string userConfig = FileUtils::ExtractExpandedPath(Config::DEFAULT_CONFIG_FILE);
385+
updateLocalConfigFile(config, userConfig.c_str());
386+
updateLocalConfigFile(config, Config::DEFAULT_ROOT_CONFIG_FILE);
355387
}
356388

357389
void ConfigShadow::updateShadowWithLocalConfig(Iotshadow::IotShadowClient iotShadowClient, PlainConfig &config)

source/shadow/ConfigShadow.h

+8
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ namespace Aws
200200
* @param jsonObj contains all device client features' configuration
201201
*/
202202
void loadFeatureConfigIntoJsonObject(PlainConfig &config, Aws::Crt::JsonObject &jsonObj) const;
203+
/**
204+
* \brief Updates the local configuration file of device client with the config shadow update
205+
* received from the AWS IOT core for persistence of the new device configuration
206+
* @param config device client local configuration
207+
* @param configFilePath file path of the local aws-iot-device-client.conf
208+
*/
209+
void updateLocalConfigFile(PlainConfig &config, const char* configFilePath) const;
210+
203211
};
204212
} // namespace Shadow
205213
} // namespace DeviceClient

0 commit comments

Comments
 (0)