blob: 2df74b05d97bd41a1bf5a97aaf42cba7a47e4660 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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"
Jeff Brown9cf36b72012-10-10 17:17:58 -070018//#define LOG_NDEBUG 0
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include "utils/Log.h"
21
22#include "hardware_legacy/uevent.h"
23#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070024#include <nativehelper/JNIHelp.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080025#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
Jeff Brown9cf36b72012-10-10 17:17:58 -070027#include <utils/Mutex.h>
28#include <utils/Vector.h>
29#include <utils/String8.h>
Steven Moreland2279b252017-07-19 09:50:45 -070030#include <nativehelper/ScopedUtfChars.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Jeff Brown9cf36b72012-10-10 17:17:58 -070032namespace android {
33
34static Mutex gMatchesMutex;
35static Vector<String8> gMatches;
36
37static void nativeSetup(JNIEnv *env, jclass clazz) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 if (!uevent_init()) {
39 jniThrowException(env, "java/lang/RuntimeException",
Jeff Brown9cf36b72012-10-10 17:17:58 -070040 "Unable to open socket for UEventObserver");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 }
42}
43
Jeff Brown9cf36b72012-10-10 17:17:58 -070044static bool isMatch(const char* buffer, size_t length) {
45 AutoMutex _l(gMatchesMutex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Jeff Brown9cf36b72012-10-10 17:17:58 -070047 for (size_t i = 0; i < gMatches.size(); i++) {
48 const String8& match = gMatches.itemAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Jeff Brown9cf36b72012-10-10 17:17:58 -070050 // Consider all zero-delimited fields of the buffer.
51 const char* field = buffer;
Jeff Brownc51cb892012-10-11 15:58:49 -070052 const char* end = buffer + length + 1;
Jeff Brown9cf36b72012-10-10 17:17:58 -070053 do {
54 if (strstr(field, match.string())) {
55 ALOGV("Matched uevent message with pattern: %s", match.string());
56 return true;
57 }
58 field += strlen(field) + 1;
59 } while (field != end);
60 }
61 return false;
62}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Jeff Brown9cf36b72012-10-10 17:17:58 -070064static 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 ALOGV("Received uevent message: %s", buffer);
75
76 if (isMatch(buffer, length)) {
77 // Assume the message is ASCII.
78 jchar message[length];
79 for (int i = 0; i < length; i++) {
80 message[i] = buffer[i];
81 }
82 return env->NewString(message, length);
83 }
84 }
85}
86
87static void nativeAddMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
88 ScopedUtfChars match(env, matchStr);
89
90 AutoMutex _l(gMatchesMutex);
91 gMatches.add(String8(match.c_str()));
92}
93
94static void nativeRemoveMatch(JNIEnv* env, jclass clazz, jstring matchStr) {
95 ScopedUtfChars match(env, matchStr);
96
97 AutoMutex _l(gMatchesMutex);
98 for (size_t i = 0; i < gMatches.size(); i++) {
99 if (gMatches.itemAt(i) == match.c_str()) {
100 gMatches.removeAt(i);
101 break; // only remove first occurrence
102 }
103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104}
105
Daniel Micay76f6a862015-09-19 17:31:01 -0400106static const JNINativeMethod gMethods[] = {
Jeff Brown9cf36b72012-10-10 17:17:58 -0700107 { "nativeSetup", "()V",
108 (void *)nativeSetup },
109 { "nativeWaitForNextEvent", "()Ljava/lang/String;",
110 (void *)nativeWaitForNextEvent },
111 { "nativeAddMatch", "(Ljava/lang/String;)V",
112 (void *)nativeAddMatch },
113 { "nativeRemoveMatch", "(Ljava/lang/String;)V",
114 (void *)nativeRemoveMatch },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115};
116
117
118int register_android_os_UEventObserver(JNIEnv *env)
119{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800120 FindClassOrDie(env, "android/os/UEventObserver");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800122 return RegisterMethodsOrDie(env, "android/os/UEventObserver", gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
125} // namespace android