root / mainbox / main.c @ e247ef4d
History | View | Annotate | Download (4.05 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 | |||
11 | cc7646f9 | Tom Mullins | #define SLEEP_MS 25 |
12 | 7bdb98c5 | Tom Mullins | |
13 | 15fa09f7 | Tom Mullins | static struct tool_t tools[] = { |
14 | cc7646f9 | Tom Mullins | 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 | 7bdb98c5 | Tom Mullins | }; |
25 | |||
26 | #define N_TOOLS (sizeof(tools)/sizeof(struct tool_t)) |
||
27 | |||
28 | volatile int run = 1; |
||
29 | cc7646f9 | Tom Mullins | volatile int refresh_cache = 0; |
30 | volatile int clear_cache = 0; |
||
31 | 7bdb98c5 | Tom Mullins | |
32 | void sigint(int sig) { |
||
33 | run = 0;
|
||
34 | } |
||
35 | |||
36 | cc7646f9 | Tom Mullins | void sigusr1(int sig) { |
37 | refresh_cache = 1;
|
||
38 | 8f961e44 | Tom Mullins | } |
39 | |||
40 | cc7646f9 | Tom Mullins | void sigusr2(int sig) { |
41 | clear_cache = 1;
|
||
42 | } |
||
43 | 8f961e44 | Tom Mullins | |
44 | cc7646f9 | Tom Mullins | 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 | 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 | i = 0;
|
||
85 | while (run) {
|
||
86 | tool_poll(&tools[i]); |
||
87 | 75cef49f | Tom Mullins | event_q_process(); |
88 | cc7646f9 | Tom Mullins | 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 | 7bdb98c5 | Tom Mullins | usleep(SLEEP_MS * (useconds_t)1000);
|
99 | i = (i+1) % N_TOOLS;
|
||
100 | } |
||
101 | 13fd9e7e | Tom Mullins | log_print("Recieved SIGINT or SIGTERM, shutting down");
|
102 | 7bdb98c5 | Tom Mullins | |
103 | 5e03b78d | Tom Mullins | log_print("Disabling tools");
|
104 | 7bdb98c5 | Tom Mullins | for (i = 0; i < N_TOOLS; i++) { |
105 | tool_request_disable(&tools[i]); |
||
106 | } |
||
107 | |||
108 | 5e03b78d | Tom Mullins | log_print("Closing modbus connection");
|
109 | 7bdb98c5 | Tom Mullins | tool_close_mb(); |
110 | |||
111 | 5e03b78d | Tom Mullins | log_print("Exiting");
|
112 | cc7646f9 | Tom Mullins | query_cleanup(); |
113 | cache_clear(); |
||
114 | 7bdb98c5 | Tom Mullins | return 0; |
115 | } |
||
116 | cc7646f9 | Tom Mullins | |
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 | 13fd9e7e | Tom Mullins | " 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 | cc7646f9 | Tom Mullins | |
130 | int main(int argc, char **argv) { |
||
131 | 13fd9e7e | Tom Mullins | int opt, status;
|
132 | cc7646f9 | Tom Mullins | 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 | 13fd9e7e | Tom Mullins | |
162 | cc7646f9 | Tom Mullins | send_signal(SIGUSR1); |
163 | return 0; |
||
164 | 13fd9e7e | Tom Mullins | |
165 | cc7646f9 | Tom Mullins | } else if (strcmp(argv[optind], "clear") == 0) { |
166 | 13fd9e7e | Tom Mullins | |
167 | cc7646f9 | Tom Mullins | send_signal(SIGUSR2); |
168 | return 0; |
||
169 | |||
170 | 13fd9e7e | Tom Mullins | } 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 (create_pid_file())
|
||
178 | /* pid file already exists, error */
|
||
179 | return 1; |
||
180 | |||
181 | status = tooltron_main(device, server); |
||
182 | |||
183 | remove_pid_file(); |
||
184 | return status;
|
||
185 | |||
186 | } |
||
187 | cc7646f9 | Tom Mullins | |
188 | 13fd9e7e | Tom Mullins | printf("Unknown command \"%s\"\n", argv[optind]);
|
189 | printf(usage, argv[0]);
|
||
190 | return 1; |
||
191 | cc7646f9 | Tom Mullins | } |