11#include " PacDriveSingleton.h"
2- #include " ../../../Log .h"
2+ #include " ../../../general/IOConfigurator .h"
33#include " ../../../general/StringExtensions.h"
44#include < string>
5- #include < algorithm>
65
76namespace DOF
87{
@@ -15,7 +14,7 @@ PacDriveSingleton& PacDriveSingleton::GetInstance()
1514
1615PacDriveSingleton::PacDriveSingleton ()
1716 : m_numDevices(0 )
18- , m_libusbContext (nullptr )
17+ , m_usbContext (nullptr )
1918{
2019 Initialize ();
2120}
@@ -24,13 +23,7 @@ PacDriveSingleton::~PacDriveSingleton() { Shutdown(); }
2423
2524void PacDriveSingleton::Initialize ()
2625{
27- int result = libusb_init (&m_libusbContext);
28- if (result < 0 )
29- {
30- Log::Exception (StringExtensions::Build (" Failed to initialize libusb: {0}" , std::to_string (result)));
31- return ;
32- }
33-
26+ m_usbContext = IOConfigurator::GetUSBContext ();
3427 EnumerateDevices ();
3528}
3629
@@ -56,12 +49,7 @@ void PacDriveSingleton::Shutdown()
5649 }
5750 }
5851 m_usbDevices.clear ();
59-
60- if (m_libusbContext)
61- {
62- libusb_exit (m_libusbContext);
63- m_libusbContext = nullptr ;
64- }
52+ m_usbContext = nullptr ;
6553 }
6654}
6755
@@ -151,8 +139,14 @@ void PacDriveSingleton::EnumerateDevices()
151139
152140 hid_free_enumeration (devices);
153141
142+ if (!m_usbContext)
143+ {
144+ m_numDevices = static_cast <int >(m_devices.size ());
145+ return ;
146+ }
147+
154148 libusb_device** usbDevices;
155- ssize_t deviceCount = libusb_get_device_list (m_libusbContext , &usbDevices);
149+ ssize_t deviceCount = libusb_get_device_list (m_usbContext , &usbDevices);
156150
157151 if (deviceCount > 0 )
158152 {
@@ -577,7 +571,10 @@ void PacDriveSingleton::OpenUsbDevice(int index)
577571 if (m_usbDevices.find (index) != m_usbDevices.end ())
578572 return ;
579573
580- libusb_device_handle* handle = libusb_open_device_with_vid_pid (m_libusbContext, device.vendorId , device.productId );
574+ if (!m_usbContext)
575+ return ;
576+
577+ libusb_device_handle* handle = libusb_open_device_with_vid_pid (m_usbContext, device.vendorId , device.productId );
581578 if (handle)
582579 {
583580 if (libusb_kernel_driver_active (handle, 0 ) == 1 )
0 commit comments