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.

1163 lines
33 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1998-2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: ComPortData.cpp
  6. * Content: Serial communications port data management class
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 01/20/98 jtk Created
  13. * 04/25/2000 jtk Derived from ComPort class
  14. ***************************************************************************/
  15. #include "dnmdmi.h"
  16. //**********************************************************************
  17. // Constant definitions
  18. //**********************************************************************
  19. ////
  20. //// number of BITS in a serial BYTE
  21. ////
  22. //#define BITS_PER_BYTE 8
  23. //
  24. ////
  25. //// maximum size of baud rate string
  26. ////
  27. //#define MAX_BAUD_STRING_SIZE 7
  28. //
  29. ////
  30. //// default size of buffers when parsing
  31. ////
  32. //#define DEFAULT_COMPONENT_BUFFER_SIZE 1000
  33. //
  34. ////
  35. //// device ID assigned to 'all adapters'
  36. ////
  37. //#define ALL_ADAPTERS_DEVICE_ID 0
  38. //
  39. ////
  40. //// NULL token
  41. ////
  42. //#define NULL_TOKEN '\0'
  43. //**********************************************************************
  44. // Macro definitions
  45. //**********************************************************************
  46. #define DPNA_BAUD_RATE_9600_W L"9600"
  47. #define DPNA_BAUD_RATE_14400_W L"14400"
  48. #define DPNA_BAUD_RATE_19200_W L"19200"
  49. #define DPNA_BAUD_RATE_38400_W L"38400"
  50. #define DPNA_BAUD_RATE_56000_W L"56000"
  51. #define DPNA_BAUD_RATE_57600_W L"57600"
  52. #define DPNA_BAUD_RATE_115200_W L"115200"
  53. // values for baud rate
  54. #define DPNA_BAUD_RATE_9600_A "9600"
  55. #define DPNA_BAUD_RATE_14400_A "14400"
  56. #define DPNA_BAUD_RATE_19200_A "19200"
  57. #define DPNA_BAUD_RATE_38400_A "38400"
  58. #define DPNA_BAUD_RATE_56000_A "56000"
  59. #define DPNA_BAUD_RATE_57600_A "57600"
  60. #define DPNA_BAUD_RATE_115200_A "115200"
  61. //**********************************************************************
  62. // Structure definitions
  63. //**********************************************************************
  64. //**********************************************************************
  65. // Variable definitions
  66. //**********************************************************************
  67. //
  68. // list of baud rates
  69. //
  70. STRING_BLOCK g_BaudRate[] =
  71. {
  72. { CBR_9600, DPNA_BAUD_RATE_9600_W, ( LENGTHOF( DPNA_BAUD_RATE_9600_W ) - 1 ), DPNA_BAUD_RATE_9600_A, ( LENGTHOF( DPNA_BAUD_RATE_9600_A ) - 1 ) },
  73. { CBR_14400, DPNA_BAUD_RATE_14400_W, ( LENGTHOF( DPNA_BAUD_RATE_14400_W ) - 1 ), DPNA_BAUD_RATE_14400_A, ( LENGTHOF( DPNA_BAUD_RATE_14400_A ) - 1 ) },
  74. { CBR_19200, DPNA_BAUD_RATE_19200_W, ( LENGTHOF( DPNA_BAUD_RATE_19200_W ) - 1 ), DPNA_BAUD_RATE_19200_A, ( LENGTHOF( DPNA_BAUD_RATE_19200_A ) - 1 ) },
  75. { CBR_38400, DPNA_BAUD_RATE_38400_W, ( LENGTHOF( DPNA_BAUD_RATE_38400_W ) - 1 ), DPNA_BAUD_RATE_38400_A, ( LENGTHOF( DPNA_BAUD_RATE_38400_A ) - 1 ) },
  76. { CBR_56000, DPNA_BAUD_RATE_56000_W, ( LENGTHOF( DPNA_BAUD_RATE_56000_W ) - 1 ), DPNA_BAUD_RATE_56000_A, ( LENGTHOF( DPNA_BAUD_RATE_56000_A ) - 1 ) },
  77. { CBR_57600, DPNA_BAUD_RATE_57600_W, ( LENGTHOF( DPNA_BAUD_RATE_57600_W ) - 1 ), DPNA_BAUD_RATE_57600_A, ( LENGTHOF( DPNA_BAUD_RATE_57600_A ) - 1 ) },
  78. { CBR_115200, DPNA_BAUD_RATE_115200_W, ( LENGTHOF( DPNA_BAUD_RATE_115200_W ) - 1 ), DPNA_BAUD_RATE_115200_A, ( LENGTHOF( DPNA_BAUD_RATE_115200_A ) - 1 ) },
  79. };
  80. const DWORD g_dwBaudRateCount = LENGTHOF( g_BaudRate );
  81. //
  82. // list of stop bit types
  83. //
  84. STRING_BLOCK g_StopBits[] =
  85. {
  86. { ONESTOPBIT, DPNA_STOP_BITS_ONE, ( LENGTHOF( DPNA_STOP_BITS_ONE ) - 1 ), DPNA_STOP_BITS_ONE_A, ( LENGTHOF( DPNA_STOP_BITS_ONE_A ) - 1 ) },
  87. { ONE5STOPBITS, DPNA_STOP_BITS_ONE_FIVE, ( LENGTHOF( DPNA_STOP_BITS_ONE_FIVE ) - 1 ), DPNA_STOP_BITS_ONE_FIVE_A, ( LENGTHOF( DPNA_STOP_BITS_ONE_FIVE_A ) - 1 ) },
  88. { TWOSTOPBITS, DPNA_STOP_BITS_TWO, ( LENGTHOF( DPNA_STOP_BITS_TWO ) - 1 ), DPNA_STOP_BITS_TWO_A, ( LENGTHOF( DPNA_STOP_BITS_TWO_A ) - 1 ) }
  89. };
  90. const DWORD g_dwStopBitsCount = LENGTHOF( g_StopBits );
  91. //
  92. // list of parity types
  93. //
  94. STRING_BLOCK g_Parity[] =
  95. {
  96. { EVENPARITY, DPNA_PARITY_EVEN, ( LENGTHOF( DPNA_PARITY_EVEN ) - 1 ), DPNA_PARITY_EVEN_A, ( LENGTHOF( DPNA_PARITY_EVEN_A ) - 1 ) },
  97. { MARKPARITY, DPNA_PARITY_MARK, ( LENGTHOF( DPNA_PARITY_MARK ) - 1 ), DPNA_PARITY_MARK_A, ( LENGTHOF( DPNA_PARITY_MARK_A ) - 1 ) },
  98. { NOPARITY, DPNA_PARITY_NONE, ( LENGTHOF( DPNA_PARITY_NONE ) - 1 ), DPNA_PARITY_NONE_A, ( LENGTHOF( DPNA_PARITY_NONE_A ) - 1 ) },
  99. { ODDPARITY, DPNA_PARITY_ODD, ( LENGTHOF( DPNA_PARITY_ODD ) - 1 ), DPNA_PARITY_ODD_A, ( LENGTHOF( DPNA_PARITY_ODD_A ) - 1 ) },
  100. { SPACEPARITY, DPNA_PARITY_SPACE, ( LENGTHOF( DPNA_PARITY_SPACE ) - 1 ), DPNA_PARITY_SPACE_A, ( LENGTHOF( DPNA_PARITY_SPACE_A ) - 1 ) }
  101. };
  102. const DWORD g_dwParityCount = LENGTHOF( g_Parity );
  103. //
  104. // list of flow control types
  105. //
  106. STRING_BLOCK g_FlowControl[] =
  107. {
  108. { FLOW_NONE, DPNA_FLOW_CONTROL_NONE, ( LENGTHOF( DPNA_FLOW_CONTROL_NONE ) - 1 ), DPNA_FLOW_CONTROL_NONE_A, ( LENGTHOF( DPNA_FLOW_CONTROL_NONE_A ) - 1 ) },
  109. { FLOW_XONXOFF, DPNA_FLOW_CONTROL_XONXOFF, ( LENGTHOF( DPNA_FLOW_CONTROL_XONXOFF ) - 1 ), DPNA_FLOW_CONTROL_XONXOFF_A, ( LENGTHOF( DPNA_FLOW_CONTROL_XONXOFF_A ) - 1 ) },
  110. { FLOW_RTS, DPNA_FLOW_CONTROL_RTS, ( LENGTHOF( DPNA_FLOW_CONTROL_RTS ) - 1 ), DPNA_FLOW_CONTROL_RTS_A, ( LENGTHOF( DPNA_FLOW_CONTROL_RTS_A ) - 1 ) },
  111. { FLOW_DTR, DPNA_FLOW_CONTROL_DTR, ( LENGTHOF( DPNA_FLOW_CONTROL_DTR ) - 1 ), DPNA_FLOW_CONTROL_DTR_A, ( LENGTHOF( DPNA_FLOW_CONTROL_DTR_A ) - 1 ) },
  112. { FLOW_RTSDTR, DPNA_FLOW_CONTROL_RTSDTR, ( LENGTHOF( DPNA_FLOW_CONTROL_RTSDTR ) - 1 ), DPNA_FLOW_CONTROL_RTSDTR_A, ( LENGTHOF( DPNA_FLOW_CONTROL_RTSDTR_A ) - 1 ) }
  113. };
  114. const DWORD g_dwFlowControlCount = LENGTHOF( g_FlowControl );
  115. //**********************************************************************
  116. // Function prototypes
  117. //**********************************************************************
  118. //**********************************************************************
  119. // Function definitions
  120. //**********************************************************************
  121. //**********************************************************************
  122. // ------------------------------
  123. // CComPortData::CComPortData - constructor
  124. //
  125. // Entry: Nothing
  126. //
  127. // Exit: Nothing
  128. //
  129. // Notes: Do not allocate anything in a constructor
  130. // ------------------------------
  131. #undef DPF_MODNAME
  132. #define DPF_MODNAME "CComPortData::CComPortData"
  133. CComPortData::CComPortData():
  134. m_dwDeviceID( INVALID_DEVICE_ID ),
  135. m_BaudRate( CBR_57600 ),
  136. m_StopBits( ONESTOPBIT ),
  137. m_Parity( NOPARITY ),
  138. m_FlowControl( FLOW_NONE )
  139. {
  140. //
  141. // verify that the DPlay8 address baud rate #defines match those in Windows
  142. //
  143. DBG_CASSERT( CBR_9600 == DPNA_BAUD_RATE_9600 );
  144. DBG_CASSERT( CBR_14400 == DPNA_BAUD_RATE_14400 );
  145. DBG_CASSERT( CBR_19200 == DPNA_BAUD_RATE_19200 );
  146. DBG_CASSERT( CBR_38400 == DPNA_BAUD_RATE_38400 );
  147. DBG_CASSERT( CBR_56000 == DPNA_BAUD_RATE_56000 );
  148. DBG_CASSERT( CBR_57600 == DPNA_BAUD_RATE_57600 );
  149. DBG_CASSERT( CBR_115200 == DPNA_BAUD_RATE_115200 );
  150. memset( m_ComPortName, 0x00, sizeof( m_ComPortName ));
  151. }
  152. //**********************************************************************
  153. //**********************************************************************
  154. // ------------------------------
  155. // CComPortData::~CComPortData - destructor
  156. //
  157. // Entry: Nothing
  158. //
  159. // Exit: Nothing
  160. // ------------------------------
  161. #undef DPF_MODNAME
  162. #define DPF_MODNAME "CComPortData::~CComPortData"
  163. CComPortData::~CComPortData()
  164. {
  165. DNASSERT( m_dwDeviceID == INVALID_DEVICE_ID );
  166. DNASSERT( m_BaudRate == CBR_57600 );
  167. DNASSERT( m_StopBits == ONESTOPBIT );
  168. DNASSERT( m_Parity == NOPARITY );
  169. DNASSERT( m_FlowControl == FLOW_NONE );
  170. }
  171. //**********************************************************************
  172. //**********************************************************************
  173. // ------------------------------
  174. // CComPortData::ComPortDataFromDP8Addresses - initialize ComPortData from a DirectPlay8 address
  175. //
  176. // Entry: Pointer to host address (may be NULL)
  177. // Pointer to device address
  178. //
  179. // Exit: Error code
  180. // ------------------------------
  181. #undef DPF_MODNAME
  182. #define DPF_MODNAME "CComPortData::ComPortDataFromDP8Addresses"
  183. HRESULT CComPortData::ComPortDataFromDP8Addresses( IDirectPlay8Address *const pHostAddress,
  184. IDirectPlay8Address *const pDeviceAddress )
  185. {
  186. HRESULT hr;
  187. UINT_PTR uIndex;
  188. CParseClass ParseClass;
  189. const PARSE_KEY ParseKeyList[] =
  190. {
  191. { DPNA_KEY_DEVICE, LENGTHOF( DPNA_KEY_DEVICE ) - 1, this, ParseDevice },
  192. { DPNA_KEY_BAUD, LENGTHOF( DPNA_KEY_BAUD) - 1, this, ParseBaud },
  193. { DPNA_KEY_STOPBITS, LENGTHOF( DPNA_KEY_STOPBITS ) - 1, this, ParseStopBits },
  194. { DPNA_KEY_PARITY, LENGTHOF( DPNA_KEY_PARITY ) - 1, this, ParseParity },
  195. { DPNA_KEY_FLOWCONTROL, LENGTHOF( DPNA_KEY_FLOWCONTROL ) - 1, this, ParseFlowControl }
  196. };
  197. DNASSERT( pDeviceAddress != NULL );
  198. //
  199. // initialize
  200. //
  201. hr = DPN_OK;
  202. //
  203. // reset parsing flags and parse
  204. //
  205. uIndex = LENGTHOF( m_ComponentInitializationState );
  206. while ( uIndex > 0 )
  207. {
  208. uIndex--;
  209. m_ComponentInitializationState[ uIndex ] = SP_ADDRESS_COMPONENT_UNINITIALIZED;
  210. }
  211. hr = ParseClass.ParseDP8Address( pDeviceAddress,
  212. &CLSID_DP8SP_SERIAL,
  213. ParseKeyList,
  214. LENGTHOF( ParseKeyList )
  215. );
  216. //
  217. // There are two addresses to parse for a comport. The device address will
  218. // be present for all commands, so do it first. The host address will be
  219. // parsed if it's available.
  220. //
  221. if ( hr != DPN_OK )
  222. {
  223. DPFX(DPFPREP, 0, "Failed address parse!" );
  224. DisplayDNError( 0, hr );
  225. goto Exit;
  226. }
  227. if ( pHostAddress != NULL )
  228. {
  229. hr = ParseClass.ParseDP8Address( pHostAddress,
  230. &CLSID_DP8SP_SERIAL,
  231. ParseKeyList,
  232. LENGTHOF( ParseKeyList )
  233. );
  234. if ( hr != DPN_OK )
  235. {
  236. DPFX(DPFPREP, 0, "Failed parse of host address!" );
  237. DisplayDNError( 0, hr );
  238. goto Exit;
  239. }
  240. }
  241. //
  242. // check for all parameters being initialized, or fail if one of the
  243. // parameters failed to initialize.
  244. //
  245. DNASSERT( hr == DPN_OK );
  246. uIndex = COMPORT_PARSE_KEY_MAX;
  247. while ( uIndex > 0 )
  248. {
  249. uIndex--;
  250. switch ( m_ComponentInitializationState[ uIndex ] )
  251. {
  252. //
  253. // This component was initialized properly. Continue checking
  254. // for other problems.
  255. //
  256. case SP_ADDRESS_COMPONENT_INITIALIZED:
  257. {
  258. break;
  259. }
  260. //
  261. // This component was not initialized, note that the address was
  262. // incomplete and that the user will need to be queried. Keep
  263. // checking components for other problems.
  264. //
  265. case SP_ADDRESS_COMPONENT_UNINITIALIZED:
  266. {
  267. hr = DPNERR_INCOMPLETEADDRESS;
  268. break;
  269. }
  270. //
  271. // initialization of this component failed, fail the parse.
  272. //
  273. case SP_ADDRESS_COMPONENT_INITIALIZATION_FAILED:
  274. {
  275. hr = DPNERR_ADDRESSING;
  276. DPFX(DPFPREP, 8, "DataPortFromDNAddress: parse failure!" );
  277. goto Failure;
  278. break;
  279. }
  280. }
  281. }
  282. //
  283. // do we indicate an attempt at initialization?
  284. //
  285. DNASSERT( ( hr == DPN_OK ) || ( hr == DPNERR_INCOMPLETEADDRESS ) );
  286. Exit:
  287. if ( hr != DPN_OK )
  288. {
  289. DPFX(DPFPREP, 0, "Problem with CComPortData::ComPortDataFromDNAddress()" );
  290. DisplayDNError( 0, hr );
  291. }
  292. return hr;
  293. Failure:
  294. goto Exit;
  295. }
  296. //**********************************************************************
  297. //**********************************************************************
  298. // ------------------------------
  299. // CComPortData::DP8AddressFromComPortData - convert a ComPortData to a DirectPlay8 address
  300. //
  301. // Entry: Address type
  302. //
  303. // Exit: Pointer to DirecctPlayAddress
  304. // ------------------------------
  305. #undef DPF_MODNAME
  306. #define DPF_MODNAME "CComPortData::DP8AddressFromComPortData"
  307. IDirectPlay8Address *CComPortData::DP8AddressFromComPortData( const ADDRESS_TYPE AddressType ) const
  308. {
  309. HRESULT hr;
  310. UINT_PTR uIndex;
  311. GUID DeviceGuid;
  312. const WCHAR *pComponentString;
  313. DWORD dwComponentStringSize;
  314. IDirectPlay8Address *pAddress;
  315. DNASSERT( ( AddressType == ADDRESS_TYPE_REMOTE_HOST ) ||
  316. ( AddressType == ADDRESS_TYPE_LOCAL_ADAPTER ) ||
  317. ( AddressType == ADDRESS_TYPE_LOCAL_ADAPTER_HOST_FORMAT ) );
  318. //
  319. // initialize
  320. //
  321. pAddress = NULL;
  322. uIndex = COMPORT_PARSE_KEY_MAX;
  323. while ( uIndex > 0 )
  324. {
  325. uIndex--;
  326. if ( m_ComponentInitializationState[ uIndex ] != SP_ADDRESS_COMPONENT_INITIALIZED )
  327. {
  328. DPFX(DPFPREP, 0, "Attempt made to extract partial ComPortData information!" );
  329. DNASSERT( FALSE );
  330. goto Failure;
  331. }
  332. }
  333. //
  334. // create output address
  335. //
  336. hr = COM_CoCreateInstance( CLSID_DirectPlay8Address,
  337. NULL,
  338. CLSCTX_INPROC_SERVER,
  339. IID_IDirectPlay8Address,
  340. reinterpret_cast<void**>( &pAddress ), FALSE );
  341. if ( hr != S_OK )
  342. {
  343. DNASSERT( pAddress == NULL );
  344. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to create Address when converting data port to address!" );
  345. goto Failure;
  346. }
  347. //
  348. // set the SP guid
  349. //
  350. hr = IDirectPlay8Address_SetSP( pAddress, &CLSID_DP8SP_SERIAL );
  351. if ( hr != DPN_OK )
  352. {
  353. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to set service provider GUID!" );
  354. DisplayDNError( 0, hr );
  355. goto Failure;
  356. }
  357. //
  358. // All serial settings are part of the local adapter. Host settings return
  359. // just the SP type.
  360. //
  361. if ( AddressType == ADDRESS_TYPE_LOCAL_ADAPTER )
  362. {
  363. DeviceIDToGuid( &DeviceGuid, GetDeviceID(), &g_SerialSPEncryptionGuid );
  364. hr = IDirectPlay8Address_SetDevice( pAddress, &DeviceGuid );
  365. if ( hr != DPN_OK )
  366. {
  367. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to add device GUID!" );
  368. DisplayDNError( 0, hr );
  369. goto Failure;
  370. }
  371. //
  372. // set baud rate
  373. //
  374. DBG_CASSERT( sizeof( SP_BAUD_RATE ) == sizeof( DWORD ) );
  375. hr = IDirectPlay8Address_AddComponent( pAddress,
  376. DPNA_KEY_BAUD,
  377. &m_BaudRate,
  378. sizeof( SP_BAUD_RATE ),
  379. DPNA_DATATYPE_DWORD );
  380. if ( hr != DPN_OK )
  381. {
  382. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to add baud rate!" );
  383. DisplayDNError( 0, hr );
  384. goto Failure;
  385. }
  386. //
  387. // set stop bits
  388. //
  389. if ( ValueToString( &pComponentString, // pointer to value string
  390. &dwComponentStringSize, // pointer to length of value string
  391. GetStopBits(), // enum value
  392. g_StopBits, // pointer to enum-string array
  393. g_dwStopBitsCount // length of enum-string array
  394. ) == FALSE )
  395. {
  396. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to convert baud rate!" );
  397. DNASSERT( FALSE );
  398. goto Failure;
  399. }
  400. hr = IDirectPlay8Address_AddComponent( pAddress,
  401. DPNA_KEY_STOPBITS,
  402. pComponentString,
  403. ( ( dwComponentStringSize + 1 ) * sizeof( WCHAR ) ),
  404. DPNA_DATATYPE_STRING );
  405. if ( hr != DPN_OK )
  406. {
  407. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to add stop bits!" );
  408. DisplayDNError( 0, hr );
  409. goto Failure;
  410. }
  411. //
  412. // set parity
  413. //
  414. if ( ValueToString( &pComponentString, // pointer to value string
  415. &dwComponentStringSize, // pointer to length of value string
  416. GetParity(), // enum value
  417. g_Parity, // pointer to enum-string array
  418. g_dwParityCount // length of enum-string array
  419. ) == FALSE )
  420. {
  421. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to convert parity!" );
  422. DNASSERT( FALSE );
  423. goto Failure;
  424. }
  425. hr = IDirectPlay8Address_AddComponent( pAddress,
  426. DPNA_KEY_PARITY,
  427. pComponentString,
  428. ( ( dwComponentStringSize + 1 ) * sizeof( WCHAR ) ),
  429. DPNA_DATATYPE_STRING );
  430. if ( hr != DPN_OK )
  431. {
  432. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to add parity!" );
  433. DisplayDNError( 0, hr );
  434. goto Failure;
  435. }
  436. //
  437. // set flow control
  438. //
  439. if ( ValueToString( &pComponentString, // pointer to value string
  440. &dwComponentStringSize, // pointer to length of value string
  441. GetFlowControl(), // enum value
  442. g_FlowControl, // pointer to enum-string array
  443. g_dwFlowControlCount // length of enum-string array
  444. ) == FALSE )
  445. {
  446. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to convert flow control!" );
  447. DNASSERT( FALSE );
  448. goto Failure;
  449. }
  450. hr = IDirectPlay8Address_AddComponent( pAddress,
  451. DPNA_KEY_FLOWCONTROL,
  452. pComponentString,
  453. ( ( dwComponentStringSize + 1 ) * sizeof( WCHAR ) ),
  454. DPNA_DATATYPE_STRING );
  455. if ( hr != DPN_OK )
  456. {
  457. DPFX(DPFPREP, 0, "DP8AddressFromComPortData: Failed to add flow control!" );
  458. DisplayDNError( 0, hr );
  459. goto Failure;
  460. }
  461. }
  462. Exit:
  463. return pAddress;
  464. Failure:
  465. if ( pAddress != NULL )
  466. {
  467. IDirectPlay8Address_Release( pAddress );
  468. pAddress = NULL;
  469. }
  470. goto Exit;
  471. }
  472. //**********************************************************************
  473. //**********************************************************************
  474. // ------------------------------
  475. // CComPortData::SetDeviceID - set device ID
  476. //
  477. // Entry: Device ID
  478. //
  479. // Exit: Error code
  480. // ------------------------------
  481. #undef DPF_MODNAME
  482. #define DPF_MODNAME "CComPortData::SetDeviceID"
  483. HRESULT CComPortData::SetDeviceID( const DWORD dwDeviceID )
  484. {
  485. HRESULT hr;
  486. //
  487. // initialize
  488. //
  489. hr = DPN_OK;
  490. if ( ( dwDeviceID > MAX_DATA_PORTS ) ||
  491. ( dwDeviceID == 0 ) )
  492. {
  493. if ( dwDeviceID != INVALID_DEVICE_ID )
  494. {
  495. hr = DPNERR_ADDRESSING;
  496. }
  497. else
  498. {
  499. m_dwDeviceID = INVALID_DEVICE_ID;
  500. DNASSERT( hr == DPN_OK );
  501. }
  502. goto Exit;
  503. }
  504. m_dwDeviceID = dwDeviceID;
  505. ClearComPortName();
  506. ComDeviceIDToString( ComPortName(), m_dwDeviceID );
  507. m_ComponentInitializationState[ COMPORT_PARSE_KEY_DEVICE ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  508. Exit:
  509. return hr;
  510. }
  511. //**********************************************************************
  512. //**********************************************************************
  513. // ------------------------------
  514. // CComPortData::SetBaudRate - set baud rate
  515. //
  516. // Entry: Baud rate
  517. //
  518. // Exit: Error code
  519. // ------------------------------
  520. #undef DPF_MODNAME
  521. #define DPF_MODNAME "CComPortData::SetBaudRate"
  522. HRESULT CComPortData::SetBaudRate( const SP_BAUD_RATE BaudRate )
  523. {
  524. HRESULT hr;
  525. hr = DPN_OK;
  526. switch ( BaudRate )
  527. {
  528. //
  529. // valid rates
  530. //
  531. case CBR_110:
  532. case CBR_300:
  533. case CBR_600:
  534. case CBR_1200:
  535. case CBR_2400:
  536. case CBR_4800:
  537. case CBR_9600:
  538. case CBR_14400:
  539. case CBR_19200:
  540. case CBR_38400:
  541. case CBR_56000:
  542. case CBR_57600:
  543. case CBR_115200:
  544. case CBR_128000:
  545. case CBR_256000:
  546. {
  547. m_BaudRate = BaudRate;
  548. m_ComponentInitializationState[ COMPORT_PARSE_KEY_BAUDRATE ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  549. break;
  550. }
  551. //
  552. // other
  553. //
  554. default:
  555. {
  556. hr = DPNERR_ADDRESSING;
  557. DPFX(DPFPREP, 0, "Invalid baud rate (%d)!", BaudRate );
  558. DNASSERT( FALSE );
  559. break;
  560. }
  561. }
  562. return hr;
  563. }
  564. //**********************************************************************
  565. //**********************************************************************
  566. // ------------------------------
  567. // CComPortData::SetStopBits - set stop bits
  568. //
  569. // Entry: Stop bits
  570. //
  571. // Exit: Error code
  572. // ------------------------------
  573. #undef DPF_MODNAME
  574. #define DPF_MODNAME "CComPortData::SetStopBits"
  575. HRESULT CComPortData::SetStopBits( const SP_STOP_BITS StopBits )
  576. {
  577. HRESULT hr;
  578. hr = DPN_OK;
  579. switch ( StopBits )
  580. {
  581. //
  582. // valid settings
  583. //
  584. case ONESTOPBIT:
  585. case ONE5STOPBITS:
  586. case TWOSTOPBITS:
  587. {
  588. m_StopBits = StopBits;
  589. m_ComponentInitializationState[ COMPORT_PARSE_KEY_STOPBITS ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  590. break;
  591. }
  592. //
  593. // other
  594. //
  595. default:
  596. {
  597. hr = DPNERR_ADDRESSING;
  598. DPFX(DPFPREP, 0, "Ivalid stop bit setting (0x%x)!", StopBits );
  599. DNASSERT( FALSE );
  600. break;
  601. }
  602. }
  603. return hr;
  604. }
  605. //**********************************************************************
  606. //**********************************************************************
  607. // ------------------------------
  608. // CComPortData::SetParity - set parity
  609. //
  610. // Entry: Parity
  611. //
  612. // Exit: Error code
  613. // ------------------------------
  614. #undef DPF_MODNAME
  615. #define DPF_MODNAME "CComPortData::SetParity"
  616. HRESULT CComPortData::SetParity( const SP_PARITY_TYPE Parity )
  617. {
  618. HRESULT hr;
  619. hr = DPN_OK;
  620. switch ( Parity )
  621. {
  622. //
  623. // valid settings
  624. //
  625. case NOPARITY:
  626. case EVENPARITY:
  627. case ODDPARITY:
  628. case MARKPARITY:
  629. case SPACEPARITY:
  630. {
  631. m_Parity = Parity;
  632. m_ComponentInitializationState[ COMPORT_PARSE_KEY_PARITY ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  633. break;
  634. }
  635. //
  636. // other
  637. //
  638. default:
  639. {
  640. hr = DPNERR_ADDRESSING;
  641. DPFX(DPFPREP, 0, "Invalid parity (0x%x)!", Parity );
  642. DNASSERT( FALSE );
  643. break;
  644. }
  645. }
  646. return hr;
  647. }
  648. //**********************************************************************
  649. //**********************************************************************
  650. // ------------------------------
  651. // CComPortData::SetFlowControl - set flow control
  652. //
  653. // Entry: Flow control
  654. //
  655. // Exit: Error code
  656. // ------------------------------
  657. #undef DPF_MODNAME
  658. #define DPF_MODNAME "CComPortData::SetFlowControl"
  659. HRESULT CComPortData::SetFlowControl( const SP_FLOW_CONTROL FlowControl )
  660. {
  661. HRESULT hr;
  662. hr = DPN_OK;
  663. switch ( FlowControl )
  664. {
  665. //
  666. // valid settings
  667. //
  668. case FLOW_NONE:
  669. case FLOW_XONXOFF:
  670. case FLOW_RTS:
  671. case FLOW_DTR:
  672. case FLOW_RTSDTR:
  673. {
  674. m_FlowControl = FlowControl;
  675. m_ComponentInitializationState[ COMPORT_PARSE_KEY_FLOWCONTROL ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  676. break;
  677. }
  678. //
  679. // other
  680. //
  681. default:
  682. {
  683. hr = DPNERR_ADDRESSING;
  684. DPFX(DPFPREP, 0, "Invalid flow control (0x%x)!", FlowControl );
  685. DNASSERT( FALSE );
  686. break;
  687. }
  688. }
  689. return hr;
  690. }
  691. //**********************************************************************
  692. //**********************************************************************
  693. // ------------------------------
  694. // CComPortData::IsEqual - is this comport data block equal to another?
  695. //
  696. // Entry: Pointer to other data block
  697. //
  698. // Exit: Boolean indicating equality
  699. // TRUE = is equal
  700. // FALSE = is not equal
  701. // ------------------------------
  702. #undef DPF_MODNAME
  703. #define DPF_MODNAME "CComPortData::IsEqual"
  704. BOOL CComPortData::IsEqual( const CComPortData *const pOtherPort ) const
  705. {
  706. BOOL fReturn;
  707. fReturn = TRUE;
  708. if ( ( GetDeviceID() != pOtherPort->GetDeviceID() ) ||
  709. ( GetBaudRate() != pOtherPort->GetBaudRate() ) ||
  710. ( GetStopBits() != pOtherPort->GetStopBits() ) ||
  711. ( GetParity() != pOtherPort->GetParity() ) ||
  712. ( GetFlowControl() != pOtherPort->GetFlowControl() ) )
  713. {
  714. fReturn = FALSE;
  715. }
  716. return fReturn;
  717. }
  718. //**********************************************************************
  719. //**********************************************************************
  720. // ------------------------------
  721. // CComPortData::Copy - copy from another data block
  722. //
  723. // Entry: Pointer to other data block
  724. //
  725. // Exit: Nothing
  726. // ------------------------------
  727. #undef DPF_MODNAME
  728. #define DPF_MODNAME "CComPortData::Copy"
  729. void CComPortData::Copy( const CComPortData *const pOtherPort )
  730. {
  731. HRESULT hr;
  732. DNASSERT( pOtherPort != NULL );
  733. // DBG_CASSERT( sizeof( m_ComPortName ) == sizeof( pOtherPort->m_ComPortName ) );
  734. // memcpy( m_ComPortName, pOtherPort->m_ComPortName, sizeof( m_ComPortName ) );
  735. hr = SetDeviceID( pOtherPort->GetDeviceID() );
  736. DNASSERT( hr == DPN_OK );
  737. hr = SetBaudRate( pOtherPort->GetBaudRate() );
  738. DNASSERT( hr == DPN_OK );
  739. hr = SetStopBits( pOtherPort->GetStopBits() );
  740. DNASSERT( hr == DPN_OK );
  741. hr = SetParity( pOtherPort->GetParity() );
  742. DNASSERT( hr == DPN_OK );
  743. hr = SetFlowControl( pOtherPort->GetFlowControl() );
  744. DNASSERT( hr == DPN_OK );
  745. //
  746. // no need to copy comport name because it was set with the device ID
  747. //
  748. // DBG_CASSERT( sizeof( m_ComPortName ) == sizeof( pOtherPort->m_ComPortName ) );
  749. // memcpy( m_ComPortName, pOtherPort->m_ComPortName, sizeof( m_ComPortName ) );
  750. }
  751. //**********************************************************************
  752. //**********************************************************************
  753. // ------------------------------
  754. // CComPortData::ParseDevice - get comport device from string
  755. //
  756. // Entry: Pointer to address component
  757. // Size of address component
  758. // Component type
  759. // Pointer to context (this obejct)
  760. //
  761. // Exit: Error code
  762. // ------------------------------
  763. #undef DPF_MODNAME
  764. #define DPF_MODNAME "CComPortData::ParseDevice"
  765. HRESULT CComPortData::ParseDevice( const void *const pAddressComponent,
  766. const DWORD dwComponentSize,
  767. const DWORD dwComponentType,
  768. void *const pContext )
  769. {
  770. HRESULT hr;
  771. CComPortData *pThisComPortData;
  772. const GUID *pDeviceGuid;
  773. DNASSERT( pAddressComponent != NULL );
  774. DNASSERT( pContext != NULL );
  775. //
  776. // initialize
  777. //
  778. hr = DPN_OK;
  779. pThisComPortData = static_cast<CComPortData*>( pContext );
  780. //
  781. // is this a COM port, and is the name small enough?
  782. //
  783. if ( dwComponentSize != sizeof( *pDeviceGuid ) )
  784. {
  785. DNASSERT( FALSE );
  786. hr = DPNERR_ADDRESSING;
  787. goto Exit;
  788. }
  789. pDeviceGuid = reinterpret_cast<const GUID*>( pAddressComponent );
  790. hr = pThisComPortData->SetDeviceID( GuidToDeviceID( pDeviceGuid, &g_SerialSPEncryptionGuid ) );
  791. if ( hr != DPN_OK )
  792. {
  793. DPFX(DPFPREP, 8, "ParseDevice: couldn't set device ID." );
  794. DisplayDNError( 8, hr );
  795. goto Exit;
  796. }
  797. DNASSERT( hr == DPN_OK );
  798. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_DEVICE ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  799. Exit:
  800. //
  801. // note initialization failures
  802. //
  803. if ( hr != DPN_OK )
  804. {
  805. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_DEVICE ] = SP_ADDRESS_COMPONENT_INITIALIZATION_FAILED;
  806. }
  807. return hr;
  808. }
  809. //**********************************************************************
  810. //**********************************************************************
  811. // ------------------------------
  812. // CComPortData::ParseBaud - get baud rate from string
  813. //
  814. // Entry: Pointer to address component
  815. // Size of component
  816. // Component type
  817. // Pointer to context (this object)
  818. //
  819. // Exit: Error code
  820. // ------------------------------
  821. #undef DPF_MODNAME
  822. #define DPF_MODNAME "CComPortData::ParseBaud"
  823. HRESULT CComPortData::ParseBaud( const void *const pAddressComponent,
  824. const DWORD dwComponentSize,
  825. const DWORD dwComponentType,
  826. void *const pContext )
  827. {
  828. HRESULT hr;
  829. CComPortData *pThisComPortData;
  830. const SP_BAUD_RATE *pBaudRate;
  831. DNASSERT( pAddressComponent != NULL );
  832. DNASSERT( pContext != NULL );
  833. //
  834. // initialize
  835. //
  836. hr = DPN_OK;
  837. pThisComPortData = static_cast<CComPortData*>( pContext );
  838. DNASSERT( sizeof( *pBaudRate ) == dwComponentSize );
  839. pBaudRate = static_cast<const SP_BAUD_RATE*>( pAddressComponent );
  840. hr = pThisComPortData->SetBaudRate( *pBaudRate );
  841. if ( hr != DPN_OK )
  842. {
  843. hr = DPNERR_ADDRESSING;
  844. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_BAUDRATE ] = SP_ADDRESS_COMPONENT_INITIALIZATION_FAILED;
  845. goto Exit;
  846. }
  847. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_BAUDRATE ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  848. Exit:
  849. return hr;
  850. }
  851. //**********************************************************************
  852. //**********************************************************************
  853. // ------------------------------
  854. // CComPortData::ParseStopBits - get stop bits from string
  855. //
  856. // Entry: Pointer to address component
  857. // Component size
  858. // Component type
  859. // Pointer to context (this object)
  860. //
  861. // Exit: Error code
  862. // ------------------------------
  863. #undef DPF_MODNAME
  864. #define DPF_MODNAME "CComPortData::ParseStopBits"
  865. HRESULT CComPortData::ParseStopBits( const void *const pAddressComponent,
  866. const DWORD dwComponentSize,
  867. const DWORD dwComponentType,
  868. void *const pContext )
  869. {
  870. HRESULT hr;
  871. CComPortData *pThisComPortData;
  872. DNASSERT( pAddressComponent != NULL );
  873. DNASSERT( pContext != NULL );
  874. //
  875. // initialize
  876. //
  877. hr = DPN_OK;
  878. pThisComPortData = static_cast<CComPortData*>( pContext );
  879. //
  880. // convert string to value
  881. //
  882. if ( StringToValue( static_cast<const WCHAR*>( pAddressComponent ), // pointer to string
  883. ( ( dwComponentSize / sizeof( WCHAR ) ) - 1 ), // length of string
  884. &pThisComPortData->m_StopBits, // pointer to destination
  885. g_StopBits, // pointer to string/enum pairs
  886. g_dwStopBitsCount // number of string/enum pairs
  887. ) == FALSE )
  888. {
  889. hr = DPNERR_ADDRESSING;
  890. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_STOPBITS ] = SP_ADDRESS_COMPONENT_INITIALIZATION_FAILED;
  891. goto Exit;
  892. }
  893. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_STOPBITS ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  894. Exit:
  895. return hr;
  896. }
  897. //**********************************************************************
  898. //**********************************************************************
  899. // ------------------------------
  900. // CComPortData::ParseParity - get parity from string
  901. //
  902. // Entry: Pointer to address component
  903. // Component size
  904. // Component type
  905. // Pointer to context (this object)
  906. //
  907. // Exit: Error code
  908. // ------------------------------
  909. #undef DPF_MODNAME
  910. #define DPF_MODNAME "CComPortData::ParseParity"
  911. HRESULT CComPortData::ParseParity( const void *const pAddressComponent,
  912. const DWORD dwComponentSize,
  913. const DWORD dwComponentType,
  914. void *const pContext )
  915. {
  916. HRESULT hr;
  917. CComPortData *pThisComPortData;
  918. DNASSERT( pAddressComponent != NULL );
  919. DNASSERT( pContext != NULL );
  920. //
  921. // initialize
  922. //
  923. hr = DPN_OK;
  924. pThisComPortData = static_cast<CComPortData*>( pContext );
  925. //
  926. // convert string to value
  927. //
  928. if ( StringToValue( static_cast<const WCHAR*>( pAddressComponent ), // pointer to string
  929. ( ( dwComponentSize / sizeof( WCHAR ) ) - 1 ), // length of string
  930. &pThisComPortData->m_Parity, // pointer to destination
  931. g_Parity, // pointer to string/enum pairs
  932. g_dwParityCount // number of string/enum pairs
  933. ) == FALSE )
  934. {
  935. hr = DPNERR_ADDRESSING;
  936. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_PARITY ] = SP_ADDRESS_COMPONENT_INITIALIZATION_FAILED;
  937. goto Exit;
  938. }
  939. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_PARITY ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  940. Exit:
  941. return hr;
  942. }
  943. //**********************************************************************
  944. //**********************************************************************
  945. // ------------------------------
  946. // CComPortData::ParseFlowControl - get flow control from string
  947. //
  948. // Entry: Pointer to address component
  949. // Component size
  950. // Component type
  951. // Pointer to context (this object)
  952. //
  953. // Exit: Error code
  954. // ------------------------------
  955. #undef DPF_MODNAME
  956. #define DPF_MODNAME "CComPortData::ParseFlowControl"
  957. HRESULT CComPortData::ParseFlowControl( const void *const pAddressComponent,
  958. const DWORD dwComponentSize,
  959. const DWORD dwComponentType,
  960. void *const pContext )
  961. {
  962. HRESULT hr;
  963. CComPortData *pThisComPortData;
  964. DNASSERT( pAddressComponent != NULL );
  965. DNASSERT( pContext != NULL );
  966. //
  967. // initialize
  968. //
  969. hr = DPN_OK;
  970. pThisComPortData = static_cast<CComPortData*>( pContext );
  971. //
  972. // convert string to value
  973. //
  974. DBG_CASSERT( sizeof( pThisComPortData->m_FlowControl ) == sizeof( VALUE_ENUM_TYPE ) );
  975. if ( StringToValue( static_cast<const WCHAR*>( pAddressComponent ), // pointer to string
  976. ( ( dwComponentSize / sizeof( WCHAR ) ) - 1 ), // length of string
  977. reinterpret_cast<VALUE_ENUM_TYPE*>( &pThisComPortData->m_FlowControl ), // pointer to destination
  978. g_FlowControl, // pointer to string/enum pairs
  979. g_dwFlowControlCount // number of string/enum pairs
  980. ) == FALSE )
  981. {
  982. hr = DPNERR_ADDRESSING;
  983. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_FLOWCONTROL ] = SP_ADDRESS_COMPONENT_INITIALIZATION_FAILED;
  984. goto Exit;
  985. }
  986. pThisComPortData->m_ComponentInitializationState[ COMPORT_PARSE_KEY_FLOWCONTROL ] = SP_ADDRESS_COMPONENT_INITIALIZED;
  987. Exit:
  988. return hr;
  989. }
  990. //**********************************************************************