Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / tool.c @ ea46eeca

History | View | Annotate | Download (4.33 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 cc7646f9 Tom Mullins
  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 7bdb98c5 Tom Mullins
}
108
109
void tool_poll(struct tool_t *tool) {
110
  unsigned char status[N_COILS];
111
112 14688426 Tom Mullins
  if (modbus_set_slave(ctx, tool->address) == -1) {
113 5e03b78d Tom Mullins
    log_print("ERROR: modbus_set_slave: %s", modbus_strerror(errno));
114 7bdb98c5 Tom Mullins
  }
115
116 15fa09f7 Tom Mullins
  /* If we can't read from the tool, we only want this error message to print
117
   * once, thus tool->connected */
118 7bdb98c5 Tom Mullins
  if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) {
119 15fa09f7 Tom Mullins
    if (tool->connected) {
120 5e03b78d Tom Mullins
      log_print("Cannot connect to %s (%d): %s", tool->name,
121 15fa09f7 Tom Mullins
          tool->address, modbus_strerror(errno));
122
      tool->connected = 0;
123
    }
124 7bdb98c5 Tom Mullins
    return;
125 15fa09f7 Tom Mullins
  } else if (!tool->connected) {
126 5e03b78d Tom Mullins
    log_print("Reconnected to %s (%d)", tool->name, tool->address);
127 15fa09f7 Tom Mullins
    tool->connected = 1;
128 7bdb98c5 Tom Mullins
  }
129
130 15fa09f7 Tom Mullins
  /*uint16_t current;
131
  if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, &current) == -1) {
132 5e03b78d Tom Mullins
    log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
133 15fa09f7 Tom Mullins
  } else {
134
    printf("Current: %d\n", current);
135
  }*/
136
137 1b054655 Tom Mullins
  /*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 dc472500 Tom Mullins
140 7bdb98c5 Tom Mullins
  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 3811693d Tom Mullins
        tool_read_user(tool);
153 15fa09f7 Tom Mullins
        if (query_user_permission(tool->address, tool->user)) {
154 7bdb98c5 Tom Mullins
          tool_grant_access(tool);
155 1b054655 Tom Mullins
        } else {
156 7bdb98c5 Tom Mullins
          tool_deny_access(tool);
157 1b054655 Tom Mullins
        }
158 7bdb98c5 Tom Mullins
      }
159
      break;
160
161
    case TS_ON:
162
      if (!status[MB_COIL_EN]) {
163 5e03b78d Tom Mullins
        log_print("Tool %s (%d) is off", tool->name, tool->address);
164 75cef49f Tom Mullins
        tool_off(tool);
165 7bdb98c5 Tom Mullins
      }
166
      break;
167
168
    case TS_REQ_DIS:
169
      if (!status[MB_COIL_EN]) {
170 5e03b78d Tom Mullins
        log_print("Tool %s (%d) is off after requested disable", tool->name,
171 15fa09f7 Tom Mullins
            tool->address);
172 75cef49f Tom Mullins
        tool_off(tool);
173 7bdb98c5 Tom Mullins
      }
174
      break;
175
176
  }
177
178
}