[examples] provide APIs to manipulate 802.15.4 frames (#4156)

This commit exposes utility functions for radio drivers to manipulate
MAC frames.
This commit is contained in:
Yakun Xu
2019-10-11 09:50:20 -07:00
committed by Jonathan Hui
parent 28ffcd1bc6
commit f691ea3edf
5 changed files with 335 additions and 228 deletions
+2
View File
@@ -43,6 +43,8 @@ libopenthread_platform_utils_a_SOURCES = \
flash.h \
logging_rtt.c \
logging_rtt.h \
mac_frame.cpp \
mac_frame.h \
settings_ram.c \
settings_flash.c \
soft_source_match_table.c \
+117
View File
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2019, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "mac_frame.h"
#include "mac/mac_frame.hpp"
using namespace ot;
bool otMacFrameDoesAddrMatch(const otRadioFrame *aFrame,
otPanId aPanId,
otShortAddress aShortAddress,
const otExtAddress *aExtAddress)
{
const Mac::Frame &frame = *static_cast<const Mac::Frame *>(aFrame);
bool rval = false;
Mac::Address dst;
Mac::PanId panid;
SuccessOrExit(frame.GetDstAddr(dst));
switch (dst.GetType())
{
case Mac::Address::kTypeShort:
SuccessOrExit(frame.GetDstPanId(panid));
rval = (panid == Mac::kPanIdBroadcast || panid == aPanId) &&
(dst.GetShort() == Mac::kShortAddrBroadcast || dst.GetShort() == aShortAddress);
break;
case Mac::Address::kTypeExtended:
SuccessOrExit(frame.GetDstPanId(panid));
rval = (panid == Mac::kPanIdBroadcast || panid == aPanId) &&
dst.GetExtended() == *static_cast<const Mac::ExtAddress *>(aExtAddress);
break;
case Mac::Address::kTypeNone:
rval = true;
break;
}
exit:
return rval;
}
bool otMacFrameIsAck(const otRadioFrame *aFrame)
{
return static_cast<const Mac::Frame *>(aFrame)->GetType() == Mac::Frame::kFcfFrameAck;
}
bool otMacFrameIsDataRequest(const otRadioFrame *aFrame)
{
return static_cast<const Mac::Frame *>(aFrame)->IsDataRequestCommand();
}
bool otMacFrameIsAckRequested(const otRadioFrame *aFrame)
{
return static_cast<const Mac::Frame *>(aFrame)->GetAckRequest();
}
otError otMacFrameGetSrcAddr(const otRadioFrame *aFrame, otMacAddress *aMacAddress)
{
otError error;
Mac::Address address;
error = static_cast<const Mac::Frame *>(aFrame)->GetSrcAddr(address);
SuccessOrExit(error);
switch (address.GetType())
{
case Mac::Address::kTypeNone:
aMacAddress->mType = OT_MAC_ADDRESS_TYPE_NONE;
break;
case Mac::Address::kTypeShort:
aMacAddress->mType = OT_MAC_ADDRESS_TYPE_SHORT;
aMacAddress->mAddress.mShortAddress = address.GetShort();
break;
case Mac::Address::kTypeExtended:
aMacAddress->mType = OT_MAC_ADDRESS_TYPE_EXTENDED;
aMacAddress->mAddress.mExtAddress = address.GetExtended();
break;
}
exit:
return error;
}
uint8_t otMacFrameGetSequence(const otRadioFrame *aFrame)
{
return static_cast<const Mac::Frame *>(aFrame)->GetSequence();
}
+147
View File
@@ -0,0 +1,147 @@
/*
* Copyright (c) 2019, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* @brief
* This file defines the mac frame interface for OpenThread platform radio drivers.
*
*/
#ifndef OPENTHREAD_UTILS_MAC_FRAME_H
#define OPENTHREAD_UTILS_MAC_FRAME_H
#include <openthread/platform/radio.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* This enumeration specifies the IEEE 802.15.4 Address type.
*
*/
typedef enum
{
OT_MAC_ADDRESS_TYPE_NONE, ///< No address.
OT_MAC_ADDRESS_TYPE_SHORT, ///< IEEE 802.15.4 Short Address.
OT_MAC_ADDRESS_TYPE_EXTENDED, ///< IEEE 802.15.4 Extended Address.
} otMacAddressType;
/**
* This structure represents an IEEE 802.15.4 short or extended Address.
*
*/
typedef struct otMacAddress
{
union
{
otShortAddress mShortAddress; ///< The IEEE 802.15.4 Short Address.
otExtAddress mExtAddress; ///< The IEEE 802.15.4 Extended Address.
} mAddress;
otMacAddressType mType; ///< The address type (short, extended, or none).
} otMacAddress;
/**
* Check if @p aFrame is an Ack frame.
*
* @param[in] aFrame A pointer to the frame.
*
* @retval true It is an ACK frame.
* @retval false It is not an ACK frame.
*
*/
bool otMacFrameIsAck(const otRadioFrame *aFrame);
/**
* Check if @p aFrame is an Data Request Command.
*
* @param[in] aFrame A pointer to the frame.
*
* @retval true It is a Data Request Command frame.
* @retval false It is not a Data Request Command frame.
*
*/
bool otMacFrameIsDataRequest(const otRadioFrame *aFrame);
/**
* Check if @p aFrame requests ACK.
*
* @param[in] aFrame A pointer to the frame.
*
* @retval true It requests ACK.
* @retval false It does not request ACK.
*
*/
bool otMacFrameIsAckRequested(const otRadioFrame *aFrame);
/**
* Check if @p aFrame matches the @p aPandId and @p aShortAddress or @p aExtAddress.
*
* @param[in] aFrame A pointer to the frame.
* @param[in] aPanId The PAN id to match with.
* @param[in] aShortAddress The short address to match with.
* @param[in] aExtAddress The extended address to match with.
*
* @retval true It is a broadcast or matches with the PAN id and one of the addresses.
* @retval false It doesn't match.
*
*/
bool otMacFrameDoesAddrMatch(const otRadioFrame *aFrame,
otPanId aPanId,
otShortAddress aShortAddress,
const otExtAddress *aExtAddress);
/**
* Get source MAC address.
*
* @param[in] aFrame A pointer to the frame.
* @param[out] aMacAddress A pointer to MAC address.
*
* @retval OT_ERROR_NONE Successfully got the source MAC address.
* @retval OT_ERROR_PARSE Failed to parse the source MAC address.
*
*/
otError otMacFrameGetSrcAddr(const otRadioFrame *aFrame, otMacAddress *aMacAddress);
/**
* Get the sequence of @p aFrame.
*
* @param[in] aFrame A pointer to the frame.
*
* @returns The sequence of the frame.
*
*/
uint8_t otMacFrameGetSequence(const otRadioFrame *aFrame);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // OPENTHREAD_UTILS_MAC_FRAME_H