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
018import java.util.Set;
019
020/**
021 * Enumerates the different options used in the discovery process.
022 */
023public enum DiscoveryOptions {
024
025        /**
026         * Append device type identifier (DD) to the discovery response.
027         * 
028         * <p>Valid for the following protocols:</p>
029         * <ul>
030         *   <li>DigiMesh</li>
031         *   <li>Point-to-multipoint (Digi Point)</li>
032         *   <li>ZigBee</li>
033         * </ul>
034         */
035        APPEND_DD(0x01, "Append device type identifier (DD)"),
036        
037        /**
038         * Local device sends response frame when discovery is issued.
039         * 
040         * <p>Valid for the following protocols:</p>
041         * <ul>
042         *   <li>DigiMesh</li>
043         *   <li>Point-to-multipoint (Digi Point)</li>
044         *   <li>ZigBee</li>
045         *   <li>802.15.4</li>
046         * </ul>
047         */
048        DISCOVER_MYSELF(0x02, "Local device sends response frame"),
049        
050        /**
051         * Append RSSI of the last hop to the discovery response.
052         * 
053         * <p>Valid for the following protocols:</p>
054         * <ul>
055         *   <li>DigiMesh</li>
056         *   <li>Point-to-multipoint (Digi Point)</li>
057         * </ul>
058         */
059        APPEND_RSSI(0x04, "Append RSSI (of the last hop)");
060        
061        // Variables.
062        private final int value;
063        
064        private final String description;
065        
066        private DiscoveryOptions(int value, String description) {
067                this.value = value;
068                this.description = description;
069        }
070        
071        /**
072         * Returns the value of the discovery option.
073         * 
074         * @return The discovery option value.
075         */
076        public int getValue() {
077                return value;
078        }
079        
080        /**
081         * Returns the description of the discovery option.
082         * 
083         * @return The discovery option description.
084         */
085        public String getDescription() {
086                return description;
087        }
088        
089        /**
090         * Calculates the total value of a combination of several options for the 
091         * given protocol.
092         * 
093         * @param protocol The {@code XBeeProtocol} to calculate the value of all 
094         *                 the given discovery options.
095         * @param options Collection of options to get the final value.
096         * 
097         * @return The value to be configured in the module depending on the given
098         *         collection of options and the protocol.
099         * 
100         * @see XBeeProtocol
101         */
102        public static int calculateDiscoveryValue (XBeeProtocol protocol, Set<DiscoveryOptions> options) {
103                // Calculate value to be configured.
104                int value = 0;
105                switch (protocol) {
106                case ZIGBEE:
107                case ZNET:
108                        for (DiscoveryOptions op: options) {
109                                if (op == DiscoveryOptions.APPEND_RSSI)
110                                        continue;
111                                value = value + op.getValue();
112                        }
113                        break;
114                case DIGI_MESH:
115                case DIGI_POINT:
116                case XLR:
117                        // TODO [XLR_DM] The next version of the XLR will add DigiMesh support.
118                        // For the moment only point-to-multipoint is supported in this kind of devices.
119                case XLR_DM:
120                        for (DiscoveryOptions op: options)
121                                value = value + op.getValue();
122                        break;
123                case RAW_802_15_4:
124                case UNKNOWN:
125                default:
126                        if (options.contains(DiscoveryOptions.DISCOVER_MYSELF))
127                                value = 1; // This is different for 802.15.4.
128                        break;
129                }
130                return value;
131        }
132        
133        /*
134         * (non-Javadoc)
135         * @see java.lang.Enum#toString()
136         */
137        @Override
138        public String toString() {
139                return String.format("%s (%d)", description, value);
140        }
141}