-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
KeyValueStoreManagerImpl.cpp
105 lines (82 loc) · 3.29 KB
/
KeyValueStoreManagerImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* 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.
*/
/**
* @file
* Platform-specific key value storage implementation for K32W
*/
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <platform/KeyValueStoreManager.h>
#include <platform/K32W/K32WConfig.h>
#include "support/CHIPMem.h"
#include <string>
#include "PDM.h"
namespace chip {
namespace DeviceLayer {
namespace PersistedStorage {
/* TODO: adjust this value */
#define MAX_NO_OF_KEYS 255
KeyValueStoreManagerImpl KeyValueStoreManagerImpl::sInstance;
CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t value_size, size_t * read_bytes_size,
size_t offset_bytes)
{
CHIP_ERROR err = CHIP_NO_ERROR;
std::hash<std::string> hash_fn;
uint16_t key_id;
uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS;
size_t read_bytes;
VerifyOrExit((key != NULL) && (value != NULL), err = CHIP_ERROR_INVALID_ARGUMENT);
key_id = hash_fn(key) % MAX_NO_OF_KEYS;
ChipLogProgress(DeviceLayer, "KVS, get key id:: %i", key_id);
err = chip::DeviceLayer::Internal::K32WConfig::ReadConfigValueBin(
chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id),
(uint8_t*)value, value_size, read_bytes);
*read_bytes_size = read_bytes;
exit:
return err;
}
CHIP_ERROR KeyValueStoreManagerImpl::_Put(const char * key, const void * value, size_t value_size)
{
CHIP_ERROR err = CHIP_NO_ERROR;
std::hash<std::string> hash_fn;
uint16_t key_id;
uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS;
VerifyOrExit((key != NULL) && (value != NULL), err = CHIP_ERROR_INVALID_ARGUMENT);
key_id = hash_fn(key) % MAX_NO_OF_KEYS;
ChipLogProgress(DeviceLayer, "KVS, put key id:: %i", key_id);
err = chip::DeviceLayer::Internal::K32WConfig::WriteConfigValueBin(
chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id), (uint8_t*)value, value_size);
exit:
return err;
}
CHIP_ERROR KeyValueStoreManagerImpl::_Delete(const char * key)
{
CHIP_ERROR err = CHIP_NO_ERROR;
std::hash<std::string> hash_fn;
uint16_t key_id;
uint8_t pdm_id_kvs = chip::DeviceLayer::Internal::K32WConfig::kPDMId_KVS;
VerifyOrExit(key != NULL, err = CHIP_ERROR_INVALID_ARGUMENT);
key_id = hash_fn(key) % MAX_NO_OF_KEYS;
ChipLogProgress(DeviceLayer, "KVS, deleting key id:: %i", key_id);
err = chip::DeviceLayer::Internal::K32WConfig::ClearConfigValue(chip::DeviceLayer::Internal::K32WConfigKey(pdm_id_kvs, key_id));
exit:
return err;
}
} // namespace PersistedStorage
} // namespace DeviceLayer
} // namespace chip