Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / tool.c @ 5e03b78d

History | View | Annotate | Download (4.29 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 14688426 Tom Mullins
  modbus_get_response_timeout(ctx, &timeout);
28 5e03b78d Tom Mullins
  log_print("Modbus response timeout is %lus %luus",
29
      timeout.tv_sec, timeout.tv_usec);
30 14688426 Tom Mullins
31 7bdb98c5 Tom Mullins
  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 5e03b78d Tom Mullins
    log_print("ERROR: modbus_write_bit: %s", modbus_strerror(errno));
42 7bdb98c5 Tom Mullins
  }
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 3811693d Tom Mullins
static void tool_read_user(struct tool_t *tool) {
52 7bdb98c5 Tom Mullins
  unsigned short serno[2];
53 14688426 Tom Mullins
  if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) {
54 5e03b78d Tom Mullins
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
55 7bdb98c5 Tom Mullins
    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 38df0012 Tom Mullins
63 5e03b78d Tom Mullins
  log_print("Granting access to %08x on %s (%d)", tool->user, tool->name,
64 15fa09f7 Tom Mullins
      tool->address);
65 38df0012 Tom Mullins
66 7bdb98c5 Tom Mullins
  tool_write_coil(MB_COIL_EN, 1);
67 dc472500 Tom Mullins
  tool->state = TS_ON;
68 38df0012 Tom Mullins
69 75cef49f Tom Mullins
  tool->event = event_alloc();
70
  tool->event->user = tool->user;
71
  tool->event->tool_id = tool->address;
72
  tool->event->tstart = time(NULL);
73 38df0012 Tom Mullins
  tool->event->succ = 1;
74 7bdb98c5 Tom Mullins
}
75
76
static void tool_deny_access(struct tool_t *tool) {
77 38df0012 Tom Mullins
  struct event_t *event;
78
79 5e03b78d Tom Mullins
  log_print("Denying access to %08x on %s (%d)", tool->user, tool->name,
80 15fa09f7 Tom Mullins
      tool->address);
81 38df0012 Tom Mullins
82 7bdb98c5 Tom Mullins
  tool_write_coil(MB_COIL_EN, 0);
83 dc472500 Tom Mullins
  tool->state = TS_OFF;
84 38df0012 Tom Mullins
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 7bdb98c5 Tom Mullins
}
93
94 75cef49f Tom Mullins
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 7bdb98c5 Tom Mullins
void tool_request_disable(struct tool_t *tool) {
102 5e03b78d Tom Mullins
  log_print("Requesting disable on %s (%d)", tool->name, tool->address);
103 7bdb98c5 Tom Mullins
  tool_write_coil(MB_COIL_REQ_DIS, 1);
104 dc472500 Tom Mullins
  tool->state = TS_REQ_DIS;
105 7bdb98c5 Tom Mullins
}
106
107
void tool_poll(struct tool_t *tool) {
108
  unsigned char status[N_COILS];
109
110 14688426 Tom Mullins
  if (modbus_set_slave(ctx, tool->address) == -1) {
111 5e03b78d Tom Mullins
    log_print("ERROR: modbus_set_slave: %s", modbus_strerror(errno));
112 7bdb98c5 Tom Mullins
  }
113
114 15fa09f7 Tom Mullins
  /* If we can't read from the tool, we only want this error message to print
115
   * once, thus tool->connected */
116 7bdb98c5 Tom Mullins
  if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) {
117 15fa09f7 Tom Mullins
    if (tool->connected) {
118 5e03b78d Tom Mullins
      log_print("Cannot connect to %s (%d): %s", tool->name,
119 15fa09f7 Tom Mullins
          tool->address, modbus_strerror(errno));
120
      tool->connected = 0;
121
    }
122 7bdb98c5 Tom Mullins
    return;
123 15fa09f7 Tom Mullins
  } else if (!tool->connected) {
124 5e03b78d Tom Mullins
    log_print("Reconnected to %s (%d)", tool->name, tool->address);
125 15fa09f7 Tom Mullins
    tool->connected = 1;
126 7bdb98c5 Tom Mullins
  }
127
128 15fa09f7 Tom Mullins
  /*uint16_t current;
129
  if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, &current) == -1) {
130 5e03b78d Tom Mullins
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
131 15fa09f7 Tom Mullins
  } else {
132
    printf("Current: %d\n", current);
133
  }*/
134
135 1b054655 Tom Mullins
  /*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 dc472500 Tom Mullins
138 7bdb98c5 Tom Mullins
  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 3811693d Tom Mullins
        tool_read_user(tool);
151 15fa09f7 Tom Mullins
        if (query_user_permission(tool->address, tool->user)) {
152 7bdb98c5 Tom Mullins
          tool_grant_access(tool);
153 1b054655 Tom Mullins
        } else {
154 7bdb98c5 Tom Mullins
          tool_deny_access(tool);
155 1b054655 Tom Mullins
        }
156 7bdb98c5 Tom Mullins
      }
157
      break;
158
159
    case TS_ON:
160
      if (!status[MB_COIL_EN]) {
161 5e03b78d Tom Mullins
        log_print("Tool %s (%d) is off", tool->name, tool->address);
162 75cef49f Tom Mullins
        tool_off(tool);
163 7bdb98c5 Tom Mullins
      }
164
      break;
165
166
    case TS_REQ_DIS:
167
      if (!status[MB_COIL_EN]) {
168 5e03b78d Tom Mullins
        log_print("Tool %s (%d) is off after requested disable", tool->name,
169 15fa09f7 Tom Mullins
            tool->address);
170 75cef49f Tom Mullins
        tool_off(tool);
171 7bdb98c5 Tom Mullins
      }
172
      break;
173
174
  }
175
176
}