module Robotics.NXT.BluetoothUtils (
bluetoothRSSI,
bluetoothLinkQuality
) where
import Control.Exception
import Control.Monad.State
#ifdef linux_HOST_OS
import Foreign.C.String
import Foreign.C.Types
#endif
import Robotics.NXT.Errors
import Robotics.NXT.Protocol
import Robotics.NXT.Types
import Robotics.NXT.Internals
#ifdef linux_HOST_OS
foreign import ccall unsafe "rssi" rssi :: CString -> IO CInt
foreign import ccall unsafe "lq" lq :: CString -> IO CInt
blueError :: Int
blueError = 1000
blueNotConnected :: Int
blueNotConnected = 1001
#endif
bluetoothRSSI :: NXT Int
bluetoothRSSI = do
addr <- bluetoothAddress
bluetoothRSSIAddr addr
bluetoothRSSIAddr :: BTAddress -> NXT Int
#ifdef linux_HOST_OS
bluetoothRSSIAddr addr = do
ret <- liftIO $ withCString addr rssi
let ret' = fromIntegral ret
case ret' of
_ | ret' == blueError -> liftIO $ throwIO $ NXTException "Could not get connection's RSSI"
| ret' == blueNotConnected -> liftIO $ throwIO $ NXTException "Connection not established"
| otherwise -> return ret'
#else
bluetoothRSSIAddr _ = do
liftIO $ throwIO $ NXTException "Not supported on this system"
#endif
bluetoothLinkQuality :: NXT Int
bluetoothLinkQuality = do
addr <- bluetoothAddress
bluetoothLinkQualityAddr addr
bluetoothLinkQualityAddr :: BTAddress -> NXT Int
#ifdef linux_HOST_OS
bluetoothLinkQualityAddr addr = do
ret <- liftIO $ withCString addr lq
let ret' = fromIntegral ret
case ret' of
_ | ret' == blueError -> liftIO $ throwIO $ NXTException "Could not get connection's link quality"
| ret' == blueNotConnected -> liftIO $ throwIO $ NXTException "Connection not established"
| otherwise -> return ret'
#else
bluetoothLinkQualityAddr _ = do
liftIO $ throwIO $ NXTException "Not supported on this system"
#endif
bluetoothAddress :: NXT BTAddress
bluetoothAddress = do
addr <- getsNXT address
case addr of
Just a -> return a
Nothing -> do
_ <- getDeviceInfo
(Just a) <- getsNXT address
return a