001/*
002 * Copyright 2017-2019, Digi International Inc.
003 *
004 * This Source Code Form is subject to the terms of the Mozilla Public
005 * License, v. 2.0. If a copy of the MPL was not distributed with this
006 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
007 *
008 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
009 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
010 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
011 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
012 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
013 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
014 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015 */
016package com.digi.xbee.api;
017
018import java.io.IOException;
019import java.net.Inet6Address;
020
021import com.digi.xbee.api.exceptions.InterfaceNotOpenException;
022import com.digi.xbee.api.exceptions.TimeoutException;
023import com.digi.xbee.api.exceptions.XBeeException;
024import com.digi.xbee.api.models.ATCommand;
025import com.digi.xbee.api.models.ATCommandResponse;
026import com.digi.xbee.api.models.XBee16BitAddress;
027import com.digi.xbee.api.models.XBee64BitAddress;
028import com.digi.xbee.api.models.XBeeProtocol;
029
030/**
031 * This class represents a remote XBee device.
032 * 
033 * @see RemoteDigiMeshDevice
034 * @see RemoteDigiPointDevice
035 * @see RemoteRaw802Device
036 * @see RemoteThreadDevice
037 * @see RemoteZigBeeDevice
038 */
039public class RemoteXBeeDevice extends AbstractXBeeDevice {
040
041        /**
042         * Class constructor. Instantiates a new {@code RemoteXBeeDevice} object 
043         * with the given local {@code XBeeDevice} which contains the connection 
044         * interface to be used.
045         * 
046         * @param localXBeeDevice The local XBee device that will behave as 
047         *                        connection interface to communicate with this 
048         *                        remote XBee device.
049         * @param addr64 The 64-bit address to identify this remote XBee device.
050         * 
051         * @throws IllegalArgumentException if {@code localXBeeDevice.isRemote() == true}.
052         * @throws NullPointerException if {@code localXBeeDevice == null} or
053         *                              if {@code addr64 == null}.
054         * 
055         * @see com.digi.xbee.api.models.XBee64BitAddress
056         */
057        public RemoteXBeeDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
058                super(localXBeeDevice, addr64);
059        }
060        
061        /**
062         * Class constructor. Instantiates a new {@code RemoteXBeeDevice} object 
063         * with the given local {@code XBeeDevice} which contains the connection 
064         * interface to be used.
065         * 
066         * @param localXBeeDevice The local XBee device that will behave as 
067         *                        connection interface to communicate with this 
068         *                        remote XBee device.
069         * @param addr64 The 64-bit address to identify this remote XBee device.
070         * @param addr16 The 16-bit address to identify this remote XBee device. It 
071         *               might be {@code null}.
072         * @param ni The node identifier of this remote XBee device. It might be 
073         *           {@code null}.
074         * 
075         * @throws IllegalArgumentException if {@code localXBeeDevice.isRemote() == true}.
076         * @throws NullPointerException if {@code localXBeeDevice == null} or
077         *                              if {@code addr64 == null}.
078         * 
079         * @see com.digi.xbee.api.models.XBee16BitAddress
080         * @see com.digi.xbee.api.models.XBee64BitAddress
081         */
082        public RemoteXBeeDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64, 
083                        XBee16BitAddress addr16, String ni) {
084                super(localXBeeDevice, addr64, addr16, ni);
085        }
086        
087        /**
088         * Class constructor. Instantiates a new {@code RemoteXBeeDevice} object 
089         * with the given local {@code XBeeDevice} which contains the connection 
090         * interface to be used.
091         * 
092         * @param localXBeeDevice The local XBee device that will behave as 
093         *                        connection interface to communicate with this 
094         *                        remote XBee device.
095         * @param ipv6Addr The IPv6 address to identify this XBee device.
096         * 
097         * @throws IllegalArgumentException if {@code localXBeeDevice.isRemote() == true}.
098         * @throws NullPointerException if {@code localXBeeDevice == null} or
099         *                              if {@code ipv6Addr == null}.
100         * 
101         * @see java.net.Inet6Address
102         */
103        public RemoteXBeeDevice(AbstractXBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
104                super(localXBeeDevice, ipv6Addr);
105        }
106        
107        /**
108         * Class constructor. Instantiates a new {@code RemoteXBeeDevice} object 
109         * with the given local {@code XBeeDevice} which contains the connection 
110         * interface to be used.
111         * 
112         * @param localXBeeDevice The local XBee device that will behave as 
113         *                        connection interface to communicate with this 
114         *                        remote XBee device.
115         * @param ipv6Addr The IPv6 address to identify this XBee device.
116         * @param ni The node identifier of this remote XBee device. It might be 
117         *           {@code null}.
118         * 
119         * @throws IllegalArgumentException if {@code localXBeeDevice.isRemote() == true}.
120         * @throws NullPointerException if {@code localXBeeDevice == null} or
121         *                              if {@code ipv6Addr == null}.
122         * 
123         * @see java.net.Inet6Address
124         */
125        public RemoteXBeeDevice(AbstractXBeeDevice localXBeeDevice, Inet6Address ipv6Addr,
126                        String ni) {
127                super(localXBeeDevice, ipv6Addr, ni);
128        }
129        
130        /**
131         * Always returns {@code true}, since it is a remote device.
132         * 
133         * @return {@code true} always.
134         */
135        @Override
136        public boolean isRemote() {
137                return true;
138        }
139        
140        /*
141         * (non-Javadoc)
142         * @see com.digi.xbee.api.AbstractXBeeDevice#reset()
143         */
144        @Override
145        public void reset() throws TimeoutException, XBeeException {
146                // Check connection.
147                if (!connectionInterface.isOpen())
148                        throw new InterfaceNotOpenException();
149                
150                logger.info(toString() + "Resetting the remote module ({})...", get64BitAddress());
151                
152                ATCommandResponse response = null;
153                try {
154                        response = sendATCommand(new ATCommand("FR"));
155                } catch (IOException e) {
156                        throw new XBeeException("Error writing in the communication interface.", e);
157                } catch (TimeoutException e) {
158                        // Remote 802.15.4 devices do not respond to the AT command.
159                        if (localXBeeDevice.getXBeeProtocol() == XBeeProtocol.RAW_802_15_4)
160                                return;
161                        else
162                                throw e;
163                }
164                
165                // Check if AT Command response is valid.
166                checkATCommandResponseIsValid(response);
167        }
168        
169        /*
170         * (non-Javadoc)
171         * @see com.digi.xbee.api.AbstractXBeeDevice#toString()
172         */
173        @Override
174        public String toString() {
175                String id = getNodeID();
176                if (id == null)
177                        id = "";
178                return String.format("%s - %s", get64BitAddress(), getNodeID());
179        }
180}