android_os_UEventObserver.cpp revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 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 "UEventObserver"
18#include "utils/Log.h"
19
20#include "hardware/uevent.h"
21#include "jni.h"
22#include "JNIHelp.h"
23#include "android_runtime/AndroidRuntime.h"
24
25namespace android
26{
27
28static void
29android_os_UEventObserver_native_setup(JNIEnv *env, jclass clazz)
30{
31    if (!uevent_init()) {
32        jniThrowException(env, "java/lang/RuntimeException",
33                          "Unable to open socket for UEventObserver");
34    }
35}
36
37static int
38android_os_UEventObserver_next_event(JNIEnv *env, jclass clazz, jbyteArray jbuffer)
39{
40    int buf_sz = env->GetArrayLength(jbuffer);
41    char *buffer = (char*)env->GetByteArrayElements(jbuffer, NULL);
42
43    int length = uevent_next_event(buffer, buf_sz - 1);
44
45    env->ReleaseByteArrayElements(jbuffer, (jbyte*)buffer, 0);
46
47    return length;
48}
49
50static JNINativeMethod gMethods[] = {
51    {"native_setup", "()V",   (void *)android_os_UEventObserver_native_setup},
52    {"next_event",   "([B)I", (void *)android_os_UEventObserver_next_event},
53};
54
55
56int register_android_os_UEventObserver(JNIEnv *env)
57{
58    jclass clazz;
59
60    clazz = env->FindClass("android/os/UEventObserver");
61    if (clazz == NULL) {
62        LOGE("Can't find android/os/UEventObserver");
63        return -1;
64    }
65
66    return AndroidRuntime::registerNativeMethods(env,
67                "android/os/UEventObserver", gMethods, NELEM(gMethods));
68}
69
70}   // namespace android
71