Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 lines
11 KiB

  1. /*++
  2. Copyright (C) 2000 Microsoft Corporation
  3. Module Name:
  4. ptp.cpp
  5. Abstract:
  6. Program for testing the PTP camera library without using WIA
  7. Author:
  8. DavePar
  9. Revision History:
  10. --*/
  11. #include <windows.h>
  12. #include <stddef.h>
  13. #include <tchar.h>
  14. #include <objbase.h>
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <usbscan.h>
  18. #include "wiatempl.h"
  19. #include "iso15740.h"
  20. #include "camera.h"
  21. #include "camusb.h"
  22. // Software Tracing goo
  23. // {9A716C69-7A13-437c-B4EC-C6D1CAAFBCD9}
  24. #define WPP_CONTROL_GUIDS \
  25. WPP_DEFINE_CONTROL_GUID(Regular,(9A716C69,7A13,437c,B4EC,C6D1CAAFBCD9), \
  26. WPP_DEFINE_BIT(Entry) \
  27. WPP_DEFINE_BIT(Exit) \
  28. WPP_DEFINE_BIT(Error) \
  29. WPP_DEFINE_BIT(Unusual) \
  30. WPP_DEFINE_BIT(Noise) \
  31. ) \
  32. #include <ptp.tmh>
  33. #define MAX_SIZE 255
  34. HRESULT EventCallback(LPVOID pCallbackParam, PPTP_EVENT pEvent)
  35. {
  36. if (pEvent)
  37. printf("EventCallback called, event = 0x%08x\n", pEvent->EventCode);
  38. else
  39. printf("EventCallback initialized\n");
  40. return S_OK;
  41. }
  42. HRESULT DataCallback(LPVOID pCallbackParam, LONG lPercentComplete,
  43. LONG lOffset, LONG lLength, BYTE **ppBuffer, LONG *plBufferSize)
  44. {
  45. printf("DataCallback called, %3d percent complete, %5d bytes transferred\n", lPercentComplete, lLength);
  46. return S_OK;
  47. }
  48. void PrintCommands()
  49. {
  50. printf("Valid commands:\n");
  51. printf(" op <#> - open handle to device named Usbscan<#>\n");
  52. printf(" gd - get DeviceInfo from device\n");
  53. printf(" os - OpenSession\n");
  54. printf(" cs - CloseSession\n");
  55. printf(" gs - GetStorageIDs\n");
  56. printf(" gsi <id> - GetStorageInfo for StoreID <id>\n");
  57. printf(" gon - GetNumObjects\n");
  58. printf(" goh - GetObjectHandles\n");
  59. printf(" goi <h> - GetObjectInfo for ObjectHandle <h>\n");
  60. printf(" go <h> - GetObject for ObjectHandle <h>\n");
  61. printf(" gt <h> - GetThumb for ObjectHandle <h>\n");
  62. printf(" do <h> - DeleteObject for ObjectHandle <h>\n");
  63. printf(" gpd <pc> - GetDevicePropDesc for PropCode <pc>\n");
  64. printf(" rs - ResetDevice\n");
  65. printf(" tp <f> - TakePicture in format <f>\n");
  66. printf(" gds - GetDeviceStatus (only valid for USB)\n");
  67. printf(" ex\n\n");
  68. }
  69. int __cdecl main(int argc, CHAR *argv[], CHAR *envp[])
  70. {
  71. WPP_INIT_TRACING(L"PTPTest");
  72. printf("\nPTP Camera Test Program\n");
  73. printf("Version 0.5\n");
  74. printf("July 28, 2000\n\n");
  75. HRESULT hr = S_OK;
  76. BOOL bSessionOpen = FALSE;
  77. char szCommand[MAX_SIZE];
  78. char *szToken;
  79. char szWhiteSpace[] = " \t";
  80. DoTraceMessage(Noise, "Entering main function\n");
  81. //
  82. // Create an USB camera object
  83. //
  84. CPTPCamera *pCamera = new CUsbCamera;
  85. while (true)
  86. {
  87. printf("Enter command: ");
  88. if (!gets(szCommand))
  89. {
  90. break;
  91. }
  92. printf("\n");
  93. szToken = strtok(szCommand, szWhiteSpace);
  94. if (!szToken)
  95. {
  96. printf("ERROR: Invalid command\n");
  97. PrintCommands();
  98. continue;
  99. }
  100. //
  101. // Interpret command
  102. //
  103. if (strcmp(szToken, "ex") == 0)
  104. {
  105. break;
  106. }
  107. if (strcmp(szToken, "help") == 0 ||
  108. strcmp(szToken, "?") == 0)
  109. {
  110. PrintCommands();
  111. continue;
  112. }
  113. else if (strcmp(szToken, "op") == 0)
  114. {
  115. szToken = strtok(NULL, szWhiteSpace);
  116. if (!szToken)
  117. {
  118. printf("ERROR: Open needs a Usbscan number--look at CreateFileName registry key\n\n");
  119. continue;
  120. }
  121. WCHAR scratch1[MAX_SIZE];
  122. WCHAR scratch2[MAX_SIZE];
  123. mbstowcs(scratch1, szToken, MAX_SIZE);
  124. wcscpy(scratch2, L"\\\\.\\Usbscan");
  125. wcscat(scratch2, scratch1);
  126. printf("Opening device %S\n\n", scratch2);
  127. hr = pCamera->Open(scratch2, EventCallback, DataCallback, NULL);
  128. }
  129. else if (strcmp(szToken, "gd") == 0)
  130. {
  131. CPtpDeviceInfo DeviceInfo;
  132. hr = pCamera->GetDeviceInfo(&DeviceInfo);
  133. }
  134. else if (strcmp(szToken, "os") == 0)
  135. {
  136. ULONG sessionId;
  137. szToken = strtok(NULL, szWhiteSpace);
  138. if (!szToken)
  139. {
  140. printf("ERROR: OpenSession needs a session number\n\n");
  141. continue;
  142. }
  143. sscanf(szToken, "%lu", &sessionId);
  144. if (sessionId == 0)
  145. {
  146. printf("ERROR: OpenSession needs a non-zero session number\n\n");
  147. continue;
  148. }
  149. printf("Opening session %d\n\n", sessionId);
  150. hr = pCamera->OpenSession(sessionId);
  151. if (SUCCEEDED(hr))
  152. bSessionOpen = TRUE;
  153. }
  154. else if (strcmp(szToken, "cs") == 0)
  155. {
  156. printf("Closing session\n\n");
  157. hr = pCamera->CloseSession();
  158. }
  159. else if (strcmp(szToken, "gs") == 0)
  160. {
  161. CArray32 StorageIds;
  162. printf("Getting storage ids\n\n");
  163. hr = pCamera->GetStorageIDs(&StorageIds);
  164. }
  165. else if (strcmp(szToken, "gsi") == 0)
  166. {
  167. ULONG StorageId;
  168. CPtpStorageInfo StorageInfo;
  169. szToken = strtok(NULL, szWhiteSpace);
  170. if (!szToken)
  171. {
  172. printf("ERROR: GetStorageInfo needs a StorageID\n\n");
  173. continue;
  174. }
  175. sscanf(szToken, "%li", &StorageId);
  176. printf("Getting storage info for storage id 0x%08x\n\n", StorageId);
  177. hr = pCamera->GetStorageInfo(StorageId, &StorageInfo);
  178. }
  179. else if (strcmp(szToken, "gon") == 0)
  180. {
  181. UINT NumObjects;
  182. printf("Getting number of objects\n\n");
  183. hr = pCamera->GetNumObjects(PTP_STORAGEID_ALL, PTP_FORMATCODE_ALL, PTP_OBJECTHANDLE_ALL, &NumObjects);
  184. }
  185. else if (strcmp(szToken, "goh") == 0)
  186. {
  187. CArray32 ObjectIds;
  188. printf("Getting object ids\n\n");
  189. hr = pCamera->GetObjectHandles(PTP_STORAGEID_ALL, PTP_FORMATCODE_ALL, PTP_OBJECTHANDLE_ALL, &ObjectIds);
  190. }
  191. else if (strcmp(szToken, "goi") == 0)
  192. {
  193. DWORD ObjectHandle;
  194. CPtpObjectInfo ObjectInfo;
  195. szToken = strtok(NULL, szWhiteSpace);
  196. if (!szToken)
  197. {
  198. printf("ERROR: GetObjectInfo needs an ObjectHandle\n\n");
  199. continue;
  200. }
  201. sscanf(szToken, "%li", &ObjectHandle);
  202. printf("Getting object info for object handle 0x%08x\n\n", ObjectHandle);
  203. hr = pCamera->GetObjectInfo(ObjectHandle, &ObjectInfo);
  204. }
  205. else if (strcmp(szToken, "go") == 0)
  206. {
  207. DWORD ObjectHandle;
  208. szToken = strtok(NULL, szWhiteSpace);
  209. if (!szToken)
  210. {
  211. printf("ERROR: GetObject needs an ObjectHandle\n\n");
  212. continue;
  213. }
  214. sscanf(szToken, "%li", &ObjectHandle);
  215. printf("Getting object for object handle 0x%08x\n\n", ObjectHandle);
  216. UINT BufferSize = 32 * 1024;
  217. BYTE *pBuffer = new BYTE[BufferSize];
  218. if (!pBuffer)
  219. {
  220. printf("ERROR: Couldn't allocate the buffer\n\n");
  221. continue;
  222. }
  223. hr = pCamera->GetObjectData(ObjectHandle, pBuffer, &BufferSize, NULL);
  224. delete []pBuffer;
  225. }
  226. else if (strcmp(szToken, "gt") == 0)
  227. {
  228. DWORD ObjectHandle;
  229. szToken = strtok(NULL, szWhiteSpace);
  230. if (!szToken)
  231. {
  232. printf("ERROR: GetThumb needs an ObjectHandle\n\n");
  233. continue;
  234. }
  235. sscanf(szToken, "%li", &ObjectHandle);
  236. printf("Getting thumbnail for object handle 0x%08x\n\n", ObjectHandle);
  237. UINT BufferSize = 16 * 1024;
  238. BYTE *pBuffer = new BYTE[BufferSize];
  239. if (!pBuffer)
  240. {
  241. printf("ERROR: Couldn't allocate the buffer\n\n");
  242. continue;
  243. }
  244. hr = pCamera->GetThumb(ObjectHandle, pBuffer, &BufferSize);
  245. delete []pBuffer;
  246. }
  247. else if (strcmp(szToken, "do") == 0)
  248. {
  249. DWORD ObjectHandle;
  250. szToken = strtok(NULL, szWhiteSpace);
  251. if (!szToken)
  252. {
  253. printf("ERROR: DeleteObject needs an ObjectHandle\n\n");
  254. continue;
  255. }
  256. sscanf(szToken, "%li", &ObjectHandle);
  257. printf("Deleting object for object handle 0x%08x\n\n", ObjectHandle);
  258. hr = pCamera->DeleteObject(ObjectHandle, PTP_FORMATCODE_NOTUSED);
  259. }
  260. else if (strcmp(szToken, "gpd") == 0)
  261. {
  262. WORD PropCode;
  263. CPtpPropDesc PropDesc;
  264. szToken = strtok(NULL, szWhiteSpace);
  265. if (!szToken)
  266. {
  267. printf("ERROR: GetDevicePropDesc needs a PropCode\n\n");
  268. continue;
  269. }
  270. sscanf(szToken, "%i", &PropCode);
  271. printf("Getting property description for prop code 0x%04x\n\n", PropCode);
  272. hr = pCamera->GetDevicePropDesc(PropCode, &PropDesc);
  273. }
  274. else if (strcmp(szToken, "rs") == 0)
  275. {
  276. printf("Resetting device...\n");
  277. hr = pCamera->ResetDevice();
  278. }
  279. else if (strcmp(szToken, "tp") == 0)
  280. {
  281. WORD Format;
  282. szToken = strtok(NULL, szWhiteSpace);
  283. if (!szToken)
  284. {
  285. printf("ERROR: TakePicture needs a format\n\n");
  286. continue;
  287. }
  288. sscanf(szToken, "%i", &Format);
  289. printf("Taking a picture in format 0x%04x\n\n", Format);
  290. hr = pCamera->InitiateCapture(PTP_STORAGEID_DEFAULT, Format);
  291. }
  292. #ifdef DEADCODE
  293. else if (strcmp(szToken, "gds") == 0)
  294. {
  295. printf("Getting device status\n\n");
  296. USB_PTPDEVICESTATUS Status;
  297. hr = ((CUsbCamera *) pCamera)->GetDeviceStatus(&Status);
  298. }
  299. #endif
  300. else
  301. {
  302. printf("ERROR: Invalid command\n");
  303. PrintCommands();
  304. continue;
  305. }
  306. if (SUCCEEDED(hr))
  307. printf("Success!!\n\n");
  308. else
  309. printf("FAILED\n\n");
  310. } // while (true)
  311. if (bSessionOpen)
  312. {
  313. printf("Closing session\n\n");
  314. hr = pCamera->CloseSession();
  315. if (SUCCEEDED(hr))
  316. printf("Success!!\n\n");
  317. else
  318. printf("FAILED\n\n");
  319. }
  320. WPP_CLEANUP();
  321. return 0;
  322. }