• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <jni.h>
25 #include <nativehelper/jni_macros.h>
26 #include <nativehelper/JNIHelp.h>
27 
28 #include "jni_helper.h"
29 
30 namespace android {
31 
32 
android_net_wifi_readKernelLogNative(JNIEnv * env,jclass cls)33 static jbyteArray android_net_wifi_readKernelLogNative(JNIEnv *env, jclass cls) {
34     JNIHelper helper(env);
35     ALOGV("Reading kernel logs");
36 
37     int size = klogctl(/* SYSLOG_ACTION_SIZE_BUFFER */ 10, 0, 0);
38     if (size < 1) {
39         ALOGD("no kernel logs");
40         return helper.newByteArray(0).detach();
41     }
42 
43     char *buf = (char *)malloc(size);
44     if (buf == NULL) {
45         ALOGD("can't allocate temporary storage");
46         return helper.newByteArray(0).detach();
47     }
48 
49     int read = klogctl(/* SYSLOG_ACTION_READ_ALL */ 3, buf, size);
50     if (read < 0) {
51         ALOGD("can't read logs - %d", read);
52         free(buf);
53         return helper.newByteArray(0).detach();
54     } else {
55         ALOGV("read %d bytes", read);
56     }
57 
58     if (read != size) {
59         ALOGV("read %d bytes, expecting %d", read, size);
60     }
61 
62     JNIObject<jbyteArray> result = helper.newByteArray(read);
63     if (result.isNull()) {
64         ALOGD("can't allocate array");
65         free(buf);
66         return result.detach();
67     }
68 
69     helper.setByteArrayRegion(result, 0, read, (jbyte*)buf);
70     free(buf);
71     return result.detach();
72 }
73 
74 // ----------------------------------------------------------------------------
75 
76 /*
77  * JNI registration.
78  */
79 static JNINativeMethod gWifiMethods[] = {
80     NATIVE_METHOD(android_net_wifi, readKernelLogNative, "()[B"),
81 };
82 
83 /* User to register native functions */
84 extern "C"
Java_com_android_server_wifi_WifiNative_registerNatives(JNIEnv * env,jclass clazz)85 jint Java_com_android_server_wifi_WifiNative_registerNatives(JNIEnv* env, jclass clazz) {
86     return jniRegisterNativeMethods(env,
87             "com/android/server/wifi/WifiNative", gWifiMethods, NELEM(gWifiMethods));
88 }
89 
90 }; // namespace android
91