Remove otPlatFlash API's and move settings from core to examples. (#889)

* Remove otPlatFlash API's and move settings from core to examples.
This commit is contained in:
pvanhorn
2016-10-28 18:42:19 -07:00
committed by Jonathan Hui
parent 43f2ba4db6
commit 85fb7a451c
25 changed files with 221 additions and 379 deletions
-1
View File
@@ -170,7 +170,6 @@
<ClCompile Include="..\..\tests\unit\test_message.cpp" />
<ClCompile Include="..\..\tests\unit\test_ncp_buffer.cpp" />
<ClCompile Include="..\..\tests\unit\test_platform.cpp" />
<ClCompile Include="..\..\tests\unit\test_settings.cpp" />
<ClCompile Include="..\..\tests\unit\test_timer.cpp" />
<ClCompile Include="..\..\tests\unit\test_toolchain.cpp" />
<ClCompile Include="..\..\tests\unit\test_util.cpp" />
@@ -36,9 +36,6 @@
<ClCompile Include="..\..\tests\unit\test_message.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tests\unit\test_settings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\tests\unit\test_timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -28,6 +28,7 @@
<ClCompile Include="..\..\examples\platforms\posix\random.c" />
<ClCompile Include="..\..\examples\platforms\posix\spi-stubs.c" />
<ClCompile Include="..\..\examples\platforms\posix\uart-windows.c" />
<ClCompile Include="..\..\examples\platforms\utils\settings.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\examples\platforms\posix\platform-posix.h" />
@@ -37,6 +37,9 @@
<ClCompile Include="..\..\examples\platforms\posix\uart-windows.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\examples\platforms\utils\settings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\examples\platforms\posix\platform-posix.h">
-1
View File
@@ -25,7 +25,6 @@
<ClCompile Include="..\..\src\core\common\crc16.cpp" />
<ClCompile Include="..\..\src\core\common\logging.cpp" />
<ClCompile Include="..\..\src\core\common\message.cpp" />
<ClCompile Include="..\..\src\core\common\settings.cpp" />
<ClCompile Include="..\..\src\core\common\tasklet.cpp" />
<ClCompile Include="..\..\src\core\common\timer.cpp" />
<ClCompile Include="..\..\src\core\common\trickle_timer.cpp" />
@@ -72,9 +72,6 @@
<ClCompile Include="..\..\src\core\common\message.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\common\settings.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\common\tasklet.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
@@ -72,9 +72,6 @@
<ClCompile Include="..\..\src\core\common\message.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\common\settings.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\common\tasklet.cpp">
<Filter>Source Files\common</Filter>
</ClCompile>
+4
View File
@@ -35,6 +35,10 @@ DIST_SUBDIRS = \
posix \
$(NULL)
EXTRA_DIST = \
utils \
$(NULL)
# Always build (e.g. for 'make all') these subdirectories.
if OPENTHREAD_EXAMPLES_POSIX
+8 -1
View File
@@ -36,6 +36,12 @@ libopenthread_cc2538_a_CPPFLAGS = \
-I$(top_srcdir)/src/core \
$(NULL)
libopenthread_cc2538_settings.cpp: $(srcdir)/../utils/settings.cpp
cp $? $@
CLEANFILES = libopenthread_cc2538_settings.cpp
libopenthread_cc2538_a_SOURCES = \
alarm.c \
flash.c \
@@ -46,6 +52,7 @@ libopenthread_cc2538_a_SOURCES = \
random.c \
startup-gcc.c \
uart.c \
libopenthread_cc2538_settings.cpp \
$(NULL)
if OPENTHREAD_ENABLE_DIAG
@@ -61,7 +68,7 @@ noinst_HEADERS = \
$(NULL)
if OPENTHREAD_BUILD_COVERAGE
CLEANFILES = $(wildcard *.gcda *.gcno)
CLEANFILES += $(wildcard *.gcda *.gcno)
endif # OPENTHREAD_BUILD_COVERAGE
include $(abs_top_nlbuild_autotools_dir)/automake/post.am
+10 -10
View File
@@ -33,7 +33,7 @@
#include <openthread-config.h>
#include <platform/alarm.h>
#include <platform/flash.h>
#include <utils/flash.h>
#include <common/code_utils.hpp>
#include "platform-cc2538.h"
@@ -71,25 +71,25 @@ static ThreadError romStatusToThread(int32_t aStatus)
return error;
}
ThreadError otPlatFlashInit(void)
ThreadError utilsFlashInit(void)
{
return kThreadError_None;
}
uint32_t otPlatFlashGetSize(void)
uint32_t utilsFlashGetSize(void)
{
uint32_t reg = (HWREG(FLASH_CTRL_DIECFG0) & 0x00000070) >> 4;
return reg ? (0x20000 * reg) : 0x10000;
}
ThreadError otPlatFlashErasePage(uint32_t aAddress)
ThreadError utilsFlashErasePage(uint32_t aAddress)
{
ThreadError error = kThreadError_None;
int32_t status;
uint32_t address;
VerifyOrExit(aAddress < otPlatFlashGetSize(), error = kThreadError_InvalidArgs);
VerifyOrExit(aAddress < utilsFlashGetSize(), error = kThreadError_InvalidArgs);
address = FLASH_BASE + aAddress - (aAddress & (FLASH_PAGE_SIZE - 1));
status = ROM_PageErase(address, FLASH_PAGE_SIZE);
@@ -99,7 +99,7 @@ exit:
return error;
}
ThreadError otPlatFlashStatusWait(uint32_t aTimeout)
ThreadError utilsFlashStatusWait(uint32_t aTimeout)
{
ThreadError error = kThreadError_None;
uint32_t start = otPlatAlarmGetNow();
@@ -116,14 +116,14 @@ exit:
return error;
}
uint32_t otPlatFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
int32_t status;
uint32_t busy = 1;
uint32_t *data;
uint32_t size = 0;
VerifyOrExit(((aAddress + aSize) < otPlatFlashGetSize()) &&
VerifyOrExit(((aAddress + aSize) < utilsFlashGetSize()) &&
(!(aAddress & 3)) && (!(aSize & 3)), aSize = 0);
data = (uint32_t *)(aData);
@@ -147,11 +147,11 @@ exit:
return size;
}
uint32_t otPlatFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
uint32_t size = 0;
VerifyOrExit((aAddress + aSize) < otPlatFlashGetSize(), ;);
VerifyOrExit((aAddress + aSize) < utilsFlashGetSize(), ;);
while (size < aSize)
{
-2
View File
@@ -33,7 +33,6 @@
*/
#include <platform/uart.h>
#include <platform/flash.h>
#include "platform-cc2538.h"
otInstance *sInstance;
@@ -44,7 +43,6 @@ void PlatformInit(int argc, char *argv[])
cc2538RandomInit();
cc2538RadioInit();
otPlatUartEnable();
otPlatFlashInit();
(void)argc;
(void)argv;
+7 -1
View File
@@ -37,6 +37,11 @@ libopenthread_posix_a_CPPFLAGS = \
-D_GNU_SOURCE \
$(NULL)
libopenthread_posix_settings.cpp: $(srcdir)/../utils/settings.cpp
cp $? $@
CLEANFILES = libopenthread_posix_settings.cpp
libopenthread_posix_a_SOURCES = \
alarm.c \
misc.c \
@@ -46,6 +51,7 @@ libopenthread_posix_a_SOURCES = \
random.c \
uart-posix.c \
flash.c \
libopenthread_posix_settings.cpp \
$(NULL)
if OPENTHREAD_ENABLE_DIAG
@@ -65,7 +71,7 @@ noinst_HEADERS = \
$(NULL)
if OPENTHREAD_BUILD_COVERAGE
CLEANFILES = $(wildcard *.gcda *.gcno)
CLEANFILES += $(wildcard *.gcda *.gcno)
endif # OPENTHREAD_BUILD_COVERAGE
include $(abs_top_nlbuild_autotools_dir)/automake/post.am
@@ -28,32 +28,32 @@
#include "platform-posix.h"
ThreadError otPlatFlashInit(void)
ThreadError utilsFlashInit(void)
{
return kThreadError_NotImplemented;
}
uint32_t otPlatFlashGetSize(void)
uint32_t utilsFlashGetSize(void)
{
return 0;
}
ThreadError otPlatFlashErasePage(uint32_t aAddress)
ThreadError utilsFlashErasePage(uint32_t aAddress)
{
return kThreadError_NotImplemented;
}
ThreadError otPlatFlashStatusWait(uint32_t aTimeout)
ThreadError utilsFlashStatusWait(uint32_t aTimeout)
{
return kThreadError_None;
}
uint32_t otPlatFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
return 0;
}
uint32_t otPlatFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
return 0;
}
+9 -9
View File
@@ -36,7 +36,7 @@
#include <string.h>
#include <openthread-config.h>
#include <platform/flash.h>
#include <utils/flash.h>
#include <common/code_utils.hpp>
@@ -50,7 +50,7 @@ enum
FLASH_PAGE_NUM = 128,
};
ThreadError otPlatFlashInit(void)
ThreadError utilsFlashInit(void)
{
ThreadError error = kThreadError_None;
char fileName[20];
@@ -83,7 +83,7 @@ ThreadError otPlatFlashInit(void)
{
for (uint16_t index = 0; index < FLASH_PAGE_NUM; index++)
{
SuccessOrExit(error = otPlatFlashErasePage(index * FLASH_PAGE_SIZE));
SuccessOrExit(error = utilsFlashErasePage(index * FLASH_PAGE_SIZE));
}
}
@@ -91,12 +91,12 @@ exit:
return error;
}
uint32_t otPlatFlashGetSize(void)
uint32_t utilsFlashGetSize(void)
{
return FLASH_SIZE;
}
ThreadError otPlatFlashErasePage(uint32_t aAddress)
ThreadError utilsFlashErasePage(uint32_t aAddress)
{
ThreadError error = kThreadError_None;
uint8_t buf = 0xff;
@@ -117,13 +117,13 @@ exit:
return error;
}
ThreadError otPlatFlashStatusWait(uint32_t aTimeout)
ThreadError utilsFlashStatusWait(uint32_t aTimeout)
{
(void)aTimeout;
return kThreadError_None;
}
uint32_t otPlatFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
uint32_t ret = 0;
uint32_t index = 0;
@@ -133,7 +133,7 @@ uint32_t otPlatFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
for (index = 0; index < aSize; index++)
{
VerifyOrExit((ret = otPlatFlashRead(aAddress + index, &byte, 1)) == 1, ;);
VerifyOrExit((ret = utilsFlashRead(aAddress + index, &byte, 1)) == 1, ;);
// Use bitwise AND to emulate the behavior of flash memory
byte &= aData[index];
VerifyOrExit((ret = (uint32_t)pwrite(sFlashFd, &byte, 1, aAddress + index)) == 1, ;);
@@ -143,7 +143,7 @@ exit:
return index;
}
uint32_t otPlatFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
{
uint32_t ret = 0;
-2
View File
@@ -43,7 +43,6 @@
#include <openthread.h>
#include <platform/alarm.h>
#include <platform/flash.h>
#include <platform/uart.h>
uint32_t NODE_ID = 1;
@@ -69,7 +68,6 @@ void PlatformInit(int argc, char *argv[])
platformAlarmInit();
platformRadioInit();
platformRandomInit();
otPlatFlashInit();
}
bool UartInitialized = false;
@@ -29,11 +29,11 @@
/**
* @file
* @brief
* This file defines flash interface.
* This file defines the flash interface used by settings.cpp.
*/
#ifndef OT_PLATFORM_FLASH_H
#define OT_PLATFORM_FLASH_H
#ifndef UTILS_FLASH_H
#define UTILS_FLASH_H
#include <stdint.h>
@@ -49,7 +49,7 @@ extern "C" {
* @retval ::kThreadError_None Initialize flash driver success.
* @retval ::kThreadError_Failed Initialize flash driver fail.
*/
ThreadError otPlatFlashInit(void);
ThreadError utilsFlashInit(void);
/**
* Get the size of flash that can be read/write by the caller.
@@ -57,11 +57,11 @@ ThreadError otPlatFlashInit(void);
*
* @returns The size of the flash.
*/
uint32_t otPlatFlashGetSize(void);
uint32_t utilsFlashGetSize(void);
/**
* Erase one flash page that include the input address.
* This is a non-blocking function. It can work with otPlatFlashStatusWait to check when erase is done.
* This is a non-blocking function. It can work with utilsFlashStatusWait to check when erase is done.
*
* The flash address starts from 0, and this function maps the input address to the physical address of flash for erasing.
* 0 is always mapped to the beginning of one flash page.
@@ -73,7 +73,7 @@ uint32_t otPlatFlashGetSize(void);
* @retval kThreadError_Failed Erase flash operation is not started.
* @retval kThreadError_InvalidArgs aAddress is out of range of flash or not aligend.
*/
ThreadError otPlatFlashErasePage(uint32_t aAddress);
ThreadError utilsFlashErasePage(uint32_t aAddress);
/**
* Check whether flash is ready or busy.
@@ -85,7 +85,7 @@ ThreadError otPlatFlashErasePage(uint32_t aAddress);
* @retval kThreadError_None Flash is ready for any operation.
* @retval kThreadError_Busy Flash is busy.
*/
ThreadError otPlatFlashStatusWait(uint32_t aTimeout);
ThreadError utilsFlashStatusWait(uint32_t aTimeout);
/**
* Write flash. The write operation only clears bits, but never set bits.
@@ -102,7 +102,7 @@ ThreadError otPlatFlashStatusWait(uint32_t aTimeout);
* It is expected the same as aSize, and may be less than aSize.
* 0 indicates that something wrong happens when writing.
*/
uint32_t otPlatFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize);
uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize);
/**
* Read flash.
@@ -119,10 +119,10 @@ uint32_t otPlatFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize);
* It is expected the same as aSize, and may be less than aSize.
* 0 indicates that something wrong happens when reading.
*/
uint32_t otPlatFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize);
uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // OT_PLATFORM_FLASH_H
#endif // UTILS_FLASH_H
@@ -39,11 +39,10 @@
#include <openthread-types.h>
#include <openthread-core-config.h>
#include <openthread-instance.h>
#include <common/code_utils.hpp>
#include <platform/settings.h>
#include <platform/flash.h>
#include <utils/flash.h>
#ifdef __cplusplus
extern "C" {
@@ -76,6 +75,40 @@ struct settingsBlock
uint16_t reserved;
} OT_TOOL_PACKED_END;
/**
* @def SETTINGS_CONFIG_BASE_ADDRESS
*
* The base address of settings.
*
*/
#ifndef SETTINGS_CONFIG_BASE_ADDRESS
#define SETTINGS_CONFIG_BASE_ADDRESS 0x39000
#endif // SETTINGS_CONFIG_BASE_ADDRESS
/**
* @def SETTINGS_CONFIG_PAGE_SIZE
*
* The page size of settings.
*
*/
#ifndef SETTINGS_CONFIG_PAGE_SIZE
#define SETTINGS_CONFIG_PAGE_SIZE 0x800
#endif // SETTINGS_CONFIG_PAGE_SIZE
/**
* @def SETTINGS_CONFIG_PAGE_NUM
*
* The page number of settings.
*
*/
#ifndef SETTINGS_CONFIG_PAGE_NUM
#define SETTINGS_CONFIG_PAGE_NUM 2
#endif // SETTINGS_CONFIG_PAGE_NUM
static uint32_t sSettingsBaseAddress;
static uint32_t sSettingsUsedSize;
static uint16_t getAlignLength(uint16_t length)
{
return (length + 3) & 0xfffc;
@@ -83,21 +116,21 @@ static uint16_t getAlignLength(uint16_t length)
static void setSettingsFlag(uint32_t aBase, uint32_t aFlag)
{
otPlatFlashWrite(aBase, reinterpret_cast<uint8_t *>(&aFlag), sizeof(aFlag));
utilsFlashWrite(aBase, reinterpret_cast<uint8_t *>(&aFlag), sizeof(aFlag));
}
static void initSettings(uint32_t aBase, uint32_t aFlag)
{
uint32_t address = aBase;
uint32_t settingsSize = OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM > 1 ?
OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE * OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM / 2 :
OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE;
uint32_t settingsSize = SETTINGS_CONFIG_PAGE_NUM > 1 ?
SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2 :
SETTINGS_CONFIG_PAGE_SIZE;
while (address < (aBase + settingsSize))
{
otPlatFlashErasePage(address);
otPlatFlashStatusWait(1000);
address += OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE;
utilsFlashErasePage(address);
utilsFlashStatusWait(1000);
address += SETTINGS_CONFIG_PAGE_SIZE;
}
setSettingsFlag(aBase, aFlag);
@@ -105,21 +138,23 @@ static void initSettings(uint32_t aBase, uint32_t aFlag)
static uint32_t swapSettingsBlock(otInstance *aInstance)
{
uint32_t oldBase = aInstance->mSettingsBaseAddress;
uint32_t oldBase = sSettingsBaseAddress;
uint32_t swapAddress = oldBase;
uint32_t usedSize = aInstance->mSettingsUsedSize;
uint8_t pageNum = OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM;
uint32_t settingsSize = pageNum > 1 ? OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE * pageNum / 2 :
OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE;
uint32_t usedSize = sSettingsUsedSize;
uint8_t pageNum = SETTINGS_CONFIG_PAGE_NUM;
uint32_t settingsSize = pageNum > 1 ? SETTINGS_CONFIG_PAGE_SIZE * pageNum / 2 :
SETTINGS_CONFIG_PAGE_SIZE;
(void)aInstance;
VerifyOrExit(pageNum > 1, ;);
aInstance->mSettingsBaseAddress = (swapAddress == OPENTHREAD_CONFIG_SETTINGS_BASE_ADDRESS) ?
sSettingsBaseAddress = (swapAddress == SETTINGS_CONFIG_BASE_ADDRESS) ?
(swapAddress + settingsSize) :
OPENTHREAD_CONFIG_SETTINGS_BASE_ADDRESS;
SETTINGS_CONFIG_BASE_ADDRESS;
initSettings(aInstance->mSettingsBaseAddress, static_cast<uint32_t>(kSettingsInSwap));
aInstance->mSettingsUsedSize = kSettingsFlagSize;
initSettings(sSettingsBaseAddress, static_cast<uint32_t>(kSettingsInSwap));
sSettingsUsedSize = kSettingsFlagSize;
swapAddress += kSettingsFlagSize;
while (swapAddress < (oldBase + usedSize))
@@ -132,7 +167,7 @@ static uint32_t swapSettingsBlock(otInstance *aInstance)
} OT_TOOL_PACKED_END addBlock;
bool valid = true;
otPlatFlashRead(swapAddress, reinterpret_cast<uint8_t *>(&addBlock.block), sizeof(struct settingsBlock));
utilsFlashRead(swapAddress, reinterpret_cast<uint8_t *>(&addBlock.block), sizeof(struct settingsBlock));
swapAddress += sizeof(struct settingsBlock);
if (!(addBlock.block.flag & kBlockAddCompleteFlag) && (addBlock.block.flag & kBlockDeleteFlag))
@@ -143,7 +178,7 @@ static uint32_t swapSettingsBlock(otInstance *aInstance)
{
struct settingsBlock block;
otPlatFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
utilsFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
if (!(block.flag & kBlockAddCompleteFlag) && (block.flag & kBlockDeleteFlag) &&
!(block.flag & kBlockIndex0Flag) && (block.key == addBlock.block.key))
@@ -157,11 +192,11 @@ static uint32_t swapSettingsBlock(otInstance *aInstance)
if (valid)
{
otPlatFlashRead(swapAddress, addBlock.data, getAlignLength(addBlock.block.length));
otPlatFlashWrite(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize,
reinterpret_cast<uint8_t *>(&addBlock),
getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock));
aInstance->mSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length));
utilsFlashRead(swapAddress, addBlock.data, getAlignLength(addBlock.block.length));
utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize,
reinterpret_cast<uint8_t *>(&addBlock),
getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock));
sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length));
}
}
else if (addBlock.block.flag == 0xff)
@@ -172,11 +207,11 @@ static uint32_t swapSettingsBlock(otInstance *aInstance)
swapAddress += getAlignLength(addBlock.block.length);
}
setSettingsFlag(aInstance->mSettingsBaseAddress, static_cast<uint32_t>(kSettingsInUse));
setSettingsFlag(sSettingsBaseAddress, static_cast<uint32_t>(kSettingsInUse));
setSettingsFlag(oldBase, static_cast<uint32_t>(kSettingsNotUse));
exit:
return settingsSize - aInstance->mSettingsUsedSize;
return settingsSize - sSettingsUsedSize;
}
static ThreadError addSetting(otInstance *aInstance, uint16_t aKey, bool aIndex0, const uint8_t *aValue,
@@ -189,9 +224,9 @@ static ThreadError addSetting(otInstance *aInstance, uint16_t aKey, bool aIndex0
struct settingsBlock block;
uint8_t data[kSettingsBlockDataSize];
} OT_TOOL_PACKED_END addBlock;
uint32_t settingsSize = OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM > 1 ?
OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE * OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM / 2 :
OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE;
uint32_t settingsSize = SETTINGS_CONFIG_PAGE_NUM > 1 ?
SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2 :
SETTINGS_CONFIG_PAGE_SIZE;
addBlock.block.flag = 0xff;
addBlock.block.key = aKey;
@@ -204,28 +239,28 @@ static ThreadError addSetting(otInstance *aInstance, uint16_t aKey, bool aIndex0
addBlock.block.flag &= (~kBlockAddBeginFlag);
addBlock.block.length = aValueLength;
if ((aInstance->mSettingsUsedSize + getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)) >=
if ((sSettingsUsedSize + getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)) >=
settingsSize)
{
VerifyOrExit(swapSettingsBlock(aInstance) >= (getAlignLength(addBlock.block.length) + sizeof(struct settingsBlock)),
error = kThreadError_NoBufs);
}
otPlatFlashWrite(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize,
reinterpret_cast<uint8_t *>(&addBlock.block),
sizeof(struct settingsBlock));
utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize,
reinterpret_cast<uint8_t *>(&addBlock.block),
sizeof(struct settingsBlock));
memset(addBlock.data, 0xff, kSettingsBlockDataSize);
memcpy(addBlock.data, aValue, addBlock.block.length);
otPlatFlashWrite(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize + sizeof(struct settingsBlock),
reinterpret_cast<uint8_t *>(addBlock.data), getAlignLength(addBlock.block.length));
utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize + sizeof(struct settingsBlock),
reinterpret_cast<uint8_t *>(addBlock.data), getAlignLength(addBlock.block.length));
addBlock.block.flag &= (~kBlockAddCompleteFlag);
otPlatFlashWrite(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize,
reinterpret_cast<uint8_t *>(&addBlock.block),
sizeof(struct settingsBlock));
aInstance->mSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length));
utilsFlashWrite(sSettingsBaseAddress + sSettingsUsedSize,
reinterpret_cast<uint8_t *>(&addBlock.block),
sizeof(struct settingsBlock));
sSettingsUsedSize += (sizeof(struct settingsBlock) + getAlignLength(addBlock.block.length));
exit:
return error;
@@ -235,17 +270,22 @@ exit:
void otPlatSettingsInit(otInstance *aInstance)
{
uint8_t index;
uint32_t settingsSize = OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM > 1 ?
OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE * OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM / 2 :
OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE;
aInstance->mSettingsBaseAddress = OPENTHREAD_CONFIG_SETTINGS_BASE_ADDRESS;
uint32_t settingsSize = SETTINGS_CONFIG_PAGE_NUM > 1 ?
SETTINGS_CONFIG_PAGE_SIZE * SETTINGS_CONFIG_PAGE_NUM / 2 :
SETTINGS_CONFIG_PAGE_SIZE;
(void)aInstance;
sSettingsBaseAddress = SETTINGS_CONFIG_BASE_ADDRESS;
utilsFlashInit();
for (index = 0; index < 2; index++)
{
uint32_t blockFlag;
aInstance->mSettingsBaseAddress += settingsSize * index;
otPlatFlashRead(aInstance->mSettingsBaseAddress, reinterpret_cast<uint8_t *>(&blockFlag), sizeof(blockFlag));
sSettingsBaseAddress += settingsSize * index;
utilsFlashRead(sSettingsBaseAddress, reinterpret_cast<uint8_t *>(&blockFlag), sizeof(blockFlag));
if (blockFlag == kSettingsInUse)
{
@@ -255,21 +295,21 @@ void otPlatSettingsInit(otInstance *aInstance)
if (index == 2)
{
initSettings(aInstance->mSettingsBaseAddress, static_cast<uint32_t>(kSettingsInUse));
initSettings(sSettingsBaseAddress, static_cast<uint32_t>(kSettingsInUse));
}
aInstance->mSettingsUsedSize = kSettingsFlagSize;
sSettingsUsedSize = kSettingsFlagSize;
while (aInstance->mSettingsUsedSize < settingsSize)
while (sSettingsUsedSize < settingsSize)
{
struct settingsBlock block;
otPlatFlashRead(aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize,
reinterpret_cast<uint8_t *>(&block), sizeof(block));
utilsFlashRead(sSettingsBaseAddress + sSettingsUsedSize,
reinterpret_cast<uint8_t *>(&block), sizeof(block));
if (!(block.flag & kBlockAddBeginFlag))
{
aInstance->mSettingsUsedSize += (getAlignLength(block.length) + sizeof(struct settingsBlock));
sSettingsUsedSize += (getAlignLength(block.length) + sizeof(struct settingsBlock));
}
else
{
@@ -299,14 +339,16 @@ ThreadError otPlatSettingsAbandonChange(otInstance *aInstance)
ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength)
{
ThreadError error = kThreadError_NotFound;
uint32_t address = aInstance->mSettingsBaseAddress + kSettingsFlagSize;
uint32_t address = sSettingsBaseAddress + kSettingsFlagSize;
int index = 0;
while (address < (aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize))
(void)aInstance;
while (address < (sSettingsBaseAddress + sSettingsUsedSize))
{
struct settingsBlock block;
otPlatFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
utilsFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
if (block.key == aKey)
{
@@ -329,7 +371,7 @@ ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex,
if (aValue)
{
VerifyOrExit(aValueLength, error = kThreadError_InvalidArgs);
otPlatFlashRead(address + sizeof(struct settingsBlock), aValue, block.length);
utilsFlashRead(address + sizeof(struct settingsBlock), aValue, block.length);
}
}
@@ -361,14 +403,16 @@ ThreadError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_
ThreadError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex)
{
ThreadError error = kThreadError_NotFound;
uint32_t address = aInstance->mSettingsBaseAddress + kSettingsFlagSize;
uint32_t address = sSettingsBaseAddress + kSettingsFlagSize;
int index = 0;
while (address < (aInstance->mSettingsBaseAddress + aInstance->mSettingsUsedSize))
(void)aInstance;
while (address < (sSettingsBaseAddress + sSettingsUsedSize))
{
struct settingsBlock block;
otPlatFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
utilsFlashRead(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
if (block.key == aKey)
{
@@ -383,13 +427,13 @@ ThreadError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aInde
{
error = kThreadError_None;
block.flag &= (~kBlockDeleteFlag);
otPlatFlashWrite(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
utilsFlashWrite(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
}
if (index == 1 && aIndex == 0)
{
block.flag &= (~kBlockIndex0Flag);
otPlatFlashWrite(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
utilsFlashWrite(address, reinterpret_cast<uint8_t *>(&block), sizeof(block));
}
index++;
@@ -404,7 +448,7 @@ ThreadError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aInde
void otPlatSettingsWipe(otInstance *aInstance)
{
initSettings(aInstance->mSettingsBaseAddress, static_cast<uint32_t>(kSettingsInUse));
initSettings(sSettingsBaseAddress, static_cast<uint32_t>(kSettingsInUse));
otPlatSettingsInit(aInstance);
}
-1
View File
@@ -39,7 +39,6 @@ ot_platform_headers =\
random.h \
uart.h \
spi-slave.h \
flash.h \
settings.h \
messagepool.h \
toolchain.h \
+1 -2
View File
@@ -45,7 +45,6 @@ libopenthread_a_SOURCES = \
common/message.cpp \
common/tasklet.cpp \
common/timer.cpp \
common/settings.cpp \
common/trickle_timer.cpp \
crypto/aes_ccm.cpp \
crypto/aes_ecb.cpp \
@@ -125,8 +124,8 @@ noinst_HEADERS = \
common/encoding.hpp \
common/logging.hpp \
common/message.hpp \
common/new.hpp \
common/settings.hpp \
common/new.hpp \
common/tasklet.hpp \
common/timer.hpp \
common/trickle_timer.hpp \
-30
View File
@@ -342,34 +342,4 @@
*/
#define OPENTHREAD_CONFIG_LOG_NETDIAG
/**
* @def OPENTHREAD_CONFIG_SETTINGS_BASE_ADDRESS
*
* The base address of settings.
*
*/
#ifndef OPENTHREAD_CONFIG_SETTINGS_BASE_ADDRESS
#define OPENTHREAD_CONFIG_SETTINGS_BASE_ADDRESS 0x39000
#endif // OPENTHREAD_CONFIG_SETTINGS_BASE_ADDRESS
/**
* @def OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE
*
* The page size of settings.
*
*/
#ifndef OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE
#define OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE 0x800
#endif // OPENTHREAD_CONFIG_SETTINGS_PAGE_SIZE
/**
* @def OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM
*
* The page number of settings.
*
*/
#ifndef OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM
#define OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM 2
#endif // OPENTHREAD_CONFIG_SETTINGS_PAGE_NUM
#endif // OPENTHREAD_CORE_DEFAULT_CONFIG_H_
-7
View File
@@ -77,13 +77,6 @@ typedef struct otInstance
Thread::Ip6::Ip6 mIp6;
Thread::ThreadNetif mThreadNetif;
//
// Platform specific variables
//
uint32_t mSettingsBaseAddress;
uint32_t mSettingsUsedSize;
// Constructor
otInstance(void);
-4
View File
@@ -78,7 +78,6 @@ check_PROGRAMS = \
test-link-quality \
test-mac-frame \
test-message \
test-settings \
test-timer \
test-toolchain \
$(NULL)
@@ -137,9 +136,6 @@ test_message_SOURCES = test_platform.cpp test_message.cpp
test_ncp_buffer_LDADD = $(COMMON_LDADD)
test_ncp_buffer_SOURCES = test_platform.cpp test_ncp_buffer.cpp
test_settings_LDADD = $(COMMON_LDADD)
test_settings_SOURCES = test_platform.cpp test_settings.cpp
test_timer_LDADD = $(COMMON_LDADD)
test_timer_SOURCES = test_platform.cpp test_timer.cpp
+55 -55
View File
@@ -61,14 +61,6 @@ uint32_t sCallCount[kCallCountIndexMax];
bool sDiagMode = false;
enum
{
kFlashSize = 0x40000,
kFlashPageSize = 0x800,
};
uint8_t sFlashBuffer[kFlashSize];
extern "C" {
void otSignalTaskletPending(otInstance *)
@@ -311,71 +303,79 @@ exit:
}
//
// Flash
// Settings
//
ThreadError otPlatFlashInit(void)
void otPlatSettingsInit(otInstance *aInstance)
{
memset(sFlashBuffer, 0xff, kFlashSize);
(void)aInstance;
}
ThreadError otPlatSettingsBeginChange(otInstance *aInstance)
{
(void)aInstance;
return kThreadError_None;
}
uint32_t otPlatFlashGetSize(void)
ThreadError otPlatSettingsCommitChange(otInstance *aInstance)
{
return kFlashSize;
}
(void)aInstance;
ThreadError otPlatFlashErasePage(uint32_t aAddress)
{
ThreadError error = kThreadError_None;
uint32_t address;
VerifyOrExit(aAddress < kFlashSize, error = kThreadError_InvalidArgs);
// Get start address of the flash page that includes aAddress
address = aAddress & (~(uint32_t)(kFlashPageSize - 1));
memset(sFlashBuffer + address, 0xff, kFlashPageSize);
exit:
return error;
}
ThreadError otPlatFlashStatusWait(uint32_t aTimeout)
{
(void)aTimeout;
return kThreadError_None;
}
uint32_t otPlatFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
ThreadError otPlatSettingsAbandonChange(otInstance *aInstance)
{
uint32_t ret = 0;
uint8_t byte;
(void)aInstance;
VerifyOrExit(aAddress < kFlashSize, ;);
for (uint32_t index = 0; index < aSize; index++)
{
byte = sFlashBuffer[aAddress + index];
byte &= aData[index];
sFlashBuffer[aAddress + index] = byte;
}
ret = aSize;
exit:
return ret;
return kThreadError_None;
}
uint32_t otPlatFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize)
ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue,
uint16_t *aValueLength)
{
uint32_t ret = 0;
(void)aInstance;
(void)aKey;
(void)aIndex;
(void)aValue;
(void)aValueLength;
VerifyOrExit(aAddress < kFlashSize, ;);
return kThreadError_None;
}
memcpy(aData, sFlashBuffer + aAddress, aSize);
ret = aSize;
ThreadError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
{
(void)aInstance;
(void)aKey;
(void)aValue;
(void)aValueLength;
exit:
return ret;
return kThreadError_None;
}
ThreadError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
{
(void)aInstance;
(void)aKey;
(void)aValue;
(void)aValueLength;
return kThreadError_None;
}
ThreadError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex)
{
(void)aInstance;
(void)aKey;
(void)aIndex;
return kThreadError_None;
}
void otPlatSettingsWipe(otInstance *aInstance)
{
(void)aInstance;
}
} // extern "C"
-159
View File
@@ -1,159 +0,0 @@
/*
* Copyright (c) 2016, 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.
*/
#include "test_util.h"
#include <string.h>
#include <openthread.h>
#include <common/debug.hpp>
#include <openthread-instance.h>
#include <platform/flash.h>
#include <platform/settings.h>
enum
{
kMaxStageDataLen = 32,
};
static uint8_t sWriteBuffer[kMaxStageDataLen];
static int sWriteBufferLength;
static otInstance sInstance;
void TestSettingsInit(void)
{
uint8_t index;
otPlatFlashInit();
otPlatSettingsInit(&sInstance);
otPlatSettingsWipe(&sInstance);
for (index = 0; index < kMaxStageDataLen; index++)
{
sWriteBuffer[index] = index;
}
sWriteBufferLength = index;
}
void TestSettingsAdd(void)
{
uint8_t key = 7;
uint8_t readBuffer[kMaxStageDataLen];
uint16_t readBufferLength;
VerifyOrQuit(otPlatSettingsAdd(&sInstance, key, sWriteBuffer, sWriteBufferLength) == kThreadError_None,
"Settings::Add::Add Fail\n");
VerifyOrQuit(otPlatSettingsGet(&sInstance, key, 0, readBuffer, &readBufferLength) == kThreadError_None,
"Settings::Add::Get Fail\n");
VerifyOrQuit(!memcmp(readBuffer, sWriteBuffer, static_cast<uint16_t>(sWriteBufferLength)),
"Settings::Add::Add Check Fail\n");
}
void TestSettingsDelete(void)
{
uint8_t key = 8;
uint8_t readBuffer[kMaxStageDataLen];
uint16_t readBufferLength;
VerifyOrQuit(otPlatSettingsAdd(&sInstance, key, sWriteBuffer, sWriteBufferLength) == kThreadError_None,
"Settings::Delete::Add Fail\n");
VerifyOrQuit(otPlatSettingsGet(&sInstance, key, 0, readBuffer, &readBufferLength) == kThreadError_None,
"Settings::Delete::Get Fail\n");
VerifyOrQuit(!memcmp(readBuffer, sWriteBuffer, static_cast<uint16_t>(sWriteBufferLength)),
"Settings::Delete::Add Check Fail\n");
VerifyOrQuit(otPlatSettingsDelete(&sInstance, key, -1) == kThreadError_None, "Settings::Delete::Delete Fail\n");
VerifyOrQuit(otPlatSettingsGet(&sInstance, key, 0, readBuffer, &readBufferLength) == kThreadError_NotFound,
"Settings::Delete::Get Fail\n");
}
void TestSettingsSet(void)
{
uint8_t key = 9;
uint8_t readBuffer[kMaxStageDataLen];
uint16_t readBufferLength;
for (uint8_t index = 0; index < 2; index++)
{
VerifyOrQuit(otPlatSettingsAdd(&sInstance, key, sWriteBuffer, sWriteBufferLength) == kThreadError_None,
"Settings::Set::Add Fail\n");
}
VerifyOrQuit(otPlatSettingsSet(&sInstance, key, sWriteBuffer, sWriteBufferLength) == kThreadError_None,
"Settings::Set::Set Fail\n");
VerifyOrQuit(otPlatSettingsGet(&sInstance, key, 0, readBuffer, &readBufferLength) == kThreadError_None,
"Settings::Set::Get Fail\n");
VerifyOrQuit(!memcmp(readBuffer, sWriteBuffer, static_cast<uint16_t>(sWriteBufferLength)),
"Settings::Set::Set Check Fail\n");
}
void TestSettingsSwap(void)
{
ThreadError error = kThreadError_None;
uint8_t key = 11;
uint16_t index = 0;
uint8_t readBuffer[kMaxStageDataLen];
uint16_t readBufferLength = kMaxStageDataLen;
while (true)
{
error = otPlatSettingsAdd(&sInstance, key, sWriteBuffer, sWriteBufferLength);
VerifyOrQuit(error == kThreadError_None || error == kThreadError_NoBufs, "Settings::Swap::Add Fail\n");
if (error == kThreadError_NoBufs)
{
break;
}
index++;
}
VerifyOrQuit(otPlatSettingsDelete(&sInstance, key, 0) == kThreadError_None, "Settings::Swap::Delete Fail\n");
VerifyOrQuit(otPlatSettingsAdd(&sInstance, key, sWriteBuffer, sWriteBufferLength) == kThreadError_None,
"Settings::Swap::Add Fail after swap\n");
VerifyOrQuit(otPlatSettingsGet(&sInstance, key, index - 1, readBuffer, &readBufferLength) == kThreadError_None,
"Settings::Swap::Get Fail\n");
VerifyOrQuit(!memcmp(readBuffer, sWriteBuffer, static_cast<uint16_t>(sWriteBufferLength)),
"Settings::Swap::Add and Swap Check Fail\n");
}
void RunSettingsTests(void)
{
TestSettingsInit();
TestSettingsAdd();
TestSettingsDelete();
TestSettingsSet();
TestSettingsSwap();
}
#ifdef ENABLE_TEST_MAIN
int main(void)
{
RunSettingsTests();
printf("All tests passed\n");
return 0;
}
#endif
-6
View File
@@ -80,9 +80,6 @@ void test_packed2();
void test_packed_union();
void test_packed_enum();
// test_settings.cpp
void RunSettingsTests();
#pragma endregion
utAssertTrue s_AssertTrue;
@@ -134,8 +131,5 @@ namespace Thread
TEST_METHOD(test_packed2) { ::test_packed2(); }
TEST_METHOD(test_packed_union) { ::test_packed_union(); }
TEST_METHOD(test_packed_enum) { ::test_packed_enum(); }
// test_settings.cpp
TEST_METHOD(RunSettingsTests) { ::RunSettingsTests(); }
};
}