• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008, 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 "SensorService"
18 
19 #define LOG_NDEBUG 0
20 #include "utils/Log.h"
21 
22 #include <hardware/sensors.h>
23 
24 #include "jni.h"
25 #include "JNIHelp.h"
26 
27 namespace android {
28 
29 static struct file_descriptor_offsets_t
30 {
31     jclass mClass;
32     jmethodID mConstructor;
33     jfieldID mDescriptor;
34 } gFileDescriptorOffsets;
35 
36 static struct parcel_file_descriptor_offsets_t
37 {
38     jclass mClass;
39     jmethodID mConstructor;
40 } gParcelFileDescriptorOffsets;
41 
42 static struct bundle_descriptor_offsets_t
43 {
44     jclass mClass;
45     jmethodID mConstructor;
46     jmethodID mPutIntArray;
47     jmethodID mPutParcelableArray;
48 } gBundleOffsets;
49 
50 /*
51  * The method below are not thread-safe and not intended to be
52  */
53 
54 static sensors_control_device_t* sSensorDevice = 0;
55 
56 static jint
android_init(JNIEnv * env,jclass clazz)57 android_init(JNIEnv *env, jclass clazz)
58 {
59     sensors_module_t* module;
60     if (hw_get_module(SENSORS_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) {
61         if (sensors_control_open(&module->common, &sSensorDevice) == 0) {
62             const struct sensor_t* list;
63             int count = module->get_sensors_list(module, &list);
64             return count;
65         }
66     }
67     return 0;
68 }
69 
70 static jobject
android_open(JNIEnv * env,jclass clazz)71 android_open(JNIEnv *env, jclass clazz)
72 {
73     native_handle_t* handle = sSensorDevice->open_data_source(sSensorDevice);
74     if (!handle) {
75         return NULL;
76     }
77 
78     // new Bundle()
79     jobject bundle = env->NewObject(
80             gBundleOffsets.mClass,
81             gBundleOffsets.mConstructor);
82 
83     if (handle->numFds > 0) {
84         jobjectArray fdArray = env->NewObjectArray(handle->numFds,
85                 gParcelFileDescriptorOffsets.mClass, NULL);
86         for (int i = 0; i < handle->numFds; i++) {
87             // new FileDescriptor()
88             jobject fd = env->NewObject(gFileDescriptorOffsets.mClass,
89                     gFileDescriptorOffsets.mConstructor);
90             env->SetIntField(fd, gFileDescriptorOffsets.mDescriptor, handle->data[i]);
91             // new ParcelFileDescriptor()
92             jobject pfd = env->NewObject(gParcelFileDescriptorOffsets.mClass,
93                     gParcelFileDescriptorOffsets.mConstructor, fd);
94             env->SetObjectArrayElement(fdArray, i, pfd);
95         }
96         // bundle.putParcelableArray("fds", fdArray);
97         env->CallVoidMethod(bundle, gBundleOffsets.mPutParcelableArray,
98                 env->NewStringUTF("fds"), fdArray);
99     }
100 
101     if (handle->numInts > 0) {
102         jintArray intArray = env->NewIntArray(handle->numInts);
103         env->SetIntArrayRegion(intArray, 0, handle->numInts, &handle->data[handle->numInts]);
104         // bundle.putIntArray("ints", intArray);
105         env->CallVoidMethod(bundle, gBundleOffsets.mPutIntArray,
106                 env->NewStringUTF("ints"), intArray);
107     }
108 
109     // delete the file handle, but don't close any file descriptors
110     native_handle_delete(handle);
111     return bundle;
112 }
113 
114 static jint
android_close(JNIEnv * env,jclass clazz)115 android_close(JNIEnv *env, jclass clazz)
116 {
117     if (sSensorDevice->close_data_source)
118         return sSensorDevice->close_data_source(sSensorDevice);
119     else
120         return 0;
121 }
122 
123 static jboolean
android_activate(JNIEnv * env,jclass clazz,jint sensor,jboolean activate)124 android_activate(JNIEnv *env, jclass clazz, jint sensor, jboolean activate)
125 {
126     int active = sSensorDevice->activate(sSensorDevice, sensor, activate);
127     return (active<0) ? false : true;
128 }
129 
130 static jint
android_set_delay(JNIEnv * env,jclass clazz,jint ms)131 android_set_delay(JNIEnv *env, jclass clazz, jint ms)
132 {
133     return sSensorDevice->set_delay(sSensorDevice, ms);
134 }
135 
136 static jint
android_data_wake(JNIEnv * env,jclass clazz)137 android_data_wake(JNIEnv *env, jclass clazz)
138 {
139     int res = sSensorDevice->wake(sSensorDevice);
140     return res;
141 }
142 
143 
144 static JNINativeMethod gMethods[] = {
145     {"_sensors_control_init",     "()I",   (void*) android_init },
146     {"_sensors_control_open",     "()Landroid/os/Bundle;",  (void*) android_open },
147     {"_sensors_control_close",     "()I",  (void*) android_close },
148     {"_sensors_control_activate", "(IZ)Z", (void*) android_activate },
149     {"_sensors_control_wake",     "()I", (void*) android_data_wake },
150     {"_sensors_control_set_delay","(I)I", (void*) android_set_delay },
151 };
152 
register_android_server_SensorService(JNIEnv * env)153 int register_android_server_SensorService(JNIEnv *env)
154 {
155     jclass clazz;
156 
157     clazz = env->FindClass("java/io/FileDescriptor");
158     gFileDescriptorOffsets.mClass = (jclass)env->NewGlobalRef(clazz);
159     gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
160     gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
161 
162     clazz = env->FindClass("android/os/ParcelFileDescriptor");
163     gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
164     gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>",
165             "(Ljava/io/FileDescriptor;)V");
166 
167     clazz = env->FindClass("android/os/Bundle");
168     gBundleOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
169     gBundleOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
170     gBundleOffsets.mPutIntArray = env->GetMethodID(clazz, "putIntArray", "(Ljava/lang/String;[I)V");
171     gBundleOffsets.mPutParcelableArray = env->GetMethodID(clazz, "putParcelableArray",
172             "(Ljava/lang/String;[Landroid/os/Parcelable;)V");
173 
174     return jniRegisterNativeMethods(env, "com/android/server/SensorService",
175             gMethods, NELEM(gMethods));
176 }
177 
178 }; // namespace android
179