Skip to content

Commit

Permalink
blocks/sources/rtlsdr: add sigterm handler to cancel reading
Browse files Browse the repository at this point in the history
and allow device close after rtlsdr_read_async() returns.

resolves #8.
  • Loading branch information
vsergeev committed Jul 5, 2016
1 parent dccbeab commit 9006298
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions radio/blocks/sources/rtlsdr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ local ffi = require('ffi')
local block = require('radio.core.block')
local platform = require('radio.core.platform')
local types = require('radio.types')
local async = require('radio.core.async')

local RtlSdrSource = block.factory("RtlSdrSource")

Expand Down Expand Up @@ -194,15 +195,38 @@ local function read_async_callback_factory(pipes)
return read_async_callback
end

function sigterm_handler_factory(dev)
local ffi = require('ffi')

ffi.cdef[[
typedef struct rtlsdr_dev rtlsdr_dev_t;
int rtlsdr_cancel_async(rtlsdr_dev_t *dev);
]]
local librtlsdr = ffi.load('rtlsdr')

function sigterm_handler(sig)
librtlsdr.rtlsdr_cancel_async(ffi.cast('rtlsdr_dev_t *', dev))
end

return ffi.cast('void (*)(int)', sigterm_handler)
end

function RtlSdrSource:run()
-- Initialize the rtlsdr in our own running process
self:initialize_rtlsdr()

-- Register SIGTERM signal handler
local handler, handler_state = async.callback(sigterm_handler_factory, tonumber(ffi.cast("intptr_t", self.dev[0])))
ffi.C.signal(ffi.C.SIGTERM, handler)

-- Start asynchronous read
local ret = librtlsdr.rtlsdr_read_async(self.dev[0], read_async_callback_factory(self.outputs[1].pipes), nil, 0, 32768)
if ret ~= 0 then
error("rtlsdr_read_async(): " .. tostring(ret))
end

-- Close rtlsdr
librtlsdr.rtlsdr_close(self.dev[0])
end

return RtlSdrSource

0 comments on commit 9006298

Please sign in to comment.