summaryrefslogtreecommitdiff
path: root/services/usb/java/com/android/server/usb/hal/gadget/UsbGadgetHidl.java
blob: 3e5ecc5eddf4b1d2345333bb1cb87de7974b4ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.server.usb.hal.gadget;

import static android.hardware.usb.UsbManager.GADGET_HAL_NOT_SUPPORTED;
import static android.hardware.usb.UsbManager.GADGET_HAL_V1_0;
import static android.hardware.usb.UsbManager.GADGET_HAL_V1_1;
import static android.hardware.usb.UsbManager.GADGET_HAL_V1_2;

import static com.android.server.usb.UsbDeviceManager.logAndPrint;
import static com.android.server.usb.UsbDeviceManager.logAndPrintException;

import android.annotation.Nullable;
import android.hardware.usb.gadget.V1_0.Status;
import android.hardware.usb.gadget.V1_0.IUsbGadget;
import android.hardware.usb.gadget.V1_2.IUsbGadgetCallback;
import android.hardware.usb.gadget.V1_2.UsbSpeed;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbManager.UsbGadgetHalVersion;
import android.hardware.usb.UsbManager.UsbHalVersion;
import android.hidl.manager.V1_0.IServiceManager;
import android.hidl.manager.V1_0.IServiceNotification;
import android.os.IHwBinder;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.IndentingPrintWriter;
import com.android.server.usb.UsbDeviceManager;

import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Objects;
/**
 *
 */
public final class UsbGadgetHidl implements UsbGadgetHal {
    // Cookie sent for usb gadget hal death notification.
    private static final int USB_GADGET_HAL_DEATH_COOKIE = 2000;
    // Proxy object for the usb gadget hal daemon.
    @GuardedBy("mGadgetProxyLock")
    private IUsbGadget mGadgetProxy;
    private UsbDeviceManager mDeviceManager;
    private final IndentingPrintWriter mPw;
    // Mutex for all mutable shared state.
    private final Object mGadgetProxyLock = new Object();
    private UsbGadgetCallback mUsbGadgetCallback;

    public @UsbGadgetHalVersion int getGadgetHalVersion() throws RemoteException {
        int version;
        synchronized(mGadgetProxyLock) {
            if (mGadgetProxy == null) {
                throw new RemoteException("IUsbGadget not initialized yet");
            }
            if (android.hardware.usb.gadget.V1_2.IUsbGadget.castFrom(mGadgetProxy) != null) {
                version = UsbManager.GADGET_HAL_V1_2;
            } else if (android.hardware.usb.gadget.V1_1.IUsbGadget.castFrom(mGadgetProxy) != null) {
                version = UsbManager.GADGET_HAL_V1_1;
            } else {
                version = UsbManager.GADGET_HAL_V1_0;
            }
            logAndPrint(Log.INFO, mPw, "USB Gadget HAL HIDL version: " + version);
            return version;
        }
    }

    final class DeathRecipient implements IHwBinder.DeathRecipient {
        private final IndentingPrintWriter mPw;

        DeathRecipient(IndentingPrintWriter pw) {
            mPw = pw;
        }

        @Override
        public void serviceDied(long cookie) {
            if (cookie == USB_GADGET_HAL_DEATH_COOKIE) {
                logAndPrint(Log.ERROR, mPw, "Usb Gadget hal service died cookie: " + cookie);
                synchronized (mGadgetProxyLock) {
                    mGadgetProxy = null;
                }
            }
        }
    }

    final class ServiceNotification extends IServiceNotification.Stub {
        @Override
        public void onRegistration(String fqName, String name, boolean preexisting) {
            logAndPrint(Log.INFO, mPw, "Usb gadget hal service started " + fqName + " " + name);
            connectToProxy(null);
        }
    }

    private void connectToProxy(IndentingPrintWriter pw) {
        synchronized (mGadgetProxyLock) {
            if (mGadgetProxy != null) {
                return;
            }

            try {
                mGadgetProxy = IUsbGadget.getService();
                mGadgetProxy.linkToDeath(new DeathRecipient(pw), USB_GADGET_HAL_DEATH_COOKIE);
            } catch (NoSuchElementException e) {
                logAndPrintException(pw, "connectToProxy: usb gadget hal service not found."
                        + " Did the service fail to start?", e);
            } catch (RemoteException e) {
                logAndPrintException(pw, "connectToProxy: usb gadget hal service not responding"
                        , e);
            }
        }
    }

    @Override
    public void systemReady() {
    }

    static boolean isServicePresent(IndentingPrintWriter pw) {
        try {
            IUsbGadget.getService(true);
        } catch (NoSuchElementException e) {
            logAndPrintException(pw, "connectToProxy: usb gadget hidl hal service not found.", e);
            return false;
        } catch (RemoteException e) {
            logAndPrintException(pw, "IUSBGadget hal service present but failed to get service", e);
        }

        return true;
    }

    public UsbGadgetHidl(UsbDeviceManager deviceManager, IndentingPrintWriter pw) {
        mDeviceManager = Objects.requireNonNull(deviceManager);
        mPw = pw;
        try {
            ServiceNotification serviceNotification = new ServiceNotification();

            boolean ret = IServiceManager.getService()
                    .registerForNotifications("android.hardware.usb.gadget@1.0::IUsbGadget",
                            "", serviceNotification);
            if (!ret) {
                logAndPrint(Log.ERROR, pw, "Failed to register service start notification");
            }
        } catch (RemoteException e) {
            logAndPrintException(pw, "Failed to register service start notification", e);
            return;
        }
        connectToProxy(mPw);
    }

    @Override
    public void getCurrentUsbFunctions(long transactionId) {
        try {
            synchronized(mGadgetProxyLock) {
                mGadgetProxy.getCurrentUsbFunctions(new UsbGadgetCallback());
            }
        } catch (RemoteException e) {
            logAndPrintException(mPw,
                    "RemoteException while calling getCurrentUsbFunctions", e);
            return;
        }
    }

    @Override
    public void getUsbSpeed(long transactionId) {
        try {
            synchronized(mGadgetProxyLock) {
                if (android.hardware.usb.gadget.V1_2.IUsbGadget.castFrom(mGadgetProxy) != null) {
                    android.hardware.usb.gadget.V1_2.IUsbGadget gadgetProxy =
                    android.hardware.usb.gadget.V1_2.IUsbGadget.castFrom(mGadgetProxy);
                    gadgetProxy.getUsbSpeed(new UsbGadgetCallback());
                }
            }
        } catch (RemoteException e) {
            logAndPrintException(mPw, "get UsbSpeed failed", e);
        }
    }

    @Override
    public void reset() {
        try {
            synchronized(mGadgetProxyLock) {
                if (android.hardware.usb.gadget.V1_2.IUsbGadget.castFrom(mGadgetProxy) != null) {
                    android.hardware.usb.gadget.V1_2.IUsbGadget gadgetProxy =
                    android.hardware.usb.gadget.V1_2.IUsbGadget.castFrom(mGadgetProxy);
                    gadgetProxy.reset();
                }
            }
        } catch (RemoteException e) {
            logAndPrintException(mPw,
                    "RemoteException while calling getUsbSpeed", e);
            return;
        }
    }

    @Override
    public void setCurrentUsbFunctions(int mRequest, long mFunctions,
            boolean mChargingFunctions, int timeout, long operationId) {
        try {
            mUsbGadgetCallback = new UsbGadgetCallback(null, mRequest,
                                      mFunctions, mChargingFunctions);
            synchronized(mGadgetProxyLock) {
                mGadgetProxy.setCurrentUsbFunctions(mFunctions, mUsbGadgetCallback, timeout);
            }
        } catch (RemoteException e) {
            logAndPrintException(mPw,
                    "RemoteException while calling setCurrentUsbFunctions"
                    + " mRequest = " + mRequest
                    + ", mFunctions = " + mFunctions
                    + ", timeout = " + timeout
                    + ", mChargingFunctions = " + mChargingFunctions
                    + ", operationId =" + operationId, e);
            return;
        }
    }

    private class UsbGadgetCallback extends IUsbGadgetCallback.Stub {
        public int mRequest;
        public long mFunctions;
        public boolean mChargingFunctions;

        UsbGadgetCallback() {
        }
        UsbGadgetCallback(IndentingPrintWriter pw, int request,
                long functions, boolean chargingFunctions) {
            mRequest = request;
            mFunctions = functions;
            mChargingFunctions = chargingFunctions;
        }

        @Override
        public void setCurrentUsbFunctionsCb(long functions,
                int status) {
            mDeviceManager.setCurrentUsbFunctionsCb(functions, status,
                    mRequest, mFunctions, mChargingFunctions);
        }

        @Override
        public void getCurrentUsbFunctionsCb(long functions,
                int status) {
            mDeviceManager.getCurrentUsbFunctionsCb(functions, status);
        }

        @Override
        public void getUsbSpeedCb(int speed) {
            mDeviceManager.getUsbSpeedCb(speed);
        }
    }
}