lwmac_internal.c
6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (C) 2015 Daniel Krebs
* 2016 INRIA
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup net_gnrc_lwmac
* @{
*
* @file
* @brief Implementation of internal functions of LWMAC
*
* @author Daniel Krebs <github@daniel-krebs.net>
* @author Shuguo Zhuo <shuguo.zhuo@inria.fr>
* @}
*/
#include <stdbool.h>
#include "periph/rtt.h"
#include "net/gnrc.h"
#include "net/gnrc/mac/mac.h"
#include "net/gnrc/netdev.h"
#include "net/gnrc/lwmac/lwmac.h"
#include "include/lwmac_internal.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
int _gnrc_lwmac_parse_packet(gnrc_pktsnip_t *pkt, gnrc_lwmac_packet_info_t *info)
{
gnrc_netif_hdr_t *netif_hdr;
gnrc_pktsnip_t *lwmac_snip;
gnrc_lwmac_hdr_t *lwmac_hdr;
assert(info != NULL);
assert(pkt != NULL);
netif_hdr = (gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_NETIF))->data;
if (netif_hdr == NULL) {
return -1;
}
/* Dissect LWMAC header, Every frame has header as first member */
lwmac_hdr = (gnrc_lwmac_hdr_t *) pkt->data;
switch (lwmac_hdr->type) {
case GNRC_LWMAC_FRAMETYPE_WR: {
lwmac_snip = gnrc_pktbuf_mark(pkt, sizeof(gnrc_lwmac_frame_wr_t),
GNRC_NETTYPE_LWMAC);
break;
}
case GNRC_LWMAC_FRAMETYPE_WA: {
lwmac_snip = gnrc_pktbuf_mark(pkt, sizeof(gnrc_lwmac_frame_wa_t),
GNRC_NETTYPE_LWMAC);
break;
}
case GNRC_LWMAC_FRAMETYPE_DATA_PENDING:
case GNRC_LWMAC_FRAMETYPE_DATA: {
lwmac_snip = gnrc_pktbuf_mark(pkt, sizeof(gnrc_lwmac_frame_data_t),
GNRC_NETTYPE_LWMAC);
break;
}
case GNRC_LWMAC_FRAMETYPE_BROADCAST: {
lwmac_snip = gnrc_pktbuf_mark(pkt, sizeof(gnrc_lwmac_frame_broadcast_t),
GNRC_NETTYPE_LWMAC);
break;
}
default: {
return -2;
}
}
/* Memory location may have changed while marking */
lwmac_hdr = lwmac_snip->data;
if (lwmac_hdr->type == GNRC_LWMAC_FRAMETYPE_WA) {
/* WA is broadcast, so get dst address out of header instead of netif */
info->dst_addr = ((gnrc_lwmac_frame_wa_t *)lwmac_hdr)->dst_addr;
}
else if (lwmac_hdr->type == GNRC_LWMAC_FRAMETYPE_WR) {
/* WR is broadcast, so get dst address out of header instead of netif */
info->dst_addr = ((gnrc_lwmac_frame_wr_t *)lwmac_hdr)->dst_addr;
}
else if (netif_hdr->dst_l2addr_len) {
info->dst_addr.len = netif_hdr->dst_l2addr_len;
memcpy(info->dst_addr.addr,
gnrc_netif_hdr_get_dst_addr(netif_hdr),
netif_hdr->dst_l2addr_len);
}
if (netif_hdr->src_l2addr_len) {
info->src_addr.len = netif_hdr->src_l2addr_len;
memcpy(info->src_addr.addr,
gnrc_netif_hdr_get_src_addr(netif_hdr),
netif_hdr->src_l2addr_len);
}
info->header = lwmac_hdr;
return 0;
}
void _gnrc_lwmac_set_netdev_state(gnrc_netdev_t *gnrc_netdev, netopt_state_t devstate)
{
gnrc_netdev->dev->driver->set(gnrc_netdev->dev,
NETOPT_STATE,
&devstate,
sizeof(devstate));
#if (GNRC_LWMAC_ENABLE_DUTYCYLE_RECORD == 1)
if (devstate == NETOPT_STATE_IDLE) {
if (!(gnrc_netdev->lwmac.lwmac_info & GNRC_LWMAC_RADIO_IS_ON)) {
gnrc_netdev->lwmac.last_radio_on_time_ticks = rtt_get_counter();
gnrc_netdev->lwmac.lwmac_info |= GNRC_LWMAC_RADIO_IS_ON;
}
return;
}
else if ((devstate == NETOPT_STATE_SLEEP) &&
(gnrc_netdev->lwmac.lwmac_info & GNRC_LWMAC_RADIO_IS_ON)) {
gnrc_netdev->lwmac.radio_off_time_ticks = rtt_get_counter();
gnrc_netdev->lwmac.awake_duration_sum_ticks +=
(gnrc_netdev->lwmac.radio_off_time_ticks -
gnrc_netdev->lwmac.last_radio_on_time_ticks);
gnrc_netdev->lwmac.lwmac_info &= ~GNRC_LWMAC_RADIO_IS_ON;
}
#endif
}
netopt_state_t _gnrc_lwmac_get_netdev_state(gnrc_netdev_t *gnrc_netdev)
{
netopt_state_t state;
if (0 < gnrc_netdev->dev->driver->get(gnrc_netdev->dev,
NETOPT_STATE,
&state,
sizeof(state))) {
return state;
}
return -1;
}
int _gnrc_lwmac_dispatch_defer(gnrc_pktsnip_t *buffer[], gnrc_pktsnip_t *pkt)
{
assert(buffer != NULL);
assert(pkt != NULL);
/* We care about speed here, so assume packet structure */
assert(pkt->next->type == GNRC_NETTYPE_LWMAC);
assert(pkt->next->next->type == GNRC_NETTYPE_NETIF);
gnrc_lwmac_frame_broadcast_t *bcast = NULL;
if (((gnrc_lwmac_hdr_t *)pkt->next->data)->type == GNRC_LWMAC_FRAMETYPE_BROADCAST) {
bcast = pkt->next->data;
}
for (unsigned i = 0; i < GNRC_MAC_DISPATCH_BUFFER_SIZE; i++) {
/* Buffer will be filled bottom-up and emptied completely so no holes */
if (buffer[i] == NULL) {
buffer[i] = pkt;
return 0;
}
else if (bcast &&
(((gnrc_lwmac_hdr_t *)buffer[i]->next->data)->type == GNRC_LWMAC_FRAMETYPE_BROADCAST) &&
(bcast->seq_nr == ((gnrc_lwmac_frame_broadcast_t *)buffer[i]->next->data)->seq_nr)) {
/* Filter same broadcasts, compare sequence number */
gnrc_netif_hdr_t *hdr_queued, *hdr_new;
hdr_new = pkt->next->next->data;
hdr_queued = buffer[i]->next->next->data;
/* Sequence numbers match, compare source addresses */
if ((hdr_new->src_l2addr_len == hdr_queued->src_l2addr_len) &&
(memcmp(gnrc_netif_hdr_get_src_addr(hdr_new),
gnrc_netif_hdr_get_src_addr(hdr_queued),
hdr_new->src_l2addr_len) == 0)) {
/* Source addresses match, same packet */
DEBUG("[LWMAC] Found duplicate broadcast packet, dropping\n");
gnrc_pktbuf_release(pkt);
return -2;
}
}
}
DEBUG("[LWMAC] Dispatch buffer full, dropping packet\n");
gnrc_pktbuf_release(pkt);
return -1;
}