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 class MiscdeviceService : public SystemAbility, public MiscdeviceServiceStub { 40 DECLARE_SYSTEM_ABILITY(MiscdeviceService) 41 MISCDEVICE_DECLARE_DELAYED_SP_SINGLETON(MiscdeviceService); 42 43 public: 44 void OnDump() override; 45 void OnStart() override; 46 void OnStop() override; 47 void OnStartFuzz(); 48 bool IsValid(int32_t lightId); 49 bool IsLightAnimationValid(const LightAnimationIPC &animation); 50 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 51 void ProcessDeathObserver(const wptr<IRemoteObject> &object); 52 virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) override; 53 virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, 54 int32_t loopCount, int32_t usage, bool systemUsage) override; 55 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM 56 virtual int32_t PlayVibratorCustom(int32_t vibratorId, int32_t fd, int64_t offset, int64_t length, int32_t usage, 57 bool systemUsage, const VibrateParameter ¶meter) override; 58 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM 59 virtual int32_t StopVibrator(int32_t vibratorId) override; 60 virtual int32_t StopVibratorByMode(int32_t vibratorId, const std::string &mode) override; 61 virtual int32_t IsSupportEffect(const std::string &effect, bool &state) override; 62 virtual int32_t GetLightList(std::vector<LightInfoIPC> &lightInfoIpcList) override; 63 virtual int32_t TurnOn(int32_t lightId, int32_t singleColor, const LightAnimationIPC &animation) override; 64 virtual int32_t TurnOff(int32_t lightId) override; 65 virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, 66 bool systemUsage, const VibrateParameter ¶meter) override; 67 virtual int32_t GetDelayTime(int32_t &delayTime) override; 68 virtual int32_t TransferClientRemoteObject(const sptr<IRemoteObject> &vibratorServiceClient) override; 69 virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, 70 int32_t usage, bool systemUsage, int32_t count) override; 71 virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; 72 73 private: 74 DISALLOW_COPY_AND_MOVE(MiscdeviceService); 75 bool InitInterface(); 76 bool InitLightInterface(); 77 std::string GetPackageName(AccessTokenID tokenId); 78 void StartVibrateThread(VibrateInfo info); 79 int32_t StopVibratorService(int32_t vibratorId); 80 void StopVibrateThread(); 81 bool ShouldIgnoreVibrate(const VibrateInfo &info); 82 std::string GetCurrentTime(); 83 void MergeVibratorParmeters(const VibrateParameter ¶meter, VibratePackage &package); 84 bool CheckVibratorParmeters(const VibrateParameter ¶meter); 85 void RegisterClientDeathRecipient(sptr<IRemoteObject> vibratorServiceClient, int32_t pid); 86 void UnregisterClientDeathRecipient(sptr<IRemoteObject> vibratorServiceClient); 87 void SaveClientPid(const sptr<IRemoteObject> &vibratorServiceClient, int32_t pid); 88 int32_t FindClientPid(const sptr<IRemoteObject> &vibratorServiceClient); 89 void DestroyClientPid(const sptr<IRemoteObject> &vibratorServiceClient); 90 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 91 int32_t SubscribeCommonEvent(const std::string &eventName, EventReceiver receiver); 92 void OnReceiveEvent(const EventFwk::CommonEventData &data); 93 int32_t CheckAuthAndParam(int32_t usage, const VibrateParameter ¶meter); 94 int32_t PlayPatternCheckAuthAndParam(int32_t usage, const VibrateParameter ¶meter); 95 int32_t PlayPrimitiveEffectCheckAuthAndParam(int32_t intensity, int32_t usage); 96 int32_t PlayVibratorEffectCheckAuthAndParam(int32_t count, int32_t usage); 97 std::mutex isVibrationPriorityReadyMutex_; 98 static bool isVibrationPriorityReady_; 99 VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance(); 100 LightHdiConnection &lightHdiConnection_ = LightHdiConnection::GetInstance(); 101 bool lightExist_; 102 bool vibratorExist_; 103 std::vector<LightInfoIPC> lightInfos_; 104 std::map<MiscdeviceDeviceId, bool> miscDeviceIdMap_; 105 MiscdeviceServiceState state_; 106 std::shared_ptr<VibratorThread> vibratorThread_ = nullptr; 107 std::mutex vibratorThreadMutex_; 108 sptr<IRemoteObject::DeathRecipient> clientDeathObserver_ = nullptr; 109 std::mutex clientDeathObserverMutex_; 110 std::map<sptr<IRemoteObject>, int32_t> clientPidMap_; 111 std::mutex clientPidMapMutex_; 112 std::mutex miscDeviceIdMapMutex_; 113 std::mutex lightInfosMutex_; 114 }; 115 } // namespace Sensors 116 } // namespace OHOS 117 #endif // MISCDEVICE_SERVICE_H 118