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