QBtSerialPortClient.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * QBtSerialPortClient.h
  3. *
  4. *
  5. * Author: Ftylitakis Nikolaos
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifndef QBTSERIALPORTCLIENT_H_
  20. #define QBTSERIALPORTCLIENT_H_
  21. #include <QBtGlobal.h>
  22. #include <QBtTypes.h>
  23. #include <QtCore/QObject>
  24. #include <QtCore/QByteArray>
  25. QBT_NAMESPACE_BEGIN
  26. //forward declaration
  27. class QBtSerialPortClientPrivate;
  28. /**
  29. * Class that provides the mechanism to connect to a bluetooth serial port
  30. * server and communicate.
  31. *
  32. * Calling connect(const QBtDevice&, const QBtService&) user specifies
  33. * on which device to connect(on which serial port server) and on which
  34. * bluetooth service. The QBtDevice instance of the server can be
  35. * aquired using QBtDeviceDiscoverer or user can create his own instance
  36. * and set at least the QBtAddress field. Also the QBtService can be
  37. * aquired using QBtServiceDiscoverer targeting the beforementioned device.
  38. *
  39. * On successfull connection, connectedToServer() singal is emitted and
  40. * user is able to sendData(const QString&) to the server. If successfull
  41. * then dataSend() signal is emitted.
  42. *
  43. * If data is recieved from the server, dataReceived(const QString) signal is
  44. * emitted containing the bytes recieved. At this point user must be carefull
  45. * to read the data recieved using the same encoding as the sender (in this
  46. * case serial port server). QString contains all the necessary functions for
  47. * character encoding that is why is QString is used instead of QByteArray.
  48. *
  49. * When connection is no longer needed, disconnect() can be called.
  50. * disconnectedFromServer() is then emitted.
  51. *
  52. * In case of error error(QBtSerialPortClient::ErrorCode) signal is emitted.
  53. */
  54. class DLL_EXPORT QBtSerialPortClient : public QObject
  55. {
  56. Q_OBJECT
  57. public:
  58. enum ErrorCode
  59. {
  60. ErrorAlreadyInUse,
  61. ErrorNotSupported,
  62. ErrorUnavailable,
  63. ErrorOpeningConnection,
  64. ErrorAlreadyConnected,
  65. ErrorUndefinedError,
  66. ErrorUnableToInitializePort,
  67. ErrorNoDeviceSelected,
  68. ErrorNoServiceSelected,
  69. ErrorConnectionError,
  70. ErrorConnectionTimeout,
  71. ErrorOnDisconnecting
  72. };
  73. public:
  74. /**
  75. * A Serial port client.
  76. * According to the bluetooth specification, up to 7 instances of this class
  77. * can be connected simultaneously to other servers.
  78. */
  79. QBtSerialPortClient(QObject* parent);
  80. /**
  81. * Destructor.
  82. *
  83. * If isConnected() then disconnect() is called.
  84. */
  85. ~QBtSerialPortClient();
  86. /**
  87. * isConnected()
  88. *
  89. * Returns whether or not the client is connected to a remote server.
  90. */
  91. bool isConnected();
  92. public slots:
  93. /**
  94. * connect()
  95. * RemoteDevice must contain at least the device address.
  96. * and remoteService at least a class ID (in case of SerialPort port field is also needed).
  97. * The remoteService info can be acquired by QBtServiceDiscoverer.
  98. */
  99. void connect(const QBtDevice& remoteDevice, const QBtService& remoteService);
  100. /**
  101. * disconnect()
  102. * Disconnects from the remote server if previously succusfully connected.
  103. */
  104. void disconnect();
  105. /**
  106. * sendData()
  107. * Send a string to the server.
  108. * If text is transmitted, it is up to the user to decide which text encoding to use.
  109. * Upon succesfull transmittion, dataSent() signal is emitted.
  110. */
  111. void sendData(const QString& data);
  112. /**
  113. * Send a array of bytes to the server, as is.
  114. * Upon succesfull transmittion, dataSent() signal is emitted.
  115. */
  116. void sendData (const QByteArray & data);
  117. signals:
  118. /**
  119. * Emitted when successfully disconnected from the remote server (initiated from client).
  120. */
  121. void disconnectedFromServer();
  122. /**
  123. * Emitted when the server initiated a disconnection.
  124. */
  125. void connectionResetByPeer ();
  126. /**
  127. * Emitted when successfully connected to the remote server.
  128. */
  129. void connectedToServer();
  130. /**
  131. * Emitted when after sendData(QString) is called, if the data is send
  132. * successfully.
  133. */
  134. void dataSent();
  135. /**
  136. * Emitted as feedback when data are received successfully.
  137. */
  138. void dataReceived(const QString & data);
  139. /**
  140. * Emitted the instance the dataReceived(const QString&) signal is emitted. User
  141. * decides which format wants to read.
  142. */
  143. void dataReceived(const QByteArray & data);
  144. /**
  145. * Emitted in case of error.
  146. */
  147. void error(QBtSerialPortClient::ErrorCode error);
  148. private:
  149. friend class QBtSerialPortClientPrivate;
  150. QBtSerialPortClientPrivate* _implPtr;
  151. };
  152. QBT_NAMESPACE_END
  153. //Q_DECLARE_METATYPE(QBT_PREPEND_NAMESPACE(QBtSerialPortClient))
  154. #endif /* QBTSERIALPORTCLIENT_H_ */