-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.hs
58 lines (35 loc) · 1.38 KB
/
Message.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{-# LANGUAGE OverloadedStrings, FlexibleInstances, RankNTypes #-}
module Message (
Message (..)
,MessageDef (..)
) where
import Data.Aeson (FromJSON, parseJSON, (.:), Value(Object))
import Control.Applicative ((<$>), (<*>))
import Common
import Data.BitVector (fromBool)
import Common
import CCSDS
import Command
import Telemetry
import Parameter
import Variable()
import Types
import Auto
instance (FromJSON a) => FromJSON (Message a) where
parseJSON json@(Object o) = Message <$> (read <$> o .: "mid") <*> parseJSON json
parseJSON _ = error "Invalid message definition."
instance (FromJSON a) => FromJSON (MessageDef a)
instance CCSDS (Message Telemetry) where
packetType (Message _ (Telemetry _)) = fromBool False
applicationProcessId (Message mid _) = safeBitVec 11 mid
secondaryHeader (Message _ (Telemetry _)) = [0, 0, 0, 0, 0, 0] -- timestamp
payload (Message _ (Telemetry ps)) = concat <$> mapM packParam ps
instance CCSDS (Message Command) where
packetType (Message _ (Command _ _)) = fromBool True
applicationProcessId (Message mid _) = safeBitVec 11 mid
secondaryHeader (Message _ (Command c _)) = [c, 0]
payload (Message _ (Command _ ps)) = concat <$> mapM packParam ps
instance (AutoShow a) => AutoShow (Message a) where
autoShow (Message mid m) = do
sm <- autoShow m
return $ "mid: " ++ showHex' mid ++ " " ++ sm