2017-06-14 08:49:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* wireless/ieee802154/mac802154_data.c
|
|
|
|
*
|
2024-09-11 09:04:25 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-09-06 13:39:36 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-06-14 08:49:27 +02:00
|
|
|
*
|
2021-09-06 13:39:36 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-06-14 08:49:27 +02:00
|
|
|
*
|
2021-09-06 13:39:36 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-06-14 08:49:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-04-23 11:51:21 +02:00
|
|
|
* Public Functions
|
2017-06-14 08:49:27 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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,
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR struct iob_s *frame)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2019-08-10 19:04:08 +02:00
|
|
|
wlinfo("Accepting outbound 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.
|
|
|
|
*/
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
if ((frame->io_len - frame->io_offset) >
|
|
|
|
IEEE802154_MAX_SAFE_MAC_PAYLOAD_SIZE)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
2019-08-16 03:28:26 +02:00
|
|
|
*frame_ctrl |= (1 << IEEE802154_FRAMECTRL_SHIFT_VERSION);
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2018-12-19 19:41:27 +01:00
|
|
|
mhr_len += IEEE802154_PANIDSIZE;
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
if (meta->destaddr.mode == IEEE802154_ADDRMODE_SHORT)
|
|
|
|
{
|
2020-04-23 11:51:21 +02:00
|
|
|
IEEE802154_SADDRCOPY(&frame->io_data[mhr_len],
|
|
|
|
meta->destaddr.saddr);
|
2018-12-19 19:41:27 +01:00
|
|
|
mhr_len += IEEE802154_SADDRSIZE;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
else if (meta->destaddr.mode == IEEE802154_ADDRMODE_EXTENDED)
|
|
|
|
{
|
2019-08-25 01:59:26 +02:00
|
|
|
int index;
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
/* The IEEE 802.15.4 Standard is confusing with regards to
|
|
|
|
* byte-order for * extended address. More research discovers
|
|
|
|
* that the extended address should be sent in reverse-canonical
|
|
|
|
* form.
|
2019-08-10 20:25:13 +02:00
|
|
|
*/
|
|
|
|
|
2019-08-25 01:59:26 +02:00
|
|
|
for (index = IEEE802154_EADDRSIZE - 1; index >= 0; index--)
|
2019-08-10 20:25:13 +02:00
|
|
|
{
|
|
|
|
frame->io_data[mhr_len++] = meta->destaddr.eaddr[index];
|
|
|
|
}
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
wlwarn("WARNING: nxmutex_lock failed: %d\n", ret);
|
2017-06-20 19:33:03 +02:00
|
|
|
return ret;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
/* If both destination and source addressing information is present, the
|
|
|
|
* MAC sublayer shall compare the destination and source PAN identifiers.
|
2017-06-14 08:49:27 +02:00
|
|
|
* [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);
|
2018-12-19 19:41:27 +01:00
|
|
|
mhr_len += IEEE802154_PANIDSIZE;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2018-12-19 19:41:27 +01:00
|
|
|
mhr_len += IEEE802154_SADDRSIZE;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
2017-07-17 07:20:01 +02:00
|
|
|
else if (meta->srcmode == IEEE802154_ADDRMODE_EXTENDED)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
2019-08-25 01:59:26 +02:00
|
|
|
int index;
|
|
|
|
|
2020-04-23 11:51:21 +02:00
|
|
|
/* The IEEE 802.15.4 Standard is confusing with regards to
|
|
|
|
* byte-order for * extended address. More research discovers
|
|
|
|
* that the extended address should be sent in reverse-canonical
|
|
|
|
* form.
|
2019-08-10 20:25:13 +02:00
|
|
|
*/
|
|
|
|
|
2019-08-25 01:59:26 +02:00
|
|
|
for (index = IEEE802154_EADDRSIZE - 1; index >= 0; index--)
|
2019-08-10 20:25:13 +02:00
|
|
|
{
|
|
|
|
frame->io_data[mhr_len++] = priv->addr.eaddr[index];
|
|
|
|
}
|
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;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = mac802154_txdesc_alloc(priv, &txdesc);
|
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 while re-acquiring
|
2022-09-06 08:18:45 +02:00
|
|
|
* lock. So the lock is not held if a failure is returned.
|
2017-06-20 19:33:03 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-20 19:50:04 +02:00
|
|
|
wlwarn("WARNING: mac802154_txdesc_alloc failed: %d\n", ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 11:51:21 +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.
|
|
|
|
*/
|
2017-06-20 22:27:22 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-23 11:51:21 +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-06-14 08:49:27 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->flags.usegts)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
2020-04-23 11:51:21 +02:00
|
|
|
/* TODO:
|
|
|
|
* Support GTS transmission. This should just change where we link
|
2017-06-14 08:49:27 +02:00
|
|
|
* the transaction. Instead of going in the CSMA transaction list, it
|
2020-04-23 11:51:21 +02:00
|
|
|
* 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-14 08:49:27 +02:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
{
|
2020-04-23 11:51:21 +02:00
|
|
|
/* If the TxOptions parameter specifies that an indirect transmission
|
|
|
|
* is required and this primitive is received by the MAC sublayer of a
|
2017-06-14 08:49:27 +02:00
|
|
|
* 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
|
|
|
{
|
2020-04-23 11:51:21 +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]
|
2017-06-14 08:49:27 +02:00
|
|
|
*
|
|
|
|
* 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);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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. */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
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:
|
2020-04-23 11:51:21 +02:00
|
|
|
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&priv->lock);
|
2017-06-20 19:23:31 +02:00
|
|
|
return ret;
|
2017-06-14 08:49:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Internal MAC Functions
|
|
|
|
****************************************************************************/
|