186 lines
5.7 KiB
C++
186 lines
5.7 KiB
C++
|
#include "IGScanManager.h"
|
||
|
#include <iostream>
|
||
|
#include "libusbex.h"
|
||
|
|
||
|
|
||
|
IGScanManager::IGScanManager():
|
||
|
brun(false)
|
||
|
{
|
||
|
#ifndef _WIN32
|
||
|
libusb_init(0);
|
||
|
//libusb_set_debug(nullptr,LIBUSB_LOG_LEVEL_DEBUG|LIBUSB_LOG_LEVEL_ERROR|LIBUSB_LOG_LEVEL_INFO);
|
||
|
#else
|
||
|
std::cout<<JSONPATH<<std::endl;
|
||
|
json tmpjs=jsonconfig::loadjson(JSONPATH);
|
||
|
if(tmpjs.is_object())
|
||
|
{
|
||
|
try{
|
||
|
supportdevice=jsonconfig::getdevicelist(tmpjs);
|
||
|
}
|
||
|
catch(...)
|
||
|
{
|
||
|
jsonconfig::savejson(JSONPATH,jsonconfig::defaultjson());
|
||
|
supportdevice=jsonconfig::getdevicelist(jsonconfig::defaultjson());
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
jsonconfig::savejson(JSONPATH,jsonconfig::defaultjson());
|
||
|
supportdevice=jsonconfig::getdevicelist(jsonconfig::defaultjson());
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
IGScanManager::~IGScanManager()
|
||
|
{
|
||
|
#ifndef _WIN32
|
||
|
libusb_hotplug_deregister_callback(nullptr, handle);
|
||
|
if(m_usbnotifythread.get()&&m_usbnotifythread->joinable())
|
||
|
{
|
||
|
brun=false;
|
||
|
m_usbnotifythread->join();
|
||
|
m_usbnotifythread.reset();
|
||
|
}
|
||
|
libusb_exit(0);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
|
||
|
std::vector<deviceinfo> IGScanManager::getonlinedevices()
|
||
|
{
|
||
|
#ifndef _WIN32
|
||
|
return m_devices;
|
||
|
#else
|
||
|
m_devices.clear();
|
||
|
auto usblist = UsbScan_List::find_all_usb();
|
||
|
for(auto it =usblist.begin();it!=usblist.end();it++)
|
||
|
for(auto i = 0;i<supportdevice.size();++i)
|
||
|
{
|
||
|
usb_scan_dev_info id =*it;
|
||
|
if(supportdevice[i].vid == id.vid &&supportdevice[i].pid == id.pid)
|
||
|
{
|
||
|
m_devices.push_back(supportdevice[i]);
|
||
|
}
|
||
|
}
|
||
|
return m_devices;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
#ifndef _WIN32
|
||
|
void IGScanManager::initnotifythread()
|
||
|
{
|
||
|
if(!m_usbnotifythread.get())
|
||
|
{
|
||
|
brun=true;
|
||
|
m_usbnotifythread.reset(new std::thread(&IGScanManager::usbnotifymain,this));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
void IGScanManager::usbnotifymain()
|
||
|
{
|
||
|
while(brun)
|
||
|
{
|
||
|
//if(m_usbscanner.get()&&m_usbscanner->IsConnected())
|
||
|
{
|
||
|
timeval tm={1,0};
|
||
|
//auto ret =libusb_handle_events_completed(nullptr,nullptr);
|
||
|
auto ret=libusb_handle_events_timeout(0,&tm);
|
||
|
if(ret<0)
|
||
|
printf("libusb_handle_events_timeout error %s\n",libusb_error_name(ret));
|
||
|
}
|
||
|
std::this_thread::sleep_for(chrono::milliseconds(10));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
int IGScanManager::OnUsbHotplugCallback(libusb_context *ctx, libusb_device *device, libusb_hotplug_event event, void *monitor)
|
||
|
{
|
||
|
IGScanManager* This = (IGScanManager*)monitor;
|
||
|
return This->OnUsbHotplug(ctx, device, event);
|
||
|
}
|
||
|
|
||
|
void IGScanManager::registehotplug()
|
||
|
{
|
||
|
auto ret = libusb_hotplug_register_callback(0,(libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
|
||
|
(libusb_hotplug_flag)LIBUSB_HOTPLUG_ENUMERATE,
|
||
|
LIBUSB_HOTPLUG_MATCH_ANY,//LIBUSB_HOTPLUG_MATCH_ANY
|
||
|
LIBUSB_HOTPLUG_MATCH_ANY,//LIBUSB_HOTPLUG_MATCH_ANY,
|
||
|
LIBUSB_HOTPLUG_MATCH_ANY,
|
||
|
OnUsbHotplugCallback,
|
||
|
this,
|
||
|
&handle);
|
||
|
if(ret!=LIBUSB_SUCCESS)
|
||
|
{
|
||
|
std::cout<<"WARRNNING register usb hotplug callback error"<<std::endl;
|
||
|
printf("regist usbhotplug callback error msg: %s \n",libusb_error_name(ret));
|
||
|
return;
|
||
|
}
|
||
|
initnotifythread();
|
||
|
}
|
||
|
int IGScanManager::OnUsbHotplug(libusb_context *ctx, libusb_device *device, libusb_hotplug_event event)
|
||
|
{
|
||
|
struct libusb_device_descriptor descriptor;
|
||
|
{
|
||
|
std::cout<<JSONPATH<<std::endl;
|
||
|
json tmpjs=jsonconfig::loadjson(JSONPATH);
|
||
|
if(tmpjs.is_object())
|
||
|
{
|
||
|
try{
|
||
|
supportdevice=jsonconfig::getdevicelist(tmpjs);
|
||
|
}
|
||
|
catch(...)
|
||
|
{
|
||
|
jsonconfig::savejson(JSONPATH,jsonconfig::defaultjson());
|
||
|
supportdevice=jsonconfig::getdevicelist(jsonconfig::defaultjson());
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
jsonconfig::savejson(JSONPATH,jsonconfig::defaultjson());
|
||
|
supportdevice=jsonconfig::getdevicelist(jsonconfig::defaultjson());
|
||
|
}
|
||
|
}
|
||
|
int ret = libusb_get_device_descriptor(device, &descriptor);
|
||
|
if (LIBUSB_SUCCESS == ret)
|
||
|
{
|
||
|
std::cout << "Got a device: vid= " << std::hex << descriptor.idVendor << ",pid=" << descriptor.idProduct << std::endl;
|
||
|
}
|
||
|
deviceinfo tmp;
|
||
|
if(std::find_if(supportdevice.begin(),supportdevice.end(),[&](deviceinfo info)->bool{
|
||
|
if((descriptor.idVendor == info.vid)&&(descriptor.idProduct == info.pid))
|
||
|
{
|
||
|
tmp = info;
|
||
|
return true;
|
||
|
}
|
||
|
std::cout<<"find_if vid= " << std::hex << descriptor.idVendor << ",pid=" << descriptor.idProduct << std::endl;
|
||
|
return false;
|
||
|
})==supportdevice.end())
|
||
|
return 0;
|
||
|
switch (event) {
|
||
|
case LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED:
|
||
|
m_devices.push_back(tmp);
|
||
|
std::cout << "USBScanner LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVEDvid = "<<tmp.vid<<" pid =" <<tmp.pid<< std::endl;
|
||
|
break;
|
||
|
case LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT:{
|
||
|
auto it= std::find_if(m_devices.begin(),m_devices.end(),[&](deviceinfo info)->bool{
|
||
|
if((tmp.vid == info.vid)&&(tmp.pid == info.pid))
|
||
|
return true;
|
||
|
return false;
|
||
|
});
|
||
|
if(it != m_devices.end())
|
||
|
m_devices.erase(it);
|
||
|
libusb_init(nullptr);
|
||
|
libusb_exit(nullptr);
|
||
|
std::cout << "USBScanner LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = "<<tmp.vid<<" pid =" <<tmp.pid<< std::endl;
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
#endif
|
||
|
|