1 /*
2  * Copyright (C) 2015 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 #include <memory>
18 
19 #include <jni.h>
20 #include <utils/Looper.h>
21 #include <utils/StrongPointer.h>
22 
23 namespace android {
24 namespace uhid {
25 
26 class DeviceCallback {
27 public:
28     DeviceCallback(JNIEnv* env, jobject callback);
29     ~DeviceCallback();
30 
31     void onDeviceOpen();
32     void onDeviceError();
33 
34 private:
35     jobject mCallbackObject;
36 };
37 
38 class Device {
39 public:
40     static Device* open(int32_t id, const char* name, int32_t vid, int32_t pid,
41             std::unique_ptr<uint8_t[]> descriptor, size_t descriptorSize,
42             std::unique_ptr<DeviceCallback> callback, sp<Looper> looper);
43 
44     Device(int32_t id, int fd, std::unique_ptr<DeviceCallback> callback, sp<Looper> looper);
45     ~Device();
46 
47     void sendReport(uint8_t* report, size_t reportSize);
48     void close();
49 
50     int handleEvents(int events);
51 
52 private:
53     int32_t mId;
54     int mFd;
55     std::unique_ptr<DeviceCallback> mDeviceCallback;
56     sp<Looper> mLooper;
57 };
58 
59 
60 } // namespace uhid
61 } // namespace android
62