Parsing WHAD messages
When a firmware receives a WHAD message, it must determine its type and dispatch it accordingly.
Determining a message type
The whad::NanoPbMsg::getType() method must be called to discover the basic
type of a received message. The return value is one of the whad::MessageType
enum values:
whad::MessageType::UnknownMsg: unknown message type, message must be discarded
whad::MessageType::GenericMsg: generic message, must be dispatched to generic messages handler
whad::MessageType::DiscoveryMsg: discovery message, see WHAD Device discovery
whad::MessageType::DomainMsg: domain-related message, must be dispatched to domain handler
Handling generic messages
Host-side message processing must determine the type of every incoming generic
message using whad::generic::GenericMsg::getType(), following the template below.
This function returns a value from the whad::generic::MessageType:
whad::generic::MessageType::UnknownMsg: unknown generic message, message must be discarded
whad::generic::MessageType::CommandResultMsg: command result message
whad::generic::MessageType::VerboseMsg: verbose message
whad::generic::MessageType::DebugMsg: debug message
whad::generic::MessageType::ProgressMsg: progress message
void generic_handler(NanoPbMsg p_message)
{
/* Wrap our NanoPbMsg into a GenericMsg instance. */
whad::generic::GenericMsg generic_msg(p_message);
switch (generic_msg.getType())
{
case whad::generic::MessageType::CommandResultMsg:
{
/* Process command result. */
if (whad_generic_cmd_result_parse(p_message, &result_code) == WHAD_SUCCESS)
{
/* Process command result. */
}
else
{
/* Error while parsing command result. */
}
}
break;
case whad::generic::MessageType::VerboseMsg:
{
/* No default parsing method available yet :/ */
}
break;
case whad::generic::MessageType::DebugMsg:
{
/* No default parsing method available yet :/ */
}
break;
case whad::generic::MessageType::UnknownMsg:
{
/* Discard message. */
}
break;
}
}
Handling domain messages
Domain messages must be dispatched to their respective domain message handler,
using the whad::NanoPbMsg::getDomain() function that returns a value
of whad::MessageDomain enum. The following code template shows how such
a dispatching process can be implemented:
void dispatch_domain_message(NanoPbMsg message)
{
Message response;
switch (message.getDomain())
{
/* Bluetooth Low Energy. */
case whad::MessageDomain::DomainBle:
{
/* Forward message to BLE domain handler. */
process_ble_message(message);
}
break;
/* Bluetooth Low Energy. */
case whad::MessageDomain::DomainDot15d4:
{
/* Forward message to Dot15d4 domain handler. */
process_dot15d4_message(message);
}
break;
/* Bluetooth Low Energy. */
case whad::MessageDomain::DomainEsb:
{
/* Forward message to ESB domain handler. */
process_esb_message(message);
}
break;
/* Bluetooth Low Energy. */
case whad::MessageDomain::DomainPhy:
{
/* Forward message to PHY domain handler. */
process_phy_message(message);
}
break;
/* Bluetooth Low Energy. */
case whad::MessageDomain::DomainUnifying:
{
/* Forward message to Unifying domain handler. */
process_unifying_message(message);
}
break;
/* Don't support other domains. */
default:
{
/* Tell the host we don't support this domain. */
whad::generic::UnsupportedDomain resp(&response);
whad::send(resp);
}
break;
}
}
Important
If the hosts sends messages for an unsupported domain, the interface must
answer with a whad::generic::UnsupportedDomain class instance.
Message API reference
-
namespace whad
Enums
-
enum MessageType
Message type enumeration.
Values:
-
enumerator UnknownMsg
Unkown message type.
-
enumerator GenericMsg
Generic message type.
-
enumerator DiscoveryMsg
Discovery message type.
-
enumerator DomainMsg
Domain-related message type.
-
enumerator UnknownMsg
-
enum MessageDomain
Message domain (if any)
Values:
-
enumerator DomainNone
No domain defined.
-
enumerator DomainBle
Related to Bluetooth Low Energy domain.
-
enumerator DomainEsb
Related to Enhanced ShockBurst domain.
-
enumerator DomainPhy
Related to PHY domain.
-
enumerator DomainUnifying
Related to Logitech Unifying domain.
-
enumerator DomainDot15d4
Related to IEEE 802.15.4 domain.
-
enumerator DomainNone
-
class NanoPbMsg
- #include <message.hpp>
Whad Nanopb message wrapper class.
Subclassed by whad::ble::BleMsg, whad::discovery::DiscoveryMsg, whad::dot15d4::Dot15d4Msg, whad::esb::EsbMsg, whad::generic::GenericMsg, whad::phy::PhyMsg, whad::unifying::UnifyingMsg
Public Functions
-
NanoPbMsg()
Nanopb message wrapper constructor.
-
NanoPbMsg(Message *message)
-
Message *getMessage(void)
Retrieve a pointer on the underlying (wrapped) Nanopb message.
- Returns:
Pointer to a Nanopb Message structure
-
Message *getRaw(void)
Convert the message into its raw representation.
- Returns:
Pointer to a Nanopb Message structure
-
MessageType getType(void)
Get message type.
- Returns:
Message type.
-
MessageDomain getDomain(void)
Get message domain.
- Returns:
Message domain.
Protected Attributes
-
Message *p_nanopbMessage
Pointer to the underlying NanoPb message structure.
-
NanoPbMsg()
-
enum MessageType