1 /*
2 * Copyright (C) 2019 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 <VehicleBus.h>
18
19 #include <android/binder_auto_utils.h>
20
21 #include <android-base/logging.h>
22
23 namespace aidl::android::hardware::automotive::vehicle {
24
VehicleBus()25 VehicleBus::VehicleBus() {
26 mDeathRecipient = ::ndk::ScopedAIBinder_DeathRecipient(
27 AIBinder_DeathRecipient_new(&VehicleBus::onBinderDied));
28 }
29
~VehicleBus()30 VehicleBus::~VehicleBus() {}
31
start()32 ::ndk::ScopedAStatus VehicleBus::start() {
33 return ::ndk::ScopedAStatus::ok();
34 }
35
setOnNewPropValuesCallback(const std::shared_ptr<IVehicleBusCallback> & callback)36 ::ndk::ScopedAStatus VehicleBus::setOnNewPropValuesCallback(
37 const std::shared_ptr<IVehicleBusCallback>& callback) {
38 std::lock_guard<std::mutex> g(mLock);
39
40 if (mVehicleBusCallback) {
41 return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
42 ERROR_INVALID_OPERATION, "Can't set callback twice!");
43
44 }
45
46 AIBinder_linkToDeath(callback->asBinder().get(), mDeathRecipient.get(),
47 static_cast<void*>(this));
48 mVehicleBusCallback = callback;
49 return ::ndk::ScopedAStatus::ok();
50 }
51
unsetOnNewPropValuesCallback(const std::shared_ptr<IVehicleBusCallback> & callback)52 ::ndk::ScopedAStatus VehicleBus::unsetOnNewPropValuesCallback(
53 const std::shared_ptr<IVehicleBusCallback>& callback) {
54 std::lock_guard<std::mutex> g(mLock);
55
56 if (mVehicleBusCallback != callback) {
57 return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
58 ERROR_INVALID_OPERATION, "Invalid callback argument");
59 }
60
61 AIBinder_unlinkToDeath(callback->asBinder().get(), mDeathRecipient.get(),
62 static_cast<void*>(this));
63 mVehicleBusCallback = nullptr;
64 return ::ndk::ScopedAStatus::ok();
65 }
66
sendPropertyEvent(const std::vector<VehiclePropValue> & propValues)67 void VehicleBus::sendPropertyEvent(
68 const std::vector<VehiclePropValue>& propValues) {
69 std::lock_guard<std::mutex> g(mLock);
70
71 if (mVehicleBusCallback == nullptr) {
72 LOG(ERROR) << "Callback isn't set";
73 return;
74 }
75 mVehicleBusCallback->onNewPropValues(propValues);
76 }
77
updateTimestamps(std::vector<VehiclePropValue> & propValues,uint64_t timestamp)78 void VehicleBus::updateTimestamps(std::vector<VehiclePropValue>& propValues,
79 uint64_t timestamp) {
80 for (auto&& pv : propValues) {
81 pv.timestamp = timestamp;
82 }
83 }
84
onBinderDied(void * cookie)85 void VehicleBus::onBinderDied(void* cookie) {
86 VehicleBus* server = reinterpret_cast<VehicleBus*>(cookie);
87 server->handleBinderDied();
88 }
89
handleBinderDied()90 void VehicleBus::handleBinderDied() {
91 std::lock_guard<std::mutex> g(mLock);
92 mVehicleBusCallback = nullptr;
93 LOG(ERROR) << "Received onBinderDied on registered VehicleBusCallback";
94 }
95
96 }; // namespace aidl::android::hardware::automotive::vehicle
97