/* ------------------------------------------------------------------------ --
--                                                                          --
--                        PC serial port connection object                  --
--                           for  event-driven programs                     --
--                                                                          --
--                                                                          --
--                                                                          --
--  Copyright @ 2001-2002     Thierry Schneider                             --
--                            thierry@tetraedre.com                         --
--                                                                          --
--                                                                          --
--                                                                          --
-- ------------------------------------------------------------------------ --
--                                                                          --
--  Filename : Tserial_event.h                                              --
--  Author   : Thierry Schneider                                            --
--  Created  : April 4th 2000                                               --
--  Modified : June 22nd 2002                                               --
--  Plateform: Windows 95, 98, NT, 2000, XP (Win32)                         --
-- ------------------------------------------------------------------------ --
--                                                                          --
--  This software is given without any warranty. It can be distributed      --
--  free of charge as long as this header remains, unchanged.               --
--                                                                          --
-- ------------------------------------------------------------------------ --
--                                                                          --
-- ------------------------------------------------------------------------ */


#ifndef TSERIAL_EVENT_H
#define TSERIAL_EVENT_H

#include <stdio.h>
#include <windows.h>

#define SERIAL_PARITY_NONE 0
#define SERIAL_PARITY_ODD  1
#define SERIAL_PARITY_EVEN 2

#define SERIAL_STOP_1 1
#define SERIAL_STOP_2 2

#define SERIAL_CONNECTED         0
#define SERIAL_DISCONNECTED      1
#define SERIAL_DATA_SENT         2
#define SERIAL_DATA_ARRIVAL      3
#define SERIAL_RING_ON           4
#define SERIAL_RING_OFF          5
#define SERIAL_CD_ON             6
#define SERIAL_CD_OFF            7
#define SERIAL_CTS_ON            8
#define SERIAL_CTS_OFF           9
#define SERIAL_DSR_ON            10
#define SERIAL_DSR_OFF           11
#define SERIAL_BREAK			 12
#define SERIAL_ERR				 13

typedef unsigned long uint32;
typedef void (*type_myCallBack) (uint32 object, uint32 event);

#ifndef __BORLANDC__
#define bool  BOOL 
#define true  TRUE
#define false FALSE
#endif

#define SERIAL_SIGNAL_NBR 7         // number of events in the thread
// events
#define SIG_POWER_DOWN     0
#define SIG_READER         1
#define SIG_READ_DONE      2    // data received has been read
#define SIG_WRITER         3
#define SIG_DATA_TO_TX     4    // data waiting to be sent
#define SIG_MODEM_EVENTS   5
#define SIG_MODEM_CHECKED  6

#define SERIAL_MAX_RX     256       // Input buffer max size
#define SERIAL_MAX_TX     256       // output buffer max size

/* -------------------------------------------------------------------- */
/* -----------------------------  Tserial  ---------------------------- */
/* -------------------------------------------------------------------- */
class Tserial_event
{
    // -------------------------------------------------------- //
protected:
    bool          ready;
    bool          check_modem;
    TCHAR         port[25];                          // port name "com1",...
    int           rate;                              // baudrate
    int           parityMode;
    int			  stopbits;

    HANDLE        serial_events[SERIAL_SIGNAL_NBR];  // events to wait on
    unsigned int  threadid;                          // ...
    HANDLE        serial_handle;                     // ...
    HANDLE        thread_handle;                     // ...
    OVERLAPPED    ovReader;                          // Overlapped structure for ReadFile
    OVERLAPPED    ovWriter;                          // Overlapped structure for WriteFile
    OVERLAPPED    ovWaitEvent;                       // Overlapped structure for WaitCommEvent
    char          tx_in_progress;                    // BOOL indicating if a WriteFile is
                                                     // in progress
    char          rx_in_progress;                    // BOOL indicating if a ReadFile is
                                                     // in progress
    char          WaitCommEventInProgress;
    char          rxBuffer[SERIAL_MAX_RX];
    int           max_rx_size;
    int           received_size;
    char          txBuffer[SERIAL_MAX_TX];
    int           tx_size;
    DWORD         dwCommEvent;                       // to store the result of the wait

    type_myCallBack manager;

    // ............................................................
    void          OnCharArrival    (char c);
    void          OnModemEvent     (unsigned long events);


    // ++++++++++++++++++++++++++++++++++++++++++++++
    // .................. EXTERNAL VIEW .............
    // ++++++++++++++++++++++++++++++++++++++++++++++
public:
    void         *owner;                // do what you want with this
    void          run(void);
                  Tserial_event();
                 ~Tserial_event();
    int           connect(TCHAR *port, int rate, int parity,
						char ByteSize, int stop, bool modem_events);

    void          setManager       (type_myCallBack manager);
    void          setRxSize        (int size);
    int           sendData         (char *buffer, int size);
    int           sendDataChar     (char d);
    int           getNbrOfBytes    (void);
    int           getDataInSize    (void);
    char *        getDataInBuffer  (void);
    void          dataHasBeenRead  (void);
    DWORD         getCommError     (void);
    int			  getThreadId	   (void);
    void          disconnect       (void);
    void		  RTS			   (bool state);
    void		  DTR			   (bool state);
    void		  BREAK			   (bool state);
    bool		CTS_State			(void);
    bool		DSR_State			(void);
    bool		CD_State			(void);

    int DoneReason;	// Event Loop Completion Reason
};
/* -------------------------------------------------------------------- */

#endif TSERIAL_EVENT_H


