blob: c45259064c4529be9f1711c9e97aaacea19ebe08 [file] [log] [blame]
San Mehat67bd2cd2010-01-12 12:18:49 -08001/*
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
17package com.android.server;
18
19import android.net.LocalSocketAddress;
20import android.net.LocalSocket;
21import android.os.Environment;
22import android.os.SystemClock;
23import android.os.SystemProperties;
Joe Onorato8a9b2202010-02-26 18:56:32 -080024import android.util.Slog;
San Mehat67bd2cd2010-01-12 12:18:49 -080025
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.OutputStream;
29import java.net.Socket;
San Mehat67bd2cd2010-01-12 12:18:49 -080030
31import java.util.List;
32import java.util.ArrayList;
33import java.util.ListIterator;
34import java.util.concurrent.BlockingQueue;
35import java.util.concurrent.LinkedBlockingQueue;
36
37/**
38 * Generic connector class for interfacing with a native
39 * daemon which uses the libsysutils FrameworkListener
40 * protocol.
41 */
42final class NativeDaemonConnector implements Runnable {
San Mehat1ff43712010-02-04 15:09:02 -080043 private static final boolean LOCAL_LOGD = false;
San Mehat67bd2cd2010-01-12 12:18:49 -080044
45 private BlockingQueue<String> mResponseQueue;
46 private OutputStream mOutputStream;
47 private String TAG = "NativeDaemonConnector";
48 private String mSocket;
49 private INativeDaemonConnectorCallbacks mCallbacks;
50
Kenny Root961aa8c2010-03-22 18:02:45 -070051 private final int BUFFER_SIZE = 4096;
52
San Mehat67bd2cd2010-01-12 12:18:49 -080053 class ResponseCode {
54 public static final int ActionInitiated = 100;
55
56 public static final int CommandOkay = 200;
57
58 // The range of 400 -> 599 is reserved for cmd failures
59 public static final int OperationFailed = 400;
60 public static final int CommandSyntaxError = 500;
61 public static final int CommandParameterError = 501;
62
63 public static final int UnsolicitedInformational = 600;
64
65 //
66 public static final int FailedRangeStart = 400;
67 public static final int FailedRangeEnd = 599;
68 }
69
70 NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks,
71 String socket, int responseQueueSize, String logTag) {
72 mCallbacks = callbacks;
73 if (logTag != null)
74 TAG = logTag;
75 mSocket = socket;
76 mResponseQueue = new LinkedBlockingQueue<String>(responseQueueSize);
77 }
78
79 public void run() {
80
81 while (true) {
82 try {
83 listenToSocket();
84 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080085 Slog.e(TAG, "Error in NativeDaemonConnector", e);
San Mehat4c27e0e2010-01-29 05:22:17 -080086 SystemClock.sleep(5000);
San Mehat67bd2cd2010-01-12 12:18:49 -080087 }
88 }
89 }
90
San Mehat4c27e0e2010-01-29 05:22:17 -080091 private void listenToSocket() throws IOException {
Kenny Root961aa8c2010-03-22 18:02:45 -070092 LocalSocket socket = null;
San Mehat67bd2cd2010-01-12 12:18:49 -080093
94 try {
95 socket = new LocalSocket();
96 LocalSocketAddress address = new LocalSocketAddress(mSocket,
97 LocalSocketAddress.Namespace.RESERVED);
98
99 socket.connect(address);
100 mCallbacks.onDaemonConnected();
101
102 InputStream inputStream = socket.getInputStream();
103 mOutputStream = socket.getOutputStream();
104
Kenny Root961aa8c2010-03-22 18:02:45 -0700105 byte[] buffer = new byte[BUFFER_SIZE];
106 int start = 0;
San Mehat67bd2cd2010-01-12 12:18:49 -0800107
108 while (true) {
Kenny Root961aa8c2010-03-22 18:02:45 -0700109 int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
San Mehat67bd2cd2010-01-12 12:18:49 -0800110 if (count < 0) break;
111
San Mehat67bd2cd2010-01-12 12:18:49 -0800112 for (int i = 0; i < count; i++) {
113 if (buffer[i] == 0) {
114 String event = new String(buffer, start, i - start);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800115 if (LOCAL_LOGD) Slog.d(TAG, String.format("RCV <- {%s}", event));
San Mehat67bd2cd2010-01-12 12:18:49 -0800116
117 String[] tokens = event.split(" ");
118 try {
119 int code = Integer.parseInt(tokens[0]);
120
121 if (code >= ResponseCode.UnsolicitedInformational) {
122 try {
123 if (!mCallbacks.onEvent(code, event, tokens)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800124 Slog.w(TAG, String.format(
San Mehat67bd2cd2010-01-12 12:18:49 -0800125 "Unhandled event (%s)", event));
126 }
127 } catch (Exception ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800128 Slog.e(TAG, String.format(
San Mehat67bd2cd2010-01-12 12:18:49 -0800129 "Error handling '%s'", event), ex);
130 }
Kenny Roota80ce062010-06-01 13:23:53 -0700131 }
132 try {
133 mResponseQueue.put(event);
134 } catch (InterruptedException ex) {
135 Slog.e(TAG, "Failed to put response onto queue", ex);
San Mehat67bd2cd2010-01-12 12:18:49 -0800136 }
137 } catch (NumberFormatException nfe) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800138 Slog.w(TAG, String.format("Bad msg (%s)", event));
San Mehat67bd2cd2010-01-12 12:18:49 -0800139 }
140 start = i + 1;
141 }
142 }
Kenny Root961aa8c2010-03-22 18:02:45 -0700143 if (start != count) {
144 final int remaining = BUFFER_SIZE - start;
145 System.arraycopy(buffer, start, buffer, 0, remaining);
146 start = remaining;
147 } else {
148 start = 0;
149 }
San Mehat67bd2cd2010-01-12 12:18:49 -0800150 }
151 } catch (IOException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800152 Slog.e(TAG, "Communications error", ex);
San Mehat4c27e0e2010-01-29 05:22:17 -0800153 throw ex;
154 } finally {
155 synchronized (this) {
156 if (mOutputStream != null) {
157 try {
158 mOutputStream.close();
159 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800160 Slog.w(TAG, "Failed closing output stream", e);
San Mehat4c27e0e2010-01-29 05:22:17 -0800161 }
162 mOutputStream = null;
San Mehat67bd2cd2010-01-12 12:18:49 -0800163 }
San Mehat4c27e0e2010-01-29 05:22:17 -0800164 }
San Mehat67bd2cd2010-01-12 12:18:49 -0800165
San Mehat4c27e0e2010-01-29 05:22:17 -0800166 try {
167 if (socket != null) {
168 socket.close();
169 }
170 } catch (IOException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800171 Slog.w(TAG, "Failed closing socket", ex);
San Mehat67bd2cd2010-01-12 12:18:49 -0800172 }
173 }
San Mehat67bd2cd2010-01-12 12:18:49 -0800174 }
175
176 private void sendCommand(String command) {
177 sendCommand(command, null);
178 }
179
180 /**
181 * Sends a command to the daemon with a single argument
182 *
183 * @param command The command to send to the daemon
184 * @param argument The argument to send with the command (or null)
185 */
186 private void sendCommand(String command, String argument) {
187 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800188 if (LOCAL_LOGD) Slog.d(TAG, String.format("SND -> {%s} {%s}", command, argument));
San Mehat67bd2cd2010-01-12 12:18:49 -0800189 if (mOutputStream == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800190 Slog.e(TAG, "No connection to daemon", new IllegalStateException());
San Mehat67bd2cd2010-01-12 12:18:49 -0800191 } else {
192 StringBuilder builder = new StringBuilder(command);
193 if (argument != null) {
194 builder.append(argument);
195 }
196 builder.append('\0');
197
198 try {
199 mOutputStream.write(builder.toString().getBytes());
200 } catch (IOException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800201 Slog.e(TAG, "IOException in sendCommand", ex);
San Mehat67bd2cd2010-01-12 12:18:49 -0800202 }
203 }
204 }
205 }
206
San Mehatdeba6932010-01-20 15:14:31 -0800207 /**
208 * Issue a command to the native daemon and return the responses
209 */
San Mehat4c27e0e2010-01-29 05:22:17 -0800210 public synchronized ArrayList<String> doCommand(String cmd)
211 throws NativeDaemonConnectorException {
San Mehat67bd2cd2010-01-12 12:18:49 -0800212 sendCommand(cmd);
213
214 ArrayList<String> response = new ArrayList<String>();
215 boolean complete = false;
216 int code = -1;
217
218 while (!complete) {
219 try {
220 String line = mResponseQueue.take();
Joe Onorato8a9b2202010-02-26 18:56:32 -0800221 if (LOCAL_LOGD) Slog.d(TAG, String.format("RSP <- {%s}", line));
San Mehat67bd2cd2010-01-12 12:18:49 -0800222 String[] tokens = line.split(" ");
223 try {
224 code = Integer.parseInt(tokens[0]);
225 } catch (NumberFormatException nfe) {
San Mehat4c27e0e2010-01-29 05:22:17 -0800226 throw new NativeDaemonConnectorException(
San Mehat67bd2cd2010-01-12 12:18:49 -0800227 String.format("Invalid response from daemon (%s)", line));
228 }
229
San Mehatec4caa02010-02-03 10:48:21 -0800230 if ((code >= 200) && (code < 600)) {
San Mehat67bd2cd2010-01-12 12:18:49 -0800231 complete = true;
San Mehatec4caa02010-02-03 10:48:21 -0800232 }
San Mehat67bd2cd2010-01-12 12:18:49 -0800233 response.add(line);
234 } catch (InterruptedException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800235 Slog.e(TAG, "Failed to process response", ex);
San Mehat67bd2cd2010-01-12 12:18:49 -0800236 }
237 }
238
239 if (code >= ResponseCode.FailedRangeStart &&
240 code <= ResponseCode.FailedRangeEnd) {
San Mehatec4caa02010-02-03 10:48:21 -0800241 /*
242 * Note: The format of the last response in this case is
243 * "NNN <errmsg>"
244 */
245 throw new NativeDaemonConnectorException(
246 code, cmd, response.get(response.size()-1).substring(4));
San Mehat67bd2cd2010-01-12 12:18:49 -0800247 }
248 return response;
249 }
San Mehatdeba6932010-01-20 15:14:31 -0800250
251 /*
252 * Issues a list command and returns the cooked list
253 */
254 public String[] doListCommand(String cmd, int expectedResponseCode)
San Mehat4c27e0e2010-01-29 05:22:17 -0800255 throws NativeDaemonConnectorException {
San Mehatdeba6932010-01-20 15:14:31 -0800256
257 ArrayList<String> rsp = doCommand(cmd);
258 String[] rdata = new String[rsp.size()-1];
259 int idx = 0;
260
San Mehat1ff43712010-02-04 15:09:02 -0800261 for (int i = 0; i < rsp.size(); i++) {
262 String line = rsp.get(i);
San Mehatdeba6932010-01-20 15:14:31 -0800263 try {
264 String[] tok = line.split(" ");
265 int code = Integer.parseInt(tok[0]);
266 if (code == expectedResponseCode) {
San Mehat80120b42010-01-26 12:48:39 -0800267 rdata[idx++] = line.substring(tok[0].length() + 1);
San Mehatdeba6932010-01-20 15:14:31 -0800268 } else if (code == NativeDaemonConnector.ResponseCode.CommandOkay) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800269 if (LOCAL_LOGD) Slog.d(TAG, String.format("List terminated with {%s}", line));
San Mehat4086f752010-02-17 09:03:29 -0800270 int last = rsp.size() -1;
271 if (i != last) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800272 Slog.w(TAG, String.format("Recv'd %d lines after end of list {%s}", (last-i), cmd));
San Mehat4086f752010-02-17 09:03:29 -0800273 for (int j = i; j <= last ; j++) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800274 Slog.w(TAG, String.format("ExtraData <%s>", rsp.get(i)));
San Mehat4086f752010-02-17 09:03:29 -0800275 }
San Mehat1ff43712010-02-04 15:09:02 -0800276 }
San Mehatdeba6932010-01-20 15:14:31 -0800277 return rdata;
278 } else {
San Mehat4c27e0e2010-01-29 05:22:17 -0800279 throw new NativeDaemonConnectorException(
San Mehatdeba6932010-01-20 15:14:31 -0800280 String.format("Expected list response %d, but got %d",
281 expectedResponseCode, code));
282 }
283 } catch (NumberFormatException nfe) {
San Mehat4c27e0e2010-01-29 05:22:17 -0800284 throw new NativeDaemonConnectorException(
285 String.format("Error reading code '%s'", line));
San Mehatdeba6932010-01-20 15:14:31 -0800286 }
287 }
San Mehat4c27e0e2010-01-29 05:22:17 -0800288 throw new NativeDaemonConnectorException("Got an empty response");
San Mehatdeba6932010-01-20 15:14:31 -0800289 }
San Mehat67bd2cd2010-01-12 12:18:49 -0800290}