Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 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> |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 18 | #include <vector> |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 19 | |
| 20 | #include <jni.h> |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace uhid { |
| 24 | |
| 25 | class DeviceCallback { |
| 26 | public: |
| 27 | DeviceCallback(JNIEnv* env, jobject callback); |
| 28 | ~DeviceCallback(); |
| 29 | |
| 30 | void onDeviceOpen(); |
Kim Low | d47c013 | 2018-11-09 17:15:13 -0800 | [diff] [blame] | 31 | void onDeviceGetReport(uint32_t requestId, uint8_t reportId); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 32 | void onDeviceError(); |
| 33 | |
| 34 | private: |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 35 | JNIEnv* getJNIEnv(); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 36 | jobject mCallbackObject; |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 37 | JavaVM* mJavaVM; |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | class Device { |
| 41 | public: |
| 42 | static Device* open(int32_t id, const char* name, int32_t vid, int32_t pid, |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 43 | std::vector<uint8_t> descriptor, std::unique_ptr<DeviceCallback> callback); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 44 | |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 45 | Device(int32_t id, int fd, std::unique_ptr<DeviceCallback> callback); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 46 | ~Device(); |
| 47 | |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 48 | void sendReport(const std::vector<uint8_t>& report) const; |
Kim Low | d47c013 | 2018-11-09 17:15:13 -0800 | [diff] [blame] | 49 | void sendGetFeatureReportReply(uint32_t id, const std::vector<uint8_t>& report) const; |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 50 | void close(); |
| 51 | |
| 52 | int handleEvents(int events); |
| 53 | |
| 54 | private: |
| 55 | int32_t mId; |
| 56 | int mFd; |
| 57 | std::unique_ptr<DeviceCallback> mDeviceCallback; |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | |
| 61 | } // namespace uhid |
| 62 | } // namespace android |