forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChipDeviceController-StorageDelegate.cpp
134 lines (110 loc) · 3.87 KB
/
ChipDeviceController-StorageDelegate.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
*
* 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.
*/
#include "controller/python/ChipDeviceController-StorageDelegate.h"
#include <cstring>
#include <map>
#include <string>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
namespace chip {
namespace Controller {
CHIP_ERROR PythonPersistentStorageDelegate::SyncGetKeyValue(const char * key, void * value, uint16_t & size)
{
ReturnErrorCodeIf(((value == nullptr) && (size != 0)), CHIP_ERROR_INVALID_ARGUMENT);
auto val = mStorage.find(key);
if (val == mStorage.end())
{
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}
uint16_t neededSize = val->second.size();
ReturnErrorCodeIf(size == 0 && neededSize == 0, CHIP_NO_ERROR);
ReturnErrorCodeIf(value == nullptr, CHIP_ERROR_BUFFER_TOO_SMALL);
if (size < neededSize)
{
memcpy(value, val->second.data(), size);
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
memcpy(value, val->second.data(), neededSize);
size = neededSize;
return CHIP_NO_ERROR;
}
CHIP_ERROR PythonPersistentStorageDelegate::SyncSetKeyValue(const char * key, const void * value, uint16_t size)
{
mStorage[key] = std::string(static_cast<const char *>(value), size);
ChipLogDetail(Controller, "SyncSetKeyValue on %s", key);
return CHIP_NO_ERROR;
}
CHIP_ERROR PythonPersistentStorageDelegate::SyncDeleteKeyValue(const char * key)
{
auto val = mStorage.find(key);
if (val == mStorage.end())
{
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}
mStorage.erase(key);
return CHIP_NO_ERROR;
}
namespace Python {
CHIP_ERROR StorageAdapter::SyncGetKeyValue(const char * key, void * value, uint16_t & size)
{
ChipLogDetail(Controller, "StorageAdapter::GetKeyValue: Key = %s, Value = %p (%u)", key, value, size);
if ((value == nullptr) && (size != 0))
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
uint16_t tmpSize = size;
bool isFound = false;
mGetKeyCb(mContext, key, (char *) value, &tmpSize, &isFound);
if (!isFound)
{
ChipLogDetail(Controller, "Key Not Found\n");
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}
if (size < tmpSize)
{
ChipLogDetail(Controller, "Buf not big enough\n");
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
ChipLogDetail(Controller, "Key Found %d\n", tmpSize);
size = tmpSize;
return CHIP_NO_ERROR;
}
CHIP_ERROR StorageAdapter::SyncSetKeyValue(const char * key, const void * value, uint16_t size)
{
ReturnErrorCodeIf(((value == nullptr) && (size != 0)), CHIP_ERROR_INVALID_ARGUMENT);
ChipLogDetail(Controller, "StorageAdapter::SetKeyValue: Key = %s, Value = %p (%u)", key, value, size);
mSetKeyCb(mContext, key, value, size);
return CHIP_NO_ERROR;
}
CHIP_ERROR StorageAdapter::SyncDeleteKeyValue(const char * key)
{
uint8_t val[1];
uint16_t size = 0;
CHIP_ERROR err = SyncGetKeyValue(key, val, size);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
return err;
}
ChipLogDetail(Controller, "StorageAdapter::DeleteKeyValue: Key = %s", key);
mDeleteKeyCb(mContext, key);
return CHIP_NO_ERROR;
}
} // namespace Python
} // namespace Controller
} // namespace chip