1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.bluetooth; 18 19 import java.util.*; 20 21 /** 22 * The result of execution of an single AT command.<p> 23 * 24 * 25 * This class can represent the final response to an AT command line, and also 26 * intermediate responses to a single command within a chained AT command 27 * line.<p> 28 * 29 * The actual responses that are intended to be send in reply to the AT command 30 * line are stored in a string array. The final response is stored as an 31 * int enum, converted to a string when toString() is called. Only a single 32 * final response is sent from multiple commands chained into a single command 33 * line.<p> 34 * @hide 35 */ 36 public class AtCommandResult { 37 // Result code enumerations 38 public static final int OK = 0; 39 public static final int ERROR = 1; 40 public static final int UNSOLICITED = 2; 41 42 private static final String OK_STRING = "OK"; 43 private static final String ERROR_STRING = "ERROR"; 44 45 private int mResultCode; // Result code 46 private StringBuilder mResponse; // Response with CRLF line breaks 47 48 /** 49 * Construct a new AtCommandResult with given result code, and an empty 50 * response array. 51 * @param resultCode One of OK, ERROR or UNSOLICITED. 52 */ 53 public AtCommandResult(int resultCode) { 54 mResultCode = resultCode; 55 mResponse = new StringBuilder(); 56 } 57 58 /** 59 * Construct a new AtCommandResult with result code OK, and the specified 60 * single line response. 61 * @param response The single line response. 62 */ 63 public AtCommandResult(String response) { 64 this(OK); 65 addResponse(response); 66 } 67 68 public int getResultCode() { 69 return mResultCode; 70 } 71 72 /** 73 * Add another line to the response. 74 */ 75 public void addResponse(String response) { 76 appendWithCrlf(mResponse, response); 77 } 78 79 /** 80 * Add the given result into this AtCommandResult object.<p> 81 * Used to combine results from multiple commands in a single command line 82 * (command chaining). 83 * @param result The AtCommandResult to add to this result. 84 */ 85 public void addResult(AtCommandResult result) { 86 if (result != null) { 87 appendWithCrlf(mResponse, result.mResponse.toString()); 88 mResultCode = result.mResultCode; 89 } 90 } 91 92 /** 93 * Generate the string response ready to send 94 */ 95 public String toString() { 96 StringBuilder result = new StringBuilder(mResponse.toString()); 97 switch (mResultCode) { 98 case OK: 99 appendWithCrlf(result, OK_STRING); 100 break; 101 case ERROR: 102 appendWithCrlf(result, ERROR_STRING); 103 break; 104 } 105 return result.toString(); 106 } 107 108 /** Append a string to a string builder, joining with a double 109 * CRLF. Used to create multi-line AT command replies 110 */ 111 public static void appendWithCrlf(StringBuilder str1, String str2) { 112 if (str1.length() > 0 && str2.length() > 0) { 113 str1.append("\r\n\r\n"); 114 } 115 str1.append(str2); 116 } 117 }; 118