1 /*
2 * Copyright (c) 2021, 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 #include "CarTelemetryImpl.h"
18 #include "CarTelemetryInternalImpl.h"
19 #include "LooperWrapper.h"
20 #include "TelemetryServer.h"
21
22 #include <android-base/chrono_utils.h>
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android/binder_interface_utils.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <utils/Looper.h>
29
30 #include <thread> // NOLINT(build/c++11)
31
32 using ::android::automotive::telemetry::CarTelemetryImpl;
33 using ::android::automotive::telemetry::CarTelemetryInternalImpl;
34 using ::android::automotive::telemetry::LooperWrapper;
35 using ::android::automotive::telemetry::TelemetryServer;
36
37 // TODO(b/174608802): handle SIGQUIT/SIGTERM
38
39 constexpr const char kCarTelemetryServiceName[] =
40 "android.frameworks.automotive.telemetry.ICarTelemetry/default";
41 constexpr const char kCarTelemetryInternalServiceName[] =
42 "android.automotive.telemetry.internal.ICarTelemetryInternal/default";
43
44 // The min delay between each ICarDataListener.onCarDataReceived() calls. It's needed to avoid
45 // too frequently making binder transactions.
46 // Binder has <1MS latency for 10KB data.
47 // TODO(b/186477983): improve sending car data after implementing CarTelemetryService.
48 // TODO(b/183444070): make it configurable using sysprop
49 constexpr const std::chrono::nanoseconds kPushCarDataDelayNs = 10ms;
50
51 // TODO(b/183444070): make it configurable using sysprop
52 // CarData count limit in the RingBuffer. In worst case it will use kMaxBufferSize * 10Kb memory,
53 // which is ~ 1MB.
54 const int kMaxBufferSize = 100;
55
main(void)56 int main(void) {
57 LOG(INFO) << "Starting cartelemetryd";
58
59 LooperWrapper looper(android::Looper::prepare(/* opts= */ 0));
60 TelemetryServer server(&looper, kPushCarDataDelayNs, kMaxBufferSize);
61 std::shared_ptr<CarTelemetryImpl> telemetry =
62 ndk::SharedRefBase::make<CarTelemetryImpl>(&server);
63 std::shared_ptr<CarTelemetryInternalImpl> telemetryInternal =
64 ndk::SharedRefBase::make<CarTelemetryInternalImpl>(&server);
65
66 // Wait for the service manager before starting ICarTelemetry service.
67 android::base::WaitForProperty("init.svc.servicemanager", "running");
68
69 LOG(VERBOSE) << "Registering " << kCarTelemetryServiceName;
70 binder_exception_t exception =
71 ::AServiceManager_addService(telemetry->asBinder().get(), kCarTelemetryServiceName);
72 if (exception != ::EX_NONE) {
73 LOG(FATAL) << "Unable to register " << kCarTelemetryServiceName
74 << ", exception=" << exception;
75 }
76
77 LOG(VERBOSE) << "Registering " << kCarTelemetryInternalServiceName;
78 exception = ::AServiceManager_addService(telemetryInternal->asBinder().get(),
79 kCarTelemetryInternalServiceName);
80 if (exception != ::EX_NONE) {
81 LOG(FATAL) << "Unable to register " << kCarTelemetryInternalServiceName
82 << ", exception=" << exception;
83 }
84
85 LOG(VERBOSE) << "Services are registered, starting thread pool";
86 ::ABinderProcess_startThreadPool(); // Starts the default 15 binder threads.
87
88 LOG(VERBOSE) << "Running the server.";
89 // Loop forever -- pushing data to ICarDataListener runs on this thread, and the binder calls
90 // remain responsive in their pool of one thread.
91 while (true) {
92 looper.pollAll(/* timeoutMillis= */ -1);
93 }
94 return 1; // never reaches
95 }
96