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