Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / main.c @ 5e03b78d

History | View | Annotate | Download (2.12 KB)

1 7bdb98c5 Tom Mullins
#include "tool.h"
2 8f961e44 Tom Mullins
#include "query.h"
3 5e03b78d Tom Mullins
#include "log.h"
4 7bdb98c5 Tom Mullins
#include <unistd.h>
5
#include <signal.h>
6
#include <strings.h>
7
#include <stdio.h>
8
9 75cef49f Tom Mullins
#define SLEEP_MS 10//250
10 7bdb98c5 Tom Mullins
11 15fa09f7 Tom Mullins
static struct tool_t tools[] = {
12 75cef49f Tom Mullins
  TOOL_DECL("test1", 1),
13
  TOOL_DECL("test2", 2),
14
  TOOL_DECL("test3", 3),
15
  TOOL_DECL("test4", 4),
16
  TOOL_DECL("test5", 5),
17
  TOOL_DECL("test6", 6),
18
  TOOL_DECL("test7", 7),
19
  TOOL_DECL("test8", 8),
20
  TOOL_DECL("test9", 9)
21 7bdb98c5 Tom Mullins
};
22
23
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t))
24
25
volatile int run = 1;
26
27
void sigint(int sig) {
28
  run = 0;
29
}
30
31 8f961e44 Tom Mullins
void print_usage(const char *name) {
32
  printf("Usage: %s [-h] [-d serial_device] [-s db_server[:port]]\n", name);
33
  printf("       -h prints this message\n");
34
  printf("       -d specifies the serial port for Modbus\n");
35
  printf("          defaults to /dev/ttyUSB0\n");
36
  printf("       -s specifies the server where the CRM is running\n");
37 5e8f735d Tom Mullins
  printf("          defaults to roboticsclub.org\n");
38 8f961e44 Tom Mullins
}
39
40 7bdb98c5 Tom Mullins
int main(int argc, char **argv) {
41 8f961e44 Tom Mullins
  int i, opt;
42 7bdb98c5 Tom Mullins
  struct sigaction sigact;
43
  const char *device = "/dev/ttyUSB0";
44 5e8f735d Tom Mullins
  const char *server = "roboticsclub.org";
45 8f961e44 Tom Mullins
46
  while ((opt = getopt(argc, argv, "hd:s:")) != -1) {
47
    switch (opt) {
48
      case 'h':
49
        print_usage(argv[0]);
50
        return 0;
51
      case 'd':
52
        device = optarg;
53
        break;
54
      case 's':
55
        server = optarg;
56
        break;
57
      default:
58
        print_usage(argv[0]);
59
        return 1;
60
    }
61
  }
62 7bdb98c5 Tom Mullins
63 5e03b78d Tom Mullins
  log_print("Serial device: %s", device);
64
  log_print("CRM server: http://%s/", server);
65 7bdb98c5 Tom Mullins
66
  bzero(&sigact, sizeof(sigact));
67
  sigact.sa_handler = sigint;
68
  sigact.sa_flags = SA_RESTART;
69
  sigemptyset(&sigact.sa_mask);
70
  sigaction(SIGINT, &sigact, NULL);
71
72 8f961e44 Tom Mullins
  if (query_init(server)) {
73
    return 1;
74
  }
75
76 7bdb98c5 Tom Mullins
  if (tool_init_mb(device)) {
77
    return 1;
78
  }
79
80 5e03b78d Tom Mullins
  log_print("Modbus initialized; polling tools...");
81 7bdb98c5 Tom Mullins
82
  i = 0;
83
  while (run) {
84
    tool_poll(&tools[i]);
85 75cef49f Tom Mullins
    event_q_process();
86 7bdb98c5 Tom Mullins
    usleep(SLEEP_MS * (useconds_t)1000);
87
    i = (i+1) % N_TOOLS;
88
  }
89
90 5e03b78d Tom Mullins
  log_print("Disabling tools");
91 7bdb98c5 Tom Mullins
  for (i = 0; i < N_TOOLS; i++) {
92
    tool_request_disable(&tools[i]);
93
  }
94
95 5e03b78d Tom Mullins
  log_print("Closing modbus connection");
96 7bdb98c5 Tom Mullins
  tool_close_mb();
97
98 5e03b78d Tom Mullins
  log_print("Exiting");
99 7bdb98c5 Tom Mullins
  return 0;
100
}