rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2021 Project CHIP Authors |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * @file |
| 21 | * Platform-specific key value storage implementation for EFR32. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #pragma once |
jmartinez-silabs | 3da3aa8 | 2022-03-31 23:54:17 -0400 | [diff] [blame] | 26 | #include <lib/core/CHIPPersistentStorageDelegate.h> |
| 27 | #include <system/SystemClock.h> |
| 28 | #include <system/SystemLayer.h> |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 29 | |
| 30 | namespace chip { |
| 31 | namespace DeviceLayer { |
| 32 | namespace PersistedStorage { |
| 33 | |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 34 | class KeyValueStoreManagerImpl final : public KeyValueStoreManager |
| 35 | { |
| 36 | // Allow the KeyValueStoreManager interface class to delegate method calls to |
| 37 | // the implementation methods provided by this class. |
| 38 | friend class KeyValueStoreManager; |
| 39 | |
| 40 | public: |
jmartinez-silabs | 3da3aa8 | 2022-03-31 23:54:17 -0400 | [diff] [blame] | 41 | CHIP_ERROR Init(void); |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 42 | CHIP_ERROR _Put(const char * key, const void * value, size_t value_size); |
jmartinez-silabs | 3da3aa8 | 2022-03-31 23:54:17 -0400 | [diff] [blame] | 43 | CHIP_ERROR _Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size = nullptr, size_t offset = 0) const; |
| 44 | CHIP_ERROR _Delete(const char * key); |
| 45 | CHIP_ERROR ErasePartition(void); |
| 46 | |
| 47 | static constexpr size_t kMaxEntries = KVS_MAX_ENTRIES; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 48 | |
Sergei Lissianoi | a3536cb | 2022-07-08 21:28:47 -0400 | [diff] [blame] | 49 | static void ForceKeyMapSave(); |
| 50 | |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 51 | private: |
jmartinez-silabs | 3da3aa8 | 2022-03-31 23:54:17 -0400 | [diff] [blame] | 52 | static void OnScheduledKeyMapSave(System::Layer * systemLayer, void * appState); |
Sergei Lissianoi | a3536cb | 2022-07-08 21:28:47 -0400 | [diff] [blame] | 53 | |
jmartinez-silabs | 3da3aa8 | 2022-03-31 23:54:17 -0400 | [diff] [blame] | 54 | void ScheduleKeyMapSave(void); |
| 55 | bool IsValidKvsNvm3Key(const uint32_t nvm3Key) const; |
| 56 | CHIP_ERROR MapKvsKeyToNvm3(const char * key, uint32_t & nvm3Key, bool isSlotNeeded = false) const; |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 57 | |
jmartinez-silabs | 3da3aa8 | 2022-03-31 23:54:17 -0400 | [diff] [blame] | 58 | // ===== Members for internal use by the following friends. |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 59 | friend KeyValueStoreManager & KeyValueStoreMgr(); |
| 60 | friend KeyValueStoreManagerImpl & KeyValueStoreMgrImpl(); |
| 61 | |
| 62 | static KeyValueStoreManagerImpl sInstance; |
| 63 | }; |
| 64 | |
rgoliver | bf60cc2 | 2021-01-26 21:15:59 -0500 | [diff] [blame] | 65 | /** |
| 66 | * Returns the public interface of the KeyValueStoreManager singleton object. |
| 67 | * |
| 68 | * Chip applications should use this to access features of the KeyValueStoreManager object |
| 69 | * that are common to all platforms. |
| 70 | */ |
| 71 | inline KeyValueStoreManager & KeyValueStoreMgr(void) |
| 72 | { |
| 73 | return KeyValueStoreManagerImpl::sInstance; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the platform-specific implementation of the KeyValueStoreManager singleton object. |
| 78 | * |
| 79 | * Chip applications can use this to gain access to features of the KeyValueStoreManager |
| 80 | * that are specific to the ESP32 platform. |
| 81 | */ |
| 82 | inline KeyValueStoreManagerImpl & KeyValueStoreMgrImpl(void) |
| 83 | { |
| 84 | return KeyValueStoreManagerImpl::sInstance; |
| 85 | } |
| 86 | |
| 87 | } // namespace PersistedStorage |
| 88 | } // namespace DeviceLayer |
| 89 | } // namespace chip |