001/**
002 * Copyright (c) 2014 Digi International Inc.,
003 * All rights not expressly granted are reserved.
004 *
005 * This Source Code Form is subject to the terms of the Mozilla Public
006 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
007 * You can obtain one at http://mozilla.org/MPL/2.0/.
008 *
009 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
010 * =======================================================================
011 */
012package com.digi.xbee.api.models;
013
014import java.util.Set;
015
016/**
017 * Enumerates the different options used in the discovery process.
018 */
019public enum DiscoveryOptions {
020
021        /**
022         * Append device type identifier (DD) to the discovery response.
023         * 
024         * <p>Valid for the following protocols:</p>
025         * <ul>
026         *   <li>DigiMesh</li>
027         *   <li>Point-to-multipoint (Digi Point)</li>
028         *   <li>ZigBee</li>
029         * </ul>
030         */
031        APPEND_DD(0x01, "Append device type identifier (DD)"),
032        
033        /**
034         * Local device sends response frame when discovery is issued.
035         * 
036         * <p>Valid for the following protocols:</p>
037         * <ul>
038         *   <li>DigiMesh</li>
039         *   <li>Point-to-multipoint (Digi Point)</li>
040         *   <li>ZigBee</li>
041         *   <li>802.15.4</li>
042         * </ul>
043         */
044        DISCOVER_MYSELF(0x02, "Local device sends response frame"),
045        
046        /**
047         * Append RSSI of the last hop to the discovery response.
048         * 
049         * <p>Valid for the following protocols:</p>
050         * <ul>
051         *   <li>DigiMesh</li>
052         *   <li>Point-to-multipoint (Digi Point)</li>
053         * </ul>
054         */
055        APPEND_RSSI(0x04, "Append RSSI (of the last hop)");
056        
057        // Variables.
058        private final int value;
059        
060        private final String description;
061        
062        private DiscoveryOptions(int value, String description) {
063                this.value = value;
064                this.description = description;
065        }
066        
067        /**
068         * Returns the value of the discovery option.
069         * 
070         * @return The discovery option value.
071         */
072        public int getValue() {
073                return value;
074        }
075        
076        /**
077         * Returns the description of the discovery option.
078         * 
079         * @return The discovery option description.
080         */
081        public String getDescription() {
082                return description;
083        }
084        
085        /**
086         * Calculates the total value of a combination of several options for the 
087         * given protocol.
088         * 
089         * @param protocol The {@code XBeeProtocol} to calculate the value of all 
090         *                 the given discovery options.
091         * @param options Collection of options to get the final value.
092         * 
093         * @return The value to be configured in the module depending on the given
094         *         collection of options and the protocol.
095         * 
096         * @see XBeeProtocol
097         */
098        public static int calculateDiscoveryValue (XBeeProtocol protocol, Set<DiscoveryOptions> options) {
099                // Calculate value to be configured.
100                int value = 0;
101                switch (protocol) {
102                case ZIGBEE:
103                case ZNET:
104                        for (DiscoveryOptions op: options) {
105                                if (op == DiscoveryOptions.APPEND_RSSI)
106                                        continue;
107                                value = value + op.getValue();
108                        }
109                        break;
110                case DIGI_MESH:
111                case DIGI_POINT:
112                case XLR:
113                        // TODO [XLR_DM] The next version of the XLR will add DigiMesh support.
114                        // For the moment only point-to-multipoint is supported in this kind of devices.
115                case XLR_DM:
116                        for (DiscoveryOptions op: options)
117                                value = value + op.getValue();
118                        break;
119                case RAW_802_15_4:
120                case UNKNOWN:
121                default:
122                        if (options.contains(DiscoveryOptions.DISCOVER_MYSELF))
123                                value = 1; // This is different for 802.15.4.
124                        break;
125                }
126                return value;
127        }
128        
129        /*
130         * (non-Javadoc)
131         * @see java.lang.Enum#toString()
132         */
133        @Override
134        public String toString() {
135                return String.format("%s (%d)", description, value);
136        }
137}