The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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_legacy/uevent.h" |
| 21 | #include "jni.h" |
| 22 | #include "JNIHelp.h" |
| 23 | #include "android_runtime/AndroidRuntime.h" |
| 24 | |
| 25 | namespace android |
| 26 | { |
| 27 | |
| 28 | static void |
| 29 | android_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 | |
| 37 | static int |
| 38 | android_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 | |
| 50 | static 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 | |
| 56 | int 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 |