1
0

deviceManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #include "main.h"
  2. #include "./deviceManager.h"
  3. #include "./deviceEventManager.h"
  4. static void
  5. DeviceManager_ReleaseDispatchables(Dispatchable** buffer, size_t count)
  6. {
  7. if(NULL == buffer)
  8. return;
  9. while(count--)
  10. {
  11. Dispatchable *object = buffer[count];
  12. if (NULL != object)
  13. object->Release();
  14. }
  15. }
  16. DeviceManager::DeviceManager()
  17. : ref(1),
  18. #pragma warning(push)
  19. #pragma warning(disable:4355)
  20. typeStore(ObjectAddedCallback, ObjectRemovedCallback, this),
  21. connectionStore(ObjectAddedCallback, ObjectRemovedCallback, this),
  22. commandStore(ObjectAddedCallback, ObjectRemovedCallback, this),
  23. deviceStore(ObjectAddedCallback, ObjectRemovedCallback, this)
  24. #pragma warning(pop)
  25. {
  26. InitializeCriticalSection(&eventLock);
  27. InitializeCriticalSection(&providerLock);
  28. }
  29. DeviceManager::~DeviceManager()
  30. {
  31. EnterCriticalSection(&providerLock);
  32. DeviceManager_ReleaseDispatchables((Dispatchable**)(providerList.size() ? &providerList.at(0) : nullptr), providerList.size());
  33. providerList.clear();
  34. LeaveCriticalSection(&providerLock);
  35. DeleteCriticalSection(&providerLock);
  36. EnterCriticalSection(&eventLock);
  37. DeviceManager_ReleaseDispatchables((Dispatchable**)(eventList.size() ? &eventList.at(0) : nullptr), eventList.size());
  38. eventList.clear();
  39. LeaveCriticalSection(&eventLock);
  40. DeleteCriticalSection(&eventLock);
  41. }
  42. HRESULT DeviceManager::CreateInstance(DeviceManager **instance)
  43. {
  44. if (NULL == instance)
  45. return E_POINTER;
  46. *instance = new DeviceManager();
  47. if (NULL == *instance)
  48. return E_OUTOFMEMORY;
  49. return S_OK;
  50. }
  51. size_t DeviceManager::AddRef()
  52. {
  53. return InterlockedIncrement((LONG*)&ref);
  54. }
  55. size_t DeviceManager::Release()
  56. {
  57. if (0 == ref)
  58. return ref;
  59. LONG r = InterlockedDecrement((LONG*)&ref);
  60. if (0 == r)
  61. delete(this);
  62. return r;
  63. }
  64. int DeviceManager::QueryInterface(GUID interface_guid, void **object)
  65. {
  66. if (NULL == object) return E_POINTER;
  67. if (IsEqualIID(interface_guid, DeviceManagerGUID))
  68. *object = static_cast<api_devicemanager*>(this);
  69. else
  70. {
  71. *object = NULL;
  72. return E_NOINTERFACE;
  73. }
  74. if (NULL == *object)
  75. return E_UNEXPECTED;
  76. AddRef();
  77. return S_OK;
  78. }
  79. size_t DeviceManager::TypeRegister(ifc_devicetype **types, size_t count)
  80. {
  81. return typeStore.AddRange((ifc_deviceobject**)types, count);
  82. }
  83. size_t DeviceManager::TypeRegisterIndirect(const char **names, size_t count, DeviceTypeCreator callback, void *user)
  84. {
  85. return typeStore.AddIndirect(names, count, (DeviceObjectCreator)callback, user);
  86. }
  87. HRESULT DeviceManager::TypeUnregister(const char *name)
  88. {
  89. return typeStore.Remove(name);
  90. }
  91. HRESULT DeviceManager::TypeFind(const char *name, ifc_devicetype **type)
  92. {
  93. return typeStore.Find(name, (ifc_deviceobject**)type);
  94. }
  95. HRESULT DeviceManager::TypeEnumerate(ifc_deviceobjectenum **enumerator)
  96. {
  97. return typeStore.Enumerate((DeviceObjectEnum**)enumerator);
  98. }
  99. size_t DeviceManager::ConnectionRegister(ifc_deviceconnection **connections, size_t count)
  100. {
  101. return connectionStore.AddRange((ifc_deviceobject**)connections, count);
  102. }
  103. size_t DeviceManager::ConnectionRegisterIndirect(const char **names, size_t count, DeviceConnectionCreator callback, void *user)
  104. {
  105. return connectionStore.AddIndirect(names, count, (DeviceObjectCreator)callback, user);
  106. }
  107. HRESULT DeviceManager::ConnectionUnregister(const char *name)
  108. {
  109. return connectionStore.Remove(name);
  110. }
  111. HRESULT DeviceManager::ConnectionFind(const char *name, ifc_deviceconnection **connection)
  112. {
  113. return connectionStore.Find(name, (ifc_deviceobject**)connection);
  114. }
  115. HRESULT DeviceManager::ConnectionEnumerate(ifc_deviceobjectenum **enumerator)
  116. {
  117. return connectionStore.Enumerate((DeviceObjectEnum**)enumerator);
  118. }
  119. size_t DeviceManager::CommandRegister(ifc_devicecommand **commands, size_t count)
  120. {
  121. return commandStore.AddRange((ifc_deviceobject**)commands, count);
  122. }
  123. size_t DeviceManager::CommandRegisterIndirect(const char **names, size_t count, DeviceCommandCreator callback, void *user)
  124. {
  125. return commandStore.AddIndirect(names, count, (DeviceObjectCreator)callback, user);
  126. }
  127. HRESULT DeviceManager::CommandUnregister(const char *name)
  128. {
  129. return commandStore.Remove(name);
  130. }
  131. HRESULT DeviceManager::CommandFind(const char *name, ifc_devicecommand **command)
  132. {
  133. return commandStore.Find(name, (ifc_deviceobject**)command);
  134. }
  135. HRESULT DeviceManager::CommandEnumerate(ifc_deviceobjectenum **enumerator)
  136. {
  137. return commandStore.Enumerate((DeviceObjectEnum**)enumerator);
  138. }
  139. size_t DeviceManager::DeviceRegister(ifc_device **devices, size_t count)
  140. {
  141. return deviceStore.AddRange((ifc_deviceobject**)devices, count);
  142. }
  143. HRESULT DeviceManager::DeviceUnregister(const char *name)
  144. {
  145. return deviceStore.Remove(name);
  146. }
  147. HRESULT DeviceManager::DeviceFind(const char *name, ifc_device **device)
  148. {
  149. return deviceStore.Find(name, (ifc_deviceobject**)device);
  150. }
  151. HRESULT DeviceManager::DeviceEnumerate(ifc_deviceobjectenum **enumerator)
  152. {
  153. return deviceStore.Enumerate((DeviceObjectEnum**)enumerator);
  154. }
  155. HRESULT DeviceManager::IsDiscoveryActive()
  156. {
  157. return (FALSE != discoveryMonitor.IsActive()) ? S_OK : S_FALSE;
  158. }
  159. HRESULT DeviceManager::BeginDiscovery()
  160. {
  161. size_t index, started;
  162. EnterCriticalSection(&providerLock);
  163. started = 0;
  164. index = providerList.size();
  165. while(index--)
  166. {
  167. if (providerList[index] && SUCCEEDED(providerList[index]->BeginDiscovery((api_devicemanager*)this)))
  168. started++;
  169. }
  170. LeaveCriticalSection(&providerLock);
  171. return (0 != started) ? S_OK : E_FAIL;
  172. }
  173. HRESULT DeviceManager::CancelDiscovery()
  174. {
  175. size_t index;
  176. EnterCriticalSection(&providerLock);
  177. index = providerList.size();
  178. while(index--)
  179. {
  180. providerList[index]->CancelDiscovery();
  181. }
  182. LeaveCriticalSection(&providerLock);
  183. return S_OK;
  184. }
  185. HRESULT DeviceManager::SetProviderActive(ifc_deviceprovider *provider, BOOL activeState)
  186. {
  187. if (FALSE != activeState)
  188. {
  189. if (FALSE != discoveryMonitor.Register(provider))
  190. EventDiscoveryStarted();
  191. }
  192. else
  193. {
  194. if (FALSE != discoveryMonitor.Unregister(provider))
  195. EventDiscoveryFinished();
  196. }
  197. return S_OK;
  198. }
  199. HRESULT DeviceManager::RegisterProvider(ifc_deviceprovider *provider)
  200. {
  201. HRESULT hr;
  202. size_t index;
  203. if (NULL == provider)
  204. return E_INVALIDARG;
  205. hr = S_OK;
  206. EnterCriticalSection(&providerLock);
  207. index = providerList.size();
  208. while(index--)
  209. {
  210. if (provider == providerList[index])
  211. {
  212. hr = S_FALSE;
  213. break;
  214. }
  215. }
  216. if(S_OK == hr)
  217. {
  218. providerList.push_back(provider);
  219. provider->AddRef();
  220. SetProviderActive(provider, (S_OK == provider->GetActive()));
  221. }
  222. LeaveCriticalSection(&providerLock);
  223. return hr;
  224. }
  225. HRESULT DeviceManager::UnregisterProvider(ifc_deviceprovider *provider)
  226. {
  227. HRESULT hr;
  228. size_t index;
  229. if (NULL == provider)
  230. return E_INVALIDARG;
  231. hr = S_FALSE;
  232. EnterCriticalSection(&providerLock);
  233. index = providerList.size();
  234. while(index--)
  235. {
  236. if (provider == providerList[index])
  237. {
  238. ifc_deviceprovider *object;
  239. object = providerList[index];
  240. SetProviderActive(object, (S_OK == object->GetActive()));
  241. providerList.erase(providerList.begin() + index);
  242. object->Release();
  243. hr = S_OK;
  244. break;
  245. }
  246. }
  247. LeaveCriticalSection(&providerLock);
  248. return hr;
  249. }
  250. HRESULT DeviceManager::Advise(ifc_devicemanagerevent *handler)
  251. {
  252. HRESULT hr;
  253. size_t index;
  254. if (NULL == handler)
  255. return E_INVALIDARG;
  256. hr = S_OK;
  257. EnterCriticalSection(&eventLock);
  258. index = eventList.size();
  259. while(index--)
  260. {
  261. if (handler == eventList[index])
  262. {
  263. hr = S_FALSE;
  264. break;
  265. }
  266. }
  267. if(S_OK == hr)
  268. {
  269. eventList.push_back(handler);
  270. handler->AddRef();
  271. }
  272. LeaveCriticalSection(&eventLock);
  273. return hr;
  274. }
  275. HRESULT DeviceManager::Unadvise(ifc_devicemanagerevent *handler)
  276. {
  277. HRESULT hr;
  278. size_t index;
  279. if (NULL == handler)
  280. return E_INVALIDARG;
  281. hr = S_FALSE;
  282. EnterCriticalSection(&eventLock);
  283. index = eventList.size();
  284. while(index--)
  285. {
  286. if (handler == eventList[index])
  287. {
  288. ifc_devicemanagerevent *object;
  289. object = eventList[index];
  290. eventList.erase(eventList.begin() + index);
  291. object->Release();
  292. hr = S_OK;
  293. break;
  294. }
  295. }
  296. LeaveCriticalSection(&eventLock);
  297. return hr;
  298. }
  299. HRESULT DeviceManager::CreateDeviceEventManager(ifc_deviceeventmanager **eventManager)
  300. {
  301. return DeviceEventManager::CreateInstance(
  302. reinterpret_cast<DeviceEventManager**>(eventManager));
  303. }
  304. HRESULT DeviceManager::CreateSupportedCommandStore(ifc_devicesupportedcommandstore **store)
  305. {
  306. return DeviceSupportedCommandStore::CreateInstance(
  307. reinterpret_cast<DeviceSupportedCommandStore**>(store));
  308. }
  309. HRESULT DeviceManager::CreateSupportedCommandEnum(ifc_devicesupportedcommand **commands, size_t count, ifc_devicesupportedcommandenum **enumerator)
  310. {
  311. return DeviceSupportedCommandEnum::CreateInstance(commands, count,
  312. reinterpret_cast<DeviceSupportedCommandEnum**>(enumerator));
  313. }
  314. HRESULT DeviceManager::CreateIconStore(ifc_deviceiconstore **store)
  315. {
  316. return DeviceIconStore::CreateInstance(
  317. reinterpret_cast<DeviceIconStore**>(store));
  318. }
  319. HRESULT DeviceManager::CreateType(const char *name, ifc_devicetype **type)
  320. {
  321. return DeviceType::CreateInstance(name, reinterpret_cast<DeviceType**>(type));
  322. }
  323. HRESULT DeviceManager::CreateCommand(const char *name, ifc_devicecommand **command)
  324. {
  325. return DeviceCommand::CreateInstance(name, reinterpret_cast<DeviceCommand**>(command));
  326. }
  327. HRESULT DeviceManager::CreateConnection(const char *name, ifc_deviceconnection **connection)
  328. {
  329. return DeviceConnection::CreateInstance(name, reinterpret_cast<DeviceConnection**>(connection));
  330. }
  331. void
  332. DeviceManager::ObjectAddedCallback(DeviceObjectStore *store, ifc_deviceobject *object, void *userData)
  333. {
  334. DeviceManager *manager;
  335. manager = (DeviceManager*)userData;
  336. if (NULL == manager)
  337. return;
  338. if (store == &manager->typeStore)
  339. manager->EventTypeAdded((ifc_devicetype*)object);
  340. else if (store == &manager->connectionStore)
  341. manager->EventConnectionAdded((ifc_deviceconnection*)object);
  342. else if (store == &manager->commandStore)
  343. manager->EventCommandAdded((ifc_devicecommand*)object);
  344. else if (store == &manager->deviceStore)
  345. manager->EventDeviceAdded((ifc_device*)object);
  346. }
  347. void
  348. DeviceManager::ObjectRemovedCallback(DeviceObjectStore *store, ifc_deviceobject *object, void *userData)
  349. {
  350. DeviceManager *manager;
  351. manager = (DeviceManager*)userData;
  352. if (NULL == manager)
  353. return;
  354. if (store == &manager->typeStore)
  355. manager->EventTypeRemoved((ifc_devicetype*)object);
  356. else if (store == &manager->connectionStore)
  357. manager->EventConnectionRemoved((ifc_deviceconnection*)object);
  358. else if (store == &manager->commandStore)
  359. manager->EventCommandRemoved((ifc_devicecommand*)object);
  360. else if (store == &manager->deviceStore)
  361. manager->EventDeviceRemoved((ifc_device*)object);
  362. }
  363. void DeviceManager::EventTypeAdded(ifc_devicetype *type)
  364. {
  365. EnterCriticalSection(&eventLock);
  366. size_t index = eventList.size();
  367. while(index--)
  368. {
  369. ifc_devicemanagerevent *handler = eventList[index];
  370. handler->TypeAdded((api_devicemanager*)this, type);
  371. }
  372. LeaveCriticalSection(&eventLock);
  373. }
  374. void DeviceManager::EventTypeRemoved(ifc_devicetype *type)
  375. {
  376. EnterCriticalSection(&eventLock);
  377. size_t index = eventList.size();
  378. while(index--)
  379. {
  380. ifc_devicemanagerevent *handler = eventList[index];
  381. handler->TypeRemoved((api_devicemanager*)this, type);
  382. }
  383. LeaveCriticalSection(&eventLock);
  384. }
  385. void DeviceManager::EventConnectionAdded(ifc_deviceconnection *connection)
  386. {
  387. EnterCriticalSection(&eventLock);
  388. size_t index = eventList.size();
  389. while(index--)
  390. {
  391. ifc_devicemanagerevent *handler = eventList[index];
  392. handler->ConnectionAdded((api_devicemanager*)this, connection);
  393. }
  394. LeaveCriticalSection(&eventLock);
  395. }
  396. void DeviceManager::EventConnectionRemoved(ifc_deviceconnection *connection)
  397. {
  398. EnterCriticalSection(&eventLock);
  399. size_t index = eventList.size();
  400. while(index--)
  401. {
  402. ifc_devicemanagerevent *handler = eventList[index];
  403. handler->ConnectionRemoved((api_devicemanager*)this, connection);
  404. }
  405. LeaveCriticalSection(&eventLock);
  406. }
  407. void DeviceManager::EventCommandAdded(ifc_devicecommand *command)
  408. {
  409. EnterCriticalSection(&eventLock);
  410. size_t index = eventList.size();
  411. while(index--)
  412. {
  413. ifc_devicemanagerevent *handler = eventList[index];
  414. handler->CommandAdded((api_devicemanager*)this, command);
  415. }
  416. LeaveCriticalSection(&eventLock);
  417. }
  418. void DeviceManager::EventCommandRemoved(ifc_devicecommand *command)
  419. {
  420. EnterCriticalSection(&eventLock);
  421. size_t index = eventList.size();
  422. while(index--)
  423. {
  424. ifc_devicemanagerevent *handler = eventList[index];
  425. handler->CommandRemoved((api_devicemanager*)this, command);
  426. }
  427. LeaveCriticalSection(&eventLock);
  428. }
  429. void DeviceManager::EventDeviceAdded(ifc_device *device)
  430. {
  431. EnterCriticalSection(&eventLock);
  432. size_t index = eventList.size();
  433. while(index--)
  434. {
  435. ifc_devicemanagerevent *handler = eventList[index];
  436. handler->DeviceAdded((api_devicemanager*)this, device);
  437. }
  438. LeaveCriticalSection(&eventLock);
  439. }
  440. void DeviceManager::EventDeviceRemoved(ifc_device *device)
  441. {
  442. EnterCriticalSection(&eventLock);
  443. size_t index = eventList.size();
  444. while(index--)
  445. {
  446. ifc_devicemanagerevent *handler = eventList[index];
  447. handler->DeviceRemoved((api_devicemanager*)this, device);
  448. }
  449. LeaveCriticalSection(&eventLock);
  450. }
  451. void DeviceManager::EventDiscoveryStarted()
  452. {
  453. EnterCriticalSection(&eventLock);
  454. size_t index = eventList.size();
  455. while(index--)
  456. {
  457. ifc_devicemanagerevent *handler = eventList[index];
  458. handler->DiscoveryStarted((api_devicemanager*)this);
  459. }
  460. LeaveCriticalSection(&eventLock);
  461. }
  462. void DeviceManager::EventDiscoveryFinished()
  463. {
  464. EnterCriticalSection(&eventLock);
  465. size_t index = eventList.size();
  466. while(index--)
  467. {
  468. ifc_devicemanagerevent *handler = eventList[index];
  469. handler->DiscoveryFinished((api_devicemanager*)this);
  470. }
  471. LeaveCriticalSection(&eventLock);
  472. }
  473. #define CBCLASS DeviceManager
  474. START_DISPATCH;
  475. CB(ADDREF, AddRef)
  476. CB(RELEASE, Release)
  477. CB(QUERYINTERFACE, QueryInterface)
  478. CB(API_TYPEREGISTER, TypeRegister)
  479. CB(API_TYPEREGISTERINDIRECT, TypeRegisterIndirect)
  480. CB(API_TYPEUNREGISTER, TypeUnregister)
  481. CB(API_TYPEFIND, TypeFind)
  482. CB(API_TYPEENUMERATE, TypeEnumerate)
  483. CB(API_CONNECTIONREGISTER, ConnectionRegister)
  484. CB(API_CONNECTIONREGISTERINDIRECT, ConnectionRegisterIndirect)
  485. CB(API_CONNECTIONUNREGISTER, ConnectionUnregister)
  486. CB(API_CONNECTIONFIND, ConnectionFind)
  487. CB(API_CONNECTIONENUMERATE, ConnectionEnumerate)
  488. CB(API_COMMANDREGISTER, CommandRegister)
  489. CB(API_COMMANDREGISTERINDIRECT, CommandRegisterIndirect)
  490. CB(API_COMMANDUNREGISTER, CommandUnregister)
  491. CB(API_COMMANDFIND, CommandFind)
  492. CB(API_COMMANDENUMERATE, CommandEnumerate)
  493. CB(API_DEVICEREGISTER, DeviceRegister)
  494. CB(API_DEVICEUNREGISTER, DeviceUnregister)
  495. CB(API_DEVICEFIND, DeviceFind)
  496. CB(API_DEVICEENUMERATE, DeviceEnumerate)
  497. CB(API_ISDISCOVERYACTIVE, IsDiscoveryActive)
  498. CB(API_BEGINDISCOVERY, BeginDiscovery)
  499. CB(API_CANCELDISCOVERY, CancelDiscovery)
  500. CB(API_REGISTERPROVIDER, RegisterProvider)
  501. CB(API_UNREGISTERPROVIDER, UnregisterProvider)
  502. CB(API_SETPROVIDERACTIVE, SetProviderActive)
  503. CB(API_ADVISE, Advise)
  504. CB(API_UNADVISE, Unadvise)
  505. CB(API_CREATEDEVICEEVENTMANAGER, CreateDeviceEventManager)
  506. CB(API_CREATESUPPORTEDCOMMANDSTORE, CreateSupportedCommandStore)
  507. CB(API_CREATESUPPORTEDCOMMANDENUM, CreateSupportedCommandEnum)
  508. CB(API_CREATEICONSTORE, CreateIconStore)
  509. CB(API_CREATETYPE, CreateType)
  510. CB(API_CREATECOMMAND, CreateCommand)
  511. CB(API_CREATECONNECTION, CreateConnection)
  512. END_DISPATCH;
  513. #undef CBCLASS