-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathXBeeDevice.java
More file actions
1992 lines (1850 loc) · 82.1 KB
/
Copy pathXBeeDevice.java
File metadata and controls
1992 lines (1850 loc) · 82.1 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
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
/**
* Copyright (c) 2014-2015 Digi International Inc.,
* All rights not expressly granted are reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
* =======================================================================
*/
package com.digi.xbee.api;
import java.io.IOException;
import com.digi.xbee.api.connection.DataReader;
import com.digi.xbee.api.connection.IConnectionInterface;
import com.digi.xbee.api.connection.serial.SerialPortParameters;
import com.digi.xbee.api.exceptions.ATCommandException;
import com.digi.xbee.api.exceptions.InterfaceAlreadyOpenException;
import com.digi.xbee.api.exceptions.InterfaceNotOpenException;
import com.digi.xbee.api.exceptions.InvalidOperatingModeException;
import com.digi.xbee.api.exceptions.OperationNotSupportedException;
import com.digi.xbee.api.exceptions.TimeoutException;
import com.digi.xbee.api.exceptions.XBeeException;
import com.digi.xbee.api.listeners.IIOSampleReceiveListener;
import com.digi.xbee.api.listeners.IModemStatusReceiveListener;
import com.digi.xbee.api.listeners.IPacketReceiveListener;
import com.digi.xbee.api.listeners.IDataReceiveListener;
import com.digi.xbee.api.models.APIOutputMode;
import com.digi.xbee.api.models.ATCommand;
import com.digi.xbee.api.models.ATCommandResponse;
import com.digi.xbee.api.models.ExplicitXBeeMessage;
import com.digi.xbee.api.models.ModemStatusEvent;
import com.digi.xbee.api.models.OperatingMode;
import com.digi.xbee.api.models.XBee16BitAddress;
import com.digi.xbee.api.models.XBee64BitAddress;
import com.digi.xbee.api.models.XBeeMessage;
import com.digi.xbee.api.models.XBeePacketsQueue;
import com.digi.xbee.api.models.XBeeProtocol;
import com.digi.xbee.api.models.XBeeTransmitOptions;
import com.digi.xbee.api.packet.APIFrameType;
import com.digi.xbee.api.packet.XBeeAPIPacket;
import com.digi.xbee.api.packet.XBeePacket;
import com.digi.xbee.api.packet.common.ExplicitAddressingPacket;
import com.digi.xbee.api.packet.common.ExplicitRxIndicatorPacket;
import com.digi.xbee.api.packet.common.ReceivePacket;
import com.digi.xbee.api.packet.common.TransmitPacket;
import com.digi.xbee.api.packet.raw.RX16Packet;
import com.digi.xbee.api.packet.raw.RX64Packet;
import com.digi.xbee.api.packet.raw.TX64Packet;
import com.digi.xbee.api.utils.HexUtils;
import com.digi.xbee.api.utils.StringUtils;
/**
* This class represents a local XBee device.
*
* @see DigiMeshDevice
* @see DigiPointDevice
* @see Raw802Device
* @see ZigBeeDevice
*/
public class XBeeDevice extends AbstractXBeeDevice {
// Constants.
private static int TIMEOUT_RESET = 5000;
private static int TIMEOUT_READ_PACKET = 3000;
private static String COMMAND_MODE_CHAR = "+";
private static String COMMAND_MODE_OK = "OK\r";
// Variables.
protected XBeeNetwork network;
private Object resetLock = new Object();
private boolean modemStatusReceived = false;
/**
* Class constructor. Instantiates a new {@code XBeeDevice} object
* physically connected to the given port name and configured at the
* provided baud rate.
*
* @param port Serial port name where XBee device is attached to.
* @param baudRate Serial port baud rate to communicate with the device.
* Other connection parameters will be set as default (8
* data bits, 1 stop bit, no parity, no flow control).
*
* @throws IllegalArgumentException if {@code baudRate < 0}.
* @throws NullPointerException if {@code port == null}.
*
* @see #XBeeDevice(IConnectionInterface)
* @see #XBeeDevice(String, SerialPortParameters)
* @see #XBeeDevice(String, int, int, int, int, int)
*/
public XBeeDevice(String port, int baudRate) {
super(port, baudRate);
}
/**
* Class constructor. Instantiates a new {@code XBeeDevice} object
* physically connected to the given port name and configured to communicate
* with the provided serial settings.
*
* @param port Serial port name where XBee device is attached to.
* @param baudRate Serial port baud rate to communicate with the device.
* @param dataBits Serial port data bits.
* @param stopBits Serial port data bits.
* @param parity Serial port data bits.
* @param flowControl Serial port data bits.
*
* @throws IllegalArgumentException if {@code baudRate < 0} or
* if {@code dataBits < 0} or
* if {@code stopBits < 0} or
* if {@code parity < 0} or
* if {@code flowControl < 0}.
* @throws NullPointerException if {@code port == null}.
*
* @see #XBeeDevice(IConnectionInterface)
* @see #XBeeDevice(String, int)
* @see #XBeeDevice(String, SerialPortParameters)
*/
public XBeeDevice(String port, int baudRate, int dataBits, int stopBits, int parity, int flowControl) {
super(port, baudRate, dataBits, stopBits, parity, flowControl);
}
/**
* Class constructor. Instantiates a new {@code XBeeDevice} object
* physically connected to the given port name and configured to communicate
* with the provided serial settings.
*
* @param port Serial port name where XBee device is attached to.
* @param serialPortParameters Object containing the serial port parameters.
*
* @throws NullPointerException if {@code port == null} or
* if {@code serialPortParameters == null}.
*
* @see #XBeeDevice(IConnectionInterface)
* @see #XBeeDevice(String, int)
* @see #XBeeDevice(String, int, int, int, int, int)
* @see com.digi.xbee.api.connection.serial.SerialPortParameters
*/
public XBeeDevice(String port, SerialPortParameters serialPortParameters) {
super(port, serialPortParameters);
}
/**
* Class constructor. Instantiates a new {@code XBeeDevice} object with the
* given connection interface.
*
* @param connectionInterface The connection interface with the physical
* XBee device.
*
* @throws NullPointerException if {@code connectionInterface == null}.
*
* @see #XBeeDevice(String, int)
* @see #XBeeDevice(String, SerialPortParameters)
* @see #XBeeDevice(String, int, int, int, int, int)
* @see com.digi.xbee.api.connection.IConnectionInterface
*/
public XBeeDevice(IConnectionInterface connectionInterface) {
super(connectionInterface);
}
/**
* Opens the connection interface associated with this XBee device.
*
* <p>When opening the device an information reading process is
* automatically performed. This includes:</p>
*
* <ul>
* <li>64-bit address.</li>
* <li>Node Identifier.</li>
* <li>Hardware version.</li>
* <li>Firmware version.</li>
* <li>XBee device protocol.</li>
* <li>16-bit address (not for DigiMesh modules).</li>
* </ul>
*
* @throws InterfaceAlreadyOpenException if this device connection is
* already open.
* @throws XBeeException if there is any problem opening this device
* connection.
*
* @see #close()
* @see #isOpen()
*/
public void open() throws XBeeException {
logger.info(toString() + "Opening the connection interface...");
// First, verify that the connection is not already open.
if (connectionInterface.isOpen())
throw new InterfaceAlreadyOpenException();
// Connect the interface.
connectionInterface.open();
logger.info(toString() + "Connection interface open.");
// Initialize the data reader.
dataReader = new DataReader(connectionInterface, operatingMode, this);
dataReader.start();
// Wait 10 milliseconds until the dataReader thread is started.
// This is because when the connection is opened immediately after
// closing it, there is sometimes a concurrency problem and the
// dataReader thread never dies.
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
// Determine the operating mode of the XBee device if it is unknown.
if (operatingMode == OperatingMode.UNKNOWN)
operatingMode = determineOperatingMode();
// Check if the operating mode is a valid and supported one.
if (operatingMode == OperatingMode.UNKNOWN) {
close();
throw new InvalidOperatingModeException("Could not determine operating mode.");
} else if (operatingMode == OperatingMode.AT) {
close();
throw new InvalidOperatingModeException(operatingMode);
}
// Read the device info (obtain its parameters and protocol).
try {
readDeviceInfo();
} catch (ATCommandException e) {
throw new XBeeException("Error reading device information.", e);
}
}
/**
* Closes the connection interface associated with this XBee device.
*
* @see #isOpen()
* @see #open()
*/
public void close() {
// Stop XBee reader.
if (dataReader != null && dataReader.isRunning())
dataReader.stopReader();
// Close interface.
connectionInterface.close();
logger.info(toString() + "Connection interface closed.");
}
/**
* Returns whether the connection interface associated to this device is
* already open.
*
* @return {@code true} if the interface is open, {@code false} otherwise.
*
* @see #close()
* @see #open()
*/
public boolean isOpen() {
if (connectionInterface != null)
return connectionInterface.isOpen();
return false;
}
/**
* Always returns {@code false}, since this is always a local device.
*
* @return {@code false} since it is a local device.
*/
@Override
public boolean isRemote() {
return false;
}
/**
* Returns the network associated with this XBee device.
*
* @return The XBee network of the device.
*
* @throws InterfaceNotOpenException if this device connection is not open.
*
* @see XBeeNetwork
*/
public XBeeNetwork getNetwork() {
if (!isOpen())
throw new InterfaceNotOpenException();
if (network == null)
network = new XBeeNetwork(this);
return network;
}
/**
* Returns the Operating mode (AT, API or API escaped) of this XBee device.
*
* @return The operating mode of this XBee device.
*
* @see com.digi.xbee.api.models.OperatingMode
*/
@Override
public OperatingMode getOperatingMode() {
return super.getOperatingMode();
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#getNextFrameID()
*/
@Override
protected int getNextFrameID() {
return super.getNextFrameID();
}
/**
* Returns this XBee device configured timeout for receiving packets in
* synchronous operations.
*
* @return The current receive timeout in milliseconds.
*
* @see #setReceiveTimeout(int)
*/
public int getReceiveTimeout() {
return receiveTimeout;
}
/**
* Configures this XBee device timeout in milliseconds for receiving
* packets in synchronous operations.
*
* @param receiveTimeout The new receive timeout in milliseconds.
*
* @throws IllegalArgumentException if {@code receiveTimeout < 0}.
*
* @see #getReceiveTimeout()
*/
public void setReceiveTimeout(int receiveTimeout) {
if (receiveTimeout < 0)
throw new IllegalArgumentException("Receive timeout cannot be less than 0.");
this.receiveTimeout = receiveTimeout;
}
/**
* Determines the operating mode of this XBee device.
*
* @return The operating mode of the XBee device.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws OperationNotSupportedException if the packet is being sent from
* a remote device.
*
* @see com.digi.xbee.api.models.OperatingMode
*/
protected OperatingMode determineOperatingMode() throws OperationNotSupportedException {
try {
// Check if device is in API or API Escaped operating modes.
operatingMode = OperatingMode.API;
dataReader.setXBeeReaderMode(operatingMode);
ATCommandResponse response = sendATCommand(new ATCommand("AP"));
if (response.getResponse() != null && response.getResponse().length > 0) {
if (response.getResponse()[0] != OperatingMode.API.getID()) {
operatingMode = OperatingMode.API_ESCAPE;
dataReader.setXBeeReaderMode(operatingMode);
}
logger.debug(toString() + "Using {}.", operatingMode.getName());
return operatingMode;
}
} catch (TimeoutException e) {
// Check if device is in AT operating mode.
operatingMode = OperatingMode.AT;
dataReader.setXBeeReaderMode(operatingMode);
try {
// It is necessary to wait at least 1 second to enter in
// command mode after sending any data to the device.
Thread.sleep(TIMEOUT_BEFORE_COMMAND_MODE);
// Try to enter in AT command mode, if so the module is in AT mode.
boolean success = enterATCommandMode();
if (success)
return OperatingMode.AT;
} catch (TimeoutException e1) {
logger.error(e1.getMessage(), e1);
} catch (InvalidOperatingModeException e1) {
logger.error(e1.getMessage(), e1);
} catch (InterruptedException e1) {
logger.error(e1.getMessage(), e1);
}
} catch (InvalidOperatingModeException e) {
logger.error("Invalid operating mode", e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return OperatingMode.UNKNOWN;
}
/**
* Attempts to put this device in AT Command mode. Only valid if device is
* working in AT mode.
*
* @return {@code true} if the device entered in AT command mode,
* {@code false} otherwise.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws InvalidOperatingModeException if the operating mode cannot be
* determined or is not supported.
* @throws TimeoutException if the configured time for this device expires.
*/
private boolean enterATCommandMode() throws InvalidOperatingModeException, TimeoutException {
if (!connectionInterface.isOpen())
throw new InterfaceNotOpenException();
if (operatingMode != OperatingMode.AT)
throw new InvalidOperatingModeException("Invalid mode. Command mode can be only accessed while in AT mode.");
// Enter in AT command mode (send '+++'). The process waits 1,5 seconds for the 'OK\n'.
byte[] readData = new byte[256];
try {
// Send the command mode sequence.
connectionInterface.writeData(StringUtils.stringToByteArray(COMMAND_MODE_CHAR));
connectionInterface.writeData(StringUtils.stringToByteArray(COMMAND_MODE_CHAR));
connectionInterface.writeData(StringUtils.stringToByteArray(COMMAND_MODE_CHAR));
// Wait some time to let the module generate a response.
Thread.sleep(TIMEOUT_ENTER_COMMAND_MODE);
// Read data from the device (it should answer with 'OK\r').
int readBytes = connectionInterface.readData(readData);
if (readBytes < COMMAND_MODE_OK.length())
throw new TimeoutException();
// Check if the read data is 'OK\r'.
String readString = StringUtils.byteArrayToString(readData, 0, readBytes);
if (!readString.contains(COMMAND_MODE_OK))
return false;
// Read data was 'OK\r'.
return true;
} catch (IOException e) {
logger.error(e.getMessage(), e);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
return false;
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#addPacketListener(com.digi.xbee.api.listeners.IPacketReceiveListener)
*/
@Override
public void addPacketListener(IPacketReceiveListener listener) {
super.addPacketListener(listener);
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#removePacketListener(com.digi.xbee.api.listeners.IPacketReceiveListener)
*/
@Override
public void removePacketListener(IPacketReceiveListener listener) {
super.removePacketListener(listener);
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#addDataListener(com.digi.xbee.api.listeners.IDataReceiveListener)
*/
@Override
public void addDataListener(IDataReceiveListener listener) {
super.addDataListener(listener);
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#removeDataListener(com.digi.xbee.api.listeners.IDataReceiveListener)
*/
@Override
public void removeDataListener(IDataReceiveListener listener) {
super.removeDataListener(listener);
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#addIOSampleListener(com.digi.xbee.api.listeners.IIOSampleReceiveListener)
*/
@Override
public void addIOSampleListener(IIOSampleReceiveListener listener) {
super.addIOSampleListener(listener);
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#removeIOSampleListener(com.digi.xbee.api.listeners.IIOSampleReceiveListener)
*/
@Override
public void removeIOSampleListener(IIOSampleReceiveListener listener) {
super.removeIOSampleListener(listener);
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#addModemStatusListener(com.digi.xbee.api.listeners.IModemStatusReceiveListener)
*/
@Override
public void addModemStatusListener(IModemStatusReceiveListener listener) {
super.addModemStatusListener(listener);
}
/*
* (non-Javadoc)
* @see com.digi.xbee.api.AbstractXBeeDevice#removeModemStatusListener(com.digi.xbee.api.listeners.IModemStatusReceiveListener)
*/
@Override
public void removeModemStatusListener(IModemStatusReceiveListener listener) {
super.removeModemStatusListener(listener);
}
/**
* Sends asynchronously the provided data to the XBee device of the network
* corresponding to the given 64-bit address.
*
* <p>Asynchronous transmissions do not wait for answer from the remote
* device or for transmit status packet.</p>
*
* @param address The 64-bit address of the XBee that will receive the data.
* @param data Byte array containing the data to be sent.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code address == null} or
* if {@code data == null}.
* @throws XBeeException if there is any XBee related exception.
*
* @see #sendData(RemoteXBeeDevice, byte[])
* @see #sendData(XBee64BitAddress, byte[])
* @see #sendData(XBee64BitAddress, XBee16BitAddress, byte[])
* @see #sendDataAsync(RemoteXBeeDevice, byte[])
* @see #sendDataAsync(XBee64BitAddress, XBee16BitAddress, byte[])
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
protected void sendDataAsync(XBee64BitAddress address, byte[] data) throws XBeeException {
// Verify the parameters are not null, if they are null, throw an exception.
if (address == null)
throw new NullPointerException("Address cannot be null");
if (data == null)
throw new NullPointerException("Data cannot be null");
// Check if device is remote.
if (isRemote())
throw new OperationNotSupportedException("Cannot send data to a remote device from a remote device.");
logger.debug(toString() + "Sending data asynchronously to {} >> {}.", address, HexUtils.prettyHexString(data));
XBeePacket xbeePacket;
switch (getXBeeProtocol()) {
case RAW_802_15_4:
xbeePacket = new TX64Packet(getNextFrameID(), address, XBeeTransmitOptions.NONE, data);
break;
default:
xbeePacket = new TransmitPacket(getNextFrameID(), address, XBee16BitAddress.UNKNOWN_ADDRESS, 0, XBeeTransmitOptions.NONE, data);
}
sendAndCheckXBeePacket(xbeePacket, true);
}
/**
* Sends asynchronously the provided data to the XBee device of the network
* corresponding to the given 64-bit/16-bit address.
*
* <p>Asynchronous transmissions do not wait for answer from the remote
* device or for transmit status packet.</p>
*
* @param address64Bit The 64-bit address of the XBee that will receive the
* data.
* @param address16Bit The 16-bit address of the XBee that will receive the
* data. If it is unknown the
* {@code XBee16BitAddress.UNKNOWN_ADDRESS} must be
* used.
* @param data Byte array containing the data to be sent.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code address64Bit == null} or
* if {@code address16Bit == null} or
* if {@code data == null}.
* @throws XBeeException if a remote device is trying to send data or
* if there is any other XBee related exception.
*
* @see #sendData(RemoteXBeeDevice, byte[])
* @see #sendData(XBee64BitAddress, byte[])
* @see #sendData(XBee64BitAddress, XBee16BitAddress, byte[])
* @see #sendDataAsync(RemoteXBeeDevice, byte[])
* @see #sendDataAsync(XBee64BitAddress, byte[])
* @see com.digi.xbee.api.models.XBee16BitAddress
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
protected void sendDataAsync(XBee64BitAddress address64Bit, XBee16BitAddress address16Bit, byte[] data) throws XBeeException {
// Verify the parameters are not null, if they are null, throw an exception.
if (address64Bit == null)
throw new NullPointerException("64-bit address cannot be null");
if (address16Bit == null)
throw new NullPointerException("16-bit address cannot be null");
if (data == null)
throw new NullPointerException("Data cannot be null");
// Check if device is remote.
if (isRemote())
throw new OperationNotSupportedException("Cannot send data to a remote device from a remote device.");
logger.debug(toString() + "Sending data asynchronously to {}[{}] >> {}.",
address64Bit, address16Bit, HexUtils.prettyHexString(data));
XBeePacket xbeePacket = new TransmitPacket(getNextFrameID(), address64Bit, address16Bit, 0, XBeeTransmitOptions.NONE, data);
sendAndCheckXBeePacket(xbeePacket, true);
}
/**
* Sends the provided data to the provided XBee device asynchronously
* choosing the optimal send method depending on the protocol of the local
* XBee device.
*
* <p>Asynchronous transmissions do not wait for answer from the remote
* device or for transmit status packet.</p>
*
* @param remoteXBeeDevice The XBee device of the network that will receive the
* data.
* @param data Byte array containing the data to be sent.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code remoteXBeeDevice == null} or
* if {@code data == null}.
* @throws XBeeException if there is any XBee related exception.
*
* @see #sendData(RemoteXBeeDevice, byte[])
* @see #sendData(XBee64BitAddress, byte[])
* @see #sendData(XBee64BitAddress, XBee16BitAddress, byte[])
* @see #sendDataAsync(XBee64BitAddress, byte[])
* @see #sendDataAsync(XBee64BitAddress, XBee16BitAddress, byte[])
* @see RemoteXBeeDevice
*/
public void sendDataAsync(RemoteXBeeDevice remoteXBeeDevice, byte[] data) throws XBeeException {
if (remoteXBeeDevice == null)
throw new NullPointerException("Remote XBee device cannot be null");
switch (getXBeeProtocol()) {
case ZIGBEE:
case DIGI_POINT:
if (remoteXBeeDevice.get64BitAddress() != null && remoteXBeeDevice.get16BitAddress() != null)
sendDataAsync(remoteXBeeDevice.get64BitAddress(), remoteXBeeDevice.get16BitAddress(), data);
else
sendDataAsync(remoteXBeeDevice.get64BitAddress(), data);
break;
case RAW_802_15_4:
if (this instanceof Raw802Device) {
if (remoteXBeeDevice.get64BitAddress() != null)
((Raw802Device)this).sendDataAsync(remoteXBeeDevice.get64BitAddress(), data);
else
((Raw802Device)this).sendDataAsync(remoteXBeeDevice.get16BitAddress(), data);
} else
sendDataAsync(remoteXBeeDevice.get64BitAddress(), data);
break;
case DIGI_MESH:
default:
sendDataAsync(remoteXBeeDevice.get64BitAddress(), data);
}
}
/**
* Sends the provided data to the XBee device of the network corresponding
* to the given 64-bit address.
*
* <p>This method blocks till a success or error response arrives or the
* configured receive timeout expires.</p>
*
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
* method and can be consulted with {@code getReceiveTimeout} method.</p>
*
* <p>For non-blocking operations use the method
* {@link #sendDataAsync(XBee64BitAddress, byte[])}.</p>
*
* @param address The 64-bit address of the XBee that will receive the data.
* @param data Byte array containing the data to be sent.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code address == null} or
* if {@code data == null}.
* @throws TimeoutException if there is a timeout sending the data.
* @throws XBeeException if there is any other XBee related exception.
*
* @see #getReceiveTimeout()
* @see #setReceiveTimeout(int)
* @see #sendData(RemoteXBeeDevice, byte[])
* @see #sendData(XBee64BitAddress, XBee16BitAddress, byte[])
* @see #sendDataAsync(RemoteXBeeDevice, byte[])
* @see #sendDataAsync(XBee64BitAddress, byte[])
* @see #sendDataAsync(XBee64BitAddress, XBee16BitAddress, byte[])
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
protected void sendData(XBee64BitAddress address, byte[] data) throws TimeoutException, XBeeException {
// Verify the parameters are not null, if they are null, throw an exception.
if (address == null)
throw new NullPointerException("Address cannot be null");
if (data == null)
throw new NullPointerException("Data cannot be null");
// Check if device is remote.
if (isRemote())
throw new OperationNotSupportedException("Cannot send data to a remote device from a remote device.");
logger.debug(toString() + "Sending data to {} >> {}.", address, HexUtils.prettyHexString(data));
XBeePacket xbeePacket;
switch (getXBeeProtocol()) {
case RAW_802_15_4:
xbeePacket = new TX64Packet(getNextFrameID(), address, XBeeTransmitOptions.NONE, data);
break;
default:
xbeePacket = new TransmitPacket(getNextFrameID(), address, XBee16BitAddress.UNKNOWN_ADDRESS, 0, XBeeTransmitOptions.NONE, data);
}
sendAndCheckXBeePacket(xbeePacket, false);
}
/**
* Sends the provided data to the XBee device of the network corresponding
* to the given 64-bit/16-bit address.
*
* <p>This method blocks till a success or error response arrives or the
* configured receive timeout expires.</p>
*
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
* method and can be consulted with {@code getReceiveTimeout} method.</p>
*
* <p>For non-blocking operations use the method
* {@link #sendDataAsync(XBee64BitAddress, XBee16BitAddress, byte[])}.</p>
*
* @param address64Bit The 64-bit address of the XBee that will receive the
* data.
* @param address16Bit The 16-bit address of the XBee that will receive the
* data. If it is unknown the
* {@code XBee16BitAddress.UNKNOWN_ADDRESS} must be
* used.
* @param data Byte array containing the data to be sent.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code address64Bit == null} or
* if {@code address16Bit == null} or
* if {@code data == null}.
* @throws TimeoutException if there is a timeout sending the data.
* @throws XBeeException if a remote device is trying to send data or
* if there is any other XBee related exception.
*
* @see #getReceiveTimeout()
* @see #setReceiveTimeout(int)
* @see #sendData(RemoteXBeeDevice, byte[])
* @see #sendData(XBee64BitAddress, byte[])
* @see #sendDataAsync(RemoteXBeeDevice, byte[])
* @see #sendDataAsync(XBee64BitAddress, byte[])
* @see #sendDataAsync(XBee64BitAddress, XBee16BitAddress, byte[])
* @see com.digi.xbee.api.models.XBee16BitAddress
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
protected void sendData(XBee64BitAddress address64Bit, XBee16BitAddress address16Bit, byte[] data) throws TimeoutException, XBeeException {
// Verify the parameters are not null, if they are null, throw an exception.
if (address64Bit == null)
throw new NullPointerException("64-bit address cannot be null");
if (address16Bit == null)
throw new NullPointerException("16-bit address cannot be null");
if (data == null)
throw new NullPointerException("Data cannot be null");
// Check if device is remote.
if (isRemote())
throw new OperationNotSupportedException("Cannot send data to a remote device from a remote device.");
logger.debug(toString() + "Sending data to {}[{}] >> {}.",
address64Bit, address16Bit, HexUtils.prettyHexString(data));
XBeePacket xbeePacket = new TransmitPacket(getNextFrameID(), address64Bit, address16Bit, 0, XBeeTransmitOptions.NONE, data);
sendAndCheckXBeePacket(xbeePacket, false);
}
/**
* Sends the provided data to the given XBee device choosing the optimal
* send method depending on the protocol of the local XBee device.
*
* <p>This method blocks till a success or error response arrives or the
* configured receive timeout expires.</p>
*
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
* method and can be consulted with {@code getReceiveTimeout} method.</p>
*
* <p>For non-blocking operations use the method
* {@link #sendDataAsync(RemoteXBeeDevice, byte[])}.</p>
*
* @param remoteXBeeDevice The XBee device of the network that will receive
* the data.
* @param data Byte array containing the data to be sent.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code xbeeDevice == null} or
* if {@code data == null}.
* @throws TimeoutException if there is a timeout sending the data.
* @throws XBeeException if there is any other XBee related exception.
*
* @see #getReceiveTimeout()
* @see #setReceiveTimeout(int)
* @see #sendData(XBee64BitAddress, byte[])
* @see #sendData(XBee64BitAddress, XBee16BitAddress, byte[])
* @see #sendDataAsync(RemoteXBeeDevice, byte[])
* @see #sendDataAsync(XBee64BitAddress, byte[])
* @see #sendDataAsync(XBee64BitAddress, XBee16BitAddress, byte[])
* @see com.digi.xbee.api.RemoteXBeeDevice
*/
public void sendData(RemoteXBeeDevice remoteXBeeDevice, byte[] data) throws TimeoutException, XBeeException {
if (remoteXBeeDevice == null)
throw new NullPointerException("Remote XBee device cannot be null");
switch (getXBeeProtocol()) {
case ZIGBEE:
case DIGI_POINT:
if (remoteXBeeDevice.get64BitAddress() != null && remoteXBeeDevice.get16BitAddress() != null)
sendData(remoteXBeeDevice.get64BitAddress(), remoteXBeeDevice.get16BitAddress(), data);
else
sendData(remoteXBeeDevice.get64BitAddress(), data);
break;
case RAW_802_15_4:
if (this instanceof Raw802Device) {
if (remoteXBeeDevice.get64BitAddress() != null)
((Raw802Device)this).sendData(remoteXBeeDevice.get64BitAddress(), data);
else
((Raw802Device)this).sendData(remoteXBeeDevice.get16BitAddress(), data);
} else
sendData(remoteXBeeDevice.get64BitAddress(), data);
break;
case DIGI_MESH:
default:
sendData(remoteXBeeDevice.get64BitAddress(), data);
}
}
/**
* Sends the provided data to all the XBee nodes of the network (broadcast).
*
* <p>This method blocks till a success or error transmit status arrives or
* the configured receive timeout expires.</p>
*
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
* method and can be consulted with {@code getReceiveTimeout} method.</p>
*
* @param data Byte array containing the data to be sent.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code data == null}.
* @throws TimeoutException if there is a timeout sending the data.
* @throws XBeeException if there is any other XBee related exception.
*
* @see #getReceiveTimeout()
* @see #setReceiveTimeout(int)
*/
public void sendBroadcastData(byte[] data) throws TimeoutException, XBeeException {
sendData(XBee64BitAddress.BROADCAST_ADDRESS, data);
}
/**
* Sends asynchronously the provided data in application layer mode to the
* XBee device of the network corresponding to the given 64-bit address.
* Application layer mode means that you need to specify the application
* layer fields to be sent with the data.
*
* <p>Asynchronous transmissions do not wait for answer from the remote
* device or for transmit status packet.</p>
*
* @param address The 64-bit address of the XBee that will receive the data.
* @param sourceEndpoint Source endpoint for the transmission.
* @param destEndpoint Destination endpoint for the transmission.
* @param clusterID Cluster ID used in the transmission.
* @param profileID Profile ID used in the transmission.
* @param data Byte array containing the data to be sent.
*
* @throws IllegalArgumentException if {@code sourceEndpoint < 0} or
* if {@code sourceEndpoint > 0xFF} or
* if {@code destEndpoint < 0} or
* if {@code destEndpoint > 0xFF} or
* if {@code clusterID < 0} or
* if {@code clusterID > 0xFFFF} or
* if {@code profileID < 0} or
* if {@code profileID > 0xFFFF}.
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code address == null} or
* if {@code data == null}.
* @throws XBeeException if there is any other XBee related exception.
*
* @see #sendExplicitData(RemoteXBeeDevice, int, int, int, int, byte[])
* @see #sendExplicitData(XBee64BitAddress, int, int, int, int, byte[])
* @see #sendExplicitData(XBee64BitAddress, XBee16BitAddress, int, int, int, int, byte[])
* @see #sendExplicitDataAsync(RemoteXBeeDevice, int, int, int, int, byte[])
* @see #sendExplicitDataAsync(XBee64BitAddress, XBee16BitAddress, int, int, int, int, byte[])
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
protected void sendExplicitDataAsync(XBee64BitAddress address, int sourceEndpoint, int destEndpoint, int clusterID,
int profileID, byte[] data) throws XBeeException {
if (address == null)
throw new NullPointerException("Address cannot be null");
if (data == null)
throw new NullPointerException("Data cannot be null.");
if (sourceEndpoint < 0 || sourceEndpoint > 0xFF)
throw new IllegalArgumentException("Source endpoint must be between 0 and 0xFF.");
if (destEndpoint < 0 || destEndpoint > 0xFF)
throw new IllegalArgumentException("Destination endpoint must be between 0 and 0xFF.");
if (clusterID < 0 || clusterID > 0xFFFF)
throw new IllegalArgumentException("Cluster ID must be between 0 and 0xFFFF.");
if (profileID < 0 || profileID > 0xFFFF)
throw new IllegalArgumentException("Profile ID must be between 0 and 0xFFFF.");
// Check if device is remote.
if (isRemote())
throw new OperationNotSupportedException("Cannot send explicit data to a remote device from a remote device.");
logger.debug(toString() + "Sending explicit data asynchronously to {} [{} - {} - {} - {}] >> {}.", address,
String.format("%02X", sourceEndpoint), String.format("%02X", destEndpoint),
String.format("%04X", clusterID), String.format("%04X", profileID),
HexUtils.prettyHexString(data));
XBeePacket xbeePacket = new ExplicitAddressingPacket(getNextFrameID(), address, XBee16BitAddress.UNKNOWN_ADDRESS, sourceEndpoint, destEndpoint, clusterID, profileID, 0, XBeeTransmitOptions.NONE, data);
sendAndCheckXBeePacket(xbeePacket, true);
}
/**
* Sends asynchronously the provided data in application layer mode to the
* XBee device of the network corresponding to the given 64-bit/16-bit
* address. Application layer mode means that you need to specify the
* application layer fields to be sent with the data.
*
* <p>Asynchronous transmissions do not wait for answer from the remote
* device or for transmit status packet.</p>
*
* @param address64Bit The 64-bit address of the XBee that will receive the
* data.
* @param address16Bit The 16-bit address of the XBee that will receive the
* data. If it is unknown the
* {@code XBee16BitAddress.UNKNOWN_ADDRESS} must be
* used.
* @param sourceEndpoint Source endpoint for the transmission.
* @param destEndpoint Destination endpoint for the transmission.
* @param clusterID Cluster ID used in the transmission.
* @param profileID Profile ID used in the transmission.
* @param data Byte array containing the data to be sent.
*
* @throws IllegalArgumentException if {@code sourceEndpoint < 0} or
* if {@code sourceEndpoint > 0xFF} or
* if {@code destEndpoint < 0} or
* if {@code destEndpoint > 0xFF} or
* if {@code clusterID < 0} or
* if {@code clusterID > 0xFFFF} or
* if {@code profileID < 0} or
* if {@code profileID > 0xFFFF}.
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws NullPointerException if {@code address64Bit == null} or
* if {@code address16Bit == null} or
* if {@code data == null}.
* @throws XBeeException if there is any other XBee related exception.
*
* @see #sendExplicitData(RemoteXBeeDevice, int, int, int, int, byte[])
* @see #sendExplicitData(XBee64BitAddress, int, int, int, int, byte[])
* @see #sendExplicitData(XBee64BitAddress, XBee16BitAddress, int, int, int, int, byte[])
* @see #sendExplicitDataAsync(RemoteXBeeDevice, int, int, int, int, byte[])
* @see #sendExplicitDataAsync(XBee64BitAddress, int, int, int, int, byte[])
* @see com.digi.xbee.api.models.XBee16BitAddress
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
protected void sendExplicitDataAsync(XBee64BitAddress address64Bit, XBee16BitAddress address16Bit, int sourceEndpoint,
int destEndpoint, int clusterID, int profileID, byte[] data) throws XBeeException {
if (address64Bit == null)
throw new NullPointerException("64-bit address cannot be null.");
if (address16Bit == null)
throw new NullPointerException("16-bit address cannot be null.");
if (data == null)
throw new NullPointerException("Data cannot be null.");
if (sourceEndpoint < 0 || sourceEndpoint > 0xFF)
throw new IllegalArgumentException("Source endpoint must be between 0 and 0xFF.");
if (destEndpoint < 0 || destEndpoint > 0xFF)
throw new IllegalArgumentException("Destination endpoint must be between 0 and 0xFF.");
if (clusterID < 0 || clusterID > 0xFFFF)
throw new IllegalArgumentException("Cluster ID must be between 0 and 0xFFFF.");
if (profileID < 0 || profileID > 0xFFFF)
throw new IllegalArgumentException("Profile ID must be between 0 and 0xFFFF.");
// Check if device is remote.
if (isRemote())
throw new OperationNotSupportedException("Cannot send explicit data to a remote device from a remote device.");
logger.debug(toString() + "Sending explicit data asynchronously to {}[{}] [{} - {} - {} - {}] >> {}.", address64Bit, address16Bit,
String.format("%02X", sourceEndpoint), String.format("%02X", destEndpoint),
String.format("%04X", clusterID), String.format("%04X", profileID),
HexUtils.prettyHexString(data));
XBeePacket xbeePacket = new ExplicitAddressingPacket(getNextFrameID(), address64Bit, address16Bit, sourceEndpoint, destEndpoint, clusterID, profileID, 0, XBeeTransmitOptions.NONE, data);
sendAndCheckXBeePacket(xbeePacket, true);
}
/**
* Sends asynchronously the provided data in application layer mode to the
* provided XBee device choosing the optimal send method depending on the
* protocol of the local XBee device. Application layer mode means that you
* need to specify the application layer fields to be sent with the data.