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:
Claudiu Olteanu 2015-08-18 20:51:10 +03:00 committed by Dirk Hohndel
parent 2aa6ffe0c8
commit 7488f5500e
3 changed files with 197 additions and 8 deletions

View file

@ -8,7 +8,6 @@
BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) : BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) :
QDialog(parent), QDialog(parent),
localDevice(new QBluetoothLocalDevice),
ui(new Ui::BtDeviceSelectionDialog) ui(new Ui::BtDeviceSelectionDialog)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -21,9 +20,16 @@ BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) :
// Disable the save button because there is no device selected // Disable the save button because there is no device selected
ui->save->setEnabled(false); ui->save->setEnabled(false);
// Add event for item selection
connect(ui->discoveredDevicesList, SIGNAL(itemClicked(QListWidgetItem*)), connect(ui->discoveredDevicesList, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(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 // Populate the list with local bluetooth devices
QList<QBluetoothHostInfo> localAvailableDevices = localDevice->allDevices(); QList<QBluetoothHostInfo> localAvailableDevices = localDevice->allDevices();
int availableDevicesSize = localAvailableDevices.size(); int availableDevicesSize = localAvailableDevices.size();
@ -56,15 +62,19 @@ BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) :
// Initialize the device discovery agent // Initialize the device discovery agent
if (localDevice->isValid()) if (localDevice->isValid())
initializeDeviceDiscoveryAgent(); initializeDeviceDiscoveryAgent();
#endif
} }
BtDeviceSelectionDialog::~BtDeviceSelectionDialog() BtDeviceSelectionDialog::~BtDeviceSelectionDialog()
{ {
delete ui; delete ui;
#if defined(Q_OS_WIN)
// Terminate the use of Winsock 2 DLL
#else
// Clean the local device // Clean the local device
delete localDevice; delete localDevice;
#endif
// Clean the device discovery agent // Clean the device discovery agent
if (remoteDeviceDiscoveryAgent->isActive()) if (remoteDeviceDiscoveryAgent->isActive())
remoteDeviceDiscoveryAgent->stop(); remoteDeviceDiscoveryAgent->stop();
@ -74,6 +84,9 @@ BtDeviceSelectionDialog::~BtDeviceSelectionDialog()
void BtDeviceSelectionDialog::on_changeDeviceState_clicked() void BtDeviceSelectionDialog::on_changeDeviceState_clicked()
{ {
#if defined(Q_OS_WIN)
// TODO add implementation
#else
if (localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff) { if (localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff) {
ui->dialogStatus->setText("Trying to turn on the local Bluetooth device..."); ui->dialogStatus->setText("Trying to turn on the local Bluetooth device...");
localDevice->powerOn(); localDevice->powerOn();
@ -81,6 +94,7 @@ void BtDeviceSelectionDialog::on_changeDeviceState_clicked()
ui->dialogStatus->setText("Trying to turn off the local Bluetooth device..."); ui->dialogStatus->setText("Trying to turn off the local Bluetooth device...");
localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff); localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff);
} }
#endif
} }
void BtDeviceSelectionDialog::on_save_clicked() void BtDeviceSelectionDialog::on_save_clicked()
@ -136,16 +150,23 @@ void BtDeviceSelectionDialog::remoteDeviceScanFinished()
void BtDeviceSelectionDialog::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode) void BtDeviceSelectionDialog::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode)
{ {
#if defined(Q_OS_WIN)
// TODO add implementation
#else
bool on = !(mode == QBluetoothLocalDevice::HostPoweredOff); bool on = !(mode == QBluetoothLocalDevice::HostPoweredOff);
ui->dialogStatus->setText(QString("The local Bluetooth device was turned %1.") ui->dialogStatus->setText(QString("The local Bluetooth device was turned %1.")
.arg(on? "ON" : "OFF")); .arg(on? "ON" : "OFF"));
ui->deviceState->setChecked(on); ui->deviceState->setChecked(on);
ui->scan->setEnabled(on); ui->scan->setEnabled(on);
#endif
} }
void BtDeviceSelectionDialog::addRemoteDevice(const QBluetoothDeviceInfo &remoteDeviceInfo) 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 // By default we use the status label and the color for the UNPAIRED state
QColor pairingColor = QColor(Qt::red); QColor pairingColor = QColor(Qt::red);
QString pairingStatusLabel = QString("UNPAIRED"); QString pairingStatusLabel = QString("UNPAIRED");
@ -168,10 +189,14 @@ void BtDeviceSelectionDialog::addRemoteDevice(const QBluetoothDeviceInfo &remote
item->setBackgroundColor(pairingColor); item->setBackgroundColor(pairingColor);
ui->discoveredDevicesList->addItem(item); ui->discoveredDevicesList->addItem(item);
#endif
} }
void BtDeviceSelectionDialog::itemClicked(QListWidgetItem *item) 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>(); QBluetoothDeviceInfo remoteDeviceInfo = item->data(Qt::UserRole).value<QBluetoothDeviceInfo>();
QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(remoteDeviceInfo.address()); QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(remoteDeviceInfo.address());
@ -184,10 +209,14 @@ void BtDeviceSelectionDialog::itemClicked(QListWidgetItem *item)
.arg(remoteDeviceInfo.address().toString())); .arg(remoteDeviceInfo.address().toString()));
ui->save->setEnabled(true); ui->save->setEnabled(true);
} }
#endif
} }
void BtDeviceSelectionDialog::localDeviceChanged(int index) void BtDeviceSelectionDialog::localDeviceChanged(int index)
{ {
#if defined(Q_OS_WIN)
// TODO add implementation
#else
QBluetoothAddress localDeviceSelectedAddress = ui->localSelectedDevice->itemData(index, Qt::UserRole).value<QBluetoothAddress>(); QBluetoothAddress localDeviceSelectedAddress = ui->localSelectedDevice->itemData(index, Qt::UserRole).value<QBluetoothAddress>();
// Delete the old localDevice // Delete the old localDevice
@ -208,10 +237,14 @@ void BtDeviceSelectionDialog::localDeviceChanged(int index)
// Initialize the device discovery agent // Initialize the device discovery agent
if (localDevice->isValid()) if (localDevice->isValid())
initializeDeviceDiscoveryAgent(); initializeDeviceDiscoveryAgent();
#endif
} }
void BtDeviceSelectionDialog::displayPairingMenu(const QPoint &pos) void BtDeviceSelectionDialog::displayPairingMenu(const QPoint &pos)
{ {
#if defined(Q_OS_WIN)
// TODO add implementation
#else
QMenu menu(this); QMenu menu(this);
QAction *pairAction = menu.addAction("Pair"); QAction *pairAction = menu.addAction("Pair");
QAction *removePairAction = menu.addAction("Remove Pairing"); QAction *removePairAction = menu.addAction("Remove Pairing");
@ -238,6 +271,7 @@ void BtDeviceSelectionDialog::displayPairingMenu(const QPoint &pos)
.arg(currentRemoteDeviceInfo.address().toString())); .arg(currentRemoteDeviceInfo.address().toString()));
localDevice->requestPairing(currentRemoteDeviceInfo.address(), QBluetoothLocalDevice::Unpaired); localDevice->requestPairing(currentRemoteDeviceInfo.address(), QBluetoothLocalDevice::Unpaired);
} }
#endif
} }
void BtDeviceSelectionDialog::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing) void BtDeviceSelectionDialog::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
@ -338,6 +372,9 @@ QString BtDeviceSelectionDialog::getSelectedDeviceName()
void BtDeviceSelectionDialog::updateLocalDeviceInformation() void BtDeviceSelectionDialog::updateLocalDeviceInformation()
{ {
#if defined(Q_OS_WIN)
// TODO add implementation
#else
// Check if the selected Bluetooth device can be accessed // Check if the selected Bluetooth device can be accessed
if (!localDevice->isValid()) { if (!localDevice->isValid()) {
QString na = QString("Not available"); QString na = QString("Not available");
@ -377,10 +414,14 @@ void BtDeviceSelectionDialog::updateLocalDeviceInformation()
connect(localDevice, SIGNAL(error(QBluetoothLocalDevice::Error)), connect(localDevice, SIGNAL(error(QBluetoothLocalDevice::Error)),
this, SLOT(error(QBluetoothLocalDevice::Error))); this, SLOT(error(QBluetoothLocalDevice::Error)));
#endif
} }
void BtDeviceSelectionDialog::initializeDeviceDiscoveryAgent() void BtDeviceSelectionDialog::initializeDeviceDiscoveryAgent()
{ {
#if defined(Q_OS_WIN)
// TODO initialize the discovery agent
#else
// Intialize the discovery agent // Intialize the discovery agent
remoteDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(localDevice->address()); remoteDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(localDevice->address());
@ -400,4 +441,46 @@ void BtDeviceSelectionDialog::initializeDeviceDiscoveryAgent()
this, SLOT(remoteDeviceScanFinished())); this, SLOT(remoteDeviceScanFinished()));
connect(remoteDeviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), connect(remoteDeviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
this, SLOT(deviceDiscoveryError(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

View file

@ -8,7 +8,23 @@
#include <QtBluetooth/qbluetoothglobal.h> #include <QtBluetooth/qbluetoothglobal.h>
#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent> #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) Q_DECLARE_METATYPE(QBluetoothDeviceInfo)
#endif #endif
@ -16,6 +32,30 @@ namespace Ui {
class BtDeviceSelectionDialog; 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 { class BtDeviceSelectionDialog : public QDialog {
Q_OBJECT Q_OBJECT
@ -42,8 +82,12 @@ private slots:
private: private:
Ui::BtDeviceSelectionDialog *ui; Ui::BtDeviceSelectionDialog *ui;
#if defined(Q_OS_WIN)
WinBluetoothDeviceDiscoveryAgent *remoteDeviceDiscoveryAgent;
#else
QBluetoothLocalDevice *localDevice; QBluetoothLocalDevice *localDevice;
QBluetoothDeviceDiscoveryAgent *remoteDeviceDiscoveryAgent; QBluetoothDeviceDiscoveryAgent *remoteDeviceDiscoveryAgent;
#endif
QSharedPointer<QBluetoothDeviceInfo> selectedRemoteDeviceInfo; QSharedPointer<QBluetoothDeviceInfo> selectedRemoteDeviceInfo;
void updateLocalDeviceInformation(); void updateLocalDeviceInformation();

View file

@ -10,6 +10,12 @@
#if defined(SSRF_CUSTOM_SERIAL) #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> #include <libdivecomputer/custom_serial.h>
extern "C" { extern "C" {
@ -19,7 +25,11 @@ typedef struct serial_t {
/* /*
* RFCOMM socket used for Bluetooth Serial communication. * RFCOMM socket used for Bluetooth Serial communication.
*/ */
#if defined(Q_OS_WIN)
SOCKET socket;
#else
QBluetoothSocket *socket; QBluetoothSocket *socket;
#endif
long timeout; long timeout;
} serial_t; } 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. // Default to blocking reads.
serial_port->timeout = -1; serial_port->timeout = -1;
#if defined(Q_OS_WIN)
// TODO connect the device
#else
// Create a RFCOMM socket // Create a RFCOMM socket
serial_port->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); 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; return QBluetoothSocket::UnknownSocketError;
} }
} }
#endif
*out = serial_port; *out = serial_port;
return DC_STATUS_SUCCESS; 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) static int qt_serial_close(serial_t *device)
{ {
if (device == NULL || device->socket == NULL) if (device == NULL)
return DC_STATUS_SUCCESS; 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(); device->socket->close();
delete device->socket; delete device->socket;
free(device); free(device);
#endif
return DC_STATUS_SUCCESS; return DC_STATUS_SUCCESS;
} }
static int qt_serial_read(serial_t *device, void* data, unsigned int size) 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) if (device == NULL || device->socket == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
@ -167,10 +197,19 @@ static int qt_serial_read(serial_t *device, void* data, unsigned int size)
} }
return nbytes; return nbytes;
#endif
} }
static int qt_serial_write(serial_t *device, const void* data, unsigned int size) 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) if (device == NULL || device->socket == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
@ -196,32 +235,54 @@ static int qt_serial_write(serial_t *device, const void* data, unsigned int size
} }
return nbytes; return nbytes;
#endif
} }
static int qt_serial_flush(serial_t *device, int queue) static int qt_serial_flush(serial_t *device, int queue)
{ {
if (device == NULL || device->socket == NULL) if (device == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
#if !defined(Q_OS_WIN)
//TODO: add implementation if (device->socket == NULL)
return DC_STATUS_INVALIDARGS;
#endif
// TODO: add implementation
return DC_STATUS_SUCCESS; return DC_STATUS_SUCCESS;
} }
static int qt_serial_get_received(serial_t *device) 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) if (device == NULL || device->socket == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
return device->socket->bytesAvailable(); return device->socket->bytesAvailable();
#endif
} }
static int qt_serial_get_transmitted(serial_t *device) 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) if (device == NULL || device->socket == NULL)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
return device->socket->bytesToWrite(); return device->socket->bytesToWrite();
#endif
} }
static int qt_serial_set_timeout(serial_t *device, long timeout) 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; return DC_STATUS_SUCCESS;
} }
const dc_serial_operations_t qt_serial_ops = { const dc_serial_operations_t qt_serial_ops = {
.open = qt_serial_open, .open = qt_serial_open,
.close = qt_serial_close, .close = qt_serial_close,