Dubs Connman wifi connect Qt - qt

I'am working on an imx6, and i'am trying to connect to a wifi network through Dbus with a Qt application.
The application connect correctly to connman via Dbus and i recieve correctly the wifi services.
The problem is that when i try to connect to a wiif network i catch this error :
"Method "Connect" with signature "ss" on interface "net.connman.Service" doesn't exist
The code that i'am using in Qt application to coonect to a wifi network is:
QDBusInterface *iface =
new QDBusInterface("net.connman","/net/connman/technology/wifi","net.connman.Service",QDBusConnection::systemBus());
if (!iface->isValid())
{
qDebug() << Q_FUNC_INFO << "Fail to connect to the Connman Technology interface: " << QDBusConnection::systemBus().lastError().message();
}
QDBusReply<void> reply = iface->call("Connect","/net/connman/service/wifi_88da1a4db14c_41684179_managed_psk","password");
if (!reply.isValid())
{
qDebug() << "Call connect result: " << reply.error().message();
}
When i try to connect to the wifi network with shell commands using connmanctl it works like a charm.

I had the same problem on imx6. The solution which works for me is creating a configuration file for network before invoking Connect method.
The file should be in /var/lib/connman and name [SSID].config .
File content:
[service_wifi_PUT_SERVICE_NAME]
Name = PUT_SSID
Type = wifi
Passphrase = PUT_PASSWORD
And try connecting in this way:
QDBusInterface *iface = new QDBusInterface("net.connman", QString{"/net/connman/service/%1"}.arg(SERVICE_NAME), "net.connman.Service", QDBusConnection::systemBus());
QDBusReply<void> reply = iface->call("Connect");
if(!reply.isValid() {
...

Related

Qt How to properly connect to a phone programmatically (Bluetooth A2DP, AVRCP, HSP, HFP) in Linux

I am trying to develop an application that uses bluez stack along with pulseaudio and ofono in order to connect to a phone and achieve tasks such as media playback (A2DP), media control (AVRCP), and handsfree-based telephony (HFP). When I connect to my phone through bluetoothctl, it automatically connects to all the available profiles, so using all profiles A2DP, AVRCP, HFP through my program is achievable. If I don't connect to my phone using bluetoothctl, handsfree /HFP modem is not enabled/powered in ofono.
However, when I use QBluetoothSocket in Qt and connect using a profile, there is always a profile that is not connected. For example connecting to Handsfree profile, telephony works, but the media control does not work. In short, I want to be able to connect to bluetooth as bluetoothctl does. What I have in Qt is as follows (in short):
static const QList<QBluetoothUuid> audioUuids = QList<QBluetoothUuid>()
<< QBluetoothUuid::HeadsetAG
<< QBluetoothUuid::AV_RemoteControlTarget;
..
void BtConnection::setConnection(int index)
{
if(m_bluetoothSocket == nullptr) {
m_bluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
qDebug() << "Created Bluetooth Socket";
}
if(m_bluetoothSocket != nullptr) {
connect(m_bluetoothSocket, SIGNAL(connected()), this, SLOT(connected()));
connect(m_bluetoothSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(m_bluetoothSocket, SIGNAL(error(QBluetoothSocket::SocketError)),
this, SLOT(connectionError(QBluetoothSocket::SocketError)));
}
m_device = get(index);
// Check if an element in m_device.serviceUuids() match with an element in audioUuids
QList<QBluetoothUuid>::const_iterator uuid;
for (uuid = audioUuids.begin(); uuid != audioUuids.end(); ++uuid) {
if(m_device.serviceUuids().indexOf(*uuid) > 0) {
// This device supports one of the uuids we have scanned for
if(m_bluetoothSocket != nullptr) {
qDebug() << "*****Connecting... " << *uuid;
m_bluetoothSocket->connectToService(m_device.address(), *uuid);
return;
}
}
}
qDebug() << "*****Cannot connect to service...";
}
I would be willing to post more of the code if this is not clear to you. Any help is greately appreciated on how to connect to bluetooth with Qt as bluetoothctl does.
Not a direct answer but you might want to check KDE's KDEConnect project. It already does what you are looking for and might either be a source of inspiration or you could contribute to the project.

QT c++ open Serial port more than once

I search an example to open multiple serial ports in qt.
My open port function settings forward from other class
void MainWindow::openSerialPort(){
SettingsDialog::Settings p = settings->settings();
serial->setPortName(p.name);
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
if (serial->open(QIODevice::ReadWrite)) {
ui->connectAction->setEnabled(false);
ui->disconnectAction->setEnabled(true);
ui->settingsAction->setEnabled(false);
showStatusMessage(tr("Connected to %1 : OK")
.arg(p.name));
} else { //gdy sie nie udalo error
QMessageBox::critical(this, tr("Error"), serial->errorString());
showStatusMessage(tr("Open error"));
}
}
As far as I remember single instance of QSerialPort if opened does it exclusively so no other instance has access to this port. I guess creating other QSP and opening other port would do the thing.

QNetworkAccesssManager icinga / nagios acknowledge

I have a function who sending ack into the icinga / nagios server.
Function - sendAcknowledge
void MNetworkConnector::sendAcknowledge(QString service, QString host)
{
QNetworkAccessManager *ackmanager;
ackmanager = new QNetworkAccessManager();
QString ackcommand = "http://nagioscore.demos.nagios.com/nagios/cgi-bin/cmd.cgi?cmd_typ=34&cmd_mod=2&host=#host#&service=#service#&com_author=nagiosadmin&com_data=Sent:+mMonitor&btnSubmit=Commit";
service = service.replace(" ", "+");
ackcommand = ackcommand.replace("#host#", host).replace("#service#", service);
connect(ackmanager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished2(QNetworkReply*)));
QNetworkRequest myReq;
QUrl myUrl(ackcommand);
myUrl.setUserName("nagiosadmin");
myUrl.setPassword("nagiosadmin");
myReq.setUrl(myUrl);
myReq.setRawHeader("Referer", "http://nagioscore.demos.nagios.com/nagios/cgi-bin/cmd.cgi");
ackmanager->get(myReq);
}
Function - replyFinished2
void MNetworkConnector::replyFinished2(QNetworkReply *r)
{
qDebug() << "reply output:" << r->readAll();
}
System returned several errors.
If I test this source on my corporate icinga server, I have error:
Not all commands could be send off successfully - Not Authorized
If I test it manualy on my corporate icinga server, I have error: Error: This appears to be a CSRF attack! The command wasn't issued via Classic-UI itself!
If I test it manualy on nagios test site, I have no error. Set service ack is Ok.
If I test this source I have error:Sorry, but you are not authorized to commit the specified command.
What's wrong? Thank you for all your ideas. At first I need solved problem in Icinga, the nagios test page I used only as alternative tests.
When the website requests authentication QNetworkAccessManager will emit the authenticationRequired() signal. Try connecting the signal with a slot and then set the username and password on the QAuthenticator object passed as argument to the slot.
Connection:
connect(ackmanager, SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)), this, SLOT(authenticationRequired(QNetworkReply *, QAuthenticator *)));
Slot:
void MNetworkConnector::authenticationRequired(QNetworkReply *r, QAuthenticator *authenticator)
{
authenticator->setUser("nagiosadmin");
authenticator->setPassword("nagiosadmin");
}
The problem was sensitive to uppercase and lowercase letters in the HOST.

QTcpSocket - Connection Refused until Port Changed

I apologize, I was not sure how to deliver a concise title for this issue.
Background: I am using QTcpSocket to connect to a PLC Simulator application. Previously I was using PLCQTLIB to connect to the simulator and everything was working fine. The library did not offer enough functionality for my project so I have created my own library for interfacing Qt with the libnodave library.
The simulator runs on IP Address 192.168.32.1 and Port 102
Current Issue: I enter the IP (192.168.32.1) and Port (102) and press connect. I receive:
TCP Error = QAbstractSocket::ConnectionRefusedError
TCP Error = QAbstractSocket::SocketTimeoutError
If I change the port to 80 and press connect, the connection is successful. However the connection to the PLC Simulator will fail because it is not listening on Port 80.
Now that a successful connection has been established to 192.168.32.1 and the current state of the connection is disconnected, I can enter the correct port 102 and connect successfully.
Question: Why does the TCP Socket not establish a connection to Port 102 until after a connection has been previously opened on Port 80? No firewalls exist and all communication is occurring on the local machine.
Declared in Header:
QTcpSocket *tcp;
Source File:
PLCLibNoDave::PLCLibNoDave()
{
tcp = new QTcpSocket();
connect(tcp, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(tcpStateChanged(QAbstractSocket::SocketState)));
connect(tcp, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(tcpErrorHandler(QAbstractSocket::SocketError)));
connect(tcp, SIGNAL(hostFound()), this, SLOT(tcpHostFound()));
connect(tcp, SIGNAL(connected()), this, SLOT(tcpConnected()));
connect(tcp, SIGNAL(disconnected()), this, SLOT(tcpDisconnected()));
}
void PLCLibNoDave::connectTCP(int port, QString ip)
{
tcp->connectToHost(ip,port);
if(!tcp->waitForConnected(3000)){
tcp->disconnectFromHost();
return;
}
tcpHandle = tcp->socketDescriptor();
if(tcpHandle == -1){
tcp->disconnectFromHost();
qDebug() << "Invalid Socket Descriptor on Connect";
return;
}
return;
}
void PLCLibNoDave::disconnectTCP()
{
tcp->disconnectFromHost();
if(tcp->state() == QAbstractSocket::UnconnectedState ||
tcp->waitForDisconnected(1000)){
tcpError = tcp->error();
}
else{
tcpError = tcp->error();
qDebug() << "Disconnect Failed: " << tcp->errorString();
}
return;
}

Create a RFCOMM Server using Qt for symbian

I´m new on Qt, Symbian devices and Bluetooth.
I have to set up a RFCOMM server to receive connection from a bluetooth device (its a pinpad) that only support SPP profile.
I searched on google and found some examples, like this: http://doc.qt.nokia.com/qtmobility/btchat.html
Tried everything but I can´t connect them. Both devices was paired, but when I try to connect it fails.
When I asked to the manufacturer, they said that it was happening because the SPP Server on my celphone was not avaiable to listen for incoming connections.
I´m creating the RFCOMM server and registering the service just like the example, but it still not working.
Someone can help me?
I'm using Qt with QtMobility 1.2.0 and my cellphone is a Nokia 500 (Symbian^3).
Here is my code:
void bluetooth::startServer()
{
QString deviceName;
QBluetoothLocalDevice localDevice;
if (rfcommServer)
return;
localDevice.powerOn();
localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
deviceName = localDevice.name();
rfcommServer = new QRfcommServer(this);
connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(vConectou())) ;
if( rfcommServer->listen(localDevice.address(),
quint16(rfcommServer->serverPort()) ) )
emit vExibeMsg("Listening");
else
emit vExibeMsg("Error");
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle,
(uint)0x00010010);
QBluetoothServiceInfo::Sequence classId;
classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Test Server"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
tr("Test Bluetooth"));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, deviceName );
serviceInfo.setServiceUuid(QBluetoothUuid(QBluetoothUuid::Rfcomm));
serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
QBluetoothServiceInfo::Sequence protocolDescriptorList;
QBluetoothServiceInfo::Sequence protocol;
/*
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
protocolDescriptorList.append(QVariant::fromValue(protocol));
protocol.clear();
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
<< QVariant::fromValue(quint8(rfcommServer->serverPort()));
protocolDescriptorList.append(QVariant::fromValue(protocol));
protocol.clear();
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
protocolDescriptorList.append(QVariant::fromValue(protocol));
*/
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
<< QVariant::fromValue(quint8(rfcommServer->serverPort()));
protocolDescriptorList.append(QVariant::fromValue(protocol));
serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
protocolDescriptorList);
if( serviceInfo.registerService() )
{
emit vExibeMsg("Waiting for connections...");
}
else
{
emit vExibeMsg("Error to create the service");
}
}

Resources