1 /*
2  * Copyright (C) 2021 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_WIFI_INTERNAL_EVENT_DISPATCHER_H
17 #define OHOS_WIFI_INTERNAL_EVENT_DISPATCHER_H
18 
19 #include <chrono>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <map>
24 #include <unordered_set>
25 #include <functional>
26 #include "wifi_internal_msg.h"
27 #include "wifi_event_handler.h"
28 #include "i_wifi_device_callback.h"
29 #include "i_wifi_scan_callback.h"
30 #include "i_wifi_hotspot_callback.h"
31 #include "i_wifi_p2p_callback.h"
32 
33 namespace OHOS {
34 namespace Wifi {
35 using StaCallbackMapType = std::map<sptr<IRemoteObject>, sptr<IWifiDeviceCallBack>>;
36 using StaCallbackInfo = std::map<sptr<IRemoteObject>, WifiCallingInfo>;
37 using ScanCallbackMapType = std::map<sptr<IRemoteObject>, sptr<IWifiScanCallback>>;
38 using ScanCallbackInfo = std::map<sptr<IRemoteObject>, WifiCallingInfo>;
39 using HotspotCallbackMapType = std::map<sptr<IRemoteObject>, sptr<IWifiHotspotCallback>>;
40 using HotspotCallbackInfo = std::map<sptr<IRemoteObject>, std::unordered_set<int>>;
41 using P2pCallbackMapType = std::map<sptr<IRemoteObject>, sptr<IWifiP2pCallback>>;
42 using P2pCallbackInfo = std::map<sptr<IRemoteObject>, WifiCallingInfo>;
43 using CallbackEventPermissionMap = std::multimap<int, std::pair<std::function<int()>, std::string>>;
44 
45 class WifiInternalEventDispatcher {
46 public:
47     WifiInternalEventDispatcher();
48     ~WifiInternalEventDispatcher();
49 
50     /**
51      * @Description Init WifiInternalEventDispatcher object
52      *
53      * @return int - init result, when 0 means success, other means some fails happened
54      */
55     int Init();
56 
57     /**
58      * @Description Send system motify message
59      *
60      * @return int - init result, when 0 means success, other means some fails happened
61      */
62     int SendSystemNotifyMsg(void);
63 
64     /**
65      * @Description Add broadcast events to the internal event broadcast queue
66      *
67      * @param msg - callback msg
68      * @return int - 0 success
69      */
70     int AddBroadCastMsg(const WifiEventCallbackMsg &msg);
71 
72     /**
73      * @Description Exit event broadcast thread
74      *
75      */
76     void Exit();
77 
78     /**
79      * @Description Event broadcast thread processing function
80      *              1. Obtain broadcast events from the internal event queue
81      *                 mEventQue
82      *              2. Send broadcast events to handles in the application
83      *                 registration list one by one. The BpWifiCallbackService
84      *                 method will eventually be called
85      *
86      * @param p WifiInternalEventDispatcher this Object
87      * @return void* - nullptr, not care this now
88      */
89     static void Run(WifiInternalEventDispatcher &instance, const WifiEventCallbackMsg &msg);
90 
91     static WifiInternalEventDispatcher &GetInstance();
92     ErrCode AddStaCallback(const sptr<IRemoteObject> &remote, const sptr<IWifiDeviceCallBack> &callback, int pid,
93         const std::string &eventName, int tokenId, int instId = 0);
94     int SetSingleStaCallback(const sptr<IWifiDeviceCallBack> &callback, const std::string &eventName, int instId = 0);
95     sptr<IWifiDeviceCallBack> GetSingleStaCallback(int instId = 0) const;
96     int RemoveStaCallback(const sptr<IRemoteObject> &remote, int instId = 0);
97     bool HasStaRemote(const sptr<IRemoteObject> &remote, int instId = 0);
98     ErrCode AddScanCallback(const sptr<IRemoteObject> &remote, const sptr<IWifiScanCallback> &callback, int pid,
99         const std::string &eventName, int tokenId, int instId = 0);
100     int SetSingleScanCallback(const sptr<IWifiScanCallback> &callback, const std::string &eventName, int instId = 0);
101     sptr<IWifiScanCallback> GetSingleScanCallback(int instId = 0) const;
102     int RemoveScanCallback(const sptr<IRemoteObject> &remote, int instId = 0);
103     bool HasScanRemote(const sptr<IRemoteObject> &remote, int instId = 0);
104     ErrCode AddHotspotCallback(const sptr<IRemoteObject> &remote, const sptr<IWifiHotspotCallback> &callback,
105         const std::string &eventName, int id = 0);
106     int SetSingleHotspotCallback(const sptr<IWifiHotspotCallback> &callback, int id = 0);
107     sptr<IWifiHotspotCallback> GetSingleHotspotCallback(int id) const;
108     int RemoveHotspotCallback(const sptr<IRemoteObject> &remote, int id = 0);
109     bool HasHotspotRemote(const sptr<IRemoteObject> &remote, int id = 0);
110     ErrCode AddP2pCallback(const sptr<IRemoteObject> &remote, const sptr<IWifiP2pCallback> &callback, int pid,
111         const std::string &eventName, int tokenId);
112     int SetSingleP2pCallback(const sptr<IWifiP2pCallback> &callback);
113     sptr<IWifiP2pCallback> GetSingleP2pCallback() const;
114     int RemoveP2pCallback(const sptr<IRemoteObject> &remote);
115     bool HasP2pRemote(const sptr<IRemoteObject> &remote);
116 
117     void InvokeScanCallbacks(const WifiEventCallbackMsg &msg);
118     void InvokeDeviceCallbacks(const WifiEventCallbackMsg &msg);
119     void InvokeHotspotCallbacks(const WifiEventCallbackMsg &msg);
120     void InvokeP2pCallbacks(const WifiEventCallbackMsg &msg);
121     bool VerifyRegisterCallbackPermission(int callbackEventId);
122     void SetAppFrozen(std::set<int> pidList, bool isFrozen);
123     void ResetAllFrozenApp();
124     bool IsAppFrozen(int pid);
125 private:
126     static void DealStaCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg);
127     static void DealScanCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg);
128     static void DealHotspotCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg);
129     static void DealP2pCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg);
130     static void SendP2pCallbackMsg(sptr<IWifiP2pCallback> &callback, const WifiEventCallbackMsg &msg,
131         int pid, int uid, int tokenId);
132 #ifdef SUPPORT_RANDOM_MAC_ADDR
133     static void updateP2pDeviceMacAddress(std::vector<WifiP2pDevice> &device);
134 #endif
135     static void PublishConnStateChangedEvent(int state, int instId, const WifiLinkedInfo &info);
136     static void PublishWifiStateChangedEvent(int state, int instId);
137     static void PublishRssiValueChangedEvent(int state, int instId);
138     static void PublishStaEvent(const WifiEventCallbackMsg &msg);
139     static void SendConfigChangeEvent(sptr<IWifiP2pCallback> &callback,  CfgInfo* cfgInfo);
140 private:
141     std::unique_ptr<WifiEventHandler> mBroadcastThread = nullptr;
142     std::mutex mStaCallbackMutex;
143     std::map<int, StaCallbackMapType> mStaCallbacks;
144     std::map<int, StaCallbackInfo> mStaCallBackInfo;
145     std::map<int, sptr<IWifiDeviceCallBack>> mStaSingleCallback;
146     std::mutex mScanCallbackMutex;
147     std::map<int, ScanCallbackMapType> mScanCallbacks;
148     std::map<int, ScanCallbackInfo> mScanCallBackInfo;
149     std::map<int, sptr<IWifiScanCallback>> mScanSingleCallback;
150     std::mutex mHotspotCallbackMutex;
151     std::map<int, HotspotCallbackMapType> mHotspotCallbacks;
152     std::map<int, HotspotCallbackInfo> mHotspotCallbackInfo;
153     std::map<int, sptr<IWifiHotspotCallback>> mHotspotSingleCallback;
154     std::mutex mP2pCallbackMutex;
155     P2pCallbackMapType mP2pCallbacks;
156     P2pCallbackInfo mP2pCallbackInfo;
157     sptr<IWifiP2pCallback> mP2pSingleCallback;
158     std::mutex mPidFrozenMutex;
159     std::set<int> frozenPidList;
160 };
161 }  // namespace Wifi
162 }  // namespace OHOS
163 #endif