forked from PiInTheSky/lora-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssdv.c
141 lines (110 loc) · 3.6 KB
/
ssdv.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
#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 <dirent.h>
#include <math.h>
#include <pthread.h>
#include <curl/curl.h>
#include "urlencode.h"
#include "base64.h"
#include "ssdv.h"
#include "global.h"
int FindSSDVPacketToSend(int ThreadIndex)
{
int i;
for (i=0; i<16; i++)
{
if (SSDVPacketArrays.Packets[ThreadIndex].Packets[i].InUse)
{
return i;
}
}
return -1;
}
size_t write_ssdv_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
return size * nmemb;
}
int UploadImagePacket(char *EncodedCallsign, char *EncodedEncoding, char *EncodedData)
{
CURL *curl;
CURLcode res;
char PostFields[1000];
int UploadedOK;
UploadedOK = 0;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if (curl)
{
// So that the response to the curl POST doesn;'t mess up my finely crafted display!
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_ssdv_data);
// Set the timeout
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
// Avoid curl library bug that happens if above timeout occurs (sigh)
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
/* Set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "http://ssdv.habhub.org/data.php");
/* Now specify the POST data */
sprintf(PostFields, "callsign=%s&encoding=%s&packet=%s", Config.Tracker, EncodedEncoding, EncodedData);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, PostFields);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res == CURLE_OK)
{
UploadedOK = 1;
}
else
{
LogMessage("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return UploadedOK;
}
void *SSDVLoop(void *arguments)
{
struct TThreadArguments *ThreadArguments;
int ThreadIndex;
char HexString[513];
ThreadArguments = (struct TThreadArguments *)arguments;
ThreadIndex = ThreadArguments->Index;
while (1)
{
int PacketIndex;
PacketIndex = FindSSDVPacketToSend(ThreadIndex);
if (PacketIndex >= 0)
{
char *EncodedCallsign, *EncodedEncoding, *EncodedData;
LogMessage("Found packet %d for thread %d from payload %s\n", PacketIndex, ThreadIndex, SSDVPacketArrays.Packets[ThreadIndex].Packets[PacketIndex].Callsign);
EncodedCallsign = url_encode(SSDVPacketArrays.Packets[ThreadIndex].Packets[PacketIndex].Callsign);
EncodedEncoding = url_encode("hex");
ConvertStringToHex(HexString, SSDVPacketArrays.Packets[ThreadIndex].Packets[PacketIndex].Packet, 256);
EncodedData = url_encode(HexString);
if (UploadImagePacket(EncodedCallsign, EncodedEncoding, EncodedData))
{
// Mark packet as sent
SSDVPacketArrays.Packets[ThreadIndex].Packets[PacketIndex].InUse = 0;
}
free(EncodedCallsign);
free(EncodedEncoding);
free(EncodedData);
}
sleep(0.1);
}
}