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) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 177 | // Note - do not log this raw message since it may contain |
| 178 | // sensitive data |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 179 | final String rawEvent = new String( |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 180 | buffer, start, i - start, StandardCharsets.UTF_8); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 181 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 182 | boolean releaseWl = false; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 183 | try { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 184 | final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent( |
| 185 | rawEvent); |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 186 | |
| 187 | log("RCV <- {" + event + "}"); |
| 188 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 189 | if (event.isClassUnsolicited()) { |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 190 | // TODO: migrate to sending NativeDaemonEvent instances |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 191 | if (mCallbacks.onCheckHoldWakeLock(event.getCode()) |
| 192 | && mWakeLock != null) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 193 | mWakeLock.acquire(); |
| 194 | releaseWl = true; |
| 195 | } |
| 196 | if (mCallbackHandler.sendMessage(mCallbackHandler.obtainMessage( |
| 197 | event.getCode(), event.getRawEvent()))) { |
| 198 | releaseWl = false; |
| 199 | } |
Irfan Sheriff | 1cd94ef | 2011-01-16 14:31:55 -0800 | [diff] [blame] | 200 | } else { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 201 | mResponseQueue.add(event.getCmdNumber(), event); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 202 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 203 | } catch (IllegalArgumentException e) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 204 | log("Problem parsing message " + e); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 205 | } finally { |
| 206 | if (releaseWl) { |
| 207 | mWakeLock.acquire(); |
| 208 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 209 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 210 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 211 | start = i + 1; |
| 212 | } |
| 213 | } |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 214 | |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 215 | if (start == 0) { |
Paul Lawrence | c38182f | 2014-11-11 12:23:22 -0800 | [diff] [blame] | 216 | log("RCV incomplete"); |
Robert Greenwalt | f0be1d89 | 2012-01-20 16:33:15 -0800 | [diff] [blame] | 217 | } |
Kenny Root | 12da9d7 | 2010-09-02 22:18:14 -0700 | [diff] [blame] | 218 | |
| 219 | // We should end at the amount we read. If not, compact then |
| 220 | // buffer and read again. |
Kenny Root | 961aa8c | 2010-03-22 18:02:45 -0700 | [diff] [blame] | 221 | if (start != count) { |
| 222 | final int remaining = BUFFER_SIZE - start; |
| 223 | System.arraycopy(buffer, start, buffer, 0, remaining); |
| 224 | start = remaining; |
| 225 | } else { |
| 226 | start = 0; |
| 227 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 228 | } |
| 229 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 230 | loge("Communications error: " + ex); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 231 | throw ex; |
| 232 | } finally { |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 233 | synchronized (mDaemonLock) { |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 234 | if (mOutputStream != null) { |
| 235 | try { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 236 | loge("closing stream for " + mSocket); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 237 | mOutputStream.close(); |
| 238 | } catch (IOException e) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 239 | loge("Failed closing output stream: " + e); |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 240 | } |
| 241 | mOutputStream = null; |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 242 | } |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 243 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 244 | |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 245 | try { |
| 246 | if (socket != null) { |
| 247 | socket.close(); |
| 248 | } |
| 249 | } catch (IOException ex) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 250 | loge("Failed closing socket: " + ex); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 253 | } |
| 254 | |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 255 | /** |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 256 | * Wrapper around argument that indicates it's sensitive and shouldn't be |
| 257 | * logged. |
| 258 | */ |
| 259 | public static class SensitiveArg { |
| 260 | private final Object mArg; |
| 261 | |
| 262 | public SensitiveArg(Object arg) { |
| 263 | mArg = arg; |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public String toString() { |
| 268 | return String.valueOf(mArg); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 273 | * Make command for daemon, escaping arguments as needed. |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 274 | */ |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 275 | @VisibleForTesting |
| 276 | static void makeCommand(StringBuilder rawBuilder, StringBuilder logBuilder, int sequenceNumber, |
| 277 | String cmd, Object... args) { |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 278 | if (cmd.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 279 | throw new IllegalArgumentException("Unexpected command: " + cmd); |
| 280 | } |
| 281 | if (cmd.indexOf(' ') >= 0) { |
| 282 | throw new IllegalArgumentException("Arguments must be separate from command"); |
Jeff Sharkey | b0aec07 | 2011-10-14 18:32:24 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 285 | rawBuilder.append(sequenceNumber).append(' ').append(cmd); |
| 286 | logBuilder.append(sequenceNumber).append(' ').append(cmd); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 287 | for (Object arg : args) { |
| 288 | final String argString = String.valueOf(arg); |
| 289 | if (argString.indexOf('\0') >= 0) { |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 290 | throw new IllegalArgumentException("Unexpected argument: " + arg); |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 291 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 292 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 293 | rawBuilder.append(' '); |
| 294 | logBuilder.append(' '); |
| 295 | |
| 296 | appendEscaped(rawBuilder, argString); |
| 297 | if (arg instanceof SensitiveArg) { |
| 298 | logBuilder.append("[scrubbed]"); |
| 299 | } else { |
| 300 | appendEscaped(logBuilder, argString); |
| 301 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 302 | } |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 303 | |
| 304 | rawBuilder.append('\0'); |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 305 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 306 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 307 | /** |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 308 | * Issue the given command to the native daemon and return a single expected |
| 309 | * response. |
| 310 | * |
| 311 | * @throws NativeDaemonConnectorException when problem communicating with |
| 312 | * native daemon, or if the response matches |
| 313 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 314 | * {@link NativeDaemonEvent#isClassServerError()}. |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 315 | */ |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 316 | public NativeDaemonEvent execute(Command cmd) throws NativeDaemonConnectorException { |
| 317 | return execute(cmd.mCmd, cmd.mArguments.toArray()); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * 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] | 322 | * response. Any arguments must be separated from base command so they can |
| 323 | * be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 324 | * |
| 325 | * @throws NativeDaemonConnectorException when problem communicating with |
| 326 | * native daemon, or if the response matches |
| 327 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 328 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 329 | */ |
| 330 | public NativeDaemonEvent execute(String cmd, Object... args) |
| 331 | throws NativeDaemonConnectorException { |
| 332 | final NativeDaemonEvent[] events = executeForList(cmd, args); |
| 333 | if (events.length != 1) { |
| 334 | throw new NativeDaemonConnectorException( |
| 335 | "Expected exactly one response, but received " + events.length); |
| 336 | } |
| 337 | return events[0]; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Issue the given command to the native daemon and return any |
| 342 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
| 343 | * final terminal response. |
| 344 | * |
| 345 | * @throws NativeDaemonConnectorException when problem communicating with |
| 346 | * native daemon, or if the response matches |
| 347 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 348 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 349 | */ |
| 350 | public NativeDaemonEvent[] executeForList(Command cmd) throws NativeDaemonConnectorException { |
| 351 | return executeForList(cmd.mCmd, cmd.mArguments.toArray()); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Issue the given command to the native daemon and return any |
| 356 | * {@link NativeDaemonEvent#isClassContinue()} responses, including the |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 357 | * final terminal response. Any arguments must be separated from base |
| 358 | * command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -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[] executeForList(String cmd, Object... args) |
San Mehat | 4c27e0e | 2010-01-29 05:22:17 -0800 | [diff] [blame] | 366 | throws NativeDaemonConnectorException { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 367 | return execute(DEFAULT_TIMEOUT, cmd, args); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 368 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 369 | |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 370 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 371 | * Issue the given command to the native daemon and return any {@linke |
| 372 | * NativeDaemonEvent@isClassContinue()} responses, including the final |
| 373 | * terminal response. Note that the timeout does not count time in deep |
| 374 | * sleep. Any arguments must be separated from base command so they can be |
| 375 | * properly escaped. |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 376 | * |
| 377 | * @throws NativeDaemonConnectorException when problem communicating with |
| 378 | * native daemon, or if the response matches |
| 379 | * {@link NativeDaemonEvent#isClassClientError()} or |
| 380 | * {@link NativeDaemonEvent#isClassServerError()}. |
| 381 | */ |
| 382 | public NativeDaemonEvent[] execute(int timeout, String cmd, Object... args) |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 383 | throws NativeDaemonConnectorException { |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 384 | final long startTime = SystemClock.elapsedRealtime(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 385 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 386 | final ArrayList<NativeDaemonEvent> events = Lists.newArrayList(); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 387 | |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 388 | final StringBuilder rawBuilder = new StringBuilder(); |
| 389 | final StringBuilder logBuilder = new StringBuilder(); |
| 390 | final int sequenceNumber = mSequenceNumber.incrementAndGet(); |
| 391 | |
| 392 | makeCommand(rawBuilder, logBuilder, sequenceNumber, cmd, args); |
| 393 | |
| 394 | final String rawCmd = rawBuilder.toString(); |
| 395 | final String logCmd = logBuilder.toString(); |
| 396 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 397 | log("SND -> {" + logCmd + "}"); |
| 398 | |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 399 | synchronized (mDaemonLock) { |
| 400 | if (mOutputStream == null) { |
| 401 | throw new NativeDaemonConnectorException("missing output stream"); |
| 402 | } else { |
| 403 | try { |
Elliott Hughes | b829283 | 2013-06-28 16:50:13 -0700 | [diff] [blame] | 404 | mOutputStream.write(rawCmd.getBytes(StandardCharsets.UTF_8)); |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 405 | } catch (IOException e) { |
| 406 | throw new NativeDaemonConnectorException("problem sending command", e); |
| 407 | } |
| 408 | } |
| 409 | } |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 410 | |
| 411 | NativeDaemonEvent event = null; |
| 412 | do { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 413 | event = mResponseQueue.remove(sequenceNumber, timeout, logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 414 | if (event == null) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 415 | loge("timed-out waiting for response to " + logCmd); |
| 416 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 417 | } |
Robert Greenwalt | 7f44ff8 | 2014-05-07 23:49:08 -0700 | [diff] [blame] | 418 | if (VDBG) log("RMV <- {" + event + "}"); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 419 | events.add(event); |
| 420 | } while (event.isClassContinue()); |
| 421 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 422 | final long endTime = SystemClock.elapsedRealtime(); |
| 423 | if (endTime - startTime > WARN_EXECUTE_DELAY_MS) { |
| 424 | loge("NDC Command {" + logCmd + "} took too long (" + (endTime - startTime) + "ms)"); |
| 425 | } |
| 426 | |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 427 | if (event.isClassClientError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 428 | throw new NativeDaemonArgumentException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 429 | } |
| 430 | if (event.isClassServerError()) { |
Robert Greenwalt | d192598 | 2012-03-12 15:37:40 -0700 | [diff] [blame] | 431 | throw new NativeDaemonFailureException(logCmd, event); |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | return events.toArray(new NativeDaemonEvent[events.size()]); |
| 435 | } |
| 436 | |
| 437 | /** |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 438 | * Append the given argument to {@link StringBuilder}, escaping as needed, |
| 439 | * and surrounding with quotes when it contains spaces. |
| 440 | */ |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 441 | @VisibleForTesting |
Jeff Sharkey | 31c6e48 | 2011-11-18 17:09:01 -0800 | [diff] [blame] | 442 | static void appendEscaped(StringBuilder builder, String arg) { |
| 443 | final boolean hasSpaces = arg.indexOf(' ') >= 0; |
| 444 | if (hasSpaces) { |
| 445 | builder.append('"'); |
| 446 | } |
| 447 | |
| 448 | final int length = arg.length(); |
| 449 | for (int i = 0; i < length; i++) { |
| 450 | final char c = arg.charAt(i); |
| 451 | |
| 452 | if (c == '"') { |
| 453 | builder.append("\\\""); |
| 454 | } else if (c == '\\') { |
| 455 | builder.append("\\\\"); |
| 456 | } else { |
| 457 | builder.append(c); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (hasSpaces) { |
| 462 | builder.append('"'); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | private static class NativeDaemonArgumentException extends NativeDaemonConnectorException { |
| 467 | public NativeDaemonArgumentException(String command, NativeDaemonEvent event) { |
| 468 | super(command, event); |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public IllegalArgumentException rethrowAsParcelableException() { |
| 473 | throw new IllegalArgumentException(getMessage(), this); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | private static class NativeDaemonFailureException extends NativeDaemonConnectorException { |
| 478 | public NativeDaemonFailureException(String command, NativeDaemonEvent event) { |
| 479 | super(command, event); |
| 480 | } |
San Mehat | deba693 | 2010-01-20 15:14:31 -0800 | [diff] [blame] | 481 | } |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 482 | |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 483 | /** |
Jeff Sharkey | 7b4596f | 2013-02-25 10:55:29 -0800 | [diff] [blame] | 484 | * Command builder that handles argument list building. Any arguments must |
| 485 | * be separated from base command so they can be properly escaped. |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 486 | */ |
| 487 | public static class Command { |
| 488 | private String mCmd; |
| 489 | private ArrayList<Object> mArguments = Lists.newArrayList(); |
| 490 | |
| 491 | public Command(String cmd, Object... args) { |
| 492 | mCmd = cmd; |
| 493 | for (Object arg : args) { |
| 494 | appendArg(arg); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | public Command appendArg(Object arg) { |
| 499 | mArguments.add(arg); |
| 500 | return this; |
| 501 | } |
| 502 | } |
| 503 | |
Jeff Sharkey | fa23c5a | 2011-08-09 21:44:24 -0700 | [diff] [blame] | 504 | /** {@inheritDoc} */ |
| 505 | public void monitor() { |
| 506 | synchronized (mDaemonLock) { } |
| 507 | } |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 508 | |
| 509 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 510 | mLocalLog.dump(fd, pw, args); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 511 | pw.println(); |
| 512 | mResponseQueue.dump(fd, pw, args); |
Robert Greenwalt | 470fd72 | 2012-01-18 12:51:15 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | private void log(String logstring) { |
| 516 | if (LOGD) Slog.d(TAG, logstring); |
| 517 | mLocalLog.log(logstring); |
| 518 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 519 | |
| 520 | private void loge(String logstring) { |
| 521 | Slog.e(TAG, logstring); |
| 522 | mLocalLog.log(logstring); |
| 523 | } |
| 524 | |
| 525 | private static class ResponseQueue { |
| 526 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 527 | private static class PendingCmd { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 528 | public final int cmdNum; |
| 529 | public final String logCmd; |
| 530 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 531 | public BlockingQueue<NativeDaemonEvent> responses = |
| 532 | new ArrayBlockingQueue<NativeDaemonEvent>(10); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 533 | |
| 534 | // The availableResponseCount member is used to track when we can remove this |
| 535 | // instance from the ResponseQueue. |
| 536 | // This is used under the protection of a sync of the mPendingCmds object. |
| 537 | // A positive value means we've had more writers retreive this object while |
| 538 | // a negative value means we've had more readers. When we've had an equal number |
| 539 | // (it goes to zero) we can remove this object from the mPendingCmds list. |
| 540 | // Note that we may have more responses for this command (and more readers |
| 541 | // coming), but that would result in a new PendingCmd instance being created |
| 542 | // and added with the same cmdNum. |
| 543 | // Also note that when this goes to zero it just means a parity of readers and |
| 544 | // writers have retrieved this object - not that they are done using it. The |
| 545 | // responses queue may well have more responses yet to be read or may get more |
| 546 | // responses added to it. But all those readers/writers have retreived and |
| 547 | // hold references to this instance already so it can be removed from |
| 548 | // mPendingCmds queue. |
| 549 | public int availableResponseCount; |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 550 | |
| 551 | public PendingCmd(int cmdNum, String logCmd) { |
| 552 | this.cmdNum = cmdNum; |
| 553 | this.logCmd = logCmd; |
| 554 | } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 557 | private final LinkedList<PendingCmd> mPendingCmds; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 558 | private int mMaxCount; |
| 559 | |
| 560 | ResponseQueue(int maxCount) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 561 | mPendingCmds = new LinkedList<PendingCmd>(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 562 | mMaxCount = maxCount; |
| 563 | } |
| 564 | |
| 565 | public void add(int cmdNum, NativeDaemonEvent response) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 566 | PendingCmd found = null; |
| 567 | synchronized (mPendingCmds) { |
| 568 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 569 | if (pendingCmd.cmdNum == cmdNum) { |
| 570 | found = pendingCmd; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 571 | break; |
| 572 | } |
| 573 | } |
| 574 | if (found == null) { |
| 575 | // 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] | 576 | while (mPendingCmds.size() >= mMaxCount) { |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 577 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 578 | "more buffered than allowed: " + mPendingCmds.size() + |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 579 | " >= " + mMaxCount); |
| 580 | // let any waiter timeout waiting for this |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 581 | PendingCmd pendingCmd = mPendingCmds.remove(); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 582 | Slog.e("NativeDaemonConnector.ResponseQueue", |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 583 | "Removing request: " + pendingCmd.logCmd + " (" + |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 584 | pendingCmd.cmdNum + ")"); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 585 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 586 | found = new PendingCmd(cmdNum, null); |
| 587 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 588 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 589 | found.availableResponseCount++; |
| 590 | // if a matching remove call has already retrieved this we can remove this |
| 591 | // instance from our list |
| 592 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 593 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 594 | try { |
| 595 | found.responses.put(response); |
| 596 | } catch (InterruptedException e) { } |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // note that the timeout does not count time in deep sleep. If you don't want |
| 600 | // the device to sleep, hold a wakelock |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 601 | public NativeDaemonEvent remove(int cmdNum, int timeoutMs, String logCmd) { |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 602 | PendingCmd found = null; |
| 603 | synchronized (mPendingCmds) { |
| 604 | for (PendingCmd pendingCmd : mPendingCmds) { |
| 605 | if (pendingCmd.cmdNum == cmdNum) { |
| 606 | found = pendingCmd; |
| 607 | break; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 610 | if (found == null) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 611 | found = new PendingCmd(cmdNum, logCmd); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 612 | mPendingCmds.add(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 613 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 614 | found.availableResponseCount--; |
| 615 | // if a matching add call has already retrieved this we can remove this |
| 616 | // instance from our list |
| 617 | if (found.availableResponseCount == 0) mPendingCmds.remove(found); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 618 | } |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 619 | NativeDaemonEvent result = null; |
| 620 | try { |
| 621 | result = found.responses.poll(timeoutMs, TimeUnit.MILLISECONDS); |
| 622 | } catch (InterruptedException e) {} |
| 623 | if (result == null) { |
| 624 | Slog.e("NativeDaemonConnector.ResponseQueue", "Timeout waiting for response"); |
| 625 | } |
| 626 | return result; |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 630 | pw.println("Pending requests:"); |
Robert Greenwalt | ef21599 | 2012-06-05 11:48:40 -0700 | [diff] [blame] | 631 | synchronized (mPendingCmds) { |
| 632 | for (PendingCmd pendingCmd : mPendingCmds) { |
Jeff Sharkey | 56cd646 | 2013-06-07 15:09:15 -0700 | [diff] [blame] | 633 | pw.println(" Cmd " + pendingCmd.cmdNum + " - " + pendingCmd.logCmd); |
Robert Greenwalt | 470007f | 2012-02-07 11:36:55 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
San Mehat | 67bd2cd | 2010-01-12 12:18:49 -0800 | [diff] [blame] | 638 | } |