1 /*
2  * Copyright (C) 2021-2023 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 WIFI_COUNTRY_CODE_MANAGER
17 #define WIFI_COUNTRY_CODE_MANAGER
18 
19 #include <bitset>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 #include "common_event_subscriber.h"
27 #include "i_wifi_country_code_change_listener.h"
28 #ifdef FEATURE_AP_SUPPORT
29 #include "i_ap_service_callbacks.h"
30 #endif
31 #ifdef FEATURE_STA_SUPPORT
32 #include "sta_service_callback.h"
33 #endif
34 #include "wifi_country_code_define.h"
35 #include "wifi_country_code_policy.h"
36 
37 namespace OHOS {
38 namespace Wifi {
39 class WifiCountryCodeManager {
40 public:
41     /**
42      * @Description get WifiCountryCodeManager instance
43      *
44      * @return WifiCountryCodeManager
45      */
46     static WifiCountryCodeManager &GetInstance();
47 
48     /**
49      * @Description wifiCountryCodeManager init
50      *
51      * @return init result
52      */
53     ErrCode Init();
54 #ifdef FEATURE_STA_SUPPORT
55     /**
56      * @Description get sta change Callback
57      *
58      * @return callBack obj
59      */
60     StaServiceCallback GetStaCallback() const;
61 #endif
62 #ifdef FEATURE_AP_SUPPORT
63     /**
64      * @Description get ap change Callback
65      *
66      * @return callBack obj
67      */
68     IApServiceCallbacks GetApCallback() const;
69 #endif
70 
71     /**
72      * @Description get wifi country code
73      *
74      * @param wifiCountryCode - wifi country code
75      */
76     void GetWifiCountryCode(std::string &wifiCountryCode) const;
77 
78     /**
79      * @Description provides the set wifiCountryCode interface for external systems
80      *
81      * @param wifiCountryCode - wifi country code
82      * @return error code
83      */
84     ErrCode SetWifiCountryCodeFromExternal(const std::string &wifiCountryCode = "");
85 
86     /**
87      * @Description register wifi country code change listener
88      *
89      * @param listener - listener obj
90      * @return error code
91      */
92     ErrCode RegisterWifiCountryCodeChangeListener(const std::shared_ptr<IWifiCountryCodeChangeListener> &listener);
93 
94     /**
95      * @Description unregister wifi country code change listener
96      *
97      * @param listener - listener obj
98      * @return error code
99      */
100     ErrCode UnregisterWifiCountryCodeChangeListener(const std::shared_ptr<IWifiCountryCodeChangeListener> &listener);
101 
102 #ifdef FEATURE_STA_SUPPORT
103     /**
104      * @Description deal wifi open result
105      *
106      * @param instId - instance Id
107      */
108     void DealStaOpened(int instId);
109 
110     /**
111      * @Description deal wifi close result
112      *
113      * @param instId - instance Id
114      */
115     void DealStaStopped(int instId);
116 #endif
117 
118     /**
119      * @Description disable WifiCountryCodeManager construct
120      *
121      * @param WifiCountryCodeManager - WifiCountryCodeManager obj
122      */
123     WifiCountryCodeManager(const WifiCountryCodeManager&) = delete;
124 
125     /**
126      * @Description WifiCountryCodeManager deconstruct
127      */
128     ~WifiCountryCodeManager();
129 
130     /**
131      * @Description disable WifiCountryCodeManager equals sign opertaor
132      */
133     WifiCountryCodeManager &operator=(const WifiCountryCodeManager &) = delete;
134 
135     /**
136      * @Description trigger update wifi country code
137      *
138      * @param triggerReason trigger reason
139      */
140     void TriggerUpdateWifiCountryCode(int triggerReason);
141 private:
142 #ifdef FEATURE_STA_SUPPORT
143     StaServiceCallback m_staCallback;
144 #endif
145 #ifdef FEATURE_AP_SUPPORT
146     IApServiceCallbacks m_apCallback;
147 #endif
148     std::map<std::string, std::shared_ptr<IWifiCountryCodeChangeListener>> m_codeChangeListeners;
149     std::string m_wifiCountryCode = DEFAULT_WIFI_COUNTRY_CODE;
150     std::shared_ptr<WifiCountryCodePolicy> m_wifiCountryCodePolicy;
151     std::mutex m_countryCodeMutex;
152     bool m_isFirstConnected = false;
153     std::bitset<WIFI_COUNTRY_CODE_POLICE_DEF_LEN> wifiCountryCodePolicyConf_;
154 
155     WifiCountryCodeManager() = default;
156     void SendCountryCodeChangeCommonEvent(const std::string &wifiCountryCode);
157     ErrCode UpdateWifiCountryCode(const std::string &externalCode = "");
158 #ifdef FEATURE_STA_SUPPORT
159     static void DealStaConnChanged(OperateResState state, const WifiLinkedInfo &info, int instId = 0);
160 #endif
161 #ifdef FEATURE_AP_SUPPORT
162     static void DealApStateChanged(ApState state, int id = 0);
163 #endif
164     bool IsAllowUpdateWifiCountryCode();
165     ErrCode UpdateWifiCountryCodeCache(const std::string &wifiCountryCode);
166     void NotifyWifiCountryCodeChangeListeners(const std::string &wifiCountryCode);
167     ErrCode UnregisterWifiCountryCodeChangeListener(const std::string &moduleName);
168     std::bitset<WIFI_COUNTRY_CODE_POLICE_DEF_LEN> GetWifiCountryCodePolicy();
169 };
170 }
171 }
172 #endif