1 /*
2  * Copyright (C) 2021-2022 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 "wifi_internal_event_dispatcher.h"
17 #include "wifi_logger.h"
18 #include "wifi_errcode.h"
19 #include "wifi_common_event_helper.h"
20 #include "wifi_common_util.h"
21 #include "wifi_auth_center.h"
22 #include "wifi_permission_utils.h"
23 #include "define.h"
24 #ifdef SUPPORT_RANDOM_MAC_ADDR
25 #include "wifi_p2p_msg.h"
26 #include "wifi_common_msg.h"
27 #include "wifi_config_center.h"
28 #include "wifi_settings.h"
29 #endif
30 
31 DEFINE_WIFILOG_LABEL("WifiInternalEventDispatcher");
32 
33 namespace OHOS {
34 namespace Wifi {
35 std::set<std::int32_t> g_CallbackEventChkSysAppList = {
36     WIFI_CBK_MSG_HOTSPOT_STATE_JOIN,
37     WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE,
38     WIFI_CBK_MSG_STREAM_DIRECTION };
39 
40 CallbackEventPermissionMap g_CallbackEventPermissionMap = {
41     { WIFI_CBK_MSG_STATE_CHANGE,
42         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
43         "ohos.permission.GET_WIFI_INFO") },
44     { WIFI_CBK_MSG_CONNECTION_CHANGE,
45         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
46         "ohos.permission.GET_WIFI_INFO") },
47     { WIFI_CBK_MSG_SCAN_STATE_CHANGE,
48         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
49         "ohos.permission.GET_WIFI_INFO") },
50     { WIFI_CBK_MSG_RSSI_CHANGE,
51         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
52         "ohos.permission.GET_WIFI_INFO") },
53     { WIFI_CBK_MSG_DEVICE_CONFIG_CHANGE,
54         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
55         "ohos.permission.GET_WIFI_INFO") },
56     { WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE,
57         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
58         "ohos.permission.GET_WIFI_INFO") },
59     { WIFI_CBK_MSG_HOTSPOT_STATE_JOIN,
60         std::make_pair(std::bind(WifiPermissionUtils::VerifyManageWifiHotspotPermission),
61         "ohos.permission.MANAGE_WIFI_HOTSPOT") },
62     { WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE,
63         std::make_pair(std::bind(WifiPermissionUtils::VerifyManageWifiHotspotPermission),
64         "ohos.permission.MANAGE_WIFI_HOTSPOT") },
65     { WIFI_CBK_MSG_P2P_STATE_CHANGE,
66         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
67         "ohos.permission.GET_WIFI_INFO") },
68     { WIFI_CBK_MSG_CONNECT_CHANGE,
69         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
70         "ohos.permission.GET_WIFI_INFO") },
71     { WIFI_CBK_MSG_THIS_DEVICE_CHANGE,
72         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
73         "ohos.permission.GET_WIFI_INFO") },
74     { WIFI_CBK_MSG_THIS_DEVICE_CHANGE,
75         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiDirectDevicePermission),
76             "ohos.permission.LOCATION") },
77     { WIFI_CBK_MSG_THIS_DEVICE_CHANGE,
78         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoInternalPermission),
79             "ohos.permission.GET_WIFI_INFO_INTERNAL") },
80     { WIFI_CBK_MSG_PERSISTENT_GROUPS_CHANGE,
81         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
82         "ohos.permission.GET_WIFI_INFO") },
83     { WIFI_CBK_MSG_PEER_CHANGE,
84         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
85         "ohos.permission.GET_WIFI_INFO") },
86 #ifndef SUPPORT_RANDOM_MAC_ADDR
87     { WIFI_CBK_MSG_PEER_CHANGE,
88         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiDirectDevicePermission),
89         "ohos.permission.LOCATION") },
90 #endif
91     { WIFI_CBK_MSG_PEER_CHANGE,
92         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoInternalPermission),
93         "ohos.permission.GET_WIFI_INFO_INTERNAL") },
94     { WIFI_CBK_MSG_DISCOVERY_CHANGE,
95         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
96         "ohos.permission.GET_WIFI_INFO") },
97     { WIFI_CBK_MSG_STREAM_DIRECTION,
98         std::make_pair(std::bind(WifiPermissionUtils::VerifyWifiConnectionPermission),
99         "ohos.permission.MANAGE_WIFI_CONNECTION") },
100     { WIFI_CBK_MSG_WPS_STATE_CHANGE,
101         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoPermission),
102         "ohos.permission.GET_WIFI_INFO") },
103     { WIFI_CBK_MSG_WPS_STATE_CHANGE,
104         std::make_pair(std::bind(WifiPermissionUtils::VerifyGetWifiInfoInternalPermission),
105         "ohos.permission.GET_WIFI_INFO_INTERNAL") },
106 };
107 
GetInstance()108 WifiInternalEventDispatcher &WifiInternalEventDispatcher::GetInstance()
109 {
110     static WifiInternalEventDispatcher gWifiEventBroadcast;
111     return gWifiEventBroadcast;
112 }
113 
WifiInternalEventDispatcher()114 WifiInternalEventDispatcher::WifiInternalEventDispatcher()
115 {}
116 
~WifiInternalEventDispatcher()117 WifiInternalEventDispatcher::~WifiInternalEventDispatcher()
118 {}
119 
Init()120 int WifiInternalEventDispatcher::Init()
121 {
122     /* first init system notify service client here ! */
123     mBroadcastThread = std::make_unique<WifiEventHandler>("InnerDisThread");
124     return 0;
125 }
126 
SendSystemNotifyMsg()127 int WifiInternalEventDispatcher::SendSystemNotifyMsg() /* parameters */
128 {
129     return 0;
130 }
131 
AddStaCallback(const sptr<IRemoteObject> & remote,const sptr<IWifiDeviceCallBack> & callback,int pid,const std::string & eventName,int tokenId,int instId)132 ErrCode WifiInternalEventDispatcher::AddStaCallback(
133     const sptr<IRemoteObject> &remote, const sptr<IWifiDeviceCallBack> &callback, int pid,
134     const std::string &eventName, int tokenId, int instId)
135 {
136     WIFI_LOGD("WifiInternalEventDispatcher::AddStaCallback, remote! instId: %{public}d", instId);
137     if (remote == nullptr || callback == nullptr) {
138         WIFI_LOGE("remote object is null!");
139         return WIFI_OPT_INVALID_PARAM;
140     }
141 
142     auto eventIter = g_staCallBackNameEventIdMap.find(eventName);
143     if (eventIter == g_staCallBackNameEventIdMap.end()) {
144         WIFI_LOGE("%{public}s, Not find callback event, eventName:%{public}s", __func__, eventName.c_str());
145         return WIFI_OPT_NOT_SUPPORTED;
146     }
147 
148     if (!VerifyRegisterCallbackPermission(eventIter->second)) {
149         WIFI_LOGE("%{public}s, VerifyRegisterCallbackPermission denied!", __func__);
150         return WIFI_OPT_PERMISSION_DENIED;
151     }
152 
153     std::unique_lock<std::mutex> lock(mStaCallbackMutex);
154     auto iter = mStaCallbacks.find(instId);
155     if (iter != mStaCallbacks.end()) {
156         (iter->second)[remote] = callback;
157         auto itr = mStaCallBackInfo[instId].find(remote);
158         if (itr != mStaCallBackInfo[instId].end()) {
159             (itr->second).regCallBackEventId.emplace(eventIter->second);
160             WIFI_LOGD("%{public}s, add callback event: %{public}d, instId: %{public}d", __func__, eventIter->second,
161                 instId);
162             return WIFI_OPT_SUCCESS;
163         } else {
164             WifiCallingInfo callbackInfo;
165             callbackInfo.callingUid = GetCallingUid();
166             callbackInfo.callingPid = pid;
167             callbackInfo.callingTokenId = tokenId;
168             callbackInfo.regCallBackEventId.emplace(eventIter->second);
169             mStaCallBackInfo[instId].insert({remote, callbackInfo});
170             WIFI_LOGD("%{public}s, add uid: %{public}d, pid: %{public}d, callback event:%{public}d,"
171                 "tokenId: %{private}d, instId: %{public}d",  __func__, callbackInfo.callingUid, callbackInfo.callingPid,
172                 eventIter->second, callbackInfo.callingTokenId, instId);
173             return WIFI_OPT_SUCCESS;
174         }
175     }
176 
177     StaCallbackMapType &staCallback = mStaCallbacks[instId];
178     staCallback[remote] = callback;
179     StaCallbackInfo &staCallbackInfo = mStaCallBackInfo[instId];
180     WifiCallingInfo callbackInfo;
181     callbackInfo.callingUid = GetCallingUid();
182     callbackInfo.callingPid = pid;
183     callbackInfo.callingTokenId = tokenId;
184     callbackInfo.regCallBackEventId.emplace(eventIter->second);
185     staCallbackInfo[remote] = callbackInfo;
186     return WIFI_OPT_SUCCESS;
187 }
188 
RemoveStaCallback(const sptr<IRemoteObject> & remote,int instId)189 int WifiInternalEventDispatcher::RemoveStaCallback(const sptr<IRemoteObject> &remote, int instId)
190 {
191     if (remote != nullptr) {
192         std::unique_lock<std::mutex> lock(mStaCallbackMutex);
193         auto iter = mStaCallbacks.find(instId);
194         if (iter != mStaCallbacks.end()) {
195             auto itr = iter->second.find(remote);
196             if (itr != iter->second.end()) {
197                 iter->second.erase(itr);
198                 mStaCallBackInfo[instId].erase(mStaCallBackInfo[instId].find(remote));
199                 WIFI_LOGD("WifiInternalEventDispatcher::RemoveStaCallback!");
200             }
201         }
202     }
203     return 0;
204 }
205 
SetSingleStaCallback(const sptr<IWifiDeviceCallBack> & callback,const std::string & eventName,int instId)206 int WifiInternalEventDispatcher::SetSingleStaCallback(const sptr<IWifiDeviceCallBack> &callback,
207     const std::string &eventName, int instId)
208 {
209     std::unique_lock<std::mutex> lock(mStaCallbackMutex);
210     mStaSingleCallback[instId] = callback;
211     return 0;
212 }
213 
GetSingleStaCallback(int instId) const214 sptr<IWifiDeviceCallBack> WifiInternalEventDispatcher::GetSingleStaCallback(int instId) const
215 {
216     auto iter = mStaSingleCallback.find(instId);
217     if (iter != mStaSingleCallback.end()) {
218         return iter->second;
219     }
220     return nullptr;
221 }
222 
HasStaRemote(const sptr<IRemoteObject> & remote,int instId)223 bool WifiInternalEventDispatcher::HasStaRemote(const sptr<IRemoteObject> &remote, int instId)
224 {
225     std::unique_lock<std::mutex> lock(mStaCallbackMutex);
226     if (remote != nullptr) {
227         auto iter = mStaCallbacks.find(instId);
228         if (iter != mStaCallbacks.end()) {
229             if (iter->second.find(remote) != iter->second.end()) {
230                 return true;
231             }
232         }
233     }
234     return false;
235 }
236 
AddScanCallback(const sptr<IRemoteObject> & remote,const sptr<IWifiScanCallback> & callback,int pid,const std::string & eventName,int tokenId,int instId)237 ErrCode WifiInternalEventDispatcher::AddScanCallback(
238     const sptr<IRemoteObject> &remote, const sptr<IWifiScanCallback> &callback, int pid,
239     const std::string &eventName, int tokenId, int instId)
240 {
241     WIFI_LOGD("WifiInternalEventDispatcher::AddScanCallback! instId: %{public}d", instId);
242     if (remote == nullptr || callback == nullptr) {
243         WIFI_LOGE("remote object is null!");
244         return WIFI_OPT_INVALID_PARAM;
245     }
246 
247     auto eventIter = g_staCallBackNameEventIdMap.find(eventName);
248     if (eventIter == g_staCallBackNameEventIdMap.end()) {
249         WIFI_LOGE("%{public}s, Not find callback event, eventName:%{public}s", __func__, eventName.c_str());
250         return WIFI_OPT_NOT_SUPPORTED;
251     }
252 
253     if (!VerifyRegisterCallbackPermission(eventIter->second)) {
254         WIFI_LOGE("%{public}s, VerifyRegisterCallbackPermission denied!", __func__);
255         return WIFI_OPT_PERMISSION_DENIED;
256     }
257 
258     std::unique_lock<std::mutex> lock(mScanCallbackMutex);
259     auto iter = mScanCallbacks.find(instId);
260     if (iter != mScanCallbacks.end()) {
261         (iter->second)[remote] = callback;
262         auto itr = mScanCallBackInfo[instId].find(remote);
263         if (itr != mScanCallBackInfo[instId].end()) {
264             (itr->second).regCallBackEventId.emplace(eventIter->second);
265             WIFI_LOGD("%{public}s, add callback event: %{public}d, instId: %{public}d", __func__, eventIter->second,
266                 instId);
267             return WIFI_OPT_SUCCESS;
268         } else {
269             WifiCallingInfo callbackInfo;
270             callbackInfo.callingUid = GetCallingUid();
271             callbackInfo.callingPid = pid;
272             callbackInfo.callingTokenId = tokenId;
273             callbackInfo.regCallBackEventId.emplace(eventIter->second);
274             mScanCallBackInfo[instId].insert({remote, callbackInfo});
275             WIFI_LOGD("%{public}s, add uid: %{public}d, pid: %{public}d, callback event:%{public}d,"
276                 "tokenId: %{private}d, instId: %{public}d",  __func__, callbackInfo.callingUid, callbackInfo.callingPid,
277                 eventIter->second, callbackInfo.callingTokenId, instId);
278             return WIFI_OPT_SUCCESS;
279         }
280     }
281 
282     ScanCallbackMapType &scanCallback = mScanCallbacks[instId];
283     scanCallback[remote] = callback;
284     ScanCallbackInfo &scanCallbackInfo = mScanCallBackInfo[instId];
285     WifiCallingInfo callbackInfo;
286     callbackInfo.callingUid = GetCallingUid();
287     callbackInfo.callingPid = pid;
288     callbackInfo.callingTokenId = tokenId;
289     callbackInfo.regCallBackEventId.emplace(eventIter->second);
290     scanCallbackInfo[remote] = callbackInfo;
291     return WIFI_OPT_SUCCESS;
292 }
293 
RemoveScanCallback(const sptr<IRemoteObject> & remote,int instId)294 int WifiInternalEventDispatcher::RemoveScanCallback(const sptr<IRemoteObject> &remote, int instId)
295 {
296     if (remote != nullptr) {
297         std::unique_lock<std::mutex> lock(mScanCallbackMutex);
298         auto iter = mScanCallbacks.find(instId);
299         if (iter != mScanCallbacks.end()) {
300             auto itr = iter->second.find(remote);
301             if (itr != iter->second.end()) {
302                 iter->second.erase(itr);
303                 mScanCallBackInfo[instId].erase(mScanCallBackInfo[instId].find(remote));
304                 WIFI_LOGD("WifiInternalEventDispatcher::RemoveScanCallback!");
305             }
306         }
307     }
308     return 0;
309 }
310 
SetSingleScanCallback(const sptr<IWifiScanCallback> & callback,const std::string & eventName,int instId)311 int WifiInternalEventDispatcher::SetSingleScanCallback(const sptr<IWifiScanCallback> &callback,
312     const std::string &eventName, int instId)
313 {
314     std::unique_lock<std::mutex> lock(mScanCallbackMutex);
315     mScanSingleCallback[instId] = callback;
316     return 0;
317 }
318 
GetSingleScanCallback(int instId) const319 sptr<IWifiScanCallback> WifiInternalEventDispatcher::GetSingleScanCallback(int instId) const
320 {
321     auto iter = mScanSingleCallback.find(instId);
322     if (iter != mScanSingleCallback.end()) {
323         return iter->second;
324     }
325     return nullptr;
326 }
327 
HasScanRemote(const sptr<IRemoteObject> & remote,int instId)328 bool WifiInternalEventDispatcher::HasScanRemote(const sptr<IRemoteObject> &remote, int instId)
329 {
330     std::unique_lock<std::mutex> lock(mScanCallbackMutex);
331     if (remote != nullptr) {
332         auto iter = mScanCallbacks.find(instId);
333         if (iter != mScanCallbacks.end()) {
334             if (iter->second.find(remote) != iter->second.end()) {
335                 return true;
336             }
337         }
338     }
339     return false;
340 }
341 
AddHotspotCallback(const sptr<IRemoteObject> & remote,const sptr<IWifiHotspotCallback> & callback,const std::string & eventName,int id)342 ErrCode WifiInternalEventDispatcher::AddHotspotCallback(
343     const sptr<IRemoteObject> &remote, const sptr<IWifiHotspotCallback> &callback, const std::string &eventName, int id)
344 {
345     WIFI_LOGD("WifiInternalEventDispatcher::AddHotspotCallback, id:%{public}d", id);
346     if (remote == nullptr || callback == nullptr) {
347         WIFI_LOGE("remote object is null!");
348         return WIFI_OPT_INVALID_PARAM;
349     }
350 
351     auto eventIter = g_apCallBackNameEventIdMap.find(eventName);
352     if (eventIter == g_apCallBackNameEventIdMap.end()) {
353         WIFI_LOGE("%{public}s, Not find callback event, eventName:%{public}s", __func__, eventName.c_str());
354         return WIFI_OPT_NOT_SUPPORTED;
355     }
356 
357     if (!VerifyRegisterCallbackPermission(eventIter->second)) {
358         WIFI_LOGE("%{public}s, VerifyRegisterCallbackPermission denied!", __func__);
359         return WIFI_OPT_PERMISSION_DENIED;
360     }
361 
362     std::unique_lock<std::mutex> lock(mHotspotCallbackMutex);
363     auto iter = mHotspotCallbacks.find(id);
364     if (iter != mHotspotCallbacks.end()) {
365         (iter->second)[remote] = callback;
366         auto itr = mHotspotCallbackInfo[id].find(remote);
367         if (itr != mHotspotCallbackInfo[id].end()) {
368             (itr->second).emplace(eventIter->second);
369             WIFI_LOGI("%{public}s, add callback event:%{public}d, id:%{public}d", __func__, eventIter->second, id);
370             return WIFI_OPT_SUCCESS;
371         }
372         mHotspotCallbackInfo[id].insert({remote, {eventIter->second}});
373         WIFI_LOGI("%{public}s, add new callback event:%{public}d, id:%{public}d", __func__, eventIter->second, id);
374         return WIFI_OPT_SUCCESS;
375     }
376 
377     HotspotCallbackMapType &hotspotCallback = mHotspotCallbacks[id];
378     hotspotCallback[remote] = callback;
379     HotspotCallbackInfo &hotspotCallbackInfo = mHotspotCallbackInfo[id];
380     hotspotCallbackInfo[remote] = {eventIter->second};
381     WIFI_LOGI("%{public}s, add ap callback event:%{public}d, id:%{public}d", __func__, eventIter->second, id);
382     return WIFI_OPT_SUCCESS;
383 }
384 
RemoveHotspotCallback(const sptr<IRemoteObject> & remote,int id)385 int WifiInternalEventDispatcher::RemoveHotspotCallback(const sptr<IRemoteObject> &remote, int id)
386 {
387     if (remote != nullptr) {
388         std::unique_lock<std::mutex> lock(mHotspotCallbackMutex);
389         auto iter = mHotspotCallbacks.find(id);
390         if (iter != mHotspotCallbacks.end()) {
391             auto item = iter->second.find(remote);
392             if (item != iter->second.end()) {
393                 iter->second.erase(item);
394                 mHotspotCallbackInfo[id].erase(mHotspotCallbackInfo[id].find(remote));
395                 WIFI_LOGD("hotspot is is %{public}d WifiInternalEventDispatcher::RemoveHotspotCallback!", id);
396             }
397         }
398     }
399     return 0;
400 }
401 
SetSingleHotspotCallback(const sptr<IWifiHotspotCallback> & callback,int id)402 int WifiInternalEventDispatcher::SetSingleHotspotCallback(const sptr<IWifiHotspotCallback> &callback, int id)
403 {
404     std::unique_lock<std::mutex> lock(mHotspotCallbackMutex);
405     mHotspotSingleCallback[id] = callback;
406     return 0;
407 }
408 
GetSingleHotspotCallback(int id) const409 sptr<IWifiHotspotCallback> WifiInternalEventDispatcher::GetSingleHotspotCallback(int id) const
410 {
411     auto iter = mHotspotSingleCallback.find(id);
412     if (iter != mHotspotSingleCallback.end()) {
413         return iter->second;
414     }
415     return nullptr;
416 }
417 
HasHotspotRemote(const sptr<IRemoteObject> & remote,int id)418 bool WifiInternalEventDispatcher::HasHotspotRemote(const sptr<IRemoteObject> &remote, int id)
419 {
420     if (remote != nullptr) {
421         std::unique_lock<std::mutex> lock(mHotspotCallbackMutex);
422         auto iter = mHotspotCallbacks.find(id);
423         if (iter != mHotspotCallbacks.end()) {
424             if (iter->second.find(remote) != iter->second.end()) {
425                 return true;
426             }
427         }
428     }
429     return false;
430 }
431 
SetSingleP2pCallback(const sptr<IWifiP2pCallback> & callback)432 int WifiInternalEventDispatcher::SetSingleP2pCallback(const sptr<IWifiP2pCallback> &callback)
433 {
434     std::unique_lock<std::mutex> lock(mP2pCallbackMutex);
435     mP2pSingleCallback = callback;
436     return 0;
437 }
438 
GetSingleP2pCallback() const439 sptr<IWifiP2pCallback> WifiInternalEventDispatcher::GetSingleP2pCallback() const
440 {
441     return mP2pSingleCallback;
442 }
443 
HasP2pRemote(const sptr<IRemoteObject> & remote)444 bool WifiInternalEventDispatcher::HasP2pRemote(const sptr<IRemoteObject> &remote)
445 {
446     std::unique_lock<std::mutex> lock(mP2pCallbackMutex);
447     if (remote != nullptr) {
448         if (mP2pCallbacks.find(remote) != mP2pCallbacks.end()) {
449             return true;
450         }
451     }
452     return false;
453 }
454 
AddP2pCallback(const sptr<IRemoteObject> & remote,const sptr<IWifiP2pCallback> & callback,int pid,const std::string & eventName,int tokenId)455 ErrCode WifiInternalEventDispatcher::AddP2pCallback(
456     const sptr<IRemoteObject> &remote, const sptr<IWifiP2pCallback> &callback, int pid,
457     const std::string &eventName, int tokenId)
458 {
459     WIFI_LOGD("WifiInternalEventDispatcher::AddP2pCallback!");
460     if (remote == nullptr || callback == nullptr) {
461         WIFI_LOGE("remote object is null!");
462         return WIFI_OPT_INVALID_PARAM;
463     }
464 
465     auto eventIter = g_p2pCallBackNameEventIdMap.find(eventName);
466     if (eventIter == g_p2pCallBackNameEventIdMap.end()) {
467         WIFI_LOGE("%{public}s, Not find callback event, eventName:%{public}s", __func__, eventName.c_str());
468         return WIFI_OPT_NOT_SUPPORTED;
469     }
470 
471     if (!VerifyRegisterCallbackPermission(eventIter->second)) {
472         WIFI_LOGE("%{public}s, VerifyRegisterCallbackPermission denied!", __func__);
473         return WIFI_OPT_PERMISSION_DENIED;
474     }
475 
476     std::unique_lock<std::mutex> lock(mP2pCallbackMutex);
477     auto iter = mP2pCallbackInfo.find(remote);
478     if (iter != mP2pCallbackInfo.end()) {
479         (iter->second).regCallBackEventId.emplace(eventIter->second);
480     } else {
481         WifiCallingInfo &callbackInfo = mP2pCallbackInfo[remote];
482         callbackInfo.callingUid = GetCallingUid();
483         callbackInfo.callingPid = pid;
484         callbackInfo.callingTokenId = tokenId;
485         callbackInfo.regCallBackEventId.emplace(eventIter->second);
486         WIFI_LOGI("%{public}s, add uid: %{public}d, pid: %{public}d, callback event: %{public}d, tokenId: %{private}d",
487             __func__, callbackInfo.callingUid, callbackInfo.callingPid,
488             eventIter->second, callbackInfo.callingTokenId);
489     }
490     mP2pCallbacks[remote] = callback;
491     WIFI_LOGI("%{public}s, add p2p callback event:%{public}d", __func__, eventIter->second);
492     return WIFI_OPT_SUCCESS;
493 }
494 
RemoveP2pCallback(const sptr<IRemoteObject> & remote)495 int WifiInternalEventDispatcher::RemoveP2pCallback(const sptr<IRemoteObject> &remote)
496 {
497     if (remote != nullptr) {
498         std::unique_lock<std::mutex> lock(mP2pCallbackMutex);
499         auto iter = mP2pCallbacks.find(remote);
500         if (iter != mP2pCallbacks.end()) {
501             mP2pCallbacks.erase(iter);
502             mP2pCallbackInfo.erase(mP2pCallbackInfo.find(remote));
503             WIFI_LOGD("WifiInternalEventDispatcher::RemoveP2pCallback!");
504         }
505     }
506     return 0;
507 }
508 
Run(WifiInternalEventDispatcher & instance,const WifiEventCallbackMsg & msg)509 void WifiInternalEventDispatcher::Run(WifiInternalEventDispatcher &instance, const WifiEventCallbackMsg &msg)
510 {
511     WIFI_LOGD("WifiInternalEventDispatcher::Run broad cast a msg %{public}d", msg.msgCode);
512     if (msg.msgCode >= WIFI_CBK_MSG_STATE_CHANGE && msg.msgCode <= WIFI_CBK_MSG_MAX_INVALID_STA) {
513         DealStaCallbackMsg(instance, msg);
514     } else if (msg.msgCode == WIFI_CBK_MSG_SCAN_STATE_CHANGE) {
515         DealScanCallbackMsg(instance, msg);
516     } else if (msg.msgCode >= WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE &&
517                msg.msgCode <= WIFI_CBK_MSG_MAX_INVALID_HOTSPOT) {
518         DealHotspotCallbackMsg(instance, msg);
519     } else if (msg.msgCode >= WIFI_CBK_MSG_P2P_STATE_CHANGE && msg.msgCode <= WIFI_CBK_MSG_MAX_INVALID_P2P) {
520         DealP2pCallbackMsg(instance, msg);
521     } else {
522         WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
523     }
524     return;
525 }
526 
AddBroadCastMsg(const WifiEventCallbackMsg & msg)527 int WifiInternalEventDispatcher::AddBroadCastMsg(const WifiEventCallbackMsg &msg)
528 {
529     WIFI_LOGD("WifiInternalEventDispatcher::AddBroadCastMsg, msgcode %{public}d", msg.msgCode);
530     if (!mBroadcastThread) {
531         return 0;
532     }
533     std::function<void()> func = std::bind([this, msg]() {
534         Run(std::ref(*this), msg);
535     });
536     int delayTime = 0;
537     bool result = mBroadcastThread->PostAsyncTask(func, delayTime);
538     if (!result) {
539         WIFI_LOGF("WifiInternalEventDispatcher::AddBroadCastMsg failed %{public}d", msg.msgCode);
540         return -1;
541     }
542     return 0;
543 }
544 
Exit()545 void WifiInternalEventDispatcher::Exit()
546 {
547     if (mBroadcastThread) {
548         mBroadcastThread.reset();
549     }
550 }
551 
PublishStaEvent(const WifiEventCallbackMsg & msg)552 void WifiInternalEventDispatcher::PublishStaEvent(const WifiEventCallbackMsg &msg)
553 {
554     switch (msg.msgCode) {
555         case WIFI_CBK_MSG_STATE_CHANGE:
556             WifiInternalEventDispatcher::PublishWifiStateChangedEvent(msg.msgData, msg.id);
557             break;
558         case WIFI_CBK_MSG_CONNECTION_CHANGE:
559             WifiInternalEventDispatcher::PublishConnStateChangedEvent(msg.msgData, msg.id, msg.linkInfo);
560             break;
561         case WIFI_CBK_MSG_RSSI_CHANGE:
562             WifiInternalEventDispatcher::PublishRssiValueChangedEvent(msg.msgData, msg.id);
563             break;
564         case WIFI_CBK_MSG_STREAM_DIRECTION:
565             break;
566         case WIFI_CBK_MSG_WPS_STATE_CHANGE:
567             break;
568         case WIFI_CBK_MSG_SEMI_STATE_CHANGE:
569             WifiCommonEventHelper::PublishWifiSemiStateChangedEvent(msg.msgData, "OnWifiSemiStateChanged");
570             break;
571         default:
572             break;
573     }
574 }
575 
DealStaCallbackMsg(WifiInternalEventDispatcher & instance,const WifiEventCallbackMsg & msg)576 void WifiInternalEventDispatcher::DealStaCallbackMsg(
577     WifiInternalEventDispatcher &instance, const WifiEventCallbackMsg &msg)
578 {
579     WIFI_LOGD("Deal Sta Event Callback Msg: %{public}d", msg.msgCode);
580 
581     PublishStaEvent(msg);
582 
583     auto callback = instance.GetSingleStaCallback(msg.id);
584     if (callback != nullptr) {
585         WIFI_LOGI("Single Callback Msg: %{public}d", msg.msgCode);
586         switch (msg.msgCode) {
587             case WIFI_CBK_MSG_STATE_CHANGE:
588                 callback->OnWifiStateChanged(msg.msgData);
589                 break;
590             case WIFI_CBK_MSG_CONNECTION_CHANGE:
591                 callback->OnWifiConnectionChanged(msg.msgData, msg.linkInfo);
592                 break;
593             case WIFI_CBK_MSG_RSSI_CHANGE:
594                 callback->OnWifiRssiChanged(msg.msgData);
595                 break;
596             case WIFI_CBK_MSG_STREAM_DIRECTION:
597                 callback->OnStreamChanged(msg.msgData);
598                 break;
599             case WIFI_CBK_MSG_WPS_STATE_CHANGE:
600                 callback->OnWifiWpsStateChanged(msg.msgData, msg.pinCode);
601                 break;
602             case WIFI_CBK_MSG_DEVICE_CONFIG_CHANGE:
603                 callback->OnDeviceConfigChanged(ConfigChange(msg.msgData));
604                 break;
605             default:
606                 WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
607                 break;
608         }
609     }
610     instance.InvokeDeviceCallbacks(msg);
611     return;
612 }
613 
DealScanCallbackMsg(WifiInternalEventDispatcher & instance,const WifiEventCallbackMsg & msg)614 void WifiInternalEventDispatcher::DealScanCallbackMsg(
615     WifiInternalEventDispatcher &instance, const WifiEventCallbackMsg &msg)
616 {
617     WIFI_LOGD("WifiInternalEventDispatcher:: Deal Scan Event Callback Msg: %{public}d", msg.msgCode);
618 
619     switch (msg.msgCode) {
620         case WIFI_CBK_MSG_SCAN_STATE_CHANGE:
621             WifiCommonEventHelper::PublishScanStateChangedEvent(msg.msgData, "OnScanStateChanged");
622             break;
623         default:
624             WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
625             break;
626     }
627 
628     auto callback = instance.GetSingleScanCallback(msg.id);
629     if (callback != nullptr) {
630         switch (msg.msgCode) {
631             case WIFI_CBK_MSG_SCAN_STATE_CHANGE:
632                 callback->OnWifiScanStateChanged(msg.msgData);
633                 break;
634             default:
635                 break;
636         }
637     }
638     instance.InvokeScanCallbacks(msg);
639     return;
640 }
641 
InvokeScanCallbacks(const WifiEventCallbackMsg & msg)642 void WifiInternalEventDispatcher::InvokeScanCallbacks(const WifiEventCallbackMsg &msg)
643 {
644     std::unique_lock<std::mutex> lock(mScanCallbackMutex);
645     auto iter = mScanCallbacks.find(msg.id);
646     if (iter != mScanCallbacks.end()) {
647         ScanCallbackMapType callbacks = iter->second;
648         ScanCallbackMapType::iterator itr;
649         for (itr = callbacks.begin(); itr != callbacks.end(); itr++) {
650             auto callback = itr->second;
651             if (callback == nullptr) {
652                 continue;
653             }
654             WIFI_LOGD("InvokeScanCallbacks, msg.msgCode: %{public}d, instId: %{public}d", msg.msgCode, msg.id);
655             auto remote = itr->first;
656             bool isFrozen = false;
657 #ifdef FEATURE_APP_FROZEN
658             int uid = mScanCallBackInfo[msg.id][remote].callingUid;
659             int pid = mScanCallBackInfo[msg.id][remote].callingPid;
660             isFrozen = IsAppFrozen(pid);
661             WIFI_LOGD("APP is hardwareProxied, uid: %{public}d, pid: %{public}d, hardwareProxied: %{public}d",
662                 uid, pid, isFrozen);
663 #endif
664             if (mScanCallBackInfo[msg.id][remote].regCallBackEventId.count(msg.msgCode) == 0) {
665                 WIFI_LOGD("Not registered callback event! msg.msgCode: %{public}d,"
666                     "instId: %{public}d", msg.msgCode, msg.id);
667                 continue;
668             }
669 
670             switch (msg.msgCode) {
671                 case WIFI_CBK_MSG_SCAN_STATE_CHANGE:
672                     if (isFrozen == false) {
673                         callback->OnWifiScanStateChanged(msg.msgData);
674                     }
675                     break;
676                 default:
677                     WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
678                     break;
679             }
680         }
681     }
682 }
683 
InvokeDeviceCallbacks(const WifiEventCallbackMsg & msg)684 void WifiInternalEventDispatcher::InvokeDeviceCallbacks(
685     const WifiEventCallbackMsg &msg) __attribute__((no_sanitize("cfi")))
686 {
687     std::unique_lock<std::mutex> lock(mStaCallbackMutex);
688     auto iter = mStaCallbacks.find(msg.id);
689     if (iter != mStaCallbacks.end()) {
690         StaCallbackMapType callbacks = iter->second;
691         StaCallbackMapType::iterator itr;
692         for (itr = callbacks.begin(); itr != callbacks.end(); itr++) {
693             auto callback = itr->second;
694             if (callback == nullptr) {
695                 continue;
696             }
697             WIFI_LOGD("InvokeDeviceCallbacks, msg.msgCode: %{public}d, instId: %{public}d", msg.msgCode, msg.id);
698             auto remote = itr->first;
699             bool isFrozen = false;
700 #ifdef FEATURE_APP_FROZEN
701             int uid = mStaCallBackInfo[msg.id][remote].callingUid;
702             int pid = mStaCallBackInfo[msg.id][remote].callingPid;
703             isFrozen = IsAppFrozen(pid);
704             WIFI_LOGD("Check calling APP is hardwareProxied,"
705                 "uid: %{public}d, pid: %{public}d, hardwareProxied: %{public}d", uid, pid, isFrozen);
706 #endif
707             if (mStaCallBackInfo[msg.id][remote].regCallBackEventId.count(msg.msgCode) == 0) {
708                 WIFI_LOGD("InvokeDeviceCallbacks, Not registered callback event! msg.msgCode: %{public}d,"
709                     "instId: %{public}d", msg.msgCode, msg.id);
710                 continue;
711             }
712 
713             switch (msg.msgCode) {
714                 case WIFI_CBK_MSG_STATE_CHANGE:
715                     callback->OnWifiStateChanged(msg.msgData);
716                     break;
717                 case WIFI_CBK_MSG_CONNECTION_CHANGE:
718                     callback->OnWifiConnectionChanged(msg.msgData, msg.linkInfo);
719                     break;
720                 case WIFI_CBK_MSG_RSSI_CHANGE:
721                     if (isFrozen == false) {
722                         callback->OnWifiRssiChanged(msg.msgData);
723                     }
724                     break;
725                 case WIFI_CBK_MSG_STREAM_DIRECTION:
726                     if (isFrozen == false) {
727                         callback->OnStreamChanged(msg.msgData);
728                     }
729                     break;
730                 case WIFI_CBK_MSG_WPS_STATE_CHANGE:
731                     callback->OnWifiWpsStateChanged(msg.msgData, msg.pinCode);
732                     break;
733                 case WIFI_CBK_MSG_DEVICE_CONFIG_CHANGE:
734                     callback->OnDeviceConfigChanged(ConfigChange(msg.msgData));
735                     break;
736                 default:
737                     WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
738                     break;
739             }
740         }
741     }
742 }
743 
InvokeHotspotCallbacks(const WifiEventCallbackMsg & msg)744 void WifiInternalEventDispatcher::InvokeHotspotCallbacks(const WifiEventCallbackMsg &msg)
745 {
746     std::unique_lock<std::mutex> lock(mHotspotCallbackMutex);
747     auto iter = mHotspotCallbacks.find(msg.id);
748     if (iter != mHotspotCallbacks.end()) {
749         HotspotCallbackMapType callbacks = iter->second;
750         HotspotCallbackMapType::iterator itr;
751         for (itr = callbacks.begin(); itr != callbacks.end(); itr++) {
752             auto callback = itr->second;
753             if (callback == nullptr) {
754                 continue;
755             }
756             auto remote = itr->first;
757             if (mHotspotCallbackInfo[msg.id][remote].count(msg.msgCode) == 0) {
758                 WIFI_LOGI("InvokeHotspotCallbacks, Not registered callback event! msg.msgCode:%{public}d", msg.msgCode);
759                 continue;
760             }
761             switch (msg.msgCode) {
762                 case WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE:
763                     callback->OnHotspotStateChanged(msg.msgData);
764                     break;
765                 case WIFI_CBK_MSG_HOTSPOT_STATE_JOIN:
766                     callback->OnHotspotStaJoin(msg.staInfo);
767                     break;
768                 case WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE:
769                     callback->OnHotspotStaLeave(msg.staInfo);
770                     break;
771                 default:
772                     WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
773                     break;
774             }
775         }
776     }
777 }
778 
DealHotspotCallbackMsg(WifiInternalEventDispatcher & instance,const WifiEventCallbackMsg & msg)779 void WifiInternalEventDispatcher::DealHotspotCallbackMsg(
780     WifiInternalEventDispatcher &instance, const WifiEventCallbackMsg &msg)
781 {
782     WIFI_LOGI("Deal Hotspot Event Callback Msg: %{public}d", msg.msgCode);
783     auto callback = instance.GetSingleHotspotCallback(msg.id);
784     if (callback != nullptr) {
785         switch (msg.msgCode) {
786             case WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE:
787                 callback->OnHotspotStateChanged(msg.msgData);
788                 break;
789             case WIFI_CBK_MSG_HOTSPOT_STATE_JOIN:
790                 callback->OnHotspotStaJoin(msg.staInfo);
791                 break;
792             case WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE:
793                 callback->OnHotspotStaLeave(msg.staInfo);
794                 break;
795             default:
796                 WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
797                 break;
798         }
799     }
800     instance.InvokeHotspotCallbacks(msg);
801     return;
802 }
803 
InvokeP2pCallbacks(const WifiEventCallbackMsg & msg)804 void WifiInternalEventDispatcher::InvokeP2pCallbacks(const WifiEventCallbackMsg &msg)
805 {
806     std::unique_lock<std::mutex> lock(mP2pCallbackMutex);
807     P2pCallbackMapType callbacks = mP2pCallbacks;
808     P2pCallbackMapType::iterator itr;
809     for (itr = callbacks.begin(); itr != callbacks.end(); itr++) {
810         auto callback = itr->second;
811         auto remote = itr->first;
812         if (mP2pCallbackInfo[remote].regCallBackEventId.count(msg.msgCode) == 0) {
813             WIFI_LOGI("InvokeP2pCallbacks, Not registered callback event! msg.msgCode:%{public}d", msg.msgCode);
814             continue;
815         }
816         int pid = mP2pCallbackInfo[remote].callingPid;
817         int uid = mP2pCallbackInfo[remote].callingUid;
818         int tokenId = mP2pCallbackInfo[remote].callingTokenId;
819         if (callback != nullptr) {
820             SendP2pCallbackMsg(callback, msg, pid, uid, tokenId);
821         }
822     }
823 }
824 
SendConfigChangeEvent(sptr<IWifiP2pCallback> & callback,CfgInfo * cfgInfo)825 void WifiInternalEventDispatcher::SendConfigChangeEvent(sptr<IWifiP2pCallback> &callback,  CfgInfo* cfgInfo)
826 {
827     if (cfgInfo == nullptr) {
828         WIFI_LOGE("cfgInfo is nullptr");
829         return;
830     }
831     callback->OnConfigChanged(cfgInfo->type, cfgInfo->data, cfgInfo->dataLen);
832     if (cfgInfo->data != nullptr) {
833         delete[] cfgInfo->data;
834         cfgInfo->data = nullptr;
835     }
836     delete cfgInfo;
837     cfgInfo = nullptr;
838 }
839 
840 #ifdef SUPPORT_RANDOM_MAC_ADDR
updateP2pDeviceMacAddress(std::vector<WifiP2pDevice> & device)841 void WifiInternalEventDispatcher::updateP2pDeviceMacAddress(std::vector<WifiP2pDevice> &device)
842 {
843     for (auto iter = device.begin(); iter != device.end(); ++iter) {
844         WifiMacAddrInfo macAddrInfo;
845         macAddrInfo.bssid = iter->GetDeviceAddress();
846         macAddrInfo.bssidType = iter->GetDeviceAddressType();
847         std::string randomMacAddr =
848             WifiConfigCenter::GetInstance().GetMacAddrPairs(WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO, macAddrInfo);
849         if (randomMacAddr.empty()) {
850             WIFI_LOGW("%{public}s: no record found, bssid:%{private}s, bssidType:%{public}d",
851                 __func__, macAddrInfo.bssid.c_str(), macAddrInfo.bssidType);
852         } else {
853             WIFI_LOGD("%{public}s: find the record, bssid:%{private}s, bssidType:%{public}d, randomMac:%{private}s",
854                 __func__, iter->GetDeviceAddress().c_str(), iter->GetDeviceAddressType(), randomMacAddr.c_str());
855             if (iter->GetDeviceAddressType() == REAL_DEVICE_ADDRESS) {
856                 iter->SetDeviceAddress(randomMacAddr);
857                 iter->SetDeviceAddressType(RANDOM_DEVICE_ADDRESS);
858                 WIFI_LOGD("%{public}s: the record is updated, bssid:%{private}s, bssidType:%{public}d",
859                     __func__, iter->GetDeviceAddress().c_str(), iter->GetDeviceAddressType());
860             }
861         }
862     }
863 }
864 #endif
865 
SendP2pCallbackMsg(sptr<IWifiP2pCallback> & callback,const WifiEventCallbackMsg & msg,int pid,int uid,int tokenId)866 void WifiInternalEventDispatcher::SendP2pCallbackMsg(sptr<IWifiP2pCallback> &callback, const WifiEventCallbackMsg &msg,
867     int pid, int uid, int tokenId)
868 {
869     if (callback == nullptr) {
870         WIFI_LOGE("%{public}s: callback is null", __func__);
871         return;
872     }
873 
874     switch (msg.msgCode) {
875         case WIFI_CBK_MSG_P2P_STATE_CHANGE:
876             callback->OnP2pStateChanged(msg.msgData);
877             break;
878         case WIFI_CBK_MSG_PERSISTENT_GROUPS_CHANGE:
879             callback->OnP2pPersistentGroupsChanged();
880             break;
881         case WIFI_CBK_MSG_THIS_DEVICE_CHANGE:
882             callback->OnP2pThisDeviceChanged(msg.p2pDevice);
883             break;
884         case WIFI_CBK_MSG_PEER_CHANGE:
885             {
886                 WIFI_LOGD("%{public}s pid: %{public}d, uid: %{public}d, tokenId: %{private}d",
887                     __func__, pid, uid, tokenId);
888             #ifdef SUPPORT_RANDOM_MAC_ADDR
889                 if ((pid != 0) && (uid != 0)) {
890                     std::vector<WifiP2pDevice> deviceVec = msg.device;
891                     if (WifiPermissionUtils::VerifyGetWifiPeersMacPermissionEx(pid, uid, tokenId) == PERMISSION_DENIED) {
892                         WIFI_LOGD("%{public}s: GET_WIFI_PEERS_MAC PERMISSION_DENIED, pid: %{public}d, uid: %{public}d",
893                             __func__, pid, uid);
894                         updateP2pDeviceMacAddress(deviceVec);
895                     }
896                     callback->OnP2pPeersChanged(deviceVec);
897                 }
898             #else
899                 callback->OnP2pPeersChanged(msg.device);
900             #endif
901                 break;
902             }
903         case WIFI_CBK_MSG_SERVICE_CHANGE:
904             callback->OnP2pServicesChanged(msg.serviceInfo);
905             break;
906         case WIFI_CBK_MSG_CONNECT_CHANGE:
907             callback->OnP2pConnectionChanged(msg.p2pInfo);
908             break;
909         case WIFI_CBK_MSG_DISCOVERY_CHANGE:
910             callback->OnP2pDiscoveryChanged(msg.msgData);
911             break;
912         case WIFI_CBK_MSG_P2P_ACTION_RESULT:
913             callback->OnP2pActionResult(msg.p2pAction, static_cast<ErrCode>(msg.msgData));
914             break;
915         case WIFI_CBK_MSG_P2P_GC_JOIN_GROUP:
916             callback->OnP2pGcJoinGroup(msg.gcInfo);
917             break;
918         case WIFI_CBK_MSG_P2P_GC_LEAVE_GROUP:
919             callback->OnP2pGcLeaveGroup(msg.gcInfo);
920             break;
921         case WIFI_CBK_MSG_CFG_CHANGE:
922             SendConfigChangeEvent(callback, msg.cfgInfo);
923             break;
924         case WIFI_CBK_MSG_PRIVATE_PEER_CHANGE:
925             callback->OnP2pPrivatePeersChanged(msg.privateWfdInfo);
926             break;
927         default:
928             WIFI_LOGI("UnKnown msgcode %{public}d", msg.msgCode);
929             break;
930     }
931     return;
932 }
933 
DealP2pCallbackMsg(WifiInternalEventDispatcher & instance,const WifiEventCallbackMsg & msg)934 void WifiInternalEventDispatcher::DealP2pCallbackMsg(
935     WifiInternalEventDispatcher &instance, const WifiEventCallbackMsg &msg)
936 {
937     WIFI_LOGI("Deal P2P Event Callback Msg: %{public}d", msg.msgCode);
938 
939     auto callback = instance.GetSingleP2pCallback();
940     if (callback != nullptr) {
941         SendP2pCallbackMsg(callback, msg, 0, 0, 0);
942     }
943     instance.InvokeP2pCallbacks(msg);
944     return;
945 }
946 
PublishConnStateChangedEvent(int state,int instId,const WifiLinkedInfo & info)947 void WifiInternalEventDispatcher::PublishConnStateChangedEvent(int state, int instId, const WifiLinkedInfo &info)
948 {
949     std::string eventData = "Other";
950     switch (state) {
951         case int(OHOS::Wifi::ConnState::CONNECTING):
952             eventData = "Connecting";
953             break;
954         case int(OHOS::Wifi::ConnState::CONNECTED):
955             eventData = "ApConnected";
956             break;
957         case int(OHOS::Wifi::ConnState::DISCONNECTING):
958             eventData = "Disconnecting";
959             break;
960         case int(OHOS::Wifi::ConnState::DISCONNECTED):
961             eventData = "Disconnected";
962             break;
963         default: {
964             eventData = "UnknownState";
965             break;
966         }
967     }
968     if ((instId == INSTID_WLAN0 && WifiCommonEventHelper::PublishConnStateChangedEvent(state, eventData)) ||
969         (instId == INSTID_WLAN1 && WifiCommonEventHelper::PublishWifi2ConnStateChangedEvent(state, eventData))) {
970         WIFI_LOGI("publish connection state changed event,%{public}s, instId %{public}d.", eventData.c_str(), instId);
971         return;
972     }
973     WIFI_LOGE("publish connection state changed event,%{public}s, instId %{public}d fail.", eventData.c_str(), instId);
974 }
975 
PublishRssiValueChangedEvent(int state,int instId)976 void WifiInternalEventDispatcher::PublishRssiValueChangedEvent(int state, int instId)
977 {
978     if (instId == INSTID_WLAN1) {
979         return ;
980     }
981     WifiLinkedInfo likedInfo;
982     WifiConfigCenter::GetInstance().GetLinkedInfo(likedInfo);
983     int signalLevel = WifiSettings::GetInstance().GetSignalLevel(state, likedInfo.band);
984     if (!WifiCommonEventHelper::PublishRssiValueChangedEvent("wifiSignalLevel", signalLevel,
985         state, "OnRssiValueChanged")) {
986         WIFI_LOGE("failed to publish rssi value changed event!");
987         return;
988     }
989     WIFI_LOGD("publish rssi value changed event.");
990 }
991 
PublishWifiStateChangedEvent(int state,int instId)992 void WifiInternalEventDispatcher::PublishWifiStateChangedEvent(int state, int instId)
993 {
994     if (instId == INSTID_WLAN0) {
995         if (!WifiCommonEventHelper::PublishPowerStateChangeEvent(state, "OnWifiPowerStateChanged")) {
996             WIFI_LOGE("failed to publish wifi state changed event!");
997             return;
998         }
999     } else if (instId == INSTID_WLAN1) {
1000         if (!WifiCommonEventHelper::PublishWifi2PowerStateChangeEvent(state, "OnWifiPowerStateChanged")) {
1001             WIFI_LOGE("failed to publish wifi state changed event!");
1002             return;
1003         }
1004     } else {
1005         WIFI_LOGE("invalid InstId!");
1006         return;
1007     }
1008 
1009     WIFI_LOGI("publish wifi state changed event, state %{public}d, instId %{public}d", state, instId);
1010 }
1011 
VerifyRegisterCallbackPermission(int callbackEventId)1012 bool WifiInternalEventDispatcher::VerifyRegisterCallbackPermission(int callbackEventId)
1013 {
1014     if (g_CallbackEventChkSysAppList.find(callbackEventId) != g_CallbackEventChkSysAppList.end()) {
1015         if (!WifiAuthCenter::IsSystemAccess()) {
1016             WIFI_LOGE("VerifyRegisterCallbackPermission:NOT System APP, PERMISSION_DENIED!");
1017             return false;
1018         }
1019     }
1020 
1021     std::pair<CallbackEventPermissionMap::iterator, CallbackEventPermissionMap::iterator>
1022         pr = g_CallbackEventPermissionMap.equal_range(callbackEventId);
1023     bool hasPermission = true;
1024     for (auto itr = pr.first; itr != pr.second; ++itr) {
1025         auto verifyPermissionFunc = itr->second.first;
1026         int result = verifyPermissionFunc();
1027         auto permissionName = itr->second.second;
1028         if (permissionName.compare("ohos.permission.GET_WIFI_INFO_INTERNAL") == 0) {
1029             if (result == PERMISSION_GRANTED) {
1030                 return true;
1031             }
1032             WIFI_LOGE("%{public}s, No permission register callback! event:%{public}d", __func__, itr->first);
1033         } else {
1034             if (result != PERMISSION_GRANTED) {
1035                 hasPermission = false;
1036                 WIFI_LOGE("%{public}s, No permission register callback! event:%{public}d", __func__, itr->first);
1037             }
1038         }
1039     }
1040     return hasPermission;
1041 }
1042 
SetAppFrozen(std::set<int> pidList,bool isFrozen)1043 void WifiInternalEventDispatcher::SetAppFrozen(std::set<int> pidList, bool isFrozen)
1044 {
1045     std::unique_lock<std::mutex> lock(mPidFrozenMutex);
1046     WIFI_LOGD("%{public}s, list size:%{public}zu, isFrozen:%{public}d", __func__, pidList.size(), isFrozen);
1047     for (auto itr : pidList) {
1048         if (isFrozen) {
1049             frozenPidList.insert(itr);
1050         } else {
1051             frozenPidList.erase(itr);
1052         }
1053     }
1054     WIFI_LOGD("%{public}s finish, size:%{public}zu", __func__, frozenPidList.size());
1055 }
1056 
ResetAllFrozenApp()1057 void WifiInternalEventDispatcher::ResetAllFrozenApp()
1058 {
1059     std::unique_lock<std::mutex> lock(mPidFrozenMutex);
1060     WIFI_LOGI("WifiInternalEventDispatcher::Reset All Frozen App");
1061     frozenPidList.clear();
1062 }
1063 
IsAppFrozen(int pid)1064 bool WifiInternalEventDispatcher::IsAppFrozen(int pid)
1065 {
1066     std::unique_lock<std::mutex> lock(mPidFrozenMutex);
1067     auto it = frozenPidList.find(pid);
1068     if (it != frozenPidList.end()) {
1069         return true;
1070     }
1071     return false;
1072 }
1073 }  // namespace Wifi
1074 }  // namespace OHOS