Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / tool.c @ cc7646f9

History | View | Annotate | Download (4.33 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
  if (tool->state == TS_ON) {
103
    log_print("Requesting disable on %s (%d)", tool->name, tool->address);
104
    tool_write_coil(MB_COIL_REQ_DIS, 1);
105
    tool->state = TS_REQ_DIS;
106
  }
107
}
108

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

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

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

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

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

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

    
144
  switch (tool->state) {
145

    
146
    case TS_INIT:
147
      tool_init(tool);
148
      break;
149

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

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

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

    
176
  }
177

    
178
}