123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650 |
- #include "main.h"
- #include "./deviceManager.h"
- #include "./deviceEventManager.h"
- static void
- DeviceManager_ReleaseDispatchables(Dispatchable** buffer, size_t count)
- {
- if(NULL == buffer)
- return;
- while(count--)
- {
- Dispatchable *object = buffer[count];
- if (NULL != object)
- object->Release();
- }
- }
- DeviceManager::DeviceManager()
- : ref(1),
- #pragma warning(push)
- #pragma warning(disable:4355)
- typeStore(ObjectAddedCallback, ObjectRemovedCallback, this),
- connectionStore(ObjectAddedCallback, ObjectRemovedCallback, this),
- commandStore(ObjectAddedCallback, ObjectRemovedCallback, this),
- deviceStore(ObjectAddedCallback, ObjectRemovedCallback, this)
- #pragma warning(pop)
- {
- InitializeCriticalSection(&eventLock);
- InitializeCriticalSection(&providerLock);
- }
- DeviceManager::~DeviceManager()
- {
- EnterCriticalSection(&providerLock);
- DeviceManager_ReleaseDispatchables((Dispatchable**)(providerList.size() ? &providerList.at(0) : nullptr), providerList.size());
- providerList.clear();
- LeaveCriticalSection(&providerLock);
- DeleteCriticalSection(&providerLock);
- EnterCriticalSection(&eventLock);
- DeviceManager_ReleaseDispatchables((Dispatchable**)(eventList.size() ? &eventList.at(0) : nullptr), eventList.size());
- eventList.clear();
- LeaveCriticalSection(&eventLock);
- DeleteCriticalSection(&eventLock);
- }
- HRESULT DeviceManager::CreateInstance(DeviceManager **instance)
- {
- if (NULL == instance)
- return E_POINTER;
- *instance = new DeviceManager();
- if (NULL == *instance)
- return E_OUTOFMEMORY;
- return S_OK;
- }
- size_t DeviceManager::AddRef()
- {
- return InterlockedIncrement((LONG*)&ref);
- }
- size_t DeviceManager::Release()
- {
- if (0 == ref)
- return ref;
-
- LONG r = InterlockedDecrement((LONG*)&ref);
- if (0 == r)
- delete(this);
-
- return r;
- }
- int DeviceManager::QueryInterface(GUID interface_guid, void **object)
- {
- if (NULL == object) return E_POINTER;
-
- if (IsEqualIID(interface_guid, DeviceManagerGUID))
- *object = static_cast<api_devicemanager*>(this);
- else
- {
- *object = NULL;
- return E_NOINTERFACE;
- }
- if (NULL == *object)
- return E_UNEXPECTED;
- AddRef();
- return S_OK;
- }
- size_t DeviceManager::TypeRegister(ifc_devicetype **types, size_t count)
- {
- return typeStore.AddRange((ifc_deviceobject**)types, count);
- }
- size_t DeviceManager::TypeRegisterIndirect(const char **names, size_t count, DeviceTypeCreator callback, void *user)
- {
- return typeStore.AddIndirect(names, count, (DeviceObjectCreator)callback, user);
- }
- HRESULT DeviceManager::TypeUnregister(const char *name)
- {
- return typeStore.Remove(name);
- }
- HRESULT DeviceManager::TypeFind(const char *name, ifc_devicetype **type)
- {
- return typeStore.Find(name, (ifc_deviceobject**)type);
- }
- HRESULT DeviceManager::TypeEnumerate(ifc_deviceobjectenum **enumerator)
- {
- return typeStore.Enumerate((DeviceObjectEnum**)enumerator);
- }
- size_t DeviceManager::ConnectionRegister(ifc_deviceconnection **connections, size_t count)
- {
-
- return connectionStore.AddRange((ifc_deviceobject**)connections, count);
- }
- size_t DeviceManager::ConnectionRegisterIndirect(const char **names, size_t count, DeviceConnectionCreator callback, void *user)
- {
- return connectionStore.AddIndirect(names, count, (DeviceObjectCreator)callback, user);
- }
- HRESULT DeviceManager::ConnectionUnregister(const char *name)
- {
- return connectionStore.Remove(name);
- }
- HRESULT DeviceManager::ConnectionFind(const char *name, ifc_deviceconnection **connection)
- {
- return connectionStore.Find(name, (ifc_deviceobject**)connection);
- }
- HRESULT DeviceManager::ConnectionEnumerate(ifc_deviceobjectenum **enumerator)
- {
- return connectionStore.Enumerate((DeviceObjectEnum**)enumerator);
- }
- size_t DeviceManager::CommandRegister(ifc_devicecommand **commands, size_t count)
- {
- return commandStore.AddRange((ifc_deviceobject**)commands, count);
- }
- size_t DeviceManager::CommandRegisterIndirect(const char **names, size_t count, DeviceCommandCreator callback, void *user)
- {
- return commandStore.AddIndirect(names, count, (DeviceObjectCreator)callback, user);
- }
- HRESULT DeviceManager::CommandUnregister(const char *name)
- {
- return commandStore.Remove(name);
- }
- HRESULT DeviceManager::CommandFind(const char *name, ifc_devicecommand **command)
- {
- return commandStore.Find(name, (ifc_deviceobject**)command);
- }
- HRESULT DeviceManager::CommandEnumerate(ifc_deviceobjectenum **enumerator)
- {
- return commandStore.Enumerate((DeviceObjectEnum**)enumerator);
- }
- size_t DeviceManager::DeviceRegister(ifc_device **devices, size_t count)
- {
- return deviceStore.AddRange((ifc_deviceobject**)devices, count);
- }
- HRESULT DeviceManager::DeviceUnregister(const char *name)
- {
- return deviceStore.Remove(name);
- }
- HRESULT DeviceManager::DeviceFind(const char *name, ifc_device **device)
- {
- return deviceStore.Find(name, (ifc_deviceobject**)device);
- }
- HRESULT DeviceManager::DeviceEnumerate(ifc_deviceobjectenum **enumerator)
- {
- return deviceStore.Enumerate((DeviceObjectEnum**)enumerator);
- }
- HRESULT DeviceManager::IsDiscoveryActive()
- {
- return (FALSE != discoveryMonitor.IsActive()) ? S_OK : S_FALSE;
- }
- HRESULT DeviceManager::BeginDiscovery()
- {
- size_t index, started;
- EnterCriticalSection(&providerLock);
-
- started = 0;
- index = providerList.size();
- while(index--)
- {
- if (providerList[index] && SUCCEEDED(providerList[index]->BeginDiscovery((api_devicemanager*)this)))
- started++;
- }
-
- LeaveCriticalSection(&providerLock);
- return (0 != started) ? S_OK : E_FAIL;
- }
- HRESULT DeviceManager::CancelDiscovery()
- {
- size_t index;
- EnterCriticalSection(&providerLock);
-
- index = providerList.size();
- while(index--)
- {
- providerList[index]->CancelDiscovery();
- }
-
- LeaveCriticalSection(&providerLock);
- return S_OK;
- }
- HRESULT DeviceManager::SetProviderActive(ifc_deviceprovider *provider, BOOL activeState)
- {
-
- if (FALSE != activeState)
- {
- if (FALSE != discoveryMonitor.Register(provider))
- EventDiscoveryStarted();
- }
- else
- {
- if (FALSE != discoveryMonitor.Unregister(provider))
- EventDiscoveryFinished();
- }
-
- return S_OK;
- }
- HRESULT DeviceManager::RegisterProvider(ifc_deviceprovider *provider)
- {
- HRESULT hr;
- size_t index;
- if (NULL == provider)
- return E_INVALIDARG;
- hr = S_OK;
- EnterCriticalSection(&providerLock);
-
- index = providerList.size();
- while(index--)
- {
- if (provider == providerList[index])
- {
- hr = S_FALSE;
- break;
- }
- }
- if(S_OK == hr)
- {
- providerList.push_back(provider);
- provider->AddRef();
- SetProviderActive(provider, (S_OK == provider->GetActive()));
- }
-
- LeaveCriticalSection(&providerLock);
-
- return hr;
- }
- HRESULT DeviceManager::UnregisterProvider(ifc_deviceprovider *provider)
- {
- HRESULT hr;
- size_t index;
- if (NULL == provider)
- return E_INVALIDARG;
- hr = S_FALSE;
- EnterCriticalSection(&providerLock);
-
- index = providerList.size();
- while(index--)
- {
- if (provider == providerList[index])
- {
- ifc_deviceprovider *object;
- object = providerList[index];
- SetProviderActive(object, (S_OK == object->GetActive()));
- providerList.erase(providerList.begin() + index);
- object->Release();
- hr = S_OK;
- break;
- }
- }
-
- LeaveCriticalSection(&providerLock);
- return hr;
- }
- HRESULT DeviceManager::Advise(ifc_devicemanagerevent *handler)
- {
- HRESULT hr;
- size_t index;
- if (NULL == handler)
- return E_INVALIDARG;
- hr = S_OK;
- EnterCriticalSection(&eventLock);
-
- index = eventList.size();
- while(index--)
- {
- if (handler == eventList[index])
- {
- hr = S_FALSE;
- break;
- }
- }
- if(S_OK == hr)
- {
- eventList.push_back(handler);
- handler->AddRef();
- }
-
- LeaveCriticalSection(&eventLock);
- return hr;
- }
- HRESULT DeviceManager::Unadvise(ifc_devicemanagerevent *handler)
- {
- HRESULT hr;
- size_t index;
- if (NULL == handler)
- return E_INVALIDARG;
- hr = S_FALSE;
- EnterCriticalSection(&eventLock);
-
- index = eventList.size();
- while(index--)
- {
- if (handler == eventList[index])
- {
- ifc_devicemanagerevent *object;
- object = eventList[index];
- eventList.erase(eventList.begin() + index);
- object->Release();
- hr = S_OK;
- break;
- }
- }
-
- LeaveCriticalSection(&eventLock);
- return hr;
- }
- HRESULT DeviceManager::CreateDeviceEventManager(ifc_deviceeventmanager **eventManager)
- {
- return DeviceEventManager::CreateInstance(
- reinterpret_cast<DeviceEventManager**>(eventManager));
- }
- HRESULT DeviceManager::CreateSupportedCommandStore(ifc_devicesupportedcommandstore **store)
- {
- return DeviceSupportedCommandStore::CreateInstance(
- reinterpret_cast<DeviceSupportedCommandStore**>(store));
- }
- HRESULT DeviceManager::CreateSupportedCommandEnum(ifc_devicesupportedcommand **commands, size_t count, ifc_devicesupportedcommandenum **enumerator)
- {
- return DeviceSupportedCommandEnum::CreateInstance(commands, count,
- reinterpret_cast<DeviceSupportedCommandEnum**>(enumerator));
- }
- HRESULT DeviceManager::CreateIconStore(ifc_deviceiconstore **store)
- {
- return DeviceIconStore::CreateInstance(
- reinterpret_cast<DeviceIconStore**>(store));
- }
- HRESULT DeviceManager::CreateType(const char *name, ifc_devicetype **type)
- {
- return DeviceType::CreateInstance(name, reinterpret_cast<DeviceType**>(type));
- }
- HRESULT DeviceManager::CreateCommand(const char *name, ifc_devicecommand **command)
- {
- return DeviceCommand::CreateInstance(name, reinterpret_cast<DeviceCommand**>(command));
- }
- HRESULT DeviceManager::CreateConnection(const char *name, ifc_deviceconnection **connection)
- {
- return DeviceConnection::CreateInstance(name, reinterpret_cast<DeviceConnection**>(connection));
- }
- void
- DeviceManager::ObjectAddedCallback(DeviceObjectStore *store, ifc_deviceobject *object, void *userData)
- {
- DeviceManager *manager;
- manager = (DeviceManager*)userData;
- if (NULL == manager)
- return;
-
- if (store == &manager->typeStore)
- manager->EventTypeAdded((ifc_devicetype*)object);
- else if (store == &manager->connectionStore)
- manager->EventConnectionAdded((ifc_deviceconnection*)object);
- else if (store == &manager->commandStore)
- manager->EventCommandAdded((ifc_devicecommand*)object);
- else if (store == &manager->deviceStore)
- manager->EventDeviceAdded((ifc_device*)object);
- }
- void
- DeviceManager::ObjectRemovedCallback(DeviceObjectStore *store, ifc_deviceobject *object, void *userData)
- {
- DeviceManager *manager;
- manager = (DeviceManager*)userData;
- if (NULL == manager)
- return;
-
- if (store == &manager->typeStore)
- manager->EventTypeRemoved((ifc_devicetype*)object);
- else if (store == &manager->connectionStore)
- manager->EventConnectionRemoved((ifc_deviceconnection*)object);
- else if (store == &manager->commandStore)
- manager->EventCommandRemoved((ifc_devicecommand*)object);
- else if (store == &manager->deviceStore)
- manager->EventDeviceRemoved((ifc_device*)object);
- }
- void DeviceManager::EventTypeAdded(ifc_devicetype *type)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->TypeAdded((api_devicemanager*)this, type);
- }
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventTypeRemoved(ifc_devicetype *type)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->TypeRemoved((api_devicemanager*)this, type);
- }
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventConnectionAdded(ifc_deviceconnection *connection)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->ConnectionAdded((api_devicemanager*)this, connection);
- }
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventConnectionRemoved(ifc_deviceconnection *connection)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->ConnectionRemoved((api_devicemanager*)this, connection);
- }
-
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventCommandAdded(ifc_devicecommand *command)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->CommandAdded((api_devicemanager*)this, command);
- }
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventCommandRemoved(ifc_devicecommand *command)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->CommandRemoved((api_devicemanager*)this, command);
- }
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventDeviceAdded(ifc_device *device)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->DeviceAdded((api_devicemanager*)this, device);
- }
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventDeviceRemoved(ifc_device *device)
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->DeviceRemoved((api_devicemanager*)this, device);
- }
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventDiscoveryStarted()
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->DiscoveryStarted((api_devicemanager*)this);
- }
-
- LeaveCriticalSection(&eventLock);
- }
- void DeviceManager::EventDiscoveryFinished()
- {
- EnterCriticalSection(&eventLock);
- size_t index = eventList.size();
- while(index--)
- {
- ifc_devicemanagerevent *handler = eventList[index];
- handler->DiscoveryFinished((api_devicemanager*)this);
- }
-
- LeaveCriticalSection(&eventLock);
- }
- #define CBCLASS DeviceManager
- START_DISPATCH;
- CB(ADDREF, AddRef)
- CB(RELEASE, Release)
- CB(QUERYINTERFACE, QueryInterface)
- CB(API_TYPEREGISTER, TypeRegister)
- CB(API_TYPEREGISTERINDIRECT, TypeRegisterIndirect)
- CB(API_TYPEUNREGISTER, TypeUnregister)
- CB(API_TYPEFIND, TypeFind)
- CB(API_TYPEENUMERATE, TypeEnumerate)
- CB(API_CONNECTIONREGISTER, ConnectionRegister)
- CB(API_CONNECTIONREGISTERINDIRECT, ConnectionRegisterIndirect)
- CB(API_CONNECTIONUNREGISTER, ConnectionUnregister)
- CB(API_CONNECTIONFIND, ConnectionFind)
- CB(API_CONNECTIONENUMERATE, ConnectionEnumerate)
- CB(API_COMMANDREGISTER, CommandRegister)
- CB(API_COMMANDREGISTERINDIRECT, CommandRegisterIndirect)
- CB(API_COMMANDUNREGISTER, CommandUnregister)
- CB(API_COMMANDFIND, CommandFind)
- CB(API_COMMANDENUMERATE, CommandEnumerate)
- CB(API_DEVICEREGISTER, DeviceRegister)
- CB(API_DEVICEUNREGISTER, DeviceUnregister)
- CB(API_DEVICEFIND, DeviceFind)
- CB(API_DEVICEENUMERATE, DeviceEnumerate)
- CB(API_ISDISCOVERYACTIVE, IsDiscoveryActive)
- CB(API_BEGINDISCOVERY, BeginDiscovery)
- CB(API_CANCELDISCOVERY, CancelDiscovery)
- CB(API_REGISTERPROVIDER, RegisterProvider)
- CB(API_UNREGISTERPROVIDER, UnregisterProvider)
- CB(API_SETPROVIDERACTIVE, SetProviderActive)
- CB(API_ADVISE, Advise)
- CB(API_UNADVISE, Unadvise)
- CB(API_CREATEDEVICEEVENTMANAGER, CreateDeviceEventManager)
- CB(API_CREATESUPPORTEDCOMMANDSTORE, CreateSupportedCommandStore)
- CB(API_CREATESUPPORTEDCOMMANDENUM, CreateSupportedCommandEnum)
- CB(API_CREATEICONSTORE, CreateIconStore)
- CB(API_CREATETYPE, CreateType)
- CB(API_CREATECOMMAND, CreateCommand)
- CB(API_CREATECONNECTION, CreateConnection)
- END_DISPATCH;
- #undef CBCLASS
|