Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global hotplug events #67

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
17c1691
First iteration of hot-plug disconnect handling
DominykasPetke Jun 11, 2024
a08b427
Add hotplug callback adding and removal
DominykasPetke Jun 11, 2024
291949d
Slight GUI file clean-up
DominykasPetke Jun 11, 2024
cb44fe6
Fix small tiny piece of undefined behaviour
DominykasPetke Jun 11, 2024
0510745
Fix all related GUI crashes
DominykasPetke Jun 11, 2024
61727ad
Make the IUSB struct nicer as well
DominykasPetke Jun 11, 2024
13ae46e
Add note in FT601 code that hotplugging events are not supported by t…
DominykasPetke Jun 11, 2024
2cf4f61
Add note on Cypress API library hotplugging capabilities
DominykasPetke Jun 12, 2024
450b74e
Don't remove the callback event in fftviewer if it did not actually a…
DominykasPetke Jun 12, 2024
fc43235
Add stopping callbacks to examples and `limeTRX`
DominykasPetke Jun 12, 2024
6b6598e
clang-format
DominykasPetke Jun 12, 2024
9339a01
Change USB serial in `IUSB::Connect` to `std::string`
DominykasPetke Jun 20, 2024
74624f0
FX3: connect to the specified device, not the first one in the list
DominykasPetke Jun 20, 2024
3bf0c95
IUSB: correct the `<` operator as MSVC's STL was complaining it was w…
DominykasPetke Jun 20, 2024
bfaa56b
FX3: make header documentation a touch nicer
DominykasPetke Jun 20, 2024
fe03b02
Add device disconnect callback on Windows
DominykasPetke Jun 20, 2024
c00de83
Fix VS2017 and VS2019 compilation
DominykasPetke Jun 20, 2024
732970b
Add proper Global Hotplug events on Windows
DominykasPetke Jun 21, 2024
f63f8bf
Fix compile error on Linux
DominykasPetke Jun 21, 2024
17781ae
Add in global callbacks in `libusb`
DominykasPetke Jun 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions GUI/DeviceConnectionPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include "limesuiteng/DeviceHandle.h"
#include "limesuiteng/DeviceRegistry.h"

#include "comms/USB/GlobalHotplugEvents.h"

#include "events.h"

namespace lime {

void DeviceConnectionPanel::EnumerateDevicesToChoice()
std::size_t DeviceConnectionPanel::EnumerateDevicesToChoice()
{
wxChoice* choice = cmbDevHandle;
choice->Clear();
Expand All @@ -38,10 +40,16 @@ void DeviceConnectionPanel::EnumerateDevicesToChoice()
choice->Enable();
btnDisconnect->Enable();
}

SetSizerAndFit(szBox);
return handles.size();
} catch (std::runtime_error& e)
{
choice->Disable();
}

SetSizerAndFit(szBox);
return 0;
}

void DeviceConnectionPanel::SetSelection(uint32_t index)
Expand All @@ -55,7 +63,7 @@ void DeviceConnectionPanel::SetSelection(uint32_t index)
DeviceConnectionPanel::DeviceConnectionPanel(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
: wxPanel(parent, id, pos, size, style, "DeviceConnectionPanel")
{
wxBoxSizer* szBox = new wxBoxSizer(wxHORIZONTAL);
szBox = new wxBoxSizer(wxHORIZONTAL);

szBox->Add(new wxStaticText(this, wxID_ANY, _("Device:")), 0, wxALIGN_CENTER_VERTICAL, 0);
cmbDevHandle = new wxChoice(this, wxNewId());
Expand All @@ -70,11 +78,14 @@ DeviceConnectionPanel::DeviceConnectionPanel(wxWindow* parent, wxWindowID id, co

EnumerateDevicesToChoice();

this->SetSizerAndFit(szBox);

// when device choice changes generate CONNECT_DEVICE
cmbDevHandle->Bind(wxEVT_CHOICE, wxCommandEventHandler(DeviceConnectionPanel::SendHandleChangeEvent), this);
btnDisconnect->Bind(wxEVT_BUTTON, wxCommandEventHandler(DeviceConnectionPanel::SendDisconnectEvent), this);

Bind(limeEVT_HOTPLUG, wxCommandEventHandler(DeviceConnectionPanel::OnDeviceHotplug), this);

GlobalHotplugEvents::AddGlobalHotplugConnectCallback(OnDeviceHotplugCallback, this);
GlobalHotplugEvents::AddGlobalHotplugDisconnectCallback(OnDeviceHotplugCallback, this);
}

DeviceConnectionPanel::~DeviceConnectionPanel()
Expand All @@ -96,4 +107,23 @@ void DeviceConnectionPanel::SendHandleChangeEvent(wxCommandEvent& inEvent)
ProcessWindowEvent(event);
}

void DeviceConnectionPanel::OnDeviceHotplug(wxCommandEvent& inEvent)
{
const auto currentSelectionString{ cmbDevHandle->GetStringSelection() };

const auto count = EnumerateDevicesToChoice();
if (count > 0)
{
const auto newSelectionIndex{ cmbDevHandle->FindString(currentSelectionString) };
cmbDevHandle->Select(newSelectionIndex);
}
}

void DeviceConnectionPanel::OnDeviceHotplugCallback(void* data)
{
auto* evt = new wxCommandEvent();
evt->SetEventType(limeEVT_HOTPLUG);
wxQueueEvent(reinterpret_cast<DeviceConnectionPanel*>(data), evt);
}

} // namespace lime
8 changes: 7 additions & 1 deletion GUI/DeviceConnectionPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <wx/panel.h>
#include <cstdint>

class wxBoxSizer;
class wxButton;
class wxChoice;

Expand All @@ -23,9 +24,14 @@ class DeviceConnectionPanel : public wxPanel
void SendDisconnectEvent(wxCommandEvent& inEvent);
void SendHandleChangeEvent(wxCommandEvent& inEvent);

void EnumerateDevicesToChoice();
std::size_t EnumerateDevicesToChoice();
void OnDeviceHotplug(wxCommandEvent& inEvent);

static void OnDeviceHotplugCallback(void* data);

wxChoice* cmbDevHandle;
wxButton* btnDisconnect;
wxBoxSizer* szBox;
};

} // namespace lime
1 change: 1 addition & 0 deletions GUI/events.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "events.h"

wxDEFINE_EVENT(limeEVT_SDR_HANDLE_SELECTED, wxCommandEvent);
wxDEFINE_EVENT(limeEVT_HOTPLUG, wxCommandEvent);
1 change: 1 addition & 0 deletions GUI/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
#include <wx/event.h>

wxDECLARE_EVENT(limeEVT_SDR_HANDLE_SELECTED, wxCommandEvent);
wxDECLARE_EVENT(limeEVT_HOTPLUG, wxCommandEvent);
12 changes: 12 additions & 0 deletions GUI/fftviewer_wxgui/fftviewer_frFFTviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "limesuiteng/StreamConfig.h"
#include "limesuiteng/complex.h"
#include <array>
#include <optional>

using namespace std;
using namespace lime;
Expand Down Expand Up @@ -463,9 +464,16 @@ void fftviewer_frFFTviewer::StreamingLoop(

const lime::complex32f_t* src[2] = { txPattern[0].data(), txPattern[1].data() };*/

std::optional<std::size_t> callbackId{ std::nullopt };
try
{
pthis->device->StreamSetup(config, chipIndex);
callbackId = pthis->device->AddHotplugDisconnectCallback(
[](void* data) {
auto* const viewer = reinterpret_cast<fftviewer_frFFTviewer*>(data);
viewer->StopStreaming();
},
pthis);
pthis->device->StreamStart(chipIndex);
} catch (std::logic_error& e)
{
Expand Down Expand Up @@ -646,6 +654,10 @@ void fftviewer_frFFTviewer::StreamingLoop(
}*/

kiss_fft_free(m_fftCalcPlan);
if (callbackId)
{
pthis->device->RemoveHotplugDisconnectCallback(callbackId.value());
}
pthis->stopProcessing.store(true);
pthis->device->StreamStop(chipIndex);

Expand Down
18 changes: 13 additions & 5 deletions GUI/limeGUIFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#endif //__BORLANDC__

#include <wx/msgdlg.h>
#include <wx/string.h>

#include "chips/LMS7002M/lms7002_mainPanel.h"

#include "limeGUIFrame.h"
#include "dlgAbout.h"
#include "lms7suiteEvents.h"
Expand All @@ -19,9 +19,7 @@
#include "utility/pnlMiniLog.h"
#include "FPGAcontrols_wxgui.h"
#include "utility/SPI_wxgui.h"
#include <wx/string.h>
#include "utility/dlgDeviceInfo.h"
#include <functional>
#include "boards/pnlBoardControls.h"
#include "protocols/LMSBoards.h"
#include "utility/SPI_wxgui.h"
Expand All @@ -34,6 +32,9 @@
#include "limesuiteng/SDRDescriptor.h"
#include "DeviceTreeNode.h"
#include "limesuiteng/Logger.h"
#include "DeviceConnectionPanel.h"

#include <functional>

using namespace std;
using namespace lime;
Expand All @@ -43,8 +44,6 @@ static constexpr int controlColumn = 1;

limeGUIFrame* limeGUIFrame::obj_ptr = nullptr;

int limeGUIFrame::m_lmsSelection = 0;

void limeGUIFrame::OnGlobalLogEvent(const lime::LogLevel level, const std::string& message)
{
if (obj_ptr == nullptr || obj_ptr->mMiniLog == nullptr)
Expand Down Expand Up @@ -399,6 +398,15 @@ void limeGUIFrame::OnDeviceHandleChange(wxCommandEvent& event)
wxPostEvent(this, evt);
UpdateConnections(lmsControl);

lmsControl->AddHotplugDisconnectCallback(
[&](void* data) {
auto* evt = new wxCommandEvent();
evt->SetEventType(limeEVT_SDR_HANDLE_SELECTED);
evt->SetString(wxEmptyString);
wxQueueEvent(this, evt);
},
this);

Fit();
} catch (std::runtime_error& e)
{
Expand Down
24 changes: 12 additions & 12 deletions GUI/limeGUIFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,28 @@
#include <wx/choice.h>

#include <map>
#include "IModuleFrame.h"
#include "ISOCPanel.h"
#include "DeviceConnectionPanel.h"
#include "dlgAbout.h"
#include "limeGUI.h"

class pnlMiniLog;
class lms7002_mainPanel;
class fftviewer_frFFTviewer;
class LMS_Programming_wxgui;
class pnlBoardControls;
class IModuleFrame;
class ISOCPanel;

namespace lime {
class SDRDevice;
class DeviceConnectionPanel;
} // namespace lime

class limeGUIFrame : public wxFrame
{
protected:
public:
limeGUIFrame(wxWindow* parent, const AppArgs& appArgs);
~limeGUIFrame();

private:
void AddModule(IModuleFrame* module, const std::string& title);
void RemoveModule(IModuleFrame* module);
// Handlers for AppFrame events.
Expand All @@ -57,13 +64,6 @@ class limeGUIFrame : public wxFrame

void DeviceTreeSelectionChanged(wxTreeEvent& event);

public:
limeGUIFrame(wxWindow* parent, const AppArgs& appArgs);

virtual ~limeGUIFrame();
static int m_lmsSelection;

protected:
static void OnGlobalLogEvent(const lime::LogLevel level, const std::string& message);
void OnLogMessage(wxCommandEvent& event);
void UpdateConnections(lime::SDRDevice* port);
Expand Down
2 changes: 1 addition & 1 deletion embedded/lms7002m/privates.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void lms7002m_sleep(long timeInMicroseconds)
void lms7002m_spi_write(lms7002m_context* self, uint16_t address, uint16_t value)
{
uint32_t mosi = address << 16 | value;
mosi |= 1 << 31;
mosi |= 0x80000000;
self->hooks.spi16_transact(&mosi, NULL, 1, self->hooks.spi16_userData);
}

Expand Down
38 changes: 35 additions & 3 deletions src/CommonFunctions.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
#pragma once

#include <string>
#include <sstream>
#include <iomanip>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>

#include "limesuiteng/types.h"

namespace lime {

const std::string strFormat(const char* format, ...);

template<typename T> std::string intToHex(T i)
template<typename T> std::string intToHex(T i, bool uppercase = false)
{
std::stringstream stream;

if (uppercase)
{
stream << std::uppercase;
}

stream << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << i;
return stream.str();
}

template<typename strView> std::vector<strView> SplitString(strView string, strView delimiter)
{
std::vector<strView> ret;

auto position{ string.find(delimiter) };

while (position != strView::npos)
{
if (position != 0)
{
ret.push_back(string.substr(0, position));
}

string = string.substr(position + delimiter.size());
position = string.find(delimiter);
}

if (string.size() > 0)
{
ret.push_back(string);
}
return ret;
}

} // namespace lime
2 changes: 2 additions & 0 deletions src/boards/DeviceFactoryFTDI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "LMS64C_FPGA_Over_USB.h"
#include "CommonFunctions.h"
#include "FT601/FT601.h"
#include "comms/USB/GlobalHotplugEvents.h"

using namespace lime;
using namespace std::literals::string_literals;
Expand All @@ -25,6 +26,7 @@ static const std::set<IUSB::VendorProductId> ids{ { 0x0403, 0x601F } };
DeviceFactoryFTDI::DeviceFactoryFTDI()
: DeviceRegistryEntry("FTDI"s)
{
GlobalHotplugEvents::AddVidPids(ids);
}

std::vector<DeviceHandle> DeviceFactoryFTDI::enumerate(const DeviceHandle& hint)
Expand Down
4 changes: 4 additions & 0 deletions src/boards/DeviceFactoryFX3.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "DeviceFactoryFX3.h"

#include <memory>
#include <set>
#include <string>
#include <string_view>

#include "limesuiteng/DeviceHandle.h"
Expand All @@ -10,6 +12,7 @@
#include "LMS64C_LMS7002M_Over_USB.h"
#include "LMS64C_FPGA_Over_USB.h"
#include "CommonFunctions.h"
#include "comms/USB/GlobalHotplugEvents.h"

using namespace lime;
using namespace std::literals::string_literals;
Expand All @@ -26,6 +29,7 @@ static const std::set<IUSB::VendorProductId> ids{ { 0x04B4, 0x00F1 }, { 0x04B4,
DeviceFactoryFX3::DeviceFactoryFX3()
: DeviceRegistryEntry("FX3"s)
{
GlobalHotplugEvents::AddVidPids(ids);
}

std::vector<DeviceHandle> DeviceFactoryFX3::enumerate(const DeviceHandle& hint)
Expand Down
10 changes: 10 additions & 0 deletions src/boards/LMS7002M_SDRDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,4 +1274,14 @@ OpStatus LMS7002M_SDRDevice::LMS7002TestSignalConfigure(LMS7002M* chip, const Ch
return OpStatus::Success;
}

std::size_t LMS7002M_SDRDevice::AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData)
{
return 0;
}

void LMS7002M_SDRDevice::RemoveHotplugDisconnectCallback(std::size_t id)
{
return;
}

} // namespace lime
3 changes: 3 additions & 0 deletions src/boards/LMS7002M_SDRDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ class LIME_API LMS7002M_SDRDevice : public SDRDevice
/// @copydoc FPGA::WriteRegister()
virtual OpStatus WriteFPGARegister(uint32_t address, uint32_t value);

std::size_t AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData) override;
void RemoveHotplugDisconnectCallback(std::size_t id) override;

protected:
static OpStatus UpdateFPGAInterfaceFrequency(LMS7002M& soc, FPGA& fpga, uint8_t chipIndex);
void SetGainInformationInDescriptor(RFSOCDescriptor& descriptor);
Expand Down
Loading