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