Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / tool.c @ master

History | View | Annotate | Download (4.44 KB)

1 7bdb98c5 Tom Mullins
#include "tool.h"
2 15fa09f7 Tom Mullins
#include "query.h"
3 75cef49f Tom Mullins
#include "event.h"
4 5e03b78d Tom Mullins
#include "log.h"
5 7bdb98c5 Tom Mullins
#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 14688426 Tom Mullins
  struct timeval timeout;
14
15 7bdb98c5 Tom Mullins
  ctx = modbus_new_rtu(device, MB_BAUD, 'N', 8, 1);
16
  if (ctx == NULL) {
17 5e03b78d Tom Mullins
    log_print("ERROR: modbus_new_rtu: %s", modbus_strerror(errno));
18 7bdb98c5 Tom Mullins
    return 1;
19
  }
20
21
  if (modbus_connect(ctx) == -1) {
22 5e03b78d Tom Mullins
    log_print("ERROR: modbus_connect: %s", modbus_strerror(errno));
23 7bdb98c5 Tom Mullins
    modbus_free(ctx);
24
    return 1;
25
  }
26
27 2dde134c Tom Mullins
  timeout.tv_sec = 0;
28
  timeout.tv_usec = MB_TIMEOUT_MS * 1000;
29
  modbus_set_response_timeout(ctx, &timeout);
30
31 14688426 Tom Mullins
  modbus_get_response_timeout(ctx, &timeout);
32 5e03b78d Tom Mullins
  log_print("Modbus response timeout is %lus %luus",
33
      timeout.tv_sec, timeout.tv_usec);
34 14688426 Tom Mullins
35 7bdb98c5 Tom Mullins
  return 0;
36
}
37
38
void tool_close_mb() {
39
  modbus_close(ctx);
40
  modbus_free(ctx);
41
}
42
43
static void tool_write_coil(int addr, int bit) {
44
  if (modbus_write_bit(ctx, addr, bit) == -1) {
45 5e03b78d Tom Mullins
    log_print("ERROR: modbus_write_bit: %s", modbus_strerror(errno));
46 7bdb98c5 Tom Mullins
  }
47
}
48
49
static void tool_init(struct tool_t *tool) {
50
  // TODO write current limit values
51
  tool_write_coil(MB_COIL_INIT, 1);
52
  tool->state = TS_OFF;
53
}
54
55 3811693d Tom Mullins
static void tool_read_user(struct tool_t *tool) {
56 7bdb98c5 Tom Mullins
  unsigned short serno[2];
57 14688426 Tom Mullins
  if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) {
58 5e03b78d Tom Mullins
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
59 7bdb98c5 Tom Mullins
    tool->user = 0;
60
  } else {
61
    tool->user = MODBUS_GET_INT32_FROM_INT16(serno, 0);
62
  }
63
}
64
65
static void tool_grant_access(struct tool_t *tool) {
66 38df0012 Tom Mullins
67 5e03b78d Tom Mullins
  log_print("Granting access to %08x on %s (%d)", tool->user, tool->name,
68 15fa09f7 Tom Mullins
      tool->address);
69 38df0012 Tom Mullins
70 7bdb98c5 Tom Mullins
  tool_write_coil(MB_COIL_EN, 1);
71 dc472500 Tom Mullins
  tool->state = TS_ON;
72 38df0012 Tom Mullins
73 75cef49f Tom Mullins
  tool->event = event_alloc();
74
  tool->event->user = tool->user;
75
  tool->event->tool_id = tool->address;
76
  tool->event->tstart = time(NULL);
77 38df0012 Tom Mullins
  tool->event->succ = 1;
78 7bdb98c5 Tom Mullins
}
79
80
static void tool_deny_access(struct tool_t *tool) {
81 38df0012 Tom Mullins
  struct event_t *event;
82
83 5e03b78d Tom Mullins
  log_print("Denying access to %08x on %s (%d)", tool->user, tool->name,
84 15fa09f7 Tom Mullins
      tool->address);
85 38df0012 Tom Mullins
86 7bdb98c5 Tom Mullins
  tool_write_coil(MB_COIL_EN, 0);
87 dc472500 Tom Mullins
  tool->state = TS_OFF;
88 38df0012 Tom Mullins
89
  event = event_alloc();
90
  event->user = tool->user;
91
  event->tool_id = tool->address;
92
  event->tstart = time(NULL);
93
  event->tend = event->tstart;
94
  event->succ = 0;
95
  event_q_push(event);
96 7bdb98c5 Tom Mullins
}
97
98 75cef49f Tom Mullins
static void tool_off(struct tool_t *tool) {
99
  tool->state = TS_OFF;
100
  tool->event->tend = time(NULL);
101
  event_q_push(tool->event);
102
  tool->event = NULL;
103
}
104
105 7bdb98c5 Tom Mullins
void tool_request_disable(struct tool_t *tool) {
106 cc7646f9 Tom Mullins
  if (tool->state == TS_ON) {
107
    log_print("Requesting disable on %s (%d)", tool->name, tool->address);
108
    tool_write_coil(MB_COIL_REQ_DIS, 1);
109
    tool->state = TS_REQ_DIS;
110
  }
111 7bdb98c5 Tom Mullins
}
112
113
void tool_poll(struct tool_t *tool) {
114
  unsigned char status[N_COILS];
115
116 14688426 Tom Mullins
  if (modbus_set_slave(ctx, tool->address) == -1) {
117 5e03b78d Tom Mullins
    log_print("ERROR: modbus_set_slave: %s", modbus_strerror(errno));
118 7bdb98c5 Tom Mullins
  }
119
120 15fa09f7 Tom Mullins
  /* If we can't read from the tool, we only want this error message to print
121
   * once, thus tool->connected */
122 7bdb98c5 Tom Mullins
  if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) {
123 15fa09f7 Tom Mullins
    if (tool->connected) {
124 5e03b78d Tom Mullins
      log_print("Cannot connect to %s (%d): %s", tool->name,
125 15fa09f7 Tom Mullins
          tool->address, modbus_strerror(errno));
126
      tool->connected = 0;
127
    }
128 7bdb98c5 Tom Mullins
    return;
129 15fa09f7 Tom Mullins
  } else if (!tool->connected) {
130 5e03b78d Tom Mullins
    log_print("Reconnected to %s (%d)", tool->name, tool->address);
131 15fa09f7 Tom Mullins
    tool->connected = 1;
132 7bdb98c5 Tom Mullins
  }
133
134 15fa09f7 Tom Mullins
  /*uint16_t current;
135
  if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, &current) == -1) {
136 5e03b78d Tom Mullins
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
137 15fa09f7 Tom Mullins
  } else {
138
    printf("Current: %d\n", current);
139
  }*/
140
141 1b054655 Tom Mullins
  /*printf("new:%d en:%d req_dis:%d init:%d\n", status[MB_COIL_NEW],
142
      status[MB_COIL_EN], status[MB_COIL_REQ_DIS], status[MB_COIL_INIT]);*/
143 dc472500 Tom Mullins
144 7bdb98c5 Tom Mullins
  if (!status[MB_COIL_INIT]) {
145
    tool->state = TS_INIT;
146
  }
147
148
  switch (tool->state) {
149
150
    case TS_INIT:
151
      tool_init(tool);
152
      break;
153
154
    case TS_OFF:
155
      if (status[MB_COIL_NEW]) {
156 3811693d Tom Mullins
        tool_read_user(tool);
157 15fa09f7 Tom Mullins
        if (query_user_permission(tool->address, tool->user)) {
158 7bdb98c5 Tom Mullins
          tool_grant_access(tool);
159 1b054655 Tom Mullins
        } else {
160 7bdb98c5 Tom Mullins
          tool_deny_access(tool);
161 1b054655 Tom Mullins
        }
162 7bdb98c5 Tom Mullins
      }
163
      break;
164
165
    case TS_ON:
166
      if (!status[MB_COIL_EN]) {
167 5e03b78d Tom Mullins
        log_print("Tool %s (%d) is off", tool->name, tool->address);
168 75cef49f Tom Mullins
        tool_off(tool);
169 7bdb98c5 Tom Mullins
      }
170
      break;
171
172
    case TS_REQ_DIS:
173
      if (!status[MB_COIL_EN]) {
174 5e03b78d Tom Mullins
        log_print("Tool %s (%d) is off after requested disable", tool->name,
175 15fa09f7 Tom Mullins
            tool->address);
176 75cef49f Tom Mullins
        tool_off(tool);
177 7bdb98c5 Tom Mullins
      }
178
      break;
179
180
  }
181
182
}