provider.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include "main.h"
  2. #include "./provider.h"
  3. typedef struct DeviceProviderThreadParam
  4. {
  5. api_devicemanager *manager;
  6. DeviceProvider *provider;
  7. HANDLE readyEvent;
  8. }DeviceProviderThreadParam;
  9. DeviceProvider::DeviceProvider()
  10. : ref(1), discoveryThread(NULL), cancelEvent(NULL)
  11. {
  12. wchar_t buffer[MAX_PATH * 2];
  13. HINSTANCE module;
  14. TestSuiteLoader loader;
  15. InitializeCriticalSection(&lock);
  16. module = Plugin_GetInstance();
  17. if (0 == GetModuleFileName(module, buffer, ARRAYSIZE(buffer)) ||
  18. FALSE == PathRemoveFileSpec(buffer))
  19. {
  20. buffer[0] = L'\0';
  21. }
  22. PathAppend(buffer, L"testprovider.xml");
  23. loader.Load(buffer, &testSuite);
  24. }
  25. DeviceProvider::~DeviceProvider()
  26. {
  27. CancelDiscovery();
  28. DeleteCriticalSection(&lock);
  29. }
  30. HRESULT DeviceProvider::CreateInstance(DeviceProvider **instance)
  31. {
  32. if (NULL == instance)
  33. return E_POINTER;
  34. *instance = new DeviceProvider();
  35. if (NULL == *instance)
  36. return E_OUTOFMEMORY;
  37. return S_OK;
  38. }
  39. size_t DeviceProvider::AddRef()
  40. {
  41. return InterlockedIncrement((LONG*)&ref);
  42. }
  43. size_t DeviceProvider::Release()
  44. {
  45. if (0 == ref)
  46. return ref;
  47. LONG r = InterlockedDecrement((LONG*)&ref);
  48. if (0 == r)
  49. delete(this);
  50. return r;
  51. }
  52. int DeviceProvider::QueryInterface(GUID interface_guid, void **object)
  53. {
  54. if (NULL == object)
  55. return E_POINTER;
  56. if (IsEqualIID(interface_guid, IFC_DeviceProvider))
  57. *object = static_cast<ifc_deviceprovider*>(this);
  58. else
  59. {
  60. *object = NULL;
  61. return E_NOINTERFACE;
  62. }
  63. if (NULL == *object)
  64. return E_UNEXPECTED;
  65. AddRef();
  66. return S_OK;
  67. }
  68. DWORD DeviceProvider::DiscoveryThread(api_devicemanager *manager)
  69. {
  70. DWORD waitResult;
  71. DWORD deviceCount;
  72. DWORD threadId;
  73. DWORD sleepTime;
  74. LARGE_INTEGER perfCounter;
  75. deviceCount = 0;
  76. threadId = GetCurrentThreadId();
  77. // aTRACE_FMT("[test provider] device discovery started (0x%X).\r\n", threadId);
  78. manager->SetProviderActive(this, TRUE);
  79. for(;;)
  80. {
  81. if (FALSE != QueryPerformanceCounter(&perfCounter))
  82. srand(perfCounter.LowPart);
  83. else
  84. srand(GetTickCount());
  85. sleepTime = (DWORD)((double)rand()/(RAND_MAX) * 0);
  86. waitResult = WaitForSingleObject(cancelEvent, sleepTime);
  87. if (WAIT_OBJECT_0 == waitResult)
  88. break;
  89. else if (WAIT_TIMEOUT == waitResult)
  90. {
  91. Device *device;
  92. deviceCount++;
  93. // aTRACE_FMT("[test provider] creating new device[%d] (0x%X).\r\n", deviceCount, threadId);
  94. device = testSuite.GetRandomDevice();
  95. if (NULL != device)
  96. {
  97. device->Connect();
  98. if (0 == manager->DeviceRegister((ifc_device**)&device, 1))
  99. device->Disconnect();
  100. }
  101. if (4 == deviceCount)
  102. break;
  103. }
  104. else
  105. {
  106. // aTRACE_FMT("[test provider] error (0x%X).\r\n", threadId);
  107. break;
  108. }
  109. }
  110. EnterCriticalSection(&lock);
  111. if (NULL != discoveryThread)
  112. {
  113. CloseHandle(discoveryThread);
  114. discoveryThread = NULL;
  115. }
  116. if (NULL != cancelEvent)
  117. {
  118. CloseHandle(cancelEvent);
  119. cancelEvent = NULL;
  120. }
  121. // aTRACE_FMT("[test provider] device discovery finished (0x%X).\r\n", threadId);
  122. LeaveCriticalSection(&lock);
  123. manager->SetProviderActive(this, FALSE);
  124. return 0;
  125. }
  126. static DWORD CALLBACK DeviceProvider_DiscoveryThreadStarter(void *user)
  127. {
  128. DeviceProviderThreadParam *param;
  129. DeviceProvider *provider;
  130. api_devicemanager *manager;
  131. DWORD result;
  132. param = (DeviceProviderThreadParam*)user;
  133. manager = param->manager;
  134. provider = param->provider;
  135. if (NULL != manager)
  136. manager->AddRef();
  137. if (NULL != param->readyEvent)
  138. SetEvent(param->readyEvent);
  139. if (NULL == manager)
  140. return -1;
  141. if (NULL != provider)
  142. result = provider->DiscoveryThread(manager);
  143. else
  144. result = -2;
  145. manager->Release();
  146. return result;
  147. }
  148. HRESULT DeviceProvider::BeginDiscovery(api_devicemanager *manager)
  149. {
  150. HRESULT hr;
  151. if (NULL == manager)
  152. return E_INVALIDARG;
  153. EnterCriticalSection(&lock);
  154. if (NULL != discoveryThread)
  155. hr = E_PENDING;
  156. else
  157. {
  158. hr = S_OK;
  159. if (NULL == cancelEvent)
  160. {
  161. cancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  162. if (NULL == cancelEvent)
  163. hr = E_FAIL;
  164. }
  165. if (SUCCEEDED(hr))
  166. {
  167. DeviceProviderThreadParam param;
  168. param.provider = this;
  169. param.manager = manager;
  170. param.readyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  171. if (NULL == param.readyEvent)
  172. hr = E_FAIL;
  173. else
  174. {
  175. DWORD threadId;
  176. discoveryThread = CreateThread(NULL, 0, DeviceProvider_DiscoveryThreadStarter, &param, 0, &threadId);
  177. if (NULL == discoveryThread)
  178. hr = E_FAIL;
  179. else
  180. WaitForSingleObject(param.readyEvent, INFINITE);
  181. CloseHandle(param.readyEvent);
  182. }
  183. }
  184. if (FAILED(hr))
  185. CancelDiscovery();
  186. }
  187. LeaveCriticalSection(&lock);
  188. return hr;
  189. }
  190. HRESULT DeviceProvider::CancelDiscovery()
  191. {
  192. HRESULT hr;
  193. HANDLE threadHandle, eventHandle;
  194. EnterCriticalSection(&lock);
  195. threadHandle = discoveryThread;
  196. eventHandle = cancelEvent;
  197. discoveryThread = NULL;
  198. cancelEvent = NULL;
  199. LeaveCriticalSection(&lock);
  200. if (NULL != threadHandle)
  201. {
  202. if (NULL != eventHandle)
  203. SetEvent(eventHandle);
  204. WaitForSingleObject(threadHandle, INFINITE);
  205. CloseHandle(threadHandle);
  206. hr = S_OK;
  207. }
  208. else
  209. hr = S_FALSE;
  210. if (NULL != eventHandle)
  211. CloseHandle(eventHandle);
  212. return hr;
  213. }
  214. HRESULT DeviceProvider::GetActive()
  215. {
  216. HRESULT hr;
  217. EnterCriticalSection(&lock);
  218. hr = (NULL != discoveryThread) ? S_OK : S_FALSE;
  219. LeaveCriticalSection(&lock);
  220. return hr;
  221. }
  222. HRESULT DeviceProvider::Register(api_devicemanager *manager)
  223. {
  224. HRESULT hr;
  225. if (NULL == manager)
  226. return E_POINTER;
  227. hr = manager->RegisterProvider(this);
  228. if (SUCCEEDED(hr))
  229. {
  230. testSuite.RegisterCommands(manager);
  231. testSuite.RegisterTypes(manager);
  232. testSuite.RegisterConnections(manager);
  233. testSuite.RegisterDevices(manager);
  234. }
  235. return hr;
  236. }
  237. HRESULT DeviceProvider::Unregister(api_devicemanager *manager)
  238. {
  239. HRESULT hr;
  240. if (NULL == manager)
  241. return E_POINTER;
  242. hr = manager->UnregisterProvider(this);
  243. testSuite.UnregisterTypes(manager);
  244. testSuite.UnregisterConnections(manager);
  245. testSuite.UnregisterCommands(manager);
  246. testSuite.UnregisterDevices(manager);
  247. return hr;
  248. }
  249. #define CBCLASS DeviceProvider
  250. START_DISPATCH;
  251. CB(ADDREF, AddRef)
  252. CB(RELEASE, Release)
  253. CB(QUERYINTERFACE, QueryInterface)
  254. CB(API_BEGINDISCOVERY, BeginDiscovery)
  255. CB(API_CANCELDISCOVERY, CancelDiscovery)
  256. CB(API_GETACTIVE, GetActive)
  257. END_DISPATCH;
  258. #undef CBCLASS