1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MISCDEVICE_SERVICE_H 17 #define MISCDEVICE_SERVICE_H 18 19 #include "accesstoken_kit.h" 20 #include "common_event_manager.h" 21 #include "system_ability.h" 22 23 #include "light_hdi_connection.h" 24 #include "miscdevice_common.h" 25 #include "miscdevice_common_event_subscriber.h" 26 #include "miscdevice_delayed_sp_singleton.h" 27 #include "miscdevice_dump.h" 28 #include "miscdevice_service_stub.h" 29 #include "vibrator_thread.h" 30 31 namespace OHOS { 32 namespace Sensors { 33 using namespace Security::AccessToken; 34 enum class MiscdeviceServiceState { 35 STATE_STOPPED, 36 STATE_RUNNING, 37 }; 38 39 struct VibratorControlInfo { 40 int motorCount; 41 std::unordered_map<int, std::shared_ptr<VibratorThread>> vibratorThreads; 42 VibratorControlInfoVibratorControlInfo43 VibratorControlInfo(const std::vector<int>& vibratorIds) : motorCount(static_cast<int>(vibratorIds.size())) 44 { 45 for (int motorId : vibratorIds) { 46 auto it = vibratorThreads.find(motorId); 47 if (it == vibratorThreads.end()) { 48 vibratorThreads[motorId] = std::make_shared<VibratorThread>(); 49 } 50 } 51 } 52 GetVibratorThreadVibratorControlInfo53 std::shared_ptr<VibratorThread> GetVibratorThread(int motorId) const 54 { 55 auto it = vibratorThreads.find(motorId); 56 if (it != vibratorThreads.end()) { 57 return it->second; 58 } 59 return nullptr; 60 } 61 }; 62 63 struct VibratorAllInfos { 64 std::vector<VibratorInfoIPC> baseInfo; 65 VibratorControlInfo controlInfo; 66 VibratorCapacity capacityInfo; 67 std::vector<HdfWaveInformation> waveInfo; VibratorAllInfosVibratorAllInfos68 VibratorAllInfos(const std::vector<int>& vibratorIds) : controlInfo(vibratorIds) {} 69 }; 70 71 class MiscdeviceService : public SystemAbility, public MiscdeviceServiceStub { 72 DECLARE_SYSTEM_ABILITY(MiscdeviceService) 73 MISCDEVICE_DECLARE_DELAYED_SP_SINGLETON(MiscdeviceService); 74 75 public: 76 void OnDump() override; 77 void OnStart() override; 78 void OnStop() override; 79 void OnStartFuzz(); 80 bool IsValid(int32_t lightId); 81 bool IsLightAnimationValid(const LightAnimationIPC &animation); 82 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 83 void ProcessDeathObserver(const wptr<IRemoteObject> &object); 84 virtual int32_t Vibrate(const VibratorIdentifierIPC& identifier, int32_t timeOut, 85 int32_t usage, bool systemUsage) override; 86 virtual int32_t PlayVibratorEffect(const VibratorIdentifierIPC& identifier, const std::string &effect, 87 int32_t loopCount, int32_t usage, bool systemUsage) override; 88 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM 89 virtual int32_t PlayVibratorCustom(const VibratorIdentifierIPC& identifier, int32_t fd, int64_t offset, 90 int64_t length, const CustomHapticInfoIPC& customHapticInfoIPC) override; 91 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM 92 virtual int32_t StopVibrator(const VibratorIdentifierIPC& identifier) override; 93 virtual int32_t StopVibratorByMode(const VibratorIdentifierIPC& identifier, const std::string &mode) override; 94 virtual int32_t IsSupportEffect(const VibratorIdentifierIPC& identifier, const std::string &effect, 95 bool &state) override; 96 virtual int32_t GetLightList(std::vector<LightInfoIPC> &lightInfoIpcList) override; 97 virtual int32_t TurnOn(int32_t lightId, int32_t singleColor, const LightAnimationIPC &animation) override; 98 virtual int32_t TurnOff(int32_t lightId) override; 99 virtual int32_t PlayPattern(const VibratorIdentifierIPC& identifier, const VibratePattern &pattern, 100 const CustomHapticInfoIPC& customHapticInfoIPC) override; 101 102 virtual int32_t PlayPackageBySessionId(const VibratorIdentifierIPC &identifier, const VibratePackageIPC &package, 103 const CustomHapticInfoIPC &customHapticInfoIPC) override; 104 virtual int32_t StopVibrateBySessionId(const VibratorIdentifierIPC &identifier, uint32_t sessionId) override; 105 virtual int32_t GetDelayTime(const VibratorIdentifierIPC& identifier, int32_t &delayTime) override; 106 virtual int32_t TransferClientRemoteObject(const sptr<IRemoteObject> &vibratorServiceClient) override; 107 virtual int32_t PlayPrimitiveEffect(const VibratorIdentifierIPC& identifier, const std::string &effect, 108 const PrimitiveEffectIPC& primitiveEffectIPC) override; 109 virtual int32_t GetVibratorCapacity(const VibratorIdentifierIPC& identifier, VibratorCapacity &capacity) override; 110 virtual int32_t GetVibratorList(const VibratorIdentifierIPC& identifier, 111 std::vector<VibratorInfoIPC>& vibratorInfoIPC) override; 112 virtual int32_t GetEffectInfo(const VibratorIdentifierIPC& identifier, const std::string& effectType, 113 EffectInfoIPC& effectInfoIPC) override; 114 virtual int32_t SubscribeVibratorPlugInfo(const sptr<IRemoteObject> &vibratorServiceClient) override; 115 116 private: 117 DISALLOW_COPY_AND_MOVE(MiscdeviceService); 118 bool InitInterface(); 119 bool InitLightInterface(); 120 std::string GetPackageName(AccessTokenID tokenId); 121 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_PRESET_INFO 122 int32_t FastVibratorEffect(const VibrateInfo &info, const VibratorIdentifierIPC& identifier); 123 #endif // OHOS_BUILD_ENABLE_VIBRATOR_PRESET_INFO 124 void StartVibrateThread(VibrateInfo info, const VibratorIdentifierIPC& identifier); 125 int32_t StopVibratorService(const VibratorIdentifierIPC& identifier); 126 void SendMsgToClient(const HdfVibratorPlugInfo &info); 127 int32_t RegisterVibratorPlugCb(); 128 std::shared_ptr<VibratorThread> GetVibratorThread(const VibratorIdentifierIPC& identifier); 129 void StopVibrateThread(std::shared_ptr<VibratorThread> vibratorThread); 130 bool ShouldIgnoreVibrate(const VibrateInfo &info, const VibratorIdentifierIPC& identifier); 131 std::string GetCurrentTime(); 132 void MergeVibratorParmeters(const VibrateParameter ¶meter, VibratePackage &package); 133 bool CheckVibratorParmeters(const VibrateParameter ¶meter); 134 void RegisterClientDeathRecipient(sptr<IRemoteObject> vibratorServiceClient, int32_t pid); 135 void UnregisterClientDeathRecipient(sptr<IRemoteObject> vibratorServiceClient); 136 void SaveClientPid(const sptr<IRemoteObject> &vibratorServiceClient, int32_t pid); 137 int32_t FindClientPid(const sptr<IRemoteObject> &vibratorServiceClient); 138 void DestroyClientPid(const sptr<IRemoteObject> &vibratorServiceClient); 139 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 140 int32_t SubscribeCommonEvent(const std::string &eventName, EventReceiver receiver); 141 void OnReceiveEvent(const EventFwk::CommonEventData &data); 142 #ifdef OHOS_BUILD_ENABLE_DO_NOT_DISTURB 143 void OnReceiveUserSwitchEvent(const EventFwk::CommonEventData &data); 144 #endif // OHOS_BUILD_ENABLE_DO_NOT_DISTURB 145 int32_t CheckAuthAndParam(int32_t usage, const VibrateParameter ¶meter, 146 const VibratorIdentifierIPC& identifier); 147 int32_t PlayPatternCheckAuthAndParam(int32_t usage, const VibrateParameter ¶meter); 148 int32_t PlayPrimitiveEffectCheckAuthAndParam(int32_t intensity, int32_t usage); 149 int32_t PlayVibratorEffectCheckAuthAndParam(int32_t count, int32_t usage); 150 int32_t GetHapticCapacityInfo(const VibratorIdentifierIPC& identifier, VibratorCapacity& capacityInfo); 151 int32_t GetAllWaveInfo(const VibratorIdentifierIPC& identifier, std::vector<HdfWaveInformation>& waveInfo); 152 int32_t GetHapticStartUpTime(const VibratorIdentifierIPC& identifier, int32_t mode, int32_t &startUpTime); 153 void GetOnlineVibratorInfo(); 154 std::vector<VibratorIdentifierIPC> CheckDeviceIdIsValid(const VibratorIdentifierIPC& identifier); 155 int32_t StartVibrateThreadControl(const VibratorIdentifierIPC& identifier, VibrateInfo& info); 156 int32_t InsertVibratorInfo(int deviceId, const std::string &deviceName, 157 const std::vector<HdfVibratorInfo> &vibratorInfo); 158 int32_t GetLocalDeviceId(int32_t &deviceId); 159 int32_t GetOneVibrator(const VibratorIdentifierIPC& actIdentifier, 160 std::vector<VibratorInfoIPC>& vibratorInfoIPC); 161 void ConvertToServerInfos(const std::vector<HdfVibratorInfo> &baseVibratorInfo, 162 const VibratorCapacity &vibratorCapacity, const std::vector<HdfWaveInformation> &waveInfomation, 163 const HdfVibratorPlugInfo &info, VibratorAllInfos &vibratorAllInfos); 164 int32_t PerformVibrationControl(const VibratorIdentifierIPC& identifier, int32_t duration, VibrateInfo& info); 165 std::mutex isVibrationPriorityReadyMutex_; 166 static bool isVibrationPriorityReady_; 167 VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance(); 168 LightHdiConnection &lightHdiConnection_ = LightHdiConnection::GetInstance(); 169 bool lightExist_; 170 bool vibratorExist_; 171 std::vector<LightInfoIPC> lightInfos_; 172 std::map<MiscdeviceDeviceId, bool> miscDeviceIdMap_; 173 MiscdeviceServiceState state_; 174 std::mutex vibratorThreadMutex_; 175 sptr<IRemoteObject::DeathRecipient> clientDeathObserver_ = nullptr; 176 std::mutex clientDeathObserverMutex_; 177 static std::map<sptr<IRemoteObject>, int32_t> clientPidMap_; 178 std::mutex clientPidMapMutex_; 179 std::mutex miscDeviceIdMapMutex_; 180 std::mutex lightInfosMutex_; 181 std::mutex devicesManageMutex_; 182 static std::map<int32_t, VibratorAllInfos> devicesManageMap_; 183 }; 184 } // namespace Sensors 185 } // namespace OHOS 186 #endif // MISCDEVICE_SERVICE_H 187