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 #define LOG_TAG "cartelemetryd_sample"
18
19 #include <aidl/android/frameworks/automotive/telemetry/BnCarTelemetryCallback.h>
20 #include <aidl/android/frameworks/automotive/telemetry/ICarTelemetry.h>
21 #include <android-base/stringprintf.h>
22 #include <android/binder_manager.h>
23 #include <utils/SystemClock.h>
24
25 #include <getopt.h>
26 #include <stdlib.h>
27 #include <sysexits.h>
28 #include <unistd.h>
29
30 #include <cstdint>
31 #include <cstdio>
32 #include <iostream>
33 #include <memory>
34 #include <vector>
35
36 namespace {
37
38 using ::aidl::android::frameworks::automotive::telemetry::CallbackConfig;
39 using ::aidl::android::frameworks::automotive::telemetry::CarData;
40 using ::aidl::android::frameworks::automotive::telemetry::ICarTelemetry;
41 using ::android::base::StringPrintf;
42
43 constexpr int32_t GET_SERVICE_TIMEOUT_S = 5;
44 constexpr int32_t GET_SERVICE_SLEEP_INTERVAL_MS = 100;
45
46 } // namespace
47
48 class CarTelemetryCallbackImpl :
49 public aidl::android::frameworks::automotive::telemetry::BnCarTelemetryCallback {
50 public:
onChange(const std::vector<int32_t> & carDataIds)51 ndk::ScopedAStatus onChange(const std::vector<int32_t>& carDataIds) {
52 for (int32_t id : carDataIds) {
53 std::cout << "CarTelemetryCallbackImpl: CarData ID=" << id << " is active";
54 }
55 return ndk::ScopedAStatus::ok();
56 }
57 };
58
printHelp()59 void printHelp() {
60 std::cerr << "Usage: --batch-size NUM --interval-micros MICROS --cardata-size LEN" << std::endl;
61 std::cerr
62 << " Sends a batch of NUM car data of size LEN each with MICROS interval between them"
63 << std::endl;
64 }
65
main(int argc,char * argv[])66 int main(int argc, char* argv[]) {
67 struct option options[] = {
68 {"batch-size", required_argument, nullptr, 'c'},
69 {"interval-micros", required_argument, nullptr, 'i'},
70 {"cardata-size", required_argument, nullptr, 's'},
71 {nullptr, 0, nullptr, 0},
72 };
73 int opt = 0;
74 int batchSize = 0;
75 int intervalInMicros = 0;
76 int cardataSize = 0;
77 int option_index = -1;
78 while ((opt = getopt_long_only(argc, argv, "", options, &option_index)) != -1) {
79 bool argError = false;
80 switch (opt) {
81 case 'c':
82 argError = sscanf(optarg, "%d", &batchSize) != 1;
83 break;
84 case 'i':
85 argError = sscanf(optarg, "%d", &intervalInMicros) != 1;
86 break;
87 case 's':
88 argError = sscanf(optarg, "%d", &cardataSize) != 1;
89 break;
90 // Unknown argument
91 case '?':
92 default:
93 printHelp();
94 return EX_USAGE;
95 }
96 if (argError) {
97 std::cerr << "Invalid argument for " << options[option_index].name << " option"
98 << std::endl;
99 printHelp();
100 return EX_USAGE;
101 }
102 }
103
104 if (batchSize == 0) {
105 std::cerr << "Required argument --batch-size was not specified" << std::endl;
106 printHelp();
107 return EX_USAGE;
108 }
109
110 if (intervalInMicros == 0) {
111 std::cerr << "Required argument --interval-micros was not specified" << std::endl;
112 printHelp();
113 return EX_USAGE;
114 }
115
116 if (cardataSize == 0) {
117 std::cerr << "Required argument --cardata-size was not specified" << std::endl;
118 printHelp();
119 return EX_USAGE;
120 }
121
122 // The name of the service is described in
123 // https://source.android.com/devices/architecture/aidl/aidl-hals#instance-names
124 const std::string instance = StringPrintf("%s/default", ICarTelemetry::descriptor);
125 std::cout << "Obtaining: " << instance << std::endl;
126 std::shared_ptr<ICarTelemetry> service = nullptr;
127 const auto startTime = std::chrono::steady_clock::now();
128 while (std::chrono::steady_clock::now() - startTime <
129 std::chrono::seconds(GET_SERVICE_TIMEOUT_S)) {
130 service = ICarTelemetry::fromBinder(
131 ndk::SpAIBinder(AServiceManager_checkService(instance.c_str())));
132 if (!service) {
133 break;
134 }
135 usleep(GET_SERVICE_SLEEP_INTERVAL_MS * 1000);
136 }
137 if (!service) {
138 std::cerr << "ICarTelemetry service not found, may be still initializing?" << std::endl;
139 return EX_UNAVAILABLE;
140 }
141
142 // Add a ICarTelemetryCallback and listen for changes in CarData IDs 1, 2, and 3
143 std::shared_ptr<CarTelemetryCallbackImpl> callback =
144 ndk::SharedRefBase::make<CarTelemetryCallbackImpl>();
145 CallbackConfig config;
146 std::cout << "Adding a CarTelemetryCallback" << std::endl;
147 ndk::ScopedAStatus addStatus = service->addCallback(config, callback);
148 if (!addStatus.isOk()) {
149 std::cerr << "Failed to add CarTelemetryCallback: " << addStatus.getMessage() << std::endl;
150 }
151
152 const int64_t batchStartTime = android::elapsedRealtime();
153 std::cout << "Started sending the batch at " << batchStartTime << " millis since boot"
154 << std::endl;
155
156 for (int i = 0; i < batchSize; i++) {
157 // Build a CarData message
158 CarData msg;
159 msg.id = 1;
160 msg.content = std::vector<uint8_t>(cardataSize);
161
162 // Send the data
163 ndk::ScopedAStatus writeStatus = service->write({msg});
164
165 if (!writeStatus.isOk()) {
166 std::cerr << "Failed to write to the service: " << writeStatus.getMessage()
167 << std::endl;
168 }
169
170 usleep(intervalInMicros);
171 }
172 const int64_t batchFinishTime = android::elapsedRealtime();
173 std::cout << "Finished sending the batch at " << batchFinishTime << " millis since boot"
174 << std::endl;
175 std::cout << "Took " << batchFinishTime - batchStartTime << " millis to send a batch of "
176 << batchSize << " carData, each with payload of " << cardataSize << " bytes"
177 << std::endl;
178
179 // Remove the ICarTelemetryCallback to prevent a dead reference
180 std::cout << "Removing a CarTelemetryCallback" << std::endl;
181 ndk::ScopedAStatus removeStatus = service->removeCallback(callback);
182 if (!removeStatus.isOk()) {
183 std::cerr << "Failed to remove CarTelemetryCallback: " << removeStatus.getMessage()
184 << std::endl;
185 }
186
187 return EX_OK;
188 }
189