aboutsummaryrefslogtreecommitdiff
path: root/hsm-gpio/Hsm/GPIO/FFI.hs
blob: c1bb9e86c30087a842d5ca39cbc07af423788c3d (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{-# LANGUAGE CApiFFI #-}

-- FFI bindings to `libgpiod` for direct GPIO hardware access.
--
-- Exposes only the minimal required subset of `libgpiod` functionality used by
-- this project. The bindings are suitable for low-level hardware control.
--
-- Future work could expand this into a comprehensive `gpiod` binding package.
module Hsm.GPIO.FFI
  ( chipOpen
  , chipClose
  , input
  , output
  , LineValue
  , active
  , inactive
  , lineSettingsNew
  , lineSettingsFree
  , lineSettingsSetDirection
  , lineSettingsSetOutputValue
  , lineConfigNew
  , lineConfigFree
  , lineConfigAddLineSettings
  , requestConfigNew
  , requestConfigFree
  , requestConfigSetConsumer
  , LineRequest
  , chipRequestLines
  , lineRequestRelease
  , lineRequestSetValue
  , lineRequestSetValues
  )
where

import Foreign.C.String (CString)
import Foreign.C.Types (CInt (CInt), CSize (CSize), CUInt (CUInt))
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable)

data Chip

foreign import capi unsafe "gpiod.h gpiod_chip_open"
  chipOpen :: CString -> IO (Ptr Chip)

foreign import capi unsafe "gpiod.h gpiod_chip_close"
  chipClose :: Ptr Chip -> IO ()

data LineSettings

newtype LineDirection
  = LineDirection CInt
  deriving Show

foreign import capi safe "gpiod.h value GPIOD_LINE_DIRECTION_INPUT"
  input :: LineDirection

foreign import capi safe "gpiod.h value GPIOD_LINE_DIRECTION_OUTPUT"
  output :: LineDirection

newtype LineValue
  = LineValue CInt
  deriving (Show, Storable)

foreign import capi safe "gpiod.h value GPIOD_LINE_VALUE_ACTIVE"
  active :: LineValue

foreign import capi safe "gpiod.h value GPIOD_LINE_VALUE_INACTIVE"
  inactive :: LineValue

foreign import capi unsafe "gpiod.h gpiod_line_settings_new"
  lineSettingsNew :: IO (Ptr LineSettings)

foreign import capi unsafe "gpiod.h gpiod_line_settings_free"
  lineSettingsFree :: Ptr LineSettings -> IO ()

foreign import capi unsafe "gpiod.h gpiod_line_settings_set_direction"
  lineSettingsSetDirection :: Ptr LineSettings -> LineDirection -> IO CInt

foreign import capi unsafe "gpiod.h gpiod_line_settings_set_output_value"
  lineSettingsSetOutputValue :: Ptr LineSettings -> LineValue -> IO CInt

data LineConfig

foreign import capi unsafe "gpiod.h gpiod_line_config_new"
  lineConfigNew :: IO (Ptr LineConfig)

foreign import capi unsafe "gpiod.h gpiod_line_config_free"
  lineConfigFree :: Ptr LineConfig -> IO ()

foreign import capi unsafe "gpiod.h gpiod_line_config_add_line_settings"
  lineConfigAddLineSettings
    :: Ptr LineConfig -> Ptr CUInt -> CSize -> Ptr LineSettings -> IO CInt

data RequestConfig

foreign import capi unsafe "gpiod.h gpiod_request_config_new"
  requestConfigNew :: IO (Ptr RequestConfig)

foreign import capi unsafe "gpiod.h gpiod_request_config_free"
  requestConfigFree :: Ptr RequestConfig -> IO ()

foreign import capi unsafe "gpiod.h gpiod_request_config_set_consumer"
  requestConfigSetConsumer :: Ptr RequestConfig -> CString -> IO ()

data LineRequest

foreign import capi unsafe "gpiod.h gpiod_chip_request_lines"
  chipRequestLines
    :: Ptr Chip -> Ptr RequestConfig -> Ptr LineConfig -> IO (Ptr LineRequest)

foreign import capi unsafe "gpiod.h gpiod_line_request_release"
  lineRequestRelease :: Ptr LineRequest -> IO ()

foreign import capi unsafe "gpiod.h gpiod_line_request_set_value"
  lineRequestSetValue :: Ptr LineRequest -> CUInt -> LineValue -> IO CInt

foreign import capi unsafe "gpiod.h gpiod_line_request_set_values"
  lineRequestSetValues :: Ptr LineRequest -> Ptr LineValue -> IO CInt