-
Notifications
You must be signed in to change notification settings - Fork 0
/
ieee802154_xbee.c
1128 lines (874 loc) · 28.9 KB
/
ieee802154_xbee.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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Linux XBEE driver, IEEE 802.15.4 version
*
* Copyright (C) 2014 Carlo Vallati
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Written by:
* Carlo Vallati <carlo.vallati@iet.unipi.it>
* Fork of the original xbee based driver from https://github.com/robbles/xbee-driver
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <net/af_ieee802154.h>
#include <net/ieee802154_netdev.h>
#include <net/ieee802154.h>
#include <net/nl802154.h>
#include <net/wpan-phy.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/slab.h> /* kmalloc() */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/interrupt.h> /* mark_bh */
#include <linux/tty.h>
#include <linux/in.h>
#include <linux/netdevice.h> /* struct device, and other headers */
#include <linux/etherdevice.h> /* eth_type_trans */
#include <linux/ip.h> /* struct iphdr */
#include <linux/tcp.h> /* struct tcphdr */
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/ip.h>
#include <net/checksum.h>
static int escape_into(char *dest, const void *srcarray, int len);
#include "ieee802154_xbee.h"
static struct wpan_phy *xbee_to_phy(const struct net_device *dev)
{
struct xbee_priv *priv = netdev_priv(dev);
return priv->phy;
}
/**
* xbee_get_phy - Return a phy corresponding to this device.
* @dev: The network device for which to return the wan-phy object
*
* This function returns a wpan-phy object corresponding to the passed
* network device. Reference counter for wpan-phy object is incremented,
* so when the wpan-phy isn't necessary, you should drop the reference
* via @wpan_phy_put() call.
*/
static struct wpan_phy *xbee_get_phy(const struct net_device *dev)
{
struct wpan_phy *phy = xbee_to_phy(dev);
return to_phy(get_device(&phy->dev));
}
/**
* xbee_get_pan_id - Retrieve the PAN ID of the device.
* @dev: The network device to retrieve the PAN of.
*
* Return the ID of the PAN from the PIB.
*/
static u16 xbee_get_pan_id(const struct net_device *dev)
{
BUG_ON(dev->type != ARPHRD_IEEE802154);
return 0xabcd;
//return 0xffff;
}
/**
* xbee_get_short_addr - Retrieve the short address of the device.
* @dev: The network device to retrieve the short address of.
*
* Returns the IEEE 802.15.4 short-form address cached for this
* device. If the device has not yet had a short address assigned
* then this should return 0xFFFF to indicate a lack of association.
*/
static u16 xbee_get_short_addr(const struct net_device *dev)
{
BUG_ON(dev->type != ARPHRD_IEEE802154);
return 0x1;
}
/**
* xbee_get_dsn - Retrieve the DSN of the device.
* @dev: The network device to retrieve the DSN for.
*
* Returns the IEEE 802.15.4 DSN for the network device.
* The DSN is the sequence number which will be added to each
* packet or MAC command frame by the MAC during transmission.
*
* DSN means 'Data Sequence Number'.
*
* Note: This is in section 7.2.1.2 of the IEEE 802.15.4-2006
* document.
*/
static u8 xbee_get_dsn(const struct net_device *dev)
{
BUG_ON(dev->type != ARPHRD_IEEE802154);
return 0x00; /* DSN are implemented in HW, so return just 0 */
}
/**
* xbee_assoc_req - Make an association request to the HW.
* @dev: The network device which we are associating to a network.
* @addr: The coordinator with which we wish to associate.
* @channel: The channel on which to associate.
* @cap: The capability information field to use in the association.
*
* Start an association with a coordinator. The coordinator's address
* and PAN ID can be found in @addr.
*
* Note: This is in section 7.3.1 and 7.5.3.1 of the IEEE
* 802.15.4-2006 document.
*/
static int xbee_assoc_req(struct net_device *dev,
struct ieee802154_addr *addr, u8 channel, u8 page, u8 cap)
{
struct wpan_phy *phy = xbee_to_phy(dev);
mutex_lock(&phy->pib_lock);
phy->current_channel = channel;
phy->current_page = page;
mutex_unlock(&phy->pib_lock);
printk(KERN_ALERT "Association request confirmed!\n");
/* We simply emulate it here */
return ieee802154_nl_assoc_confirm(dev, xbee_get_short_addr(dev),
IEEE802154_SUCCESS);
}
/**
* xbee_assoc_resp - Send an association response to a device.
* @dev: The network device on which to send the response.
* @addr: The address of the device to respond to.
* @short_addr: The assigned short address for the device (if any).
* @status: The result of the association request.
*
* Queue the association response of the coordinator to another
* device's attempt to associate with the network which we
* coordinate. This is then added to the indirect-send queue to be
* transmitted to the end device when it polls for data.
*
* Note: This is in section 7.3.2 and 7.5.3.1 of the IEEE
* 802.15.4-2006 document.
*/
static int xbee_assoc_resp(struct net_device *dev,
struct ieee802154_addr *addr, u16 short_addr, u8 status)
{
return 0;
}
/**
* xbee_disassoc_req - Disassociate a device from a network.
* @dev: The network device on which we're disassociating a device.
* @addr: The device to disassociate from the network.
* @reason: The reason to give to the device for being disassociated.
*
* This sends a disassociation notification to the device being
* disassociated from the network.
*
* Note: This is in section 7.5.3.2 of the IEEE 802.15.4-2006
* document, with the reason described in 7.3.3.2.
*/
static int xbee_disassoc_req(struct net_device *dev,
struct ieee802154_addr *addr, u8 reason)
{
return ieee802154_nl_disassoc_confirm(dev, IEEE802154_SUCCESS);
}
/**
* xbee_start_req - Start an IEEE 802.15.4 PAN.
* @dev: The network device on which to start the PAN.
* @addr: The coordinator address to use when starting the PAN.
* @channel: The channel on which to start the PAN.
* @bcn_ord: Beacon order.
* @sf_ord: Superframe order.
* @pan_coord: Whether or not we are the PAN coordinator or just
* requesting a realignment perhaps?
* @blx: Battery Life Extension feature bitfield.
* @coord_realign: Something to realign something else.
*
* If pan_coord is non-zero then this starts a network with the
* provided parameters, otherwise it attempts a coordinator
* realignment of the stated network instead.
*
* Note: This is in section 7.5.2.3 of the IEEE 802.15.4-2006
* document, with 7.3.8 describing coordinator realignment.
*/
static int xbee_start_req(struct net_device *dev, struct ieee802154_addr *addr,
u8 channel, u8 page,
u8 bcn_ord, u8 sf_ord, u8 pan_coord, u8 blx,
u8 coord_realign)
{
struct wpan_phy *phy = xbee_to_phy(dev);
mutex_lock(&phy->pib_lock);
phy->current_channel = channel;
phy->current_page = page;
mutex_unlock(&phy->pib_lock);
/* We don't emulate beacons here at all, so START should fail */
ieee802154_nl_start_confirm(dev, IEEE802154_INVALID_PARAMETER);
return 0;
}
/**
* xbee_scan_req - Start a channel scan.
* @dev: The network device on which to perform a channel scan.
* @type: The type of scan to perform.
* @channels: The channel bitmask to scan.
* @duration: How long to spend on each channel.
*
* This starts either a passive (energy) scan or an active (PAN) scan
* on the channels indicated in the @channels bitmask. The duration of
* the scan is measured in terms of superframe duration. Specifically,
* the scan will spend aBaseSuperFrameDuration * ((2^n) + 1) on each
* channel.
*
* Note: This is in section 7.5.2.1 of the IEEE 802.15.4-2006 document.
*/
static int xbee_scan_req(struct net_device *dev, u8 type, u32 channels,
u8 page, u8 duration)
{
u8 edl[27] = {};
return ieee802154_nl_scan_confirm(dev, IEEE802154_SUCCESS, type,
channels, page,
type == IEEE802154_MAC_SCAN_ED ? edl : NULL);
}
static struct ieee802154_mlme_ops xbee_mlme = {
.assoc_req = xbee_assoc_req,
.assoc_resp = xbee_assoc_resp,
.disassoc_req = xbee_disassoc_req,
.start_req = xbee_start_req,
.scan_req = xbee_scan_req,
.get_phy = xbee_get_phy,
.get_pan_id = xbee_get_pan_id,
.get_short_addr = xbee_get_short_addr,
.get_dsn = xbee_get_dsn,
};
static int ieee802154_xbee_open(struct net_device *dev)
{
printk(KERN_ALERT "[NET open called ]\n");
netif_start_queue(dev);
return 0;
}
static int ieee802154_xbee_close(struct net_device *dev)
{
printk(KERN_ALERT "[NET release called ]\n");
netif_stop_queue(dev);
return 0;
}
/*
* Adds in the checksum and sends the packet off to the serial driver.
*/
static void xbee_hw_tx(char *frame, int len, struct net_device *dev)
{
int i;
unsigned char checksum;
int actual;
printk(KERN_ALERT "[NET hardware_tx called, %d bytes ]\n", len);
//Add start delimiter
*frame = 0x7E;
//Checksum
checksum = 0;
for(i=3; i<(len-1); i++) {
if(frame[i] == 0x7D) {
i++;
checksum += (unsigned char) frame[i] ^ 0x20;
} else {
checksum += ((unsigned char)frame[i]);
}
}
checksum = 0xFF - checksum;
if (checksum == 0x7E || checksum == 0x7D || checksum == 0x11 || checksum == 0x13) {
frame[len-1] = 0x7D;
frame[len] = checksum ^ 0x20;
len++;
} else {
frame[len-1] = checksum;
}
//Send the data to serial driver
if(main_tty != NULL) {
set_bit(TTY_DO_WRITE_WAKEUP, &main_tty->flags);
printk(KERN_ALERT "Writing the data to tty...\n");
print_hex_dump(KERN_ALERT, "", DUMP_PREFIX_OFFSET, 16, 1, frame, len, 0);
actual = main_tty->driver->ops->write(main_tty, frame, len);
if(actual != len) {
printk(KERN_ALERT "Write failed!\n");
}
} else {
printk(KERN_ALERT "No tty is attached!\n");
}
kfree(frame);
}
static netdev_tx_t ieee802154_xbee_xmit(struct sk_buff *skb,
struct net_device *dev)
{
struct ieee802154_mac_cb* cb;
int framelen, datalen;
char *frame;
struct xbee_priv *priv = netdev_priv(dev);
struct xbee_tx_header header = {
.length = 0x00,
.api_id = 0x00,
.frame_id = 0x02,
.options = 0x04
};
dev->stats.tx_packets++;
dev->stats.tx_bytes += skb->len;
if(main_tty == NULL) {
printk(KERN_ALERT "No tty attached!\nMake sure ldisc_daemon is running\n");
return 0;
}
printk(KERN_ALERT "[NET tx called, %d bytes in sk_buff, pkt type %hu, protocol %hu]\n", skb->len, skb->pkt_type, skb->protocol);
cb = mac_cb(skb);
//header.address = cpu_to_be64(0x000000000000FFFF); // So far broadcast
header.address = cpu_to_be64p( (__be64*) &(cb->dest.extended_addr) );
printk(KERN_ALERT "MAC DST addr %llx\n", cb->dest.extended_addr);
header.length = cpu_to_be16(skb->len + 1 + 10);
datalen = skb->len + 1;
// Allocate buffer to hold entire serial frame, including start byte and checksum
frame = kmalloc(sizeof(struct xbee_tx_header) + (2*datalen) + 2, GFP_KERNEL);
/* Assemble the frame */
/* Escaping the XBee header */
printk(KERN_ALERT "Adding XBEE header- length is %lu\n", sizeof(header) + 1);
framelen = escape_into((frame + 1), &header, sizeof(header));
printk(KERN_ALERT "Adding data - length is %u\n", datalen);
framelen += escape_into((frame + framelen + 1 ), (skb->data), datalen );
framelen++; // Checksum char at the beginning
/* save the timestamp */
dev->trans_start = jiffies;
//Stop the interface from sending more data, get the spinlock, and send it!
netif_stop_queue(dev);
spin_lock(&priv->lock);
xbee_hw_tx(frame, framelen, dev);
//Free the skbuff, we've copied all the data out
dev_kfree_skb(skb);
spin_unlock(&priv->lock);
return NETDEV_TX_OK;
}
/*
* Copies bytes from one array into another, escaping them if necessary.
* Returns the size of the new array, including character escapes.
* The destination array MUST be at least twice the size of the source
*/
static int escape_into(char *dest, const void *srcarray, int len) {
int i, j=0;
const char *src = srcarray;
for(i=0; i<len; i++) {
char unesc = *(src + i);
// We support API mode 1 (escape characters is for API 2, if we want to enable mode 2 we need to implement escape management at RX)
if( unesc == 0x7D || unesc == 0x7E || unesc == 0x11 || unesc == 0x13) {
dest[j] = 0x7D;
dest[j+1] = unesc ^ 0x20;
j+=2;
} else {
dest[j] = unesc;
j++;
}
}
return j;
}
static int ieee802154_xbee_ioctl(struct net_device *dev, struct ifreq *ifr,
int cmd)
{
struct sockaddr_ieee802154 *sa =
(struct sockaddr_ieee802154 *)&ifr->ifr_addr;
u16 pan_id, short_addr;
printk(KERN_ALERT "Xbee ioctl called\n");
switch (cmd) {
case SIOCGIFADDR:
/* FIXME: fixed here, get from device IRL */
pan_id = xbee_get_pan_id(dev);
short_addr = xbee_get_short_addr(dev);
if (pan_id == IEEE802154_PANID_BROADCAST ||
short_addr == IEEE802154_ADDR_BROADCAST)
return -EADDRNOTAVAIL;
sa->family = AF_IEEE802154;
sa->addr.addr_type = IEEE802154_ADDR_SHORT;
sa->addr.pan_id = pan_id;
sa->addr.short_addr = short_addr;
return 0;
}
return -ENOIOCTLCMD;
}
static int ieee802154_xbee_mac_addr(struct net_device *dev, void *p)
{
return -EBUSY; /* HW address is built into the device */
}
static const struct net_device_ops xbee_ops = {
.ndo_open = ieee802154_xbee_open,
.ndo_stop = ieee802154_xbee_close,
.ndo_start_xmit = ieee802154_xbee_xmit,
.ndo_do_ioctl = ieee802154_xbee_ioctl,
.ndo_set_mac_address = ieee802154_xbee_mac_addr,
};
static int xbee_header_create(struct sk_buff *skb,
struct net_device *dev,
unsigned short type,
const void *_daddr,
const void *_saddr,
unsigned len)
{
struct ieee802154_mac_cb* cb;
struct ieee802154_addr* da = (struct ieee802154_addr*) _daddr;
cb = mac_cb(skb);
memcpy(&(cb->dest.extended_addr),&(da->extended_addr), 8);
printk(KERN_ALERT "MAC DST in header addr %llx\n", cb->dest.extended_addr);
printk(KERN_ALERT "Adding xbee header\n");
//print_hex_dump(KERN_ALERT, "", DUMP_PREFIX_OFFSET, 16, 1, (const char*) _saddr, 8, false);
return 0;
}
/*static int
xbee_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{
struct ieee802154_hdr hdr;
struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
printk(KERN_ALERT "Parse header\n");
if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
printk(KERN_ALERT "malformed packet\n");
return 0;
}
*addr = hdr.source;
return sizeof(*addr);
}*/
static struct header_ops xbee_header_ops = {
.create = xbee_header_create,
//.parse = xbee_header_parse,
};
static void ieee802154_xbee_destruct(struct net_device *dev)
{
struct wpan_phy *phy = xbee_to_phy(dev);
wpan_phy_unregister(phy);
free_netdev(dev);
wpan_phy_free(phy);
}
static void ieee802154_xbee_setup(struct net_device *dev)
{
struct xbee_priv *priv;
/*
* Then, initialize the priv field. This encloses the statistics
* and a few private fields.
*/
priv = netdev_priv(dev);
memset(priv, 0, sizeof(struct xbee_priv));
spin_lock_init(&priv->lock);
dev->addr_len = IEEE802154_ADDR_LEN;
dev->header_ops = &xbee_header_ops;
memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
dev->features = NETIF_F_HW_CSUM;
dev->needed_tailroom = 2; /* FCS */
dev->mtu = 127;
dev->tx_queue_len = 10;
dev->type = ARPHRD_IEEE802154;
dev->flags = IFF_NOARP | IFF_BROADCAST;
dev->watchdog_timeo = 0;
dev->destructor = ieee802154_xbee_destruct;
}
static int ieee802154xbee_probe(struct platform_device *pdev)
{
struct net_device *dev;
struct xbee_priv *priv;
struct wpan_phy *phy = wpan_phy_alloc(0);
int err;
if (!phy)
return -ENOMEM;
dev = alloc_netdev(sizeof(struct xbee_priv), "hardwpan%d", ieee802154_xbee_setup);
if (!dev) {
wpan_phy_free(phy);
return -ENOMEM;
}
memcpy(dev->dev_addr, "\xba\xbe\xca\xfe\xde\xad\xbe\xef",
dev->addr_len);
/*
* For now we'd like to emulate 2.4 GHz-only device,
* both O-QPSK and CSS
*/
/* 2.4 GHz O-QPSK 802.15.4-2003 */
phy->channels_supported[0] |= 0x7FFF800;
/* 2.4 GHz CSS 802.15.4a-2007 */
phy->channels_supported[3] |= 0x3fff;
phy->transmit_power = 0xbf;
dev->netdev_ops = &xbee_ops;
dev->ml_priv = &xbee_mlme;
priv = netdev_priv(dev);
priv->phy = phy;
wpan_phy_set_dev(phy, &pdev->dev);
SET_NETDEV_DEV(dev, &phy->dev);
platform_set_drvdata(pdev, dev);
priv->rbuff = kmalloc(XBEE_MAXFRAME, GFP_KERNEL);
priv->rcount = 0;
priv->frame_status = UNFRAMED;
priv->frame_len = 0;
priv->escaped = 0;
xbee_dev = dev; // This should be removed in a future!
err = wpan_phy_register(phy);
if (err)
goto out;
err = register_netdev(dev);
if (err < 0)
goto out;
dev_info(&pdev->dev, "Added ieee802154 HardMAC hardware\n");
return 0;
out:
unregister_netdev(dev);
return err;
}
static int ieee802154xbee_remove(struct platform_device *pdev)
{
struct net_device *dev = platform_get_drvdata(pdev);
struct xbee_priv *priv = netdev_priv(xbee_dev);
kfree(priv->rbuff);
unregister_netdev(dev);
return 0;
}
static struct platform_device *ieee802154xbee_dev;
static struct platform_driver ieee802154xbee_driver = {
.probe = ieee802154xbee_probe,
.remove = ieee802154xbee_remove,
.driver = {
.name = "ieee802154xbee",
.owner = THIS_MODULE,
},
};
/*
***************LDISC Functions*************
*/
/*
* The following routines are called from above (user space/tty).
*/
static int n_xbee_open(struct tty_struct *tty) {
// Don't allow more than one tty to be attached
if( main_tty != NULL )
return -EPERM;
if (!try_module_get(THIS_MODULE))
return -ENODEV;
if (tty->ops->stop)
tty->ops->stop(tty);
main_tty = tty;
printk(KERN_ALERT "Attached to a tty!\n\n");
tty_driver_flush_buffer(tty);
tty_ldisc_flush(tty);
// For kernels < 3.10 it needs to be set
tty->receive_room = 4096;
/*priv->rbuff = kmalloc(XBEE_MAXFRAME, GFP_KERNEL);
priv->rcount = 0;
priv->frame_status = UNFRAMED;
priv->frame_len = 0;*/
return 0;
}
static void n_xbee_close(struct tty_struct *tty) {
//struct xbee_priv *priv = netdev_priv(xbee_dev);
printk(KERN_ALERT "CLOSE CALLED\n");
//tty_driver_flush_buffer(tty);
//tty_ldisc_flush(tty);
module_put(THIS_MODULE);
main_tty = NULL;
//kfree(priv->rbuff);
}
static void n_xbee_flush_buffer(struct tty_struct *tty) {
printk(KERN_ALERT "FLUSH_BUFFER CALLED\n");
tty_driver_flush_buffer(tty);
}
static ssize_t n_xbee_chars_in_buffer(struct tty_struct *tty) {
printk(KERN_ALERT "CHARS_IN_BUFFER CALLED\n");
return 0;
}
static int n_xbee_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg) {
printk(KERN_ALERT "IOCTL CALLED\n");
return -EPERM;
}
static unsigned int n_xbee_poll(struct tty_struct *tty, struct file *file, struct poll_table_struct *poll) {
printk(KERN_ALERT "POLL CALLED\n");
return 0;
}
/*
* The following routines are called from below (serial port driver).
*/
static char checksum_validate(unsigned char *buffer, int len, unsigned char checksum) {
int i = 0;
while(len--) {
checksum += buffer[i++];
}
if(checksum == 0xFF)
return true;
printk(KERN_ALERT "[XBEE] CSUM should be 0xFF, is 0x%x\n", checksum);
return false;
}
/*
* Create the skb for the upper layer
*/
void xbee_rx(struct net_device *dev, unsigned char *data, int len, unsigned char* addr) {
struct xbee_priv *priv = netdev_priv(dev);
struct sk_buff *skb;
struct ieee802154_mac_cb* cb;
struct ieee802154_hdr hdr;
int hlen;
int packet_stat;
memset(&hdr.fc, 0, sizeof(hdr.fc));
skb = dev_alloc_skb(len+sizeof(struct ieee802154_hdr));
if (!skb) {
if (printk_ratelimit())
printk(KERN_NOTICE "[NET] xbee rx: low on mem - packet dropped\n");
priv->stats.rx_dropped++;
return;
}
skb_reserve(skb, len);
skb->dev = dev;
//skb->pkt_type = PACKET_HOST;
skb->protocol = htons(ETH_P_IEEE802154);
cb = mac_cb(skb);
hdr.source.mode = IEEE802154_ADDR_LONG;
hdr.source.extended_addr = be64_to_cpup((__be64*)addr);
hdr.source.pan_id = xbee_get_pan_id(dev);
//cb->source.mode = IEEE802154_ADDR_LONG;
//cb->source.pan_id = xbee_get_pan_id(dev);
//cb->source.extended_addr = be64_to_cpup((__be64*)addr);
hdr.dest.mode = IEEE802154_ADDR_LONG;
hdr.dest.extended_addr = IEEE802154_ADDR_BROADCAST;
hdr.dest.pan_id = xbee_get_pan_id(dev);
hlen = ieee802154_hdr_push(skb, &hdr);
if (hlen < 0)
return ;
skb->mac_len = hlen;
skb_reset_mac_header(skb);
if (len > ieee802154_max_payload(&hdr))
return ;
// Put all the data into the socket buffer
memcpy(skb_push(skb, len), data, len);
skb->ip_summed = CHECKSUM_UNNECESSARY; // don't check it (does this make any difference?)
skb->len = len;
packet_stat = netif_rx(skb);
if(packet_stat == NET_RX_SUCCESS) {
printk(KERN_ALERT "[NET] Packet received successfully\n");
} else if(packet_stat == NET_RX_DROP) {
printk(KERN_ALERT "[NET] Packet was dropped!\n");
}
printk(KERN_ALERT "MAC SRC addr %llx\n", hdr.source.extended_addr);
dev->stats.rx_packets++;
dev->stats.rx_bytes += skb->len;
}
/*
* Receive a packet from device and send data to the right place
*/
void xbee_receive_packet(struct net_device *dev, unsigned char *data, int len)
{
struct xbee_priv *priv = netdev_priv(dev);
printk(KERN_ALERT "[XBEE] receive_packet called ]\n");
switch(*data) {
case IEEE802154_RECEIVE_PACKET:
printk(KERN_ALERT "[XBEE] 802154 packet received \n");
xbee_rx(dev, (data + 11), len - 11, data + 1 );
goto out;
case IEEE802154_TRANSMIT_STATUS:
printk(KERN_ALERT "[XBEE] Zigbee Transmit Status Frame received\n");
if(len != 3) {
printk(KERN_ALERT "[XBEE] Packet is wrong length for a Transmit Status Packet!\n");
goto out;
}
if(data[2] == TRANSMIT_SUCCESS) {
printk(KERN_ALERT "[XBEE] Packet delivery reported as success!\n");
//TODO: report which packet by reading message (and notify super-paranoid drivers?)
} else {
if(data[2] == TRANSMIT_INVALID) {
printk(KERN_ALERT "[XBEE] Transmit failed: invalid endpoint\n");
} else if(data[2] == TRANSMIT_NO_ACK) {
printk(KERN_ALERT "[XBEE] No ACK received, is ACK transmission set on the receiver?\n");
} else if(data[2] == TRANSMIT_ADDRNOTFOUND) {
printk(KERN_ALERT "[XBEE] Transmit failed: address not found\n");
} else {
printk(KERN_ALERT "[XBEE] Packet delivery failed with status %d\n", data[2]);
}
}
goto out;
case NODE_IDENTIFICATION_INDICATOR:
printk(KERN_ALERT "[XBEE] NODE_IDENTIFICATION_INDICATOR Frame received\n");
break;
case MODEM_STATUS:
printk(KERN_ALERT "[XBEE] Zigbee Modem Status Frame received\n");
break;
case AT_COMMAND:
printk(KERN_ALERT "[XBEE] Zigbee AT_COMMAND Frame received\n");
break;
case AT_COMMAND_QUEUE_PARAMETER_VALUE:
printk(KERN_ALERT "[XBEE] AT_COMMAND_QUEUE_PARAMETER_VALUE Frame received\n");
break;
case AT_COMMAND_RESPONSE:
printk(KERN_ALERT "[XBEE] AT_COMMAND_RESPONSE Frame received\n");
break;
case REMOTE_COMMAND_REQUEST:
printk(KERN_ALERT "[XBEE] REMOTE_COMMAND_REQUEST Frame received\n");
break;
case REMOTE_COMMAND_RESPONSE:
printk(KERN_ALERT "[XBEE] REMOTE_COMMAND_RESPONSE Frame received\n");
break;
case EXPLICIT_ADDRESSING_COMMAND_FRAME:
printk(KERN_ALERT "[XBEE] EXPLICIT_ADDRESSING_COMMAND_FRAME received\n");
break;
default:
printk(KERN_ALERT "[XBEE] UNKNOWN Frame Received : errors %lu\n", priv->stats.rx_dropped++);
}
out:
return;
}
/*
* Called from the serial driver when new received data is ready
*/
static void n_xbee_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count) {
unsigned char temp;
struct xbee_priv *priv = netdev_priv(xbee_dev);
unsigned char checksum;
//printk(KERN_ALERT "[XBEE] RECEIVE_BUF CALLED %d chars received\n", count);
while(count--) {
temp = *cp++;
//Start a new frame if 0x7E received, even if we seem to be in the middle of one
if(temp == 0x7E) {
printk(KERN_ALERT "[XBEE] INIT FRAME : %02X\n", temp );
priv->frame_status = FRAME_LEN_HIGH;
priv->frame_len = 0;
priv->rcount = 0;
continue;
}
//Escape characters after 0x7D byte
if(temp == 0x7D) {
priv->escaped=1;
continue;
}
// The last one was an escaped char
if(priv->escaped){
temp = temp ^ 0x20;
printk(KERN_ALERT "[XBEE] ESCAPED : %02X\n", temp );
priv->escaped=0;
}
switch(priv->frame_status) {
case UNFRAMED:
printk(KERN_ALERT "Unframed data received: 0x%x\n", temp);
break;
case FRAME_LEN_HIGH:
printk(KERN_ALERT "[XBEE] LEN HIGH : %02X\n", temp );
priv->frame_len = (temp) << 8;
priv->frame_status = FRAME_LEN_LOW;
break;
case FRAME_LEN_LOW:
printk(KERN_ALERT "[XBEE] LEN LOW : %02X\n", temp );
priv->frame_len |= (temp);
priv->frame_status = FRAME_DATA;
priv->rcount = 0;
break;
case FRAME_DATA:
//Add to the frame buffer
priv->rbuff[priv->rcount++] = (temp);
//Is the frame done?
if(priv->rcount == (priv->frame_len )) {
priv->frame_status = FRAME_CHECKSUM;
}
//Is the frame FUBAR?
if(priv->rcount > XBEE_MAXFRAME || priv->rcount > priv->frame_len || priv->frame_len > XBEE_MAXFRAME) {
priv->rcount = 0;
priv->frame_status = UNFRAMED;
printk(KERN_ALERT "[XBEE] A frame got pwned!! rcount %d max %d frame_len %d : %lu errors\n", priv->rcount, XBEE_MAXFRAME, priv->frame_len,priv->stats.rx_dropped++);
}
break;
case FRAME_CHECKSUM:
checksum = temp;
printk(KERN_ALERT "[XBEE] Frame checksum : %02X\n", checksum);
print_hex_dump(KERN_ALERT, "", DUMP_PREFIX_OFFSET, 16, 1, priv->rbuff, priv->frame_len, 0);
if(checksum_validate(priv->rbuff, priv->frame_len , checksum)) {
//Hand off to xbee_receive_packet for networking/packet ID
xbee_receive_packet(xbee_dev, priv->rbuff, priv->frame_len );
} else {
printk(KERN_ALERT "[XBEE] CHECKSUM FAIL : %lu errors\n", priv->stats.rx_dropped++);
}
priv->frame_status = UNFRAMED;
priv->frame_len = 0;