blob: 8324d00e8837253baae00fb9074e70c181b24344 [file] [log] [blame]
Martin Brabham58a15572019-08-21 14:18:14 -07001/******************************************************************************
2 *
3 * Copyright 2019 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18#pragma once
19
20#include <string>
21
22#include "hci/address.h"
23#include "hci/class_of_device.h"
24
25namespace bluetooth::hci {
26
27/**
28 * Used to determine device functionality
29 */
30enum DeviceType { DUAL, CLASSIC, LE };
31
32/**
33 * Represents a physical HCI device.
34 *
35 * <p>Contains all of the metadata required to represent a phycial device.
36 *
37 * <p>Devices should only be created and modified by HCI.
38 */
39class Device {
40 public:
41 virtual ~Device() = default;
42
43 Address GetAddress() const {
44 return address_;
45 }
46
47 /**
48 * Returns 1 of 3 enum values for device's type (DUAL, CLASSIC, LE)
49 */
50 DeviceType GetDeviceType() const {
51 return device_type_;
52 }
53
54 /**
55 * Unique identifier for bluetooth devices
56 *
57 * @return string representation of the uuid
58 */
59 std::string /** use UUID when ported */ GetUuid() {
60 return uid_;
61 }
62
63 std::string GetName() {
64 return name_;
65 }
66
67 ClassOfDevice GetClassOfDevice() {
68 return class_of_device_;
69 }
70
71 bool IsBonded() {
72 return is_bonded_;
73 }
74
75 bool operator==(const Device& rhs) const {
76 return this->uid_ == rhs.uid_ && this->address_ == rhs.address_ && this->device_type_ == rhs.device_type_ &&
77 this->is_bonded_ == rhs.is_bonded_;
78 }
79
80 protected:
81 friend class DeviceDatabase;
82 friend class DualDevice;
83
84 /**
85 * @param raw_address the address of the device
86 * @param device_type specify the type of device to create
87 */
88 Device(Address address, DeviceType device_type)
89 : address_(address), device_type_(device_type), uid_(generate_uid()), name_(""), class_of_device_() {}
90
91 /**
92 * Called only by friend class DeviceDatabase
93 *
94 * @param address
95 */
96 virtual void SetAddress(Address address) {
97 address_ = address;
98 uid_ = generate_uid();
99 }
100
101 /**
102 * Set the type of the device.
103 *
104 * <p>Needed by dual mode to arbitrarily set the valure to DUAL for corresponding LE/Classic devices
105 *
106 * @param type of device
107 */
108 void SetDeviceType(DeviceType type) {
109 device_type_ = type;
110 }
111
112 void SetName(std::string& name) {
113 name_ = name;
114 }
115
116 void SetClassOfDevice(ClassOfDevice class_of_device) {
117 class_of_device_ = class_of_device;
118 }
119
120 void SetIsBonded(bool is_bonded) {
121 is_bonded_ = is_bonded;
122 }
123
124 private:
125 Address address_{Address::kEmpty};
126 DeviceType device_type_;
127 std::string uid_;
128 std::string name_;
129 ClassOfDevice class_of_device_;
130 bool is_bonded_ = false;
131
132 /* Uses specific information about the device to calculate a UID */
133 std::string generate_uid();
134};
135
136} // namespace bluetooth::hci