Source code of Windows XP (NT5)
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.

756 lines
24 KiB

  1. /* Comport.h
  2. *
  3. * Copyright (c) 1993-1995 by DataBeam Corporation, Lexington, KY
  4. *
  5. * Abstract:
  6. * This is the interface file for the ComPort class. This class is the
  7. * interface to the Win32 Comm port.
  8. *
  9. * If this class is instantiated in in-band call control mode, it will
  10. * open the Windows port and set it up properly. It will use the
  11. * configuration data from the Configuration object. Refer to the MCAT
  12. * Developer's Toolkit Manual for a complete listing of the configurable
  13. * items.
  14. *
  15. * If this class is instantiated in out-of-band mode, it is passed a
  16. * file handle. It assumes that this port has been properly initialized.
  17. * It get the configuration data that it needs from the port_configuration
  18. * structure passed in.
  19. *
  20. * Caveats:
  21. * None.
  22. *
  23. * Authors:
  24. * James P. Galvin
  25. * James W. Lawwill
  26. */
  27. #ifndef _T123_COM_PORT_H_
  28. #define _T123_COM_PORT_H_
  29. /*
  30. ** Return values from this class
  31. */
  32. typedef enum
  33. {
  34. COMPORT_NO_ERROR,
  35. COMPORT_INITIALIZATION_FAILED,
  36. COMPORT_NOT_OPEN,
  37. COMPORT_ALREADY_OPEN,
  38. COMPORT_READ_FAILED,
  39. COMPORT_WRITE_FAILED,
  40. COMPORT_CONFIGURATION_ERROR
  41. }
  42. ComPortError, * PComPortError;
  43. /*
  44. ** Miscellaneous Definitions
  45. */
  46. #define OUTPUT_FLOW_CONTROL 0x0001
  47. #define INPUT_FLOW_CONTROL 0x0002
  48. #define DEFAULT_PATH "."
  49. #define DEFAULT_MODEM_TYPE ""
  50. #define DEFAULT_COM_PORT 2
  51. #define DEFAULT_BAUD_RATE 9600
  52. #define DEFAULT_PARITY NOPARITY
  53. #define DEFAULT_DATA_BITS 8
  54. #define DEFAULT_STOP_BITS 1
  55. #define DEFAULT_FLOW_CONTROL OUTPUT_FLOW_CONTROL
  56. // #define DEFAULT_TX_BUFFER_SIZE 0
  57. #define DEFAULT_TX_BUFFER_SIZE 1024
  58. #define DEFAULT_RX_BUFFER_SIZE 10240
  59. #define DEFAULT_READ_INTERVAL_TIMEOUT 10
  60. #define DEFAULT_READ_TOTAL_TIMEOUT_MULTIPLIER 0
  61. #define DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT 100
  62. // #define DEFAULT_INTERNAL_RX_BUFFER_SIZE 1024
  63. #define DEFAULT_INTERNAL_RX_BUFFER_SIZE DEFAULT_RX_BUFFER_SIZE
  64. #define DEFAULT_COUNT_OF_READ_ERRORS 10
  65. #define DEFAULT_BYTE_COUNT_INTERVAL 0
  66. #define COM_TRANSMIT_BUFFER 0x0001
  67. #define COM_RECEIVE_BUFFER 0x0002
  68. #define SYNCHRONOUS_WRITE_TIMEOUT 500
  69. #define MODEM_IDENTIFIER_STRING_LENGTH 16
  70. #define COMPORT_IDENTIFIER_STRING_LENGTH 16
  71. class ComPort : public IProtocolLayer
  72. {
  73. public:
  74. ComPort(TransportController *owner_object,
  75. ULONG message_base,
  76. PLUGXPRT_PARAMETERS *pParams,
  77. PhysicalHandle physical_handle,
  78. HANDLE hevtClose);
  79. virtual ~ComPort(void);
  80. LONG Release(void);
  81. ComPortError Open(void);
  82. ComPortError Close(void);
  83. ComPortError Reset(void);
  84. ComPortError ReleaseReset(void);
  85. ULONG GetBaudRate(void) { return Baud_Rate; }
  86. ProtocolLayerError SynchronousDataRequest(
  87. LPBYTE buffer,
  88. ULONG length,
  89. ULONG *bytes_accepted);
  90. BOOL ProcessReadEvent(void);
  91. BOOL ProcessWriteEvent(void);
  92. /*
  93. ** Functions overridden from the ProtocolLayer object
  94. */
  95. ProtocolLayerError DataRequest (
  96. ULONG_PTR identifier,
  97. LPBYTE buffer_address,
  98. ULONG length,
  99. PULong bytes_accepted);
  100. ProtocolLayerError RegisterHigherLayer (
  101. ULONG_PTR identifier,
  102. PMemoryManager memory_manager,
  103. IProtocolLayer * higher_layer);
  104. ProtocolLayerError RemoveHigherLayer (
  105. ULONG_PTR identifier);
  106. ProtocolLayerError PollReceiver(void);
  107. ProtocolLayerError GetParameters (
  108. USHORT * max_packet_size,
  109. USHORT * prepend,
  110. USHORT * append);
  111. ProtocolLayerError DataRequest (
  112. ULONG_PTR identifier,
  113. PMemory memory,
  114. PULong bytes_accepted);
  115. ProtocolLayerError DataIndication (
  116. LPBYTE buffer_address,
  117. ULONG length,
  118. PULong bytes_accepted);
  119. ProtocolLayerError PollTransmitter (
  120. ULONG_PTR identifier,
  121. USHORT data_to_transmit,
  122. USHORT * pending_data,
  123. USHORT * holding_data);
  124. PLUGXPRT_PSTN_CALL_CONTROL GetCallControlType(void) { return Call_Control_Type; }
  125. BOOL PerformAutomaticDisconnect(void) { return Automatic_Disconnect; }
  126. BOOL IsWriteActive(void) { return Write_Active; }
  127. private:
  128. ProtocolLayerError WriteData(
  129. BOOL synchronous,
  130. LPBYTE buffer_address,
  131. ULONG length,
  132. PULong bytes_accepted);
  133. void ReportInitializationFailure(ComPortError);
  134. private:
  135. LONG m_cRef;
  136. BOOL m_fClosed;
  137. TransportController *m_pController; // owner object
  138. ULONG m_nMsgBase;
  139. BOOL Automatic_Disconnect;
  140. PLUGXPRT_PSTN_CALL_CONTROL Call_Control_Type;
  141. DWORD Count_Errors_On_ReadFile;
  142. ULONG Baud_Rate;
  143. ULONG Tx_Buffer_Size;
  144. ULONG Rx_Buffer_Size;
  145. ULONG Byte_Count;
  146. ULONG Last_Byte_Count;
  147. ULONG m_nReadBufferOffset;
  148. DWORD m_cbRead;
  149. ULONG m_cbReadBufferSize;
  150. LPBYTE m_pbReadBuffer;
  151. HANDLE m_hCommLink;
  152. HANDLE m_hCommLink2;
  153. HANDLE m_hevtClose;
  154. HANDLE m_hevtPendingRead;
  155. HANDLE m_hevtPendingWrite;
  156. PEventObject Write_Event_Object;
  157. PEventObject Read_Event_Object;
  158. OVERLAPPED m_WriteOverlapped;
  159. OVERLAPPED m_ReadOverlapped;
  160. DWORD Event_Mask;
  161. BOOL Read_Active;
  162. BOOL Write_Active;
  163. IProtocolLayer *m_pMultiplexer; // higher layer
  164. };
  165. typedef ComPort * PComPort;
  166. #endif
  167. /*
  168. * Documentation for Public class members
  169. */
  170. /*
  171. * ComPort::ComPort (
  172. * PTransportResources transport_resources,
  173. * PChar port_string,
  174. * IObject * owner_object,
  175. * ULONG message_base,
  176. * BOOL automatic_disconnect,
  177. * PhysicalHandle physical_handle);
  178. *
  179. * Functional Description
  180. * This is a constructor for the ComPort class. It initializes internal
  181. * variables from the configuration object. It opens the Win32 comm port
  182. * and initializes it properly.
  183. *
  184. * Formal Parameters
  185. * transport_resources (i) - Pointer to TransportResources structure.
  186. * port_string (i) - String specifing the configuration heading
  187. * to use.
  188. * owner_object (i) - Pointer to object that owns this object.
  189. * If this object needs to contact its owner,
  190. * it calls the OwnerCallback function.
  191. * message_base (i) - When this object issues an OwnerCallback,
  192. * it should OR the message_base with the
  193. * actual message.
  194. * automatic_disconnect(i) - This object, at some point, will be asked
  195. * if it should break its physical connection
  196. * if the logical connections are broken. The
  197. * owner of this object is telling it how to
  198. * respond.
  199. * physical_handle (i) - This is the handle associated with this
  200. * ComPort. When the ComPort registers an
  201. * event to be monitored, it includes its
  202. * physical_handle.
  203. *
  204. * Return Value
  205. * None
  206. *
  207. * Side Effects
  208. * None
  209. *
  210. * Caveats
  211. * None
  212. */
  213. /*
  214. * ComPort::ComPort (
  215. * PTransportResources transport_resources,
  216. * IObject * owner_object,
  217. * ULONG message_base,
  218. * ULONG handle,
  219. * PPortConfiguration port_configuration,
  220. * PhysicalHandle physical_handle);
  221. *
  222. * Functional Description
  223. * This is a constructor for the ComPort class. It initializes internal
  224. * variables from the port_configuration structure. It uses the file
  225. * handle passed in and prepares to send and receive data.
  226. *
  227. * Formal Parameters
  228. * transport_resources (i) - Pointer to TransportResources structure.
  229. * owner_object (i) - Pointer to object that owns this object.
  230. * If this object needs to contact its owner,
  231. * it calls the OwnerCallback function.
  232. * message_base (i) - When this object issues an OwnerCallback,
  233. * it should OR the message_base with the
  234. * actual message.
  235. * handle (i) - File handle to be used as the comm port.
  236. * port_configuration (i) - Pointer to PortConfiguration structure.
  237. * physical_handle (i) - This is the handle associated with this
  238. * ComPort. When the ComPort registers an
  239. * event to be monitored, it includes its
  240. * physical_handle.
  241. *
  242. * Return Value
  243. * None
  244. *
  245. * Side Effects
  246. * None
  247. *
  248. * Caveats
  249. * None
  250. */
  251. /*
  252. * ComPort::~Comport (void)
  253. *
  254. * Functional Description
  255. * This is the destructor for the Comport class. It releases all memory
  256. * that was used by the class and deletes all timers
  257. *
  258. * Formal Parameters
  259. * None
  260. *
  261. * Return Value
  262. * None
  263. *
  264. * Side Effects
  265. * None
  266. *
  267. * Caveats
  268. * None
  269. */
  270. /*
  271. * ComPortError ComPort::Open (void);
  272. *
  273. * Functional Description
  274. * This function opens the comm port and configures it with the values
  275. * found in the configuration object. It uses the Physical_API_Enabled
  276. * flag in the g_TransportResource structure to determine if it needst to
  277. * open the comm port.
  278. *
  279. * Formal Parameters
  280. * None
  281. *
  282. * Return Value
  283. * COM_NO_ERROR - Successful open and configuration
  284. * COM_INITIALIZATION_FAILED - One of many problems could have
  285. * occurred. For example, the com
  286. * port is open by another application
  287. * or one of the parameters in the
  288. * configuration file is improper.
  289. *
  290. * When this error occurs, a callback is
  291. * made to the user that indicates there
  292. * is an error condition.
  293. *
  294. * Side Effects
  295. * None
  296. *
  297. * Caveats
  298. * None
  299. */
  300. /*
  301. * ComPortError ComPort::Close (void);
  302. *
  303. * Functional Description
  304. * If the Physical API is not enabled, this function makes the necessary
  305. * calls to close the Comm Windows port. It first clears the DTR signal
  306. * to haugup the modem.
  307. *
  308. * Regardless of the Physical API being enabled, it also flushes the comm
  309. * buffers.
  310. *
  311. * Formal Parameters
  312. * None
  313. *
  314. * Return Value
  315. * COMPORT_NO_ERROR - Com Port closed successfully
  316. * COMPORT_NOT_OPEN - Com Port is not open, we can't close it
  317. *
  318. * Side Effects
  319. * None
  320. *
  321. * Caveats
  322. * None
  323. */
  324. /*
  325. * ComPortError ComPort::Reset (void);
  326. *
  327. * Functional Description
  328. * This function clears the DTR signal on the Com port.
  329. *
  330. * Formal Parameters
  331. * None
  332. *
  333. * Return Value
  334. * COMPORT_NO_ERROR - Com port reset
  335. * COMPORT_NOT_OPEN - Com port is not open, we can't access it
  336. *
  337. * Side Effects
  338. * None
  339. *
  340. * Caveats
  341. * None
  342. */
  343. /*
  344. * ComPortError ComPort::ReleaseReset (void);
  345. *
  346. * Functional Description
  347. * This function releases the previous reset. It sets the DTR signal on
  348. * the com port.
  349. *
  350. * Formal Parameters
  351. * None
  352. *
  353. * Return Value
  354. * COMPORT_NO_ERROR - Com port reset
  355. * COMPORT_NOT_OPEN - Com port is not open, we can't access it
  356. *
  357. * Side Effects
  358. * None
  359. *
  360. * Caveats
  361. * None
  362. */
  363. /*
  364. * ComPortError ComPort::FlushBuffers (
  365. * USHORT buffer_mask)
  366. *
  367. * Functional Description
  368. * This function issues the Windows cals to flush the input and/or
  369. * output buffers.
  370. *
  371. * Formal Parameters
  372. * buffer_mask - (i) If COM_TRANSMIT_BUFFER is set, we flush the
  373. * output buffer.
  374. * If COM_RECEIVE_BUFFER is set, we flush the
  375. * input buffer.
  376. *
  377. * Return Value
  378. * COMPORT_NO_ERROR - Successful operation
  379. * COMPORT_NOT_OPEN - Com port is not open, we can't access it
  380. *
  381. * Side Effects
  382. * None
  383. *
  384. * Caveats
  385. * None
  386. */
  387. /*
  388. * ULONG ComPort::GetBaudRate (void);
  389. *
  390. * Functional Description
  391. * This function returns the baud rate of the port
  392. *
  393. * Formal Parameters
  394. * None.
  395. *
  396. * Return Value
  397. * Baud rate of the port.
  398. *
  399. * Side Effects
  400. * None
  401. *
  402. * Caveats
  403. * None
  404. */
  405. /*
  406. * ProtocolLayerError ComPort::SynchronousDataRequest (
  407. * FPUChar buffer_address,
  408. * ULONG length,
  409. * FPULong bytes_accepted)
  410. *
  411. * Functional Description:
  412. * This function is called to send data out the port in a synchronous
  413. * manner. In other words, we will not return from the function until
  414. * all of the bytes are actually written to the modem or a timeout occurs.
  415. *
  416. * Formal Parameters
  417. * buffer_address (i) - Address of buffer to write.
  418. * length (i) - Length of buffer
  419. * bytes_accepted (o) - Number of bytes actually written
  420. *
  421. * Return Value
  422. * PROTOCOL_LAYER_NO_ERROR - Success
  423. * PROTOCOL_LAYER_ERROR - Failure
  424. *
  425. * Side Effects
  426. * None
  427. *
  428. * Caveats
  429. * None
  430. */
  431. /*
  432. * BOOL ComPort::ProcessReadEvent (void)
  433. *
  434. * Functional Description:
  435. * This function is called when a READ event is actually set. This means
  436. * that the read operation has completed or an error occured.
  437. *
  438. * Formal Parameters
  439. * None.
  440. *
  441. * Side Effects
  442. * None
  443. *
  444. * Caveats
  445. * None
  446. */
  447. /*
  448. * BOOL ComPort::ProcessWriteEvent (
  449. * HANDLE event);
  450. *
  451. * Functional Description:
  452. * This function is called when a WRITE event is actually set. This means
  453. * that the write operation has completed or an error occured.
  454. *
  455. * Formal Parameters
  456. * event (i) - Object event that occured
  457. *
  458. * Side Effects
  459. * None
  460. *
  461. * Caveats
  462. * None
  463. */
  464. /*
  465. * BOOL ComPort::ProcessControlEvent (void)
  466. *
  467. * Functional Description:
  468. * This function is called when a CONTROL event is actually set. This
  469. * means that the CONTROL operation has occured. In our case the RLSD
  470. * signal has changed.
  471. *
  472. * Formal Parameters
  473. * None.
  474. *
  475. * Side Effects
  476. * None
  477. *
  478. * Caveats
  479. * None
  480. */
  481. /*
  482. * ProtocolLayerError DataLink::DataRequest (
  483. * ULONG identifier,
  484. * LPBYTE buffer_address,
  485. * USHORT length,
  486. * USHORT * bytes_accepted);
  487. *
  488. * Functional Description
  489. * This function is called by a higher layer to request transmission of
  490. * a packet.
  491. *
  492. * Formal Parameters
  493. * identifier (i) - Identifier of the higher layer
  494. * buffer_address (i) - Buffer address
  495. * length (i) - Length of packet to transmit
  496. * bytes_accepted (o) - Number of bytes accepted by the DataLink.
  497. * This value will either be 0 or the packet
  498. * length since this layer has a packet interface.
  499. *
  500. * Return Value
  501. * PROTOCOL_LAYER_NO_ERROR - No error occured
  502. *
  503. * Side Effects
  504. * None
  505. *
  506. * Caveats
  507. * None
  508. */
  509. /*
  510. * ProtocolLayerError DataLink::RegisterHigherLayer (
  511. * ULONG identifier,
  512. * PMemoryManager memory_manager,
  513. * IProtocolLayer * higher_layer);
  514. *
  515. * Functional Description
  516. * This function is called by the higher layer to register its identifier
  517. * and its address. When this object needs to send a packet up, it calls
  518. * the higher_layer with a Data Indication
  519. *
  520. * Formal Parameters
  521. * identifier (i) - Unique identifier of the higher layer. If we
  522. * were doing multiplexing at this layer, this
  523. * would have greater significance.
  524. * memory_manager (i) - Pointer to outbound memory manager
  525. * higher_layer (i) - Address of higher layer
  526. *
  527. * Return Value
  528. * PROTOCOL_LAYER_NO_ERROR - No error occured
  529. * PROTOCOL_LAYER_REGISTRATION_ERROR - Error occured on registration
  530. *
  531. * Side Effects
  532. * None
  533. *
  534. * Caveats
  535. * None
  536. *
  537. */
  538. /*
  539. * ProtocolLayerError DataLink::RemoveHigherLayer (
  540. * ULONG);
  541. *
  542. * Functional Description
  543. * This function is called by the higher layer to remove its identifier
  544. * and its address. If the higher layer removes itself from us, we have
  545. * no place to send incoming data
  546. *
  547. * Formal Parameters
  548. * None used
  549. *
  550. * Return Value
  551. * PROTOCOL_LAYER_NO_ERROR - No error occured
  552. *
  553. * Side Effects
  554. * None
  555. *
  556. * Caveats
  557. * None
  558. */
  559. /*
  560. * ProtocolLayerError DataLink::PollReceiver (
  561. * ULONG identifier);
  562. *
  563. * Functional Description
  564. * This function is called to give the DataLink a chance pass packets
  565. * to higher layers
  566. *
  567. * Formal Parameters
  568. * identifier (i) - Not used
  569. *
  570. * Return Value
  571. * PROTOCOL_LAYER_NO_ERROR - No error occured
  572. *
  573. * Side Effects
  574. * None
  575. *
  576. * Caveats
  577. * None
  578. */
  579. /*
  580. * ProtocolLayerError DataLink::GetParameters (
  581. * ULONG identifier,
  582. * USHORT * max_packet_size,
  583. * USHORT * prepend_size,
  584. * USHORT * append_size);
  585. *
  586. * Functional Description:
  587. * This function returns the maximum packet size that it can handle via
  588. * its DataRequest() function.
  589. *
  590. * Formal Parameters
  591. * identifier (i) - Not used
  592. * max_packet_size (o) - Address to return max. packet size in.
  593. * prepend_size (o) - Return number of bytes prepended to each packet
  594. * append_size (o) - Return number of bytes appended to each packet
  595. *
  596. * Return Value
  597. * PROTOCOL_LAYER_NO_ERROR - No error occured
  598. *
  599. * Side Effects
  600. * None
  601. *
  602. * Caveats
  603. * None
  604. */
  605. /*
  606. * BOOL ComPort::PerformAutomaticDisconnect (void)
  607. *
  608. * Functional Description:
  609. * This function returns TRUE if we want to terminate a physical connection
  610. * as soon as the logical connections are disconnected.
  611. *
  612. * Formal Parameters
  613. * None
  614. *
  615. * Return Value
  616. * TRUE - If we want to drop the physical connection after all
  617. * logical connections are dropped.
  618. *
  619. * Side Effects
  620. * None
  621. *
  622. * Caveats
  623. * None
  624. */
  625. /*
  626. * ProtocolLayerError DataLink::DataRequest (
  627. * ULONG identifier,
  628. * PMemory memory,
  629. * PULong bytes_accepted);
  630. *
  631. * Functional Description
  632. * This function is called by a higher layer to request transmission of
  633. * a packet.
  634. *
  635. * Formal Parameters
  636. * identifier (i) - Identifier of the higher layer
  637. * memory (i) - Pointer to memory object
  638. * bytes_accepted (o) - Number of bytes accepted by the DataLink.
  639. * This value will either be 0 or the packet
  640. * length since this layer has a packet interface.
  641. *
  642. * Return Value
  643. * PROTOCOL_LAYER_ERROR - Not supported.
  644. *
  645. * Side Effects
  646. * None
  647. *
  648. * Caveats
  649. * None
  650. */
  651. /*
  652. * ProtocolLayerError ComPort::DataIndication (
  653. * LPBYTE buffer_address,
  654. * ULONG length,
  655. * PULong bytes_accepted);
  656. *
  657. * Functional Description
  658. * This function will never be called. It is only here because this
  659. * class inherits from ProtocolLayer.
  660. *
  661. * Formal Parameters
  662. * buffer_address (i) - Buffer address
  663. * memory (i) - Pointer to memory object
  664. * bytes_accepted (o) - Number of bytes accepted by the DataLink.
  665. * This value will either be 0 or the packet
  666. * length since this layer has a packet interface.
  667. *
  668. * Return Value
  669. * PROTOCOL_LAYER_ERROR - Not supported.
  670. *
  671. * Side Effects
  672. * None
  673. *
  674. * Caveats
  675. * None
  676. */
  677. /*
  678. * ProtocolLayerError ComPort::PollTransmitter (
  679. * ULONG identifier,
  680. * USHORT data_to_transmit,
  681. * USHORT * pending_data,
  682. * USHORT * holding_data);
  683. *
  684. * Functional Description
  685. * This function does nothing.
  686. *
  687. * Formal Parameters
  688. * None used.
  689. *
  690. * Return Value
  691. * PROTOCOL_LAYER_NO_ERROR - No error occured
  692. *
  693. * Side Effects
  694. * None
  695. *
  696. * Caveats
  697. * None
  698. */
  699.