0 | module Bindings.RtlSdr.EEProm
 1 |
 2 | import Bindings.RtlSdr.Device
 3 | import Bindings.RtlSdr.Error
 4 | import Bindings.RtlSdr.Raw.EEProm
 5 | import Bindings.RtlSdr.Raw.Support
 6 |
 7 | import Data.Bits
 8 | import Data.Buffer
 9 | import Data.List
10 | import System.FFI
11 |
12 | %default total
13 |
14 | decodeRetError : Int -> RTLSDR_ERROR
15 | decodeRetError e = case e of
16 |                         -1 => RtlSdrHandleInvalid
17 |                         -2 => RtlSdrEEPromSizeExceeded
18 |                         -3 => RtlSdrEEPromNotFound
19 |                         _ =>  RtlSdrError -- unknonwn
20 |
21 | ||| Read EEPROM connected to RTL device
22 | |||
23 | ||| @h is the device handle
24 | ||| @o is the offset address where the data should be read from
25 | ||| @l is the length of the data to read
26 | export
27 | readEEProm : Ptr RtlSdrHandle -> Int -> Int -> IO (Either RTLSDR_ERROR Buffer)
28 | readEEProm h o l = do
29 |   Just buf <- newBuffer l
30 |     | Nothing => io_pure $ Left RtlSdrError
31 |   r <- fromPrim $ read_eeprom h buf o l
32 |   io_pure $ if r < 0 then Left (decodeRetError r) else Right buf
33 |
34 |
35 | ||| Write EEPROM connected to RTL device
36 | |||
37 | ||| @h is the device handle
38 | ||| @b is the buffer of data to be written
39 | ||| @o is the offset address where the data should be written to
40 | export
41 | writeEEProm : Ptr RtlSdrHandle -> Buffer -> Int -> IO (Either RTLSDR_ERROR ())
42 | writeEEProm h b o = do
43 |   len <- rawSize b
44 |   r <- fromPrim $ write_eeprom h b o len
45 |   io_pure $ if r < 0 then Left (decodeRetError r) else Right ()
46 |