Statistics
| Branch: | Revision:

root / mainbox / tool.c @ 5e03b78d

History | View | Annotate | Download (4.29 KB)

1
#include "tool.h"
2
#include "query.h"
3
#include "event.h"
4
#include "log.h"
5
#include "tooltron_mb.h"
6
#include <modbus.h>
7
#include <stdio.h>
8
#include <errno.h>
9

    
10
modbus_t *ctx;
11

    
12
int tool_init_mb(const char *device) {
13
  struct timeval timeout;
14

    
15
  ctx = modbus_new_rtu(device, MB_BAUD, 'N', 8, 1);
16
  if (ctx == NULL) {
17
    log_print("ERROR: modbus_new_rtu: %s", modbus_strerror(errno));
18
    return 1;
19
  }
20

    
21
  if (modbus_connect(ctx) == -1) {
22
    log_print("ERROR: modbus_connect: %s", modbus_strerror(errno));
23
    modbus_free(ctx);
24
    return 1;
25
  }
26

    
27
  modbus_get_response_timeout(ctx, &timeout);
28
  log_print("Modbus response timeout is %lus %luus",
29
      timeout.tv_sec, timeout.tv_usec);
30

    
31
  return 0;
32
}
33

    
34
void tool_close_mb() {
35
  modbus_close(ctx);
36
  modbus_free(ctx);
37
}
38

    
39
static void tool_write_coil(int addr, int bit) {
40
  if (modbus_write_bit(ctx, addr, bit) == -1) {
41
    log_print("ERROR: modbus_write_bit: %s", modbus_strerror(errno));
42
  }
43
}
44

    
45
static void tool_init(struct tool_t *tool) {
46
  // TODO write current limit values
47
  tool_write_coil(MB_COIL_INIT, 1);
48
  tool->state = TS_OFF;
49
}
50

    
51
static void tool_read_user(struct tool_t *tool) {
52
  unsigned short serno[2];
53
  if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) {
54
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
55
    tool->user = 0;
56
  } else {
57
    tool->user = MODBUS_GET_INT32_FROM_INT16(serno, 0);
58
  }
59
}
60

    
61
static void tool_grant_access(struct tool_t *tool) {
62

    
63
  log_print("Granting access to %08x on %s (%d)", tool->user, tool->name,
64
      tool->address);
65

    
66
  tool_write_coil(MB_COIL_EN, 1);
67
  tool->state = TS_ON;
68

    
69
  tool->event = event_alloc();
70
  tool->event->user = tool->user;
71
  tool->event->tool_id = tool->address;
72
  tool->event->tstart = time(NULL);
73
  tool->event->succ = 1;
74
}
75

    
76
static void tool_deny_access(struct tool_t *tool) {
77
  struct event_t *event;
78

    
79
  log_print("Denying access to %08x on %s (%d)", tool->user, tool->name,
80
      tool->address);
81

    
82
  tool_write_coil(MB_COIL_EN, 0);
83
  tool->state = TS_OFF;
84

    
85
  event = event_alloc();
86
  event->user = tool->user;
87
  event->tool_id = tool->address;
88
  event->tstart = time(NULL);
89
  event->tend = event->tstart;
90
  event->succ = 0;
91
  event_q_push(event);
92
}
93

    
94
static void tool_off(struct tool_t *tool) {
95
  tool->state = TS_OFF;
96
  tool->event->tend = time(NULL);
97
  event_q_push(tool->event);
98
  tool->event = NULL;
99
}
100

    
101
void tool_request_disable(struct tool_t *tool) {
102
  log_print("Requesting disable on %s (%d)", tool->name, tool->address);
103
  tool_write_coil(MB_COIL_REQ_DIS, 1);
104
  tool->state = TS_REQ_DIS;
105
}
106

    
107
void tool_poll(struct tool_t *tool) {
108
  unsigned char status[N_COILS];
109

    
110
  if (modbus_set_slave(ctx, tool->address) == -1) {
111
    log_print("ERROR: modbus_set_slave: %s", modbus_strerror(errno));
112
  }
113

    
114
  /* If we can't read from the tool, we only want this error message to print
115
   * once, thus tool->connected */
116
  if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) {
117
    if (tool->connected) {
118
      log_print("Cannot connect to %s (%d): %s", tool->name,
119
          tool->address, modbus_strerror(errno));
120
      tool->connected = 0;
121
    }
122
    return;
123
  } else if (!tool->connected) {
124
    log_print("Reconnected to %s (%d)", tool->name, tool->address);
125
    tool->connected = 1;
126
  }
127

    
128
  /*uint16_t current;
129
  if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, &current) == -1) {
130
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
131
  } else {
132
    printf("Current: %d\n", current);
133
  }*/
134

    
135
  /*printf("new:%d en:%d req_dis:%d init:%d\n", status[MB_COIL_NEW],
136
      status[MB_COIL_EN], status[MB_COIL_REQ_DIS], status[MB_COIL_INIT]);*/
137

    
138
  if (!status[MB_COIL_INIT]) {
139
    tool->state = TS_INIT;
140
  }
141

    
142
  switch (tool->state) {
143

    
144
    case TS_INIT:
145
      tool_init(tool);
146
      break;
147

    
148
    case TS_OFF:
149
      if (status[MB_COIL_NEW]) {
150
        tool_read_user(tool);
151
        if (query_user_permission(tool->address, tool->user)) {
152
          tool_grant_access(tool);
153
        } else {
154
          tool_deny_access(tool);
155
        }
156
      }
157
      break;
158

    
159
    case TS_ON:
160
      if (!status[MB_COIL_EN]) {
161
        log_print("Tool %s (%d) is off", tool->name, tool->address);
162
        tool_off(tool);
163
      }
164
      break;
165

    
166
    case TS_REQ_DIS:
167
      if (!status[MB_COIL_EN]) {
168
        log_print("Tool %s (%d) is off after requested disable", tool->name,
169
            tool->address);
170
        tool_off(tool);
171
      }
172
      break;
173

    
174
  }
175

    
176
}