This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
xbee2.c
663 lines (573 loc) · 14.9 KB
/
xbee2.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/completion.h>
#include <linux/tty.h>
#include <linux/skbuff.h>
#include <linux/sched.h>
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <net/mac802154.h>
#include <net/wpan-phy.h>
#define N_IEEE802154_XBEE 25
#define VERSION 1
#define XBEE_CHAR_NEWFRM 0x7e
#define ZIGBEE_EXPLICIT_RX_INDICATOR 0x91
enum {
STATE_WAIT_START1,
STATE_WAIT_START2,
STATE_WAIT_COMMAND,
STATE_WAIT_PARAM1,
STATE_WAIT_PARAM2,
STATE_WAIT_DATA
};
/*********************************************************************/
struct xb_device {
struct tty_struct *tty;
struct ieee802154_dev *dev;
/* locks the ldisc for the command */
struct mutex mutex;
/* command completition */
wait_queue_head_t frame_waitq;
struct sk_buff *frame;
int frame_esc;
int frame_len;
// struct list_head frame_pend;
/* Command (rx) processing */
int state;
};
struct xbee_frame {
struct list_head list;
int ack;
u16 len;
u8 id;
unsigned char *raw_data;
u8 csum;
/* parsed elements */
u8 fid;
u8 status;
unsigned char cmd[2];
u8 addr[IEEE802154_ADDR_LEN];
// u8 saddr[IEEE802154_SHORT_ALEN];
__le16 saddr;
u8 flags;
u8 rssi;
unsigned char *data;
};
static void
cleanup(struct xb_device *zbdev)
{
pr_debug("%s\n", __func__);
zbdev->state = STATE_WAIT_START1;
zbdev->frame = alloc_skb(SKB_MAX_ALLOC, GFP_KERNEL);
/* zbdev->id = 0;
zbdev->param1 = 0;
zbdev->param2 = 0;
zbdev->index = 0;
zbdev->pending_id = 0;
zbdev->pending_size = 0;
if (zbdev->pending_data)
{
kfree(zbdev->pending_data);
zbdev->pending_data = NULL;
}
*/
}
/*
* Callbacks from mac802154 to the driver. Must handle xmit().
*
* See net/mac802154/ieee802154_dev.c, include/net/mac802154.h,
* and net/mac802154/mac802154.h from linux-wsn.
*/
/**
* xbee_ieee802154_set_channel - Set radio for listening on specific channel.
*
* @dev: ...
* @page: ...
* @channel: ...
*/
static int xbee_ieee802154_set_channel(struct ieee802154_dev *dev,
int page, int channel){
pr_debug("%s\n", __func__);
return 0;
}
/**
* xbee_ieee802154_ed - Handler that 802.15.4 module calls for Energy Detection.
*
* @dev: ...
* @level: ...
*/
static int xbee_ieee802154_ed(struct ieee802154_dev *dev, u8 *level){
pr_debug("%s\n", __func__);
return 0;
}
/**
* xbee_ieee802154_addr - ...
*
* @dev: ...
* @addr: ...
*/
static int xbee_ieee802154_addr(struct ieee802154_dev *dev,
u8 addr[IEEE802154_ADDR_LEN]){
pr_debug("%s\n", __func__);
return 0;
}
/**
* xbee_ieee802154_xmit - Handler that 802.15.4 module calls for each transmitted frame.
*
* @dev: ...
* @skb: ...
*/
static int xbee_ieee802154_xmit(struct ieee802154_dev *dev,
struct sk_buff *skb)
{
pr_debug("%s\n", __func__);
return 0;
}
static int xbee_ieee802154_filter(struct ieee802154_dev *dev,
struct ieee802154_hw_addr_filt *filt,
unsigned long changed)
{
pr_debug("%s\n", __func__);
return 0;
}
/**
* xbee_ieee802154_start - For device initialisation before the first interface is attached.
*
* @dev: ...
*/
static int xbee_ieee802154_start(struct ieee802154_dev *dev)
{
struct xb_device *zbdev;
int ret = 0;
pr_debug("%s\n", __func__);
zbdev = dev->priv;
if (NULL == zbdev) {
printk(KERN_ERR "%s: wrong phy\n", __func__);
return -EINVAL;
}
pr_debug("%s end (retval: %d)\n", __func__, ret);
return ret;
}
/**
* xbee_ieee802154_stop - For device cleanup after last interface is removed.
*
* @dev: ...
*/
static void xbee_ieee802154_stop(struct ieee802154_dev *dev){
struct xb_device *zbdev;
pr_debug("%s\n", __func__);
zbdev = dev->priv;
if (NULL == zbdev) {
printk(KERN_ERR "%s: wrong phy\n", __func__);
return;
}
pr_debug("%s end\n", __func__);
}
static int xbee_frm_xbee_verify(struct sk_buff *skb){
int length;
uint8_t checksum = 0;
uint16_t i;
pr_debug("%s skb->len %d\n", __func__, skb->len);
if (skb->len > 2)
{
length = (*skb->data << 8) & 0xff00; // MSB
length |= *(skb->data+1) & 0xff; // LSB
pr_debug("%s length %d\n", __func__, length);
if (skb->len == length+3){
for(i=0;i<length;++i) {
checksum += *(skb->data+i+2);
}
checksum = 0xFF - checksum;
if (checksum==*(skb->data+length+3-1))
return 0;
}
}
return 1;
}
static int xbee_frm_xbee_id(struct sk_buff *skb){
pr_debug("%s\n", __func__);
return *(skb->data+2);
}
static int xbee_frame_peak_done(struct xb_device *xbdev){
pr_debug("%s\n", __func__);
return !xbee_frm_xbee_verify(xbdev->frame);
}
static void xbee_frame_recv_rx64(struct xb_device *xbdev,
struct sk_buff *skb)
{
struct sk_buff *lskb;
pr_debug("%s\n", __func__);
lskb = alloc_skb(skb->len, GFP_ATOMIC);
skb_put(lskb, skb->len);
skb_copy_to_linear_data(lskb, skb->data, skb->len);
ieee802154_rx_irqsafe(xbdev->dev, lskb, skb->len);
// ieee802154_rx(xbdev->dev, skb, rssi);
}
/**
* xbee_recv_frame - ...
*
* @xbdev: ...
* @frame: ...
*
* Verify the XBee frame, then take appropriate action depending on the
* frame type.
*/
static void xbee_frm_xbee_recv(struct xb_device *xbdev,
struct sk_buff *skb)
{
u8 id;
int err;
pr_debug("%s\n", __func__);
/* verify length and checksum */
err = xbee_frm_xbee_verify(skb);
if (err) {
// XBEE_WARN("dropping invalid frame 0x%x", err);
printk(KERN_WARNING "%s: dropping invalid frame 0x%x\n", __func__, err);
return;
}
id = xbee_frm_xbee_id(skb);
switch (id) {
case ZIGBEE_EXPLICIT_RX_INDICATOR:
xbee_frame_recv_rx64(xbdev, skb);
break;
/* case XBEE_FRM_STAT:
xbee_frame_recv_stat(skb);
break;
case XBEE_FRM_CMDR:
xbee_frame_recv_cmdr(skb);
break;
case XBEE_FRM_RCMDR:
xbee_frame_recv_rcmdr(skb);
break;
case XBEE_FRM_TXSTAT:
xbee_frame_recv_txstat(skb);
break;
case XBEE_FRM_RX64:
xbee_frame_recv_rx64(xbdev, skb);
break;
case XBEE_FRM_RX16:
xbee_frame_recv_rx16(skb);
break;
case XBEE_FRM_RX64IO:
case XBEE_FRM_RX16IO:
XBEE_WARN("received unimplemented frame type 0x%x", id);
skb_free(skb);
break;
case XBEE_FRM_CMD:
case XBEE_FRM_CMDQ:
case XBEE_FRM_RCMD:
case XBEE_FRM_TX64:
case XBEE_FRM_TX16:
XBEE_WARN("received tx-only frame type 0x%x", id);
skb_free(skb);
break;*/
default:
// XBEE_WARN("received unknown frame type 0x%x", id);
pr_debug("%s received unknown frame type 0x%x\n", __func__, id);
// kfree_skb(skb);
break;
}
}
static int xbee_frm_new(struct xb_device *xbdev,
struct sk_buff **skb)
{
struct sk_buff *new_skb;
new_skb = alloc_skb(SKB_MAX_ALLOC, GFP_KERNEL);
if (new_skb == NULL) {
// XBEE_ERR("failed to allocate new skb");
printk(KERN_ERR "%s: failed to allocate new skb\n", __func__);
return 1;
} else {
// pr_debug("%s 1 skb %d\n", __func__, skb);
// pr_debug("%s 1 *skb %d\n", __func__, *skb);
*skb = xbdev->frame;
// pr_debug("%s 2 skb %d\n", __func__, skb);
// pr_debug("%s 2 *skb %d\n", __func__, *skb);
xbdev->frame = new_skb;
//// *skb = old_skb;
xbdev->frame_len = 0;
return 0;
}
}
/*****************************************************************************
* Line discipline interface for IEEE 802.15.4 serial device
*****************************************************************************/
/**
* xbee_ieee802154_ops - ieee802154 MCPS ops.
*
* This is part of linux-wsn. It is similar to netdev_ops.
*/
static struct ieee802154_ops xbee_ieee802154_ops = {
.owner = THIS_MODULE,
.xmit = xbee_ieee802154_xmit,
.ed = xbee_ieee802154_ed,
.set_channel = xbee_ieee802154_set_channel,
.set_hw_addr_filt = xbee_ieee802154_filter,
.start = xbee_ieee802154_start,
.stop = xbee_ieee802154_stop,
.ieee_addr = xbee_ieee802154_addr,
};
/*
* See Documentation/tty.txt for details.
*/
/**
* xbee_ldisc_open - Initialize line discipline and register with ieee802154.
*
* @tty: TTY info for line.
*
* Called from a process context to change the TTY line discipline.
* Called from a process context
*
*
*/
static int xbee_ldisc_open(struct tty_struct *tty)
{
struct ieee802154_dev *dev;
struct xb_device *xbdev = tty->disc_data;
int err;
pr_debug("%s\n", __func__);
if (!capable(CAP_NET_ADMIN))
return -EPERM;
/*
* TODO: The traditional method to check if another line discipline
* is still installed has been to check tty->disc_data for a non-NULL
* address, even though line disciplines are under no obligation
* to clear it upon uninstallation.
*/
if (tty->disc_data != NULL)
return -EBUSY;
// if (tty->ops->stop)
// tty->ops->stop(tty);
tty_driver_flush_buffer(tty);
dev = ieee802154_alloc_device(sizeof(*xbdev), &xbee_ieee802154_ops);
if (!dev)
return -ENOMEM;
xbdev = dev->priv;
xbdev->dev = dev;
mutex_init(&xbdev->mutex);
// init_completion(&xbdev->cmd_resp_done);
init_waitqueue_head(&xbdev->frame_waitq);
dev->extra_tx_headroom = 0;
/* only 2.4 GHz band */
dev->phy->channels_supported[0] = 0x7fff800;
dev->flags = IEEE802154_HW_OMIT_CKSUM;
dev->parent = tty->dev;
xbdev->tty = tty_kref_get(tty);
cleanup(xbdev);
tty->disc_data = xbdev;
// tty->receive_room = MAX_DATA_SIZE;
tty->receive_room = 65536;
// dev->ml_priv = &xbee_ieee802154_mlme_ops;
if (tty->ldisc->ops->flush_buffer)
tty->ldisc->ops->flush_buffer(tty);
tty_driver_flush_buffer(tty);
err = ieee802154_register_device(dev);
if (err) {
// XBEE_ERROR("%s: device register failed\n", __func__);
printk(KERN_ERR "%s: device register failed\n", __func__);
goto err;
}
return 0;
err:
tty->disc_data = NULL;
tty_kref_put(tty);
xbdev->tty = NULL;
ieee802154_unregister_device(xbdev->dev);
ieee802154_free_device(xbdev->dev);
return err;
}
/**
* xbee_ldisc_close - Close line discipline and unregister with ieee802154.
*
* @tty: TTY info for line.
*/
static void xbee_ldisc_close(struct tty_struct *tty){
struct xb_device *zbdev;
pr_debug("%s\n", __func__);
zbdev = tty->disc_data;
if (NULL == zbdev) {
printk(KERN_WARNING "%s: match is not found\n", __func__);
return;
}
tty->disc_data = NULL;
tty_kref_put(tty);
zbdev->tty = NULL;
mutex_destroy(&zbdev->mutex);
ieee802154_unregister_device(zbdev->dev);
tty_ldisc_flush(tty);
tty_driver_flush_buffer(tty);
ieee802154_free_device(zbdev->dev);
}
/**
* xbee_ldisc_ioctl - ...
*
* @tty: TTY info for line.
* @file: ...
* @cmd: ...
* @arg: ...
*/
static int xbee_ldisc_ioctl(struct tty_struct *tty, struct file *file,
unsigned int cmd, unsigned long arg){
pr_debug("%s\n", __func__);
return 0;
}
/**
* xbee_ldisc_hangup - Hang up line discipline.
*
* @tty: TTY info for line.
*
* "The line discipline should cease I/O to the tty.
* No further calls into the ldisc code will occur."
*/
static int xbee_ldisc_hangup(struct tty_struct *tty)
{
pr_debug("%s\n", __func__);
return 0;
}
/**
* xbee_ldisc_recv_buf - Receive serial bytes.
*
* @tty: TTY info for line.
* @cp: ...
* @fp: ...
* @count: ...
*
* Directly called from flush_to_ldisc() which is called from
* tty_flip_buffer_push(), either in IRQ context if low latency tty or from
* a normal worker thread otherwise.
*/
static void xbee_ldisc_recv_buf(struct tty_struct *tty,
const unsigned char *cp,
char *fp, int count)
{
// struct ieee802154_dev *dev;
struct xb_device *xbdev;
char c;
int ret;
struct sk_buff *skb;
pr_debug("%s\n", __func__);
/* Debug info */
// printk(KERN_INFO "%s, received %d bytes\n", __func__,
// count);
#ifdef DEBUG
print_hex_dump_bytes("ieee802154_tty_receive ", DUMP_PREFIX_NONE,
cp, count);
#endif
/* Actual processing */
xbdev = tty->disc_data;
if (NULL == xbdev) {
printk(KERN_ERR "%s(): record for tty is not found\n",
__func__);
return;
}
/* copy (i wish i could just lock and link them in an array) buffers */
while (count--) {
c = *cp++;
/* escape this byte */
/*
if (xbdev->frame_esc) {
c ^= 0x20;
xbdev->frame_esc = 0;
}
*/
// pr_debug("char %x\n", c);
switch (c) {
// case XBEE_CHAR_ESC: /* escape next byte */
// xbdev->frame_esc = 1;
// break;
case XBEE_CHAR_NEWFRM: /* new frame */
// XBEE_INFO("new frame");
pr_debug("new frame\n");
/* switch to new frame buffer */
// pr_debug("1 xbdev->frame->len %d\n", xbdev->frame->len);
// pr_debug("1 xbdev->frame %d\n", xbdev->frame);
// pr_debug("skb %d\n", skb);
// pr_debug("&skb %d\n", &skb);
ret = xbee_frm_new(xbdev, &skb);
// pr_debug("2 xbdev->frame->len %d\n", xbdev->frame->len);
// pr_debug("2 &xbdev->frame %d\n", xbdev->frame);
// pr_debug("skb %d\n", skb);
// pr_debug("&skb %d\n", &skb);
// pr_debug("skb->len %d\n", skb->len);
// if (unlikely(ret))
if (ret){
// XBEE_ERR("derp");
/* submit old frame buffer to stack */
}
else {
xbee_frm_xbee_recv(xbdev, skb);
kfree_skb(skb);
}
/* reset overflow status */
// overflow = 0;
break;
default:
pr_debug("append to frame buffer\n");
/* check for frame buffer overflow */
// if (overflow || xbdev->frame_len == XBEE_FRAME_MAXLEN) {
// overflow = 1;
// continue;
// }
/* append to frame buffer */
memcpy(skb_put(xbdev->frame, 1), &c, 1);
/* increase current frame buffer len */
xbdev->frame_len += 1;
// pr_debug("xbdev->frame_len %d\n", xbdev->frame_len);
// pr_debug("xbdev->frame->len %d\n", xbdev->frame->len);
break;
}
}
// if (overflow)
// XBEE_WARN("was buffer overflow");
/* peak at frame to check if completed */
if (xbee_frame_peak_done(xbdev)) {
ret = xbee_frm_new(xbdev, &skb);
if (ret){
// if (unlikely(ret))
// XBEE_ERR("derp");
}
else {
xbee_frm_xbee_recv(xbdev, skb);
kfree_skb(skb);
}
}
}
/*********************************************************************/
/**
* xbee_ldisc - TTY line discipline ops.
*/
static struct tty_ldisc_ops xbee_ldisc_ops = {
.owner = THIS_MODULE,
.magic = TTY_LDISC_MAGIC,
.name = "n_ieee802154_xbee",
// .flags = 0,
.open = xbee_ldisc_open,
.close = xbee_ldisc_close,
.ioctl = xbee_ldisc_ioctl,
.hangup = xbee_ldisc_hangup,
.receive_buf = xbee_ldisc_recv_buf,
};
static int __init xbee_init(void)
{
pr_debug("%s\n", __func__);
printk(KERN_INFO "Initializing ZigBee TTY interface\n");
if (tty_register_ldisc(N_IEEE802154_XBEE, &xbee_ldisc_ops) != 0) {
printk(KERN_ERR "%s: line discipline register failed\n",
__func__);
return -EINVAL;
}
return 0;
}
static void __exit xbee_exit(void)
{
pr_debug("%s\n", __func__);
if (tty_unregister_ldisc(N_IEEE802154_XBEE) != 0)
printk(KERN_CRIT
"failed to unregister ZigBee line discipline.\n");
}
module_init(xbee_init);
module_exit(xbee_exit);
MODULE_DESCRIPTION("Digi XBee IEEE 802.15.4 serial driver");
MODULE_ALIAS_LDISC(N_IEEE802154_XBEE);
//MODULE_LICENSE("Dual GPL/CC0");
MODULE_LICENSE("GPL");