-
Notifications
You must be signed in to change notification settings - Fork 10
/
gatt_client_process.h
141 lines (118 loc) · 4.45 KB
/
gatt_client_process.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
/* mbed Microcontroller Library
* Copyright (c) 2006-2019 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GATT_CLIENT_PROCESS_H_
#define GATT_CLIENT_PROCESS_H_
#include "ble_process.h"
using namespace std::literals::chrono_literals;
/**
* Simple GattClient wrapper.
* It will alternate between advertising and scanning to obtain a connection to GattServer.
*/
class GattClientProcess : public BLEProcess
{
public:
GattClientProcess(events::EventQueue &event_queue, BLE &ble_interface) :
BLEProcess(event_queue, ble_interface)
{
}
/** Name we advertise as */
const char* get_device_name() override
{
static const char name[] = "GattClient";
return name;
}
/** Name of device we want to connect to */
const char* get_peer_device_name()
{
static const char name[] = "GattServer";
return name;
}
private:
/** Alternate between scanning and advertising */
virtual void start_activity()
{
static bool scan = true;
if (scan) {
_event_queue.call([this]() { start_scanning(); });
} else {
_event_queue.call([this]() { start_advertising(); });
}
scan = !scan;
_is_connecting = false;
}
/** scan for GattServer */
void start_scanning()
{
ble::ScanParameters scan_params;
_ble.gap().setScanParameters(scan_params);
ble_error_t ret = _ble.gap().startScan(ble::scan_duration_t(ble::millisecond_t(4000)));
if (ret == ble_error_t::BLE_ERROR_NONE) {
printf("Started scanning for \"%s\"\r\n", get_peer_device_name());
} else {
printf("Starting scan failed\r\n");
}
}
/** Restarts main activity */
void onScanTimeout(const ble::ScanTimeoutEvent &event) override {
start_activity();
}
/** Check advertising report for name and connect to any device with the name GattServer */
void onAdvertisingReport(const ble::AdvertisingReportEvent &event) override {
/* don't bother with analysing scan result if we're already connecting */
if (_is_connecting) {
return;
}
if (!event.getType().connectable()) {
/* we're only interested in connectable devices */
return;
}
ble::AdvertisingDataParser adv_data(event.getPayload());
/* parse the advertising payload, looking for a discoverable device */
while (adv_data.hasNext()) {
ble::AdvertisingDataParser::element_t field = adv_data.next();
/* connect to a discoverable device */
if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME) {
if (field.value.size() == strlen(get_peer_device_name()) &&
(memcmp(field.value.data(), get_peer_device_name(), field.value.size()) == 0)) {
printf("We found \"%s\", connecting...\r\n", get_peer_device_name());
ble_error_t error = _ble.gap().stopScan();
if (error) {
print_error(error, "Error caused by Gap::stopScan");
return;
}
const ble::ConnectionParameters connection_params;
error = _ble.gap().connect(
event.getPeerAddressType(),
event.getPeerAddress(),
connection_params
);
if (error) {
_ble.gap().startScan();
return;
}
/* we may have already scan events waiting
* to be processed so we need to remember
* that we are already connecting and ignore them */
_is_connecting = true;
return;
}
}
}
}
private:
bool _is_connecting = false;
};
#endif /* GATT_CLIENT_PROCESS_H_ */