From 8921feff4c9189e3ce75f199bd8eac0fb19d9bc4 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Fri, 7 Feb 2020 10:46:37 -0800 Subject: [PATCH] [posix] implement otPlatFlash APIs (#4552) --- .travis/script.sh | 15 ++- examples/platforms/simulation/flash.c | 114 ++++++++---------- .../openthread-core-simulation-config.h | 10 ++ tests/toranj/build.sh | 20 +-- 4 files changed, 82 insertions(+), 77 deletions(-) diff --git a/.travis/script.sh b/.travis/script.sh index 4b2c700b9..d42f697af 100755 --- a/.travis/script.sh +++ b/.travis/script.sh @@ -66,6 +66,7 @@ python --version || die -DOPENTHREAD_CONFIG_LINK_RAW_ENABLE=1 \ -DOPENTHREAD_CONFIG_MAC_FILTER_ENABLE=1 \ -DOPENTHREAD_CONFIG_NCP_UART_ENABLE=1 \ + -DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1 \ -DOPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE=1 \ -DOPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE=1 \ -DOPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE=1 \ @@ -486,6 +487,7 @@ build_samr21() { -DOPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE=1 \ -DOPENTHREAD_CONFIG_MAC_FILTER_ENABLE=1 \ -DOPENTHREAD_CONFIG_NCP_UART_ENABLE=1 \ + -DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1 \ -DOPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE=1 \ -DOPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE=1 \ -DOPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE=1 \ @@ -522,6 +524,7 @@ build_samr21() { -DOPENTHREAD_CONFIG_LEGACY_ENABLE=1 \ -DOPENTHREAD_CONFIG_MAC_FILTER_ENABLE=1 \ -DOPENTHREAD_CONFIG_NCP_SPI_ENABLE=1 \ + -DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1 \ -DOPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE=1" git checkout -- . || die @@ -558,9 +561,10 @@ build_samr21() { --disable-tests || die make -j 8 || die - export CPPFLAGS=" \ - -DOPENTHREAD_CONFIG_ANOUNCE_SENDER_ENABLE=1 \ - -DOPENTHREAD_CONFIG_TIME_SYNC_ENABLE=1 \ + export CPPFLAGS=" \ + -DOPENTHREAD_CONFIG_ANOUNCE_SENDER_ENABLE=1 \ + -DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1 \ + -DOPENTHREAD_CONFIG_TIME_SYNC_ENABLE=1 \ -DOPENTHREAD_CONFIG_NCP_UART_ENABLE=1" git checkout -- . || die @@ -575,8 +579,9 @@ build_samr21() { --with-examples=simulation || die make -j 8 || die - export CPPFLAGS=" \ - -DOPENTHREAD_CONFIG_NCP_UART_ENABLE=1" + export CPPFLAGS=" \ + -DOPENTHREAD_CONFIG_NCP_UART_ENABLE=1 \ + -DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1" git checkout -- . || die git clean -xfd || die diff --git a/examples/platforms/simulation/flash.c b/examples/platforms/simulation/flash.c index 060c02f2f..df53db731 100644 --- a/examples/platforms/simulation/flash.c +++ b/examples/platforms/simulation/flash.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, The OpenThread Authors. + * Copyright (c) 2020, The OpenThread Authors. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,6 +28,7 @@ #include "platform-simulation.h" +#include #include #include #include @@ -36,24 +37,19 @@ #include #include - -#include "utils/code_utils.h" -#include "utils/flash.h" +#include static int sFlashFd = -1; -uint32_t sEraseAddress; enum { - FLASH_SIZE = 0x40000, - FLASH_PAGE_SIZE = 0x800, - FLASH_PAGE_NUM = 128, + SWAP_SIZE = 2048, + SWAP_NUM = 2, }; -otError utilsFlashInit(void) +void otPlatFlashInit(otInstance *aInstance) { - otError error = OT_ERROR_NONE; - const char *path = OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH; + const char *path = OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH; char fileName[sizeof(OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH) + 32]; struct stat st; bool create = false; @@ -81,84 +77,78 @@ otError utilsFlashInit(void) sFlashFd = open(fileName, O_RDWR | O_CREAT | O_CLOEXEC, 0600); lseek(sFlashFd, 0, SEEK_SET); - otEXPECT_ACTION(sFlashFd >= 0, error = OT_ERROR_FAILED); + assert(sFlashFd >= 0); if (create) { - for (uint16_t index = 0; index < FLASH_PAGE_NUM; index++) + for (uint8_t index = 0; index < SWAP_NUM; index++) { - error = utilsFlashErasePage(index * FLASH_PAGE_SIZE); - otEXPECT(error == OT_ERROR_NONE); + otPlatFlashErase(aInstance, index); } } - -exit: - return error; } -uint32_t utilsFlashGetSize(void) +uint32_t otPlatFlashGetSwapSize(otInstance *aInstance) { - return FLASH_SIZE; + OT_UNUSED_VARIABLE(aInstance); + + return SWAP_SIZE; } -otError utilsFlashErasePage(uint32_t aAddress) +void otPlatFlashErase(otInstance *aInstance, uint8_t aSwapIndex) { - otError error = OT_ERROR_NONE; + OT_UNUSED_VARIABLE(aInstance); + + uint8_t buffer[SWAP_SIZE]; uint32_t address; - uint8_t dummyPage[FLASH_SIZE]; + ssize_t rval; - otEXPECT_ACTION(sFlashFd >= 0, error = OT_ERROR_FAILED); - otEXPECT_ACTION(aAddress < FLASH_SIZE, error = OT_ERROR_INVALID_ARGS); + assert((sFlashFd >= 0) && (aSwapIndex < SWAP_NUM)); - // Get start address of the flash page that includes aAddress - address = aAddress & (~(uint32_t)(FLASH_PAGE_SIZE - 1)); + address = aSwapIndex ? SWAP_SIZE : 0; + memset(buffer, 0xff, sizeof(buffer)); - // set the page to the erased state. - memset((void *)(&dummyPage[0]), 0xff, FLASH_PAGE_SIZE); - - // Write the page - ssize_t r; - r = pwrite(sFlashFd, &(dummyPage[0]), FLASH_PAGE_SIZE, (off_t)address); - otEXPECT_ACTION(((int)r) == ((int)(FLASH_PAGE_SIZE)), error = OT_ERROR_FAILED); - -exit: - return error; + rval = pwrite(sFlashFd, buffer, sizeof(buffer), (off_t)address); + assert(rval == SWAP_SIZE); } -otError utilsFlashStatusWait(uint32_t aTimeout) +void otPlatFlashRead(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, void *aData, uint32_t aSize) { - OT_UNUSED_VARIABLE(aTimeout); + OT_UNUSED_VARIABLE(aInstance); - return OT_ERROR_NONE; + uint32_t address; + ssize_t rval; + + assert((sFlashFd >= 0) && (aSwapIndex < SWAP_NUM) && (aSize <= SWAP_SIZE) && (aOffset <= (SWAP_SIZE - aSize))); + + address = aSwapIndex ? SWAP_SIZE : 0; + + rval = pread(sFlashFd, aData, aSize, (off_t)(address + aOffset)); + assert((uint32_t)rval == aSize); } -uint32_t utilsFlashWrite(uint32_t aAddress, uint8_t *aData, uint32_t aSize) +void otPlatFlashWrite(otInstance *aInstance, uint8_t aSwapIndex, uint32_t aOffset, const void *aData, uint32_t aSize) { - uint32_t ret = 0; - uint32_t index = 0; + OT_UNUSED_VARIABLE(aInstance); + + uint32_t address; uint8_t byte; + ssize_t rval; - otEXPECT(sFlashFd >= 0 && aAddress < FLASH_SIZE); + assert((sFlashFd >= 0) && (aSwapIndex < SWAP_NUM) && (aSize <= SWAP_SIZE) && (aOffset <= (SWAP_SIZE - aSize))); - for (index = 0; index < aSize; index++) + address = aSwapIndex ? SWAP_SIZE : 0; + address += aOffset; + + for (uint32_t offset = 0; offset < aSize; offset++) { - otEXPECT((ret = utilsFlashRead(aAddress + index, &byte, 1)) == 1); + rval = pread(sFlashFd, &byte, sizeof(byte), (off_t)(address + offset)); + assert(rval == sizeof(byte)); + // Use bitwise AND to emulate the behavior of flash memory - byte &= aData[index]; - otEXPECT((ret = (uint32_t)pwrite(sFlashFd, &byte, 1, (off_t)(aAddress + index))) == 1); + byte &= ((uint8_t *)aData)[offset]; + + rval = pwrite(sFlashFd, &byte, sizeof(byte), (off_t)(address + offset)); + assert(rval == sizeof(byte)); } - -exit: - return index; -} - -uint32_t utilsFlashRead(uint32_t aAddress, uint8_t *aData, uint32_t aSize) -{ - uint32_t ret = 0; - - otEXPECT(sFlashFd >= 0 && aAddress < FLASH_SIZE); - ret = (uint32_t)pread(sFlashFd, aData, aSize, (off_t)aAddress); - -exit: - return ret; } diff --git a/examples/platforms/simulation/openthread-core-simulation-config.h b/examples/platforms/simulation/openthread-core-simulation-config.h index 6d801827a..b1ea2e6bc 100644 --- a/examples/platforms/simulation/openthread-core-simulation-config.h +++ b/examples/platforms/simulation/openthread-core-simulation-config.h @@ -123,6 +123,16 @@ */ #define OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE 1 +/** + * @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. + * + */ +#define OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE 1 + /** * @def CLI_COAP_SECURE_USE_COAP_DEFAULT_HANDLER * diff --git a/tests/toranj/build.sh b/tests/toranj/build.sh index fa52ae8b0..b424bdf30 100755 --- a/tests/toranj/build.sh +++ b/tests/toranj/build.sh @@ -107,9 +107,9 @@ case ${build_config} in echo "===================================================================================================" ./bootstrap || die cd "${top_builddir}" - ${top_srcdir}/configure \ - CPPFLAGS="$cppflags_config" \ - --with-examples=simulation \ + ${top_srcdir}/configure \ + CPPFLAGS="$cppflags_config -DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1" \ + --with-examples=simulation \ $configure_options || die make -j 8 || die ;; @@ -120,13 +120,13 @@ case ${build_config} in echo "====================================================================================================" ./bootstrap || die cd "${top_builddir}" - ${top_srcdir}/configure \ - CPPFLAGS="$cppflags_config" \ - --enable-coverage=${coverage} \ - --enable-ncp \ - --enable-radio-only \ - --with-examples=simulation \ - --disable-docs \ + ${top_srcdir}/configure \ + CPPFLAGS="$cppflags_config -DOPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE=1" \ + --enable-coverage=${coverage} \ + --enable-ncp \ + --enable-radio-only \ + --with-examples=simulation \ + --disable-docs \ --enable-tests=$tests || die make -j 8 || die ;;