1 /*
2  * Copyright (c) 2022-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 OHOS_DM_NOTIFY_H
17 #define OHOS_DM_NOTIFY_H
18 
19 #include <chrono>
20 #include <condition_variable>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <vector>
26 
27 #include "device_manager_callback.h"
28 #include "dm_device_info.h"
29 #include "dm_subscribe_info.h"
30 #include "dm_single_instance.h"
31 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
32 #include "ffrt.h"
33 #endif
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 class DeviceManagerNotify {
38     DM_DECLARE_SINGLE_INSTANCE(DeviceManagerNotify);
39 
40 public:
41     void RegisterDeathRecipientCallback(const std::string &pkgName, std::shared_ptr<DmInitCallback> dmInitCallback);
42     void UnRegisterDeathRecipientCallback(const std::string &pkgName);
43     void RegisterDeviceStateCallback(const std::string &pkgName, std::shared_ptr<DeviceStateCallback> callback);
44     void UnRegisterDeviceStateCallback(const std::string &pkgName);
45     void UnRegisterDeviceStatusCallback(const std::string &pkgName);
46     void RegisterDeviceStatusCallback(const std::string &pkgName, std::shared_ptr<DeviceStatusCallback> callback);
47     void RegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId,
48                                    std::shared_ptr<DiscoveryCallback> callback);
49     void UnRegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId);
50     void RegisterPublishCallback(const std::string &pkgName, int32_t publishId,
51         std::shared_ptr<PublishCallback> callback);
52     void UnRegisterPublishCallback(const std::string &pkgName, int32_t publishId);
53     void RegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId,
54                                       std::shared_ptr<AuthenticateCallback> callback);
55     void UnRegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId);
56     void UnRegisterPackageCallback(const std::string &pkgName);
57     void RegisterDeviceManagerFaCallback(const std::string &pkgName, std::shared_ptr<DeviceManagerUiCallback> callback);
58     void UnRegisterDeviceManagerFaCallback(const std::string &pkgName);
59     void RegisterCredentialCallback(const std::string &pkgName, std::shared_ptr<CredentialCallback> callback);
60     void UnRegisterCredentialCallback(const std::string &pkgName);
61     void RegisterBindCallback(const std::string &pkgName, const PeerTargetId &targetId,
62         std::shared_ptr<BindTargetCallback> callback);
63     void RegisterUnbindCallback(const std::string &pkgName, const PeerTargetId &targetId,
64         std::shared_ptr<UnbindTargetCallback> callback);
65     void OnBindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result, int32_t status,
66         std::string content);
67     void OnUnbindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result, std::string content);
68     void RegisterPinHolderCallback(const std::string &pkgName, std::shared_ptr<PinHolderCallback> callback);
69     void RegisterDeviceScreenStatusCallback(const std::string &pkgName,
70         std::shared_ptr<DeviceScreenStatusCallback> callback);
71     void UnRegisterDeviceScreenStatusCallback(const std::string &pkgName);
72     void RegisterCredentialAuthStatusCallback(const std::string &pkgName,
73         std::shared_ptr<CredentialAuthStatusCallback> callback);
74     void UnRegisterCredentialAuthStatusCallback(const std::string &pkgName);
75 
76 public:
77     static void DeviceInfoOnline(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk);
78     static void DeviceInfoOffline(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk);
79     static void DeviceInfoChanged(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk);
80     static void DeviceInfoReady(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk);
81     static void DeviceBasicInfoOnline(const DmDeviceBasicInfo &deviceBasicInfo,
82         std::shared_ptr<DeviceStatusCallback> tempCbk);
83     static void DeviceBasicInfoOffline(const DmDeviceBasicInfo &deviceBasicInfo,
84         std::shared_ptr<DeviceStatusCallback> tempCbk);
85     static void DeviceBasicInfoChanged(const DmDeviceBasicInfo &deviceBasicInfo,
86         std::shared_ptr<DeviceStatusCallback> tempCbk);
87     static void DeviceBasicInfoReady(const DmDeviceBasicInfo &deviceBasicInfo,
88         std::shared_ptr<DeviceStatusCallback> tempCbk);
89 public:
90     void OnRemoteDied();
91     void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo);
92     void OnDeviceOnline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo);
93     void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &deviceInfo);
94     void OnDeviceOffline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo);
95     void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &deviceInfo);
96     void OnDeviceChanged(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo);
97     void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &deviceInfo);
98     void OnDeviceReady(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo);
99     void OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, const DmDeviceInfo &deviceInfo);
100     void OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, const DmDeviceBasicInfo &deviceBasicInfo);
101     void OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason);
102     void OnDiscoverySuccess(const std::string &pkgName, uint16_t subscribeId);
103     void OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult);
104     void OnAuthResult(const std::string &pkgName, const std::string &deviceId, const std::string &token,
105                       int32_t status, int32_t reason);
106     void OnUiCall(std::string &pkgName, std::string &paramJson);
107     void OnCredentialResult(const std::string &pkgName, int32_t &action, const std::string &credentialResult);
108     void OnPinHolderCreate(const std::string &deviceId, const std::string &pkgName, DmPinType pinType,
109         const std::string &payload);
110     void OnPinHolderDestroy(const std::string &pkgName, DmPinType pinType, const std::string &payload);
111     void OnCreateResult(const std::string &pkgName, int32_t result);
112     void OnDestroyResult(const std::string &pkgName, int32_t result);
113     void OnPinHolderEvent(const std::string &pkgName, DmPinHolderEvent event, int32_t result,
114                           const std::string &content);
115     std::map<std::string, std::shared_ptr<DmInitCallback>> GetDmInitCallback();
116     void OnDeviceScreenStatus(const std::string &pkgName, const DmDeviceInfo &deviceInfo);
117     void OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo,
118                                 uint16_t deviceTypeId, int32_t errcode);
119 
120 private:
121 #if !defined(__LITEOS_M__)
122     std::mutex lock_;
123 #endif
124     std::map<std::string, std::shared_ptr<DeviceStateCallback>> deviceStateCallback_;
125     std::map<std::string, std::shared_ptr<DeviceStatusCallback>> deviceStatusCallback_;
126     std::map<std::string, std::map<uint16_t, std::shared_ptr<DiscoveryCallback>>> deviceDiscoveryCallbacks_;
127     std::map<std::string, std::map<int32_t, std::shared_ptr<PublishCallback>>> devicePublishCallbacks_;
128     std::map<std::string, std::map<std::string, std::shared_ptr<AuthenticateCallback>>> authenticateCallback_;
129     std::map<std::string, std::shared_ptr<DmInitCallback>> dmInitCallback_;
130     std::map<std::string, std::shared_ptr<DeviceManagerUiCallback>> dmUiCallback_;
131     std::map<std::string, std::shared_ptr<CredentialCallback>> credentialCallback_;
132     std::map<std::string, std::map<PeerTargetId, std::shared_ptr<BindTargetCallback>>> bindCallback_;
133     std::map<std::string, std::map<PeerTargetId, std::shared_ptr<UnbindTargetCallback>>> unbindCallback_;
134     std::map<std::string, std::shared_ptr<PinHolderCallback>> pinHolderCallback_;
135     std::map<std::string, std::shared_ptr<DeviceScreenStatusCallback>> deviceScreenStatusCallback_;
136     std::map<std::string, std::shared_ptr<CredentialAuthStatusCallback>> credentialAuthStatusCallback_;
137     std::mutex bindLock_;
138 };
139 } // namespace DistributedHardware
140 } // namespace OHOS
141 #endif // OHOS_DM_NOTIFY_H
142