• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 
29 namespace OHOS {
30 namespace Sensors {
31 
32 struct VibratorDecodeHandle {
33     void *handle;
34     IVibratorDecoder *decoder;
35     IVibratorDecoder *(*create)(const RawFileDescriptor &);
36     void (*destroy)(IVibratorDecoder *);
37 
VibratorDecodeHandleVibratorDecodeHandle38     VibratorDecodeHandle(): handle(nullptr), decoder(nullptr),
39         create(nullptr), destroy(nullptr) {}
40 
FreeVibratorDecodeHandle41     void Free()
42     {
43         if (handle != nullptr) {
44             dlclose(handle);
45             handle = nullptr;
46         }
47         decoder = nullptr;
48         create = nullptr;
49         destroy = nullptr;
50     }
51 };
52 
53 class VibratorServiceClient : public Singleton<VibratorServiceClient> {
54 public:
55     ~VibratorServiceClient() override;
56     int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage);
57     int32_t Vibrate(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage);
58 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
59     int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage,
60         const VibratorParameter &parameter);
61 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
62     int32_t StopVibrator(int32_t vibratorId, const std::string &mode);
63     int32_t StopVibrator(int32_t vibratorId);
64     int32_t IsSupportEffect(const std::string &effect, bool &state);
65     void ProcessDeathObserver(const wptr<IRemoteObject> &object);
66     int32_t PreProcess(const VibratorFileDescription &fd, VibratorPackage &package);
67     int32_t GetDelayTime(int32_t &delayTime);
68     int32_t PlayPattern(const VibratorPattern &pattern, int32_t usage, const VibratorParameter &parameter);
69     int32_t FreeVibratorPackage(VibratorPackage &package);
70 
71 private:
72     int32_t InitServiceClient();
73     int32_t LoadDecoderLibrary(const std::string& path);
74     int32_t ConvertVibratePackage(const VibratePackage& inPkg, VibratorPackage &outPkg);
75     sptr<IRemoteObject::DeathRecipient> serviceDeathObserver_ = nullptr;
76     sptr<IMiscdeviceService> miscdeviceProxy_ = nullptr;
77     VibratorDecodeHandle decodeHandle_;
78     std::mutex clientMutex_;
79     std::mutex decodeMutex_;
80 };
81 }  // namespace Sensors
82 }  // namespace OHOS
83 #endif  // VIBRATOR_SERVICE_CLIENT_H
84