Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / main.c @ cc7646f9

History | View | Annotate | Download (3.81 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

    
102
  log_print("Disabling tools");
103
  for (i = 0; i < N_TOOLS; i++) {
104
    tool_request_disable(&tools[i]);
105
  }
106

    
107
  log_print("Closing modbus connection");
108
  tool_close_mb();
109

    
110
  log_print("Exiting");
111
  query_cleanup();
112
  cache_clear();
113
  return 0;
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
}