Skip to content

Commit 422df29

Browse files
committed
plugins: mcumgr: Updates and fixes to bluetooth form and connection
1 parent c349401 commit 422df29

5 files changed

Lines changed: 171 additions & 4 deletions

File tree

plugins/mcumgr/bluetooth_setup.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ bluetooth_setup::bluetooth_setup(QWidget *parent) :
2828
ui(new Ui::bluetooth_setup)
2929
{
3030
ui->setupUi(this);
31+
ui->btn_connect->setEnabled(false);
32+
ui->btn_disconnect->setEnabled(false);
33+
ui->btn_refresh->setEnabled(false);
3134
}
3235

3336
bluetooth_setup::~bluetooth_setup()
@@ -78,3 +81,37 @@ void bluetooth_setup::add_debug(QString data)
7881
ui->edit_debug->appendPlainText(data);
7982
}
8083

84+
void bluetooth_setup::discovery_state(bool started)
85+
{
86+
ui->btn_refresh->setEnabled(!started);
87+
88+
if (started == true)
89+
{
90+
ui->btn_connect->setEnabled(false);
91+
ui->btn_disconnect->setEnabled(false);
92+
}
93+
}
94+
95+
void bluetooth_setup::connection_state(bool connected)
96+
{
97+
ui->btn_connect->setEnabled(!connected);
98+
ui->btn_disconnect->setEnabled(connected);
99+
ui->btn_refresh->setEnabled(!connected);
100+
}
101+
102+
void bluetooth_setup::on_list_devices_currentRowChanged(int row)
103+
{
104+
if (row >= 0)
105+
{
106+
bool scanning;
107+
bool connecting;
108+
109+
emit bluetooth_status(&scanning, &connecting);
110+
111+
ui->btn_connect->setEnabled((connecting == false ? true : false));
112+
}
113+
else
114+
{
115+
ui->btn_connect->setEnabled(false);
116+
}
117+
}

plugins/mcumgr/bluetooth_setup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,22 @@ class bluetooth_setup : public QDialog
4040
void clear_devices();
4141
void add_device(QString *data);
4242
void add_debug(QString data);
43+
void discovery_state(bool started);
44+
void connection_state(bool connected);
4345

4446
private slots:
4547
void on_btn_refresh_clicked();
4648
void on_btn_connect_clicked();
4749
void on_btn_disconnect_clicked();
4850
void on_btn_close_clicked();
4951
void on_list_devices_itemDoubleClicked(QListWidgetItem *item);
52+
void on_list_devices_currentRowChanged(int row);
5053

5154
signals:
5255
void refresh_devices();
5356
void connect_to_device(uint16_t index);
5457
void disconnect_from_device();
58+
void bluetooth_status(bool *scanning, bool *connecting);
5559

5660
private:
5761
Ui::bluetooth_setup *ui;

plugins/mcumgr/bluetooth_setup.ui

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
</property>
2525
</widget>
2626
<widget class="QPushButton" name="btn_refresh">
27+
<property name="enabled">
28+
<bool>false</bool>
29+
</property>
2730
<property name="geometry">
2831
<rect>
2932
<x>20</x>
@@ -37,6 +40,9 @@
3740
</property>
3841
</widget>
3942
<widget class="QPushButton" name="btn_connect">
43+
<property name="enabled">
44+
<bool>false</bool>
45+
</property>
4046
<property name="geometry">
4147
<rect>
4248
<x>100</x>
@@ -50,6 +56,9 @@
5056
</property>
5157
</widget>
5258
<widget class="QPushButton" name="btn_disconnect">
59+
<property name="enabled">
60+
<bool>false</bool>
61+
</property>
5362
<property name="geometry">
5463
<rect>
5564
<x>180</x>

plugins/mcumgr/smp_bluetooth.cpp

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ smp_bluetooth::smp_bluetooth(QObject *parent)
6060
QObject::connect(bluetooth_window, SIGNAL(refresh_devices()), this, SLOT(form_refresh_devices()));
6161
QObject::connect(bluetooth_window, SIGNAL(connect_to_device(uint16_t)), this, SLOT(form_connect_to_device(uint16_t)));
6262
QObject::connect(bluetooth_window, SIGNAL(disconnect_from_device()), this, SLOT(form_disconnect_from_device()));
63+
QObject::connect(bluetooth_window, SIGNAL(bluetooth_status(bool*,bool*)), this, SLOT(form_bluetooth_status(bool*,bool*)));
6364

6465
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
6566
discoveryAgent->setLowEnergyDiscoveryTimeout(8000);
@@ -132,6 +133,7 @@ void smp_bluetooth::deviceUpdated(const QBluetoothDeviceInfo &info, QBluetoothDe
132133
void smp_bluetooth::finished()
133134
{
134135
bluetooth_window->add_debug("Discovery finished");
136+
bluetooth_window->discovery_state(false);
135137
}
136138

137139
void smp_bluetooth::connected()
@@ -174,6 +176,8 @@ void smp_bluetooth::disconnected()
174176

175177
controller = nullptr;
176178
}
179+
180+
bluetooth_window->connection_state(false);
177181
}
178182

179183
void smp_bluetooth::discovery_finished()
@@ -299,7 +303,81 @@ void smp_bluetooth::mcumgr_service_state_changed(QLowEnergyService::ServiceState
299303

300304
void smp_bluetooth::errorz(QLowEnergyController::Error error)
301305
{
302-
bluetooth_window->add_debug(QString::number(error));
306+
bool disconnect_from_device = false;
307+
QString err;
308+
switch (error) {
309+
case QLowEnergyController::NoError:
310+
{
311+
err = "No error";
312+
break;
313+
}
314+
case QLowEnergyController::UnknownError:
315+
{
316+
disconnect_from_device = true;
317+
err = "Unknown error";
318+
break;
319+
}
320+
case QLowEnergyController::UnknownRemoteDeviceError:
321+
{
322+
disconnect_from_device = true;
323+
err = "Unknown remote device";
324+
break;
325+
}
326+
case QLowEnergyController::NetworkError:
327+
{
328+
disconnect_from_device = true;
329+
err = "Network error";
330+
break;
331+
}
332+
case QLowEnergyController::InvalidBluetoothAdapterError:
333+
{
334+
disconnect_from_device = true;
335+
err = "Invalud bluetooth adapter";
336+
break;
337+
}
338+
case QLowEnergyController::ConnectionError:
339+
{
340+
disconnect_from_device = true;
341+
err = "Connection error";
342+
break;
343+
}
344+
case QLowEnergyController::AdvertisingError:
345+
{
346+
err = "Advertising error";
347+
break;
348+
}
349+
case QLowEnergyController::RemoteHostClosedError:
350+
{
351+
disconnect_from_device = true;
352+
err = "Remote host closed";
353+
break;
354+
}
355+
case QLowEnergyController::AuthorizationError:
356+
{
357+
err = "Authorisation error";
358+
break;
359+
}
360+
default:
361+
{
362+
err = "Other";
363+
break;
364+
}
365+
};
366+
// bluetooth_window->add_debug(QString::number(error));
367+
bluetooth_window->add_debug(err);
368+
369+
if (disconnect_from_device == true)
370+
{
371+
if (device_connected == true)
372+
{
373+
disconnect(true);
374+
}
375+
else
376+
{
377+
bluetooth_window->discovery_state(false);
378+
bluetooth_window->connection_state(false);
379+
}
380+
}
303381
}
304382

305383
int smp_bluetooth::is_connected()
@@ -320,6 +398,11 @@ void smp_bluetooth::connection_updated(QLowEnergyConnectionParameters parameters
320398

321399
int smp_bluetooth::send(smp_message *message)
322400
{
401+
if (device_connected == false)
402+
{
403+
return -1;
404+
}
405+
323406
retry_count = 0;
324407
sendbuffer.clear();
325408

@@ -375,6 +458,11 @@ void smp_bluetooth::mcumgr_service_error(QLowEnergyService::ServiceError error)
375458

376459
void smp_bluetooth::form_refresh_devices()
377460
{
461+
if (device_connected == true)
462+
{
463+
return;
464+
}
465+
378466
bluetooth_window->clear_devices();
379467
bluetooth_device_list.clear();
380468

@@ -384,10 +472,16 @@ void smp_bluetooth::form_refresh_devices()
384472
}
385473

386474
discoveryAgent->start();
475+
bluetooth_window->discovery_state(true);
387476
}
388477

389478
void smp_bluetooth::form_connect_to_device(uint16_t index)
390479
{
480+
if (discoveryAgent->isActive())
481+
{
482+
discoveryAgent->stop();
483+
}
484+
391485
if (controller)
392486
{
393487
QObject::disconnect(controller, SIGNAL(connected()), this, SLOT(connected()));
@@ -421,18 +515,30 @@ void smp_bluetooth::form_connect_to_device(uint16_t index)
421515
// controller->setRemoteAddressType(QLowEnergyController::PublicAddress);
422516
// }
423517
controller->connectToDevice();
518+
519+
bluetooth_window->connection_state(true);
424520
}
425521

426522
void smp_bluetooth::form_disconnect_from_device()
427523
{
428-
controller->disconnectFromDevice();
524+
if (device_connected == true)
525+
{
526+
controller->disconnectFromDevice();
527+
}
429528
}
430529

431-
432530
int smp_bluetooth::connect()
433531
{
434532
bluetooth_window->show();
435-
discoveryAgent->start();
533+
534+
if (device_connected == false)
535+
{
536+
bluetooth_window->clear_devices();
537+
bluetooth_device_list.clear();
538+
discoveryAgent->start();
539+
bluetooth_window->discovery_state(true);
540+
}
541+
436542
return 0;
437543
}
438544

@@ -442,6 +548,10 @@ int smp_bluetooth::disconnect(bool force)
442548
{
443549
controller->disconnectFromDevice();
444550
}
551+
else
552+
{
553+
bluetooth_window->connection_state(false);
554+
}
445555

446556
return 0;
447557
}
@@ -473,3 +583,9 @@ void smp_bluetooth::close_connect_dialog()
473583
bluetooth_window->close();
474584
}
475585
}
586+
587+
void smp_bluetooth::form_bluetooth_status(bool *scanning, bool *connecting)
588+
{
589+
*scanning = discoveryAgent->isActive();
590+
*connecting = device_connected;
591+
}

plugins/mcumgr/smp_bluetooth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private slots:
6969
void form_refresh_devices();
7070
void form_connect_to_device(uint16_t index);
7171
void form_disconnect_from_device();
72+
void form_bluetooth_status(bool *scanning, bool *connecting);
7273
// void connection_updated(QLowEnergyConnectionParameters parameters);
7374

7475
signals:

0 commit comments

Comments
 (0)