root / mainbox / tool.c @ 1085ef77
History | View | Annotate | Download (3.08 KB)
1 |
#include "tool.h" |
---|---|
2 |
#include "tooltron_mb.h" |
3 |
#include <modbus.h> |
4 |
#include <stdio.h> |
5 |
#include <errno.h> |
6 |
#include <stdlib.h> // TODO temporary |
7 |
|
8 |
modbus_t *ctx; |
9 |
|
10 |
int tool_init_mb(const char *device) { |
11 |
struct timeval timeout;
|
12 |
|
13 |
ctx = modbus_new_rtu(device, MB_BAUD, 'N', 8, 1); |
14 |
if (ctx == NULL) { |
15 |
fprintf(stderr, "modbus_new_rtu: %s\n", modbus_strerror(errno));
|
16 |
return 1; |
17 |
} |
18 |
|
19 |
if (modbus_connect(ctx) == -1) { |
20 |
fprintf(stderr, "modbus_connect: %s\n", modbus_strerror(errno));
|
21 |
modbus_free(ctx); |
22 |
return 1; |
23 |
} |
24 |
|
25 |
modbus_get_response_timeout(ctx, &timeout); |
26 |
printf("Response timeout is %lus %luus\n", timeout.tv_sec, timeout.tv_usec);
|
27 |
|
28 |
return 0; |
29 |
} |
30 |
|
31 |
void tool_close_mb() {
|
32 |
modbus_close(ctx); |
33 |
modbus_free(ctx); |
34 |
} |
35 |
|
36 |
static void tool_write_coil(int addr, int bit) { |
37 |
if (modbus_write_bit(ctx, addr, bit) == -1) { |
38 |
fprintf(stderr, "modbus_write_bit: %s\n", modbus_strerror(errno));
|
39 |
} |
40 |
} |
41 |
|
42 |
static void tool_init(struct tool_t *tool) { |
43 |
// TODO write current limit values
|
44 |
tool_write_coil(MB_COIL_INIT, 1);
|
45 |
tool->state = TS_OFF; |
46 |
} |
47 |
|
48 |
static void tool_read_user(struct tool_t *tool) { |
49 |
unsigned short serno[2]; |
50 |
if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) { |
51 |
fprintf(stderr, "modbus_read_registers: %s\n", modbus_strerror(errno));
|
52 |
tool->user = 0;
|
53 |
} else {
|
54 |
tool->user = MODBUS_GET_INT32_FROM_INT16(serno, 0);
|
55 |
} |
56 |
} |
57 |
|
58 |
static void tool_grant_access(struct tool_t *tool) { |
59 |
printf("Granting access to %08x on %s\n", tool->user, tool->name);
|
60 |
tool_write_coil(MB_COIL_EN, 1);
|
61 |
tool->state = TS_ON; |
62 |
} |
63 |
|
64 |
static void tool_deny_access(struct tool_t *tool) { |
65 |
printf("Denying access to %08x on %s\n", tool->user, tool->name);
|
66 |
tool_write_coil(MB_COIL_EN, 0);
|
67 |
tool->state = TS_OFF; |
68 |
} |
69 |
|
70 |
void tool_request_disable(struct tool_t *tool) { |
71 |
printf("Requesting disable on %s\n", tool->name);
|
72 |
tool_write_coil(MB_COIL_REQ_DIS, 1);
|
73 |
tool->state = TS_REQ_DIS; |
74 |
} |
75 |
|
76 |
void tool_poll(struct tool_t *tool) { |
77 |
unsigned char status[N_COILS]; |
78 |
|
79 |
if (modbus_set_slave(ctx, tool->address) == -1) { |
80 |
fprintf(stderr, "modbus_set_slave: %s\n", modbus_strerror(errno));
|
81 |
} |
82 |
|
83 |
if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) { |
84 |
fprintf(stderr, "modbus_read_bits: %s\n", modbus_strerror(errno));
|
85 |
return;
|
86 |
} |
87 |
|
88 |
/*printf("new:%d en:%d req_dis:%d init:%d\n", status[MB_COIL_NEW],
|
89 |
status[MB_COIL_EN], status[MB_COIL_REQ_DIS], status[MB_COIL_INIT]);*/
|
90 |
|
91 |
if (!status[MB_COIL_INIT]) {
|
92 |
tool->state = TS_INIT; |
93 |
} |
94 |
|
95 |
switch (tool->state) {
|
96 |
|
97 |
case TS_INIT:
|
98 |
tool_init(tool); |
99 |
break;
|
100 |
|
101 |
case TS_OFF:
|
102 |
if (status[MB_COIL_NEW]) {
|
103 |
tool_read_user(tool); |
104 |
// TODO check actual credentials
|
105 |
if (tool->user == 0x023acbf6) { |
106 |
tool_grant_access(tool); |
107 |
} else {
|
108 |
tool_deny_access(tool); |
109 |
} |
110 |
} |
111 |
break;
|
112 |
|
113 |
case TS_ON:
|
114 |
if (!status[MB_COIL_EN]) {
|
115 |
printf("Tool %s is off\n", tool->name);
|
116 |
tool->state = TS_OFF; |
117 |
} |
118 |
break;
|
119 |
|
120 |
case TS_REQ_DIS:
|
121 |
if (!status[MB_COIL_EN]) {
|
122 |
printf("Tool %s is off after requested disable\n", tool->name);
|
123 |
tool->state = TS_OFF; |
124 |
} |
125 |
break;
|
126 |
|
127 |
} |
128 |
|
129 |
} |