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.net.Inet6Address;
019
020import com.digi.xbee.api.connection.IConnectionInterface;
021import com.digi.xbee.api.connection.serial.SerialPortParameters;
022import com.digi.xbee.api.exceptions.InterfaceNotOpenException;
023import com.digi.xbee.api.exceptions.TimeoutException;
024import com.digi.xbee.api.exceptions.XBeeDeviceException;
025import com.digi.xbee.api.exceptions.XBeeException;
026import com.digi.xbee.api.listeners.IExplicitDataReceiveListener;
027import com.digi.xbee.api.models.APIOutputMode;
028import com.digi.xbee.api.models.ExplicitXBeeMessage;
029import com.digi.xbee.api.models.XBee64BitAddress;
030import com.digi.xbee.api.models.XBeeProtocol;
031
032/**
033 * This class represents a local DigiMesh device.
034 * 
035 * @see CellularDevice
036 * @see DigiPointDevice
037 * @see Raw802Device
038 * @see ThreadDevice
039 * @see WiFiDevice
040 * @see XBeeDevice
041 * @see ZigBeeDevice
042 */
043public class DigiMeshDevice extends XBeeDevice {
044
045        // Constants
046        private static final String OPERATION_EXCEPTION = "Operation not supported in DigiMesh protocol.";
047        
048        /**
049         * Class constructor. Instantiates a new {@code DigiMeshDevice} object in the 
050         * given port name and baud rate.
051         * 
052         * @param port Serial port name where DigiMesh device is attached to.
053         * @param baudRate Serial port baud rate to communicate with the device. 
054         *                 Other connection parameters will be set as default (8 
055         *                 data bits, 1 stop bit, no parity, no flow control).
056         * 
057         * @throws IllegalArgumentException if {@code baudRate < 0}.
058         * @throws NullPointerException if {@code port == null}.
059         * 
060         * @see #DigiMeshDevice(IConnectionInterface)
061         * @see #DigiMeshDevice(String, SerialPortParameters)
062         * @see #DigiMeshDevice(String, int, int, int, int, int)
063         */
064        public DigiMeshDevice(String port, int baudRate) {
065                this(XBee.createConnectiontionInterface(port, baudRate));
066        }
067        
068        /**
069         * Class constructor. Instantiates a new {@code DigiMeshDevice} object in the 
070         * given serial port name and settings.
071         * 
072         * @param port Serial port name where DigiMesh device is attached to.
073         * @param baudRate Serial port baud rate to communicate with the device.
074         * @param dataBits Serial port data bits.
075         * @param stopBits Serial port data bits.
076         * @param parity Serial port data bits.
077         * @param flowControl Serial port data bits.
078         * 
079         * @throws IllegalArgumentException if {@code baudRate < 0} or
080         *                                  if {@code dataBits < 0} or
081         *                                  if {@code stopBits < 0} or
082         *                                  if {@code parity < 0} or
083         *                                  if {@code flowControl < 0}.
084         * @throws NullPointerException if {@code port == null}.
085         * 
086         * @see #DigiMeshDevice(IConnectionInterface)
087         * @see #DigiMeshDevice(String, int)
088         * @see #DigiMeshDevice(String, SerialPortParameters)
089         */
090        public DigiMeshDevice(String port, int baudRate, int dataBits, int stopBits, int parity, int flowControl) {
091                this(port, new SerialPortParameters(baudRate, dataBits, stopBits, parity, flowControl));
092        }
093        
094        /**
095         * Class constructor. Instantiates a new {@code DigiMeshDevice} object in the 
096         * given serial port name and parameters.
097         * 
098         * @param port Serial port name where DigiMesh device is attached to.
099         * @param serialPortParameters Object containing the serial port parameters.
100         * 
101         * @throws NullPointerException if {@code port == null} or
102         *                              if {@code serialPortParameters == null}.
103         * 
104         * @see #DigiMeshDevice(IConnectionInterface)
105         * @see #DigiMeshDevice(String, int)
106         * @see #DigiMeshDevice(String, int, int, int, int, int)
107         * @see com.digi.xbee.api.connection.serial.SerialPortParameters
108         */
109        public DigiMeshDevice(String port, SerialPortParameters serialPortParameters) {
110                this(XBee.createConnectiontionInterface(port, serialPortParameters));
111        }
112        
113        /**
114         * Class constructor. Instantiates a new {@code DigiMeshDevice} object with the 
115         * given connection interface.
116         * 
117         * @param connectionInterface The connection interface with the physical 
118         *                            DigiMesh device.
119         * 
120         * @throws NullPointerException if {@code connectionInterface == null}
121         * 
122         * @see #DigiMeshDevice(String, int)
123         * @see #DigiMeshDevice(String, SerialPortParameters)
124         * @see #DigiMeshDevice(String, int, int, int, int, int)
125         * @see com.digi.xbee.api.connection.IConnectionInterface
126         */
127        public DigiMeshDevice(IConnectionInterface connectionInterface) {
128                super(connectionInterface);
129        }
130        
131        /*
132         * (non-Javadoc)
133         * @see com.digi.xbee.api.XBeeDevice#open()
134         */
135        @Override
136        public void open() throws XBeeException {
137                super.open();
138                if (xbeeProtocol != XBeeProtocol.DIGI_MESH)
139                        throw new XBeeDeviceException("XBee device is not a " + getXBeeProtocol().getDescription() + " device, it is a " + xbeeProtocol.getDescription() + " device.");
140        }
141        
142        /*
143         * (non-Javadoc)
144         * @see com.digi.xbee.api.XBeeDevice#getNetwork()
145         */
146        @Override
147        public XBeeNetwork getNetwork() {
148                if (!isOpen())
149                        throw new InterfaceNotOpenException();
150                if (network == null)
151                        network = new DigiMeshNetwork(this);
152                return network;
153        }
154        
155        /*
156         * (non-Javadoc)
157         * @see com.digi.xbee.api.XBeeDevice#getXBeeProtocol()
158         */
159        @Override
160        public XBeeProtocol getXBeeProtocol() {
161                return XBeeProtocol.DIGI_MESH;
162        }
163        
164        /*
165         * (non-Javadoc)
166         * @see com.digi.xbee.api.XBeeDevice#sendDataAsync(com.digi.xbee.api.models.XBee64BitAddress, byte[])
167         */
168        @Override
169        public void sendDataAsync(XBee64BitAddress address, byte[] data) throws XBeeException {
170                super.sendDataAsync(address, data);
171        }
172        
173        /*
174         * (non-Javadoc)
175         * @see com.digi.xbee.api.XBeeDevice#sendData(com.digi.xbee.api.models.XBee64BitAddress, byte[])
176         */
177        @Override
178        public void sendData(XBee64BitAddress address, byte[] data) throws TimeoutException, XBeeException {
179                super.sendData(address, data);
180        }
181        
182        /*
183         * (non-Javadoc)
184         * @see com.digi.xbee.api.XBeeDevice#readExplicitData()
185         */
186        @Override
187        public ExplicitXBeeMessage readExplicitData() {
188                return super.readExplicitData();
189        }
190        
191        /*
192         * (non-Javadoc)
193         * @see com.digi.xbee.api.XBeeDevice#readExplicitData(int)
194         */
195        @Override
196        public ExplicitXBeeMessage readExplicitData(int timeout) {
197                return super.readExplicitData(timeout);
198        }
199        
200        /*
201         * (non-Javadoc)
202         * @see com.digi.xbee.api.XBeeDevice#readExplicitDataFrom(com.digi.xbee.api.RemoteXBeeDevice)
203         */
204        @Override
205        public ExplicitXBeeMessage readExplicitDataFrom(RemoteXBeeDevice remoteXBeeDevice) {
206                return super.readExplicitDataFrom(remoteXBeeDevice);
207        }
208        
209        /*
210         * (non-Javadoc)
211         * @see com.digi.xbee.api.XBeeDevice#readExplicitDataFrom(com.digi.xbee.api.RemoteXBeeDevice, int)
212         */
213        @Override
214        public ExplicitXBeeMessage readExplicitDataFrom(
215                        RemoteXBeeDevice remoteXBeeDevice, int timeout) {
216                return super.readExplicitDataFrom(remoteXBeeDevice, timeout);
217        }
218        
219        /*
220         * (non-Javadoc)
221         * @see com.digi.xbee.api.AbstractXBeeDevice#addExplicitDataListener(com.digi.xbee.api.listeners.IExplicitDataReceiveListener)
222         */
223        @Override
224        public void addExplicitDataListener(IExplicitDataReceiveListener listener) {
225                super.addExplicitDataListener(listener);
226        }
227        
228        /*
229         * (non-Javadoc)
230         * @see com.digi.xbee.api.AbstractXBeeDevice#removeExplicitDataListener(com.digi.xbee.api.listeners.IExplicitDataReceiveListener)
231         */
232        @Override
233        public void removeExplicitDataListener(IExplicitDataReceiveListener listener) {
234                super.removeExplicitDataListener(listener);
235        }
236        
237        /*
238         * (non-Javadoc)
239         * @see com.digi.xbee.api.XBeeDevice#getAPIOutputMode()
240         */
241        @Override
242        public APIOutputMode getAPIOutputMode() throws TimeoutException, XBeeException {
243                return super.getAPIOutputMode();
244        }
245        
246        /*
247         * (non-Javadoc)
248         * @see com.digi.xbee.api.XBeeDevice#setAPIOutputMode(com.digi.xbee.api.models.APIOutputMode)
249         */
250        @Override
251        public void setAPIOutputMode(APIOutputMode apiOutputMode) throws TimeoutException, XBeeException {
252                super.setAPIOutputMode(apiOutputMode);
253        }
254        
255        /*
256         * (non-Javadoc)
257         * @see com.digi.xbee.api.XBeeDevice#sendExplicitData(com.digi.xbee.api.RemoteXBeeDevice, int, int, int, int, byte[])
258         */
259        @Override
260        public void sendExplicitData(RemoteXBeeDevice remoteXBeeDevice, int sourceEndpoint, int destEndpoint, int clusterID,
261                        int profileID, byte[] data) throws TimeoutException, XBeeException {
262                super.sendExplicitData(remoteXBeeDevice, sourceEndpoint, destEndpoint, clusterID, profileID, data);
263        }
264        
265        /*
266         * (non-Javadoc)
267         * @see com.digi.xbee.api.XBeeDevice#sendExplicitData(com.digi.xbee.api.models.XBee64BitAddress, int, int, int, int, byte[])
268         */
269        @Override
270        public void sendExplicitData(XBee64BitAddress address, int sourceEndpoint, int destEndpoint, int clusterID,
271                        int profileID, byte[] data) throws TimeoutException, XBeeException {
272                super.sendExplicitData(address, sourceEndpoint, destEndpoint, clusterID, profileID, data);
273        }
274        
275        /*
276         * (non-Javadoc)
277         * @see com.digi.xbee.api.XBeeDevice#sendBroadcastExplicitData(int, int, int, int, byte[])
278         */
279        @Override
280        public void sendBroadcastExplicitData(int sourceEndpoint, int destEndpoint, int clusterID, int profileID, 
281                        byte[] data) throws TimeoutException, XBeeException {
282                super.sendBroadcastExplicitData(sourceEndpoint, destEndpoint, clusterID, profileID, data);
283        }
284        
285        /*
286         * (non-Javadoc)
287         * @see com.digi.xbee.api.XBeeDevice#sendExplicitDataAsync(com.digi.xbee.api.RemoteXBeeDevice, int, int, int, int, byte[])
288         */
289        @Override
290        public void sendExplicitDataAsync(RemoteXBeeDevice xbeeDevice, int sourceEndpoint, int destEndpoint, int clusterID,
291                        int profileID, byte[] data) throws XBeeException {
292                super.sendExplicitDataAsync(xbeeDevice, sourceEndpoint, destEndpoint, clusterID, profileID, data);
293        }
294        
295        /*
296         * (non-Javadoc)
297         * @see com.digi.xbee.api.XBeeDevice#sendExplicitDataAsync(com.digi.xbee.api.models.XBee64BitAddress, int, int, int, int, byte[])
298         */
299        @Override
300        public void sendExplicitDataAsync(XBee64BitAddress address, int sourceEndpoint, int destEndpoint, int clusterID,
301                        int profileID, byte[] data) throws XBeeException {
302                super.sendExplicitDataAsync(address, sourceEndpoint, destEndpoint, clusterID, profileID, data);
303        }
304        
305        /**
306         * @deprecated DigiMesh protocol does not have an associated IPv6 address.
307         */
308        @Override
309        public Inet6Address getIPv6Address() {
310                // DigiMesh protocol does not have IPv6 address.
311                return null;
312        }
313        
314        /**
315         * @deprecated Operation not supported in DigiMesh protocol. This method
316         *             will raise an {@link UnsupportedOperationException}.
317         */
318        @Override
319        public Inet6Address getIPv6DestinationAddress()
320                        throws TimeoutException, XBeeException {
321                // Not supported in DigiMesh.
322                throw new UnsupportedOperationException(OPERATION_EXCEPTION);
323        }
324        
325        /**
326         * @deprecated Operation not supported in DigiMesh protocol. This method
327         *             will raise an {@link UnsupportedOperationException}.
328         */
329        @Override
330        public void setIPv6DestinationAddress(Inet6Address ipv6Address)
331                        throws TimeoutException, XBeeException {
332                // Not supported in DigiMesh.
333                throw new UnsupportedOperationException(OPERATION_EXCEPTION);
334        }
335}