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; |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 21 | import android.os.Build; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 23 | import android.os.Message; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 24 | import android.os.PowerManager; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 25 | import android.os.SystemClock; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 26 | import android.util.LocalLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 27 | import android.util.Slog; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 28 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 29 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 30 | import com.google.android.collect.Lists; |
| 31 | |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 32 | import java.io.FileDescriptor; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | import java.io.OutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 36 | import java.io.PrintWriter; |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 37 | import java.nio.charset.StandardCharsets; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 38 | import java.util.ArrayList; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 39 | import java.util.concurrent.atomic.AtomicInteger; |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 40 | import java.util.concurrent.ArrayBlockingQueue; |
| 41 | import java.util.concurrent.BlockingQueue; |
| 42 | import java.util.concurrent.TimeUnit; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 43 | import java.util.LinkedList; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 46 | * Generic connector class for interfacing with a native daemon which uses the |
| 47 | * {@code libsysutils} FrameworkListener protocol. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 48 | */ |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 49 | final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdog.Monitor { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 50 | private static final boolean LOGD = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 51 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 52 | private final String TAG; |
| 53 | |
| 54 | private String mSocket; |
| 55 | private OutputStream mOutputStream; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 56 | private LocalLog mLocalLog; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 57 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 58 | private final ResponseQueue mResponseQueue; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 59 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 60 | private final PowerManager.WakeLock mWakeLock; |
| 61 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 62 | private INativeDaemonConnectorCallbacks mCallbacks; |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 63 | private Handler mCallbackHandler; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 64 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 65 | private AtomicInteger mSequenceNumber; |
| 66 | |
| 67 | private static final int DEFAULT_TIMEOUT = 1 * 60 * 1000; /* 1 minute */ |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 68 | private static final long WARN_EXECUTE_DELAY_MS = 500; /* .5 sec */ |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 69 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 70 | /** Lock held whenever communicating with native daemon. */ |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 71 | private final Object mDaemonLock = new Object(); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 72 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 73 | private final int BUFFER_SIZE = 4096; |
| 74 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 75 | NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket, |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 76 | int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl) { |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 77 | mCallbacks = callbacks; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 78 | mSocket = socket; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 79 | mResponseQueue = new ResponseQueue(responseQueueSize); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 80 | mWakeLock = wl; |
| 81 | if (mWakeLock != null) { |
| 82 | mWakeLock.setReferenceCounted(true); |
| 83 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 84 | mSequenceNumber = new AtomicInteger(0); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 85 | TAG = logTag != null ? logTag : "NativeDaemonConnector"; |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 86 | mLocalLog = new LocalLog(maxLogSize); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 89 | @Override |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 90 | public void run() { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 91 | mCallbackHandler = new Handler(FgThread.get().getLooper(), this); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 92 | |
| 93 | while (true) { |
| 94 | try { |
| 95 | listenToSocket(); |
| 96 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 97 | loge("Error in NativeDaemonConnector: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 98 | SystemClock.sleep(5000); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 103 | @Override |
| 104 | public boolean handleMessage(Message msg) { |
| 105 | String event = (String) msg.obj; |
| 106 | try { |
Robert Greenwalt | 2d34b4a | 2012-04-20 13:08:02 -0700 | [diff] [blame] | 107 | if (!mCallbacks.onEvent(msg.what, event, NativeDaemonEvent.unescapeArgs(event))) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 108 | log(String.format("Unhandled event '%s'", event)); |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 109 | } |
| 110 | } catch (Exception e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 111 | loge("Error handling '" + event + "': " + e); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 112 | } finally { |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 113 | if (mCallbacks.onCheckHoldWakeLock(msg.what) && mWakeLock != null) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 114 | mWakeLock.release(); |
| 115 | } |
Chia-chi Yeh | e5750a3 | 2011-08-03 14:42:11 -0700 | [diff] [blame] | 116 | } |
| 117 | return true; |
| 118 | } |
| 119 | |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 120 | private LocalSocketAddress determineSocketAddress() { |
| 121 | // If we're testing, set up a socket in a namespace that's accessible to test code. |
| 122 | // In order to ensure that unprivileged apps aren't able to impersonate native daemons on |
| 123 | // production devices, even if said native daemons ill-advisedly pick a socket name that |
| 124 | // starts with __test__, only allow this on debug builds. |
| 125 | if (mSocket.startsWith("__test__") && Build.IS_DEBUGGABLE) { |
| 126 | return new LocalSocketAddress(mSocket); |
| 127 | } else { |
| 128 | return new LocalSocketAddress(mSocket, LocalSocketAddress.Namespace.RESERVED); |
| 129 | } |
| 130 | } |
| 131 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 132 | private void listenToSocket() throws IOException { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 133 | LocalSocket socket = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 134 | |
| 135 | try { |
| 136 | socket = new LocalSocket(); |
Lorenzo Colitti | 7421a01 | 2013-08-20 22:51:24 +0900 | [diff] [blame] | 137 | LocalSocketAddress address = determineSocketAddress(); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 138 | |
| 139 | socket.connect(address); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 140 | |
| 141 | InputStream inputStream = socket.getInputStream(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 142 | synchronized (mDaemonLock) { |
| 143 | mOutputStream = socket.getOutputStream(); |
| 144 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 145 | |
anga | 030bc88 | 2011-02-01 14:10:25 +0100 | [diff] [blame] | 146 | mCallbacks.onDaemonConnected(); |
| 147 | |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 148 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 149 | int start = 0; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 150 | |
| 151 | while (true) { |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 152 | int count = inputStream.read(buffer, start, BUFFER_SIZE - start); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 153 | if (count < 0) { |
| 154 | loge("got " + count + " reading with start = " + start); |
| 155 | break; |
| 156 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 157 | |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 158 | // Add our starting point to the count and reset the start. |
| 159 | count += start; |
| 160 | start = 0; |
| 161 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 162 | for (int i = 0; i < count; i++) { |
| 163 | if (buffer[i] == 0) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 164 | final String rawEvent = new String( |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 165 | buffer, start, i - start, StandardCharsets.UTF_8); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 166 | log("RCV <- {" + rawEvent + "}"); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 167 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 168 | boolean releaseWl = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 169 | try { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 170 | final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent( |
| 171 | rawEvent); |
| 172 | if (event.isClassUnsolicited()) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 173 | // TODO: migrate to sending NativeDaemonEvent instances |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 174 | if (mCallbacks.onCheckHoldWakeLock(event.getCode()) |
| 175 | && mWakeLock != null) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 176 | mWakeLock.acquire(); |
| 177 | releaseWl = true; |
| 178 | } |
| 179 | if (mCallbackHandler.sendMessage(mCallbackHandler.obtainMessage( |
| 180 | event.getCode(), event.getRawEvent()))) { |
| 181 | releaseWl = false; |
| 182 | } |
Irfan Sheriff | 1cd94ef | 2011-01-16 14:31:55 -0800 | [diff] [blame] | 183 | } else { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 184 | mResponseQueue.add(event.getCmdNumber(), event); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 185 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 186 | } catch (IllegalArgumentException e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 187 | log("Problem parsing message: " + rawEvent + " - " + e); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 188 | } finally { |
| 189 | if (releaseWl) { |
| 190 | mWakeLock.acquire(); |
| 191 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 192 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 193 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 194 | start = i + 1; |
| 195 | } |
| 196 | } |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 197 | if (start == 0) { |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 198 | final String rawEvent = new String(buffer, start, count, StandardCharsets.UTF_8); |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 199 | log("RCV incomplete <- {" + rawEvent + "}"); |
| 200 | } |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 201 | |
| 202 | // We should end at the amount we read. If not, compact then |
| 203 | // buffer and read again. |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 204 | if (start != count) { |
| 205 | final int remaining = BUFFER_SIZE - start; |
| 206 | System.arraycopy(buffer, start, buffer, 0, remaining); |
| 207 | start = remaining; |
| 208 | } else { |
| 209 | start = 0; |
| 210 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 211 | } |
| 212 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 213 | loge("Communications error: " + ex); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 214 | throw ex; |
| 215 | } finally { |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 216 | synchronized (mDaemonLock) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 217 | if (mOutputStream != null) { |
| 218 | try { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 219 | loge("closing stream for " + mSocket); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 220 | mOutputStream.close(); |
| 221 | } catch (IOException e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 222 | loge("Failed closing output stream: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 223 | } |
| 224 | mOutputStream = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 225 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 226 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 227 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 228 | try { |
| 229 | if (socket != null) { |
| 230 | socket.close(); |
| 231 | } |
| 232 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 233 | loge("Failed closing socket: " + ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 236 | } |
| 237 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 238 | /** |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 239 | * Wrapper around argument that indicates it's sensitive and shouldn't be |
| 240 | * logged. |
| 241 | */ |
| 242 | public static class SensitiveArg { |
| 243 | private final Object mArg; |
| 244 | |
| 245 | public SensitiveArg(Object arg) { |
| 246 | mArg = arg; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public String toString() { |
| 251 | return String.valueOf(mArg); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 256 | * Make command for daemon, escaping arguments as needed. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 257 | */ |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 258 | @VisibleForTesting |
| 259 | static void makeCommand(StringBuilder rawBuilder, StringBuilder logBuilder, int sequenceNumber, |
| 260 | String cmd, Object... args) { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 261 | if (cmd.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 262 | throw new IllegalArgumentException("Unexpected command: " + cmd); |
| 263 | } |
| 264 | if (cmd.indexOf(' ') >= 0) { |
| 265 | throw new IllegalArgumentException("Arguments must be separate from command"); |
Jeff Sharkey | b0aec07 | 2011-10-14 18:32:24 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 268 | rawBuilder.append(sequenceNumber).append(' ').append(cmd); |
| 269 | logBuilder.append(sequenceNumber).append(' ').append(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 270 | for (Object arg : args) { |
| 271 | final String argString = String.valueOf(arg); |
| 272 | if (argString.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 273 | throw new IllegalArgumentException("Unexpected argument: " + arg); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 274 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 275 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 276 | rawBuilder.append(' '); |
| 277 | logBuilder.append(' '); |
| 278 | |
| 279 | appendEscaped(rawBuilder, argString); |
| 280 | if (arg instanceof SensitiveArg) { |
| 281 | logBuilder.append("[scrubbed]"); |
| 282 | } else { |
| 283 | appendEscaped(logBuilder, argString); |
| 284 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 285 | } |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 286 | |
| 287 | rawBuilder.append('\0'); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 288 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 289 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 290 | /** |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 291 | * Issue the given command to the native daemon and return a single expected |
| 292 | * response. |
| 293 | * |
| 294 | * @throws NativeDaemonConnectorException when problem communicating with |
| 295 | * native daemon, or if the response matches |
| 296 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 297 | * {@link NativeDaemonEvent#isClassServerError()}. |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 298 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 299 | public NativeDaemonEvent execute(Command cmd) throws NativeDaemonConnectorException { |
| 300 | return execute(cmd.mCmd, cmd.mArguments.toArray()); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Issue the given command to the native daemon and return a single expected |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 305 | * response. Any arguments must be separated from base command so they can |
| 306 | * be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 307 | * |
| 308 | * @throws NativeDaemonConnectorException when problem communicating with |
| 309 | * native daemon, or if the response matches |
| 310 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 311 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 312 | */ |
| 313 | public NativeDaemonEvent execute(String cmd, Object... args) |
| 314 | throws NativeDaemonConnectorException { |
| 315 | final NativeDaemonEvent[] events = executeForList(cmd, args); |
| 316 | if (events.length != 1) { |
| 317 | throw new NativeDaemonConnectorException( |
| 318 | "Expected exactly one response, but received " + events.length); |
| 319 | } |
| 320 | return events[0]; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Issue the given command to the native daemon and return any |
| 325 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 326 | * final terminal response. |
| 327 | * |
| 328 | * @throws NativeDaemonConnectorException when problem communicating with |
| 329 | * native daemon, or if the response matches |
| 330 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 331 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 332 | */ |
| 333 | public NativeDaemonEvent[] executeForList(Command cmd) throws NativeDaemonConnectorException { |
| 334 | return executeForList(cmd.mCmd, cmd.mArguments.toArray()); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Issue the given command to the native daemon and return any |
| 339 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 340 | * final terminal response. Any arguments must be separated from base |
| 341 | * command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 342 | * |
| 343 | * @throws NativeDaemonConnectorException when problem communicating with |
| 344 | * native daemon, or if the response matches |
| 345 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 346 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 347 | */ |
| 348 | public NativeDaemonEvent[] executeForList(String cmd, Object... args) |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 349 | throws NativeDaemonConnectorException { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 350 | return execute(DEFAULT_TIMEOUT, cmd, args); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 351 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 352 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 353 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 354 | * Issue the given command to the native daemon and return any {@linke |
| 355 | * NativeDaemonEvent@isClassContinue()} responses, including the final |
| 356 | * terminal response. Note that the timeout does not count time in deep |
| 357 | * sleep. Any arguments must be separated from base command so they can be |
| 358 | * properly escaped. |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 359 | * |
| 360 | * @throws NativeDaemonConnectorException when problem communicating with |
| 361 | * native daemon, or if the response matches |
| 362 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 363 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 364 | */ |
| 365 | public NativeDaemonEvent[] execute(int timeout, String cmd, Object... args) |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 366 | throws NativeDaemonConnectorException { |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 367 | final long startTime = SystemClock.elapsedRealtime(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 368 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 369 | final ArrayList<NativeDaemonEvent> events = Lists.newArrayList(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 370 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 371 | final StringBuilder rawBuilder = new StringBuilder(); |
| 372 | final StringBuilder logBuilder = new StringBuilder(); |
| 373 | final int sequenceNumber = mSequenceNumber.incrementAndGet(); |
| 374 | |
| 375 | makeCommand(rawBuilder, logBuilder, sequenceNumber, cmd, args); |
| 376 | |
| 377 | final String rawCmd = rawBuilder.toString(); |
| 378 | final String logCmd = logBuilder.toString(); |
| 379 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 380 | log("SND -> {" + logCmd + "}"); |
| 381 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 382 | synchronized (mDaemonLock) { |
| 383 | if (mOutputStream == null) { |
| 384 | throw new NativeDaemonConnectorException("missing output stream"); |
| 385 | } else { |
| 386 | try { |
Elliott Hughes | b829283 | 2013-06-28 16:50:13 -0700 | [diff] [blame] | 387 | mOutputStream.write(rawCmd.getBytes(StandardCharsets.UTF_8)); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 388 | } catch (IOException e) { |
| 389 | throw new NativeDaemonConnectorException("problem sending command", e); |
| 390 | } |
| 391 | } |
| 392 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 393 | |
| 394 | NativeDaemonEvent event = null; |
| 395 | do { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 396 | event = mResponseQueue.remove(sequenceNumber, timeout, logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 397 | if (event == null) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 398 | loge("timed-out waiting for response to " + logCmd); |
| 399 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 400 | } |
Robert Greenwalt | b5aff3f | 2012-05-15 17:26:57 -0700 | [diff] [blame] | 401 | log("RMV <- {" + event + "}"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 402 | events.add(event); |
| 403 | } while (event.isClassContinue()); |
| 404 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 405 | final long endTime = SystemClock.elapsedRealtime(); |
| 406 | if (endTime - startTime > WARN_EXECUTE_DELAY_MS) { |
| 407 | loge("NDC Command {" + logCmd + "} took too long (" + (endTime - startTime) + "ms)"); |
| 408 | } |
| 409 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 410 | if (event.isClassClientError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 411 | throw new NativeDaemonArgumentException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 412 | } |
| 413 | if (event.isClassServerError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 414 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | return events.toArray(new NativeDaemonEvent[events.size()]); |
| 418 | } |
| 419 | |
| 420 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 421 | * Append the given argument to {@link StringBuilder}, escaping as needed, |
| 422 | * and surrounding with quotes when it contains spaces. |
| 423 | */ |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 424 | @VisibleForTesting |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 425 | static void appendEscaped(StringBuilder builder, String arg) { |
| 426 | final boolean hasSpaces = arg.indexOf(' ') >= 0; |
| 427 | if (hasSpaces) { |
| 428 | builder.append('"'); |
| 429 | } |
| 430 | |
| 431 | final int length = arg.length(); |
| 432 | for (int i = 0; i < length; i++) { |
| 433 | final char c = arg.charAt(i); |
| 434 | |
| 435 | if (c == '"') { |
| 436 | builder.append("\\\""); |
| 437 | } else if (c == '\\') { |
| 438 | builder.append("\\\\"); |
| 439 | } else { |
| 440 | builder.append(c); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if (hasSpaces) { |
| 445 | builder.append('"'); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | private static class NativeDaemonArgumentException extends NativeDaemonConnectorException { |
| 450 | public NativeDaemonArgumentException(String command, NativeDaemonEvent event) { |
| 451 | super(command, event); |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public IllegalArgumentException rethrowAsParcelableException() { |
| 456 | throw new IllegalArgumentException(getMessage(), this); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | private static class NativeDaemonFailureException extends NativeDaemonConnectorException { |
| 461 | public NativeDaemonFailureException(String command, NativeDaemonEvent event) { |
| 462 | super(command, event); |
| 463 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 464 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 465 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 466 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 467 | * Command builder that handles argument list building. Any arguments must |
| 468 | * be separated from base command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 469 | */ |
| 470 | public static class Command { |
| 471 | private String mCmd; |
| 472 | private ArrayList<Object> mArguments = Lists.newArrayList(); |
| 473 | |
| 474 | public Command(String cmd, Object... args) { |
| 475 | mCmd = cmd; |
| 476 | for (Object arg : args) { |
| 477 | appendArg(arg); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | public Command appendArg(Object arg) { |
| 482 | mArguments.add(arg); |
| 483 | return this; |
| 484 | } |
| 485 | } |
| 486 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 487 | /** {@inheritDoc} */ |
| 488 | public void monitor() { |
| 489 | synchronized (mDaemonLock) { } |
| 490 | } |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 491 | |
| 492 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 493 | mLocalLog.dump(fd, pw, args); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 494 | pw.println(); |
| 495 | mResponseQueue.dump(fd, pw, args); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | private void log(String logstring) { |
| 499 | if (LOGD) Slog.d(TAG, logstring); |
| 500 | mLocalLog.log(logstring); |
| 501 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 502 | |
| 503 | private void loge(String logstring) { |
| 504 | Slog.e(TAG, logstring); |
| 505 | mLocalLog.log(logstring); |
| 506 | } |
| 507 | |
| 508 | private static class ResponseQueue { |
| 509 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 510 | private static class PendingCmd { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 511 | public final int cmdNum; |
| 512 | public final String logCmd; |
| 513 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 514 | public BlockingQueue<NativeDaemonEvent> responses = |
| 515 | new ArrayBlockingQueue<NativeDaemonEvent>(10); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 516 | |
| 517 | // The availableResponseCount member is used to track when we can remove this |
| 518 | // instance from the ResponseQueue. |
| 519 | // This is used under the protection of a sync of the mPendingCmds object. |
| 520 | // A positive value means we've had more writers retreive this object while |
| 521 | // a negative value means we've had more readers. When we've had an equal number |
| 522 | // (it goes to zero) we can remove this object from the mPendingCmds list. |
| 523 | // Note that we may have more responses for this command (and more readers |
| 524 | // coming), but that would result in a new PendingCmd instance being created |
| 525 | // and added with the same cmdNum. |
| 526 | // Also note that when this goes to zero it just means a parity of readers and |
| 527 | // writers have retrieved this object - not that they are done using it. The |
| 528 | // responses queue may well have more responses yet to be read or may get more |
| 529 | // responses added to it. But all those readers/writers have retreived and |
| 530 | // hold references to this instance already so it can be removed from |
| 531 | // mPendingCmds queue. |
| 532 | public int availableResponseCount; |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 533 | |
| 534 | public PendingCmd(int cmdNum, String logCmd) { |
| 535 | this.cmdNum = cmdNum; |
| 536 | this.logCmd = logCmd; |
| 537 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 540 | private final LinkedList<PendingCmd> mPendingCmds; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 541 | private int mMaxCount; |
| 542 | |
| 543 | ResponseQueue(int maxCount) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 544 | mPendingCmds = new LinkedList<PendingCmd>(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 545 | mMaxCount = maxCount; |
| 546 | } |
| 547 | |
| 548 | public void add(int cmdNum, NativeDaemonEvent response) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 549 | PendingCmd found = null; |
| 550 | synchronized (mPendingCmds) { |
| 551 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 552 | if (pendingCmd.cmdNum == cmdNum) { |
| 553 | found = pendingCmd; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | } |
| 557 | if (found == null) { |
| 558 | // didn't find it - make sure our queue isn't too big before adding |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 559 | while (mPendingCmds.size() >= mMaxCount) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 560 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 561 | "more buffered than allowed: " + mPendingCmds.size() + |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 562 | " >= " + mMaxCount); |
| 563 | // let any waiter timeout waiting for this |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 564 | PendingCmd pendingCmd = mPendingCmds.remove(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 565 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 566 | "Removing request: " + pendingCmd.logCmd + " (" + |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 567 | pendingCmd.cmdNum + ")"); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 568 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 569 | found = new PendingCmd(cmdNum, null); |
| 570 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 571 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 572 | found.availableResponseCount++; |
| 573 | // if a matching remove call has already retrieved this we can remove this |
| 574 | // instance from our list |
| 575 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 576 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 577 | try { |
| 578 | found.responses.put(response); |
| 579 | } catch (InterruptedException e) { } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // note that the timeout does not count time in deep sleep. If you don't want |
| 583 | // the device to sleep, hold a wakelock |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 584 | public NativeDaemonEvent remove(int cmdNum, int timeoutMs, String logCmd) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 585 | PendingCmd found = null; |
| 586 | synchronized (mPendingCmds) { |
| 587 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 588 | if (pendingCmd.cmdNum == cmdNum) { |
| 589 | found = pendingCmd; |
| 590 | break; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 593 | if (found == null) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 594 | found = new PendingCmd(cmdNum, logCmd); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 595 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 596 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 597 | found.availableResponseCount--; |
| 598 | // if a matching add call has already retrieved this we can remove this |
| 599 | // instance from our list |
| 600 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 601 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 602 | NativeDaemonEvent result = null; |
| 603 | try { |
| 604 | result = found.responses.poll(timeoutMs, TimeUnit.MILLISECONDS); |
| 605 | } catch (InterruptedException e) {} |
| 606 | if (result == null) { |
| 607 | Slog.e("NativeDaemonConnector.ResponseQueue", "Timeout waiting for response"); |
| 608 | } |
| 609 | return result; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 613 | pw.println("Pending requests:"); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 614 | synchronized (mPendingCmds) { |
| 615 | for (PendingCmd pendingCmd : mPendingCmds) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 616 | pw.println(" Cmd " + pendingCmd.cmdNum + " - " + pendingCmd.logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 621 | } |