summaryrefslogtreecommitdiffstats
path: root/src/serialport.cpp
blob: 3fe766b559bf8b13804505040d196c536fb7c680 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
/*
    License...
*/

#include "serialport.h"
#include "serialportinfo.h"
#include "serialport_p.h"
#include "serialportengine_p.h"

#if QT_VERSION >= 0x040700
#  include <QtCore/qelapsedtimer.h>
#else
#  include <QtCore/qdatetime.h>
#endif

#ifndef SERIALPORT_BUFFERSIZE
#  define SERIALPORT_BUFFERSIZE 16384
#endif

QT_BEGIN_NAMESPACE_SERIALPORT

//----------------------------------------------------------------

/* Public methods */

/*! \internal

    Constructs a SerialPortPrivate. Initializes all members.
*/
SerialPortPrivate::SerialPortPrivate(SerialPort *parent)
    : m_readBufferMaxSize(0)
    , m_readBuffer(SERIALPORT_BUFFERSIZE)
    , m_writeBuffer(SERIALPORT_BUFFERSIZE)
    , m_isBuffered(false)
    , m_readSerialNotifierCalled(false)
    , m_readSerialNotifierState(false)
    , m_readSerialNotifierStateSet(false)
    , m_emittedReadyRead(false)
    , m_emittedBytesWritten(false)
    , m_engine(0)
    , q_ptr(parent)
    , m_inRate(SerialPort::UnknownRate)
    , m_outRate(SerialPort::UnknownRate)
    , m_dataBits(SerialPort::UnknownDataBits)
    , m_parity(SerialPort::UnknownParity)
    , m_stopBits(SerialPort::UnknownStopBits)
    , m_flow(SerialPort::UnknownFlowControl)
    , m_policy(SerialPort::IgnorePolicy)
    , m_portError(SerialPort::NoError)
    , m_restoreSettingsOnClose(true)
{
    m_engine = SerialPortEngine::create(this);
    Q_ASSERT(m_engine);
}

/*! \internal

    Destructs the SerialPort. Also if the native engine is
    exists, then delete it.
*/
SerialPortPrivate::~SerialPortPrivate()
{
    if (m_engine)
        delete m_engine;
}

/*! \internal

    Sets the internal variable system location, by converting
    the serial \a port name. Converting serial port name does
    native engine, this conversion is platform-dependent.
    That is, performed the converting inverse by
    method SerialPortPrivate::port().
*/
void SerialPortPrivate::setPort(const QString &port)
{
    m_systemLocation = m_engine->toSystemLocation(port);
}

/*! \internal

    Returns the serial port name obtained by converting internal
    the system location variable. Converting system location does
    native engine, this conversion is platform-dependent. That is,
    performed the converting inverse by method
    SerialPortPrivate::setPort().
*/
QString SerialPortPrivate::port() const
{
    return m_engine->fromSystemLocation(m_systemLocation);
}

/*! \internal

    Opens the serial port with a given internal system location
    vaeiable for the given open \a mode. This operation is
    platform-dependent, and it provides native engine.
*/
bool SerialPortPrivate::open(QIODevice::OpenMode mode)
{
    return m_engine->open(m_systemLocation, mode);
}

/*! \internal

    Closes the serial port specified in the internal variable
    as system location. This operation is platform-dependent,
    and it provides native engine.
*/
void SerialPortPrivate::close()
{
    m_engine->close(m_systemLocation);
}

/*! \internal

    Sets the desired speed \a rate for a given direction \a dir
    for the serial port. This operation is platform-dependent,
    and it provides native engine.
*/
bool SerialPortPrivate::setRate(qint32 rate, SerialPort::Directions dir)
{
    if (m_engine->setRate(rate, dir)) {
        if (dir & SerialPort::Input)
            m_inRate = rate;
        if (dir & SerialPort::Output)
            m_outRate = rate;
        return true;
    }
    return false;
}

/*! \internal

    Returns the current value of the speed of a given direction
    \a dir from the corresponding internal variables.
*/
qint32 SerialPortPrivate::rate(SerialPort::Directions dir) const
{
    if (dir == SerialPort::AllDirections)
        return (m_inRate == m_outRate) ? (m_inRate) : SerialPort::UnknownRate;
    return (dir & SerialPort::Input) ? (m_inRate) : (m_outRate);
}

/*! \internal

    Sets the desired number of data bits \a dataBits in byte
    for the serial port. This operation is platform-dependent,
    and it provides native engine.
*/
bool SerialPortPrivate::setDataBits(SerialPort::DataBits dataBits)
{
    if (m_engine->setDataBits(dataBits)) {
        m_dataBits = dataBits;
        return true;
    }
    return false;
}

/*! \internal

    Returns the current number of data bits in byte from the
    corresponding internal variable.
*/
SerialPort::DataBits SerialPortPrivate::dataBits() const
{
    return m_dataBits;
}

/*! \internal

    Sets the desired \a parity control mode for the serial port.
    This operation is platform-dependent, and it provides
    native engine.
*/
bool SerialPortPrivate::setParity(SerialPort::Parity parity)
{
    if (m_engine->setParity(parity)) {
        m_parity = parity;
        return true;
    }
    return false;
}

/*! \internal

    Returns the current parity control mode from the
    corresponding internal variable.
*/
SerialPort::Parity SerialPortPrivate::parity() const
{
    return m_parity;
}

/*! \internal

    Sets the desired number of stop bits \a stopBits in frame for
    the serial port. This operation is platform-dependent, and
    it provides native engine.
*/
bool SerialPortPrivate::setStopBits(SerialPort::StopBits stopBits)
{
    if (m_engine->setStopBits(stopBits)) {
        m_stopBits = stopBits;
        return true;
    }
    return false;
}

/*! \internal

    Returns the current number of stop bits in frame from the
    corresponding internal variable.
*/
SerialPort::StopBits SerialPortPrivate::stopBits() const
{
    return m_stopBits;
}

/*! \internal

    Sets the desired \a flow control mode for the serial port.
    This operation is platform-dependent, and it provides
    native engine.
*/
bool SerialPortPrivate::setFlowControl(SerialPort::FlowControl flow)
{
    if (m_engine->setFlowControl(flow)) {
        m_flow = flow;
        return true;
    }
    return false;
}

/*! \internal

    Returns the current flow comtrol mode from the
    corresponding internal variable.
*/
SerialPort::FlowControl SerialPortPrivate::flowControl() const
{
    return m_flow;
}

/*! \internal

    Returns DTR signal state. This operation is platform-dependent,
    and it provides native engine.
*/
bool SerialPortPrivate::dtr() const
{
    return m_engine->lines() & SerialPort::Dtr;
}

/*! \internal

    Returns RTS signal state. This operation is platform-dependent,
    and it provides native engine.
*/
bool SerialPortPrivate::rts() const
{
    return m_engine->lines() & SerialPort::Rts;
}

/*! \internal

    Set desired DTR signal state \a set. This operation is
    platform-dependent, and it provides native engine.
*/
bool SerialPortPrivate::setDtr(bool set)
{
    return m_engine->setDtr(set);
}

/*! \internal

    Set desired DTR signal state \a set. This operation is
    platform-dependent, and it provides native engine.
*/
bool SerialPortPrivate::setRts(bool set)
{
    return m_engine->setRts(set);
}

/*! \internal

    Returns the bitmap signals of RS-232 lines. This operation is
    platform-dependent, and it provides native engine.
*/
SerialPort::Lines SerialPortPrivate::lines() const
{
    return m_engine->lines();
}

/*! \internal

    Writes pending data in the write buffers to the serial port.
    The function writes as much as it can without blocking.

    It is usually invoked by canWriteNotification after one or more
    calls to write().

    Emits bytesWritten().
*/
bool SerialPortPrivate::flush()
{
    Q_Q(SerialPort);

    if (m_writeBuffer.isEmpty())
        return false;

    int nextSize = m_writeBuffer.nextDataBlockSize();
    const char *ptr = m_writeBuffer.readPointer();

    // Attempt to write it all in one chunk.
    qint64 written = write(ptr, nextSize);
    if (written < 0) {
        m_writeBuffer.clear();
        return false;
    }

    // Remove what we wrote so far.
    m_writeBuffer.free(written);
    if (written > 0) {
        // Don't emit bytesWritten() recursively.
        if (!m_emittedBytesWritten) {
            m_emittedBytesWritten = true;
            emit q->bytesWritten(written);
            m_emittedBytesWritten = false;
        }
    }

    if (m_writeBuffer.isEmpty() && m_engine->isWriteNotificationEnabled())
        m_engine->setWriteNotificationEnabled(false);

    return true;
}

/*! \internal

    Resets and clears the receiving and transmitting driver buffers
    (UART). This operation is platform-dependent, and it set
    provides native engine.
*/
bool SerialPortPrivate::reset()
{
    return m_engine->reset();
}

/*! \internal

    Sends a continuous stream of zero bits during a specified period
    of time \a duration in msec. This operation is platform-dependent,
    and it set provides native engine.
*/
bool SerialPortPrivate::sendBreak(int duration)
{
    return m_engine->sendBreak(duration);
}

/*! \internal

    Control the signal break, depending on the \a set. This operation
    is platform-dependent, and it set provides native engine.
*/
bool SerialPortPrivate::setBreak(bool set)
{
    return m_engine->setBreak(set);
}

/*! \internal

    Sets the type of \a policy processing received symbol at parity
    error is detected the corresponding internal variable. The value
    of this variable is further taken into account when processing
    received symbol in the implementation of native methods of reading
    in native engine.
*/
bool SerialPortPrivate::setDataErrorPolicy(SerialPort::DataErrorPolicy policy)
{
    const bool ret = (policy == m_policy) || m_engine->setDataErrorPolicy(policy);
    if (ret)
        m_policy = policy;
    return ret;
}

/*! \internal

    Returns the current value internal policy variable.
*/
SerialPort::DataErrorPolicy SerialPortPrivate::dataErrorPolicy() const
{
    return m_policy;
}

/*! \internal

    Returns the current error code.
*/
SerialPort::PortError SerialPortPrivate::error() const
{
    return m_portError;
}

/*! \internal

    Clears error code.
*/
void SerialPortPrivate::unsetError()
{
    m_portError = SerialPort::NoError;
}

/*! \internal

    Set \a error code in the corresponding internal variable.
*/
void SerialPortPrivate::setError(SerialPort::PortError error)
{
    m_portError = error;
}

/*! \internal

    Returns the number of bytes available for reading, which
    are in the receive buffer driver (UART). This operation
    is platform-dependent, and it set provides native engine.
*/
qint64 SerialPortPrivate::bytesAvailable() const
{
    return m_engine->bytesAvailable();
}

/*! \internal

    Returns the number of bytes ready to send to that are in
    the transmit buffer driver (UART). This operation
    is platform-dependent, and it set provides native engine.
*/
qint64 SerialPortPrivate::bytesToWrite() const
{
    return m_engine->bytesToWrite();
}

/*! \internal

    Reads data into the pointer \a data of length \a len,
    which are located in the receive buffer driver (UART).
    This operation is platform-dependent, and it set provides
    native engine.
*/
qint64 SerialPortPrivate::read(char *data, qint64 len)
{
    return m_engine->read(data, len);
}

/*! \internal

    Write data from the pointer \a data of length \a len,
    to the transmit buffer driver (UART).
    This operation is platform-dependent, and it set provides
    native engine.
*/
qint64 SerialPortPrivate::write(const char *data, qint64 len)
{
    return m_engine->write(data, len);
}

/*! \internal

    Implements a generic "wait" method, expected within the \a timeout
    in millisecond occurrence of any of the two events:

    - appearance in the reception buffer driver (UART) at least one
    character, if the flag \a checkRead is set on true
    - devastation of the transmit buffer driver (UART) with the transfer
    of the last character, if the flag \a checkWrite is set on true

    Also, all these events can happen simultaneously. The result of
    catch of the corresponding event returns to the variables
    \a selectForRead and \a selectForWrite. This operation is
    platform-dependent, and it set provides native engine.
*/
bool SerialPortPrivate::waitForReadOrWrite(int timeout,
                                           bool checkRead, bool checkWrite,
                                           bool *selectForRead, bool *selectForWrite)
{
    return m_engine->select(timeout, checkRead, checkWrite, selectForRead, selectForWrite);
}

/*! \internal

    Clears internal read and write buffers of the class.
*/
void SerialPortPrivate::clearBuffers()
{
    m_writeBuffer.clear();
    m_readBuffer.clear();
}

/*! \internal

    Reads data from the serial port into the read buffer. Returns
    true on success; otherwise false. This operation takes place
    automatically when the driver (UART) in the input buffer have
    of at least one byte, i.e. by event.
*/
bool SerialPortPrivate::readFromPort()
{
    qint64 bytesToRead = (m_policy == SerialPort::IgnorePolicy) ?
                bytesAvailable() : 1;

    if (bytesToRead <= 0)
        return false;

    if (m_readBufferMaxSize
            && (bytesToRead > (m_readBufferMaxSize - m_readBuffer.size()))) {

        bytesToRead = m_readBufferMaxSize - m_readBuffer.size();
    }

    char *ptr = m_readBuffer.reserve(bytesToRead);
    qint64 readBytes = read(ptr, bytesToRead);

    if (readBytes <= 0) {
        m_readBuffer.chop(bytesToRead);
        return false;
    }
    m_readBuffer.chop(int(bytesToRead - ((readBytes < 0) ? qint64(0) : readBytes)));
    return true;
}

/*! \internal

    This method is called from native engine when new data is
    available for reading, ie is the handler read notification.
    Handles recursive calls.
*/
bool SerialPortPrivate::canReadNotification()
{
    Q_Q(SerialPort);

#if defined (Q_OS_WINCE)
    m_engine->lockNotification(SerialPortEngine::CanReadLocker, true);
#endif
    // Prevent recursive calls.
    if (m_readSerialNotifierCalled) {
        if (!m_readSerialNotifierStateSet) {
            m_readSerialNotifierStateSet = true;
            m_readSerialNotifierState = m_engine->isReadNotificationEnabled();
            m_engine->setReadNotificationEnabled(false);
        }
    }
    m_readSerialNotifierCalled = true;

    //if (!m_isBuffered)
    //    this->serialEngine->setReadNotificationEnabled(false);

    // If buffered, read data from the serial into the read buffer.
    qint64 newBytes = 0;
    if (m_isBuffered) {
        // Return if there is no space in the buffer.
        if (m_readBufferMaxSize
                && (m_readBuffer.size() >= m_readBufferMaxSize)) {

            m_readSerialNotifierCalled = false;
            return false;
        }

        // If reading from the serial fails after getting a read
        // notification, close the serial.
        newBytes = m_readBuffer.size();

        if (!readFromPort()) {
            m_readSerialNotifierCalled = false;
            return false;
        }
        newBytes = m_readBuffer.size() - newBytes;

        // If read buffer is full, disable the read serial notifier.
        if (m_readBufferMaxSize
                && (m_readBuffer.size() == m_readBufferMaxSize)) {

            m_engine->setReadNotificationEnabled(false);
        }
    }

    // Only emit readyRead() when not recursing, and only if there is data available.
    bool hasData = (m_isBuffered) ? (newBytes > 0) : (bytesAvailable() > 0);

    if ((!m_emittedReadyRead) && hasData) {
        m_emittedReadyRead = true;
        emit q->readyRead();
        m_emittedReadyRead = false;
    }

    if ((!hasData) && m_engine->isReadNotificationEnabled())
        m_engine->setReadNotificationEnabled(true);

    // Reset the read serial notifier state if we reentered inside the
    // readyRead() connected slot.
    if (m_readSerialNotifierStateSet &&
            (m_readSerialNotifierState != m_engine->isReadNotificationEnabled())) {

        m_engine->setReadNotificationEnabled(m_readSerialNotifierState);
        m_readSerialNotifierStateSet = false;
    }
    m_readSerialNotifierCalled = false;
    return true;
}

/*! \internal

    This method is called from native engine when the serial
    port ready for writing, ie is the handler write notification.
    Handles recursive calls.
*/
bool SerialPortPrivate::canWriteNotification()
{
#if defined (Q_OS_WINCE)
    m_engine->lockNotification(SerialPortEngine::CanWriteLocker, true);
#endif

#if defined (Q_OS_WIN)
    if (m_engine->isWriteNotificationEnabled())
        m_engine->setWriteNotificationEnabled(false);
#endif

    int tmp = m_writeBuffer.size();
    flush();

#if defined (Q_OS_WIN)
    if (!m_writeBuffer.isEmpty())
        m_engine->setWriteNotificationEnabled(true);
#else
    if (m_writeBuffer.isEmpty())
        m_engine->setWriteNotificationEnabled(false);
#endif
    return (m_writeBuffer.size() < tmp);
}

/*! \internal

    This method is called from native engine when the serial
    port hardware detect an I/O error. For example, the error
    parity, frame, etc., ie is the handler error notification.
    Handles recursive calls.
*/
bool SerialPortPrivate::canErrorNotification()
{
#if defined (Q_OS_WINCE)
    m_engine->lockNotification(SerialPortEngine::CanErrorLocker, true);
#endif
    return m_engine->processIOErrors();
}

//----------------------------------------------------------------
//************* SerialPort

/*!
    \class SerialPort
    \brief The SerialPort class provides the base functionality
    common to all serial ports types.

    \reentrant
    \ingroup serial
    \inmodule QSerialDevice

    The basis of SerialPort was chosen class QAbstractSocket, so
    their the functionality and behavior is similar in some cases.
    For example, in terms of I/O operations, the implementation of
    the wait methods, the internal architecture and etc. Especially,
    the implementation of some methods of SerialPort take directly
    from QAbstractSocket with minimal changes.

    SerialPort only provides common functionality, which includes
    configuring, sending/receiving data stream, obtaining the status
    and control signals RS-232 lines. In this case, are not supported
    by specific terminal features as echo, control CR/LF, etc., ie
    serial port always works in binary mode. Also, do not support the
    native ability to configure timeouts and delays in reading, that
    is, reading functions always return immediately. Organization of
    the delays can be implemented in software, for example:

    <<!!! My
    \*snippet doc/src/snippets/network/tcpwait.cpp 0
    <<!!!

    Also, the SerialPort implementation does not provide tracking and
    notification when the signal lines of the RS-232 serial port is
    change. This implementation is cumbersome and is not required in
    usual use serial port.

    So, getting started with the SerialPort should by creating an
    object of that class. Object can be created on the stack or
    the heap:

    <<!!! My
    \*snippet doc/src/snippets/network/tcpwait.cpp 0
    <<!!!

    Next, you must give the name of the object desired serial port,
    which is really present in the system by calling setPort(). In
    this case, the name must have a certain format which is platform
    dependent. If you are unsure of the serial port name, for this
    you can use the class SerialPortInfo to obtain the correct serial
    port name. You can also use SerialPortInfo as an input parameter
    to the method setPort(). To retrieve current serial port name use
    method portName()

    <<!!! My
    \*snippet doc/src/snippets/network/tcpwait.cpp 0
    <<!!!

    Now you can open the serial port using the method open(). In
    this case, the serial port can be open in r/o, w/o or r/w mode,
    by setting the appropriate values of the input mode variable of
    this method. If nothing is specified, the default serial port
    opens in r/w mode. Also, always at the opening of the serial port,
    it opens with a internal flag of exclusive access, ie another
    process or thread can not access to an already open serial port,
    it is made for unification. If successful, the opening of the
    SerialPort will try to determine the current serial port
    settings and write them to internal parameters of the class.

    To access the current parameters of the serial port used methods
    rate(), dataBits(), parity(), stopBits(), flowControl(). If you
    are satisfied with these settings, you can go to proceed to I/O
    operation. Otherwise, you can reconfigure the port to the desired
    setting using setRate(), setDataBits(), setParity(),
    setStopBits(), setFlowControl().

    Read or write data by calling read() or write(), or use the
    convenience functions readLine() and readAll(). SerialPort also
    inherits getChar(), putChar(), and ungetChar() from QIODevice,
    which work on single bytes. The bytesWritten() signal is
    emitted when data has been written to the serial port (i.e.,
    when the connected to the port device has read the data). Note
    that Qt does not limit the write buffer size. You can monitor
    its size by listening to this signal.

    The readyRead() signal is emitted every time a new chunk of data
    has arrived. bytesAvailable() then returns the number of bytes
    that are available for reading. Typically, you would connect the
    readyRead() signal to a slot and read all available data there.

    If you don't read all the data at once, the remaining data will
    still be available later, and any new incoming data will be
    appended to SerialPort's internal read buffer. To limit the size
    of the read buffer, call setReadBufferSize().

    To obtain status signals line ports, as well as control him signals
    methods are used dtr(), rts(), lines(), setDtr(), setRts().

    To close the serial port, call close(). After all pending data has
    been written to the serial port, SerialPort actually closes the
    descriptor.

    SerialPort provides a set of functions that suspend the calling
    thread until certain signals are emitted. These functions can be
    used to implement blocking serial port:

    \list
    \o waitForReadyRead() blocks until new data is available for
    reading.

    \o waitForBytesWritten() blocks until one payload of data has been
    written to the serial port.
    \endlist

    We show an example:

    <<!!!
    \*snippet doc/src/snippets/network/tcpwait.cpp 0
    <<!!!

    If \l{QIODevice::}{waitForReadyRead()} returns false, the
    connection has been closed or an error has occurred. Programming
    with a blocking serial port is radically different from
    programming with a non-blocking serial port. A blocking serial port
    doesn't require an event loop and typically leads to simpler code.
    However, in a GUI application, blocking serial port should only be
    used in non-GUI threads, to avoid freezing the user interface.

    <<!!!
    See the \*l network/fortuneclient and \*l network/blockingfortuneclient
    examples for an overview of both approaches.
    <<!!!

    We discourage the use of the blocking functions together
    with signals. One of the two possibilities should be used.

    SerialPort can be used with QTextStream and QDataStream's stream
    operators (operator<<() and operator>>()). There is one issue to
    be aware of, though: You must make sure that enough data is
    available before attempting to read it using operator>>().

    \sa SerialPortInfo
*/

/*!
    \enum SerialPort::Direction

    This enum describes possibly direction of data transmission.
    It is used to set the speed of the device separately for
    each direction in some operating systems (POSIX).

    \value Input Input direction.
    \value Output Output direction
    \value AllDirections Simultaneously in two directions.

    \sa setRate(), rate()
*/

/*!
    \enum SerialPort::Rate

    This enum describes the baud rate at which the
    communications device operates. In this enum is listed
    only part the most common of the standard rates.

    \value Rate1200 1200 baud.
    \value Rate2400 2400 baud.
    \value Rate4800 4800 baud.
    \value Rate9600 9600 baud.
    \value Rate19200 19200 baud.
    \value Rate38400 38400 baud.
    \value Rate57600 57600 baud.
    \value Rate115200 115200 baud.
    \value UnknownRate Unknown baud rate.

    \sa setRate(), rate()
*/

/*!
    \enum SerialPort::DataBits

    This enum describes possibly the number of bits in the
    bytes transmitted and received.

    \value Data5 Five bits.
    \value Data6 Six bits
    \value Data7 Seven bits
    \value Data8 Eight bits.
    \value UnknownDataBits Unknown number of bits.

    \sa setDataBits(), dataBits()
*/

/*!
    \enum SerialPort::Parity

    This enum describes possibly parity scheme to be used.

    \value NoParity No parity.
    \value EvenParity Even parity.
    \value OddParity Odd parity.
    \value SpaceParity Space parity.
    \value MarkParity Mark parity.
    \value UnknownParity Unknown parity.

    \sa setParity(), parity()
*/

/*!
    \enum SerialPort::StopBits

    This enum describes possibly the number of
    stop bits to be used.

    \value OneStop 1 stop bit.
    \value OneAndHalfStop 1.5 stop bits.
    \value TwoStop 2 stop bits.
    \value UnknownStopBits Unknown number of stop bit.

    \sa setStopBits(), stopBits()
*/

/*!
    \enum SerialPort::FlowControl

    This enum describes possibly flow control to
    be used.

    \value NoFlowControl No flow control.
    \value HardwareControl Hardware flow control (RTS/CTS).
    \value SoftwareControl Software flow control (XON/XOFF).
    \value UnknownFlowControl Unknown flow control.

    \sa setFlowControl(), flowControl()
*/

/*!
    \enum SerialPort::Line

    This enum describes possibly RS-232 pinout signals.

    \value Le DSR (data set ready/line enable).
    \value Dtr DTR (data terminal ready).
    \value Rts RTS (request to send).
    \value St Secondary TXD (transmit).
    \value Sr Secondary RXD (receive).
    \value Cts CTS (clear to send).
    \value Dcd DCD (data carrier detect).
    \value Ri RNG (ring).
    \value Dsr DSR (data set ready).

    \sa lines(), setDtr(), setRts(), dtr(), rts()
*/

/*!
    \enum SerialPort::DataErrorPolicy

    This enum describes policies manipulate for received
    symbols with detected parity errors.

    \value SkipPolicy Skip the bad character.
    \value PassZeroPolicy Replaces bad character to zero.
    \value IgnorePolicy Ignore the error for a bad character.
    \value StopReceivingPolicy Stopping data reception on error.
    \value UnknownPolicy Unknown policy.

    \sa setDataErrorPolicy(), dataErrorPolicy()
*/

/*!
    \enum SerialPort::PortError

    This enum describes the errors that may be returned by the error()
    function.

    \value NoError No error occurred.
    \value NoSuchDeviceError An error occurred when it attempts to
           open no existing device.
    \value PermissionDeniedError An error occurred when it attempts to
           open is already opened device by another process or user do
           not have permission to open.
    \value DeviceAlreadyOpenedError An error occurred when it attempts to
           reused open already opened device in this object.
    \value DeviceIsNotOpenedError An error occurred when it attempts to
           control still closed device.
    \value ParityError The hardware detected a parity error
           when reading data.
    \value FramingError The hardware detected a framing error
           when reading data.
    \value BreakConditionError The hardware detected a break condition on
           the input line.
    \value IoError I/O error occurred when read/write data.
    \value UnsupportedPortOperationError The requested device operation is
           not supported or prohibited by the local operating system.
    \value UnknownPortError An unidentified error occurred.

    \sa error(), unsetError()
*/



/* Public methods */

/*!
    Constructs a new serial port object with the given \a parent.
*/
SerialPort::SerialPort(QObject *parent)
    : QIODevice(parent)
    , d_ptr(new SerialPortPrivate(this))
{}

/*!
    Constructs a new serial port object with the given \a parent
    to represent the serial port with the specified \a name.

    The name should have a specific format, see setPort().
*/
SerialPort::SerialPort(const QString &name, QObject *parent)
    : QIODevice(parent)
    , d_ptr(new SerialPortPrivate(this))
{
    setPort(name);
}

/*!
    Constructs a new serial port object with the given \a parent
    to represent the serial port with the specified class \a info.
*/
SerialPort::SerialPort(const SerialPortInfo &info, QObject *parent)
    : QIODevice(parent)
    , d_ptr(new SerialPortPrivate(this))
{
    setPort(info);
}

/*!
    Destroys the serial port object, closing it if necessary.
*/
SerialPort::~SerialPort()
{
    /**/
    close();
    delete d_ptr;
}

/*!
    Sets the \a name of the port. The name may be a any format:
    or short, or also as system location (with all the prefixes and
    postfixed). As a result, this name will automatically be written
    and converted into an internal variable as system location.

    \sa portName(), SerialPortInfo
*/
void SerialPort::setPort(const QString &name)
{
    Q_D(SerialPort);
    d->setPort(name);
}

/*!
    Sets the port that are stored in instance \a info.

    \sa portName(), SerialPortInfo
*/
void SerialPort::setPort(const SerialPortInfo &info)
{
    Q_D(SerialPort);
    d->setPort(info.systemLocation());
}

/*!
    Returns the name set by setPort() or to the SerialPort constructors.
    This name is short, ie it extract and convert out from the internal
    variable system location of the device. Conversion algorithm is
    platform specific:
    \table
    \header
        \o Platform
        \o Brief Description
    \row
        \o Windows
        \o Removes the prefix "\\\\.\\" from the system location
           and returns the remainder of the string.
    \row
        \o Windows CE
        \o Removes the postfix ":" from the system location
           and returns the remainder of the string.
    \row
        \o Symbian
        \o Returns the system location as it is,
           as it is equivalent to the port name.
    \row
        \o GNU/Linux
        \o Removes the prefix "/dev/" from the system location
           and returns the remainder of the string.
    \row
        \o MacOSX
        \o Removes the prefix "/dev/cu." and "/dev/tty." from the
           system location and returns the remainder of the string.
    \row
        \o Other *nix
        \o The same as for GNU/Linux.
    \endtable

    \sa setPort(), SerialPortInfo::portName()
*/
QString SerialPort::portName() const
{
    Q_D(const SerialPort);
    return d->port();
}

/*! \reimp
    Opens the serial port using OpenMode \a mode, returning true if
    successful; otherwise false with saved error code which can be
    obtained by calling error().

    \warning The \a mode must be QIODevice::ReadOnly, QIODevice::WriteOnly,
    or QIODevice::ReadWrite. It may also have additional flags, such as
    QIODevice::Unbuffered. Other modes unsupported.

    \sa QIODevice::OpenMode, setPort()
*/
bool SerialPort::open(OpenMode mode)
{
    Q_D(SerialPort);

    if (isOpen()) {
        d->setError(SerialPort::DeviceAlreadyOpenedError);
        return false;
    }

    // Define while not supported modes.
    static OpenMode unsupportedModes = (Append | Truncate | Text);
    if ((mode & unsupportedModes) || (mode == NotOpen)) {
        d->setError(SerialPort::UnsupportedPortOperationError);
        return false;
    }

    unsetError();
    if (d->open(mode)) {
        QIODevice::open(mode);
        d->clearBuffers();

        if (mode & ReadOnly)
            d->m_engine->setReadNotificationEnabled(true);
        if (mode & WriteOnly)
            d->m_engine->setWriteNotificationEnabled(true);

        d->m_isBuffered = !(mode & Unbuffered);
        return true;
    }
    return false;
}

/*! \reimp
    Calls SerialPort::flush() and closes the serial port.
    Errors from flush are ignored.

    \sa QIODevice::close()
*/
void SerialPort::close()
{
    Q_D(SerialPort);
    if (!isOpen()) {
        d->setError(SerialPort::DeviceIsNotOpenedError);
        return;
    }

    flush();
    QIODevice::close();
    d->m_engine->setReadNotificationEnabled(false);
    d->m_engine->setWriteNotificationEnabled(false);
    d->clearBuffers();
    d->close();
}

/*!
    Sets or clear the flag \a restore, which allows to restore the
    previous settings on closing the serial port. If it flag
    is true that the settings will be restored; otherwise not.
    The default SerialPort is configured to restore the settings.

    \sa restoreSettingsOnClose()
*/
void SerialPort::setRestoreSettingsOnClose(bool restore)
{
    Q_D( SerialPort);
    d->m_restoreSettingsOnClose = restore;
}

/*!
    Returns the current status of the restore flag settings on
    closing the port. The default SerialPort is configured to
    restore the settings.

    \sa setRestoreSettingsOnClose()
*/
bool SerialPort::restoreSettingsOnClose() const
{
    Q_D(const SerialPort);
    return d->m_restoreSettingsOnClose;
}

/*!
    Sets the desired data rate \a rate for a given direction \a dir.
    If successful, returns true; otherwise false with saved error
    code which can be obtained by calling error(). To set the speed
    can use enumeration SerialPort::Rate or any positive qint32 value.

    \warning For OS Windows, Windows CE, Symbian supported only
    AllDirections flag.

    \sa rate()
*/
bool SerialPort::setRate(qint32 rate, Directions dir)
{
    Q_D(SerialPort);
    return d->setRate(rate, dir);
}

/*!
    Returns the current baud rate of the chosen direction \a dir.

    \warning For OS Windows, Windows CE, Symbian return
    equal rate in any direction.

    \sa setRate()
*/
qint32 SerialPort::rate(Directions dir) const
{
    Q_D(const SerialPort);
    return d->rate(dir);
}

/*!
    Sets the desired number of data bits \a dataBits in byte.
    If successful, returns true; otherwise false with saved error
    code which can be obtained by calling error().

    \sa dataBits()
*/
bool SerialPort::setDataBits(DataBits dataBits)
{
    Q_D(SerialPort);
    return d->setDataBits(dataBits);
}

/*!
    Returns the current number of data bits in byte.

    \sa setDataBits()
*/
SerialPort::DataBits SerialPort::dataBits() const
{
    Q_D(const SerialPort);
    return d->dataBits();
}

/*!
    Sets the desired parity \a parity checking mode.
    If successful, returns true; otherwise false with saved error
    code which can be obtained by calling error().

    \sa parity()
*/
bool SerialPort::setParity(Parity parity)
{
    Q_D(SerialPort);
    return d->setParity(parity);
}

/*!
    Returns the current parity checking mode.

    \sa setParity()
*/
SerialPort::Parity SerialPort::parity() const
{
    Q_D(const SerialPort);
    return d->parity();
}

/*!
    Sets the desired number of stop bits \a stopBits in frame.
    If successful, returns true; otherwise false with saved error
    code which can be obtained by calling error().

    \sa stopBits()
*/
bool SerialPort::setStopBits(StopBits stopBits)
{
    Q_D(SerialPort);
    return d->setStopBits(stopBits);
}

/*!
    Returns the current number of stop bits.

    \sa setStopBits()
*/
SerialPort::StopBits SerialPort::stopBits() const
{
    Q_D(const SerialPort);
    return d->stopBits();
}

/*!
    Sets the desired number flow control mode \a flow.
    If successful, returns true; otherwise false with saved error
    code which can be obtained by calling error().

    \sa flowControl()
*/
bool SerialPort::setFlowControl(FlowControl flow)
{
    Q_D(SerialPort);
    return d->setFlowControl(flow);
}

/*!
    Returns the current flow control mode.

    \sa setFlowControl()
*/
SerialPort::FlowControl SerialPort::flowControl() const
{
    Q_D(const SerialPort);
    return d->flowControl();
}

/*!
    Returns the current state of the line signal DTR.
    If the signal state high, the return true;
    otherwise false;

    \sa lines()
*/
bool SerialPort::dtr() const
{
    Q_D(const SerialPort);
    return d->dtr();
}

/*!
    Returns the current state of the line signal RTS.
    If the signal state high, the return true;
    otherwise false;

    \sa lines()
*/
bool SerialPort::rts() const
{
    Q_D(const SerialPort);
    return d->rts();
}

/*!
    Returns the bitmap states of line signals.
    From this result it is possible to allocate the state of the
    desired signal by applying a mask "AND", where the mask is
    the desired enum from SerialPort::Lines.

    \sa dtr(), rts(), setDtr(), setRts()
*/
SerialPort::Lines SerialPort::lines() const
{
    Q_D(const SerialPort);
    return d->lines();
}

/*!
    This function writes as much as possible from the internal write
    buffer to the underlying serial port, without blocking. If any data
    was written, this function returns true; otherwise false is returned.

    Call this function if you need SerialPort to start sending buffered
    data immediately. The number of bytes successfully written depends on
    the operating system. In most cases, you do not need to call this
    function, because SerialPort will start sending data automatically
    once control goes back to the event loop. In the absence of an event
    loop, call waitForBytesWritten() instead.

    \sa write(), waitForBytesWritten()
*/
bool SerialPort::flush()
{
    Q_D(SerialPort);
    return d->flush() || d->m_engine->flush();
}

/*! \reimp
    Resets and clears all buffers of the serial port, including an
    internal class buffer and the UART (driver) buffer. If successful,
    returns true; otherwise false.
*/
bool SerialPort::reset()
{
    Q_D(SerialPort);
    d->clearBuffers();
    return d->reset();
}

/*! \reimp

     Returns true if no more data is currently
     available for reading; otherwise returns false.

     This function is most commonly used when reading data from the
     serial port in a loop. For example:

     <<!!!
     \*snippet doc/src/snippets/code/src_network_socket_qabstractsocket.cpp 2
     <<!!!

     \sa bytesAvailable(), readyRead()
 */
bool SerialPort::atEnd() const
{
    Q_D(const SerialPort);
    return QIODevice::atEnd() && (!isOpen() || d->m_readBuffer.isEmpty());
}

/*!
    Sets the error policy \a policy process received character in
    the case of parity error detection. If successful, returns
    true; otherwise false. By default is set policy IgnorePolicy.

    \sa dataErrorPolicy()
*/
bool SerialPort::setDataErrorPolicy(DataErrorPolicy policy)
{
    Q_D(SerialPort);
    return d->setDataErrorPolicy(policy);
}

/*!
    Returns current error policy.

    \sa setDataErrorPolicy()
*/
SerialPort::DataErrorPolicy SerialPort::dataErrorPolicy() const
{
    Q_D(const SerialPort);
    return d->dataErrorPolicy();
}

/*!
    Returns the serial port error status.

    The I/O device status returns an error code. For example, if open()
    returns false, or a read/write operation returns -1, this function can
    be called to find out the reason why the operation failed.

    \sa unsetError()
*/
SerialPort::PortError SerialPort::error() const
{
    Q_D(const SerialPort);
    return d->error();
}

/*!
    Sets the serial port's error to SerialPort::NoError.

    \sa error()
*/
void SerialPort::unsetError()
{
    Q_D(SerialPort);
    d->unsetError();
}

/*!
    Returns the size of the internal read buffer. This limits the
    amount of data that the client can receive before you call read()
    or readAll().

    A read buffer size of 0 (the default) means that the buffer has
    no size limit, ensuring that no data is lost.

    \sa setReadBufferSize(), read()
*/
qint64 SerialPort::readBufferSize() const
{
    Q_D(const SerialPort);
    return d->m_readBufferMaxSize;
}

/*!
    Sets the size of SerialPort's internal read buffer to be \a
    size bytes.

    If the buffer size is limited to a certain size, SerialPort
    won't buffer more than this size of data. Exceptionally, a buffer
    size of 0 means that the read buffer is unlimited and all
    incoming data is buffered. This is the default.

    This option is useful if you only read the data at certain points
    in time (e.g., in a real-time streaming application) or if you
    want to protect your serial port against receiving too much data,
    which may eventually cause your application to run out of memory.

    \sa readBufferSize(), read()
*/
void SerialPort::setReadBufferSize(qint64 size)
{
    Q_D(SerialPort);

    if (d->m_readBufferMaxSize == size)
        return;
    d->m_readBufferMaxSize = size;
    if (!d->m_readSerialNotifierCalled && d->m_engine) {
        // Ensure that the read notification is enabled if we've now got
        // room in the read buffer.
        // But only if we're not inside canReadNotification --
        // that will take care on its own.
        if (size == 0 || d->m_readBuffer.size() < size)
            d->m_engine->setReadNotificationEnabled(true);
    }
}

/*! \reimp
    Always returned true. Serial port is sequential device.
*/
bool SerialPort::isSequential() const
{
    return true;
}

/*! \reimp
    Returns the number of incoming bytes that are waiting to be read.

    \sa bytesToWrite(), read()
*/
qint64 SerialPort::bytesAvailable() const
{
    Q_D(const SerialPort);
    qint64 ret;
    if (d->m_isBuffered)
        ret = qint64(d->m_readBuffer.size());
    else
        ret = d->bytesAvailable();
    return ret + QIODevice::bytesAvailable();
}

/*! \reimp
    Returns the number of bytes that are waiting to be written. The
    bytes are written when control goes back to the event loop or
    when flush() is called.

    \sa bytesAvailable(), flush()
*/
qint64 SerialPort::bytesToWrite() const
{
    Q_D(const SerialPort);
    qint64 ret;
    if (d->m_isBuffered)
        ret = qint64(d->m_writeBuffer.size());
    else
        ret = d->bytesToWrite();
    return ret + QIODevice::bytesToWrite();
}

/*! \reimp
    Returns true if a line of data can be read from the serial port;
    otherwise returns false.

    \sa readLine()
*/
bool SerialPort::canReadLine() const
{
    Q_D(const SerialPort);
    bool hasLine = d->m_readBuffer.canReadLine();
    return (hasLine || QIODevice::canReadLine());
}

// Returns the difference between msecs and elapsed. If msecs is -1,
// however, -1 is returned.
static int qt_timeout_value(int msecs, int elapsed)
{
    if (msecs == -1) { return msecs; }
    msecs -= elapsed;
    return (msecs < 0) ? 0 : msecs;
}

/*! \reimp
    This function blocks until new data is available for reading and the
    \l{QIODevice::}{readyRead()} signal has been emitted. The function
    will timeout after \a msecs milliseconds.

    The function returns true if the readyRead() signal is emitted and
    there is new data available for reading; otherwise it returns false
    (if an error occurred or the operation timed out).

    \sa waitForBytesWritten()
*/
bool SerialPort::waitForReadyRead(int msecs)
{
    Q_D(SerialPort);

    if (d->m_isBuffered && (!d->m_readBuffer.isEmpty()))
        return true;

    if (d->m_engine->isReadNotificationEnabled())
        d->m_engine->setReadNotificationEnabled(false);

#if QT_VERSION >= 0x040700
    QElapsedTimer stopWatch;
#else
    QTime stopWatch;
#endif

    stopWatch.start();

    forever {
        bool readyToRead = false;
        bool readyToWrite = false;
        if (!d->waitForReadOrWrite(qt_timeout_value(msecs, stopWatch.elapsed()),
                                   true, (!d->m_writeBuffer.isEmpty()),
                                   &readyToRead, &readyToWrite)) {
            return false;
        }
        if (readyToRead) {
            if (d->canReadNotification())
                return true;
        }
        if (readyToWrite)
            d->canWriteNotification();
    }
    return false;
}

/*! \reimp
*/
bool SerialPort::waitForBytesWritten(int msecs)
{
    Q_D(SerialPort);

    if (d->m_isBuffered && d->m_writeBuffer.isEmpty())
        return false;

#if QT_VERSION >= 0x040700
    QElapsedTimer stopWatch;
#else
    QTime stopWatch;
#endif

    stopWatch.start();

    forever {
        bool readyToRead = false;
        bool readyToWrite = false;
        if (!d->waitForReadOrWrite(qt_timeout_value(msecs, stopWatch.elapsed()),
                                   true, (!d->m_writeBuffer.isEmpty()),
                                   &readyToRead, &readyToWrite)) {
            return false;
        }
        if (readyToRead) {
            if(!d->canReadNotification())
                return false;
        }
        if (readyToWrite) {
            if (d->canWriteNotification()) {
                return true;
            }
        }
    }
    return false;
}

/* Public slots */

/*!
    Sets the desired state of the line signal DTR,
    depending on the flag \a set. If successful, returns true;
    otherwise false.

    If the flag is true then DTR signal is established in the high;
    otherwise low.

    \sa lines(), dtr()
*/
bool SerialPort::setDtr(bool set)
{
    Q_D(SerialPort);
    return d->setDtr(set);
}

/*!
    Sets the desired state of the line signal RTS,
    depending on the flag \a set. If successful, returns true;
    otherwise false.

    If the flag is true then RTS signal is established in the high;
    otherwise low.

    \sa lines(), rts()
*/
bool SerialPort::setRts(bool set)
{
    Q_D(SerialPort);
    return d->setRts(set);
}

/*!
    Sends a continuous stream of zero bits during a specified period
    of time \a duration in msec if the terminal is using asynchronous
    serial data. If successful, returns true; otherwise false.

    If duration is zero then zero bits are transmitted by at least
    0.25 seconds, but no more than 0.5 seconds.

    If duration is non zero then zero bits are transmitted within a certain
    period of time depending on implementation.

    \sa setBreak(), clearBreak()
*/
bool SerialPort::sendBreak(int duration)
{
    Q_D(SerialPort);
    return d->sendBreak(duration);
}

/*!
    Control the signal break, depending on the flag \a set.
    If successful, returns true; otherwise false.

    If set is true then enable break transmission; otherwise disable.

    \sa clearBreak(), sendBreak()
*/
bool SerialPort::setBreak(bool set)
{
    Q_D(SerialPort);
    return d->setBreak(set);
}

/* Protected methods */

/*! \reimp
*/
qint64 SerialPort::readData(char *data, qint64 maxSize)
{
    Q_D(SerialPort);

    if (d->m_engine->isReadNotificationEnabled() && d->m_isBuffered)
        d->m_engine->setReadNotificationEnabled(true);

    if (!d->m_isBuffered) {
        qint64 readBytes = d->read(data, maxSize);
        return readBytes;
    }

    if (d->m_readBuffer.isEmpty())
        return qint64(0);

    // If readFromSerial() read data, copy it to its destination.
    if (maxSize == 1) {
        *data = d->m_readBuffer.getChar();
        return 1;
    }

    qint64 bytesToRead = qMin(qint64(d->m_readBuffer.size()), maxSize);
    qint64 readSoFar = 0;
    while (readSoFar < bytesToRead) {
        const char *ptr = d->m_readBuffer.readPointer();
        int bytesToReadFromThisBlock = qMin(int(bytesToRead - readSoFar),
                                            d->m_readBuffer.nextDataBlockSize());
        memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
        readSoFar += bytesToReadFromThisBlock;
        d->m_readBuffer.free(bytesToReadFromThisBlock);
    }
    return readSoFar;
}

/*! \reimp
*/
qint64 SerialPort::readLineData(char *data, qint64 maxSize)
{
    return QIODevice::readLineData(data, maxSize);
}

/*! \reimp
*/
qint64 SerialPort::writeData(const char *data, qint64 maxSize)
{
    Q_D(SerialPort);

    if (!d->m_isBuffered) {
        qint64 written = d->write(data, maxSize);
        if (written < 0) {
            //Error
        } else if (!d->m_writeBuffer.isEmpty()) {
            d->m_engine->setWriteNotificationEnabled(true);
        }

        if (written >= 0)
            emit bytesWritten(written);

        return written;
    }

    char *ptr = d->m_writeBuffer.reserve(maxSize);
    if (maxSize == 1)
        *ptr = *data;
    else
        memcpy(ptr, data, maxSize);

    if (!d->m_writeBuffer.isEmpty())
        d->m_engine->setWriteNotificationEnabled(true);

    return maxSize;
}

/*!
    \fn bool SerialPort::clearBreak(bool clear)

    Control the signal break, depending on the flag \a clear.
    If successful, returns true; otherwise false.

    If clear is false then enable break transmission; otherwise disable.

    \sa setBreak(), sendBreak()
*/

#include "moc_serialport.cpp"

QT_END_NAMESPACE_SERIALPORT