blob: 51fb432b2891720ed01d95d28c298810f931155c [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 Qiu1ec4e7e2015-09-17 21:49:00 -070029#include "dbus_bindings/org.chromium.apmanager.Device.h"
Peter Qiufb39ba42014-11-21 09:09:59 -080030
31namespace apmanager {
32
Peter Qiu7e0ffcf2014-12-02 12:53:27 -080033class Manager;
34
Peter Qiufb39ba42014-11-21 09:09:59 -080035// Abstraction for WiFi Device (PHY). Each device can have one or more
36// interfaces defined on it.
37class Device : public base::RefCounted<Device>,
38 public org::chromium::apmanager::DeviceAdaptor,
39 public org::chromium::apmanager::DeviceInterface {
40 public:
41 struct WiFiInterface {
42 WiFiInterface() : iface_index(0), iface_type(0) {}
43 WiFiInterface(const std::string& in_iface_name,
44 const std::string& in_device_name,
45 uint32_t in_iface_index,
46 uint32_t in_iface_type)
47 : iface_name(in_iface_name),
48 device_name(in_device_name),
49 iface_index(in_iface_index),
50 iface_type(in_iface_type) {}
51 std::string iface_name;
52 std::string device_name;
53 uint32_t iface_index;
54 uint32_t iface_type;
55 bool Equals(const WiFiInterface& other) const {
56 return this->iface_name == other.iface_name &&
57 this->device_name == other.device_name &&
58 this->iface_index == other.iface_index &&
59 this->iface_type == other.iface_type;
60 }
61 };
62
Peter Qiu8e785b92014-11-24 10:01:08 -080063 struct BandCapability {
64 std::vector<uint32_t> frequencies;
65 uint16_t ht_capability_mask;
66 uint16_t vht_capability_mask;
67 };
68
Peter Qiu7e0ffcf2014-12-02 12:53:27 -080069 Device(Manager* manager, const std::string& device_name);
Peter Qiufb39ba42014-11-21 09:09:59 -080070 virtual ~Device();
71
72 // Register Device DBus object.
73 void RegisterAsync(
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070074 brillo::dbus_utils::ExportedObjectManager* object_manager,
Peter Qiuc9ce1f12014-12-05 11:14:29 -080075 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -070076 brillo::dbus_utils::AsyncEventSequencer* sequencer,
Peter Qiufb39ba42014-11-21 09:09:59 -080077 int device_identifier);
78
79 // Register/deregister WiFi interface on this device.
80 virtual void RegisterInterface(const WiFiInterface& interface);
81 virtual void DeregisterInterface(const WiFiInterface& interface);
82
Peter Qiu1ff67a72014-11-22 07:06:10 -080083 // Parse device capability from NL80211 message.
84 void ParseWiphyCapability(const shill::Nl80211Message& msg);
Peter Qiufb39ba42014-11-21 09:09:59 -080085
Peter Qiu0ca183b2015-03-09 13:41:06 -070086 // Claim ownership of this device for AP operation. When |full_control| is
87 // set to true, this will claim all interfaces reside on this device.
88 // When it is set to false, this will only claim the interface used for AP
89 // operation.
90 virtual bool ClaimDevice(bool full_control);
91 // Release any claimed interfaces.
Peter Qiufb39ba42014-11-21 09:09:59 -080092 virtual bool ReleaseDevice();
93
94 // Return true if interface with |interface_name| resides on this device,
95 // false otherwise.
96 virtual bool InterfaceExists(const std::string& interface_name);
97
Peter Qiu8e785b92014-11-24 10:01:08 -080098 // Get HT and VHT capability string based on the operating channel.
99 // Return true and set the output capability string if such capability
100 // exist for the band the given |channel| is in, false otherwise.
101 virtual bool GetHTCapability(uint16_t channel, std::string* ht_cap);
102 virtual bool GetVHTCapability(uint16_t channel, std::string* vht_cap);
103
Peter Qiufb39ba42014-11-21 09:09:59 -0800104 private:
105 friend class DeviceTest;
106
Peter Qiu8e785b92014-11-24 10:01:08 -0800107 // Get the HT secondary channel location base on the primary channel.
108 // Return true and set the output |above| flag if channel is valid,
109 // otherwise return false.
110 static bool GetHTSecondaryChannelLocation(uint16_t channel, bool* above);
111
Peter Qiufb39ba42014-11-21 09:09:59 -0800112 // Determine preferred interface to used for AP operation based on the list
113 // of interfaces reside on this device
114 void UpdatePreferredAPInterface();
115
Peter Qiu8e785b92014-11-24 10:01:08 -0800116 // Get the capability for the band the given |channel| is in. Return true
117 // and set the output |capability| pointer if such capability exist for the
118 // band the given |channel| is in, false otherwise.
119 bool GetBandCapability(uint16_t channel, BandCapability* capability);
120
Peter Qiu7e0ffcf2014-12-02 12:53:27 -0800121 Manager* manager_;
122
Peter Qiufb39ba42014-11-21 09:09:59 -0800123 // List of WiFi interfaces live on this device (PHY).
124 std::vector<WiFiInterface> interface_list_;
125
126 dbus::ObjectPath dbus_path_;
Alex Vakulenko8d0c31b2015-10-13 09:14:24 -0700127 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
Peter Qiufb39ba42014-11-21 09:09:59 -0800128
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 Qiufb39ba42014-11-21 09:09:59 -0800138 DISALLOW_COPY_AND_ASSIGN(Device);
139};
140
141} // namespace apmanager
142
143#endif // APMANAGER_DEVICE_H_