summaryrefslogtreecommitdiff
path: root/device.h
blob: 36be3480856b95f3f4ca68ba96a765fdaa819397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef APMANAGER_DEVICE_H_
#define APMANAGER_DEVICE_H_

#include <set>
#include <string>
#include <vector>

#include <base/macros.h>
#include <base/memory/ref_counted.h>
#include <shill/net/byte_string.h>
#include <shill/net/nl80211_message.h>

#include "apmanager/dbus_adaptors/org.chromium.apmanager.Device.h"

namespace apmanager {

class Manager;

// Abstraction for WiFi Device (PHY). Each device can have one or more
// interfaces defined on it.
class Device : public base::RefCounted<Device>,
               public org::chromium::apmanager::DeviceAdaptor,
               public org::chromium::apmanager::DeviceInterface {
 public:
  struct WiFiInterface {
    WiFiInterface() : iface_index(0), iface_type(0) {}
    WiFiInterface(const std::string& in_iface_name,
                  const std::string& in_device_name,
                  uint32_t in_iface_index,
                  uint32_t in_iface_type)
        : iface_name(in_iface_name),
          device_name(in_device_name),
          iface_index(in_iface_index),
          iface_type(in_iface_type) {}
    std::string iface_name;
    std::string device_name;
    uint32_t iface_index;
    uint32_t iface_type;
    bool Equals(const WiFiInterface& other) const {
      return this->iface_name == other.iface_name &&
             this->device_name == other.device_name &&
             this->iface_index == other.iface_index &&
             this->iface_type == other.iface_type;
    }
  };

  struct BandCapability {
    std::vector<uint32_t> frequencies;
    uint16_t ht_capability_mask;
    uint16_t vht_capability_mask;
  };

  Device(Manager* manager, const std::string& device_name);
  virtual ~Device();

  // Register Device DBus object.
  void RegisterAsync(
      chromeos::dbus_utils::ExportedObjectManager* object_manager,
      const scoped_refptr<dbus::Bus>& bus,
      chromeos::dbus_utils::AsyncEventSequencer* sequencer,
      int device_identifier);

  // Register/deregister WiFi interface on this device.
  virtual void RegisterInterface(const WiFiInterface& interface);
  virtual void DeregisterInterface(const WiFiInterface& interface);

  // Parse device capability from NL80211 message.
  void ParseWiphyCapability(const shill::Nl80211Message& msg);

  // Claim ownership of this device for AP operation. When |full_control| is
  // set to true, this will claim all interfaces reside on this device.
  // When it is set to false, this will only claim the interface used for AP
  // operation.
  virtual bool ClaimDevice(bool full_control);
  // Release any claimed interfaces.
  virtual bool ReleaseDevice();

  // Return true if interface with |interface_name| resides on this device,
  // false otherwise.
  virtual bool InterfaceExists(const std::string& interface_name);

  // Get HT and VHT capability string based on the operating channel.
  // Return true and set the output capability string if such capability
  // exist for the band the given |channel| is in, false otherwise.
  virtual bool GetHTCapability(uint16_t channel, std::string* ht_cap);
  virtual bool GetVHTCapability(uint16_t channel, std::string* vht_cap);

 private:
  friend class DeviceTest;

  // Get the HT secondary channel location base on the primary channel.
  // Return true and set the output |above| flag if channel is valid,
  // otherwise return false.
  static bool GetHTSecondaryChannelLocation(uint16_t channel, bool* above);

  // Determine preferred interface to used for AP operation based on the list
  // of interfaces reside on this device
  void UpdatePreferredAPInterface();

  // Get the capability for the band the given |channel| is in. Return true
  // and set the output |capability| pointer if such capability exist for the
  // band the given |channel| is in, false otherwise.
  bool GetBandCapability(uint16_t channel, BandCapability* capability);

  Manager* manager_;

  // List of WiFi interfaces live on this device (PHY).
  std::vector<WiFiInterface> interface_list_;

  dbus::ObjectPath dbus_path_;
  std::unique_ptr<chromeos::dbus_utils::DBusObject> dbus_object_;

  // Flag indicating if this device supports AP mode interface or not.
  bool supports_ap_mode_;

  // Wiphy band capabilities.
  std::vector<BandCapability> band_capability_;

  // List of claimed interfaces.
  std::set<std::string> claimed_interfaces_;

  DISALLOW_COPY_AND_ASSIGN(Device);
};

}  // namespace apmanager

#endif  // APMANAGER_DEVICE_H_