-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path0001-Added-XBee-driver-support.patch
4597 lines (4592 loc) · 136 KB
/
0001-Added-XBee-driver-support.patch
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
From 3e52c3dc8cdd4dfebf518e58df1770460e410803 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Taveira?= <joao.p.taveira@gmail.com>
Date: Sun, 5 Jan 2014 16:12:15 +0000
Subject: [PATCH 1/4] Added XBee driver support
---
drivers/net/ieee802154/Kconfig | 4 +
drivers/net/ieee802154/Makefile | 1 +
drivers/net/ieee802154/xbee.c | 4539 +++++++++++++++++++++++++++++++++++++++
include/uapi/linux/tty.h | 1 +
4 files changed, 4545 insertions(+)
create mode 100644 drivers/net/ieee802154/xbee.c
diff --git a/drivers/net/ieee802154/Kconfig b/drivers/net/ieee802154/Kconfig
index 08ae465..acc8eb8 100644
--- a/drivers/net/ieee802154/Kconfig
+++ b/drivers/net/ieee802154/Kconfig
@@ -30,6 +30,10 @@ config IEEE802154_FAKELB
This driver can also be built as a module. To do so say M here.
The module will be called 'fakelb'.
+config IEEE802154_XBEE
+ depends on IEEE802154_DRIVERS && MAC802154
+ tristate "Xbee UART driver"
+
config IEEE802154_AT86RF230
depends on IEEE802154_DRIVERS && MAC802154
tristate "AT86RF230/231 transceiver driver"
diff --git a/drivers/net/ieee802154/Makefile b/drivers/net/ieee802154/Makefile
index abb0c08..72899e3 100644
--- a/drivers/net/ieee802154/Makefile
+++ b/drivers/net/ieee802154/Makefile
@@ -1,4 +1,5 @@
obj-$(CONFIG_IEEE802154_FAKEHARD) += fakehard.o
obj-$(CONFIG_IEEE802154_FAKELB) += fakelb.o
+obj-$(CONFIG_IEEE802154_XBEE) += xbee.o
obj-$(CONFIG_IEEE802154_AT86RF230) += at86rf230.o
obj-$(CONFIG_IEEE802154_MRF24J40) += mrf24j40.o
diff --git a/drivers/net/ieee802154/xbee.c b/drivers/net/ieee802154/xbee.c
new file mode 100644
index 0000000..ec349fd
--- /dev/null
+++ b/drivers/net/ieee802154/xbee.c
@@ -0,0 +1,4539 @@
+/*
+ * Xbee: XBee TTY for Xbee RF 868 OEM using 802.15.4 stack
+ *
+ * Authors:
+ * Joao Pedro Taveira <joao.silva@inov.pt>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+/**
+ * @file main.c
+ *
+ * @date Jun 25, 2013
+ * @author Joao Pedro Taveira
+ */
+
+
+#ifndef __KERNEL__
+#include <stdio.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <termios.h>
+#include <sys/time.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <pthread.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define BUG() do{exit(1);}while(0);
+
+#ifndef pr_fmt
+#define pr_fmt(fmt) fmt
+#endif
+
+#define KERN_EMERG "emerg: " /* system is unusable */
+#define KERN_ALERT "alert: " /* action must be taken immediately */
+#define KERN_CRIT "crit : " /* critical conditions */
+#define KERN_ERR "error: " /* error conditions */
+#define KERN_WARNING "warn : " /* warning conditions */
+#define KERN_NOTICE "notic: " /* normal but significant condition */
+#define KERN_INFO "info : " /* informational */
+#define KERN_DEBUG "debug: " /* debug-level messages */
+
+#define printk printf
+
+#define pr_debug(fmt, ...) \
+ printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
+
+#define IEEE802154_ADDR_LEN 8
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+
+#ifdef __GNUC__
+__extension__ typedef __signed__ long long __s64;
+__extension__ typedef unsigned long long __u64;
+#else
+typedef __signed__ long long __s64;
+typedef unsigned long long __u64;
+#endif /* __GNUC__ */
+
+/*
+ * Below are truly Linux-specific types that should never collide with
+ * any application/library that wants linux/types.h.
+ */
+
+#ifdef __CHECKER__
+#define __bitwise__ __attribute__((bitwise))
+#else
+#define __bitwise__
+#endif
+#ifdef __CHECK_ENDIAN__
+#define __bitwise __bitwise__
+#else
+#define __bitwise
+#endif
+
+typedef __u16 __bitwise __le16;
+typedef __u16 __bitwise __be16;
+typedef __u32 __bitwise __le32;
+typedef __u32 __bitwise __be32;
+typedef __u64 __bitwise __le64;
+typedef __u64 __bitwise __be64;
+
+typedef __u16 __bitwise __sum16;
+typedef __u32 __bitwise __wsum;
+
+struct ieee802154_hw_addr_filt {
+ __le16 pan_id; /* Each independent PAN selects a unique
+ * identifier. This PAN id allows communication
+ * between devices within a network using short
+ * addresses and enables transmissions between
+ * devices across independent networks.
+ */
+ __le16 short_addr;
+ uint8_t ieee_addr[IEEE802154_ADDR_LEN];
+ uint8_t pan_coord;
+};
+
+struct ieee802154_dev {
+ /* filled by the driver */
+ int extra_tx_headroom;
+ uint32_t flags;
+ struct device *parent;
+
+ /* filled by mac802154 core */
+ struct ieee802154_hw_addr_filt hw_filt;
+ void *priv;
+ struct wpan_phy *phy;
+};
+
+#else
+#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/netdevice.h>
+#include <net/mac802154.h>
+#include <net/ieee802154.h>
+#include <net/ieee802154_netdev.h>
+#include <net/wpan-phy.h>
+
+#define MAX_DATA_SIZE 127
+
+#endif /* __KERNEL__ */
+
+/*
+ * API Operation
+ *
+ * Start Del 1B Len 2-3B Bytes 4-n Byte n+1
+ * +------------+ +-----+------+ +------------+ +------------+
+ * | 0x7E | | MSB | LSB | |API-specifc | | ChkSum 1B |
+ * +------------+ +-----+------+ +------------+ +------------+
+ *
+ * Escape characters. When sending or receiving a UART data frame,
+ * specific data values must be escaped (flagged) so they do not
+ * interfere with the data frame sequencing. To escape an
+ * interfering data byte, insert 0x7D and follow it with the byte
+ * to be escaped XOR’d with 0x20.
+ *
+ * Data bytes that need to be escaped:
+ * 0x7E – Frame Delimiter
+ * 0x7D – Escape
+ * 0x11 – XON
+ * 0x13 – XOFF
+ * Example - Raw UART Data Frame (before escaping interfering bytes):
+ * 0x7E 0x00 0x02 0x23 0x11 0xCB
+ * 0x11 needs to be escaped which results in the following frame:
+ * 0x7E 0x00 0x02 0x23 0x7D 0x31 0xCB
+ * Note: In the above example, the length of the raw data (excluding
+ * the checksum) is 0x0002 and the checksum of the non-escaped data
+ * (excluding frame delimiter and length) is calculated as:
+ * 0xFF - (0x23 + 0x11) = (0xFF - 0x34) = 0xCB.
+ *
+ * Length
+ * The length field has two-byte value that specifies the number of
+ * bytes that will be contained in the frame data field. It does not
+ * include the checksum field.
+ */
+
+#define XBEE_SYSFS_STATS 1
+
+#define XBEE_START_DELIM 0x7E
+#define XBEE_ESCAPE 0x7D
+#define XBEE_XON 0x11
+#define XBEE_XOFF 0x13
+
+#define XBEE_ADDR_LEN 8
+#define XBEE_DEFAULT_BROADCAST_RADIUS 1
+
+#define XBEE_MAX_FRAME_ID 0x3F /* 0011 1111 mask */
+#define XBEE_TRANSMIT_SPLITTED 0x80 /* 1000 0000 mask */
+#define XBEE_TRANSMIT_PART2 0x40 /* 0100 0000 mask */
+
+//typedef enum mode {
+// MODE_IDLE,
+// MODE_RX,
+// MODE_TX,
+// MODE_CMD,
+// MODE_SLEEP
+//} xbee_pro_mode_t;
+
+typedef enum recv_state {
+ RECV_STATE_WAIT_START_DELIM,
+ RECV_STATE_WAIT_MSB,
+ RECV_STATE_WAIT_LSB,
+ RECV_STATE_WAIT_DATA,
+ RECV_STATE_WAIT_CHECKSUM
+} xbee_recv_state_t;
+
+enum motes {
+ NOT_SUPPORTED_MOTE = -1,
+ UNKNOWN_MOTE = 0,
+ XBEE_XB08_DP_MOTE = 1,
+ HM_TRP_433D_MOTE,
+ HM_TRP_868D_MOTE,
+ MAX_MOTE
+};
+
+struct xbee_device {
+ /* Relative devices */
+#ifdef __KERNEL__
+ struct tty_struct *tty;
+ struct ieee802154_dev *dev;
+ int device_driver;
+#else
+ int tty_fd;
+ const char *tty_path;
+#endif
+
+#ifdef __KERNEL__
+ struct mutex mutex;
+#else
+ pthread_mutex_t mutex;
+#endif /* __KERNEL__ */
+ uint8_t frame_id_counter;
+
+ int escaped_mode;
+// xbee_pro_mode_t mode;
+
+ /* Command (rx) processing */
+ uint16_t recv_data_size;
+ unsigned int recv_data_offset;
+ int recv_data_escape_next;
+ uint8_t *recv_data;
+ int recv_state;
+ uint8_t recv_data_sum;
+
+ wait_queue_head_t wq;
+ struct workqueue_struct *frames_workqueue;
+
+#ifndef __KERNEL__
+ int rx_thread_running;
+ int rx_thread_end;
+ pthread_t rx_thread_tid;
+#endif /* !__KERNEL__ */
+
+ u8 dev_addr[IEEE802154_ADDR_LEN];
+ u16 pan_id;
+ //u8 rf_max_payload;
+
+};
+
+static u8 global_lqi = 0;
+
+#define FRAME_TYPE_AT_CMD 0x08
+#define FRAME_TYPE_AT_CMD_QUEUE 0x09
+#define FRAME_TYPE_TRANSMIT_REQ 0x10
+#define FRAME_TYPE_EXPLICIT_ADDR_CMD 0x11
+#define FRAME_TYPE_REMOTE_AT_CMD_REQ 0x17
+#define FRAME_TYPE_AT_CMD_RESP 0x88
+#define FRAME_TYPE_TRANSMIT_STATUS2 0x89
+#define FRAME_TYPE_MODEM_STATUS 0x8A
+#define FRAME_TYPE_TRANSMIT_STATUS1 0x8B
+#define FRAME_TYPE_RECEIVE_PACKET 0x90
+#define FRAME_TYPE_EXPLICIT_RX_INDICATOR 0x91
+#define FRAME_TYPE_NODE_ID_INDICATOR 0x95
+#define FRAME_TYPE_REMOTE_CMD_RESP 0x97
+
+#define FRAME_TYPE_RAW_TRANSMIT_REQ 0x20 /* simple RAW packet */
+#define FRAME_TYPE_RAW_RECEIVE FRAME_TYPE_RAW_TRANSMIT_REQ
+#define FRAME_TYPE_RAW_TRANSMIT_STATUS 0xA0 /* RAW packet send completion */
+
+#define TRANSMIT_OPTION_DISABLE_ACK(x) (x | 0x01)
+#define TRANSMIT_OPTION_ENABLE_ACK(x) (x & ~0x01)
+
+#define TRANSMIT_OPTION_DISABLE_ROUTE_DISCOVERY(x) (x | 0x02)
+#define TRANSMIT_OPTION_ENABLE_ROUTE_DISCOVERY(x) (x & ~0x02)
+
+typedef char at_command_t[2];
+typedef char at_command_param_t[4];
+
+typedef struct api_generic {
+ uint8_t api_id;
+} api_frame_generic_t;
+
+typedef struct at_cmd {
+ uint8_t api_id;
+ uint8_t frame_id;
+ at_command_t at_command;
+ at_command_param_t parameter_value;
+ uint8_t parameter_value_len;
+} api_frame_at_cmd_t;
+
+typedef struct at_cmd_queue {
+ uint8_t api_id;
+ uint8_t frame_id;
+ at_command_t at_command;
+ at_command_param_t parameter_value;
+ uint8_t parameter_value_len;
+} api_frame_at_cmd_queue_t;
+
+typedef struct transmit_req {
+ uint8_t api_id;
+ uint8_t frame_id;
+ uint8_t dst_addr[XBEE_ADDR_LEN]; // 0x000000000000FFFF - Broadcast address
+ uint16_t reserved; // 0xFFFE
+ uint8_t brd_radius;
+ uint8_t options; // bit 0: disable ACK bit 1: Dont Attempt route Disc
+ uint8_t *payload;
+ uint8_t payload_len;
+} api_frame_transmit_req_t;
+
+typedef struct raw_packet {
+ uint8_t api_id;
+ union {
+ uint8_t frame_id;
+ uint8_t lqi;
+ };
+ uint8_t *payload;
+ uint8_t payload_len;
+} api_frame_raw_packet_t;
+
+typedef struct explicit_addr_cmd {
+ uint8_t api_id;
+ uint8_t frame_id;
+ uint8_t dst_addr[XBEE_ADDR_LEN]; // 0x000000000000FFFF - Broadcast address
+ uint16_t reserved; // 0xFFFE
+ uint8_t src_endpoint;
+ uint8_t dst_endpoint;
+ uint16_t cluster_id;
+ uint16_t profile_id;
+ uint8_t brd_radius;
+ uint8_t options; // bit 0: disable ACK bit 1: Dont Attempt route Disc
+ uint8_t *payload;
+ uint8_t payload_len;
+} api_frame_explicit_addr_cmd_t;
+
+typedef struct remote_at_cmd_req {
+ uint8_t api_id;
+ uint8_t frame_id;
+ uint8_t dst_addr[XBEE_ADDR_LEN]; // 0x000000000000FFFF - Broadcast address
+ uint16_t reserved; // 0xFFFE
+ uint8_t remote_options; // 0x02 - Apply changes on remote. (If not set, AC command must be sent before changes will take effect.) All other bits must be set to 0.
+ at_command_t at_command;
+ at_command_param_t parameter_value;
+ uint8_t parameter_value_len;
+} api_frame_remote_at_cmd_req_t;
+
+typedef struct at_cmd_resp {
+ uint8_t api_id;
+ uint8_t frame_id;
+ at_command_t at_command;
+ uint8_t cmd_status; // 0 = OK; 1 = ERROR; 2 = Invalid Command; 3 = Invalid Parameter
+ at_command_param_t cmd_data;
+ uint8_t cmd_data_len;
+} api_frame_at_cmd_resp_t;
+
+typedef struct modem_status {
+ uint8_t api_id;
+ uint8_t status;
+ // 0x00: Hardware reset
+ // 0x01: Watchdog timer reset
+ // 0x0B: Network woke up
+ // 0x0C: Network went to sleep
+} api_frame_modem_status_t;
+
+typedef struct transmit_status {
+ uint8_t api_id;
+ uint8_t frame_id;
+ uint16_t reserved; // 0xFFFE
+ uint8_t transmit_retry_count; // #transmission retries took place
+ uint8_t delivery_status;
+ // 0x00 = Success
+ // 0x01 = MAC ACK Failure
+ // 0x15 = Invalid destination endpoint
+ // 0x21 = Network ACK Failure
+ // 0x25 = Route Not Found
+ uint8_t discovery_status;
+ // 0x00 = No Discovery Overhead
+ // 0x02 = Route Discovery
+} api_frame_transmit_status1_t;
+
+typedef struct transmit_status2 {
+ uint8_t api_id;
+ uint8_t frame_id;
+ uint8_t delivery_status;
+ // 0x00 - Success, no errors were detected on transmission.
+ // 0x01 - An expected MAC acknowledgment never occurred.
+ // 0x02 - CCA failure.
+ // 0x03 - Transmission was purged because it was attempted before the stack was up.
+ // 0x04 - Physical error occurred on the interface with the WiFi transceiver.
+ // 0x18 - No Buffers.
+ // 0x03 - Packet was purged without being transmitted.
+ // 0x21 - An expected network acknowledgment never occurred.
+ // 0x22 - Not joined to network.
+ // 0x23 - Self-addressed.
+ // 0x24 - Address not found.
+ // 0x25 - Route not found.
+ // 0x26 - Broadcast relay was not heard.
+ // 0x2B - Invalid Binding Table Index.
+ // 0x2C - Invalid Endpoint.
+ // 0x31 - A software error occurred.
+ // 0x32 - Resource Error.
+ // 0x74 - Data payload too large.
+ // 0x76 - Attempt to create a client socket failed.
+ // 0xBB - Key not authorized.
+} api_frame_transmit_status2_t;
+
+typedef struct raw_transmit_status {
+ uint8_t api_id;
+ uint8_t frame_id;
+ uint8_t retries;
+ uint8_t delivery_status;
+} api_frame_raw_transmit_status_t;
+
+typedef struct receive_packet {
+ uint8_t api_id;
+ uint8_t src_addr[XBEE_ADDR_LEN];
+ uint16_t reserved; // 0xFFFE
+ uint8_t options; // 0x01 - Packet Acknowledged; 0x02 - Packet was a broadcast packet
+ uint8_t *payload;
+ uint8_t payload_len;
+} api_frame_receive_packet_t;
+
+typedef struct explicit_rx_indicator {
+ uint8_t api_id;
+ uint8_t src_addr[XBEE_ADDR_LEN];
+ uint16_t reserved; // 0xFFFE
+ uint8_t src_endpoint;
+ uint8_t dst_endpoint;
+ uint16_t cluster_id;
+ uint16_t profile_id;
+ uint8_t options; // 0x01 - Packet Acknowledged; 0x02 - Packet was a broadcast packet
+ uint8_t *payload;
+ uint8_t payload_len;
+} api_frame_explicit_rx_indicator_t;
+
+typedef struct node_id_indicator {
+ uint8_t api_id;
+ uint8_t sender_addr[XBEE_ADDR_LEN]; // 64bit addr sender
+ uint16_t sender_short_addr; // 16bit addr senders
+ uint8_t options; // 0x01 - Packet Acknowledged; 0x02 - Packet was a broadcast packet
+ uint16_t src_short_addr; // Set to the 16-bit network address of the remote. Set to 0xFFFE if unknown.
+ uint8_t src_addr[XBEE_ADDR_LEN];
+ unsigned char *ni_string;
+ uint16_t parent_short_addr;
+} api_frame_node_id_indicator_t;
+
+typedef struct remote_cmd_resp {
+ uint8_t api_id;
+ uint8_t frame_id;
+ uint8_t src_addr[XBEE_ADDR_LEN];
+ uint16_t reserved; // 0xFFFE
+ at_command_t at_command;
+ uint8_t cmd_status; // 0 = OK; 1 = ERROR; 2 = Invalid Command; 3 = Invalid Parameter
+ at_command_param_t cmd_data;
+ uint8_t cmd_data_len;
+} api_frame_remote_cmd_resp_t;
+
+typedef struct xbee_api_frame {
+ uint16_t len;
+ uint8_t api_id;
+ uint8_t *raw_data;
+ union api_cmd_data_union {
+ api_frame_generic_t api_generic;
+ api_frame_at_cmd_t at_cmd;
+ api_frame_at_cmd_queue_t at_cmd_queue;
+ api_frame_transmit_req_t transmit_req;
+ api_frame_explicit_addr_cmd_t explitcit_addr_cmd;
+ api_frame_remote_at_cmd_req_t remote_at_cmd_req;
+ api_frame_at_cmd_resp_t at_cmd_resp;
+ api_frame_modem_status_t modem_status;
+ api_frame_transmit_status1_t transmit_status;
+ api_frame_transmit_status2_t transmit_status2;
+ api_frame_receive_packet_t receive_packet;
+ api_frame_explicit_rx_indicator_t explicit_rx_indicator;
+ api_frame_node_id_indicator_t node_id_indicator;
+ api_frame_remote_cmd_resp_t remote_cmd_resp;
+
+ api_frame_raw_packet_t raw_packet;
+ api_frame_raw_transmit_status_t raw_transmit_status;
+ } *api_data;
+ uint8_t checksum;
+
+#ifdef __KERNEL__
+ struct completion complete;
+ struct mutex mutex;
+#else
+ pthread_cond_t complete;
+ pthread_mutex_t mutex;
+#endif
+ struct xbee_api_frame *response;
+
+} _api_frame_t;
+
+/*
+ * TTY Helpers
+ */
+
+#ifndef __KERNEL__
+
+/*
+ * functions from:
+ * http://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c
+ */
+
+static int set_interface_attribs(int fd, int speed, int parity) {
+ struct termios tty;
+ memset(&tty, 0, sizeof tty);
+ if (tcgetattr(fd, &tty) != 0) {
+ printk(KERN_ERR "%s(): error %d from tcgetattr", __func__, errno);
+ return -1;
+ }
+
+ cfsetospeed(&tty, speed);
+ cfsetispeed(&tty, speed);
+
+ tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
+ // disable IGNBRK for mismatched speed tests; otherwise receive break
+ // as \000 chars
+ tty.c_iflag &= ~IGNBRK; // ignore break signal
+ tty.c_lflag = 0; // no signaling chars, no echo,
+ // no canonical processing
+ tty.c_oflag = 0; // no remapping, no delays
+ tty.c_cc[VMIN] = 0; // read doesn't block
+ tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
+
+ tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
+
+ tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
+ // enable reading
+ tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
+ tty.c_cflag |= parity;
+ tty.c_cflag &= ~CSTOPB;
+ tty.c_cflag &= ~CRTSCTS;
+
+ if (tcsetattr(fd, TCSANOW, &tty) != 0) {
+ printk(KERN_ERR "%s(): error %d from tcsetattr", __func__, errno);
+ return -1;
+ }
+ return 0;
+}
+
+static void
+set_blocking (int fd, int should_block)
+{
+ struct termios tty;
+ memset(&tty, 0, sizeof tty);
+ if (tcgetattr(fd, &tty) != 0) {
+ printk(KERN_ERR "%s(): error %d from tggetattr", __func__, errno);
+ return;
+ }
+
+ tty.c_cc[VMIN] = should_block ? 1 : 0;
+ tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
+
+ if (tcsetattr(fd, TCSANOW, &tty) != 0)
+ printk(KERN_ERR "%s(): error %d setting term attributes", __func__,
+ errno);
+}
+
+
+/*
+ * XBee Dev helpers
+ */
+static
+struct xbee_device *xbee_device_new()
+{
+ struct xbee_device *xbee_dev;
+#ifdef __KERNEL__
+ xbee_dev = kzalloc(sizeof(struct xbee_device),GFP_KERNEL);
+#else
+ xbee_dev = calloc(1,sizeof(struct xbee_device));
+#endif
+ if(!xbee_dev)
+ {
+ printk(KERN_ERR "%s(): unable to allocate memory\n", __func__);
+ BUG();
+ }
+ xbee_dev->escaped_mode = 1;
+ xbee_dev->frame_id_counter = 1;
+ xbee_dev->recv_state = RECV_STATE_WAIT_START_DELIM;
+// xbee_dev->rf_max_payload = 100;
+
+#ifdef __KERNEL__
+ mutex_init(&xbee_dev->mutex);
+#else
+ pthread_mutex_init(&xbee_dev->mutex,NULL);
+#endif
+
+ return xbee_dev;
+}
+
+static
+void xbee_device_release(struct xbee_device *xbee_dev)
+{
+ if(!xbee_dev)
+ {
+ printk(KERN_ERR "%s: xbee_dev NULL pointer\n", __func__);
+ BUG();
+ }
+ if(xbee_dev->recv_data)
+ {
+#ifdef __KERNEL__
+ kfree(xbee_dev->recv_data);
+#else
+ free(xbee_dev->recv_data);
+#endif
+ xbee_dev->recv_data = NULL;
+ }
+
+#ifndef __KERNEL__
+ pthread_mutex_destroy(&xbee_dev->mutex);
+#endif
+
+#ifndef __KERNEL__
+ if(xbee_dev->tty_fd>0)
+ {
+ close(xbee_dev->tty_fd);
+ }
+#endif
+
+#ifdef __KERNEL__
+ kfree(xbee_dev);
+#else
+ free(xbee_dev);
+#endif
+ xbee_dev = NULL;
+}
+
+#endif /* !__KERNEL__ */
+
+#ifdef __KERNEL__
+#ifdef XBEE_SYSFS_STATS
+typedef struct xbee_mac_stat {
+ int defined;
+ u8 addr[XBEE_ADDR_LEN];
+ unsigned long last_update; // in jiffies
+ u8 last_lqi;
+} xbee_mac_stat_t;
+
+#define XBEE_MOTE_MAXCOUNT 256
+typedef struct xbee_mac_stats {
+ struct mutex mutex;
+ xbee_mac_stat_t mote[XBEE_MOTE_MAXCOUNT];
+} xbee_stats_t;
+
+static xbee_stats_t *stats = NULL;
+
+int xbee_addr_equals(u8 addr1[XBEE_ADDR_LEN], u8 addr2[XBEE_ADDR_LEN])
+{
+ int i = 0;
+ for(i=0;i<XBEE_ADDR_LEN;i++)
+ {
+ if(addr1[i] != addr2[i])
+ return 0;
+ }
+ return 1;
+}
+
+xbee_mac_stat_t *xbee_stat_get_unsafe(u8 addr[XBEE_ADDR_LEN])
+{
+ int i = 0;
+ xbee_mac_stat_t *mac_stat = NULL;
+ for(i=0;i<XBEE_MOTE_MAXCOUNT;i++)
+ {
+ mac_stat = &stats->mote[i];
+ if(!mac_stat->defined)
+ {
+ memcpy(mac_stat->addr,addr, XBEE_ADDR_LEN);
+ mac_stat->defined = 1;
+ break;
+ } else if(xbee_addr_equals(addr,mac_stat->addr)){
+ break;
+ }
+ }
+ return mac_stat;
+}
+
+int xbee_stat_update_lqi(u8 addr[XBEE_ADDR_LEN], u8 lqi)
+{
+ xbee_mac_stat_t *mac_stat = NULL;
+ mutex_lock(&stats->mutex);
+ mac_stat = xbee_stat_get_unsafe(addr);
+ if(mac_stat)
+ {
+ mac_stat->last_lqi = lqi;
+ mac_stat->last_update = jiffies;
+ }
+ mutex_unlock(&stats->mutex);
+ return 0;
+}
+
+static const char *
+eui64_address_to_str(uint8_t addr[XBEE_ADDR_LEN],char eui64_addr_str[24]);
+
+#define XBEE_LQI_TABLE_MAX_LEN 4096
+
+static ssize_t
+xbee_stat_get_lqi_table(char str[XBEE_LQI_TABLE_MAX_LEN])
+{
+ ssize_t count = 0;
+ ssize_t len = 0;
+ int i = 0;
+ unsigned long now = jiffies;
+ unsigned long diff = 0;
+ xbee_mac_stat_t *mac_stat = NULL;
+ char eui64_addr_str[24] = {};
+ mutex_lock(&stats->mutex);
+ for(i = 0;i<XBEE_MOTE_MAXCOUNT; i++)
+ {
+ mac_stat = &stats->mote[i];
+ if(mac_stat && mac_stat->defined)
+ {
+ diff = (long) now - (long) mac_stat->last_update;
+ count = sprintf(str+len,"%s\t%6ld ms\t%3d (0x%02X)\n",eui64_address_to_str(mac_stat->addr,eui64_addr_str),diff * 1000 / HZ, mac_stat->last_lqi,mac_stat->last_lqi);
+ len += count;
+ }
+ }
+ mutex_unlock(&stats->mutex);
+ return len;
+}
+
+static int xbee_stats_init(void){
+ stats = kzalloc(sizeof(xbee_stats_t),GFP_KERNEL);
+ if(!stats)
+ return -ENOMEM;
+ mutex_init(&stats->mutex);
+ return 0;
+}
+
+static int xbee_stats_tini(void) {
+ if(stats)
+ kfree(stats);
+ return 0;
+}
+
+#endif
+#endif
+
+static uint8_t
+_xbee_device_gen_frame_id(struct xbee_device *xbee_dev)
+{
+ uint8_t frame_id;
+ if(!xbee_dev)
+ return 0;
+#ifdef __KERNEL__
+ mutex_lock(&xbee_dev->mutex);
+#else
+ pthread_mutex_lock(&xbee_dev->mutex);
+#endif
+ frame_id = xbee_dev->frame_id_counter = (xbee_dev->frame_id_counter+1)%XBEE_MAX_FRAME_ID;
+ if(frame_id == 0)
+ {
+ frame_id = xbee_dev->frame_id_counter = 1;
+ }
+#ifdef __KERNEL__
+ mutex_unlock(&xbee_dev->mutex);
+#else
+ pthread_mutex_unlock(&xbee_dev->mutex);
+#endif
+ return frame_id;
+}
+
+
+static int escape_required(uint8_t c);
+
+#ifndef __KERNEL__
+static int
+xbee_device_write_to_phy(struct xbee_device *xbee_dev, unsigned char *buf, unsigned int len)
+{
+ int i = 0;
+ uint8_t raw_byte = 0;
+ uint8_t escape_value = XBEE_ESCAPE;
+ unsigned char *raw_data = (unsigned char *) buf;
+ for(i = 0;i<len;i++){
+ raw_byte = ((unsigned char)*(raw_data+i));
+ if(i > 2 && xbee_dev->escaped_mode && escape_required(raw_byte))
+ {
+ //printf("%02X ",XBEE_ESCAPE);
+ write(xbee_dev->tty_fd,&escape_value,1);
+ raw_byte = raw_byte^0x20;
+ }
+ write(xbee_dev->tty_fd,&raw_byte,1);
+ //printf("%02X ",raw_byte);
+ }
+ printf("\n");
+ return len;
+}
+
+static int
+xbee_device_init(struct xbee_device *xbee_dev)
+{
+ if(!xbee_dev)
+ {
+ printk(KERN_ERR "%s: xbee_dev NULL pointer\n", __func__);
+ BUG();
+ }
+#ifndef __KERNEL__
+ if((xbee_dev->tty_fd = open(xbee_dev->tty_path,O_RDWR | O_NOCTTY | O_SYNC)) < 0)
+ {
+ printk(KERN_ERR "%s: error opening tty %s: %s\n", __func__, xbee_dev->tty_path,strerror(errno));
+ return -1;
+ }
+
+ set_interface_attribs (xbee_dev->tty_fd, B9600, 0); // set speed to 115,200 bps, 8n1 (no parity)
+ set_blocking (xbee_dev->tty_fd, 0); // set no blocking
+#endif /* !__KERNEL__ */
+ return 0;
+}
+#endif /* !__KERNEL__ */
+
+#ifdef __KERNEL__
+
+#define XBEE_FRAME_MAX_SIZE 255
+
+static struct mote_id {
+ int type;
+ u8 id[4];
+ char *description;
+} mote_ids[4] = {
+ {
+ .type = XBEE_XB08_DP_MOTE,
+ .id = {0x00,0x06,0x00,0x00},
+ .description = "XBee 868 point to multi-point (868MHz for EU market)",
+ },
+ {
+ .type = HM_TRP_433D_MOTE,
+ .id = {0xFF,0xFF,0x00,0x43},
+ .description = "HM-TRP Series 100mW Transceiver modules (HM-TRP-433D)",
+ },
+ {
+ .type = HM_TRP_868D_MOTE,
+ .id = {0xFF,0xFF,0x00,0x86},
+ .description = "HM-TRP Series 100mW Transceiver modules (HM-TRP-868D)",
+ },
+ {
+ .type = NOT_SUPPORTED_MOTE,
+ .id = {0x00,0x00,0x00,0x00},
+ .description = "Not supported device",
+ },
+};
+
+int mote_id_equals(u8 id1[4], u8 id2[4]){
+ int i = 0;
+ for(i=0;i<4;i++){
+ if(id1[i] != id2[i])
+ return 0;
+ }
+ return 1;
+}
+
+static int
+xbee_api_at_command_read_u32(struct xbee_device *xbee_dev, const char cmdid[3], uint8_t value[4]);
+
+static int
+xbee_device_get_type(struct xbee_device *xbee_dev){
+ int mote_type = UNKNOWN_MOTE;
+ u8 mote_id[4] = {0};
+ int i = 0;
+ int err = 0;
+ mutex_lock(&xbee_dev->mutex);
+ mote_type = xbee_dev->device_driver;
+ mutex_unlock(&xbee_dev->mutex);
+ if(mote_type != UNKNOWN_MOTE){
+ return mote_type;
+ }
+ err = xbee_api_at_command_read_u32(xbee_dev,"DD",mote_id);
+ if(err) {
+ printk(KERN_ERR "%s: error getting device type\n", __func__);
+ return err;
+ }
+ else {
+ print_hex_dump_bytes("xbee_device_get_type(): device type: ", DUMP_PREFIX_NONE, mote_id, 4);
+ }
+ while(mote_type != NOT_SUPPORTED_MOTE){
+ mote_type = mote_ids[i].type;
+ //printk(KERN_INFO "%s: checking type: %d: %s\n", __func__,mote_ids[i].type,mote_ids[i].description);
+ //print_hex_dump_bytes("xbee_device_get_type(): checking: ", DUMP_PREFIX_NONE, mote_ids[i].id, 4);
+ if(mote_id_equals(mote_ids[i].id,mote_id) || mote_type == NOT_SUPPORTED_MOTE){
+ mutex_lock(&xbee_dev->mutex);
+ xbee_dev->device_driver = mote_type;
+ mutex_unlock(&xbee_dev->mutex);
+ printk(KERN_INFO "xbee: found mote: %s\n", mote_ids[i].description);
+ break;
+ }
+ i++;
+ }
+ return mote_type;
+}
+
+static int
+xbee_device_write_to_phy(struct xbee_device *xbee_dev, unsigned char *buf, unsigned int len){
+ struct tty_struct *tty;
+ unsigned int sent = 0;
+
+ int i = 0;
+ u8 raw_byte = 0;
+ u8 escape_value = XBEE_ESCAPE;
+ unsigned char *raw_data = (unsigned char *) buf;
+
+ BUG_ON(!xbee_dev);
+ tty = xbee_dev->tty;
+ if (!tty)
+ return -ENODEV;
+
+ /* Debug info */
+ pr_debug("%s(): %d bytes frame\n", __func__, len);
+#ifdef DEBUG
+ print_hex_dump_bytes("send_pending_data ", DUMP_PREFIX_NONE,
+ buf, len);
+#endif
+
+ pr_debug("%s(): sending %d\n", __func__,len);
+ for(i = 0;i<len;i++){
+ raw_byte = ((unsigned char)*(raw_data+i));
+ if(i > 0 && xbee_dev->escaped_mode && escape_required(raw_byte))
+ {
+ if (tty->driver->ops->write(tty, &escape_value,1) != 1) {
+ printk(KERN_ERR "%s: device write failed\n", __func__);
+ return -1;
+ }
+ raw_byte = raw_byte^0x20;
+ }
+ if (tty->driver->ops->write(tty, &raw_byte,1) != 1) {
+ printk(KERN_ERR "%s: device write failed\n", __func__);
+ return -1;
+ }
+ sent++;
+ }
+ return sent;
+}
+#endif /* __KERNEL__ */
+
+static _api_frame_t *temporary_frames_list[1000] = {};
+static int
+_api_frame_should_wait_response(_api_frame_t *api_frame);
+
+static int
+xbee_device_add_pendent_req(struct xbee_device *xbee_dev, _api_frame_t *api_frame)
+{
+ int index = 0;
+
+ if(xbee_dev == NULL)
+ {
+ printk(KERN_ERR "%s: xbee_dev NULL pointer\n", __func__);
+ BUG();
+ }
+ if(api_frame == NULL)