Revision cc7646f9
Major changes to mainbox code.
-Added local cache for RFID -> tool permissions
-Queries new page /crm/roboauth/%08x, which gives bitmask of all tool
permissions instead of just one
-Added pid file at /var/run/tooltron.pid
-Now must be run as "tooltron run" and also has "tooltron refresh" and
"tooltron clear" which send signals to the pid in the pid file
-Other things I'm forgetting
mainbox/main.c | ||
---|---|---|
1 | 1 |
#include "tool.h" |
2 | 2 |
#include "query.h" |
3 |
#include "cache.h" |
|
3 | 4 |
#include "log.h" |
5 |
#include "util.h" |
|
4 | 6 |
#include <unistd.h> |
5 | 7 |
#include <signal.h> |
6 |
#include <strings.h>
|
|
8 |
#include <string.h> |
|
7 | 9 |
#include <stdio.h> |
8 | 10 |
|
9 |
#define SLEEP_MS 10//250
|
|
11 |
#define SLEEP_MS 25
|
|
10 | 12 |
|
11 | 13 |
static struct tool_t tools[] = { |
12 |
TOOL_DECL("test1", 1), |
|
13 |
TOOL_DECL("test2", 2), |
|
14 |
TOOL_DECL("test3", 3), |
|
15 |
TOOL_DECL("test4", 4), |
|
16 |
TOOL_DECL("test5", 5), |
|
17 |
TOOL_DECL("test6", 6), |
|
18 |
TOOL_DECL("test7", 7), |
|
19 |
TOOL_DECL("test8", 8), |
|
20 |
TOOL_DECL("test9", 9) |
|
14 |
TOOL_DECL("Mill", 1), |
|
15 |
TOOL_DECL("Lathe", 2), |
|
16 |
TOOL_DECL("Drill Press", 3), |
|
17 |
TOOL_DECL("Drill Press", 4), |
|
18 |
TOOL_DECL("CNC", 5), |
|
19 |
TOOL_DECL("Bandsaw", 6), |
|
20 |
TOOL_DECL("Circular Saw", 7), |
|
21 |
TOOL_DECL("Wood Band Saw", 8), |
|
22 |
TOOL_DECL("Chop/Miter Saw", 9), |
|
23 |
TOOL_DECL("Belt Sander", 10) |
|
21 | 24 |
}; |
22 | 25 |
|
23 | 26 |
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t)) |
24 | 27 |
|
25 | 28 |
volatile int run = 1; |
29 |
volatile int refresh_cache = 0; |
|
30 |
volatile int clear_cache = 0; |
|
26 | 31 |
|
27 | 32 |
void sigint(int sig) { |
28 | 33 |
run = 0; |
29 | 34 |
} |
30 | 35 |
|
31 |
void print_usage(const char *name) { |
|
32 |
printf("Usage: %s [-h] [-d serial_device] [-s db_server[:port]]\n", name); |
|
33 |
printf(" -h prints this message\n"); |
|
34 |
printf(" -d specifies the serial port for Modbus\n"); |
|
35 |
printf(" defaults to /dev/ttyUSB0\n"); |
|
36 |
printf(" -s specifies the server where the CRM is running\n"); |
|
37 |
printf(" defaults to roboticsclub.org\n"); |
|
36 |
void sigusr1(int sig) { |
|
37 |
refresh_cache = 1; |
|
38 | 38 |
} |
39 | 39 |
|
40 |
int main(int argc, char **argv) { |
|
41 |
int i, opt; |
|
42 |
struct sigaction sigact; |
|
43 |
const char *device = "/dev/ttyUSB0"; |
|
44 |
const char *server = "roboticsclub.org"; |
|
40 |
void sigusr2(int sig) { |
|
41 |
clear_cache = 1; |
|
42 |
} |
|
45 | 43 |
|
46 |
while ((opt = getopt(argc, argv, "hd:s:")) != -1) { |
|
47 |
switch (opt) { |
|
48 |
case 'h': |
|
49 |
print_usage(argv[0]); |
|
50 |
return 0; |
|
51 |
case 'd': |
|
52 |
device = optarg; |
|
53 |
break; |
|
54 |
case 's': |
|
55 |
server = optarg; |
|
56 |
break; |
|
57 |
default: |
|
58 |
print_usage(argv[0]); |
|
59 |
return 1; |
|
60 |
} |
|
61 |
} |
|
44 |
void send_signal(int sig) { |
|
45 |
pid_t pid; |
|
46 |
|
|
47 |
pid = read_pid_file(); |
|
48 |
|
|
49 |
if (pid > 0) |
|
50 |
kill(pid, sig); |
|
51 |
} |
|
52 |
|
|
53 |
int tooltron_main(const char *device, const char *server) { |
|
54 |
struct sigaction sigact; |
|
55 |
int i; |
|
62 | 56 |
|
63 | 57 |
log_print("Serial device: %s", device); |
64 | 58 |
log_print("CRM server: http://%s/", server); |
65 | 59 |
|
66 | 60 |
bzero(&sigact, sizeof(sigact)); |
67 |
sigact.sa_handler = sigint; |
|
68 | 61 |
sigact.sa_flags = SA_RESTART; |
69 | 62 |
sigemptyset(&sigact.sa_mask); |
63 |
|
|
64 |
sigact.sa_handler = sigint; |
|
70 | 65 |
sigaction(SIGINT, &sigact, NULL); |
66 |
sigaction(SIGTERM, &sigact, NULL); |
|
67 |
|
|
68 |
sigact.sa_handler = sigusr1; |
|
69 |
sigaction(SIGUSR1, &sigact, NULL); |
|
70 |
|
|
71 |
sigact.sa_handler = sigusr2; |
|
72 |
sigaction(SIGUSR2, &sigact, NULL); |
|
71 | 73 |
|
72 | 74 |
if (query_init(server)) { |
73 | 75 |
return 1; |
... | ... | |
83 | 85 |
while (run) { |
84 | 86 |
tool_poll(&tools[i]); |
85 | 87 |
event_q_process(); |
88 |
if (refresh_cache) { |
|
89 |
log_print("Recieved SIGUSR1, refreshing cache content"); |
|
90 |
query_refresh_cache(); |
|
91 |
refresh_cache = 0; |
|
92 |
} |
|
93 |
if (clear_cache) { |
|
94 |
log_print("Recieved SIGUSR2, clearing cache"); |
|
95 |
cache_clear(); |
|
96 |
clear_cache = 0; |
|
97 |
} |
|
86 | 98 |
usleep(SLEEP_MS * (useconds_t)1000); |
87 | 99 |
i = (i+1) % N_TOOLS; |
88 | 100 |
} |
... | ... | |
96 | 108 |
tool_close_mb(); |
97 | 109 |
|
98 | 110 |
log_print("Exiting"); |
111 |
query_cleanup(); |
|
112 |
cache_clear(); |
|
99 | 113 |
return 0; |
100 | 114 |
} |
115 |
|
|
116 |
char *usage = |
|
117 |
"Usage: %s [-h] [-d device] [-s server[:port]] <cmd>\n" |
|
118 |
" -h prints this message\n" |
|
119 |
" -d specifies the serial port for Modbus\n" |
|
120 |
" defaults to /dev/ttyUSB0\n" |
|
121 |
" -s specifies the server where the CRM is running\n" |
|
122 |
" defaults to roboticsclub.org\n" |
|
123 |
" <cmd> can be any of the following:\n" |
|
124 |
" run runs tooltron if it is not already running\n" |
|
125 |
" refresh signals an already running tooltron to refresh its cache\n" |
|
126 |
" clear signals an already running tooltron to clear its cache\n"; |
|
127 |
|
|
128 |
int main(int argc, char **argv) { |
|
129 |
int opt; |
|
130 |
const char *device = "/dev/ttyUSB0"; |
|
131 |
const char *server = "roboticsclub.org"; |
|
132 |
|
|
133 |
while ((opt = getopt(argc, argv, "hd:s:")) != -1) { |
|
134 |
switch (opt) { |
|
135 |
case 'h': |
|
136 |
/* Print usage, not an error */ |
|
137 |
printf(usage, argv[0]); |
|
138 |
return 0; |
|
139 |
case 'd': |
|
140 |
device = optarg; |
|
141 |
break; |
|
142 |
case 's': |
|
143 |
server = optarg; |
|
144 |
break; |
|
145 |
default: |
|
146 |
/* Unknown option, error */ |
|
147 |
printf(usage, argv[0]); |
|
148 |
return 1; |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
if (optind >= argc) { |
|
153 |
/* Not enough arguments, error */ |
|
154 |
printf(usage, argv[0]); |
|
155 |
return 1; |
|
156 |
} |
|
157 |
|
|
158 |
if (strcmp(argv[optind], "refresh") == 0) { |
|
159 |
send_signal(SIGUSR1); |
|
160 |
return 0; |
|
161 |
} else if (strcmp(argv[optind], "clear") == 0) { |
|
162 |
send_signal(SIGUSR2); |
|
163 |
return 0; |
|
164 |
} else if (strcmp(argv[optind], "run") != 0) { |
|
165 |
/* <cmd> is not "refresh", "clear", or "run", error */ |
|
166 |
printf(usage, argv[0]); |
|
167 |
return 1; |
|
168 |
} |
|
169 |
|
|
170 |
if (create_pid_file()) |
|
171 |
/* pid file already exists, error */ |
|
172 |
return 1; |
|
173 |
|
|
174 |
return tooltron_main(device, server); |
|
175 |
} |
Also available in: Unified diff