mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			Merge pull request #9136 from yossi2le/yossi_tdbstore_nvstore_co_exist
TDBStore and NVStore should create an error if co exist.pull/9305/head
						commit
						cc9562e0a5
					
				| 
						 | 
				
			
			@ -24,6 +24,7 @@
 | 
			
		|||
#include "mbed_error.h"
 | 
			
		||||
#include "mbed_wait_api.h"
 | 
			
		||||
#include "MbedCRC.h"
 | 
			
		||||
#include "SystemStorage.h"
 | 
			
		||||
 | 
			
		||||
using namespace mbed;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -989,6 +990,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 &&
 | 
			
		||||
            avoid_conflict_nvstore_tdbstore(TDBSTORE) == 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];
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 <algorithm>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -854,6 +856,12 @@ int NVStore::init()
 | 
			
		|||
        return NVSTORE_SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //Check if we are on internal memory && try to set the internal memory for TDBStore use.
 | 
			
		||||
    ret = avoid_conflict_nvstore_tdbstore(NVSTORE);
 | 
			
		||||
    //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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MBED_WEAK int avoid_conflict_nvstore_tdbstore(owner_type_e in_mem_owner)
 | 
			
		||||
{
 | 
			
		||||
    int status = MBED_SUCCESS;
 | 
			
		||||
    static PlatformMutex _mutex;
 | 
			
		||||
    static owner_type_e internal_memory_owner = NONE;
 | 
			
		||||
 | 
			
		||||
    _mutex.lock();
 | 
			
		||||
 | 
			
		||||
    if (internal_memory_owner != NONE &&
 | 
			
		||||
            internal_memory_owner != in_mem_owner) {
 | 
			
		||||
 | 
			
		||||
        status = MBED_ERROR_ALREADY_INITIALIZED;
 | 
			
		||||
 | 
			
		||||
    } else {
 | 
			
		||||
 | 
			
		||||
        internal_memory_owner = in_mem_owner;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    _mutex.unlock();
 | 
			
		||||
 | 
			
		||||
    return status;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Align a value to a specified size.
 | 
			
		||||
// Parameters :
 | 
			
		||||
// val           - [IN]   Value.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,36 @@
 | 
			
		|||
/*
 | 
			
		||||
 * 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
 | 
			
		||||
} owner_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_owner     Enum parameter to specify NVSTORE or KVSTORE as the storage owner
 | 
			
		||||
 * @returns                    MBED_SUCCESS if succeeded or MBED_ERROR_ALREADY_INITIALIZED if fails.
 | 
			
		||||
 */
 | 
			
		||||
int avoid_conflict_nvstore_tdbstore(owner_type_e in_mem_owner);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
		Loading…
	
		Reference in New Issue