aboutsummaryrefslogtreecommitdiff
path: root/hsm-battery/Hsm/Battery/FFI.hsc
blob: 3ccb5c1c4951427d4680598e4c76a14330e63216 (plain)
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 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