-
Notifications
You must be signed in to change notification settings - Fork 3
/
btle.h
162 lines (124 loc) · 4.97 KB
/
btle.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <string>
#include <vector>
#include <bluetoothapis.h>
#include <bluetoothleapis.h>
#include "base.h"
namespace btle {
inline
bool operator==(const BTH_LE_UUID& x, const BTH_LE_UUID& y) {
if (x.IsShortUuid) {
return y.IsShortUuid && x.Value.ShortUuid == y.Value.ShortUuid;
} else {
return !y.IsShortUuid && x.Value.LongUuid == y.Value.LongUuid;
}
}
struct DeviceInfo {
DeviceInfo() {
address.ullLong = BLUETOOTH_NULL_ADDRESS;
}
std::wstring path;
std::string id;
std::string friendly_name;
BLUETOOTH_ADDRESS address;
};
class Device;
class Service;
class Characteristic;
class CharacteristicValue;
class Descriptor;
class DescriptorValue;
class Characteristic : public RefCounted<Characteristic> {
public:
explicit Characteristic(const BTH_LE_GATT_CHARACTERISTIC& characteristic) : characteristic_(characteristic) {
}
const BTH_LE_GATT_CHARACTERISTIC& info() const { return characteristic_; }
BTH_LE_GATT_CHARACTERISTIC& info() { return characteristic_; }
const scoped_refptr<CharacteristicValue>& value() const { return value_; }
void set_value(const scoped_refptr<CharacteristicValue>& value) { value_ = value; }
const std::vector<scoped_refptr<Descriptor>>& descriptors() const { return descriptors_; }
std::vector<scoped_refptr<Descriptor>>& descriptors() { return descriptors_; }
private:
BTH_LE_GATT_CHARACTERISTIC characteristic_;
scoped_refptr<CharacteristicValue> value_;
std::vector<scoped_refptr<Descriptor>> descriptors_;
};
class Service : public RefCounted<Service> {
public:
explicit Service(const BTH_LE_GATT_SERVICE& service) : service_(service) {
}
const BTH_LE_GATT_SERVICE& info() const { return service_; }
BTH_LE_GATT_SERVICE& info() { return service_; }
const std::vector<scoped_refptr<Characteristic>>& characteristics() const { return characteristics_; }
std::vector<scoped_refptr<Characteristic>>& characteristics() { return characteristics_; }
scoped_refptr<Characteristic> FindCharacteristic(const BTH_LE_UUID& uuid) {
for(std::vector<scoped_refptr<Characteristic>>::const_iterator it = characteristics_.begin(); it != characteristics_.end(); it++) {
if ((*it)->info().CharacteristicUuid == uuid)
return (*it);
}
return scoped_refptr<Characteristic>();
}
private:
BTH_LE_GATT_SERVICE service_;
std::vector<scoped_refptr<Characteristic>> characteristics_;
};
class Device : public RefCounted<Device> {
public:
explicit Device(const DeviceInfo& device) : device_(device) {
}
const DeviceInfo& info() const { return device_; }
const std::vector<scoped_refptr<Service>>& services() const { return services_; }
std::vector<scoped_refptr<Service>>& services() { return services_; }
scoped_refptr<Service> FindService(const BTH_LE_UUID& uuid) {
for(std::vector<scoped_refptr<Service>>::const_iterator it = services_.begin(); it != services_.end(); it++) {
if ((*it)->info().ServiceUuid == uuid)
return (*it);
}
return scoped_refptr<Service>();
}
private:
DeviceInfo device_;
std::vector<scoped_refptr<Service>> services_;
};
class CharacteristicValue : public RefCounted<CharacteristicValue> {
public:
explicit CharacteristicValue() {
}
explicit CharacteristicValue(scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>& value) : value_(value.Pass()) {
}
const BTH_LE_GATT_CHARACTERISTIC_VALUE& info() const { return *value_.get(); }
BTH_LE_GATT_CHARACTERISTIC_VALUE& info() { return *value_.get(); }
void SetByte(UINT value) {
SetData(&value, sizeof(UINT8));
}
void SetData(UINT* data, size_t size) {
size_t required_length = size + offsetof(BTH_LE_GATT_CHARACTERISTIC_VALUE, Data);
BTH_LE_GATT_CHARACTERISTIC_VALUE* gatt_value = reinterpret_cast<BTH_LE_GATT_CHARACTERISTIC_VALUE*>(new UINT8[required_length]);
gatt_value->DataSize = size;
memcpy(gatt_value->Data, data, size);
value_.set(gatt_value);
}
private:
scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE> value_;
};
class Descriptor : public RefCounted<Descriptor> {
public:
explicit Descriptor(const BTH_LE_GATT_DESCRIPTOR& descriptor) : descriptor_(descriptor) {
}
const BTH_LE_GATT_DESCRIPTOR& info() const { return descriptor_; }
BTH_LE_GATT_DESCRIPTOR& info() { return descriptor_; }
const scoped_refptr<DescriptorValue>& value() const { return value_; }
void set_value(const scoped_refptr<DescriptorValue>& value) { value_ = value; }
private:
BTH_LE_GATT_DESCRIPTOR descriptor_;
scoped_refptr<DescriptorValue> value_;
};
class DescriptorValue : public RefCounted<DescriptorValue> {
public:
explicit DescriptorValue(scoped_ptr<BTH_LE_GATT_DESCRIPTOR_VALUE>& value) : value_(value.Pass()) {
}
const BTH_LE_GATT_DESCRIPTOR_VALUE& info() const { return *value_.get(); }
BTH_LE_GATT_DESCRIPTOR_VALUE& info() { return *value_.get(); }
private:
scoped_ptr<BTH_LE_GATT_DESCRIPTOR_VALUE> value_;
};
}