blob: 36f605b7601d7e837d88830153e27029657d04a0 [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
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//
Peter Qiufb39ba42014-11-21 09:09:59 -080016
17#ifndef APMANAGER_DEVICE_H_
18#define APMANAGER_DEVICE_H_
19
Peter Qiu0ca183b2015-03-09 13:41:06 -070020#include <set>
Peter Qiufb39ba42014-11-21 09:09:59 -080021#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
Peter Qiuf9335402015-11-16 12:09:16 -080029#include "apmanager/device_adaptor_interface.h"
Peter Qiufb39ba42014-11-21 09:09:59 -080030
31namespace apmanager {
32
Peter Qiuf9335402015-11-16 12:09:16 -080033class ControlInterface;
Peter Qiu7e0ffcf2014-12-02 12:53:27 -080034class Manager;
35
Peter Qiufb39ba42014-11-21 09:09:59 -080036// Abstraction for WiFi Device (PHY). Each device can have one or more
37// interfaces defined on it.
Peter Qiuf9335402015-11-16 12:09:16 -080038class Device : public base::RefCounted<Device> {
Peter Qiufb39ba42014-11-21 09:09:59 -080039 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
Peter Qiu8e785b92014-11-24 10:01:08 -080062 struct BandCapability {
63 std::vector<uint32_t> frequencies;
64 uint16_t ht_capability_mask;
65 uint16_t vht_capability_mask;
66 };
67
Peter Qiuf9335402015-11-16 12:09:16 -080068 Device(Manager* manager,
69 const std::string& device_name,
70 int identifier);
Peter Qiufb39ba42014-11-21 09:09:59 -080071 virtual ~Device();
72
Peter Qiufb39ba42014-11-21 09:09:59 -080073 // Register/deregister WiFi interface on this device.
74 virtual void RegisterInterface(const WiFiInterface& interface);
75 virtual void DeregisterInterface(const WiFiInterface& interface);
76
Peter Qiu1ff67a72014-11-22 07:06:10 -080077 // Parse device capability from NL80211 message.
78 void ParseWiphyCapability(const shill::Nl80211Message& msg);
Peter Qiufb39ba42014-11-21 09:09:59 -080079
Peter Qiu0ca183b2015-03-09 13:41:06 -070080 // 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.
Peter Qiufb39ba42014-11-21 09:09:59 -080086 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
Peter Qiu8e785b92014-11-24 10:01:08 -080092 // 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
Peter Qiuf9335402015-11-16 12:09:16 -080098 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
Peter Qiufb39ba42014-11-21 09:09:59 -0800107 private:
108 friend class DeviceTest;
109
Peter Qiu8e785b92014-11-24 10:01:08 -0800110 // 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
Peter Qiufb39ba42014-11-21 09:09:59 -0800115 // Determine preferred interface to used for AP operation based on the list
116 // of interfaces reside on this device
117 void UpdatePreferredAPInterface();
118
Peter Qiu8e785b92014-11-24 10:01:08 -0800119 // 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
Peter Qiu7e0ffcf2014-12-02 12:53:27 -0800124 Manager* manager_;
125
Peter Qiufb39ba42014-11-21 09:09:59 -0800126 // List of WiFi interfaces live on this device (PHY).
127 std::vector<WiFiInterface> interface_list_;
128
Peter Qiu3d95ac72014-12-06 18:26:18 -0800129 // Flag indicating if this device supports AP mode interface or not.
130 bool supports_ap_mode_;
131
Peter Qiu8e785b92014-11-24 10:01:08 -0800132 // Wiphy band capabilities.
133 std::vector<BandCapability> band_capability_;
134
Peter Qiu0ca183b2015-03-09 13:41:06 -0700135 // List of claimed interfaces.
136 std::set<std::string> claimed_interfaces_;
137
Peter Qiuf9335402015-11-16 12:09:16 -0800138 // Unique device identifier.
139 int identifier_;
140
141 // Adaptor for communicating with remote clients.
142 std::unique_ptr<DeviceAdaptorInterface> adaptor_;
143
Peter Qiufb39ba42014-11-21 09:09:59 -0800144 DISALLOW_COPY_AND_ASSIGN(Device);
145};
146
147} // namespace apmanager
148
149#endif // APMANAGER_DEVICE_H_