blob: d8d288604af9dae66ac66ae8567559f7d7243e53 [file] [log] [blame]
rgoliverbf60cc22021-01-26 21:15:59 -05001/*
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-silabs3da3aa82022-03-31 23:54:17 -040026#include <lib/core/CHIPPersistentStorageDelegate.h>
27#include <system/SystemClock.h>
28#include <system/SystemLayer.h>
rgoliverbf60cc22021-01-26 21:15:59 -050029
30namespace chip {
31namespace DeviceLayer {
32namespace PersistedStorage {
33
rgoliverbf60cc22021-01-26 21:15:59 -050034class 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
40public:
jmartinez-silabs3da3aa82022-03-31 23:54:17 -040041 CHIP_ERROR Init(void);
rgoliverbf60cc22021-01-26 21:15:59 -050042 CHIP_ERROR _Put(const char * key, const void * value, size_t value_size);
jmartinez-silabs3da3aa82022-03-31 23:54:17 -040043 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;
rgoliverbf60cc22021-01-26 21:15:59 -050048
Sergei Lissianoia3536cb2022-07-08 21:28:47 -040049 static void ForceKeyMapSave();
50
rgoliverbf60cc22021-01-26 21:15:59 -050051private:
jmartinez-silabs3da3aa82022-03-31 23:54:17 -040052 static void OnScheduledKeyMapSave(System::Layer * systemLayer, void * appState);
Sergei Lissianoia3536cb2022-07-08 21:28:47 -040053
jmartinez-silabs3da3aa82022-03-31 23:54:17 -040054 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;
rgoliverbf60cc22021-01-26 21:15:59 -050057
jmartinez-silabs3da3aa82022-03-31 23:54:17 -040058 // ===== Members for internal use by the following friends.
rgoliverbf60cc22021-01-26 21:15:59 -050059 friend KeyValueStoreManager & KeyValueStoreMgr();
60 friend KeyValueStoreManagerImpl & KeyValueStoreMgrImpl();
61
62 static KeyValueStoreManagerImpl sInstance;
63};
64
rgoliverbf60cc22021-01-26 21:15:59 -050065/**
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 */
71inline 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 */
82inline KeyValueStoreManagerImpl & KeyValueStoreMgrImpl(void)
83{
84 return KeyValueStoreManagerImpl::sInstance;
85}
86
87} // namespace PersistedStorage
88} // namespace DeviceLayer
89} // namespace chip