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 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 19 | import android.net.LocalSocket; |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 20 | import android.net.LocalSocketAddress; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
| 22 | import android.os.HandlerThread; |
| 23 | import android.os.Message; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 24 | import android.os.SystemClock; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 25 | import android.util.LocalLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 26 | import android.util.Slog; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 27 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 28 | import com.google.android.collect.Lists; |
| 29 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 30 | import java.nio.charset.Charsets; |
| 31 | import java.io.FileDescriptor; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.io.OutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 35 | import java.io.PrintWriter; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 36 | import java.util.ArrayList; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 37 | import java.util.concurrent.atomic.AtomicInteger; |
| 38 | import java.util.LinkedList; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 41 | * Generic connector class for interfacing with a native daemon which uses the |
| 42 | * {@code libsysutils} FrameworkListener protocol. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 43 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 44 | final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdog.Monitor { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 45 | private static final boolean LOGD = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 46 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 47 | private final String TAG; |
| 48 | |
| 49 | private String mSocket; |
| 50 | private OutputStream mOutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 51 | private LocalLog mLocalLog; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 52 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 53 | private final ResponseQueue mResponseQueue; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 54 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 55 | private INativeDaemonConnectorCallbacks mCallbacks; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 56 | private Handler mCallbackHandler; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 57 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 58 | private AtomicInteger mSequenceNumber; |
| 59 | |
| 60 | private static final int DEFAULT_TIMEOUT = 1 * 60 * 1000; /* 1 minute */ |
| 61 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 62 | /** Lock held whenever communicating with native daemon. */ |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 63 | private final Object mDaemonLock = new Object(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 64 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 65 | private final int BUFFER_SIZE = 4096; |
| 66 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 67 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket, |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 68 | int responseQueueSize, String logTag, int maxLogSize) { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 69 | mCallbacks = callbacks; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 70 | mSocket = socket; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 71 | mResponseQueue = new ResponseQueue(responseQueueSize); |
| 72 | mSequenceNumber = new AtomicInteger(0); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 73 | TAG = logTag != null ? logTag : "NativeDaemonConnector"; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 74 | mLocalLog = new LocalLog(maxLogSize); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 77 | @Override |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 78 | public void run() { |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 79 | HandlerThread thread = new HandlerThread(TAG + ".CallbackHandler"); |
| 80 | thread.start(); |
| 81 | mCallbackHandler = new Handler(thread.getLooper(), this); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 82 | |
| 83 | while (true) { |
| 84 | try { |
| 85 | listenToSocket(); |
| 86 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 87 | loge("Error in NativeDaemonConnector: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 88 | SystemClock.sleep(5000); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public boolean handleMessage(Message msg) { |
| 95 | String event = (String) msg.obj; |
| 96 | try { |
| 97 | if (!mCallbacks.onEvent(msg.what, event, event.split(" "))) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 98 | log(String.format("Unhandled event '%s'", event)); |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 99 | } |
| 100 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 101 | loge("Error handling '" + event + "': " + e); |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 106 | private void listenToSocket() throws IOException { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 107 | LocalSocket socket = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 108 | |
| 109 | try { |
| 110 | socket = new LocalSocket(); |
| 111 | LocalSocketAddress address = new LocalSocketAddress(mSocket, |
| 112 | LocalSocketAddress.Namespace.RESERVED); |
| 113 | |
| 114 | socket.connect(address); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 115 | |
| 116 | InputStream inputStream = socket.getInputStream(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 117 | synchronized (mDaemonLock) { |
| 118 | mOutputStream = socket.getOutputStream(); |
| 119 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 120 | |
anga | 030bc88 | 2011-02-01 14:10:25 +0100 | [diff] [blame] | 121 | mCallbacks.onDaemonConnected(); |
| 122 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 123 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 124 | int start = 0; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 125 | |
| 126 | while (true) { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 127 | int count = inputStream.read(buffer, start, BUFFER_SIZE - start); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 128 | if (count < 0) { |
| 129 | loge("got " + count + " reading with start = " + start); |
| 130 | break; |
| 131 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 132 | |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 133 | // Add our starting point to the count and reset the start. |
| 134 | count += start; |
| 135 | start = 0; |
| 136 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 137 | for (int i = 0; i < count; i++) { |
| 138 | if (buffer[i] == 0) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 139 | final String rawEvent = new String( |
| 140 | buffer, start, i - start, Charsets.UTF_8); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 141 | log("RCV <- {" + rawEvent + "}"); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 142 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 143 | try { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 144 | final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent( |
| 145 | rawEvent); |
| 146 | if (event.isClassUnsolicited()) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 147 | // TODO: migrate to sending NativeDaemonEvent instances |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 148 | mCallbackHandler.sendMessage(mCallbackHandler.obtainMessage( |
| 149 | event.getCode(), event.getRawEvent())); |
Irfan Sheriff | 1cd94ef | 2011-01-16 14:31:55 -0800 | [diff] [blame] | 150 | } else { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 151 | mResponseQueue.add(event.getCmdNumber(), event); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 152 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 153 | } catch (IllegalArgumentException e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 154 | log("Problem parsing message: " + rawEvent + " - " + e); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 155 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 156 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 157 | start = i + 1; |
| 158 | } |
| 159 | } |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 160 | if (start == 0) { |
| 161 | final String rawEvent = new String(buffer, start, count, Charsets.UTF_8); |
| 162 | log("RCV incomplete <- {" + rawEvent + "}"); |
| 163 | } |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 164 | |
| 165 | // We should end at the amount we read. If not, compact then |
| 166 | // buffer and read again. |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 167 | if (start != count) { |
| 168 | final int remaining = BUFFER_SIZE - start; |
| 169 | System.arraycopy(buffer, start, buffer, 0, remaining); |
| 170 | start = remaining; |
| 171 | } else { |
| 172 | start = 0; |
| 173 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 174 | } |
| 175 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 176 | loge("Communications error: " + ex); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 177 | throw ex; |
| 178 | } finally { |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 179 | synchronized (mDaemonLock) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 180 | if (mOutputStream != null) { |
| 181 | try { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 182 | loge("closing stream for " + mSocket); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 183 | mOutputStream.close(); |
| 184 | } catch (IOException e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 185 | loge("Failed closing output stream: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 186 | } |
| 187 | mOutputStream = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 188 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 189 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 190 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 191 | try { |
| 192 | if (socket != null) { |
| 193 | socket.close(); |
| 194 | } |
| 195 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 196 | loge("Failed closing socket: " + ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 199 | } |
| 200 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 201 | /** |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 202 | * Make command for daemon, escaping arguments as needed. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 203 | */ |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 204 | private void makeCommand(StringBuilder builder, String cmd, Object... args) |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 205 | throws NativeDaemonConnectorException { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 206 | // TODO: eventually enforce that cmd doesn't contain arguments |
| 207 | if (cmd.indexOf('\0') >= 0) { |
| 208 | throw new IllegalArgumentException("unexpected command: " + cmd); |
Jeff Sharkey | b0aec07 | 2011-10-14 18:32:24 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 211 | builder.append(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 212 | for (Object arg : args) { |
| 213 | final String argString = String.valueOf(arg); |
| 214 | if (argString.indexOf('\0') >= 0) { |
| 215 | throw new IllegalArgumentException("unexpected argument: " + arg); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 216 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 217 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 218 | builder.append(' '); |
| 219 | appendEscaped(builder, argString); |
| 220 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 221 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 222 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 223 | /** |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 224 | * Issue the given command to the native daemon and return a single expected |
| 225 | * response. |
| 226 | * |
| 227 | * @throws NativeDaemonConnectorException when problem communicating with |
| 228 | * native daemon, or if the response matches |
| 229 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 230 | * {@link NativeDaemonEvent#isClassServerError()}. |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 231 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 232 | public NativeDaemonEvent execute(Command cmd) throws NativeDaemonConnectorException { |
| 233 | return execute(cmd.mCmd, cmd.mArguments.toArray()); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Issue the given command to the native daemon and return a single expected |
| 238 | * response. |
| 239 | * |
| 240 | * @throws NativeDaemonConnectorException when problem communicating with |
| 241 | * native daemon, or if the response matches |
| 242 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 243 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 244 | */ |
| 245 | public NativeDaemonEvent execute(String cmd, Object... args) |
| 246 | throws NativeDaemonConnectorException { |
| 247 | final NativeDaemonEvent[] events = executeForList(cmd, args); |
| 248 | if (events.length != 1) { |
| 249 | throw new NativeDaemonConnectorException( |
| 250 | "Expected exactly one response, but received " + events.length); |
| 251 | } |
| 252 | return events[0]; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Issue the given command to the native daemon and return any |
| 257 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 258 | * final terminal response. |
| 259 | * |
| 260 | * @throws NativeDaemonConnectorException when problem communicating with |
| 261 | * native daemon, or if the response matches |
| 262 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 263 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 264 | */ |
| 265 | public NativeDaemonEvent[] executeForList(Command cmd) throws NativeDaemonConnectorException { |
| 266 | return executeForList(cmd.mCmd, cmd.mArguments.toArray()); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Issue the given command to the native daemon and return any |
| 271 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 272 | * final terminal response. |
| 273 | * |
| 274 | * @throws NativeDaemonConnectorException when problem communicating with |
| 275 | * native daemon, or if the response matches |
| 276 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 277 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 278 | */ |
| 279 | public NativeDaemonEvent[] executeForList(String cmd, Object... args) |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 280 | throws NativeDaemonConnectorException { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 281 | return execute(DEFAULT_TIMEOUT, cmd, args); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 282 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 283 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 284 | /** |
| 285 | * Issue the given command to the native daemon and return any |
| 286 | * {@linke NativeDaemonEvent@isClassContinue()} responses, including the |
| 287 | * final terminal response. Note that the timeout does not count time in |
| 288 | * deep sleep. |
| 289 | * |
| 290 | * @throws NativeDaemonConnectorException when problem communicating with |
| 291 | * native daemon, or if the response matches |
| 292 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 293 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 294 | */ |
| 295 | public NativeDaemonEvent[] execute(int timeout, String cmd, Object... args) |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 296 | throws NativeDaemonConnectorException { |
| 297 | final ArrayList<NativeDaemonEvent> events = Lists.newArrayList(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 298 | |
| 299 | final int sequenceNumber = mSequenceNumber.incrementAndGet(); |
| 300 | final StringBuilder cmdBuilder = |
| 301 | new StringBuilder(Integer.toString(sequenceNumber)).append(' '); |
| 302 | |
| 303 | makeCommand(cmdBuilder, cmd, args); |
| 304 | |
| 305 | final String logCmd = cmdBuilder.toString(); /* includes cmdNum, cmd, args */ |
| 306 | log("SND -> {" + logCmd + "}"); |
| 307 | |
| 308 | cmdBuilder.append('\0'); |
| 309 | final String sentCmd = cmdBuilder.toString(); /* logCmd + \0 */ |
| 310 | |
| 311 | synchronized (mDaemonLock) { |
| 312 | if (mOutputStream == null) { |
| 313 | throw new NativeDaemonConnectorException("missing output stream"); |
| 314 | } else { |
| 315 | try { |
| 316 | mOutputStream.write(sentCmd.getBytes(Charsets.UTF_8)); |
| 317 | } catch (IOException e) { |
| 318 | throw new NativeDaemonConnectorException("problem sending command", e); |
| 319 | } |
| 320 | } |
| 321 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 322 | |
| 323 | NativeDaemonEvent event = null; |
| 324 | do { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 325 | event = mResponseQueue.remove(sequenceNumber, timeout, sentCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 326 | if (event == null) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 327 | loge("timed-out waiting for response to " + logCmd); |
| 328 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 329 | } |
| 330 | events.add(event); |
| 331 | } while (event.isClassContinue()); |
| 332 | |
| 333 | if (event.isClassClientError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 334 | throw new NativeDaemonArgumentException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 335 | } |
| 336 | if (event.isClassServerError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 337 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return events.toArray(new NativeDaemonEvent[events.size()]); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Issue a command to the native daemon and return the raw responses. |
| 345 | * |
| 346 | * @deprecated callers should move to {@link #execute(String, Object...)} |
| 347 | * which returns parsed {@link NativeDaemonEvent}. |
| 348 | */ |
| 349 | @Deprecated |
| 350 | public ArrayList<String> doCommand(String cmd) throws NativeDaemonConnectorException { |
| 351 | final ArrayList<String> rawEvents = Lists.newArrayList(); |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 352 | final NativeDaemonEvent[] events = executeForList(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 353 | for (NativeDaemonEvent event : events) { |
| 354 | rawEvents.add(event.getRawEvent()); |
| 355 | } |
| 356 | return rawEvents; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Issues a list command and returns the cooked list of all |
| 361 | * {@link NativeDaemonEvent#getMessage()} which match requested code. |
| 362 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 363 | @Deprecated |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 364 | public String[] doListCommand(String cmd, int expectedCode) |
| 365 | throws NativeDaemonConnectorException { |
| 366 | final ArrayList<String> list = Lists.newArrayList(); |
| 367 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 368 | final NativeDaemonEvent[] events = executeForList(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 369 | for (int i = 0; i < events.length - 1; i++) { |
| 370 | final NativeDaemonEvent event = events[i]; |
| 371 | final int code = event.getCode(); |
| 372 | if (code == expectedCode) { |
| 373 | list.add(event.getMessage()); |
| 374 | } else { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 375 | throw new NativeDaemonConnectorException( |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 376 | "unexpected list response " + code + " instead of " + expectedCode); |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 379 | |
| 380 | final NativeDaemonEvent finalEvent = events[events.length - 1]; |
| 381 | if (!finalEvent.isClassOk()) { |
| 382 | throw new NativeDaemonConnectorException("unexpected final event: " + finalEvent); |
| 383 | } |
| 384 | |
| 385 | return list.toArray(new String[list.size()]); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Append the given argument to {@link StringBuilder}, escaping as needed, |
| 390 | * and surrounding with quotes when it contains spaces. |
| 391 | */ |
| 392 | // @VisibleForTesting |
| 393 | static void appendEscaped(StringBuilder builder, String arg) { |
| 394 | final boolean hasSpaces = arg.indexOf(' ') >= 0; |
| 395 | if (hasSpaces) { |
| 396 | builder.append('"'); |
| 397 | } |
| 398 | |
| 399 | final int length = arg.length(); |
| 400 | for (int i = 0; i < length; i++) { |
| 401 | final char c = arg.charAt(i); |
| 402 | |
| 403 | if (c == '"') { |
| 404 | builder.append("\\\""); |
| 405 | } else if (c == '\\') { |
| 406 | builder.append("\\\\"); |
| 407 | } else { |
| 408 | builder.append(c); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (hasSpaces) { |
| 413 | builder.append('"'); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | private static class NativeDaemonArgumentException extends NativeDaemonConnectorException { |
| 418 | public NativeDaemonArgumentException(String command, NativeDaemonEvent event) { |
| 419 | super(command, event); |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public IllegalArgumentException rethrowAsParcelableException() { |
| 424 | throw new IllegalArgumentException(getMessage(), this); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | private static class NativeDaemonFailureException extends NativeDaemonConnectorException { |
| 429 | public NativeDaemonFailureException(String command, NativeDaemonEvent event) { |
| 430 | super(command, event); |
| 431 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 432 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 433 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 434 | /** |
| 435 | * Command builder that handles argument list building. |
| 436 | */ |
| 437 | public static class Command { |
| 438 | private String mCmd; |
| 439 | private ArrayList<Object> mArguments = Lists.newArrayList(); |
| 440 | |
| 441 | public Command(String cmd, Object... args) { |
| 442 | mCmd = cmd; |
| 443 | for (Object arg : args) { |
| 444 | appendArg(arg); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | public Command appendArg(Object arg) { |
| 449 | mArguments.add(arg); |
| 450 | return this; |
| 451 | } |
| 452 | } |
| 453 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 454 | /** {@inheritDoc} */ |
| 455 | public void monitor() { |
| 456 | synchronized (mDaemonLock) { } |
| 457 | } |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 458 | |
| 459 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 460 | mLocalLog.dump(fd, pw, args); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 461 | pw.println(); |
| 462 | mResponseQueue.dump(fd, pw, args); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | private void log(String logstring) { |
| 466 | if (LOGD) Slog.d(TAG, logstring); |
| 467 | mLocalLog.log(logstring); |
| 468 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 469 | |
| 470 | private void loge(String logstring) { |
| 471 | Slog.e(TAG, logstring); |
| 472 | mLocalLog.log(logstring); |
| 473 | } |
| 474 | |
| 475 | private static class ResponseQueue { |
| 476 | |
| 477 | private static class Response { |
| 478 | public int cmdNum; |
| 479 | public LinkedList<NativeDaemonEvent> responses = new LinkedList<NativeDaemonEvent>(); |
| 480 | public String request; |
| 481 | public Response(int c, String r) {cmdNum = c; request = r;} |
| 482 | } |
| 483 | |
| 484 | private final LinkedList<Response> mResponses; |
| 485 | private int mMaxCount; |
| 486 | |
| 487 | ResponseQueue(int maxCount) { |
| 488 | mResponses = new LinkedList<Response>(); |
| 489 | mMaxCount = maxCount; |
| 490 | } |
| 491 | |
| 492 | public void add(int cmdNum, NativeDaemonEvent response) { |
| 493 | Response found = null; |
| 494 | synchronized (mResponses) { |
| 495 | for (Response r : mResponses) { |
| 496 | if (r.cmdNum == cmdNum) { |
| 497 | found = r; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | if (found == null) { |
| 502 | // didn't find it - make sure our queue isn't too big before adding |
| 503 | // another.. |
| 504 | while (mResponses.size() >= mMaxCount) { |
| 505 | Slog.e("NativeDaemonConnector.ResponseQueue", |
| 506 | "more buffered than allowed: " + mResponses.size() + |
| 507 | " >= " + mMaxCount); |
| 508 | // let any waiter timeout waiting for this |
| 509 | Response r = mResponses.remove(); |
| 510 | Slog.e("NativeDaemonConnector.ResponseQueue", |
| 511 | "Removing request: " + r.request + " (" + r.cmdNum + ")"); |
| 512 | } |
| 513 | found = new Response(cmdNum, null); |
| 514 | mResponses.add(found); |
| 515 | } |
| 516 | found.responses.add(response); |
| 517 | } |
| 518 | synchronized (found) { |
| 519 | found.notify(); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | // note that the timeout does not count time in deep sleep. If you don't want |
| 524 | // the device to sleep, hold a wakelock |
| 525 | public NativeDaemonEvent remove(int cmdNum, int timeoutMs, String origCmd) { |
| 526 | long endTime = SystemClock.uptimeMillis() + timeoutMs; |
| 527 | long nowTime; |
| 528 | Response found = null; |
| 529 | while (true) { |
| 530 | synchronized (mResponses) { |
| 531 | for (Response response : mResponses) { |
| 532 | if (response.cmdNum == cmdNum) { |
| 533 | found = response; |
| 534 | // how many response fragments are left |
| 535 | switch (response.responses.size()) { |
| 536 | case 0: // haven't got any - must wait |
| 537 | break; |
| 538 | case 1: // last one - remove this from the master list |
| 539 | mResponses.remove(response); // fall through |
| 540 | default: // take one and move on |
| 541 | response.request = origCmd; |
| 542 | return response.responses.remove(); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | nowTime = SystemClock.uptimeMillis(); |
| 547 | if (endTime <= nowTime) { |
| 548 | Slog.e("NativeDaemonConnector.ResponseQueue", |
| 549 | "Timeout waiting for response"); |
| 550 | return null; |
| 551 | } |
| 552 | /* pre-allocate so we have something unique to wait on */ |
| 553 | if (found == null) { |
| 554 | found = new Response(cmdNum, origCmd); |
| 555 | mResponses.add(found); |
| 556 | } |
| 557 | } |
| 558 | try { |
| 559 | synchronized (found) { |
| 560 | found.wait(endTime - nowTime); |
| 561 | } |
| 562 | } catch (InterruptedException e) { |
| 563 | // loop around to check if we're done or if it's time to stop waiting |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 569 | pw.println("Pending requests:"); |
| 570 | synchronized (mResponses) { |
| 571 | for (Response response : mResponses) { |
| 572 | pw.println(" Cmd " + response.cmdNum + " - " + response.request); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 577 | } |