From 5b187ec93192a17de1a9790ec0a5f20b2e076ef6 Mon Sep 17 00:00:00 2001 From: Devin Moore Date: Fri, 4 Nov 2022 19:06:50 +0000 Subject: [PATCH] Register new AIDL SensorManager For now we can register both HIDL and AIDL sensorservice, and the vendor can choose which one to use. Test: atest VtsHalSensorManagerTargetTest Bug: 205764765 Change-Id: I26ee176baea59f86281ae3a46ea580443064b5ef --- services/core/jni/Android.bp | 2 + .../jni/com_android_server_SystemServer.cpp | 96 ++++++++++++------- .../java/com/android/server/SystemServer.java | 13 ++- 3 files changed, 75 insertions(+), 36 deletions(-) diff --git a/services/core/jni/Android.bp b/services/core/jni/Android.bp index 301fca722adcc..93a4ebffc12c3 100644 --- a/services/core/jni/Android.bp +++ b/services/core/jni/Android.bp @@ -132,6 +132,7 @@ cc_defaults { "libschedulerservicehidl", "libsensorservice", "libsensorservicehidl", + "libsensorserviceaidl", "libgui", "libtimestats_atoms_proto", "libusbhost", @@ -180,6 +181,7 @@ cc_defaults { "android.hidl.token@1.0-utils", "android.frameworks.schedulerservice@1.0", "android.frameworks.sensorservice@1.0", + "android.frameworks.sensorservice-V1-ndk", "android.frameworks.stats@1.0", "android.frameworks.stats-V1-ndk", "android.system.suspend.control-V1-cpp", diff --git a/services/core/jni/com_android_server_SystemServer.cpp b/services/core/jni/com_android_server_SystemServer.cpp index b171a075578c4..be18f6419e157 100644 --- a/services/core/jni/com_android_server_SystemServer.cpp +++ b/services/core/jni/com_android_server_SystemServer.cpp @@ -14,35 +14,31 @@ * limitations under the License. */ -#include -#include - -#include -#include - -#include -#include - +#include #include #include #include #include +#include +#include +#include #include #include - +#include #include +#include +#include #include +#include #include #include #include - -#include -#include - -#include +#include #include #include -#include + +#include +#include using namespace std::chrono_literals; @@ -57,7 +53,9 @@ static void startStatsAidlService() { const std::string instance = std::string() + IStats::descriptor + "/default"; const binder_exception_t err = AServiceManager_addService(statsService->asBinder().get(), instance.c_str()); - LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register AIDL %s: %d", instance.c_str(), err); + if (err != EX_NONE) { + ALOGW("Cannot register AIDL %s: %d", instance.c_str(), err); + } } static void startStatsHidlService() { @@ -69,6 +67,42 @@ static void startStatsHidlService() { ALOGW_IF(err != android::OK, "Cannot register HIDL %s: %d", IStats::descriptor, err); } +static void startSensorManagerAidlService(JNIEnv* env) { + using ::aidl::android::frameworks::sensorservice::ISensorManager; + using ::android::frameworks::sensorservice::implementation::SensorManagerAidl; + + JavaVM* vm; + LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Cannot get Java VM"); + + std::shared_ptr sensorService = + ndk::SharedRefBase::make(vm); + const std::string instance = std::string() + ISensorManager::descriptor + "/default"; + const binder_exception_t err = + AServiceManager_addService(sensorService->asBinder().get(), instance.c_str()); + LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register AIDL %s: %d", instance.c_str(), err); +} + +static void startSensorManagerHidlService(JNIEnv* env) { + using ::android::frameworks::sensorservice::V1_0::ISensorManager; + using ::android::frameworks::sensorservice::V1_0::implementation::SensorManager; + using ::android::hardware::configureRpcThreadpool; + using ::android::hidl::manager::V1_0::IServiceManager; + + JavaVM* vm; + LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Cannot get Java VM"); + + android::sp sensorService = new SensorManager(vm); + if (IServiceManager::Transport::HWBINDER == + android::hardware::defaultServiceManager1_2()->getTransport(ISensorManager::descriptor, + "default")) { + android::status_t err = sensorService->registerAsService(); + LOG_ALWAYS_FATAL_IF(err != android::OK, "Cannot register %s: %d", + ISensorManager::descriptor, err); + } else { + ALOGW("%s is deprecated. Skipping registration.", ISensorManager::descriptor); + } +} + } // namespace namespace android { @@ -78,6 +112,12 @@ static void android_server_SystemServer_startIStatsService(JNIEnv* /* env */, jo startStatsAidlService(); } +static void android_server_SystemServer_startISensorManagerService(JNIEnv* env, + jobject /* clazz */) { + startSensorManagerHidlService(env); + startSensorManagerAidlService(env); +} + static void android_server_SystemServer_startMemtrackProxyService(JNIEnv* env, jobject /* clazz */) { using aidl::android::hardware::memtrack::MemtrackProxy; @@ -93,35 +133,19 @@ static void android_server_SystemServer_startMemtrackProxyService(JNIEnv* env, LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register %s: %d", memtrackProxyService, err); } -static void android_server_SystemServer_startHidlServices(JNIEnv* env, jobject /* clazz */) { +static void android_server_SystemServer_startHidlServices(JNIEnv* /* env */, jobject /* clazz */) { using ::android::frameworks::schedulerservice::V1_0::ISchedulingPolicyService; using ::android::frameworks::schedulerservice::V1_0::implementation::SchedulingPolicyService; - using ::android::frameworks::sensorservice::V1_0::ISensorManager; - using ::android::frameworks::sensorservice::V1_0::implementation::SensorManager; using ::android::hardware::configureRpcThreadpool; using ::android::hidl::manager::V1_0::IServiceManager; - status_t err; - configureRpcThreadpool(5, false /* callerWillJoin */); - JavaVM *vm; - LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Cannot get Java VM"); - - sp sensorService = new SensorManager(vm); - if (IServiceManager::Transport::HWBINDER == - hardware::defaultServiceManager1_2()->getTransport(ISensorManager::descriptor, "default")) { - err = sensorService->registerAsService(); - LOG_ALWAYS_FATAL_IF(err != OK, "Cannot register %s: %d", ISensorManager::descriptor, err); - } else { - ALOGW("%s is deprecated. Skipping registration.", ISensorManager::descriptor); - } - sp schedulingService = new SchedulingPolicyService(); if (IServiceManager::Transport::HWBINDER == hardware::defaultServiceManager1_2()->getTransport(ISchedulingPolicyService::descriptor, "default")) { - err = schedulingService->registerAsService("default"); + status_t err = schedulingService->registerAsService("default"); LOG_ALWAYS_FATAL_IF(err != OK, "Cannot register %s: %d", ISchedulingPolicyService::descriptor, err); } else { @@ -156,6 +180,8 @@ static void android_server_SystemServer_setIncrementalServiceSystemReady(JNIEnv* static const JNINativeMethod gMethods[] = { /* name, signature, funcPtr */ {"startIStatsService", "()V", (void*)android_server_SystemServer_startIStatsService}, + {"startISensorManagerService", "()V", + (void*)android_server_SystemServer_startISensorManagerService}, {"startMemtrackProxyService", "()V", (void*)android_server_SystemServer_startMemtrackProxyService}, {"startHidlServices", "()V", (void*)android_server_SystemServer_startHidlServices}, diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index c2a694423b0d5..a953bcbe54537 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -466,6 +466,7 @@ public final class SystemServer implements Dumpable { private final long mRuntimeStartUptime; private static final String START_HIDL_SERVICES = "StartHidlServices"; + private static final String START_SENSOR_MANAGER_SERVICE = "StartISensorManagerService"; private static final String START_BLOB_STORE_SERVICE = "startBlobStoreManagerService"; private static final String SYSPROP_START_COUNT = "sys.system_server.start_count"; @@ -484,6 +485,9 @@ public final class SystemServer implements Dumpable { /** Start the IStats services. This is a blocking call and can take time. */ private static native void startIStatsService(); + /** Start the ISensorManager service. This is a blocking call and can take time. */ + private static native void startISensorManagerService(); + /** * Start the memtrack proxy service. */ @@ -1597,9 +1601,16 @@ public final class SystemServer implements Dumpable { wm.onInitReady(); t.traceEnd(); - // Start receiving calls from HIDL services. Start in in a separate thread + // Start receiving calls from SensorManager services. Start in a separate thread // because it need to connect to SensorManager. This has to start // after PHASE_WAIT_FOR_SENSOR_SERVICE is done. + SystemServerInitThreadPool.submit(() -> { + TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog(); + traceLog.traceBegin(START_SENSOR_MANAGER_SERVICE); + startISensorManagerService(); + traceLog.traceEnd(); + }, START_SENSOR_MANAGER_SERVICE); + SystemServerInitThreadPool.submit(() -> { TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog(); traceLog.traceBegin(START_HIDL_SERVICES);