{-# LANGUAGE CApiFFI #-} {-# LANGUAGE DeriveAnyClass #-} module Hsm.Battery.FFI ( I2CMsg (..) , I2CRdWrIoctlData (..) , i2cRdWr , i2cMRd , i2cMNoStart , ioctl ) where import Data.Word (Word16, Word32, Word8) import Foreign.C.Types (CInt (CInt)) import Foreign.CStorable (CStorable, cAlignment, cPeek, cPoke, cSizeOf) import Foreign.Ptr (Ptr) import Foreign.Storable (Storable (..)) import GHC.Generics (Generic) import System.Posix.Types (Fd (Fd)) data I2CMsg = I2CMsg { addr :: Word16 , flags :: Word16 , len :: Word16 , buf :: Ptr Word8 } deriving (CStorable, Generic, Show) instance Storable I2CMsg where sizeOf = cSizeOf alignment = cAlignment poke = cPoke peek = cPeek data I2CRdWrIoctlData = I2CRdWrIoctlData { msgs :: Ptr I2CMsg , nmsgs :: Word32 } deriving (CStorable, Generic, Show) instance Storable I2CRdWrIoctlData where sizeOf = cSizeOf alignment = cAlignment poke = cPoke peek = cPeek foreign import capi safe "linux/i2c-dev.h value I2C_RDWR" i2cRdWr :: Int foreign import capi safe "linux/i2c.h value I2C_M_RD" i2cMRd :: Int foreign import capi safe "linux/i2c.h value I2C_M_NOSTART" i2cMNoStart :: Int foreign import capi safe "sys/ioctl.h" ioctl :: Fd -> Int -> Ptr a -> IO Int