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
018/**
019 * This class represents an AT command used to read or set different properties 
020 * of the XBee device.
021 * 
022 * <p>AT commands can be sent directly to the connected device or to remote 
023 * devices and may have parameters.</p> 
024 * 
025 * <p>After executing an AT Command, an AT Response is received from the 
026 * device.</p>
027 * 
028 * @see ATCommandResponse
029 */
030public class ATCommand {
031        
032        // Variables
033        private final String command;
034        
035        private byte[] parameter;
036        
037        /**
038         * Class constructor. Instantiates a new object of type {@code ATCommand} 
039         * with the given parameters.
040         * 
041         * @param command The AT Command alias.
042         * 
043         * @throws IllegalArgumentException if {@code command.length() != 2}.
044         * @throws NullPointerException if {@code command == null}.
045         */
046        public ATCommand(String command) {
047                this(command, (String)null);
048        }
049        
050        /**
051         * Class constructor. Instantiates a new object of type {@code ATCommand} 
052         * with the given parameters.
053         * 
054         * <p>If not parameter is required the constructor 
055         * {@link #ATCommand(String)} is recommended.</p>
056         * 
057         * @param command The AT Command alias.
058         * @param parameter The command parameter as string.
059         * 
060         * @throws IllegalArgumentException if {@code command.length() != 2}.
061         * @throws NullPointerException if {@code command == null}.
062         */
063        public ATCommand(String command, String parameter) {
064                this(command, parameter == null ? null : parameter.getBytes());
065        }
066        
067        /**
068         * Class constructor. Instantiates a new object of type {@code ATCommand} 
069         * with the given parameters.
070         * 
071         * <p>If not parameter is required the constructor 
072         * {@link #ATCommand(String)} is recommended.</p>
073         * 
074         * @param command The AT Command alias.
075         * @param parameter The command parameter as byte array.
076         * 
077         * @throws IllegalArgumentException if {@code command.length() != 2}.
078         * @throws NullPointerException if {@code command == null}.
079         */
080        public ATCommand(String command, byte[] parameter) {
081                if (command == null)
082                        throw new NullPointerException("Command cannot be null.");
083                if (command.length() != 2)
084                        throw new IllegalArgumentException("Command lenght must be 2.");
085                
086                this.command = command;
087                this.parameter = parameter;
088        }
089        
090        /**
091         * Returns the AT command alias.
092         * 
093         * @return The AT command alias.
094         */
095        public String getCommand() {
096                return command;
097        }
098        
099        /**
100         * Returns the AT command parameter.
101         * 
102         * @return The AT command parameter, {@code null} if the command does not 
103         *         have a parameter.
104         */
105        public byte[] getParameter() {
106                return parameter;
107        }
108        
109        /**
110         * Returns the AT command parameter in string format.
111         * 
112         * @return The AT command parameter, {@code null} if the command does not 
113         *         have a parameter.
114         */
115        public String getParameterString() {
116                if (parameter == null)
117                        return null;
118                return new String(parameter);
119        }
120        
121        /**
122         * Sets the AT command parameter as string.
123         * 
124         * @param parameter The AT command parameter as string.
125         */
126        public void setParameter(String parameter) {
127                this.parameter = parameter.getBytes();
128        }
129        
130        /**
131         * Sets the AT command parameter as byte array.
132         * 
133         * @param parameter The AT command parameter as byte array.
134         */
135        public void setParameter(byte[] parameter) {
136                this.parameter = parameter;
137        }
138}