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