Skip to content

Commit

Permalink
Fix python PersistentStorageDelegate to latest API description (#20287)
Browse files Browse the repository at this point in the history
* Fix python PersistentStorageDelegate to latest API description

* Restyle

* Address PR comment

* Address PR comment
  • Loading branch information
tehampson authored and pull[bot] committed Aug 3, 2023
1 parent 3736a19 commit 4569377
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
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);
mSetKeyCb(mContext, key, value, size);
return CHIP_NO_ERROR;
}
Expand Down
4 changes: 1 addition & 3 deletions src/controller/python/ChipDeviceController-StorageDelegate.h
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 All @@ -68,8 +68,6 @@ class StorageAdapter : public PersistentStorageDelegate
SetGetKeyValueCb mGetKeyCb;
SyncDeleteKeyValueCb mDeleteKeyCb;
PyObject * mContext;

std::map<std::string, std::string> mStorage;
};

} // namespace Python
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])
raise ValueError('value is not expected to be None')
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

0 comments on commit 4569377

Please sign in to comment.