mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
Add skeleton for Bluetooth custom serial implementation on Windows platforms
Add a skeleton which will be used to develop the Bluetooth custom serial implementation for Windows platforms. Signed-off-by: Claudiu Olteanu <olteanu.claudiu@ymail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
2aa6ffe0c8
commit
7488f5500e
3 changed files with 197 additions and 8 deletions
|
@ -8,7 +8,6 @@
|
|||
|
||||
BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
localDevice(new QBluetoothLocalDevice),
|
||||
ui(new Ui::BtDeviceSelectionDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
@ -21,9 +20,16 @@ BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) :
|
|||
// Disable the save button because there is no device selected
|
||||
ui->save->setEnabled(false);
|
||||
|
||||
// Add event for item selection
|
||||
connect(ui->discoveredDevicesList, SIGNAL(itemClicked(QListWidgetItem*)),
|
||||
this, SLOT(itemClicked(QListWidgetItem*)));
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO do the initialization
|
||||
#else
|
||||
// Initialize the local Bluetooth device
|
||||
localDevice = new QBluetoothLocalDevice();
|
||||
|
||||
// Populate the list with local bluetooth devices
|
||||
QList<QBluetoothHostInfo> localAvailableDevices = localDevice->allDevices();
|
||||
int availableDevicesSize = localAvailableDevices.size();
|
||||
|
@ -56,15 +62,19 @@ BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) :
|
|||
// Initialize the device discovery agent
|
||||
if (localDevice->isValid())
|
||||
initializeDeviceDiscoveryAgent();
|
||||
#endif
|
||||
}
|
||||
|
||||
BtDeviceSelectionDialog::~BtDeviceSelectionDialog()
|
||||
{
|
||||
delete ui;
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
// Terminate the use of Winsock 2 DLL
|
||||
#else
|
||||
// Clean the local device
|
||||
delete localDevice;
|
||||
|
||||
#endif
|
||||
// Clean the device discovery agent
|
||||
if (remoteDeviceDiscoveryAgent->isActive())
|
||||
remoteDeviceDiscoveryAgent->stop();
|
||||
|
@ -74,6 +84,9 @@ BtDeviceSelectionDialog::~BtDeviceSelectionDialog()
|
|||
|
||||
void BtDeviceSelectionDialog::on_changeDeviceState_clicked()
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO add implementation
|
||||
#else
|
||||
if (localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff) {
|
||||
ui->dialogStatus->setText("Trying to turn on the local Bluetooth device...");
|
||||
localDevice->powerOn();
|
||||
|
@ -81,6 +94,7 @@ void BtDeviceSelectionDialog::on_changeDeviceState_clicked()
|
|||
ui->dialogStatus->setText("Trying to turn off the local Bluetooth device...");
|
||||
localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void BtDeviceSelectionDialog::on_save_clicked()
|
||||
|
@ -136,16 +150,23 @@ void BtDeviceSelectionDialog::remoteDeviceScanFinished()
|
|||
|
||||
void BtDeviceSelectionDialog::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO add implementation
|
||||
#else
|
||||
bool on = !(mode == QBluetoothLocalDevice::HostPoweredOff);
|
||||
|
||||
ui->dialogStatus->setText(QString("The local Bluetooth device was turned %1.")
|
||||
.arg(on? "ON" : "OFF"));
|
||||
ui->deviceState->setChecked(on);
|
||||
ui->scan->setEnabled(on);
|
||||
#endif
|
||||
}
|
||||
|
||||
void BtDeviceSelectionDialog::addRemoteDevice(const QBluetoothDeviceInfo &remoteDeviceInfo)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO add the remote device
|
||||
#else
|
||||
// By default we use the status label and the color for the UNPAIRED state
|
||||
QColor pairingColor = QColor(Qt::red);
|
||||
QString pairingStatusLabel = QString("UNPAIRED");
|
||||
|
@ -168,10 +189,14 @@ void BtDeviceSelectionDialog::addRemoteDevice(const QBluetoothDeviceInfo &remote
|
|||
item->setBackgroundColor(pairingColor);
|
||||
|
||||
ui->discoveredDevicesList->addItem(item);
|
||||
#endif
|
||||
}
|
||||
|
||||
void BtDeviceSelectionDialog::itemClicked(QListWidgetItem *item)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO enable the save button and log the information about the selected item
|
||||
#else
|
||||
QBluetoothDeviceInfo remoteDeviceInfo = item->data(Qt::UserRole).value<QBluetoothDeviceInfo>();
|
||||
QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(remoteDeviceInfo.address());
|
||||
|
||||
|
@ -184,10 +209,14 @@ void BtDeviceSelectionDialog::itemClicked(QListWidgetItem *item)
|
|||
.arg(remoteDeviceInfo.address().toString()));
|
||||
ui->save->setEnabled(true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void BtDeviceSelectionDialog::localDeviceChanged(int index)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO add implementation
|
||||
#else
|
||||
QBluetoothAddress localDeviceSelectedAddress = ui->localSelectedDevice->itemData(index, Qt::UserRole).value<QBluetoothAddress>();
|
||||
|
||||
// Delete the old localDevice
|
||||
|
@ -208,10 +237,14 @@ void BtDeviceSelectionDialog::localDeviceChanged(int index)
|
|||
// Initialize the device discovery agent
|
||||
if (localDevice->isValid())
|
||||
initializeDeviceDiscoveryAgent();
|
||||
#endif
|
||||
}
|
||||
|
||||
void BtDeviceSelectionDialog::displayPairingMenu(const QPoint &pos)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO add implementation
|
||||
#else
|
||||
QMenu menu(this);
|
||||
QAction *pairAction = menu.addAction("Pair");
|
||||
QAction *removePairAction = menu.addAction("Remove Pairing");
|
||||
|
@ -238,6 +271,7 @@ void BtDeviceSelectionDialog::displayPairingMenu(const QPoint &pos)
|
|||
.arg(currentRemoteDeviceInfo.address().toString()));
|
||||
localDevice->requestPairing(currentRemoteDeviceInfo.address(), QBluetoothLocalDevice::Unpaired);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void BtDeviceSelectionDialog::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
|
||||
|
@ -338,6 +372,9 @@ QString BtDeviceSelectionDialog::getSelectedDeviceName()
|
|||
|
||||
void BtDeviceSelectionDialog::updateLocalDeviceInformation()
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO add implementation
|
||||
#else
|
||||
// Check if the selected Bluetooth device can be accessed
|
||||
if (!localDevice->isValid()) {
|
||||
QString na = QString("Not available");
|
||||
|
@ -377,10 +414,14 @@ void BtDeviceSelectionDialog::updateLocalDeviceInformation()
|
|||
|
||||
connect(localDevice, SIGNAL(error(QBluetoothLocalDevice::Error)),
|
||||
this, SLOT(error(QBluetoothLocalDevice::Error)));
|
||||
#endif
|
||||
}
|
||||
|
||||
void BtDeviceSelectionDialog::initializeDeviceDiscoveryAgent()
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO initialize the discovery agent
|
||||
#else
|
||||
// Intialize the discovery agent
|
||||
remoteDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(localDevice->address());
|
||||
|
||||
|
@ -400,4 +441,46 @@ void BtDeviceSelectionDialog::initializeDeviceDiscoveryAgent()
|
|||
this, SLOT(remoteDeviceScanFinished()));
|
||||
connect(remoteDeviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
|
||||
this, SLOT(deviceDiscoveryError(QBluetoothDeviceDiscoveryAgent::Error)));
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
WinBluetoothDeviceDiscoveryAgent::WinBluetoothDeviceDiscoveryAgent(QObject *parent) : QThread(parent)
|
||||
{
|
||||
// Initialize the internal flags by their default values
|
||||
running = false;
|
||||
stopped = false;
|
||||
lastError = QBluetoothDeviceDiscoveryAgent::NoError;
|
||||
lastErrorToString = QString("No error");
|
||||
}
|
||||
|
||||
WinBluetoothDeviceDiscoveryAgent::~WinBluetoothDeviceDiscoveryAgent()
|
||||
{
|
||||
}
|
||||
|
||||
bool WinBluetoothDeviceDiscoveryAgent::isActive() const
|
||||
{
|
||||
return running;
|
||||
}
|
||||
|
||||
QString WinBluetoothDeviceDiscoveryAgent::errorToString() const
|
||||
{
|
||||
return lastErrorToString;
|
||||
}
|
||||
|
||||
QBluetoothDeviceDiscoveryAgent::Error WinBluetoothDeviceDiscoveryAgent::error() const
|
||||
{
|
||||
return lastError;
|
||||
}
|
||||
|
||||
void WinBluetoothDeviceDiscoveryAgent::run()
|
||||
{
|
||||
// TODO initialize the resources and start the device discovery
|
||||
}
|
||||
|
||||
void WinBluetoothDeviceDiscoveryAgent::stop()
|
||||
{
|
||||
// Stop the inqury
|
||||
stopped = true;
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -8,7 +8,23 @@
|
|||
#include <QtBluetooth/qbluetoothglobal.h>
|
||||
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
|
||||
|
||||
#if QT_VERSION < 0x050500
|
||||
#if defined(Q_OS_WIN)
|
||||
#include <QThread>
|
||||
#include <winsock2.h>
|
||||
#include <ws2bth.h>
|
||||
|
||||
#define SUCCESS 0
|
||||
#define BTH_ADDR_STR_LEN 40
|
||||
|
||||
#undef ERROR // this is already declared in our headers
|
||||
#undef IGNORE // this is already declared in our headers
|
||||
#undef DC_VERSION // this is already declared in libdivecomputer header
|
||||
#endif
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
Q_DECLARE_METATYPE(QBluetoothDeviceInfo)
|
||||
Q_DECLARE_METATYPE(QBluetoothDeviceDiscoveryAgent::Error)
|
||||
#elif QT_VERSION < 0x050500
|
||||
Q_DECLARE_METATYPE(QBluetoothDeviceInfo)
|
||||
#endif
|
||||
|
||||
|
@ -16,6 +32,30 @@ namespace Ui {
|
|||
class BtDeviceSelectionDialog;
|
||||
}
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
class WinBluetoothDeviceDiscoveryAgent : public QThread {
|
||||
Q_OBJECT
|
||||
signals:
|
||||
void deviceDiscovered(const QBluetoothDeviceInfo &info);
|
||||
void error(QBluetoothDeviceDiscoveryAgent::Error error);
|
||||
|
||||
public:
|
||||
WinBluetoothDeviceDiscoveryAgent(QObject *parent);
|
||||
~WinBluetoothDeviceDiscoveryAgent();
|
||||
bool isActive() const;
|
||||
QString errorToString() const;
|
||||
QBluetoothDeviceDiscoveryAgent::Error error() const;
|
||||
virtual void run();
|
||||
virtual void stop();
|
||||
|
||||
private:
|
||||
bool running;
|
||||
bool stopped;
|
||||
QString lastErrorToString;
|
||||
QBluetoothDeviceDiscoveryAgent::Error lastError;
|
||||
};
|
||||
#endif
|
||||
|
||||
class BtDeviceSelectionDialog : public QDialog {
|
||||
Q_OBJECT
|
||||
|
||||
|
@ -42,8 +82,12 @@ private slots:
|
|||
|
||||
private:
|
||||
Ui::BtDeviceSelectionDialog *ui;
|
||||
#if defined(Q_OS_WIN)
|
||||
WinBluetoothDeviceDiscoveryAgent *remoteDeviceDiscoveryAgent;
|
||||
#else
|
||||
QBluetoothLocalDevice *localDevice;
|
||||
QBluetoothDeviceDiscoveryAgent *remoteDeviceDiscoveryAgent;
|
||||
#endif
|
||||
QSharedPointer<QBluetoothDeviceInfo> selectedRemoteDeviceInfo;
|
||||
|
||||
void updateLocalDeviceInformation();
|
||||
|
|
|
@ -10,6 +10,12 @@
|
|||
|
||||
#if defined(SSRF_CUSTOM_SERIAL)
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <ws2bth.h>
|
||||
#endif
|
||||
|
||||
#include <libdivecomputer/custom_serial.h>
|
||||
|
||||
extern "C" {
|
||||
|
@ -19,7 +25,11 @@ typedef struct serial_t {
|
|||
/*
|
||||
* RFCOMM socket used for Bluetooth Serial communication.
|
||||
*/
|
||||
#if defined(Q_OS_WIN)
|
||||
SOCKET socket;
|
||||
#else
|
||||
QBluetoothSocket *socket;
|
||||
#endif
|
||||
long timeout;
|
||||
} serial_t;
|
||||
|
||||
|
@ -40,6 +50,9 @@ static int qt_serial_open(serial_t **out, dc_context_t *context, const char* dev
|
|||
// Default to blocking reads.
|
||||
serial_port->timeout = -1;
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO connect the device
|
||||
#else
|
||||
// Create a RFCOMM socket
|
||||
serial_port->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
|
||||
|
||||
|
@ -118,7 +131,7 @@ static int qt_serial_open(serial_t **out, dc_context_t *context, const char* dev
|
|||
return QBluetoothSocket::UnknownSocketError;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
*out = serial_port;
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
@ -126,19 +139,36 @@ static int qt_serial_open(serial_t **out, dc_context_t *context, const char* dev
|
|||
|
||||
static int qt_serial_close(serial_t *device)
|
||||
{
|
||||
if (device == NULL || device->socket == NULL)
|
||||
if (device == NULL)
|
||||
return DC_STATUS_SUCCESS;
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
// TODO do the cleanup
|
||||
#else
|
||||
if (device->socket == NULL) {
|
||||
free(device);
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
device->socket->close();
|
||||
|
||||
delete device->socket;
|
||||
free(device);
|
||||
#endif
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static int qt_serial_read(serial_t *device, void* data, unsigned int size)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
// TODO read *size* bytes from the device
|
||||
|
||||
return 0;
|
||||
#else
|
||||
if (device == NULL || device->socket == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
|
@ -167,10 +197,19 @@ static int qt_serial_read(serial_t *device, void* data, unsigned int size)
|
|||
}
|
||||
|
||||
return nbytes;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int qt_serial_write(serial_t *device, const void* data, unsigned int size)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
// TODO write *size* bytes from data to the device
|
||||
|
||||
return 0;
|
||||
#else
|
||||
if (device == NULL || device->socket == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
|
@ -196,32 +235,54 @@ static int qt_serial_write(serial_t *device, const void* data, unsigned int size
|
|||
}
|
||||
|
||||
return nbytes;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int qt_serial_flush(serial_t *device, int queue)
|
||||
{
|
||||
if (device == NULL || device->socket == NULL)
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
//TODO: add implementation
|
||||
#if !defined(Q_OS_WIN)
|
||||
if (device->socket == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
#endif
|
||||
// TODO: add implementation
|
||||
|
||||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static int qt_serial_get_received(serial_t *device)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
// TODO use WSAIoctl to get the information
|
||||
|
||||
return 0;
|
||||
#else
|
||||
if (device == NULL || device->socket == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
return device->socket->bytesAvailable();
|
||||
#endif
|
||||
}
|
||||
|
||||
static int qt_serial_get_transmitted(serial_t *device)
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
if (device == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
// TODO add implementation
|
||||
|
||||
return 0;
|
||||
#else
|
||||
if (device == NULL || device->socket == NULL)
|
||||
return DC_STATUS_INVALIDARGS;
|
||||
|
||||
return device->socket->bytesToWrite();
|
||||
#endif
|
||||
}
|
||||
|
||||
static int qt_serial_set_timeout(serial_t *device, long timeout)
|
||||
|
@ -234,6 +295,7 @@ static int qt_serial_set_timeout(serial_t *device, long timeout)
|
|||
return DC_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
const dc_serial_operations_t qt_serial_ops = {
|
||||
.open = qt_serial_open,
|
||||
.close = qt_serial_close,
|
||||
|
|
Loading…
Reference in a new issue