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