From 50b9a83e70e8dfbdcd2cd90b4a64e7071ad22a21 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Fri, 2 Mar 2012 00:26:12 +0100 Subject: ebus-racket: * dumper * use racket/base * use racket/pretty pretty-print * support layer7 parser (--layer7) * layer2 * define constants as #xHEX values instead of bytestrings * layer7 * support more datatypes * reformat with emacs * layer7 * support more datatypes * reformat with emacs --- ebus-racket/layer2.rkt | 57 ++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) (limited to 'ebus-racket/layer2.rkt') diff --git a/ebus-racket/layer2.rkt b/ebus-racket/layer2.rkt index dbdd411..42d50b3 100644 --- a/ebus-racket/layer2.rkt +++ b/ebus-racket/layer2.rkt @@ -1,10 +1,14 @@ -#lang racket -(require (planet bzlib/parseq:1:3)) - -; Ebus SYN Byte-String -(define ebus-const-syn (string->bytes/latin-1 "\xaa")) -(define ebus-const-escape (string->bytes/latin-1 "\xa9")) -(define ebus-const-ackok (string->bytes/latin-1 "\x00")) +#lang racket/base +(require racket/bool + (planet bzlib/parseq:1:3)) + +;; Ebus SYN +(define ebus-const-syn #xaa) +;; Ebus Escape-Sequence Start +(define ebus-const-escape #xa9) +;; Ebus ACK +(define ebus-const-ackok #x00) +;; Ebus Broadcast Address (define ebus-const-broadcastaddr 254) (struct ebus-body-broadcast (crc) #:transparent) @@ -12,38 +16,38 @@ (struct ebus-body-mastermaster (crc) #:transparent) (struct ebus-body-masterslave - (crc payloadSlaveLength payloadSlave crcSlave) - #:transparent) + (crc payloadSlaveLength payloadSlave crcSlave) + #:transparent) (struct ebus-paket - (source destination primaryCommand secondaryCommand payloadLength payload body) - #:transparent) + (source destination primaryCommand secondaryCommand payloadLength payload body) + #:transparent) ;; single, maybe escaped, payload data byte -(define ebus-payload (choice (seq escape-seq <- (bytes= ebus-const-escape) +(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) ebus-const-syn)))) + ((= 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 <- (bytes= ebus-const-syn) + syn <- ebus-const-syn (return (ebus-body-broadcast crc))))) (define parse-ebus-mastermaster (token (seq crc <- any-byte - ack <- (bytes= ebus-const-ackok) ; ACK des Empfängers - syn <- (bytes= ebus-const-syn) ; SYN des Senders + 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 <- (bytes= ebus-const-ackok) ;ACK des Empfängers + ack <- ebus-const-ackok ;; ACK des Empfängers payloadSlaveLength <- any-byte payloadSlave <- (repeat ebus-payload payloadSlaveLength payloadSlaveLength) crcSlave <- any-byte - ackSlave <- (bytes= ebus-const-ackok) ;ACK des Senders - synSlave <- (bytes= ebus-const-syn) ;SYN des Senders + 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))) @@ -68,7 +72,6 @@ (return (length syncs)))) - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (read-ebus input-port) @@ -81,19 +84,13 @@ ((eof-object? (peek-byte input-port)) eof) (else - (display (format "drop ~s ~n" (peek-byte input-port))) - ; skip one byte + (let ([byte (read-byte input-port)]) + (display (format "drop ~s 0x~x ~n" byte byte))) + ;; skip one byte (read-byte input-port) - ;(file-position input-port (+ 1 (file-position input-port))) (read-ebus input-port))))) -(define (read-ebus-loop input-port) - (let ([paket (read-ebus (current-input-port))]) - (display (format "Paket ~s~n" paket)) - (cond ((not (eof-object? paket)) (read-ebus-loop input-port))))) - (provide (prefix-out layer2- read-ebus) - (prefix-out layer2- read-ebus-loop) (prefix-out layer2- (struct-out ebus-paket)) (prefix-out layer2- (struct-out ebus-body-broadcast)) (prefix-out layer2- (struct-out ebus-body-mastermaster)) -- cgit v1.2.1