1 /*
2  * Copyright (C) 2022 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 #ifdef LAZY_SERVICE
18 #define LOG_TAG "[email protected]"
19 #else
20 #define LOG_TAG "[email protected]"
21 #endif
22 
23 #include <aidl/android/hardware/camera/provider/ICameraProvider.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26 #include <apex_update_listener.h>
27 #include <cutils/properties.h>
28 #include <hidl/HidlTransportSupport.h>
29 #include <malloc.h>
30 #include <utils/AndroidThreads.h>
31 #include <utils/Errors.h>
32 
33 #include <cinttypes>
34 
35 #include "aidl_camera_build_version.h"
36 #include "aidl_camera_provider.h"
37 
38 using aidl::android::hardware::camera::provider::ICameraProvider;
39 using ::android::hardware::camera::provider::implementation::AidlCameraProvider;
40 
41 #ifdef LAZY_SERVICE
42 const bool kLazyService = true;
43 #else
44 const bool kLazyService = false;
45 #endif
46 
47 const std::string kProviderInstance = "/internal/0";
48 
main()49 int main() {
50   ALOGI("Google camera provider service is starting.");
51   mallopt(M_DECAY_TIME, 1);
52   android::hardware::configureRpcThreadpool(/*maxThreads=*/6,
53                                             /*callerWillJoin=*/true);
54 
55   // Don't depend on vndbinder setting up threads in case we stop using them
56   // some day
57   ABinderProcess_setThreadPoolMaxThreadCount(6);
58   ABinderProcess_startThreadPool();
59 
60 #ifdef __ANDROID_APEX__
61   int start_count = property_get_int32("vendor.camera.hal.start.count", 0);
62   property_set("vendor.camera.hal.start.count",
63                std::to_string(++start_count).c_str());
64   property_set("vendor.camera.hal.version",
65                std::to_string(kHalManifestBuildNumber).c_str());
66   property_set("vendor.camera.hal.build_id", kAndroidBuildId);
67   auto start_on_update =
68       ApexUpdateListener::Make("com.google.pixel.camera.hal", [](auto, auto) {
69         ALOGI("APEX version updated. starting.");
70         exit(0);
71       });
72   ALOGI(
73       "Using ApexUpdateListener: %p Start Count: %d Current Version: %s "
74       "(%" PRId64 ")",
75       start_on_update.get(), start_count, kAndroidBuildId,
76       kHalManifestBuildNumber);
77 #else
78   ALOGI("Not using ApexUpdateListener since not running in an apex.");
79 #endif
80 
81   std::shared_ptr<ICameraProvider> camera_provider =
82       AidlCameraProvider::Create();
83   if (camera_provider == nullptr) {
84     return android::NO_INIT;
85   }
86   std::string instance =
87       std::string() + AidlCameraProvider::descriptor + kProviderInstance;
88   if (kLazyService) {
89     if (AServiceManager_registerLazyService(camera_provider->asBinder().get(),
90                                             instance.c_str()) != STATUS_OK) {
91       ALOGE("Cannot register AIDL Google camera provider lazy service");
92       return android::NO_INIT;
93     }
94   } else {
95     if (AServiceManager_addService(camera_provider->asBinder().get(),
96                                    instance.c_str()) != STATUS_OK) {
97       ALOGE("Cannot register AIDL Google camera provider service");
98       return android::NO_INIT;
99     }
100   }
101   androidSetThreadName("google.camera.provider");
102   ABinderProcess_joinThreadPool();
103 
104   // In normal operation, the threadpool should never return.
105   return EXIT_FAILURE;
106 }
107