Skip to content

Commit

Permalink
Add the python-C++ translation.
Browse files Browse the repository at this point in the history
  • Loading branch information
harimau-qirex committed Sep 7, 2024
1 parent f633e43 commit 6ba0d13
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/controller/python/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
70 changes: 70 additions & 0 deletions src/controller/python/chip/bdx/Bdx.py
Original file line number Diff line number Diff line change
@@ -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)
86 changes: 86 additions & 0 deletions src/controller/python/chip/bdx/bdx.cpp
Original file line number Diff line number Diff line change
@@ -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 <protocols/bdx/BdxMessages.h>

#include <controller/python/chip/native/PyChipError.h>

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.
}

}

0 comments on commit 6ba0d13

Please sign in to comment.