diff options
Diffstat (limited to 'ebus-racket/layer2.rkt')
-rw-r--r-- | ebus-racket/layer2.rkt | 127 |
1 files changed, 67 insertions, 60 deletions
diff --git a/ebus-racket/layer2.rkt b/ebus-racket/layer2.rkt index 42d50b3..e0f9fbd 100644 --- a/ebus-racket/layer2.rkt +++ b/ebus-racket/layer2.rkt @@ -24,49 +24,54 @@ #:transparent) ;; single, maybe escaped, payload data byte -(define ebus-payload (choice (seq escape-seq <- ebus-const-escape - escape-code <- (byte-in (list 0 1)) - (return (cond - ((= escape-code 0) ebus-const-escape) - ((= escape-code 1) bytes ebus-const-syn)))) - any-byte - )) - -(define parse-ebus-broadcast (token (seq crc <- any-byte - syn <- ebus-const-syn - (return (ebus-body-broadcast crc))))) - -(define parse-ebus-mastermaster (token (seq crc <- any-byte - ack <- ebus-const-ackok ;; ACK des Empfängers - syn <- ebus-const-syn ;; SYN des Senders - (return (ebus-body-mastermaster crc))))) - -(define parse-ebus-masterslave (token (seq crc <- any-byte - ack <- ebus-const-ackok ;; ACK des Empfängers - payloadSlaveLength <- any-byte - payloadSlave <- (repeat ebus-payload payloadSlaveLength payloadSlaveLength) - crcSlave <- any-byte - ackSlave <- ebus-const-ackok ;; ACK des Senders - synSlave <- ebus-const-syn ;; SYN des Senders - (return (ebus-body-masterslave crc payloadSlaveLength payloadSlave crcSlave))))) +(define ebus-payload + (choice (seq escape-seq <- ebus-const-escape + escape-code <- (byte-in (list 0 1)) + (return (cond + ((= escape-code 0) ebus-const-escape) + ((= escape-code 1) bytes ebus-const-syn)))) + any-byte + )) + +(define parse-ebus-broadcast + (token (seq crc <- any-byte + syn <- ebus-const-syn + (return (ebus-body-broadcast crc))))) + +(define parse-ebus-mastermaster + (token (seq crc <- any-byte + ack <- ebus-const-ackok ;; ACK des Empfängers + syn <- ebus-const-syn ;; SYN des Senders + (return (ebus-body-mastermaster crc))))) + +(define parse-ebus-masterslave + (token (seq crc <- any-byte + ack <- ebus-const-ackok ;; ACK des Empfängers + payloadSlaveLength <- any-byte + payloadSlave <- (repeat ebus-payload payloadSlaveLength payloadSlaveLength) + crcSlave <- any-byte + ackSlave <- ebus-const-ackok ;; ACK des Senders + synSlave <- ebus-const-syn ;; SYN des Senders + (return (ebus-body-masterslave crc payloadSlaveLength payloadSlave crcSlave))))) (define parse-ebus-master-or-slave (token (choice parse-ebus-mastermaster parse-ebus-masterslave))) -(define parse-ebus-paket (token (seq source <- any-byte - destination <- any-byte - primaryCommand <- any-byte - secondaryCommand <- any-byte - payloadLength <- any-byte - payload <- (repeat ebus-payload payloadLength payloadLength) - body <- (cond ((= destination ebus-const-broadcastaddr) parse-ebus-broadcast) - (else parse-ebus-master-or-slave)) - (return (ebus-paket source - destination - primaryCommand - secondaryCommand - payloadLength - payload - body))))) +(define parse-ebus-paket + (token (seq source <- any-byte + destination <- any-byte + primaryCommand <- any-byte + secondaryCommand <- any-byte + payloadLength <- any-byte + payload <- (repeat ebus-payload payloadLength payloadLength) + body <- (cond ((= destination ebus-const-broadcastaddr) parse-ebus-broadcast) + (else parse-ebus-master-or-slave)) + (return (ebus-paket source + destination + primaryCommand + secondaryCommand + payloadLength + payload + body))))) (define ebus-sync (tokens syncs <- (seq (repeat (string->bytes/latin-1 "\xaa"))) (return (length syncs)))) @@ -75,23 +80,25 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (read-ebus input-port) - (let ([syn ((make-reader ebus-sync #:sof? #f #:eof? #f) input-port)] - [paket ((make-reader parse-ebus-paket #:sof? #f #:eof? #f) input-port)]) - (cond ((not (false? syn)) - (display (format "drop ~s x SYN (~s) ~n" syn ebus-const-syn)))) - (cond ((not (false? paket)) - paket) - ((eof-object? (peek-byte input-port)) - eof) - (else - (let ([byte (read-byte input-port)]) - (display (format "drop ~s 0x~x ~n" byte byte))) - ;; skip one byte - (read-byte input-port) - (read-ebus input-port))))) - -(provide (prefix-out layer2- read-ebus) - (prefix-out layer2- (struct-out ebus-paket)) - (prefix-out layer2- (struct-out ebus-body-broadcast)) - (prefix-out layer2- (struct-out ebus-body-mastermaster)) - (prefix-out layre2- (struct-out ebus-body-masterslave))) + (define syn ((make-reader ebus-sync #:sof? #f #:eof? #f) input-port)) + (define paket ((make-reader parse-ebus-paket #:sof? #f #:eof? #f) input-port)) + (cond ((not (false? syn)) + (display (format "drop ~s x SYN (~s) ~n" syn ebus-const-syn)))) + (cond ((not (false? paket)) + paket) + ((eof-object? (peek-byte input-port)) + eof) + (else + (let ([byte (read-byte input-port)]) + (display (format "drop ~s 0x~x ~n" byte byte))) + ;; skip one byte + (read-byte input-port) + (read-ebus input-port)))) + +(provide + ;; Read Layer Ebus-Paket `ebus-paket` + (prefix-out layer2- read-ebus) + (prefix-out layer2- (struct-out ebus-paket)) + (prefix-out layer2- (struct-out ebus-body-broadcast)) + (prefix-out layer2- (struct-out ebus-body-mastermaster)) + (prefix-out layer2- (struct-out ebus-body-masterslave))) |