nuttx/net/sixlowpan/sixlowpan_framer.c

293 lines
9.3 KiB
C
Raw Normal View History

2017-03-30 23:38:56 +02:00
/****************************************************************************
* net/sixlowpan/sixlowpan_framer.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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
2017-05-08 00:58:49 +02:00
* 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
2017-03-30 23:38:56 +02:00
* without specific prior written permission.
*
2017-05-08 00:58:49 +02:00
* 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.
2017-03-30 23:38:56 +02:00
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
2017-03-30 23:38:56 +02:00
#include <debug.h>
#include "nuttx/net/net.h"
#include "nuttx/wireless/ieee802154/ieee802154_mac.h"
2017-03-30 23:38:56 +02:00
#include "sixlowpan/sixlowpan_internal.h"
#ifdef CONFIG_NET_6LOWPAN
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sixlowpan_anyaddrnull
2017-03-30 23:38:56 +02:00
*
* Description:
* If the destination address is all zero in the MAC header buf, then it is
* broadcast on the 802.15.4 network.
2017-03-30 23:38:56 +02:00
*
* Input parameters:
* addr - The address to check
* addrlen - The length of the address in bytes
2017-03-30 23:38:56 +02:00
*
* Returned Value:
* True if the address is all zero.
2017-03-30 23:38:56 +02:00
*
****************************************************************************/
static bool sixlowpan_anyaddrnull(FAR const uint8_t *addr, uint8_t addrlen)
2017-03-30 23:38:56 +02:00
{
while (addrlen-- > 0)
2017-03-30 23:38:56 +02:00
{
if (addr[addrlen] != 0x00)
2017-03-30 23:38:56 +02:00
{
return false;
}
}
return true;
}
/****************************************************************************
* Name: sixlowpan_saddrnull
*
* Description:
* If the destination address is all zero in the MAC header buf, then it is
* broadcast on the 802.15.4 network.
*
* Input parameters:
* eaddr - The short address to check
*
* Returned Value:
* The address length associated with the address mode.
*
****************************************************************************/
static inline bool sixlowpan_saddrnull(FAR const uint8_t *saddr)
{
return sixlowpan_anyaddrnull(saddr, NET_6LOWPAN_SADDRSIZE);
}
/****************************************************************************
* Name: sixlowpan_eaddrnull
*
* Description:
* If the destination address is all zero in the MAC header buf, then it is
* broadcast on the 802.15.4 network.
*
* Input parameters:
* eaddr - The extended address to check
*
* Returned Value:
* The address length associated with the address mode.
*
****************************************************************************/
static inline bool sixlowpan_eaddrnull(FAR const uint8_t *eaddr)
{
return sixlowpan_anyaddrnull(eaddr, NET_6LOWPAN_EADDRSIZE);
}
2017-03-30 23:38:56 +02:00
/****************************************************************************
* Public Functions
2017-03-30 23:38:56 +02:00
****************************************************************************/
/****************************************************************************
* Name: sixlowpan_meta_data
2017-03-30 23:38:56 +02:00
*
* Description:
* Based on the collected attributes and addresses, construct the MAC meta
* data structure that we need to interface with the IEEE802.15.4 MAC.
2017-03-30 23:38:56 +02:00
*
* Input Parameters:
* ieee - IEEE 802.15.4 MAC driver state reference.
* pktmeta - Meta-data specific to the current outgoing frame
* meta - Location to return the corresponding meta data.
* paylen - The size of the data payload to be sent.
2017-03-30 23:38:56 +02:00
*
* Returned Value:
* Ok is returned on success; Othewise a negated errno value is returned.
*
* Assumptions:
* Called with the network locked.
2017-03-30 23:38:56 +02:00
*
****************************************************************************/
int sixlowpan_meta_data(FAR struct ieee802154_driver_s *ieee,
FAR const struct packet_metadata_s *pktmeta,
FAR struct ieee802154_frame_meta_s *meta,
uint16_t paylen)
2017-03-30 23:38:56 +02:00
{
bool rcvrnull;
/* Initialize all settings to all zero */
2017-03-30 23:38:56 +02:00
memset(meta, 0, sizeof(struct ieee802154_frame_meta_s));
2017-03-30 23:38:56 +02:00
/* Source address mode */
2017-03-30 23:38:56 +02:00
meta->srcaddr_mode = pktmeta->sextended != 0?
IEEE802154_ADDRMODE_EXTENDED :
IEEE802154_ADDRMODE_SHORT;
2017-03-30 23:38:56 +02:00
/* Check for a broadcast destination address (all zero) */
2017-03-30 23:38:56 +02:00
if (pktmeta->dextended != 0)
{
/* Extended destination address mode */
rcvrnull = sixlowpan_eaddrnull(pktmeta->dest.eaddr.u8);
}
else
{
/* Short destination address mode */
rcvrnull = sixlowpan_saddrnull(pktmeta->dest.saddr.u8);
}
2017-03-30 23:38:56 +02:00
if (rcvrnull)
{
meta->msdu_flags.ack_tx = TRUE;
2017-03-30 23:38:56 +02:00
}
/* Destination address */
2017-03-30 23:38:56 +02:00
/* If the output address is NULL, then it is broadcast on the 802.15.4
* network.
2017-03-30 23:38:56 +02:00
*/
if (rcvrnull)
{
/* Broadcast requires short address mode. */
meta->destaddr.mode = IEEE802154_ADDRMODE_SHORT;
meta->destaddr.saddr[0] = 0;
meta->destaddr.saddr[1] = 0;
2017-03-30 23:38:56 +02:00
}
else if (pktmeta->dextended != 0)
{
/* Extended destination address mode */
meta->destaddr.mode = IEEE802154_ADDRMODE_EXTENDED;
sixlowpan_eaddrcopy(&meta->destaddr.eaddr, pktmeta->dest.eaddr.u8);
}
2017-03-30 23:38:56 +02:00
else
{
/* Short destination address mode */
meta->destaddr.mode = IEEE802154_ADDRMODE_SHORT;
sixlowpan_saddrcopy(&meta->destaddr.saddr, pktmeta->dest.saddr.u8);
}
2017-03-30 23:38:56 +02:00
IEEE802154_SADDRCOPY(meta->destaddr.panid, pktmeta->dpanid);
/* Handle associated with MSDU. Will increment once per packet, not
* necesarily per frame: The same MSDU handle will be used for each
* fragment of a disassembled packet.
*/
meta->msdu_handle = ieee->i_msdu_handle++;
#ifdef CONFIG_IEEE802154_SECURITY
# warning CONFIG_IEEE802154_SECURITY not yet supported
#endif
#ifdef CONFIG_IEEE802154_UWB
# warning CONFIG_IEEE802154_UWB not yet supported
#endif
/* Ranging left zero */
2017-03-30 23:38:56 +02:00
return OK;
}
2017-03-30 23:38:56 +02:00
/****************************************************************************
* Name: sixlowpan_frame_hdrlen
2017-03-30 23:38:56 +02:00
*
* Description:
* This function is before the first frame has been sent in order to
* determine what the size of the IEEE802.15.4 header will be. No frame
* buffer is required to make this determination.
*
* Input parameters:
* ieee - A reference IEEE802.15.4 MAC network device structure.
* meta - Meta data that describes the MAC header
2017-03-30 23:38:56 +02:00
*
* Returned Value:
* The frame header length is returnd on success; otherwise, a negated
* errno value is return on failure.
*
****************************************************************************/
int sixlowpan_frame_hdrlen(FAR struct ieee802154_driver_s *ieee,
FAR const struct ieee802154_frame_meta_s *meta)
2017-03-30 23:38:56 +02:00
{
return ieee->i_get_mhrlen(ieee, meta);
2017-03-30 23:38:56 +02:00
}
/****************************************************************************
* Name: sixlowpan_frame_submit
2017-03-30 23:38:56 +02:00
*
* Description:
* This function is called after eiether (1) the IEEE802.15.4 MAC driver
* polls for TX data or (2) after the IEEE802.15.4 MAC driver provides a
* new incoming frame and the network responds with an outgoing packet. It
* submits any new outgoing frame to the MAC.
2017-03-30 23:38:56 +02:00
*
* Input parameters:
* ieee - A reference IEEE802.15.4 MAC network device structure.
* meta - Meta data that describes the MAC header
* frame - The IOB containing the frame to be submitted.
2017-03-30 23:38:56 +02:00
*
* Returned Value:
* Zero (OK) is returned on success; otherwise, a negated errno value is
* return on any failure.
2017-03-30 23:38:56 +02:00
*
****************************************************************************/
int sixlowpan_frame_submit(FAR struct ieee802154_driver_s *ieee,
FAR const struct ieee802154_frame_meta_s *meta,
FAR struct iob_s *frame)
2017-03-30 23:38:56 +02:00
{
return ieee->i_req_data(ieee, meta, frame);
2017-03-30 23:38:56 +02:00
}
#endif /* CONFIG_NET_6LOWPAN */