-
Notifications
You must be signed in to change notification settings - Fork 0
/
BP35A1.cpp
414 lines (394 loc) · 16.8 KB
/
BP35A1.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "BP35A1.hpp"
#include "SkSendTo.hpp"
bool BP35A1::settingRegister(const RegisterNum registerNum, const String &arg) {
char c[32];
snprintf(c, sizeof(c), "S%X %s", registerNum, arg.c_str());
String s = String(c);
return this->execCommand(SKCmd::setRegister, &s) > 0 && this->returnOk() ? true : false;
}
BP35A1::BP35A1(String ID, String Password, int uart_nr)
: HardwareSerial(uart_nr), WPassword(Password), WID(ID) {}
void BP35A1::setStatusChangeCallback(void (*callback)(SkStatus)) {
this->callback = callback;
}
size_t BP35A1::execCommand(const SKCmd skCmdNum, const String *const arg) {
String command = arg == nullptr ? this->skCmd[skCmdNum] : this->skCmd[skCmdNum] + " " + *arg;
log_d(">> %s", command.c_str());
size_t ret = this->println(command);
this->flush();
return ret;
}
bool BP35A1::connectionLoop(const uint32_t timeoutms, const uint32_t delayms) {
static int scanRetryCounter = 0;
if (this->callback != NULL) {
this->callback(this->skStatus);
}
switch (this->skStatus) {
case SkStatus::uninitialized:
default:
if (this->configuration(1)) {
this->skStatus = SkStatus::scanning;
}
break;
case SkStatus::scanning: {
switch (this->scanStatus) {
case ScanStatus::uninitialized:
default:
if (this->scanning(scanRetryCounter + 3)) {
this->scanStatus = ScanStatus::waitBeacon;
}
break;
case ScanStatus::waitBeacon:
if (this->waitEvent(&this->beaconEventCallback, timeoutms, delayms)) {
this->scanStatus = ScanStatus::checkScanResult;
} else {
scanRetryCounter++;
this->scanStatus = ScanStatus::uninitialized;
}
break;
case ScanStatus::checkScanResult:
this->scanStatus = this->parseScanResult() ? ScanStatus::scanned : ScanStatus::uninitialized;
break;
}
if (this->scanStatus == ScanStatus::scanned) {
this->skStatus = SkStatus::connecting;
}
break;
}
case SkStatus::connecting:
switch (this->connectStatus) {
case ConnectStatus::uninitialized:
default: {
this->execCommand(SKCmd::convertMac2IPv6, &this->CommunicationParameter.macAddress);
std::vector<String> response;
if (this->waitResponse(&response, 1) && response.front().length() == 40) {
this->CommunicationParameter.ipv6Address = response.front();
this->CommunicationParameter.ipv6Address.trim();
this->connectStatus = ConnectStatus::getIpv6;
}
} break;
case ConnectStatus::getIpv6:
if (this->settingRegister(RegisterNum::ChannelNumber, this->CommunicationParameter.channel) && this->settingRegister(RegisterNum::PanId, this->CommunicationParameter.panId)) {
this->connectStatus = ConnectStatus::setComunicationParam;
} else {
this->connectStatus = ConnectStatus::uninitialized;
}
break;
case ConnectStatus::setComunicationParam:
this->execCommand(SKCmd::joinSKStack, &this->CommunicationParameter.ipv6Address);
this->connectStatus = this->returnOk() ? ConnectStatus::waitSuccessPANA : ConnectStatus::uninitialized;
break;
case ConnectStatus::waitSuccessPANA:
this->connectStatus = this->waitEvent(&this->panaEventCallback, 100000) ? ConnectStatus::connected : ConnectStatus::uninitialized;
break;
}
if (this->connectStatus == ConnectStatus::connected) {
this->skStatus = SkStatus::connected;
}
break;
case SkStatus::connected:
return true;
}
return false;
}
/// @brief 初期化
/// @details Wi-SUNアダプタと接続して通信可能な状態にする
bool BP35A1::initialize(const uint32_t tryTimes) {
uint32_t retryCount = 0;
while (retryCount < tryTimes) {
if (this->callback != NULL) {
this->callback(this->skStatus);
}
log_d("Initialize status : %d", this->skStatus);
switch (this->skStatus) {
case SkStatus::uninitialized:
default:
if (this->configuration(1)) {
this->skStatus = SkStatus::scanning;
} else {
retryCount++;
}
break;
case SkStatus::scanning:
if (this->scan(1)) {
this->skStatus = SkStatus::connecting;
} else {
retryCount++;
}
break;
case SkStatus::connecting:
if (this->connect(1)) {
this->skStatus = SkStatus::connected;
} else {
retryCount++;
}
break;
}
if (this->skStatus == SkStatus::connected) {
this->printParam();
break;
}
}
return this->skStatus == SkStatus::connected;
}
bool BP35A1::waitEvent(const std::vector<Event::Callback> *const callback, const uint32_t timeoutms, const uint32_t delayms) {
std::vector<String> response;
String terminator = "EVENT";
while (1) {
response.clear();
if (this->waitResponse(&response, 0, &terminator, timeoutms, delayms)) {
for (String line : response) {
Event receivedEvent = Event(line.c_str(), line.length());
switch (receivedEvent.doEvent(callback)) {
case Event::CallbackResult::Success:
return true;
case Event::CallbackResult::Failed:
return false;
default:
if (receivedEvent.type != Event::Type::Invalid)
log_d("Received Event : %02X", receivedEvent.type);
break;
}
}
}
}
}
bool BP35A1::connect(const uint32_t tryTimes) {
uint32_t retryCount = 0;
while (retryCount < tryTimes) {
log_d("Connect status : %d", this->connectStatus);
switch (this->connectStatus) {
case ConnectStatus::uninitialized:
default: {
this->execCommand(SKCmd::convertMac2IPv6, &this->CommunicationParameter.macAddress);
std::vector<String> response;
if (this->waitResponse(&response, 1) && response.front().length() == 40) {
this->CommunicationParameter.ipv6Address = response.front();
this->CommunicationParameter.ipv6Address.trim();
this->connectStatus = ConnectStatus::getIpv6;
}
}
retryCount++;
break;
case ConnectStatus::getIpv6:
if (this->settingRegister(RegisterNum::ChannelNumber, this->CommunicationParameter.channel) && this->settingRegister(RegisterNum::PanId, this->CommunicationParameter.panId)) {
this->connectStatus = ConnectStatus::setComunicationParam;
} else {
this->connectStatus = ConnectStatus::uninitialized;
}
break;
case ConnectStatus::setComunicationParam:
this->execCommand(SKCmd::joinSKStack, &this->CommunicationParameter.ipv6Address);
this->connectStatus = this->returnOk() ? ConnectStatus::waitSuccessPANA : ConnectStatus::uninitialized;
break;
case ConnectStatus::waitSuccessPANA:
this->connectStatus = this->waitEvent(&this->panaEventCallback, 100000) ? ConnectStatus::connected : ConnectStatus::uninitialized;
break;
}
if (this->connectStatus == ConnectStatus::connected) {
break;
}
}
return this->connectStatus == ConnectStatus::connected;
}
void BP35A1::printParam() {
log_d("BP35A1 Version: %s", this->eVer.c_str());
log_d("MAC: %s", this->CommunicationParameter.macAddress.c_str());
log_d("Channel: %s", this->CommunicationParameter.channel.c_str());
log_d("PanID %s", this->CommunicationParameter.panId.c_str());
log_d("MAC %s", this->CommunicationParameter.macAddress.c_str());
log_d("IPv6 %s", this->CommunicationParameter.ipv6Address.c_str());
log_d("dest IPv6 %s", this->CommunicationParameter.destIpv6Address.c_str());
log_d("BP35A1 %s", this->eVer.c_str());
}
bool BP35A1::scanning(const uint32_t duration) {
char s[16];
snprintf(s, sizeof(s), "%d %X %d", (uint8_t)this->scanMode, this->scanChannelMask, duration);
String arg = String(s);
this->execCommand(SKCmd::scanSKStack, &arg);
return this->returnOk();
}
bool BP35A1::scan(const uint32_t tryTimes) {
uint32_t retryCount = 0;
static int scanRetryCounter = 0;
while (retryCount < tryTimes) {
log_d("Scan status : %d", this->scanStatus);
switch (this->scanStatus) {
case ScanStatus::uninitialized:
default:
if (this->scanning(scanRetryCounter + 3)) {
this->scanStatus = ScanStatus::waitBeacon;
}
retryCount++;
break;
case ScanStatus::waitBeacon:
if (this->waitEvent(&this->beaconEventCallback, scanRetryCounter * 10000)) {
this->scanStatus = ScanStatus::checkScanResult;
} else {
scanRetryCounter++;
this->scanStatus = ScanStatus::uninitialized;
}
break;
case ScanStatus::checkScanResult:
this->scanStatus = this->parseScanResult() ? ScanStatus::scanned : ScanStatus::uninitialized;
break;
}
if (this->scanStatus == ScanStatus::scanned) {
break;
}
}
return this->scanStatus == ScanStatus::scanned;
}
bool BP35A1::configuration(const uint32_t tryTimes) {
uint32_t retryCount = 0;
while (retryCount < tryTimes) {
log_d("Initialize status : %d", this->initializeStatus);
switch (this->initializeStatus) {
case InitializeStatus::uninitialized:
default: {
this->execCommand(SKCmd::terminateSKStack);
this->execCommand(SKCmd::resetSKStack);
this->execCommand(SKCmd::disableEcho);
this->execCommand(SKCmd::getSKStackVersion);
std::vector<String> response;
String terminator = "EVER";
if (waitResponse(&response, 0, &terminator)) {
this->eVer = response.front();
this->initializeStatus = InitializeStatus::getSkVer;
}
}
retryCount++;
break;
case InitializeStatus::getSkVer:
this->execCommand(SKCmd::setSKStackPassword, &this->WPassword);
this->initializeStatus = this->returnOk() ? InitializeStatus::setSkSetpwd : InitializeStatus::uninitialized;
break;
case InitializeStatus::setSkSetpwd:
this->execCommand(SKCmd::setSKStackID, &this->WID);
this->initializeStatus = this->returnOk() ? InitializeStatus::checkDataFormat : InitializeStatus::uninitialized;
break;
case InitializeStatus::checkDataFormat: {
const String terminator = "OK 01";
this->execCommand(SKCmd::readOpt);
this->initializeStatus = this->waitResponse(nullptr, 0, &terminator) ? InitializeStatus::initialized : InitializeStatus::setDataFormat;
} break;
case InitializeStatus::setDataFormat: {
const String arg = "01";
this->execCommand(SKCmd::writeOpt, &arg);
this->initializeStatus = this->returnOk() ? InitializeStatus::initialized : InitializeStatus::uninitialized;
} break;
}
if (this->initializeStatus == InitializeStatus::initialized) {
break;
}
}
return this->initializeStatus == InitializeStatus::initialized;
}
void BP35A1::discardBuffer(const uint32_t delayms) {
delay(delayms);
while (this->available()) {
this->read();
}
}
bool BP35A1::returnOk(const uint32_t timeoutms, const uint32_t delayms) {
const String terminator = "OK";
return this->waitResponse(nullptr, 0, &terminator, timeoutms, delayms);
}
/// @brief レスポンス待機
/// @param response 文字列を格納用ベクタ
/// @param lines レスポンス行数上限
/// @param terminator レスポンス待機終了文字
/// @param timeoutms レスポンス待機タイムアウト
/// @param delayms 待機時間
bool BP35A1::waitResponse(std::vector<String> *const response, const uint32_t lines, const String *const terminator, const uint32_t timeoutms, const uint32_t delayms) {
uint32_t timeout = 0;
uint32_t linecounter = 0;
while (timeoutms == 0 || timeout < timeoutms) {
while (this->available()) {
// 1行読み込み
String line = this->readStringUntil('\n');
log_d("<< %s", line.c_str());
// response指定時は結果を格納
if (response != nullptr) {
response->push_back(line);
}
linecounter++;
// 行数指定時は行数チェック
if (lines != 0) {
if (linecounter >= lines) {
return true;
}
} else {
// 終端文字指定時は文字存在チェック
if (terminator != nullptr) {
if (line.indexOf(*terminator) > -1) {
return true;
}
} else {
// 何も指定がない場合はFAIL / OKチェック
if (line.indexOf("FAIL ER") > -1) {
log_d("Command execute error.");
this->discardBuffer();
return false;
} else if (line.indexOf("OK") > -1) {
return true;
}
}
}
}
timeout += delayms;
delay(delayms);
}
log_d("Timeout");
return false;
}
bool BP35A1::parseScanResult() {
std::vector<String> response;
String terminator = "PairID";
if (waitResponse(&response, 0, &terminator)) {
for (String line : response) {
if (line.indexOf("Channel:") > -1) {
this->CommunicationParameter.channel = line.substring(line.indexOf("Channel:") + 8);
this->CommunicationParameter.channel.trim();
} else if (line.indexOf("Pan ID:") > -1) {
this->CommunicationParameter.panId = line.substring(line.indexOf("Pan ID:") + 7);
this->CommunicationParameter.panId.trim();
} else if (line.indexOf("Addr:") > -1) {
this->CommunicationParameter.macAddress = line.substring(line.indexOf("Addr:") + 5);
this->CommunicationParameter.macAddress.trim();
}
}
if (this->CommunicationParameter.macAddress.isEmpty() || this->CommunicationParameter.channel.isEmpty() || this->CommunicationParameter.panId.isEmpty()) {
return false;
} else {
return true;
}
} else {
return false;
}
}
bool BP35A1::sendUdpData(const uint8_t *data, const uint16_t length, const uint32_t delayms, const uint32_t timeoutms) {
// send request
skSendTo udpData = skSendTo(length, this->CommunicationParameter.ipv6Address);
this->print(udpData.getSendString());
this->write(data, length);
this->print("\r\n");
// dump log
char logBuffer[length * 2 + 1];
memset(logBuffer, '\0', sizeof(logBuffer));
for (size_t i = 0; i < length; i++) {
snprintf(&logBuffer[i * 2], sizeof(logBuffer) - (i * 2), "%02X", data[i]);
}
log_d(">> %s%s", udpData.getSendString().c_str(), logBuffer);
// send check
return this->waitEvent(&this->udpSendEventCallback, timeoutms, delayms);
}
ErxUdp BP35A1::getUdpData(const uint32_t delayms, const uint32_t timeoutms) {
std::vector<String> response;
String terminator = "ERXUDP " + this->CommunicationParameter.ipv6Address;
if (this->waitResponse(&response, 0, &terminator, timeoutms, delayms)) {
// receive response
return ErxUdp(response.back());
}
return ErxUdp();
}