-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHmBle.cpp
213 lines (181 loc) · 4.42 KB
/
HmBle.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "HmBle.hpp"
void HmBle::readAll() {
if (bleSerial->available() > 0) {
do {
cmdSerial->write(bleSerial->read());
} while (bleSerial->available() > 0);
cmdSerial->println();
}
}
void HmBle::send(const char* cmd, bool echo) {
if (echo) {
cmdSerial->printf("> %s\r\n", cmd);
}
bleSerial->print(cmd);
delay(200);
readAll();
}
void HmBle::sendf(const char* fmt, ...) {
char buf[32];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
send(buf);
}
void HmBle::usage() {
cmdSerial->print(
"Commands:\r\n"
"info Show info\r\n"
"wakeup Wakup device from sleep mode\r\n"
"reboot Reboot\r\n"
"factory Perform factory reset\r\n"
"beacon [name] Configure as beacon\r\n"
"beaconl [name] Configure as beacon with powersaving\r\n"
"slave Configure as peripheral device\r\n"
"master [addr] Configure as central device and connect to addr\r\n"
"console Serial console\r\n"
);
}
void HmBle::handleInput() {
char *line, *cmd, *args;
if (cmdSerial->available() <= 0) {
// No input
return;
}
if (!(line = conEmu.read_key(cmdSerial->read()))) {
// Line not complete (yet)
return;
}
// Tokenize string to get command
if (!(cmd = strtok(line, " "))) {
promt();
return;
}
switch (str2int(cmd)) {
case str2int("info"):
case str2int("i"):
info();
break;
case str2int("wakeup"):
case str2int("w"):
wakeup();
break;
case str2int("reboot"):
case str2int("r"):
send("AT+RESET");
break;
case str2int("factory"):
case str2int("f"):
factoryReset();
break;
case str2int("beacon"):
case str2int("b"):
if ((args = strtok(NULL, "")))
configureBeacon(args);
break;
case str2int("beaconl"):
case str2int("bl"):
if ((args = strtok(NULL, ""))) {
configureBeacon(args);
configureBeaconPowersave();
}
break;
case str2int("slave"):
case str2int("s"):
configureSlave();
break;
case str2int("master"):
case str2int("m"):
if ((args = strtok(NULL, "")))
configureMaster(args);
break;
case str2int("console"):
case str2int("c"):
console();
break;
case str2int("help"):
case str2int("h"):
usage();
break;
default:
if (strlen(cmd) >= 2 && !strncmp(cmd, "AT", 2)) {
// Send AT command
send(cmd);
} else {
usage();
}
break;
}
// Print promt
promt();
}
void HmBle::info() {
send("AT");
send("AT+VERR?");
send("AT+ADDR?");
send("AT+NAME?");
send("AT+MODE?");
send("AT+ROLE?");
send("AT+IBEA?");
send("AT+BAUD?");
send("AT+RADD?");
}
void HmBle::wakeup() {
// Send a long string (length > 80 or more) to wakeup the device
send("I am iron man, I am iron man, I am iron man, I am iron man, I am iron man, I am iron man...");
}
void HmBle::factoryReset() {
send("AT+RENEW"); // Factory reset
send("AT+RESET"); // Reboot
delay(5000); // Wait for reboot
send("AT"); // Check status
}
void HmBle::configureBeacon(char* args) {
send("AT+MARJ0x0A00"); // iBeacon Major number
send("AT+MINO0x00A0"); // iBeacon Minor number
sendf("AT+NAME%s", args); // Set Name
send("AT+ADVI5"); // Advertising interval
send("AT+ADTY0"); // Advertising type connectable
send("AT+IBEA1"); // Enable iBeacon mode
send("AT+DELO2"); // Broadcast-only
send("AT+RESET"); // Reboot
/* UUID
send("AT+IBE074278BDA"); // Set Beacon UUID1
send("AT+IBE1B6444520"); // Set Beacon UUID2
send("AT+IBE28F0C720E"); // Set Beacon UUID3
send("AT+IBE3AF059935"); // Set Beacon UUID4
*/
}
void HmBle::configureBeaconPowersave() {
send("AT+ADVI5"); // Advertising interval
send("AT+ADTY3"); // Disable connection
send("AT+PWRM0"); // Enable auto-sleep
}
void HmBle::configureSlave() {
send("AT+ROLE0"); // Set role to peripheral
send("AT+ADDR?"); // Show addr
}
void HmBle::configureMaster(char* args) {
send("AT+ROLE1"); // Set role to central
sendf("AT+CON%s", args); // Set device to connect to
}
void HmBle::console() {
char key;
while (true) {
if (bleSerial->available() > 0) {
key = bleSerial->read();
if (key == '\r') {
cmdSerial->write('\n');
}
cmdSerial->write(key);
}
if (cmdSerial->available() > 0) {
key = cmdSerial->read();
bleSerial->write(key);
}
}
}
void HmBle::promt() {
cmdSerial->write("\r\n# ");
}