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 the response of an AT Command sent by the connected 
020 * XBee device or by a remote device after executing an AT Command.
021 * 
022 * <p>Among the executed command, this object contains the response data and 
023 * the command status.</p>
024 * 
025 * @see ATCommand
026 * @see ATCommandStatus
027 */
028public class ATCommandResponse {
029        
030        // Variables
031        private final ATCommand command;
032        
033        private final byte[] response;
034        
035        private final ATCommandStatus status;
036        
037        /**
038         * Class constructor. Instantiates a new object of type 
039         * {@code ATCommandResponse} with the given parameters.
040         * 
041         * @param command The {@code ATCommand} that generated the response.
042         * 
043         * @throws NullPointerException if {@code command == null}.
044         * 
045         * @see ATCommand
046         */
047        public ATCommandResponse(ATCommand command) {
048                this(command, null, ATCommandStatus.OK);
049        }
050        
051        /**
052         * Class constructor. Instantiates a new object of type 
053         * {@code ATCommandResponse} with the given parameters.
054         * 
055         * @param command The {@code ATCommand} that generated the response.
056         * @param status The {@code ATCommandStatus} containing the response 
057         *               status.
058         * 
059         * @throws NullPointerException if {@code command == null} or 
060         *                              if {@code status == null}.
061         * @see ATCommand
062         * @see ATCommandStatus
063         */
064        public ATCommandResponse(ATCommand command, ATCommandStatus status) {
065                this(command, null, status);
066        }
067        
068        /**
069         * Class constructor. Instantiates a new object of type 
070         * {@code ATCommandResponse} with the given parameters.
071         * 
072         * @param command The {@code ATCommand} that generated the response.
073         * @param response The command response in byte array format.
074         * 
075         * @throws NullPointerException if {@code command == null} or 
076         *                              if {@code response == null}.
077         * 
078         * @see ATCommand
079         */
080        public ATCommandResponse(ATCommand command, byte[] response) {
081                this(command, response, ATCommandStatus.OK);
082        }
083        
084        /**
085         * Class constructor. Instantiates a new object of type 
086         * {@code ATCommandResponse} with the given parameters.
087         * 
088         * @param command The {@code ATCommand} that generated the response.
089         * @param response The command response in byte array format.
090         * @param status The {@code ATCommandStatus} containing the response 
091         *               status.
092         * 
093         * @throws NullPointerException if {@code command == null} or 
094         *                              if {@code status == null}.
095         * 
096         * @see ATCommand
097         * @see ATCommandStatus
098         */
099        public ATCommandResponse(ATCommand command, byte[] response, ATCommandStatus status) {
100                if (command == null)
101                        throw new NullPointerException("Command cannot be null.");
102                if (status == null)
103                        throw new NullPointerException("Status cannot be null.");
104                
105                this.command = command;
106                this.response = response;
107                this.status = status;
108        }
109        
110        /**
111         * Returns the AT command that generated the response.
112         * 
113         * @return The AT command that generated the response.
114         * 
115         * @see ATCommand
116         */
117        public ATCommand getCommand() {
118                return command;
119        }
120        
121        /**
122         * Returns the AT command response data in byte array format if any.
123         * 
124         * @return The AT command response data in byte array format, 
125         *         {@code null} if there is not response data.
126         */
127        public byte[] getResponse() {
128                return response;
129        }
130        
131        /**
132         * Returns the AT command response data as string if any.
133         * 
134         * @return The AT command response data as string, {@code null} if there 
135         *         is not response data.
136         */
137        public String getResponseString() {
138                if (response == null)
139                        return null;
140                return new String(response);
141        }
142        
143        /**
144         * Returns the AT command response status.
145         * 
146         * @return The AT command response status.
147         * 
148         * @see ATCommandStatus
149         */
150        public ATCommandStatus getResponseStatus() {
151                return status;
152        }
153}