2017-06-14 08:49:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* wireless/ieee802154/mac802154_get_mhrlen.c
|
|
|
|
*
|
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 "mac802154.h"
|
|
|
|
#include "mac802154_internal.h"
|
|
|
|
|
|
|
|
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-04-13 10:08:03 +02:00
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Map between ieee802154_addrmode_e enum and actual address length */
|
|
|
|
|
|
|
|
static const uint8_t mac802154_addr_length[4] =
|
|
|
|
{
|
|
|
|
0, 0, 2, 8
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2017-06-14 08:49:27 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mac802154_get_mhrlen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the MAC header length given the frame meta-data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mac802154_get_mhrlen(MACHANDLE mac,
|
|
|
|
FAR const struct ieee802154_frame_meta_s *meta)
|
|
|
|
{
|
|
|
|
FAR struct ieee802154_privmac_s *priv =
|
|
|
|
(FAR struct ieee802154_privmac_s *)mac;
|
|
|
|
int ret = 3; /* Always frame control (2 bytes) and seq. num (1 byte) */
|
|
|
|
|
2020-04-13 10:08:03 +02:00
|
|
|
/* Check to make sure both the dest address and the source address are not
|
|
|
|
* set to NONE
|
|
|
|
*/
|
2017-06-14 08:49:27 +02:00
|
|
|
|
|
|
|
if (meta->destaddr.mode == IEEE802154_ADDRMODE_NONE &&
|
2017-07-17 07:20:01 +02:00
|
|
|
meta->srcmode == IEEE802154_ADDRMODE_NONE)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:08:03 +02:00
|
|
|
/* The source address can only be set to NONE
|
|
|
|
* if the device is the PAN coord
|
|
|
|
*/
|
2017-06-14 08:49:27 +02:00
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->srcmode == IEEE802154_ADDRMODE_NONE &&
|
2017-06-14 08:49:27 +02:00
|
|
|
priv->devmode != IEEE802154_DEVMODE_PANCOORD)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the destination address length */
|
|
|
|
|
|
|
|
ret += mac802154_addr_length[meta->destaddr.mode];
|
|
|
|
|
|
|
|
/* Add the source address length */
|
|
|
|
|
2020-04-13 10:08:03 +02:00
|
|
|
ret += mac802154_addr_length[meta->srcmode];
|
2017-06-14 08:49:27 +02:00
|
|
|
|
2020-04-13 10:08:03 +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
|
|
|
{
|
|
|
|
ret += 2; /* 2 bytes for destination PAN ID */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:08:03 +02:00
|
|
|
/* If we are here, PAN ID compression is off, so include the dest and
|
|
|
|
* source PAN ID if the respective address is included
|
2017-06-14 08:49:27 +02:00
|
|
|
*/
|
|
|
|
|
2017-07-17 07:20:01 +02:00
|
|
|
if (meta->srcmode != IEEE802154_ADDRMODE_NONE)
|
2017-06-14 08:49:27 +02:00
|
|
|
{
|
|
|
|
ret += 2; /* 2 bytes for source PAN ID */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meta->destaddr.mode != IEEE802154_ADDRMODE_NONE)
|
|
|
|
{
|
|
|
|
ret += 2; /* 2 bytes for destination PAN ID */
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|