From b71ecd26a9a0822a890f9aa494c6821356ef6bea Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sun, 26 Feb 2012 02:15:43 +0100 Subject: ebus-racket: cli interface --- ebus-racket/.gitignore | 1 + ebus-racket/dumper.rkt | 27 ++++++++++++ ebus-racket/parse.rkt | 110 ------------------------------------------------- ebus-racket/parser.rkt | 98 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 110 deletions(-) create mode 100644 ebus-racket/.gitignore create mode 100755 ebus-racket/dumper.rkt delete mode 100644 ebus-racket/parse.rkt create mode 100644 ebus-racket/parser.rkt diff --git a/ebus-racket/.gitignore b/ebus-racket/.gitignore new file mode 100644 index 0000000..724bbe1 --- /dev/null +++ b/ebus-racket/.gitignore @@ -0,0 +1 @@ +compiled/ diff --git a/ebus-racket/dumper.rkt b/ebus-racket/dumper.rkt new file mode 100755 index 0000000..7ac1da6 --- /dev/null +++ b/ebus-racket/dumper.rkt @@ -0,0 +1,27 @@ +#! /usr/bin/env racket +#lang racket + +(require "parser.ss") + +;(define verbose? (make-parameter #f)) +(define connect-host? (make-parameter null)) +(define connect-port? (make-parameter null)) + +(define greeting + (command-line + #:once-each +; [("-v") "Verbose mode" (verbose? #t)] + [("-c" "--connect") host port + "Connect to server " + (connect-host? host) + (connect-port? (string->number port)) + ])) +; Connect +(if (or (null? (connect-host?)) (null? (connect-port?))) + (display "Using stdin") + (let-values ([(cin cout) (tcp-connect (connect-host?) (connect-port?))]) + (display (format "Connected to ~s ~s ~n" (connect-host?) (connect-port?))) + (current-input-port cin))) + +; +(read-ebus-loop (current-input-port)) diff --git a/ebus-racket/parse.rkt b/ebus-racket/parse.rkt deleted file mode 100644 index dcc749c..0000000 --- a/ebus-racket/parse.rkt +++ /dev/null @@ -1,110 +0,0 @@ -#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")) -(define ebus-const-broadcastaddr 254) - -(struct ebus-header - (source destination primaryCommand secondaryCommand payloadLength) - #:transparent) - -(struct ebus-body-broadcast (crc) #:transparent) - -(struct ebus-body-mastermaster (crc) #:transparent) - -(struct ebus-body-masterslave - (crc payloadSlaveLength payloadSlave crcSlave) - #:transparent) - -(struct ebus-paket (header body) #:transparent) - -(define parse-ebus-header (token (seq source <- any-byte - destination <- any-byte - primaryCommand <- any-byte - secondaryCommand <- any-byte - payloadLength <- any-byte - (return (ebus-header source - destination - primaryCommand - secondaryCommand - payloadLength)) ))) -;; 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 1) ebus-const-syn)))) - any-byte - )) - -(define parse-ebus-broadcast (token (seq crc <- any-byte - syn <- (bytes= 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 - (return (ebus-body-mastermaster crc))))) - -(define parse-ebus-masterslave (token (seq crc <- any-byte - ack <- (bytes= 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 - (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 header <- parse-ebus-header - payload <- (repeat ebus-payload (ebus-header-payloadLength header) (ebus-header-payloadLength header)) - body <- (cond ((= (ebus-header-destination header) ebus-const-broadcastaddr) parse-ebus-broadcast) - (else parse-ebus-master-or-slave)) - (return (ebus-paket header body))))) - -(define ebus-sync (tokens syncs <- (seq (repeat (string->bytes/latin-1 "\xaa"))) - (return (length syncs)))) - - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(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 - (display (format "drop ~s ~n" (peek-byte input-port))) - ; 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))) - - - -;(define-values (cin cout) -; (tcp-connect "localhost" 2222)) - - -; -;(define (work-on port) -; (pretty-print token) diff --git a/ebus-racket/parser.rkt b/ebus-racket/parser.rkt new file mode 100644 index 0000000..1b9ed5b --- /dev/null +++ b/ebus-racket/parser.rkt @@ -0,0 +1,98 @@ +#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")) +(define ebus-const-broadcastaddr 254) + +(struct ebus-header + (source destination primaryCommand secondaryCommand payloadLength) + #:transparent) + +(struct ebus-body-broadcast (crc) #:transparent) + +(struct ebus-body-mastermaster (crc) #:transparent) + +(struct ebus-body-masterslave + (crc payloadSlaveLength payloadSlave crcSlave) + #:transparent) + +(struct ebus-paket (header body) #:transparent) + +(define parse-ebus-header (token (seq source <- any-byte + destination <- any-byte + primaryCommand <- any-byte + secondaryCommand <- any-byte + payloadLength <- any-byte + (return (ebus-header source + destination + primaryCommand + secondaryCommand + payloadLength)) ))) +;; 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 1) ebus-const-syn)))) + any-byte + )) + +(define parse-ebus-broadcast (token (seq crc <- any-byte + syn <- (bytes= 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 + (return (ebus-body-mastermaster crc))))) + +(define parse-ebus-masterslave (token (seq crc <- any-byte + ack <- (bytes= 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 + (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 header <- parse-ebus-header + payload <- (repeat ebus-payload (ebus-header-payloadLength header) (ebus-header-payloadLength header)) + body <- (cond ((= (ebus-header-destination header) ebus-const-broadcastaddr) parse-ebus-broadcast) + (else parse-ebus-master-or-slave)) + (return (ebus-paket header body))))) + +(define ebus-sync (tokens syncs <- (seq (repeat (string->bytes/latin-1 "\xaa"))) + (return (length syncs)))) + + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(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 + (display (format "drop ~s ~n" (peek-byte input-port))) + ; 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 read-ebus + read-ebus-loop) \ No newline at end of file -- cgit v1.2.1