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 VIBRATOR_SERVICE_CLIENT_H
17 #define VIBRATOR_SERVICE_CLIENT_H
18 
19 #include <dlfcn.h>
20 #include <mutex>
21 
22 #include "iremote_object.h"
23 #include "singleton.h"
24 
25 #include "i_vibrator_decoder.h"
26 #include "miscdevice_service_proxy.h"
27 #include "vibrator_agent_type.h"
28 #include "vibrator_client_stub.h"
29 
30 namespace OHOS {
31 namespace Sensors {
32 
33 struct VibratorDecodeHandle {
34     void *handle;
35     IVibratorDecoder *decoder;
36     IVibratorDecoder *(*create)(const JsonParser &);
37     void (*destroy)(IVibratorDecoder *);
38 
VibratorDecodeHandleVibratorDecodeHandle39     VibratorDecodeHandle(): handle(nullptr), decoder(nullptr),
40         create(nullptr), destroy(nullptr) {}
41 
FreeVibratorDecodeHandle42     void Free()
43     {
44         if (handle != nullptr) {
45             dlclose(handle);
46             handle = nullptr;
47         }
48         decoder = nullptr;
49         create = nullptr;
50         destroy = nullptr;
51     }
52 };
53 
54 class VibratorServiceClient : public Singleton<VibratorServiceClient> {
55 public:
56     ~VibratorServiceClient() override;
57     int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage);
58     int32_t Vibrate(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage, bool systemUsage);
59 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
60     int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage,
61         bool systemUsage, const VibratorParameter &parameter);
62 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
63     int32_t StopVibrator(int32_t vibratorId, const std::string &mode);
64     int32_t StopVibrator(int32_t vibratorId);
65     bool IsHdHapticSupported();
66     int32_t IsSupportEffect(const std::string &effect, bool &state);
67     void ProcessDeathObserver(const wptr<IRemoteObject> &object);
68     int32_t PreProcess(const VibratorFileDescription &fd, VibratorPackage &package);
69     int32_t GetDelayTime(int32_t &delayTime);
70     int32_t PlayPattern(const VibratorPattern &pattern, int32_t usage, bool systemUsage,
71         const VibratorParameter &parameter);
72     int32_t FreeVibratorPackage(VibratorPackage &package);
73     int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, int32_t usage,
74         bool systemUsage, int32_t count);
75     bool IsSupportVibratorCustom();
76 
77 private:
78     int32_t InitServiceClient();
79     int32_t LoadDecoderLibrary(const std::string& path);
80     int32_t ConvertVibratePackage(const VibratePackage& inPkg, VibratorPackage &outPkg);
81     int32_t TransferClientRemoteObject();
82     int32_t GetVibratorCapacity();
83     sptr<IRemoteObject::DeathRecipient> serviceDeathObserver_ = nullptr;
84     sptr<IMiscdeviceService> miscdeviceProxy_ = nullptr;
85     sptr<VibratorClientStub> vibratorClient_ = nullptr;
86     VibratorDecodeHandle decodeHandle_;
87     VibratorCapacity capacity_;
88     std::mutex clientMutex_;
89     std::mutex decodeMutex_;
90 };
91 }  // namespace Sensors
92 }  // namespace OHOS
93 #endif  // VIBRATOR_SERVICE_CLIENT_H
94