root / mainbox / tool.c @ master
History | View | Annotate | Download (4.44 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 |
timeout.tv_sec = 0;
|
28 |
timeout.tv_usec = MB_TIMEOUT_MS * 1000;
|
29 |
modbus_set_response_timeout(ctx, &timeout); |
30 |
|
31 |
modbus_get_response_timeout(ctx, &timeout); |
32 |
log_print("Modbus response timeout is %lus %luus",
|
33 |
timeout.tv_sec, timeout.tv_usec); |
34 |
|
35 |
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 |
log_print("ERROR: modbus_write_bit: %s", modbus_strerror(errno));
|
46 |
} |
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 |
static void tool_read_user(struct tool_t *tool) { |
56 |
unsigned short serno[2]; |
57 |
if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) { |
58 |
log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
|
59 |
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 |
|
67 |
log_print("Granting access to %08x on %s (%d)", tool->user, tool->name,
|
68 |
tool->address); |
69 |
|
70 |
tool_write_coil(MB_COIL_EN, 1);
|
71 |
tool->state = TS_ON; |
72 |
|
73 |
tool->event = event_alloc(); |
74 |
tool->event->user = tool->user; |
75 |
tool->event->tool_id = tool->address; |
76 |
tool->event->tstart = time(NULL);
|
77 |
tool->event->succ = 1;
|
78 |
} |
79 |
|
80 |
static void tool_deny_access(struct tool_t *tool) { |
81 |
struct event_t *event;
|
82 |
|
83 |
log_print("Denying access to %08x on %s (%d)", tool->user, tool->name,
|
84 |
tool->address); |
85 |
|
86 |
tool_write_coil(MB_COIL_EN, 0);
|
87 |
tool->state = TS_OFF; |
88 |
|
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 |
} |
97 |
|
98 |
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 |
void tool_request_disable(struct tool_t *tool) { |
106 |
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 |
} |
112 |
|
113 |
void tool_poll(struct tool_t *tool) { |
114 |
unsigned char status[N_COILS]; |
115 |
|
116 |
if (modbus_set_slave(ctx, tool->address) == -1) { |
117 |
log_print("ERROR: modbus_set_slave: %s", modbus_strerror(errno));
|
118 |
} |
119 |
|
120 |
/* If we can't read from the tool, we only want this error message to print
|
121 |
* once, thus tool->connected */
|
122 |
if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) { |
123 |
if (tool->connected) {
|
124 |
log_print("Cannot connect to %s (%d): %s", tool->name,
|
125 |
tool->address, modbus_strerror(errno)); |
126 |
tool->connected = 0;
|
127 |
} |
128 |
return;
|
129 |
} else if (!tool->connected) { |
130 |
log_print("Reconnected to %s (%d)", tool->name, tool->address);
|
131 |
tool->connected = 1;
|
132 |
} |
133 |
|
134 |
/*uint16_t current;
|
135 |
if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, ¤t) == -1) {
|
136 |
log_print("ERROR: modbus_read_registers: %s", modbus_strerror(errno));
|
137 |
} else {
|
138 |
printf("Current: %d\n", current);
|
139 |
}*/
|
140 |
|
141 |
/*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 |
|
144 |
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 |
tool_read_user(tool); |
157 |
if (query_user_permission(tool->address, tool->user)) {
|
158 |
tool_grant_access(tool); |
159 |
} else {
|
160 |
tool_deny_access(tool); |
161 |
} |
162 |
} |
163 |
break;
|
164 |
|
165 |
case TS_ON:
|
166 |
if (!status[MB_COIL_EN]) {
|
167 |
log_print("Tool %s (%d) is off", tool->name, tool->address);
|
168 |
tool_off(tool); |
169 |
} |
170 |
break;
|
171 |
|
172 |
case TS_REQ_DIS:
|
173 |
if (!status[MB_COIL_EN]) {
|
174 |
log_print("Tool %s (%d) is off after requested disable", tool->name,
|
175 |
tool->address); |
176 |
tool_off(tool); |
177 |
} |
178 |
break;
|
179 |
|
180 |
} |
181 |
|
182 |
} |