diff --git a/src/controller/python/BUILD.gn b/src/controller/python/BUILD.gn index 58d9cb1f7c4622..5fa37bbe8a0d36 100644 --- a/src/controller/python/BUILD.gn +++ b/src/controller/python/BUILD.gn @@ -63,6 +63,7 @@ shared_library("ChipDeviceCtrl") { "ChipDeviceController-StorageDelegate.cpp", "ChipDeviceController-StorageDelegate.h", "OpCredsBinding.cpp", + "chip/bdx/bdx.cpp", "chip/clusters/attribute.cpp", "chip/clusters/command.cpp", "chip/commissioning/PlaceholderOperationalCredentialsIssuer.h", @@ -166,6 +167,7 @@ chip_python_wheel_action("chip-core") { "chip/ChipStack.py", "chip/FabricAdmin.py", "chip/__init__.py", + "chip/bdx/Bdx.py", "chip/ble/__init__.py", "chip/ble/commissioning/__init__.py", "chip/ble/get_adapters.py", diff --git a/src/controller/python/chip/bdx/Bdx.py b/src/controller/python/chip/bdx/Bdx.py new file mode 100644 index 00000000000000..593d8f885a6082 --- /dev/null +++ b/src/controller/python/chip/bdx/Bdx.py @@ -0,0 +1,70 @@ +# +# Copyright (c) 2024 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. +# + +import ctypes +from ctypes import CFUNCTYPE, POINTER, c_bool, c_char_p, c_size_t, c_uint8, c_uint16, c_uint32, c_void_p, cast, py_object + + +_OnTransferObtainedCallbackFunct = CFUNCTYPE( + None, py_object, PyChipError, c_void_p, c_uint8, c_uint16, c_uint64, c_uint64, c_uint8_p, c_uint16, c_uint8_p, c_size_t) +_OnDataReceivedCallbackFunct = CFUNCTYPE( + None, py_object, c_uint16, c_uint8, PyChipError) +_OnTransferCompletedCallbackFunct = CFUNCTYPE( + None, py_object, PyChipError) + + +@_OnTransferObtainedCallbackFunct +def _OnTransferObtainedCallback(closure, result: PyChipError, bdxTransfer, transferControlFlags: int, maxBlockSize: int, + startOffset: int, length: int, fileDesignator, fileDesignatorLength: int, metadata, + metadataLength: int): + # TODO: Call the closure with the rest of the parameters. + pass + + +@_OnDataReceivedCallbackFunct +def _OnDataReceivedCallback(closure, dataBuffer, bufferLength: int): + # TODO: Call the closure with the data. + pass + + +@_OnTransferCompletedCallbackFunct +def _OnTransferCompletedCallback(closure, result: PyChipError): + # TODO: Call the closure. + pass + + +def Init(): + handle = chip.native.GetLibraryHandle() + # Uses one of the type decorators as an indicator for everything being initialized. + if not handle.pychip_Bdx_ExpectBdxTransfer.argtypes: + setter = chip.native.NativeLibraryHandleMethodArguments(handle) + + setter.Set('pychip_Bdx_ExpectBdxTransfer', + PyChipError, []) + setter.Set('pychip_Bdx_StopExpectingBdxTransfer', + PyChipError, []) + setter.Set('pychip_Bdx_AcceptSendTransfer', + PyChipError, [c_void_p, py_object, py_object]) + setter.Set('pychip_Bdx_AcceptReceiveTransfer', + PyChipError, [c_void_p, c_uint8_p, c_size_t]) + setter.Set('pychip_Bdx_RejectTransfer', + PyChipError, [c_void_p]) + setter.Set('pychip_Bdx_InitCallbacks', None, [ + _OnTransferObtainedCallbackFunct, _OnDataReceivedCallbackFunct, _OnTransferCompletedCallbackFunct]) + + handle.pychip_Bdx_InitCallbacks( + _OnTransferObtainedCallback, _OnDataReceivedCallback, _OnTransferCompletedCallback) diff --git a/src/controller/python/chip/bdx/bdx.cpp b/src/controller/python/chip/bdx/bdx.cpp new file mode 100644 index 00000000000000..f6fb506f4199a3 --- /dev/null +++ b/src/controller/python/chip/bdx/bdx.cpp @@ -0,0 +1,86 @@ +/* + * + * Copyright (c) 2024 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. + */ + +#include + +#include + +using PyObject = void *; + +namespace chip { +namespace python { + +using OnTransferObtainedCallback = void (*)(PyObject context, PyChipError result, void * bdxTransfer, + bdx::TransferControlFlags transferControlFlags, uint16_t maxBlockSize, + uint64_t startOffset, uint64_t length, const uint8_t * fileDesignator, + uint16_t fileDesignatorLength, const uint8_t * metadata, size_t metadataLength); +using OnDataReceivedCallback = void (*)(PyObject context, const uint8_t * dataBuffer, size_t bufferLength); +using OnTransferCompletedCallback = void (*)(PyObject context, PyChipError result); + +OnTransferObtainedCallback gOnTransferObtainedCallback = nullptr; +OnDataReceivedCallback gOnDataReceivedCallback = nullptr; +OnTransferCompletedCallback gOnTransferCompletedCallback = nullptr; + +} // namespace python +} // namespace chip + +using namespace chip::python; + +extern "C" { + +void pychip_Bdx_InitCallbacks(OnTransferObtainedCallback onTransferObtainedCallback, + OnDataReceivedCallback onDataReceivedCallback, + OnTransferCompletedCallback onTransferCompletedCallback) +{ + OnTransferObtainedCallback gOnTransferObtainedCallback = onTransferObtainedCallback; + OnDataReceivedCallback gOnDataReceivedCallback = onDataReceivedCallback; + OnTransferCompletedCallback gOnTransferCompletedCallback = onTransferCompletedCallback; +} + +PyChipError pychip_Bdx_ExpectBdxTransfer() +{ + // TODO: Call BdxTransferManager::ExpectATransfer. +} + +PyChipError pychip_Bdx_StopExpectingBdxTransfer() +{ + // TODO: Call BdxTransferManager::StopExpectingATransfer. +} + +PyChipError pychip_Bdx_AcceptSendTransfer(void * transfer, PyObject dataReceivedContext, PyObject transferCompletedContext) +{ + // TODO: Pass transferCompletedContext to transfer so that the system will call gOnTransferCompletedCallback with + // transferCompletedContext eventually. + // TODO: Call transfer->AcceptSend with a data callback to call gOnDataReceivedCallback with dataReceivedContext. +} + +PyChipError pychip_Bdx_AcceptReceiveTransfer(void * transfer, const uint8_t * dataBuffer, size_t dataLength, + void * transferCompletedContext) +{ + // TODO: Pass transferCompletedContext to transfer so that the system will call gOnTransferCompletedCallback with + // transferCompletedContext eventually. + // TODO: Call transfer->AcceptReceive with the data. +} + +PyChipError pychip_Bdx_RejectTransfer(void * transfer) +{ + // TODO: Pass transferCompletedContext to transfer so that the system will call gOnTransferCompletedCallback with + // transferCompletedContext eventually. + // TODO: Call transfer->Reject. +} + +}