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

For CFFI, remove variadic arguments #31159

Merged
Prev Previous commit
Next Next commit
Address comments from PR
tehampson committed Jan 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit a5ee5bd6f10650ffd08dbd9d39a8a62dd3538b31
31 changes: 13 additions & 18 deletions src/controller/python/chip/clusters/Attribute.py
Original file line number Diff line number Diff line change
@@ -962,17 +962,14 @@ def WriteAttributes(future: Future, eventLoop, device,
for idx, attr in enumerate(attributes):
if attr.Attribute.must_use_timed_write and timedRequestTimeoutMs is None or timedRequestTimeoutMs == 0:
raise chip.interaction_model.InteractionModelError(chip.interaction_model.Status.NeedsTimedInteraction)
path = chip.interaction_model.AttributePathIBstruct.parse(
b'\x00' * chip.interaction_model.AttributePathIBstruct.sizeof())
path.EndpointId = attr.EndpointId
path.ClusterId = attr.Attribute.cluster_id
path.AttributeId = attr.Attribute.attribute_id
path.DataVersion = attr.DataVersion
path.HasDataVersion = attr.HasDataVersion
path = chip.interaction_model.AttributePathIBstruct.build(path)

tlv = attr.Attribute.ToTLV(None, attr.Data)

pyWriteAttributes[idx].attributePath = cast(ctypes.c_char_p(path), c_void_p)
pyWriteAttributes[idx].attributePath.endpointId = c_uint16(attr.EndpointId)
pyWriteAttributes[idx].attributePath.clusterId = c_uint32(attr.Attribute.cluster_id)
pyWriteAttributes[idx].attributePath.attributeId = c_uint32(attr.Attribute.attribute_id)
pyWriteAttributes[idx].attributePath.dataVersion = c_uint32(attr.DataVersion)
pyWriteAttributes[idx].attributePath.hasDataVersion = c_uint8(attr.HasDataVersion)
pyWriteAttributes[idx].tlvData = cast(ctypes.c_char_p(bytes(tlv)), c_void_p)
pyWriteAttributes[idx].tlvLength = c_size_t(len(tlv))

@@ -998,16 +995,14 @@ def WriteGroupAttributes(groupId: int, devCtrl: c_void_p, attributes: List[Attri
pyWriteAttributesArrayType = PyWriteAttributeData * numberOfAttributes
pyWriteAttributes = pyWriteAttributesArrayType()
for idx, attr in enumerate(attributes):
path = chip.interaction_model.AttributePathIBstruct.parse(
b'\x00' * chip.interaction_model.AttributePathIBstruct.sizeof())
path.EndpointId = attr.EndpointId
path.ClusterId = attr.Attribute.cluster_id
path.AttributeId = attr.Attribute.attribute_id
path.DataVersion = attr.DataVersion
path.HasDataVersion = attr.HasDataVersion
path = chip.interaction_model.AttributePathIBstruct.build(path)

tlv = attr.Attribute.ToTLV(None, attr.Data)
pyWriteAttributes[idx].attributePath = cast(ctypes.c_char_p(path), c_void_p)

pyWriteAttributes[idx].attributePath.endpointId = c_uint16(attr.EndpointId)
pyWriteAttributes[idx].attributePath.clusterId = c_uint32(attr.Attribute.cluster_id)
pyWriteAttributes[idx].attributePath.attributeId = c_uint32(attr.Attribute.attribute_id)
pyWriteAttributes[idx].attributePath.dataVersion = c_uint32(attr.DataVersion)
pyWriteAttributes[idx].attributePath.hasDataVersion = c_uint8(attr.HasDataVersion)
pyWriteAttributes[idx].tlvData = cast(ctypes.c_char_p(bytes(tlv)), c_void_p)
pyWriteAttributes[idx].tlvLength = c_size_t(len(tlv))

8 changes: 3 additions & 5 deletions src/controller/python/chip/clusters/Command.py
Original file line number Diff line number Diff line change
@@ -325,13 +325,11 @@ def SendBatchCommands(future: Future, eventLoop, device, commands: List[InvokeRe
if clusterCommand.must_use_timed_invoke and timedRequestTimeoutMs is None or timedRequestTimeoutMs == 0:
raise chip.interaction_model.InteractionModelError(chip.interaction_model.Status.NeedsTimedInteraction)

commandPath = chip.interaction_model.CommandPathIBStruct.build({
"EndpointId": command.EndpointId,
"ClusterId": clusterCommand.cluster_id,
"CommandId": clusterCommand.command_id})
payloadTLV = clusterCommand.ToTLV()

pyBatchCommandsData[idx].commandPath = cast(c_char_p(commandPath), c_void_p)
pyBatchCommandsData[idx].commandPath.endpointId = c_uint16(command.EndpointId)
pyBatchCommandsData[idx].commandPath.clusterId = c_uint32(clusterCommand.cluster_id)
pyBatchCommandsData[idx].commandPath.commandId = c_uint32(clusterCommand.command_id)
pyBatchCommandsData[idx].tlvData = cast(c_char_p(bytes(payloadTLV)), c_void_p)
pyBatchCommandsData[idx].tlvLength = c_size_t(len(payloadTLV))

32 changes: 14 additions & 18 deletions src/controller/python/chip/clusters/attribute.cpp
Original file line number Diff line number Diff line change
@@ -359,25 +359,23 @@ PyChipError pychip_WriteClient_WriteAttributes(void * appContext, DeviceProxy *

for (size_t i = 0; i < attributeDataLength; i++)
{
void * path = writeAttributesData[i].mAttributePath;
void * tlv = writeAttributesData[i].mTlvData;
size_t length = writeAttributesData[i].mTlvLength;
python::PyAttributePath path = writeAttributesData[i].attributePath;
void * tlv = writeAttributesData[i].tlvData;
size_t length = writeAttributesData[i].tlvLength;

python::AttributePath pathObj;
memcpy(&pathObj, path, sizeof(python::AttributePath));
uint8_t * tlvBuffer = reinterpret_cast<uint8_t *>(tlv);

TLV::TLVReader reader;
reader.Init(tlvBuffer, static_cast<uint32_t>(length));
reader.Next();
Optional<DataVersion> dataVersion;
if (pathObj.hasDataVersion == 1)
if (path.hasDataVersion == 1)
{
dataVersion.SetValue(pathObj.dataVersion);
dataVersion.SetValue(path.dataVersion);
}
SuccessOrExit(err =
client->PutPreencodedAttribute(chip::app::ConcreteDataAttributePath(pathObj.endpointId, pathObj.clusterId,
pathObj.attributeId, dataVersion),
client->PutPreencodedAttribute(chip::app::ConcreteDataAttributePath(path.endpointId, path.clusterId,
path.attributeId, dataVersion),
reader));
}

@@ -417,26 +415,24 @@ PyChipError pychip_WriteClient_WriteGroupAttributes(size_t groupIdSizeT, chip::C

for (size_t i = 0; i < attributeDataLength; i++)
{
void * path = writeAttributesData[i].mAttributePath;
void * tlv = writeAttributesData[i].mTlvData;
size_t length = writeAttributesData[i].mTlvLength;
python::PyAttributePath path = writeAttributesData[i].attributePath;
void * tlv = writeAttributesData[i].tlvData;
size_t length = writeAttributesData[i].tlvLength;

python::AttributePath pathObj;
memcpy(&pathObj, path, sizeof(python::AttributePath));
uint8_t * tlvBuffer = reinterpret_cast<uint8_t *>(tlv);

TLV::TLVReader reader;
reader.Init(tlvBuffer, static_cast<uint32_t>(length));
reader.Next();
Optional<DataVersion> dataVersion;
if (pathObj.hasDataVersion == 1)
if (path.hasDataVersion == 1)
{
dataVersion.SetValue(pathObj.dataVersion);
dataVersion.SetValue(path.dataVersion);
}
// Using kInvalidEndpointId as that used when sending group write requests.
SuccessOrExit(err =
client->PutPreencodedAttribute(chip::app::ConcreteDataAttributePath(kInvalidEndpointId, pathObj.clusterId,
pathObj.attributeId, dataVersion),
client->PutPreencodedAttribute(chip::app::ConcreteDataAttributePath(kInvalidEndpointId, path.clusterId,
path.attributeId, dataVersion),
reader));
}

21 changes: 7 additions & 14 deletions src/controller/python/chip/clusters/command.cpp
Original file line number Diff line number Diff line change
@@ -68,13 +68,6 @@ OnCommandSenderResponseCallback gOnCommandSenderResponseCallback = nullptr;
OnCommandSenderErrorCallback gOnCommandSenderErrorCallback = nullptr;
OnCommandSenderDoneCallback gOnCommandSenderDoneCallback = nullptr;

struct __attribute__((packed)) CommandPath
{
chip::EndpointId endpointId;
chip::ClusterId clusterId;
chip::CommandId commandId;
};

class CommandSenderCallback : public CommandSender::ExtendableCallback
{
public:
@@ -283,16 +276,16 @@ PyChipError pychip_CommandSender_SendBatchCommands(void * appContext, DeviceProx

for (size_t i = 0; i < length; i++)
{
void * commandPath = batchCommandData[i].mCommandPath;
void * tlv = batchCommandData[i].mTlvData;
size_t tlvLength = batchCommandData[i].mTlvLength;
chip::EndpointId endpointId = batchCommandData[i].commandPath.endpointId;
chip::ClusterId clusterId = batchCommandData[i].commandPath.clusterId;
chip::CommandId commandId = batchCommandData[i].commandPath.commandId;
void * tlv = batchCommandData[i].tlvData;
size_t tlvLength = batchCommandData[i].tlvLength;

python::CommandPath invokeRequestInfoObj;
memcpy(&invokeRequestInfoObj, commandPath, sizeof(python::CommandPath));
const uint8_t * tlvBuffer = reinterpret_cast<const uint8_t *>(tlv);

app::CommandPathParams cmdParams = { invokeRequestInfoObj.endpointId, /* group id */ 0, invokeRequestInfoObj.clusterId,
invokeRequestInfoObj.commandId, (app::CommandPathFlags::kEndpointIdValid) };
app::CommandPathParams cmdParams = { endpointId, /* group id */ 0, clusterId,
commandId, (app::CommandPathFlags::kEndpointIdValid) };

CommandSender::AdditionalCommandParameters additionalParams;

30 changes: 24 additions & 6 deletions src/controller/python/chip/interaction_model/Delegate.h
Original file line number Diff line number Diff line change
@@ -26,20 +26,38 @@ namespace python {

static constexpr ClusterStatus kUndefinedClusterStatus = 0xFF;

// This needs to match the python definition that uses the same name.
struct PyCommandPath
{
chip::EndpointId endpointId;
chip::ClusterId clusterId;
chip::CommandId commandId;
};

// This needs to match the python definition that uses the same name.
struct PyInvokeRequestData
{
void * mCommandPath;
void * mTlvData;
size_t mTlvLength;
PyCommandPath commandPath;
void * tlvData;
size_t tlvLength;
};

// This needs to match the python definition that uses the same name.
struct PyAttributePath
{
chip::EndpointId endpointId;
chip::ClusterId clusterId;
chip::AttributeId attributeId;
chip::DataVersion dataVersion;
uint8_t hasDataVersion;
};

// This needs to match the python definition that uses the same name.
struct PyWriteAttributeData
{
void * mAttributePath;
void * mTlvData;
size_t mTlvLength;
PyAttributePath attributePath;
void * tlvData;
size_t tlvLength;
};

} // namespace python
8 changes: 4 additions & 4 deletions src/controller/python/chip/interaction_model/__init__.py
Original file line number Diff line number Diff line change
@@ -26,12 +26,12 @@

from chip.exceptions import ChipStackException

from .delegate import (AttributePath, AttributePathIBstruct, CommandPathIBStruct, DataVersionFilterIBstruct, EventPath,
from .delegate import (AttributePath, AttributePathIBstruct, DataVersionFilterIBstruct, EventPath,
EventPathIBstruct, PyInvokeRequestData, PyWriteAttributeData, SessionParameters, SessionParametersStruct)

__all__ = ["AttributePath", "AttributePathIBstruct", "CommandPathIBStruct",
"DataVersionFilterIBstruct", "EventPath", "EventPathIBstruct",
"InteractionModelError", "PyInvokeRequestData", "PyWriteAttributeData", "SessionParameters", "SessionParametersStruct", "Status"]
__all__ = ["AttributePath", "AttributePathIBstruct", "DataVersionFilterIBstruct",
"EventPath", "EventPathIBstruct", "InteractionModelError", "PyInvokeRequestData",
"PyWriteAttributeData", "SessionParameters", "SessionParametersStruct", "Status"]


# defined src/controller/python/chip/interaction_model/Delegate.h
58 changes: 44 additions & 14 deletions src/controller/python/chip/interaction_model/delegate.py
Original file line number Diff line number Diff line change
@@ -46,12 +46,6 @@
"AttributeId" / Int32ul,
)

CommandPathIBStruct = Struct(
"EndpointId" / Int16ul,
"ClusterId" / Int32ul,
"CommandId" / Int32ul,
)

# AttributePath should not contain padding
AttributePathIBstruct = Struct(
"EndpointId" / Int16ul,
@@ -134,6 +128,23 @@ class SessionParameters:
maxPathsPerInvoke: int


class PyCommandPath(ctypes.Structure):
''' InvokeRequest Path struct that has c++ counterpart for CFFI.

We are using the following struct for passing the information of InvokeRequest between Python and C++:

```c
struct PyCommandPath
{
chip::EndpointId endpointId;
chip::ClusterId clusterId;
chip::CommandId commandId;
};
```
'''
_fields_ = [('endpointId', ctypes.c_uint16), ('clusterId', ctypes.c_uint32), ('commandId', ctypes.c_uint32)]


class PyInvokeRequestData(ctypes.Structure):
''' InvokeRequest struct that has c++ counterpart for CFFI.

@@ -142,13 +153,32 @@ class PyInvokeRequestData(ctypes.Structure):
```c
struct PyInvokeRequestData
{
void * mCommandPath;
void * mTlvData;
size_t mTlvLength;
PyCommandPath commandPath;
void * tlvData;
size_t tlvLength;
};
```
'''
_fields_ = [('commandPath', PyCommandPath), ('tlvData', ctypes.c_void_p), ('tlvLength', ctypes.c_size_t)]


class PyAttributePath(ctypes.Structure):
''' Attributed Path struct that has c++ counterpart for CFFI.

We are using the following struct for passing the information of WriteAttributes between Python and C++:

```c
struct PyAttributePath
{
chip::EndpointId endpointId;
chip::ClusterId clusterId;
chip::AttributeId attributeId;
chip::DataVersion dataVersion;
uint8_t hasDataVersion;
};
```
'''
_fields_ = [('commandPath', ctypes.c_void_p), ('tlvData', ctypes.c_void_p), ('tlvLength', ctypes.c_size_t)]
_fields_ = [('endpointId', ctypes.c_uint16), ('clusterId', ctypes.c_uint32), ('attributeId', ctypes.c_uint32), ('dataVersion', ctypes.c_uint32), ('hasDataVersion', ctypes.c_uint8)]


class PyWriteAttributeData(ctypes.Structure):
@@ -159,13 +189,13 @@ class PyWriteAttributeData(ctypes.Structure):
```c
struct PyWriteAttributeData
{
void * mAttributePath;
void * mTlvData;
size_t mTlvLength;
PyAttributePath attributePath;
void * tlvData;
size_t tlvLength;
};
```
'''
_fields_ = [('attributePath', ctypes.c_void_p), ('tlvData', ctypes.c_void_p), ('tlvLength', ctypes.c_size_t)]
_fields_ = [('attributePath', PyAttributePath), ('tlvData', ctypes.c_void_p), ('tlvLength', ctypes.c_size_t)]


# typedef void (*PythonInteractionModelDelegate_OnCommandResponseStatusCodeReceivedFunct)(uint64_t commandSenderPtr,