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