-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathbcmxcp_ser.c
342 lines (260 loc) · 7.41 KB
/
bcmxcp_ser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "main.h"
#include "bcmxcp.h"
#include "bcmxcp_io.h"
#include "serial.h"
#define SUBDRIVER_NAME "RS-232 communication subdriver"
#define SUBDRIVER_VERSION "0.20"
/* communication driver description structure */
upsdrv_info_t comm_upsdrv_info = {
SUBDRIVER_NAME,
SUBDRIVER_VERSION,
NULL,
0,
{ NULL }
};
#define PW_MAX_BAUD 5
struct pw_baud_rate {
int rate;
int name;
} pw_baud_rates[] = {
{ B19200, 19200 },
{ B9600, 9600 },
{ B4800, 4800 },
{ B2400, 2400 },
{ B1200, 1200 },
/* end of structure. */
{ 0, 0 }
};
unsigned char AUT[4] = {0xCF, 0x69, 0xE8, 0xD5}; /* Autorisation command */
static void send_command(unsigned char *command, int command_length)
{
int retry = 0, sent;
unsigned char sbuf[128];
/* Prepare the send buffer */
sbuf[0] = PW_COMMAND_START_BYTE;
sbuf[1] = (unsigned char)(command_length);
memcpy(sbuf+2, command, command_length);
command_length += 2;
/* Add checksum */
sbuf[command_length] = calc_checksum(sbuf);
command_length += 1;
upsdebug_hex (3, "send_command", sbuf, command_length);
while (retry++ < PW_MAX_TRY) {
if (retry == PW_MAX_TRY) {
ser_send_char(upsfd, 0x1d); /* last retry is preceded by a ESC.*/
usleep(250000);
}
sent = ser_send_buf(upsfd, sbuf, command_length);
if (sent == command_length) {
return;
}
}
}
void send_read_command(unsigned char command)
{
send_command(&command, 1);
}
void send_write_command(unsigned char *command, int command_length)
{
send_command(command, command_length);
}
/* get the answer of a command from the ups. And check that the answer is for this command */
int get_answer(unsigned char *data, unsigned char command)
{
unsigned char my_buf[128]; /* packet has a maximum length of 121+5 bytes */
int length, end_length = 0, res, endblock = 0, start = 0;
unsigned char block_number, sequence, pre_sequence = 0;
while (endblock != 1){
do {
/* Read PW_COMMAND_START_BYTE byte */
res = ser_get_char(upsfd, my_buf, 1, 0);
if (res != 1) {
upsdebugx(1,"Receive error (PW_COMMAND_START_BYTE): %d, cmd=%x!!!\n", res, command);
return -1;
}
start++;
} while ((my_buf[0] != PW_COMMAND_START_BYTE) && (start < 128));
if (start == 128) {
ser_comm_fail("Receive error (PW_COMMAND_START_BYTE): packet not on start!!%x\n", my_buf[0]);
return -1;
}
/* Read block number byte */
res = ser_get_char(upsfd, my_buf+1, 1, 0);
if (res != 1) {
ser_comm_fail("Receive error (Block number): %d!!!\n", res);
return -1;
}
block_number = (unsigned char)my_buf[1];
if (command <= 0x43) {
if ((command - 0x30) != block_number){
ser_comm_fail("Receive error (Request command): %x!!!\n", block_number);
return -1;
}
}
if (command >= 0x89) {
if ((command == 0xA0) && (block_number != 0x01)){
ser_comm_fail("Receive error (Requested only mode command): %x!!!\n", block_number);
return -1;
}
if ((command != 0xA0) && (block_number != 0x09)){
ser_comm_fail("Receive error (Control command): %x!!!\n", block_number);
return -1;
}
}
/* Read data length byte */
res = ser_get_char(upsfd, my_buf+2, 1, 0);
if (res != 1) {
ser_comm_fail("Receive error (length): %d!!!\n", res);
return -1;
}
length = (unsigned char)my_buf[2];
if (length < 1) {
ser_comm_fail("Receive error (length): packet length %x!!!\n", length);
return -1;
}
/* Read sequence byte */
res = ser_get_char(upsfd, my_buf+3, 1, 0);
if (res != 1) {
ser_comm_fail("Receive error (sequence): %d!!!\n", res);
return -1;
}
sequence = (unsigned char)my_buf[3];
if ((sequence & 0x80) == 0x80) {
endblock = 1;
}
if ((sequence & 0x07) != (pre_sequence + 1)) {
ser_comm_fail("Not the right sequence received %x!!!\n", sequence);
return -1;
}
pre_sequence = sequence;
/* Try to read all the remainig bytes */
res = ser_get_buf_len(upsfd, my_buf+4, length, 1, 0);
if (res != length) {
ser_comm_fail("Receive error (data): got %d bytes instead of %d!!!\n", res, length);
return -1;
}
/* Get the checksum byte */
res = ser_get_char(upsfd, my_buf+(4+length), 1, 0);
if (res != 1) {
ser_comm_fail("Receive error (checksum): %x!!!\n", res);
return -1;
}
/* now we have the whole answer from the ups, we can checksum it */
if (!checksum_test(my_buf)) {
ser_comm_fail("checksum error! ");
return -1;
}
memcpy(data+end_length, my_buf+4, length);
end_length += length;
}
upsdebug_hex (5, "get_answer", data, end_length);
ser_comm_good();
return end_length;
}
static int command_sequence(unsigned char *command, int command_length, unsigned char *answer)
{
int bytes_read, retry = 0;
while (retry++ < PW_MAX_TRY) {
if (retry == PW_MAX_TRY) {
ser_flush_in(upsfd, "", 0);
}
send_write_command(command, command_length);
bytes_read = get_answer(answer, *command);
if (bytes_read > 0) {
return bytes_read;
}
}
return -1;
}
/* Sends a single command (length=1). and get the answer */
int command_read_sequence(unsigned char command, unsigned char *answer)
{
int bytes_read;
bytes_read = command_sequence(&command, 1, answer);
if (bytes_read < 1) {
ser_comm_fail("Error executing command");
}
return bytes_read;
}
/* Sends a setup command (length > 1) */
int command_write_sequence(unsigned char *command, int command_length, unsigned char *answer)
{
int bytes_read;
bytes_read = command_sequence(command, command_length, answer);
if (bytes_read < 1) {
ser_comm_fail("Error executing command");
}
return bytes_read;
}
void upsdrv_comm_good()
{
ser_comm_good();
}
void pw_comm_setup(const char *port)
{
unsigned char command = PW_SET_REQ_ONLY_MODE;
unsigned char id_command = PW_ID_BLOCK_REQ;
unsigned char answer[256];
int i = 0, baud, mybaud = 0, ret = -1;
if (getval("baud_rate") != NULL)
{
baud = atoi(getval("baud_rate"));
for(i = 0; i < PW_MAX_BAUD; i++) {
if (baud == pw_baud_rates[i].name) {
mybaud = pw_baud_rates[i].rate;
break;
}
}
if (mybaud == 0) {
fatalx(EXIT_FAILURE, "Specified baudrate \"%s\" is invalid!", getval("baud_rate"));
}
ser_set_speed(upsfd, device_path, mybaud);
ser_send_char(upsfd, 0x1d); /* send ESC to take it out of menu */
usleep(90000);
send_write_command(AUT, 4);
usleep(500000);
ret = command_sequence(&command, 1, answer);
if (ret <= 0) {
usleep(500000);
ret = command_sequence(&id_command, 1, answer);
}
if (ret > 0) {
upslogx(LOG_INFO, "Connected to UPS on %s with baudrate %d", port, baud);
return;
}
upslogx(LOG_ERR, "No response from UPS on %s with baudrate %d", port, baud);
}
upslogx(LOG_INFO, "Attempting to autodect baudrate");
for (i=0; i<PW_MAX_BAUD; i++) {
ser_set_speed(upsfd, device_path, pw_baud_rates[i].rate);
ser_send_char(upsfd, 0x1d); /* send ESC to take it out of menu */
usleep(90000);
send_write_command(AUT, 4);
usleep(500000);
ret = command_sequence(&command, 1, answer);
if (ret <= 0) {
usleep(500000);
ret = command_sequence(&id_command, 1, answer);
}
if (ret > 0) {
upslogx(LOG_INFO, "Connected to UPS on %s with baudrate %d", port, pw_baud_rates[i].name);
return;
}
upsdebugx(2, "No response from UPS on %s with baudrate %d", port, pw_baud_rates[i].name);
}
fatalx(EXIT_FAILURE, "Can't connect to the UPS on port %s!\n", port);
}
int upsdrv_initups(void)
{
upsfd = ser_open(device_path);
pw_comm_setup(device_path);
return 1;
}
void upsdrv_cleanup(void)
{
/* free(dynamic_mem); */
ser_close(upsfd, device_path);
}
void upsdrv_reconnect(void)
{
}