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_SERVICE_LISTENER_H
17 #define OHOS_DM_SERVICE_LISTENER_H
18 
19 #include <map>
20 #include <string>
21 #include <unordered_set>
22 #include <mutex>
23 
24 #include "dm_device_info.h"
25 #include "idevice_manager_service_listener.h"
26 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
27 #include "kv_adapter_manager.h"
28 #endif
29 #if !defined(__LITEOS_M__)
30 #include "ipc_notify_dmfa_result_req.h"
31 #include "ipc_server_listener.h"
32 #endif
33 #include "ipc_notify_device_state_req.h"
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 class DeviceManagerServiceListener : public IDeviceManagerServiceListener {
38 public:
DeviceManagerServiceListener()39     DeviceManagerServiceListener() {};
~DeviceManagerServiceListener()40     virtual ~DeviceManagerServiceListener() {};
41 
42     void OnDeviceStateChange(const std::string &pkgName, const DmDeviceState &state, const DmDeviceInfo &info) override;
43 
44     void OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, const DmDeviceInfo &info) override;
45 
46     void OnDeviceFound(const std::string &pkgName, uint16_t subscribeId, DmDeviceBasicInfo &info) override;
47 
48     void OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason) override;
49 
50     void OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId) override;
51 
52     void OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult) override;
53 
54     void OnAuthResult(const std::string &pkgName, const std::string &deviceId, const std::string &token, int32_t status,
55                       int32_t reason) override;
56 
57     void OnUiCall(std::string &pkgName, std::string &paramJson) override;
58 
59     void OnCredentialResult(const std::string &pkgName, int32_t action, const std::string &resultInfo) override;
60 
61     void OnBindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result,
62         int32_t status, std::string content) override;
63 
64     void OnUnbindResult(const std::string &pkgName, const PeerTargetId &targetId, int32_t result,
65         std::string content) override;
66 
67     void OnPinHolderCreate(const std::string &pkgName, const std::string &deviceId, DmPinType pinType,
68         const std::string &payload) override;
69     void OnPinHolderDestroy(const std::string &pkgName, DmPinType pinType, const std::string &payload) override;
70     void OnCreateResult(const std::string &pkgName, int32_t result) override;
71     void OnDestroyResult(const std::string &pkgName, int32_t result) override;
72     void OnPinHolderEvent(const std::string &pkgName, DmPinHolderEvent event, int32_t result,
73         const std::string &content) override;
74 
75     void OnDeviceScreenStateChange(const std::string &pkgName, DmDeviceInfo &devInfo) override;
76     void OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo, uint16_t deviceTypeId,
77                                 int32_t errcode) override;
78 private:
79     void ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName,
80         const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo);
81     void SetDeviceInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq, const std::string &pkgName,
82         const DmDeviceState &state, const DmDeviceInfo &deviceInfo, const DmDeviceBasicInfo &deviceBasicInfo);
83     void ProcessDeviceStateChange(const DmDeviceState &state, const DmDeviceInfo &info,
84         const DmDeviceBasicInfo &deviceBasicInfo);
85     void ProcessAppStateChange(const std::string &pkgName, const DmDeviceState &state,
86         const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo);
87     std::string ComposeOnlineKey(const std::string &pkgName, const std::string &devId);
88     void SetDeviceScreenInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq, const std::string &pkgName,
89         const DmDeviceInfo &deviceInfo);
90 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
91     int32_t ConvertUdidHashToAnoyAndSave(const std::string &pkgName, DmDeviceInfo &deviceInfo);
92     int32_t ConvertUdidHashToAnoyDeviceId(const std::string &pkgName, const std::string &udidHash,
93         std::string &anoyDeviceId);
94 #endif
95 private:
96 #if !defined(__LITEOS_M__)
97     IpcServerListener ipcServerListener_;
98     static std::mutex alreadyOnlineSetLock_;
99     static std::unordered_set<std::string> alreadyOnlineSet_;
100 #endif
101 };
102 } // namespace DistributedHardware
103 } // namespace OHOS
104 #endif // OHOS_DM_SERVICE_LISTENER_H