• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #ifndef ANDROID_OS_VIBRATOR_MANAGER_HAL_WRAPPER_H
18 #define ANDROID_OS_VIBRATOR_MANAGER_HAL_WRAPPER_H
19 
20 #include <android/hardware/vibrator/IVibratorManager.h>
21 #include <vibratorservice/VibratorHalController.h>
22 #include <unordered_map>
23 
24 namespace android {
25 
26 namespace vibrator {
27 
28 // VibratorManager HAL capabilities.
29 enum class ManagerCapabilities : int32_t {
30     NONE = 0,
31     SYNC = hardware::vibrator::IVibratorManager::CAP_SYNC,
32     PREPARE_ON = hardware::vibrator::IVibratorManager::CAP_PREPARE_ON,
33     PREPARE_PERFORM = hardware::vibrator::IVibratorManager::CAP_PREPARE_PERFORM,
34     PREPARE_COMPOSE = hardware::vibrator::IVibratorManager::CAP_PREPARE_COMPOSE,
35     MIXED_TRIGGER_ON = hardware::vibrator::IVibratorManager::IVibratorManager::CAP_MIXED_TRIGGER_ON,
36     MIXED_TRIGGER_PERFORM = hardware::vibrator::IVibratorManager::CAP_MIXED_TRIGGER_PERFORM,
37     MIXED_TRIGGER_COMPOSE = hardware::vibrator::IVibratorManager::CAP_MIXED_TRIGGER_COMPOSE,
38     TRIGGER_CALLBACK = hardware::vibrator::IVibratorManager::CAP_TRIGGER_CALLBACK
39 };
40 
41 inline ManagerCapabilities operator|(ManagerCapabilities lhs, ManagerCapabilities rhs) {
42     using underlying = typename std::underlying_type<ManagerCapabilities>::type;
43     return static_cast<ManagerCapabilities>(static_cast<underlying>(lhs) |
44                                             static_cast<underlying>(rhs));
45 }
46 
47 inline ManagerCapabilities& operator|=(ManagerCapabilities& lhs, ManagerCapabilities rhs) {
48     return lhs = lhs | rhs;
49 }
50 
51 inline ManagerCapabilities operator&(ManagerCapabilities lhs, ManagerCapabilities rhs) {
52     using underlying = typename std::underlying_type<ManagerCapabilities>::type;
53     return static_cast<ManagerCapabilities>(static_cast<underlying>(lhs) &
54                                             static_cast<underlying>(rhs));
55 }
56 
57 inline ManagerCapabilities& operator&=(ManagerCapabilities& lhs, ManagerCapabilities rhs) {
58     return lhs = lhs & rhs;
59 }
60 
61 // Wrapper for VibratorManager HAL handlers.
62 class ManagerHalWrapper {
63 public:
64     ManagerHalWrapper() = default;
65     virtual ~ManagerHalWrapper() = default;
66 
67     virtual HalResult<void> ping() = 0;
68 
69     /* reloads wrapped HAL service instance without waiting. This can be used to reconnect when the
70      * service restarts, to rapidly retry after a failure.
71      */
72     virtual void tryReconnect() = 0;
73 
74     virtual HalResult<ManagerCapabilities> getCapabilities() = 0;
75     virtual HalResult<std::vector<int32_t>> getVibratorIds() = 0;
76     virtual HalResult<std::shared_ptr<HalController>> getVibrator(int32_t id) = 0;
77 
78     virtual HalResult<void> prepareSynced(const std::vector<int32_t>& ids) = 0;
79     virtual HalResult<void> triggerSynced(const std::function<void()>& completionCallback) = 0;
80     virtual HalResult<void> cancelSynced() = 0;
81 };
82 
83 // Wrapper for the VibratorManager over single Vibrator HAL.
84 class LegacyManagerHalWrapper : public ManagerHalWrapper {
85 public:
LegacyManagerHalWrapper()86     LegacyManagerHalWrapper() : LegacyManagerHalWrapper(std::make_shared<HalController>()) {}
LegacyManagerHalWrapper(std::shared_ptr<HalController> controller)87     explicit LegacyManagerHalWrapper(std::shared_ptr<HalController> controller)
88           : mController(std::move(controller)) {}
89     virtual ~LegacyManagerHalWrapper() = default;
90 
91     HalResult<void> ping() override final;
92     void tryReconnect() override final;
93 
94     HalResult<ManagerCapabilities> getCapabilities() override final;
95     HalResult<std::vector<int32_t>> getVibratorIds() override final;
96     HalResult<std::shared_ptr<HalController>> getVibrator(int32_t id) override final;
97 
98     HalResult<void> prepareSynced(const std::vector<int32_t>& ids) override final;
99     HalResult<void> triggerSynced(const std::function<void()>& completionCallback) override final;
100     HalResult<void> cancelSynced() override final;
101 
102 private:
103     const std::shared_ptr<HalController> mController;
104 };
105 
106 // Wrapper for the AIDL VibratorManager HAL.
107 class AidlManagerHalWrapper : public ManagerHalWrapper {
108 public:
AidlManagerHalWrapper(std::shared_ptr<CallbackScheduler> callbackScheduler,sp<hardware::vibrator::IVibratorManager> handle)109     explicit AidlManagerHalWrapper(std::shared_ptr<CallbackScheduler> callbackScheduler,
110                                    sp<hardware::vibrator::IVibratorManager> handle)
111           : mHandle(std::move(handle)), mCallbackScheduler(callbackScheduler) {}
112     virtual ~AidlManagerHalWrapper() = default;
113 
114     HalResult<void> ping() override final;
115     void tryReconnect() override final;
116 
117     HalResult<ManagerCapabilities> getCapabilities() override final;
118     HalResult<std::vector<int32_t>> getVibratorIds() override final;
119     HalResult<std::shared_ptr<HalController>> getVibrator(int32_t id) override final;
120 
121     HalResult<void> prepareSynced(const std::vector<int32_t>& ids) override final;
122     HalResult<void> triggerSynced(const std::function<void()>& completionCallback) override final;
123     HalResult<void> cancelSynced() override final;
124 
125 private:
126     std::mutex mHandleMutex;
127     std::mutex mCapabilitiesMutex;
128     std::mutex mVibratorsMutex;
129     sp<hardware::vibrator::IVibratorManager> mHandle GUARDED_BY(mHandleMutex);
130     std::optional<ManagerCapabilities> mCapabilities GUARDED_BY(mCapabilitiesMutex);
131     std::optional<std::vector<int32_t>> mVibratorIds GUARDED_BY(mVibratorsMutex);
132     std::unordered_map<int32_t, std::shared_ptr<HalController>> mVibrators
133             GUARDED_BY(mVibratorsMutex);
134     std::shared_ptr<CallbackScheduler> mCallbackScheduler;
135 
136     sp<hardware::vibrator::IVibratorManager> getHal();
137     std::shared_ptr<HalWrapper> connectToVibrator(int32_t vibratorId,
138                                                   std::shared_ptr<CallbackScheduler> scheduler);
139 };
140 
141 }; // namespace vibrator
142 
143 }; // namespace android
144 
145 #endif // ANDROID_OS_VIBRATOR_MANAGER_HAL_WRAPPER_H
146