1 /*
2 * Copyright (C) 2024 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 <core_jni_helpers.h>
18 #include <jni.h>
19 #include <linux/hidraw.h>
20 #include <linux/input.h>
21 #include <nativehelper/JNIHelp.h>
22 #include <sys/ioctl.h>
23
24 /*
25 * This file defines simple wrappers around the kernel UAPI HIDRAW driver's ioctl() commands.
26 * See kernel example samples/hidraw/hid-example.c
27 *
28 * All methods expect an open file descriptor int from Java.
29 */
30
31 namespace android {
32
33 namespace {
34
35 // Max sizes we allow for results from string ioctl calls, copied from UAPI linux/uhid.h.
36 // The ioctl implementation writes at most this many bytes to the provided buffer:
37 constexpr int NAME_SIZE_MAX = 128; // HIDIOCGRAWNAME (device name)
38 constexpr int UNIQ_SIZE_MAX = 64; // HIDIOCGRAWUNIQ (BT address or USB serial number)
39
40 } // anonymous namespace
41
com_android_server_accessibility_BrailleDisplayConnection_getHidrawDescSize(JNIEnv * env,jclass,int fd)42 static jint com_android_server_accessibility_BrailleDisplayConnection_getHidrawDescSize(
43 JNIEnv* env, jclass /*clazz*/, int fd) {
44 int size = 0;
45 if (ioctl(fd, HIDIOCGRDESCSIZE, &size) < 0) {
46 return -1;
47 }
48 return size;
49 }
50
com_android_server_accessibility_BrailleDisplayConnection_getHidrawDesc(JNIEnv * env,jclass,int fd,int descSize)51 static jbyteArray com_android_server_accessibility_BrailleDisplayConnection_getHidrawDesc(
52 JNIEnv* env, jclass /*clazz*/, int fd, int descSize) {
53 struct hidraw_report_descriptor desc;
54 desc.size = descSize;
55 if (ioctl(fd, HIDIOCGRDESC, &desc) < 0) {
56 return nullptr;
57 }
58 jbyteArray result = env->NewByteArray(descSize);
59 if (result != nullptr) {
60 env->SetByteArrayRegion(result, 0, descSize, (jbyte*)desc.value);
61 }
62 // Local ref is not deleted because it is returned to Java
63 return result;
64 }
65
com_android_server_accessibility_BrailleDisplayConnection_getHidrawUniq(JNIEnv * env,jclass,int fd)66 static jstring com_android_server_accessibility_BrailleDisplayConnection_getHidrawUniq(
67 JNIEnv* env, jclass /*clazz*/, int fd) {
68 char buf[UNIQ_SIZE_MAX];
69 if (ioctl(fd, HIDIOCGRAWUNIQ(UNIQ_SIZE_MAX), buf) < 0) {
70 return nullptr;
71 }
72 // Local ref is not deleted because it is returned to Java
73 return env->NewStringUTF(buf);
74 }
75
com_android_server_accessibility_BrailleDisplayConnection_getHidrawBusType(JNIEnv * env,jclass,int fd)76 static jint com_android_server_accessibility_BrailleDisplayConnection_getHidrawBusType(
77 JNIEnv* env, jclass /*clazz*/, int fd) {
78 struct hidraw_devinfo info;
79 if (ioctl(fd, HIDIOCGRAWINFO, &info) < 0) {
80 return -1;
81 }
82 return info.bustype;
83 }
84
com_android_server_accessibility_BrailleDisplayConnection_getHidrawName(JNIEnv * env,jclass,int fd)85 static jstring com_android_server_accessibility_BrailleDisplayConnection_getHidrawName(
86 JNIEnv* env, jclass /*clazz*/, int fd) {
87 char buf[NAME_SIZE_MAX];
88 if (ioctl(fd, HIDIOCGRAWNAME(NAME_SIZE_MAX), buf) < 0) {
89 return nullptr;
90 }
91 // Local ref is not deleted because it is returned to Java
92 return env->NewStringUTF(buf);
93 }
94
95 static const JNINativeMethod gMethods[] = {
96 {"nativeGetHidrawDescSize", "(I)I",
97 (void*)com_android_server_accessibility_BrailleDisplayConnection_getHidrawDescSize},
98 {"nativeGetHidrawDesc", "(II)[B",
99 (void*)com_android_server_accessibility_BrailleDisplayConnection_getHidrawDesc},
100 {"nativeGetHidrawUniq", "(I)Ljava/lang/String;",
101 (void*)com_android_server_accessibility_BrailleDisplayConnection_getHidrawUniq},
102 {"nativeGetHidrawBusType", "(I)I",
103 (void*)com_android_server_accessibility_BrailleDisplayConnection_getHidrawBusType},
104 {"nativeGetHidrawName", "(I)Ljava/lang/String;",
105 (void*)com_android_server_accessibility_BrailleDisplayConnection_getHidrawName},
106 };
107
register_com_android_server_accessibility_BrailleDisplayConnection(JNIEnv * env)108 int register_com_android_server_accessibility_BrailleDisplayConnection(JNIEnv* env) {
109 return RegisterMethodsOrDie(env, "com/android/server/accessibility/BrailleDisplayConnection",
110 gMethods, NELEM(gMethods));
111 }
112
113 }; // namespace android
114