forked from bootc/PolyController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowfsd.c
498 lines (414 loc) · 10.5 KB
/
owfsd.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* This file is part of the PolyController firmware source code.
* Copyright (C) 2011 Chris Boot.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <string.h>
#include <contiki-net.h>
#include <init.h>
#include <util/delay.h>
#include <onewire.h>
#if CONFIG_APPS_SYSLOG
#include "syslog.h"
#endif
#include "drivers/ds2482.h"
#define OWFSD_PORT 15862
#define CMD_RESET 'R'
#define CMD_BYTES 'B'
#define CMD_BITS 'b'
#define CMD_SEARCH 'A'
#define CMD_BYTE_SPU 'P'
#define CMD_RET_ERROR 'E' // only used to send back to client
#define ERR_OK 0
#define ERR_INVALID 1 // invalid command or request size
#define ERR_BUFSZ 2 // buffer too long, short read, etc...
#define ERR_OWSD 3 // bus short detected
#define ERR_OWERR 4 // general bus fault
#define ERR_NOLOCK 5 // bus access without a lock
#ifndef CONFIG_APPS_OWFSD_MAX_CONNS
#define MAX_CONNS (UIP_CONNS / 2)
#else /* CONFIG_APPS_OWFSD_MAX_CONNS */
#define MAX_CONNS CONFIG_APPS_OWFSD_MAX_CONNS
#endif /* CONFIG_APPS_OWFSD_MAX_CONNS */
#ifndef CONFIG_APPS_OWFSD_BUFFER_SIZE
#define OW_BUFLEN 64
#else /* CONFIG_APPS_OWFSD_BUFFER_SIZE */
#define OW_BUFLEN CONFIG_APPS_OWFSD_BUFFER_SIZE
#endif /* CONFIG_APPS_OWFSD_BUFFER_SIZE */
#define LOCK_TIMER_INTERVAL (3 * CLOCK_SECOND)
struct owfsd_state;
struct owfs_command {
uint8_t cmd;
int (* fn)(struct owfsd_state *s);
struct {
uint8_t bus_op : 1; // Bus operation; requires lock
uint8_t lock_auto : 1; // Causes bus reset; auto-acquires lock
} flags;
};
struct owfs_packet {
uint8_t len;
uint8_t cmd;
union {
uint8_t bytes[OW_BUFLEN];
struct {
ow_addr_t addr;
uint8_t flags;
} search;
struct {
uint8_t delay;
uint8_t byte;
} spu;
uint8_t error;
} buf;
};
struct owfsd_state {
struct psock sock;
struct pt pt;
uint8_t buf_in[OW_BUFLEN];
uint8_t status;
struct owfs_packet pkt;
struct owfs_command cmd;
struct timer lock_timer;
struct {
uint8_t locked : 1;
} flags;
};
static int cmd_reset(struct owfsd_state *s);
static int cmd_byte(struct owfsd_state *s);
static int cmd_bit(struct owfsd_state *s);
static int cmd_search(struct owfsd_state *s);
static int cmd_byte_spu(struct owfsd_state *s);
static const struct owfs_command commands[] PROGMEM = {
{ CMD_RESET, cmd_reset, { .bus_op = 1, .lock_auto = 1, } },
{ CMD_BYTES, cmd_byte, { .bus_op = 1, } },
{ CMD_BITS, cmd_bit, { .bus_op = 1, } },
{ CMD_SEARCH, cmd_search, { .bus_op = 1, .lock_auto = 1, } },
{ CMD_BYTE_SPU, cmd_byte_spu, { .bus_op = 1, } },
{} // end-of-table marker
};
static uint8_t conns_free = MAX_CONNS;
PROCESS(owfsd_process, "owfsd");
INIT_PROCESS(owfsd_process);
static PT_THREAD(read_bytes(struct owfsd_state *s, uint8_t len)) {
PSOCK_BEGIN(&s->sock);
// Check buffer length
if (len > OW_BUFLEN) {
s->status = ERR_BUFSZ;
PSOCK_EXIT(&s->sock);
}
// Read exactly len bytes
s->sock.bufsize = len;
PSOCK_READBUF(&s->sock);
// Check length
if (PSOCK_DATALEN(&s->sock) != len) {
s->status = ERR_BUFSZ;
PSOCK_EXIT(&s->sock);
}
// Read successful
s->status = ERR_OK;
PSOCK_END(&s->sock);
}
static PT_THREAD(send_response(struct owfsd_state *s)) {
PSOCK_BEGIN(&s->sock);
// Send the response
s->sock.bufsize = OW_BUFLEN;
PSOCK_SEND(&s->sock, (uint8_t *)&s->pkt, s->pkt.len + 2);
PSOCK_END(&s->sock);
}
static PT_THREAD(send_error(struct owfsd_state *s)) {
PSOCK_BEGIN(&s->sock);
// Clobber response size & length
s->pkt.len = sizeof(s->pkt.buf.error);
s->pkt.cmd = CMD_RET_ERROR;
// Copy over error code
s->pkt.buf.error = s->status;
// Send the error response
s->sock.bufsize = OW_BUFLEN;
PSOCK_SEND(&s->sock, (uint8_t *)&s->pkt, s->pkt.len + 2);
PSOCK_END(&s->sock);
}
static int cmd_reset(struct owfsd_state *s) {
if (s->pkt.len != 0) {
return ERR_INVALID;
}
// Reset the bus
int ret = ow_reset();
if (ret == -2) {
#if CONFIG_APPS_SYSLOG
// Log something
syslog_P(LOG_DAEMON | LOG_ERR,
PSTR("1-Wire bus short circuit detected"));
#endif
return ERR_OWSD;
}
else if (ret < 0) {
#if CONFIG_APPS_SYSLOG
// Log something
syslog_P(LOG_DAEMON | LOG_ERR,
PSTR("1-Wire bus reset failure"));
#endif
return ERR_OWERR;
}
return ERR_OK;
}
static int cmd_byte(struct owfsd_state *s) {
if (s->pkt.len == 0) {
return ERR_INVALID;
}
// Read/write bytes
int ret = ow_block(s->pkt.buf.bytes, s->pkt.len);
if (ret < 0) {
return ERR_OWERR;
}
return ERR_OK;
}
static int cmd_bit(struct owfsd_state *s) {
if (s->pkt.len == 0) {
return ERR_INVALID;
}
// Loop through the data buffer touching bits
for (int i = 0; i < s->pkt.len; i++) {
int ret = ow_touch_bit(s->pkt.buf.bytes[i]);
if (ret < 0) {
return ERR_OWERR;
}
s->pkt.buf.bytes[i] = ret ? 0xff : 0x00;
}
return ERR_OK;
}
static int cmd_search(struct owfsd_state *s) {
if (s->pkt.len != sizeof(s->pkt.buf.search)) {
return ERR_INVALID;
}
ow_search_t src;
// Set up the fields
memcpy(&src.rom_no, &s->pkt.buf.search.addr, sizeof(src.rom_no));
src.last_discrepancy = s->pkt.buf.search.flags & 0x7f;
src.last_family_discrepancy = 0;
src.last_device_flag = 0;
src.alarm = s->pkt.buf.search.flags & 0x80 ? 1 : 0;
int ret = ow_search_next(&src);
if (ret < 0) {
#if CONFIG_APPS_SYSLOG
// Log something
syslog_P(LOG_DAEMON | LOG_ERR,
PSTR("1-Wire bus search failed"));
#endif
return ERR_OWERR;
}
else if (ret == 0) {
memset(&s->pkt.buf.search.addr, 0, sizeof(ow_addr_t));
s->pkt.buf.search.flags = 0xff; // no devices on bus
return ERR_OK;
}
// Copy back the found 1-Wire address
memcpy(&s->pkt.buf.search.addr, &src.rom_no, sizeof(src.rom_no));
// Found last device?
if (src.last_device_flag) {
s->pkt.buf.search.flags = 0xfe;
}
else {
s->pkt.buf.search.flags = src.last_discrepancy;
}
return ERR_OK;
}
static int cmd_byte_spu(struct owfsd_state *s) {
if (s->pkt.len != sizeof(s->pkt.buf.spu)) {
return ERR_INVALID;
}
// Send power byte command
int ret = ow_write_byte_power(s->pkt.buf.spu.byte);
if (ret) {
return ERR_OWERR;
}
// Wait for delay * 10ms
while (s->pkt.buf.spu.delay--) {
// FIXME: find a better way of doing this!
_delay_ms(10);
}
ret = ow_level_std();
if (ret) {
return ERR_OWERR;
}
return ERR_OK;
}
static PT_THREAD(handle_connection(struct owfsd_state *s)) {
PT_BEGIN(&s->pt);
while (1) {
// Read length byte and command
PT_WAIT_THREAD(&s->pt, read_bytes(s, 2));
if (s->status) {
PT_WAIT_THREAD(&s->pt, send_error(s));
continue;
}
// Read in the length and command bytes
s->pkt.len = s->buf_in[0];
s->pkt.cmd = s->buf_in[1];
// Make sure the length isn't too long
if (s->pkt.len > OW_BUFLEN) {
// Weed to consume the sent bytes anyway
while (s->pkt.len) {
PT_WAIT_THREAD(&s->pt, read_bytes(s,
s->pkt.len < OW_BUFLEN ? s->pkt.len : OW_BUFLEN));
s->pkt.len -= s->pkt.len < OW_BUFLEN ? s->pkt.len : OW_BUFLEN;
}
// Send error
s->status = ERR_BUFSZ;
PT_WAIT_THREAD(&s->pt, send_error(s));
continue;
}
else if (s->pkt.len) {
// Read in the data packet
PT_WAIT_THREAD(&s->pt, read_bytes(s, s->pkt.len));
if (s->status) {
PT_WAIT_THREAD(&s->pt, send_error(s));
continue;
}
// Copy the packet contents
memcpy(s->pkt.buf.bytes, s->buf_in, s->pkt.len);
}
// Get the command info
const struct owfs_command *cmd = commands;
do {
uint8_t c = pgm_read_byte(&cmd->cmd);
if (!c) {
memset(&s->cmd, 0, sizeof(s->cmd));
break;
}
else if (c == s->pkt.cmd) {
memcpy_P(&s->cmd, cmd, sizeof(s->cmd));
break;
}
else {
cmd++;
}
} while (1);
// Sanity check command
if (!s->cmd.cmd) {
s->status = ERR_INVALID;
PT_WAIT_THREAD(&s->pt, send_error(s));
continue;
}
// Check if a lock is required
if (s->cmd.flags.bus_op) {
if (s->flags.locked) {
// Refresh lock timer
timer_restart(&s->lock_timer);
}
else if (s->cmd.flags.lock_auto) {
// Acquire a lock
while (!ow_lock()) {
uip_poll_conn(uip_conn);
PT_YIELD(&s->pt);
}
s->flags.locked = 1;
timer_set(&s->lock_timer, LOCK_TIMER_INTERVAL);
}
else {
s->status = ERR_NOLOCK;
PT_WAIT_THREAD(&s->pt, send_error(s));
continue;
}
}
// Run command
s->status = s->cmd.fn(s);
// Check command result
if (s->status) {
PT_WAIT_THREAD(&s->pt, send_error(s));
}
else {
// Send response
PT_WAIT_THREAD(&s->pt, send_response(s));
}
}
PT_END(&s->pt);
}
static void owfsd_appcall(void *state) {
struct owfsd_state *s = (struct owfsd_state *)state;
if (uip_closed() || uip_aborted() || uip_timedout()) {
if (s != NULL) {
// Make sure we release the lock
if (s->flags.locked) {
ow_unlock();
}
// Free state data
free(s);
tcp_markconn(uip_conn, NULL);
conns_free++;
}
}
else if (uip_connected()) {
// Allocate a connection if we can
if (conns_free) {
s = calloc(1, sizeof(*s));
conns_free--;
}
else {
s = NULL;
}
// Make sure we got some memory
if (s == NULL) {
// Reset the connection so the remote end knows something is up
uip_abort();
#if CONFIG_APPS_SYSLOG
// Log something
syslog_P(LOG_DAEMON | LOG_WARNING,
PSTR("%d.%d.%d.%d: too much going on, try later"),
uip_ipaddr_to_quad(&uip_conn->ripaddr));
#endif
return;
}
// Set up the connection
tcp_markconn(uip_conn, s);
PSOCK_INIT(&s->sock, s->buf_in, OW_BUFLEN);
PT_INIT(&s->pt);
#if CONFIG_APPS_SYSLOG
// Log something
syslog_P(LOG_DAEMON | LOG_INFO,
PSTR("%d.%d.%d.%d: Connected"),
uip_ipaddr_to_quad(&uip_conn->ripaddr));
#endif
// Handle the connection
handle_connection(s);
}
else if (s != NULL) {
handle_connection(s);
// Check for expired locks
if (s->flags.locked && timer_expired(&s->lock_timer)) {
ow_unlock();
s->flags.locked = 0;
}
}
else {
uip_abort();
}
}
PROCESS_THREAD(owfsd_process, ev, data) {
PROCESS_BEGIN();
tcp_listen(UIP_HTONS(OWFSD_PORT));
while (1) {
PROCESS_WAIT_EVENT();
if (ev == tcpip_event) {
owfsd_appcall(data);
}
else if (ev == PROCESS_EVENT_EXIT) {
PROCESS_EXIT();
}
}
PROCESS_END();
}