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 <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 "common_event_manager.h"
28 #include "nocopyable.h"
29 #include "system_ability.h"
30 #include "thread_ex.h"
31 #include "want.h"
32 
33 #include "file_utils.h"
34 #include "json_parser.h"
35 #include "light_hdi_connection.h"
36 #include "miscdevice_common.h"
37 #include "miscdevice_common_event_subscriber.h"
38 #include "miscdevice_delayed_sp_singleton.h"
39 #include "miscdevice_dump.h"
40 #include "miscdevice_service_stub.h"
41 #include "vibrator_hdi_connection.h"
42 #include "vibrator_infos.h"
43 #include "vibrator_thread.h"
44 
45 namespace OHOS {
46 namespace Sensors {
47 using namespace Security::AccessToken;
48 enum class MiscdeviceServiceState {
49     STATE_STOPPED,
50     STATE_RUNNING,
51 };
52 
53 class MiscdeviceService : public SystemAbility, public MiscdeviceServiceStub {
54     DECLARE_SYSTEM_ABILITY(MiscdeviceService)
55     MISCDEVICE_DECLARE_DELAYED_SP_SINGLETON(MiscdeviceService);
56 
57 public:
58     void OnDump() override;
59     void OnStart() override;
60     void OnStop() override;
61     void OnStartFuzz();
62     bool IsValid(int32_t lightId);
63     bool IsLightAnimationValid(const LightAnimationIPC &animation);
64     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
65     void ProcessDeathObserver(const wptr<IRemoteObject> &object);
66     virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) override;
67     virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect,
68                                        int32_t loopCount, int32_t usage, bool systemUsage) override;
69 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
70     virtual int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage,
71         bool systemUsage, const VibrateParameter &parameter) override;
72 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
73     virtual int32_t StopVibrator(int32_t vibratorId) override;
74     virtual int32_t StopVibrator(int32_t vibratorId, const std::string &mode) override;
75     virtual int32_t IsSupportEffect(const std::string &effect, bool &state) override;
76     virtual std::vector<LightInfoIPC> GetLightList() override;
77     virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation) override;
78     virtual int32_t TurnOff(int32_t lightId) override;
79     virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage,
80         bool systemUsage, const VibrateParameter &parameter) override;
81     virtual int32_t GetDelayTime(int32_t &delayTime) override;
82     virtual int32_t TransferClientRemoteObject(const sptr<IRemoteObject> &vibratorServiceClient) override;
83     virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity,
84                                         int32_t usage, bool systemUsage, int32_t count) override;
85     virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) override;
86 
87 private:
88     DISALLOW_COPY_AND_MOVE(MiscdeviceService);
89     bool InitInterface();
90     bool InitLightInterface();
91     std::string GetPackageName(AccessTokenID tokenId);
92     void StartVibrateThread(VibrateInfo info);
93     void StopVibrateThread();
94     bool ShouldIgnoreVibrate(const VibrateInfo &info);
95     std::string GetCurrentTime();
96     void MergeVibratorParmeters(const VibrateParameter &parameter, VibratePackage &package);
97     bool CheckVibratorParmeters(const VibrateParameter &parameter);
98     void RegisterClientDeathRecipient(sptr<IRemoteObject> vibratorServiceClient, int32_t pid);
99     void UnregisterClientDeathRecipient(sptr<IRemoteObject> vibratorServiceClient);
100     void SaveClientPid(const sptr<IRemoteObject> &vibratorServiceClient, int32_t pid);
101     int32_t FindClientPid(const sptr<IRemoteObject> &vibratorServiceClient);
102     void DestroyClientPid(const sptr<IRemoteObject> &vibratorServiceClient);
103     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
104     int32_t SubscribeCommonEvent(const std::string &eventName, EventReceiver receiver);
105     void OnReceiveEvent(const EventFwk::CommonEventData &data);
106     std::mutex isVibrationPriorityReadyMutex_;
107     static bool isVibrationPriorityReady_;
108     VibratorHdiConnection &vibratorHdiConnection_ = VibratorHdiConnection::GetInstance();
109     LightHdiConnection &lightHdiConnection_ = LightHdiConnection::GetInstance();
110     bool lightExist_;
111     bool vibratorExist_;
112     std::vector<LightInfoIPC> lightInfos_;
113     std::map<MiscdeviceDeviceId, bool> miscDeviceIdMap_;
114     MiscdeviceServiceState state_;
115     std::shared_ptr<VibratorThread> vibratorThread_ = nullptr;
116     std::mutex vibratorThreadMutex_;
117     sptr<IRemoteObject::DeathRecipient> clientDeathObserver_ = nullptr;
118     std::mutex clientDeathObserverMutex_;
119     std::map<sptr<IRemoteObject>, int32_t> clientPidMap_;
120     std::mutex clientPidMapMutex_;
121     std::mutex miscDeviceIdMapMutex_;
122     std::mutex lightInfosMutex_;
123 };
124 }  // namespace Sensors
125 }  // namespace OHOS
126 #endif  // MISCDEVICE_SERVICE_H
127