1 // 2 // Copyright (C) 2014 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 APMANAGER_DEVICE_H_ 18 #define APMANAGER_DEVICE_H_ 19 20 #include <set> 21 #include <string> 22 #include <vector> 23 24 #include <base/macros.h> 25 #include <base/memory/ref_counted.h> 26 #include <shill/net/byte_string.h> 27 #include <shill/net/nl80211_message.h> 28 29 #include "apmanager/device_adaptor_interface.h" 30 31 namespace apmanager { 32 33 class ControlInterface; 34 class Manager; 35 36 // Abstraction for WiFi Device (PHY). Each device can have one or more 37 // interfaces defined on it. 38 class Device : public base::RefCounted<Device> { 39 public: 40 struct WiFiInterface { 41 WiFiInterface() : iface_index(0), iface_type(0) {} 42 WiFiInterface(const std::string& in_iface_name, 43 const std::string& in_device_name, 44 uint32_t in_iface_index, 45 uint32_t in_iface_type) 46 : iface_name(in_iface_name), 47 device_name(in_device_name), 48 iface_index(in_iface_index), 49 iface_type(in_iface_type) {} 50 std::string iface_name; 51 std::string device_name; 52 uint32_t iface_index; 53 uint32_t iface_type; 54 bool Equals(const WiFiInterface& other) const { 55 return this->iface_name == other.iface_name && 56 this->device_name == other.device_name && 57 this->iface_index == other.iface_index && 58 this->iface_type == other.iface_type; 59 } 60 }; 61 62 struct BandCapability { 63 std::vector<uint32_t> frequencies; 64 uint16_t ht_capability_mask; 65 uint16_t vht_capability_mask; 66 }; 67 68 Device(Manager* manager, 69 const std::string& device_name, 70 int identifier); 71 virtual ~Device(); 72 73 // Register/deregister WiFi interface on this device. 74 virtual void RegisterInterface(const WiFiInterface& interface); 75 virtual void DeregisterInterface(const WiFiInterface& interface); 76 77 // Parse device capability from NL80211 message. 78 void ParseWiphyCapability(const shill::Nl80211Message& msg); 79 80 // Claim ownership of this device for AP operation. When |full_control| is 81 // set to true, this will claim all interfaces reside on this device. 82 // When it is set to false, this will only claim the interface used for AP 83 // operation. 84 virtual bool ClaimDevice(bool full_control); 85 // Release any claimed interfaces. 86 virtual bool ReleaseDevice(); 87 88 // Return true if interface with |interface_name| resides on this device, 89 // false otherwise. 90 virtual bool InterfaceExists(const std::string& interface_name); 91 92 // Get HT and VHT capability string based on the operating channel. 93 // Return true and set the output capability string if such capability 94 // exist for the band the given |channel| is in, false otherwise. 95 virtual bool GetHTCapability(uint16_t channel, std::string* ht_cap); 96 virtual bool GetVHTCapability(uint16_t channel, std::string* vht_cap); 97 98 void SetDeviceName(const std::string& device_name); 99 std::string GetDeviceName() const; 100 void SetPreferredApInterface(const std::string& interface_name); 101 std::string GetPreferredApInterface() const; 102 void SetInUse(bool in_use); 103 bool GetInUse() const; 104 105 int identifier() const { return identifier_; } 106 107 private: 108 friend class DeviceTest; 109 110 // Get the HT secondary channel location base on the primary channel. 111 // Return true and set the output |above| flag if channel is valid, 112 // otherwise return false. 113 static bool GetHTSecondaryChannelLocation(uint16_t channel, bool* above); 114 115 // Determine preferred interface to used for AP operation based on the list 116 // of interfaces reside on this device 117 void UpdatePreferredAPInterface(); 118 119 // Get the capability for the band the given |channel| is in. Return true 120 // and set the output |capability| pointer if such capability exist for the 121 // band the given |channel| is in, false otherwise. 122 bool GetBandCapability(uint16_t channel, BandCapability* capability); 123 124 Manager* manager_; 125 126 // List of WiFi interfaces live on this device (PHY). 127 std::vector<WiFiInterface> interface_list_; 128 129 // Flag indicating if this device supports AP mode interface or not. 130 bool supports_ap_mode_; 131 132 // Wiphy band capabilities. 133 std::vector<BandCapability> band_capability_; 134 135 // List of claimed interfaces. 136 std::set<std::string> claimed_interfaces_; 137 138 // Unique device identifier. 139 int identifier_; 140 141 // Adaptor for communicating with remote clients. 142 std::unique_ptr<DeviceAdaptorInterface> adaptor_; 143 144 DISALLOW_COPY_AND_ASSIGN(Device); 145 }; 146 147 } // namespace apmanager 148 149 #endif // APMANAGER_DEVICE_H_ 150