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