From 36f42916cadd07bba5d0812566fba4546641cde9 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sun, 26 Feb 2012 19:49:39 +0100 Subject: ebus-racked: rename to layer2-/layer7- --- ebus-racket/dumper.rkt | 4 +- ebus-racket/layer2-test.rkt | 41 ++++++++++++++++++ ebus-racket/layer2.rkt | 100 ++++++++++++++++++++++++++++++++++++++++++++ ebus-racket/layer7.rkt | 26 ++++++++++++ ebus-racket/parser-test.rkt | 41 ------------------ ebus-racket/parser.rkt | 100 -------------------------------------------- 6 files changed, 169 insertions(+), 143 deletions(-) create mode 100644 ebus-racket/layer2-test.rkt create mode 100644 ebus-racket/layer2.rkt create mode 100644 ebus-racket/layer7.rkt delete mode 100644 ebus-racket/parser-test.rkt delete mode 100644 ebus-racket/parser.rkt diff --git a/ebus-racket/dumper.rkt b/ebus-racket/dumper.rkt index adbc6fe..3c66015 100755 --- a/ebus-racket/dumper.rkt +++ b/ebus-racket/dumper.rkt @@ -1,7 +1,7 @@ #! /usr/bin/env racket #lang racket -(require (prefix-in parser: "parser.rkt")) +(require "layer2.rkt") ;(define verbose? (make-parameter #f)) (define connect-host? (make-parameter null)) @@ -24,4 +24,4 @@ (current-input-port cin))) ; -(parser:read-ebus-loop (current-input-port)) +(layer2-read-ebus-loop (current-input-port)) diff --git a/ebus-racket/layer2-test.rkt b/ebus-racket/layer2-test.rkt new file mode 100644 index 0000000..ed20ca6 --- /dev/null +++ b/ebus-racket/layer2-test.rkt @@ -0,0 +1,41 @@ +#lang racket +(require rackunit + rackunit/text-ui + "layer2.rkt") + +(define layer2-test + (test-suite + "Tests for Ebus Parser" + (test-case + "Test sample Master-Master Paket" + (let + ([paket (layer2-read-ebus (open-input-bytes (bytes + 170 ; SYN + 170 + 003 ; Source + 241 ; Destination + 008 ; primaryCommand + 000 ; secondaryCommand + 008 ; payloadLength + 128 ; p1 + 040 ; p2 + 230 ; p3 + 002 ; p4 + 000 ; p5 + 002 ; p6 + 000 ; p7 + 010 ; p8 + 128 ; CRC + 000 ; ACK + 170 ; SYN + 170)))]) + (check-eq? (layer2-ebus-paket-source paket) 003) + (check-eq? (layer2-ebus-paket-destination paket) 241) + (check-eq? (layer2-ebus-paket-primaryCommand paket) 008) + (check-eq? (layer2-ebus-paket-secondaryCommand paket) 000) + (check-eq? (layer2-ebus-paket-payloadLength paket) 008) + (check-eq? (layer2-ebus-paket-payloadLength paket) + (length (layer2-ebus-paket-payload paket))) + )))) + +(run-tests layer2-test) diff --git a/ebus-racket/layer2.rkt b/ebus-racket/layer2.rkt new file mode 100644 index 0000000..dbdd411 --- /dev/null +++ b/ebus-racket/layer2.rkt @@ -0,0 +1,100 @@ +#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-body-broadcast (crc) #:transparent) + +(struct ebus-body-mastermaster (crc) #:transparent) + +(struct ebus-body-masterslave + (crc payloadSlaveLength payloadSlave crcSlave) + #:transparent) + +(struct ebus-paket + (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) + 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 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)))) + + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(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 (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)) + (prefix-out layre2- (struct-out ebus-body-masterslave))) diff --git a/ebus-racket/layer7.rkt b/ebus-racket/layer7.rkt new file mode 100644 index 0000000..5b8f496 --- /dev/null +++ b/ebus-racket/layer7.rkt @@ -0,0 +1,26 @@ +#lang racket +(require (planet lizorkin/sxml:2:1/sxml)) +(require (planet lizorkin/ssax:2:0/ssax)) + +(define definition + (ssax:xml->sxml (open-input-file "../ebus-xml/ebus.xml") + '[(#f . "http://xapek.org/ebus/0.1")])) + + +(define (ebus-paket primaryCommand secondaryCommand) + ((sxpath (string-append "//packet[@primary=" (number->string primaryCommand) + " and @secondary=" (number->string secondaryCommand) "]")) + definition)) + +;; returns the full device-definition +(define (device address) + ((sxpath (string-append "//devices/device[@address=" (number->string address) "]")) + definition)) + +;; returns device-name in a list or empty-list +(define (device-name address) + ((sxpath "@name/text()") (device address))) + +(provide (prefix-out layer7- ebus-paket) + (prefix-out layer7- device) + (prefix-out layer7- device-name)) \ No newline at end of file diff --git a/ebus-racket/parser-test.rkt b/ebus-racket/parser-test.rkt deleted file mode 100644 index 1b4bc71..0000000 --- a/ebus-racket/parser-test.rkt +++ /dev/null @@ -1,41 +0,0 @@ -#lang racket -(require rackunit - rackunit/text-ui - (prefix-in parser: "parser.rkt")) - -(define parser-test - (test-suite - "Tests for Ebus Parser" - (test-case - "Test sample Master-Master Paket" - (let - ([paket (parser:read-ebus (open-input-bytes (bytes - 170 ; SYN - 170 - 003 ; Source - 241 ; Destination - 008 ; primaryCommand - 000 ; secondaryCommand - 008 ; payloadLength - 128 ; p1 - 040 ; p2 - 230 ; p3 - 002 ; p4 - 000 ; p5 - 002 ; p6 - 000 ; p7 - 010 ; p8 - 128 ; CRC - 000 ; ACK - 170 ; SYN - 170)))]) - (check-eq? (parser:ebus-paket-source paket) 003) - (check-eq? (parser:ebus-paket-destination paket) 241) - (check-eq? (parser:ebus-paket-primaryCommand paket) 008) - (check-eq? (parser:ebus-paket-secondaryCommand paket) 000) - (check-eq? (parser:ebus-paket-payloadLength paket) 008) - (check-eq? (parser:ebus-paket-payloadLength paket) - (length (parser:ebus-paket-payload paket))) - )))) - -(run-tests parser-test) \ No newline at end of file diff --git a/ebus-racket/parser.rkt b/ebus-racket/parser.rkt deleted file mode 100644 index 773e86e..0000000 --- a/ebus-racket/parser.rkt +++ /dev/null @@ -1,100 +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-body-broadcast (crc) #:transparent) - -(struct ebus-body-mastermaster (crc) #:transparent) - -(struct ebus-body-masterslave - (crc payloadSlaveLength payloadSlave crcSlave) - #:transparent) - -(struct ebus-paket - (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) - 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 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)))) - - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(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 - (struct-out ebus-paket) - (struct-out ebus-body-broadcast) - (struct-out ebus-body-mastermaster) - (struct-out ebus-body-masterslave)) \ No newline at end of file -- cgit v1.2.1