001/**
002 * Copyright 2017, 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.models;
017
018/**
019 * This class represents an Access Point for the Wi-Fi protocol. It contains 
020 * SSID, the encryption type and the link quality between the Wi-Fi module and 
021 * the access point.
022 * 
023 * <p>This class is used within the XBee Java Library to list the access points 
024 * and connect to a specific one in the Wi-Fi protocol.</p>
025 * 
026 * @since 1.2.0
027 */
028public class AccessPoint {
029
030        // Constants.
031        private static final String ERROR_CHANNEL = "Channel cannot be negative.";
032        private static final String ERROR_SIGNAL_QUALITY = "Signal quality must be between 0 and 100.";
033        
034        // Variables.
035        private final String ssid;
036        
037        private final WiFiEncryptionType encryptionType;
038        
039        private int channel;
040        private int signalQuality;
041        
042        /**
043         * Class constructor. Instantiates a new object of type 
044         * {@code AccessPoint} with the given parameters.
045         * 
046         * @param ssid The SSID of the access point.
047         * @param encryptionType The encryption type configured in the access 
048         *                       point.
049         * 
050         * @throws IllegalArgumentException if {@code ssid.length < 0}.
051         * @throws NullPointerException if {@code ssid == null} or 
052         *                              if {@code encryptionType == null}.
053         * 
054         * @see #AccessPoint(String, WiFiEncryptionType, int, int)
055         * @see com.digi.xbee.api.models.WiFiEncryptionType
056         */
057        public AccessPoint(String ssid, WiFiEncryptionType encryptionType) {
058                this(ssid, encryptionType, 0, 0);
059        }
060        
061        /**
062         * Class constructor. Instantiates a new object of type 
063         * {@code AccessPoint} with the given parameters.
064         * 
065         * @param ssid The SSID of the access point.
066         * @param encryptionType The encryption type configured in the access 
067         *                       point.
068         * @param channel Operating channel of the access point.
069         * @param signalQuality Signal quality with the access point in %.
070         * 
071         * @throws IllegalArgumentException if {@code ssid.length < 0} or 
072         *                                  if {@code channel < 0} or 
073         *                                  if {@code signalQuality < 0} or 
074         *                                  if {@code signalQuality > 100}.
075         * @throws NullPointerException if {@code ssid == null} or
076         *                              if {@code encryptionType == null}.
077         * 
078         * @see #AccessPoint(String, WiFiEncryptionType)
079         * @see com.digi.xbee.api.models.WiFiEncryptionType
080         */
081        public AccessPoint(String ssid, WiFiEncryptionType encryptionType, int channel,
082                        int signalQuality) {
083                if (ssid == null)
084                        throw new NullPointerException("SSID cannot be null.");
085                if (encryptionType == null)
086                        throw new NullPointerException("Encryption type cannot be null.");
087                
088                if (ssid.length() == 0)
089                        throw new IllegalArgumentException("SSID cannot be empty.");
090                if (channel < 0)
091                        throw new IllegalArgumentException(ERROR_CHANNEL);
092                if (signalQuality < 0 || signalQuality > 100)
093                        throw new IllegalArgumentException(ERROR_SIGNAL_QUALITY);
094                
095                this.ssid = ssid;
096                this.encryptionType = encryptionType;
097                this.channel = channel;
098                this.signalQuality = signalQuality;
099        }
100        
101        /**
102         * Returns the SSID of the access point.
103         * 
104         * @return The SSID of the access point.
105         */
106        public String getSSID() {
107                return ssid;
108        }
109        
110        /**
111         * Returns the encryption type of the access point.
112         * 
113         * @return The encryption type of the access point.
114         * 
115         * @see com.digi.xbee.api.models.WiFiEncryptionType
116         */
117        public WiFiEncryptionType getEncryptionType() {
118                return encryptionType;
119        }
120        
121        /**
122         * Returns the operating channel of the access point.
123         * 
124         * @return The operating channel of the access point.
125         * 
126         * @see #setChannel(int)
127         */
128        public int getChannel() {
129                return channel;
130        }
131        
132        /**
133         * Sets the new channel of the access point.
134         * 
135         * @param channel The new channel of the access point.
136         * 
137         * @see #getChannel()
138         */
139        public void setChannel(int channel) {
140                if (channel < 0)
141                        throw new IllegalArgumentException(ERROR_CHANNEL);
142                
143                this.channel = channel;
144        }
145        
146        /**
147         * Returns the signal quality with the access point in %.
148         * 
149         * @return The signal quality with the access point in %.
150         * 
151         * @see #setSignalQuality(int)
152         */
153        public int getSignalQuality() {
154                return signalQuality;
155        }
156        
157        /**
158         * Sets the new signal quality with the access point.
159         * 
160         * @param signalQuality The new signal quality with the access point.
161         * 
162         * @see #getSignalQuality()
163         */
164        public void setSignalQuality(int signalQuality) {
165                if (signalQuality < 0 || signalQuality > 100)
166                        throw new IllegalArgumentException(ERROR_SIGNAL_QUALITY);
167                
168                this.signalQuality = signalQuality;
169        }
170        
171        /*
172         * (non-Javadoc)
173         * @see java.lang.Object#toString()
174         */
175        @Override
176        public String toString() {
177                StringBuilder message = new StringBuilder();
178                message.append(ssid);
179                message.append(" (");
180                message.append(encryptionType.name());
181                message.append(") - CH: ");
182                message.append(channel);
183                message.append(" - Signal: ");
184                message.append(signalQuality);
185                message.append("%");
186                
187                return message.toString();
188        }
189}