Contiki-NG
csma.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35 * The 802.15.4 standard CSMA protocol (nonbeacon-enabled)
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  * Simon Duquennoy <simon.duquennoy@inria.fr>
39  */
40 
41 #include "net/mac/csma/csma.h"
43 #include "net/mac/mac-sequence.h"
44 #include "net/packetbuf.h"
45 #include "net/netstack.h"
46 
47 /* Log configuration */
48 #include "sys/log.h"
49 #define LOG_MODULE "CSMA"
50 #define LOG_LEVEL LOG_LEVEL_MAC
51 
52 /*---------------------------------------------------------------------------*/
53 static void
54 send_packet(mac_callback_t sent, void *ptr)
55 {
56  csma_output_packet(sent, ptr);
57 }
58 /*---------------------------------------------------------------------------*/
59 static void
60 input_packet(void)
61 {
62 #if CSMA_SEND_SOFT_ACK
63  int original_datalen;
64  uint8_t *original_dataptr;
65 
66  original_datalen = packetbuf_datalen();
67  original_dataptr = packetbuf_dataptr();
68 #endif
69 
70  if(packetbuf_datalen() == CSMA_ACK_LEN) {
71  /* Ignore ack packets */
72  LOG_DBG("ignored ack\n");
73  } else if(NETSTACK_FRAMER.parse() < 0) {
74  LOG_ERR("failed to parse %u\n", packetbuf_datalen());
75  } else if(!linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER),
78  LOG_WARN("not for us\n");
79  } else {
80  int duplicate = 0;
81 
82  /* Check for duplicate packet. */
83  duplicate = mac_sequence_is_duplicate();
84  if(duplicate) {
85  /* Drop the packet. */
86  LOG_WARN("drop duplicate link layer packet from ");
87  LOG_WARN_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
88  LOG_WARN_(", seqno %u\n", packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO));
89  } else {
91  }
92 
93 #if CSMA_SEND_SOFT_ACK
94  {
95  frame802154_t info154;
96  frame802154_parse(original_dataptr, original_datalen, &info154);
97  if(info154.fcf.frame_type == FRAME802154_DATAFRAME &&
98  info154.fcf.ack_required != 0 &&
99  linkaddr_cmp((linkaddr_t *)&info154.dest_addr,
100  &linkaddr_node_addr)) {
101  uint8_t ackdata[CSMA_ACK_LEN] = {0, 0, 0};
102 
103  ackdata[0] = FRAME802154_ACKFRAME;
104  ackdata[1] = 0;
105  ackdata[2] = info154.seq;
106  NETSTACK_RADIO.send(ackdata, CSMA_ACK_LEN);
107  }
108  }
109 #endif /* CSMA_SEND_SOFT_ACK */
110  if(!duplicate) {
111  LOG_INFO("received packet from ");
112  LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
113  LOG_INFO_(", seqno %u, len %u\n", packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO), packetbuf_datalen());
114  NETSTACK_NETWORK.input();
115  }
116  }
117 }
118 /*---------------------------------------------------------------------------*/
119 static int
120 on(void)
121 {
122  return NETSTACK_RADIO.on();
123 }
124 /*---------------------------------------------------------------------------*/
125 static int
126 off(void)
127 {
128  return NETSTACK_RADIO.off();
129 }
130 /*---------------------------------------------------------------------------*/
131 static void
132 init(void)
133 {
134  csma_output_init();
135  on();
136 }
137 /*---------------------------------------------------------------------------*/
138 const struct mac_driver csma_driver = {
139  "CSMA",
140  init,
141  send_packet,
142  input_packet,
143  on,
144  off
145 };
146 /*---------------------------------------------------------------------------*/
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:143
int(* on)(void)
Turn the MAC layer on.
Definition: mac.h:75
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:204
The 802.15.4 standard CSMA protocol (nonbeacon-enabled).
The structure of a MAC protocol driver in Contiki.
Definition: mac.h:62
The 802.15.4 standard CSMA protocol (nonbeacon-enabled)
static void send_packet(linkaddr_t *dest)
This function is called by the 6lowpan code to send out a packet.
Definition: sicslowpan.c:1475
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:500
int mac_sequence_is_duplicate(void)
Tell whether the packetbuf is a duplicate packet.
Definition: mac-sequence.c:72
Header file for MAC sequence numbers management
int(* off)(void)
Turn the MAC layer off.
Definition: mac.h:78
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:155
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
int packetbuf_holds_broadcast(void)
Checks whether the current packet is a broadcast.
Definition: packetbuf.c:231
uint8_t frame_type
3 bit.
Definition: frame802154.h:153
Parameters used by the frame802154_create() function.
Definition: frame802154.h:198
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
void(* init)(void)
Initialize the MAC driver.
Definition: mac.h:66
uint8_t seq
Sequence number.
Definition: frame802154.h:205
Header file for the Packet buffer (packetbuf) management
Include file for the Contiki low-layer network stack (NETSTACK)
void mac_sequence_register_seqno(void)
Register the sequence number of the packetbuf.
Definition: mac-sequence.c:101
uint8_t dest_addr[8]
Destination address.
Definition: frame802154.h:202
Stores data about an incoming packet.
Definition: tsch-types.h:146
Header file for the logging system
uint8_t ack_required
1 bit.
Definition: frame802154.h:156