Skip to content

Commit 16d9b2a

Browse files
authored
[posix] add runtime configuration file path support (openthread#11514)
This commit enhances the POSIX platform configuration system to support dynamic configuration file paths at runtime instead of only build-time paths. ConfigFile class: - Replace const char* mFilePath with char mFilePath[kFilePathMaxSize] to allow dynamic path updates after construction - Add SetFilePath() and GetFilePath() methods - Update constructor to use SetFilePath() with proper bounds checking Configuration class: - Add SetFactoryConfigFile() and SetProductConfigFile() methods to update factory and product config file paths Radio URL parameter support: - Add support for 'product-config-file' parameter in radio URL - Add support for 'factory-config-file' parameter in radio URL
1 parent 3a31754 commit 16d9b2a

6 files changed

Lines changed: 98 additions & 5 deletions

File tree

src/posix/platform/config_file.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@
4343
namespace ot {
4444
namespace Posix {
4545

46-
ConfigFile::ConfigFile(const char *aFilePath)
47-
: mFilePath(aFilePath)
46+
ConfigFile::ConfigFile(const char *aFilePath) { SetFilePath(aFilePath); }
47+
48+
void ConfigFile::SetFilePath(const char *aFilePath)
4849
{
49-
assert(mFilePath != nullptr);
50-
VerifyOrDie(strlen(mFilePath) + strlen(kSwapSuffix) < kFilePathMaxSize, OT_EXIT_FAILURE);
50+
assert(aFilePath != nullptr);
51+
VerifyOrDie(strlen(aFilePath) + strlen(kSwapSuffix) < kFilePathMaxSize, OT_EXIT_FAILURE);
52+
strncpy(mFilePath, aFilePath, kFilePathMaxSize - 1);
53+
mFilePath[kFilePathMaxSize - 1] = '\0';
5154
}
5255

5356
bool ConfigFile::HasKey(const char *aKey) const

src/posix/platform/config_file.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ class ConfigFile
5050
*/
5151
explicit ConfigFile(const char *aFilePath);
5252

53+
/**
54+
* Sets the configuration file path.
55+
*
56+
* @param[in] aFilePath A pointer to the null-terminated file path.
57+
*/
58+
void SetFilePath(const char *aFilePath);
59+
60+
/**
61+
* Gets the configuration file path.
62+
*
63+
* @returns A pointer to the null-terminated file path.
64+
*/
65+
const char *GetFilePath(void) const { return mFilePath; }
66+
5367
/**
5468
* Gets a configuration from the configuration file.
5569
*
@@ -113,7 +127,7 @@ class ConfigFile
113127

114128
void Strip(char *aString) const;
115129

116-
const char *mFilePath;
130+
char mFilePath[kFilePathMaxSize];
117131
};
118132

119133
} // namespace Posix

src/posix/platform/configuration.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ const char Configuration::kKeySupportedChannelMask[] = "supported_channel_mask";
5454
const char Configuration::kKeyPreferredChannelMask[] = "preferred_channel_mask";
5555
const char Configuration::kCommaDelimiter[] = ",";
5656

57+
otError Configuration::SetFactoryConfigFile(const char *aFilePath)
58+
{
59+
otError error = OT_ERROR_NONE;
60+
61+
VerifyOrExit(aFilePath != nullptr, error = OT_ERROR_INVALID_ARGS);
62+
mFactoryConfigFile.SetFilePath(aFilePath);
63+
LogInfo("Factory configuration file path set to: %s", mFactoryConfigFile.GetFilePath());
64+
65+
exit:
66+
if (error == OT_ERROR_INVALID_ARGS)
67+
{
68+
LogCrit("Failed to set factory config file: %s", otThreadErrorToString(error));
69+
}
70+
return error;
71+
}
72+
73+
otError Configuration::SetProductConfigFile(const char *aFilePath)
74+
{
75+
otError error = OT_ERROR_NONE;
76+
77+
VerifyOrExit(aFilePath != nullptr, error = OT_ERROR_INVALID_ARGS);
78+
mProductConfigFile.SetFilePath(aFilePath);
79+
LogInfo("Product configuration file path set to: %s", mProductConfigFile.GetFilePath());
80+
81+
exit:
82+
if (error == OT_ERROR_INVALID_ARGS)
83+
{
84+
LogCrit("Failed to set product config file: %s", otThreadErrorToString(error));
85+
}
86+
return error;
87+
}
88+
5789
otError Configuration::SetRegion(uint16_t aRegionCode)
5890
{
5991
otError error = OT_ERROR_NONE;

src/posix/platform/configuration.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ class Configuration : public Logger<Configuration>
6666
{
6767
}
6868

69+
/**
70+
* @brief Sets the path for the factory configuration file.
71+
*
72+
* @param[in] aFilePath A null-terminated C string representing the new path to the
73+
* factory configuration file. This parameter MUST NOT be `nullptr`.
74+
*
75+
* @retval OT_ERROR_NONE The factory configuration file path was successfully updated.
76+
* @retval OT_ERROR_INVALID_ARGS If @p aFilePath is `nullptr`.
77+
*/
78+
otError SetFactoryConfigFile(const char *aFilePath);
79+
80+
/**
81+
* @brief Sets the path for the product configuration file.
82+
*
83+
* @param[in] aFilePath A null-terminated C string representing the new path to the
84+
* product configuration file. This parameter MUST NOT be `nullptr`.
85+
*
86+
* @retval OT_ERROR_NONE The product configuration file path was successfully updated.
87+
* @retval OT_ERROR_INVALID_ARGS If @p aFilePath is `nullptr`.
88+
*/
89+
otError SetProductConfigFile(const char *aFilePath);
90+
6991
/**
7092
* Set the region code.
7193
*

src/posix/platform/radio.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ void Radio::ProcessRadioUrl(const RadioUrl &aRadioUrl)
170170
mRadioSpinel.SetBusLatency(busLatency);
171171
}
172172

173+
#if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
174+
if (aRadioUrl.HasParam("product-config-file"))
175+
{
176+
const char *configFile = aRadioUrl.GetValue("product-config-file");
177+
SuccessOrDie(sConfig.SetProductConfigFile(configFile));
178+
}
179+
180+
if (aRadioUrl.HasParam("factory-config-file"))
181+
{
182+
const char *configFile = aRadioUrl.GetValue("factory-config-file");
183+
SuccessOrDie(sConfig.SetFactoryConfigFile(configFile));
184+
}
185+
#endif // OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
186+
173187
ProcessMaxPowerTable(aRadioUrl);
174188

175189
#if OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE

src/posix/platform/radio_url.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ const char *otSysGetRadioUrlHelpString(void)
130130
" Upto three IIDs can be provided with each IID separated by ',' \n"
131131
" e.g. iid-list=1,2,3 \n"
132132
#endif
133+
#if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
134+
" product-config-file[=path] Specify a custom path to the openthread.conf product configuration\n"
135+
" file.\n"
136+
" If not specified, the default path set at build time is used.\n"
137+
" factory-config-file[=path] Specify a custom path to the openthread.conf factory configuration\n"
138+
" file.\n"
139+
" If not specified, the default path set at build time is used.\n"
140+
#endif // OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
133141
;
134142
}
135143

0 commit comments

Comments
 (0)