From 494ed088e84b92af85d2f7260d242b06615fab69 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Tue, 28 Feb 2012 22:38:10 +0100 Subject: ebus-racket: layer7: parse bit,data1b,data2b --- ebus-racket/layer7-test.rkt | 46 ++++++++++++++++++++++++++++++++++ ebus-racket/layer7.rkt | 60 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 ebus-racket/layer7-test.rkt diff --git a/ebus-racket/layer7-test.rkt b/ebus-racket/layer7-test.rkt new file mode 100644 index 0000000..9654f06 --- /dev/null +++ b/ebus-racket/layer7-test.rkt @@ -0,0 +1,46 @@ +#lang racket +(require rackunit + rackunit/text-ui + "layer2.rkt" + "layer7.rkt") + +(define layer7-test + (test-suite + "Tests for Ebus Layer 7 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 + 200 ; p5 + 002 ; p6 + 000 ; p7 + 010 ; p8 + 128 ; CRC + 000 ; ACK + 170 ; SYN + 170)))] + [fields (layer7-paket-fields paket)]) + (check-true (= 5 (length fields)) "Anzahl der gelesenen Felder") + (for ([field fields]) + (display field) + (display "\n") + (cond ((= (list-ref field 2) 0) (check-equal? (list-ref field 3) 40.5 "Wert TK_soll")) + ((= (list-ref field 2) 2) (check-equal? (list-ref field 3) 2.8984375 "Wert TA_ist")) + ((= (list-ref field 2) 4) (check-equal? (list-ref field 3) 200 "Wert L_zwang")) + ((= (list-ref field 2) 5) (check-equal? (list-ref field 3) 0 "Wert Status")) + ((= (list-ref field 2) 6) (check-equal? (list-ref field 3) 10 "Wert TB_soll")))))) ;end test-case + )) + + +(run-tests layer7-test) \ No newline at end of file diff --git a/ebus-racket/layer7.rkt b/ebus-racket/layer7.rkt index 5b8f496..8f8f39e 100644 --- a/ebus-racket/layer7.rkt +++ b/ebus-racket/layer7.rkt @@ -1,16 +1,19 @@ #lang racket -(require (planet lizorkin/sxml:2:1/sxml)) -(require (planet lizorkin/ssax:2:0/ssax)) +(require (planet lizorkin/sxml:2:1/sxml) + (planet lizorkin/ssax:2:0/ssax) + "layer2.rkt") (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)) +(define (paket ebus-paket) + (let ([primaryCommand (layer2-ebus-paket-primaryCommand ebus-paket)] + [secondaryCommand (layer2-ebus-paket-secondaryCommand ebus-paket)]) + ((sxpath (string-append "//packet[@primary=" (number->string primaryCommand) + " and @secondary=" (number->string secondaryCommand) "]")) + definition))) ;; returns the full device-definition (define (device address) @@ -21,6 +24,49 @@ (define (device-name address) ((sxpath "@name/text()") (device address))) -(provide (prefix-out layer7- ebus-paket) + +(define (paket-fields ebus-paket) + (let* ([paket-definition (paket ebus-paket)] + [paket-name (first ((sxpath "@name/text()") paket-definition))] + [fields ((sxpath "fields/*") paket-definition)] + [values (for/list ([field fields]) + (let ([datatype ((sxpath "name()") field)] + [name (string-append paket-name "." (first ((sxpath "@name/text()") field)))] + [offset (string->number (first ((sxpath "@offset/text()") field)))]) + (cond ((string=? "bit" datatype) + (list name datatype offset + (field-decoder-bit (list-ref (layer2-ebus-paket-payload ebus-paket) offset)))) + ((string=? "data1b" datatype) + (list name datatype offset + (field-decoder-data1b (list-ref (layer2-ebus-paket-payload ebus-paket) offset)))) + ((string=? "data2b" datatype) + (list name datatype offset + (field-decoder-data2b (list-ref (layer2-ebus-paket-payload ebus-paket) offset) + (list-ref (layer2-ebus-paket-payload ebus-paket) (+ offset 1))))) + (else (display (string-append "unknown datatype: " datatype)) + (void))) + ))]) + ; filter invalid values + (for/list ([value values] #:when (not (void? value))) + value))) + +;; type bit +(define (field-decoder-bit value) + (cond ((= value 1) 1) + (else 0))) + +;; type data1b +(define (field-decoder-data1b value) + value) + +;; type data2b +(define (field-decoder-data2b lowByte highByte) + (cond ((= (bitwise-and highByte 128) 128) + (* -1 (+ (+ 256 (bitwise-not highByte)) + (/ (+ 256 (bitwise-not (+ lowByte 1))) 256.0)))) + (else (+ highByte (/ lowByte 256.0))))) + +(provide (prefix-out layer7- paket) + (prefix-out layer7- paket-fields) (prefix-out layer7- device) (prefix-out layer7- device-name)) \ No newline at end of file -- cgit v1.2.1