San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 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 com.android.server; |
| 18 | |
| 19 | import android.net.LocalSocketAddress; |
| 20 | import android.net.LocalSocket; |
| 21 | import android.os.Environment; |
| 22 | import android.os.SystemClock; |
| 23 | import android.os.SystemProperties; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.io.InputStream; |
| 28 | import java.io.OutputStream; |
| 29 | import java.net.Socket; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 30 | |
| 31 | import java.util.List; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.ListIterator; |
| 34 | import java.util.concurrent.BlockingQueue; |
| 35 | import java.util.concurrent.LinkedBlockingQueue; |
| 36 | |
| 37 | /** |
| 38 | * Generic connector class for interfacing with a native |
| 39 | * daemon which uses the libsysutils FrameworkListener |
| 40 | * protocol. |
| 41 | */ |
| 42 | final class NativeDaemonConnector implements Runnable { |
San Mehat | 1ff4371 | 2010-02-04 15:09:02 -0800 | [diff] [blame] | 43 | private static final boolean LOCAL_LOGD = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 44 | |
| 45 | private BlockingQueue<String> mResponseQueue; |
| 46 | private OutputStream mOutputStream; |
| 47 | private String TAG = "NativeDaemonConnector"; |
| 48 | private String mSocket; |
| 49 | private INativeDaemonConnectorCallbacks mCallbacks; |
| 50 | |
| 51 | class ResponseCode { |
| 52 | public static final int ActionInitiated = 100; |
| 53 | |
| 54 | public static final int CommandOkay = 200; |
| 55 | |
| 56 | // The range of 400 -> 599 is reserved for cmd failures |
| 57 | public static final int OperationFailed = 400; |
| 58 | public static final int CommandSyntaxError = 500; |
| 59 | public static final int CommandParameterError = 501; |
| 60 | |
| 61 | public static final int UnsolicitedInformational = 600; |
| 62 | |
| 63 | // |
| 64 | public static final int FailedRangeStart = 400; |
| 65 | public static final int FailedRangeEnd = 599; |
| 66 | } |
| 67 | |
| 68 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, |
| 69 | String socket, int responseQueueSize, String logTag) { |
| 70 | mCallbacks = callbacks; |
| 71 | if (logTag != null) |
| 72 | TAG = logTag; |
| 73 | mSocket = socket; |
| 74 | mResponseQueue = new LinkedBlockingQueue<String>(responseQueueSize); |
| 75 | } |
| 76 | |
| 77 | public void run() { |
| 78 | |
| 79 | while (true) { |
| 80 | try { |
| 81 | listenToSocket(); |
| 82 | } catch (Exception e) { |
| 83 | Log.e(TAG, "Error in NativeDaemonConnector", e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 84 | SystemClock.sleep(5000); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 89 | private void listenToSocket() throws IOException { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 90 | LocalSocket socket = null; |
| 91 | |
| 92 | try { |
| 93 | socket = new LocalSocket(); |
| 94 | LocalSocketAddress address = new LocalSocketAddress(mSocket, |
| 95 | LocalSocketAddress.Namespace.RESERVED); |
| 96 | |
| 97 | socket.connect(address); |
| 98 | mCallbacks.onDaemonConnected(); |
| 99 | |
| 100 | InputStream inputStream = socket.getInputStream(); |
| 101 | mOutputStream = socket.getOutputStream(); |
| 102 | |
| 103 | byte[] buffer = new byte[4096]; |
| 104 | |
| 105 | while (true) { |
| 106 | int count = inputStream.read(buffer); |
| 107 | if (count < 0) break; |
| 108 | |
| 109 | int start = 0; |
| 110 | for (int i = 0; i < count; i++) { |
| 111 | if (buffer[i] == 0) { |
| 112 | String event = new String(buffer, start, i - start); |
San Mehat | 1ff4371 | 2010-02-04 15:09:02 -0800 | [diff] [blame] | 113 | if (LOCAL_LOGD) Log.d(TAG, String.format("RCV <- {%s}", event)); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 114 | |
| 115 | String[] tokens = event.split(" "); |
| 116 | try { |
| 117 | int code = Integer.parseInt(tokens[0]); |
| 118 | |
| 119 | if (code >= ResponseCode.UnsolicitedInformational) { |
| 120 | try { |
| 121 | if (!mCallbacks.onEvent(code, event, tokens)) { |
| 122 | Log.w(TAG, String.format( |
| 123 | "Unhandled event (%s)", event)); |
| 124 | } |
| 125 | } catch (Exception ex) { |
| 126 | Log.e(TAG, String.format( |
| 127 | "Error handling '%s'", event), ex); |
| 128 | } |
| 129 | } else { |
| 130 | try { |
| 131 | mResponseQueue.put(event); |
| 132 | } catch (InterruptedException ex) { |
| 133 | Log.e(TAG, "Failed to put response onto queue", ex); |
| 134 | } |
| 135 | } |
| 136 | } catch (NumberFormatException nfe) { |
| 137 | Log.w(TAG, String.format("Bad msg (%s)", event)); |
| 138 | } |
| 139 | start = i + 1; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } catch (IOException ex) { |
| 144 | Log.e(TAG, "Communications error", ex); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 145 | throw ex; |
| 146 | } finally { |
| 147 | synchronized (this) { |
| 148 | if (mOutputStream != null) { |
| 149 | try { |
| 150 | mOutputStream.close(); |
| 151 | } catch (IOException e) { |
| 152 | Log.w(TAG, "Failed closing output stream", e); |
| 153 | } |
| 154 | mOutputStream = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 155 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 156 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 157 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 158 | try { |
| 159 | if (socket != null) { |
| 160 | socket.close(); |
| 161 | } |
| 162 | } catch (IOException ex) { |
| 163 | Log.w(TAG, "Failed closing socket", ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private void sendCommand(String command) { |
| 169 | sendCommand(command, null); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Sends a command to the daemon with a single argument |
| 174 | * |
| 175 | * @param command The command to send to the daemon |
| 176 | * @param argument The argument to send with the command (or null) |
| 177 | */ |
| 178 | private void sendCommand(String command, String argument) { |
| 179 | synchronized (this) { |
San Mehat | 1ff4371 | 2010-02-04 15:09:02 -0800 | [diff] [blame] | 180 | if (LOCAL_LOGD) Log.d(TAG, String.format("SND -> {%s} {%s}", command, argument)); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 181 | if (mOutputStream == null) { |
| 182 | Log.e(TAG, "No connection to daemon", new IllegalStateException()); |
| 183 | } else { |
| 184 | StringBuilder builder = new StringBuilder(command); |
| 185 | if (argument != null) { |
| 186 | builder.append(argument); |
| 187 | } |
| 188 | builder.append('\0'); |
| 189 | |
| 190 | try { |
| 191 | mOutputStream.write(builder.toString().getBytes()); |
| 192 | } catch (IOException ex) { |
| 193 | Log.e(TAG, "IOException in sendCommand", ex); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Issue a command to the native daemon and return the responses |
| 201 | */ |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 202 | public synchronized ArrayList<String> doCommand(String cmd) |
| 203 | throws NativeDaemonConnectorException { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 204 | sendCommand(cmd); |
| 205 | |
| 206 | ArrayList<String> response = new ArrayList<String>(); |
| 207 | boolean complete = false; |
| 208 | int code = -1; |
| 209 | |
| 210 | while (!complete) { |
| 211 | try { |
| 212 | String line = mResponseQueue.take(); |
San Mehat | 1ff4371 | 2010-02-04 15:09:02 -0800 | [diff] [blame] | 213 | if (LOCAL_LOGD) Log.d(TAG, String.format("RSP <- {%s}", line)); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 214 | String[] tokens = line.split(" "); |
| 215 | try { |
| 216 | code = Integer.parseInt(tokens[0]); |
| 217 | } catch (NumberFormatException nfe) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 218 | throw new NativeDaemonConnectorException( |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 219 | String.format("Invalid response from daemon (%s)", line)); |
| 220 | } |
| 221 | |
San Mehat | ec4caa0 | 2010-02-03 10:48:21 -0800 | [diff] [blame] | 222 | if ((code >= 200) && (code < 600)) { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 223 | complete = true; |
San Mehat | ec4caa0 | 2010-02-03 10:48:21 -0800 | [diff] [blame] | 224 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 225 | response.add(line); |
| 226 | } catch (InterruptedException ex) { |
San Mehat | ec4caa0 | 2010-02-03 10:48:21 -0800 | [diff] [blame] | 227 | Log.e(TAG, "Failed to process response", ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | if (code >= ResponseCode.FailedRangeStart && |
| 232 | code <= ResponseCode.FailedRangeEnd) { |
San Mehat | ec4caa0 | 2010-02-03 10:48:21 -0800 | [diff] [blame] | 233 | /* |
| 234 | * Note: The format of the last response in this case is |
| 235 | * "NNN <errmsg>" |
| 236 | */ |
| 237 | throw new NativeDaemonConnectorException( |
| 238 | code, cmd, response.get(response.size()-1).substring(4)); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 239 | } |
| 240 | return response; |
| 241 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * Issues a list command and returns the cooked list |
| 245 | */ |
| 246 | public String[] doListCommand(String cmd, int expectedResponseCode) |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 247 | throws NativeDaemonConnectorException { |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 248 | |
| 249 | ArrayList<String> rsp = doCommand(cmd); |
| 250 | String[] rdata = new String[rsp.size()-1]; |
| 251 | int idx = 0; |
| 252 | |
San Mehat | 1ff4371 | 2010-02-04 15:09:02 -0800 | [diff] [blame] | 253 | for (int i = 0; i < rsp.size(); i++) { |
| 254 | String line = rsp.get(i); |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 255 | try { |
| 256 | String[] tok = line.split(" "); |
| 257 | int code = Integer.parseInt(tok[0]); |
| 258 | if (code == expectedResponseCode) { |
San Mehat | 80120b4 | 2010-01-26 12:48:39 -0800 | [diff] [blame] | 259 | rdata[idx++] = line.substring(tok[0].length() + 1); |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 260 | } else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) { |
San Mehat | 1ff4371 | 2010-02-04 15:09:02 -0800 | [diff] [blame] | 261 | if (LOCAL_LOGD) Log.d(TAG, String.format("List terminated with {%s}", line)); |
San Mehat | 4086f75 | 2010-02-17 09:03:29 -0800 | [diff] [blame^] | 262 | int last = rsp.size() -1; |
| 263 | if (i != last) { |
| 264 | Log.w(TAG, String.format("Recv'd %d lines after end of list {%s}", (last-i), cmd)); |
| 265 | for (int j = i; j <= last ; j++) { |
| 266 | Log.w(TAG, String.format("ExtraData <%s>", rsp.get(i))); |
| 267 | } |
San Mehat | 1ff4371 | 2010-02-04 15:09:02 -0800 | [diff] [blame] | 268 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 269 | return rdata; |
| 270 | } else { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 271 | throw new NativeDaemonConnectorException( |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 272 | String.format("Expected list response %d, but got %d", |
| 273 | expectedResponseCode, code)); |
| 274 | } |
| 275 | } catch (NumberFormatException nfe) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 276 | throw new NativeDaemonConnectorException( |
| 277 | String.format("Error reading code '%s'", line)); |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 280 | throw new NativeDaemonConnectorException("Got an empty response"); |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 281 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 282 | } |