Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix python PersistentStorageDelegate to latest API description #20287

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ CHIP_ERROR StorageAdapter::SyncGetKeyValue(const char * key, void * value, uint1
}

uint16_t tmpSize = size;
bool isFound = false;

mGetKeyCb(mContext, key, (char *) value, &tmpSize);
mGetKeyCb(mContext, key, (char *) value, &tmpSize, &isFound);

if (tmpSize == 0)
if (!isFound)
{
ChipLogDetail(Controller, "Key Not Found\n");
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
Expand All @@ -107,8 +108,8 @@ CHIP_ERROR StorageAdapter::SyncGetKeyValue(const char * key, void * value, uint1

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);
mStorage[key] = std::string(static_cast<const char *>(value), size);
tehampson marked this conversation as resolved.
Show resolved Hide resolved
mSetKeyCb(mContext, key, value, size);
return CHIP_NO_ERROR;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Python {
using PyObject = void;

using SyncSetKeyValueCb = void (*)(PyObject * appContext, const char * key, const void * value, uint16_t size);
using SetGetKeyValueCb = void (*)(PyObject * appContext, const char * key, char * value, uint16_t * size);
using SetGetKeyValueCb = void (*)(PyObject * appContext, const char * key, char * value, uint16_t * size, bool * isFound);
using SyncDeleteKeyValueCb = void (*)(PyObject * appContext, const char * key);

class StorageAdapter : public PersistentStorageDelegate
Expand Down
15 changes: 10 additions & 5 deletions src/controller/python/chip/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
_SyncSetKeyValueCbFunct = CFUNCTYPE(
None, py_object, c_char_p, POINTER(c_char), c_uint16)
_SyncGetKeyValueCbFunct = CFUNCTYPE(
None, py_object, c_char_p, POINTER(c_char), POINTER(c_uint16))
None, py_object, c_char_p, POINTER(c_char), POINTER(c_uint16), POINTER(c_bool))
_SyncDeleteKeyValueCbFunct = CFUNCTYPE(None, py_object, c_char_p)


Expand All @@ -45,7 +45,7 @@ def _OnSyncSetKeyValueCb(storageObj, key: str, value, size):


@_SyncGetKeyValueCbFunct
def _OnSyncGetKeyValueCb(storageObj, key: str, value, size):
def _OnSyncGetKeyValueCb(storageObj, key: str, value, size, is_found):
''' This does not adhere to the API requirements of
PersistentStorageDelegate::SyncGetKeyValue, but that is okay since
the C++ storage binding layer is capable of adapting results from
Expand All @@ -57,7 +57,7 @@ def _OnSyncGetKeyValueCb(storageObj, key: str, value, size):
except Exception as ex:
keyValue = None

if (keyValue):
if (keyValue is not None):
sizeOfValue = size[0]
sizeToCopy = min(sizeOfValue, len(keyValue))

Expand All @@ -74,13 +74,15 @@ def _OnSyncGetKeyValueCb(storageObj, key: str, value, size):
# will use the value in size[0] to determine if it should
# return CHIP_ERROR_BUFFER_TOO_SMALL.
size[0] = len(keyValue)
is_found[0] = True
else:
is_found[0] = False
size[0] = 0


@_SyncDeleteKeyValueCbFunct
def _OnSyncDeleteKeyValueCb(storageObj, key):
storageObj.SetSdkKey(key.decode("utf-8"), None)
storageObj.DeleteSdkKey(key.decode("utf-8"))


class PersistentStorage:
Expand Down Expand Up @@ -151,7 +153,7 @@ def SetSdkKey(self, key: str, value: bytes):
raise ValueError("Invalid Key")

if (value is None):
del(self.jsonData['sdk-config'][key])
self.jsonData['sdk-config'][key] = None
tehampson marked this conversation as resolved.
Show resolved Hide resolved
else:
self.jsonData['sdk-config'][key] = base64.b64encode(
value).decode("utf-8")
Expand All @@ -161,6 +163,9 @@ def SetSdkKey(self, key: str, value: bytes):
def GetSdkKey(self, key: str):
return base64.b64decode(self.jsonData['sdk-config'][key])

def DeleteSdkKey(self, key: str):
del(self.jsonData['sdk-config'][key])

def GetUnderlyingStorageAdapter(self):
return self._storageAdapterObj

Expand Down