1 /*
2  * Copyright (c) 2021-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 #include "bus_center_client_proxy.h"
17 
18 #include "bus_center_client_proxy_standard.h"
19 #include "lnn_log.h"
20 #include "softbus_client_info_manager.h"
21 #include "softbus_bus_center.h"
22 #include "softbus_def.h"
23 #include "softbus_errcode.h"
24 #include "softbus_permission.h"
25 
26 using namespace OHOS;
27 
GetClientProxy(const char * pkgName,int32_t pid)28 static sptr<BusCenterClientProxy> GetClientProxy(const char *pkgName, int32_t pid)
29 {
30     sptr<IRemoteObject> clientObject = SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxy(pkgName, pid);
31     sptr<BusCenterClientProxy> clientProxy = new (std::nothrow) BusCenterClientProxy(clientObject);
32     return clientProxy;
33 }
34 
ClientOnJoinLNNResult(PkgNameAndPidInfo * info,void * addr,uint32_t addrTypeLen,const char * networkId,int32_t retCode)35 int32_t ClientOnJoinLNNResult(PkgNameAndPidInfo *info, void *addr, uint32_t addrTypeLen,
36     const char *networkId, int32_t retCode)
37 {
38     if (info == nullptr) {
39         LNN_LOGE(LNN_EVENT, "pkgName is null");
40         return SOFTBUS_ERR;
41     }
42     sptr<BusCenterClientProxy> clientProxy = GetClientProxy(info->pkgName, info->pid);
43     if (clientProxy == nullptr) {
44         LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
45         return SOFTBUS_ERR;
46     }
47     return clientProxy->OnJoinLNNResult(addr, addrTypeLen, networkId, retCode);
48 }
49 
ClientOnLeaveLNNResult(const char * pkgName,int32_t pid,const char * networkId,int32_t retCode)50 int32_t ClientOnLeaveLNNResult(
51     const char *pkgName, int32_t pid, const char *networkId, int32_t retCode)
52 {
53     if (pkgName == nullptr) {
54         LNN_LOGE(LNN_EVENT, "pkgName is null");
55         return SOFTBUS_INVALID_PARAM;
56     }
57     sptr<BusCenterClientProxy> clientProxy = GetClientProxy(pkgName, pid);
58     if (clientProxy == nullptr) {
59         LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
60         return SOFTBUS_ERR;
61     }
62     return clientProxy->OnLeaveLNNResult(networkId, retCode);
63 }
64 
ClinetOnNodeOnlineStateChanged(bool isOnline,void * info,uint32_t infoTypeLen)65 int32_t ClinetOnNodeOnlineStateChanged(bool isOnline, void *info, uint32_t infoTypeLen)
66 {
67     std::multimap<std::string, sptr<IRemoteObject>> proxyMap;
68     SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxyMap(proxyMap);
69     for (auto proxy : proxyMap) {
70         sptr<BusCenterClientProxy> clientProxy = new (std::nothrow) BusCenterClientProxy(proxy.second);
71         clientProxy->OnNodeOnlineStateChanged(proxy.first.c_str(), isOnline, info, infoTypeLen);
72     }
73     return SOFTBUS_OK;
74 }
75 
ClinetOnNodeBasicInfoChanged(void * info,uint32_t infoTypeLen,int32_t type)76 int32_t ClinetOnNodeBasicInfoChanged(void *info, uint32_t infoTypeLen, int32_t type)
77 {
78     std::multimap<std::string, sptr<IRemoteObject>> proxyMap;
79     SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxyMap(proxyMap);
80     for (auto proxy : proxyMap) {
81         sptr<BusCenterClientProxy> clientProxy = new (std::nothrow) BusCenterClientProxy(proxy.second);
82         clientProxy->OnNodeBasicInfoChanged(proxy.first.c_str(), info, infoTypeLen, type);
83     }
84     return SOFTBUS_OK;
85 }
86 
ClientOnNodeStatusChanged(void * info,uint32_t infoTypeLen,int32_t type)87 int32_t ClientOnNodeStatusChanged(void *info, uint32_t infoTypeLen, int32_t type)
88 {
89     std::multimap<std::string, sptr<IRemoteObject>> proxyMap;
90     SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxyMap(proxyMap);
91     if (proxyMap.empty()) {
92         LNN_LOGE(LNN_EVENT, "proxyMap is empty");
93         return SOFTBUS_NETWORK_REMOTE_NULL;
94     }
95     for (const auto &proxy : proxyMap) {
96         sptr<BusCenterClientProxy> clientProxy = new (std::nothrow) BusCenterClientProxy(proxy.second);
97         clientProxy->OnNodeStatusChanged(proxy.first.c_str(), info, infoTypeLen, type);
98     }
99     return SOFTBUS_OK;
100 }
101 
ClinetOnLocalNetworkIdChanged()102 int32_t ClinetOnLocalNetworkIdChanged()
103 {
104     std::multimap<std::string, sptr<IRemoteObject>> proxyMap;
105     SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxyMap(proxyMap);
106     for (auto proxy : proxyMap) {
107         sptr<BusCenterClientProxy> clientProxy = new (std::nothrow) BusCenterClientProxy(proxy.second);
108         clientProxy->OnLocalNetworkIdChanged(proxy.first.c_str());
109     }
110     return SOFTBUS_OK;
111 }
112 
ClinetNotifyDeviceNotTrusted(const char * msg)113 int32_t ClinetNotifyDeviceNotTrusted(const char *msg)
114 {
115     std::multimap<std::string, sptr<IRemoteObject>> proxyMap;
116     SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxyMap(proxyMap);
117     for (auto proxy : proxyMap) {
118         const char *dmPkgName = "ohos.distributedhardware.devicemanager";
119         if (strcmp(dmPkgName, proxy.first.c_str()) != 0) {
120             continue;
121         }
122         sptr<BusCenterClientProxy> clientProxy = new (std::nothrow) BusCenterClientProxy(proxy.second);
123         clientProxy->OnNodeDeviceNotTrusted(proxy.first.c_str(), msg);
124     }
125     return SOFTBUS_OK;
126 }
127 
ClientNotifyHichainProofException(const char * proofInfo,uint32_t proofLen,uint16_t deviceTypeId,int32_t errCode)128 int32_t ClientNotifyHichainProofException(
129     const char *proofInfo, uint32_t proofLen, uint16_t deviceTypeId, int32_t errCode)
130 {
131     std::multimap<std::string, sptr<IRemoteObject>> proxyMap;
132     SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxyMap(proxyMap);
133     const char *dmPkgName = "ohos.distributedhardware.devicemanager";
134     for (auto proxy : proxyMap) {
135         if (strcmp(dmPkgName, proxy.first.c_str()) != 0) {
136             continue;
137         }
138         sptr<BusCenterClientProxy> clientProxy = new (std::nothrow) BusCenterClientProxy(proxy.second);
139         if (clientProxy == nullptr) {
140             LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
141             return SOFTBUS_ERR;
142         }
143         clientProxy->OnHichainProofException(proxy.first.c_str(), proofInfo, proofLen, deviceTypeId, errCode);
144     }
145     return SOFTBUS_OK;
146 }
147 
ClientOnTimeSyncResult(const char * pkgName,int32_t pid,const void * info,uint32_t infoTypeLen,int32_t retCode)148 int32_t ClientOnTimeSyncResult(const char *pkgName, int32_t pid, const void *info,
149     uint32_t infoTypeLen, int32_t retCode)
150 {
151     if (pkgName == nullptr) {
152         LNN_LOGE(LNN_EVENT, "pkgName is null");
153         return SOFTBUS_INVALID_PARAM;
154     }
155     sptr<BusCenterClientProxy> clientProxy = GetClientProxy(pkgName, pid);
156     if (clientProxy == nullptr) {
157         LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
158         return SOFTBUS_ERR;
159     }
160     return clientProxy->OnTimeSyncResult(info, infoTypeLen, retCode);
161 }
162 
ClientOnPublishLNNResult(const char * pkgName,int32_t pid,int32_t publishId,int32_t reason)163 int32_t ClientOnPublishLNNResult(const char *pkgName, int32_t pid, int32_t publishId,
164     int32_t reason)
165 {
166     if (pkgName == nullptr) {
167         LNN_LOGE(LNN_EVENT, "pkgName is null");
168         return SOFTBUS_INVALID_PARAM;
169     }
170     sptr<BusCenterClientProxy> clientProxy = GetClientProxy(pkgName, pid);
171     if (clientProxy == nullptr) {
172         LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
173         return SOFTBUS_ERR;
174     }
175     clientProxy->OnPublishLNNResult(publishId, reason);
176     return SOFTBUS_OK;
177 }
178 
ClientOnRefreshLNNResult(const char * pkgName,int32_t pid,int32_t refreshId,int32_t reason)179 int32_t ClientOnRefreshLNNResult(const char *pkgName, int32_t pid, int32_t refreshId,
180     int32_t reason)
181 {
182     if (pkgName == nullptr) {
183         LNN_LOGE(LNN_EVENT, "pkgName is null");
184         return SOFTBUS_INVALID_PARAM;
185     }
186     sptr<BusCenterClientProxy> clientProxy = GetClientProxy(pkgName, pid);
187     if (clientProxy == nullptr) {
188         LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
189         return SOFTBUS_ERR;
190     }
191     clientProxy->OnRefreshLNNResult(refreshId, reason);
192     return SOFTBUS_OK;
193 }
194 
ClientOnRefreshDeviceFound(const char * pkgName,int32_t pid,const void * device,uint32_t deviceLen)195 int32_t ClientOnRefreshDeviceFound(
196     const char *pkgName, int32_t pid, const void *device, uint32_t deviceLen)
197 {
198     if (pkgName == nullptr) {
199         LNN_LOGE(LNN_EVENT, "pkgName is null");
200         return SOFTBUS_INVALID_PARAM;
201     }
202     sptr<BusCenterClientProxy> clientProxy = GetClientProxy(pkgName, pid);
203     if (clientProxy == nullptr) {
204         LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
205         return SOFTBUS_ERR;
206     }
207     clientProxy->OnRefreshDeviceFound(device, deviceLen);
208     return SOFTBUS_OK;
209 }
210 
ClientOnDataLevelChanged(const char * pkgName,int32_t pid,const char * networkId,const DataLevelInfo * dataLevelInfo)211 int32_t ClientOnDataLevelChanged(const char *pkgName, int32_t pid, const char *networkId,
212     const DataLevelInfo *dataLevelInfo)
213 {
214     if (pkgName == nullptr || networkId == nullptr || dataLevelInfo == nullptr) {
215         LNN_LOGE(LNN_EVENT, "param is invalid");
216         return SOFTBUS_INVALID_PARAM;
217     }
218 
219     sptr<BusCenterClientProxy> clientProxy = GetClientProxy(pkgName, pid);
220     if (clientProxy == nullptr) {
221         LNN_LOGE(LNN_EVENT, "bus center client proxy is nullptr");
222         return SOFTBUS_ERR;
223     }
224     clientProxy->OnDataLevelChanged(networkId, dataLevelInfo);
225     return SOFTBUS_OK;
226 }
227