Statistics
| Branch: | Revision:

root / mainbox / main.c @ 15fa09f7

History | View | Annotate | Download (1.93 KB)

1
#include "tool.h"
2
#include "query.h"
3
#include <unistd.h>
4
#include <signal.h>
5
#include <strings.h>
6
#include <stdio.h>
7

    
8
#define SLEEP_MS 250
9

    
10
static struct tool_t tools[] = {
11
  TOOL_DECL("testa", 4),
12
  TOOL_DECL("testb", 5)
13
};
14

    
15
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t))
16

    
17
volatile int run = 1;
18

    
19
void sigint(int sig) {
20
  run = 0;
21
}
22

    
23
void print_usage(const char *name) {
24
  printf("Usage: %s [-h] [-d serial_device] [-s db_server[:port]]\n", name);
25
  printf("       -h prints this message\n");
26
  printf("       -d specifies the serial port for Modbus\n");
27
  printf("          defaults to /dev/ttyUSB0\n");
28
  printf("       -s specifies the server where the CRM is running\n");
29
  printf("          defaults to minecraft.roboclub.org:8000\n");
30
}
31

    
32
int main(int argc, char **argv) {
33
  int i, opt;
34
  struct sigaction sigact;
35
  const char *device = "/dev/ttyUSB0";
36
  const char *server = "minecraft.roboclub.org:8000";
37

    
38
  while ((opt = getopt(argc, argv, "hd:s:")) != -1) {
39
    switch (opt) {
40
      case 'h':
41
        print_usage(argv[0]);
42
        return 0;
43
      case 'd':
44
        device = optarg;
45
        break;
46
      case 's':
47
        server = optarg;
48
        break;
49
      default:
50
        print_usage(argv[0]);
51
        return 1;
52
    }
53
  }
54

    
55
  printf("Serial device: %s\n", device);
56
  printf("CRM server: http://%s/\n", server);
57

    
58
  bzero(&sigact, sizeof(sigact));
59
  sigact.sa_handler = sigint;
60
  sigact.sa_flags = SA_RESTART;
61
  sigemptyset(&sigact.sa_mask);
62
  sigaction(SIGINT, &sigact, NULL);
63

    
64
  if (query_init(server)) {
65
    return 1;
66
  }
67

    
68
  if (tool_init_mb(device)) {
69
    return 1;
70
  }
71

    
72
  printf("Modbus initialized; polling tools...\n");
73

    
74
  i = 0;
75
  while (run) {
76
    tool_poll(&tools[i]);
77
    usleep(SLEEP_MS * (useconds_t)1000);
78
    i = (i+1) % N_TOOLS;
79
  }
80

    
81
  printf("\nDisabling tools\n");
82
  for (i = 0; i < N_TOOLS; i++) {
83
    tool_request_disable(&tools[i]);
84
  }
85

    
86
  printf("Closing modbus connection\n");
87
  tool_close_mb();
88

    
89
  printf("Exiting\n");
90
  return 0;
91
}