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.

650 lines
14 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved
  3. Module Name:
  4. RmsDvice.cpp
  5. Abstract:
  6. Implementation of CRmsDevice
  7. Author:
  8. Brian Dodd [brian] 15-Nov-1996
  9. Revision History:
  10. --*/
  11. #include "stdafx.h"
  12. #include "RmsDvice.h"
  13. /////////////////////////////////////////////////////////////////////////////
  14. //
  15. // IRmsDevice implementation
  16. //
  17. CRmsDevice::CRmsDevice(
  18. void
  19. )
  20. /*++
  21. Routine Description:
  22. CRmsDevice constructor
  23. Arguments:
  24. None
  25. Return Value:
  26. None
  27. --*/
  28. {
  29. // Initialize values
  30. m_deviceName = RMS_UNDEFINED_STRING;
  31. m_deviceType = RmsDeviceUnknown;
  32. m_sizeofDeviceInfo = 0;
  33. memset(m_deviceInfo, 0, MaxInfo);
  34. m_port = 0xff;
  35. m_bus = 0xff;
  36. m_targetId = 0xff;
  37. m_lun = 0xff;
  38. }
  39. HRESULT
  40. CRmsDevice::CompareTo(
  41. IN IUnknown *pCollectable,
  42. OUT SHORT *pResult
  43. )
  44. /*++
  45. Implements:
  46. CRmsDevice::CompareTo
  47. --*/
  48. {
  49. HRESULT hr = E_FAIL;
  50. SHORT result = 1;
  51. WsbTraceIn( OLESTR("CRmsDevice::CompareTo"), OLESTR("") );
  52. try {
  53. // Validate arguments - Okay if pResult is NULL
  54. WsbAssertPointer( pCollectable );
  55. CComQIPtr<IRmsDevice, &IID_IRmsDevice> pDevice = pCollectable;
  56. WsbAssertPointer( pDevice );
  57. CComQIPtr<IRmsComObject, &IID_IRmsComObject> pObject = pCollectable;
  58. WsbAssertPointer( pObject );
  59. switch ( m_findBy ) {
  60. case RmsFindByDeviceInfo:
  61. {
  62. //
  63. // What we really want to do here is compare by
  64. // a unique device identifier like a serial number.
  65. //
  66. // However, since we don't have a serial number for
  67. // all devices, we'll compare using a best effort
  68. // strategy using for following criteria:
  69. //
  70. // 1) DeviceName, this is unique for fixed drives and
  71. // floppy drives, and SCSI devices, but may not
  72. // survive accross reboot, and may not be the same
  73. // for a device if it's SCSI address is changed.
  74. //
  75. // NOTE: We can't tell if the address was changed
  76. // a device.
  77. //
  78. // NOTE: We can't tell if the drive letter was
  79. // changed for a fixed drive.
  80. //
  81. // TODO: Add serial number support.
  82. //
  83. CWsbBstrPtr name;
  84. // Get the target device name
  85. pDevice->GetDeviceName( &name );
  86. // Compare the names
  87. result = (SHORT)wcscmp( m_deviceName, name );
  88. hr = ( 0 == result ) ? S_OK : S_FALSE;
  89. }
  90. break;
  91. case RmsFindByDeviceAddress:
  92. {
  93. BYTE port, bus, targetId, lun;
  94. // Get the target device address
  95. pDevice->GetDeviceAddress(&port, &bus, &targetId, &lun);
  96. if( (m_port == port) &&
  97. (m_bus == bus) &&
  98. (m_targetId == targetId) &&
  99. (m_lun == lun) ) {
  100. // Device addresses match
  101. hr = S_OK;
  102. result = 0;
  103. }
  104. else {
  105. hr = S_FALSE;
  106. result = 1;
  107. }
  108. }
  109. break;
  110. case RmsFindByDeviceName:
  111. {
  112. CWsbBstrPtr name;
  113. // Get the target device name
  114. pDevice->GetDeviceName( &name );
  115. // Compare the names
  116. result = (SHORT)wcscmp( m_deviceName, name );
  117. hr = ( 0 == result ) ? S_OK : S_FALSE;
  118. }
  119. break;
  120. case RmsFindByDeviceType:
  121. {
  122. RmsDevice type;
  123. // Get the target device name
  124. pDevice->GetDeviceType( (LONG *) &type );
  125. if ( m_deviceType == type ) {
  126. // Device types match
  127. hr = S_OK;
  128. result = 0;
  129. }
  130. else {
  131. hr = S_FALSE;
  132. result = 1;
  133. }
  134. }
  135. break;
  136. default:
  137. //
  138. // Since devices aren't CWsbCollectables, we should
  139. // never come here. CRmsDrive, or CRmsChanger will
  140. // handle the default case.
  141. //
  142. WsbAssertHr( E_UNEXPECTED );
  143. break;
  144. }
  145. }
  146. WsbCatch(hr);
  147. if (0 != pResult) {
  148. *pResult = result;
  149. }
  150. WsbTraceOut(OLESTR("CRmsDevice::CompareTo"), OLESTR("hr = <%ls>, result = <%ls>"), WsbHrAsString(hr), WsbPtrToShortAsString(pResult));
  151. return(hr);
  152. }
  153. HRESULT
  154. CRmsDevice::GetSizeMax(
  155. OUT ULARGE_INTEGER* pcbSize
  156. )
  157. /*++
  158. Implements:
  159. IPersistStream::GetSizeMax
  160. --*/
  161. {
  162. HRESULT hr = E_NOTIMPL;
  163. // ULONG deviceNameLen;
  164. WsbTraceIn(OLESTR("CRmsDevice::GetSizeMax"), OLESTR(""));
  165. // try {
  166. // WsbAssert(0 != pcbSize, E_POINTER);
  167. // deviceNameLen = SysStringByteLen(m_deviceName);
  168. // // Get max size
  169. // pcbSize->QuadPart = WsbPersistSizeOf(LONG) + // length of m_deviceName
  170. // deviceNameLen + // m_deviceName
  171. // WsbPersistSizeOf(LONG) + // m_deviceType
  172. // WsbPersistSizeOf(SHORT) + // m_sizeOfDeviceInfo
  173. // MaxInfo + // m_deviceInfo
  174. // WsbPersistSizeOf(BYTE) + // m_port
  175. // WsbPersistSizeOf(BYTE) + // m_bus
  176. // WsbPersistSizeOf(BYTE) + // m_targetId
  177. // WsbPersistSizeOf(BYTE); // m_lun
  178. // } WsbCatch(hr);
  179. WsbTraceOut(OLESTR("CRmsDevice::GetSizeMax"), OLESTR("hr = <%ls>, Size = <%ls>"), WsbHrAsString(hr), WsbPtrToUliAsString(pcbSize));
  180. return(hr);
  181. }
  182. HRESULT
  183. CRmsDevice::Load(
  184. IN IStream* pStream
  185. )
  186. /*++
  187. Implements:
  188. IPersistStream::Load
  189. --*/
  190. {
  191. HRESULT hr = S_OK;
  192. ULONG ulBytes = 0;
  193. WsbTraceIn(OLESTR("CRmsDevice::Load"), OLESTR(""));
  194. try {
  195. ULONG temp;
  196. WsbAssert(0 != pStream, E_POINTER);
  197. WsbAffirmHr(CRmsChangerElement::Load(pStream));
  198. // Read value
  199. m_deviceName.Free();
  200. WsbAffirmHr(WsbBstrFromStream(pStream, &m_deviceName));
  201. WsbAffirmHr(WsbLoadFromStream(pStream, &temp));
  202. m_deviceType = (RmsDevice)temp;
  203. WsbAffirmHr(WsbLoadFromStream(pStream, &m_sizeofDeviceInfo));
  204. WsbAffirmHr(WsbLoadFromStream(pStream, &(m_deviceInfo [0]), MaxInfo));
  205. WsbAffirmHr(WsbLoadFromStream(pStream, &m_port));
  206. WsbAffirmHr(WsbLoadFromStream(pStream, &m_bus));
  207. WsbAffirmHr(WsbLoadFromStream(pStream, &m_targetId));
  208. WsbAffirmHr(WsbLoadFromStream(pStream, &m_lun));
  209. } WsbCatch(hr);
  210. WsbTraceOut(OLESTR("CRmsDevice::Load"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  211. return(hr);
  212. }
  213. HRESULT
  214. CRmsDevice::Save(
  215. IN IStream* pStream,
  216. IN BOOL clearDirty
  217. )
  218. /*++
  219. Implements:
  220. IPersistStream::Save
  221. --*/
  222. {
  223. HRESULT hr = S_OK;
  224. ULONG ulBytes = 0;
  225. WsbTraceIn(OLESTR("CRmsDevice::Save"), OLESTR("clearDirty = <%ls>"), WsbBoolAsString(clearDirty));
  226. try {
  227. WsbAssert(0 != pStream, E_POINTER);
  228. WsbAffirmHr(CRmsChangerElement::Save(pStream, clearDirty));
  229. // Write value
  230. WsbAffirmHr(WsbBstrToStream(pStream, m_deviceName));
  231. WsbAffirmHr(WsbSaveToStream(pStream, (ULONG) m_deviceType));
  232. WsbAffirmHr(WsbSaveToStream(pStream, m_sizeofDeviceInfo));
  233. WsbAffirmHr(WsbSaveToStream(pStream, &(m_deviceInfo [0]), MaxInfo));
  234. WsbAffirmHr(WsbSaveToStream(pStream, m_port));
  235. WsbAffirmHr(WsbSaveToStream(pStream, m_bus));
  236. WsbAffirmHr(WsbSaveToStream(pStream, m_targetId));
  237. WsbAffirmHr(WsbSaveToStream(pStream, m_lun));
  238. } WsbCatch(hr);
  239. WsbTraceOut(OLESTR("CRmsDevice::Save"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  240. return(hr);
  241. }
  242. HRESULT
  243. CRmsDevice::Test(
  244. OUT USHORT *pPassed,
  245. OUT USHORT *pFailed
  246. )
  247. /*++
  248. Implements:
  249. IWsbTestable::Test
  250. --*/
  251. {
  252. HRESULT hr = S_OK;
  253. CComPtr<IRmsMediaSet> pMediaSet1;
  254. CComPtr<IRmsMediaSet> pMediaSet2;
  255. CComPtr<IPersistFile> pFile1;
  256. CComPtr<IPersistFile> pFile2;
  257. LONG i;
  258. LONG passfail = TRUE;
  259. CWsbBstrPtr bstrVal1 = OLESTR("5A5A5A");
  260. CWsbBstrPtr bstrWork1;
  261. CWsbBstrPtr bstrWork2;
  262. SHORT ucharLenVal1 = 10;
  263. UCHAR ucharVal1[MaxInfo] = {1,2,3,4,5,6,7,8,9,10};
  264. SHORT ucharLenWork1;
  265. UCHAR ucharWork1[MaxInfo];
  266. BYTE byteVal1 = 1;
  267. BYTE byteVal2 = 2;
  268. BYTE byteVal3 = 3;
  269. BYTE byteVal4 = 4;
  270. BYTE byteWork1;
  271. BYTE byteWork2;
  272. BYTE byteWork3;
  273. BYTE byteWork4;
  274. WsbTraceIn(OLESTR("CRmsDevice::Test"), OLESTR(""));
  275. try {
  276. // Get the MediaSet interface.
  277. hr = S_OK;
  278. try {
  279. WsbAssertHr(((IUnknown*) (IRmsMediaSet*) this)->QueryInterface(IID_IRmsMediaSet, (void**) &pMediaSet1));
  280. // Test SetDeviceName & GetDeviceName interface
  281. bstrWork1 = bstrVal1;
  282. SetDeviceName(bstrWork1);
  283. GetDeviceName(&bstrWork2);
  284. if (bstrWork1 == bstrWork2){
  285. (*pPassed)++;
  286. } else {
  287. (*pFailed)++;
  288. }
  289. // Test SetDeviceInfo & GetDeviceInfo interface
  290. SetDeviceInfo(ucharVal1, ucharLenVal1);
  291. GetDeviceInfo(ucharWork1, &ucharLenWork1);
  292. if (ucharLenVal1 == ucharLenWork1){
  293. (*pPassed)++;
  294. } else {
  295. (*pFailed)++;
  296. }
  297. passfail = TRUE;
  298. for(i = 0; i < ucharLenVal1; i++){
  299. if(ucharVal1[i] != ucharWork1[i]){
  300. passfail = FALSE;
  301. break;
  302. }
  303. }
  304. if (passfail == TRUE){
  305. (*pPassed)++;
  306. } else {
  307. (*pFailed)++;
  308. }
  309. // Test SetDeviceAddress & GetDeviceAddress
  310. SetDeviceAddress(byteVal1, byteVal2, byteVal3, byteVal4);
  311. GetDeviceAddress(&byteWork1, &byteWork2, &byteWork3, &byteWork4);
  312. if ((byteVal1 == byteWork1) &&
  313. (byteVal2 == byteWork2) &&
  314. (byteVal3 == byteWork3) &&
  315. (byteVal4 == byteWork4)){
  316. (*pPassed)++;
  317. } else {
  318. (*pFailed)++;
  319. }
  320. } WsbCatch(hr);
  321. // Tally up the results
  322. hr = S_OK;
  323. if (*pFailed) {
  324. hr = S_FALSE;
  325. }
  326. } WsbCatch(hr);
  327. WsbTraceOut(OLESTR("CRmsDevice::Test"), OLESTR("hr = <%ls>"), WsbHrAsString(hr));
  328. return(hr);
  329. }
  330. STDMETHODIMP
  331. CRmsDevice::GetDeviceName(
  332. BSTR *pName
  333. )
  334. /*++
  335. Implements:
  336. IRmsDevice::GetDeviceName
  337. --*/
  338. {
  339. WsbAssertPointer (pName);
  340. m_deviceName. CopyToBstr (pName);
  341. return S_OK;
  342. }
  343. STDMETHODIMP
  344. CRmsDevice::SetDeviceName(
  345. BSTR name
  346. )
  347. /*++
  348. Implements:
  349. IRmsDevice::SetDeviceName
  350. --*/
  351. {
  352. m_deviceName = name;
  353. // m_isDirty = TRUE;
  354. return S_OK;
  355. }
  356. STDMETHODIMP
  357. CRmsDevice::GetDeviceType(
  358. LONG *pType
  359. )
  360. /*++
  361. Implements:
  362. IRmsDevice::GetDeviceType
  363. --*/
  364. {
  365. *pType = m_deviceType;
  366. return S_OK;
  367. }
  368. STDMETHODIMP
  369. CRmsDevice::SetDeviceType(
  370. LONG type
  371. )
  372. /*++
  373. Implements:
  374. IRmsDevice::SetDeviceType
  375. --*/
  376. {
  377. m_deviceType = (RmsDevice)type;
  378. // m_isDirty = TRUE;
  379. return S_OK;
  380. }
  381. STDMETHODIMP
  382. CRmsDevice::GetDeviceInfo(
  383. UCHAR *pId,
  384. SHORT *pSize
  385. )
  386. /*++
  387. Implements:
  388. IRmsDevice::GetDeviceInfo
  389. --*/
  390. {
  391. memmove (pId, m_deviceInfo, m_sizeofDeviceInfo);
  392. *pSize = m_sizeofDeviceInfo;
  393. return S_OK;
  394. }
  395. STDMETHODIMP
  396. CRmsDevice::SetDeviceInfo(
  397. UCHAR *pId,
  398. SHORT size
  399. )
  400. /*++
  401. Implements:
  402. IRmsDevice::SetDeviceInfo
  403. --*/
  404. {
  405. memmove (m_deviceInfo, pId, size);
  406. m_sizeofDeviceInfo = size;
  407. // m_isDirty = TRUE;
  408. return S_OK;
  409. }
  410. STDMETHODIMP
  411. CRmsDevice::GetDeviceAddress(
  412. LPBYTE pPort,
  413. LPBYTE pBus,
  414. LPBYTE pId,
  415. LPBYTE pLun
  416. )
  417. /*++
  418. Implements:
  419. IRmsDevice::GetDeviceAddress
  420. --*/
  421. {
  422. *pPort = m_port;
  423. *pBus = m_bus;
  424. *pId = m_targetId;
  425. *pLun = m_lun;
  426. return S_OK;
  427. }
  428. STDMETHODIMP
  429. CRmsDevice::SetDeviceAddress(
  430. BYTE port,
  431. BYTE bus,
  432. BYTE id,
  433. BYTE lun
  434. )
  435. /*++
  436. Implements:
  437. IRmsDevice::SetDeviceAddress
  438. --*/
  439. {
  440. m_port = port;
  441. m_bus = bus;
  442. m_targetId = id;
  443. m_lun = lun;
  444. // m_isDirty = TRUE;
  445. return S_OK;
  446. }