GithubHelp home page GithubHelp logo

Comments (9)

makar853 avatar makar853 commented on June 29, 2024

Sorry, I forgot my hackRf at work. So I can't try to recreate this error till Monday. But firstly I would check if this error can be caused by calling OpenDevice() two times in a row with the same hackrf_device_info object or if NetHackrf.Close() wasn't called before trying to establish new connection with the hackrf.

from nethackrf.

makar853 avatar makar853 commented on June 29, 2024

I could get a HACKRF_ERROR_NOT_FOUND error by calling OpenDevice twice in the same hackrf_device_info object. Is it possible that you tried to read Device property from your class several times?

from nethackrf.

ztamas83 avatar ztamas83 commented on June 29, 2024

Thanks for checking! Theoretically it is possible as I use the following code but do not await so the application can do it's job once the txStartCallback was triggered, after it's done the cts is cancelled in the application and the Device should be closed.
There are subsequent transmission tasks, so it is theoretically possible that the Device is not release before the next call.
Will try to figure out if this is the case.

private async Task TransmitAsync(CancellationTokenSource cts, byte[] buf, double carrierFreqMHz, byte amplitude, double sampleFreqMHz, int TxVgaGainDb, Action txStartCallback = null)
        {
            // Use "using" statement so the connection to the USB device is closed upon finish
            using (var dev = Device)
            {
                dev.CarrierFrequencyMHz = carrierFreqMHz;
                dev.SampleFrequencyMHz = sampleFreqMHz;
                dev.TXVGAGainDb = TxVgaGainDb;

                //dataStream must be disposed in order to stop transmission
                using (var dataStream = dev.StartTX())
                {
                    // only log every second, otherwise it will spam
                    var logTime = DateTime.Now.AddSeconds(LogIntervalSec);

                    Log.Debug($"RF transmission started. f = {carrierFreqMHz}, A = {amplitude}");

                    txStartCallback?.Invoke();

                    while (!cts.IsCancellationRequested)
                    {
                        await dataStream.WriteAsync(buf, 0, buf.Length);
                        if (DateTime.Now >= logTime)
                        {
                            Log.Trace("RF transmission ongoing ...");
                            logTime = DateTime.Now.AddSeconds(LogIntervalSec);
                        }
                    }
                }
                Log.Debug("RF transmission ended");
                dev.Reset();
            }
        }

from nethackrf.

ztamas83 avatar ztamas83 commented on June 29, 2024

I changed the transmit function to use a semaphore and ensure only one access, but still got the error. Since the semaphore release is in the "finally" section the NetHackRf.Dispose() should already be handled. I added some logs to be able to see if there are multiple open requests, but no.

private nethackrf.NetHackrf Device
        {
            get
            {
                if (_deviceInfo == null)
                {
                    throw new InvalidOperationException("Device not initialized");
                }
                Log.Trace("Device access request");
                return _deviceInfo.OpenDevice();
            }
        }


private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);

private async Task TransmitAsync(CancellationTokenSource cts, byte[] buf, double carrierFreqMHz, byte amplitude, double sampleFreqMHz, int TxVgaGainDb, Action txStartCallback = null)
        {
            try
            {
                if (!await _semaphore.WaitAsync(0))
                {
                    Log.Warn("Another transmission is ongoing, waiting max 5s to enter semaphore.");
                    if (!await _semaphore.WaitAsync(5000, cts.Token))
                    {
                        throw new Exception("HackRF transmission resource could not be granted");
                    }
                }
                else { Log.Trace("Semaphore granted access to HackRF transmission!"); }

                // Use "using" statement so the connection to the USB device is closed upon finish
                using (var dev = Device)
                {
                    dev.CarrierFrequencyMHz = carrierFreqMHz;
                    dev.SampleFrequencyMHz = sampleFreqMHz;
                    dev.TXVGAGainDb = TxVgaGainDb;

                    //dataStream must be disposed in order to stop transmission
                    using (var dataStream = dev.StartTX())
                    {
                        // only log every second, otherwise it will spam
                        var logTime = DateTime.Now.AddSeconds(LogIntervalSec);

                        Log.Debug($"RF transmission started. f = {carrierFreqMHz}, A = {amplitude}");

                        txStartCallback?.Invoke();

                        while (!cts.IsCancellationRequested)
                        {
                            await dataStream.WriteAsync(buf, 0, buf.Length);
                            if (DateTime.Now >= logTime)
                            {
                                Log.Trace("RF transmission ongoing ...");
                                logTime = DateTime.Now.AddSeconds(LogIntervalSec);
                            }
                        }
                    }
                    Log.Debug("RF transmission ended");
                    dev.Reset();
                }
                Log.Debug("Device released");
            }
            finally
            {
                _semaphore.Release();
            }
        }

The logs are as follows:

Line 1905: 2023-04-24 11:30:27.4198 TRACE (24) Cpac.Hw.HackRf.HackRfDevice : Semaphore granted access to HackRF transmission!
Line 1906: 2023-04-24 11:30:27.4198 TRACE (24) Cpac.Hw.HackRf.HackRfDevice : Device access request
Line 1907: 2023-04-24 11:30:27.4614 DEBUG (24) Cpac.Hw.HackRf.HackRfDevice : RF transmission started. f = 1575.42, A = 0
Line 1908: 2023-04-24 11:30:33.4174 TRACE (30) Cpac.Hw.HackRf.HackRfDevice : RF transmission ongoing ...
Line 1909: 2023-04-24 11:30:40.3259 TRACE ( 8) Cpac.Hw.HackRf.HackRfDevice : RF transmission ongoing ...
Line 1910: 2023-04-24 11:30:40.3369 DEBUG ( 8) Cpac.Hw.HackRf.HackRfDevice : RF transmission ended
Line 1911: 2023-04-24 11:30:40.8486 DEBUG ( 8) Cpac.Hw.HackRf.HackRfDevice : Device released
Line 5211: 2023-04-24 11:33:31.4375 TRACE (22) Cpac.Hw.HackRf.HackRfDevice : Semaphore granted access to HackRF transmission!
Line 5212: 2023-04-24 11:33:31.4375 TRACE (22) Cpac.Hw.HackRf.HackRfDevice : Device access request
Line 5213: 2023-04-24 11:33:31.4657 DEBUG (22) Cpac.Hw.HackRf.HackRfDevice : RF transmission started. f = 871.4, A = 0
Line 5218: 2023-04-24 11:33:32.4683 TRACE (42) Cpac.Hw.HackRf.HackRfDevice : RF transmission ongoing ...
Line 5224: 2023-04-24 11:33:33.5055 TRACE (41) Cpac.Hw.HackRf.HackRfDevice : RF transmission ongoing ...
Line 5226: 2023-04-24 11:33:34.5128 DEBUG (36) Cpac.Hw.HackRf.HackRfDevice : RF transmission ended
Line 5227: 2023-04-24 11:33:35.0242 DEBUG (36) Cpac.Hw.HackRf.HackRfDevice : Device released
Line 5241: 2023-04-24 11:33:35.0242 TRACE (40) Cpac.Hw.HackRf.HackRfDevice : Semaphore granted access to HackRF transmission!
Line 5242: 2023-04-24 11:33:35.0242 TRACE (40) Cpac.Hw.HackRf.HackRfDevice : Device access request
Line 5243: 2023-04-24 11:33:35.0541 ERROR (40) Cpac.Hw.HackRf.HackRfDevice : System.Exception: Error "HACKRF_ERROR_NOT_FOUND" have occured in OpenDevice.

from nethackrf.

makar853 avatar makar853 commented on June 29, 2024

I see that when the error uccured, you were calling OpenDevice after only 30ms from disposing. Before that there was a 3 second delay between releasing and accessing. Maybe libusb library is still doing something with usb connection in those 30ms? By the way, is it necessary to release and open the same device again in the middle of the program? If so, than I think there should be atleast Thread.Sleep or something. Maybe it would help.

from nethackrf.

ztamas83 avatar ztamas83 commented on June 29, 2024

Yes, the open/close is necessary as I want to provide a secure implementation and don't trust the users that they will close themselves. So I provide an interface with a few parameters only and handle all device related stuff here.
I added a small delay before releasing the semaphore, hope it helps.

from nethackrf.

ztamas83 avatar ztamas83 commented on June 29, 2024

I unfortunately couldn't solve the issue, however I understand now that it's not a problem with this codebase but probably libusb. Closing this issue, thanks for the support!

from nethackrf.

makar853 avatar makar853 commented on June 29, 2024

I also have no success with recreating this bug. All I can suggest at this point is to move OpenDevice and Dispose/Reset methods to constructor and destructor of your class respectively and to store nethackrf object in a private field. Thus, users wouldn't have direct control over the usb connection and you don't have to constantly close and open connection.

from nethackrf.

makar853 avatar makar853 commented on June 29, 2024

Only now I have noticed that you are using Reset method to close connection rather than Dispose. And almost immediately I got hackrf_not_found error. Reset method also reboots the usb logic of hackrf. So the device disconnects and connects again after the reboot. Amount of time taken by this process is quite non-deterministic and depends on both your OS and usb-hub.
As I said I would either call OpenDevice and reset only in constructor/destructor methods or just use Dispose instead of Reset

from nethackrf.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.