• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025-2025 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 #include "interoperable_device_observer.h"
17 #include "interoperable_communication_manager.h"
18 #include "telephony_log_wrapper.h"
19 #include "telephony_permission.h"
20 
21 namespace OHOS {
22 namespace Telephony {
InteroperableDeviceObserver()23 InteroperableDeviceObserver::InteroperableDeviceObserver()
24 {
25 }
26 
~InteroperableDeviceObserver()27 InteroperableDeviceObserver::~InteroperableDeviceObserver()
28 {
29 }
30 
Init()31 void InteroperableDeviceObserver::Init()
32 {
33     auto dmInitCallback = std::make_shared<DeviceInitCallback>();
34     DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(PACKAGE_NAME, dmInitCallback);
35     auto stateCallback = std::make_shared<DmStateCallback>();
36     int32_t ret = DistributedHardware::DeviceManager::
37         GetInstance().RegisterDevStateCallback(PACKAGE_NAME, "", stateCallback);
38     if (ret != 0) {
39         TELEPHONY_LOGE("RegisterDevStateCallback fail");
40     }
41 }
42 
RegisterDevStatusCallback(const std::shared_ptr<IInteroperableDeviceStateCallback> & callback)43 void InteroperableDeviceObserver::RegisterDevStatusCallback(
44     const std::shared_ptr<IInteroperableDeviceStateCallback> &callback)
45 {
46     std::lock_guard<ffrt::mutex> lock(mutex_);
47     if (callback == nullptr) {
48         TELEPHONY_LOGE("reg dev status callback null");
49         return;
50     }
51     auto iter = std::find(callbacks_.begin(), callbacks_.end(), callback);
52     if (iter != callbacks_.end()) {
53         TELEPHONY_LOGI("callback already reg");
54         return;
55     }
56     callbacks_.push_back(callback);
57     TELEPHONY_LOGI("reg dev status callback");
58 }
59 
UnRegisterDevStatusCallback(const std::shared_ptr<IInteroperableDeviceStateCallback> & callback)60 void InteroperableDeviceObserver::UnRegisterDevStatusCallback(
61     const std::shared_ptr<IInteroperableDeviceStateCallback> &callback)
62 {
63     std::lock_guard<ffrt::mutex> lock(mutex_);
64     auto iter = std::find(callbacks_.begin(), callbacks_.end(), callback);
65     if (iter != callbacks_.end()) {
66         callbacks_.erase(iter);
67     }
68     TELEPHONY_LOGI("un-reg dev status callback");
69 }
70 
OnDeviceOnline(const std::string & networkId,const std::string & devName,uint16_t devType)71 void InteroperableDeviceObserver::OnDeviceOnline(const std::string &networkId, const std::string &devName,
72                                                  uint16_t devType)
73 {
74     std::list<std::shared_ptr<IInteroperableDeviceStateCallback>> callbacks;
75     {
76         std::lock_guard<ffrt::mutex> lock(mutex_);
77         callbacks = callbacks_;
78     }
79     for (auto& callback : callbacks) {
80         if (callback != nullptr) {
81             callback->OnDeviceOnline(networkId, devName, devType);
82         }
83     }
84 }
85 
OnDeviceOffline(const std::string & networkId,const std::string & devName,uint16_t devType)86 void InteroperableDeviceObserver::OnDeviceOffline(const std::string &networkId, const std::string &devName,
87                                                   uint16_t devType)
88 {
89     std::list<std::shared_ptr<IInteroperableDeviceStateCallback>> callbacks;
90     {
91         std::lock_guard<ffrt::mutex> lock(mutex_);
92         callbacks = callbacks_;
93     }
94     for (auto& callback : callbacks) {
95         if (callback != nullptr) {
96             callback->OnDeviceOffline(networkId, devName, devType);
97         }
98     }
99 }
100 
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)101 void DmStateCallback::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo)
102 {
103     TELEPHONY_LOGI("DmStateCallback receive device online");
104     DelayedSingleton<InteroperableCommunicationManager>::GetInstance()->OnDeviceOnline(deviceInfo);
105 }
106 
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)107 void DmStateCallback::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo)
108 {
109     TELEPHONY_LOGI("DmStateCallback receive device offline");
110     DelayedSingleton<InteroperableCommunicationManager>::GetInstance()->OnDeviceOffline(deviceInfo);
111 }
112 }
113 }