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