-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlServer.cpp
245 lines (191 loc) · 5.88 KB
/
ControlServer.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
#include "ControlServer.h"
#include "APServer.h"
#include <vector>
#include "TimeClock.h"
#include "Streaming.h"
using namespace std;
extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "user_interface.h"
#include "cont.h"
}
SINGLETON_CPP(ControlServer)
ControlServer::ControlServer(){
configuration = singleton(Configuration);
configuration->addObserver(this);
buffer = NULL;
server = NULL;
setup();
}
void ControlServer::setup(){
lastPacketTime = millis();
serverDiscovered = false;
serverIsAlive = true;
if (buffer)
delete [] buffer;
if (server) {
delete server;
}
server = new WiFiServer(configuration->ControlServer->port);
server->begin();
buffer = new char[configuration->ControlServer->packetLength];
obtainServerEndpoint();
}
bool ControlServer::incomingCommand(){
bool packetReceived = false;
if (client.connected()){
packetReceived = client.available();
}else{
client = server->available();
packetReceived = client.connected() && client.available();
}
unsigned long actualTime = millis();
if (packetReceived){
lastPacketTime = actualTime;
serverIsAlive = true;
}else if (actualTime - lastPacketTime >= configuration->ControlServer->keepAliveSeconds*1000){
serverIsAlive = false;
}
return packetReceived;
}
void ControlServer::externalCommandReceived(){
lastPacketTime = millis();
}
SenderoControlHeader ControlServer::processCommand(){
if (!client.connected()){
Serial.println("NO CONNECTION!!!");
}
SenderoControlHeader header;
int qty = client.readBytes((byte*)&header,header.size());
if (qty != header.size()){
Serial.println("Lei menos del cabezal!!!!");
}
SenderoControlHeader::Command command = header.type();
TimeClock* clock = singleton(TimeClock);
Serial.println(header.toString());
if (header.requestClockFlag){
/* we should review the first sync. If the offset is smaller than the
calculated with the streaming packets, we could be in trouble.
Because this first sync has the huge error of RTT */
unsigned long currentTime = clock->rawTime();
byte time[4];
writeBuffer<unsigned long>(time,currentTime);
client.write((uint8_t*)&header,header.size());
client.write((uint8_t*)&time,sizeof(time));
}
if (header.requestStatsFlag){
Serial.println("el ratee");
Serial.println(configuration->Stats->bitRate);
/* stats are dirty if streaming is active */
configuration->Stats->dirty = singleton(Streaming)->active;
String stats = configuration->Stats->toString();
Serial.println("requestStatsFlag");
Serial.println(stats);
client.write((uint8_t*)&header,header.size());
client.write((uint8_t*)stats.c_str(),stats.length() + 1);
}
if (header.clockCorrectionOffsetFlag){
byte offsetBytes[4];
client.readBytes((byte*)offsetBytes,4);
long offset = readBuffer<long>(offsetBytes);
// clock->addServerOffsetSample(offset, false,0,0,0);
}
if (header.configurationFlag){
int i = 0;
while(true){
yield();
byte b = client.read();
if (b == -1){
delay(100);
continue;
}
buffer[i] = (char)b;
if (buffer[i] == '\0'){
break;
}
i++;
}
String configs = String(buffer);
Dictionary dict = parseParameters(configs);
configuration->setValues(dict,false);
Serial.println(String("Configs setted! ") + configs);
}
if (header.closeConnectionFlag){
client.stop();
Serial.println("Connection closed!");
}
return header;
}
void ControlServer::obtainServerEndpoint(){
Serial.println("Discovering server on port " + String(configuration->ControlServer->discoveryPort) + "...");
APServer* ap = singleton(APServer);
while(true){
yield();
/* allow connections from AP */
ap->handleClient();
// send broadcast packet asking IP and Port of server
Serial.println("Sending message to register");
IPAddress ip = WiFi.localIP();
ip[3] = 255;
int res = udp.beginPacket(ip,configuration->ControlServer->discoveryPort);
String data = "Device: " + String(configuration->Device->number);
udp.write(data.c_str(),data.length());
udp.endPacket();
// wait for response
LOOP_UNTIL(2000){
yield();
/* allow connections from AP */
ap->handleClient();
if (client = server->available()){
configuration->Streaming->serverIP = client.remoteIP().toString();
Serial.printf("got client!, %s\n", configuration->Streaming->serverIP.c_str());
Serial.println("waiting for configurations and clockSync...");
/* receive initial configuration */
bool configurationReceived = false;
bool clockSync = false;
while (!configurationReceived){
yield();
/* allow connections from AP */
ap->handleClient();
if (incomingCommand()){
SenderoControlHeader h = processCommand();
configurationReceived = configurationReceived || h.configurationFlag;
clockSync = clockSync || h.clockCorrectionOffsetFlag;
}
}
Serial.println("Going for streaming!");
return;
}
}
}
}
void ControlServer::commandReceivedFlashLed(){
flashLed(200,150,5);
}
ControlServer::~ControlServer(){
if (buffer)
delete []buffer;
singleton(Configuration)->removeObserver(this);
}
void ControlServer::configurationChanged(){
Serial.println("ControlServer::configurationChanged()");
setup();
}
template<typename T> T ControlServer::readBuffer(void* buffer){
int size = sizeof(T);
T result = 0;
for(int i = 0; i < size; i++){
byte b = ((byte*)buffer)[i];
result |= (b << (i*8));
}
return result;
}
template<typename T> void ControlServer::writeBuffer(void* buffer, T data){
int size = sizeof(T);
for(int i = 0; i < size; i++){
((byte*)buffer)[i] = (byte)((data >> (i*8)) & 0xFF);
}
}