Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / main.c @ 5e03b78d

History | View | Annotate | Download (2.12 KB)

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

    
9
#define SLEEP_MS 10//250
10

    
11
static struct tool_t tools[] = {
12
  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
};
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
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
  printf("          defaults to roboticsclub.org\n");
38
}
39

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

    
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

    
63
  log_print("Serial device: %s", device);
64
  log_print("CRM server: http://%s/", server);
65

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

    
76
  if (tool_init_mb(device)) {
77
    return 1;
78
  }
79

    
80
  log_print("Modbus initialized; polling tools...");
81

    
82
  i = 0;
83
  while (run) {
84
    tool_poll(&tools[i]);
85
    event_q_process();
86
    usleep(SLEEP_MS * (useconds_t)1000);
87
    i = (i+1) % N_TOOLS;
88
  }
89

    
90
  log_print("Disabling tools");
91
  for (i = 0; i < N_TOOLS; i++) {
92
    tool_request_disable(&tools[i]);
93
  }
94

    
95
  log_print("Closing modbus connection");
96
  tool_close_mb();
97

    
98
  log_print("Exiting");
99
  return 0;
100
}