• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #include <unistd.h>
17 
18 #include <android-base/logging.h>
19 #include <android/binder_process.h>
20 #include <hidl/HidlTransportSupport.h>
21 #include <utils/Errors.h>
22 #include <utils/Looper.h>
23 #include <utils/StrongPointer.h>
24 
25 #include "AudioControl.h"
26 #include "WatchdogClient.h"
27 #include "vsockinfo.h"
28 
29 // libhidl:
30 using android::hardware::configureRpcThreadpool;
31 using android::hardware::joinRpcThreadpool;
32 
33 // Generated HIDL files
34 using android::hardware::automotive::audiocontrol::V2_0::IAudioControl;
35 
36 using android::Looper;
37 using android::OK;
38 using android::hardware::automotive::audiocontrol::V2_0::implementation::AudioControl;
39 using android::hardware::automotive::audiocontrol::V2_0::implementation::WatchdogClient;
40 using android::hardware::automotive::utils::VsockConnectionInfo;
41 
42 // Main service entry point
main()43 int main() {
44     const auto si = VsockConnectionInfo::fromRoPropertyStore(
45             {
46                     "ro.boot.vendor.audiocontrol.server.cid",
47                     "ro.vendor.audiocontrol.server.cid",
48             },
49             {
50                     "ro.boot.vendor.audiocontrol.server.port",
51                     "ro.vendor.audiocontrol.server.port",
52             });
53 
54     if (!si) {
55         LOG(ERROR) << "failed to get server connection cid/port; audio control server disabled.";
56     } else {
57         LOG(INFO) << "Creating audio control server at " << si->str();
58     }
59 
60     // Create an instance of our service class
61     android::sp<AudioControl> service = new AudioControl(si ? si->str() : "");
62     configureRpcThreadpool(4, false /*callerWillJoin*/);
63 
64     if (service->registerAsService() != OK) {
65         LOG(ERROR) << "registerAsService failed";
66         return 1;
67     }
68 
69     // Start audio control server
70     service->ServerStart();
71 
72     // Setup a binder thread pool to be a car watchdog client.
73     ABinderProcess_setThreadPoolMaxThreadCount(1);
74     ABinderProcess_startThreadPool();
75     android::sp<Looper> looper(Looper::prepare(0 /* opts */));
76     std::shared_ptr<WatchdogClient> watchdogClient =
77             ndk::SharedRefBase::make<WatchdogClient>(looper, service.get());
78     if (!watchdogClient->initialize()) {
79         ALOGE("Failed to initialize car watchdog client");
80         return 1;
81     }
82 
83     while (true) {
84         looper->pollAll(-1 /* timeoutMillis */);
85     }
86     service->ServerJoin();
87 
88     // We don't ever actually expect to return, so return an error if we do get here
89     return 2;
90 }
91