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 //#define LOG_NDEBUG 0
19
20 #include "utils/Log.h"
21
22 #include "hardware_legacy/uevent.h"
23 #include "jni.h"
24 #include <nativehelper/JNIHelp.h>
25 #include "core_jni_helpers.h"
26
27 #include <utils/Mutex.h>
28 #include <utils/Vector.h>
29 #include <utils/String8.h>
30 #include <nativehelper/ScopedUtfChars.h>
31
32 namespace android {
33
34 static Mutex gMatchesMutex;
35 static Vector<String8> gMatches;
36
nativeSetup(JNIEnv * env,jclass clazz)37 static void nativeSetup(JNIEnv *env, jclass clazz) {
38 if (!uevent_init()) {
39 jniThrowException(env, "java/lang/RuntimeException",
40 "Unable to open socket for UEventObserver");
41 }
42 }
43
isMatch(const char * buffer,size_t length)44 static bool isMatch(const char* buffer, size_t length) {
45 AutoMutex _l(gMatchesMutex);
46
47 for (size_t i = 0; i < gMatches.size(); i++) {
48 const String8& match = gMatches.itemAt(i);
49
50 // Consider all zero-delimited fields of the buffer.
51 const char* field = buffer;
52 const char* end = buffer + length + 1;
53 do {
54 if (strstr(field, match.c_str())) {
55 ALOGV("Matched uevent message with pattern: %s", match.c_str());
56 return true;
57 }
58 field += strlen(field) + 1;
59 } while (field != end);
60 }
61 return false;
62 }
63
nativeWaitForNextEvent(JNIEnv * env,jclass clazz)64 static jstring nativeWaitForNextEvent(JNIEnv *env, jclass clazz) {
65 char buffer[1024];
66
67 for (;;) {
68 int length = uevent_next_event(buffer, sizeof(buffer) - 1);
69 if (length <= 0) {
70 return NULL;
71 }
72 buffer[length] = '\0';
73
74 IF_ALOGV() {
75 std::string message(buffer, length);
76 std::replace(message.begin(), message.end(), '\0', ' ');
77 ALOGV("Received uevent message: %s", message.c_str());
78 }
79
80 if (isMatch(buffer, length)) {
81 // Assume the message is ASCII.
82 jchar message[length];
83 for (int i = 0; i < length; i++) {
84 message[i] = buffer[i];
85 }
86 return env->NewString(message, length);
87 }
88 }
89 }
90
nativeAddMatch(JNIEnv * env,jclass clazz,jstring matchStr)91 static void nativeAddMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
92 ScopedUtfChars match(env, matchStr);
93
94 AutoMutex _l(gMatchesMutex);
95 gMatches.add(String8(match.c_str()));
96 }
97
nativeRemoveMatch(JNIEnv * env,jclass clazz,jstring matchStr)98 static void nativeRemoveMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
99 ScopedUtfChars match(env, matchStr);
100
101 AutoMutex _l(gMatchesMutex);
102 for (size_t i = 0; i < gMatches.size(); i++) {
103 if (gMatches.itemAt(i) == match.c_str()) {
104 gMatches.removeAt(i);
105 break; // only remove first occurrence
106 }
107 }
108 }
109
110 static const JNINativeMethod gMethods[] = {
111 { "nativeSetup", "()V",
112 (void *)nativeSetup },
113 { "nativeWaitForNextEvent", "()Ljava/lang/String;",
114 (void *)nativeWaitForNextEvent },
115 { "nativeAddMatch", "(Ljava/lang/String;)V",
116 (void *)nativeAddMatch },
117 { "nativeRemoveMatch", "(Ljava/lang/String;)V",
118 (void *)nativeRemoveMatch },
119 };
120
121
register_android_os_UEventObserver(JNIEnv * env)122 int register_android_os_UEventObserver(JNIEnv *env)
123 {
124 FindClassOrDie(env, "android/os/UEventObserver");
125
126 return RegisterMethodsOrDie(env, "android/os/UEventObserver", gMethods, NELEM(gMethods));
127 }
128
129 } // namespace android
130