Skip to content

Commit cc9562e

Browse files
authored
Merge pull request #9136 from yossi2le/yossi_tdbstore_nvstore_co_exist
TDBStore and NVStore should create an error if co exist.
2 parents 5a5ad8d + 677dbd1 commit cc9562e

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

features/storage/kvstore/tdbstore/TDBStore.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mbed_error.h"
2525
#include "mbed_wait_api.h"
2626
#include "MbedCRC.h"
27+
#include "SystemStorage.h"
2728

2829
using namespace mbed;
2930

@@ -989,6 +990,13 @@ int TDBStore::init()
989990
goto end;
990991
}
991992

993+
//Check if we are on internal memory && try to set the internal memory for TDBStore use.
994+
if (strcmp(_bd->get_type(), "FLASHIAP") == 0 &&
995+
avoid_conflict_nvstore_tdbstore(TDBSTORE) == MBED_ERROR_ALREADY_INITIALIZED) {
996+
997+
MBED_ERROR(MBED_ERROR_ALREADY_INITIALIZED, "TDBStore in internal memory can not be initialize when NVStore is in use");
998+
}
999+
9921000
_max_keys = initial_max_keys;
9931001

9941002
ram_table = new ram_table_entry_t[_max_keys];

features/storage/nvstore/source/nvstore.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
#if NVSTORE_ENABLED
2222

2323
#include "FlashIAP.h"
24+
#include "SystemStorage.h"
2425
#include "mbed_critical.h"
2526
#include "mbed_assert.h"
27+
#include "mbed_error.h"
2628
#include "mbed_wait_api.h"
2729
#include <algorithm>
2830
#include <string.h>
@@ -854,6 +856,12 @@ int NVStore::init()
854856
return NVSTORE_SUCCESS;
855857
}
856858

859+
//Check if we are on internal memory && try to set the internal memory for TDBStore use.
860+
ret = avoid_conflict_nvstore_tdbstore(NVSTORE);
861+
//NVstore in internal memory can not be initialize when TDBStore is in use
862+
MBED_ASSERT(ret != MBED_ERROR_ALREADY_INITIALIZED);
863+
864+
857865
// This handles the case that init function is called by more than one thread concurrently.
858866
// Only the one who gets the value of 1 in _init_attempts_val will proceed, while others will
859867
// wait until init is finished.

features/storage/system_storage/SystemStorage.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include "SystemStorage.h"
1617
#include "BlockDevice.h"
1718
#include "FileSystem.h"
1819
#include "FATFileSystem.h"
1920
#include "LittleFileSystem.h"
21+
#include "mbed_error.h"
22+
2023

2124
#if COMPONENT_SPIF
2225
#include "SPIFBlockDevice.h"
@@ -40,6 +43,31 @@
4043

4144
using namespace mbed;
4245

46+
47+
48+
MBED_WEAK int avoid_conflict_nvstore_tdbstore(owner_type_e in_mem_owner)
49+
{
50+
int status = MBED_SUCCESS;
51+
static PlatformMutex _mutex;
52+
static owner_type_e internal_memory_owner = NONE;
53+
54+
_mutex.lock();
55+
56+
if (internal_memory_owner != NONE &&
57+
internal_memory_owner != in_mem_owner) {
58+
59+
status = MBED_ERROR_ALREADY_INITIALIZED;
60+
61+
} else {
62+
63+
internal_memory_owner = in_mem_owner;
64+
}
65+
66+
_mutex.unlock();
67+
68+
return status;
69+
}
70+
4371
// Align a value to a specified size.
4472
// Parameters :
4573
// val - [IN] Value.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_SYSTEM_STORAGE_H
17+
#define MBED_SYSTEM_STORAGE_H
18+
19+
#include "mbed_error.h"
20+
21+
typedef enum {
22+
NONE = 0,
23+
NVSTORE,
24+
TDBSTORE
25+
} owner_type_e;
26+
27+
/**
28+
* @brief Try to get an ownership for the internal flash memory storage type.
29+
* KVSTORE or NVSTORE is the current option and once the ownership is taken by one
30+
* second one can not be initialize.
31+
* @param[in] in_mem_owner Enum parameter to specify NVSTORE or KVSTORE as the storage owner
32+
* @returns MBED_SUCCESS if succeeded or MBED_ERROR_ALREADY_INITIALIZED if fails.
33+
*/
34+
int avoid_conflict_nvstore_tdbstore(owner_type_e in_mem_owner);
35+
36+
#endif

0 commit comments

Comments
 (0)