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
|
/*
* Hot Beverage Companion – server module
*
* Copyright (C) 2017-2018 Gediminas Jakutis
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <poll.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include "util.h"
#include "net.h"
#include "ui.h"
struct settings {
struct timespec period;
enum uitype ui;
unsigned short int port;
};
static int init(int argc, char ***argv, const struct settings * const settings);
static struct settings parseargs(int argc, char **argv);
static enum uitype identify_ui(const char * const s);
static struct timespec calc_period(const char * const s);
static size_t printhelp(const char * const name);
int main(int argc, char **argv)
{
struct settings settings;
int ret;
int nd;
settings = parseargs(argc, argv);
nd = init(argc, &argv, &settings);
if (nd == ERROR) {
ret = nd;
goto fail;
}
ret = ui_startloop();
fail:
net_close(nd);
return ret;
}
static int init(int argc, char ***argv, const struct settings * const settings)
{
int ret = A_OK;
int nd;
nd = net_init(settings->port);
if (nd == ERROR) {
ret = nd;
goto fail;
}
ret = ui_init(argc, argv, settings->ui, nd, settings->period);
if (ret == ERROR) {
net_close(nd);
goto fail;
}
fail:
return ret;
}
static struct settings parseargs(int argc, char **argv)
{
/* collection of defaults */
struct settings ret = {{0, 200 * 1000 * 1000}, /* 200 ms */
ui_curses,
2191};
ssize_t i;
if (argc == 1) {
return ret;
}
for (i = 1; i < argc; ++i) {
if (!(strncmp(argv[i], "--period=", 8))) {
ret.period = strlen(argv[i]) > 9 ? calc_period(argv[i] + 9) : (struct timespec) {0, printhelp(*argv)};
} else if (!(strncmp(argv[i], "--ui=", 5))) {
ret.ui = strlen(argv[i]) > 5 ? identify_ui(argv[i] + 5) : printhelp(*argv);
} else if (!(strncmp(argv[i], "--port=", 7))) {
ret.port = strlen(argv[i]) > 7 ? strtoul(argv[i] + 7, NULL, 10) : printhelp(*argv);
} else {
printhelp(*argv);
}
}
return ret;
}
static enum uitype identify_ui(const char * const s)
{
/* TODO: stub! */
return ui_curses;
}
static struct timespec calc_period(const char * const s)
{
/* TODO: stub! */
return (struct timespec) {0, 200 * 1000 * 1000};
}
static size_t printhelp(const char * const name)
{
printf( "This is a coffeetemp companion application\n"
"\n"
"usage:\t%s [options]\n"
"\n"
"Options:\n"
" --period=<num> set network polling rate to <num> milliseconds (default: 200)\n"
" --ui=<name> set the UI frontend (default: curses)\n"
" * possible choices: [none, file, curses, gtk]\n"
" --port=<num> set the listening port number to <num>\n"
"\n"
"In case more than one same option is provided, the last one take precedence\n"
"\n",
name);
exit(EXIT_FAILURE);
return 0;
}
|