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
|
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include "anzeige.h"
#include "fonts.h"
#include "input_gadgets.h"
#include "multiplexer.h"
void drop_privileges();
void handle_signals(int signo);
t_scroll_data scroll_data;
t_input_thread input_threads[THREAD_IDX_MAX];
int main(int argc, char **argv)
{
int ret_val;
memset(&scroll_data, 0, sizeof(t_scroll_data));
memset(&input_threads, 0, sizeof(t_input_thread) * THREAD_IDX_MAX);
// Set up gpi pointer for direct register access
ret_val = multiplexer_setup_root();
if (ret_val)
return ret_val;
// Drop root privileges
drop_privileges();
// set up / register signal handler
struct sigaction new_action, old_action;
new_action.sa_handler = handle_signals;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
const int fetch_signals[4] = {
SIGINT, SIGHUP, SIGTERM, SIGUSR1
};
for (int i = 0; i < sizeof(fetch_signals)/sizeof(fetch_signals[0]); i++) {
sigaction (fetch_signals[i], NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction (fetch_signals[i], &new_action, NULL);
}
init_symbols_boundaries();
/************************************************************************\
* You are about to change the GPIO settings of your computer. *
* Mess this up and it will stop working! *
* It might be a good idea to 'sync' before running this program *
* so at least you still have your code changes written to the SD-card! *
\************************************************************************/
// configure the gpio pins and start the multiplexer thread
multiplexer_setup_scroll(&scroll_data);
#define TEXT_BUFFER_LENGTH SCROLL_BUF_LEN / 5
char *text_buffer;
char *text_buffer_end;
char text_buffers[OUTPUT_IDX_MAX][MAX_TEXT_OUTPUT_LEN];
char output_text_buffer[MAX_TEXT_OUTPUT_LEN];
char humidity_text_buffer[MAX_TEXT_OUTPUT_LEN];
double outside_temperature = 0, outside_humidity = 0, inside_temperature = 0, inside_humidity = 0;
int updated = 0;
memset(humidity_text_buffer, 0 ,MAX_TEXT_OUTPUT_LEN);
text_buffer = malloc(TEXT_BUFFER_LENGTH);
if (text_buffer == NULL) {
fprintf(stderr, "malloc failed to allocate %d bytes\n", TEXT_BUFFER_LENGTH);
turn_off_display();
return EXIT_FAILURE;
}
memset(text_buffer, 0, TEXT_BUFFER_LENGTH);
memset(text_buffers, 0, OUTPUT_IDX_MAX * MAX_TEXT_OUTPUT_LEN);
memset(output_text_buffer, 0, MAX_TEXT_OUTPUT_LEN);
for (int i=0; i<THREAD_IDX_MAX; i++) {
input_threads[i].keep_running = 1;
input_threads[i].last_update = 0;
}
pthread_create(&input_threads[THREAD_IDX_WARNINGS].thread_id, NULL, gadgets_watch_weather_warnings, &input_threads[THREAD_IDX_WARNINGS]);
pthread_create(&input_threads[THREAD_IDX_OUTSIDE].thread_id, NULL, gadgets_watch_current_temperature, &input_threads[THREAD_IDX_OUTSIDE]);
pthread_create(&input_threads[THREAD_IDX_FORECAST].thread_id, NULL, gadgets_watch_weather_forecast, &input_threads[THREAD_IDX_FORECAST]);
pthread_create(&input_threads[THREAD_IDX_INSIDE].thread_id, NULL, gadgets_watch_humidity, &input_threads[THREAD_IDX_INSIDE]);
scroll_data . shine_until = time(NULL) + 10;
while (scroll_data . keep_running) {
if (got_input()) {
scroll_data . shine_until = time(NULL) + 60;
for (int i=0; i<THREAD_IDX_MAX; i++)
input_threads[i].force_update = 1;
}
for (int i=0; i<THREAD_IDX_MAX; i++) {
int outdated = (input_threads[i].last_update < time(NULL) - 3600);
if (input_threads[i].output_ready || outdated) {
if (outdated)
snprintf(output_text_buffer, MAX_TEXT_OUTPUT_LEN, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 0xEE, 0x01, 0xEE, 0x02, 0xEE, 0x01, 0xEE, 0x02, 0xEE, 0x05, 0xEE, 0x06, 0xEE, 0x05, 0xEE, 0x06);
else
strncpy(output_text_buffer, input_threads[i].text_output, MAX_TEXT_OUTPUT_LEN);
if (strcmp(text_buffers[i], output_text_buffer)) {
strncpy(text_buffers[i], output_text_buffer, MAX_TEXT_OUTPUT_LEN);
updated = 1;
}
if ((i == THREAD_IDX_OUTSIDE) || (i == THREAD_IDX_INSIDE)) {
if (outdated) {
snprintf(humidity_text_buffer, MAX_TEXT_OUTPUT_LEN, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 0xEE, 0x01, 0xEE, 0x02, 0xEE, 0x01, 0xEE, 0x02, 0xEE, 0x05, 0xEE, 0x06, 0xEE, 0x05, 0xEE, 0x06);
} else {
if (i == THREAD_IDX_OUTSIDE) {
if (outside_temperature != input_threads[i].double_output[0])
outside_temperature = input_threads[i].double_output[0];
if (outside_humidity != input_threads[i].double_output[1])
outside_humidity = input_threads[i].double_output[1];
}
if (i == THREAD_IDX_INSIDE) {
if (inside_temperature != input_threads[i].double_output[0])
inside_temperature = input_threads[i].double_output[0];
if (inside_humidity != input_threads[i].double_output[1])
inside_humidity = input_threads[i].double_output[1];
}
if (((outside_temperature!=0) || (outside_humidity!=0)) &&
((inside_temperature!=0) || (inside_humidity!=0))) {
gadgets_calculate_humidity_level(
humidity_text_buffer,
MAX_TEXT_OUTPUT_LEN,
outside_temperature,
outside_humidity,
inside_temperature,
inside_humidity
);
}
}
if (strcmp(text_buffers[THREAD_IDX_INSIDE], humidity_text_buffer)) {
strncpy(text_buffers[THREAD_IDX_INSIDE], humidity_text_buffer, MAX_TEXT_OUTPUT_LEN);
updated = 1;
}
}
input_threads[i].output_ready = 0;
}
}
if (updated)
updated ++;
if (updated > 15) {
updated = 0;
memset(text_buffer, 0, TEXT_BUFFER_LENGTH);
text_buffer_end = text_buffer;
for (int i=0; i<OUTPUT_IDX_MAX; i++) {
if ((text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end > 0) && (*text_buffers[i]))
text_buffer_end += snprintf(
text_buffer_end,
text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end,
"%s; ",
text_buffers[i]
);
}
struct tm time_struct;
time_t now;
memset(&time_struct, 0, sizeof(time_struct));
now = time(NULL);
localtime_r(&now, &time_struct);
for (int i = 0; i < (time_struct . tm_wday + 6) % 7 + 1; i++) {
if (text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end <= 0)
break;
text_buffer_end += snprintf(
text_buffer_end,
text_buffer + TEXT_BUFFER_LENGTH - text_buffer_end,
"%c%c%c%c",
0xEE,
i==0 ? 0x01 : 0x03 + 2 * (i & 0x01),
0xEE,
i==0 ? 0x02 : 0x04 + 2 * (i & 0x01)
);
}
scroll_data . input_len =
render(
text_buffer,
text_buffer_end - text_buffer,
scroll_data . input,
sizeof(scroll_data . input),
1
);
scroll_data . update = 1;
}
usleep(100000);
}
for (int i=0; i<THREAD_IDX_MAX; i++)
input_threads[i].keep_running = 0;
free(text_buffer);
pthread_join(scroll_data . thread_id, NULL);
turn_off_display();
for (int i=0; i<THREAD_IDX_MAX; i++)
pthread_join(input_threads[i].thread_id, NULL);
return EXIT_SUCCESS;
} // main
//
// Drop (root) privileges
//
void drop_privileges()
{
/* Drop superuser privileges in correct order */
int is_already_unprivileged = 0;
if (setgid(65534) == -1) { // nobody
perror("can't drop group privileges");
if (! (is_already_unprivileged = (errno == 1)))
exit(-1);
}
if (setuid(65534) == -1) { // nobody
perror("can't drop user privileges");
if (is_already_unprivileged != (errno == 1))
exit(-1);
}
} // drop_root
void handle_signals(int signo)
{
switch (signo) {
case SIGINT:
case SIGHUP:
case SIGTERM:
fprintf(stderr, "stopping\n");
scroll_data . keep_running = 0;
break;
case SIGUSR1:
for (int i=0; i<THREAD_IDX_MAX; i++)
input_threads[i] . force_update = 1;
scroll_data . shine_until = time(NULL) + 60;
break;
}
}
|