├── rxtxSerial.dll ├── librxtxSerial.so ├── librxtxSerial.jnilib ├── docs ├── XBee-ZB-manual.pdf ├── XBee-802.15.4-manual.pdf ├── XBee-ZNet-2.5-manual.pdf ├── api │ ├── resources │ │ └── inherit.gif │ ├── package-list │ ├── stylesheet.css │ ├── index.html │ ├── overview-frame.html │ └── com │ │ └── rapplogic │ │ └── xbee │ │ ├── package-frame.html │ │ ├── util │ │ └── package-frame.html │ │ └── api │ │ ├── wpan │ │ └── package-frame.html │ │ ├── zigbee │ │ └── package-frame.html │ │ └── package-frame.html ├── diagrams │ ├── xbee-explorer.png │ ├── arduino-to-arduino.png │ ├── computer-to-arduino.png │ ├── computer-to-stand-alone.png │ ├── xbee-series2-pin-layout.png │ └── xbee-series2-pin-layout-big.png ├── XBee-Digimesh-90000991_C.pdf └── APP_NOTE_XBee_ZigBee_Device_Profile.pdf ├── .gitignore ├── src └── main │ └── java │ └── com │ └── rapplogic │ └── xbee │ ├── RxTxSerialEventListener.java │ ├── api │ ├── NoRequestResponse.java │ ├── PacketListener.java │ ├── IPacketParser.java │ ├── ResponseFilter.java │ ├── CollectTerminator.java │ ├── XBeePacketHandler.java │ ├── XBeeParseException.java │ ├── XBeeNotConnectedException.java │ ├── XBeeTimeoutException.java │ ├── XBeeAddress.java │ ├── AtCommandQueue.java │ ├── XBeeFrameIdResponse.java │ ├── XBeePacketLength.java │ ├── zigbee │ │ ├── ZBForceSampleRequest.java │ │ ├── ZNetRxResponse.java │ │ ├── AssociationStatus.java │ │ ├── ZNetExplicitRxResponse.java │ │ ├── ZNetRxBaseResponse.java │ │ └── ZBNodeDiscover.java │ ├── GenericResponse.java │ ├── wpan │ │ ├── RxResponse64.java │ │ ├── RxResponse16.java │ │ ├── RxResponse.java │ │ ├── RxBaseResponse.java │ │ ├── TxRequestBase.java │ │ ├── TxStatusResponse.java │ │ ├── WpanNodeDiscover.java │ │ ├── TxRequest16.java │ │ └── TxRequest64.java │ ├── XBeeException.java │ ├── ErrorResponse.java │ ├── IXBee.java │ ├── HardwareVersion.java │ ├── XBeeRequest.java │ ├── Checksum.java │ ├── XBeeAddress16.java │ ├── XBeeConfiguration.java │ ├── RemoteAtResponse.java │ ├── ModemStatusResponse.java │ ├── ApiId.java │ ├── XBeeAddress64.java │ ├── AtCommandResponse.java │ └── AtCommand.java │ ├── socket │ ├── ServerNotAvailableException.java │ ├── SocketXBeeExample.java │ └── SocketXBeeConnection.java │ ├── util │ ├── InputStreamWrapper.java │ ├── IIntArray.java │ ├── IIntInputStream.java │ ├── ExceptionHandler.java │ ├── IntArrayInputStream.java │ ├── IntArrayOutputStream.java │ └── DoubleByte.java │ ├── examples │ ├── ResponseHandlerExample.java │ ├── wpan │ │ ├── Processing.java │ │ ├── ApiReceiverExample.java │ │ ├── WpanNodeDiscoverExample.java │ │ ├── ApiSenderExample.java │ │ └── IoSamplesExample.java │ ├── zigbee │ │ ├── BroadcastReceiverExample.java │ │ ├── SleepTestCoordinator.java │ │ ├── ZNetExplicitReceiverExample.java │ │ ├── BroadcastSenderExample.java │ │ ├── ZNetIoSampleExample.java │ │ ├── ZBNodeDiscoverExample.java │ │ ├── ZBForceSampleExample.java │ │ ├── ZNetExplicitSenderExample.java │ │ └── ZNetReceiverExample.java │ ├── ApiAtExample.java │ └── RemoteAtExample.java │ ├── XBeeConnection.java │ ├── AbstractXBeeConnection.java │ └── test │ └── OpenCloseConnectionsTest.java ├── log4j.properties └── pom.xml /rxtxSerial.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/rxtxSerial.dll -------------------------------------------------------------------------------- /librxtxSerial.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/librxtxSerial.so -------------------------------------------------------------------------------- /librxtxSerial.jnilib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/librxtxSerial.jnilib -------------------------------------------------------------------------------- /docs/XBee-ZB-manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/XBee-ZB-manual.pdf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/*.iml 2 | **/*.idea 3 | **/project/project 4 | **/project/target 5 | **/target 6 | **/.DS_Store 7 | -------------------------------------------------------------------------------- /docs/XBee-802.15.4-manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/XBee-802.15.4-manual.pdf -------------------------------------------------------------------------------- /docs/XBee-ZNet-2.5-manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/XBee-ZNet-2.5-manual.pdf -------------------------------------------------------------------------------- /docs/api/resources/inherit.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/api/resources/inherit.gif -------------------------------------------------------------------------------- /docs/diagrams/xbee-explorer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/diagrams/xbee-explorer.png -------------------------------------------------------------------------------- /docs/XBee-Digimesh-90000991_C.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/XBee-Digimesh-90000991_C.pdf -------------------------------------------------------------------------------- /docs/diagrams/arduino-to-arduino.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/diagrams/arduino-to-arduino.png -------------------------------------------------------------------------------- /docs/diagrams/computer-to-arduino.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/diagrams/computer-to-arduino.png -------------------------------------------------------------------------------- /docs/diagrams/computer-to-stand-alone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/diagrams/computer-to-stand-alone.png -------------------------------------------------------------------------------- /docs/diagrams/xbee-series2-pin-layout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/diagrams/xbee-series2-pin-layout.png -------------------------------------------------------------------------------- /docs/APP_NOTE_XBee_ZigBee_Device_Profile.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/APP_NOTE_XBee_ZigBee_Device_Profile.pdf -------------------------------------------------------------------------------- /docs/diagrams/xbee-series2-pin-layout-big.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrewrapp/xbee-api/HEAD/docs/diagrams/xbee-series2-pin-layout-big.png -------------------------------------------------------------------------------- /docs/api/package-list: -------------------------------------------------------------------------------- 1 | com.rapplogic.xbee 2 | com.rapplogic.xbee.api 3 | com.rapplogic.xbee.api.wpan 4 | com.rapplogic.xbee.api.zigbee 5 | com.rapplogic.xbee.util 6 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/RxTxSerialEventListener.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee; 2 | 3 | import gnu.io.SerialPortEvent; 4 | 5 | public interface RxTxSerialEventListener { 6 | public void handleSerialEvent(SerialPortEvent event); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/NoRequestResponse.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.api; 2 | 3 | /** 4 | * This filter is used to capture only RX packets that are not a response to a TX packet. 5 | * This includes I/O samples, RX packets, modem status etc. 6 | * 7 | * @author andrew 8 | * 9 | */ 10 | public interface NoRequestResponse { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/socket/ServerNotAvailableException.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.socket; 2 | 3 | public class ServerNotAvailableException extends Exception { 4 | 5 | public ServerNotAvailableException(String message, Throwable cause) { 6 | super(message, cause); 7 | } 8 | 9 | public ServerNotAvailableException(String message) { 10 | super(message); 11 | } 12 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/PacketListener.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.api; 2 | 3 | /** 4 | * Add an implementation of this interface to xbee.addPacketListener to get notifications 5 | * of new packets 6 | *

7 | * @author andrew 8 | */ 9 | public interface PacketListener { 10 | public void processResponse(XBeeResponse response); 11 | // public void handleError(XBeeResponse error); 12 | // public void connectionClosed(); 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/util/InputStreamWrapper.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.util; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | public class InputStreamWrapper implements IIntInputStream { 7 | 8 | private InputStream in; 9 | 10 | public InputStreamWrapper(InputStream in) { 11 | this.in = in; 12 | } 13 | 14 | public int read() throws IOException { 15 | return in.read(); 16 | } 17 | 18 | public int read(String s) throws IOException { 19 | return in.read(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/IPacketParser.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.api; 2 | 3 | import java.io.IOException; 4 | 5 | public interface IPacketParser { 6 | int read(String context) throws IOException; 7 | int[] readRemainingBytes() throws IOException; 8 | int getFrameDataBytesRead(); 9 | int getRemainingBytes(); 10 | int getBytesRead(); 11 | XBeePacketLength getLength(); 12 | ApiId getApiId(); 13 | int getIntApiId(); 14 | // TODO move to util 15 | XBeeAddress16 parseAddress16() throws IOException; 16 | XBeeAddress64 parseAddress64() throws IOException; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/ResponseHandlerExample.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.examples; 2 | 3 | import java.io.IOException; 4 | 5 | import com.rapplogic.xbee.api.IPacketParser; 6 | import com.rapplogic.xbee.api.XBee; 7 | import com.rapplogic.xbee.api.XBeeResponse; 8 | 9 | public class ResponseHandlerExample { 10 | 11 | public ResponseHandlerExample() { 12 | XBee xbee = new XBee(); 13 | XBee.registerResponseHandler(0x88, MyResponse.class); 14 | // xbee.open(..); 15 | } 16 | 17 | public static class MyResponse extends XBeeResponse { 18 | 19 | @Override 20 | protected void parse(IPacketParser parser) throws IOException { 21 | // this.setxxx(parser.read("AT Response Frame Id")); 22 | // this.setxxy(parser.read("AT Response Char 1")); 23 | // this.setxxz(parser.read("AT Response Char 2")); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/XBeeConnection.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | 7 | /** 8 | * Represents a protocol independent connection to a XBee radio (e.g. could be a serial connection, socket, xmpp etc.) 9 | * Important: The implementation must call this.notify() when new data is available; otherwise the XBee input stream thread 10 | * will go into a forever wait. 11 | *

12 | * You must implement read(int) and available() on the InputStream and 13 | *

14 | * pipeToInputStream(int) and flush() on the OutputStream 15 | *

16 | * It's recommended to implement close 17 | * 18 | * @author andrew 19 | * 20 | */ 21 | public interface XBeeConnection { 22 | public OutputStream getOutputStream(); 23 | public InputStream getInputStream(); 24 | public void close() throws IOException; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/util/IIntArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.util; 21 | 22 | public interface IIntArray { 23 | public int[] getIntArray(); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/ResponseFilter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | public interface ResponseFilter { 23 | public boolean accept(XBeeResponse response); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/CollectTerminator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | public interface CollectTerminator { 23 | public boolean stop(XBeeResponse response); 24 | } 25 | -------------------------------------------------------------------------------- /log4j.properties: -------------------------------------------------------------------------------- 1 | #log4j.rootCategory=DEBUG, main-appender 2 | # default log setting 3 | log4j.logger.com.rapplogic.xbee=DEBUG, main-appender, console-appender 4 | # override default log setting to log examples at the DEBUG level 5 | log4j.logger.com.rapplogic.xbee.examples=DEBUG 6 | 7 | # defines the XBee.log 8 | log4j.appender.main-appender=org.apache.log4j.RollingFileAppender 9 | log4j.appender.main-appender.File=XBee.log 10 | log4j.appender.main-appender.Append=true 11 | log4j.appender.main-appender.MaxFileSize=10MB 12 | log4j.appender.main-appender.MaxBackupIndex=100 13 | log4j.appender.main-appender.ImmediateFlush=true 14 | log4j.appender.main-appender.layout=org.apache.log4j.PatternLayout 15 | log4j.appender.main-appender.layout.ConversionPattern=[%d] [%t] [%p] [%c] %m %n 16 | 17 | # Console log 18 | log4j.appender.console-appender=org.apache.log4j.ConsoleAppender 19 | log4j.appender.console-appender.layout=org.apache.log4j.PatternLayout 20 | log4j.appender.console-appender.layout.ConversionPattern=[%d] [%t] [%p] [%c] %m %n -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeePacketHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | public interface XBeePacketHandler { 23 | public void handlePacket(XBeeResponse response); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/util/IIntInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.util; 21 | 22 | import java.io.IOException; 23 | 24 | // TODO rename to IIntInputStream 25 | public interface IIntInputStream { 26 | public int read() throws IOException; 27 | public int read(String s) throws IOException; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeParseException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | public class XBeeParseException extends RuntimeException { 23 | 24 | private static final long serialVersionUID = 6752060371295132748L; 25 | 26 | public XBeeParseException(String s) { 27 | super(s); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeNotConnectedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | /** 23 | * Indicates there is not connection to the radio 24 | *

25 | * @author andrew 26 | * 27 | */ 28 | public class XBeeNotConnectedException extends RuntimeException { 29 | 30 | public XBeeNotConnectedException() { 31 | super(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/util/ExceptionHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.util; 21 | 22 | import com.rapplogic.xbee.api.XBeeException; 23 | 24 | public class ExceptionHandler { 25 | 26 | public static void handleAndThrow(Exception e) throws XBeeException { 27 | if (e instanceof XBeeException) { 28 | throw (XBeeException) e; 29 | } else { 30 | throw new XBeeException(e); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeTimeoutException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | /** 23 | * Indicates an operation did not succeed within the alloted time 24 | *

25 | * @author andrew 26 | * 27 | */ 28 | public class XBeeTimeoutException extends XBeeException { 29 | 30 | private static final long serialVersionUID = 1045336675379821890L; 31 | 32 | public XBeeTimeoutException() { 33 | super(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeAddress.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import com.rapplogic.xbee.util.ByteUtils; 23 | 24 | /** 25 | * Represents a XBee Address. 26 | *

27 | * @author andrew 28 | * 29 | */ 30 | public abstract class XBeeAddress { 31 | 32 | 33 | public XBeeAddress() { 34 | 35 | } 36 | 37 | public abstract int[] getAddress(); 38 | 39 | public String toString() { 40 | return ByteUtils.toBase16(this.getAddress()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/AtCommandQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | /** 23 | * AT Command Queue 24 | *

25 | * API ID: 0x9 26 | *

27 | * @author andrew 28 | * 29 | */ 30 | public class AtCommandQueue extends AtCommand { 31 | //TODO test 32 | public AtCommandQueue(String command) { 33 | this(command, null, DEFAULT_FRAME_ID); 34 | } 35 | 36 | public AtCommandQueue(String command, int[] value, int frameId) { 37 | super(command, value, frameId); 38 | } 39 | 40 | public ApiId getApiId() { 41 | return ApiId.AT_COMMAND_QUEUE; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /docs/api/stylesheet.css: -------------------------------------------------------------------------------- 1 | /* Javadoc style sheet */ 2 | 3 | /* Define colors, fonts and other style attributes here to override the defaults */ 4 | 5 | /* Page background color */ 6 | body { background-color: #FFFFFF; color:#000000 } 7 | 8 | /* Headings */ 9 | h1 { font-size: 145% } 10 | 11 | /* Table colors */ 12 | .TableHeadingColor { background: #CCCCFF; color:#000000 } /* Dark mauve */ 13 | .TableSubHeadingColor { background: #EEEEFF; color:#000000 } /* Light mauve */ 14 | .TableRowColor { background: #FFFFFF; color:#000000 } /* White */ 15 | 16 | /* Font used in left-hand frame lists */ 17 | .FrameTitleFont { font-size: 100%; font-family: Helvetica, Arial, sans-serif; color:#000000 } 18 | .FrameHeadingFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif; color:#000000 } 19 | .FrameItemFont { font-size: 90%; font-family: Helvetica, Arial, sans-serif; color:#000000 } 20 | 21 | /* Navigation bar fonts and colors */ 22 | .NavBarCell1 { background-color:#EEEEFF; color:#000000} /* Light mauve */ 23 | .NavBarCell1Rev { background-color:#00008B; color:#FFFFFF} /* Dark Blue */ 24 | .NavBarFont1 { font-family: Arial, Helvetica, sans-serif; color:#000000;color:#000000;} 25 | .NavBarFont1Rev { font-family: Arial, Helvetica, sans-serif; color:#FFFFFF;color:#FFFFFF;} 26 | 27 | .NavBarCell2 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF; color:#000000} 28 | .NavBarCell3 { font-family: Arial, Helvetica, sans-serif; background-color:#FFFFFF; color:#000000} 29 | 30 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeFrameIdResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import com.rapplogic.xbee.util.ByteUtils; 23 | 24 | /** 25 | * Represents all XBee responses that contain a frame id 26 | *

27 | * @author andrew 28 | * 29 | */ 30 | public abstract class XBeeFrameIdResponse extends XBeeResponse { 31 | 32 | private int frameId; 33 | 34 | public int getFrameId() { 35 | return frameId; 36 | } 37 | 38 | public void setFrameId(int frameId) { 39 | this.frameId = frameId; 40 | } 41 | 42 | public String toString() { 43 | return super.toString() + ",frameId=" + ByteUtils.toBase16(this.frameId); 44 | } 45 | } -------------------------------------------------------------------------------- /docs/api/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Generated Documentation (Untitled) 7 | 8 | 19 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | <H2> 30 | Frame Alert</H2> 31 | 32 | <P> 33 | This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. 34 | <BR> 35 | Link to<A HREF="overview-summary.html">Non-frame version.</A> 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeePacketLength.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import com.rapplogic.xbee.util.DoubleByte; 23 | 24 | /** 25 | * Supports a 16-bit XBee packet length 26 | *

27 | * @author andrew 28 | * 29 | */ 30 | public class XBeePacketLength extends DoubleByte { 31 | 32 | /** 33 | * Manual says max packet length is 100 bytes so not sure why 2 bytes are needed 34 | * 35 | * @param msb 36 | * @param lsb 37 | */ 38 | public XBeePacketLength(int msb, int lsb) { 39 | super(msb, lsb); 40 | } 41 | 42 | public XBeePacketLength(int length) { 43 | super(length); 44 | } 45 | 46 | public int getLength() { 47 | return this.get16BitValue(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/socket/SocketXBeeExample.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.socket; 2 | 3 | import com.rapplogic.xbee.XBeeConnection; 4 | import com.rapplogic.xbee.api.*; 5 | import org.apache.log4j.Logger; 6 | import org.apache.log4j.PropertyConfigurator; 7 | 8 | import java.io.IOException; 9 | import java.net.SocketException; 10 | import java.net.UnknownHostException; 11 | 12 | public class SocketXBeeExample { 13 | 14 | private final static Logger log = Logger.getLogger(SocketXBeeExample.class); 15 | 16 | public static void main(String[] args) throws UnknownHostException, XBeeException, IOException { 17 | PropertyConfigurator.configure("log4j.properties"); 18 | 19 | try { 20 | // must disable start checks until bug fixed 21 | XBee xbee = new XBee(new XBeeConfiguration().withStartupChecks(false)); 22 | xbee.initProviderConnection((XBeeConnection)new SocketXBeeConnection("pi", 9000)); 23 | 24 | 25 | for (int i = 0; i < 3; i++) { 26 | log.debug("Sending AI command"); 27 | 28 | try { 29 | AtCommandResponse response = (AtCommandResponse) xbee.sendSynchronous(new AtCommand("AI")); 30 | log.debug("Received AI response " + response); 31 | } catch (XBeeException e) { 32 | if (e.getCause() != null && e.getCause() instanceof SocketException) { 33 | // socket disconnected.. java.net.SocketException: Broken pipe 34 | // reconnect 35 | throw e; 36 | } 37 | } 38 | 39 | Thread.sleep(1000); 40 | } 41 | 42 | log.debug("Done"); 43 | 44 | } catch (Throwable t) { 45 | log.error("xbee socket client failed", t); 46 | } 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /docs/api/overview-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Overview List 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 |
20 |
23 | 24 | 25 | 26 | 42 | 43 |
All Classes 27 |

28 | 29 | Packages 30 |
31 | com.rapplogic.xbee 32 |
33 | com.rapplogic.xbee.api 34 |
35 | com.rapplogic.xbee.api.wpan 36 |
37 | com.rapplogic.xbee.api.zigbee 38 |
39 | com.rapplogic.xbee.util 40 |
41 |

44 | 45 |

46 |   47 | 48 | 49 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/zigbee/ZBForceSampleRequest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.zigbee; 21 | 22 | import com.rapplogic.xbee.api.XBeeAddress16; 23 | import com.rapplogic.xbee.api.XBeeAddress64; 24 | import com.rapplogic.xbee.api.XBeeRequest; 25 | import com.rapplogic.xbee.api.RemoteAtRequest; 26 | 27 | /** 28 | * Uses Remote AT to send a Force Sample (IS) AT command to a remote XBee 29 | * 30 | * @author andrew 31 | * 32 | */ 33 | public class ZBForceSampleRequest extends RemoteAtRequest { 34 | 35 | /** 36 | * Creates a Force Sample Remote AT request 37 | * 38 | * @param dest64 39 | * @param command 40 | */ 41 | public ZBForceSampleRequest(XBeeAddress64 dest64) { 42 | super(XBeeRequest.DEFAULT_FRAME_ID, dest64, XBeeAddress16.ZNET_BROADCAST, false, "IS", null); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/GenericResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.io.IOException; 23 | 24 | /** 25 | * Container for unknown response 26 | *

27 | * @author andrew 28 | * 29 | */ 30 | public class GenericResponse extends XBeeResponse { 31 | 32 | private int genericApiId; 33 | 34 | public GenericResponse() { 35 | 36 | } 37 | 38 | public int getGenericApiId() { 39 | return genericApiId; 40 | } 41 | 42 | public void setGenericApiId(int genericApiId) { 43 | this.genericApiId = genericApiId; 44 | } 45 | 46 | public void parse(IPacketParser parser) throws IOException { 47 | //eat packet bytes -- they will be save to bytearray and stored in response 48 | parser.readRemainingBytes(); 49 | // TODO gotta save it because it isn't know to the enum apiId won't 50 | this.setGenericApiId(parser.getIntApiId()); 51 | } 52 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/RxResponse64.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import java.io.IOException; 23 | 24 | import com.rapplogic.xbee.api.IPacketParser; 25 | import com.rapplogic.xbee.api.XBeeAddress64; 26 | 27 | /** 28 | * Series 1 XBee. 64-bit address Receive packet. 29 | * This packet is received when a remote radio transmits a TxRequest64 30 | * packet to this radio's SH + SL address. 31 | *

32 | * Note: MY address must be set to 0xffff to receive this packet type. 33 | *

34 | * API ID: 0x80 35 | * 36 | * @author andrew 37 | */ 38 | public class RxResponse64 extends RxResponse { 39 | 40 | public XBeeAddress64 getRemoteAddress() { 41 | return (XBeeAddress64) this.getSourceAddress(); 42 | } 43 | 44 | public void parse(IPacketParser parser) throws IOException { 45 | this.setSourceAddress(parser.parseAddress64()); 46 | super.parseBase(parser); 47 | super.parse(parser); 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | /** 23 | * I usually detest checked exceptions but given this is a public api, it is reasonable to 24 | * notify users what they can expect. 25 | * 26 | * @author andrew 27 | * 28 | */ 29 | public class XBeeException extends Exception { 30 | 31 | private static final long serialVersionUID = -5501299728920565639L; 32 | private Exception cause; 33 | 34 | public XBeeException(String message) { 35 | super(message); 36 | } 37 | 38 | public XBeeException(String message, Exception e) { 39 | super(message, e); 40 | } 41 | 42 | public XBeeException() { 43 | super(); 44 | } 45 | 46 | public XBeeException(Exception cause) { 47 | super(); 48 | this.setCause(cause); 49 | } 50 | 51 | public Exception getCause() { 52 | return cause; 53 | } 54 | 55 | public void setCause(Exception cause) { 56 | this.cause = cause; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/AbstractXBeeConnection.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.io.OutputStream; 6 | import java.io.PipedInputStream; 7 | import java.io.PipedOutputStream; 8 | 9 | import org.apache.log4j.Logger; 10 | 11 | /** 12 | * This is a bit of a misnomer as we are not connected to anything. This class serves as a buffer 13 | * and provides protocol independent input/output streams for xbee-api 14 | * 15 | * @author andrew 16 | * 17 | */ 18 | public abstract class AbstractXBeeConnection implements XBeeConnection { 19 | 20 | private final static Logger log = Logger.getLogger(XBeeConnection.class); 21 | 22 | // we write to this to provide xbee-api with RX packets 23 | private PipedOutputStream pipeToInputStream = new PipedOutputStream(); 24 | // xbee api reads from this to parse packets 25 | private PipedInputStream xbeeInputStream = new PipedInputStream(); 26 | 27 | public void init() { 28 | 29 | } 30 | 31 | public AbstractXBeeConnection() { 32 | try { 33 | xbeeInputStream.connect(pipeToInputStream); 34 | } catch (IOException e) { 35 | // won't happen 36 | } 37 | } 38 | 39 | //Writes the byte to the input stream to be parsed 40 | public void pipeToInputStream(int b) throws IOException { 41 | pipeToInputStream.write(b); 42 | 43 | // TODO ideally we don't notify until end of packet but this should be fine, it will just block on read 44 | 45 | // critical: notify XBee that data is available 46 | synchronized(this) { 47 | log.debug("Notifying the XBee input stream thread that new data is available"); 48 | this.notify(); 49 | } 50 | } 51 | 52 | public abstract OutputStream getOutputStream(); 53 | 54 | public InputStream getInputStream() { 55 | return this.xbeeInputStream; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/RxResponse16.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import java.io.IOException; 23 | 24 | import com.rapplogic.xbee.api.ApiId; 25 | import com.rapplogic.xbee.api.IPacketParser; 26 | import com.rapplogic.xbee.api.NoRequestResponse; 27 | import com.rapplogic.xbee.api.XBeeAddress16; 28 | 29 | /** 30 | * Series 1 XBee. 16-bit address Receive packet. 31 | * This packet is received when a remote radio transmits a TxRequest16 32 | * packet to this radio's MY address 33 | *

34 | * API ID: 0x81 35 | * 36 | * @author andrew 37 | * 38 | */ 39 | public class RxResponse16 extends RxResponse implements NoRequestResponse { 40 | 41 | public XBeeAddress16 getRemoteAddress() { 42 | return (XBeeAddress16) this.getSourceAddress(); 43 | } 44 | 45 | public void parse(IPacketParser parser) throws IOException { 46 | this.setSourceAddress(parser.parseAddress16()); 47 | super.parseBase(parser); 48 | super.parse(parser); 49 | } 50 | } -------------------------------------------------------------------------------- /docs/api/com/rapplogic/xbee/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.rapplogic.xbee 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | com.rapplogic.xbee 18 | 19 | 20 | 27 | 28 |
21 | Interfaces  22 | 23 |
24 | RxTxSerialEventListener 25 |
26 | XBeeConnection
29 | 30 | 31 | 32 | 33 | 40 | 41 |
34 | Classes  35 | 36 |
37 | RxTxSerialComm 38 |
39 | XBeePin
42 | 43 | 44 | 45 | 46 | 51 | 52 |
47 | Enums  48 | 49 |
50 | XBeePin.Capability
53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/util/IntArrayInputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.util; 21 | 22 | /** 23 | * //TODO replace with nio.IntBuffer 24 | */ 25 | public class IntArrayInputStream implements IIntInputStream { 26 | 27 | private int[] source; 28 | private int pos; 29 | 30 | public IntArrayInputStream(int[] source) { 31 | this.source = source; 32 | } 33 | 34 | public int read() { 35 | if (pos >= source.length) { 36 | throw new IllegalStateException("end of input stream"); 37 | } 38 | 39 | return source[pos++]; 40 | } 41 | 42 | /** 43 | * Reads size bytes from the input stream and returns the bytes in an array 44 | * 45 | * @param size 46 | * @return 47 | * Apr 13, 2009 48 | */ 49 | public int[] read(int size) { 50 | int[] block = new int[size]; 51 | System.arraycopy(source, pos, block, 0, size); 52 | // index pos 53 | pos+=size; 54 | return block; 55 | } 56 | 57 | public int read(String s) { 58 | return read(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/util/IntArrayOutputStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.util; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * TODO replace with nio.IntBuffer 27 | */ 28 | public class IntArrayOutputStream implements IIntArray { 29 | 30 | private List intList = new ArrayList(); 31 | 32 | public IntArrayOutputStream() { 33 | 34 | } 35 | 36 | public void write (int val) { 37 | intList.add(val); 38 | } 39 | 40 | public void write(int[] val) { 41 | for (int aVal : val) { 42 | this.write(aVal); 43 | } 44 | } 45 | 46 | public int[] getIntArray() { 47 | //int[] integer = (int[]) intList.toArray(new int[0]); 48 | // TODO there has got to be a better way -- how to convert list to int[] array? 49 | int[] intArr = new int[intList.size()]; 50 | 51 | int i = 0; 52 | 53 | for (Integer integer : intList) { 54 | intArr[i++] = integer; 55 | } 56 | 57 | return intArr; 58 | } 59 | 60 | public List getInternalList() { 61 | return intList; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/RxResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import java.io.IOException; 23 | 24 | import com.rapplogic.xbee.api.IPacketParser; 25 | import com.rapplogic.xbee.util.ByteUtils; 26 | 27 | public class RxResponse extends RxBaseResponse { 28 | 29 | private int[] data; 30 | 31 | public RxResponse() { 32 | 33 | } 34 | 35 | public int[] getData() { 36 | return data; 37 | } 38 | 39 | public void setData(int[] data) { 40 | this.data = data; 41 | } 42 | 43 | public void parse(IPacketParser parser) throws IOException { 44 | int[] payload = new int[parser.getLength().getLength() - parser.getFrameDataBytesRead()]; 45 | 46 | int bytesRead = parser.getFrameDataBytesRead(); 47 | 48 | for (int i = 0; i < parser.getLength().getLength() - bytesRead; i++) { 49 | payload[i] = parser.read("Payload byte " + i); 50 | //log.debug("rx data payload [" + i + "] " + payload[i]); 51 | } 52 | 53 | this.setData(payload); 54 | } 55 | 56 | public String toString() { 57 | return super.toString() + 58 | ",data=" + ByteUtils.toBase16(this.data); 59 | } 60 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/zigbee/ZNetRxResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.zigbee; 21 | 22 | import java.io.IOException; 23 | 24 | import com.rapplogic.xbee.api.IPacketParser; 25 | import com.rapplogic.xbee.api.NoRequestResponse; 26 | import com.rapplogic.xbee.util.ByteUtils; 27 | 28 | /** 29 | * Series 2 XBee. This packet is received when 30 | * a remote XBee sends a ZNetTxRequest 31 | *

32 | * API ID: 0x90 33 | *

34 | * @author andrew 35 | * 36 | */ 37 | public class ZNetRxResponse extends ZNetRxBaseResponse implements NoRequestResponse { 38 | 39 | private int[] data; 40 | 41 | public ZNetRxResponse() { 42 | super(); 43 | } 44 | 45 | public int[] getData() { 46 | return data; 47 | } 48 | 49 | public void setData(int[] data) { 50 | this.data = data; 51 | } 52 | 53 | public void parse(IPacketParser parser) throws IOException { 54 | this.parseAddress(parser); 55 | this.parseOption(parser); 56 | this.setData(parser.readRemainingBytes()); 57 | } 58 | 59 | public String toString() { 60 | return super.toString() + 61 | ",data=" + ByteUtils.toBase16(this.data); 62 | } 63 | } -------------------------------------------------------------------------------- /docs/api/com/rapplogic/xbee/util/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.rapplogic.xbee.util 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | com.rapplogic.xbee.util 18 | 19 | 20 | 27 | 28 |
21 | Interfaces  22 | 23 |
24 | IIntArray 25 |
26 | IIntArrayInputStream
29 | 30 | 31 | 32 | 33 | 48 | 49 |
34 | Classes  35 | 36 |
37 | ByteUtils 38 |
39 | DoubleByte 40 |
41 | ExceptionHandler 42 |
43 | InputStreamWrapper 44 |
45 | IntArrayInputStream 46 |
47 | IntArrayOutputStream
50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/wpan/Processing.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.examples.wpan; 2 | 3 | import java.util.Queue; 4 | import java.util.concurrent.ConcurrentLinkedQueue; 5 | 6 | import org.apache.log4j.PropertyConfigurator; 7 | 8 | import com.rapplogic.xbee.api.PacketListener; 9 | import com.rapplogic.xbee.api.XBee; 10 | import com.rapplogic.xbee.api.XBeeResponse; 11 | import com.rapplogic.xbee.api.wpan.RxResponseIoSample; 12 | 13 | public class Processing { 14 | XBee xbee; 15 | Queue queue = new ConcurrentLinkedQueue(); 16 | boolean message; 17 | XBeeResponse response; 18 | 19 | void setup() { 20 | try { 21 | //optional. set up logging 22 | PropertyConfigurator.configure(dataPath("")+"log4j.properties"); 23 | 24 | xbee = new XBee(); 25 | // replace with your COM port 26 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 27 | 28 | xbee.addPacketListener(new PacketListener() { 29 | public void processResponse(XBeeResponse response) { 30 | queue.offer(response); 31 | } 32 | } 33 | ); 34 | } 35 | catch (Exception e) { 36 | System.out.println("XBee failed to initialize"); 37 | e.printStackTrace(); 38 | System.exit(1); 39 | } 40 | } 41 | 42 | void draw() { 43 | try { 44 | readPackets(); 45 | } 46 | catch (Exception e) { 47 | e.printStackTrace(); 48 | } 49 | } 50 | 51 | void readPackets() throws Exception { 52 | 53 | while ((response = queue.poll()) != null) { 54 | // we got something! 55 | try { 56 | RxResponseIoSample ioSample = (RxResponseIoSample) response; 57 | 58 | println("We received a sample from " + ioSample.getSourceAddress()); 59 | 60 | if (ioSample.containsAnalog()) { 61 | println("10-bit temp reading (pin 19) is " + 62 | ioSample.getSamples()[0].getAnalog1()); 63 | } 64 | } 65 | catch (ClassCastException e) { 66 | // not an IO Sample 67 | } 68 | } 69 | } 70 | 71 | void println(String s) {}; 72 | String dataPath(String s) {return null;}; 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/ErrorResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | /** 23 | * Represents a Java error during packet parsing. 24 | * This is the only class that extends XBeeResponse and does not map 25 | * to a XBee API ID 26 | *

27 | * @author andrew 28 | * 29 | */ 30 | public class ErrorResponse extends XBeeResponse { 31 | 32 | private String errorMsg; 33 | private Exception exception; 34 | 35 | public ErrorResponse() { 36 | super(); 37 | this.setApiId(ApiId.ERROR_RESPONSE); 38 | this.setError(true); 39 | } 40 | 41 | /** 42 | * A bit redundant in that it is the same as getException.getMessage() 43 | * 44 | * @return 45 | */ 46 | public String getErrorMsg() { 47 | return errorMsg; 48 | } 49 | 50 | public void setErrorMsg(String errorMsg) { 51 | this.errorMsg = errorMsg; 52 | } 53 | 54 | public Exception getException() { 55 | return exception; 56 | } 57 | 58 | public void setException(Exception exception) { 59 | this.exception = exception; 60 | } 61 | 62 | public void parse(IPacketParser parser) { 63 | // nothing to do 64 | } 65 | 66 | public String toString() { 67 | return super.toString() + ",errorMsg=" + this.errorMsg + ",exception=" + this.exception; 68 | } 69 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/BroadcastReceiverExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.XBee; 26 | import com.rapplogic.xbee.api.XBeeException; 27 | import com.rapplogic.xbee.api.XBeeResponse; 28 | 29 | /** 30 | * @author andrew 31 | */ 32 | public class BroadcastReceiverExample { 33 | 34 | private final static Logger log = Logger.getLogger(BroadcastReceiverExample.class); 35 | 36 | private BroadcastReceiverExample() throws XBeeException { 37 | 38 | XBee xbee = new XBee(); 39 | 40 | try { 41 | // replace with your com port and baud rate. this is the com port of my coordinator 42 | xbee.open("/dev/tty.usbserial-A6005uPi", 9600); 43 | 44 | while (true) { 45 | XBeeResponse response = xbee.getResponse(); 46 | log.info("received response " + response); 47 | } 48 | } finally { 49 | if (xbee != null && xbee.isConnected()) { 50 | xbee.close(); 51 | } 52 | } 53 | } 54 | 55 | public static void main(String[] args) throws XBeeException, InterruptedException { 56 | PropertyConfigurator.configure("log4j.properties"); 57 | new BroadcastReceiverExample(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/IXBee.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.io.IOException; 23 | import java.util.List; 24 | 25 | /** 26 | * XBee interface 27 | * 28 | * @author andrew 29 | * 30 | */ 31 | public interface IXBee { 32 | public void open(String port, int baudRate) throws XBeeException; 33 | 34 | public void addPacketListener(PacketListener packetListener); 35 | 36 | public void removePacketListener(PacketListener packetListener); 37 | 38 | public void sendPacket(XBeePacket packet) throws IOException; 39 | 40 | public void sendPacket(int[] packet) throws IOException; 41 | 42 | public void sendAsynchronous(XBeeRequest xbeeRequest) throws XBeeException; 43 | 44 | public XBeeResponse sendSynchronous(final XBeeRequest xbeeRequest, int timeout) throws XBeeTimeoutException, XBeeException; 45 | 46 | public XBeeResponse getResponse() throws XBeeException; 47 | 48 | public XBeeResponse getResponse(int timeout) throws XBeeException, XBeeTimeoutException; 49 | 50 | public void close(); 51 | 52 | public int getCurrentFrameId(); 53 | 54 | public int getNextFrameId(); 55 | 56 | public void updateFrameId(int val); 57 | 58 | public boolean isConnected(); 59 | 60 | public void clearResponseQueue(); 61 | 62 | public List collectResponses(int wait, CollectTerminator terminator) throws XBeeException; 63 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/HardwareVersion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | 23 | /** 24 | * Represents a XBee Address. 25 | *

26 | * @author andrew 27 | * 28 | */ 29 | public class HardwareVersion { 30 | 31 | public enum RadioType { 32 | SERIES1("Series 1"), 33 | SERIES1_PRO("Series 1 Pro"), 34 | SERIES2("Series 2"), 35 | SERIES2_PRO("Series 2 Pro"), 36 | SERIES2B_PRO("Series 2B Pro"), 37 | UNKNOWN("Unknown"); 38 | 39 | private String name; 40 | 41 | RadioType(String name) { 42 | this.name = name; 43 | } 44 | 45 | public String toString() { 46 | return name; 47 | } 48 | } 49 | 50 | public static RadioType parse(AtCommandResponse response) throws XBeeException { 51 | 52 | if (!response.getCommand().equals("HV")) { 53 | throw new IllegalArgumentException("This is only applicable to the HV command"); 54 | } 55 | 56 | if (!response.isOk()) { 57 | throw new XBeeException("Attempt to query HV parameter failed"); 58 | } 59 | 60 | switch (response.getValue()[0]) { 61 | case 0x17: 62 | return RadioType.SERIES1; 63 | case 0x18: 64 | return RadioType.SERIES1_PRO; 65 | case 0x19: 66 | return RadioType.SERIES2; 67 | case 0x1a: 68 | return RadioType.SERIES2_PRO; 69 | case 0x1e: 70 | return RadioType.SERIES2B_PRO; 71 | } 72 | 73 | return RadioType.UNKNOWN; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/SleepTestCoordinator.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.examples.zigbee; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.apache.log4j.PropertyConfigurator; 5 | 6 | import com.rapplogic.xbee.api.RemoteAtRequest; 7 | import com.rapplogic.xbee.api.RemoteAtResponse; 8 | import com.rapplogic.xbee.api.XBee; 9 | import com.rapplogic.xbee.api.XBeeAddress64; 10 | import com.rapplogic.xbee.api.XBeeConfiguration; 11 | import com.rapplogic.xbee.api.XBeeException; 12 | import com.rapplogic.xbee.api.XBeeTimeoutException; 13 | 14 | /** 15 | * The premise of this example is we have a end device configured in cyclic sleep. We'll send a command 16 | * to the coordinator to turn on/off a pin. Periodically when the end device wakes up, it will poll the 17 | * coordinator and receive packets sent while it was sleeping 18 | * 19 | * @author andrew 20 | * 21 | */ 22 | public class SleepTestCoordinator { 23 | 24 | private final static Logger log = Logger.getLogger(ZNetSenderExample.class); 25 | 26 | public SleepTestCoordinator(String args[]) throws XBeeTimeoutException, XBeeException, InterruptedException { 27 | 28 | PropertyConfigurator.configure("log4j.properties"); 29 | 30 | XBee xbee = new XBee(new XBeeConfiguration().withStartupChecks(false)); 31 | 32 | //coord 33 | xbee.open("/dev/tty.usbserial-A6005uRz", 9600); 34 | 35 | // replace with end device's 64-bit address (SH + SL) 36 | // router (firmware 23A7) 37 | XBeeAddress64 addr64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x0a, 0x3e, 0x02); 38 | 39 | if (args.length == 1 && (args[0].equals("on") || args[0].equals("off"))) { 40 | 41 | log.info("Turning D0 " + args[0]); 42 | 43 | RemoteAtRequest request = new RemoteAtRequest(addr64, "D0", new int[] {args[0].equals("on") ? 5 : 4}); 44 | 45 | RemoteAtResponse response = (RemoteAtResponse) xbee.sendSynchronous(request, 15000); 46 | 47 | if (response.isOk()) { 48 | log.info("Successfully turned " + args[0] + " pin 20 (D0)"); 49 | } else { 50 | log.warn("Failed to turn " + args[0] + " pin 20. status is " + response.getStatus()); 51 | } 52 | 53 | Thread.sleep(2000); 54 | 55 | if (xbee != null && xbee.isConnected()) { 56 | xbee.close(); 57 | } 58 | } else { 59 | System.err.println("arg should be on or off"); 60 | } 61 | } 62 | 63 | public static void main(String args[]) throws XBeeTimeoutException, XBeeException, InterruptedException { 64 | new SleepTestCoordinator(args); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeRequest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.io.Serializable; 23 | 24 | 25 | /** 26 | * The super class of all XBee transmit packets. 27 | * Constructs frame data portion of an XBee packet 28 | *

29 | * @author andrew 30 | * 31 | */ 32 | 33 | public abstract class XBeeRequest implements Serializable { 34 | 35 | private static final long serialVersionUID = -9181542059678009341L; 36 | 37 | public static final int DEFAULT_FRAME_ID = 1; 38 | // XBee will not generate a TX Status Packet if this frame id sent 39 | public static final int NO_RESPONSE_FRAME_ID = 0; 40 | 41 | private ApiId apiId; 42 | private int frameId; 43 | 44 | public XBeeRequest() { 45 | 46 | } 47 | 48 | // TODO create XBeePacket(XBeeRequest) constructor and move operation there 49 | public XBeePacket getXBeePacket() { 50 | int[] frameData = this.getFrameData(); 51 | 52 | if (frameData == null) { 53 | throw new RuntimeException("frame data is null"); 54 | } 55 | 56 | // TODO xbee packet should handle api/frame id 57 | XBeePacket packet = new XBeePacket(frameData); 58 | 59 | return packet; 60 | } 61 | 62 | public abstract int[] getFrameData(); 63 | 64 | public ApiId getApiId() { 65 | return apiId; 66 | } 67 | 68 | public int getFrameId() { 69 | return frameId; 70 | } 71 | 72 | public String toString() { 73 | return "apiId=" + this.getApiId() + ",frameId=" + this.getFrameId(); 74 | } 75 | 76 | public void setApiId(ApiId apiId) { 77 | this.apiId = apiId; 78 | } 79 | 80 | public void setFrameId(int frameId) { 81 | this.frameId = frameId; 82 | } 83 | 84 | // TODO clear method to reuse request 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/test/OpenCloseConnectionsTest.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.test; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.apache.log4j.PropertyConfigurator; 5 | 6 | import com.rapplogic.xbee.api.AtCommand; 7 | import com.rapplogic.xbee.api.XBee; 8 | import com.rapplogic.xbee.api.XBeeException; 9 | 10 | /** 11 | * Tests opening and closing connections to the radio 12 | * 13 | * @author andrew 14 | * 15 | */ 16 | public class OpenCloseConnectionsTest { 17 | 18 | private final static Logger log = Logger.getLogger(OpenCloseConnectionsTest.class); 19 | 20 | private XBee xbee = new XBee(); 21 | 22 | public static void main(String[] args) throws XBeeException, InterruptedException { 23 | PropertyConfigurator.configure("log4j.properties"); 24 | new OpenCloseConnectionsTest(); 25 | } 26 | 27 | public OpenCloseConnectionsTest() throws XBeeException, InterruptedException { 28 | 29 | // series 1 30 | String device = "/dev/tty.usbserial-A4004Rim"; 31 | // series 2 32 | // String device = "/dev/tty.usbserial-A6005v5M"; 33 | 34 | log.info("opening connection"); 35 | 36 | // xbee.setStartupChecks(false); 37 | 38 | // first connect directly to end device and configure. then comment out configureXXX methods and connect to coordinator 39 | xbee.open(device, 9600); 40 | 41 | if (!xbee.isConnected()) throw new RuntimeException("Should be connected"); 42 | 43 | try { 44 | log.info("attempting duplicate open"); 45 | xbee.open(device, 9600); 46 | throw new RuntimeException("fail -- already open"); 47 | } catch (Exception e) { 48 | log.debug("Expected", e); 49 | } 50 | 51 | log.info("sending channel command"); 52 | 53 | if (!xbee.sendAtCommand(new AtCommand("CH")).isOk()) throw new RuntimeException("fail"); 54 | log.info("closing connection"); 55 | xbee.close(); 56 | 57 | if (xbee.isConnected()) { 58 | throw new RuntimeException("Should be disconnected"); 59 | } 60 | 61 | try { 62 | log.info("sending at command, but we're disconnected"); 63 | xbee.sendAtCommand(new AtCommand("CH")).isOk(); 64 | throw new RuntimeException("Should be disconnected"); 65 | } catch (Exception e) { 66 | log.debug("Expected", e); 67 | } 68 | 69 | log.info("reconnecting"); 70 | xbee.open(device, 9600); 71 | if (!xbee.sendAtCommand(new AtCommand("CH")).isOk()) throw new RuntimeException("fail"); 72 | 73 | log.info("closing conn"); 74 | xbee.close(); 75 | 76 | try { 77 | log.info("try duplicate close"); 78 | xbee.close(); 79 | throw new RuntimeException("Already closed"); 80 | } catch (Exception e) { 81 | log.debug("Expected", e); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /docs/api/com/rapplogic/xbee/api/wpan/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.rapplogic.xbee.api.wpan 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | com.rapplogic.xbee.api.wpan 18 | 19 | 20 | 45 | 46 |
21 | Classes  22 | 23 |
24 | IoSample 25 |
26 | NodeDiscover 27 |
28 | RxBaseResponse 29 |
30 | RxResponse 31 |
32 | RxResponse16 33 |
34 | RxResponse64 35 |
36 | RxResponseIoSample 37 |
38 | TxRequest16 39 |
40 | TxRequest64 41 |
42 | TxRequestBase 43 |
44 | TxStatusResponse
47 | 48 | 49 | 50 | 51 | 58 | 59 |
52 | Enums  53 | 54 |
55 | TxRequestBase.Option 56 |
57 | TxStatusResponse.Status
60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/ZNetExplicitReceiverExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.ApiId; 26 | import com.rapplogic.xbee.api.XBee; 27 | import com.rapplogic.xbee.api.XBeeResponse; 28 | import com.rapplogic.xbee.api.zigbee.ZNetExplicitRxResponse; 29 | 30 | /** 31 | * Set AO=1 for to enable explicit frames for this example 32 | * 33 | * @author andrew 34 | * 35 | */ 36 | public class ZNetExplicitReceiverExample { 37 | 38 | private final static Logger log = Logger.getLogger(ZNetExplicitReceiverExample.class); 39 | 40 | private ZNetExplicitReceiverExample() throws Exception { 41 | XBee xbee = new XBee(); 42 | 43 | try { 44 | // replace with the com port or your receiving XBee 45 | // this is the com port of my end device on my mac 46 | xbee.open("/dev/tty.usbserial-A6005uRz", 9600); 47 | 48 | while (true) { 49 | 50 | try { 51 | // we wait here until a packet is received. 52 | XBeeResponse response = xbee.getResponse(); 53 | 54 | if (response.getApiId() == ApiId.ZNET_EXPLICIT_RX_RESPONSE) { 55 | ZNetExplicitRxResponse rx = (ZNetExplicitRxResponse) response; 56 | 57 | log.info("received explicit packet response " + response.toString()); 58 | } else { 59 | log.debug("received unexpected packet " + response.toString()); 60 | } 61 | } catch (Exception e) { 62 | log.error(e); 63 | } 64 | } 65 | } finally { 66 | if (xbee != null && xbee.isConnected()) { 67 | xbee.close(); 68 | } 69 | } 70 | } 71 | 72 | public static void main(String[] args) throws Exception { 73 | // init log4j 74 | PropertyConfigurator.configure("log4j.properties"); 75 | new ZNetExplicitReceiverExample(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/zigbee/AssociationStatus.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.zigbee; 21 | 22 | import java.util.EnumSet; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import com.rapplogic.xbee.api.AtCommandResponse; 27 | 28 | // TODO create for wpan 29 | public enum AssociationStatus { 30 | SUCCESS (0, "Successful completion - Coordinator started or Router/End Device found and joined with a parent."), 31 | NO_PAN (0x21, "Scan found no PANs"), 32 | NO_VALID_PAN (0x22, "Scan found no valid PANs based on current SC and ID settings"), 33 | NJ_EXPIRED (0x23, "Valid Coordinator or Routers found, but they are not allowing joining (NJ expired)"), 34 | NJ_FAILED (0x27, "Node Joining attempt failed (typically due to incompatible security settings)"), 35 | COORDINATOR_START_FAILED (0x2a, "Coordinator Start attempt failed"), 36 | SCANNING_FOR_PARENT (0xff, "Scanning for a Parent"), 37 | EXISTING_COORDINATOR_CHECK (0x2b, "Checking for an existing coordinator"); 38 | 39 | private static final Map lookup = new HashMap(); 40 | 41 | static { 42 | for(AssociationStatus s : EnumSet.allOf(AssociationStatus.class)) { 43 | lookup.put(s.getValue(), s); 44 | } 45 | } 46 | 47 | public static AssociationStatus get(int value) { 48 | return lookup.get(value); 49 | } 50 | 51 | public static AssociationStatus get(AtCommandResponse response) { 52 | return AssociationStatus.get(response.getValue()[0]); 53 | } 54 | 55 | private final int value; 56 | private final String description; 57 | 58 | AssociationStatus(int value, String description) { 59 | this.value = value; 60 | this.description = description; 61 | 62 | } 63 | 64 | public int getValue() { 65 | return value; 66 | } 67 | 68 | public String getDescription() { 69 | return description; 70 | } 71 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/Checksum.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import org.apache.log4j.Logger; 23 | 24 | import com.rapplogic.xbee.util.ByteUtils; 25 | 26 | /** 27 | * Computes and verifies packet checksums 28 | *

29 | * @author andrew 30 | * 31 | */ 32 | public class Checksum { 33 | 34 | private final static Logger log = Logger.getLogger(Checksum.class); 35 | 36 | public int checksum = 0; 37 | 38 | /** 39 | * Don't add Checksum byte when computing checksum!! 40 | * 41 | * @param val 42 | */ 43 | public void addByte(int val) { 44 | checksum+= val; 45 | } 46 | 47 | /** 48 | * Computes checksum and stores in checksum instance variable 49 | * 50 | * @return 51 | */ 52 | public void compute() { 53 | // discard values > 1 byte 54 | checksum = 0xff & checksum; 55 | // perform 2s complement 56 | checksum = 0xff - checksum; 57 | 58 | log.debug("computed checksum is " + ByteUtils.formatByte(checksum)); 59 | } 60 | 61 | /** 62 | * First add all relevant bytes, including checksum 63 | * 64 | * @return 65 | */ 66 | public boolean verify() { 67 | checksum = checksum & 0xff; 68 | log.debug("verify checksum is " + checksum); 69 | return 0xff == checksum; 70 | } 71 | 72 | public int getChecksum() { 73 | return checksum; 74 | } 75 | 76 | public static void main(String[] args) { 77 | //83 56 78 24 00 01 02 00 03 ff 85 78 | Checksum ck = new Checksum(); 79 | 80 | ck.addByte(0x83); 81 | ck.addByte(0x56); 82 | ck.addByte(0x78); 83 | ck.addByte(0x26); 84 | ck.addByte(0x00); 85 | ck.addByte(0x01); 86 | ck.addByte(0x02); 87 | ck.addByte(0x00); 88 | ck.addByte(0x03); 89 | ck.addByte(0xff); 90 | // checksum 91 | ck.addByte(0x83); 92 | 93 | // checksum is 0x83 94 | //ck.compute(); 95 | ck.verify(); 96 | 97 | System.out.println("checksum is " + ByteUtils.formatByte(ck.getChecksum())); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/ApiAtExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.AtCommand; 26 | import com.rapplogic.xbee.api.XBee; 27 | import com.rapplogic.xbee.api.XBeeConfiguration; 28 | import com.rapplogic.xbee.api.XBeeException; 29 | 30 | /** 31 | * The AtCommand/AtCommandResponse classes are supported by both ZNet and WPAN XBees but certain 32 | * commands are specific to ZNet or WPAN. 33 | * 34 | * Commands that are ZNet specific are located in the ZNetApiAtTest. 35 | * 36 | * Refer to the manual for more information on available commands 37 | * 38 | * @author andrew 39 | * 40 | */ 41 | public class ApiAtExample { 42 | 43 | // TODO split class in to WPAN class 44 | 45 | private final static Logger log = Logger.getLogger(ApiAtExample.class); 46 | 47 | private XBee xbee = new XBee(new XBeeConfiguration().withStartupChecks(false)); 48 | 49 | public ApiAtExample() throws XBeeException { 50 | 51 | try { 52 | // replace with port and baud rate of your XBee 53 | xbee.open("/dev/tty.usbserial-A6005uPi", 9600); 54 | 55 | // // set D1 analog input 56 | // this.sendCommand(new AtCommand("D1", 2)); 57 | // // set D2 digital input 58 | // this.sendCommand(new AtCommand("D2", 3)); 59 | // // send sample every 5 seconds 60 | // this.sendCommand(new AtCommand("IR", new int[] {0x13, 0x88})); 61 | 62 | log.info("MY is " + xbee.sendAtCommand(new AtCommand("MY"))); 63 | // log.info("SH is " + xbee.sendAtCommand(new AtCommand("SH"))); 64 | // log.info("SL is " + xbee.sendAtCommand(new AtCommand("SL"))); 65 | } catch (Exception e) { 66 | log.error("at command failed", e); 67 | } finally { 68 | if (xbee != null && xbee.isConnected()) { 69 | xbee.close(); 70 | } 71 | } 72 | } 73 | 74 | public static void main(String[] args) throws XBeeException { 75 | PropertyConfigurator.configure("log4j.properties"); 76 | new ApiAtExample(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/BroadcastSenderExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.XBee; 26 | import com.rapplogic.xbee.api.XBeeAddress64; 27 | import com.rapplogic.xbee.api.XBeeException; 28 | import com.rapplogic.xbee.api.zigbee.ZNetTxRequest; 29 | import com.rapplogic.xbee.util.ByteUtils; 30 | 31 | /** 32 | * @author andrew 33 | */ 34 | public class BroadcastSenderExample { 35 | 36 | private final static Logger log = Logger.getLogger(BroadcastSenderExample.class); 37 | 38 | private BroadcastSenderExample() throws XBeeException { 39 | 40 | XBee xbee = new XBee(); 41 | 42 | try { 43 | // replace with your com port and baud rate. this is the com port of my coordinator 44 | xbee.open("/dev/ttyUSB0", 9600); 45 | 46 | while (true) { 47 | // put some arbitrary data in the payload 48 | int[] payload = ByteUtils.stringToIntArray("the\nquick\nbrown\nfox"); 49 | 50 | ZNetTxRequest request = new ZNetTxRequest(XBeeAddress64.BROADCAST, payload); 51 | // make it a broadcast packet 52 | request.setOption(ZNetTxRequest.Option.BROADCAST); 53 | 54 | log.info("request packet bytes (base 16) " + ByteUtils.toBase16(request.getXBeePacket().getPacket())); 55 | 56 | xbee.sendAsynchronous(request); 57 | // we just assume it was sent. that's just the way it is with broadcast. 58 | // no transmit status response is sent, so don't bother calling getResponse() 59 | 60 | try { 61 | // wait a bit then send another packet 62 | Thread.sleep(15000); 63 | } catch (InterruptedException e) { 64 | } 65 | } 66 | } finally { 67 | if (xbee != null && xbee.isConnected()) { 68 | xbee.close(); 69 | } 70 | } 71 | } 72 | 73 | public static void main(String[] args) throws XBeeException, InterruptedException { 74 | PropertyConfigurator.configure("log4j.properties"); 75 | new BroadcastSenderExample(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/util/DoubleByte.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.util; 21 | 22 | public class DoubleByte { 23 | 24 | private int msb; 25 | private int lsb; 26 | 27 | public DoubleByte() { 28 | 29 | } 30 | 31 | /** 32 | * Decomposes a 16bit int into high and low bytes 33 | * 34 | * @param val 35 | */ 36 | public DoubleByte(int val) { 37 | if (val > 0xFFFF || val < 0) { 38 | throw new IllegalArgumentException("value is out of range"); 39 | } 40 | 41 | // split address into high and low bytes 42 | msb = val >> 8; 43 | lsb = val & 0xFF; 44 | } 45 | 46 | /** 47 | * Constructs a 16bit value from two bytes (high and low) 48 | * 49 | * @param msb 50 | * @param lsb 51 | */ 52 | public DoubleByte(int msb, int lsb) { 53 | 54 | if (msb > 0xFF || lsb > 0xFF) { 55 | throw new IllegalArgumentException("msb or lsb are out of range"); 56 | } 57 | 58 | this.msb = msb; 59 | this.lsb = lsb; 60 | } 61 | 62 | public int getMsb() { 63 | return msb; 64 | } 65 | 66 | public int getLsb() { 67 | return lsb; 68 | } 69 | 70 | public int get16BitValue() { 71 | return (this.msb << 8) + this.lsb; 72 | } 73 | 74 | public void setMsb(int msb) { 75 | this.msb = msb; 76 | } 77 | 78 | public void setLsb(int lsb) { 79 | this.lsb = lsb; 80 | } 81 | 82 | public int[] getArray() { 83 | return new int[] { this.msb, this.lsb }; 84 | } 85 | 86 | @Override 87 | public boolean equals(Object o) { 88 | if (this == o) return true; 89 | if (o == null || getClass() != o.getClass()) return false; 90 | 91 | DoubleByte that = (DoubleByte) o; 92 | 93 | if (lsb != that.lsb) return false; 94 | if (msb != that.msb) return false; 95 | 96 | return true; 97 | } 98 | 99 | @Override 100 | public int hashCode() { 101 | int result = msb; 102 | result = 31 * result + lsb; 103 | return result; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeAddress16.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import com.rapplogic.xbee.util.DoubleByte; 23 | 24 | /** 25 | * Represents a 16-bit XBee Address. 26 | *

27 | * @author andrew 28 | * 29 | */ 30 | public class XBeeAddress16 extends XBeeAddress { 31 | 32 | public static final XBeeAddress16 BROADCAST = new XBeeAddress16(0xFF, 0xFF); 33 | public static final XBeeAddress16 ZNET_BROADCAST = new XBeeAddress16(0xFF, 0xFE); 34 | 35 | private DoubleByte doubleByte = new DoubleByte(); 36 | 37 | /** 38 | * Provide address as msb byte and lsb byte 39 | * 40 | * @param msb 41 | * @param lsb 42 | */ 43 | public XBeeAddress16(int msb, int lsb) { 44 | this.doubleByte.setMsb(msb); 45 | this.doubleByte.setLsb(lsb); 46 | } 47 | 48 | public XBeeAddress16(int[] arr) { 49 | this.doubleByte.setMsb(arr[0]); 50 | this.doubleByte.setLsb(arr[1]); 51 | } 52 | 53 | public XBeeAddress16() { 54 | 55 | } 56 | 57 | public int get16BitValue() { 58 | return this.doubleByte.get16BitValue(); 59 | } 60 | 61 | public int getMsb() { 62 | return this.doubleByte.getMsb(); 63 | } 64 | 65 | public void setMsb(int msb) { 66 | this.doubleByte.setMsb(msb); 67 | } 68 | 69 | public int getLsb() { 70 | return this.doubleByte.getLsb(); 71 | } 72 | 73 | public void setLsb(int lsb) { 74 | this.doubleByte.setLsb(lsb); 75 | } 76 | 77 | @Override 78 | public boolean equals(Object o) { 79 | if (this == o) return true; 80 | if (o == null || getClass() != o.getClass()) return false; 81 | 82 | XBeeAddress16 that = (XBeeAddress16) o; 83 | 84 | if (doubleByte != null ? !doubleByte.equals(that.doubleByte) : that.doubleByte != null) return false; 85 | 86 | return true; 87 | } 88 | 89 | @Override 90 | public int hashCode() { 91 | return doubleByte != null ? doubleByte.hashCode() : 0; 92 | } 93 | 94 | @Override 95 | public int[] getAddress() { 96 | return new int[] { this.doubleByte.getMsb(), this.doubleByte.getLsb() }; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.api; 2 | 3 | public class XBeeConfiguration { 4 | 5 | private boolean shutdownHook = false; 6 | private boolean startupChecks = false; 7 | private int maxQueueSize = 100; 8 | private int sendSynchronousTimeout = 5000; 9 | private ResponseFilter responseQueueFilter; 10 | 11 | private final ResponseFilter noRequestResponseQueueFilter = new ResponseFilter() { 12 | public boolean accept(XBeeResponse response) { 13 | return response instanceof NoRequestResponse; 14 | } 15 | }; 16 | 17 | public XBeeConfiguration() { 18 | 19 | } 20 | 21 | /** 22 | * Controls is a startup check is performed when connecting to the XBee. 23 | * The startup check attempts to determine the firmware type and if it is 24 | * configured correctly for use with this software. Default is true. 25 | * 26 | * @param startupChecks 27 | */ 28 | public XBeeConfiguration withShutdownHook(boolean shutdownHook) { 29 | this.shutdownHook = shutdownHook; 30 | return this; 31 | } 32 | 33 | /** 34 | * Controls is a startup check is performed when connecting to the XBee. 35 | * The startup check attempts to determine the firmware type and if it is 36 | * configured correctly for use with this software. Default is true. 37 | * 38 | * @param startupChecks 39 | */ 40 | public XBeeConfiguration withStartupChecks(boolean startupChecks) { 41 | this.startupChecks = startupChecks; 42 | return this; 43 | } 44 | 45 | /** 46 | * Sets the maximum size of the internal queue that supports the getResponse(..) method. 47 | * Packets are removed from the head of the queue once this limit is reached. The default is 100 48 | * 49 | * @param size 50 | */ 51 | public XBeeConfiguration withMaxQueueSize(int size) { 52 | if (size <= 0) { 53 | throw new IllegalArgumentException("Size must be > 0"); 54 | } 55 | 56 | this.maxQueueSize = size; 57 | return this; 58 | } 59 | 60 | public XBeeConfiguration withResponseQueueFilter(ResponseFilter filter) { 61 | this.responseQueueFilter = filter; 62 | return this; 63 | } 64 | 65 | public XBeeConfiguration withSendSynchronousTimeout(int sendSynchronousTimeout) { 66 | this.sendSynchronousTimeout = sendSynchronousTimeout; 67 | return this; 68 | } 69 | 70 | /** 71 | * Only adds responses that implement NoRequestResponse 72 | * 73 | * @return 74 | */ 75 | public XBeeConfiguration withNoRequestResponseQueueFilter() { 76 | this.responseQueueFilter = this.noRequestResponseQueueFilter; 77 | return this; 78 | } 79 | 80 | public boolean isStartupChecks() { 81 | return startupChecks; 82 | } 83 | 84 | public int getMaxQueueSize() { 85 | return maxQueueSize; 86 | } 87 | 88 | public ResponseFilter getResponseQueueFilter() { 89 | return responseQueueFilter; 90 | } 91 | 92 | public int getSendSynchronousTimeout() { 93 | return sendSynchronousTimeout; 94 | } 95 | 96 | public boolean isShutdownHook() { 97 | return shutdownHook; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/RxBaseResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import java.io.IOException; 23 | 24 | import com.rapplogic.xbee.api.IPacketParser; 25 | import com.rapplogic.xbee.api.XBeeAddress; 26 | import com.rapplogic.xbee.api.XBeeResponse; 27 | import com.rapplogic.xbee.util.ByteUtils; 28 | import com.rapplogic.xbee.util.IIntInputStream; 29 | 30 | /** 31 | * Series 1 XBee. Common elements of 16 and 64 bit Address Receive packets 32 | *

33 | * @author andrew 34 | * 35 | */ 36 | public abstract class RxBaseResponse extends XBeeResponse { 37 | 38 | private XBeeAddress sourceAddress; 39 | 40 | private int rssi; 41 | private int options; 42 | 43 | public RxBaseResponse() { 44 | 45 | } 46 | 47 | public int getRssi() { 48 | return rssi; 49 | } 50 | 51 | public void setRssi(int rssi) { 52 | this.rssi = rssi; 53 | } 54 | 55 | public int getOptions() { 56 | return options; 57 | } 58 | 59 | public void setOptions(int options) { 60 | this.options = options; 61 | } 62 | 63 | public boolean isAddressBroadcast() { 64 | return ByteUtils.getBit(options, 2); 65 | } 66 | 67 | public boolean isPanBroadcast() { 68 | return ByteUtils.getBit(options, 3); 69 | } 70 | 71 | /** 72 | * Returns either a XBeeAddress16 or XBeeAddress64 73 | * depending on if the packet is configured for 16 or 64 bit addressing. 74 | * 75 | * @return 76 | */ 77 | public XBeeAddress getSourceAddress() { 78 | return sourceAddress; 79 | } 80 | 81 | public void setSourceAddress(XBeeAddress sourceAddress) { 82 | this.sourceAddress = sourceAddress; 83 | } 84 | 85 | protected void parseBase(IPacketParser parser) throws IOException { 86 | int rssi = parser.read("RSSI"); 87 | 88 | // rssi is a negative dbm value 89 | this.setRssi(-rssi); 90 | 91 | int options = parser.read("Options"); 92 | 93 | this.setOptions(options); 94 | } 95 | 96 | public String toString() { 97 | return super.toString() + ",sourceAddress=" + this.getSourceAddress() + ",rssi=" + this.getRssi() + ",options=" + this.getOptions() + 98 | ",isAddressBroadcast=" + this.isAddressBroadcast() + ",isPanBroadcast=" + this.isPanBroadcast(); 99 | } 100 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/ZNetIoSampleExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.ApiId; 26 | import com.rapplogic.xbee.api.PacketListener; 27 | import com.rapplogic.xbee.api.XBee; 28 | import com.rapplogic.xbee.api.XBeeResponse; 29 | import com.rapplogic.xbee.api.wpan.IoSample; 30 | import com.rapplogic.xbee.api.wpan.RxResponseIoSample; 31 | import com.rapplogic.xbee.api.zigbee.ZNetRxIoSampleResponse; 32 | 33 | /** 34 | * Series 2 XBees -- Example of receiving I/O samples. To configure your radio for this example, connect 35 | * your end device to your serial connection and run the configureIOSamples() method 36 | * in ZNetApiAtTest. 37 | * 38 | * @author andrew 39 | * 40 | */ 41 | public class ZNetIoSampleExample implements PacketListener { 42 | 43 | private final static Logger log = Logger.getLogger(ZNetIoSampleExample.class); 44 | 45 | private ZNetIoSampleExample() throws Exception { 46 | XBee xbee = new XBee(); 47 | 48 | try { 49 | // replace with the com port of your XBee coordinator 50 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 51 | xbee.addPacketListener(this); 52 | 53 | // wait forever 54 | synchronized(this) { this.wait(); } 55 | } finally { 56 | if (xbee != null && xbee.isConnected()) { 57 | xbee.close(); 58 | } 59 | } 60 | } 61 | 62 | /** 63 | * Called by XBee API thread when a packet is received 64 | */ 65 | public void processResponse(XBeeResponse response) { 66 | // This is a I/O sample response. You will only get this is you are connected to a Coordinator that is configured to 67 | // receive I/O samples from a remote XBee. 68 | 69 | if (response.getApiId() == ApiId.ZNET_IO_SAMPLE_RESPONSE) { 70 | ZNetRxIoSampleResponse ioSample = (ZNetRxIoSampleResponse) response; 71 | 72 | log.debug("received i/o sample packet. contains analog is " + ioSample.containsAnalog() + ", contains digital is " + ioSample.containsDigital()); 73 | 74 | // check the value of the input pins 75 | log.debug("pin 20 (DO) digital is " + ioSample.isD0On()); 76 | log.debug("pin 19 (D1) analog is " + ioSample.getAnalog1()); 77 | } 78 | } 79 | 80 | public static void main(String[] args) throws Exception { 81 | // init log4j 82 | PropertyConfigurator.configure("log4j.properties"); 83 | new ZNetIoSampleExample(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/RemoteAtResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.io.IOException; 23 | 24 | 25 | //TODO Now supported by series 1 XBee. parseIoSample now needs to handle series 1 and 2 26 | 27 | /** 28 | * Supported by both series 1 (10C8 firmware and later) and series 2. 29 | * Represents a response, corresponding to a RemoteAtRequest. 30 | *

31 | * API ID: 0x97 32 | */ 33 | public class RemoteAtResponse extends AtCommandResponse { 34 | 35 | private XBeeAddress64 remoteAddress64; 36 | private XBeeAddress16 remoteAddress16; 37 | 38 | public RemoteAtResponse() { 39 | 40 | } 41 | 42 | public boolean isSixteenBitAddressUnknown() { 43 | return remoteAddress16.getMsb() == 0xff && remoteAddress16.getLsb() == 0xfe; 44 | } 45 | 46 | public XBeeAddress64 getRemoteAddress64() { 47 | return remoteAddress64; 48 | } 49 | 50 | public void setRemoteAddress64( 51 | XBeeAddress64 sixtyFourBitResponderAddress) { 52 | this.remoteAddress64 = sixtyFourBitResponderAddress; 53 | } 54 | 55 | public XBeeAddress16 getRemoteAddress16() { 56 | return remoteAddress16; 57 | } 58 | 59 | public void setRemoteAddress16( 60 | XBeeAddress16 sixteenBitResponderAddress) { 61 | this.remoteAddress16 = sixteenBitResponderAddress; 62 | } 63 | 64 | /** 65 | * @deprecated use getCommand instead 66 | * @return 67 | * Mar 4, 2009 68 | */ 69 | public String getCommandName() { 70 | return super.getCommand(); 71 | } 72 | 73 | /** 74 | * @deprecated use getValue instead 75 | * @return 76 | * Mar 4, 2009 77 | */ 78 | public int[] getCommandData() { 79 | return super.getValue(); 80 | } 81 | 82 | public void parse(IPacketParser parser) throws IOException { 83 | this.setFrameId(parser.read("Remote AT Response Frame Id")); 84 | 85 | this.setRemoteAddress64(parser.parseAddress64()); 86 | 87 | this.setRemoteAddress16(parser.parseAddress16()); 88 | 89 | char cmd1 = (char)parser.read("Command char 1"); 90 | char cmd2 = (char)parser.read("Command char 2"); 91 | //this.setCommand(new String(new char[] {cmd1, cmd2})); 92 | this.setChar1(cmd1); 93 | this.setChar2(cmd2); 94 | 95 | int status = parser.read("AT Response Status"); 96 | this.setStatus(RemoteAtResponse.Status.get(status)); 97 | 98 | this.setValue(parser.readRemainingBytes()); 99 | } 100 | 101 | public String toString() { 102 | return super.toString() + 103 | ",remoteAddress64=" + this.remoteAddress64 + 104 | ",remoteAddress16=" + this.remoteAddress16; 105 | } 106 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/ModemStatusResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.io.IOException; 23 | import java.util.EnumSet; 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | import org.apache.log4j.Logger; 28 | 29 | /** 30 | * RF module status messages are sent from the module in response to specific conditions. 31 | *

32 | * API ID: 0x8a 33 | *

34 | * @author andrew 35 | * 36 | */ 37 | public class ModemStatusResponse extends XBeeResponse implements NoRequestResponse { 38 | 39 | private final static Logger log = Logger.getLogger(ModemStatusResponse.class); 40 | 41 | public enum Status { 42 | HARDWARE_RESET (0), 43 | WATCHDOG_TIMER_RESET (1), 44 | ASSOCIATED (2), 45 | DISASSOCIATED (3), 46 | SYNCHRONIZATION_LOST (4), 47 | COORDINATOR_REALIGNMENT (5), 48 | COORDINATOR_STARTED (6), 49 | NETWORK_SEC_KEY_UPDATED (7), 50 | VOLTAGE_SUPPLY_EXCEEDED (0x0D), 51 | MODEM_CONFIG_CHANGED (0x11), 52 | STACK_ERROR (0x80), 53 | UNKNOWN(-1); 54 | 55 | 56 | private static final Map lookup = new HashMap(); 57 | 58 | static { 59 | for(Status s : EnumSet.allOf(Status.class)) { 60 | lookup.put(s.getValue(), s); 61 | } 62 | } 63 | 64 | public static Status get(int value) { 65 | return lookup.get(value); 66 | } 67 | 68 | private final int value; 69 | 70 | Status(int value) { 71 | this.value = value; 72 | } 73 | 74 | public int getValue() { 75 | return value; 76 | } 77 | } 78 | 79 | private Status status; 80 | 81 | public ModemStatusResponse() { 82 | 83 | } 84 | 85 | public Status getStatus() { 86 | return status; 87 | } 88 | 89 | public void setStatus(Status status) { 90 | this.status = status; 91 | } 92 | 93 | protected void parse(IPacketParser parser) throws IOException { 94 | int value = parser.read("Modem Status"); 95 | 96 | if (value >= 0x80) { 97 | this.setStatus(ModemStatusResponse.Status.STACK_ERROR); 98 | } else { 99 | ModemStatusResponse.Status status = ModemStatusResponse.Status.get(value); 100 | 101 | if (status == null) { 102 | log.warn("Unknown status " + value); 103 | this.setStatus(ModemStatusResponse.Status.UNKNOWN); 104 | } else { 105 | this.setStatus(status); 106 | } 107 | } 108 | } 109 | 110 | public String toString() { 111 | return super.toString() + ",status=" + this.status; 112 | } 113 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/TxRequestBase.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import java.util.EnumSet; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import com.rapplogic.xbee.api.XBeeRequest; 27 | import com.rapplogic.xbee.util.ByteUtils; 28 | 29 | /** 30 | * Series 1 XBee. Super class for 16 and 64 bit address Transmit packets 31 | *

32 | * @author andrew 33 | * 34 | */ 35 | public abstract class TxRequestBase extends XBeeRequest { 36 | 37 | /** 38 | * Maximum payload size as specified in the series 1 XBee manual. 39 | * This is provided for reference only and is not used for validation 40 | */ 41 | public final static int MAX_PAYLOAD_SIZE = 100; 42 | 43 | private int maxPayloadSize; 44 | 45 | public enum Option { 46 | 47 | UNICAST (0), 48 | DISABLE_ACK (1), 49 | BROADCAST(4); 50 | 51 | private static final Map lookup = new HashMap(); 52 | 53 | static { 54 | for(Option s : EnumSet.allOf(Option.class)) { 55 | lookup.put(s.getValue(), s); 56 | } 57 | } 58 | 59 | public static Option get(int value) { 60 | return lookup.get(value); 61 | } 62 | 63 | private final int value; 64 | 65 | Option(int value) { 66 | this.value = value; 67 | } 68 | 69 | public int getValue() { 70 | return value; 71 | } 72 | } 73 | 74 | private int[] payload; 75 | private Option option; 76 | 77 | // TODO inconsistency: tx uses setPayload, rx uses getData 78 | public int[] getPayload() { 79 | return payload; 80 | } 81 | 82 | public void setPayload(int[] payload) { 83 | 84 | if (this.getMaxPayloadSize() > 0 && payload.length > this.getMaxPayloadSize()) { 85 | throw new IllegalArgumentException("Payload exceeds user-defined maximum payload size of " + this.getMaxPayloadSize() + " bytes. Please re-package into multiple packets"); 86 | } 87 | 88 | this.payload = payload; 89 | } 90 | 91 | public Option getOption() { 92 | return option; 93 | } 94 | 95 | public void setOption(Option option) { 96 | this.option = option; 97 | } 98 | 99 | public String toString() { 100 | return super.toString() + ",option=" + this.option + 101 | ",payload=" + ByteUtils.toBase16(this.payload); 102 | } 103 | 104 | public int getMaxPayloadSize() { 105 | return maxPayloadSize; 106 | } 107 | 108 | public void setMaxPayloadSize(int maxPayloadSize) { 109 | this.maxPayloadSize = maxPayloadSize; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/ApiId.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.util.EnumSet; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import com.rapplogic.xbee.util.ByteUtils; 27 | 28 | public enum ApiId { 29 | /** 30 | * API ID: 0x0 31 | */ 32 | TX_REQUEST_64 (0x0), 33 | /** 34 | * API ID: 0x1 35 | */ 36 | TX_REQUEST_16 (0x1), 37 | /** 38 | * API ID: 0x08 39 | */ 40 | AT_COMMAND (0x08), 41 | /** 42 | * API ID: 0x09 43 | */ 44 | AT_COMMAND_QUEUE (0x09), 45 | /** 46 | * API ID: 0x17 47 | */ 48 | REMOTE_AT_REQUEST (0x17), 49 | /** 50 | * API ID: 0x10 51 | */ 52 | ZNET_TX_REQUEST (0x10), 53 | /** 54 | * API ID: 0x11 55 | */ 56 | ZNET_EXPLICIT_TX_REQUEST (0x11), 57 | /** 58 | * API ID: 0x80 59 | */ 60 | RX_64_RESPONSE (0x80), 61 | /** 62 | * API ID: 0x81 63 | */ 64 | RX_16_RESPONSE (0x81), 65 | /** 66 | * API ID: 0x82 67 | */ 68 | RX_64_IO_RESPONSE (0x82), 69 | /** 70 | * API ID: 0x83 71 | */ 72 | RX_16_IO_RESPONSE (0x83), 73 | /** 74 | * API ID: 0x88 75 | */ 76 | AT_RESPONSE (0x88), 77 | /** 78 | * API ID: 0x89 79 | */ 80 | TX_STATUS_RESPONSE (0x89), 81 | /** 82 | * API ID: 0x8a 83 | */ 84 | MODEM_STATUS_RESPONSE (0x8a), 85 | /** 86 | * API ID: 0x90 87 | */ 88 | ZNET_RX_RESPONSE (0x90), 89 | /** 90 | * API ID: 0x91 91 | */ 92 | ZNET_EXPLICIT_RX_RESPONSE (0x91), 93 | /** 94 | * API ID: 0x8b 95 | */ 96 | ZNET_TX_STATUS_RESPONSE (0x8b), 97 | /** 98 | * API ID: 0x97 99 | */ 100 | REMOTE_AT_RESPONSE (0x97), 101 | /** 102 | * API ID: 0x92 103 | */ 104 | ZNET_IO_SAMPLE_RESPONSE (0x92), 105 | /** 106 | * API ID: 0x95 107 | */ 108 | ZNET_IO_NODE_IDENTIFIER_RESPONSE (0x95), 109 | /** 110 | * Indicates that we've parsed a packet for which we didn't know how to handle the API type. This will be parsed into a GenericResponse 111 | */ 112 | UNKNOWN (0xff), 113 | /** 114 | * This is returned if an error occurs during packet parsing and does not correspond to a XBee API ID. 115 | */ 116 | ERROR_RESPONSE (-1); 117 | 118 | private static final Map lookup = new HashMap(); 119 | 120 | static { 121 | for(ApiId s : EnumSet.allOf(ApiId.class)) { 122 | lookup.put(s.getValue(), s); 123 | } 124 | } 125 | 126 | public static ApiId get(int value) { 127 | return lookup.get(value); 128 | } 129 | 130 | private final int value; 131 | 132 | ApiId(int value) { 133 | this.value = value; 134 | } 135 | 136 | public int getValue() { 137 | return value; 138 | } 139 | 140 | public String toString() { 141 | return this.name() + " (" + ByteUtils.toBase16(this.getValue()) + ")"; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/TxStatusResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import java.io.IOException; 23 | import java.util.EnumSet; 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | import com.rapplogic.xbee.api.IPacketParser; 28 | import com.rapplogic.xbee.api.XBeeFrameIdResponse; 29 | 30 | /** 31 | * Series 1 XBee. This is sent out the UART of the transmitting XBee immediately following 32 | * a Transmit packet. Indicates if the Transmit (TxRequest16 or TxRequest64) 33 | * was successful. 34 | *

35 | * API ID: 0x89 36 | *

37 | * @author andrew 38 | * 39 | */ 40 | public class TxStatusResponse extends XBeeFrameIdResponse { 41 | 42 | public enum Status { 43 | 44 | SUCCESS (0), 45 | NO_ACK (1), 46 | CCA_FAILURE(2), 47 | PURGED(3); 48 | 49 | private static final Map lookup = new HashMap(); 50 | 51 | static { 52 | for(Status s : EnumSet.allOf(Status.class)) { 53 | lookup.put(s.getValue(), s); 54 | } 55 | } 56 | 57 | public static Status get(int value) { 58 | return lookup.get(value); 59 | } 60 | 61 | private final int value; 62 | 63 | Status(int value) { 64 | this.value = value; 65 | } 66 | 67 | public int getValue() { 68 | return value; 69 | } 70 | } 71 | 72 | private Status status; 73 | 74 | public TxStatusResponse() { 75 | 76 | } 77 | 78 | public Status getStatus() { 79 | return status; 80 | } 81 | 82 | public void setStatus(Status status) { 83 | this.status = status; 84 | } 85 | 86 | /** 87 | * Returns true if the delivery status is SUCCESS 88 | */ 89 | public boolean isSuccess() { 90 | return this.status == Status.SUCCESS; 91 | } 92 | 93 | // isError() was overridding XBeeResponse isError() 94 | 95 | public boolean isAckError() { 96 | return this.status == Status.NO_ACK; 97 | } 98 | 99 | public boolean isCcaError() { 100 | return this.status == Status.CCA_FAILURE; 101 | } 102 | 103 | public boolean isPurged() { 104 | return this.status == Status.PURGED; 105 | } 106 | 107 | 108 | public void parse(IPacketParser parser) throws IOException { 109 | // frame id 110 | int frameId = parser.read("TxStatus Frame Id"); 111 | this.setFrameId(frameId); 112 | 113 | //log.debug("frame id is " + frameId); 114 | 115 | // Status: 0=Success, 1= No Ack, 2= CCA Failure, 3= Purge 116 | int status = parser.read("TX Status"); 117 | this.setStatus(TxStatusResponse.Status.get(status)); 118 | } 119 | 120 | public String toString() { 121 | return super.toString() + ",status=" + this.getStatus(); 122 | } 123 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/wpan/ApiReceiverExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.wpan; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.ApiId; 26 | import com.rapplogic.xbee.api.ErrorResponse; 27 | import com.rapplogic.xbee.api.XBee; 28 | import com.rapplogic.xbee.api.XBeeResponse; 29 | import com.rapplogic.xbee.api.wpan.RxResponse16; 30 | import com.rapplogic.xbee.api.wpan.RxResponse64; 31 | import com.rapplogic.xbee.util.ByteUtils; 32 | 33 | /** 34 | * Receives IO samples from remote radio 35 | * I have a photoresistor connected to analog0 and a thermistor is connected to analog1 36 | * Also there is a breadboard switch connected to digital2 with change detect configured 37 | * 38 | * @author andrew 39 | * 40 | */ 41 | public class ApiReceiverExample { 42 | 43 | private final static Logger log = Logger.getLogger(ApiReceiverExample.class); 44 | 45 | private long last = System.currentTimeMillis(); 46 | 47 | private ApiReceiverExample() throws Exception { 48 | XBee xbee = new XBee(); 49 | 50 | int count = 0; 51 | int errors = 0; 52 | 53 | try { 54 | // my end device 55 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 56 | // my coordinator 57 | //xbee.open("/dev/tty.usbserial-A4004Rim", 9600); 58 | 59 | while (true) { 60 | 61 | try { 62 | XBeeResponse response = xbee.getResponse(); 63 | count++; 64 | 65 | if (response.isError()) { 66 | log.info("response contains errors", ((ErrorResponse)response).getException()); 67 | errors++; 68 | } 69 | 70 | for (int i = 0; i < response.getPacketBytes().length; i++) { 71 | log.info("packet [" + i + "] " + ByteUtils.toBase16(response.getPacketBytes()[i])); 72 | } 73 | 74 | if (response.getApiId() == ApiId.RX_16_RESPONSE) { 75 | log.info("Received RX 16 packet " + ((RxResponse16)response)); 76 | } else if (response.getApiId() == ApiId.RX_64_RESPONSE) { 77 | log.info("Received RX 64 packet " + ((RxResponse64)response)); 78 | } else { 79 | log.info("Ignoring mystery packet " + response.toString()); 80 | } 81 | 82 | log.debug("Received response: " + response.toString() + ", count is " + count + ", errors is " + errors); 83 | } catch (Exception e) { 84 | log.error(e); 85 | } 86 | } 87 | } finally { 88 | if (xbee != null && xbee.isConnected()) { 89 | xbee.close(); 90 | } 91 | } 92 | } 93 | 94 | public static void main(String[] args) throws Exception { 95 | // init log4j 96 | PropertyConfigurator.configure("log4j.properties"); 97 | new ApiReceiverExample(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/XBeeAddress64.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.util.StringTokenizer; 23 | import java.util.Arrays; 24 | 25 | /** 26 | * Represents a 64-bit XBee Address 27 | *

28 | * @author andrew 29 | * 30 | */ 31 | public class XBeeAddress64 extends XBeeAddress { 32 | 33 | public static final XBeeAddress64 BROADCAST = new XBeeAddress64(new int[] {0, 0, 0, 0, 0, 0, 0xff, 0xff}); 34 | public static final XBeeAddress64 ZNET_COORDINATOR = new XBeeAddress64(new int[] {0, 0, 0, 0, 0, 0, 0, 0}); 35 | 36 | private int[] address; 37 | 38 | /** 39 | * Parses an 64-bit XBee address from a string representation 40 | * May be contain spaces ## ## ## ## ## ## ## ## or without ################ but cannot use the 0x prefix 41 | * ex: 0013A200408B98FF or 00 13 A2 00 40 8B 98 FF 42 | * 43 | * @param addressStr 44 | */ 45 | public XBeeAddress64(String addressStr) { 46 | address = new int[8]; 47 | 48 | if (addressStr.contains(" ")) { 49 | StringTokenizer st = new StringTokenizer(addressStr, " "); 50 | 51 | for (int i = 0; i < address.length; i++) { 52 | String byteStr = st.nextToken(); 53 | address[i] = Integer.parseInt(byteStr, 16); 54 | } 55 | } else { 56 | // secretly also handle no space format 57 | for (int i = 0; i < address.length; i++) { 58 | address[i] = Integer.parseInt(addressStr.substring(i*2, i*2+2), 16); 59 | } 60 | } 61 | } 62 | 63 | /** 64 | * Creates a 64-bit address 65 | * 66 | * @param b1 MSB 67 | * @param b2 68 | * @param b3 69 | * @param b4 70 | * @param b5 71 | * @param b6 72 | * @param b7 73 | * @param b8 LSB 74 | */ 75 | public XBeeAddress64(int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8) { 76 | address = new int[8]; 77 | 78 | address[0] = b1; 79 | address[1] = b2; 80 | address[2] = b3; 81 | address[3] = b4; 82 | address[4] = b5; 83 | address[5] = b6; 84 | address[6] = b7; 85 | address[7] = b8; 86 | } 87 | 88 | public XBeeAddress64(int[] address) { 89 | this.address = address; 90 | } 91 | 92 | public XBeeAddress64() { 93 | address = new int[8]; 94 | } 95 | 96 | public void setAddress(int[] address) { 97 | this.address = address; 98 | } 99 | 100 | @Override 101 | public boolean equals(Object o) { 102 | if (this == o) return true; 103 | if (o == null || getClass() != o.getClass()) return false; 104 | 105 | XBeeAddress64 that = (XBeeAddress64) o; 106 | 107 | if (!Arrays.equals(address, that.address)) return false; 108 | 109 | return true; 110 | } 111 | 112 | @Override 113 | public int hashCode() { 114 | return address != null ? Arrays.hashCode(address) : 0; 115 | } 116 | 117 | @Override 118 | public int[] getAddress() { 119 | return address; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/ZBNodeDiscoverExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import java.util.List; 23 | 24 | import org.apache.log4j.Logger; 25 | import org.apache.log4j.PropertyConfigurator; 26 | 27 | import com.rapplogic.xbee.api.ApiId; 28 | import com.rapplogic.xbee.api.AtCommand; 29 | import com.rapplogic.xbee.api.AtCommandResponse; 30 | import com.rapplogic.xbee.api.PacketListener; 31 | import com.rapplogic.xbee.api.XBee; 32 | import com.rapplogic.xbee.api.XBeeException; 33 | import com.rapplogic.xbee.api.XBeeResponse; 34 | import com.rapplogic.xbee.api.zigbee.ZBNodeDiscover; 35 | import com.rapplogic.xbee.util.ByteUtils; 36 | 37 | /** 38 | * Example of performing a node discover for Series 2 XBees. 39 | * You must connect to the coordinator to run this example and 40 | * have one or more end device/routers that are associated. 41 | * 42 | * @author andrew 43 | * 44 | */ 45 | public class ZBNodeDiscoverExample { 46 | 47 | private final static Logger log = Logger.getLogger(ZBNodeDiscoverExample.class); 48 | 49 | private XBee xbee = new XBee(); 50 | 51 | public ZBNodeDiscoverExample() throws XBeeException, InterruptedException { 52 | 53 | try { 54 | // replace with your serial port 55 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 56 | 57 | 58 | // get the Node discovery timeout 59 | xbee.sendAsynchronous(new AtCommand("NT")); 60 | AtCommandResponse nodeTimeout = (AtCommandResponse) xbee.getResponse(); 61 | 62 | // default is 6 seconds 63 | int nodeDiscoveryTimeout = ByteUtils.convertMultiByteToInt(nodeTimeout.getValue()) * 100; 64 | log.info("Node discovery timeout is " + nodeDiscoveryTimeout + " milliseconds"); 65 | 66 | log.info("Sending Node Discover command"); 67 | xbee.sendAsynchronous(new AtCommand("ND")); 68 | 69 | // NOTE: increase NT if you are not seeing all your nodes reported 70 | 71 | List responses = xbee.collectResponses(nodeDiscoveryTimeout); 72 | 73 | log.info("Time is up! You should have heard back from all nodes by now. If not make sure all nodes are associated and/or try increasing the node timeout (NT)"); 74 | 75 | for (XBeeResponse response : responses) { 76 | if (response instanceof AtCommandResponse) { 77 | AtCommandResponse atResponse = (AtCommandResponse) response; 78 | 79 | if (atResponse.getCommand().equals("ND") && atResponse.getValue() != null && atResponse.getValue().length > 0) { 80 | ZBNodeDiscover nd = ZBNodeDiscover.parse((AtCommandResponse)response); 81 | log.info("Node Discover is " + nd); 82 | } 83 | } 84 | } 85 | } finally { 86 | if (xbee != null && xbee.isConnected()) { 87 | xbee.close(); 88 | } 89 | } 90 | } 91 | 92 | public static void main(String[] args) throws XBeeException, InterruptedException { 93 | PropertyConfigurator.configure("log4j.properties"); 94 | new ZBNodeDiscoverExample(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/ZBForceSampleExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.XBee; 26 | import com.rapplogic.xbee.api.XBeeAddress64; 27 | import com.rapplogic.xbee.api.XBeeRequest; 28 | import com.rapplogic.xbee.api.XBeeResponse; 29 | import com.rapplogic.xbee.api.XBeeTimeoutException; 30 | import com.rapplogic.xbee.api.RemoteAtResponse; 31 | import com.rapplogic.xbee.api.zigbee.ZBForceSampleRequest; 32 | import com.rapplogic.xbee.api.zigbee.ZNetRxIoSampleResponse; 33 | 34 | /** 35 | * To run this sample you will need to configure pin 19 (D1) to Analog input on the remote XBee: D1=2. 36 | * To do this you can use X-CTU, the AtCommand [new AtCommand("D1", 2)] with your remote XBee connected to the serial port 37 | * or with RemoteAtRequest and send the request through the coordinator. 38 | * 39 | * @author andrew 40 | * 41 | */ 42 | public class ZBForceSampleExample { 43 | 44 | private final static Logger log = Logger.getLogger(ZBForceSampleExample.class); 45 | 46 | private ZBForceSampleExample() throws Exception { 47 | XBee xbee = new XBee(); 48 | 49 | try { 50 | // replace with the com port of your XBee coordinator 51 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 52 | 53 | while (true) { 54 | // All XBees allow you to request an I/O sample on a local XBee (serial connected), however this is not very interesting. 55 | // With ZNet/ZB Pro radios we can use Remote AT to force an I/O sample on an end device. 56 | // The following code issues a force sample on a XBee end device and parses the io sample. 57 | 58 | // replace with your end device 64-bit address 59 | XBeeAddress64 addr64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x0a, 0x3e, 0x02); 60 | 61 | XBeeRequest request = new ZBForceSampleRequest(addr64); 62 | 63 | try { 64 | XBeeResponse response = xbee.sendSynchronous(request, 6000); 65 | RemoteAtResponse remoteAt = (RemoteAtResponse) response; 66 | 67 | if (remoteAt.isOk()) { 68 | // extract the i/o sample 69 | ZNetRxIoSampleResponse ioSample = ZNetRxIoSampleResponse.parseIsSample(remoteAt); 70 | // make sure you configured the remote XBee to D1=2 (analog input) or you will get an error 71 | log.info("10 bit analog1 sample is " + ioSample.getAnalog1()); 72 | } else { 73 | log.info("Remote AT request failed: " + response); 74 | } 75 | } catch (XBeeTimeoutException e) { 76 | log.info("request timed out"); 77 | } 78 | 79 | // wait a bit 80 | Thread.sleep(2000); 81 | } 82 | } finally { 83 | if (xbee != null && xbee.isConnected()) { 84 | xbee.close(); 85 | } 86 | } 87 | } 88 | 89 | public static void main(String[] args) throws Exception { 90 | // init log4j 91 | PropertyConfigurator.configure("log4j.properties"); 92 | new ZBForceSampleExample(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/WpanNodeDiscover.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import org.apache.log4j.Logger; 23 | 24 | import com.rapplogic.xbee.api.AtCommandResponse; 25 | import com.rapplogic.xbee.api.XBeeAddress16; 26 | import com.rapplogic.xbee.api.XBeeAddress64; 27 | import com.rapplogic.xbee.util.IntArrayInputStream; 28 | 29 | // tested ok via xmpp on 4/13/09 30 | 31 | /** 32 | * Series 1 XBee. Parses a Node Discover (ND) AT Command Response 33 | */ 34 | public class WpanNodeDiscover { 35 | 36 | private final static Logger log = Logger.getLogger(WpanNodeDiscover.class); 37 | 38 | private XBeeAddress16 nodeAddress16; 39 | private XBeeAddress64 nodeAddress64; 40 | private int rssi; 41 | private String nodeIdentifier; 42 | 43 | public static WpanNodeDiscover parse(AtCommandResponse response) { 44 | 45 | if (!response.getCommand().equals("ND")) { 46 | throw new IllegalArgumentException("This method is only applicable for the ND command"); 47 | } 48 | 49 | int[] data = response.getValue(); 50 | 51 | if (data == null || data.length == 0) { 52 | throw new IllegalArgumentException("ND command has no value"); 53 | } 54 | 55 | IntArrayInputStream in = new IntArrayInputStream(data); 56 | 57 | WpanNodeDiscover nd = new WpanNodeDiscover(); 58 | 59 | nd.setNodeAddress16(new XBeeAddress16(in.read(2))); 60 | 61 | nd.setNodeAddress64(new XBeeAddress64(in.read(8))); 62 | 63 | nd.setRssi(-1*in.read()); 64 | 65 | StringBuilder ni = new StringBuilder(); 66 | 67 | int ch; 68 | 69 | // NI is terminated with 0 70 | while ((ch = in.read()) != 0) { 71 | if (ch < 32 || ch > 126) { 72 | throw new RuntimeException("Node Identifier " + ch + " is non-ascii"); 73 | } 74 | 75 | ni.append((char)ch); 76 | } 77 | 78 | nd.setNodeIdentifier(ni.toString()); 79 | 80 | return nd; 81 | } 82 | 83 | public String toString() { 84 | return "nodeAddress16=" + this.nodeAddress16 + 85 | ", nodeAddress64=" + this.nodeAddress64 + 86 | ", rssi=" + this.rssi + 87 | ", nodeIdentifier=" + this.nodeIdentifier; 88 | } 89 | 90 | 91 | public XBeeAddress16 getNodeAddress16() { 92 | return nodeAddress16; 93 | } 94 | 95 | 96 | public void setNodeAddress16(XBeeAddress16 my) { 97 | this.nodeAddress16 = my; 98 | } 99 | 100 | 101 | public XBeeAddress64 getNodeAddress64() { 102 | return nodeAddress64; 103 | } 104 | 105 | 106 | public void setNodeAddress64(XBeeAddress64 serial) { 107 | this.nodeAddress64 = serial; 108 | } 109 | 110 | 111 | public String getNodeIdentifier() { 112 | return nodeIdentifier; 113 | } 114 | 115 | 116 | public void setNodeIdentifier(String nodeIdentifier) { 117 | this.nodeIdentifier = nodeIdentifier; 118 | } 119 | 120 | public int getRssi() { 121 | return rssi; 122 | } 123 | 124 | public void setRssi(int rssi) { 125 | this.rssi = rssi; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/zigbee/ZNetExplicitRxResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.zigbee; 21 | 22 | import java.io.IOException; 23 | 24 | import com.rapplogic.xbee.api.IPacketParser; 25 | import com.rapplogic.xbee.util.ByteUtils; 26 | import com.rapplogic.xbee.util.DoubleByte; 27 | 28 | // TODO deprecate all ZNet* classes and replace with ZigBee* 29 | /** 30 | * Series 2 XBee. This packet is received when a remote XBee sends a ZNetExplicitTxRequest 31 | *

32 | * Radio must be configured for explicit frames to use this class (AO=1) 33 | *

34 | * API ID: 0x91 35 | */ 36 | public class ZNetExplicitRxResponse extends ZNetRxResponse { 37 | 38 | private int sourceEndpoint; 39 | private int destinationEndpoint; 40 | private DoubleByte clusterId; 41 | private DoubleByte profileId; 42 | 43 | public ZNetExplicitRxResponse() { 44 | super(); 45 | } 46 | 47 | public int getSourceEndpoint() { 48 | return sourceEndpoint; 49 | } 50 | 51 | public void setSourceEndpoint(int sourceEndpoint) { 52 | this.sourceEndpoint = sourceEndpoint; 53 | } 54 | 55 | public int getDestinationEndpoint() { 56 | return destinationEndpoint; 57 | } 58 | 59 | public void setDestinationEndpoint(int destinationEndpoint) { 60 | this.destinationEndpoint = destinationEndpoint; 61 | } 62 | 63 | public DoubleByte getClusterId() { 64 | return clusterId; 65 | } 66 | 67 | public void setClusterId(DoubleByte clusterId) { 68 | this.clusterId = clusterId; 69 | } 70 | 71 | public DoubleByte getProfileId() { 72 | return profileId; 73 | } 74 | 75 | public void setProfileId(DoubleByte profileId) { 76 | this.profileId = profileId; 77 | } 78 | 79 | public void parse(IPacketParser parser) throws IOException { 80 | this.parseAddress(parser); 81 | 82 | this.setSourceEndpoint(parser.read("Reading Source Endpoint")); 83 | this.setDestinationEndpoint(parser.read("Reading Destination Endpoint")); 84 | DoubleByte clusterId = new DoubleByte(); 85 | clusterId.setMsb(parser.read("Reading Cluster Id MSB")); 86 | clusterId.setLsb(parser.read("Reading Cluster Id LSB")); 87 | this.setClusterId(clusterId); 88 | 89 | DoubleByte profileId = new DoubleByte(); 90 | profileId.setMsb(parser.read("Reading Profile Id MSB")); 91 | profileId.setLsb(parser.read("Reading Profile Id LSB")); 92 | this.setProfileId(profileId); 93 | 94 | this.parseOption(parser); 95 | this.setData(parser.readRemainingBytes()); 96 | } 97 | 98 | public String toString() { 99 | return super.toString() + 100 | ",sourceEndpoint=" + ByteUtils.toBase16(this.getSourceEndpoint()) + 101 | ",destinationEndpoint=" + ByteUtils.toBase16(this.getDestinationEndpoint()) + 102 | ",clusterId(msb)=" + ByteUtils.toBase16(this.getClusterId().getMsb()) + 103 | ",clusterId(lsb)=" + ByteUtils.toBase16(this.getClusterId().getLsb()) + 104 | ",profileId(msb)=" + ByteUtils.toBase16(this.getProfileId().getMsb()) + 105 | ",profileId(lsb)=" + ByteUtils.toBase16(this.getProfileId().getLsb()); 106 | } 107 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/TxRequest16.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import com.rapplogic.xbee.api.ApiId; 23 | import com.rapplogic.xbee.api.XBeeAddress16; 24 | import com.rapplogic.xbee.util.IntArrayOutputStream; 25 | 26 | /** 27 | * Series 1 XBee. 16-bit address Transmit Packet. This is received on the destination XBee 28 | * radio as a RxResponse16 response 29 | *

30 | * API ID: 0x1 31 | *

32 | * @author andrew 33 | * 34 | */ 35 | public class TxRequest16 extends TxRequestBase { 36 | 37 | private XBeeAddress16 remoteAddr16; 38 | 39 | /** 40 | * 16 bit Tx Request with default frame id and awk option 41 | * 42 | * Keep in mind that if you programmed the destination address with X-CTU, the unit is 43 | * hex, so if you set MY=1234, use 0x1234. 44 | * 45 | * @param remoteAddr16 46 | * @param payload 47 | */ 48 | public TxRequest16(XBeeAddress16 remoteAddr16, int[] payload) { 49 | this(remoteAddr16, DEFAULT_FRAME_ID, Option.UNICAST, payload); 50 | } 51 | 52 | /** 53 | * 16 bit Tx Request with frame id argument 54 | * 55 | * 56 | * Payload size is limited to 100 bytes, according to MaxStream documentation. 57 | * 58 | * @param remoteAddr16 59 | * @param frameId 60 | * @param payload 61 | */ 62 | public TxRequest16(XBeeAddress16 remoteAddr16, int frameId, int[] payload) { 63 | this(remoteAddr16, frameId, Option.UNICAST, payload); 64 | } 65 | 66 | /** 67 | * Note: if option is DISABLE_ACK_OPTION you will not get a ack response and you must use the asynchronous send method 68 | * 69 | * @param remoteAddr16 70 | * @param frameId 71 | * @param payload 72 | * @param option 73 | */ 74 | public TxRequest16(XBeeAddress16 remoteAddr16, int frameId, Option option, int[] payload) { 75 | this.remoteAddr16 = remoteAddr16; 76 | this.setFrameId(frameId); 77 | this.setOption(option); 78 | this.setPayload(payload); 79 | } 80 | 81 | public int[] getFrameData() { 82 | // 3/6/10 fixed bug -- broadcast address is used with broadcast option, not no ACK 83 | 84 | IntArrayOutputStream out = new IntArrayOutputStream(); 85 | 86 | // api id 87 | out.write(this.getApiId().getValue()); 88 | // frame id (arbitrary byte that will be sent back with ack) 89 | out.write(this.getFrameId()); 90 | // destination address (broadcast is 0xFFFF) 91 | out.write(remoteAddr16.getAddress()); 92 | // options byte disable ack = 1, send pan id = 4 93 | out.write(this.getOption().getValue()); 94 | out.write(this.getPayload()); 95 | 96 | return out.getIntArray(); 97 | } 98 | 99 | public ApiId getApiId() { 100 | return ApiId.TX_REQUEST_16; 101 | } 102 | 103 | public XBeeAddress16 getRemoteAddr16() { 104 | return remoteAddr16; 105 | } 106 | 107 | public void setRemoteAddr16(XBeeAddress16 remoteAddr16) { 108 | this.remoteAddr16 = remoteAddr16; 109 | } 110 | 111 | public String toString() { 112 | return super.toString() + 113 | ",remoteAddress16=" + this.remoteAddr16; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/socket/SocketXBeeConnection.java: -------------------------------------------------------------------------------- 1 | package com.rapplogic.xbee.socket; 2 | 3 | import com.rapplogic.xbee.AbstractXBeeConnection; 4 | import org.apache.log4j.Logger; 5 | 6 | import java.io.IOException; 7 | import java.io.OutputStream; 8 | import java.net.Socket; 9 | import java.util.concurrent.ExecutorService; 10 | import java.util.concurrent.Executors; 11 | import java.util.concurrent.ThreadFactory; 12 | 13 | // TODO handle reconnects 14 | public class SocketXBeeConnection extends AbstractXBeeConnection { 15 | 16 | private final static Logger log = Logger.getLogger(SocketXBeeConnection.class); 17 | 18 | private Socket socket; 19 | private XBeeSocketOutputStream xBeeSocketOutputStream; 20 | private OutputStream out; 21 | 22 | public SocketXBeeConnection(final String host, final Integer port) throws ServerNotAvailableException { 23 | init(host, port); 24 | } 25 | 26 | @Override 27 | public OutputStream getOutputStream() { 28 | return xBeeSocketOutputStream; 29 | } 30 | 31 | private ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory() { 32 | @Override 33 | public Thread newThread(Runnable runnable) { 34 | Thread thread = Executors.defaultThreadFactory().newThread(runnable); 35 | thread.setDaemon(false); 36 | return thread; 37 | } 38 | }); 39 | 40 | private void init(final String host, final Integer port) throws ServerNotAvailableException { 41 | Socket socket; 42 | 43 | try { 44 | socket = new Socket(host, port); 45 | log.info("Successfully connected to socket server"); 46 | } catch (IOException e) { 47 | log.warn("Unable to connect to host:port " + host + ":" + port); 48 | throw new ServerNotAvailableException("Unable to connect to host:port " + host + ":" + port, e); 49 | } 50 | 51 | try { 52 | out = socket.getOutputStream(); 53 | xBeeSocketOutputStream = new XBeeSocketOutputStream(); 54 | } catch (IOException e) { 55 | throw new RuntimeException("Outputstream not available for socket", e); 56 | } 57 | 58 | try { 59 | socket.getInputStream(); 60 | } catch (IOException e) { 61 | throw new RuntimeException("Inputstream not available for socket", e); 62 | } 63 | 64 | this.socket = socket; 65 | this.pipeSocketInputStreamToXBee(); 66 | } 67 | 68 | private void pipeSocketInputStreamToXBee() { 69 | 70 | executorService.submit(new Runnable() { 71 | @Override 72 | public void run() { 73 | int b = 0; 74 | 75 | try { 76 | while ((b = socket.getInputStream().read()) != -1) { 77 | pipeToInputStream(b); 78 | } 79 | 80 | log.debug("End of socket input stream.. exiting"); 81 | } catch (IOException e) { 82 | log.warn("Error reading from socket input stream " + e.toString() + "... closing socket"); 83 | tryClose(); 84 | } catch (Throwable t) { 85 | log.error("Error reading from input to the xbee output stream", t); 86 | 87 | try { 88 | close(); 89 | } catch (Throwable t2) {} 90 | 91 | } 92 | } 93 | }); 94 | } 95 | 96 | public class XBeeSocketOutputStream extends OutputStream { 97 | 98 | @Override 99 | public void write(int i) throws IOException { 100 | try { 101 | out.write(i); 102 | } catch (IOException e) { 103 | log.warn("Failed to write byte " + i + " to output stream. closing socket. error: " + e.toString() + ", socket " + socketStatus()); 104 | tryClose(); 105 | throw e; 106 | } 107 | } 108 | } 109 | 110 | private String socketStatus() { 111 | return "isConnected: " + socket.isConnected() + ", isBound: " + socket.isBound() + ", isClosed: " + socket.isClosed(); 112 | } 113 | 114 | private void tryClose() { 115 | try { 116 | log.info("Closing socket"); 117 | close(); 118 | } catch (Exception e) { 119 | log.warn("Failed to close socket " + e.toString()); 120 | } 121 | } 122 | 123 | @Override 124 | public void close() throws IOException { 125 | socket.close(); 126 | executorService.shutdownNow(); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/ZNetExplicitSenderExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.XBee; 26 | import com.rapplogic.xbee.api.XBeeAddress16; 27 | import com.rapplogic.xbee.api.XBeeAddress64; 28 | import com.rapplogic.xbee.api.XBeeException; 29 | import com.rapplogic.xbee.api.XBeeResponse; 30 | import com.rapplogic.xbee.api.zigbee.ZNetExplicitTxRequest; 31 | import com.rapplogic.xbee.api.zigbee.ZNetTxRequest; 32 | import com.rapplogic.xbee.util.DoubleByte; 33 | 34 | /** 35 | * Set AO=1 for to enable explicit frames for this example. 36 | * Once set, you should use explicit tx/rx packets instead of plain vanilla tx requests (ZNetTxRequest). 37 | * You can still send ZNetTxRequest requests but they will be received as explicit responses (ZNetExplicitRxResponse) 38 | * 39 | * @author andrew 40 | * 41 | */ 42 | public class ZNetExplicitSenderExample { 43 | 44 | private final static Logger log = Logger.getLogger(ZNetExplicitSenderExample.class); 45 | 46 | private ZNetExplicitSenderExample() throws XBeeException { 47 | 48 | XBee xbee = new XBee(); 49 | 50 | try { 51 | // replace with your com port and baud rate. this is the com port of my coordinator 52 | //xbee.open("COM5", 9600); 53 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 54 | 55 | // replace with end device's 64-bit address (SH + SL) 56 | XBeeAddress64 addr64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x0a, 0x3e, 0x02); 57 | 58 | // create an array of arbitrary data to send 59 | int[] payload = new int[] { 0, 0x66, 0xee }; 60 | 61 | // loopback test 62 | int sourceEndpoint = 0; 63 | int destinationEndpoint = ZNetExplicitTxRequest.Endpoint.DATA.getValue(); 64 | 65 | DoubleByte clusterId = new DoubleByte(0x0, ZNetExplicitTxRequest.ClusterId.SERIAL_LOOPBACK.getValue()); 66 | //DoubleByte clusterId = new DoubleByte(0x0, ZNetExplicitTxRequest.ClusterId.TRANSPARENT_SERIAL.getValue()); 67 | 68 | // first request we just send 64-bit address. we get 16-bit network address with status response 69 | ZNetExplicitTxRequest request = new ZNetExplicitTxRequest(0xff, addr64, XBeeAddress16.ZNET_BROADCAST, 70 | ZNetTxRequest.DEFAULT_BROADCAST_RADIUS, ZNetTxRequest.Option.UNICAST, payload, sourceEndpoint, destinationEndpoint, clusterId, ZNetExplicitTxRequest.znetProfileId); 71 | 72 | log.info("sending explicit " + request.toString()); 73 | 74 | while (true) { 75 | xbee.sendAsynchronous(request); 76 | 77 | XBeeResponse response = xbee.getResponse(); 78 | 79 | log.info("received response " + response.toString()); 80 | 81 | try { 82 | // wait a bit then send another packet 83 | Thread.sleep(5000); 84 | } catch (InterruptedException e) { 85 | } 86 | } 87 | } finally { 88 | if (xbee != null && xbee.isConnected()) { 89 | xbee.close(); 90 | } 91 | } 92 | } 93 | 94 | public static void main(String[] args) throws XBeeException, InterruptedException { 95 | PropertyConfigurator.configure("log4j.properties"); 96 | new ZNetExplicitSenderExample(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /docs/api/com/rapplogic/xbee/api/zigbee/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.rapplogic.xbee.api.zigbee 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | com.rapplogic.xbee.api.zigbee 18 | 19 | 20 | 43 | 44 |
21 | Classes  22 | 23 |
24 | NodeDiscover 25 |
26 | ZBForceSampleRequest 27 |
28 | ZNetExplicitRxResponse 29 |
30 | ZNetExplicitTxRequest 31 |
32 | ZNetNodeIdentificationResponse 33 |
34 | ZNetRxBaseResponse 35 |
36 | ZNetRxIoSampleResponse 37 |
38 | ZNetRxResponse 39 |
40 | ZNetTxRequest 41 |
42 | ZNetTxStatusResponse
45 | 46 | 47 | 48 | 49 | 74 | 75 |
50 | Enums  51 | 52 |
53 | AssociationStatus 54 |
55 | NodeDiscover.DeviceType 56 |
57 | ZNetExplicitTxRequest.ClusterId 58 |
59 | ZNetExplicitTxRequest.Endpoint 60 |
61 | ZNetNodeIdentificationResponse.DeviceType 62 |
63 | ZNetNodeIdentificationResponse.Option 64 |
65 | ZNetNodeIdentificationResponse.SourceAction 66 |
67 | ZNetRxBaseResponse.Option 68 |
69 | ZNetTxRequest.Option 70 |
71 | ZNetTxStatusResponse.DeliveryStatus 72 |
73 | ZNetTxStatusResponse.DiscoveryStatus
76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/zigbee/ZNetReceiverExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.zigbee; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.ApiId; 26 | import com.rapplogic.xbee.api.AtCommand; 27 | import com.rapplogic.xbee.api.AtCommandResponse; 28 | import com.rapplogic.xbee.api.XBee; 29 | import com.rapplogic.xbee.api.XBeeResponse; 30 | import com.rapplogic.xbee.api.zigbee.ZNetRxResponse; 31 | import com.rapplogic.xbee.util.ByteUtils; 32 | 33 | /** 34 | * This class is the companion to ZNetSenderTest.java, and as such, it receives packets sent by ZNetSenderTest.java 35 | * See the ZNetSenderTest.java for information on how to configure your XBee for this demo 36 | * 37 | * You can start ZNetSenderTest.java and this class in any order but it's generally best to start this class first. 38 | * 39 | * @author andrew 40 | * 41 | */ 42 | public class ZNetReceiverExample { 43 | 44 | private final static Logger log = Logger.getLogger(ZNetReceiverExample.class); 45 | 46 | private ZNetReceiverExample() throws Exception { 47 | XBee xbee = new XBee(); 48 | 49 | try { 50 | // replace with the com port of your receiving XBee (typically your end device) 51 | // router 52 | xbee.open("/dev/tty.usbserial-A6005uPi", 9600); 53 | 54 | while (true) { 55 | 56 | try { 57 | // we wait here until a packet is received. 58 | XBeeResponse response = xbee.getResponse(); 59 | 60 | log.info("received response " + response.toString()); 61 | 62 | if (response.getApiId() == ApiId.ZNET_RX_RESPONSE) { 63 | // we received a packet from ZNetSenderTest.java 64 | ZNetRxResponse rx = (ZNetRxResponse) response; 65 | 66 | log.info("Received RX packet, option is " + rx.getOption() + ", sender 64 address is " + ByteUtils.toBase16(rx.getRemoteAddress64().getAddress()) + ", remote 16-bit address is " + ByteUtils.toBase16(rx.getRemoteAddress16().getAddress()) + ", data is " + ByteUtils.toBase16(rx.getData())); 67 | 68 | // optionally we may want to get the signal strength (RSSI) of the last hop. 69 | // keep in mind if you have routers in your network, this will be the signal of the last hop. 70 | AtCommand at = new AtCommand("DB"); 71 | xbee.sendAsynchronous(at); 72 | XBeeResponse atResponse = xbee.getResponse(); 73 | 74 | if (atResponse.getApiId() == ApiId.AT_RESPONSE) { 75 | // remember rssi is a negative db value 76 | log.info("RSSI of last response is " + -((AtCommandResponse)atResponse).getValue()[0]); 77 | } else { 78 | // we didn't get an AT response 79 | log.info("expected RSSI, but received " + atResponse.toString()); 80 | } 81 | } else { 82 | log.debug("received unexpected packet " + response.toString()); 83 | } 84 | } catch (Exception e) { 85 | log.error(e); 86 | } 87 | } 88 | } finally { 89 | if (xbee != null && xbee.isConnected()) { 90 | xbee.close(); 91 | } 92 | } 93 | } 94 | 95 | public static void main(String[] args) throws Exception { 96 | // init log4j 97 | PropertyConfigurator.configure("log4j.properties"); 98 | new ZNetReceiverExample(); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/wpan/WpanNodeDiscoverExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.wpan; 21 | 22 | import java.util.List; 23 | 24 | import org.apache.log4j.Logger; 25 | import org.apache.log4j.PropertyConfigurator; 26 | 27 | import com.rapplogic.xbee.api.AtCommand; 28 | import com.rapplogic.xbee.api.AtCommandResponse; 29 | import com.rapplogic.xbee.api.CollectTerminator; 30 | import com.rapplogic.xbee.api.XBee; 31 | import com.rapplogic.xbee.api.XBeeException; 32 | import com.rapplogic.xbee.api.XBeeResponse; 33 | import com.rapplogic.xbee.api.wpan.WpanNodeDiscover; 34 | import com.rapplogic.xbee.util.ByteUtils; 35 | 36 | /** 37 | * Example of performing a node discover for Series 1 XBees. 38 | * You must connect to the coordinator to run this example and 39 | * have one or more end devices that are associated. 40 | * 41 | * @author andrew 42 | * 43 | */ 44 | public class WpanNodeDiscoverExample { 45 | 46 | private final static Logger log = Logger.getLogger(WpanNodeDiscoverExample.class); 47 | 48 | private XBee xbee = new XBee(); 49 | 50 | public WpanNodeDiscoverExample() throws XBeeException, InterruptedException { 51 | 52 | try { 53 | // my coordinator com/baud 54 | xbee.open("/dev/tty.usbserial-A4004Rim", 9600); 55 | 56 | // get the Node discovery timeout 57 | xbee.sendAsynchronous(new AtCommand("NT")); 58 | AtCommandResponse nodeTimeout = (AtCommandResponse) xbee.getResponse(); 59 | 60 | // default is 2.5 seconds for series 1 61 | int nodeDiscoveryTimeout = ByteUtils.convertMultiByteToInt(nodeTimeout.getValue()) * 100; 62 | log.info("Node discovery timeout is " + nodeDiscoveryTimeout + " milliseconds"); 63 | 64 | xbee.sendAsynchronous(new AtCommand("ND")); 65 | 66 | // collect responses up to the timeout or until the terminating response is received, whichever occurs first 67 | List responses = xbee.collectResponses(10000, new CollectTerminator() { 68 | public boolean stop(XBeeResponse response) { 69 | if (response instanceof AtCommandResponse) { 70 | AtCommandResponse at = (AtCommandResponse) response; 71 | if (at.getCommand().equals("ND") && at.getValue() != null && at.getValue().length == 0) { 72 | log.debug("Found terminating response"); 73 | return true; 74 | } 75 | } 76 | return false; 77 | } 78 | }); 79 | 80 | //TODO check for terminating node 81 | 82 | log.info("Time is up! You should have heard back from all nodes by now. If not make sure all nodes are associated and/or try increasing the node timeout (NT)"); 83 | 84 | for (XBeeResponse response : responses) { 85 | if (response instanceof AtCommandResponse) { 86 | AtCommandResponse atResponse = (AtCommandResponse) response; 87 | 88 | if (atResponse.getCommand().equals("ND") && atResponse.getValue() != null && atResponse.getValue().length > 0) { 89 | WpanNodeDiscover nd = WpanNodeDiscover.parse((AtCommandResponse)response); 90 | log.info("Node Discover is " + nd); 91 | } 92 | } 93 | } 94 | } finally { 95 | if (xbee != null && xbee.isConnected()) { 96 | xbee.close(); 97 | } 98 | } 99 | } 100 | 101 | public static void main(String[] args) throws XBeeException, InterruptedException { 102 | PropertyConfigurator.configure("log4j.properties"); 103 | new WpanNodeDiscoverExample(); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/wpan/TxRequest64.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.wpan; 21 | 22 | import com.rapplogic.xbee.api.ApiId; 23 | import com.rapplogic.xbee.api.XBeeAddress64; 24 | import com.rapplogic.xbee.util.IntArrayOutputStream; 25 | 26 | // TODO test setting DH/DL to 0 and SH/SL 27 | 28 | /** 29 | * Series 1 XBee. 64-bit address Transmit Packet. This is received on the destination XBee 30 | * radio as a RxResponse64 response 31 | *

32 | * Constructs frame data portion of a 64-bit transmit request 33 | *

34 | * Note: The MY address of the receiving XBee must be set to 0xffff to receive this as a RxResponse64; 35 | * otherwise the packet will be transmitted but will be received as a RxResponse16 36 | *

37 | * API ID: 0x0 38 | *

39 | * @author andrew 40 | * 41 | */ 42 | public class TxRequest64 extends TxRequestBase { 43 | 44 | private XBeeAddress64 remoteAddr64; 45 | 46 | /** 47 | * 16 bit Tx Request with default frame id and awk option 48 | * 49 | * @param destinationAddress 50 | * @param payload 51 | */ 52 | public TxRequest64(XBeeAddress64 destination, int[] payload) { 53 | this(destination, DEFAULT_FRAME_ID, Option.UNICAST, payload); 54 | } 55 | 56 | /** 57 | * 16 bit Tx Request. 58 | * 59 | * Keep in mind that if you programmed the destination address with AT commands, it is in Hex, 60 | * so prepend int with 0x (e.g. 0x1234). 61 | * 62 | * Payload size is limited to 100 bytes, according to MaxStream documentation. 63 | * 64 | * @param destinationAddress 65 | * @param awkFrameId 66 | * @param payload 67 | */ 68 | public TxRequest64(XBeeAddress64 destination, int frameId, int[] payload) { 69 | this(destination, frameId, Option.UNICAST, payload); 70 | } 71 | 72 | /** 73 | * Note: if option is DISABLE_ACK_OPTION you will not get a ack response and you must use the asynchronous send method 74 | * 75 | * @param destinationAddress 76 | * @param awkFrameId 77 | * @param payload 78 | * @param option 79 | */ 80 | public TxRequest64(XBeeAddress64 remoteAddr64, int frameId, Option option, int[] payload) { 81 | this.remoteAddr64 = remoteAddr64; 82 | this.setFrameId(frameId); 83 | this.setOption(option); 84 | this.setPayload(payload); 85 | } 86 | 87 | public int[] getFrameData() { 88 | 89 | // 3/6/10 fixed bug -- broadcast address is used with broadcast option, not no ACK 90 | 91 | IntArrayOutputStream out = new IntArrayOutputStream(); 92 | 93 | // api id 94 | out.write(this.getApiId().getValue()); 95 | // frame id (arbitrary byte that will be sent back with ack) 96 | out.write(this.getFrameId()); 97 | // destination high (broadcast is 0xFFFF) 98 | 99 | // add 64-bit dest address 100 | out.write(remoteAddr64.getAddress()); 101 | 102 | // options byte disable ack = 1, send pan id = 4 103 | out.write(this.getOption().getValue()); 104 | out.write(this.getPayload()); 105 | 106 | return out.getIntArray(); 107 | } 108 | 109 | public ApiId getApiId() { 110 | return ApiId.TX_REQUEST_64; 111 | } 112 | 113 | public XBeeAddress64 getRemoteAddr64() { 114 | return remoteAddr64; 115 | } 116 | 117 | public void setRemoteAddr64(XBeeAddress64 remoteAddr64) { 118 | this.remoteAddr64 = remoteAddr64; 119 | } 120 | 121 | public String toString() { 122 | return super.toString() + 123 | ",remoteAddress64=" + this.remoteAddr64.toString(); 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/RemoteAtExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.RemoteAtRequest; 26 | import com.rapplogic.xbee.api.RemoteAtResponse; 27 | import com.rapplogic.xbee.api.XBee; 28 | import com.rapplogic.xbee.api.XBeeAddress64; 29 | import com.rapplogic.xbee.api.XBeeException; 30 | import com.rapplogic.xbee.api.XBeeTimeoutException; 31 | 32 | /** 33 | * This example uses Remote AT to turn on/off I/O pins. 34 | * This example is more interesting if you connect a LED to pin 20 on your end device. 35 | * Remember to use a resistor to limit the current flow. I used a 215 Ohm resistor. 36 | *

37 | * Note: if your coordinator is powered on and receiving I/O samples, make sure you power off/on to drain 38 | * the traffic before running this example. 39 | * 40 | * @author andrew 41 | * 42 | */ 43 | public class RemoteAtExample { 44 | 45 | private final static Logger log = Logger.getLogger(RemoteAtExample.class); 46 | 47 | private RemoteAtExample() throws XBeeException, InterruptedException { 48 | 49 | XBee xbee = new XBee(); 50 | 51 | try { 52 | // replace with your coordinator com/baud 53 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 54 | // xbee.open("COM5", 9600); 55 | 56 | // replace with SH + SL of your end device 57 | XBeeAddress64 addr64 = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x0a, 0x3e, 0x02); 58 | 59 | // turn on end device (pin 20) D0 (Digital output high = 5) 60 | //RemoteAtRequest request = new RemoteAtRequest(addr64, "D0", new int[] {5}); 61 | //RemoteAtRequest request = new RemoteAtRequest(addr64, "IR", new int[] {0x7f, 0xff}); 62 | //RemoteAtRequest request = new RemoteAtRequest(addr64, "D5", new int[] {3}); 63 | //RemoteAtRequest request = new RemoteAtRequest(addr64, "D0", new int[] {2}); 64 | //RemoteAtRequest request = new RemoteAtRequest(addr64, "P2", new int[] {3}); 65 | RemoteAtRequest request = new RemoteAtRequest(addr64, "P0", new int[] {1}); 66 | 67 | RemoteAtResponse response = (RemoteAtResponse) xbee.sendSynchronous(request, 10000); 68 | 69 | if (response.isOk()) { 70 | log.info("successfully turned on pin 20 (D0)"); 71 | } else { 72 | throw new RuntimeException("failed to turn on pin 20. status is " + response.getStatus()); 73 | } 74 | 75 | System.exit(0); 76 | 77 | // wait a bit 78 | Thread.sleep(5000); 79 | // 80 | // // now turn off end device D0 81 | request.setValue(new int[] {4}); 82 | 83 | response = (RemoteAtResponse) xbee.sendSynchronous(request, 10000); 84 | 85 | if (response.isOk()) { 86 | log.info("successfully turned off pin 20 (D0)"); 87 | } else { 88 | throw new RuntimeException("failed to turn off pin 20. status is " + response.getStatus()); 89 | } 90 | 91 | } catch (XBeeTimeoutException e) { 92 | log.error("request timed out. make sure you remote XBee is configured and powered on"); 93 | } catch (Exception e) { 94 | log.error("unexpected error", e); 95 | } finally { 96 | if (xbee != null && xbee.isConnected()) { 97 | xbee.close(); 98 | } 99 | } 100 | } 101 | 102 | public static void main(String[] args) throws XBeeException, InterruptedException { 103 | PropertyConfigurator.configure("log4j.properties"); 104 | new RemoteAtExample(); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/AtCommandResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import java.io.IOException; 23 | import java.util.EnumSet; 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | import com.rapplogic.xbee.util.ByteUtils; 28 | 29 | /** 30 | * Sent in response to an AtCommand 31 | *

32 | * API ID: 0x88 33 | *

34 | * @author andrew 35 | * 36 | */ 37 | public class AtCommandResponse extends XBeeFrameIdResponse { 38 | 39 | public enum Status { 40 | // 0 = OK 41 | // 1 = ERROR 42 | // 2 = Invalid Command 43 | // 3 = Invalid Parameter 44 | // 4 = Remote Command Transmission Failed 45 | 46 | OK (0), 47 | ERROR (1), 48 | INVALID_COMMAND (2), 49 | INVALID_PARAMETER (3), 50 | NO_RESPONSE (4); // series 1 remote AT only according to spec. also series 2 in 2x64 zb pro firmware 51 | 52 | private static final Map lookup = new HashMap(); 53 | 54 | static { 55 | for(Status s : EnumSet.allOf(Status.class)) { 56 | lookup.put(s.getValue(), s); 57 | } 58 | } 59 | 60 | public static Status get(int value) { 61 | return lookup.get(value); 62 | } 63 | 64 | private final int value; 65 | 66 | Status(int value) { 67 | this.value = value; 68 | } 69 | 70 | public int getValue() { 71 | return value; 72 | } 73 | } 74 | 75 | private int char1; 76 | private int char2; 77 | private Status status; 78 | // response value msb to lsb 79 | private int[] value; 80 | 81 | public AtCommandResponse() { 82 | 83 | } 84 | 85 | public int getChar1() { 86 | return char1; 87 | } 88 | 89 | 90 | public void setChar1(int char1) { 91 | this.char1 = char1; 92 | } 93 | 94 | 95 | public int getChar2() { 96 | return char2; 97 | } 98 | 99 | 100 | public void setChar2(int char2) { 101 | this.char2 = char2; 102 | } 103 | 104 | public Status getStatus() { 105 | return status; 106 | } 107 | 108 | public void setStatus(Status status) { 109 | this.status = status; 110 | } 111 | 112 | public boolean isOk() { 113 | return status == Status.OK; 114 | } 115 | 116 | // TODO should return null if not specified 117 | /** 118 | * Returns the command data byte array. 119 | * A zero length array will be returned if the command data is not specified. 120 | * This is the case if the at command set a value, or executed a command that does 121 | * not have a value (like FR) 122 | * 123 | * @return 124 | */ 125 | public int[] getValue() { 126 | return value; 127 | } 128 | 129 | public void setValue(int[] data) { 130 | this.value = data; 131 | } 132 | 133 | public String getCommand() { 134 | return String.valueOf((char)this.char1) + String.valueOf((char)this.char2); 135 | } 136 | 137 | public void parse(IPacketParser parser) throws IOException { 138 | this.setFrameId(parser.read("AT Response Frame Id")); 139 | this.setChar1(parser.read("AT Response Char 1")); 140 | this.setChar2(parser.read("AT Response Char 2")); 141 | this.setStatus(Status.get(parser.read("AT Response Status"))); 142 | 143 | this.setValue(parser.readRemainingBytes()); 144 | } 145 | 146 | public String toString() { 147 | return "command=" + this.getCommand() + 148 | ",status=" + this.getStatus() + ",value=" + 149 | (this.value == null ? "null" : ByteUtils.toBase16(this.getValue())) + 150 | "," + 151 | super.toString(); 152 | } 153 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/AtCommand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api; 21 | 22 | import com.rapplogic.xbee.util.ByteUtils; 23 | import com.rapplogic.xbee.util.IntArrayOutputStream; 24 | 25 | /** 26 | * API technique to set/query commands 27 | *

28 | * WARNING: Any changes made will not survive a power cycle unless written to memory with WR command 29 | * According to the manual, the WR command can only be written so many times.. however many that is. 30 | *

31 | * API ID: 0x8 32 | *

33 | * Determining radio type with HV:
34 | * Byte 1, Part Number
35 | * x17, XB24 (series 1)
36 | * x18, XBP24 (series 1)
37 | * x19, XB24-B (series 2)
38 | * x1A, XBP24-B (series 2)
39 | *

40 | * XB24-ZB
41 | * XBP24-ZB
42 | * @author andrew 43 | */ 44 | public class AtCommand extends XBeeRequest { 45 | 46 | private String command; 47 | private int[] value; 48 | 49 | // // common i/o pin settings. it is up to the developer to ensure the setting is applicable to the pin (e.g. not all pins support analog input) 50 | // public enum IoSetting { 51 | // DISABLED (new int[] {0x0}), 52 | // ANALOG_INPUT (new int[] {0x2}), 53 | // DIGITAL_INPUT (new int[] {0x3}), 54 | // DIGITAL_OUTPUT_LOW (new int[] {0x4}), 55 | // DIGITAL_OUTPUT_HIGH (new int[] {0x5}); 56 | // 57 | // private final int[] value; 58 | // 59 | // IoSetting(int[] value) { 60 | // this.value = value; 61 | // } 62 | // } 63 | 64 | public AtCommand(String command) { 65 | this(command, null, DEFAULT_FRAME_ID); 66 | } 67 | 68 | public AtCommand(String command, int value) { 69 | this(command, new int[] {value}, DEFAULT_FRAME_ID); 70 | } 71 | 72 | public AtCommand(String command, int value[]) { 73 | this(command, value, DEFAULT_FRAME_ID); 74 | } 75 | 76 | /** 77 | * Warning: frameId must be > 0 for a response 78 | * 79 | * @param command 80 | * @param value 81 | * @param frameId 82 | */ 83 | public AtCommand(String command, int[] value, int frameId) { 84 | this.command = command; 85 | this.value = value; 86 | this.setFrameId(frameId); 87 | } 88 | 89 | public int[] getFrameData() { 90 | if (command.length() > 2) { 91 | throw new IllegalArgumentException("Command should be two characters. Do not include AT prefix"); 92 | } 93 | 94 | IntArrayOutputStream out = new IntArrayOutputStream(); 95 | 96 | // api id 97 | out.write(this.getApiId().getValue()); 98 | // frame id 99 | out.write(this.getFrameId()); 100 | // at command byte 1 101 | out.write((int) command.substring(0, 1).toCharArray()[0]); 102 | // at command byte 2 103 | out.write((int) command.substring(1, 2).toCharArray()[0]); 104 | 105 | // int value is up to four bytes to represent command value 106 | if (value != null) { 107 | out.write(value); 108 | } 109 | 110 | return out.getIntArray(); 111 | } 112 | 113 | public ApiId getApiId() { 114 | return ApiId.AT_COMMAND; 115 | } 116 | 117 | public String getCommand() { 118 | return command; 119 | } 120 | 121 | public void setCommand(String command) { 122 | this.command = command; 123 | } 124 | 125 | public int[] getValue() { 126 | return value; 127 | } 128 | 129 | public void setValue(int[] value) { 130 | this.value = value; 131 | } 132 | 133 | public String toString() { 134 | return super.toString() + 135 | ",command=" + this.command + 136 | ",value=" + (value == null ? "null" : ByteUtils.toBase16(value)); 137 | } 138 | } -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/wpan/ApiSenderExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.wpan; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.ApiId; 26 | import com.rapplogic.xbee.api.XBee; 27 | import com.rapplogic.xbee.api.XBeeAddress16; 28 | import com.rapplogic.xbee.api.XBeePacket; 29 | import com.rapplogic.xbee.api.XBeeResponse; 30 | import com.rapplogic.xbee.api.wpan.TxRequest16; 31 | import com.rapplogic.xbee.api.wpan.TxRequest64; 32 | import com.rapplogic.xbee.api.wpan.TxStatusResponse; 33 | 34 | /** 35 | * Sends a TX Request every 5000 ms and waits for TX status packet. 36 | * If the radio is sending samples it will continue to wait for tx status. 37 | * 38 | * @author andrew 39 | * 40 | */ 41 | public class ApiSenderExample { 42 | 43 | private final static Logger log = Logger.getLogger(ApiSenderExample.class); 44 | 45 | private ApiSenderExample() throws Exception { 46 | 47 | XBee xbee = new XBee(); 48 | 49 | final int sleep = 5000; 50 | 51 | int count = 0; 52 | int errors = 0; 53 | int ackErrors = 0; 54 | int ccaErrors = 0; 55 | int purgeErrors = 0; 56 | 57 | long now; 58 | 59 | try { 60 | // replace with port and baud rate of your XBee 61 | xbee.open("/dev/tty.usbserial-A6005uPi", 9600); 62 | 63 | while (true) { 64 | 65 | // log.debug("Sending count " + count); 66 | // XBeeResponse response = xbee.sendTxRequest16(destination, 0x0a, payload); 67 | 68 | // int[] payload = new int[] {1,2,3,4,5,6,7,8}; 69 | // to verify correct byte escaping, we'll send a start byte 70 | int[] payload = new int[] { XBeePacket.SpecialByte.START_BYTE.getValue() }; 71 | 72 | // specify the remote XBee 16-bit MY address 73 | XBeeAddress16 destination = new XBeeAddress16(0x18, 0x74); 74 | TxRequest16 tx = new TxRequest16(destination, payload); 75 | // or send a TX64 (same thing except we are addressing by SH+SL address) 76 | // XBeeAddress64 destination = new XBeeAddress64(0, 0x13, 0xa2, 0, 0x40, 0x08, 0xb4, 0x8f); 77 | // TxRequest64 tx2 = new TxRequest64(destination64, payload); 78 | 79 | now = System.currentTimeMillis(); 80 | xbee.sendAsynchronous(tx); 81 | 82 | XBeeResponse response = null; 83 | 84 | while (true) { 85 | // blocks until we get response 86 | response = xbee.getResponse(); 87 | 88 | if (response.getApiId() != ApiId.TX_STATUS_RESPONSE) { 89 | log.debug("expected tx status but received " + response); 90 | } else { 91 | // log.debug("got tx status"); 92 | 93 | if (((TxStatusResponse) response).getFrameId() != tx.getFrameId()) { 94 | throw new RuntimeException("frame id does not match"); 95 | } 96 | 97 | if (((TxStatusResponse) response).getStatus() != TxStatusResponse.Status.SUCCESS) { 98 | errors++; 99 | 100 | if (((TxStatusResponse) response).isAckError()) { 101 | ackErrors++; 102 | } else if (((TxStatusResponse) response).isCcaError()) { 103 | ccaErrors++; 104 | } else if (((TxStatusResponse) response).isPurged()) { 105 | purgeErrors++; 106 | } 107 | 108 | log.debug("Tx status failure with status: " + ((TxStatusResponse) response).getStatus()); 109 | } else { 110 | // success 111 | log.debug("Success. count is " + count + ", errors is " + errors + ", in " + (System.currentTimeMillis() - now) + ", ack errors " 112 | + ackErrors + ", ccaErrors " + ccaErrors + ", purge errors " + purgeErrors); 113 | } 114 | 115 | count++; 116 | 117 | break; 118 | } 119 | } 120 | 121 | Thread.sleep(sleep); 122 | } 123 | } finally { 124 | if (xbee != null && xbee.isConnected()) { 125 | xbee.close(); 126 | } 127 | } 128 | } 129 | 130 | public static void main(String[] args) throws Exception { 131 | // init log4j 132 | PropertyConfigurator.configure("log4j.properties"); 133 | new ApiSenderExample(); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/examples/wpan/IoSamplesExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.examples.wpan; 21 | 22 | import org.apache.log4j.Logger; 23 | import org.apache.log4j.PropertyConfigurator; 24 | 25 | import com.rapplogic.xbee.api.ApiId; 26 | import com.rapplogic.xbee.api.ErrorResponse; 27 | import com.rapplogic.xbee.api.XBee; 28 | import com.rapplogic.xbee.api.XBeeResponse; 29 | import com.rapplogic.xbee.api.wpan.IoSample; 30 | import com.rapplogic.xbee.api.wpan.RxResponseIoSample; 31 | import com.rapplogic.xbee.util.ByteUtils; 32 | 33 | /** 34 | * Series 1 XBee -- receive IO samples from remote radio 35 | * In this example we are going to set pin 20 to analog input, pins 11 and 12 to digital input, and configure change detect for pin 12. 36 | * Change detect sends a sample whenever the remote XBee detects a change from or to ground; 37 | * it does not detect the transition from open circuit to high or vice versa. 38 | * 39 | * Configuration: 40 | * 41 | * This assumes we are starting with the factory default settings and the XBee is in API mode (AP = 2) 42 | * 43 | * Remote radio: 44 | * 45 | * MY = 1 necessary? 46 | * sets pin 20 to analog input 47 | * D0 = 2 48 | * sets pin 11 to digital input 49 | * D4 = 3 50 | * sets pin 12 to digital input for change detect 51 | * D7 = 3 52 | * turn on change detect for D7. we need a bit mask of 10000000 (if you can't convert base 2 to hex in your head either, it's 0x80) 53 | * why 10000000? we place a 1 in the position of each input we want to enable, so if you want to enable for D4 and D7 it would be 10010000 54 | * IC = 80 55 | * we want 1 sample per packet 56 | * IT = 1 57 | * receive samples every 5 seconds 58 | * IR = 1388 59 | * (coordinator address) necessary? doesn't it always send to coordinator? 60 | * DL = 0 61 | * (save configuration) 62 | * WR 63 | * 64 | * Coordinator: 65 | * 66 | * (set as coordinator) 67 | * CE = 1 68 | * (set address) 69 | * MY = 0 70 | * (end device) necessary? 71 | * DL = 1 72 | * 73 | * 74 | * @author andrew 75 | * 76 | */ 77 | public class IoSamplesExample { 78 | 79 | private final static Logger log = Logger.getLogger(IoSamplesExample.class); 80 | 81 | private IoSamplesExample() throws Exception { 82 | XBee xbee = new XBee(); 83 | 84 | try { 85 | xbee.open("/dev/tty.usbserial-A6005v5M", 9600); 86 | 87 | while (true) { 88 | 89 | try { 90 | XBeeResponse response = xbee.getResponse(); 91 | 92 | log.info("Received i/o response: " + response); 93 | log.info("packet bytes is " + ByteUtils.toBase16(response.getPacketBytes())); 94 | 95 | if (response.isError()) { 96 | log.info("response contains errors", ((ErrorResponse)response).getException()); 97 | continue; 98 | } 99 | 100 | if (response.getApiId() == ApiId.RX_16_IO_RESPONSE) { 101 | RxResponseIoSample ioSample = (RxResponseIoSample)response; 102 | 103 | log.info("Received I/O sample from " + ioSample.getSourceAddress()); 104 | // optionally output the rssi strength 105 | //log.info("rssi is " + ioSample.getRssi()); 106 | 107 | // loops just once since IT = 1 108 | for (IoSample sample: ioSample.getSamples()) { 109 | if (ioSample.containsAnalog()) { 110 | log.info("Analog pin 20 10-bit reading is " + sample.getAnalog0()); 111 | log.info("Digital pin 11 is " + (sample.isD4On() ? "on" : "off")); 112 | log.info("Digital pin 12 is " + (sample.isD7On() ? "on" : "off")); 113 | } else { 114 | // we know it's change detect since analog was not sent 115 | log.info("Received change detect for Digital pin 12: " + (sample.isD7On() ? "on" : "off")); 116 | } 117 | } 118 | } else { 119 | // not what we expected 120 | log.info("Ignoring mystery packet " + response.toString()); 121 | } 122 | } catch (Exception e) { 123 | log.error(e); 124 | } 125 | } 126 | } finally { 127 | if (xbee != null && xbee.isConnected()) { 128 | xbee.close(); 129 | } 130 | } 131 | } 132 | 133 | public static void main(String[] args) throws Exception { 134 | // init log4j 135 | PropertyConfigurator.configure("log4j.properties"); 136 | new IoSamplesExample(); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/zigbee/ZNetRxBaseResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.zigbee; 21 | 22 | import java.io.IOException; 23 | import java.util.EnumSet; 24 | import java.util.HashMap; 25 | import java.util.Map; 26 | 27 | import org.apache.log4j.Logger; 28 | 29 | import com.rapplogic.xbee.api.IPacketParser; 30 | import com.rapplogic.xbee.api.XBeeAddress16; 31 | import com.rapplogic.xbee.api.XBeeAddress64; 32 | import com.rapplogic.xbee.api.XBeeResponse; 33 | 34 | /** 35 | * Series 2 XBee. Super class of all Receive packets. 36 | *

37 | * Note: ZNet RX packets do not include RSSI since it is a mesh network and potentially requires several 38 | * hops to get to the destination. The RSSI of the last hop is available using the DB AT command. 39 | * If your network is not mesh (i.e. composed of a single coordinator and end devices -- no routers) 40 | * then the DB command should provide accurate RSSI. 41 | *

42 | * @author Andrew Rapp 43 | * 44 | */ 45 | public abstract class ZNetRxBaseResponse extends XBeeResponse { 46 | 47 | private final static Logger log = Logger.getLogger(ZNetRxBaseResponse.class); 48 | 49 | public enum Option { 50 | // 0x01 - Packet Acknowledged 51 | // 0x02 - Packet was a broadcast packet 52 | // 0x20 - Packet encrypted with APS encryption 53 | // 0x40 - Packet was sent from an end device (if known) 54 | // Note: Option values can be combined. For example, a 55 | // 0x40 and a 0x01 will show as a 0x41. Other possible 56 | // values 0x21, 0x22, 0x41, 0x42, 0x60, 0x61, 0x62. 57 | 58 | // TODO ugh this is mess now with bitfield indicators 59 | // TODO ditch the enum, and replace with a class that has isBroadcast(), isPacketAcknowledged() etc 60 | 61 | PACKET_ACKNOWLEDGED (0x01), 62 | BROADCAST_PACKET (0x02), 63 | PACKET_ENCRYPTED_WITH_APS (0x20), 64 | PACKET_SENT_FROM_END_DEVICE(0x40), 65 | PACKET_ACKNOWLEDGED_0x21 (0x21), 66 | PACKET_ACKNOWLEDGED_0x41 (0x41), 67 | PACKET_ACKNOWLEDGED_0x61 (0x61), 68 | PACKET_ENCRYPTED_WITH_APS_PACKET_SENT_FROM_END_DEVICE (0x60), 69 | BROADCAST_PACKET_0x22 (0x22), 70 | BROADCAST_PACKET_0x42 (0x42), 71 | BROADCAST_PACKET_0x62 (0x62), 72 | UNKNOWN(-1); 73 | 74 | private static final Map lookup = new HashMap(); 75 | 76 | static { 77 | for(Option s : EnumSet.allOf(Option.class)) { 78 | lookup.put(s.getValue(), s); 79 | } 80 | } 81 | 82 | public static Option get(int value) { 83 | return lookup.get(value); 84 | } 85 | 86 | private final int value; 87 | 88 | Option(int value) { 89 | this.value = value; 90 | } 91 | 92 | public int getValue() { 93 | return value; 94 | } 95 | } 96 | 97 | // TODO where is frame id?? 98 | 99 | private XBeeAddress64 remoteAddress64; 100 | private XBeeAddress16 remoteAddress16; 101 | private Option option; 102 | 103 | public ZNetRxBaseResponse() { 104 | 105 | } 106 | 107 | public XBeeAddress64 getRemoteAddress64() { 108 | return remoteAddress64; 109 | } 110 | 111 | public void setRemoteAddress64(XBeeAddress64 remoteAddress64) { 112 | this.remoteAddress64 = remoteAddress64; 113 | } 114 | 115 | public XBeeAddress16 getRemoteAddress16() { 116 | return remoteAddress16; 117 | } 118 | 119 | public void setRemoteAddress16(XBeeAddress16 remoteAddress16) { 120 | this.remoteAddress16 = remoteAddress16; 121 | } 122 | 123 | public Option getOption() { 124 | return option; 125 | } 126 | 127 | public void setOption(Option option) { 128 | this.option = option; 129 | } 130 | 131 | protected void parseAddress(IPacketParser parser) throws IOException { 132 | this.setRemoteAddress64(parser.parseAddress64()); 133 | this.setRemoteAddress16(parser.parseAddress16()); 134 | } 135 | 136 | protected static Option getOption(int option) { 137 | if (Option.get(option) != null) { 138 | return Option.get(option); 139 | } else { 140 | log.warn("Unknown response option " + option); 141 | return Option.UNKNOWN; 142 | } 143 | } 144 | 145 | protected void parseOption(IPacketParser parser) throws IOException { 146 | int option = parser.read("ZNet RX Response Option"); 147 | this.setOption(this.getOption(option)); 148 | } 149 | 150 | public String toString() { 151 | return super.toString() + 152 | ",remoteAddress64=" + this.remoteAddress64 + 153 | ",remoteAddress16=" + this.remoteAddress16 + 154 | ",option=" + this.option; 155 | } 156 | } -------------------------------------------------------------------------------- /docs/api/com/rapplogic/xbee/api/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | com.rapplogic.xbee.api 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | com.rapplogic.xbee.api 18 | 19 | 20 | 33 | 34 |
21 | Interfaces  22 | 23 |
24 | IXBee 25 |
26 | NoRequestResponse 27 |
28 | PacketListener 29 |
30 | ResponseQueueFilter 31 |
32 | XBeePacketHandler
35 | 36 | 37 | 38 | 39 | 84 | 85 |
40 | Classes  41 | 42 |
43 | AtCommand 44 |
45 | AtCommandQueue 46 |
47 | AtCommandResponse 48 |
49 | Checksum 50 |
51 | ErrorResponse 52 |
53 | GenericResponse 54 |
55 | InputStreamThread 56 |
57 | ModemStatusResponse 58 |
59 | PacketParser 60 |
61 | RemoteAtRequest 62 |
63 | RemoteAtResponse 64 |
65 | XBee 66 |
67 | XBeeAddress 68 |
69 | XBeeAddress16 70 |
71 | XBeeAddress64 72 |
73 | XBeeConfiguration 74 |
75 | XBeeFrameIdResponse 76 |
77 | XBeePacket 78 |
79 | XBeePacketLength 80 |
81 | XBeeRequest 82 |
83 | XBeeResponse
86 | 87 | 88 | 89 | 90 | 101 | 102 |
91 | Enums  92 | 93 |
94 | ApiId 95 |
96 | AtCommandResponse.Status 97 |
98 | ModemStatusResponse.Status 99 |
100 | XBeePacket.SpecialByte
103 | 104 | 105 | 106 | 107 | 116 | 117 |
108 | Exceptions  109 | 110 |
111 | XBeeException 112 |
113 | XBeeParseException 114 |
115 | XBeeTimeoutException
118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | com.rapplogic 6 | xbee-api 7 | jar 8 | 0.9.4-SNAPSHOT 9 | 10 | ${project.groupId}:${project.artifactId} 11 | A java library for communicating with XBee radios 12 | https://github.com/andrewrapp/xbee-api/ 13 | 14 | 15 | 16 | GPL license, Version 3.0 17 | https://www.gnu.org/licenses/gpl-3.0.html 18 | 19 | 20 | 21 | 22 | 23 | Andrew Rapp 24 | andrew.rapp+github.com@gmail.com 25 | 26 | 27 | 28 | 29 | scm:git:git@github.com:andrewrapp/xbee-api.git 30 | scm:git:git@github.com:andrewrapp/xbee-api.git 31 | https://github.com/andrewrapp/xbee-api/ 32 | 33 | 34 | 35 | UTF-8 36 | 1.6 37 | 1.6 38 | 39 | 40 | 41 | 42 | ossrh 43 | https://oss.sonatype.org/content/repositories/snapshots 44 | 45 | 46 | ossrh 47 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.apache.maven.plugins 55 | maven-compiler-plugin 56 | 57 | 1.6 58 | 1.6 59 | 60 | 61 | 62 | 63 | org.apache.maven.plugins 64 | maven-gpg-plugin 65 | 66 | 67 | sign-artifacts 68 | deploy 69 | 70 | sign 71 | 72 | 73 | 74 | 75 | 76 | 77 | org.sonatype.plugins 78 | nexus-staging-maven-plugin 79 | 1.6.7 80 | true 81 | 82 | ossrh 83 | https://oss.sonatype.org/ 84 | true 85 | 86 | 87 | 88 | 89 | org.apache.maven.plugins 90 | maven-source-plugin 91 | 92 | 93 | attach-sources 94 | 95 | jar 96 | 97 | 98 | 99 | 100 | 101 | 102 | org.apache.maven.plugins 103 | maven-javadoc-plugin 104 | 105 | 106 | attach-javadocs 107 | 108 | jar 109 | 110 | 111 | 112 | 113 | 114 | 115 | -Xdoclint:none 116 | 117 | 118 | 119 | 120 | pl.project13.maven 121 | git-commit-id-plugin 122 | 2.2.1 123 | 124 | 125 | get-the-git-infos 126 | 127 | revision 128 | 129 | 130 | 131 | validate-the-git-infos 132 | 133 | revision 134 | 135 | package 136 | 137 | 138 | 139 | 140 | true 141 | ${project.build.outputDirectory}/git.properties 142 | 143 | git.build.* 144 | git.commit.user.email 145 | git.commit.message.* 146 | git.commit.id.describe 147 | git.commit.user.name 148 | git.remote.origin.url 149 | git.commit.id.abbrev 150 | git.closest.tag.name 151 | git.closest.tag.commit.count 152 | git.remote.origin.url 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | org.rxtx 162 | rxtx 163 | 2.1.7 164 | 165 | 166 | log4j 167 | log4j 168 | 1.2.16 169 | 170 | 171 | -------------------------------------------------------------------------------- /src/main/java/com/rapplogic/xbee/api/zigbee/ZBNodeDiscover.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2008 Andrew Rapp. All rights reserved. 3 | * 4 | * This file is part of XBee-API. 5 | * 6 | * XBee-API is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * XBee-API is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with XBee-API. If not, see . 18 | */ 19 | 20 | package com.rapplogic.xbee.api.zigbee; 21 | 22 | import java.util.EnumSet; 23 | import java.util.HashMap; 24 | import java.util.Map; 25 | 26 | import org.apache.log4j.Logger; 27 | 28 | import com.rapplogic.xbee.api.AtCommandResponse; 29 | import com.rapplogic.xbee.api.XBeeAddress16; 30 | import com.rapplogic.xbee.api.XBeeAddress64; 31 | import com.rapplogic.xbee.util.ByteUtils; 32 | import com.rapplogic.xbee.util.IntArrayInputStream; 33 | 34 | /** 35 | * Series 2 XBee. Parses a Node Discover (ND) AT Command Response 36 | *

37 | * @author andrew 38 | * 39 | */ 40 | public class ZBNodeDiscover { 41 | 42 | private final static Logger log = Logger.getLogger(ZBNodeDiscover.class); 43 | 44 | public enum DeviceType { 45 | DEV_TYPE_COORDINATOR (0), 46 | DEV_TYPE_ROUTER (1), 47 | DEV_TYPE_END_DEVICE (2); 48 | 49 | private static final Map lookup = new HashMap(); 50 | 51 | static { 52 | for(DeviceType s : EnumSet.allOf(DeviceType.class)) { 53 | lookup.put(s.getValue(), s); 54 | } 55 | } 56 | 57 | public static DeviceType get(int value) { 58 | return lookup.get(value); 59 | } 60 | 61 | private final int value; 62 | 63 | DeviceType(int value) { 64 | this.value = value; 65 | } 66 | 67 | public int getValue() { 68 | return value; 69 | } 70 | } 71 | 72 | private XBeeAddress16 nodeAddress16; 73 | private XBeeAddress64 nodeAddress64; 74 | private String nodeIdentifier; 75 | private XBeeAddress16 parent; 76 | private DeviceType deviceType; 77 | private int status; 78 | private int[] profileId; 79 | private int[] mfgId; 80 | 81 | public DeviceType getDeviceType() { 82 | return deviceType; 83 | } 84 | 85 | 86 | public void setDeviceType(DeviceType deviceType) { 87 | this.deviceType = deviceType; 88 | } 89 | 90 | 91 | public int getStatus() { 92 | return status; 93 | } 94 | 95 | 96 | public void setStatus(int status) { 97 | this.status = status; 98 | } 99 | 100 | 101 | public int[] getProfileId() { 102 | return profileId; 103 | } 104 | 105 | 106 | public void setProfileId(int[] profileId) { 107 | this.profileId = profileId; 108 | } 109 | 110 | 111 | public int[] getMfgId() { 112 | return mfgId; 113 | } 114 | 115 | 116 | public void setMfgId(int[] mfgId) { 117 | this.mfgId = mfgId; 118 | } 119 | 120 | 121 | public static ZBNodeDiscover parse(AtCommandResponse response) { 122 | 123 | if (!response.getCommand().equals("ND")) { 124 | throw new RuntimeException("This method is only applicable for the ND command"); 125 | } 126 | 127 | int[] data = response.getValue(); 128 | 129 | IntArrayInputStream in = new IntArrayInputStream(data); 130 | 131 | ZBNodeDiscover nd = new ZBNodeDiscover(); 132 | 133 | nd.setNodeAddress16(new XBeeAddress16(in.read(2))); 134 | 135 | nd.setNodeAddress64(new XBeeAddress64(in.read(8))); 136 | 137 | StringBuffer ni = new StringBuffer(); 138 | 139 | int ch; 140 | 141 | // NI is terminated with 0 142 | while ((ch = in.read()) != 0) { 143 | if (ch < 32 || ch > 126) { 144 | throw new RuntimeException("Node Identifier " + ch + " is non-ascii"); 145 | } 146 | 147 | ni.append((char)ch); 148 | } 149 | 150 | nd.setNodeIdentifier(ni.toString()); 151 | 152 | nd.setParent(new XBeeAddress16(in.read(2))); 153 | nd.setDeviceType(DeviceType.get(in.read())); 154 | // TODO this is being reported as 1 (router) for my end device 155 | nd.setStatus(in.read()); 156 | nd.setProfileId(in.read(2)); 157 | nd.setMfgId(in.read(2)); 158 | 159 | return nd; 160 | } 161 | 162 | public String toString() { 163 | return "nodeAddress16=" + this.nodeAddress16 + 164 | ", nodeAddress64=" + this.nodeAddress64 + 165 | ", nodeIdentifier=" + this.nodeIdentifier + 166 | ", parentAddress=" + this.getParent() + 167 | ", deviceType=" + this.deviceType + 168 | ", status=" + this.status + 169 | ", profileId=" + ByteUtils.toBase16(this.profileId) + 170 | ", mfgId=" + ByteUtils.toBase16(this.mfgId); 171 | } 172 | 173 | 174 | public XBeeAddress16 getNodeAddress16() { 175 | return nodeAddress16; 176 | } 177 | 178 | 179 | public void setNodeAddress16(XBeeAddress16 my) { 180 | this.nodeAddress16 = my; 181 | } 182 | 183 | 184 | public XBeeAddress64 getNodeAddress64() { 185 | return nodeAddress64; 186 | } 187 | 188 | 189 | public void setNodeAddress64(XBeeAddress64 serial) { 190 | this.nodeAddress64 = serial; 191 | } 192 | 193 | 194 | public String getNodeIdentifier() { 195 | return nodeIdentifier; 196 | } 197 | 198 | 199 | public void setNodeIdentifier(String nodeIdentifier) { 200 | this.nodeIdentifier = nodeIdentifier; 201 | } 202 | 203 | 204 | public XBeeAddress16 getParent() { 205 | return parent; 206 | } 207 | 208 | 209 | public void setParent(XBeeAddress16 parent) { 210 | this.parent = parent; 211 | } 212 | } 213 | --------------------------------------------------------------------------------