mirror of
https://github.com/espressif/openthread.git
synced 2026-10-04 06:47:36 +00:00
[settings] add option to store settings in volatile storage (#3811)
This commit is contained in:
committed by
Jonathan Hui
parent
2266b5eb27
commit
5d6ed3dd71
@@ -53,6 +53,7 @@ MAC_FILTER ?= 0
|
|||||||
MTD_NETDIAG ?= 0
|
MTD_NETDIAG ?= 0
|
||||||
PLATFORM_UDP ?= 0
|
PLATFORM_UDP ?= 0
|
||||||
SERVICE ?= 0
|
SERVICE ?= 0
|
||||||
|
SETTINGS_RAM ?= 0
|
||||||
# SLAAC is enabled by default
|
# SLAAC is enabled by default
|
||||||
SLAAC ?= 1
|
SLAAC ?= 1
|
||||||
SNTP_CLIENT ?= 0
|
SNTP_CLIENT ?= 0
|
||||||
@@ -195,6 +196,10 @@ CFLAGS += -DOPENTHREAD_CONFIG_LOG_OUTPUT=OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UA
|
|||||||
CXXFLAGS += -DOPENTHREAD_CONFIG_LOG_OUTPUT=OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART
|
CXXFLAGS += -DOPENTHREAD_CONFIG_LOG_OUTPUT=OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(SETTINGS_RAM),1)
|
||||||
|
COMMONCFLAGS += -DOPENTHREAD_SETTINGS_RAM=1
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(FULL_LOGS),1)
|
ifeq ($(FULL_LOGS),1)
|
||||||
# HINT: Add more here, or comment out ones you do not need/want
|
# HINT: Add more here, or comment out ones you do not need/want
|
||||||
LOG_FLAGS += -DOPENTHREAD_CONFIG_LOG_LEVEL=OT_LOG_LEVEL_DEBG
|
LOG_FLAGS += -DOPENTHREAD_CONFIG_LOG_LEVEL=OT_LOG_LEVEL_DEBG
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ libopenthread_qpg6095_a_SOURCES = \
|
|||||||
uart.c \
|
uart.c \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
|
libopenthread_qpg6095_a_LIBADD = \
|
||||||
|
$(top_builddir)/examples/platforms/utils/libopenthread_platform_utils_a-settings_ram.o
|
||||||
|
|
||||||
if OPENTHREAD_ENABLE_DIAG
|
if OPENTHREAD_ENABLE_DIAG
|
||||||
libopenthread_qpg6095_a_SOURCES += \
|
libopenthread_qpg6095_a_SOURCES += \
|
||||||
|
|||||||
@@ -42,6 +42,8 @@
|
|||||||
|
|
||||||
#include "settings_qorvo.h"
|
#include "settings_qorvo.h"
|
||||||
|
|
||||||
|
#if !OPENTHREAD_SETTINGS_RAM
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* Public Function Definitions
|
* Public Function Definitions
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
@@ -53,24 +55,6 @@ void otPlatSettingsInit(otInstance *aInstance)
|
|||||||
qorvoSettingsInit();
|
qorvoSettingsInit();
|
||||||
}
|
}
|
||||||
|
|
||||||
otError otPlatSettingsBeginChange(otInstance *aInstance)
|
|
||||||
{
|
|
||||||
OT_UNUSED_VARIABLE(aInstance);
|
|
||||||
return OT_ERROR_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
otError otPlatSettingsCommitChange(otInstance *aInstance)
|
|
||||||
{
|
|
||||||
OT_UNUSED_VARIABLE(aInstance);
|
|
||||||
return OT_ERROR_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
otError otPlatSettingsAbandonChange(otInstance *aInstance)
|
|
||||||
{
|
|
||||||
OT_UNUSED_VARIABLE(aInstance);
|
|
||||||
return OT_ERROR_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength)
|
otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength)
|
||||||
{
|
{
|
||||||
otError error = OT_ERROR_NOT_FOUND;
|
otError error = OT_ERROR_NOT_FOUND;
|
||||||
@@ -136,3 +120,5 @@ void otPlatSettingsWipe(otInstance *aInstance)
|
|||||||
qorvoSettingsWipe();
|
qorvoSettingsWipe();
|
||||||
otPlatSettingsInit(aInstance);
|
otPlatSettingsInit(aInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* OPENTHREAD_SETTINGS_RAM */
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ libopenthread_platform_utils_a_CPPFLAGS = \
|
|||||||
libopenthread_platform_utils_a_SOURCES = \
|
libopenthread_platform_utils_a_SOURCES = \
|
||||||
debug_uart.c \
|
debug_uart.c \
|
||||||
logging_rtt.c \
|
logging_rtt.c \
|
||||||
|
settings_ram.c \
|
||||||
settings_flash.c \
|
settings_flash.c \
|
||||||
soft_source_match_table.c \
|
soft_source_match_table.c \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|||||||
@@ -101,6 +101,8 @@ struct settingsBlock
|
|||||||
#error "Invalid value for `SETTINGS_CONFIG_PAGE_NUM` (should be >= 2)"
|
#error "Invalid value for `SETTINGS_CONFIG_PAGE_NUM` (should be >= 2)"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !OPENTHREAD_SETTINGS_RAM
|
||||||
|
|
||||||
static uint32_t sSettingsBaseAddress;
|
static uint32_t sSettingsBaseAddress;
|
||||||
static uint32_t sSettingsUsedSize;
|
static uint32_t sSettingsUsedSize;
|
||||||
|
|
||||||
@@ -446,3 +448,5 @@ void otPlatSettingsWipe(otInstance *aInstance)
|
|||||||
initSettings(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_USE);
|
initSettings(sSettingsBaseAddress, (uint32_t)OT_SETTINGS_IN_USE);
|
||||||
otPlatSettingsInit(aInstance);
|
otPlatSettingsInit(aInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif /* OPENTHREAD_SETTINGS_RAM */
|
||||||
|
|||||||
@@ -0,0 +1,229 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019, The OpenThread Authors.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the copyright holder nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* This file implements OpenThread platform abstraction for storage of settings in RAM.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <openthread-core-config.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <openthread/instance.h>
|
||||||
|
#include <openthread/platform/settings.h>
|
||||||
|
|
||||||
|
#define SETTINGS_BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
#if OPENTHREAD_SETTINGS_RAM
|
||||||
|
|
||||||
|
static uint8_t sSettingsBuf[SETTINGS_BUFFER_SIZE];
|
||||||
|
static uint16_t sSettingsBufLength;
|
||||||
|
|
||||||
|
struct settingsBlock
|
||||||
|
{
|
||||||
|
uint16_t key;
|
||||||
|
uint16_t length;
|
||||||
|
};
|
||||||
|
|
||||||
|
// settings API
|
||||||
|
void otPlatSettingsInit(otInstance *aInstance)
|
||||||
|
{
|
||||||
|
OT_UNUSED_VARIABLE(aInstance);
|
||||||
|
|
||||||
|
sSettingsBufLength = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength)
|
||||||
|
{
|
||||||
|
OT_UNUSED_VARIABLE(aInstance);
|
||||||
|
|
||||||
|
uint16_t i = 0;
|
||||||
|
uint16_t valueLength = 0;
|
||||||
|
uint16_t readLength;
|
||||||
|
int currentIndex = 0;
|
||||||
|
const struct settingsBlock *currentBlock;
|
||||||
|
otError error = OT_ERROR_NOT_FOUND;
|
||||||
|
|
||||||
|
while (i < sSettingsBufLength)
|
||||||
|
{
|
||||||
|
currentBlock = (struct settingsBlock *)&sSettingsBuf[i];
|
||||||
|
|
||||||
|
if (aKey == currentBlock->key)
|
||||||
|
{
|
||||||
|
if (currentIndex == aIndex)
|
||||||
|
{
|
||||||
|
readLength = currentBlock->length;
|
||||||
|
|
||||||
|
// Perform read only if an input buffer was passed in
|
||||||
|
if (aValue != NULL && aValueLength != NULL)
|
||||||
|
{
|
||||||
|
// Adjust read length if input buffer size is smaller
|
||||||
|
if (readLength > *aValueLength)
|
||||||
|
{
|
||||||
|
readLength = *aValueLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(aValue, &sSettingsBuf[i + sizeof(struct settingsBlock)], readLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
valueLength = currentBlock->length;
|
||||||
|
error = OT_ERROR_NONE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += sizeof(struct settingsBlock) + currentBlock->length;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aValueLength != NULL)
|
||||||
|
{
|
||||||
|
*aValueLength = valueLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
|
||||||
|
{
|
||||||
|
uint16_t i = 0;
|
||||||
|
uint16_t currentBlockLength;
|
||||||
|
uint16_t nextBlockStart;
|
||||||
|
const struct settingsBlock *currentBlock;
|
||||||
|
|
||||||
|
// Delete all entries of aKey
|
||||||
|
while (i < sSettingsBufLength)
|
||||||
|
{
|
||||||
|
currentBlock = (struct settingsBlock *)&sSettingsBuf[i];
|
||||||
|
currentBlockLength = sizeof(struct settingsBlock) + currentBlock->length;
|
||||||
|
|
||||||
|
if (aKey == currentBlock->key)
|
||||||
|
{
|
||||||
|
nextBlockStart = i + currentBlockLength;
|
||||||
|
|
||||||
|
if (nextBlockStart < sSettingsBufLength)
|
||||||
|
{
|
||||||
|
memmove(&sSettingsBuf[i], &sSettingsBuf[nextBlockStart], sSettingsBufLength - nextBlockStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(sSettingsBufLength >= currentBlockLength);
|
||||||
|
sSettingsBufLength -= currentBlockLength;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
i += currentBlockLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return otPlatSettingsAdd(aInstance, aKey, aValue, aValueLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
|
||||||
|
{
|
||||||
|
OT_UNUSED_VARIABLE(aInstance);
|
||||||
|
|
||||||
|
otError error;
|
||||||
|
struct settingsBlock *currentBlock;
|
||||||
|
const uint16_t newBlockLength = sizeof(struct settingsBlock) + aValueLength;
|
||||||
|
|
||||||
|
if (sSettingsBufLength + newBlockLength <= sizeof(sSettingsBuf))
|
||||||
|
{
|
||||||
|
currentBlock = (struct settingsBlock *)&sSettingsBuf[sSettingsBufLength];
|
||||||
|
currentBlock->key = aKey;
|
||||||
|
currentBlock->length = aValueLength;
|
||||||
|
|
||||||
|
memcpy(&sSettingsBuf[sSettingsBufLength + sizeof(struct settingsBlock)], aValue, aValueLength);
|
||||||
|
sSettingsBufLength += newBlockLength;
|
||||||
|
|
||||||
|
error = OT_ERROR_NONE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
error = OT_ERROR_NO_BUFS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex)
|
||||||
|
{
|
||||||
|
OT_UNUSED_VARIABLE(aInstance);
|
||||||
|
|
||||||
|
uint16_t i = 0;
|
||||||
|
int currentIndex = 0;
|
||||||
|
uint16_t nextBlockStart;
|
||||||
|
uint16_t currentBlockLength;
|
||||||
|
const struct settingsBlock *currentBlock;
|
||||||
|
otError error = OT_ERROR_NOT_FOUND;
|
||||||
|
|
||||||
|
while (i < sSettingsBufLength)
|
||||||
|
{
|
||||||
|
currentBlock = (struct settingsBlock *)&sSettingsBuf[i];
|
||||||
|
currentBlockLength = sizeof(struct settingsBlock) + currentBlock->length;
|
||||||
|
|
||||||
|
if (aKey == currentBlock->key)
|
||||||
|
{
|
||||||
|
if (currentIndex == aIndex)
|
||||||
|
{
|
||||||
|
nextBlockStart = i + currentBlockLength;
|
||||||
|
|
||||||
|
if (nextBlockStart < sSettingsBufLength)
|
||||||
|
{
|
||||||
|
memmove(&sSettingsBuf[i], &sSettingsBuf[nextBlockStart], sSettingsBufLength - nextBlockStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(sSettingsBufLength >= currentBlockLength);
|
||||||
|
sSettingsBufLength -= currentBlockLength;
|
||||||
|
|
||||||
|
error = OT_ERROR_NONE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
currentIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i += currentBlockLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
void otPlatSettingsWipe(otInstance *aInstance)
|
||||||
|
{
|
||||||
|
otPlatSettingsInit(aInstance);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // OPENTHREAD_SETTINGS_RAM
|
||||||
@@ -130,6 +130,8 @@ otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint
|
|||||||
* The given setting was changed or staged.
|
* The given setting was changed or staged.
|
||||||
* @retval OT_ERROR_NOT_IMPLEMENTED
|
* @retval OT_ERROR_NOT_IMPLEMENTED
|
||||||
* This function is not implemented on this platform.
|
* This function is not implemented on this platform.
|
||||||
|
* @retval OT_ERROR_NO_BUFS
|
||||||
|
* No space remaining to store the given setting.
|
||||||
*/
|
*/
|
||||||
otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
|
otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
|
||||||
|
|
||||||
@@ -163,6 +165,8 @@ otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *a
|
|||||||
* The given setting was added or staged to be added.
|
* The given setting was added or staged to be added.
|
||||||
* @retval OT_ERROR_NOT_IMPLEMENTED
|
* @retval OT_ERROR_NOT_IMPLEMENTED
|
||||||
* This function is not implemented on this platform.
|
* This function is not implemented on this platform.
|
||||||
|
* @retval OT_ERROR_NO_BUFS
|
||||||
|
* No space remaining to store the given setting.
|
||||||
*/
|
*/
|
||||||
otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
|
otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user