forked from anroOfCode/linux-cc2520-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpl.c
190 lines (158 loc) · 3.77 KB
/
lpl.c
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
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/hrtimer.h>
#include "lpl.h"
#include "packet.h"
#include "cc2520.h"
struct cc2520_interface *lpl_top;
struct cc2520_interface *lpl_bottom;
static int cc2520_lpl_tx(u8 * buf, u8 len);
static void cc2520_lpl_tx_done(u8 status);
static void cc2520_lpl_rx_done(u8 *buf, u8 len);
static enum hrtimer_restart cc2520_lpl_timer_cb(struct hrtimer *timer);
static void cc2520_lpl_start_timer(void);
static int lpl_window;
static int lpl_interval;
static bool lpl_enabled;
static struct hrtimer lpl_timer;
static u8* cur_tx_buf;
static u8 cur_tx_len;
static spinlock_t state_sl;
enum cc2520_lpl_state_enum {
CC2520_LPL_IDLE,
CC2520_LPL_TX,
CC2520_LPL_TIMER_EXPIRED
};
static int lpl_state;
int cc2520_lpl_init()
{
lpl_top->tx = cc2520_lpl_tx;
lpl_bottom->tx_done = cc2520_lpl_tx_done;
lpl_bottom->rx_done = cc2520_lpl_rx_done;
lpl_window = CC2520_DEF_LPL_LISTEN_WINDOW;
lpl_interval = CC2520_DEF_LPL_WAKEUP_INTERVAL;
lpl_enabled = true;
cur_tx_buf = kmalloc(PKT_BUFF_SIZE, GFP_KERNEL);
if (!cur_tx_buf) {
goto error;
}
spin_lock_init(&state_sl);
lpl_state = CC2520_LPL_IDLE;
hrtimer_init(&lpl_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
lpl_timer.function = &cc2520_lpl_timer_cb;
return 0;
error:
if (cur_tx_buf) {
kfree(cur_tx_buf);
cur_tx_buf = NULL;
}
return -EFAULT;
}
void cc2520_lpl_free()
{
if (cur_tx_buf) {
kfree(cur_tx_buf);
cur_tx_buf = NULL;
}
hrtimer_cancel(&lpl_timer);
}
static int cc2520_lpl_tx(u8 * buf, u8 len)
{
if (lpl_enabled) {
spin_lock(&state_sl);
if (lpl_state == CC2520_LPL_IDLE) {
lpl_state = CC2520_LPL_TX;
spin_unlock(&state_sl);
memcpy(cur_tx_buf, buf, len);
cur_tx_len = len;
lpl_bottom->tx(cur_tx_buf, cur_tx_len);
cc2520_lpl_start_timer();
}
else {
spin_unlock(&state_sl);
lpl_top->tx_done(-CC2520_TX_BUSY);
}
return 0;
}
else {
return lpl_bottom->tx(buf, len);
}
}
static void cc2520_lpl_tx_done(u8 status)
{
if (lpl_enabled) {
spin_lock(&state_sl);
if (cc2520_packet_requires_ack_wait(cur_tx_buf)) {
if (status == CC2520_TX_SUCCESS) {
lpl_state = CC2520_LPL_IDLE;
spin_unlock(&state_sl);
hrtimer_cancel(&lpl_timer);
lpl_top->tx_done(status);
}
else if (lpl_state == CC2520_LPL_TIMER_EXPIRED) {
lpl_state = CC2520_LPL_IDLE;
spin_unlock(&state_sl);
lpl_top->tx_done(-CC2520_TX_FAILED);
}
else {
spin_unlock(&state_sl);
//printk(KERN_INFO "[cc2520] - lpl retransmit.\n");
lpl_bottom->tx(cur_tx_buf, cur_tx_len);
}
}
else {
if (lpl_state == CC2520_LPL_TIMER_EXPIRED) {
lpl_state = CC2520_LPL_IDLE;
spin_unlock(&state_sl);
lpl_top->tx_done(CC2520_TX_SUCCESS);
}
else {
spin_unlock(&state_sl);
lpl_bottom->tx(cur_tx_buf, cur_tx_len);
}
}
}
else {
lpl_top->tx_done(status);
}
// if packet requires ack, examine status.
// if success terminate LPL window
// else if status != TIMER_EXPIRED resend
// else resend
}
static void cc2520_lpl_rx_done(u8 *buf, u8 len)
{
lpl_top->rx_done(buf, len);
}
static void cc2520_lpl_start_timer()
{
ktime_t kt;
kt = ktime_set(0, 1000 * (lpl_interval + 2 * lpl_window));
hrtimer_start(&lpl_timer, kt, HRTIMER_MODE_REL);
}
static enum hrtimer_restart cc2520_lpl_timer_cb(struct hrtimer *timer)
{
spin_lock(&state_sl);
if (lpl_state == CC2520_LPL_TX) {
lpl_state = CC2520_LPL_TIMER_EXPIRED;
spin_unlock(&state_sl);
}
else {
spin_unlock(&state_sl);
INFO((KERN_INFO "[cc2520] - lpl timer in improbable state.\n"));
}
return HRTIMER_NORESTART;
}
void cc2520_lpl_set_enabled(bool enabled)
{
lpl_enabled = enabled;
}
void cc2520_lpl_set_listen_length(int length)
{
lpl_window = length;
}
void cc2520_lpl_set_wakeup_interval(int interval)
{
lpl_interval = interval;
}