File indexing completed on 2024-04-28 15:17:56

0001 /*
0002  * BluezQt - Asynchronous BlueZ wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #ifndef BLUEZQT_PROFILE_H
0010 #define BLUEZQT_PROFILE_H
0011 
0012 #include <QObject>
0013 
0014 #include "bluezqt_export.h"
0015 #include "request.h"
0016 #include "types.h"
0017 
0018 class QLocalSocket;
0019 class QDBusObjectPath;
0020 class QDBusUnixFileDescriptor;
0021 
0022 namespace BluezQt
0023 {
0024 class Device;
0025 
0026 /**
0027  * @class BluezQt::Profile profile.h <BluezQt/Profile>
0028  *
0029  * Bluetooth profile.
0030  *
0031  * This class represents a Bluetooth profile.
0032  *
0033  * It is only needed to reimplement pure virtual functions.
0034  * You don't need to set any additional properties.
0035  *
0036  * But you may need to specify at least channel number or PSM in case it couldn't be
0037  * determined from UUID. It is also a good idea to provide name for the profile.
0038  *
0039  * Setting the channel number with setChannel() will make the profile use RFCOMM, while
0040  * setting the PSM with setPsm() will make the profile use L2CAP.
0041  *
0042  * @note The return value of requests will be sent asynchronously with Request class.
0043  *       It is also possible to cancel/reject all requests.
0044  *
0045  */
0046 class BLUEZQT_EXPORT Profile : public QObject
0047 {
0048     Q_OBJECT
0049 
0050     Q_PROPERTY(QString uuid READ uuid)
0051 
0052 public:
0053     /** Local role to identify sides in asymmetric profiles. */
0054     enum LocalRole {
0055         /** Indicates that this is a client. */
0056         ClientRole,
0057         /** Indicates that this is a server. */
0058         ServerRole,
0059     };
0060 
0061     /**
0062      * Creates a new Profile object.
0063      *
0064      * @param parent
0065      */
0066     explicit Profile(QObject *parent = nullptr);
0067 
0068     /**
0069      * Destroys a Profile object.
0070      */
0071     ~Profile() override;
0072 
0073     /**
0074      * D-Bus object path of the profile.
0075      *
0076      * The path where the profile will be registered.
0077      *
0078      * @note You must provide valid object path!
0079      *
0080      * @return object path of agent
0081      */
0082     virtual QDBusObjectPath objectPath() const = 0;
0083 
0084     /**
0085      * UUID of the profile.
0086      *
0087      * @return UUID of the profile
0088      */
0089     virtual QString uuid() const = 0;
0090 
0091     /**
0092      * Sets the human readable name of the profile.
0093      *
0094      * @param name name of the profile
0095      */
0096     void setName(const QString &name);
0097 
0098     /**
0099      * Sets the primary service class UUID (if different from profile UUID).
0100      *
0101      * @param service service UUID
0102      */
0103     void setService(const QString &service);
0104 
0105     /**
0106      * Sets the local role to identify side.
0107      *
0108      * For asymmetric profiles that do not have UUIDs available
0109      * to uniquely identify each side this parameter allows
0110      * specifying the precise local role.
0111      *
0112      * @param role local role
0113      */
0114     void setLocalRole(LocalRole role);
0115 
0116     /**
0117      * Sets the RFCOMM channel number.
0118      *
0119      * Available channel number range is 0-31.
0120      * Setting channel number to 0 will automatically choose
0121      * correct channel number for profile UUID.
0122      *
0123      * @param channel channel number
0124      */
0125     void setChannel(quint16 channel);
0126 
0127     /**
0128      * Sets the L2CAP port number.
0129      *
0130      * PSM (Protocol Service Multiplexer) is a port number
0131      * in L2CAP.
0132      *
0133      * Setting PSM to 0 will automatically choose correct
0134      * PSM for profile UUID.
0135      *
0136      * @param psm PSM
0137      */
0138     void setPsm(quint16 psm);
0139 
0140     /**
0141      * Sets whether the pairing is required to connect.
0142      *
0143      * @param require require to pair
0144      */
0145     void setRequireAuthentication(bool require);
0146 
0147     /**
0148      * Sets whether the authorization is required to connect.
0149      *
0150      * @param require require to authorize
0151      */
0152     void setRequireAuthorization(bool require);
0153 
0154     /**
0155      * Sets whether the profile is automatically connected.
0156      *
0157      * In case of a client UUID this will force connection
0158      * of the RFCOMM or L2CAP channels when a remote device
0159      * is connected.
0160      *
0161      * @param autoConnect autoconnect the profile
0162      */
0163     void setAutoConnect(bool autoConnect);
0164 
0165     /**
0166      * Sets a SDP record.
0167      *
0168      * This allows to provide a manual SDP record, otherwise it will
0169      * be generated automatically.
0170      *
0171      * @param serviceRecord SDP record
0172      */
0173     void setServiceRecord(const QString &serviceRecord);
0174 
0175     /**
0176      * Sets the profile version.
0177      *
0178      * @param version version of the profile
0179      */
0180     void setVersion(quint16 version);
0181 
0182     /**
0183      * Sets the profile features.
0184      *
0185      * @param features features of the profile
0186      */
0187     void setFeatures(quint16 features);
0188 
0189     /**
0190      * Creates a socket from file descriptor.
0191      *
0192      * @param fd socket file descriptor
0193      * @return socket
0194      *
0195      * @see newConnection()
0196      */
0197     QSharedPointer<QLocalSocket> createSocket(const QDBusUnixFileDescriptor &fd);
0198 
0199     /**
0200      * Requests the new connection.
0201      *
0202      * Common properties:
0203      * <ul>
0204      *  <li>quint16 Version - Profile version</li>
0205      *  <li>quint16 Features - Profile features</li>
0206      * </ul>
0207      *
0208      * To create socket from fd, you can use:
0209      * @code
0210      *  QSharedPointer<QLocalSocket> socket = createSocket(fd);
0211      *  if (!socket->isValid()) {
0212      *      delete socket;
0213      *      request.cancel();
0214      *      return;
0215      *  }
0216      * @endcode
0217      *
0218      * @param device device that requested connection
0219      * @param fd socket file descriptor
0220      * @param properties additional properties
0221      * @param request request to be used for sending reply
0222      */
0223     virtual void newConnection(DevicePtr device, const QDBusUnixFileDescriptor &fd, const QVariantMap &properties, const Request<> &request);
0224 
0225     /**
0226      * Requests the disconnection of the profile.
0227      *
0228      * This method gets called when a profile gets disconnected.
0229      *
0230      * @param device device to be disconnected
0231      * @param request request to be used for sending reply
0232      */
0233     virtual void requestDisconnection(DevicePtr device, const Request<> &request);
0234 
0235     /**
0236      * Indicates that the profile was unregistered.
0237      *
0238      * This method gets called when the Bluetooth daemon
0239      * unregisters the profile.
0240      *
0241      * A profile can use it to do cleanup tasks. There is no need
0242      * to unregister the profile, because when this method gets called
0243      * it has already been unregistered.
0244      *
0245      */
0246     virtual void release();
0247 
0248 private:
0249     class ProfilePrivate *const d;
0250 
0251     friend class Manager;
0252 };
0253 
0254 } // namespace BluezQt
0255 
0256 #endif // BLUEZQT_PROFILE_H