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/ICarTelemetry.h>
20 #include <android-base/logging.h>
21 #include <android-base/stringprintf.h>
22 #include <android/binder_manager.h>
23 #include <utils/SystemClock.h>
24 
25 using ::aidl::android::frameworks::automotive::telemetry::CarData;
26 using ::aidl::android::frameworks::automotive::telemetry::ICarTelemetry;
27 using ::android::base::StringPrintf;
28 
main(int argc,char * argv[])29 int main(int argc, char* argv[]) {
30     const auto started_at_millis = android::elapsedRealtime();
31 
32     // The name of the service is described in
33     // https://source.android.com/devices/architecture/aidl/aidl-hals#instance-names
34     const std::string instance = StringPrintf("%s/default", ICarTelemetry::descriptor);
35     LOG(INFO) << "Obtaining: " << instance;
36     std::shared_ptr<ICarTelemetry> service = ICarTelemetry::fromBinder(
37             ndk::SpAIBinder(AServiceManager_getService(instance.c_str())));
38     if (!service) {
39         LOG(ERROR) << "ICarTelemetry service not found, may be still initializing?";
40         return 1;
41     }
42 
43     LOG(INFO) << "Building a CarData message, delta_since_start: "
44               << android::elapsedRealtime() - started_at_millis << " millis";
45 
46     // Build a CarData message
47     // TODO(b/174608802): set a correct data ID and content
48     CarData msg;
49     msg.id = 101;
50     msg.content = {1, 0, 1, 0};
51 
52     LOG(INFO) << "Sending the car data, delta_since_start: "
53               << android::elapsedRealtime() - started_at_millis << " millis";
54 
55     // Send the data
56     ndk::ScopedAStatus writeStatus = service->write({msg});
57 
58     if (!writeStatus.isOk()) {
59         LOG(WARNING) << "Failed to write to the service: " << writeStatus.getMessage();
60     }
61 
62     // Note: On a device the delta_since_start was between 1ms to 4ms
63     //      (service side was not fully implemented yet during the test).
64     LOG(INFO) << "Finished, delta_since_start: " << android::elapsedRealtime() - started_at_millis
65               << " millis";
66 
67     return 0;
68 }
69