module DBus
(
Message
, MethodCall
, methodCall
, methodCallPath
, methodCallInterface
, methodCallMember
, methodCallSender
, methodCallDestination
, methodCallAutoStart
, methodCallReplyExpected
, methodCallBody
, MethodReturn
, methodReturn
, methodReturnSerial
, methodReturnSender
, methodReturnDestination
, methodReturnBody
, MethodError
, methodError
, methodErrorName
, methodErrorSerial
, methodErrorSender
, methodErrorDestination
, methodErrorBody
, methodErrorMessage
, Signal
, signal
, signalPath
, signalMember
, signalInterface
, signalSender
, signalDestination
, signalBody
, ReceivedMessage(ReceivedMethodCall, ReceivedMethodReturn, ReceivedMethodError, ReceivedSignal)
, receivedMessageSerial
, receivedMessageSender
, receivedMessageBody
, Variant
, IsVariant(..)
, variantType
, IsAtom
, IsValue
, typeOf
, Signature
, Type(..)
, signature
, signature_
, signatureTypes
, formatSignature
, parseSignature
, ObjectPath
, objectPath_
, formatObjectPath
, parseObjectPath
, InterfaceName
, interfaceName_
, formatInterfaceName
, parseInterfaceName
, MemberName
, memberName_
, formatMemberName
, parseMemberName
, ErrorName
, errorName_
, formatErrorName
, parseErrorName
, BusName
, busName_
, formatBusName
, parseBusName
, Structure
, structureItems
, Array
, arrayItems
, Dictionary
, dictionaryItems
, Address
, addressMethod
, addressParameters
, address
, formatAddress
, formatAddresses
, parseAddress
, parseAddresses
, getSystemAddress
, getSessionAddress
, getStarterAddress
, Endianness (..)
, marshal
, MarshalError
, marshalErrorMessage
, unmarshal
, UnmarshalError
, unmarshalErrorMessage
, Serial
, serialValue
, firstSerial
, nextSerial
, UUID
, formatUUID
, randomUUID
) where
import Control.Monad (replicateM)
import qualified Data.ByteString.Char8 as Char8
import Data.Word (Word16)
import System.Random (randomRIO)
import Text.Printf (printf)
import DBus.Internal.Address
import DBus.Internal.Message
import qualified DBus.Internal.Types
import DBus.Internal.Types hiding (typeOf)
import DBus.Internal.Wire
typeOf :: IsValue a => a -> Type
typeOf = DBus.Internal.Types.typeOf
methodCall :: ObjectPath -> InterfaceName -> MemberName -> MethodCall
methodCall path iface member = MethodCall path (Just iface) member Nothing Nothing True True []
methodReturn :: Serial -> MethodReturn
methodReturn s = MethodReturn s Nothing Nothing []
methodError :: Serial -> ErrorName -> MethodError
methodError s name = MethodError name s Nothing Nothing []
signal :: ObjectPath -> InterfaceName -> MemberName -> Signal
signal path iface member = Signal path iface member Nothing Nothing []
receivedMessageSerial :: ReceivedMessage -> Serial
receivedMessageSerial (ReceivedMethodCall s _) = s
receivedMessageSerial (ReceivedMethodReturn s _) = s
receivedMessageSerial (ReceivedMethodError s _) = s
receivedMessageSerial (ReceivedSignal s _) = s
receivedMessageSerial (ReceivedUnknown s _) = s
receivedMessageSender :: ReceivedMessage -> Maybe BusName
receivedMessageSender (ReceivedMethodCall _ msg) = methodCallSender msg
receivedMessageSender (ReceivedMethodReturn _ msg) = methodReturnSender msg
receivedMessageSender (ReceivedMethodError _ msg) = methodErrorSender msg
receivedMessageSender (ReceivedSignal _ msg) = signalSender msg
receivedMessageSender (ReceivedUnknown _ msg) = unknownMessageSender msg
receivedMessageBody :: ReceivedMessage -> [Variant]
receivedMessageBody (ReceivedMethodCall _ msg) = methodCallBody msg
receivedMessageBody (ReceivedMethodReturn _ msg) = methodReturnBody msg
receivedMessageBody (ReceivedMethodError _ msg) = methodErrorBody msg
receivedMessageBody (ReceivedSignal _ msg) = signalBody msg
receivedMessageBody (ReceivedUnknown _ msg) = unknownMessageBody msg
marshal :: Message msg => Endianness -> Serial -> msg -> Either MarshalError Char8.ByteString
marshal = marshalMessage
unmarshal :: Char8.ByteString -> Either UnmarshalError ReceivedMessage
unmarshal = unmarshalMessage
newtype UUID = UUID Char8.ByteString
deriving (Eq, Ord, Show)
formatUUID :: UUID -> String
formatUUID (UUID bytes) = Char8.unpack bytes
randomUUID :: IO UUID
randomUUID = do
let hexInt16 i = printf "%04x" (i :: Int)
int16s <- replicateM 8 (randomRIO (0, fromIntegral (maxBound :: Word16)))
return (UUID (Char8.pack (concatMap hexInt16 int16s)))