getDeviceHandle :: DeviceManager -> Device -> FlowT (ErrorCode ': OpenErrors) Sys Handle
getDeviceHandle dm dev = do

   -- get a fresh device number
   num <- atomically $ do
            n <- readTVar (dmDevNum dm)
            writeTVar (dmDevNum dm) (n+1)
            return n

   let 
      devname = "./dev" ++ show num
      devfd   = dmDevFS dm
      logS    = textFormat ("Opening device " % string % " into " % string)
                  (showDevice dev) devname

   sysLogSequenceL logS <| do
      -- create special file in device fs
      liftFlowT <| createDeviceFile (Just devfd) devname dev BitSet.empty
      -- on success, try to open it
      let flgs = BitSet.fromList [HandleReadWrite,HandleNonBlocking]
      hdl <- liftFlowT <| open (Just devfd) devname flgs BitSet.empty
      -- then remove it
      liftFlowT <| sysUnlinkAt devfd devname False
         `onFlowError` sysWarningShow "Unlinking special device file failed"
      return hdl

-- | Release a device handle
getDeviceHandle :: DeviceManager -> Device -> FlowT (ErrorCode ': OpenErrors) Sys Handle
getDeviceHandle dm dev = do

   -- get a fresh device number
   num <- atomically $ do
            n <- readTVar (dmDevNum dm)
            writeTVar (dmDevNum dm) (n+1)
            return n

   let 
      devname = "./dev" ++ show num
      devfd   = dmDevFS dm
      logS    = textFormat ("Opening device " % string % " into " % string)
                  (showDevice dev) devname

   sysLogSequenceL logS <| do
      -- create special file in device fs
      liftFlowT <| createDeviceFile (Just devfd) devname dev BitSet.empty
      -- on success, try to open it
      let flgs = BitSet.fromList [HandleReadWrite,HandleNonBlocking]
      hdl <- liftFlowT <| open (Just devfd) devname flgs BitSet.empty
      -- then remove it
      liftFlowT <| sysUnlinkAt devfd devname False
         `onFlowError` sysWarningShow "Unlinking special device file failed"
      return hdl

-- | Release a device handle
getDeviceHandle dm dev = do

   -- get a fresh device number
   num <- atomically $ do
            n <- readTVar (dmDevNum dm)
            writeTVar (dmDevNum dm) (n+1)
            return n

   let 
      devname = "./dev" ++ show num
      devfd   = dmDevFS dm
      logS    = textFormat ("Opening device " % string % " into " % string)
                  (showDevice dev) devname

   sysLogSequenceL logS <| do
      -- create special file in device fs
      liftFlowT <| createDeviceFile (Just devfd) devname dev BitSet.empty
      -- on success, try to open it
      let flgs = BitSet.fromList [HandleReadWrite,HandleNonBlocking]
      hdl <- liftFlowT <| open (Just devfd) devname flgs BitSet.empty
      -- then remove it
      liftFlowT <| sysUnlinkAt devfd devname False
         `onFlowError` sysWarningShow "Unlinking special device file failed"
      return hdl

-- | Release a device handle