-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbleUART.cpp
More file actions
170 lines (159 loc) · 3.98 KB
/
Copy pathbleUART.cpp
File metadata and controls
170 lines (159 loc) · 3.98 KB
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
163
164
165
166
167
168
169
170
#include "bleUART.h"
#include "config.h"
bool bleUART::discoverDevice(BLEDevice dev_) {
Serial.print("Test: ");
Serial.println(dev_.address());
if(dev_.address() != addr) {
Serial.println("Not my device...");
return false;
}
Serial.print("My device: ");
Serial.println(dev_.localName());
if(found) {
Serial.println("Already found...");
} else {
// copy device
device = dev_;
found = true;
}
return true;
}
bool bleUART::open(void) {
if(!found) {
Serial.println("Not discovered! Can't open!");
return false;
}
// connect
if(!device.connect()) {
Serial.print("Failed to connect to: ");
Serial.println(addr);
// tell run not to try reconnect for this time....
reconnect_time = millis();
return false;
}
Serial.println("Connected...");
connected = true;
reconnect_time = 0;
connect_time = millis();
connect_time_fuzz = connect_time & 0x400;
connect_time_fuzz *= 100; // semi random time up to ~100s
// get service
if(!device.discoverService(svcUUID.c_str())) {
Serial.print("Could not find service ");
Serial.print(svcUUID);
Serial.print(" for device ");
Serial.println(addr);
close();
return false;
}
Serial.print("Found service: ");
Serial.println(svcUUID);
// get read characteristic
rdCHAR = device.characteristic(rdUUID.c_str());
if(!rdCHAR) {
Serial.print("Could not find read characteristic ");
Serial.print(rdUUID);
Serial.print(" for device ");
Serial.println(addr);
close();
return false;
}
Serial.println("Found read characteristic");
if(!rdCHAR.canSubscribe()) {
Serial.print("Read characteristic ");
Serial.print(rdUUID);
Serial.print(" not subscribable for device ");
Serial.println(addr);
close();
return false;
}
if(!rdCHAR.subscribe()) {
Serial.print("Subscribe failed for read characteristic ");
Serial.print(rdUUID);
Serial.print(" for device ");
Serial.println(addr);
close();
return false;
}
Serial.println("Subscribed");
// get write characteristic
wrCHAR = device.characteristic(wrUUID.c_str());
if(!wrCHAR) {
Serial.print("Could not find write characteristic ");
Serial.print(wrUUID);
Serial.print(" for device ");
Serial.println(addr);
close();
return false;
}
Serial.println("Found write characteristic");
if(!wrCHAR.canWrite()) {
Serial.print("Write characteristic ");
Serial.print(wrUUID);
Serial.print(" not writable for device ");
Serial.println(addr);
close();
return false;
}
return true;
}
void bleUART::close(void) {
if(!found) return;
if(connected) {
device.disconnect();
}
connected = false;
}
int bleUART::run(void) {
// sanity check
if(!found) {
Serial.println("Attempt to run UART which was not discovered?");
return -1;
}
// connect
if(!connected) {
// disconnect lockout - not actually an error, but do nothing...
if(reconnect_time && millis() - reconnect_time < 500UL) return 0;
Serial.print("try connect ");
Serial.println(myId());
if(!open()) {
Serial.print("Failed to open device: ");
Serial.println(myId());
return -1;
}
}
// test for restart time
if(millis() - connect_time > BAT_CFG_BLE_MAX_CONNECT_TIME + connect_time_fuzz) {
Serial.print("CONNECTED TOO LONG - RESTART: ");
Serial.println(myId());
close();
return 0;
}
return 1;
}
int bleUART::read(uint8_t * buf, int len) {
if(!connected) return -1;
// check for read data?
if(!rdCHAR.valueUpdated()) return 0;
int i = rdCHAR.readValue(buf, len);
if(i == 0) {
Serial.print("Read failed: ");
Serial.println(myId());
//close();
return -1;
}
return i;
}
int bleUART::write(uint8_t * buf, int len) {
if(!connected) return -1;
if(!wrCHAR.writeValue(buf, len)) {
Serial.print("Write failed: ");
Serial.println(myId());
//close();
return -1;
}
int i = wrCHAR.valueLength();
//Serial.print("Wrote: ");
//Serial.println(i);
return i;
}