summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Network/EBus/Layer2.hs11
-rw-r--r--Test.hs46
-rw-r--r--Test/Layer2.hs57
-rw-r--r--exec/ebus-dump-layer2.hs35
4 files changed, 123 insertions, 26 deletions
diff --git a/Network/EBus/Layer2.hs b/Network/EBus/Layer2.hs
index 04d652c..d91bcc7 100644
--- a/Network/EBus/Layer2.hs
+++ b/Network/EBus/Layer2.hs
@@ -1,3 +1,14 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module : Network.EBus.Layer2
+-- Copyright : Yves Fischer 2012
+-- License : BSD3
+--
+-- Stability : experimental
+-- Portability : portable
+--
+-- This module contains a Attoparsec based Parser for eBus Layer 2
+
module Network.EBus.Layer2(
ebusParserLayer2,
EbusPacket,
diff --git a/Test.hs b/Test.hs
index c5393bc..ba2c4d6 100644
--- a/Test.hs
+++ b/Test.hs
@@ -1,6 +1,13 @@
module Main(main) where
+import Test.HUnit
+import Test.HUnit.Base
+
+import Data.Word (Word8)
+import Data.Bits (xor)
+
import Network.EBus.Layer2 as L2
+import Test.Layer2 as Test.Layer2
-- main = do
-- -- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.)
@@ -29,19 +36,40 @@ import Network.EBus.Layer2 as L2
-- p <- iterParser parser
-- return p
-import Test.Framework
-import Test.Framework.Providers.HUnit
+main = runTestTT $ TestList Test.Layer2.tests
+-- defaultMain [
+-- testGroup "SomeModule" [ testCase "foo" foooo]
+-- ]
+
+--tests = TestList [TestLabel "bla" foooo]
-main =
- defaultMain [
- testGroup "SomeModule" [ testCase "foo" foooo]
- ]
+tests = TestList [ ]
+
+
+--"Test:" ~: L2.ebusCrc [1 .. 7] ~=? 96
+-- , "Test:" ~: L2.ebusCrc [8,1,1,16,255,88,255,60,9] ~=? 247]
-foooo :: IO()
-foooo = do
- print "OK"
+-- listEnumerator :: Enumerator ByteString IO a
+-- listEnumerator = enumLists l
+-- where l::[[ByteString]]
+-- l = [[pack [1,2,3]]]
+
+
+--foooo = -- TestList(
+-- ~: ~=? "asd" 2 2
+-- assertBool "foo" True
+
+
+ -- print $ ebusCrc [1,2,3]
+ -- quickCheck (\s(\s -> length (take5 s) == 5)
+ -- if ebusCrc [1,2,3,4,5,6,7] == 96
+ -- then IO ()
+ -- else assertFailure "Failed"
+ -- print "\nOK"
+ -- )
+
-- import Control.Concurrent (forkOS)
-- import Control.Concurrent.MVar (MVar, newEmptyMVar, readMVar, putMVar)
-- import Control.Exception (finally)
diff --git a/Test/Layer2.hs b/Test/Layer2.hs
new file mode 100644
index 0000000..d50535f
--- /dev/null
+++ b/Test/Layer2.hs
@@ -0,0 +1,57 @@
+module Test.Layer2 (tests) where
+
+import Data.ByteString (ByteString, pack)
+import Data.Enumerator (Enumerator, Iteratee, enumLists, run, ($$))
+import Data.Word (Word8)
+import Data.Attoparsec.Enumerator (iterParser, ParseError, errorContexts)
+import Control.Exception (SomeException)
+
+import Network.EBus.Layer2
+
+import Data.Either
+import Test.HUnit
+import Test.HUnit.Base
+
+import Control.Monad
+
+tests = [TestCase testcase]
+
+
+
+
+testcase :: IO()
+testcase = do
+ let x = parsePackageFromList [170,170,241,254,5,3,8,1,1,16,255,88,255,60,9,247]
+ join $ liftM foooo x
+ -- where
+ -- testResults :: (Monad m) => m(Either SomeException EbusPacket) -> m()
+ -- testResults s = do
+ -- let z = case s of
+ -- Right x -> print "true"
+ -- Left y -> print "false"
+ -- if z then print "foo" else print "bl"
+ where foooo xs = do
+ case xs of
+ Right _ -> print "success"
+ Left e -> fail (show e)
+
+
+-- * Utils
+
+-- | Take a list and parse as ebus datastream
+parsePackageFromList :: [Word8] -> IO (Either SomeException EbusPacket)
+parsePackageFromList xs =
+ run ( listEnumerator xs $$ parserIteratee )
+
+
+-- | Transform List xs into a Enumerator
+listEnumerator :: [Word8] -> Enumerator ByteString IO a
+listEnumerator xs = enumLists l
+ where l::[[ByteString]]
+ l = [[pack xs]]
+
+parserIteratee :: Iteratee ByteString IO EbusPacket
+parserIteratee = iterParser ebusParserLayer2
+
+
+
diff --git a/exec/ebus-dump-layer2.hs b/exec/ebus-dump-layer2.hs
index 37a7bbe..06ffc6f 100644
--- a/exec/ebus-dump-layer2.hs
+++ b/exec/ebus-dump-layer2.hs
@@ -1,10 +1,10 @@
module Main(main) where
import Control.Exception
-import Data.Enumerator (Enumerator, Iteratee, run, ($$))
-import Data.Enumerator.Binary (enumHandle)
-import Data.ByteString (ByteString)
import Data.Attoparsec.Enumerator (iterParser, ParseError, errorContexts)
+import Data.ByteString (ByteString,pack)
+import Data.Enumerator (Enumerator, Iteratee, enumLists, run, ($$))
+import Data.Enumerator.Binary (enumHandle)
import System.Exit
import System.IO (hSetBinaryMode,stdin)
@@ -15,24 +15,23 @@ main = do
-- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.)
hSetBinaryMode stdin True
loop 900
+ where loop :: Int -> IO()
+ loop 0 = do
+ print "finished"
+ loop n = do
+ readAndDumpPacket
+ loop $ n - 1
-loop :: Int -> IO()
-loop 0 = do
- print "finished"
-loop n = do
- readAndDumpPacket
- loop (n - 1)
-
-
+-- | Read one eBus Packet and dump layer2 to stdout
readAndDumpPacket :: IO()
readAndDumpPacket = do
-- * run
-- Run an iteratee until it finishes, and return either the final value (if it succeeded) or the error (if it failed).
-- * run_
-- Like run, except errors are converted to exceptions and thrown. Primarily useful for small scripts or other simple cases.
- maybePacket <- run( enumSource $$ runParser )
+ maybePacket <- run( stdinEnumerator $$ parserIteratee)
case maybePacket of
- Right result -> do { print "Result"; print result}
+ Right result -> do { print "Result"; print $ result}
Left exc -> case fromException (exc::SomeException) of
Just e
| any (\ctx -> ctx == "demandInput") (errorContexts (e::ParseError)) -> do
@@ -41,10 +40,12 @@ readAndDumpPacket = do
| otherwise -> do putStr "Other Error: "; print e
_ -> putStr ""
-enumSource :: Enumerator ByteString IO a
-enumSource = enumHandle 1 stdin
+stdinEnumerator :: Enumerator ByteString IO a
+stdinEnumerator = enumHandle 1 stdin
-runParser :: Iteratee ByteString IO EbusPacket
-runParser = do
+parserIteratee :: Iteratee ByteString IO EbusPacket
+parserIteratee = do
p <- iterParser ebusParserLayer2
return p
+
+