Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / main.c @ 5e8f735d

History | View | Annotate | Download (2.1 KB)

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