-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathsondehub.c
executable file
·461 lines (387 loc) · 13.5 KB
/
sondehub.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#define __USE_XOPEN
#define _GNU_SOURCE
#include <time.h>
#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 <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <dirent.h>
#include <math.h>
#include <pthread.h>
#include <curl/curl.h>
#include <wiringPi.h>
#include "global.h"
#include "gateway.h"
#include "sondehub.h"
pthread_mutex_t crit = PTHREAD_MUTEX_INITIALIZER; // To protect IncomingSondehubPayloads
struct TPayload IncomingSondehubPayloads[2];
struct TPayload ActiveSondehubPayloads[2];
void SetSondehubSentence(int Channel, char *tmp)
{
pthread_mutex_lock(&crit); // lock the critical section
if (sscanf(tmp + 2, "%31[^,],%u,%8[^,],%lf,%lf,%d",
IncomingSondehubPayloads[Channel].Payload,
&IncomingSondehubPayloads[Channel].Counter,
IncomingSondehubPayloads[Channel].Time,
&IncomingSondehubPayloads[Channel].Latitude,
&IncomingSondehubPayloads[Channel].Longitude,
&IncomingSondehubPayloads[Channel].Altitude) == 6)
{
IncomingSondehubPayloads[Channel].PacketSNR = Config.LoRaDevices[Channel].PacketSNR;
IncomingSondehubPayloads[Channel].PacketRSSI = Config.LoRaDevices[Channel].PacketRSSI;
IncomingSondehubPayloads[Channel].Frequency = Config.LoRaDevices[Channel].Frequency + Config.LoRaDevices[Channel].FrequencyOffset;
strcpy(IncomingSondehubPayloads[Channel].Telemetry, tmp);
IncomingSondehubPayloads[Channel].InUse = (IncomingSondehubPayloads[Channel].Latitude != 0) && (IncomingSondehubPayloads[Channel].Longitude != 0);
}
pthread_mutex_unlock(&crit); // unlock once you are done
}
size_t sondehub_write_data( void *buffer, size_t size, size_t nmemb, void *userp )
{
// LogMessage("%s\n", (char *)buffer);
return size * nmemb;
}
int UploadJSONToServer(char *url, char *json)
{
CURL *curl;
CURLcode res;
char curl_error[CURL_ERROR_SIZE];
/* get a curl handle */
curl = curl_easy_init( );
if ( curl )
{
bool result;
struct curl_slist *headers = NULL;
int retries;
long int http_resp;
// So that the response to the curl PUT doesn't mess up my finely crafted display!
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, sondehub_write_data );
// Set the timeout
curl_easy_setopt( curl, CURLOPT_TIMEOUT, 15 );
// RJH capture http errors and report
// curl_easy_setopt( curl, CURLOPT_FAILONERROR, 1 );
curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, curl_error );
// Avoid curl library bug that happens if above timeout occurs (sigh)
curl_easy_setopt( curl, CURLOPT_NOSIGNAL, 1 );
// Set the headers
headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8" );
// PUT https://api.v2.sondehub.org/amateur/telemetry with content-type application/json
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
retries = 0;
do
{
// Perform the request, res will get the return code
res = curl_easy_perform( curl );
// Check for errors
if (res == CURLE_OK)
{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (http_resp == 200)
{
// Data submitted OK
result = true;
}
else if ((http_resp >= 201) && (http_resp <= 209))
{
//Data submitted, but with some issues. Refer to response for details.
LogMessage("20x response to %s\n", json);
LogError(http_resp, "JSON: ", json);
LogError(http_resp, "RESP: ", curl_error);
result = true; // Don't retry - this partially failed due to the uploaded JSON containing something that the server complained about
}
else if ((http_resp >= 400) && (http_resp <= 409))
{
LogMessage("%d response to %s\n", http_resp, json);
LogError(http_resp, "JSON: ", json);
LogError(http_resp, "RESP: ", curl_error);
result = true; // Don't retry - this failed due to the uploaded JSON containing something that the server rejected
}
else if ((http_resp >= 500) && (http_resp <= 509))
{
// Server busy, retry
result = false;
}
else
{
// Undocumented response; log but don't retry
LogMessage("Unexpected HTTP response %ld for URL '%s'\n", http_resp, url);
LogError(http_resp, "JSON: ", json);
LogError(http_resp, "RESP: ", curl_error);
result = false;
}
}
else
{
http_resp = 0;
LogMessage("Failed for URL '%s'\n", url);
LogMessage("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
LogMessage("error: %s\n", curl_error);
LogError(res, "JSON: ", json);
LogError(res, "RESP: ", curl_error);
// Likely a network error, so return false to requeue
result = false;
}
} while ((!result) && (++retries < 5));
// always cleanup
curl_slist_free_all( headers );
curl_easy_cleanup( curl );
return result;
}
else
{
/* CURL error, return false so we requeue */
return false;
}
}
void ExtractFields(char *Telemetry, char *ExtractedFields)
{
char Line[256], FieldList[32];
char *token;
ExtractedFields[0] = '\0';
FieldList[0] = '\0';
strcpy(Line, Telemetry);
token = strtok(Line, ",*");
while ((token != NULL) && (FieldList[0] == '\0'))
{
if (strncmp(token, "012345", 6) == 0)
{
strcpy(FieldList, token);
}
token = strtok(NULL, ",*");
}
if (FieldList[0])
{
// We have a field list field, so go through that and extract each field, adding fields to the JSON for Sondehub
int i;
char Value[64];
strcpy(Line, Telemetry);
token = strtok(Line, ",*");
for (i=1; FieldList[i]; i++)
{
token = strtok(NULL, ",*");
Value[0] = '\0';
// LogMessage("Field %d type %c is '%s'\n", i, FieldList[i], token);
switch (FieldList[i])
{
case '6':
sprintf(Value, "\"sats\":%s,", token);
break;
case '7':
sprintf(Value, "\"vel_h\":%s,", token);
break;
case '8':
sprintf(Value, "\"heading\":%s,", token);
break;
case '9':
if (strchr(token, '.'))
{
// volts
sprintf(Value, "\"batt\":%s,", token);
}
else
{
// mV --> V
sprintf(Value, "\"batt\":%.1lf,", atof(token) / 1000.0);
}
break;
case 'A':
sprintf(Value, "\"temp\":%s,", token);
break;
case 'B':
sprintf(Value, "\"ext_temperature\":%s,", token);
break;
case 'C':
sprintf(Value, "\"pred_lat\":%s,", token);
break;
case 'D':
sprintf(Value, "\"pred_lon\":%s,", token);
break;
case 'R':
sprintf(Value, "\"pressure\":%s,", token);
break;
}
if (*Value)
{
strcat(ExtractedFields, Value);
}
}
}
// LogMessage("Extracted: '%s'\n", ExtractedFields);
}
void BuildPayloadTime(char *Result, char *TimeInSentence, struct tm *tm)
{
struct tm tm2;
memcpy(&tm2, tm, sizeof(tm2));
strptime(TimeInSentence, "%H:%M:%S", &tm2);
// Test for payload time being yesterday
if ((tm2.tm_hour == 23) && (tm->tm_hour == 00))
{
tm2.tm_mday--;
timegm(&tm2);
}
strftime(Result, 32, "%Y-%0m-%0dT%H:%M:%SZ", &tm2);
}
int UploadSondehubPosition(int Channel)
{
char json[1000], now[32], payload_time[32], ExtractedFields[256], uploader_position[256];
time_t rawtime;
struct tm *tm;
// Get formatted timestamp for now
time(&rawtime);
tm = gmtime(&rawtime);
strftime(now, sizeof(now), "%Y-%0m-%0dT%H:%M:%SZ", tm);
BuildPayloadTime(payload_time, ActiveSondehubPayloads[Channel].Time, tm);
// Find field list and extract fields
ExtractFields(ActiveSondehubPayloads[Channel].Telemetry, ExtractedFields);
if ((Config.latitude >= -90) && (Config.latitude <= 90) && (Config.longitude >= -180) && (Config.longitude <= 180))
{
sprintf(uploader_position, "\"uploader_position\": ["
" %.3lf," // Listener Latitude
" %.3lf," // Listener Longitude
" %.0lf" // Listener Altitude
"],",
Config.latitude, Config.longitude, Config.altitude
);
}
else
{
uploader_position[0] = '\0';
}
// Create json as required by sondehub-amateur
sprintf(json, "[{\"software_name\": \"LoRa Gateway\"," // Fixed software name
"\"software_version\": \"%s\"," // Version
"\"uploader_callsign\": \"%s\"," // User callsign
"\"time_received\": \"%s\"," // UTC
"\"payload_callsign\": \"%s\"," // Payload callsign
"\"datetime\":\"%s\"," // UTC from payload
"\"lat\": %.5lf," // Latitude
"\"lon\": %.5lf," // Longitude
"\"alt\": %d," // Altitude
"\"frequency\": %.4lf," // Frequency
"\"modulation\": \"LoRa Mode %d\"," // Modulation
"\"snr\": %d," // SNR
"\"rssi\": %d," // RSSI
"\"raw\": \"%s\"," // Sentence
"%s"
"%s"
"\"uploader_antenna\": \"%s\""
"}]",
Config.Version, Config.Tracker, now,
ActiveSondehubPayloads[Channel].Payload, payload_time,
ActiveSondehubPayloads[Channel].Latitude, ActiveSondehubPayloads[Channel].Longitude, ActiveSondehubPayloads[Channel].Altitude,
ActiveSondehubPayloads[Channel].Frequency,
Config.LoRaDevices[Channel].SpeedMode,
ActiveSondehubPayloads[Channel].PacketSNR, ActiveSondehubPayloads[Channel].PacketRSSI,
ActiveSondehubPayloads[Channel].Telemetry,
ExtractedFields,
uploader_position,
Config.antenna);
return UploadJSONToServer("https://api.v2.sondehub.org/amateur/telemetry", json);
}
char *SanitiseCallsignForMQTT(char *Callsign)
{
static char Result[32];
char *ptr;
strcpy(Result, Callsign);
ptr = strchr(Result, '/');
while (ptr)
{
*ptr = '-';
ptr = strchr(ptr, '/');
}
return Result;
}
int UploadListenerToSondehub(void)
{
char json[1000], now[32], doc_time[32];
time_t rawtime;
struct tm *tm, *doc_tm;
// Get formatted timestamp for now
time( &rawtime );
tm = gmtime( &rawtime );
strftime( now, sizeof( now ), "%Y-%0m-%0dT%H:%M:%SZ", tm );
// Get formatted timestamp for doc timestamp
doc_tm = gmtime( &rawtime );
strftime(doc_time, sizeof( doc_time ), "%Y-%0m-%0dT%H:%M:%SZ", doc_tm);
// Create json as required by sondehub-amateur
sprintf(json, "{\"software_name\": \"LoRa Gateway\"," // Fixed software name
"\"software_version\": \"%s\"," // Version
"\"uploader_callsign\": \"%s\"," // User callsign
"\"uploader_position\": ["
" %.3lf," // Listener Latitude
" %.3lf," // Listener Longitude
" %.0lf" // Listener Altitude
"],"
"\"uploader_radio\": \"%s\","
"\"uploader_antenna\": \"%s\""
"}",
Config.Version, SanitiseCallsignForMQTT(Config.Tracker),
Config.latitude, Config.longitude, Config.altitude,
Config.radio, Config.antenna);
return UploadJSONToServer("https://api.v2.sondehub.org/amateur/listeners", json);
}
void *SondehubLoop( void *vars )
{
while (1)
{
static long ListenerCountdown = 0;
int Channel;
// Copy from incoming to active
pthread_mutex_lock(&crit); // lock the critical section
for (Channel=0; Channel<=1; Channel++)
{
// Copy new incoming payload, if there is one
if (IncomingSondehubPayloads[Channel].InUse)
{
// copy from incoming to active
memcpy(&ActiveSondehubPayloads[Channel], &IncomingSondehubPayloads[Channel], sizeof(struct TPayload));
IncomingSondehubPayloads[Channel].InUse = 0;
}
}
pthread_mutex_unlock(&crit); // unlock once you are done
// Upload from active section
for (Channel=0; Channel<=1; Channel++)
{
// Try to upload active payload, if there is one
if (ActiveSondehubPayloads[Channel].InUse)
{
ChannelPrintf(Channel, 6, 1, "SH");
UploadSondehubPosition(Channel); // Upload, with limited retries if needed
ActiveSondehubPayloads[Channel].InUse = 0;
ChannelPrintf(Channel, 6, 1, " ");
}
}
// Listener position uploads
if ((Config.latitude >= -90) && (Config.latitude <= 90) && (Config.longitude >= -180) && (Config.longitude <= 180))
{
if (--ListenerCountdown <= 0)
{
if (UploadListenerToSondehub())
{
LogMessage("Uploaded listener info to Sondehub/amateur\n");
ListenerCountdown = 216000; // Every 6 hours
}
else
{
LogMessage("Failed to upload listener info to Sondehub/amateur\n");
ListenerCountdown = 600; // Try again in 1 minute
}
}
}
usleep(100000);
}
}