Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / main.c @ master

History | View | Annotate | Download (4.63 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
#include <sys/time.h>
11

    
12
static struct tool_t tools[] = {
13
  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
};
24

    
25
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t))
26

    
27
volatile int run = 1;
28
volatile int refresh_cache = 0;
29
volatile int clear_cache = 0;
30

    
31
void sigint(int sig) {
32
  run = 0;
33
}
34

    
35
void sigusr1(int sig) {
36
  refresh_cache = 1;
37
}
38

    
39
void sigusr2(int sig) {
40
  clear_cache = 1;
41
}
42

    
43
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
  struct timeval time_prev, time_cur;
55
  int i, diff;
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
  gettimeofday(&time_prev, NULL);
85

    
86
  i = 0;
87
  while (run) {
88
    tool_poll(&tools[i]);
89
    event_q_process();
90
    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

    
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
    i = (i+1) % N_TOOLS;
112
  }
113
  log_print("Recieved SIGINT or SIGTERM, shutting down");
114

    
115
  log_print("Disabling tools");
116
  for (i = 0; i < N_TOOLS; i++) {
117
    tool_request_disable(&tools[i]);
118
  }
119

    
120
  log_print("Closing modbus connection");
121
  tool_close_mb();
122

    
123
  log_print("Exiting");
124
  query_cleanup();
125
  cache_clear();
126
  return 0;
127
}
128

    
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
"          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

    
142
int main(int argc, char **argv) {
143
  int opt, status, as_root;
144
  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

    
174
    send_signal(SIGUSR1);
175
    return 0;
176

    
177
  } else if (strcmp(argv[optind], "clear") == 0) {
178

    
179
    send_signal(SIGUSR2);
180
    return 0;
181

    
182
  } 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
    /* 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

    
200
    status = tooltron_main(device, server);
201

    
202
    if (as_root)
203
      remove_pid_file();
204
    return status;
205

    
206
  }
207

    
208
  printf("Unknown command \"%s\"\n", argv[optind]);
209
  printf(usage, argv[0]);
210
  return 1;
211
}