summaryrefslogtreecommitdiffstats
path: root/src/serialportengine_p_win.h
blob: ba1aa956525eeea27cd8716ed1f68906eea3eaa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
    License...
*/

#ifndef SERIALPORTENGINE_P_WIN_H
#define SERIALPORTENGINE_P_WIN_H

#include "serialport.h"
#include "serialportengine_p.h"

#include <qt_windows.h>
#if defined (Q_OS_WINCE)
#  include <QtCore/qmutex.h>
#  include <QtCore/qthread.h>
#  include <QtCore/qtimer.h>
#else
#  include <QtCore/private/qwineventnotifier_p.h>
#endif

QT_BEGIN_NAMESPACE

#if defined (Q_OS_WINCE)

class WinCeWaitCommEventBreaker : public QThread
{
    Q_OBJECT
public:
    WinCeWaitCommEventBreaker(HANDLE descriptor, int timeout, QObject *parent = 0)
        : QThread(parent), m_descriptor(descriptor),
          m_timeout(timeout), m_worked(false) {
        start();
    }
    virtual ~WinCeWaitCommEventBreaker() {
        stop();
        wait();
    }
    void stop() { exit(0); }
    bool isWorked() const { return m_worked; }

protected:
    void run() {
        QTimer timer;
        QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(procTimeout()), Qt::DirectConnection);
        timer.start(m_timeout);
        exec();
        m_worked = true;
    }

private slots:
    void procTimeout() {
        ::SetCommMask(m_descriptor, 0);
        stop();
    }

private:
    HANDLE m_descriptor;
    int m_timeout;
    volatile bool m_worked;
};
#endif

#if defined (Q_OS_WINCE)
class WinSerialPortEngine : public QThread, public SerialPortEngine
        #else
class WinSerialPortEngine : public QWinEventNotifier, public SerialPortEngine
        #endif
{
    Q_OBJECT
public:
    WinSerialPortEngine(SerialPortPrivate *parent);
    virtual ~WinSerialPortEngine();

    virtual bool open(const QString &location, QIODevice::OpenMode mode);
    virtual void close(const QString &location);

    virtual SerialPort::Lines lines() const;

    virtual bool setDtr(bool set);
    virtual bool setRts(bool set);

    virtual bool flush();
    virtual bool reset();

    virtual bool sendBreak(int duration);
    virtual bool setBreak(bool set);

    virtual qint64 bytesAvailable() const;
    virtual qint64 bytesToWrite() const;

    virtual qint64 read(char *data, qint64 len);
    virtual qint64 write(const char *data, qint64 len);
    virtual bool select(int timeout,
                        bool checkRead, bool checkWrite,
                        bool *selectForRead, bool *selectForWrite);

    virtual QString toSystemLocation(const QString &port) const;
    virtual QString fromSystemLocation(const QString &location) const;

    virtual bool setRate(qint32 rate, SerialPort::Directions dir);
    virtual bool setDataBits(SerialPort::DataBits dataBits);
    virtual bool setParity(SerialPort::Parity parity);
    virtual bool setStopBits(SerialPort::StopBits stopBits);
    virtual bool setFlowControl(SerialPort::FlowControl flowControl);

    virtual bool setDataErrorPolicy(SerialPort::DataErrorPolicy policy);

    virtual bool isReadNotificationEnabled() const;
    virtual void setReadNotificationEnabled(bool enable);
    virtual bool isWriteNotificationEnabled() const;
    virtual void setWriteNotificationEnabled(bool enable);

    virtual bool processIOErrors();

#if defined (Q_OS_WINCE)
    virtual void lockNotification(NotificationLockerType type, bool uselocker);
    virtual void unlockNotification(NotificationLockerType type);
#endif

protected:
    virtual void detectDefaultSettings();

#if defined (Q_OS_WINCE)
    virtual void run();
#else
    virtual bool event(QEvent *e);
#endif

private:
    DCB m_currDCB;
    DCB m_oldDCB;
    COMMTIMEOUTS m_currCommTimeouts;
    COMMTIMEOUTS m_oldCommTimeouts;
    HANDLE m_descriptor;
    bool m_flagErrorFromCommEvent;
    DWORD m_currentMask;
    DWORD m_setMask;

#if defined (Q_OS_WINCE)
    QMutex m_readNotificationMutex;
    QMutex m_writeNotificationMutex;
    QMutex m_errorNotificationMutex;
    QMutex m_settingsChangeMutex;
    QMutex m_setCommMaskMutex;
    volatile bool m_running;
#else
    OVERLAPPED m_ovRead;
    OVERLAPPED m_ovWrite;
    OVERLAPPED m_ovSelect;
    OVERLAPPED m_ov;

    bool createEvents(bool rx, bool tx);
    void closeEvents();
    void setMaskAndActivateEvent();
#endif

    bool updateDcb();
    bool updateCommTimeouts();
};

QT_END_NAMESPACE

#endif // SERIALPORTENGINE_P_WIN_H