1 /*
2  * Copyright (C) 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 #ifndef OHOS_WIFI_INTERNAL_EVENT_DISPATCHER_LITE_H
17 #define OHOS_WIFI_INTERNAL_EVENT_DISPATCHER_LITE_H
18 
19 #include <atomic>
20 #include <chrono>
21 #include <condition_variable>
22 #include <deque>
23 #include <memory>
24 #include <mutex>
25 #include <thread>
26 #include <string>
27 #include <map>
28 #include <unordered_set>
29 
30 #include "wifi_internal_msg.h"
31 #include "i_wifi_device_callback.h"
32 #include "i_wifi_scan_callback.h"
33 
34 namespace OHOS {
35 namespace Wifi {
36 class WifiInternalEventDispatcher {
37 public:
38     WifiInternalEventDispatcher();
39     ~WifiInternalEventDispatcher();
40 
41     /**
42      * @Description Init WifiInternalEventDispatcher object
43      *
44      * @return int - init result, when 0 means success, other means some fails happened
45      */
46     int Init();
47 
48     /**
49      * @Description Send system motify message
50      *
51      * @return int - init result, when 0 means success, other means some fails happened
52      */
53     int SendSystemNotifyMsg(void);
54 
55     /**
56      * @Description Add broadcast events to the internal event broadcast queue
57      *
58      * @param msg - callback msg
59      * @return int - 0 success
60      */
61     int AddBroadCastMsg(const WifiEventCallbackMsg &msg);
62 
63     /**
64      * @Description Exit event broadcast thread
65      *
66      */
67     void Exit();
68 
69     /**
70      * @Description Event broadcast thread processing function
71      *              1. Obtain broadcast events from the internal event queue
72      *                 mEventQue
73      *              2. Send broadcast events to handles in the application
74      *                 registration list one by one. The BpWifiCallbackService
75      *                 method will eventually be called
76      *
77      * @param p WifiInternalEventDispatcher this Object
78      * @return void* - nullptr, not care this now
79      */
80     static void Run(WifiInternalEventDispatcher &instance);
81 
82     static WifiInternalEventDispatcher &GetInstance();
83     int SetSingleStaCallback(const std::shared_ptr<IWifiDeviceCallBack> &callback, const std::string &eventName,
84         int instId = 0);
85     std::shared_ptr<IWifiDeviceCallBack> GetSingleStaCallback() const;
86     std::unordered_set<int>& GetStaSingleCallbackEvent();
87     int SetSingleScanCallback(const std::shared_ptr<IWifiScanCallback> &callback, const std::string &eventName,
88         int instId = 0);
89     std::shared_ptr<IWifiScanCallback> GetSingleScanCallback() const;
90     std::unordered_set<int>& GetScanSingleCallbackEvent();
91 private:
92     static void DealStaCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg);
93     static void DealScanCallbackMsg(WifiInternalEventDispatcher &pInstance, const WifiEventCallbackMsg &msg);
94     static void PublishConnStateChangedEvent(int state, const WifiLinkedInfo &info);
95     static void PublishWifiStateChangedEvent(int state);
96     static void PublishRssiValueChangedEvent(int state);
97 private:
98     std::thread mBroadcastThread;
99     std::atomic<bool> mRunFlag;
100     std::mutex mMutex;
101     std::condition_variable mCondition;
102     std::deque<WifiEventCallbackMsg> mEventQue;
103     std::shared_ptr<IWifiDeviceCallBack> mStaSingleCallback;
104     std::unordered_set<int> mStaSingleCallbackEvent;
105     std::shared_ptr<IWifiScanCallback> mScanSingleCallback;
106     std::unordered_set<int> mScanSingleCallbackEvent;
107 };
108 }  // namespace Wifi
109 }  // namespace OHOS
110 #endif
111