0 | module Bindings.RtlSdr.Device
2 | import Bindings.RtlSdr.Raw.Device
3 | import Bindings.RtlSdr.Raw.Support
4 | import Bindings.RtlSdr.Error
6 | import public Bindings.RtlSdr.Raw.Device
15 | rtlsdr_open : Nat -> IO (Maybe (Ptr RtlSdrHandle))
16 | rtlsdr_open idx = do
17 | v <- prim__castPtr <$> malloc 4
19 | p <- fromPrim $
idris_rtlsdr_open (cast {to = Int} idx) v
21 | free $
prim__forgetPtr v
22 | io_pure $
if ret == 0 then Just (prim__castPtr p) else Nothing
26 | rtlsdr_close : Ptr RtlSdrHandle -> IO (Either RTLSDR_ERROR ())
28 | r <- fromPrim $
close h
29 | io_pure $
if r == 0 then Right () else Left RtlSdrError
33 | record DeviceUSBStrings where
34 | constructor MkDeviceUSBStrings
40 | Show DeviceUSBStrings where
41 | show ds = ds.manufact ++ ", " ++ ds.product ++ ", " ++ ds.serial ++ "."
47 | getDeviceUSBStrings : Nat -> IO (Either RTLSDR_ERROR DeviceUSBStrings)
48 | getDeviceUSBStrings i = do
51 | let n = get_device_count
53 | m <- prim__castPtr <$> malloc 256
54 | p <- prim__castPtr <$> malloc 256
55 | s <- prim__castPtr <$> malloc 256
56 | let r = get_device_usb_strings (cast {to = Int} i) m p s
57 | let ds = MkDeviceUSBStrings (idris_rtlsdr_getstring m) (idris_rtlsdr_getstring p) (idris_rtlsdr_getstring s)
59 | free $
prim__forgetPtr m
60 | free $
prim__forgetPtr p
61 | free $
prim__forgetPtr s
62 | io_pure $
if (r == 0 && n > 0) then Right ds else Left RtlSdrError
64 | decodeRetError : Int -> RTLSDR_ERROR
65 | decodeRetError e = case e of
66 | -
1 => RtlSdrDeviceNameEmpty
67 | -
2 => RtlSdrDeviceNotFound
68 | -
3 => RtlSdrDeviceFoundButNotMatching
77 | getDeviceIndexBySerial : String -> Either RTLSDR_ERROR Nat
78 | getDeviceIndexBySerial s = do
79 | let r = get_index_by_serial s
80 | if r < 0 then Left (decodeRetError r) else Right (cast {to = Nat} r)