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_CONTROLLER_H 18 #define ANDROID_OS_VIBRATOR_MANAGER_HAL_CONTROLLER_H 19 20 #include <android/hardware/vibrator/IVibratorManager.h> 21 #include <vibratorservice/VibratorHalController.h> 22 #include <vibratorservice/VibratorManagerHalWrapper.h> 23 #include <unordered_map> 24 25 namespace android { 26 27 namespace vibrator { 28 29 std::shared_ptr<ManagerHalWrapper> connectManagerHal(std::shared_ptr<CallbackScheduler> scheduler); 30 31 // Controller for VibratorManager HAL handle. 32 class ManagerHalController : public ManagerHalWrapper { 33 public: 34 using Connector = 35 std::function<std::shared_ptr<ManagerHalWrapper>(std::shared_ptr<CallbackScheduler>)>; 36 ManagerHalController()37 ManagerHalController() 38 : ManagerHalController(std::make_shared<CallbackScheduler>(), &connectManagerHal) {} ManagerHalController(std::shared_ptr<CallbackScheduler> callbackScheduler,Connector connector)39 ManagerHalController(std::shared_ptr<CallbackScheduler> callbackScheduler, Connector connector) 40 : mConnector(connector), mCallbackScheduler(callbackScheduler), mConnectedHal(nullptr) {} 41 virtual ~ManagerHalController() = default; 42 43 /* Connects to the HAL service, possibly waiting for the registered service to 44 * become available. This will automatically be called at the first API usage if it was not 45 * manually called beforehand. Calling this manually during the setup phase can avoid slowing 46 * the first API call later on. This will fallback to a legacy manager implementation if the 47 * service is not available. 48 */ 49 virtual void init(); 50 51 /* reloads HAL service instance without waiting. This relies on the HAL found by init() 52 * to rapidly reconnect to the specific HAL service, or defers to init() if it was never called. 53 */ 54 void tryReconnect() override final; 55 56 HalResult<void> ping() override final; 57 58 HalResult<ManagerCapabilities> getCapabilities() override final; 59 HalResult<std::vector<int32_t>> getVibratorIds() override final; 60 HalResult<std::shared_ptr<HalController>> getVibrator(int32_t id) override final; 61 62 HalResult<void> prepareSynced(const std::vector<int32_t>& ids) override final; 63 HalResult<void> triggerSynced(const std::function<void()>& completionCallback) override final; 64 HalResult<void> cancelSynced() override final; 65 66 private: 67 Connector mConnector; 68 std::shared_ptr<CallbackScheduler> mCallbackScheduler; 69 std::mutex mConnectedHalMutex; 70 // Shared pointer to allow local copies to be used by different threads. 71 std::shared_ptr<ManagerHalWrapper> mConnectedHal GUARDED_BY(mConnectedHalMutex); 72 73 template <typename T> 74 HalResult<T> processHalResult(HalResult<T> result, const char* functionName); 75 76 template <typename T> 77 using hal_fn = std::function<HalResult<T>(std::shared_ptr<ManagerHalWrapper>)>; 78 79 template <typename T> 80 HalResult<T> apply(hal_fn<T>& halFn, const char* functionName); 81 }; 82 83 }; // namespace vibrator 84 85 }; // namespace android 86 87 #endif // ANDROID_OS_VIBRATOR_MANAGER_HAL_CONTROLLER_H 88