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 | #define LOG_TAG "HidCommandDevice" |
| 18 | |
| 19 | #include "com_android_commands_hid_Device.h" |
| 20 | |
| 21 | #include <linux/uhid.h> |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <cstdio> |
| 25 | #include <cstring> |
| 26 | #include <memory> |
| 27 | #include <unistd.h> |
| 28 | |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 29 | #include <jni.h> |
Steven Moreland | c95dca8 | 2017-08-01 10:18:40 -0700 | [diff] [blame] | 30 | #include <nativehelper/JNIHelp.h> |
| 31 | #include <nativehelper/ScopedPrimitiveArray.h> |
Steven Moreland | 65e2ca2 | 2017-08-10 15:55:12 -0700 | [diff] [blame] | 32 | #include <nativehelper/ScopedUtfChars.h> |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 33 | #include <android/looper.h> |
| 34 | #include <android/log.h> |
| 35 | |
| 36 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) |
| 37 | #define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__) |
| 38 | #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) |
| 39 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace uhid { |
| 43 | |
| 44 | static const char* UHID_PATH = "/dev/uhid"; |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 45 | |
| 46 | static struct { |
| 47 | jmethodID onDeviceOpen; |
| 48 | jmethodID onDeviceError; |
| 49 | } gDeviceCallbackClassInfo; |
| 50 | |
Aurimas Liutikas | 1c8cbb5 | 2016-02-19 13:44:25 -0800 | [diff] [blame] | 51 | static int handleLooperEvents(int /* fd */, int events, void* data) { |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 52 | Device* d = reinterpret_cast<Device*>(data); |
| 53 | return d->handleEvents(events); |
| 54 | } |
| 55 | |
| 56 | static void checkAndClearException(JNIEnv* env, const char* methodName) { |
| 57 | if (env->ExceptionCheck()) { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 58 | LOGE("An exception was thrown by callback '%s'.", methodName); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 59 | env->ExceptionClear(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | DeviceCallback::DeviceCallback(JNIEnv* env, jobject callback) : |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 64 | mCallbackObject(env->NewGlobalRef(callback)) { |
| 65 | env->GetJavaVM(&mJavaVM); |
| 66 | } |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 67 | |
| 68 | DeviceCallback::~DeviceCallback() { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 69 | JNIEnv* env = getJNIEnv(); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 70 | env->DeleteGlobalRef(mCallbackObject); |
| 71 | } |
| 72 | |
| 73 | void DeviceCallback::onDeviceError() { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 74 | JNIEnv* env = getJNIEnv(); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 75 | env->CallVoidMethod(mCallbackObject, gDeviceCallbackClassInfo.onDeviceError); |
| 76 | checkAndClearException(env, "onDeviceError"); |
| 77 | } |
| 78 | |
| 79 | void DeviceCallback::onDeviceOpen() { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 80 | JNIEnv* env = getJNIEnv(); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 81 | env->CallVoidMethod(mCallbackObject, gDeviceCallbackClassInfo.onDeviceOpen); |
| 82 | checkAndClearException(env, "onDeviceOpen"); |
| 83 | } |
| 84 | |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 85 | JNIEnv* DeviceCallback::getJNIEnv() { |
| 86 | JNIEnv* env; |
| 87 | mJavaVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6); |
| 88 | return env; |
| 89 | } |
| 90 | |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 91 | Device* 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] | 92 | std::vector<uint8_t> descriptor, std::unique_ptr<DeviceCallback> callback) { |
| 93 | |
| 94 | size_t size = descriptor.size(); |
| 95 | if (size > HID_MAX_DESCRIPTOR_SIZE) { |
| 96 | LOGE("Received invalid hid report with descriptor size %zu, skipping", size); |
| 97 | return nullptr; |
| 98 | } |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 99 | |
| 100 | int fd = ::open(UHID_PATH, O_RDWR | O_CLOEXEC); |
| 101 | if (fd < 0) { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 102 | LOGE("Failed to open uhid: %s", strerror(errno)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 103 | return nullptr; |
| 104 | } |
| 105 | |
| 106 | struct uhid_event ev; |
| 107 | memset(&ev, 0, sizeof(ev)); |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 108 | ev.type = UHID_CREATE2; |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 109 | strlcpy(reinterpret_cast<char*>(ev.u.create2.name), name, sizeof(ev.u.create2.name)); |
| 110 | memcpy(&ev.u.create2.rd_data, descriptor.data(), |
| 111 | size * sizeof(ev.u.create2.rd_data[0])); |
| 112 | ev.u.create2.rd_size = size; |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 113 | ev.u.create2.bus = BUS_BLUETOOTH; |
| 114 | ev.u.create2.vendor = vid; |
| 115 | ev.u.create2.product = pid; |
| 116 | ev.u.create2.version = 0; |
| 117 | ev.u.create2.country = 0; |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 118 | |
| 119 | errno = 0; |
| 120 | ssize_t ret = TEMP_FAILURE_RETRY(::write(fd, &ev, sizeof(ev))); |
| 121 | if (ret < 0 || ret != sizeof(ev)) { |
| 122 | ::close(fd); |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 123 | LOGE("Failed to create uhid node: %s", strerror(errno)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 124 | return nullptr; |
| 125 | } |
| 126 | |
| 127 | // Wait for the device to actually be created. |
| 128 | ret = TEMP_FAILURE_RETRY(::read(fd, &ev, sizeof(ev))); |
| 129 | if (ret < 0 || ev.type != UHID_START) { |
| 130 | ::close(fd); |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 131 | LOGE("uhid node failed to start: %s", strerror(errno)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 132 | return nullptr; |
| 133 | } |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 134 | return new Device(id, fd, std::move(callback)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 137 | Device::Device(int32_t id, int fd, std::unique_ptr<DeviceCallback> callback) : |
| 138 | mId(id), mFd(fd), mDeviceCallback(std::move(callback)) { |
| 139 | ALooper* aLooper = ALooper_forThread(); |
| 140 | if (aLooper == NULL) { |
| 141 | LOGE("Could not get ALooper, ALooper_forThread returned NULL"); |
| 142 | aLooper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); |
| 143 | } |
| 144 | ALooper_addFd(aLooper, fd, 0, ALOOPER_EVENT_INPUT, handleLooperEvents, |
| 145 | reinterpret_cast<void*>(this)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | Device::~Device() { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 149 | ALooper* looper = ALooper_forThread(); |
| 150 | if (looper != NULL) { |
| 151 | ALooper_removeFd(looper, mFd); |
| 152 | } else { |
| 153 | LOGE("Could not remove fd, ALooper_forThread() returned NULL!"); |
| 154 | } |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 155 | struct uhid_event ev; |
| 156 | memset(&ev, 0, sizeof(ev)); |
| 157 | ev.type = UHID_DESTROY; |
| 158 | TEMP_FAILURE_RETRY(::write(mFd, &ev, sizeof(ev))); |
| 159 | ::close(mFd); |
| 160 | mFd = -1; |
| 161 | } |
| 162 | |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 163 | void Device::sendReport(const std::vector<uint8_t>& report) const { |
| 164 | if (report.size() > UHID_DATA_MAX) { |
| 165 | LOGE("Received invalid report of size %zu, skipping", report.size()); |
| 166 | return; |
| 167 | } |
| 168 | |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 169 | struct uhid_event ev; |
| 170 | memset(&ev, 0, sizeof(ev)); |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 171 | ev.type = UHID_INPUT2; |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 172 | ev.u.input2.size = report.size(); |
| 173 | memcpy(&ev.u.input2.data, report.data(), report.size() * sizeof(ev.u.input2.data[0])); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 174 | ssize_t ret = TEMP_FAILURE_RETRY(::write(mFd, &ev, sizeof(ev))); |
| 175 | if (ret < 0 || ret != sizeof(ev)) { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 176 | LOGE("Failed to send hid event: %s", strerror(errno)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | int Device::handleEvents(int events) { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 181 | if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) { |
| 182 | LOGE("uhid node was closed or an error occurred. events=0x%x", events); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 183 | mDeviceCallback->onDeviceError(); |
| 184 | return 0; |
| 185 | } |
| 186 | struct uhid_event ev; |
| 187 | ssize_t ret = TEMP_FAILURE_RETRY(::read(mFd, &ev, sizeof(ev))); |
| 188 | if (ret < 0) { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 189 | LOGE("Failed to read from uhid node: %s", strerror(errno)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 190 | mDeviceCallback->onDeviceError(); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | if (ev.type == UHID_OPEN) { |
| 195 | mDeviceCallback->onDeviceOpen(); |
| 196 | } |
| 197 | |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | } // namespace uhid |
| 202 | |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 203 | std::vector<uint8_t> getData(JNIEnv* env, jbyteArray javaArray) { |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 204 | ScopedByteArrayRO scopedArray(env, javaArray); |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 205 | size_t size = scopedArray.size(); |
| 206 | std::vector<uint8_t> data; |
| 207 | data.reserve(size); |
| 208 | for (size_t i = 0; i < size; i++) { |
| 209 | data.push_back(static_cast<uint8_t>(scopedArray[i])); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 210 | } |
| 211 | return data; |
| 212 | } |
| 213 | |
Aurimas Liutikas | 1c8cbb5 | 2016-02-19 13:44:25 -0800 | [diff] [blame] | 214 | static jlong openDevice(JNIEnv* env, jclass /* clazz */, jstring rawName, jint id, jint vid, jint pid, |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 215 | jbyteArray rawDescriptor, jobject callback) { |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 216 | ScopedUtfChars name(env, rawName); |
| 217 | if (name.c_str() == nullptr) { |
| 218 | return 0; |
| 219 | } |
| 220 | |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 221 | std::vector<uint8_t> desc = getData(env, rawDescriptor); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 222 | |
| 223 | std::unique_ptr<uhid::DeviceCallback> cb(new uhid::DeviceCallback(env, callback)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 224 | |
| 225 | uhid::Device* d = uhid::Device::open( |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 226 | id, reinterpret_cast<const char*>(name.c_str()), vid, pid, desc, std::move(cb)); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 227 | return reinterpret_cast<jlong>(d); |
| 228 | } |
| 229 | |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 230 | static void sendReport(JNIEnv* env, jclass /* clazz */, jlong ptr, jbyteArray rawReport) { |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 231 | std::vector<uint8_t> report = getData(env, rawReport); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 232 | uhid::Device* d = reinterpret_cast<uhid::Device*>(ptr); |
| 233 | if (d) { |
Siarhei Vishniakou | 6c6dd65 | 2018-07-16 17:01:40 +0100 | [diff] [blame] | 234 | d->sendReport(report); |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 235 | } else { |
| 236 | LOGE("Could not send report, Device* is null!"); |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Aurimas Liutikas | 1c8cbb5 | 2016-02-19 13:44:25 -0800 | [diff] [blame] | 240 | static void closeDevice(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) { |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 241 | uhid::Device* d = reinterpret_cast<uhid::Device*>(ptr); |
| 242 | if (d) { |
| 243 | delete d; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static JNINativeMethod sMethods[] = { |
| 248 | { "nativeOpenDevice", |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 249 | "(Ljava/lang/String;III[B" |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 250 | "Lcom/android/commands/hid/Device$DeviceCallback;)J", |
| 251 | reinterpret_cast<void*>(openDevice) }, |
| 252 | { "nativeSendReport", "(J[B)V", reinterpret_cast<void*>(sendReport) }, |
| 253 | { "nativeCloseDevice", "(J)V", reinterpret_cast<void*>(closeDevice) }, |
| 254 | }; |
| 255 | |
| 256 | int register_com_android_commands_hid_Device(JNIEnv* env) { |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 257 | jclass clazz = env->FindClass("com/android/commands/hid/Device$DeviceCallback"); |
| 258 | if (clazz == NULL) { |
| 259 | LOGE("Unable to find class 'DeviceCallback'"); |
| 260 | return JNI_ERR; |
| 261 | } |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 262 | uhid::gDeviceCallbackClassInfo.onDeviceOpen = |
Siarhei Vishniakou | 55656e4 | 2017-03-31 17:23:39 -0700 | [diff] [blame] | 263 | env->GetMethodID(clazz, "onDeviceOpen", "()V"); |
| 264 | uhid::gDeviceCallbackClassInfo.onDeviceError = |
| 265 | env->GetMethodID(clazz, "onDeviceError", "()V"); |
| 266 | if (uhid::gDeviceCallbackClassInfo.onDeviceOpen == NULL || |
| 267 | uhid::gDeviceCallbackClassInfo.onDeviceError == NULL) { |
| 268 | LOGE("Unable to obtain onDeviceOpen or onDeviceError methods"); |
| 269 | return JNI_ERR; |
| 270 | } |
| 271 | |
Michael Wright | 1f2c768 | 2015-06-03 13:46:48 +0100 | [diff] [blame] | 272 | return jniRegisterNativeMethods(env, "com/android/commands/hid/Device", |
| 273 | sMethods, NELEM(sMethods)); |
| 274 | } |
| 275 | |
| 276 | } // namespace android |
| 277 | |
| 278 | jint JNI_OnLoad(JavaVM* jvm, void*) { |
| 279 | JNIEnv *env = NULL; |
| 280 | if (jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) { |
| 281 | return JNI_ERR; |
| 282 | } |
| 283 | |
| 284 | if (android::register_com_android_commands_hid_Device(env) < 0 ){ |
| 285 | return JNI_ERR; |
| 286 | } |
| 287 | |
| 288 | return JNI_VERSION_1_6; |
| 289 | } |