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.HashMap;
015
016/**
017 * Enumerates all the special bytes of the XBee protocol that must be escaped 
018 * when working on API 2 mode.
019 * 
020 * @see OperatingMode
021 */
022public enum SpecialByte {
023
024        // Enumeration elements
025        ESCAPE_BYTE(0x7D),
026        HEADER_BYTE (0x7E),
027        XON_BYTE(0x11),
028        XOFF_BYTE(0x13);
029        
030        // Variables
031        private static final HashMap<Integer, SpecialByte> lookupTable = new HashMap<Integer, SpecialByte>();
032        
033        static {
034                for (SpecialByte sb:values())
035                        lookupTable.put(sb.getValue(), sb);
036        }
037        
038        private final int value;
039        
040        /**
041         * Class constructor. Instantiates a new {@code SpecialByte} enumeration 
042         * entry with the given value.
043         * 
044         * @param value Value of the special byte.
045         */
046        private SpecialByte(int value) {
047                this.value = value;
048        }
049        
050        /**
051         * Returns the special byte value.
052         * 
053         * @return The special byte value.
054         */
055        public int getValue() {
056                return value;
057        }
058        
059        /**
060         * Returns the {@code SpecialByte} entry associated with the given value.
061         * 
062         * @param value Value of the {@code SpecialByte} to retrieve.
063         * 
064         * @return {@code SpecialByte} associated to the given value, {@code null} 
065         *         if it does not exist in the list.
066         */
067        public static SpecialByte get(int value) {
068                return lookupTable.get(value);
069        }
070        
071        /**
072         * Escapes the byte by performing a XOR operation with {@code 0x20} value.
073         * 
074         * @return Escaped byte value.
075         */
076        public int escapeByte() {
077                return value ^ 0x20;
078        }
079        
080        /**
081         * Checks whether the given byte is special or not.
082         * 
083         * @param byteToCheck Byte to check.
084         * 
085         * @return {@code true} if given byte is special, {@code false} otherwise.
086         */
087        public static boolean isSpecialByte(int byteToCheck) {
088                return get(byteToCheck) != null;
089        }
090}