1 /* 2 * Copyright (c) 2021-2023 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 <memory> 20 #include <mutex> 21 #include <set> 22 #include <string> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "accesstoken_kit.h" 27 #include "nocopyable.h" 28 #include "system_ability.h" 29 #include "thread_ex.h" 30 31 #include "file_utils.h" 32 #include "json_parser.h" 33 #include "light_hdi_connection.h" 34 #include "miscdevice_common.h" 35 #include "miscdevice_delayed_sp_singleton.h" 36 #include "miscdevice_dump.h" 37 #include "miscdevice_service_stub.h" 38 #include "vibrator_hdi_connection.h" 39 #include "vibrator_infos.h" 40 #include "vibrator_thread.h" 41 42 namespace OHOS { 43 namespace Sensors { 44 using namespace Security::AccessToken; 45 enum class MiscdeviceServiceState { 46 STATE_STOPPED, 47 STATE_RUNNING, 48 }; 49 50 class MiscdeviceService : public SystemAbility, public MiscdeviceServiceStub { 51 DECLARE_SYSTEM_ABILITY(MiscdeviceService) 52 MISCDEVICE_DECLARE_DELAYED_SP_SINGLETON(MiscdeviceService); 53 54 public: 55 void OnDump() override; 56 void OnStart() override; 57 void OnStop() override; 58 void OnStartFuzz(); 59 bool IsValid(int32_t lightId); 60 bool IsLightAnimationValid(const LightAnimationIPC &animation); 61 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 62 virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) override; 63 virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, 64 int32_t loopCount, int32_t usage) override; 65 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM 66 virtual int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, 67 const VibrateParameter ¶meter) override; 68 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM 69 virtual int32_t StopVibrator(int32_t vibratorId) override; 70 virtual int32_t StopVibrator(int32_t vibratorId, const std::string &mode) override; 71 virtual int32_t IsSupportEffect(const std::string &effect, bool &state) override; 72 virtual std::vector<LightInfoIPC> GetLightList() override; 73 virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation) override; 74 virtual int32_t TurnOff(int32_t lightId) override; 75 virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, 76 const VibrateParameter ¶meter) override; 77 virtual int32_t GetDelayTime(int32_t &delayTime) override; 78 79 private: 80 DISALLOW_COPY_AND_MOVE(MiscdeviceService); 81 bool InitInterface(); 82 bool InitLightInterface(); 83 std::string GetPackageName(AccessTokenID tokenId); 84 void StartVibrateThread(VibrateInfo info); 85 void StopVibrateThread(); 86 bool ShouldIgnoreVibrate(const VibrateInfo &info); 87 void MergeVibratorParmeters(const VibrateParameter ¶meter, VibratePackage &package); 88 bool CheckVibratorParmeters(const VibrateParameter ¶meter); 89 bool InitLightList(); 90 VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance(); 91 LightHdiConnection &lightHdiConnection_ = LightHdiConnection::GetInstance(); 92 bool lightExist_; 93 bool vibratorExist_; 94 std::vector<LightInfoIPC> lightInfos_; 95 std::map<MiscdeviceDeviceId, bool> miscDeviceIdMap_; 96 MiscdeviceServiceState state_; 97 std::shared_ptr<VibratorThread> vibratorThread_ = nullptr; 98 std::mutex vibratorThreadMutex_; 99 }; 100 } // namespace Sensors 101 } // namespace OHOS 102 #endif // MISCDEVICE_SERVICE_H 103