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