diff options
-rw-r--r-- | ebus-racket/parser-test.rkt | 27 | ||||
-rw-r--r-- | ebus-racket/parser.rkt | 39 |
2 files changed, 24 insertions, 42 deletions
diff --git a/ebus-racket/parser-test.rkt b/ebus-racket/parser-test.rkt index 672dff2..1b4bc71 100644 --- a/ebus-racket/parser-test.rkt +++ b/ebus-racket/parser-test.rkt @@ -11,7 +11,6 @@ (let ([paket (parser:read-ebus (open-input-bytes (bytes 170 ; SYN - 170 170 003 ; Source 241 ; Destination @@ -30,27 +29,13 @@ 000 ; ACK 170 ; SYN 170)))]) - (check-eq? (parser:ebus-header-source (parser:ebus-paket-header paket)) 003) - (check-eq? (parser:ebus-header-destination (parser:ebus-paket-header paket)) 241) - (check-eq? (parser:ebus-header-primaryCommand (parser:ebus-paket-header paket)) 008) - (check-eq? (parser:ebus-header-secondaryCommand (parser:ebus-paket-header paket)) 000) - (check-eq? (parser:ebus-header-payloadLength (parser:ebus-paket-header paket)) 008) - (check-eq? (parser:ebus-header-payloadLength (parser:ebus-paket-header paket)) + (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))) )))) - -;(check-equal? (my-+ 1 1) 2 "Simple addition") - - ;(check-equal? (my-* 1 2) 2 "Simple multiplication") -; -; (test-case -; "List has length 4 and all elements even" -; (let ([lst (list 2 4 6 9)]) -; (check = (length lst) 4) -; (for-each -; (lambda (elt) -; (check-pred even? elt)) -; lst))))) - (run-tests parser-test)
\ No newline at end of file diff --git a/ebus-racket/parser.rkt b/ebus-racket/parser.rkt index 51b9b55..773e86e 100644 --- a/ebus-racket/parser.rkt +++ b/ebus-racket/parser.rkt @@ -7,10 +7,6 @@ (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) @@ -19,18 +15,10 @@ (crc payloadSlaveLength payloadSlave crcSlave) #:transparent) -(struct ebus-paket (header payload 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)) ))) +(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)) @@ -60,11 +48,21 @@ (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) +(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 header payload body))))) + (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)))) @@ -97,7 +95,6 @@ (provide read-ebus read-ebus-loop (struct-out ebus-paket) - (struct-out ebus-header) (struct-out ebus-body-broadcast) (struct-out ebus-body-mastermaster) (struct-out ebus-body-masterslave))
\ No newline at end of file |