forked from PiInTheSky/lora-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
353 lines (283 loc) · 8.54 KB
/
server.c
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
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h> // Standard input/output definitions
#include <string.h> // String function definitions
#include <fcntl.h> // File control definitions
#include <unistd.h> // UNIX standard function definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
#include <stdint.h>
#include <stdlib.h>
#include <dirent.h>
#include <math.h>
#include <pthread.h>
#include <wiringPi.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "server.h"
#include "config.h"
#include "global.h"
#include "gateway.h"
extern bool run;
void EncryptMessage(char *Code, char *Message)
{
int i, Len;
Len = strlen(Code);
if (Len > 0)
{
i = 0;
while (*Message)
{
*Message = (*Message ^ Code[i]) | 0x80;
Message++;
i = (i + 1) % Len;
}
}
}
void ProcessJSONClientLine(int connfd, char *line)
{
line[strcspn(line, "\r\n")] = '\0'; // Get rid of CR LF
LogMessage("Received '%s' from JSON client\n", line);
if (strchr(line, '=') != NULL)
{
// Setting
char *setting, *value, *saveptr = NULL;
setting = strtok_r(line, "=", &saveptr);
value = strtok_r( NULL, "\n", &saveptr);
SetConfigValue(setting, value);
}
else if (strchr(line, ':') != NULL)
{
// Command with parameters
char *command, *value, *saveptr = NULL;
command = strtok_r(line, ":", &saveptr);
value = strtok_r(NULL, "\n", &saveptr);
if (strcasecmp(command, "send") == 0)
{
int channel;
channel = *value != '0';
value++;
LogMessage("LoRa[%d]: To send '%s'\n", channel, value);
EncryptMessage(Config.UplinkCode, value);
strcpy(Config.LoRaDevices[channel].UplinkMessage, value);
}
}
else
{
// single-word request
if (strcasecmp(line, "settings") == 0)
{
int Index;
char SettingName[64], SettingValue[256], packet[4096];
LogMessage("Responding to settings request\n");
Index = 0;
packet[0] = '\0';
while (SettingAsString(Index, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue)))
{
char temp[300];
sprintf(temp, "{\"class\":\"SET\",\"set\":\"%s\",\"val\":%s}\r\n", SettingName, SettingValue);
if ((strlen(temp) + strlen(packet)) < sizeof(packet))
{
strcat(packet, temp);
}
Index++;
}
send(connfd, packet, strlen(packet), MSG_NOSIGNAL);
}
else if (strcasecmp(line, "save") == 0)
{
LogMessage("Saving Settings\n");
SaveConfigFile();
}
}
}
int SendJSON(int connfd)
{
int Channel, PayloadIndex, port_closed;
char sendBuff[4000], line[400];
port_closed = 0;
sendBuff[0] = '\0';
// Send any packets that we've not sent yet
for (PayloadIndex=0; PayloadIndex<MAX_PAYLOADS; PayloadIndex++)
{
if (Config.Payloads[PayloadIndex].InUse && Config.Payloads[PayloadIndex].SendToClients)
{
Channel = Config.Payloads[PayloadIndex].Channel;
sprintf(line, "{\"class\":\"POSN\",\"index\":%d,\"channel\":%d,\"payload\":\"%s\",\"time\":\"%s\",\"lat\":%.5lf,\"lon\":%.5lf,\"alt\":%d,\"rate\":%.1lf,\"snr\":%d,\"rssi\":%d,\"ferr\":%.1lf,\"sentence\":\"%s\"}\r\n",
PayloadIndex,
Channel,
Config.Payloads[PayloadIndex].Payload,
Config.Payloads[PayloadIndex].Time,
Config.Payloads[PayloadIndex].Latitude,
Config.Payloads[PayloadIndex].Longitude,
Config.Payloads[PayloadIndex].Altitude,
Config.Payloads[PayloadIndex].AscentRate,
Config.LoRaDevices[Channel].PacketSNR,
Config.LoRaDevices[Channel].PacketRSSI,
Config.LoRaDevices[Channel].FrequencyError,
Config.Payloads[PayloadIndex].Telemetry);
strcat(sendBuff, line);
Config.Payloads[PayloadIndex].SendToClients = 0;
}
}
// Send Channel Status (RSSI only at present)
for (Channel=0; Channel<=1; Channel++)
{
if (Config.LoRaDevices[Channel].InUse)
{
sprintf(line, "{\"class\":\"STATS\",\"index\":%d,\"rssi\":%d}\r\n",
Channel,
Config.LoRaDevices[Channel].CurrentRSSI);
strcat(sendBuff, line);
}
}
if (!run)
{
port_closed = 1;
}
else if (sendBuff[0])
{
if (send(connfd, sendBuff, strlen(sendBuff), MSG_NOSIGNAL ) <= 0)
{
LogMessage( "Disconnected from client\n" );
port_closed = 1;
}
}
return port_closed;
}
void *ServerLoop( void *some_void_ptr )
{
static char *ChannelName[3] = {"HAB", "JSON", "DATA"};
struct sockaddr_in serv_addr;
struct TServerInfo *ServerInfo;
ServerInfo = (struct TServerInfo *)some_void_ptr;
ServerInfo->sockfd = socket( AF_INET, SOCK_STREAM, 0 );
memset( &serv_addr, '0', sizeof( serv_addr ) );
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl( INADDR_ANY );
serv_addr.sin_port = htons(ServerInfo->Port);
LogMessage( "Listening on %s port %d\n", ChannelName[ServerInfo->ServerIndex], ServerInfo->Port);
if (setsockopt(ServerInfo->sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
{
LogMessage( "setsockopt(SO_REUSEADDR) failed" );
}
if (bind(ServerInfo->sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
LogMessage( "Server failed errno %d\n", errno );
exit( -1 );
}
listen(ServerInfo->sockfd, 10);
while (run)
{
static char *ChannelName[3] = {"HAB", "JSON", "DATA"};
int MSPerLoop=100;
int ms=0;
int connfd;
Config.EnableDev=1;
fcntl(ServerInfo->sockfd, F_SETFL, fcntl(ServerInfo->sockfd, F_GETFL) & ~O_NONBLOCK); // Blocking mode so we wait for a connection
connfd = accept(ServerInfo->sockfd, ( struct sockaddr * ) NULL, NULL ); // Wait for connection
LogMessage( "Connected to %s client\n", ChannelName[ServerInfo->ServerIndex]);
ServerInfo->Connected = 1;
fcntl(connfd, F_SETFL, fcntl(ServerInfo->sockfd, F_GETFL) | O_NONBLOCK); // Non-blocking, so we don't block on receiving any commands from client
while (ServerInfo->Connected)
{
int bytecount;
ms += MSPerLoop;
// Listen part
bytecount = -1;
if (ServerInfo->ServerIndex == 0)
{
static char lines[4096];
char packet[4096];
while ((bytecount = recv(connfd, packet, sizeof(packet), 0)) > 0)
{
// JSON server
char *lf;
packet[bytecount] = 0;
strcat(lines, packet);
while ((lf = strchr(lines, '\n')) != NULL)
{
// Terminate string
*lf = '\0';
// Process this line
ProcessJSONClientLine(connfd, lines);
// Shift any other lines over
strcpy(lines, lf+1);
}
}
if (bytecount == 0)
{
// -1 is no more data, 0 means port closed
ServerInfo->Connected = 0;
}
}
else if (ServerInfo->ServerIndex == 1)
{
char RxByte;
while ((bytecount = recv(connfd, &RxByte, 1, 0)) > 0)
{
Config.LoRaDevices[Config.HABChannel].FromTelnetBuffer[Config.LoRaDevices[Config.HABChannel].FromTelnetBufferCount++] = RxByte;
LogMessage("KEYB BUFFER %d BYTES\n", Config.LoRaDevices[Config.HABChannel].FromTelnetBufferCount);
}
}
else if (ServerInfo->ServerIndex == 2)
{
char RxByte;
while ((bytecount = recv(connfd, &RxByte, 1, 0)) > 0)
{
// Nothing as we are only sending
}
}
if (bytecount == 0)
{
// -1 is no more data, 0 means port closed
LogMessage("Disconnected from %s client\n", ServerInfo->ServerIndex ? "HAB" : "JSON");
ServerInfo->Connected = 0;
}
if (ServerInfo->Connected)
{
// Send part
if (ServerInfo->ServerIndex == 0)
{
// Send to JSON client
if (SendJSON(connfd))
{
ServerInfo->Connected = 0;
}
}
else if (ServerInfo->ServerIndex == 1)
{
// Telnet port (provides Telnet-like connection to HAB)
int Channel;
for (Channel=0; Channel<=1; Channel++)
{
if (Config.LoRaDevices[Channel].ToTelnetBufferCount > 0)
{
send(connfd, Config.LoRaDevices[Channel].ToTelnetBuffer, Config.LoRaDevices[Channel].ToTelnetBufferCount, 0);
Config.LoRaDevices[Channel].ToTelnetBufferCount = 0;
}
}
}
else if (ServerInfo->ServerIndex == 2)
{
// Direct telemetry port
int Channel;
for (Channel=0; Channel<=1; Channel++)
{
if (Config.LoRaDevices[Channel].LocalDataCount > 0)
{
send(connfd, Config.LoRaDevices[Channel].LocalDataBuffer, Config.LoRaDevices[Channel].LocalDataCount, 0);
Config.LoRaDevices[Channel].LocalDataCount = 0;
}
}
}
}
delay(MSPerLoop);
}
close(connfd);
}
return NULL;
}