NXT / lib / Robotics / NXT / BluetoothUtils.hs

{-# LANGUAGE ForeignFunctionInterface, CPP #-}
{-# CFILES ffi/blue.c #-}

module Robotics.NXT.BluetoothUtils (
  -- * Bluetooth utils
  -- | `getDeviceInfo` returns zero for Bluetooth signal strength as this is not implemented in current NXT firmware versions. 
  -- Here are functions which retrieve that from a host (computer) Bluetooth stack.
  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 function call for C function which returns RSSI Bluetooth value of a connection to a given Bluetooth address
foreign import ccall unsafe "rssi" rssi :: CString -> IO CInt

-- Foreign function call for C function which returns link quality Bluetooth value of a connection to a given Bluetooth address
foreign import ccall unsafe "lq" lq :: CString -> IO CInt

-- As defined in blue.h
blueError :: Int
blueError = 1000
blueNotConnected :: Int
blueNotConnected = 1001
#endif

{-|
Gets received signal strength indicator (RSSI) of the Bluetooth connection to the NXT brick.

Currently supported only on Linux. It throws a 'NXTException' otherwise.
-}
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

{-|
Gets link quality of the Bluetooth connection to the NXT brick.

Currently supported only on Linux. It throws a 'NXTException' otherwise.
-}
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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.