Skip to content

Commit

Permalink
Refine MessageSendDestination API
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed May 26, 2021
1 parent 654c123 commit 5abbedc
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/app/clusters/ias-zone-server/ias-zone-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +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(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen,
uint8_t * message, EmberStatus status)
void emberAfIasZoneClusterServerMessageSentCallback(const 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;
Expand Down
56 changes: 28 additions & 28 deletions src/app/util/af-main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ void emAfInitializeMessageSentCallbackArray(void)
}
}

static EmberStatus send(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message,
bool broadcast, EmberNodeId alias, uint8_t sequence, EmberAfMessageSentFunction callback)
static EmberStatus send(const MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength,
uint8_t * message, bool broadcast, EmberNodeId alias, uint8_t sequence, EmberAfMessageSentFunction callback)
{
EmberStatus status;
uint8_t index;
Expand Down Expand Up @@ -330,8 +330,8 @@ EmberStatus emberAfSendMulticastWithAliasWithCallback(GroupId multicastId, Ember
uint8_t * message, EmberNodeId alias, uint8_t sequence,
EmberAfMessageSentFunction callback)
{
apsFrame->groupId = multicastId;
MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingMulticast(multicastId));
apsFrame->groupId = multicastId;
const MessageSendDestination destination = MessageSendDestination::Multicast(multicastId);
return send(destination, apsFrame, messageLength, message,
true, // broadcast
alias, sequence, callback);
Expand All @@ -340,8 +340,8 @@ EmberStatus emberAfSendMulticastWithAliasWithCallback(GroupId multicastId, Ember
EmberStatus emberAfSendMulticastWithCallback(GroupId multicastId, EmberApsFrame * apsFrame, uint16_t messageLength,
uint8_t * message, EmberAfMessageSentFunction callback)
{
apsFrame->groupId = multicastId;
MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingMulticast(multicastId));
apsFrame->groupId = multicastId;
const MessageSendDestination destination = MessageSendDestination::Multicast(multicastId);
return send(destination, apsFrame, messageLength, message,
true, // broadcast?
0, // alias
Expand Down Expand Up @@ -423,20 +423,20 @@ EmberStatus emberAfSendMulticastToBindings(EmberApsFrame * apsFrame, uint16_t me
// return emberAfSendBroadcastWithCallback(destination, apsFrame, messageLength, message, NULL);
//}

EmberStatus emberAfSendUnicastWithCallback(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength,
uint8_t * message, EmberAfMessageSentFunction callback)
EmberStatus emberAfSendUnicastWithCallback(const 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 (destination.Is<MessageSendDestination::OutGoingBinding>())
if (destination.IsViaBinding())
{
// 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
// messages with any cluster id, not just the one set in the binding.
EmberBindingTableEntry binding;
// TODO: This cast should go away once
// https://github.com/project-chip/connectedhomeip/issues/3584 is fixed.
EmberStatus status = emberGetBinding(destination.Get<MessageSendDestination::OutGoingBinding>().mBindingIndex, &binding);
EmberStatus status = emberGetBinding(destination.Get<MessageSendDestination::VariantViaBinding>().mBindingIndex, &binding);
if (status != EMBER_SUCCESS)
{
return status;
Expand All @@ -451,7 +451,7 @@ EmberStatus emberAfSendUnicastWithCallback(MessageSendDestination & destination,
callback);
}

EmberStatus emberAfSendUnicast(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength,
EmberStatus emberAfSendUnicast(const MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength,
uint8_t * message)
{
return emberAfSendUnicastWithCallback(destination, apsFrame, messageLength, message, NULL);
Expand All @@ -474,9 +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;
MessageSendDestination destination = MessageSendDestination(MessageSendDestination::OutGoingBinding(i));
status = send(destination, apsFrame, messageLength, message,
apsFrame->destinationEndpoint = binding.remote;
const MessageSendDestination destination = MessageSendDestination::ViaBinding(i);
status = send(destination, apsFrame, messageLength, message,
false, // broadcast?
0, // alias
0, // sequence
Expand Down Expand Up @@ -559,7 +559,7 @@ static void printMessage(EmberApsFrame * apsFrame, uint16_t messageLength, uint8
emberAfAppPrintln("");
}

void emAfMessageSentHandler(MessageSendDestination & destination, EmberApsFrame * apsFrame, EmberStatus status,
void emAfMessageSentHandler(const MessageSendDestination & destination, EmberApsFrame * apsFrame, EmberStatus status,
uint16_t messageLength, uint8_t * messageContents, uint8_t messageTag)
{
EmberAfMessageSentFunction callback;
Expand Down Expand Up @@ -599,7 +599,7 @@ void emAfMessageSentHandler(MessageSendDestination & destination, EmberApsFrame
}

#ifdef EMBER_AF_PLUGIN_FRAGMENTATION
void emAfFragmentationMessageSentHandler(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint8_t * buffer,
void emAfFragmentationMessageSentHandler(const MessageSendDestination & destination, EmberApsFrame * apsFrame, uint8_t * buffer,
uint16_t bufLen, EmberStatus status, uint8_t messageTag)
{
// the fragmented message is no longer in process
Expand All @@ -612,7 +612,7 @@ void emAfFragmentationMessageSentHandler(MessageSendDestination & destination, E
}
#endif // EMBER_AF_PLUGIN_FRAGMENTATION

EmberStatus emAfSend(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint8_t messageLength, uint8_t * message,
EmberStatus emAfSend(const 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:
Expand All @@ -632,9 +632,9 @@ EmberStatus emAfSend(MessageSendDestination & destination, EmberApsFrame * apsFr
EmberStatus status = EMBER_SUCCESS;
switch (destination.GetType())
{
case MessageSendDestination::OutGoingBinding::VariantId: {
case MessageSendDestination::VariantViaBinding::VariantId: {
EmberBindingTableEntry binding;
status = emberGetBinding(destination.Get<MessageSendDestination::OutGoingBinding>().mBindingIndex, &binding);
status = emberGetBinding(destination.Get<MessageSendDestination::VariantViaBinding>().mBindingIndex, &binding);
if (status != EMBER_SUCCESS)
{
break;
Expand All @@ -647,32 +647,32 @@ EmberStatus emAfSend(MessageSendDestination & destination, EmberApsFrame * apsFr
status = chipSendUnicast(binding.nodeId, apsFrame, messageLength, message);
break;
}
case MessageSendDestination::OutGoingAddressTable::VariantId:
case MessageSendDestination::VariantViaAddressTable::VariantId:
// No implementation yet.
status = EMBER_ERR_FATAL;
break;
case MessageSendDestination::OutGoingDirect::VariantId:
case MessageSendDestination::VariantDirect::VariantId:
status =
chipSendUnicast(destination.Get<MessageSendDestination::OutGoingDirect>().mNodeId, apsFrame, messageLength, message);
chipSendUnicast(destination.Get<MessageSendDestination::VariantDirect>().mNodeId, apsFrame, messageLength, message);
break;
case MessageSendDestination::OutGoingMulticast::VariantId:
case MessageSendDestination::VariantMulticast::VariantId:
// No implementation yet.
status = EMBER_ERR_FATAL;
break;
case MessageSendDestination::OutGoingMulticastWithAlias::VariantId:
case MessageSendDestination::VariantMulticastWithAlias::VariantId:
// No implementation yet.
status = EMBER_ERR_FATAL;
break;
case MessageSendDestination::OutGoingBroadcast::VariantId:
case MessageSendDestination::VariantBroadcast::VariantId:
// No implementation yet.
status = EMBER_ERR_FATAL;
break;
case MessageSendDestination::OutGoingBroadcastWithAlias::VariantId:
case MessageSendDestination::VariantBroadcastWithAlias::VariantId:
// No implementation yet.
status = EMBER_ERR_FATAL;
break;
case MessageSendDestination::OutGoingExchange::VariantId:
status = chipSendUnicast(destination.Get<MessageSendDestination::OutGoingExchange>().mExchangeContext, apsFrame,
case MessageSendDestination::VariantViaExchange::VariantId:
status = chipSendUnicast(destination.Get<MessageSendDestination::VariantViaExchange>().mExchangeContext, apsFrame,
messageLength, message);
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions src/app/util/af-main.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ 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(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint8_t messageLength, uint8_t * message,
uint8_t * messageTag, EmberNodeId alias, uint8_t sequence);
EmberStatus emAfSend(const 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);

Expand Down
72 changes: 43 additions & 29 deletions src/app/util/af-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1259,84 +1259,98 @@ namespace chip {
class MessageSendDestination
{
public:
struct OutGoingBinding
struct VariantViaBinding
{
static constexpr const std::size_t VariantId = 1;
explicit OutGoingBinding(uint8_t bindingIndex) : mBindingIndex(bindingIndex) {}
explicit VariantViaBinding(uint8_t bindingIndex) : mBindingIndex(bindingIndex) {}
uint8_t mBindingIndex;
};

struct OutGoingAddressTable
struct VariantViaAddressTable
{
static constexpr const std::size_t VariantId = 2;
};

struct OutGoingDirect
struct VariantDirect
{
static constexpr const std::size_t VariantId = 3;
explicit OutGoingDirect(NodeId nodeId) : mNodeId(nodeId) {}
explicit VariantDirect(NodeId nodeId) : mNodeId(nodeId) {}
NodeId mNodeId;
};

struct OutGoingMulticast
struct VariantMulticast
{
static constexpr const std::size_t VariantId = 4;
explicit OutGoingMulticast(GroupId groupId) : mGroupId(groupId) {}
explicit VariantMulticast(GroupId groupId) : mGroupId(groupId) {}
GroupId mGroupId;
};

struct OutGoingMulticastWithAlias
struct VariantMulticastWithAlias
{
static constexpr const std::size_t VariantId = 5;
explicit OutGoingMulticastWithAlias(GroupId groupId) : mGroupId(groupId) {}
explicit VariantMulticastWithAlias(GroupId groupId) : mGroupId(groupId) {}
GroupId mGroupId;
};

struct OutGoingBroadcast
struct VariantBroadcast
{
static constexpr const std::size_t VariantId = 6;
};

struct OutGoingBroadcastWithAlias
struct VariantBroadcastWithAlias
{
static constexpr const std::size_t VariantId = 7;
};

struct OutGoingExchange
struct VariantViaExchange
{
static constexpr const std::size_t VariantId = 8;
explicit OutGoingExchange(Messaging::ExchangeContext * exchangeContext) : mExchangeContext(exchangeContext) {}
explicit VariantViaExchange(Messaging::ExchangeContext * exchangeContext) : mExchangeContext(exchangeContext) {}
Messaging::ExchangeContext * mExchangeContext;
};

MessageSendDestination() = default;
MessageSendDestination(MessageSendDestination & that) = default;
MessageSendDestination(const MessageSendDestination & that) = default;
MessageSendDestination(MessageSendDestination && that) = default;

template <typename OutGoing>
MessageSendDestination(OutGoing && destination)
static MessageSendDestination ViaBinding(uint8_t bindingIndex)
{
mDestination.Set<OutGoing>(std::forward<OutGoing>(destination));
return MessageSendDestination(VariantViaBinding(bindingIndex));
}

std::size_t GetType() { return mDestination.GetType(); }
static MessageSendDestination Direct(NodeId nodeId) { return MessageSendDestination(VariantDirect(nodeId)); }

template <typename OutGoing>
bool Is()
static MessageSendDestination Multicast(GroupId groupId) { return MessageSendDestination(VariantMulticast(groupId)); }

static MessageSendDestination MulticastWithAlias(GroupId groupId)
{
return mDestination.Is<OutGoing>();
return MessageSendDestination(VariantMulticastWithAlias(groupId));
}

template <typename OutGoing>
OutGoing & Get()
static MessageSendDestination ViaExchange(Messaging::ExchangeContext * exchangeContext)
{
return mDestination.Get<OutGoing>();
return MessageSendDestination(VariantViaExchange(exchangeContext));
}

std::size_t GetType() const { return mDestination.GetType(); }

bool IsViaBinding() const { return mDestination.Is<VariantViaBinding>(); }

template <typename Destination>
const Destination & Get() const
{
return mDestination.Get<Destination>();
}

private:
Variant<OutGoingBinding, OutGoingAddressTable, OutGoingDirect, OutGoingMulticast, OutGoingMulticastWithAlias, OutGoingBroadcast,
OutGoingBroadcastWithAlias, OutGoingExchange>
template <typename Destination>
MessageSendDestination(Destination && destination)
{
mDestination.Set<Destination>(std::forward<Destination>(destination));
}

Variant<VariantViaBinding, VariantViaAddressTable, VariantDirect, VariantMulticast, VariantMulticastWithAlias, VariantBroadcast,
VariantBroadcastWithAlias, VariantViaExchange>
mDestination;
};
} // namespace chip
Expand All @@ -1346,8 +1360,8 @@ class MessageSendDestination
*
* This function is called when a message is sent.
*/
typedef void (*EmberAfMessageSentFunction)(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen,
uint8_t * message, EmberStatus status);
typedef void (*EmberAfMessageSentFunction)(const chip::MessageSendDestination & destination, EmberApsFrame * apsFrame,
uint16_t msgLen, uint8_t * message, EmberStatus status);

/**
* @brief The EmberAfMessageStruct is a struct wrapper that
Expand All @@ -1359,7 +1373,7 @@ typedef struct
EmberAfMessageSentFunction callback;
EmberApsFrame * apsFrame;
uint8_t * message;
chip::MessageSendDestination destination;
const chip::MessageSendDestination destination;
uint16_t messageLength;
bool broadcast;
} EmberAfMessageStruct;
Expand Down
9 changes: 5 additions & 4 deletions src/app/util/af.h
Original file line number Diff line number Diff line change
Expand Up @@ -1297,13 +1297,13 @@ EmberStatus emberAfSendMulticastWithCallback(chip::GroupId multicastId, EmberAps
/**
* @brief Sends unicast.
*/
EmberStatus emberAfSendUnicast(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength,
EmberStatus emberAfSendUnicast(const chip::MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t messageLength,
uint8_t * message);

/**
* @brief Sends unicast with attached message sent callback.
*/
EmberStatus emberAfSendUnicastWithCallback(chip::MessageSendDestination & destination, EmberApsFrame * apsFrame,
EmberStatus emberAfSendUnicastWithCallback(const chip::MessageSendDestination & destination, EmberApsFrame * apsFrame,
uint16_t messageLength, uint8_t * message, EmberAfMessageSentFunction callback);

/**
Expand Down Expand Up @@ -1390,12 +1390,13 @@ EmberStatus emberAfSendCommandMulticastToBindings(void);
* using the emberAfFill... macros from the client command API.
* It will be sent as unicast.
*/
EmberStatus emberAfSendCommandUnicast(chip::MessageSendDestination & destination);
EmberStatus emberAfSendCommandUnicast(const chip::MessageSendDestination & destination);

/**
* @brief emberAfSendCommandUnicast with attached message sent callback.
*/
EmberStatus emberAfSendCommandUnicastWithCallback(chip::MessageSendDestination & destination, EmberAfMessageSentFunction callback);
EmberStatus emberAfSendCommandUnicastWithCallback(const chip::MessageSendDestination & destination,
EmberAfMessageSentFunction callback);

/**
* @brief Sends the command prepared with emberAfFill.... macro.
Expand Down
4 changes: 2 additions & 2 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ void emberAfClusterDefaultResponseCallback(EndpointId endpoint, ClusterId cluste
}

// This function is used to call the per-cluster message sent callback
void emberAfClusterMessageSentWithMfgCodeCallback(MessageSendDestination & destination, EmberApsFrame * apsFrame, uint16_t msgLen,
uint8_t * message, EmberStatus status, uint16_t mfgCode)
void emberAfClusterMessageSentWithMfgCodeCallback(const MessageSendDestination & destination, EmberApsFrame * apsFrame,
uint16_t msgLen, uint8_t * message, EmberStatus status, uint16_t mfgCode)
{
if (apsFrame != NULL && message != NULL && msgLen != 0)
{
Expand Down
Loading

0 comments on commit 5abbedc

Please sign in to comment.