• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef OHOS_DEVICE_MANAGER_UTILS_H
16 #define OHOS_DEVICE_MANAGER_UTILS_H
17 
18 #include <atomic>
19 
20 #include "json_object.h"
21 
22 #include "device_manager_callback.h"
23 #include "dm_device_info.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 
28 enum DmFfiDevStatusChange { UNKNOWN = 0, AVAILABLE = 1, UNAVAILABLE = 2, CHANGE = 3};
29 
30 class DmFfiInitCallback : public DmInitCallback {
31 public:
DmFfiInitCallback(const std::string & bundleName)32     explicit DmFfiInitCallback(const std::string &bundleName) : bundleName_(bundleName)
33     {
34     }
35     ~DmFfiInitCallback() override = default;
36     void OnRemoteDied() override;
37 
38 private:
39     std::string bundleName_;
40 };
41 
42 class DmFfiDeviceStatusCallback : public DeviceStatusCallback {
43 public:
DmFfiDeviceStatusCallback(const std::string & bundleName)44     explicit DmFfiDeviceStatusCallback(const std::string &bundleName) : bundleName_(bundleName)
45     {
46     }
47     ~DmFfiDeviceStatusCallback() override = default;
48     void OnDeviceOnline(const DmDeviceBasicInfo &deviceBasicInfo) override;
49     void OnDeviceReady(const DmDeviceBasicInfo &deviceBasicInfo) override;
50     void OnDeviceOffline(const DmDeviceBasicInfo &deviceBasicInfo) override;
51     void OnDeviceChanged(const DmDeviceBasicInfo &deviceBasicInfo) override;
52 private:
53     std::string bundleName_;
54 };
55 
56 class DmFfiDiscoveryCallback : public DiscoveryCallback {
57 public:
DmFfiDiscoveryCallback(const std::string & bundleName)58     explicit DmFfiDiscoveryCallback(const std::string &bundleName): refCount_(0), bundleName_(bundleName)
59     {
60     }
61     ~DmFfiDiscoveryCallback() override = default;
62     void OnDeviceFound(uint16_t subscribeId,
63         const OHOS::DistributedHardware::DmDeviceBasicInfo &deviceBasicInfo) override;
64     void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override;
65     void OnDiscoverySuccess(uint16_t subscribeId) override;
66     void IncreaseRefCount();
67     void DecreaseRefCount();
68     int32_t GetRefCount();
69 
70 private:
71     std::atomic<int32_t> refCount_;
72     std::string bundleName_;
73 };
74 
75 class DmFfiPublishCallback : public PublishCallback {
76 public:
DmFfiPublishCallback(const std::string & bundleName)77     explicit DmFfiPublishCallback(const std::string &bundleName): refCount_(0), bundleName_(bundleName)
78     {
79     }
80     ~DmFfiPublishCallback() override = default;
81     void OnPublishResult(int32_t publishId, int32_t publishResult) override;
82     void IncreaseRefCount();
83     void DecreaseRefCount();
84     int32_t GetRefCount();
85 
86 private:
87     std::atomic<int32_t> refCount_;
88     std::string bundleName_;
89 };
90 
91 class DmFfiDeviceManagerUiCallback : public DeviceManagerUiCallback {
92 public:
DmFfiDeviceManagerUiCallback(const std::string & bundleName)93     explicit DmFfiDeviceManagerUiCallback(const std::string &bundleName) : bundleName_(bundleName)
94     {
95     }
96     ~DmFfiDeviceManagerUiCallback() override = default;
97     void OnCall(const std::string &paramJson) override;
98 
99 private:
100     std::string bundleName_;
101 };
102 
103 class DmFfiAuthenticateCallback : public AuthenticateCallback {
104 public:
DmFfiAuthenticateCallback(const std::string & bundleName)105     explicit DmFfiAuthenticateCallback(const std::string &bundleName) : bundleName_(bundleName)
106     {
107     }
108     ~DmFfiAuthenticateCallback() override = default;
109     void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason) override;
110 
111 private:
112     std::string bundleName_;
113 };
114 
115 class DmFfiBindTargetCallback : public BindTargetCallback {
116 public:
DmFfiBindTargetCallback(std::string & bundleName)117     explicit DmFfiBindTargetCallback(std::string &bundleName) : bundleName_(bundleName)
118     {
119     }
120     ~DmFfiBindTargetCallback() override = default;
121     void OnBindResult(const PeerTargetId &targetId, int32_t result, int32_t status, std::string content) override;
122 
123 private:
124     std::string bundleName_;
125 };
126 
127 const std::string &GetDeviceTypeById(DmDeviceType type);
128 char *MallocCStr(const char *in);
129 void InsertMapParames(JsonObject &bindParamObj, std::map<std::string, std::string> &bindParamMap);
130 } // namespace DistributedHardware
131 } // namespace OHOS
132 
133 #endif // OHOS_DEVICE_MANAGER_IMPL_H
134