diff options
-rw-r--r-- | ebus-racket/parse.rkt | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/ebus-racket/parse.rkt b/ebus-racket/parse.rkt index 46d91fd..dcc749c 100644 --- a/ebus-racket/parse.rkt +++ b/ebus-racket/parse.rkt @@ -31,12 +31,12 @@ primaryCommand secondaryCommand payloadLength)) ))) -;; 1 payload data byte -(define ebus-payload (choice (seq ebus-const-escape - escape-code <- (byte-in (list #"\0" #"\1")) +;; single, maybe escaped, payload data byte +(define ebus-payload (choice (seq escape-seq <- (bytes= ebus-const-escape) + escape-code <- (byte-in (list 0 1)) (return (cond - ((= escape-code #"\0") ebus-const-escape) - ((= escape-code #"\0") ebus-const-syn)))) + ((= escape-code 0) ebus-const-escape) + ((= escape-code 1) ebus-const-syn)))) any-byte )) @@ -69,26 +69,36 @@ (define ebus-sync (tokens syncs <- (seq (repeat (string->bytes/latin-1 "\xaa"))) (return (length syncs)))) -(define input-port (open-input-file "../doc/dumps/dump_2011-12-17_23-04-00.bin")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(define (read-ebus) +(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)) - (cond ((not (false? syn)) - (display (format "drop ~s x SYN (~s) ~n" syn ebus-const-syn)))) - (display (format "Paket ~s~n" paket))) + paket) + ((eof-object? (peek-byte input-port)) + eof) (else (display (format "drop ~s ~n" (peek-byte input-port))) - (file-position input-port (+ 1 (file-position input-port))) - )) - (cond ((not (eof-object? (peek-byte input-port))) - (read-ebus))))) + ; skip one byte + (read-byte input-port) + ;(file-position input-port (+ 1 (file-position input-port))) + (read-ebus input-port))))) + +(define (main input-port) + (let ([paket (read-ebus input-port)]) + (display (format "Paket ~s~n" paket)) + (cond ((not (eof-object? paket)) (main input-port))))) + +;(define input-port (open-input-file "../doc/dumps/dump_2011-12-17_23-04-00.bin")) +(main (current-input-port)) + +;((make-reader ebus-payload #:sof? #f #:eof? #f) (open-input-bytes (bytes 169 1 3 96))) -(read-ebus) ;(define-values (cin cout) |