1 /*
2  * Copyright (c) 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 #ifndef OHOS_WIFI_HAL_DEVICE_MANAGE_H
17 #define OHOS_WIFI_HAL_DEVICE_MANAGE_H
18 
19 #ifdef HDI_CHIP_INTERFACE_SUPPORT
20 #include <string>
21 #include <functional>
22 #include <mutex>
23 #include <map>
24 #include <vector>
25 #include <algorithm>
26 #include <chrono>
27 #include <atomic>
28 #include "singleton.h"
29 #include "v1_0/ichip_controller.h"
30 
31 namespace OHOS {
32 namespace Wifi {
33 using OHOS::HDI::Wlan::Chip::V1_0::IChipController;
34 using OHOS::HDI::Wlan::Chip::V1_0::IChipControllerCallback;
35 using OHOS::HDI::Wlan::Chip::V1_0::IConcreteChip;
36 using OHOS::HDI::Wlan::Chip::V1_0::IConcreteChipCallback;
37 using OHOS::HDI::Wlan::Chip::V1_0::IChipIface;
38 using OHOS::HDI::Wlan::Chip::V1_0::IChipIfaceCallback;
39 using OHOS::HDI::Wlan::Chip::V1_0::ErrorCode;
40 using OHOS::HDI::Wlan::Chip::V1_0::IfaceType;
41 using OHOS::HDI::Wlan::Chip::V1_0::ComboIface;
42 using OHOS::HDI::Wlan::Chip::V1_0::UsableMode;
43 using OHOS::HDI::Wlan::Chip::V1_0::ScanParams;
44 using OHOS::HDI::Wlan::Chip::V1_0::ScanResultsInfo;
45 using OHOS::HDI::Wlan::Chip::V1_0::PnoScanParams;
46 using OHOS::HDI::Wlan::Chip::V1_0::SignalPollResult;
47 using IfaceDestoryCallback = std::function<void(std::string&, int)>;
48 using RssiReportCallback = std::function<void(int, int)>;
49 
50 constexpr IfaceType IFACE_TYPE_DEFAULT = (IfaceType)255;
51 const std::vector<IfaceType> IFACE_TYPES_BY_PRIORITY = {IfaceType::AP, IfaceType::STA, IfaceType::P2P};
52 
53 struct InterfaceCacheEntry {
54     sptr<IConcreteChip> chip;
55     uint32_t chipId;
56     std::string name;
57     IfaceType type;
58     uint64_t creationTime;
59     std::vector<IfaceDestoryCallback> ifaceDestoryCallback;
60 
InterfaceCacheEntryInterfaceCacheEntry61     InterfaceCacheEntry()
62     {
63         chip = nullptr;
64         chipId = 0;
65         name = "";
66         type = IFACE_TYPE_DEFAULT;
67         creationTime = 0;
68         ifaceDestoryCallback.clear();
69     }
70 };
71 
72 struct WifiIfaceInfo {
73     std::string name;
74     sptr<IChipIface> iface;
75 
WifiIfaceInfoWifiIfaceInfo76     WifiIfaceInfo()
77     {
78         Clear();
79     }
80 
ClearWifiIfaceInfo81     void Clear()
82     {
83         name = "";
84         iface = nullptr;
85     }
86 };
87 
88 struct WifiChipInfo {
89     sptr<IConcreteChip> chip;
90     uint32_t chipId;
91     bool currentModeIdValid;
92     uint32_t currentModeId;
93     uint32_t chipCapabilities;
94     std::vector<UsableMode> availableModes;
95     std::map<IfaceType, std::vector<WifiIfaceInfo>> ifaces;
96 
WifiChipInfoWifiChipInfo97     WifiChipInfo()
98     {
99         chip = nullptr;
100         chipId = 0;
101         currentModeIdValid = false;
102         currentModeId = 0;
103         chipCapabilities = 0;
104         availableModes.clear();
105         ifaces.clear();
106     }
107 
WifiChipInfoWifiChipInfo108     WifiChipInfo(const WifiChipInfo &other)
109     {
110         chip = other.chip;
111         chipId = other.chipId;
112         currentModeIdValid = other.currentModeIdValid;
113         currentModeId = other.currentModeId;
114         chipCapabilities = other.chipCapabilities;
115         availableModes = other.availableModes;
116         ifaces = other.ifaces;
117     }
118 
119     WifiChipInfo& operator=(const WifiChipInfo &other)
120     {
121         chip = other.chip;
122         chipId = other.chipId;
123         currentModeIdValid = other.currentModeIdValid;
124         currentModeId = other.currentModeId;
125         chipCapabilities = other.chipCapabilities;
126         availableModes = other.availableModes;
127         ifaces = other.ifaces;
128         return *this;
129     }
130 };
131 
132 struct IfaceCreationData {
133     WifiChipInfo chipInfo;
134     uint32_t chipModeId;
135     std::vector<WifiIfaceInfo> interfacesToBeRemovedFirst;
136 
IfaceCreationDataIfaceCreationData137     IfaceCreationData()
138     {
139         chipModeId = 0;
140         interfacesToBeRemovedFirst.clear();
141     }
142 
isEmptyIfaceCreationData143     bool isEmpty()
144     {
145         return chipInfo.chip == nullptr;
146     }
147 };
148 
149 class ChipControllerCallback : public IChipControllerCallback {
150 public:
151     ChipControllerCallback() = default;
152     virtual ~ChipControllerCallback() = default;
153 
OnVendorHalRestart(ErrorCode code)154     virtual int32_t OnVendorHalRestart(ErrorCode code) override { return 0; }
155 };
156 
157 class ChipIfaceCallback : public IChipIfaceCallback {
158 public:
159     ChipIfaceCallback() = default;
160     virtual ~ChipIfaceCallback() = default;
161 
162     virtual int32_t OnScanResultsCallback(uint32_t event) override;
163     virtual int32_t OnRssiReport(int32_t index, int32_t c0Rssi, int32_t c1Rssi) override;
164 };
165 
166 class HalDeviceManager {
167     DECLARE_DELAYED_SINGLETON(HalDeviceManager)
168 
169 public:
170     /**
171      * @Description start chip hdi
172      *
173      * @param
174      * @return bool
175      */
176     bool StartChipHdi();
177 
178     /**
179      * @Description stop chip hdi
180      *
181      * @param
182      * @return void
183      */
184     void StopChipHdi();
185 
186     /* ************************ Iface Manager Interface ************************** */
187     /**
188      * @Description create sta iface
189      *
190      * @param ifaceDestoryCallback: [in] iface destory callback function
191      * @param ifaceName: [out] iface name
192      * @return bool
193      */
194     bool CreateStaIface(const IfaceDestoryCallback &ifaceDestoryCallback, const RssiReportCallback &rssiReportCallback,
195         std::string &ifaceName, int instId);
196 
197     /**
198      * @Description create ap iface
199      *
200      * @param ifaceDestoryCallback: [in] iface destory callback function
201      * @param ifaceName: [out] iface name
202      * @return bool
203      */
204     bool CreateApIface(const IfaceDestoryCallback &ifaceDestoryCallback, std::string &ifaceName);
205 
206     /**
207      * @Description create p2p iface
208      *
209      * @param ifaceDestoryCallback: [in] iface destory callback function
210      * @param ifaceName: [out] iface name
211      * @return bool
212      */
213     bool CreateP2pIface(const IfaceDestoryCallback &ifaceDestoryCallback, std::string &ifaceName);
214 
215     /**
216      * @Description remove sta iface
217      *
218      * @param ifaceName: [in] iface name
219      * @return bool
220      */
221     bool RemoveStaIface(const std::string &ifaceName);
222 
223     /**
224      * @Description remove ap iface
225      *
226      * @param ifaceName: [in] iface name
227      * @return bool
228      */
229     bool RemoveApIface(const std::string &ifaceName);
230 
231     /**
232      * @Description remove p2p iface
233      *
234      * @param ifaceName: [in] iface name
235      * @return bool
236      */
237     bool RemoveP2pIface(const std::string &ifaceName);
238 
239     /* ************************ Sta Interface ************************** */
240     /**
241      * @Description start scan.
242      *
243      * @param ifaceName: [in] iface name
244      * @param scanParams: [in] scan params
245      * @return bool
246      */
247     bool Scan(const std::string &ifaceName, const ScanParams &scanParams);
248 
249     /**
250      * @Description start pno scan.
251      *
252      * @param ifaceName: [in] iface name
253      * @param scanParams: [in] scan params
254      * @return bool
255      */
256     bool StartPnoScan(const std::string &ifaceName, const PnoScanParams &scanParams);
257 
258     /**
259      * @Description stop pno scan.
260      *
261      * @param ifaceName: [in] iface name
262      * @return bool
263      */
264     bool StopPnoScan(const std::string &ifaceName);
265 
266     /**
267      * @Description get scan infos.
268      *
269      * @param ifaceName: [in] iface name
270      * @param scanResultsInfo: [out] scan results info
271      * @return bool
272      */
273     bool GetScanInfos(const std::string &ifaceName, std::vector<ScanResultsInfo> &scanResultsInfo);
274 
275     /**
276      * @Description Obtain connection signaling information.
277      *
278      * @param ifaceName: [in] iface name
279      * @param signalPollResult: [out] signal poll result
280      * @return bool
281      */
282     bool GetConnectSignalInfo(const std::string &ifaceName, SignalPollResult &signalPollResult);
283 
284     /**
285      * @Description set power save mode
286      *
287      * @param ifaceName: [in] iface name
288      * @param mode: [in] power save mode
289      * @return bool
290      */
291     bool SetPmMode(const std::string &ifaceName, int mode);
292 
293     /**
294      * @Description set data packet identification mark rule
295      *
296      * @param ifaceName: [in] iface name
297      * @return bool
298      */
299     bool SetDpiMarkRule(const std::string &ifaceName, int uid, int protocol, int enable);
300 
301     /**
302      * @Description set sta mac address.
303      *
304      * @param ifaceName: [in] iface name
305      * @param mac: [in] mac address
306      * @return bool
307      */
308     bool SetStaMacAddress(const std::string &ifaceName, const std::string &mac);
309 
310     /**
311      * @Description set network updown.
312      *
313      * @param ifaceName: [in] iface name
314      * @param upDown: [in] up or down
315      * @return bool
316      */
317     bool SetNetworkUpDown(const std::string &ifaceName, bool upDown);
318 
319     /**
320      * @Description get chipset category
321      *
322      * @param ifaceName: [in] iface name
323      * @param chipsetCategory: [out] chipset category
324      * @return bool
325      */
326     bool GetChipsetCategory(const std::string &ifaceName, uint32_t &chipsetCategory);
327 
328     /**
329      * @Description get chipset feature capability
330      *
331      * @param ifaceName: [in] iface name
332      * @param chipsetFeatrureCapability: [out] chipset feature capability
333      * @return bool
334      */
335     bool GetChipsetWifiFeatrureCapability(const std::string &ifaceName, int &chipsetFeatrureCapability);
336 
337     /* ************************ Ap Interface ************************** */
338     /**
339      * @Description Obtains the hotspot frequency supported by a specified frequency band.
340      *
341      * @param ifaceName: [in] iface name
342      * @param band: [in] frequency band
343      * @param frequencies: [in] frequency
344      * @return bool
345      */
346     bool GetFrequenciesByBand(const std::string &ifaceName, int32_t band, std::vector<int> &frequencies);
347 
348     /**
349      * @Description set the power mode.
350      *
351      * @param ifaceName: [in] iface name
352      * @param model: [in] power mode
353      * @return bool
354      */
355     bool SetPowerModel(const std::string &ifaceName, int model);
356 
357     /**
358      * @Description set wifi tx power for sar.
359      *
360      * @param ifaceName: [in] iface name
361      * @param power: [in] power
362      * @return bool
363      */
364     bool SetTxPower(int power);
365 
366     int32_t IfaceSetTxPower(const std::string &ifaceName,
367                             const std::map<std::string, sptr<IChipIface>> &mWifiIfaces, int power);
368 
369     /**
370      * @Description get the power mode.
371      *
372      * @param ifaceName: [in] iface name
373      * @param model: [in] power mode
374      * @return bool
375      */
376     bool GetPowerModel(const std::string &ifaceName, int &model);
377 
378     /**
379      * @Description Sets the Wi-Fi country code.
380      *
381      * @param ifaceName: [in] iface name
382      * @param code: [in] country code
383      * @return bool
384      */
385     bool SetWifiCountryCode(const std::string &ifaceName, const std::string &code);
386 
387     /**
388      * @Description set ap mac address.
389      *
390      * @param ifaceName: [in] iface name
391      * @param mac: [in] mac address
392      * @return bool
393      */
394     bool SetApMacAddress(const std::string &ifaceName, const std::string &mac);
395 
396 private:
397     void ResetHalDeviceManagerInfo();
398     bool CheckReloadChipHdiService();
399     bool CheckChipHdiStarted();
400     bool GetIfaceName(sptr<IChipIface> &iface, std::string &ifaceName);
401     bool GetIfaceType(sptr<IChipIface> &iface, IfaceType &ifaceType);
402     void GetP2pIfaceInfo(WifiChipInfo &wifiChipInfo);
403     void GetApIfaceInfo(WifiChipInfo &wifiChipInfo);
404     void GetStaIfaceInfo(WifiChipInfo &wifiChipInfo);
405     bool GetIfaceInfo(WifiChipInfo &wifiChipInfo);
406     bool GetChipInfo(uint32_t chipId, WifiChipInfo &wifiChipInfo);
407     bool GetAllChipInfo(std::vector<WifiChipInfo> &wifiChipInfos);
408     bool ValidateInterfaceCache(std::vector<WifiChipInfo> &wifiChipInfos);
409     void SelectInterfacesToDelete(int excessInterfaces, IfaceType requestedIfaceType, IfaceType existingIfaceType,
410         std::vector<WifiIfaceInfo> &existingIface, std::vector<WifiIfaceInfo> &interfacesToBeRemovedFirst);
411     bool AllowedToBeDeleteIfaceTypeForRequestedType(IfaceType requestedIfaceType, IfaceType existingIfaceType);
412     bool CreateTheNeedChangeChipModeIfaceData(WifiChipInfo &wifiChipInfo, IfaceType createIfaceType,
413         UsableMode &chipMode, IfaceCreationData &ifaceCreationData);
414     bool CanIfaceComboSupportRequest(WifiChipInfo &wifiChipInfo, UsableMode &chipMode, std::vector<int> &chipIfaceCombo,
415         IfaceType createIfaceType, IfaceCreationData &ifaceCreationData);
416     void ExpandIfaceCombos(ComboIface &chipIfaceCombo, std::vector<std::vector<int>> &expandedIfaceCombos);
417     bool CompareIfaceCreationData(IfaceCreationData &data1, IfaceCreationData &data2);
418     bool ExecuteChipReconfiguration(IfaceCreationData &ifaceCreationData, IfaceType createIfaceType,
419         sptr<IChipIface> &iface);
420     void FindBestIfaceCreationProposal(std::vector<std::vector<int>> &expandedIfaceCombos, WifiChipInfo &chipInfo,
421         UsableMode &chipMode, IfaceType createIfaceType, IfaceCreationData &bestIfaceCreationProposal);
422     bool CreateIfaceIfPossible(std::vector<WifiChipInfo> &wifiChipInfos, IfaceType createIfaceType,
423         const IfaceDestoryCallback &ifaceDestoryCallback, std::string &ifaceName, sptr<IChipIface> &iface);
424     bool CreateIface(IfaceType createIfaceType, const IfaceDestoryCallback &ifaceDestoryCallback,
425         std::string &ifaceName, sptr<IChipIface> &iface);
426     void DispatchIfaceDestoryCallback(std::string &removeIfaceName, IfaceType removeIfaceType, bool isCallback,
427         IfaceType createIfaceType);
428     bool GetChip(const std::string &removeIfaceName, IfaceType removeIfaceType, sptr<IConcreteChip> &chip);
429     bool RemoveIface(sptr<IChipIface> &iface, bool isCallback, IfaceType createIfaceType);
430     IChipIface *FindIface(const std::string &ifaceName);
431     // death recipient
432     static void AddChipHdiDeathRecipient();
433     static void RemoveChipHdiDeathRecipient();
434 
435 private:
436     std::map<std::pair<std::string, IfaceType>, InterfaceCacheEntry> mInterfaceInfoCache;
437     std::map<std::string, sptr<IChipIface>> mIWifiStaIfaces;
438     std::map<std::string, sptr<IChipIface>> mIWifiApIfaces;
439     std::map<std::string, sptr<IChipIface>> mIWifiP2pIfaces;
440     sptr<IChipController> g_IWifi{nullptr};
441     sptr<ChipControllerCallback> g_chipControllerCallback{nullptr};
442     sptr<ChipIfaceCallback> g_chipIfaceCallback{nullptr};
443     static std::atomic_bool g_chipHdiServiceDied;
444     static std::mutex mMutex;
445 };
446 
447 }  // namespace Wifi
448 }  // namespace OHOS
449 #endif
450 #endif