1 /* 2 * Copyright (c) 2021 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 "system_ability.h" 28 #include "thread_ex.h" 29 30 #include "light_hdi_connection.h" 31 #include "miscdevice_common.h" 32 #include "miscdevice_dump.h" 33 #include "miscdevice_service_stub.h" 34 #include "nocopyable.h" 35 #include "vibrator_hdi_connection.h" 36 #include "vibrator_infos.h" 37 #include "vibrator_thread.h" 38 namespace OHOS { 39 namespace Sensors { 40 using namespace Security::AccessToken; 41 enum class MiscdeviceServiceState { 42 STATE_STOPPED, 43 STATE_RUNNING, 44 }; 45 46 class MiscdeviceService : public SystemAbility, public MiscdeviceServiceStub { 47 DECLARE_SYSTEM_ABILITY(MiscdeviceService) 48 public: 49 explicit MiscdeviceService(int32_t systemAbilityId, bool runOnCreate = false); 50 ~MiscdeviceService(); 51 void OnDump() override; 52 void OnStart() override; 53 void OnStop() override; 54 bool IsValid(int32_t lightId); 55 bool IsLightAnimationValid(const LightAnimation &animation); 56 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 57 virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) override; 58 virtual int32_t CancelVibrator(int32_t vibratorId) override; 59 virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, 60 int32_t loopCount, int32_t usage) override; 61 virtual int32_t StopVibratorEffect(int32_t vibratorId, const std::string &effect) override; 62 virtual std::vector<LightInfo> GetLightList() override; 63 virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation) override; 64 virtual int32_t TurnOff(int32_t lightId) override; 65 66 private: 67 DISALLOW_COPY_AND_MOVE(MiscdeviceService); 68 bool InitInterface(); 69 bool InitLightInterface(); 70 std::string GetPackageName(AccessTokenID tokenId); 71 void StartVibrateThread(VibrateInfo info); 72 bool ShouldIgnoreVibrate(const VibrateInfo &info); 73 bool InitLightList(); 74 VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance(); 75 LightHdiConnection &lightHdiConnection_ = LightHdiConnection::GetInstance(); 76 bool lightExist_; 77 bool vibratorExist_; 78 std::vector<LightInfo> lightInfos_; 79 std::map<MiscdeviceDeviceId, bool> miscDdeviceIdMap_; 80 MiscdeviceServiceState state_; 81 std::shared_ptr<VibratorThread> vibratorThread_; 82 std::mutex vibratorThreadMutex_; 83 }; 84 } // namespace Sensors 85 } // namespace OHOS 86 #endif // MISCDEVICE_SERVICE_H 87