2017-06-14 08:49:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* wireless/ieee802154/mac802154_data.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
|
|
|
* Copyright (C) 2017 Verge Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
* Author: Anthony Merlino <anthony@vergeaero.com>
|
|
|
|
*
|
|
|
|
* 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 NuttX 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 OWNER 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nuttx/mm/iob.h>
|
|
|
|
|
|
|
|
#include "mac802154.h"
|
|
|
|
#include "mac802154_internal.h"
|
|
|
|
#include "mac802154_data.h"
|
|
|
|
|
|
|
|
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public MAC Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mac802154_req_data
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The MCPS-DATA.request primitive requests the transfer of a data SPDU
|
|
|
|
* (i.e., MSDU) from a local SSCS entity to a single peer SSCS entity.
|
|
|
|
* Confirmation is returned via the
|
|
|
|
* struct mac802154_maccb_s->conf_data callback.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-15 18:46:48 +02:00
|
|
|
int mac802154_req_data(MACHANDLE mac,
|
2017-06-14 08:49:27 +02:00
|
|
|
FAR const struct ieee802154_frame_meta_s *meta,
|
|
|
|
FAR struct iob_s *frame)
|
|
|
|
{
|
|
|
|
FAR struct ieee802154_privmac_s *priv =
|
|
|
|
(FAR struct ieee802154_privmac_s *)mac;
|
|
|
|
FAR struct ieee802154_txdesc_s *txdesc;
|
|
|
|
uint16_t *frame_ctrl;
|
2017-06-19 11:55:28 +02:00
|
|
|
uint8_t mhr_len = 3;
|
2017-06-14 08:49:27 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-06-20 22:27:22 +02:00
|
|
|
wlinfo("Received frame io_len=%u io_offset=%u\n",
|
2017-06-21 00:06:35 +02:00
|
|
|
frame->io_len, frame->io_offset);
|
2017-06-20 22:27:22 +02:00
|
|
|
|
2017-06-14 08:49:27 +02:00
|
|
|
/* Check the required frame size */
|
|
|
|
|
|
|
|
if (frame->io_len > IEEE802154_MAX_PHY_PACKET_SIZE)
|
2017-06-20 22:27:22 +02:00
|
|
|
{
|
|
|
|
return -E2BIG;
|
|
|
|
}
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
/* Cast the first two bytes of the IOB to a uint16_t frame control field */
|
|
|
|
|
|
|
|
frame_ctrl = (FAR uint16_t *)&frame->io_data[0];
|
|
|
|
|
|
|
|
/* Ensure we start with a clear frame control field */
|
|
|
|
|
|
|
|
*frame_ctrl = 0;
|
|
|
|
|
|
|
|
/* Set the frame type to Data */
|
|
|
|
|
|
|
|
*frame_ctrl |= IEEE802154_FRAME_DATA << IEEE802154_FRAMECTRL_SHIFT_FTYPE;
|
|
|
|
|
|
|
|
/* If the msduLength is greater than aMaxMACSafePayloadSize, the MAC
|
|
|
|
* sublayer will set the Frame Version to one. [1] pg. 118.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((frame->io_len - frame->io_offset) > IEEE802154_MAX_SAFE_MAC_PAYLOAD_SIZE)
|
|
|
|
{
|
|
|
|
*frame_ctrl |= IEEE802154_FRAMECTRL_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the TXOptions parameter specifies that an acknowledged transmission
|
|
|
|
* is required, the AR field will be set appropriately, as described in
|
|
|
|
* 5.1.6.4 [1] pg. 118.
|
|
|
|
*/
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
*frame_ctrl |= (meta->flags.ackreq << IEEE802154_FRAMECTRL_SHIFT_ACKREQ);
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
/* If the destination address is present, copy the PAN ID and one of the
|
|
|
|
* addresses, depending on mode, into the MHR.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (meta->destaddr.mode != IEEE802154_ADDRMODE_NONE)
|
|
|
|
{
|
2017-06-19 11:55:28 +02:00
|
|
|
IEEE802154_PANIDCOPY(&frame->io_data[mhr_len], meta->destaddr.panid);
|
2017-06-14 08:49:27 +02:00
|
|
|
mhr_len += 2;
|
|
|
|
|
|
|
|
if (meta->destaddr.mode == IEEE802154_ADDRMODE_SHORT)
|
|
|
|
{
|
2017-06-19 11:55:28 +02:00
|
|
|
IEEE802154_SADDRCOPY(&frame->io_data[mhr_len], meta->destaddr.saddr);
|
2017-06-14 08:49:27 +02:00
|
|
|
mhr_len += 2;
|
|
|
|
}
|
|
|
|
else if (meta->destaddr.mode == IEEE802154_ADDRMODE_EXTENDED)
|
|
|
|
{
|
2017-06-19 11:55:28 +02:00
|
|
|
IEEE802154_EADDRCOPY(&frame->io_data[mhr_len], meta->destaddr.eaddr);
|
|
|
|
mhr_len += IEEE802154_EADDRSIZE;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the destination addr mode inside the frame control field */
|
|
|
|
|
|
|
|
*frame_ctrl |= (meta->destaddr.mode << IEEE802154_FRAMECTRL_SHIFT_DADDR);
|
|
|
|
|
|
|
|
/* From this point on, we need exclusive access to the privmac struct */
|
|
|
|
|
2017-07-08 05:31:13 +02:00
|
|
|
ret = mac802154_lock(priv, true);
|
2017-06-14 08:49:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-06-20 19:33:03 +02:00
|
|
|
/* Should only fail if interrupted by a signal */
|
2017-06-20 19:23:31 +02:00
|
|
|
|
2017-06-20 19:50:04 +02:00
|
|
|
wlwarn("WARNING: mac802154_takesem failed: %d\n", ret);
|
2017-06-20 19:33:03 +02:00
|
|
|
return ret;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If both destination and source addressing information is present, the MAC
|
|
|
|
* sublayer shall compare the destination and source PAN identifiers.
|
|
|
|
* [1] pg. 41.
|
|
|
|
*/
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->srcmode != IEEE802154_ADDRMODE_NONE &&
|
2017-06-14 08:49:27 +02:00
|
|
|
meta->destaddr.mode != IEEE802154_ADDRMODE_NONE)
|
|
|
|
{
|
|
|
|
/* If the PAN identifiers are identical, the PAN ID Compression field
|
|
|
|
* shall be set to one, and the source PAN identifier shall be omitted
|
|
|
|
* from the transmitted frame. [1] pg. 41.
|
|
|
|
*/
|
|
|
|
|
2017-06-19 11:55:28 +02:00
|
|
|
if (IEEE802154_PANIDCMP(meta->destaddr.panid, priv->addr.panid))
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
|
|
|
*frame_ctrl |= IEEE802154_FRAMECTRL_PANIDCOMP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->srcmode != IEEE802154_ADDRMODE_NONE)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
|
|
|
/* If the destination address is not included, or if PAN ID Compression
|
|
|
|
* is off, we need to include the Source PAN ID.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((meta->destaddr.mode == IEEE802154_ADDRMODE_NONE) ||
|
|
|
|
(!(*frame_ctrl & IEEE802154_FRAMECTRL_PANIDCOMP)))
|
|
|
|
{
|
2017-06-19 11:55:28 +02:00
|
|
|
IEEE802154_PANIDCOPY(&frame->io_data[mhr_len], priv->addr.panid);
|
2017-06-14 08:49:27 +02:00
|
|
|
mhr_len += 2;
|
|
|
|
}
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->srcmode == IEEE802154_ADDRMODE_SHORT)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
2017-06-19 11:55:28 +02:00
|
|
|
IEEE802154_SADDRCOPY(&frame->io_data[mhr_len], priv->addr.saddr);
|
2017-06-14 08:49:27 +02:00
|
|
|
mhr_len += 2;
|
|
|
|
}
|
2017-07-17 07:20:01 +02:00
|
|
|
else if (meta->srcmode == IEEE802154_ADDRMODE_EXTENDED)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
2017-06-19 11:55:28 +02:00
|
|
|
IEEE802154_EADDRCOPY(&frame->io_data[mhr_len], priv->addr.eaddr);
|
|
|
|
mhr_len += IEEE802154_EADDRSIZE;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If this device is not the PAN coordinator, it shouldn't be sending
|
|
|
|
* frames with a source address mode of NONE
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->devmode != IEEE802154_DEVMODE_PANCOORD)
|
|
|
|
{
|
2017-06-20 19:23:31 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_sem;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the source addr mode inside the frame control field */
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
*frame_ctrl |= (meta->srcmode << IEEE802154_FRAMECTRL_SHIFT_SADDR);
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
/* Each time a data or a MAC command frame is generated, the MAC sublayer
|
|
|
|
* shall copy the value of macDSN into the Sequence Number field of the MHR
|
|
|
|
* of the outgoing frame and then increment it by one. [1] pg. 40.
|
|
|
|
*/
|
|
|
|
|
|
|
|
frame->io_data[2] = priv->dsn++;
|
|
|
|
|
|
|
|
/* The MAC header we just created must never have exceeded where the app
|
|
|
|
* data starts. This should never happen since the offset should have
|
|
|
|
* been set via the same logic to calculate the header length as the logic
|
|
|
|
* here that created the header
|
|
|
|
*/
|
|
|
|
|
2017-06-20 22:27:22 +02:00
|
|
|
wlinfo("mhr_len=%u\n", mhr_len);
|
2017-06-14 08:49:27 +02:00
|
|
|
DEBUGASSERT(mhr_len == frame->io_offset);
|
|
|
|
|
|
|
|
/* Allocate the txdesc, waiting if necessary, allow interruptions */
|
|
|
|
|
|
|
|
ret = mac802154_txdesc_alloc(priv, &txdesc, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-06-20 19:33:03 +02:00
|
|
|
/* Should only fail if interrupted by a signal while re-acquiring
|
|
|
|
* exclsem. So the lock is not held if a failure is returned.
|
|
|
|
*/
|
|
|
|
|
2017-06-20 19:50:04 +02:00
|
|
|
wlwarn("WARNING: mac802154_txdesc_alloc failed: %d\n", ret);
|
2017-06-20 19:33:03 +02:00
|
|
|
return ret;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 22:27:22 +02:00
|
|
|
/* Set the offset to 0 to include the header ( we do not want to
|
|
|
|
* modify the frame until AFTER the last place that -EINTR could
|
|
|
|
* be returned and could generate a retry. Subsequent error returns
|
|
|
|
* are fatal and no retry should occur.
|
|
|
|
*/
|
|
|
|
|
|
|
|
frame->io_offset = 0;
|
|
|
|
|
|
|
|
/* Then initialize the TX descriptor */
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
txdesc->conf->handle = meta->handle;
|
2017-06-14 08:49:27 +02:00
|
|
|
txdesc->frame = frame;
|
|
|
|
txdesc->frametype = IEEE802154_FRAME_DATA;
|
2018-03-28 17:50:11 +02:00
|
|
|
txdesc->ackreq = meta->flags.ackreq;
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
/* If the TxOptions parameter specifies that a GTS transmission is required,
|
|
|
|
* the MAC sublayer will determine whether it has a valid GTS as described
|
|
|
|
* 5.1.7.3. If a valid GTS could not be found, the MAC sublayer will discard
|
|
|
|
* the MSDU. If a valid GTS was found, the MAC sublayer will defer, if
|
|
|
|
* necessary, until the GTS. If the TxOptions parameter specifies that a GTS
|
|
|
|
* transmission is not required, the MAC sublayer will transmit the MSDU using
|
|
|
|
* either slotted CSMA-CA in the CAP for a beacon-enabled PAN or unslotted
|
|
|
|
* CSMA-CA for a nonbeacon-enabled PAN. Specifying a GTS transmission in the
|
|
|
|
* TxOptions parameter overrides an indirect transmission request.
|
|
|
|
* [1] pg. 118.
|
|
|
|
*/
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->flags.usegts)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
|
|
|
/* TODO: Support GTS transmission. This should just change where we link
|
|
|
|
* the transaction. Instead of going in the CSMA transaction list, it
|
|
|
|
* should be linked to the GTS' transaction list. We'll need to check if
|
|
|
|
* the GTS is valid, and then find the GTS, before linking. Note, we also
|
|
|
|
* don't have to try and kick-off any transmission here.
|
|
|
|
*/
|
|
|
|
|
2017-06-20 19:23:31 +02:00
|
|
|
ret = -ENOTSUP;
|
2017-06-20 22:27:22 +02:00
|
|
|
goto errout_with_txdesc;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the TxOptions parameter specifies that an indirect transmission is
|
|
|
|
* required and this primitive is received by the MAC sublayer of a
|
|
|
|
* coordinator, the data frame is sent using indirect transmission, as
|
|
|
|
* described in 5.1.5 and 5.1.6.3. [1]
|
|
|
|
*/
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->flags.indirect)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
|
|
|
/* If the TxOptions parameter specifies that an indirect transmission
|
|
|
|
* is required and if the device receiving this primitive is not a
|
|
|
|
* coordinator, the destination address is not present, or the
|
|
|
|
* TxOptions parameter also specifies a GTS transmission, the indirect
|
|
|
|
* transmission option will be ignored. [1]
|
|
|
|
*
|
|
|
|
* NOTE: We don't just ignore the parameter. Instead, we throw an
|
|
|
|
* error, since this really shouldn't be happening.
|
|
|
|
*/
|
|
|
|
|
2017-06-15 18:46:48 +02:00
|
|
|
if (priv->devmode >= IEEE802154_DEVMODE_COORD &&
|
2017-06-14 08:49:27 +02:00
|
|
|
meta->destaddr.mode != IEEE802154_ADDRMODE_NONE)
|
|
|
|
{
|
|
|
|
/* Copy in a reference to the destination address to assist in
|
|
|
|
* searching when data is requested.
|
|
|
|
*/
|
|
|
|
|
|
|
|
memcpy(&txdesc->destaddr, &meta->destaddr,
|
|
|
|
sizeof(struct ieee802154_addr_s));
|
|
|
|
mac802154_setupindirect(priv, txdesc);
|
2017-07-08 05:31:13 +02:00
|
|
|
mac802154_unlock(priv)
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-20 19:23:31 +02:00
|
|
|
ret = -EINVAL;
|
2017-06-20 22:27:22 +02:00
|
|
|
goto errout_with_txdesc;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Link the transaction into the CSMA transaction list */
|
|
|
|
|
|
|
|
sq_addlast((FAR sq_entry_t *)txdesc, &priv->csma_queue);
|
|
|
|
|
|
|
|
/* We no longer need to have the MAC layer locked. */
|
|
|
|
|
2017-07-08 05:31:13 +02:00
|
|
|
mac802154_unlock(priv)
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
/* Notify the radio driver that there is data available */
|
|
|
|
|
|
|
|
priv->radio->txnotify(priv->radio, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2017-06-20 19:23:31 +02:00
|
|
|
|
2017-06-20 22:27:22 +02:00
|
|
|
errout_with_txdesc:
|
2017-06-20 23:00:23 +02:00
|
|
|
/* Free TX the descriptor, but preserve the IOB. */
|
|
|
|
|
|
|
|
txdesc->frame = NULL;
|
|
|
|
mac802154_txdesc_free(priv, txdesc);
|
2017-06-20 22:27:22 +02:00
|
|
|
|
2017-06-20 19:23:31 +02:00
|
|
|
errout_with_sem:
|
2017-07-08 05:31:13 +02:00
|
|
|
mac802154_unlock(priv)
|
2017-06-20 19:23:31 +02:00
|
|
|
return ret;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Internal MAC Functions
|
|
|
|
****************************************************************************/
|