QBtSerialPortServer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef QBTSERIALPORTSERVER_H
  16. #define QBTSERIALPORTSERVER_H
  17. #include <QBtGlobal.h>
  18. #include <QBtTypes.h>
  19. #include <QBtServiceAdvertiser.h>
  20. #include <QtCore/QObject>
  21. #include <QtCore/QByteArray>
  22. #include <QBtUuid.h>
  23. QBT_NAMESPACE_BEGIN
  24. //forward declaration
  25. class QBtSerialPortServerPrivate;
  26. /**
  27. * This class creates a Serial Port Server.
  28. *
  29. * After instantiation user can call startServer(const QString&)
  30. * to start the server and wait for a client to connect. From the
  31. * serviceName argument user can specify the name of the RFCOMM service
  32. * which is how the service will be identified by other devices. By default
  33. * the name is MyRFCOMM.
  34. *
  35. * If server is started successfully then serverStarted() signal is emitted.
  36. *
  37. * When a remote client is connected to the local running server
  38. * clientConnected(const QBtAddress &) is emitted and from that point and
  39. * on the data transmittion can start.
  40. *
  41. * To send data to the client use sendData(const QString &). If successfull
  42. * dataSent() signal is emitted.
  43. *
  44. * When data are received successfully from the remote client dataReceived(const QString &)
  45. * signal is emitted.
  46. *
  47. * If client disconnects, clientDisconnected() will be emitted and user must
  48. * call startServer(const QString &) to initialize server again to be ready to
  49. * accept new connection.
  50. *
  51. * The connection to a client (if any) is closed when stopServer() is called along
  52. * with the termination of advertising of the SPP service.
  53. *
  54. * In case of an error error(QBtSerialPortServer::ErrorCode) is called.
  55. */
  56. class DLL_EXPORT QBtSerialPortServer : public QObject
  57. {
  58. Q_OBJECT
  59. public:
  60. enum ErrorCode
  61. {
  62. ErrorAlreadyInUse,
  63. ErrorNotSupported,
  64. ErrorUnavailable,
  65. ErrorUndefinedError
  66. };
  67. public:
  68. QBtSerialPortServer(QObject* parent);
  69. ~QBtSerialPortServer();
  70. /**
  71. * Get the status of the connection.
  72. * @return true if connected.
  73. */
  74. bool isConnected();
  75. /**
  76. * If startServer() function is already called then using this function
  77. * the user can acquire the information of the transmitting service that
  78. * represents this server to the outside world.
  79. */
  80. const QBtService & getTransmittingServiceInfo() const;
  81. protected:
  82. void setTransmittingService (const QBtService& service);
  83. /**
  84. * Set the flag inside this object which represents the connection status.
  85. * NOTE: This flag is updated automatically whenever the connection status
  86. * changes.
  87. */
  88. void setConnectionStatus(bool connected);
  89. /**
  90. * startAdvertisingService()
  91. *
  92. * Initializes a new advertiser and starts the transmitting of the
  93. * service passed as argument.
  94. */
  95. void startAdvertisingService(const QBtService& service);
  96. /**
  97. * stopAdvertisingService()
  98. *
  99. * Stops the transmittion of the service (if any)
  100. */
  101. void stopAdvertisingService();
  102. public slots:
  103. /*
  104. * When server is started, a custom bluetooth service is advertised
  105. * through QBtServiceAdvertiser. This service can be retrieved using
  106. * getTransmittingServiceInfo(). If needed, the user can set the name
  107. * of the transmitting service when calling this function.
  108. */
  109. void startServer (const QBtUuid & serviceId, const QString& serviceName = "My RFCOMM service");
  110. /**
  111. * stopServer()
  112. *
  113. * Stops the server, terminates the service transmittion and disconnects
  114. * from any device connected
  115. */
  116. void stopServer();
  117. /**
  118. * sendData()
  119. *
  120. * If connected to a remote client, sends the data passed as argument
  121. * to the client. If successfull, dataSent() signal is emitted.
  122. *
  123. * @param data The data to be send.
  124. */
  125. void sendData (const QString & data);
  126. signals:
  127. /**
  128. * Emitted when server is successfully started and ready to use.
  129. */
  130. void serverStarted();
  131. /**
  132. * Emitted when service advertising is terminated. If any client is connected the
  133. * a clientDisconnected() signal will be emitted as well.
  134. */
  135. void serverStopped();
  136. /**
  137. * Emitted when a remote client is connected to the local running server.
  138. */
  139. void clientConnected (const QBtAddress & clientAddress);
  140. /**
  141. * Emitted when a remote client is disconnected either by a normal disconnection
  142. * or by a problem to the connection (ex. loss of signal, power off bluetooth hardware device)
  143. */
  144. void clientDisconnected();
  145. /**
  146. * Emitted after calling sendData() function if data are send successfully to the remote client.
  147. */
  148. void dataSent();
  149. /**
  150. * Emitted when data are received successfully from the remote client.
  151. */
  152. void dataReceived (const QString & data);
  153. /**
  154. * Emitted in case of error.
  155. */
  156. void error (QBtSerialPortServer::ErrorCode code);
  157. private:
  158. // Service Advertiser
  159. QBtServiceAdvertiser* _advertiser;
  160. //transmitting service
  161. QBtService _service;
  162. //pointer to implementation
  163. QBtSerialPortServerPrivate *_implPtr;
  164. // status
  165. bool _isConnected;
  166. friend class QBtSerialPortServerPrivate;
  167. };
  168. QBT_NAMESPACE_END
  169. //Q_DECLARE_METATYPE(QBT_PREPEND_NAMESPACE(QBtSerialPortServer))
  170. #endif // QBTSERIALPORTSERVER_H