android_hardware_SensorManager.cpp revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 "Sensors"
18
19#include <hardware/sensors.h>
20
21#include "jni.h"
22#include "JNIHelp.h"
23
24
25namespace android {
26
27/*
28 * The method below are not thread-safe and not intended to be
29 */
30
31static jint
32android_data_open(JNIEnv *env, jclass clazz, jobject fdo)
33{
34    jclass FileDescriptor = env->FindClass("java/io/FileDescriptor");
35    jfieldID offset = env->GetFieldID(FileDescriptor, "descriptor", "I");
36    int fd = env->GetIntField(fdo, offset);
37    return sensors_data_open(fd); // doesn't take ownership of fd
38}
39
40static jint
41android_data_close(JNIEnv *env, jclass clazz)
42{
43    return sensors_data_close();
44}
45
46static jint
47android_data_poll(JNIEnv *env, jclass clazz, jfloatArray values, jint sensors)
48{
49    sensors_data_t data;
50    int res = sensors_data_poll(&data, sensors);
51    if (res) {
52        env->SetFloatArrayRegion(values, 0, 3, data.vector.v);
53        // return the sensor's number
54        res = 31 - __builtin_clz(res);
55        // and its status in the top 4 bits
56        res |= data.vector.status << 28;
57    }
58    return res;
59}
60
61static jint
62android_data_get_sensors(JNIEnv *env, jclass clazz)
63{
64    return sensors_data_get_sensors();
65}
66
67static JNINativeMethod gMethods[] = {
68    {"_sensors_data_open",  "(Ljava/io/FileDescriptor;)I",  (void*) android_data_open },
69    {"_sensors_data_close", "()I",   (void*) android_data_close },
70    {"_sensors_data_poll",  "([FI)I", (void*) android_data_poll },
71    {"_sensors_data_get_sensors","()I",   (void*) android_data_get_sensors },
72};
73
74}; // namespace android
75
76using namespace android;
77
78int register_android_hardware_SensorManager(JNIEnv *env)
79{
80    return jniRegisterNativeMethods(env, "android/hardware/SensorManager",
81            gMethods, NELEM(gMethods));
82}
83