root / mainbox / main.c @ 0ed3db9d
History | View | Annotate | Download (4.29 KB)
1 |
#include "tool.h" |
---|---|
2 |
#include "query.h" |
3 |
#include "cache.h" |
4 |
#include "log.h" |
5 |
#include "util.h" |
6 |
#include <unistd.h> |
7 |
#include <signal.h> |
8 |
#include <string.h> |
9 |
#include <stdio.h> |
10 |
|
11 |
#define SLEEP_MS 25 |
12 |
|
13 |
static struct tool_t tools[] = { |
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) |
24 |
}; |
25 |
|
26 |
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t)) |
27 |
|
28 |
volatile int run = 1; |
29 |
volatile int refresh_cache = 0; |
30 |
volatile int clear_cache = 0; |
31 |
|
32 |
void sigint(int sig) { |
33 |
run = 0;
|
34 |
} |
35 |
|
36 |
void sigusr1(int sig) { |
37 |
refresh_cache = 1;
|
38 |
} |
39 |
|
40 |
void sigusr2(int sig) { |
41 |
clear_cache = 1;
|
42 |
} |
43 |
|
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;
|
56 |
|
57 |
log_print("Serial device: %s", device);
|
58 |
log_print("CRM server: http://%s/", server);
|
59 |
|
60 |
bzero(&sigact, sizeof(sigact));
|
61 |
sigact.sa_flags = SA_RESTART; |
62 |
sigemptyset(&sigact.sa_mask); |
63 |
|
64 |
sigact.sa_handler = sigint; |
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);
|
73 |
|
74 |
if (query_init(server)) {
|
75 |
return 1; |
76 |
} |
77 |
|
78 |
if (tool_init_mb(device)) {
|
79 |
return 1; |
80 |
} |
81 |
|
82 |
log_print("Modbus initialized; polling tools...");
|
83 |
|
84 |
i = 0;
|
85 |
while (run) {
|
86 |
tool_poll(&tools[i]); |
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 |
} |
98 |
usleep(SLEEP_MS * (useconds_t)1000);
|
99 |
i = (i+1) % N_TOOLS;
|
100 |
} |
101 |
log_print("Recieved SIGINT or SIGTERM, shutting down");
|
102 |
|
103 |
log_print("Disabling tools");
|
104 |
for (i = 0; i < N_TOOLS; i++) { |
105 |
tool_request_disable(&tools[i]); |
106 |
} |
107 |
|
108 |
log_print("Closing modbus connection");
|
109 |
tool_close_mb(); |
110 |
|
111 |
log_print("Exiting");
|
112 |
query_cleanup(); |
113 |
cache_clear(); |
114 |
return 0; |
115 |
} |
116 |
|
117 |
char *usage =
|
118 |
"Usage: %s [-h] [-d device] [-s server[:port]] <cmd>\n"
|
119 |
" -h prints this message\n"
|
120 |
" -d specifies the serial port for Modbus\n"
|
121 |
" defaults to /dev/ttyUSB0\n"
|
122 |
" -s specifies the server where the CRM is running\n"
|
123 |
" defaults to roboticsclub.org\n"
|
124 |
" <cmd> can be any of the following:\n"
|
125 |
" run runs tooltron if it is not already running\n"
|
126 |
" stop signals a running tooltron to shut down\n"
|
127 |
" refresh signals a running tooltron to refresh its cache\n"
|
128 |
" clear signals a running tooltron to clear its cache\n";
|
129 |
|
130 |
int main(int argc, char **argv) { |
131 |
int opt, status, as_root;
|
132 |
const char *device = "/dev/ttyUSB0"; |
133 |
const char *server = "roboticsclub.org"; |
134 |
|
135 |
while ((opt = getopt(argc, argv, "hd:s:")) != -1) { |
136 |
switch (opt) {
|
137 |
case 'h': |
138 |
/* Print usage, not an error */
|
139 |
printf(usage, argv[0]);
|
140 |
return 0; |
141 |
case 'd': |
142 |
device = optarg; |
143 |
break;
|
144 |
case 's': |
145 |
server = optarg; |
146 |
break;
|
147 |
default:
|
148 |
/* Unknown option, error */
|
149 |
printf(usage, argv[0]);
|
150 |
return 1; |
151 |
} |
152 |
} |
153 |
|
154 |
if (optind >= argc) {
|
155 |
/* Not enough arguments, error */
|
156 |
printf(usage, argv[0]);
|
157 |
return 1; |
158 |
} |
159 |
|
160 |
if (strcmp(argv[optind], "refresh") == 0) { |
161 |
|
162 |
send_signal(SIGUSR1); |
163 |
return 0; |
164 |
|
165 |
} else if (strcmp(argv[optind], "clear") == 0) { |
166 |
|
167 |
send_signal(SIGUSR2); |
168 |
return 0; |
169 |
|
170 |
} else if (strcmp(argv[optind], "stop") == 0) { |
171 |
|
172 |
send_signal(SIGTERM); |
173 |
return 0; |
174 |
|
175 |
} else if (strcmp(argv[optind], "run") == 0) { |
176 |
|
177 |
/* if we're not root, don't bother with the pid file */
|
178 |
if (geteuid() == 0) { |
179 |
as_root = 1;
|
180 |
if (create_pid_file())
|
181 |
/* pid file already exists, error */
|
182 |
return 1; |
183 |
} else {
|
184 |
as_root = 0;
|
185 |
log_print("Warning: not root. Running without a PID file");
|
186 |
} |
187 |
|
188 |
status = tooltron_main(device, server); |
189 |
|
190 |
if (as_root)
|
191 |
remove_pid_file(); |
192 |
return status;
|
193 |
|
194 |
} |
195 |
|
196 |
printf("Unknown command \"%s\"\n", argv[optind]);
|
197 |
printf(usage, argv[0]);
|
198 |
return 1; |
199 |
} |