diff --git a/src/app/clusters/ias-zone-client/ias-zone-client.cpp b/src/app/clusters/ias-zone-client/ias-zone-client.cpp index fe17ebdb647ced..ffd0713c132297 100644 --- a/src/app/clusters/ias-zone-client/ias-zone-client.cpp +++ b/src/app/clusters/ias-zone-client/ias-zone-client.cpp @@ -390,7 +390,8 @@ static void removeServer(uint8_t * ieeeAddress) static EmberStatus sendCommand(EmberNodeId destAddress) { emberAfSetCommandEndpoints(myEndpoint, emberAfIasZoneClientKnownServers[currentIndex].endpoint); - EmberStatus status = emberAfSendCommandUnicast(EMBER_OUTGOING_DIRECT, destAddress); + MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingDirect(destAddress)); + EmberStatus status = emberAfSendCommandUnicast(destination); emberAfIasZoneClusterPrintln("Sent IAS Zone Client Command to 0x%2X (%d -> %d) status: 0x%X", destAddress, myEndpoint, emberAfIasZoneClientKnownServers[currentIndex].endpoint, status); if (status != EMBER_SUCCESS) diff --git a/src/app/clusters/ias-zone-server/ias-zone-server.cpp b/src/app/clusters/ias-zone-server/ias-zone-server.cpp index 50723c4cc8c721..c7455bab37df22 100644 --- a/src/app/clusters/ias-zone-server/ias-zone-server.cpp +++ b/src/app/clusters/ias-zone-server/ias-zone-server.cpp @@ -762,9 +762,8 @@ void emberAfPluginIasZoneServerPrintQueueConfig(void) // destination when the destination is the only router the node is joined to. // In that case, the command will never have been sent, as the device will have // had no router by which to send the command. -void emberAfIasZoneClusterServerMessageSentCallback(EmberOutgoingMessageType type, MessageSendDestination destination, - EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, - EmberStatus status) +void emberAfIasZoneClusterServerMessageSentCallback(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, + uint8_t * message, EmberStatus status) { #if defined(EMBER_AF_PLUGIN_IAS_ZONE_SERVER_ENABLE_QUEUE) uint8_t frameControl; diff --git a/src/app/clusters/messaging-client/messaging-client.cpp b/src/app/clusters/messaging-client/messaging-client.cpp index 504b1168b76580..7aa8ba0d8afe15 100644 --- a/src/app/clusters/messaging-client/messaging-client.cpp +++ b/src/app/clusters/messaging-client/messaging-client.cpp @@ -328,8 +328,9 @@ EmberAfStatus emberAfPluginMessagingClientConfirmMessage(EndpointId endpoint) emberAfGetCurrentTime(), 0x00, ""); // The source and destination are reversed for the confirmation. emberAfSetCommandEndpoints(messageTable[ep].clientEndpoint, esiEntry->endpoint); - status = ((emberAfSendCommandUnicast(EMBER_OUTGOING_DIRECT, nodeId) == EMBER_SUCCESS) ? EMBER_ZCL_STATUS_SUCCESS - : EMBER_ZCL_STATUS_FAILURE); + MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingDirect(nodeId)); + status = + ((emberAfSendCommandUnicast(destination) == EMBER_SUCCESS) ? EMBER_ZCL_STATUS_SUCCESS : EMBER_ZCL_STATUS_FAILURE); } } else diff --git a/src/app/clusters/messaging-server/messaging-server.cpp b/src/app/clusters/messaging-server/messaging-server.cpp index e5ad2086aa59b6..47ebddaf6dfc6a 100644 --- a/src/app/clusters/messaging-server/messaging-server.cpp +++ b/src/app/clusters/messaging-server/messaging-server.cpp @@ -226,7 +226,8 @@ void emberAfPluginMessagingServerDisplayMessage(EmberNodeId nodeId, uint8_t srcE message.startTime, message.durationInMinutes, message.message, message.extendedMessageControl); emberAfSetCommandEndpoints(srcEndpoint, dstEndpoint); emberAfGetCommandApsFrame()->options |= EMBER_APS_OPTION_SOURCE_EUI64; - status = emberAfSendCommandUnicast(EMBER_OUTGOING_DIRECT, nodeId); + MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingDirect(nodeId)); + status = emberAfSendCommandUnicast(destination); if (status != EMBER_SUCCESS) { emberAfMessagingClusterPrintln("Error in display %x", status); @@ -248,7 +249,8 @@ void emberAfPluginMessagingServerCancelMessage(EmberNodeId nodeId, uint8_t srcEn ZCL_CANCEL_MESSAGE_COMMAND_ID, "wu", message.messageId, message.messageControl); emberAfSetCommandEndpoints(srcEndpoint, dstEndpoint); emberAfGetCommandApsFrame()->options |= EMBER_APS_OPTION_SOURCE_EUI64; - status = emberAfSendCommandUnicast(EMBER_OUTGOING_DIRECT, nodeId); + MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingDirect(nodeId)); + status = emberAfSendCommandUnicast(destination); if (status != EMBER_SUCCESS) { emberAfMessagingClusterPrintln("Error in cancel %x", status); diff --git a/src/app/reporting/reporting.cpp b/src/app/reporting/reporting.cpp index 3c2f3cbe88233e..7546e48f3b6805 100644 --- a/src/app/reporting/reporting.cpp +++ b/src/app/reporting/reporting.cpp @@ -74,8 +74,8 @@ static void removeConfigurationAndScheduleTick(uint8_t index); static EmberAfStatus configureReceivedAttribute(const EmberAfClusterCommand * cmd, AttributeId attributeId, uint8_t mask, uint16_t timeout); static void putReportableChangeInResp(const EmberAfPluginReportingEntry * entry, EmberAfAttributeType dataType); -static void retrySendReport(EmberOutgoingMessageType type, MessageSendDestination destination, EmberApsFrame * apsFrame, - uint16_t msgLen, uint8_t * message, EmberStatus status); +static void retrySendReport(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, + EmberStatus status); static uint32_t computeStringHash(uint8_t * data, uint8_t length); EmberEventControl emberAfPluginReportingTickEventControl; @@ -100,13 +100,13 @@ EmberAfStatus emberAfPluginReportingConfiguredCallback(const EmberAfPluginReport return EMBER_ZCL_STATUS_SUCCESS; } -static void retrySendReport(EmberOutgoingMessageType type, MessageSendDestination destination, EmberApsFrame * apsFrame, - uint16_t msgLen, uint8_t * message, EmberStatus status) +static void retrySendReport(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, + EmberStatus status) { // Retry once, and do so by unicasting without a pointer to this callback if (status != EMBER_SUCCESS) { - emberAfSendUnicast(type, destination, apsFrame, msgLen, message); + emberAfSendUnicast(destination, apsFrame, msgLen, message); } } diff --git a/src/app/util/af-main-common.cpp b/src/app/util/af-main-common.cpp index 6ffd8cd5983822..4220d153643ef3 100644 --- a/src/app/util/af-main-common.cpp +++ b/src/app/util/af-main-common.cpp @@ -202,9 +202,8 @@ void emAfInitializeMessageSentCallbackArray(void) } } -static EmberStatus send(EmberOutgoingMessageType type, MessageSendDestination destination, EmberApsFrame * apsFrame, - uint16_t messageLength, uint8_t * message, bool broadcast, EmberNodeId alias, uint8_t sequence, - EmberAfMessageSentFunction callback) +static EmberStatus send(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message, + bool broadcast, EmberNodeId alias, uint8_t sequence, EmberAfMessageSentFunction callback) { EmberStatus status; uint8_t index; @@ -268,7 +267,7 @@ static EmberStatus send(EmberOutgoingMessageType type, MessageSendDestination de { EmberAfMessageStruct messageStruct = { - callback, apsFrame, message, destination, messageLength, type, broadcast, + callback, apsFrame, message, destination, messageLength, broadcast, }; // Called prior to fragmentation in case the mesasge does not go out over the // Zigbee radio, and instead goes to some other transport that does not require @@ -285,7 +284,7 @@ static EmberStatus send(EmberOutgoingMessageType type, MessageSendDestination de if (messageLength <= EMBER_AF_MAXIMUM_SEND_PAYLOAD_LENGTH) { - status = emAfSend(type, destination, apsFrame, (uint8_t) messageLength, message, &messageTag, alias, sequence); + status = emAfSend(destination, apsFrame, (uint8_t) messageLength, message, &messageTag, alias, sequence); } else { @@ -297,7 +296,7 @@ static EmberStatus send(EmberOutgoingMessageType type, MessageSendDestination de if ((status != EMBER_SUCCESS) && (callback == emberAfPluginCriticalMessageQueueEnqueueCallback || callback == emAfPluginCriticalMessageQueueRetryCallback)) { - callback(type, destination, apsFrame, messageLength, message, status); + callback(destination, apsFrame, messageLength, message, status); } #endif // EMBER_AF_PLUGIN_CRITICAL_MESSAGE_QUEUE @@ -331,8 +330,9 @@ EmberStatus emberAfSendMulticastWithAliasWithCallback(GroupId multicastId, Ember uint8_t * message, EmberNodeId alias, uint8_t sequence, EmberAfMessageSentFunction callback) { - apsFrame->groupId = multicastId; - return send(EMBER_OUTGOING_MULTICAST_WITH_ALIAS, MessageSendDestination(multicastId), apsFrame, messageLength, message, + apsFrame->groupId = multicastId; + MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingMulticast(multicastId)); + return send(destination, apsFrame, messageLength, message, true, // broadcast alias, sequence, callback); } @@ -340,8 +340,9 @@ EmberStatus emberAfSendMulticastWithAliasWithCallback(GroupId multicastId, Ember EmberStatus emberAfSendMulticastWithCallback(GroupId multicastId, EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message, EmberAfMessageSentFunction callback) { - apsFrame->groupId = multicastId; - return send(EMBER_OUTGOING_MULTICAST, MessageSendDestination(multicastId), apsFrame, messageLength, message, + apsFrame->groupId = multicastId; + MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingMulticast(multicastId)); + return send(destination, apsFrame, messageLength, message, true, // broadcast? 0, // alias 0, // sequence @@ -422,13 +423,12 @@ EmberStatus emberAfSendMulticastToBindings(EmberApsFrame * apsFrame, uint16_t me // return emberAfSendBroadcastWithCallback(destination, apsFrame, messageLength, message, NULL); //} -EmberStatus emberAfSendUnicastWithCallback(EmberOutgoingMessageType type, MessageSendDestination destination, - EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message, - EmberAfMessageSentFunction callback) +EmberStatus emberAfSendUnicastWithCallback(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength, + uint8_t * message, EmberAfMessageSentFunction callback) { // The source endpoint in the APS frame MAY NOT be valid at this point if the // outgoing type is "via binding." - if (type == EMBER_OUTGOING_VIA_BINDING) + if (destination.Is()) { // If using binding, set the endpoints based on those in the binding. The // cluster in the binding is not used because bindings can be used to send @@ -436,7 +436,7 @@ EmberStatus emberAfSendUnicastWithCallback(EmberOutgoingMessageType type, Messag EmberBindingTableEntry binding; // TODO: This cast should go away once // https://github.com/project-chip/connectedhomeip/issues/3584 is fixed. - EmberStatus status = emberGetBinding(destination.mBindingIndex, &binding); + EmberStatus status = emberGetBinding(destination.Get().mBindingIndex, &binding); if (status != EMBER_SUCCESS) { return status; @@ -444,17 +444,17 @@ EmberStatus emberAfSendUnicastWithCallback(EmberOutgoingMessageType type, Messag apsFrame->sourceEndpoint = binding.local; apsFrame->destinationEndpoint = binding.remote; } - return send(type, destination, apsFrame, messageLength, message, + return send(destination, apsFrame, messageLength, message, false, // broadcast? 0, // alias 0, // sequence callback); } -EmberStatus emberAfSendUnicast(EmberOutgoingMessageType type, MessageSendDestination destination, EmberApsFrame * apsFrame, - uint16_t messageLength, uint8_t * message) +EmberStatus emberAfSendUnicast(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength, + uint8_t * message) { - return emberAfSendUnicastWithCallback(type, destination, apsFrame, messageLength, message, NULL); + return emberAfSendUnicastWithCallback(destination, apsFrame, messageLength, message, NULL); } EmberStatus emberAfSendUnicastToBindingsWithCallback(EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message, @@ -474,8 +474,9 @@ EmberStatus emberAfSendUnicastToBindingsWithCallback(EmberApsFrame * apsFrame, u if (binding.type == EMBER_UNICAST_BINDING && binding.local == apsFrame->sourceEndpoint && binding.clusterId == apsFrame->clusterId) { - apsFrame->destinationEndpoint = binding.remote; - status = send(EMBER_OUTGOING_VIA_BINDING, MessageSendDestination(i), apsFrame, messageLength, message, + apsFrame->destinationEndpoint = binding.remote; + MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingBinding(i)); + status = send(destination, apsFrame, messageLength, message, false, // broadcast? 0, // alias 0, // sequence @@ -547,7 +548,7 @@ void emAfPrintStatus(const char * task, EmberStatus status) // Functions called by the Serial Command Line Interface (CLI) // ****************************************************************** -static void printMessage(EmberIncomingMessageType type, EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * messageContents) +static void printMessage(EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * messageContents) { emberAfAppPrint("Cluster: 0x%2X, %d bytes,", apsFrame->clusterId, messageLength); if (messageLength >= 3) @@ -558,14 +559,14 @@ static void printMessage(EmberIncomingMessageType type, EmberApsFrame * apsFrame emberAfAppPrintln(""); } -void emAfMessageSentHandler(EmberOutgoingMessageType type, MessageSendDestination destination, EmberApsFrame * apsFrame, - EmberStatus status, uint16_t messageLength, uint8_t * messageContents, uint8_t messageTag) +void emAfMessageSentHandler(MessageSendDestination & destination, EmberApsFrame * apsFrame, EmberStatus status, + uint16_t messageLength, uint8_t * messageContents, uint8_t messageTag) { EmberAfMessageSentFunction callback; if (status != EMBER_SUCCESS) { emberAfAppPrint("%ptx %x, ", "ERROR: ", status); - printMessage(type, apsFrame, messageLength, messageContents); + printMessage(apsFrame, messageLength, messageContents); } callback = getMessageSentCallback(messageTag); @@ -576,7 +577,7 @@ void emAfMessageSentHandler(EmberOutgoingMessageType type, MessageSendDestinatio if (messageContents != NULL && messageContents[0] & ZCL_CLUSTER_SPECIFIC_COMMAND) { emberAfClusterMessageSentWithMfgCodeCallback( - type, destination, apsFrame, messageLength, messageContents, status, + destination, apsFrame, messageLength, messageContents, status, // If the manufacturer specific flag is set // get read it as next part of message // else use null code. @@ -587,24 +588,23 @@ void emAfMessageSentHandler(EmberOutgoingMessageType type, MessageSendDestinatio if (callback != NULL) { - (*callback)(type, destination, apsFrame, messageLength, messageContents, status); + (*callback)(destination, apsFrame, messageLength, messageContents, status); } #ifdef EMBER_AF_GENERATED_PLUGIN_MESSAGE_SENT_FUNCTION_CALLS EMBER_AF_GENERATED_PLUGIN_MESSAGE_SENT_FUNCTION_CALLS #endif - emberAfMessageSentCallback(type, destination, apsFrame, messageLength, messageContents, status); + emberAfMessageSentCallback(destination, apsFrame, messageLength, messageContents, status); } #ifdef EMBER_AF_PLUGIN_FRAGMENTATION -void emAfFragmentationMessageSentHandler(EmberOutgoingMessageType type, MessageSendDestination destination, - EmberApsFrame * apsFrame, uint8_t * buffer, uint16_t bufLen, EmberStatus status, - uint8_t messageTag) +void emAfFragmentationMessageSentHandler(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint8_t * buffer, + uint16_t bufLen, EmberStatus status, uint8_t messageTag) { // the fragmented message is no longer in process emberAfDebugPrintln("%pend.", "Fragmentation:"); - emAfMessageSentHandler(type, destination, apsFrame, status, bufLen, buffer, messageTag); + emAfMessageSentHandler(destination, apsFrame, status, bufLen, buffer, messageTag); // EMZIGBEE-4437: setting back the buffers to the original in case someone set // that to something else. @@ -612,8 +612,8 @@ void emAfFragmentationMessageSentHandler(EmberOutgoingMessageType type, MessageS } #endif // EMBER_AF_PLUGIN_FRAGMENTATION -EmberStatus emAfSend(EmberOutgoingMessageType type, MessageSendDestination destination, EmberApsFrame * apsFrame, - uint8_t messageLength, uint8_t * message, uint8_t * messageTag, EmberNodeId alias, uint8_t sequence) +EmberStatus emAfSend(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint8_t messageLength, uint8_t * message, + uint8_t * messageTag, EmberNodeId alias, uint8_t sequence) { // TODO: There's an impedance mismatch here in a few ways: // 1) The caller expects to get a messageTag out that will identify this @@ -630,11 +630,11 @@ EmberStatus emAfSend(EmberOutgoingMessageType type, MessageSendDestination desti // tracks this. *messageTag = INVALID_MESSAGE_TAG; EmberStatus status = EMBER_SUCCESS; - switch (type) + switch (destination.GetType()) { - case EMBER_OUTGOING_VIA_BINDING: { + case MessageSendDestination::OutGoingBinding::VariantId: { EmberBindingTableEntry binding; - status = emberGetBinding(destination.mBindingIndex, &binding); + status = emberGetBinding(destination.Get().mBindingIndex, &binding); if (status != EMBER_SUCCESS) { break; @@ -647,31 +647,33 @@ EmberStatus emAfSend(EmberOutgoingMessageType type, MessageSendDestination desti status = chipSendUnicast(binding.nodeId, apsFrame, messageLength, message); break; } - case EMBER_OUTGOING_VIA_ADDRESS_TABLE: + case MessageSendDestination::OutGoingAddressTable::VariantId: // No implementation yet. status = EMBER_ERR_FATAL; break; - case EMBER_OUTGOING_DIRECT: - status = chipSendUnicast(destination.mNodeId, apsFrame, messageLength, message); + case MessageSendDestination::OutGoingDirect::VariantId: + status = + chipSendUnicast(destination.Get().mNodeId, apsFrame, messageLength, message); break; - case EMBER_OUTGOING_MULTICAST: + case MessageSendDestination::OutGoingMulticast::VariantId: // No implementation yet. status = EMBER_ERR_FATAL; break; - case EMBER_OUTGOING_MULTICAST_WITH_ALIAS: + case MessageSendDestination::OutGoingMulticastWithAlias::VariantId: // No implementation yet. status = EMBER_ERR_FATAL; break; - case EMBER_OUTGOING_BROADCAST: + case MessageSendDestination::OutGoingBroadcast::VariantId: // No implementation yet. status = EMBER_ERR_FATAL; break; - case EMBER_OUTGOING_BROADCAST_WITH_ALIAS: + case MessageSendDestination::OutGoingBroadcastWithAlias::VariantId: // No implementation yet. status = EMBER_ERR_FATAL; break; - case EMBER_OUTGOING_VIA_EXCHANGE: - status = chipSendUnicast(destination.mExchangeContext, apsFrame, messageLength, message); + case MessageSendDestination::OutGoingExchange::VariantId: + status = chipSendUnicast(destination.Get().mExchangeContext, apsFrame, + messageLength, message); break; default: status = EMBER_BAD_ARGUMENT; diff --git a/src/app/util/af-main.h b/src/app/util/af-main.h index 777fde0ebe4b90..8d0566b2a60b2d 100644 --- a/src/app/util/af-main.h +++ b/src/app/util/af-main.h @@ -121,10 +121,10 @@ bool emAfProcessZdo(EmberNodeId sender, EmberApsFrame * apsFrame, uint8_t * mess void emAfIncomingMessageHandler(EmberIncomingMessageType type, EmberApsFrame * apsFrame, uint8_t lastHopLqi, int8_t lastHopRssi, uint16_t messageLength, uint8_t * messageContents); -EmberStatus emAfSend(EmberOutgoingMessageType type, chip::MessageSendDestination destination, EmberApsFrame * apsFrame, - uint8_t messageLength, uint8_t * message, uint8_t * messageTag, EmberNodeId alias, uint8_t sequence); -void emAfMessageSentHandler(EmberOutgoingMessageType type, uint64_t indexOrDestination, EmberApsFrame * apsFrame, - EmberStatus status, uint16_t messageLength, uint8_t * messageContents, uint8_t messageTag); +EmberStatus emAfSend(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint8_t messageLength, uint8_t * message, + uint8_t * messageTag, EmberNodeId alias, uint8_t sequence); +void emAfMessageSentHandler(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, EmberStatus status, + uint16_t messageLength, uint8_t * messageContents, uint8_t messageTag); void emAfStackStatusHandler(EmberStatus status); void emAfNetworkInit(void); diff --git a/src/app/util/af-types.h b/src/app/util/af-types.h index 7df8c212ca8b7d..e9bdb6a7a6f9ce 100644 --- a/src/app/util/af-types.h +++ b/src/app/util/af-types.h @@ -56,6 +56,7 @@ #include // For various types. #include +#include #ifdef EZSP_HOST #include "app/util/ezsp/ezsp-enum.h" @@ -1252,26 +1253,91 @@ typedef void (*EmberAfDefaultResponseFunction)(chip::EndpointId endpoint, chip:: namespace chip { /** - * @brief a type that represents where we are trying to send a message. This - * must always be paired with an EmberOutgoingMessageType that identifies - * which arm of the union is in use. - */ -union MessageSendDestination -{ - explicit constexpr MessageSendDestination(uint8_t aBindingIndex) : mBindingIndex(aBindingIndex) {} - explicit constexpr MessageSendDestination(NodeId aNodeId) : mNodeId(aNodeId) {} - explicit constexpr MessageSendDestination(GroupId aGroupId) : mGroupId(aGroupId) {} - explicit constexpr MessageSendDestination(Messaging::ExchangeContext * aExchangeContext) : mExchangeContext(aExchangeContext) {} - - // Used when the type is EMBER_OUTGOING_VIA_BINDING - uint8_t mBindingIndex; - // Used when the type is EMBER_OUTGOING_DIRECT - NodeId mNodeId; - // Used when the type is EMBER_OUTGOING_MULTICAST or - // EMBER_OUTGOING_MULTICAST_WITH_ALIAS - GroupId mGroupId; - // Used when the type is EMBER_OUTGOING_VIA_EXCHANGE - Messaging::ExchangeContext * mExchangeContext; + * @brief a type that represents where we are trying to send a message. + * The variant type identifies which arm of the union is in use. + */ +class MessageSendDestination +{ +public: + struct OutGoingBinding + { + static constexpr const std::size_t VariantId = 1; + explicit OutGoingBinding(uint8_t bindingIndex) : mBindingIndex(bindingIndex) {} + uint8_t mBindingIndex; + }; + + struct OutGoingAddressTable + { + static constexpr const std::size_t VariantId = 2; + }; + + struct OutGoingDirect + { + static constexpr const std::size_t VariantId = 3; + explicit OutGoingDirect(NodeId nodeId) : mNodeId(nodeId) {} + NodeId mNodeId; + }; + + struct OutGoingMulticast + { + static constexpr const std::size_t VariantId = 4; + explicit OutGoingMulticast(GroupId groupId) : mGroupId(groupId) {} + GroupId mGroupId; + }; + + struct OutGoingMulticastWithAlias + { + static constexpr const std::size_t VariantId = 5; + explicit OutGoingMulticastWithAlias(GroupId groupId) : mGroupId(groupId) {} + GroupId mGroupId; + }; + + struct OutGoingBroadcast + { + static constexpr const std::size_t VariantId = 6; + }; + + struct OutGoingBroadcastWithAlias + { + static constexpr const std::size_t VariantId = 7; + }; + + struct OutGoingExchange + { + static constexpr const std::size_t VariantId = 8; + explicit OutGoingExchange(Messaging::ExchangeContext * exchangeContext) : mExchangeContext(exchangeContext) {} + Messaging::ExchangeContext * mExchangeContext; + }; + + MessageSendDestination() = default; + MessageSendDestination(MessageSendDestination & that) = default; + MessageSendDestination(const MessageSendDestination & that) = default; + MessageSendDestination(MessageSendDestination && that) = default; + + template + MessageSendDestination(OutGoing && destination) + { + mDestination.Set(std::forward(destination)); + } + + std::size_t GetType() { return mDestination.GetType(); } + + template + bool Is() + { + return mDestination.Is(); + } + + template + OutGoing & Get() + { + return mDestination.Get(); + } + +private: + Variant + mDestination; }; } // namespace chip @@ -1280,8 +1346,8 @@ union MessageSendDestination * * This function is called when a message is sent. */ -typedef void (*EmberAfMessageSentFunction)(EmberOutgoingMessageType type, chip::MessageSendDestination destination, - EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status); +typedef void (*EmberAfMessageSentFunction)(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, + uint8_t * message, EmberStatus status); /** * @brief The EmberAfMessageStruct is a struct wrapper that @@ -1295,7 +1361,6 @@ typedef struct uint8_t * message; chip::MessageSendDestination destination; uint16_t messageLength; - EmberOutgoingMessageType type; bool broadcast; } EmberAfMessageStruct; diff --git a/src/app/util/af.h b/src/app/util/af.h index 239a8ba183227f..5ccbe4ef533e78 100644 --- a/src/app/util/af.h +++ b/src/app/util/af.h @@ -1297,15 +1297,14 @@ EmberStatus emberAfSendMulticastWithCallback(chip::GroupId multicastId, EmberAps /** * @brief Sends unicast. */ -EmberStatus emberAfSendUnicast(EmberOutgoingMessageType type, chip::MessageSendDestination destination, EmberApsFrame * apsFrame, - uint16_t messageLength, uint8_t * message); +EmberStatus emberAfSendUnicast(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength, + uint8_t * message); /** * @brief Sends unicast with attached message sent callback. */ -EmberStatus emberAfSendUnicastWithCallback(EmberOutgoingMessageType type, chip::MessageSendDestination destination, - EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message, - EmberAfMessageSentFunction callback); +EmberStatus emberAfSendUnicastWithCallback(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, + uint16_t messageLength, uint8_t * message, EmberAfMessageSentFunction callback); /** * @brief Unicasts the message to each remote node in the binding table that @@ -1391,13 +1390,12 @@ EmberStatus emberAfSendCommandMulticastToBindings(void); * using the emberAfFill... macros from the client command API. * It will be sent as unicast. */ -EmberStatus emberAfSendCommandUnicast(EmberOutgoingMessageType type, uint16_t indexOrDestination); +EmberStatus emberAfSendCommandUnicast(chip::MessageSendDestination & destination); /** * @brief emberAfSendCommandUnicast with attached message sent callback. */ -EmberStatus emberAfSendCommandUnicastWithCallback(EmberOutgoingMessageType type, uint64_t indexOrDestination, - EmberAfMessageSentFunction callback); +EmberStatus emberAfSendCommandUnicastWithCallback(chip::MessageSendDestination & destination, EmberAfMessageSentFunction callback); /** * @brief Sends the command prepared with emberAfFill.... macro. diff --git a/src/app/util/attribute-storage.cpp b/src/app/util/attribute-storage.cpp index 549a92872b5b0c..b014f2fd1486d0 100644 --- a/src/app/util/attribute-storage.cpp +++ b/src/app/util/attribute-storage.cpp @@ -213,9 +213,8 @@ void emberAfClusterDefaultResponseCallback(EndpointId endpoint, ClusterId cluste } // This function is used to call the per-cluster message sent callback -void emberAfClusterMessageSentWithMfgCodeCallback(EmberOutgoingMessageType type, MessageSendDestination destination, - EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status, - uint16_t mfgCode) +void emberAfClusterMessageSentWithMfgCodeCallback(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, + uint8_t * message, EmberStatus status, uint16_t mfgCode) { if (apsFrame != NULL && message != NULL && msgLen != 0) { @@ -230,7 +229,7 @@ void emberAfClusterMessageSentWithMfgCodeCallback(EmberOutgoingMessageType type, if (f != NULL) { // emberAfPushEndpointNetworkIndex(apsFrame->sourceEndpoint); - ((EmberAfMessageSentFunction) f)(type, destination, apsFrame, msgLen, message, status); + ((EmberAfMessageSentFunction) f)(destination, apsFrame, msgLen, message, status); // emberAfPopNetworkIndex(); } } @@ -240,11 +239,10 @@ void emberAfClusterMessageSentWithMfgCodeCallback(EmberOutgoingMessageType type, // This function is used to call the per-cluster message sent callback, and // wraps the emberAfClusterMessageSentWithMfgCodeCallback with a // EMBER_AF_NULL_MANUFACTURER_CODE. -void emberAfClusterMessageSentCallback(EmberOutgoingMessageType type, MessageSendDestination destination, EmberApsFrame * apsFrame, - uint16_t msgLen, uint8_t * message, EmberStatus status) +void emberAfClusterMessageSentCallback(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, + uint8_t * message, EmberStatus status) { - emberAfClusterMessageSentWithMfgCodeCallback(type, destination, apsFrame, msgLen, message, status, - EMBER_AF_NULL_MANUFACTURER_CODE); + emberAfClusterMessageSentWithMfgCodeCallback(destination, apsFrame, msgLen, message, status, EMBER_AF_NULL_MANUFACTURER_CODE); } // This function is used to call the per-cluster attribute changed callback diff --git a/src/app/util/attribute-storage.h b/src/app/util/attribute-storage.h index deb925e01f781b..75715247437318 100644 --- a/src/app/util/attribute-storage.h +++ b/src/app/util/attribute-storage.h @@ -189,12 +189,12 @@ void emberAfClusterDefaultResponseWithMfgCodeCallback(chip::EndpointId endpoint, EmberAfStatus status, uint8_t clientServerMask, uint16_t manufacturerCode); // Calls the message sent callback for a specific cluster, and wraps emberAfClusterMessageSentWithMfgCodeCallback -void emberAfClusterMessageSentCallback(EmberOutgoingMessageType type, chip::MessageSendDestination destination, - EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status); +void emberAfClusterMessageSentCallback(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, + uint8_t * message, EmberStatus status); // Calls the message sent callback for a specific cluster. -void emberAfClusterMessageSentWithMfgCodeCallback(EmberOutgoingMessageType type, chip::MessageSendDestination destination, - EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status, +void emberAfClusterMessageSentWithMfgCodeCallback(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, + uint16_t msgLen, uint8_t * message, EmberStatus status, uint16_t manufacturerCode); // Used to retrieve a manufacturer code from an attribute metadata diff --git a/src/app/util/client-api.cpp b/src/app/util/client-api.cpp index efae02ecec684a..800dd404206ef4 100644 --- a/src/app/util/client-api.cpp +++ b/src/app/util/client-api.cpp @@ -330,16 +330,15 @@ EmberStatus emberAfSendCommandMulticastToBindings(void) return emberAfSendMulticastToBindings(emAfCommandApsFrame, *emAfResponseLengthPtr, emAfZclBuffer); } -// EmberStatus emberAfSendCommandUnicastWithCallback(EmberOutgoingMessageType type, uint16_t indexOrDestination, -// EmberAfMessageSentFunction callback) +// EmberStatus emberAfSendCommandUnicastWithCallback(chip::MessageSendDestination & destination, EmberAfMessageSentFunction +// callback) // { -// return emberAfSendUnicastWithCallback(type, indexOrDestination, emAfCommandApsFrame, *emAfResponseLengthPtr, emAfZclBuffer, -// callback); +// return emberAfSendUnicastWithCallback(destination, emAfCommandApsFrame, *emAfResponseLengthPtr, emAfZclBuffer, callback); // } -// EmberStatus emberAfSendCommandUnicast(EmberOutgoingMessageType type, uint16_t indexOrDestination) +// EmberStatus emberAfSendCommandUnicast(chip::MessageSendDestination & destination) // { -// return emberAfSendCommandUnicastWithCallback(type, indexOrDestination, NULL); +// return emberAfSendCommandUnicastWithCallback(destination, NULL); // } // EmberStatus emberAfSendCommandBroadcastWithCallback(EmberNodeId destination, EmberAfMessageSentFunction callback) diff --git a/src/app/util/types_stub.h b/src/app/util/types_stub.h index 3446702bb84510..37382566ff6684 100644 --- a/src/app/util/types_stub.h +++ b/src/app/util/types_stub.h @@ -365,38 +365,6 @@ enum EMBER_INCOMING_BROADCAST_LOOPBACK }; -/** - * @brief Defines the possible outgoing message types. - */ -#ifdef DOXYGEN_SHOULD_SKIP_THIS -enum EmberOutgoingMessageType -#else -typedef uint8_t EmberOutgoingMessageType; -enum -#endif -{ - /** Unicast sent directly to an EmberNodeId. */ - EMBER_OUTGOING_DIRECT, - /** Unicast sent using an entry in the address table. */ - EMBER_OUTGOING_VIA_ADDRESS_TABLE, - /** Unicast sent using an entry in the binding table. */ - EMBER_OUTGOING_VIA_BINDING, - /** Multicast message. This value is passed to emberMessageSentHandler() only. - * It may not be passed to emberSendUnicast(). */ - EMBER_OUTGOING_MULTICAST, - /** An aliased multicast message. This value is passed to emberMessageSentHandler() only. - * It may not be passed to emberSendUnicast(). */ - EMBER_OUTGOING_MULTICAST_WITH_ALIAS, - /** An aliased Broadcast message. This value is passed to emberMessageSentHandler() only. - * It may not be passed to emberSendUnicast(). */ - EMBER_OUTGOING_BROADCAST_WITH_ALIAS, - /** A broadcast message. This value is passed to emberMessageSentHandler() only. - * It may not be passed to emberSendUnicast(). */ - EMBER_OUTGOING_BROADCAST, - /** A messaging going via some existing exchange. */ - EMBER_OUTGOING_VIA_EXCHANGE, -}; - /** @brief Endpoint information (a ZigBee Simple Descriptor). * * This is a ZigBee Simple Descriptor and contains information diff --git a/src/app/util/util.cpp b/src/app/util/util.cpp index 1197a43bf95775..57bd9577b98d20 100644 --- a/src/app/util/util.cpp +++ b/src/app/util/util.cpp @@ -754,9 +754,11 @@ EmberStatus emberAfSendResponseWithCallback(EmberAfMessageSentFunction callback) } else if (!isBroadcastDestination(emberAfResponseDestination)) { - label = 'U'; - status = emberAfSendUnicastWithCallback(EMBER_OUTGOING_VIA_EXCHANGE, MessageSendDestination(emberAfResponseDestination), - &emberAfResponseApsFrame, appResponseLength, appResponseData, callback); + label = 'U'; + MessageSendDestination destination = + MessageSendDestination(MessageSendDestination::OutGoingExchange(emberAfResponseDestination)); + status = + emberAfSendUnicastWithCallback(destination, &emberAfResponseApsFrame, appResponseLength, appResponseData, callback); } else { diff --git a/src/app/zap-templates/templates/app/callback-stub-src.zapt b/src/app/zap-templates/templates/app/callback-stub-src.zapt index 59cc60244d8f01..9aa03119bbcc27 100644 --- a/src/app/zap-templates/templates/app/callback-stub-src.zapt +++ b/src/app/zap-templates/templates/app/callback-stub-src.zapt @@ -323,7 +323,7 @@ bool __attribute__((weak)) emberAfPreMessageSendCallback( * @param status Ver.: always */ bool __attribute__((weak)) emberAfMessageSentCallback( - EmberOutgoingMessageType type, MessageSendDestination destination, + MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status) { diff --git a/src/app/zap-templates/templates/app/callback.zapt b/src/app/zap-templates/templates/app/callback.zapt index fe1372723d2b27..c8a74a05662ab1 100644 --- a/src/app/zap-templates/templates/app/callback.zapt +++ b/src/app/zap-templates/templates/app/callback.zapt @@ -81,7 +81,7 @@ void emberAf{{asCamelCased name false}}Cluster{{asCamelCased side false}}Manufac * @param message The message that was sent * @param status The status of the sent message */ -void emberAf{{asCamelCased name false}}Cluster{{asCamelCased side false}}MessageSentCallback(EmberOutgoingMessageType type, chip::MessageSendDestination destination, EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status); +void emberAf{{asCamelCased name false}}Cluster{{asCamelCased side false}}MessageSentCallback(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status); /** @brief {{name}} Cluster {{asCamelCased side false}} Pre Attribute Changed * @@ -372,7 +372,7 @@ bool emberAfPreMessageSendCallback(EmberAfMessageStruct * messageStruct, EmberSt * @param message Ver.: always * @param status Ver.: always */ -bool emberAfMessageSentCallback(EmberOutgoingMessageType type, chip::MessageSendDestination destination, EmberApsFrame * apsFrame, +bool emberAfMessageSentCallback(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen, uint8_t * message, EmberStatus status); /** @brief Pre Attribute Change diff --git a/src/lib/support/Variant.h b/src/lib/support/Variant.h index 168840ea49c17d..1aa36cdc6fe54d 100644 --- a/src/lib/support/Variant.h +++ b/src/lib/support/Variant.h @@ -70,6 +70,15 @@ struct VariantCurry<> inline static void Copy(std::size_t that_t, const void * that_v, void * this_v) {} }; +// Using assert inside template will encoded template name into the assert +// error message, which cost lots of flash space, this function can strip the +// type name, and the function name should be as short as possible to save +// space. +inline void AssertStrip(bool exp) +{ + assert(exp); +} + } // namespace Internal /** @@ -96,8 +105,8 @@ template struct Variant { private: - static constexpr std::size_t kDataSize = std::max(sizeof(Ts)...); - static constexpr std::size_t kDataAlign = std::max(alignof(Ts)...); + static constexpr std::size_t kDataSize = std::max({ sizeof(Ts)... }); + static constexpr std::size_t kDataAlign = std::max({ alignof(Ts)... }); static constexpr std::size_t kInvalidType = SIZE_MAX; using Data = typename std::aligned_storage::type; @@ -137,12 +146,14 @@ struct Variant } template - bool Is() + bool Is() const { return (mTypeId == T::VariantId); } - bool Valid() { return (mTypeId != kInvalidType); } + std::size_t GetType() const { return mTypeId; } + + bool Valid() const { return (mTypeId != kInvalidType); } template void Set(Args &&... args) @@ -155,29 +166,15 @@ struct Variant template T & Get() { - if (mTypeId == T::VariantId) - { - return *reinterpret_cast(&mData); - } - else - { - assert(false); - return *static_cast(nullptr); - } + Internal::AssertStrip(mTypeId == T::VariantId); + return *reinterpret_cast(&mData); } template const T & Get() const { - if (mTypeId == T::VariantId) - { - return *reinterpret_cast(&mData); - } - else - { - assert(false); - return *static_cast(nullptr); - } + Internal::AssertStrip(mTypeId == T::VariantId); + return *reinterpret_cast(&mData); } ~Variant() { Curry::Destroy(mTypeId, &mData); }