root / mainbox / main.c @ master
History | View | Annotate | Download (4.63 KB)
1 | 7bdb98c5 | Tom Mullins | #include "tool.h" |
---|---|---|---|
2 | 8f961e44 | Tom Mullins | #include "query.h" |
3 | cc7646f9 | Tom Mullins | #include "cache.h" |
4 | 5e03b78d | Tom Mullins | #include "log.h" |
5 | cc7646f9 | Tom Mullins | #include "util.h" |
6 | 7bdb98c5 | Tom Mullins | #include <unistd.h> |
7 | #include <signal.h> |
||
8 | cc7646f9 | Tom Mullins | #include <string.h> |
9 | 7bdb98c5 | Tom Mullins | #include <stdio.h> |
10 | 2dde134c | Tom Mullins | #include <sys/time.h> |
11 | 7bdb98c5 | Tom Mullins | |
12 | 15fa09f7 | Tom Mullins | static struct tool_t tools[] = { |
13 | cc7646f9 | Tom Mullins | TOOL_DECL("Mill", 1), |
14 | TOOL_DECL("Lathe", 2), |
||
15 | TOOL_DECL("Drill Press", 3), |
||
16 | TOOL_DECL("Drill Press", 4), |
||
17 | TOOL_DECL("CNC", 5), |
||
18 | TOOL_DECL("Bandsaw", 6), |
||
19 | TOOL_DECL("Circular Saw", 7), |
||
20 | TOOL_DECL("Wood Band Saw", 8), |
||
21 | TOOL_DECL("Chop/Miter Saw", 9), |
||
22 | TOOL_DECL("Belt Sander", 10) |
||
23 | 7bdb98c5 | Tom Mullins | }; |
24 | |||
25 | #define N_TOOLS (sizeof(tools)/sizeof(struct tool_t)) |
||
26 | |||
27 | volatile int run = 1; |
||
28 | cc7646f9 | Tom Mullins | volatile int refresh_cache = 0; |
29 | volatile int clear_cache = 0; |
||
30 | 7bdb98c5 | Tom Mullins | |
31 | void sigint(int sig) { |
||
32 | run = 0;
|
||
33 | } |
||
34 | |||
35 | cc7646f9 | Tom Mullins | void sigusr1(int sig) { |
36 | refresh_cache = 1;
|
||
37 | 8f961e44 | Tom Mullins | } |
38 | |||
39 | cc7646f9 | Tom Mullins | void sigusr2(int sig) { |
40 | clear_cache = 1;
|
||
41 | } |
||
42 | 8f961e44 | Tom Mullins | |
43 | cc7646f9 | Tom Mullins | void send_signal(int sig) { |
44 | pid_t pid; |
||
45 | |||
46 | pid = read_pid_file(); |
||
47 | |||
48 | if (pid > 0) |
||
49 | kill(pid, sig); |
||
50 | } |
||
51 | |||
52 | int tooltron_main(const char *device, const char *server) { |
||
53 | struct sigaction sigact;
|
||
54 | 2dde134c | Tom Mullins | struct timeval time_prev, time_cur;
|
55 | int i, diff;
|
||
56 | 7bdb98c5 | Tom Mullins | |
57 | 5e03b78d | Tom Mullins | log_print("Serial device: %s", device);
|
58 | log_print("CRM server: http://%s/", server);
|
||
59 | 7bdb98c5 | Tom Mullins | |
60 | bzero(&sigact, sizeof(sigact));
|
||
61 | sigact.sa_flags = SA_RESTART; |
||
62 | sigemptyset(&sigact.sa_mask); |
||
63 | cc7646f9 | Tom Mullins | |
64 | sigact.sa_handler = sigint; |
||
65 | 7bdb98c5 | Tom Mullins | sigaction(SIGINT, &sigact, NULL);
|
66 | cc7646f9 | Tom Mullins | 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 | 7bdb98c5 | Tom Mullins | |
74 | 8f961e44 | Tom Mullins | if (query_init(server)) {
|
75 | return 1; |
||
76 | } |
||
77 | |||
78 | 7bdb98c5 | Tom Mullins | if (tool_init_mb(device)) {
|
79 | return 1; |
||
80 | } |
||
81 | |||
82 | 5e03b78d | Tom Mullins | log_print("Modbus initialized; polling tools...");
|
83 | 7bdb98c5 | Tom Mullins | |
84 | 2dde134c | Tom Mullins | gettimeofday(&time_prev, NULL);
|
85 | |||
86 | 7bdb98c5 | Tom Mullins | i = 0;
|
87 | while (run) {
|
||
88 | tool_poll(&tools[i]); |
||
89 | 75cef49f | Tom Mullins | event_q_process(); |
90 | cc7646f9 | Tom Mullins | if (refresh_cache) {
|
91 | log_print("Recieved SIGUSR1, refreshing cache content");
|
||
92 | query_refresh_cache(); |
||
93 | refresh_cache = 0;
|
||
94 | } |
||
95 | if (clear_cache) {
|
||
96 | log_print("Recieved SIGUSR2, clearing cache");
|
||
97 | cache_clear(); |
||
98 | clear_cache = 0;
|
||
99 | } |
||
100 | 2dde134c | Tom Mullins | |
101 | // slow loop period down to MB_TIMEOUT_MS
|
||
102 | gettimeofday(&time_cur, NULL);
|
||
103 | diff = (time_cur.tv_sec - time_prev.tv_sec) * 1000000
|
||
104 | + (time_cur.tv_usec - time_prev.tv_usec); |
||
105 | diff = MB_TIMEOUT_MS * 1000 - diff;
|
||
106 | if (diff > 0) { |
||
107 | usleep(diff); |
||
108 | } |
||
109 | gettimeofday(&time_prev, NULL);
|
||
110 | |||
111 | 7bdb98c5 | Tom Mullins | i = (i+1) % N_TOOLS;
|
112 | } |
||
113 | 13fd9e7e | Tom Mullins | log_print("Recieved SIGINT or SIGTERM, shutting down");
|
114 | 7bdb98c5 | Tom Mullins | |
115 | 5e03b78d | Tom Mullins | log_print("Disabling tools");
|
116 | 7bdb98c5 | Tom Mullins | for (i = 0; i < N_TOOLS; i++) { |
117 | tool_request_disable(&tools[i]); |
||
118 | } |
||
119 | |||
120 | 5e03b78d | Tom Mullins | log_print("Closing modbus connection");
|
121 | 7bdb98c5 | Tom Mullins | tool_close_mb(); |
122 | |||
123 | 5e03b78d | Tom Mullins | log_print("Exiting");
|
124 | cc7646f9 | Tom Mullins | query_cleanup(); |
125 | cache_clear(); |
||
126 | 7bdb98c5 | Tom Mullins | return 0; |
127 | } |
||
128 | cc7646f9 | Tom Mullins | |
129 | char *usage =
|
||
130 | "Usage: %s [-h] [-d device] [-s server[:port]] <cmd>\n"
|
||
131 | " -h prints this message\n"
|
||
132 | " -d specifies the serial port for Modbus\n"
|
||
133 | " defaults to /dev/ttyUSB0\n"
|
||
134 | " -s specifies the server where the CRM is running\n"
|
||
135 | " defaults to roboticsclub.org\n"
|
||
136 | " <cmd> can be any of the following:\n"
|
||
137 | " run runs tooltron if it is not already running\n"
|
||
138 | 13fd9e7e | Tom Mullins | " stop signals a running tooltron to shut down\n"
|
139 | " refresh signals a running tooltron to refresh its cache\n"
|
||
140 | " clear signals a running tooltron to clear its cache\n";
|
||
141 | cc7646f9 | Tom Mullins | |
142 | int main(int argc, char **argv) { |
||
143 | 0ed3db9d | Tom Mullins | int opt, status, as_root;
|
144 | cc7646f9 | Tom Mullins | const char *device = "/dev/ttyUSB0"; |
145 | const char *server = "roboticsclub.org"; |
||
146 | |||
147 | while ((opt = getopt(argc, argv, "hd:s:")) != -1) { |
||
148 | switch (opt) {
|
||
149 | case 'h': |
||
150 | /* Print usage, not an error */
|
||
151 | printf(usage, argv[0]);
|
||
152 | return 0; |
||
153 | case 'd': |
||
154 | device = optarg; |
||
155 | break;
|
||
156 | case 's': |
||
157 | server = optarg; |
||
158 | break;
|
||
159 | default:
|
||
160 | /* Unknown option, error */
|
||
161 | printf(usage, argv[0]);
|
||
162 | return 1; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if (optind >= argc) {
|
||
167 | /* Not enough arguments, error */
|
||
168 | printf(usage, argv[0]);
|
||
169 | return 1; |
||
170 | } |
||
171 | |||
172 | if (strcmp(argv[optind], "refresh") == 0) { |
||
173 | 13fd9e7e | Tom Mullins | |
174 | cc7646f9 | Tom Mullins | send_signal(SIGUSR1); |
175 | return 0; |
||
176 | 13fd9e7e | Tom Mullins | |
177 | cc7646f9 | Tom Mullins | } else if (strcmp(argv[optind], "clear") == 0) { |
178 | 13fd9e7e | Tom Mullins | |
179 | cc7646f9 | Tom Mullins | send_signal(SIGUSR2); |
180 | return 0; |
||
181 | |||
182 | 13fd9e7e | Tom Mullins | } else if (strcmp(argv[optind], "stop") == 0) { |
183 | |||
184 | send_signal(SIGTERM); |
||
185 | return 0; |
||
186 | |||
187 | } else if (strcmp(argv[optind], "run") == 0) { |
||
188 | |||
189 | 0ed3db9d | Tom Mullins | /* if we're not root, don't bother with the pid file */
|
190 | if (geteuid() == 0) { |
||
191 | as_root = 1;
|
||
192 | if (create_pid_file())
|
||
193 | /* pid file already exists, error */
|
||
194 | return 1; |
||
195 | } else {
|
||
196 | as_root = 0;
|
||
197 | log_print("Warning: not root. Running without a PID file");
|
||
198 | } |
||
199 | 13fd9e7e | Tom Mullins | |
200 | status = tooltron_main(device, server); |
||
201 | |||
202 | 0ed3db9d | Tom Mullins | if (as_root)
|
203 | remove_pid_file(); |
||
204 | 13fd9e7e | Tom Mullins | return status;
|
205 | |||
206 | } |
||
207 | cc7646f9 | Tom Mullins | |
208 | 13fd9e7e | Tom Mullins | printf("Unknown command \"%s\"\n", argv[optind]);
|
209 | printf(usage, argv[0]);
|
||
210 | return 1; |
||
211 | cc7646f9 | Tom Mullins | } |