Revision 15fa09f7
Made errors less noisy
If it can't connect to a tool, it will print an error once, and then
print another message when it is able to reconnect.
mainbox/main.c | ||
---|---|---|
7 | 7 |
|
8 | 8 |
#define SLEEP_MS 250 |
9 | 9 |
|
10 |
struct tool_t tools[] = { |
|
11 |
{4, "test", TS_INIT} |
|
10 |
static struct tool_t tools[] = { |
|
11 |
TOOL_DECL("testa", 4), |
|
12 |
TOOL_DECL("testb", 5) |
|
12 | 13 |
}; |
13 | 14 |
|
14 | 15 |
#define N_TOOLS (sizeof(tools)/sizeof(struct tool_t)) |
mainbox/tool.c | ||
---|---|---|
1 | 1 |
#include "tool.h" |
2 |
#include "query.h" |
|
2 | 3 |
#include "tooltron_mb.h" |
3 | 4 |
#include <modbus.h> |
4 | 5 |
#include <stdio.h> |
... | ... | |
56 | 57 |
} |
57 | 58 |
|
58 | 59 |
static void tool_grant_access(struct tool_t *tool) { |
59 |
printf("Granting access to %08x on %s\n", tool->user, tool->name); |
|
60 |
printf("Granting access to %08x on %s (%d)\n", tool->user, tool->name, |
|
61 |
tool->address); |
|
60 | 62 |
tool_write_coil(MB_COIL_EN, 1); |
61 | 63 |
tool->state = TS_ON; |
62 | 64 |
} |
63 | 65 |
|
64 | 66 |
static void tool_deny_access(struct tool_t *tool) { |
65 |
printf("Denying access to %08x on %s\n", tool->user, tool->name); |
|
67 |
printf("Denying access to %08x on %s (%d)\n", tool->user, tool->name, |
|
68 |
tool->address); |
|
66 | 69 |
tool_write_coil(MB_COIL_EN, 0); |
67 | 70 |
tool->state = TS_OFF; |
68 | 71 |
} |
69 | 72 |
|
70 | 73 |
void tool_request_disable(struct tool_t *tool) { |
71 |
printf("Requesting disable on %s\n", tool->name);
|
|
74 |
printf("Requesting disable on %s (%d)\n", tool->name, tool->address);
|
|
72 | 75 |
tool_write_coil(MB_COIL_REQ_DIS, 1); |
73 | 76 |
tool->state = TS_REQ_DIS; |
74 | 77 |
} |
... | ... | |
80 | 83 |
fprintf(stderr, "modbus_set_slave: %s\n", modbus_strerror(errno)); |
81 | 84 |
} |
82 | 85 |
|
86 |
/* If we can't read from the tool, we only want this error message to print |
|
87 |
* once, thus tool->connected */ |
|
83 | 88 |
if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) { |
84 |
fprintf(stderr, "modbus_read_bits: %s\n", modbus_strerror(errno)); |
|
89 |
if (tool->connected) { |
|
90 |
fprintf(stderr, "Cannot connect to %s (%d): %s\n", tool->name, |
|
91 |
tool->address, modbus_strerror(errno)); |
|
92 |
tool->connected = 0; |
|
93 |
} |
|
85 | 94 |
return; |
95 |
} else if (!tool->connected) { |
|
96 |
fprintf(stderr, "Reconnected to %s (%d)\n", tool->name, tool->address); |
|
97 |
tool->connected = 1; |
|
86 | 98 |
} |
87 | 99 |
|
100 |
/*uint16_t current; |
|
101 |
if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, ¤t) == -1) { |
|
102 |
fprintf(stderr, "modbus_read_registers: %s\n", modbus_strerror(errno)); |
|
103 |
} else { |
|
104 |
printf("Current: %d\n", current); |
|
105 |
}*/ |
|
106 |
|
|
88 | 107 |
/*printf("new:%d en:%d req_dis:%d init:%d\n", status[MB_COIL_NEW], |
89 | 108 |
status[MB_COIL_EN], status[MB_COIL_REQ_DIS], status[MB_COIL_INIT]);*/ |
90 | 109 |
|
... | ... | |
101 | 120 |
case TS_OFF: |
102 | 121 |
if (status[MB_COIL_NEW]) { |
103 | 122 |
tool_read_user(tool); |
104 |
// TODO check actual credentials |
|
105 |
if (tool->user == 0x023acbf6) { |
|
123 |
if (query_user_permission(tool->address, tool->user)) { |
|
106 | 124 |
tool_grant_access(tool); |
107 | 125 |
} else { |
108 | 126 |
tool_deny_access(tool); |
... | ... | |
112 | 130 |
|
113 | 131 |
case TS_ON: |
114 | 132 |
if (!status[MB_COIL_EN]) { |
115 |
printf("Tool %s is off\n", tool->name);
|
|
133 |
printf("Tool %s (%d) is off\n", tool->name, tool->address);
|
|
116 | 134 |
tool->state = TS_OFF; |
117 | 135 |
} |
118 | 136 |
break; |
119 | 137 |
|
120 | 138 |
case TS_REQ_DIS: |
121 | 139 |
if (!status[MB_COIL_EN]) { |
122 |
printf("Tool %s is off after requested disable\n", tool->name); |
|
140 |
printf("Tool %s (%d) is off after requested disable\n", tool->name, |
|
141 |
tool->address); |
|
123 | 142 |
tool->state = TS_OFF; |
124 | 143 |
} |
125 | 144 |
break; |
mainbox/tool.h | ||
---|---|---|
9 | 9 |
}; |
10 | 10 |
|
11 | 11 |
struct tool_t { |
12 |
int address; |
|
13 | 12 |
const char *name; |
13 |
int address; |
|
14 |
int connected; |
|
14 | 15 |
enum toolstate_t state; |
15 | 16 |
unsigned int user; |
16 | 17 |
}; |
17 | 18 |
|
19 |
#define TOOL_DECL(name, addr) {name, addr, 1, TS_INIT, 0} |
|
20 |
|
|
18 | 21 |
int tool_init_mb(const char *device); |
19 | 22 |
void tool_close_mb(); |
20 | 23 |
void tool_request_disable(struct tool_t *tool); |
Also available in: Unified diff