[flash] integrate flash driver with build option (#4552)

This commit is contained in:
Jonathan Hui
2020-03-18 20:51:41 -07:00
parent b3d05af6ff
commit 8220981b6e
5 changed files with 215 additions and 18 deletions
+1
View File
@@ -75,6 +75,7 @@ Instance::Instance(void)
#if OPENTHREAD_MTD || OPENTHREAD_FTD
, mNotifier(*this)
, mSettings(*this)
, mSettingsDriver(*this)
, mMessagePool(*this)
, mIp6(*this)
, mThreadNetif(*this)
+9 -3
View File
@@ -323,9 +323,10 @@ private:
// Notifier, Settings, and MessagePool are initialized before
// other member variables since other classes/objects from their
// constructor may use them.
Notifier mNotifier;
Settings mSettings;
MessagePool mMessagePool;
Notifier mNotifier;
Settings mSettings;
SettingsDriver mSettingsDriver;
MessagePool mMessagePool;
Ip6::Ip6 mIp6;
ThreadNetif mThreadNetif;
@@ -390,6 +391,11 @@ template <> inline Settings &Instance::Get(void)
return mSettings;
}
template <> inline SettingsDriver &Instance::Get(void)
{
return mSettingsDriver;
}
template <> inline MeshForwarder &Instance::Get(void)
{
return mThreadNetif.mMeshForwarder;
+97 -15
View File
@@ -37,6 +37,7 @@
#include "common/code_utils.hpp"
#include "common/instance.hpp"
#include "common/locator-getters.hpp"
#include "common/logging.hpp"
#include "meshcop/dataset.hpp"
#include "thread/mle.hpp"
@@ -94,19 +95,105 @@ void SettingsBase::LogFailure(otError error, const char *aText, bool aIsDelete)
// LCOV_EXCL_STOP
void Settings::Init(void)
#if !OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE
SettingsDriver::SettingsDriver(Instance &aInstance)
: InstanceLocator(aInstance)
{
}
void SettingsDriver::Init(void)
{
otPlatSettingsInit(&GetInstance());
}
void Settings::Deinit(void)
void SettingsDriver::Deinit(void)
{
otPlatSettingsDeinit(&GetInstance());
}
void Settings::Wipe(void)
otError SettingsDriver::Add(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
{
return otPlatSettingsAdd(&GetInstance(), aKey, aValue, aValueLength);
}
otError SettingsDriver::Delete(uint16_t aKey, int aIndex)
{
return otPlatSettingsDelete(&GetInstance(), aKey, aIndex);
}
otError SettingsDriver::Get(uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) const
{
return otPlatSettingsGet(&GetInstance(), aKey, aIndex, aValue, aValueLength);
}
otError SettingsDriver::Set(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
{
return otPlatSettingsSet(&GetInstance(), aKey, aValue, aValueLength);
}
void SettingsDriver::Wipe(void)
{
otPlatSettingsWipe(&GetInstance());
}
#else // !OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE
SettingsDriver::SettingsDriver(Instance &aInstance)
: InstanceLocator(aInstance)
, mFlash(aInstance)
{
}
void SettingsDriver::Init(void)
{
mFlash.Init();
}
void SettingsDriver::Deinit(void)
{
}
otError SettingsDriver::Add(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
{
return mFlash.Add(aKey, aValue, aValueLength);
}
otError SettingsDriver::Delete(uint16_t aKey, int aIndex)
{
return mFlash.Delete(aKey, aIndex);
}
otError SettingsDriver::Get(uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) const
{
return mFlash.Get(aKey, aIndex, aValue, aValueLength);
}
otError SettingsDriver::Set(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
{
return mFlash.Set(aKey, aValue, aValueLength);
}
void SettingsDriver::Wipe(void)
{
mFlash.Wipe();
}
#endif // !OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE
void Settings::Init(void)
{
Get<SettingsDriver>().Init();
}
void Settings::Deinit(void)
{
Get<SettingsDriver>().Deinit();
}
void Settings::Wipe(void)
{
Get<SettingsDriver>().Wipe();
otLogInfoCore("Non-volatile: Wiped all info");
}
@@ -286,7 +373,7 @@ otError Settings::ChildInfoIterator::Delete(void)
otError error = OT_ERROR_NONE;
VerifyOrExit(!mIsDone, error = OT_ERROR_INVALID_STATE);
SuccessOrExit(error = otPlatSettingsDelete(&GetInstance(), kKeyChildInfo, mIndex));
SuccessOrExit(error = Get<SettingsDriver>().Delete(kKeyChildInfo, mIndex));
LogChildInfo("Removed", mChildInfo);
exit:
@@ -300,8 +387,8 @@ void Settings::ChildInfoIterator::Read(void)
otError error;
mChildInfo.Init();
SuccessOrExit(error = otPlatSettingsGet(&GetInstance(), kKeyChildInfo, mIndex,
reinterpret_cast<uint8_t *>(&mChildInfo), &length));
SuccessOrExit(
error = Get<SettingsDriver>().Get(kKeyChildInfo, mIndex, reinterpret_cast<uint8_t *>(&mChildInfo), &length));
LogChildInfo("Read", mChildInfo);
exit:
@@ -310,27 +397,22 @@ exit:
otError Settings::Read(Key aKey, void *aBuffer, uint16_t &aSize) const
{
otError error;
SuccessOrExit(error = otPlatSettingsGet(&GetInstance(), aKey, 0, reinterpret_cast<uint8_t *>(aBuffer), &aSize));
exit:
return error;
return Get<SettingsDriver>().Get(aKey, 0, reinterpret_cast<uint8_t *>(aBuffer), &aSize);
}
otError Settings::Save(Key aKey, const void *aValue, uint16_t aSize)
{
return otPlatSettingsSet(&GetInstance(), aKey, reinterpret_cast<const uint8_t *>(aValue), aSize);
return Get<SettingsDriver>().Set(aKey, reinterpret_cast<const uint8_t *>(aValue), aSize);
}
otError Settings::Add(Key aKey, const void *aValue, uint16_t aSize)
{
return otPlatSettingsAdd(&GetInstance(), aKey, reinterpret_cast<const uint8_t *>(aValue), aSize);
return Get<SettingsDriver>().Add(aKey, reinterpret_cast<const uint8_t *>(aValue), aSize);
}
otError Settings::Delete(Key aKey)
{
return otPlatSettingsDelete(&GetInstance(), aKey, -1);
return Get<SettingsDriver>().Delete(aKey, -1);
}
} // namespace ot
+96
View File
@@ -39,6 +39,7 @@
#include "common/encoding.hpp"
#include "common/locator.hpp"
#include "mac/mac_types.hpp"
#include "utils/flash.hpp"
#if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE
#include "utils/slaac_address.hpp"
#endif
@@ -49,6 +50,101 @@ namespace MeshCoP {
class Dataset;
}
class SettingsDriver : public InstanceLocator
{
public:
/**
* Constructor.
*/
explicit SettingsDriver(Instance &aInstance);
/**
* This method initializes the settings storage driver.
*
*/
void Init(void);
/**
* This method deinitializes the settings driver.
*
*/
void Deinit(void);
/**
* This method adds a value to @p aKey.
*
* @param[in] aKey The key associated with the value.
* @param[in] aValue A pointer to where the new value of the setting should be read from.
* MUST NOT be NULL if @p aValueLength is non-zero.
* @param[in] aValueLength The length of the data pointed to by @p aValue. May be zero.
*
* @retval OT_ERROR_NONE The value was added.
* @retval OT_ERROR_NO_BUFS Not enough space to store the value.
*
*/
otError Add(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
/**
* This method removes a value from @p aKey.
*
* @param[in] aKey The key associated with the value.
* @param[in] aIndex The index of the value to be removed.
* If set to -1, all values for @p aKey will be removed.
*
* @retval OT_ERROR_NONE The given key and index was found and removed successfully.
* @retval OT_ERROR_NOT_FOUND The given key or index was not found.
*
*/
otError Delete(uint16_t aKey, int aIndex);
/**
* This method fetches the value identified by @p aKey.
*
* @param[in] aKey The key associated with the requested value.
* @param[in] aIndex The index of the specific item to get.
* @param[out] aValue A pointer to where the value of the setting should be written.
* May be NULL if just testing for the presence or length of a key.
* @param[inout] aValueLength A pointer to the length of the value.
* When called, this should point to an integer containing the maximum bytes that
* can be written to @p aValue.
* At return, the actual length of the setting is written.
* May be NULL if performing a presence check.
*
* @retval OT_ERROR_NONE The value was fetched successfully.
* @retval OT_ERROR_NOT_FOUND The key was not found.
*
*/
otError Get(uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) const;
/**
* This method sets or replaces the value identified by @p aKey.
*
* If there was more than one value previously associated with @p aKey, then they are all deleted and replaced with
* this single entry.
*
* @param[in] aKey The key associated with the value.
* @param[in] aValue A pointer to where the new value of the setting should be read from.
* MUST NOT be NULL if @p aValueLength is non-zero.
* @param[in] aValueLength The length of the data pointed to by @p aValue. May be zero.
*
* @retval OT_ERROR_NONE The value was changed.
* @retval OT_ERROR_NO_BUFS Not enough space to store the value.
*
*/
otError Set(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
/**
* This method remves all values.
*
*/
void Wipe(void);
#if OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE
private:
Flash mFlash;
#endif
};
/**
* This class defines the base class used by `Settings` and `Settings::ChildInfoIterator`.
*
@@ -332,6 +332,18 @@
#define OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH "tmp"
#endif
/**
* @def OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE
*
* Define to 1 to enable otPlatFlash* APIs to support non-volatile storage.
*
* When defined to 1, the platform MUST implement the otPlatFlash* APIs instead of the otPlatSettings* APIs.
*
*/
#ifndef OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE
#define OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE 0
#endif
/**
* @def OPENTHREAD_CONFIG_FAILED_CHILD_TRANSMISSIONS
*