-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontrol-app.c
94 lines (85 loc) · 2.12 KB
/
control-app.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
// SPDX-License-Identifier: GPL-2.0-only
#include <stdio.h>
#include <unistd.h> //needed for named pipes
#include <fcntl.h>
#include <stdbool.h> //boolean type
#include <sys/stat.h>
#include <ncurses.h>
#include <string.h>
// Options to choose from
char *fi_choices[] = {
"corrupt next message sent",
"drop next message",
"trigger Safe State",
};
int n_fi_choices = sizeof(fi_choices) / sizeof(char *);
// Corresponding messages to be sent
char *fi_message_strings[] = {
"corrupt message",
"skip message",
"safe state",
};
// print selection window with highlighted selection
void print_menu(WINDOW *W, int selection)
{
box(W, 0, 0);
int y = 1;
for (int i = 0; i < n_fi_choices; i++) {
if (i == selection) {
wattron(W, A_REVERSE);
mvwprintw(W, y, 1, "%s", fi_choices[i]);
wattroff(W, A_REVERSE);
} else {
mvwprintw(W, y, 1, "%s", fi_choices[i]);
}
y++;
}
wrefresh(W);
}
// write selected command to the signal-source-contro-pipe
void send_control_message(int selection)
{
const char *Pipe = "/tmp/signal-source-control-pipe";
mkfifo(Pipe, 0666);
int fd = open(Pipe, O_WRONLY | O_NONBLOCK);
write(fd, fi_message_strings[selection], strlen(fi_message_strings[selection]));
close(fd);
}
int main(void)
{
int selection = 0; // currently highlighted row
int key_pressed;
initscr(); // Start curses mode
cbreak();
WINDOW *W;
W = newwin(n_fi_choices+2, 50, 3, 1);
keypad(W, TRUE);
printw("Welcome to the signal-source-control panel\n");
printw("Enter to Send selected control message\n");
printw("ESC to quit Application\n\n");
refresh();
while (1) {
print_menu(W, selection); //refresh window
key_pressed = wgetch(W);
switch (key_pressed) {
case KEY_UP:
selection--;
if (selection < 0) {
selection = 0;
}
break;
case KEY_DOWN:
selection++;
if (selection >= n_fi_choices-1) {
selection = n_fi_choices-1;
}
break;
case 10: // Enter key, send highlighted message
send_control_message(selection);
break;
case 27: // ESC key, quit application
endwin(); //End curses mode and return
return 0;
}
}
}