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.HashMap;
019
020/**
021 * Enumerates all the possible states of an AT Command after executing it. 
022 * {@code ATCommadResponse} objects will contain an entry of this enumerator 
023 * indicating the status of the AT Command that was executed.
024 * 
025 * @see ATCommandResponse
026 */
027public enum ATCommandStatus {
028
029        // Enumeration elements
030        OK (0, "Status OK"),
031        ERROR (1, "Status Error"),
032        INVALID_COMMAND (2, "Invalid command"), 
033        INVALID_PARAMETER (3, "Invalid parameter"),
034        TX_FAILURE (4, "TX failure"),
035        UNKNOWN (255, "Unknown status");
036        
037        // Variables
038        private int id;
039        
040        private final String description;
041        
042        private final static HashMap<Integer, ATCommandStatus> lookupTable = new HashMap<Integer, ATCommandStatus>();
043        
044        static {
045                for (ATCommandStatus at:values())
046                        lookupTable.put(at.getId(), at);
047        }
048        
049        /**
050         * Class constructor. Instantiates a new enumeration element of type 
051         * {@code ATCommandStatus} with the given parameters.
052         * 
053         * @param id AT Command Status ID.
054         * @param description AT Command Status description.
055         */
056        ATCommandStatus(int id, String description) {
057                this.id = id;
058                this.description = description;
059        }
060        
061        /**
062         * Returns the AT Command Status ID.
063         * 
064         * @return The AT Command Status ID.
065         */
066        public int getId() {
067                return id;
068        }
069        
070        /**
071         * Returns the AT Command Status description.
072         * 
073         * @return AT Command Status description.
074         */
075        public String getDescription() {
076                return description;
077        }
078        
079        /**
080         * Returns the {@code ATCommandStatus} associated to the given ID.
081         * 
082         * @param id ID to retrieve the corresponding {@code ATCommandStatus}.
083         * 
084         * @return The {@code ATCommandStatus} associated to the given ID.
085         */
086        public static ATCommandStatus get(int id) {
087                ATCommandStatus status = lookupTable.get(id % 16);
088                if (status == null)
089                        status = UNKNOWN;
090                status.id = id;
091                return status;
092        }
093        
094        /*
095         * (non-Javadoc)
096         * @see java.lang.Enum#toString()
097         */
098        @Override
099        public String toString() {
100                return description;
101        }
102}