File indexing completed on 2024-05-12 04:58:50

0001 /**
0002  * SPDX-FileCopyrightText: 2016 Saikrishna Arcot <saiarcot895@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "bluetoothdevicelink.h"
0008 
0009 #include "../linkprovider.h"
0010 #include "bluetoothdownloadjob.h"
0011 #include "bluetoothlinkprovider.h"
0012 #include "bluetoothuploadjob.h"
0013 #include "connectionmultiplexer.h"
0014 #include "core_debug.h"
0015 #include "multiplexchannel.h"
0016 
0017 BluetoothDeviceLink::BluetoothDeviceLink(const DeviceInfo &deviceInfo,
0018                                          BluetoothLinkProvider *parent,
0019                                          ConnectionMultiplexer *connection,
0020                                          QSharedPointer<MultiplexChannel> socket)
0021     : DeviceLink(deviceInfo.id, parent)
0022     , mConnection(connection)
0023     , mChannel(socket)
0024     , mDeviceInfo(deviceInfo)
0025 {
0026     connect(socket.data(), &QIODevice::readyRead, this, &BluetoothDeviceLink::dataReceived);
0027 
0028     // We take ownership of the connection.
0029     // When the link provider destroys us,
0030     // the socket (and the reader) will be
0031     // destroyed as well
0032     mConnection->setParent(this);
0033     connect(socket.data(), &MultiplexChannel::aboutToClose, this, &QObject::deleteLater);
0034 }
0035 
0036 bool BluetoothDeviceLink::sendPacket(NetworkPacket &np)
0037 {
0038     if (np.hasPayload()) {
0039         BluetoothUploadJob *uploadJob = new BluetoothUploadJob(np.payload(), mConnection, this);
0040         np.setPayloadTransferInfo(uploadJob->transferInfo());
0041         uploadJob->start();
0042     }
0043     // TODO: handle too-big packets
0044     int written = mChannel->write(np.serialize());
0045     return (written != -1);
0046 }
0047 
0048 void BluetoothDeviceLink::dataReceived()
0049 {
0050     while (mChannel->canReadLine()) {
0051         const QByteArray serializedPacket = mChannel->readLine();
0052 
0053         // qCDebug(KDECONNECT_CORE) << "BluetoothDeviceLink dataReceived" << packet;
0054 
0055         NetworkPacket packet;
0056         NetworkPacket::unserialize(serializedPacket, &packet);
0057 
0058         if (packet.hasPayloadTransferInfo()) {
0059             BluetoothDownloadJob *downloadJob = new BluetoothDownloadJob(mConnection, packet.payloadTransferInfo(), this);
0060             downloadJob->start();
0061             packet.setPayload(downloadJob->payload(), packet.payloadSize());
0062         }
0063 
0064         Q_EMIT receivedPacket(packet);
0065     }
0066 }
0067 
0068 #include "moc_bluetoothdevicelink.cpp"