-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathCHIPPersistentStorageDelegateBridge.mm
162 lines (133 loc) · 5.4 KB
/
CHIPPersistentStorageDelegateBridge.mm
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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.
*/
#import "CHIPPersistentStorageDelegateBridge.h"
#include <memory>
#include <string>
#include <support/Base64.h>
namespace {
std::string StringToBase64(const std::string & value)
{
std::unique_ptr<char[]> buffer(new char[BASE64_ENCODED_LEN(value.length())]);
uint32_t len
= chip::Base64Encode32(static_cast<const uint8_t *>(value.data()), static_cast<uint32_t>(value.length()), buffer.get());
if (len == UINT32_MAX) {
return "";
}
return std::string(buffer.get(), len);
}
std::string Base64ToString(const std::string & b64Value)
{
std::unique_ptr<char[]> buffer(new char[BASE64_MAX_DECODED_LEN(value.length())]);
uint32_t len = chip::Base64Decode32(b64Value.data(), std::static_cast<uint32_t>(b64Value.length()), buffer.get());
if (len == UINT32_MAX) {
return "";
}
return std::string(buffer.get(), len);
}
} // namespace
CHIPPersistentStorageDelegateBridge::CHIPPersistentStorageDelegateBridge(void)
: mDelegate(nil)
{
mDefaultPersistentStorage = [[NSUserDefaults alloc] init];
mWorkQueue = dispatch_queue_create("com.zigbee.chip.framework.storage.workqueue", DISPATCH_QUEUE_SERIAL);
}
CHIPPersistentStorageDelegateBridge::~CHIPPersistentStorageDelegateBridge(void) {}
void CHIPPersistentStorageDelegateBridge::setFrameworkDelegate(
_Nullable id<CHIPPersistentStorageDelegate> delegate, _Nullable dispatch_queue_t queue)
{
dispatch_async(mWorkQueue, ^{
if (delegate && queue) {
mDelegate = delegate;
mQueue = queue;
} else {
mDelegate = nil;
mQueue = nil;
}
});
}
CHIP_ERROR CHIPPersistentStorageDelegateBridge::SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) = 0;
{
__block CHIP_ERROR error = CHIP_NO_ERROR;
NSString * keyString = [NSString stringWithUTF8String:key];
dispatch_sync(mWorkQueue, ^{
NSLog(@"PersistentStorageDelegate Sync Get Value for Key: %@", keyString);
NSString * valueString = nil;
id<CHIPPersistentStorageDelegate> strongDelegate = mDelegate;
if (strongDelegate) {
valueString = [strongDelegate CHIPGetKeyValue:keyString];
} else {
valueString = [mDefaultPersistentStorage objectForKey:keyString];
}
if (valueString != nil) {
std::string decoded = Base64ToString([valueString UTF8String]);
if (decoded.length() > UINT16_MAX) {
error = CHIP_ERROR_BUFFER_TOO_SMALL;
} else {
if (value != nullptr) {
memcpy(value, decoded.data(), std::min(decoded.length(), size));
if (size < decoded.length()) {
error = CHIP_ERROR_NO_MEMORY;
}
} else {
error = CHIP_ERROR_NO_MEMORY;
}
size = decoded.length();
}
} else {
error = CHIP_ERROR_KEY_NOT_FOUND;
}
});
return error;
}
CHIP_ERROR CHIPPersistentStorageDelegateBridge::SyncSetKeyValue(const char * key, const void * value, uint16_t size) = 0;
{
std::string base64Value = StringToBase64(std::string(static_cast<const char *> value, size));
NSString * keyString = [NSString stringWithUTF8String:key];
NSString * valueString = [NSString stringWithUTF8String:base64Value.c_str()];
NSLog(@"PersistentStorageDelegate Set Key %@", keyString);
id<CHIPPersistentStorageDelegate> strongDelegate = mDelegate;
if (strongDelegate && mQueue) {
dispatch_sync(mQueue, ^{
[strongDelegate CHIPSetKeyValue:keyString value:valueString];
});
} else {
[mDefaultPersistentStorage setObject:valueString forKey:keyString];
}
// TODO: ideally the error from the dispatch should be returned
// however we expect to replace the storage delegate with KVS so for now
// we return no error (return used to be void due to async dispatch anyway)
return CHIP_NO_ERROR;
}
CHIP_ERROR CHIPPersistentStorageDelegateBridge::SyncDeleteKeyValue(const char * key)
{
NSString * keyString = [NSString stringWithUTF8String:key];
dispatch_async(mWorkQueue, ^{
NSLog(@"PersistentStorageDelegate Delete Key: %@", keyString);
id<CHIPPersistentStorageDelegate> strongDelegate = mDelegate;
if (strongDelegate && mQueue) {
dispatch_sync(mQueue, ^{
[strongDelegate CHIPDeleteKeyValue:keyString];
});
} else {
[mDefaultPersistentStorage removeObjectForKey:keyString];
}
});
// TODO: ideally the error from the dispatch should be returned
// however we expect to replace the storage delegate with KVS so for now
// we return no error (return used to be void due to async dispatch anyway)
return CHIP_NO_ERROR;
}