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