1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef WIFICOND_SERVER_H_
18 #define WIFICOND_SERVER_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <android-base/macros.h>
25 #include <wifi_system/interface_tool.h>
26 
27 #include "android/net/wifi/nl80211/BnWificond.h"
28 #include "android/net/wifi/nl80211/IApInterface.h"
29 #include "android/net/wifi/nl80211/IClientInterface.h"
30 #include "android/net/wifi/nl80211/IInterfaceEventCallback.h"
31 #include "android/net/wifi/nl80211/IWificondEventCallback.h"
32 
33 #include "wificond/ap_interface_impl.h"
34 #include "wificond/client_interface_impl.h"
35 
36 namespace android {
37 namespace wificond {
38 
39 class NL80211Packet;
40 class NetlinkUtils;
41 class ScanUtils;
42 
43 struct InterfaceInfo;
44 
45 class Server : public android::net::wifi::nl80211::BnWificond {
46  public:
47   Server(std::unique_ptr<wifi_system::InterfaceTool> if_tool,
48          NetlinkUtils* netlink_utils,
49          ScanUtils* scan_utils);
50   ~Server() override = default;
51 
52   android::binder::Status registerWificondEventCallback(
53       const android::sp<android::net::wifi::nl80211::IWificondEventCallback>&
54           callback) override;
55   android::binder::Status unregisterWificondEventCallback(
56       const android::sp<android::net::wifi::nl80211::IWificondEventCallback>&
57           callback) override;
58 
59   android::binder::Status RegisterCallback(
60       const android::sp<android::net::wifi::nl80211::IInterfaceEventCallback>&
61           callback) override;
62   android::binder::Status UnregisterCallback(
63       const android::sp<android::net::wifi::nl80211::IInterfaceEventCallback>&
64           callback) override;
65   // Returns a vector of available frequencies for 2.4GHz channels.
66   android::binder::Status getAvailable2gChannels(
67       ::std::optional<::std::vector<int32_t>>* out_frequencies) override;
68   // Returns a vector of available frequencies for 5GHz non-DFS channels.
69   android::binder::Status getAvailable5gNonDFSChannels(
70       ::std::optional<::std::vector<int32_t>>* out_frequencies) override;
71   // Returns a vector of available frequencies for DFS channels.
72   android::binder::Status getAvailableDFSChannels(
73       ::std::optional<::std::vector<int32_t>>* out_frequencies) override;
74   // Returns a vector of available frequencies for 6GHz channels.
75   android::binder::Status getAvailable6gChannels(
76       ::std::optional<::std::vector<int32_t>>* out_frequencies) override;
77   // Returns a vector of available frequencies for 60GHz channels.
78   android::binder::Status getAvailable60gChannels(
79       ::std::optional<::std::vector<int32_t>>* out_frequencies) override;
80 
81   android::binder::Status createApInterface(
82       const std::string& iface_name,
83       android::sp<android::net::wifi::nl80211::IApInterface>*
84           created_interface) override;
85 
86   android::binder::Status createClientInterface(
87       const std::string& iface_name,
88       android::sp<android::net::wifi::nl80211::IClientInterface>*
89           created_interface) override;
90 
91   android::binder::Status tearDownApInterface(
92       const std::string& iface_name,
93       bool* out_success) override;
94 
95   android::binder::Status tearDownClientInterface(
96       const std::string& iface_name,
97       bool* out_success) override;
98 
99   android::binder::Status tearDownInterfaces() override;
100 
101   android::binder::Status GetClientInterfaces(
102       std::vector<android::sp<android::IBinder>>* out_client_ifs) override;
103   android::binder::Status GetApInterfaces(
104       std::vector<android::sp<android::IBinder>>* out_ap_ifs) override;
105   status_t dump(int fd, const Vector<String16>& args) override;
106 
107   // Returns device wiphy capabilities for an interface
108   android::binder::Status getDeviceWiphyCapabilities(
109       const std::string& iface_name,
110       ::std::optional<net::wifi::nl80211::DeviceWiphyCapabilities>* capabilities) override;
111 
112  private:
113   // Request interface information from kernel and setup local interface object.
114   // This assumes that interface should be in STATION mode. Even if we setup
115   // interface on behalf of createApInterace(), it is Hostapd that configure
116   // the interface to Ap mode later.
117   // Returns true on success, false otherwise.
118   bool SetupInterface(const std::string& iface_name, InterfaceInfo* interface,
119       uint32_t *wiphy_index);
120   void LogSupportedBands(uint32_t wiphy_index);
121   void OnRegDomainChanged(uint32_t wiphy_index, std::string& country_code);
122   void BroadcastClientInterfaceReady(
123       android::sp<android::net::wifi::nl80211::IClientInterface> network_interface);
124   void BroadcastApInterfaceReady(
125       android::sp<android::net::wifi::nl80211::IApInterface> network_interface);
126   void BroadcastClientInterfaceTornDown(
127       android::sp<android::net::wifi::nl80211::IClientInterface> network_interface);
128   void BroadcastApInterfaceTornDown(
129       android::sp<android::net::wifi::nl80211::IApInterface> network_interface);
130   void BroadcastRegDomainChanged(std::string country_code);
131   void MarkDownAllInterfaces();
132   int FindWiphyIndex(const std::string& iface_name);
133   bool GetBandInfo(int wiphy_index, BandInfo* band_info);
134   int GetWiphyIndexFromBand(int band);
135   void UpdateBandWiphyIndexMap(int wiphy_index);
136   void EraseBandWiphyIndexMap(int wiphy_index);
137   bool hasNoIfaceForWiphyIndex(int wiphy_index);
138 
139   const std::unique_ptr<wifi_system::InterfaceTool> if_tool_;
140   NetlinkUtils* const netlink_utils_;
141   ScanUtils* const scan_utils_;
142 
143   // Chips serves mutually exclusive bands.
144   std::map<uint32_t, uint32_t> band_to_wiphy_index_map_;
145   std::map<std::string, uint32_t> iface_to_wiphy_index_map_;
146   std::map<std::string, std::unique_ptr<ApInterfaceImpl>> ap_interfaces_;
147   std::map<std::string, std::unique_ptr<ClientInterfaceImpl>> client_interfaces_;
148   std::vector<android::sp<android::net::wifi::nl80211::IInterfaceEventCallback>>
149       interface_event_callbacks_;
150   std::vector<android::sp<android::net::wifi::nl80211::IWificondEventCallback>>
151       wificond_event_callbacks_;
152 
153   // Cached interface list from kernel for dumping.
154   std::vector<InterfaceInfo> debug_interfaces_;
155 
156   DISALLOW_COPY_AND_ASSIGN(Server);
157 };
158 
159 }  // namespace wificond
160 }  // namespace android
161 
162 #endif  // WIFICOND_SERVER_H_
163