Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / main.c @ 5305e5e7

History | View | Annotate | Download (2.13 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 10//250
9

    
10
static struct tool_t tools[] = {
11
  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
};
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
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
  printf("          defaults to minecraft.roboclub.org:8000\n");
37
}
38

    
39
int main(int argc, char **argv) {
40
  int i, opt;
41
  struct sigaction sigact;
42
  const char *device = "/dev/ttyUSB0";
43
  const char *server = "minecraft.roboclub.org:8000";
44

    
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

    
62
  printf("Serial device: %s\n", device);
63
  printf("CRM server: http://%s/\n", server);
64

    
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
  if (query_init(server)) {
72
    return 1;
73
  }
74

    
75
  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
    event_q_process();
85
    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
}