-
Notifications
You must be signed in to change notification settings - Fork 145
/
phone_home.c
325 lines (275 loc) · 7.12 KB
/
phone_home.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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <curl/curl.h>
#include <jansson.h>
#include "phone_home.h"
typedef struct _sized_buffer {
char *buffer;
size_t size;
} SizedBuffer;
static size_t git_data_write_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
{
SizedBuffer *sbuf = (SizedBuffer *)userdata;
size_t old_size, new_buf_size = size * nmemb;
char *r_buf;
if (ptr) {
old_size = sbuf->size;
r_buf = (char *)realloc(sbuf->buffer, old_size + new_buf_size);
if (r_buf) {
memcpy(r_buf + old_size, ptr, new_buf_size);
sbuf->buffer = r_buf;
sbuf->size += new_buf_size;
} else {
printf("buffer realloc failed\n");
}
}
return new_buf_size;
}
static CURLcode git_request(const char *url, char **out_data)
{
CURL *c_handle;
CURLcode c_ret = CURLE_OK;
SizedBuffer sized_buffer = { NULL, 0 };
long resp_code;
c_handle = curl_easy_init();
if (!c_handle) {
printf("curl_easy_init() failed: %s\n",
curl_easy_strerror(c_ret));
goto cleanup;
}
c_ret = curl_easy_setopt(c_handle, CURLOPT_URL, url);
if (c_ret != CURLE_OK) {
printf("curl_easy_setop(with CURLOPT_URL) failed: %s\n",
curl_easy_strerror(c_ret));
goto cleanup;
}
c_ret = curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "iio-oscilloscope");
if (c_ret != CURLE_OK) {
printf("curl_easy_setop(with CURLOPT_USERAGENT) failed: %s\n",
curl_easy_strerror(c_ret));
goto cleanup;
}
c_ret = curl_easy_setopt(c_handle, CURLOPT_WRITEFUNCTION, git_data_write_cb);
if (c_ret != CURLE_OK) {
printf("curl_easy_setop(with CURLOPT_WRITEFUNCTION failed: %s\n",
curl_easy_strerror(c_ret));
goto cleanup;
}
c_ret = curl_easy_setopt(c_handle, CURLOPT_WRITEDATA, &sized_buffer);
if (c_ret != CURLE_OK) {
printf("curl_easy_setop(with CURLOPT_WRITEDATA) failed: %s\n",
curl_easy_strerror(c_ret));
goto cleanup;
}
c_ret = curl_easy_perform(c_handle);
if (c_ret != CURLE_OK) {
printf("curl_easy_perform() failed: %s\n", curl_easy_strerror(c_ret));
goto cleanup;
}
c_ret = curl_easy_getinfo(c_handle, CURLINFO_RESPONSE_CODE, &resp_code);
if (c_ret != CURLE_OK) {
printf("curl_easy_getinfo() failed: %s\n", curl_easy_strerror(c_ret));
goto cleanup;
}
if (resp_code != 200) {
printf("Request returns code: %ld\n", resp_code);
if (sized_buffer.buffer) {
free(sized_buffer.buffer);
sized_buffer.buffer = NULL;
}
goto cleanup;
}
if (sized_buffer.buffer) {
char *r_buf;
r_buf = (char *)realloc(sized_buffer.buffer, sized_buffer.size + 1);
if (r_buf) {
sized_buffer.buffer = r_buf;
sized_buffer.buffer[sized_buffer.size] = '\0';
} else {
free(sized_buffer.buffer);
sized_buffer.buffer = NULL;
}
}
*out_data = sized_buffer.buffer;
cleanup:
if (c_handle) {
curl_easy_cleanup(c_handle);
c_handle = NULL;
}
return c_ret;
}
static json_t* decode_text(const char *text)
{
json_t *root = NULL;
json_error_t err;
if (!text)
goto exit;
root = json_loads(text, 0, &err);
if (!root) {
printf("json_loads() failed: %s\n", err.text);
goto exit;
}
if (!json_is_array(root)) {
printf("json_is_array() failed\n");
json_decref(root);
root = NULL;
}
exit:
return root;
}
static json_t * get_latest_release(json_t *root)
{
json_t *elem, *publish_at;
json_t *latest_release = NULL;
const char *newest_date = NULL;
size_t i;
for (i = 0; i < json_array_size(root); i++) {
elem = json_array_get(root, i);
if (!json_is_object(elem)) {
printf("json_is_object(elem) failed\n");
break;
}
publish_at = json_object_get(elem, "published_at");
if (!json_is_string(publish_at)) {
printf("json_is_string(publish_at) failed\n");
break;
}
if (!newest_date) {
newest_date = json_string_value(publish_at);
latest_release = elem;
}
else if (strcmp(newest_date, json_string_value(publish_at)) < 0) {
newest_date = json_string_value(publish_at);
latest_release = elem;
}
}
return latest_release;
}
static json_t * decode_url_feedback(const char *url)
{
json_t *j_root = NULL;
char *data = NULL;
CURLcode c_ret;
c_ret = git_request(url, &data);
if (c_ret != CURLE_OK) {
printf("git_request from %s failed\n", url);
goto fail;
}
if (!data) {
printf("Could not get data from %s", url);
goto fail;
}
j_root = decode_text(data);
free(data);
fail:
return j_root;
}
Release * release_new(void)
{
return calloc(1, sizeof(Release));
}
void release_dispose(Release *_this)
{
if (!_this)
return;
if (_this->name)
free(_this->name);
if (_this->build_date)
free(_this->build_date);
if (_this->commit)
free(_this->commit);
if (_this->url)
free(_this->url);
if (_this->windows_dld_url)
free(_this->windows_dld_url);
}
Release * release_get_latest(void)
{
Release *release = NULL;
const char *SERVER_URL = "https://api.github.com/repos/analogdevicesinc/iio-oscilloscope/releases";
json_t *j_root, *j_release;
bool release_abort = false;
j_root = decode_url_feedback(SERVER_URL);
if (!j_root) {
printf("Could not decode data about git releases\n");
goto cleanup_and_fail;
}
j_release = get_latest_release(j_root);
if (!j_release) {
printf("No release found\n");
goto cleanup_and_fail;
}
release = release_new();
if (!release) {
printf("%s\n", strerror(errno));
goto cleanup_and_fail;
}
release->name = strdup(json_string_value(json_object_get(j_release, "name")));
release->build_date = strdup(json_string_value(json_object_get(j_release, "created_at")));
release->url = strdup(json_string_value(json_object_get(j_release, "html_url")));
/* Get the release SHA commit */
json_t *j_tags, *tag, *name, *commit;
const char *tag_name;
size_t i;
tag_name = json_string_value(json_object_get(j_release, "tag_name"));
j_tags = decode_url_feedback("https://api.github.com/repos/analogdevicesinc/iio-oscilloscope/tags");
if (!j_tags) {
printf("Could not decode data about git tags\n");
release_abort = true;
goto cleanup_and_fail;
}
for (i = 0; i < json_array_size(j_tags); i++) {
tag = json_array_get(j_tags, i);
if (!json_is_object(tag))
break;
name = json_object_get(tag, "name");
if (!json_is_string(name))
break;
if (tag_name && !strcmp(json_string_value(name), tag_name)) {
commit = json_object_get(tag, "commit");
if (!json_is_object(commit))
break;
release->commit = strdup(json_string_value(
json_object_get(commit, "sha")));
}
}
if (!release->commit) {
printf("Could not find release SHA commit\n");
release_abort = true;
goto cleanup_and_fail;
}
/* Get download link for the windows build */
json_t *assets, *w_build;
assets = json_object_get(j_release, "assets");
if (!assets) {
release_abort = true;
goto cleanup_and_fail;
}
w_build = json_array_get(assets, 0);
if (!w_build) {
release_abort = true;
goto cleanup_and_fail;
}
release->windows_dld_url = strdup(json_string_value(json_object_get(w_build, "browser_download_url")));
cleanup_and_fail:
if (release_abort) {
release_dispose(release);
release = NULL;
}
json_decref(j_root);
return release;
}
int phone_home_init(void)
{
if (curl_global_init(CURL_GLOBAL_DEFAULT)) {
printf("curl_global_init() failed!\n");
return 0;
}
return 1;
}
void phone_home_terminate(void)
{
curl_global_cleanup();
}