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 #include <vector>
19 
20 #include <jni.h>
21 
22 #include <android-base/unique_fd.h>
23 
24 namespace android {
25 namespace uhid {
26 
27 class DeviceCallback {
28 public:
29     DeviceCallback(JNIEnv* env, jobject callback);
30     ~DeviceCallback();
31 
32     void onDeviceOpen();
33     void onDeviceGetReport(uint32_t requestId, uint8_t reportId);
34     void onDeviceSetReport(uint8_t rType, const std::vector<uint8_t>& data);
35     void onDeviceOutput(uint8_t rType, const std::vector<uint8_t>& data);
36     void onDeviceError();
37 
38 private:
39     JNIEnv* getJNIEnv();
40     jobject mCallbackObject;
41     JavaVM* mJavaVM;
42 };
43 
44 class Device {
45 public:
46     static std::unique_ptr<Device> open(int32_t id, const char* name, int32_t vid, int32_t pid,
47                                         uint16_t bus, const std::vector<uint8_t>& descriptor,
48                                         std::unique_ptr<DeviceCallback> callback);
49 
50     ~Device();
51 
52     void sendReport(const std::vector<uint8_t>& report) const;
53     void sendGetFeatureReportReply(uint32_t id, const std::vector<uint8_t>& report) const;
54     void close();
55 
56     int handleEvents(int events);
57 
58 private:
59     Device(int32_t id, android::base::unique_fd fd, std::unique_ptr<DeviceCallback> callback);
60     int32_t mId;
61     android::base::unique_fd mFd;
62     std::unique_ptr<DeviceCallback> mDeviceCallback;
63 };
64 
65 
66 } // namespace uhid
67 } // namespace android
68