From e2cac92d2304b21e7745e37a18c98c47086fc4b4 Mon Sep 17 00:00:00 2001 From: Claudiu Olteanu Date: Tue, 18 Aug 2015 21:37:11 +0300 Subject: [PATCH] Add implementation for BTH custom serial open method on Windows platforms Implement the custom serial open method using the WinSocket2 API. First the device address is converted from text representation into a sockaddr structure. Then a connection is initiated to the device using device's Serial Port service. Signed-off-by: Claudiu Olteanu Signed-off-by: Dirk Hohndel --- qtserialbluetooth.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/qtserialbluetooth.cpp b/qtserialbluetooth.cpp index 37ea22597..0edd3d42b 100644 --- a/qtserialbluetooth.cpp +++ b/qtserialbluetooth.cpp @@ -51,7 +51,49 @@ static int qt_serial_open(serial_t **out, dc_context_t *context, const char* dev serial_port->timeout = -1; #if defined(Q_OS_WIN) - // TODO connect the device + // Create a RFCOMM socket + serial_port->socket = ::socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); + + if (serial_port->socket == INVALID_SOCKET) + return DC_STATUS_IO; + + SOCKADDR_BTH socketBthAddress; + int socketBthAddressBth = sizeof (socketBthAddress); + char *address = strdup(devaddr); + + ZeroMemory(&socketBthAddress, socketBthAddressBth); + qDebug() << "Trying to connect to address " << devaddr; + + if (WSAStringToAddressA(address, + AF_BTH, + NULL, + (LPSOCKADDR) &socketBthAddress, + &socketBthAddressBth + ) != 0) { + qDebug() << "FAiled to convert the address " << address; + free(address); + + return DC_STATUS_IO; + } + + free(address); + + socketBthAddress.addressFamily = AF_BTH; + socketBthAddress.port = BT_PORT_ANY; + memset(&socketBthAddress.serviceClassId, 0, sizeof(socketBthAddress.serviceClassId)); + socketBthAddress.serviceClassId = SerialPortServiceClass_UUID; + + // Try to connect to the device + if (::connect(serial_port->socket, + (struct sockaddr *) &socketBthAddress, + socketBthAddressBth + ) != 0) { + qDebug() << "Failed to connect to device"; + + return DC_STATUS_NODEVICE; + } + + qDebug() << "Succesfully connected to device"; #else // Create a RFCOMM socket serial_port->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);