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