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
21namespace android {
22namespace uhid {
23
24class DeviceCallback {
25public:
26    DeviceCallback(JNIEnv* env, jobject callback);
27    ~DeviceCallback();
28
29    void onDeviceOpen();
30    void onDeviceError();
31
32private:
33    JNIEnv* getJNIEnv();
34    jobject mCallbackObject;
35    JavaVM* mJavaVM;
36};
37
38class Device {
39public:
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);
43
44    Device(int32_t id, int fd, std::unique_ptr<DeviceCallback> callback);
45    ~Device();
46
47    void sendReport(uint8_t* report, size_t reportSize);
48    void close();
49
50    int handleEvents(int events);
51
52private:
53    int32_t mId;
54    int mFd;
55    std::unique_ptr<DeviceCallback> mDeviceCallback;
56};
57
58
59} // namespace uhid
60} // namespace android
61