• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
20  * The result of execution of a single AT command.<p>
21  *
22  *
23  * This class can represent the final response to an AT command line, and also
24  * intermediate responses to a single command within a chained AT command
25  * line.<p>
26  *
27  * The actual responses that are intended to be send in reply to the AT command
28  * line are stored in a string array. The final response is stored as an
29  * int enum, converted to a string when toString() is called. Only a single
30  * final response is sent from multiple commands chained into a single command
31  * line.<p>
32  * @hide
33  */
34 public class AtCommandResult {
35     // Result code enumerations
36     public static final int OK = 0;
37     public static final int ERROR = 1;
38     public static final int UNSOLICITED = 2;
39 
40     private static final String OK_STRING = "OK";
41     private static final String ERROR_STRING = "ERROR";
42 
43     private int mResultCode;  // Result code
44     private StringBuilder mResponse; // Response with CRLF line breaks
45 
46     /**
47      * Construct a new AtCommandResult with given result code, and an empty
48      * response array.
49      * @param resultCode One of OK, ERROR or UNSOLICITED.
50      */
AtCommandResult(int resultCode)51     public AtCommandResult(int resultCode) {
52         mResultCode = resultCode;
53         mResponse = new StringBuilder();
54     }
55 
56     /**
57      * Construct a new AtCommandResult with result code OK, and the specified
58      * single line response.
59      * @param response The single line response.
60      */
AtCommandResult(String response)61     public AtCommandResult(String response) {
62         this(OK);
63         addResponse(response);
64     }
65 
getResultCode()66     public int getResultCode() {
67         return mResultCode;
68     }
69 
70     /**
71      * Add another line to the response.
72      */
addResponse(String response)73     public void addResponse(String response) {
74         appendWithCrlf(mResponse, response);
75     }
76 
77     /**
78      * Add the given result into this AtCommandResult object.<p>
79      * Used to combine results from multiple commands in a single command line
80      * (command chaining).
81      * @param result The AtCommandResult to add to this result.
82      */
addResult(AtCommandResult result)83     public void addResult(AtCommandResult result) {
84         if (result != null) {
85             appendWithCrlf(mResponse, result.mResponse.toString());
86             mResultCode = result.mResultCode;
87         }
88     }
89 
90     /**
91      * Generate the string response ready to send
92      */
toString()93     public String toString() {
94         StringBuilder result = new StringBuilder(mResponse.toString());
95         switch (mResultCode) {
96         case OK:
97             appendWithCrlf(result, OK_STRING);
98             break;
99         case ERROR:
100             appendWithCrlf(result, ERROR_STRING);
101             break;
102         }
103         return result.toString();
104     }
105 
106     /** Append a string to a string builder, joining with a double
107      * CRLF. Used to create multi-line AT command replies
108      */
appendWithCrlf(StringBuilder str1, String str2)109     public static void appendWithCrlf(StringBuilder str1, String str2) {
110         if (str1.length() > 0 && str2.length() > 0) {
111             str1.append("\r\n\r\n");
112         }
113         str1.append(str2);
114     }
115 };
116