Skip to content

Commit

Permalink
Stop casting away const for readonly cluster data. (#14517)
Browse files Browse the repository at this point in the history
We have data declared constexpr, but then we const_cast (using C-style
casts) and pass around pointers to non-const to that data, which seems
like a bad idea.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Feb 18, 2022
1 parent 12a4294 commit 4393120
Show file tree
Hide file tree
Showing 55 changed files with 172 additions and 179 deletions.
10 changes: 6 additions & 4 deletions examples/bridge-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ uint8_t * ToZclCharString(uint8_t * zclString, const char * cString, uint8_t max
}

// Converted into bytes and mapped the (label, value)
void EncodeFixedLabel(const char * label, const char * value, uint8_t * buffer, uint16_t length, EmberAfAttributeMetadata * am)
void EncodeFixedLabel(const char * label, const char * value, uint8_t * buffer, uint16_t length,
const EmberAfAttributeMetadata * am)
{
_LabelStruct labelStruct;

Expand Down Expand Up @@ -209,7 +210,8 @@ EmberAfStatus HandleReadBridgedDeviceBasicAttribute(Device * dev, chip::Attribut
return EMBER_ZCL_STATUS_SUCCESS;
}

EmberAfStatus HandleReadFixedLabelAttribute(Device * dev, EmberAfAttributeMetadata * am, uint8_t * buffer, uint16_t maxReadLength)
EmberAfStatus HandleReadFixedLabelAttribute(Device * dev, const EmberAfAttributeMetadata * am, uint8_t * buffer,
uint16_t maxReadLength)
{
if ((am->attributeId == ZCL_LABEL_LIST_ATTRIBUTE_ID) && (maxReadLength <= kFixedLabelAttributeArraySize))
{
Expand Down Expand Up @@ -257,7 +259,7 @@ EmberAfStatus HandleWriteOnOffAttribute(Device * dev, chip::AttributeId attribut
}

EmberAfStatus emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer,
const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer,
uint16_t maxReadLength)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
Expand All @@ -284,7 +286,7 @@ EmberAfStatus emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterI
}

EmberAfStatus emberAfExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer)
const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);

Expand Down
10 changes: 6 additions & 4 deletions examples/bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ int RemoveDeviceEndpoint(Device * dev)
return -1;
}

void EncodeFixedLabel(const char * label, const char * value, uint8_t * buffer, uint16_t length, EmberAfAttributeMetadata * am)
void EncodeFixedLabel(const char * label, const char * value, uint8_t * buffer, uint16_t length,
const EmberAfAttributeMetadata * am)
{
char zclOctetStrBuf[kFixedLabelElementsOctetStringSize];
_LabelStruct labelStruct;
Expand Down Expand Up @@ -402,7 +403,8 @@ EmberAfStatus HandleWriteOnOffAttribute(DeviceOnOff * dev, chip::AttributeId att
return EMBER_ZCL_STATUS_SUCCESS;
}

EmberAfStatus HandleReadFixedLabelAttribute(Device * dev, EmberAfAttributeMetadata * am, uint8_t * buffer, uint16_t maxReadLength)
EmberAfStatus HandleReadFixedLabelAttribute(Device * dev, const EmberAfAttributeMetadata * am, uint8_t * buffer,
uint16_t maxReadLength)
{
if ((am->attributeId == ZCL_LABEL_LIST_ATTRIBUTE_ID) && (maxReadLength <= kFixedLabelAttributeArraySize))
{
Expand Down Expand Up @@ -451,7 +453,7 @@ EmberAfStatus HandleReadSwitchAttribute(DeviceSwitch * dev, chip::AttributeId at
}

EmberAfStatus emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer,
const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer,
uint16_t maxReadLength)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
Expand Down Expand Up @@ -485,7 +487,7 @@ EmberAfStatus emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterI
}

EmberAfStatus emberAfExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer)
const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);

Expand Down
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static CHIP_ERROR CmdAppServerClusters(int argc, char ** argv)

for (uint8_t clusterIndex = 0; clusterIndex < clusterCount; clusterIndex++)
{
EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, clusterIndex, server);
const EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, clusterIndex, server);
streamer_printf(streamer_get(), " - Cluster 0x%04X\r\n", cluster->clusterId);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/app-platform/ContentAppPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using GetSetupPINResponseType = app::Clusters::AccountLogin::Commands::GetSetupP
#define DEVICE_VERSION_DEFAULT 1

EmberAfStatus emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer,
const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer,
uint16_t maxReadLength)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
Expand All @@ -65,7 +65,7 @@ EmberAfStatus emberAfExternalAttributeReadCallback(EndpointId endpoint, ClusterI
}

EmberAfStatus emberAfExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId,
EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer)
const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer)
{
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);

Expand Down
2 changes: 1 addition & 1 deletion src/app/clusters/descriptor/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ CHIP_ERROR DescriptorAttrAccess::ReadClientServerAttribute(EndpointId endpoint,

for (uint8_t clusterIndex = 0; clusterIndex < clusterCount; clusterIndex++)
{
EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, clusterIndex, server);
const EmberAfCluster * cluster = emberAfGetNthCluster(endpoint, clusterIndex, server);
ReturnErrorOnFailure(encoder.Encode(cluster->clusterId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,10 @@ CHIP_ERROR PowerSourceConfigurationAttrAccess::Read(const ConcreteReadAttributeP
{
case Sources::Id:
err = aEncoder.EncodeList([](const auto & encoder) -> CHIP_ERROR {
uint16_t clusterCount = 0;

for (uint16_t endpointIndex = 0; endpointIndex < emberAfEndpointCount(); endpointIndex++)
for (auto endpoint : EnabledEndpointsWithServerCluster(PowerSource::Id))
{
clusterCount = emberAfClusterCount(endpointIndex, true);

for (uint8_t clusterIndex = 0; clusterIndex < clusterCount; clusterIndex++)
{
EmberAfCluster * cluster = emberAfGetNthCluster(endpointIndex, clusterIndex, true);

if (cluster->clusterId == PowerSource::Id)
{
ReturnErrorOnFailure(encoder.Encode(endpointIndex));
break; // There is only 1 server cluster per endpoint
}
}
ReturnErrorOnFailure(encoder.Encode(endpoint));
}

return CHIP_NO_ERROR;
});
break;
Expand Down
6 changes: 3 additions & 3 deletions src/app/util/af-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ typedef struct
/**
* Pointer to attribute metadata array for this cluster.
*/
EmberAfAttributeMetadata * attributes;
const EmberAfAttributeMetadata * attributes;
/**
* Total number of attributes
*/
Expand Down Expand Up @@ -328,7 +328,7 @@ typedef struct
* Pointer to the cluster structs, describing clusters on this
* endpoint type.
*/
EmberAfCluster * cluster;
const EmberAfCluster * cluster;
/**
* Number of clusters in this endpoint type.
*/
Expand Down Expand Up @@ -385,7 +385,7 @@ typedef struct
/**
* Endpoint type for this endpoint.
*/
EmberAfEndpointType * endpointType;
const EmberAfEndpointType * endpointType;
/**
* Network index for this endpoint.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/app/util/af.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
*
* @return Returns pointer to the attribute metadata location.
*/
EmberAfAttributeMetadata * emberAfLocateAttributeMetadata(chip::EndpointId endpoint, chip::ClusterId clusterId,
chip::AttributeId attributeId, uint8_t mask);
const EmberAfAttributeMetadata * emberAfLocateAttributeMetadata(chip::EndpointId endpoint, chip::ClusterId clusterId,
chip::AttributeId attributeId, uint8_t mask);

/**
* @brief Returns true if endpoint contains the ZCL cluster with specified id.
Expand Down
Loading

0 comments on commit 4393120

Please sign in to comment.