This repository has been archived by the owner on Nov 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hektor.c
80 lines (58 loc) · 1.91 KB
/
hektor.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
// This program is free software. It comes without any warranty, to the extent
// permitted by applicable law. You can redistribute it and/or modify it under
// the terms of the Do What The Fuck You Want To Public License, Version 2, as
// published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
// details.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <yajl/yajl_gen.h>
#include "info.h"
#include "modem.h"
int main(int argc, char **argv) {
bstring info_page;
info_t info;
yajl_gen yajl;
const unsigned char *json;
size_t json_len;
curl_global_init(CURL_GLOBAL_NOTHING);
for (size_t i = 0; argv[i]; i += 1) {
if (strcmp(argv[i], "-r") == 0) {
modem_touch(MODEM_RESTART_URL);
return EXIT_SUCCESS;
}
}
info_page = modem_fetch(MODEM_INFO_URL);
if (!info_page) {
fprintf(stderr, "hektor: Couldn't fetch '%s'\n", MODEM_INFO_URL);
return EXIT_FAILURE;
}
info_init(&info, info_page);
yajl = yajl_gen_alloc(NULL);
assert(yajl);
yajl_gen_config(yajl, yajl_gen_beautify, true);
#define yajl_gen_strst(y, s) yajl_gen_string((y), (s), sizeof(s) - 1)
yajl_gen_map_open(yajl);
yajl_gen_strst(yajl, "connection");
yajl_gen_integer(yajl, info.conn);
yajl_gen_strst(yajl, "fapped");
yajl_gen_bool(yajl, info.fap == FAP_ACTIVE);
yajl_gen_strst(yajl, "refill_secs");
yajl_gen_integer(yajl, info.refill_secs);
yajl_gen_strst(yajl, "refill_ts");
yajl_gen_integer(yajl, info.refill_ts);
yajl_gen_strst(yajl, "usage_allowed");
yajl_gen_integer(yajl, info.usage_allowed);
yajl_gen_strst(yajl, "usage_remain");
yajl_gen_integer(yajl, info.usage_remain);
yajl_gen_map_close(yajl);
#undef yajl_gen_strst
yajl_gen_get_buf(yajl, &json, &json_len);
printf("%s", json);
bdestroy(info_page);
yajl_gen_free(yajl);
curl_global_cleanup();
return EXIT_SUCCESS;
}