-
Notifications
You must be signed in to change notification settings - Fork 12
/
Mobile.cpp
332 lines (296 loc) · 8.18 KB
/
Mobile.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
/*
* Copyright (c) 2012 Yeelink.net by dapingliu <[email protected]>
*
* This file contains the interface service Mobile APP. This service is used
* to commucate with APP for recieving control commands and sending lights
* status.
*/
#include "Mobile.h"
#include "EepromManage.h"
#include <avr/EEPROM.h>
Mobile::~Mobile() {
delete this->_mobileIntf;
this->_mobileIntf = NULL;
}
//do setup here
void Mobile::begin(CommonData* cmdata) {
this->cmdata = cmdata;
this->_mobileIntf = new EthernetServer(MOBILEINTPORT);
this->_mobileIntf->begin();
this->_rxSize = 0;
// connHBTime = 0;
}
//used in main loop
void Mobile::loop() {
EthernetClient _conn = this->_mobileIntf->available();
if (_conn) {
if (_conn.connected()) {
while (_conn.available()) {
//read on char each time
char c = _conn.read();
if ((c == '\n') || (this->_rxSize >= (MOBILE_RX_BUF_SIZE-2))) {
this->_rxBuf[this->_rxSize] = c;
this->_rxSize++;
this->cmdata->leds->setLocalFastFlash(500);
// a message has been get out, handle it
this->processMessage(&_conn, this->_rxBuf, this->_rxSize);
//reset buffer pointer
this->_rxSize = 0;
}
else {
this->_rxBuf[this->_rxSize] = c;
this->_rxSize++;
}
}
//no more data in W5100 buffer
this->_rxSize = 0;;
}
else {
_conn.stop();
}
}
}
// handle one message in buffer
void Mobile::processMessage(EthernetClient* conn, uint8_t* buf, uint8_t buf_size){
uint8_t p = 0;
// message is splited by blank space to 2 parts:
// 1: message method
// 2: message parameter
while(p < buf_size){
if(buf[p] == 0x20) {
break;
}
else {
p++;
}
}
// Heart beat
if(0 == strncmp((char*)buf, "HB", 2)) {
conn->print("HACK\r\n");
return;
}
if(0 == strncmp((char*)buf, "T ", 2)) {
cmdata->com->write((char*)buf);
// conn->print("TB\r\n");
return;
}
if(0 == strncmp((char*)buf, "RT", 2)) {
cmdata->com->write((char*)buf);
// conn->print("RTB 0001,10,0\r\n");
return;
}
// Get List
if(0 == strncmp((char*)buf, "GL", 2)) {
this->_printDeviceList(conn);
return;
}
// Control
if(0 == strncmp((char*)buf, "C ", 2)) {
uint8_t len = buf_size - p;
char *mac = NULL;
char *R = NULL;
char *G = NULL;
char *B = NULL;
char *L = NULL;
char *effect = NULL;
// send message to COM
this->cmdata->com->write((char*)buf, buf_size);
// replay with Control Back
conn->print("CB\r\n");
//get mac addr str
mac = (char*)&buf[++p];
while((p < len) && (buf[p] != ',')) { p++; }
buf[p] = '\0'; p++;
//get R
R = (char*)&buf[p];
while((p < len) && (buf[p] != ',')) { p++; }
buf[p] = '\0'; p++;
//get G
G = (char*)&buf[p];
while((p < len) && (buf[p] != ',')) { p++; }
buf[p] = '\0'; p++;
//get B
B = (char*)&buf[p];
while((p < len) && (buf[p] != ',')) { p++; }
buf[p] = '\0'; p++;
//get L
L = (char*)&buf[p];
while((p < len) && (buf[p] != ',')) { p++; }
buf[p] = '\0'; p++;
//get effect
effect = (char*)&buf[p];
while((p < len) && (buf[p] != ',')) { p++; }
buf[p] = '\0'; p++;
//store status into deviceList
for(int i=0; i<this->cmdata->deviceCount; i++) {
if(0 == strncmp(mac, this->cmdata->deviceList[i].MAC, DEVICE_IP_LEN)) {
switch(this->cmdata->deviceList[i].type)
{
case HARDWARE_RGBLED:
if (*R && *G && *B) {
this->cmdata->deviceList[i].R = atoi(R);
this->cmdata->deviceList[i].G = atoi(G);
this->cmdata->deviceList[i].B = atoi(B);
}
if(*L) { this->cmdata->deviceList[i].L = atoi(L); }
if(*effect) { this->cmdata->deviceList[i].effect = atoi(effect); }
break;
case HARDWARE_POWERSOCKET:
if(*L == '0') {
this->cmdata->deviceList[i].powerStatus = false;
}
else {
this->cmdata->deviceList[i].powerStatus = true;
}
break;
default:
break;
}
break;
}
}
return;
}
// Discover
// when get discover message from APP, send a DSC message to zigbee
// coordinator for light searching
if(0 == strncmp((char*)buf, "D", 1)) {
this->cmdata->com->write("DSC\n");
conn->print("DB\r\n");
return;
}
// When receives "ADD" msg, try to add the new device.
/* if(0 == strncmp((char*)buf, "ADD", 3)) {
char *mac;
eeprom_device_list_t deviceList;
char buffer[25];
char *type;
// If max lamps exceed, just return
if(this->cmdata->deviceCount > MAX_SUPPORTED_LAMPS-1 )
return;
//get mac
mac = (char*)&buf[++p];
while((buf[p] != ',')) { p++; }
buf[p] = '\0'; p++;
//get type
type = (char*)&buf[p++];
buf[p] = '\0';
// If the mac is already exists, just return
for(int i=0; i<this->cmdata->deviceCount; i++) {
if(0 == strncmp(mac, this->cmdata->deviceList[i].MAC, DEVICE_IP_LEN)) {
return;
}
}
(void)strncpy((char*)deviceList.mac, mac, E2DA_ZIGBEE_MAC_LEN);
(void)strncpy(this->cmdata->deviceList[this->cmdata->deviceCount].MAC, mac, E2DA_ZIGBEE_MAC_LEN);
//get type
deviceList.type = atoi(type);
this->cmdata->deviceList[this->cmdata->deviceCount].type = deviceList.type;
//Init other field
this->cmdata->deviceList[this->cmdata->deviceCount].R = 255;
this->cmdata->deviceList[this->cmdata->deviceCount].G = 255;
this->cmdata->deviceList[this->cmdata->deviceCount].B = 255;
this->cmdata->deviceList[this->cmdata->deviceCount].L = 100;
this->cmdata->deviceList[this->cmdata->deviceCount].effect = 0;
this->cmdata->deviceList[this->cmdata->deviceCount].online = false;
this->cmdata->deviceList[this->cmdata->deviceCount].powerStatus = 0;
this->cmdata->deviceList[this->cmdata->deviceCount].LQI = 0;
this->cmdata->deviceCount++;
// Add new device to EEPROM
EepromManage::AddEepromDeviceList(deviceList);
sprintf(buffer, "DMADD %s\n", mac);
this->cmdata->com->write(buffer);
// conn->print(mac);
conn->print("AB\r\n");
return;
}*/
// EEPROMDUMP
// dump 1024 bytes EEPROM data and output to connection by hexes
/*f(0 == strncmp((char*)buf, "EEPROMDUMP", 10)) {
for(uint16_t i=0; i<MAX_EEPROM_SIZE; i++) {
char data[2];
sprintf(data, "%02X", eeprom_read_byte((uint8_t*)i));
conn->print(data);
}
conn->print("\n");
return;
}
// Clear EEPROM data
if(0 == strncmp((char*)buf, "EEPROMRESET", 11)) {
for(uint16_t i=0; i<MAX_EEPROM_SIZE; i++) {
eeprom_write_byte((unsigned char*)i, 0xff);
}
return;
}*/
// Refresh
// When get a R message, box will send a DSC to get all devices and
// wait for 4s to get all the S messages from COM, then add MAC address
// into EEPROM if it does not been
if(0 == strncmp((char*)buf, "R", 1)) {
this->cmdata->com->write("R\n");
return;
}
return;
}
// send message to available connection
void Mobile::sendMessageToClient(uint8_t* msg, uint8_t len) {
this->_mobileIntf->write(msg, len);
}
//check if exceeds 2 local connection, drop them
void Mobile::dropMobileClent(byte num) {
byte connSock[MAX_SOCK_NUM], j=0;
bool maxConnFlag = false;
for (byte sock = 0; sock < MAX_SOCK_NUM; sock++) {
EthernetClient client(sock);
if (EthernetClass::_server_port[sock] == MOBILEINTPORT) {
if(client.connected()){
connSock[j++] = sock;
if(j==2) {
maxConnFlag = true;
break;
}
}
}
}
if(maxConnFlag) {
for(j=0;j<num;j++){
EthernetClient client(connSock[j]);
client.stop();
}
}
}
void Mobile::_printDeviceList(EthernetClient* conn) {
conn->print("GLB ");
for(int i=0; i<this->cmdata->deviceCount; i++) {
conn->write((uint8_t*)this->cmdata->deviceList[i].MAC, DEVICE_IP_LEN);
conn->print(",");
conn->print(this->cmdata->deviceList[i].type);
conn->print(",");
conn->print(this->cmdata->deviceList[i].online ? "1" : "0");
conn->print(",");
conn->print(this->cmdata->deviceList[i].LQI);
conn->print(",");
switch(this->cmdata->deviceList[i].type) {
case HARDWARE_RGBLED:
conn->print(this->cmdata->deviceList[i].R);
conn->print(",");
conn->print(this->cmdata->deviceList[i].G);
conn->print(",");
conn->print(this->cmdata->deviceList[i].B);
conn->print(",");
conn->print(this->cmdata->deviceList[i].L);
conn->print(",");
conn->print(this->cmdata->deviceList[i].effect);
conn->print(";");
break;
case HARDWARE_POWERSOCKET:
conn->print(this->cmdata->deviceList[i].powerStatus);
conn->print(";");
break;
default:
conn->print("0,0,0,0,0;");
break;
}
}
conn->print("\r\n");
}