//--------------------------------------------------------------------------- // USBDeviceManager.cpp // // SILAB, Phys. Inst Bonn, HK // // Handle multiple USB devices associated to slbusb.sys device driver // // History: // // 09 10 11 13 14 15 // 18.06.09 HK - removed DeviceTypeList (new SiUSBDeviceManager) // device type identification now with DeviceClass // 21.11.02 HK - changed HandleDeviceChange function: // does not stop and restart available devices anymore during // search loop // 22.02.02 PF - added FindDevice() // - added call to HandleDeviceChange() in constructor // - made DeviceList private // 03.10.01 HK created //------------------------------------------------------------------------------ #ifdef __VISUALC__ #include #endif #include #include "SiUSBDeviceManager.h" //------------------------------------------------------------------------------ TUSBDeviceManager::TUSBDeviceManager() { onDevicePlugged = NULL; onDeviceUnplugged = NULL; DeviceContext = NULL; busy = false; for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) // init device list { DeviceList[i].DevicePresent = false; DeviceList[i].Device = new TUSBDevice(i); } } //------------------------------------------------------------------------------ TUSBDeviceManager::~TUSBDeviceManager() { for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) // delete devices delete DeviceList[i].Device; } //------------------------------------------------------------------------------ void TUSBDeviceManager::SetAddCallBack(void (*addfunc) (TUSBDevice*, void*), void *cont) { onDevicePlugged = addfunc; DeviceContext = cont; } //------------------------------------------------------------------------------ void TUSBDeviceManager::SetRemoveCallBack(void (*remfunc) (TUSBDevice*, void*), void *cont) { onDeviceUnplugged = remfunc; DeviceContext = cont; } //------------------------------------------------------------------------------ bool TUSBDeviceManager::HandleDeviceChange(void) // called on WM_DEVICECHAN GE { bool something_changed = false; busy = true; for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) { if(!DeviceList[i].Device->DeviceAvailable()) // check if device is available { if (DeviceList[i].DevicePresent) // device removed, still listed { DeviceList[i].Device->StopDriver(); RemoveDeviceFromList(i); something_changed = true; #ifdef CF__LINUX printf("removed device\n"); #else OutputDebugStringA((const char*) "removed device\n"); #endif if ( onDeviceUnplugged != NULL) onDeviceUnplugged(DeviceList[i].Device, DeviceContext); } } else { if (!DeviceList[i].DevicePresent) // new device found, not listed yet { if (DeviceList[i].Device->StartDriver()) // start driver { AddDeviceToList(i); // add pointer to list something_changed = true; #ifdef CF__LINUX printf("added device\n"); #else OutputDebugStringA((const char*) "added device\n"); #endif if ( onDevicePlugged != NULL) onDevicePlugged(DeviceList[i].Device, DeviceContext); } else #ifdef CF__LINUX printf("ERROR: Couldn't start device\n"); #else OutputDebugStringA((const char*) "ERROR: Couldn't start device\n"); #endif } } } busy = false; // UpdateDevStringList(); #ifdef CF__LINUX if(SF_DEBUG_USB>0) printf("Called HandleDeviceChange()\n"); #else OutputDebugStringA((const char*) "Called HandleDeviceChange()\n"); #endif return something_changed; } //------------------------------------------------------------------------------ #ifdef CF__LINUX bool TUSBDeviceManager::HandleDeviceChange2(int device) // called on WM_DEVICECHAN GE { bool something_changed = false; busy = true; // usleep(300000); int USB_DEV_1=0; int USB_DEV_2=MAX_USB_DEV_NUMBER; if ( 0<=device && deviceDeviceAvailable()) // check if device is available { if (DeviceList[i].DevicePresent) // device removed, still listed { DeviceList[i].Device->StopDriver(); RemoveDeviceFromList(i); something_changed = true; if ( onDeviceUnplugged != NULL) onDeviceUnplugged(DeviceList[i].Device, DeviceContext); } } else { if (!DeviceList[i].DevicePresent) // new device found, not listed yet { if (DeviceList[i].Device->StartDriver()) // start driver { AddDeviceToList(i); // add pointer to list something_changed = true; if ( onDevicePlugged != NULL) onDevicePlugged(DeviceList[i].Device, DeviceContext); } else ShowMessage("Couldn't start device"); } } } busy = false; return something_changed; } #endif //------------------------------------------------------------------------------ bool TUSBDeviceManager::IsBusy() { return busy; } //------------------------------------------------------------------------------ void TUSBDeviceManager::ForceRefresh() { for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) { if (DeviceList[i].DevicePresent) // device removed, still listed { DeviceList[i].Device->StopDriver(); RemoveDeviceFromList(i); if ( onDeviceUnplugged != NULL) onDeviceUnplugged(DeviceList[i].Device, DeviceContext); } } #ifdef CF__LINUX usleep(300000); #else // Sleep(300); #endif HandleDeviceChange(); } //------------------------------------------------------------------------------ bool TUSBDeviceManager::AddDeviceToList(int index) { DeviceList[index].DevicePresent = true; /* TUSBDevice * USBDev = DeviceList[index].Device; for (int i = 0; i < (sizeof(DeviceTypeList)/sizeof(SLB_USB_DEVICE_TYPE)); i++) { if ((DeviceTypeList[i].VendorID == USBDev->Desc->idVendor) && (DeviceTypeList[i].ProductID == USBDev->Desc->idProduct)) { if (DeviceTypeList[i].FirmwareFilename != NULL) // unconfigured device { if (!USBDev->LoadFirmwareFromFile(DeviceTypeList[i].FirmwareFilename)) return false; } } } */ return true; } //------------------------------------------------------------------------------ bool TUSBDeviceManager::RemoveDeviceFromList(int index) { DeviceList[index].DevicePresent = false; // DeviceList[index].Device = NULL; return true; } //------------------------------------------------------------------------------ int TUSBDeviceManager::GetNumberofDevices() { int count = 0; for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) { if (DeviceList[i].DevicePresent) count++; } return count; } //------------------------------------------------------------------------------ int TUSBDeviceManager::GetMaxNumberofDevices() { return MAX_USB_DEV_NUMBER; } //------------------------------------------------------------------------------ void * TUSBDeviceManager::GetDevice(int Id) { for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) { if (Id == -1) // get first available device { if (DeviceList[i].DevicePresent) return DeviceList[i].Device; } if (DeviceList[i].Device->Id == Id && DeviceList[i].DevicePresent) return DeviceList[i].Device; } return NULL; } //------------------------------------------------------------------------------ /* void * TUSBDeviceManager::GetDevice(SLB_USB_DEVICE_TYPE_NAME Type) { for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) if (DeviceList[i].DevicePresent) if (DeviceList[i].DeviceTypeName == Type) return DeviceList[i].Device; return NULL; } */ //------------------------------------------------------------------------------ void * TUSBDeviceManager::GetDevice(int Id, int DevClass) { for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) if (DeviceList[i].DevicePresent) if (((Id == -1) || (DeviceList[i].Device->Id == Id)) && (DeviceList[i].Device->DeviceClass == DevClass)) return (void*)(DeviceList[i].Device); return NULL; } //------------------------------------------------------------------------------ int TUSBDeviceManager::DevToIndex(TUSBDevice *dev) { for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) if (DeviceList[i].Device == dev) return i; return -1; } //------------------------------------------------------------------------------ int TUSBDeviceManager::DevToId(TUSBDevice *dev) { for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) if (DeviceList[i].Device == dev) return DeviceList[i].Device->Id; return -1; } //------------------------------------------------------------------------------ int TUSBDeviceManager::UpdateDevStringList() { int count =0; std:: stringstream tmp; devInfoStrings.clear(); for (int i = 0; i < MAX_USB_DEV_NUMBER; i++) { if (DeviceList[i].DevicePresent) { count ++; tmp << (int)DeviceList[i].Device->GetId() << " " << " " << DeviceList[i].Device->GetName() << " " << " " << (int)DeviceList[i].Device->GetClass() << " " << (int)DeviceList[i].Device->GetFWVersion() << std::endl; //OutputDebugString((const char*) tmp.str().c_str()); } } return count; }