1 /*
2 * Copyright 2017, 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 "wifi-jni"
18
19 #include <ctype.h>
20 #include <stdlib.h>
21 #include <sys/klog.h>
22
23 #include <log/log.h>
24 #include <nativehelper/JniConstants.h>
25 #include <nativehelper/ScopedBytes.h>
26 #include <nativehelper/ScopedUtfChars.h>
27 #include <jni.h>
28
29 #include "jni_helper.h"
30
31 namespace android {
32
33
android_net_wifi_readKernelLog(JNIEnv * env,jclass cls)34 static jbyteArray android_net_wifi_readKernelLog(JNIEnv *env, jclass cls) {
35 JNIHelper helper(env);
36 ALOGV("Reading kernel logs");
37
38 int size = klogctl(/* SYSLOG_ACTION_SIZE_BUFFER */ 10, 0, 0);
39 if (size < 1) {
40 ALOGD("no kernel logs");
41 return helper.newByteArray(0).detach();
42 }
43
44 char *buf = (char *)malloc(size);
45 if (buf == NULL) {
46 ALOGD("can't allocate temporary storage");
47 return helper.newByteArray(0).detach();
48 }
49
50 int read = klogctl(/* SYSLOG_ACTION_READ_ALL */ 3, buf, size);
51 if (read < 0) {
52 ALOGD("can't read logs - %d", read);
53 free(buf);
54 return helper.newByteArray(0).detach();
55 } else {
56 ALOGV("read %d bytes", read);
57 }
58
59 if (read != size) {
60 ALOGV("read %d bytes, expecting %d", read, size);
61 }
62
63 JNIObject<jbyteArray> result = helper.newByteArray(read);
64 if (result.isNull()) {
65 ALOGD("can't allocate array");
66 free(buf);
67 return result.detach();
68 }
69
70 helper.setByteArrayRegion(result, 0, read, (jbyte*)buf);
71 free(buf);
72 return result.detach();
73 }
74
75 // ----------------------------------------------------------------------------
76
77 /*
78 * JNI registration.
79 */
80 static JNINativeMethod gWifiMethods[] = {
81 /* name, signature, funcPtr */
82 {"readKernelLogNative", "()[B", (void*)android_net_wifi_readKernelLog},
83 };
84
85 /* User to register native functions */
86 extern "C"
Java_com_android_server_wifi_WifiNative_registerNatives(JNIEnv * env,jclass clazz)87 jint Java_com_android_server_wifi_WifiNative_registerNatives(JNIEnv* env, jclass clazz) {
88 // initialization needed for unit test APK
89 JniConstants::init(env);
90
91 return jniRegisterNativeMethods(env,
92 "com/android/server/wifi/WifiNative", gWifiMethods, NELEM(gWifiMethods));
93 }
94
95 }; // namespace android
96