1 /*
2  * Copyright (C) 2018 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 "CarServiceHelperService-JNI"
18 
19 //#define LOG_NDEBUG 0
20 
21 #include "jni.h"
22 
23 #include <nativehelper/JNIHelp.h>
24 #include <suspend/autosuspend.h>
25 #include <utils/Log.h>
26 
27 namespace android {
28 
29 // ----------------------------------------------------------------------------
30 
nativeForceSuspend(JNIEnv *,jclass,jint timeoutMs)31 static jint nativeForceSuspend(JNIEnv* /* env */, jclass /* clazz */, jint timeoutMs) {
32     jint ret = autosuspend_force_suspend(timeoutMs);
33     ALOGD("nativeForceSuspend returned %d", ret);
34     return ret;
35 }
36 
37 // ----------------------------------------------------------------------------
38 
39 static const JNINativeMethod gCarServiceHelperServiceMethods[] = {
40     /* name, signature, funcPtr */
41     { "nativeForceSuspend", "(I)I",
42             (void*) nativeForceSuspend },
43 };
44 
register_android_internal_car_CarServiceHelperService(JNIEnv * env)45 int register_android_internal_car_CarServiceHelperService(JNIEnv* env) {
46     int res = jniRegisterNativeMethods(env, "com/android/internal/car/CarServiceHelperService",
47             gCarServiceHelperServiceMethods, NELEM(gCarServiceHelperServiceMethods));
48     (void) res;  // Faked use when LOG_NDEBUG.
49     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
50     return 0;
51 }
52 
53 } /* namespace android */
54 
55 
56 using namespace android;
57 
JNI_OnLoad(JavaVM * vm,void *)58 extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
59 {
60     JNIEnv* env = NULL;
61     jint result = -1;
62 
63     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
64         ALOGE("GetEnv failed!");
65         return result;
66     }
67     ALOG_ASSERT(env, "Could not retrieve the env!");
68 
69     register_android_internal_car_CarServiceHelperService(env);
70     return JNI_VERSION_1_4;
71 }
72