nes-proj/core/net/mac/csma.c

154 lines
4.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2010, Swedish Institute of Computer Science.
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
*
* This file is part of the Contiki operating system.
*
*/
/**
* \file
2017-05-17 19:47:48 +00:00
* The 802.15.4 standard CSMA protocol (nonbeacon-enabled)
* \author
* Adam Dunkels <adam@sics.se>
2017-05-17 19:47:48 +00:00
* Simon Duquennoy <simon.duquennoy@ri.se>
*/
#include "net/mac/csma.h"
2017-05-17 19:47:48 +00:00
#include "net/mac/csma-output.h"
#include "net/mac/mac-sequence.h"
#include "net/packetbuf.h"
#include "net/netstack.h"
2010-01-27 07:36:31 +00:00
#include <string.h>
#include <stdio.h>
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else /* DEBUG */
#define PRINTF(...)
#endif /* DEBUG */
2016-04-22 21:27:43 +00:00
/*---------------------------------------------------------------------------*/
static void
2017-05-17 19:47:48 +00:00
send_packet(mac_callback_t sent, void *ptr)
{
2017-05-17 19:47:48 +00:00
csma_output_packet(sent, ptr);
}
/*---------------------------------------------------------------------------*/
static void
2017-05-17 19:47:48 +00:00
input_packet(void)
{
2017-05-17 19:47:48 +00:00
#if CSMA_SEND_802154_ACK
int original_datalen;
uint8_t *original_dataptr;
2017-05-17 19:47:48 +00:00
original_datalen = packetbuf_datalen();
original_dataptr = packetbuf_dataptr();
#endif
2017-05-17 19:47:48 +00:00
#if CSMA_802154_AUTOACK
if(packetbuf_datalen() == CSMA_ACK_LEN) {
/* Ignore ack packets */
PRINTF("csma: ignored ack\n");
} else
#endif /* CSMA_802154_AUTOACK */
if(NETSTACK_FRAMER.parse() < 0) {
PRINTF("csma: failed to parse %u\n", packetbuf_datalen());
} else if(!linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
&linkaddr_node_addr) &&
!packetbuf_holds_broadcast()) {
PRINTF("csma: not for us\n");
} else {
2017-05-17 19:47:48 +00:00
int duplicate = 0;
#if CSMA_802154_AUTOACK || CSMA_802154_AUTOACK_HW
/* Check for duplicate packet. */
duplicate = mac_sequence_is_duplicate();
if(duplicate) {
/* Drop the packet. */
PRINTF("csma: drop duplicate link layer packet %u\n",
packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO));
} else {
mac_sequence_register_seqno();
}
2017-05-17 19:47:48 +00:00
#endif /* CSMA_802154_AUTOACK */
#if CSMA_SEND_802154_ACK
{
frame802154_t info154;
frame802154_parse(original_dataptr, original_datalen, &info154);
if(info154.fcf.frame_type == FRAME802154_DATAFRAME &&
info154.fcf.ack_required != 0 &&
linkaddr_cmp((linkaddr_t *)&info154.dest_addr,
&linkaddr_node_addr)) {
uint8_t ackdata[CSMA_ACK_LEN] = {0, 0, 0};
ackdata[0] = FRAME802154_ACKFRAME;
ackdata[1] = 0;
ackdata[2] = info154.seq;
NETSTACK_RADIO.send(ackdata, CSMA_ACK_LEN);
2013-11-20 12:40:56 +00:00
}
}
2017-05-17 19:47:48 +00:00
#endif /* CSMA_SEND_802154_ACK */
if(!duplicate) {
NETSTACK_NETWORK.input();
}
}
}
/*---------------------------------------------------------------------------*/
static int
on(void)
{
2017-05-17 13:09:19 +00:00
return NETSTACK_RADIO.on();
}
/*---------------------------------------------------------------------------*/
static int
2017-05-17 13:09:19 +00:00
off(void)
{
2017-05-17 13:09:19 +00:00
return NETSTACK_RADIO.off();
}
/*---------------------------------------------------------------------------*/
static void
init(void)
{
2017-05-17 19:47:48 +00:00
csma_output_init();
2017-05-17 13:09:19 +00:00
on();
}
/*---------------------------------------------------------------------------*/
const struct mac_driver csma_driver = {
"CSMA",
init,
send_packet,
input_packet,
on,
2017-05-17 12:34:29 +00:00
off
};
/*---------------------------------------------------------------------------*/