com_android_commands_hid_Device.cpp revision 1c8cbb54407b9c1d56626de80a796a395012a92c
11f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright/* 21f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * Copyright (C) 2015 The Android Open Source Project 31f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * 41f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * Licensed under the Apache License, Version 2.0 (the "License"); 51f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * you may not use this file except in compliance with the License. 61f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * You may obtain a copy of the License at 71f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * 81f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * http://www.apache.org/licenses/LICENSE-2.0 91f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * 101f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * Unless required by applicable law or agreed to in writing, software 111f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * distributed under the License is distributed on an "AS IS" BASIS, 121f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * See the License for the specific language governing permissions and 141f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright * limitations under the License. 151f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright */ 161f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 171f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#define LOG_TAG "HidCommandDevice" 181f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 191f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include "com_android_commands_hid_Device.h" 201f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 211f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <linux/uhid.h> 221f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 231f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <fcntl.h> 241f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <cstdio> 251f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <cstring> 261f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <memory> 271f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <unistd.h> 281f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 291f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <android_runtime/AndroidRuntime.h> 301f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <android_runtime/Log.h> 311f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <android_os_MessageQueue.h> 321f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <core_jni_helpers.h> 331f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <jni.h> 341f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <JNIHelp.h> 351f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <ScopedPrimitiveArray.h> 361f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <ScopedUtfChars.h> 371f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <utils/Log.h> 381f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <utils/Looper.h> 391f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright#include <utils/StrongPointer.h> 401f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 411f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightnamespace android { 421f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightnamespace uhid { 431f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 441f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightstatic const char* UHID_PATH = "/dev/uhid"; 451f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightstatic const size_t UHID_MAX_NAME_LENGTH = 128; 461f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 471f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightstatic struct { 481f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright jmethodID onDeviceOpen; 491f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright jmethodID onDeviceError; 501f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} gDeviceCallbackClassInfo; 511f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 521c8cbb54407b9c1d56626de80a796a395012a92cAurimas Liutikasstatic int handleLooperEvents(int /* fd */, int events, void* data) { 531f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright Device* d = reinterpret_cast<Device*>(data); 541f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return d->handleEvents(events); 551f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 561f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 571f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightstatic void checkAndClearException(JNIEnv* env, const char* methodName) { 581f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (env->ExceptionCheck()) { 591f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ALOGE("An exception was thrown by callback '%s'.", methodName); 601f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright LOGE_EX(env); 611f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright env->ExceptionClear(); 621f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 631f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 641f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 651f2c7688c1f673790d61645632ae5e1838f021a4Michael WrightDeviceCallback::DeviceCallback(JNIEnv* env, jobject callback) : 661f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright mCallbackObject(env->NewGlobalRef(callback)) { } 671f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 681f2c7688c1f673790d61645632ae5e1838f021a4Michael WrightDeviceCallback::~DeviceCallback() { 691f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright JNIEnv* env = AndroidRuntime::getJNIEnv(); 701f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright env->DeleteGlobalRef(mCallbackObject); 711f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 721f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 731f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightvoid DeviceCallback::onDeviceError() { 741f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright JNIEnv* env = AndroidRuntime::getJNIEnv(); 751f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright env->CallVoidMethod(mCallbackObject, gDeviceCallbackClassInfo.onDeviceError); 761f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright checkAndClearException(env, "onDeviceError"); 771f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 781f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 791f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightvoid DeviceCallback::onDeviceOpen() { 801f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright JNIEnv* env = AndroidRuntime::getJNIEnv(); 811f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright env->CallVoidMethod(mCallbackObject, gDeviceCallbackClassInfo.onDeviceOpen); 821f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright checkAndClearException(env, "onDeviceOpen"); 831f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 841f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 851f2c7688c1f673790d61645632ae5e1838f021a4Michael WrightDevice* Device::open(int32_t id, const char* name, int32_t vid, int32_t pid, 861f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright std::unique_ptr<uint8_t[]> descriptor, size_t descriptorSize, 871f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright std::unique_ptr<DeviceCallback> callback, sp<Looper> looper) { 881f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 891f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright int fd = ::open(UHID_PATH, O_RDWR | O_CLOEXEC); 901f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (fd < 0) { 911f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ALOGE("Failed to open uhid: %s", strerror(errno)); 921f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return nullptr; 931f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 941f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 951f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright struct uhid_event ev; 961f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright memset(&ev, 0, sizeof(ev)); 971f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.type = UHID_CREATE; 981f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright strncpy((char*)ev.u.create.name, name, UHID_MAX_NAME_LENGTH); 991f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.create.rd_data = descriptor.get(); 1001f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.create.rd_size = descriptorSize; 1011f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.create.bus = BUS_BLUETOOTH; 1021f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.create.vendor = vid; 1031f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.create.product = pid; 1041f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.create.version = 0; 1051f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.create.country = 0; 1061f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1071f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright errno = 0; 1081f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ssize_t ret = TEMP_FAILURE_RETRY(::write(fd, &ev, sizeof(ev))); 1091f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (ret < 0 || ret != sizeof(ev)) { 1101f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ::close(fd); 1111f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ALOGE("Failed to create uhid node: %s", strerror(errno)); 1121f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return nullptr; 1131f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1141f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1151f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright // Wait for the device to actually be created. 1161f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ret = TEMP_FAILURE_RETRY(::read(fd, &ev, sizeof(ev))); 1171f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (ret < 0 || ev.type != UHID_START) { 1181f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ::close(fd); 1191f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ALOGE("uhid node failed to start: %s", strerror(errno)); 1201f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return nullptr; 1211f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1221f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1231f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return new Device(id, fd, std::move(callback), looper); 1241f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 1251f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1261f2c7688c1f673790d61645632ae5e1838f021a4Michael WrightDevice::Device(int32_t id, int fd, std::unique_ptr<DeviceCallback> callback, sp<Looper> looper) : 1271f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright mId(id), mFd(fd), mDeviceCallback(std::move(callback)), mLooper(looper) { 1281f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright looper->addFd(fd, 0, Looper::EVENT_INPUT, handleLooperEvents, reinterpret_cast<void*>(this)); 1291f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 1301f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1311f2c7688c1f673790d61645632ae5e1838f021a4Michael WrightDevice::~Device() { 1321f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright mLooper->removeFd(mFd); 1331f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright struct uhid_event ev; 1341f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright memset(&ev, 0, sizeof(ev)); 1351f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.type = UHID_DESTROY; 1361f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright TEMP_FAILURE_RETRY(::write(mFd, &ev, sizeof(ev))); 1371f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ::close(mFd); 1381f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright mFd = -1; 1391f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 1401f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1411f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightvoid Device::sendReport(uint8_t* report, size_t reportSize) { 1421f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright struct uhid_event ev; 1431f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright memset(&ev, 0, sizeof(ev)); 1441f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.type = UHID_INPUT; 1451f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ev.u.input.size = reportSize; 1461f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright memcpy(&ev.u.input.data, report, reportSize); 1471f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ssize_t ret = TEMP_FAILURE_RETRY(::write(mFd, &ev, sizeof(ev))); 1481f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (ret < 0 || ret != sizeof(ev)) { 1491f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ALOGE("Failed to send hid event: %s", strerror(errno)); 1501f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1511f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 1521f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1531f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightint Device::handleEvents(int events) { 1541f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { 1551f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ALOGE("uhid node was closed or an error occurred. events=0x%x", events); 1561f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright mDeviceCallback->onDeviceError(); 1571f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return 0; 1581f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1591f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright struct uhid_event ev; 1601f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ssize_t ret = TEMP_FAILURE_RETRY(::read(mFd, &ev, sizeof(ev))); 1611f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (ret < 0) { 1621f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ALOGE("Failed to read from uhid node: %s", strerror(errno)); 1631f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright mDeviceCallback->onDeviceError(); 1641f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return 0; 1651f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1661f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1671f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (ev.type == UHID_OPEN) { 1681f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright mDeviceCallback->onDeviceOpen(); 1691f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1701f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1711f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return 1; 1721f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 1731f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1741f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} // namespace uhid 1751f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1761f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightstd::unique_ptr<uint8_t[]> getData(JNIEnv* env, jbyteArray javaArray, size_t& outSize) { 1771f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ScopedByteArrayRO scopedArray(env, javaArray); 1781f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright outSize = scopedArray.size(); 1791f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright std::unique_ptr<uint8_t[]> data(new uint8_t[outSize]); 1801f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright for (size_t i = 0; i < outSize; i++) { 1811f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright data[i] = static_cast<uint8_t>(scopedArray[i]); 1821f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1831f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return data; 1841f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 1851f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1861c8cbb54407b9c1d56626de80a796a395012a92cAurimas Liutikasstatic jlong openDevice(JNIEnv* env, jclass /* clazz */, jstring rawName, jint id, jint vid, jint pid, 1871f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright jbyteArray rawDescriptor, jobject queue, jobject callback) { 1881f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright ScopedUtfChars name(env, rawName); 1891f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (name.c_str() == nullptr) { 1901f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return 0; 1911f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 1921f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1931f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright size_t size; 1941f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright std::unique_ptr<uint8_t[]> desc = getData(env, rawDescriptor, size); 1951f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1961f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright std::unique_ptr<uhid::DeviceCallback> cb(new uhid::DeviceCallback(env, callback)); 1971f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright sp<Looper> looper = android_os_MessageQueue_getMessageQueue(env, queue)->getLooper(); 1981f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 1991f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright uhid::Device* d = uhid::Device::open( 2001f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright id, reinterpret_cast<const char*>(name.c_str()), vid, pid, 2011f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright std::move(desc), size, std::move(cb), std::move(looper)); 2021f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return reinterpret_cast<jlong>(d); 2031f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 2041f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2051c8cbb54407b9c1d56626de80a796a395012a92cAurimas Liutikasstatic void sendReport(JNIEnv* env, jclass /* clazz */, jlong ptr,jbyteArray rawReport) { 2061f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright size_t size; 2071f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright std::unique_ptr<uint8_t[]> report = getData(env, rawReport, size); 2081f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright uhid::Device* d = reinterpret_cast<uhid::Device*>(ptr); 2091f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (d) { 2101f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright d->sendReport(report.get(), size); 2111f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 2121f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 2131f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2141c8cbb54407b9c1d56626de80a796a395012a92cAurimas Liutikasstatic void closeDevice(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) { 2151f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright uhid::Device* d = reinterpret_cast<uhid::Device*>(ptr); 2161f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (d) { 2171f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright delete d; 2181f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 2191f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 2201f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2211f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightstatic JNINativeMethod sMethods[] = { 2221f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright { "nativeOpenDevice", 2231f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright "(Ljava/lang/String;III[BLandroid/os/MessageQueue;" 2241f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright "Lcom/android/commands/hid/Device$DeviceCallback;)J", 2251f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright reinterpret_cast<void*>(openDevice) }, 2261f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright { "nativeSendReport", "(J[B)V", reinterpret_cast<void*>(sendReport) }, 2271f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright { "nativeCloseDevice", "(J)V", reinterpret_cast<void*>(closeDevice) }, 2281f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright}; 2291f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2301f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightint register_com_android_commands_hid_Device(JNIEnv* env) { 2311f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright jclass clazz = FindClassOrDie(env, "com/android/commands/hid/Device$DeviceCallback"); 2321f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright uhid::gDeviceCallbackClassInfo.onDeviceOpen = 2331f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright GetMethodIDOrDie(env, clazz, "onDeviceOpen", "()V"); 2341f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright uhid::gDeviceCallbackClassInfo.onDeviceError= 2351f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright GetMethodIDOrDie(env, clazz, "onDeviceError", "()V"); 2361f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return jniRegisterNativeMethods(env, "com/android/commands/hid/Device", 2371f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright sMethods, NELEM(sMethods)); 2381f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 2391f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2401f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} // namespace android 2411f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2421f2c7688c1f673790d61645632ae5e1838f021a4Michael Wrightjint JNI_OnLoad(JavaVM* jvm, void*) { 2431f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright JNIEnv *env = NULL; 2441f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) { 2451f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return JNI_ERR; 2461f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 2471f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2481f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright if (android::register_com_android_commands_hid_Device(env) < 0 ){ 2491f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return JNI_ERR; 2501f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright } 2511f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright 2521f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright return JNI_VERSION_1_6; 2531f2c7688c1f673790d61645632ae5e1838f021a4Michael Wright} 254