blob: 8ef03d436ad20f41b68ca3a3042df547d66a6449 [file] [log] [blame]
Mike Lockwood24236072010-06-23 17:36:36 -04001/*
2 * Copyright (C) 2010 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.content.ContentResolver;
20import android.content.Context;
21import android.content.Intent;
Mike Lockwood770126a2010-12-09 22:30:37 -080022import android.hardware.UsbManager;
Mike Lockwood24236072010-06-23 17:36:36 -040023import android.net.Uri;
24import android.os.Handler;
25import android.os.Message;
26import android.os.UEventObserver;
27import android.provider.Settings;
28import android.util.Log;
29import android.util.Slog;
30
31import java.io.File;
32import java.io.FileNotFoundException;
33import java.io.FileReader;
34import java.util.ArrayList;
35
36/**
Mike Lockwood770126a2010-12-09 22:30:37 -080037 * <p>UsbService monitors for changes to USB state.
Mike Lockwood24236072010-06-23 17:36:36 -040038 */
Mike Lockwood770126a2010-12-09 22:30:37 -080039class UsbService {
40 private static final String TAG = UsbService.class.getSimpleName();
Mike Lockwood24236072010-06-23 17:36:36 -040041 private static final boolean LOG = false;
42
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080043 private static final String USB_CONNECTED_MATCH =
44 "DEVPATH=/devices/virtual/switch/usb_connected";
45 private static final String USB_CONFIGURATION_MATCH =
46 "DEVPATH=/devices/virtual/switch/usb_configuration";
47 private static final String USB_FUNCTIONS_MATCH =
48 "DEVPATH=/devices/virtual/usb_composite/";
49 private static final String USB_CONNECTED_PATH =
50 "/sys/class/switch/usb_connected/state";
51 private static final String USB_CONFIGURATION_PATH =
52 "/sys/class/switch/usb_configuration/state";
53 private static final String USB_COMPOSITE_CLASS_PATH =
54 "/sys/class/usb_composite";
Mike Lockwood24236072010-06-23 17:36:36 -040055
56 private static final int MSG_UPDATE = 0;
57
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080058 // Delay for debouncing USB disconnects.
59 // We often get rapid connect/disconnect events when enabling USB functions,
60 // which need debouncing.
61 private static final int UPDATE_DELAY = 1000;
62
63 // current connected and configuration state
64 private int mConnected;
65 private int mConfiguration;
66
67 // last broadcasted connected and configuration state
68 private int mLastConnected = -1;
69 private int mLastConfiguration = -1;
Mike Lockwood24236072010-06-23 17:36:36 -040070
71 // lists of enabled and disabled USB functions
72 private final ArrayList<String> mEnabledFunctions = new ArrayList<String>();
73 private final ArrayList<String> mDisabledFunctions = new ArrayList<String>();
74
75 private boolean mSystemReady;
76
77 private final Context mContext;
78
Mike Lockwood770126a2010-12-09 22:30:37 -080079 private final UEventObserver mUEventObserver = new UEventObserver() {
80 @Override
81 public void onUEvent(UEventObserver.UEvent event) {
82 if (Log.isLoggable(TAG, Log.VERBOSE)) {
83 Slog.v(TAG, "USB UEVENT: " + event.toString());
84 }
Mike Lockwood24236072010-06-23 17:36:36 -040085
Mike Lockwood770126a2010-12-09 22:30:37 -080086 synchronized (this) {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080087 String name = event.get("SWITCH_NAME");
88 String state = event.get("SWITCH_STATE");
89 if (name != null && state != null) {
Mike Lockwood770126a2010-12-09 22:30:37 -080090 try {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080091 int intState = Integer.parseInt(state);
92 if ("usb_connected".equals(name)) {
93 mConnected = intState;
Mike Lockwood770126a2010-12-09 22:30:37 -080094 // trigger an Intent broadcast
95 if (mSystemReady) {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -080096 // debounce disconnects
97 update(mConnected == 0);
98 }
99 } else if ("usb_configuration".equals(name)) {
100 mConfiguration = intState;
101 // trigger an Intent broadcast
102 if (mSystemReady) {
103 update(mConnected == 0);
Mike Lockwood770126a2010-12-09 22:30:37 -0800104 }
Mike Lockwood24236072010-06-23 17:36:36 -0400105 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800106 } catch (NumberFormatException e) {
107 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwood24236072010-06-23 17:36:36 -0400108 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800109 } else {
110 String function = event.get("FUNCTION");
111 String enabledStr = event.get("ENABLED");
112 if (function != null && enabledStr != null) {
113 // Note: we do not broadcast a change when a function is enabled or disabled.
114 // We just record the state change for the next broadcast.
115 boolean enabled = "1".equals(enabledStr);
116 if (enabled) {
117 if (!mEnabledFunctions.contains(function)) {
118 mEnabledFunctions.add(function);
119 }
120 mDisabledFunctions.remove(function);
121 } else {
122 if (!mDisabledFunctions.contains(function)) {
123 mDisabledFunctions.add(function);
124 }
125 mEnabledFunctions.remove(function);
Mike Lockwood24236072010-06-23 17:36:36 -0400126 }
Mike Lockwood24236072010-06-23 17:36:36 -0400127 }
128 }
129 }
130 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800131 };
132
133 public UsbService(Context context) {
134 mContext = context;
135 init(); // set initial status
136
David 'Digit' Turner49db8532011-01-17 00:19:37 +0100137 if (mConfiguration >= 0) {
138 mUEventObserver.startObserving(USB_CONNECTED_MATCH);
139 mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
140 mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
141 }
Mike Lockwood24236072010-06-23 17:36:36 -0400142 }
Mike Lockwood770126a2010-12-09 22:30:37 -0800143
Mike Lockwood24236072010-06-23 17:36:36 -0400144 private final void init() {
145 char[] buffer = new char[1024];
146
David 'Digit' Turner49db8532011-01-17 00:19:37 +0100147 mConfiguration = -1;
Mike Lockwood24236072010-06-23 17:36:36 -0400148 try {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800149 FileReader file = new FileReader(USB_CONNECTED_PATH);
Mike Lockwood24236072010-06-23 17:36:36 -0400150 int len = file.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700151 file.close();
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800152 mConnected = Integer.valueOf((new String(buffer, 0, len)).trim());
153
154 file = new FileReader(USB_CONFIGURATION_PATH);
155 len = file.read(buffer, 0, 1024);
156 file.close();
157 mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim());
Mike Lockwood24236072010-06-23 17:36:36 -0400158
159 } catch (FileNotFoundException e) {
David 'Digit' Turner49db8532011-01-17 00:19:37 +0100160 Slog.i(TAG, "This kernel does not have USB configuration switch support");
Mike Lockwood24236072010-06-23 17:36:36 -0400161 } catch (Exception e) {
162 Slog.e(TAG, "" , e);
163 }
David 'Digit' Turner49db8532011-01-17 00:19:37 +0100164 if (mConfiguration < 0)
165 return;
Mike Lockwood24236072010-06-23 17:36:36 -0400166
167 try {
168 File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
169 for (int i = 0; i < files.length; i++) {
170 File file = new File(files[i], "enable");
171 FileReader reader = new FileReader(file);
172 int len = reader.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700173 reader.close();
Mike Lockwood24236072010-06-23 17:36:36 -0400174 int value = Integer.valueOf((new String(buffer, 0, len)).trim());
175 String functionName = files[i].getName();
176 if (value == 1) {
177 mEnabledFunctions.add(functionName);
178 } else {
179 mDisabledFunctions.add(functionName);
180 }
181 }
182 } catch (FileNotFoundException e) {
183 Slog.w(TAG, "This kernel does not have USB composite class support");
184 } catch (Exception e) {
185 Slog.e(TAG, "" , e);
186 }
187 }
188
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400189 private void initHostSupport() {
Mike Lockwood264f6cd2011-01-06 10:38:10 -0500190 // temporarily disabled
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400191 }
192
Mike Lockwood24236072010-06-23 17:36:36 -0400193 void systemReady() {
194 synchronized (this) {
Mike Lockwoodda39f0e2010-07-27 18:44:30 -0400195 if (mContext.getResources().getBoolean(
196 com.android.internal.R.bool.config_hasUsbHostSupport)) {
197 // start monitoring for connected USB devices
198 initHostSupport();
199 }
200
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800201 update(false);
Mike Lockwood24236072010-06-23 17:36:36 -0400202 mSystemReady = true;
203 }
204 }
205
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800206 private final void update(boolean delayed) {
207 mHandler.removeMessages(MSG_UPDATE);
208 mHandler.sendEmptyMessageDelayed(MSG_UPDATE, delayed ? UPDATE_DELAY : 0);
Mike Lockwood24236072010-06-23 17:36:36 -0400209 }
210
211 private final Handler mHandler = new Handler() {
Mike Lockwood709981e2010-06-28 09:58:58 -0400212 private void addEnabledFunctions(Intent intent) {
213 // include state of all USB functions in our extras
214 for (int i = 0; i < mEnabledFunctions.size(); i++) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800215 intent.putExtra(mEnabledFunctions.get(i), UsbManager.USB_FUNCTION_ENABLED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400216 }
217 for (int i = 0; i < mDisabledFunctions.size(); i++) {
Mike Lockwood770126a2010-12-09 22:30:37 -0800218 intent.putExtra(mDisabledFunctions.get(i), UsbManager.USB_FUNCTION_DISABLED);
Mike Lockwood709981e2010-06-28 09:58:58 -0400219 }
220 }
221
Mike Lockwood24236072010-06-23 17:36:36 -0400222 @Override
223 public void handleMessage(Message msg) {
224 switch (msg.what) {
225 case MSG_UPDATE:
226 synchronized (this) {
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800227 if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) {
Mike Lockwood24236072010-06-23 17:36:36 -0400228
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800229 final ContentResolver cr = mContext.getContentResolver();
230 if (Settings.Secure.getInt(cr,
231 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
232 Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
233 return;
234 }
235
236 mLastConnected = mConnected;
237 mLastConfiguration = mConfiguration;
238
239 // send a sticky broadcast containing current USB state
240 Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
241 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
242 intent.putExtra(UsbManager.USB_CONNECTED, mConnected != 0);
243 intent.putExtra(UsbManager.USB_CONFIGURATION, mConfiguration);
Mike Lockwood709981e2010-06-28 09:58:58 -0400244 addEnabledFunctions(intent);
Mike Lockwoodb92df0f2010-12-10 16:19:32 -0800245 mContext.sendStickyBroadcast(intent);
Mike Lockwood24236072010-06-23 17:36:36 -0400246 }
Mike Lockwood24236072010-06-23 17:36:36 -0400247 }
248 break;
249 }
250 }
251 };
252}