-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBoxCOM.cpp
More file actions
226 lines (203 loc) · 5.34 KB
/
BoxCOM.cpp
File metadata and controls
226 lines (203 loc) · 5.34 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
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
/*
* Copyright (c) 2012 Yeelink.net by dapingliu <dapingliu@yeelink.net>
*
* This file contains the SSDP service on YeeBox. This service is used to finding
* the bridge by mobile APP.
*/
#include "BoxCOM.h"
//do setup here
void BoxCOM::begin(CommonData* cmdata) {
this->_cmdata = cmdata;
// this->_lastHBTime = 0;
Serial.begin(BOX_COM_BAUD);
//Check zigbee is alive
this->_cmdata->com->write("HB\n");
this->_cmdata->_waitStartTime = millis();
}
//used in main loop
void BoxCOM::loop() {
// if(millis() - this->_lastHBTime > HB_TIMEOUT) {
//now we lost HB with CC2530
//there is an issue that when run long time, Arduino UART will in a
//unknown state, and can not send/recv data correctly, we should reset
//Serial to do a workaround
// Serial.end();
// Serial.begin(BOX_COM_BAUD);
// }
while (Serial.available()) {
char c = Serial.read();
if ((c == '\n') && this->_rxSize < (BOXCOM_RX_BUF_MAX_SIZE - 2)) {
this->_rxBuf[this->_rxSize] = c;
this->_rxSize++;
// a message has been get out, handle it
this->processMessage();
//reset buffer pointer
this->_rxSize = 0;
break;
}
else if(this->_rxSize >= (BOXCOM_RX_BUF_MAX_SIZE - 2)) {
//buffer overflow, clear buffer
this->_rxSize = 0;
this->_rxBuf[this->_rxSize] = c;
this->_rxSize++;
}
else {
this->_rxBuf[this->_rxSize] = c;
this->_rxSize++;
}
}
}
int BoxCOM::write(const char *str) {
// Serial.flush();
return Serial.write(str);
}
int BoxCOM::write(const char *buffer, unsigned int size) {
// Serial.flush();
return Serial.write((uint8_t*)buffer, size);
}
void BoxCOM::processMessage() {
uint8_t p = 0;
// message is splited by blank space to 2 parts:
// 1: message method
// 2: message parameter
while(p < _rxSize){
if(_rxBuf[p] == 0x20) {
p++;
break;
}
else {
p++;
}
}
// Status
if(0 == strncmp((char*)_rxBuf, "S ", 2)) {
//this->_cmdata->com->write("SB\n");
onStatusMsg((char*)&_rxBuf[p], this->_rxSize - p, false);
// this->_lastHBTime = millis();
return;
}
// Heartbeat
else if(0 == strncmp((char*)_rxBuf, "HB", 2)) {
_cmdata->com->write("HACK\n");
// this->_lastHBTime = millis();
return;
}
else if( (0 == strncmp((char*)_rxBuf, "TB", 2)) || (0 == strncmp((char*)_rxBuf, "RTB", 3))){
_cmdata->mobile->sendMessageToClient(_rxBuf, _rxSize);
return;
}
// Heartbeat ACK
else if(0 == strncmp((char*)_rxBuf, "HACK", 4)) {
// this->_lastHBTime = millis();
if(_cmdata->_mode == BOXMODE_INIT){
//During box setup, if receives HB ACK, send "R" to get the current zigbee status.
_cmdata->com->write("R\n");
// When reveives zigbee status in BOXMODE_INIT, it indicates yeebox setup is
// completed. Change the box status to BOXMODE_NORMAL.
_cmdata->_mode = BOXMODE_INIT_WAIT;
_cmdata->_waitStartTime = millis();
}
return;
}
// New
else if(0 == strncmp((char*)_rxBuf, "NEW", 3)) {
onStatusMsg((char*)&_rxBuf[p], _rxSize - p, true);
// this->_lastHBTime = millis();
return;
}
}
bool BoxCOM::onStatusMsg(char *msg, uint8_t len, bool addNewFlag) {
uint8_t p = 0;
uint8_t hardwareType = 0;
char *mac = NULL;
char *type = NULL;
char *online = NULL;
char *R = NULL;
char *G = NULL;
char *B = NULL;
char *L = NULL;
char *effect = NULL;
char *powerStatus = NULL;
char *LQI = NULL;
//get mac addr str
mac = msg;
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
//get type
type = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
//get online
online = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
hardwareType = (unsigned short)atoi(type);
if(*online == '1'){
switch(hardwareType) {
case HARDWARE_RGBLED:
//get R
R = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
//get G
G = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
//get B
B = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
//get L
L = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0';
if(p >= len) return false;
//get efftct
effect = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
//get LQI
LQI = &msg[p];
while((p < len) && (msg[p] != '\n')) { p++; }
msg[p] = '\0'; p++;
break;
case HARDWARE_POWERSOCKET:
//get socket status
powerStatus = &msg[p];
while((p < len) && (msg[p] != ',')) { p++; }
msg[p] = '\0'; p++;
if(p >= len) return false;
//get LQI
LQI = &msg[p];
while((p < len) && (msg[p] != '\n')) { p++; }
msg[p] = '\0'; p++;
break;
default:
return false;
break;
}
}
else { //online == '0'
;
}
//Add new device if required.
if(addNewFlag){
if(this->_cmdata->addNewDevice(mac, hardwareType) == false) {
//already exists, only update status
this->_cmdata->logic->updateDeviceStatus(mac, hardwareType, online, R, G, B, L, powerStatus, LQI, false);
return false;
}
}
this->_cmdata->logic->updateDeviceStatus(mac, hardwareType, online, R, G, B, L, powerStatus, LQI, addNewFlag);
return true;
}
//void BoxCOM::onCtrlBackMsg(char *msg, uint8_t len) {
// this->_cmdata->mobile->sendMessageToClient((uint8_t*)msg, len);
//}