diff --git a/features/storage/kvstore/tdbstore/TDBStore.cpp b/features/storage/kvstore/tdbstore/TDBStore.cpp index 9ba1fd4130..794031fc28 100644 --- a/features/storage/kvstore/tdbstore/TDBStore.cpp +++ b/features/storage/kvstore/tdbstore/TDBStore.cpp @@ -24,6 +24,7 @@ #include "mbed_error.h" #include "mbed_wait_api.h" #include "MbedCRC.h" +#include "SystemStorage.h" using namespace mbed; @@ -952,6 +953,7 @@ int TDBStore::init() uint32_t actual_data_size; int os_ret, ret = MBED_SUCCESS, reserved_ret; uint16_t versions[_num_areas]; + internal_mem_resident_type_e out_in_mem_res; _mutex.lock(); @@ -959,6 +961,13 @@ int TDBStore::init() goto end; } + //Check if we are on internal memory && try to set the internal memory for TDBStore use. + if (strcmp(_bd->get_type(), "FLASHIAP")==0 && + set_internal_storage_ownership(TDBSTORE, &out_in_mem_res) == MBED_ERROR_ALREADY_INITIALIZED) { + + MBED_ERROR(MBED_ERROR_ALREADY_INITIALIZED, "TDBStore in internal memory can not be initialize when NVStore is in use"); + } + _max_keys = initial_max_keys; ram_table = new ram_table_entry_t[_max_keys]; diff --git a/features/storage/nvstore/source/nvstore.cpp b/features/storage/nvstore/source/nvstore.cpp index e6df0f6cfa..3563b63e78 100644 --- a/features/storage/nvstore/source/nvstore.cpp +++ b/features/storage/nvstore/source/nvstore.cpp @@ -21,8 +21,10 @@ #if NVSTORE_ENABLED #include "FlashIAP.h" +#include "SystemStorage.h" #include "mbed_critical.h" #include "mbed_assert.h" +#include "mbed_error.h" #include "mbed_wait_api.h" #include #include @@ -849,11 +851,18 @@ int NVStore::init() uint16_t keys[NVSTORE_NUM_AREAS]; uint16_t actual_size; uint8_t owner; + internal_mem_resident_type_e out_in_mem_res; if (_init_done) { return NVSTORE_SUCCESS; } + //Check if we are on internal memory && try to set the internal memory for TDBStore use. + ret = set_internal_storage_ownership(NVSTORE, &out_in_mem_res); + //NVstore in internal memory can not be initialize when TDBStore is in use + MBED_ASSERT(ret != MBED_ERROR_ALREADY_INITIALIZED); + + // This handles the case that init function is called by more than one thread concurrently. // Only the one who gets the value of 1 in _init_attempts_val will proceed, while others will // wait until init is finished. diff --git a/features/storage/system_storage/SystemStorage.cpp b/features/storage/system_storage/SystemStorage.cpp index 0fff9c6c27..c008ee2925 100644 --- a/features/storage/system_storage/SystemStorage.cpp +++ b/features/storage/system_storage/SystemStorage.cpp @@ -13,10 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "SystemStorage.h" #include "BlockDevice.h" #include "FileSystem.h" #include "FATFileSystem.h" #include "LittleFileSystem.h" +#include "mbed_error.h" + #if COMPONENT_SPIF #include "SPIFBlockDevice.h" @@ -40,6 +43,31 @@ using namespace mbed; +static internal_mem_resident_type_e internal_memory_residency = NONE; +static SingletonPtr system_storage_mutex; + +MBED_WEAK int set_internal_storage_ownership(internal_mem_resident_type_e in_mem_res, internal_mem_resident_type_e *out_mem_res) +{ + int status = MBED_SUCCESS; + + system_storage_mutex->lock(); + + if (internal_memory_residency != NONE && + internal_memory_residency != in_mem_res) { + + status = MBED_ERROR_ALREADY_INITIALIZED; + + } else { + + internal_memory_residency = in_mem_res; + } + + *out_mem_res = internal_memory_residency; + system_storage_mutex->unlock(); + + return status; +} + // Align a value to a specified size. // Parameters : // val - [IN] Value. diff --git a/features/storage/system_storage/SystemStorage.h b/features/storage/system_storage/SystemStorage.h new file mode 100644 index 0000000000..af062f3fa3 --- /dev/null +++ b/features/storage/system_storage/SystemStorage.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018 ARM Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MBED_SYSTEM_STORAGE_H +#define MBED_SYSTEM_STORAGE_H + +#include "mbed_error.h" + +typedef enum { + NONE = 0, + NVSTORE, + TDBSTORE +} internal_mem_resident_type_e; + +/** + * @brief Try to get an ownership for the internal flash memory storage type. + * KVSTORE or NVSTORE is the current option and once the ownership is taken by one + * second one can not be initialize. + * @param[in] in_mem_res Enum parameter to specify NVSTORE or KVSTORE as the storage owner + * @param[in] out_mem_res Enum parameter which specify who is the current owner of the storage. + * @returns MBED_SUCCESS if succeeded or MBED_ERROR_ALREADY_INITIALIZED if fails. + */ +int set_internal_storage_ownership(internal_mem_resident_type_e in_mem_res, internal_mem_resident_type_e *out_mem_res); + +#endif