blob: 695a8a3898fb0ad49cf909c4a26e49ea6e2c0a70 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 "Sensors"
18
19#include <hardware/sensors.h>
20
21#include "jni.h"
22#include "JNIHelp.h"
23
24namespace android {
25
26static struct file_descriptor_offsets_t
27{
28 jclass mClass;
29 jmethodID mConstructor;
30 jfieldID mDescriptor;
31} gFileDescriptorOffsets;
32
33static struct parcel_file_descriptor_offsets_t
34{
35 jclass mClass;
36 jmethodID mConstructor;
37} gParcelFileDescriptorOffsets;
38
39/*
40 * The method below are not thread-safe and not intended to be
41 */
42
43static sensors_control_device_t* sSensorDevice = 0;
44
45static jint
46android_init(JNIEnv *env, jclass clazz)
47{
48 sensors_module_t* module;
49 if (hw_get_module(SENSORS_HARDWARE_MODULE_ID, (const hw_module_t**)&module) == 0) {
50 if (sensors_control_open(&module->common, &sSensorDevice) == 0) {
51 const struct sensor_t* list;
52 int count = module->get_sensors_list(module, &list);
53 return count;
54 }
55 }
56 return 0;
57}
58
59static jobject
60android_open(JNIEnv *env, jclass clazz)
61{
62 int fd = sSensorDevice->open_data_source(sSensorDevice);
63 // new FileDescriptor()
64 jobject filedescriptor = env->NewObject(
65 gFileDescriptorOffsets.mClass,
66 gFileDescriptorOffsets.mConstructor);
67
68 if (filedescriptor != NULL) {
69 env->SetIntField(filedescriptor, gFileDescriptorOffsets.mDescriptor, fd);
70 // new ParcelFileDescriptor()
71 return env->NewObject(gParcelFileDescriptorOffsets.mClass,
72 gParcelFileDescriptorOffsets.mConstructor,
73 filedescriptor);
74 }
75 close(fd);
76 return NULL;
77}
78
79static jboolean
80android_activate(JNIEnv *env, jclass clazz, jint sensor, jboolean activate)
81{
82 int active = sSensorDevice->activate(sSensorDevice, sensor, activate);
83 return (active<0) ? false : true;
84}
85
86static jint
87android_set_delay(JNIEnv *env, jclass clazz, jint ms)
88{
89 return sSensorDevice->set_delay(sSensorDevice, ms);
90}
91
92static jint
93android_data_wake(JNIEnv *env, jclass clazz)
94{
95 int res = sSensorDevice->wake(sSensorDevice);
96 return res;
97}
98
99
100static JNINativeMethod gMethods[] = {
101 {"_sensors_control_init", "()I", (void*) android_init },
102 {"_sensors_control_open", "()Landroid/os/ParcelFileDescriptor;", (void*) android_open },
103 {"_sensors_control_activate", "(IZ)Z", (void*) android_activate },
104 {"_sensors_control_wake", "()I", (void*) android_data_wake },
105 {"_sensors_control_set_delay","(I)I", (void*) android_set_delay },
106};
107
108int register_android_server_SensorService(JNIEnv *env)
109{
110 jclass clazz;
111
112 clazz = env->FindClass("java/io/FileDescriptor");
113 gFileDescriptorOffsets.mClass = (jclass)env->NewGlobalRef(clazz);
114 gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V");
115 gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I");
116
117 clazz = env->FindClass("android/os/ParcelFileDescriptor");
118 gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
119 gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
120
121 return jniRegisterNativeMethods(env, "com/android/server/SensorService",
122 gMethods, NELEM(gMethods));
123}
124
125}; // namespace android