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 #pragma once
18 
19 #include <aidl/device/generic/car/emulator/BnVehicleBus.h>
20 #include <aidl/device/generic/car/emulator/IVehicleBusCallback.h>
21 
22 #include <android-base/thread_annotations.h>
23 
24 namespace aidl::android::hardware::automotive::vehicle {
25 
26 class VehicleBus : public device::generic::car::emulator::BnVehicleBus {
27 public:
28     using IVehicleBusCallback = aidl::device::generic::car::emulator::IVehicleBusCallback;
29 
30     enum {
31         ERROR_INVALID_OPERATION = 1,
32     };
33 
34     VehicleBus();
35     virtual ~VehicleBus();
36 
37     virtual ::ndk::ScopedAStatus setOnNewPropValuesCallback(
38         const std::shared_ptr<IVehicleBusCallback>& callback);
39     virtual ::ndk::ScopedAStatus unsetOnNewPropValuesCallback(
40         const std::shared_ptr<IVehicleBusCallback>& callback);
41     virtual ::ndk::ScopedAStatus start();
42 
43 protected:
44     void sendPropertyEvent(const std::vector<VehiclePropValue>& propValues);
45     static void updateTimestamps(std::vector<VehiclePropValue>& propValues,
46                                  uint64_t timestamp);
47 
48   private:
49     std::mutex mLock;
50     std::shared_ptr<IVehicleBusCallback> mVehicleBusCallback GUARDED_BY(mLock);
51     ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
52 
53     void handleBinderDied();
54     static void onBinderDied(void* cookie);
55 
56 };
57 
58 };  // namespace aidl::android::hardware::automotive::vehicle
59