From a293bd11bc0ab5268ea18154f5f05493e00c03dc Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sat, 9 Jan 2021 20:01:09 +0100 Subject: [PATCH 1/9] Add a .pri file for libnymea-app --- libnymea-app/libnymea-app.pri | 320 ++++++++++++++++++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 libnymea-app/libnymea-app.pri diff --git a/libnymea-app/libnymea-app.pri b/libnymea-app/libnymea-app.pri new file mode 100644 index 00000000..c5141efa --- /dev/null +++ b/libnymea-app/libnymea-app.pri @@ -0,0 +1,320 @@ +!win32:!nozeroconf { + # To enable this on Windows we'd need to install Bonjour + # https://support.apple.com/kb/DL999 + message("Building with QtZeroConf") + DEFINES += QZEROCONF_STATIC + DEFINES += WITH_ZEROCONF + include(../QtZeroConf/qtzeroconf.pri) +} else { + message("Building without QtZeroConf") +} + +include(../nymea-remoteproxy/libnymea-remoteproxyclient/libnymea-remoteproxyclient.pri) + + +QT -= gui +QT += network websockets bluetooth charts quick + +LIBS += -lssl -lcrypto + +INCLUDEPATH += \ + $${PWD} \ + $$top_srcdir/QtZeroConf + +SOURCES += \ + $${PWD}/configuration/networkmanager.cpp \ + $${PWD}/engine.cpp \ + $${PWD}/models/barseriesadapter.cpp \ + $${PWD}/models/sortfilterproxymodel.cpp \ + $${PWD}/models/xyseriesadapter.cpp \ + $${PWD}/ruletemplates/calendaritemtemplate.cpp \ + $${PWD}/ruletemplates/timedescriptortemplate.cpp \ + $${PWD}/ruletemplates/timeeventitemtemplate.cpp \ + $${PWD}/scripting/scriptautosaver.cpp \ + $${PWD}/types/browseritem.cpp \ + $${PWD}/types/browseritems.cpp \ + $${PWD}/types/networkdevice.cpp \ + $${PWD}/types/networkdevices.cpp \ + $${PWD}/types/package.cpp \ + $${PWD}/types/packages.cpp \ + $${PWD}/types/repositories.cpp \ + $${PWD}/types/repository.cpp \ + $${PWD}/types/script.cpp \ + $${PWD}/types/scripts.cpp \ + $${PWD}/types/types.cpp \ + $${PWD}/types/vendor.cpp \ + $${PWD}/types/vendors.cpp \ + $${PWD}/types/deviceclass.cpp \ + $${PWD}/types/device.cpp \ + $${PWD}/types/param.cpp \ + $${PWD}/types/params.cpp \ + $${PWD}/types/paramtype.cpp \ + $${PWD}/types/paramtypes.cpp \ + $${PWD}/types/statetype.cpp \ + $${PWD}/types/statetypes.cpp \ + $${PWD}/types/statetypesproxy.cpp \ + $${PWD}/types/eventtype.cpp \ + $${PWD}/types/eventtypes.cpp \ + $${PWD}/types/actiontype.cpp \ + $${PWD}/types/actiontypes.cpp \ + $${PWD}/types/state.cpp \ + $${PWD}/types/states.cpp \ + $${PWD}/types/statesproxy.cpp \ + $${PWD}/types/plugin.cpp \ + $${PWD}/types/plugins.cpp \ + $${PWD}/types/rules.cpp \ + $${PWD}/types/rule.cpp \ + $${PWD}/types/eventdescriptor.cpp \ + $${PWD}/types/eventdescriptors.cpp \ + $${PWD}/types/ruleaction.cpp \ + $${PWD}/types/ruleactions.cpp \ + $${PWD}/types/ruleactionparams.cpp \ + $${PWD}/types/ruleactionparam.cpp \ + $${PWD}/types/logentry.cpp \ + $${PWD}/types/stateevaluators.cpp \ + $${PWD}/types/stateevaluator.cpp \ + $${PWD}/types/statedescriptor.cpp \ + $${PWD}/types/paramdescriptor.cpp \ + $${PWD}/types/paramdescriptors.cpp \ + $${PWD}/types/interface.cpp \ + $${PWD}/types/interfaces.cpp \ + $${PWD}/types/timedescriptor.cpp \ + $${PWD}/types/timeeventitem.cpp \ + $${PWD}/types/calendaritem.cpp \ + $${PWD}/types/timeeventitems.cpp \ + $${PWD}/types/calendaritems.cpp \ + $${PWD}/types/repeatingoption.cpp \ + $${PWD}/types/tag.cpp \ + $${PWD}/types/tags.cpp \ + $${PWD}/types/wirelessaccesspoint.cpp \ + $${PWD}/types/wirelessaccesspoints.cpp \ + $${PWD}/types/tokeninfo.cpp \ + $${PWD}/types/tokeninfos.cpp \ + $${PWD}/types/userinfo.cpp \ + $${PWD}/types/ioconnection.cpp \ + $${PWD}/types/ioconnections.cpp \ + $${PWD}/types/ioconnectionwatcher.cpp \ + $${PWD}/connection/nymeahost.cpp \ + $${PWD}/connection/nymeahosts.cpp \ + $${PWD}/connection/nymeaconnection.cpp \ + $${PWD}/connection/nymeatransportinterface.cpp \ + $${PWD}/connection/websockettransport.cpp \ + $${PWD}/connection/tcpsockettransport.cpp \ + $${PWD}/connection/bluetoothtransport.cpp \ + $${PWD}/connection/awsclient.cpp \ + $${PWD}/connection/discovery/nymeadiscovery.cpp \ + $${PWD}/connection/discovery/upnpdiscovery.cpp \ + $${PWD}/connection/discovery/zeroconfdiscovery.cpp \ + $${PWD}/connection/discovery/bluetoothservicediscovery.cpp \ + $${PWD}/devicemanager.cpp \ + $${PWD}/jsonrpc/jsontypes.cpp \ + $${PWD}/jsonrpc/jsonrpcclient.cpp \ + $${PWD}/jsonrpc/jsonhandler.cpp \ + $${PWD}/devices.cpp \ + $${PWD}/devicesproxy.cpp \ + $${PWD}/deviceclasses.cpp \ + $${PWD}/deviceclassesproxy.cpp \ + $${PWD}/devicediscovery.cpp \ + $${PWD}/models/packagesfiltermodel.cpp \ + $${PWD}/models/taglistmodel.cpp \ + $${PWD}/scripting/codecompletion.cpp \ + $${PWD}/scripting/completionmodel.cpp \ + $${PWD}/scriptmanager.cpp \ + $${PWD}/scriptsyntaxhighlighter.cpp \ + $${PWD}/usermanager.cpp \ + $${PWD}/vendorsproxy.cpp \ + $${PWD}/pluginsproxy.cpp \ + $${PWD}/interfacesmodel.cpp \ + $${PWD}/rulemanager.cpp \ + $${PWD}/models/rulesfiltermodel.cpp \ + $${PWD}/models/logsmodel.cpp \ + $${PWD}/logmanager.cpp \ + $${PWD}/wifisetup/bluetoothdevice.cpp \ + $${PWD}/wifisetup/bluetoothdeviceinfo.cpp \ + $${PWD}/wifisetup/bluetoothdeviceinfos.cpp \ + $${PWD}/wifisetup/bluetoothdiscovery.cpp \ + $${PWD}/wifisetup/wirelesssetupmanager.cpp \ + $${PWD}/wifisetup/networkmanagercontroller.cpp \ + $${PWD}/models/logsmodelng.cpp \ + $${PWD}/models/interfacesproxy.cpp \ + $${PWD}/models/tagsproxymodel.cpp \ + $${PWD}/tagsmanager.cpp \ + $${PWD}/models/wirelessaccesspointsproxy.cpp \ + $${PWD}/ruletemplates/ruletemplate.cpp \ + $${PWD}/ruletemplates/ruletemplates.cpp \ + $${PWD}/ruletemplates/eventdescriptortemplate.cpp \ + $${PWD}/ruletemplates/ruleactiontemplate.cpp \ + $${PWD}/ruletemplates/stateevaluatortemplate.cpp \ + $${PWD}/ruletemplates/statedescriptortemplate.cpp \ + $${PWD}/connection/cloudtransport.cpp \ + $${PWD}/connection/sigv4utils.cpp \ + $${PWD}/ruletemplates/ruleactionparamtemplate.cpp \ + $${PWD}/configuration/serverconfiguration.cpp \ + $${PWD}/configuration/serverconfigurations.cpp \ + $${PWD}/configuration/nymeaconfiguration.cpp \ + $${PWD}/configuration/mqttpolicy.cpp \ + $${PWD}/configuration/mqttpolicies.cpp \ + $${PWD}/models/devicemodel.cpp \ + $${PWD}/system/systemcontroller.cpp \ + $${PWD}/thinggroup.cpp \ + $${PWD}/zigbee/zigbeeadapters.cpp \ + $${PWD}/zigbee/zigbeeadaptersproxy.cpp \ + $${PWD}/zigbee/zigbeemanager.cpp \ + $${PWD}/zigbee/zigbeeadapter.cpp \ + $${PWD}/zigbee/zigbeenetwork.cpp \ + $${PWD}/zigbee/zigbeenetworks.cpp + + + +HEADERS += \ + $${PWD}/configuration/networkmanager.h \ + $${PWD}/engine.h \ + $${PWD}/models/barseriesadapter.h \ + $${PWD}/models/sortfilterproxymodel.h \ + $${PWD}/models/xyseriesadapter.h \ + $${PWD}/ruletemplates/calendaritemtemplate.h \ + $${PWD}/ruletemplates/timedescriptortemplate.h \ + $${PWD}/ruletemplates/timeeventitemtemplate.h \ + $${PWD}/scripting/scriptautosaver.h \ + $${PWD}/types/browseritem.h \ + $${PWD}/types/browseritems.h \ + $${PWD}/types/networkdevice.h \ + $${PWD}/types/networkdevices.h \ + $${PWD}/types/package.h \ + $${PWD}/types/packages.h \ + $${PWD}/types/repositories.h \ + $${PWD}/types/repository.h \ + $${PWD}/types/script.h \ + $${PWD}/types/scripts.h \ + $${PWD}/types/types.h \ + $${PWD}/types/vendor.h \ + $${PWD}/types/vendors.h \ + $${PWD}/types/deviceclass.h \ + $${PWD}/types/device.h \ + $${PWD}/types/param.h \ + $${PWD}/types/params.h \ + $${PWD}/types/paramtype.h \ + $${PWD}/types/paramtypes.h \ + $${PWD}/types/statetype.h \ + $${PWD}/types/statetypes.h \ + $${PWD}/types/statetypesproxy.h \ + $${PWD}/types/eventtype.h \ + $${PWD}/types/eventtypes.h \ + $${PWD}/types/actiontype.h \ + $${PWD}/types/actiontypes.h \ + $${PWD}/types/state.h \ + $${PWD}/types/states.h \ + $${PWD}/types/statesproxy.h \ + $${PWD}/types/plugin.h \ + $${PWD}/types/plugins.h \ + $${PWD}/types/rules.h \ + $${PWD}/types/rule.h \ + $${PWD}/types/eventdescriptor.h \ + $${PWD}/types/eventdescriptors.h \ + $${PWD}/types/ruleaction.h \ + $${PWD}/types/ruleactions.h \ + $${PWD}/types/ruleactionparams.h \ + $${PWD}/types/ruleactionparam.h \ + $${PWD}/types/logentry.h \ + $${PWD}/types/stateevaluators.h \ + $${PWD}/types/stateevaluator.h \ + $${PWD}/types/statedescriptor.h \ + $${PWD}/types/paramdescriptor.h \ + $${PWD}/types/paramdescriptors.h \ + $${PWD}/types/interface.h \ + $${PWD}/types/interfaces.h \ + $${PWD}/types/timedescriptor.h \ + $${PWD}/types/timeeventitem.h \ + $${PWD}/types/calendaritem.h \ + $${PWD}/types/timeeventitems.h \ + $${PWD}/types/calendaritems.h \ + $${PWD}/types/repeatingoption.h \ + $${PWD}/types/tag.h \ + $${PWD}/types/tags.h \ + $${PWD}/types/wirelessaccesspoint.h \ + $${PWD}/types/wirelessaccesspoints.h \ + $${PWD}/types/tokeninfo.h \ + $${PWD}/types/tokeninfos.h \ + $${PWD}/types/userinfo.h \ + $${PWD}/types/ioconnection.h \ + $${PWD}/types/ioconnections.h \ + $${PWD}/types/ioconnectionwatcher.h \ + $${PWD}/connection/nymeahost.h \ + $${PWD}/connection/nymeahosts.h \ + $${PWD}/connection/nymeaconnection.h \ + $${PWD}/connection/nymeatransportinterface.h \ + $${PWD}/connection/websockettransport.h \ + $${PWD}/connection/tcpsockettransport.h \ + $${PWD}/connection/bluetoothtransport.h \ + $${PWD}/connection/awsclient.h \ + $${PWD}/connection/sigv4utils.h \ + $${PWD}/connection/discovery/nymeadiscovery.h \ + $${PWD}/connection/discovery/upnpdiscovery.h \ + $${PWD}/connection/discovery/zeroconfdiscovery.h \ + $${PWD}/connection/discovery/bluetoothservicediscovery.h \ + $${PWD}/devicemanager.h \ + $${PWD}/jsonrpc/jsontypes.h \ + $${PWD}/jsonrpc/jsonrpcclient.h \ + $${PWD}/jsonrpc/jsonhandler.h \ + $${PWD}/devices.h \ + $${PWD}/devicesproxy.h \ + $${PWD}/deviceclasses.h \ + $${PWD}/deviceclassesproxy.h \ + $${PWD}/devicediscovery.h \ + $${PWD}/models/packagesfiltermodel.h \ + $${PWD}/models/taglistmodel.h \ + $${PWD}/scripting/codecompletion.h \ + $${PWD}/scripting/completionmodel.h \ + $${PWD}/scriptmanager.h \ + $${PWD}/scriptsyntaxhighlighter.h \ + $${PWD}/usermanager.h \ + $${PWD}/vendorsproxy.h \ + $${PWD}/pluginsproxy.h \ + $${PWD}/interfacesmodel.h \ + $${PWD}/rulemanager.h \ + $${PWD}/models/rulesfiltermodel.h \ + $${PWD}/models/logsmodel.h \ + $${PWD}/logmanager.h \ + $${PWD}/wifisetup/bluetoothdevice.h \ + $${PWD}/wifisetup/bluetoothdeviceinfo.h \ + $${PWD}/wifisetup/bluetoothdeviceinfos.h \ + $${PWD}/wifisetup/bluetoothdiscovery.h \ + $${PWD}/wifisetup/wirelesssetupmanager.h \ + $${PWD}/wifisetup/networkmanagercontroller.h \ + $${PWD}/libnymea-app-core.h \ + $${PWD}/models/logsmodelng.h \ + $${PWD}/models/interfacesproxy.h \ + $${PWD}/tagsmanager.h \ + $${PWD}/models/tagsproxymodel.h \ + $${PWD}/models/wirelessaccesspointsproxy.h \ + $${PWD}/ruletemplates/ruletemplate.h \ + $${PWD}/ruletemplates/ruletemplates.h \ + $${PWD}/ruletemplates/eventdescriptortemplate.h \ + $${PWD}/ruletemplates/ruleactiontemplate.h \ + $${PWD}/ruletemplates/stateevaluatortemplate.h \ + $${PWD}/ruletemplates/statedescriptortemplate.h \ + $${PWD}/connection/cloudtransport.h \ + $${PWD}/ruletemplates/ruleactionparamtemplate.h \ + $${PWD}/configuration/serverconfiguration.h \ + $${PWD}/configuration/serverconfigurations.h \ + $${PWD}/configuration/nymeaconfiguration.h \ + $${PWD}/configuration/mqttpolicy.h \ + $${PWD}/configuration/mqttpolicies.h \ + $${PWD}/models/devicemodel.h \ + $${PWD}/system/systemcontroller.h \ + $${PWD}/thinggroup.h \ + $${PWD}/zigbee/zigbeeadapters.h \ + $${PWD}/zigbee/zigbeeadaptersproxy.h \ + $${PWD}/zigbee/zigbeemanager.h \ + $${PWD}/zigbee/zigbeeadapter.h \ + $${PWD}/zigbee/zigbeenetwork.h \ + $${PWD}/zigbee/zigbeenetworks.h + +ubports: { + DEFINES += UBPORTS +} + +# https://bugreports.qt.io/browse/QTBUG-83165 +android: { + DESTDIR = $${ANDROID_TARGET_ARCH} +} From 85a03577bae602d368d239c63f5fc3b7d62217e1 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sat, 9 Jan 2021 20:06:50 +0100 Subject: [PATCH 2/9] Use the .pri ourselves --- libnymea-app/libnymea-app.pro | 319 +--------------------------------- 1 file changed, 1 insertion(+), 318 deletions(-) diff --git a/libnymea-app/libnymea-app.pro b/libnymea-app/libnymea-app.pro index b0cdc5b9..f813b818 100644 --- a/libnymea-app/libnymea-app.pro +++ b/libnymea-app/libnymea-app.pro @@ -3,321 +3,4 @@ TEMPLATE = lib CONFIG += staticlib include(../config.pri) -!win32:!nozeroconf { - # To enable this on Windows we'd need to install Bonjour - # https://support.apple.com/kb/DL999 - message("Building with QtZeroConf") - DEFINES += QZEROCONF_STATIC - DEFINES += WITH_ZEROCONF - include(../QtZeroConf/qtzeroconf.pri) -} else { - message("Building without QtZeroConf") -} - -include(../nymea-remoteproxy/libnymea-remoteproxyclient/libnymea-remoteproxyclient.pri) - - -QT -= gui -QT += network websockets bluetooth charts quick - -LIBS += -lssl -lcrypto - -INCLUDEPATH += $$top_srcdir/QtZeroConf - -SOURCES += \ - configuration/networkmanager.cpp \ - engine.cpp \ - models/barseriesadapter.cpp \ - models/sortfilterproxymodel.cpp \ - models/xyseriesadapter.cpp \ - ruletemplates/calendaritemtemplate.cpp \ - ruletemplates/timedescriptortemplate.cpp \ - ruletemplates/timeeventitemtemplate.cpp \ - scripting/scriptautosaver.cpp \ - types/browseritem.cpp \ - types/browseritems.cpp \ - types/networkdevice.cpp \ - types/networkdevices.cpp \ - types/package.cpp \ - types/packages.cpp \ - types/repositories.cpp \ - types/repository.cpp \ - types/script.cpp \ - types/scripts.cpp \ - types/types.cpp \ - types/vendor.cpp \ - types/vendors.cpp \ - types/deviceclass.cpp \ - types/device.cpp \ - types/param.cpp \ - types/params.cpp \ - types/paramtype.cpp \ - types/paramtypes.cpp \ - types/statetype.cpp \ - types/statetypes.cpp \ - types/statetypesproxy.cpp \ - types/eventtype.cpp \ - types/eventtypes.cpp \ - types/actiontype.cpp \ - types/actiontypes.cpp \ - types/state.cpp \ - types/states.cpp \ - types/statesproxy.cpp \ - types/plugin.cpp \ - types/plugins.cpp \ - types/rules.cpp \ - types/rule.cpp \ - types/eventdescriptor.cpp \ - types/eventdescriptors.cpp \ - types/ruleaction.cpp \ - types/ruleactions.cpp \ - types/ruleactionparams.cpp \ - types/ruleactionparam.cpp \ - types/logentry.cpp \ - types/stateevaluators.cpp \ - types/stateevaluator.cpp \ - types/statedescriptor.cpp \ - types/paramdescriptor.cpp \ - types/paramdescriptors.cpp \ - types/interface.cpp \ - types/interfaces.cpp \ - types/timedescriptor.cpp \ - types/timeeventitem.cpp \ - types/calendaritem.cpp \ - types/timeeventitems.cpp \ - types/calendaritems.cpp \ - types/repeatingoption.cpp \ - types/tag.cpp \ - types/tags.cpp \ - types/wirelessaccesspoint.cpp \ - types/wirelessaccesspoints.cpp \ - types/tokeninfo.cpp \ - types/tokeninfos.cpp \ - types/userinfo.cpp \ - types/ioconnection.cpp \ - types/ioconnections.cpp \ - types/ioconnectionwatcher.cpp \ - connection/nymeahost.cpp \ - connection/nymeahosts.cpp \ - connection/nymeaconnection.cpp \ - connection/nymeatransportinterface.cpp \ - connection/websockettransport.cpp \ - connection/tcpsockettransport.cpp \ - connection/bluetoothtransport.cpp \ - connection/awsclient.cpp \ - connection/discovery/nymeadiscovery.cpp \ - connection/discovery/upnpdiscovery.cpp \ - connection/discovery/zeroconfdiscovery.cpp \ - connection/discovery/bluetoothservicediscovery.cpp \ - devicemanager.cpp \ - jsonrpc/jsontypes.cpp \ - jsonrpc/jsonrpcclient.cpp \ - jsonrpc/jsonhandler.cpp \ - devices.cpp \ - devicesproxy.cpp \ - deviceclasses.cpp \ - deviceclassesproxy.cpp \ - devicediscovery.cpp \ - models/packagesfiltermodel.cpp \ - models/taglistmodel.cpp \ - scripting/codecompletion.cpp \ - scripting/completionmodel.cpp \ - scriptmanager.cpp \ - scriptsyntaxhighlighter.cpp \ - usermanager.cpp \ - vendorsproxy.cpp \ - pluginsproxy.cpp \ - interfacesmodel.cpp \ - rulemanager.cpp \ - models/rulesfiltermodel.cpp \ - models/logsmodel.cpp \ - logmanager.cpp \ - wifisetup/bluetoothdevice.cpp \ - wifisetup/bluetoothdeviceinfo.cpp \ - wifisetup/bluetoothdeviceinfos.cpp \ - wifisetup/bluetoothdiscovery.cpp \ - wifisetup/wirelesssetupmanager.cpp \ - wifisetup/networkmanagercontroller.cpp \ - models/logsmodelng.cpp \ - models/interfacesproxy.cpp \ - models/tagsproxymodel.cpp \ - tagsmanager.cpp \ - models/wirelessaccesspointsproxy.cpp \ - ruletemplates/ruletemplate.cpp \ - ruletemplates/ruletemplates.cpp \ - ruletemplates/eventdescriptortemplate.cpp \ - ruletemplates/ruleactiontemplate.cpp \ - ruletemplates/stateevaluatortemplate.cpp \ - ruletemplates/statedescriptortemplate.cpp \ - connection/cloudtransport.cpp \ - connection/sigv4utils.cpp \ - ruletemplates/ruleactionparamtemplate.cpp \ - configuration/serverconfiguration.cpp \ - configuration/serverconfigurations.cpp \ - configuration/nymeaconfiguration.cpp \ - configuration/mqttpolicy.cpp \ - configuration/mqttpolicies.cpp \ - models/devicemodel.cpp \ - system/systemcontroller.cpp \ - thinggroup.cpp \ - zigbee/zigbeeadapters.cpp \ - zigbee/zigbeeadaptersproxy.cpp \ - zigbee/zigbeemanager.cpp \ - zigbee/zigbeeadapter.cpp \ - zigbee/zigbeenetwork.cpp \ - zigbee/zigbeenetworks.cpp - - - -HEADERS += \ - configuration/networkmanager.h \ - engine.h \ - models/barseriesadapter.h \ - models/sortfilterproxymodel.h \ - models/xyseriesadapter.h \ - ruletemplates/calendaritemtemplate.h \ - ruletemplates/timedescriptortemplate.h \ - ruletemplates/timeeventitemtemplate.h \ - scripting/scriptautosaver.h \ - types/browseritem.h \ - types/browseritems.h \ - types/networkdevice.h \ - types/networkdevices.h \ - types/package.h \ - types/packages.h \ - types/repositories.h \ - types/repository.h \ - types/script.h \ - types/scripts.h \ - types/types.h \ - types/vendor.h \ - types/vendors.h \ - types/deviceclass.h \ - types/device.h \ - types/param.h \ - types/params.h \ - types/paramtype.h \ - types/paramtypes.h \ - types/statetype.h \ - types/statetypes.h \ - types/statetypesproxy.h \ - types/eventtype.h \ - types/eventtypes.h \ - types/actiontype.h \ - types/actiontypes.h \ - types/state.h \ - types/states.h \ - types/statesproxy.h \ - types/plugin.h \ - types/plugins.h \ - types/rules.h \ - types/rule.h \ - types/eventdescriptor.h \ - types/eventdescriptors.h \ - types/ruleaction.h \ - types/ruleactions.h \ - types/ruleactionparams.h \ - types/ruleactionparam.h \ - types/logentry.h \ - types/stateevaluators.h \ - types/stateevaluator.h \ - types/statedescriptor.h \ - types/paramdescriptor.h \ - types/paramdescriptors.h \ - types/interface.h \ - types/interfaces.h \ - types/timedescriptor.h \ - types/timeeventitem.h \ - types/calendaritem.h \ - types/timeeventitems.h \ - types/calendaritems.h \ - types/repeatingoption.h \ - types/tag.h \ - types/tags.h \ - types/wirelessaccesspoint.h \ - types/wirelessaccesspoints.h \ - types/tokeninfo.h \ - types/tokeninfos.h \ - types/userinfo.h \ - types/ioconnection.h \ - types/ioconnections.h \ - types/ioconnectionwatcher.h \ - connection/nymeahost.h \ - connection/nymeahosts.h \ - connection/nymeaconnection.h \ - connection/nymeatransportinterface.h \ - connection/websockettransport.h \ - connection/tcpsockettransport.h \ - connection/bluetoothtransport.h \ - connection/awsclient.h \ - connection/sigv4utils.h \ - connection/discovery/nymeadiscovery.h \ - connection/discovery/upnpdiscovery.h \ - connection/discovery/zeroconfdiscovery.h \ - connection/discovery/bluetoothservicediscovery.h \ - devicemanager.h \ - jsonrpc/jsontypes.h \ - jsonrpc/jsonrpcclient.h \ - jsonrpc/jsonhandler.h \ - devices.h \ - devicesproxy.h \ - deviceclasses.h \ - deviceclassesproxy.h \ - devicediscovery.h \ - models/packagesfiltermodel.h \ - models/taglistmodel.h \ - scripting/codecompletion.h \ - scripting/completionmodel.h \ - scriptmanager.h \ - scriptsyntaxhighlighter.h \ - usermanager.h \ - vendorsproxy.h \ - pluginsproxy.h \ - interfacesmodel.h \ - rulemanager.h \ - models/rulesfiltermodel.h \ - models/logsmodel.h \ - logmanager.h \ - wifisetup/bluetoothdevice.h \ - wifisetup/bluetoothdeviceinfo.h \ - wifisetup/bluetoothdeviceinfos.h \ - wifisetup/bluetoothdiscovery.h \ - wifisetup/wirelesssetupmanager.h \ - wifisetup/networkmanagercontroller.h \ - libnymea-app-core.h \ - models/logsmodelng.h \ - models/interfacesproxy.h \ - tagsmanager.h \ - models/tagsproxymodel.h \ - models/wirelessaccesspointsproxy.h \ - ruletemplates/ruletemplate.h \ - ruletemplates/ruletemplates.h \ - ruletemplates/eventdescriptortemplate.h \ - ruletemplates/ruleactiontemplate.h \ - ruletemplates/stateevaluatortemplate.h \ - ruletemplates/statedescriptortemplate.h \ - connection/cloudtransport.h \ - ruletemplates/ruleactionparamtemplate.h \ - configuration/serverconfiguration.h \ - configuration/serverconfigurations.h \ - configuration/nymeaconfiguration.h \ - configuration/mqttpolicy.h \ - configuration/mqttpolicies.h \ - models/devicemodel.h \ - system/systemcontroller.h \ - thinggroup.h \ - zigbee/zigbeeadapters.h \ - zigbee/zigbeeadaptersproxy.h \ - zigbee/zigbeemanager.h \ - zigbee/zigbeeadapter.h \ - zigbee/zigbeenetwork.h \ - zigbee/zigbeenetworks.h - -ubports: { - DEFINES += UBPORTS -} - -# https://bugreports.qt.io/browse/QTBUG-83165 -android: { - DESTDIR = $${ANDROID_TARGET_ARCH} -} +include(libnymea-app.pri) From 456bd70bc5084a776a24a83ad77578089c63d967 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 11 Jan 2021 11:43:51 +0100 Subject: [PATCH 3/9] rework-bluetooth-setup --- libnymea-app/libnymea-app-core.h | 2 + libnymea-app/libnymea-app.pri | 2 + .../wifisetup/bluetoothdeviceinfo.cpp | 7 +- libnymea-app/wifisetup/bluetoothdeviceinfo.h | 3 +- libnymea-app/wifisetup/bluetoothdiscovery.cpp | 26 +- libnymea-app/wifisetup/btwifisetup.cpp | 302 ++++++++++++++++++ libnymea-app/wifisetup/btwifisetup.h | 199 ++++++++++++ .../wifisetup/networkmanagercontroller.cpp | 2 +- 8 files changed, 523 insertions(+), 20 deletions(-) create mode 100644 libnymea-app/wifisetup/btwifisetup.cpp create mode 100644 libnymea-app/wifisetup/btwifisetup.h diff --git a/libnymea-app/libnymea-app-core.h b/libnymea-app/libnymea-app-core.h index e9a4bbbd..4496fe7a 100644 --- a/libnymea-app/libnymea-app-core.h +++ b/libnymea-app/libnymea-app-core.h @@ -73,6 +73,7 @@ #include "configuration/serverconfigurations.h" #include "configuration/mqttpolicy.h" #include "configuration/mqttpolicies.h" +#include "wifisetup/btwifisetup.h" #include "wifisetup/networkmanagercontroller.h" #include "types/wirelessaccesspoint.h" #include "types/wirelessaccesspoints.h" @@ -270,6 +271,7 @@ void registerQmlTypes() { qmlRegisterType(uri, 1, 0, "TagListModel"); qmlRegisterType(uri, 1, 0, "TagListProxyModel"); + qmlRegisterType(uri, 1, 0, "BtWiFiSetup"); qmlRegisterType(uri, 1, 0, "NetworkManagerController"); qmlRegisterType(uri, 1, 0, "BluetoothDiscovery"); qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDeviceInfo", "Can't create this in QML. Get it from the DeviceInfos."); diff --git a/libnymea-app/libnymea-app.pri b/libnymea-app/libnymea-app.pri index c5141efa..d3a0f5cf 100644 --- a/libnymea-app/libnymea-app.pri +++ b/libnymea-app/libnymea-app.pri @@ -22,6 +22,7 @@ INCLUDEPATH += \ $$top_srcdir/QtZeroConf SOURCES += \ + $$PWD/wifisetup/btwifisetup.cpp \ $${PWD}/configuration/networkmanager.cpp \ $${PWD}/engine.cpp \ $${PWD}/models/barseriesadapter.cpp \ @@ -167,6 +168,7 @@ SOURCES += \ HEADERS += \ + $$PWD/wifisetup/btwifisetup.h \ $${PWD}/configuration/networkmanager.h \ $${PWD}/engine.h \ $${PWD}/models/barseriesadapter.h \ diff --git a/libnymea-app/wifisetup/bluetoothdeviceinfo.cpp b/libnymea-app/wifisetup/bluetoothdeviceinfo.cpp index 762ee42b..850e9f4f 100644 --- a/libnymea-app/wifisetup/bluetoothdeviceinfo.cpp +++ b/libnymea-app/wifisetup/bluetoothdeviceinfo.cpp @@ -41,6 +41,11 @@ BluetoothDeviceInfo::BluetoothDeviceInfo(const QBluetoothDeviceInfo &deviceInfo) m_deviceInfo = deviceInfo; } +BluetoothDeviceInfo::~BluetoothDeviceInfo() +{ + qDebug() << "~BluetoothDeviceInfo"; +} + QString BluetoothDeviceInfo::address() const { #ifdef Q_OS_MAC @@ -62,7 +67,7 @@ bool BluetoothDeviceInfo::isLowEnergy() const return m_deviceInfo.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration; } -QBluetoothDeviceInfo BluetoothDeviceInfo::getBluetoothDeviceInfo() const +QBluetoothDeviceInfo BluetoothDeviceInfo::bluetoothDeviceInfo() const { return m_deviceInfo; } diff --git a/libnymea-app/wifisetup/bluetoothdeviceinfo.h b/libnymea-app/wifisetup/bluetoothdeviceinfo.h index 6eda3a47..f72363d2 100644 --- a/libnymea-app/wifisetup/bluetoothdeviceinfo.h +++ b/libnymea-app/wifisetup/bluetoothdeviceinfo.h @@ -45,12 +45,13 @@ class BluetoothDeviceInfo : public QObject public: BluetoothDeviceInfo(); BluetoothDeviceInfo(const QBluetoothDeviceInfo &deviceInfo); + ~BluetoothDeviceInfo(); QString address() const; QString name() const; bool isLowEnergy() const; - QBluetoothDeviceInfo getBluetoothDeviceInfo() const; + QBluetoothDeviceInfo bluetoothDeviceInfo() const; void setBluetoothDeviceInfo(const QBluetoothDeviceInfo &deviceInfo); signals: diff --git a/libnymea-app/wifisetup/bluetoothdiscovery.cpp b/libnymea-app/wifisetup/bluetoothdiscovery.cpp index 84a3bb07..1e4703e5 100644 --- a/libnymea-app/wifisetup/bluetoothdiscovery.cpp +++ b/libnymea-app/wifisetup/bluetoothdiscovery.cpp @@ -174,34 +174,26 @@ void BluetoothDiscovery::onBluetoothHostModeChanged(const QBluetoothLocalDevice: void BluetoothDiscovery::deviceDiscovered(const QBluetoothDeviceInfo &deviceInfo) { - if (!deviceInfo.isValid()) - return; - - BluetoothDeviceInfo *deviceInformation = new BluetoothDeviceInfo(deviceInfo); - bool isLowEnergy = deviceInfo.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration; - - qDebug() << "BluetoothDiscovery: [+]" << deviceInformation->name() << "(" << deviceInformation->address() << ")" << (isLowEnergy ? "LE" : ""); - - if (!isLowEnergy || deviceInformation->name().isEmpty()) { - delete deviceInformation; + if (!deviceInfo.isValid() + || !deviceInfo.coreConfigurations().testFlag(QBluetoothDeviceInfo::LowEnergyCoreConfiguration) + || deviceInfo.name().isEmpty()) { return; } - // Check if we already have added this device info foreach (BluetoothDeviceInfo *di, m_deviceInfos->deviceInfos()) { - if (di->address() == deviceInformation->address()) { - qWarning() << "BluetoothDiscover: device" << deviceInformation->name() << "(" << deviceInformation->address() << ") already added"; - deviceInformation->deleteLater(); + if (di->address() == deviceInfo.address().toString()) { return; } } + BluetoothDeviceInfo *deviceInformation = new BluetoothDeviceInfo(deviceInfo); +// qDebug() << "BluetoothDiscovery: [+]" << deviceInformation->name() << "(" << deviceInformation->address() << ")" << (isLowEnergy ? "LE" : "") << deviceInfo.majorDeviceClass() << deviceInfo.minorDeviceClass() << deviceInfo.serviceClasses(); m_deviceInfos->addBluetoothDeviceInfo(deviceInformation); } void BluetoothDiscovery::discoveryFinished() { - qDebug() << "BluetoothDiscovery: Discovery finished"; + qDebug() << "BluetoothDiscovery: Discovery finished" << m_discoveryEnabled << this; if (m_discoveryEnabled) { qDebug() << "BluetoothDiscovery: Restarting discovery"; m_discoveryAgent->start(); @@ -240,9 +232,9 @@ void BluetoothDiscovery::start() m_discoveryAgent->stop(); } - m_deviceInfos->clearModel(); +// m_deviceInfos->clearModel(); - qDebug() << "BluetoothDiscovery: Start discovering."; + qDebug() << "BluetoothDiscovery: Starting discovery."; m_discoveryAgent->start(); emit discoveringChanged(); } diff --git a/libnymea-app/wifisetup/btwifisetup.cpp b/libnymea-app/wifisetup/btwifisetup.cpp new file mode 100644 index 00000000..47a686dc --- /dev/null +++ b/libnymea-app/wifisetup/btwifisetup.cpp @@ -0,0 +1,302 @@ +#include "btwifisetup.h" +#include "bluetoothdeviceinfo.h" +#include "types/wirelessaccesspoints.h" +#include "types/wirelessaccesspoint.h" + +#include + +static QBluetoothUuid wifiServiceUuid = QBluetoothUuid(QUuid("e081fec0-f757-4449-b9c9-bfa83133f7fc")); +static QBluetoothUuid wifiCommanderCharacteristicUuid = QBluetoothUuid(QUuid("e081fec1-f757-4449-b9c9-bfa83133f7fc")); +static QBluetoothUuid wifiResponseCharacteristicUuid = QBluetoothUuid(QUuid("e081fec2-f757-4449-b9c9-bfa83133f7fc")); +static QBluetoothUuid wifiStatusCharacteristicUuid = QBluetoothUuid(QUuid("e081fec3-f757-4449-b9c9-bfa83133f7fc")); + +static QBluetoothUuid networkServiceUuid = QBluetoothUuid(QUuid("ef6d6610-b8af-49e0-9eca-ab343513641c")); +static QBluetoothUuid networkStatusCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6611-b8af-49e0-9eca-ab343513641c")); +static QBluetoothUuid networkCommanderCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6612-b8af-49e0-9eca-ab343513641c")); +static QBluetoothUuid networkResponseCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6613-b8af-49e0-9eca-ab343513641c")); +static QBluetoothUuid networkingEnabledCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6614-b8af-49e0-9eca-ab343513641c")); +static QBluetoothUuid wirelessEnabledCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6615-b8af-49e0-9eca-ab343513641c")); + +static QBluetoothUuid systemServiceUuid = QBluetoothUuid(QUuid("e081fed0-f757-4449-b9c9-bfa83133f7fc")); +static QBluetoothUuid systemCommanderCharacteristicUuid = QBluetoothUuid(QUuid("e081fed1-f757-4449-b9c9-bfa83133f7fc")); +static QBluetoothUuid systemResponseCharacteristicUuid = QBluetoothUuid(QUuid("e081fed2-f757-4449-b9c9-bfa83133f7fc")); + +BtWiFiSetup::BtWiFiSetup(QObject *parent) : QObject(parent) +{ + m_accessPoints = new WirelessAccessPoints(this); + qRegisterMetaType("const BluetoothDeviceInfo*"); +} + +void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) +{ + qDebug() << "device" << device; + if (m_btController) { + delete m_btController; + m_status = StatusDisconnected; + emit statusChanged(m_status); + + } + + m_btController = QLowEnergyController::createCentral(device->bluetoothDeviceInfo(), this); + connect(m_btController, &QLowEnergyController::connected, this, [this](){ + qDebug() << "Bluetooth connected"; + m_btController->discoverServices(); + }); + + connect(m_btController, &QLowEnergyController::disconnected, this, [this](){ + qDebug() << "Bluetooth disconnected"; + m_status = StatusDisconnected; + emit statusChanged(m_status); + }); + + typedef void (QLowEnergyController::*errorsSignal)(QLowEnergyController::Error); + connect(m_btController, static_cast(&QLowEnergyController::error), this, [this](QLowEnergyController::Error error){ + qDebug() << "Bluetooth error:" << error; + emit this->error(); + }); + + connect(m_btController, &QLowEnergyController::discoveryFinished, this, [this](){ + qDebug() << "Bluetooth service discovery finished"; + setupServices(); + }); + + m_status = StatusConnectingToBluetooth; + emit statusChanged(m_status); + m_btController->connectToDevice(); +} + +void BtWiFiSetup::connectDeviceToWiFi(const QString &ssid) +{ + if (m_status != StatusConnectedToBluetooth) { + qWarning() << "Cannot connect to wifi in state" << m_status; + } +} + +BtWiFiSetup::Status BtWiFiSetup::status() const +{ + return m_status; +} + +QString BtWiFiSetup::modelNumber() const +{ + return m_modelNumber; +} + +QString BtWiFiSetup::manufacturer() const +{ + return m_manufacturer; +} + +QString BtWiFiSetup::softwareRevision() const +{ + return m_softwareRevision; +} + +QString BtWiFiSetup::firmwareRevision() const +{ + return m_firmwareRevision; +} + +QString BtWiFiSetup::hardwareRevision() const +{ + return m_hardwareRevision; +} + +BtWiFiSetup::NetworkStatus BtWiFiSetup::networkStatus() const +{ + return m_networkStatus; +} + +BtWiFiSetup::WirelessStatus BtWiFiSetup::wirelessStatus() const +{ + return m_wirelessStatus; +} + +bool BtWiFiSetup::networkingEnabled() const +{ + return m_networkingEnabled; +} + +bool BtWiFiSetup::wirelessEnabled() const +{ + return m_wirelessEnabled; +} + +WirelessAccessPoints *BtWiFiSetup::accessPoints() const +{ + return m_accessPoints; +} + +WirelessAccessPoint *BtWiFiSetup::currentConnection() const +{ + return m_currentConnection; +} + +void BtWiFiSetup::setupServices() +{ + m_deviceInformationService = m_btController->createServiceObject(QBluetoothUuid::DeviceInformation, m_btController); + m_networkService = m_btController->createServiceObject(networkServiceUuid, m_btController); + m_wifiService = m_btController->createServiceObject(wifiServiceUuid, m_btController); + m_systemService = m_btController->createServiceObject(systemServiceUuid, m_btController); + + if (!m_wifiService || !m_deviceInformationService || !m_networkService) { + qWarning() << "Required services not found on remote device."; + m_btController->disconnectFromDevice(); + return; + } + + // Device information + connect(m_deviceInformationService, &QLowEnergyService::stateChanged, this, [this](QLowEnergyService::ServiceState state) { + if (state != QLowEnergyService::ServiceDiscovered) + return; + qDebug() << "Device info service discovered"; + m_manufacturer = QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::ManufacturerNameString).value()); + emit manufacturerChanged(); + m_modelNumber = QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::ModelNumberString).value()); + emit modelNumberChanged(); + m_softwareRevision = QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::SoftwareRevisionString).value()); + emit softwareRevisionChanged(); + m_firmwareRevision = QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::FirmwareRevisionString).value()); + emit firmwareRevisionChanged(); + m_hardwareRevision = QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::HardwareRevisionString).value()); + emit hardwareRevisionChanged(); + }); + m_deviceInformationService->discoverDetails(); + + + // network service + connect(m_networkService, &QLowEnergyService::stateChanged, this, [this](QLowEnergyService::ServiceState state){ + if (state != QLowEnergyService::ServiceDiscovered) + return; + qDebug() << "Network service discovered"; + QLowEnergyCharacteristic networkCharacteristic = m_networkService->characteristic(networkStatusCharacteristicUuid); + QLowEnergyCharacteristic networkingEnabledCharacteristic = m_networkService->characteristic(networkingEnabledCharacteristicUuid); + QLowEnergyCharacteristic wirelessEnabledCharacteristic = m_networkService->characteristic(wirelessEnabledCharacteristicUuid); + if (!networkCharacteristic.isValid() || !networkingEnabledCharacteristic.isValid() || !wirelessEnabledCharacteristic.isValid()) { + qWarning() << "Required characteristics not found on remote device (NetworkService)"; + m_btController->disconnectFromDevice(); + return; + } + // Enable notifications + m_networkService->writeDescriptor(networkCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); + m_networkService->writeDescriptor(networkingEnabledCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); + m_networkService->writeDescriptor(wirelessEnabledCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); + + m_networkStatus = static_cast(networkCharacteristic.value().toHex().toUInt(nullptr, 16)); + emit networkStatusChanged(); + m_networkingEnabled = networkingEnabledCharacteristic.value().toHex().toUInt(nullptr, 16); + emit networkingEnabledChanged(); + m_wirelessEnabled = wirelessEnabledCharacteristic.value().toHex().toUInt(nullptr, 16); + emit wirelessEnabledChanged(); + + }); + connect(m_networkService, &QLowEnergyService::characteristicChanged, this, &BtWiFiSetup::characteristicChanged); + m_networkService->discoverDetails(); + + // Wifi service + connect(m_wifiService, &QLowEnergyService::stateChanged, this, [this](QLowEnergyService::ServiceState state){ + if (state != QLowEnergyService::ServiceDiscovered) + return; + + qDebug() << "Wifi service discovered"; + m_status = StatusConnectedToBluetooth; + emit statusChanged(m_status); + + // Enable notifations + m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); + m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiStatusCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); + + QVariantMap request; + request.insert("c", (int)WirelessServiceCommandGetNetworks); + streamData(request); + }); + connect(m_wifiService, &QLowEnergyService::characteristicChanged, this, &BtWiFiSetup::characteristicChanged); + m_wifiService->discoverDetails(); + + + // System service (optional) + if (m_systemService) { + connect(m_systemService, &QLowEnergyService::stateChanged, this, [this](QLowEnergyService::ServiceState state){ + if (state != QLowEnergyService::ServiceDiscovered) + return; + qDebug() << "System service discovered"; + m_systemService->writeDescriptor(m_systemService->characteristic(systemResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); + + }); + m_systemService->discoverDetails(); + } +} + +void BtWiFiSetup::streamData(const QVariantMap &request) +{ + QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); + if (!characteristic.isValid()) { + qWarning() << "WifiSetupManager: WirelessService: Wireless commander characteristic not valid"; + return; + } + + QByteArray data = QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact) + '\n'; + qDebug() << "WifiSetupManager: WirelessService: Start streaming response data:" << data.count() << "bytes"; + + int sentDataLength = 0; + QByteArray remainingData = data; + while (!remainingData.isEmpty()) { + QByteArray package = remainingData.left(20); + sentDataLength += package.count(); + m_wifiService->writeCharacteristic(characteristic, package); + remainingData = remainingData.remove(0, package.count()); + } + + qDebug() << "WifiSetupManager: WirelessService: Finished streaming request data"; +} + +void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) +{ + WirelessServiceCommand command = static_cast(data.value("c").toInt()); + WirelessServiceResponse responseCode = (WirelessServiceResponse)data.value("r").toInt(); + if (responseCode != WirelessServiceResponseSuccess) { + qWarning() << "Error in wifi command:" << responseCode; + return; + } + + qDebug() << "command reply:" << command; + switch (command) { + case WirelessServiceCommandGetNetworks: + + m_accessPoints->clearModel(); + + foreach (const QVariant &data, data.value("p").toList()) { + WirelessAccessPoint *accessPoint = new WirelessAccessPoint(this); + + accessPoint->setSsid(data.toMap().value("e").toString()); + accessPoint->setMacAddress(data.toMap().value("m").toString()); + accessPoint->setSignalStrength(data.toMap().value("s").toInt()); + accessPoint->setProtected(data.toMap().value("p").toBool()); + accessPoint->setHostAddress(""); + m_accessPoints->addWirelessAccessPoint(accessPoint); + } + } +} + +void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &data) +{ + m_inputBuffers[characteristic.uuid()].append(data); + if (m_inputBuffers[characteristic.uuid()].endsWith("\n")) { + QByteArray data = m_inputBuffers.take(characteristic.uuid()); + + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + if (error.error != QJsonParseError::NoError) { + qWarning() << "Invalid json data received:" << error.errorString() << data; + m_btController->disconnectFromDevice(); + return; + } + + if (characteristic.uuid() == wifiResponseCharacteristicUuid) { + processWiFiPacket(jsonDoc.toVariant().toMap()); + } else { + qWarning() << "Unhandled packet from characteristic" << characteristic.uuid(); + } + + } +} diff --git a/libnymea-app/wifisetup/btwifisetup.h b/libnymea-app/wifisetup/btwifisetup.h new file mode 100644 index 00000000..c4b65989 --- /dev/null +++ b/libnymea-app/wifisetup/btwifisetup.h @@ -0,0 +1,199 @@ +#ifndef BTWIFISETUP_H +#define BTWIFISETUP_H + +#include +#include +#include + +class BluetoothDeviceInfo; +class WirelessAccessPoints; +class WirelessAccessPoint; + +class BtWiFiSetup : public QObject +{ + Q_OBJECT + Q_PROPERTY(Status status READ status NOTIFY statusChanged) + + Q_PROPERTY(QString modelNumber READ modelNumber NOTIFY modelNumberChanged) + Q_PROPERTY(QString manufacturer READ manufacturer NOTIFY manufacturerChanged) + Q_PROPERTY(QString softwareRevision READ softwareRevision NOTIFY softwareRevisionChanged) + Q_PROPERTY(QString firmwareRevision READ firmwareRevision NOTIFY firmwareRevisionChanged) + Q_PROPERTY(QString hardwareRevision READ hardwareRevision NOTIFY hardwareRevisionChanged) + + Q_PROPERTY(NetworkStatus networkStatus READ networkStatus NOTIFY networkStatusChanged) + Q_PROPERTY(WirelessStatus wirelessStatus READ wirelessStatus NOTIFY wirelessStatusChanged) + Q_PROPERTY(bool networkingEnabled READ networkingEnabled NOTIFY networkingEnabledChanged) + Q_PROPERTY(bool wirelessEnabled READ wirelessEnabled NOTIFY wirelessEnabledChanged) + + Q_PROPERTY(WirelessAccessPoints *accessPoints READ accessPoints CONSTANT) + Q_PROPERTY(WirelessAccessPoint *currentConnection READ currentConnection NOTIFY currentConnectionChanged) + +public: + enum Status { + StatusDisconnected, + StatusConnectingToBluetooth, + StatusConnectedToBluetooth, + StatusConnectingToWiFi, + StatusConnectedToWiFi + }; + Q_ENUM(Status) + + enum WirelessServiceCommand { + WirelessServiceCommandInvalid = -1, + WirelessServiceCommandGetNetworks = 0x00, + WirelessServiceCommandConnect = 0x01, + WirelessServiceCommandConnectHidden = 0x02, + WirelessServiceCommandDisconnect = 0x03, + WirelessServiceCommandScan = 0x04, + WirelessServiceCommandGetCurrentConnection = 0x05 + }; + Q_ENUM(WirelessServiceCommand) + + enum WirelessServiceResponse { + WirelessServiceResponseSuccess = 0x00, + WirelessServiceResponseIvalidCommand = 0x01, + WirelessServiceResponseIvalidParameters = 0x02, + WirelessServiceResponseNetworkManagerNotAvailable = 0x03, + WirelessServiceResponseWirelessNotAvailable = 0x04, + WirelessServiceResponseWirelessNotEnabled = 0x05, + WirelessServiceResponseNetworkingNotEnabled = 0x06, + WirelessServiceResponseUnknownError = 0x07 + }; + Q_ENUM(WirelessServiceResponse) + + enum NetworkServiceCommand { + NetworkServiceCommandInvalid = -1, + NetworkServiceCommandEnableNetworking = 0x00, + NetworkServiceCommandDisableNetworking = 0x01, + NetworkServiceCommandEnableWireless = 0x02, + NetworkServiceCommandDisableWireless = 0x03 + }; + Q_ENUM(NetworkServiceCommand) + + enum NetworkServiceResponse { + NetworkServiceResponseSuccess = 0x00, + NetworkServiceResponseIvalidValue = 0x01, + NetworkServiceResponseNetworkManagerNotAvailable = 0x02, + NetworkServiceResponseWirelessNotAvailable = 0x03, + NetworkServiceResponseUnknownError = 0x04, + }; + Q_ENUM(NetworkServiceResponse) + + enum SystemServiceCommand { + SystemServiceCommandInvalid = -1, + SystemServiceCommandPushAuthentication = 0x00 + }; + Q_ENUM(SystemServiceCommand) + + enum SystemServiceResponse { + SystemServiceResponseSuccess = 0x00, + SystemServiceResponseUnknownError = 0x01, + SystemServiceResponseInvalidCommand = 0x02, + SystemServiceResponseInvalidValue = 0x03, + SystemServiceResponsePushServiceUnavailable = 0x04, + }; + Q_ENUM(SystemServiceResponse) + + enum NetworkStatus { + NetworkStatusUnknown = 0x00, + NetworkStatusAsleep = 0x01, + NetworkStatusDisconnected = 0x02, + NetworkStatusDisconnecting = 0x03, + NetworkStatusConnecting = 0x04, + NetworkStatusLocal = 0x05, + NetworkStatusConnectedSite = 0x06, + NetworkStatusGlobal = 0x07 + }; + Q_ENUM(NetworkStatus) + + enum WirelessStatus { + WirelessStatusUnknown = 0x00, + WirelessStatusUnmanaged = 0x01, + WirelessStatusUnavailable = 0x02, + WirelessStatusDisconnected = 0x03, + WirelessStatusPrepare = 0x04, + WirelessStatusConfig = 0x05, + WirelessStatusNeedAuth = 0x06, + WirelessStatusIpConfig = 0x07, + WirelessStatusIpCheck = 0x08, + WirelessStatusSecondaries = 0x09, + WirelessStatusActivated = 0x0A, + WirelessStatusDeactivating = 0x0B, + WirelessStatusFailed = 0x0C + }; + Q_ENUM(WirelessStatus) + + explicit BtWiFiSetup(QObject *parent = nullptr); + + Q_INVOKABLE void connectToDevice(const BluetoothDeviceInfo *device); + Q_INVOKABLE void connectDeviceToWiFi(const QString &ssid); + + Status status() const; + + QString modelNumber() const; + QString manufacturer() const; + QString softwareRevision() const; + QString firmwareRevision() const; + QString hardwareRevision() const; + + NetworkStatus networkStatus() const; + WirelessStatus wirelessStatus() const; + bool networkingEnabled() const; + bool wirelessEnabled() const; + + WirelessAccessPoints *accessPoints() const; + WirelessAccessPoint *currentConnection() const; + +signals: + void statusChanged(Status status); + void error(); + + void modelNumberChanged(); + void manufacturerChanged(); + void softwareRevisionChanged(); + void firmwareRevisionChanged(); + void hardwareRevisionChanged(); + + void networkStatusChanged(); + void wirelessStatusChanged(); + void networkingEnabledChanged(); + void wirelessEnabledChanged(); + + void currentConnectionChanged(); + +private: + void setupServices(); + void streamData(const QVariantMap &request); + void processWiFiPacket(const QVariantMap &data); + +private slots: + void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &data); + +private: + Status m_status = StatusDisconnected; + QLowEnergyController *m_btController = nullptr; + + QLowEnergyService *m_deviceInformationService = nullptr; + QLowEnergyService *m_networkService = nullptr; + QLowEnergyService *m_wifiService = nullptr; + QLowEnergyService *m_systemService = nullptr; + + QHash m_inputBuffers; + + QString m_modelNumber; + QString m_manufacturer; + QString m_softwareRevision; + QString m_firmwareRevision; + QString m_hardwareRevision; + + NetworkStatus m_networkStatus = NetworkStatusUnknown; + WirelessStatus m_wirelessStatus = WirelessStatusUnknown; + bool m_networkingEnabled = false; + bool m_wirelessEnabled = false; + + WirelessAccessPoints *m_accessPoints = nullptr; + WirelessAccessPoint *m_currentConnection = nullptr; + +}; + +#endif // BTWIFISETUP_H diff --git a/libnymea-app/wifisetup/networkmanagercontroller.cpp b/libnymea-app/wifisetup/networkmanagercontroller.cpp index 56342f23..9e2943dd 100644 --- a/libnymea-app/wifisetup/networkmanagercontroller.cpp +++ b/libnymea-app/wifisetup/networkmanagercontroller.cpp @@ -71,7 +71,7 @@ void NetworkManagerController::connectDevice() return; } - m_wirelessSetupManager = new WirelessSetupManager(m_bluetoothDeviceInfo->getBluetoothDeviceInfo(), this); + m_wirelessSetupManager = new WirelessSetupManager(m_bluetoothDeviceInfo->bluetoothDeviceInfo(), this); emit managerChanged(); m_wirelessSetupManager->connectDevice(); From 8df96a2d5365ba183814b0f6d986695d1fee02fc Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 11 Jan 2021 14:07:36 +0100 Subject: [PATCH 4/9] Some more work --- libnymea-app/libnymea-app-core.h | 2 +- libnymea-app/wifisetup/btwifisetup.cpp | 87 +++++++++++++++++++++----- libnymea-app/wifisetup/btwifisetup.h | 9 ++- 3 files changed, 80 insertions(+), 18 deletions(-) diff --git a/libnymea-app/libnymea-app-core.h b/libnymea-app/libnymea-app-core.h index 4496fe7a..89a9edd9 100644 --- a/libnymea-app/libnymea-app-core.h +++ b/libnymea-app/libnymea-app-core.h @@ -277,7 +277,7 @@ void registerQmlTypes() { qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDeviceInfo", "Can't create this in QML. Get it from the DeviceInfos."); qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDeviceInfos", "Can't create this in QML. Get it from the BluetoothDiscovery."); qmlRegisterUncreatableType(uri, 1, 0, "WirelessSetupManager", "Can't create this in QML. Get it from the NetworkManagerControler."); - qmlRegisterUncreatableType(uri, 1, 0, "WirelessAccessPoints", "Can't create this in QML. Get it from the WirelessAccessPoints."); + qmlRegisterUncreatableType(uri, 1, 0, "WirelessAccessPoint", "Can't create this in QML. Get it from the WirelessAccessPoints."); qmlRegisterUncreatableType(uri, 1, 0, "WirelessAccessPoints", "Can't create this in QML. Get it from the Engine instance."); qmlRegisterType(uri, 1, 0, "WirelessAccessPointsProxy"); diff --git a/libnymea-app/wifisetup/btwifisetup.cpp b/libnymea-app/wifisetup/btwifisetup.cpp index 47a686dc..c04837c8 100644 --- a/libnymea-app/wifisetup/btwifisetup.cpp +++ b/libnymea-app/wifisetup/btwifisetup.cpp @@ -52,7 +52,7 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) typedef void (QLowEnergyController::*errorsSignal)(QLowEnergyController::Error); connect(m_btController, static_cast(&QLowEnergyController::error), this, [this](QLowEnergyController::Error error){ qDebug() << "Bluetooth error:" << error; - emit this->error(); + emit this->bluetoothConnectionError(); }); connect(m_btController, &QLowEnergyController::discoveryFinished, this, [this](){ @@ -65,11 +65,28 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) m_btController->connectToDevice(); } -void BtWiFiSetup::connectDeviceToWiFi(const QString &ssid) +void BtWiFiSetup::disconnectFromDevice() +{ + if (m_btController) { + m_btController->disconnectFromDevice(); + m_btController->deleteLater(); + m_btController = nullptr; + } +} + +void BtWiFiSetup::connectDeviceToWiFi(const QString &ssid, const QString &password) { if (m_status != StatusConnectedToBluetooth) { qWarning() << "Cannot connect to wifi in state" << m_status; } + + QVariantMap request; + request.insert("c", (int)WirelessServiceCommandConnect); + QVariantMap parameters; + parameters.insert("e", ssid); + parameters.insert("p", password); + request.insert("p", parameters); + streamData(request); } BtWiFiSetup::Status BtWiFiSetup::status() const @@ -206,9 +223,7 @@ void BtWiFiSetup::setupServices() m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiStatusCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - QVariantMap request; - request.insert("c", (int)WirelessServiceCommandGetNetworks); - streamData(request); + loadNetworks(); }); connect(m_wifiService, &QLowEnergyService::characteristicChanged, this, &BtWiFiSetup::characteristicChanged); m_wifiService->discoverDetails(); @@ -255,7 +270,7 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) WirelessServiceCommand command = static_cast(data.value("c").toInt()); WirelessServiceResponse responseCode = (WirelessServiceResponse)data.value("r").toInt(); if (responseCode != WirelessServiceResponseSuccess) { - qWarning() << "Error in wifi command:" << responseCode; + qWarning() << "Error in wifi command" << command << ":" << responseCode; return; } @@ -275,28 +290,70 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) accessPoint->setHostAddress(""); m_accessPoints->addWirelessAccessPoint(accessPoint); } + break; + case WirelessServiceCommandConnect: + qDebug() << "Connect call succeeded"; + m_status = StatusConnectingToWiFi; + emit statusChanged(m_status); + break; } } +void BtWiFiSetup::loadNetworks() +{ + QVariantMap request; + request.insert("c", (int)WirelessServiceCommandGetNetworks); + streamData(request); +} + +void BtWiFiSetup::loadCurrentConnection() +{ + QVariantMap request; + request.insert("c", (int)WirelessServiceCommandGetCurrentConnection); + streamData(request); +} + void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &data) -{ - m_inputBuffers[characteristic.uuid()].append(data); - if (m_inputBuffers[characteristic.uuid()].endsWith("\n")) { +{ + if (characteristic.uuid() == wifiResponseCharacteristicUuid) { + m_inputBuffers[characteristic.uuid()].append(data); + if (!m_inputBuffers[characteristic.uuid()].endsWith("\n")) { + return; + } QByteArray data = m_inputBuffers.take(characteristic.uuid()); - QJsonParseError error; QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); if (error.error != QJsonParseError::NoError) { - qWarning() << "Invalid json data received:" << error.errorString() << data; + qWarning() << "Invalid json data received:" << error.errorString() << data << "from characteristic:" << characteristic.uuid(); m_btController->disconnectFromDevice(); return; } + processWiFiPacket(jsonDoc.toVariant().toMap()); - if (characteristic.uuid() == wifiResponseCharacteristicUuid) { - processWiFiPacket(jsonDoc.toVariant().toMap()); - } else { - qWarning() << "Unhandled packet from characteristic" << characteristic.uuid(); + } else if (characteristic.uuid() == wifiStatusCharacteristicUuid) { + + m_wirelessStatus = static_cast(data.toHex().toInt(nullptr, 16)); + qDebug() << "Wireless status changed" << m_wirelessStatus; + emit wirelessStatusChanged(); + + if (m_wirelessStatus == WirelessStatusFailed) { + emit wifiSetupError(); + } else if (m_wirelessStatus == WirelessStatusActivated) { + loadCurrentConnection(); } + } else if (characteristic.uuid() == networkStatusCharacteristicUuid) { + m_networkStatus = static_cast(data.toHex().toInt(nullptr, 16)); + qDebug() << "Network status changed:" << m_networkStatus; + if (m_networkStatus == NetworkStatusGlobal) { + m_status = StatusConnectedToWiFi; + emit statusChanged(m_status); + + loadCurrentConnection(); + } + + } else { + qWarning() << "Unhandled packet from characteristic" << characteristic.uuid(); } + } diff --git a/libnymea-app/wifisetup/btwifisetup.h b/libnymea-app/wifisetup/btwifisetup.h index c4b65989..c7fa8607 100644 --- a/libnymea-app/wifisetup/btwifisetup.h +++ b/libnymea-app/wifisetup/btwifisetup.h @@ -126,7 +126,8 @@ public: explicit BtWiFiSetup(QObject *parent = nullptr); Q_INVOKABLE void connectToDevice(const BluetoothDeviceInfo *device); - Q_INVOKABLE void connectDeviceToWiFi(const QString &ssid); + Q_INVOKABLE void disconnectFromDevice(); + Q_INVOKABLE void connectDeviceToWiFi(const QString &ssid, const QString &password); Status status() const; @@ -146,7 +147,8 @@ public: signals: void statusChanged(Status status); - void error(); + void bluetoothConnectionError(); + void wifiSetupError(); void modelNumberChanged(); void manufacturerChanged(); @@ -166,6 +168,9 @@ private: void streamData(const QVariantMap &request); void processWiFiPacket(const QVariantMap &data); + void loadNetworks(); + void loadCurrentConnection(); + private slots: void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &data); From c293474fd45755d995c9131f931b8b97c6cc3c56 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 12 Jan 2021 00:04:19 +0100 Subject: [PATCH 5/9] Fix the UI --- libnymea-app/libnymea-app-core.h | 3 - libnymea-app/libnymea-app.pri | 4 - libnymea-app/wifisetup/btwifisetup.cpp | 138 ++- libnymea-app/wifisetup/btwifisetup.h | 14 +- .../wifisetup/networkmanagercontroller.cpp | 78 -- .../wifisetup/networkmanagercontroller.h | 68 -- .../wifisetup/wirelesssetupmanager.cpp | 1028 ----------------- libnymea-app/wifisetup/wirelesssetupmanager.h | 275 ----- nymea-app/ui/Nymea.qml | 4 +- nymea-app/ui/RootItem.qml | 12 +- nymea-app/ui/appsettings/CloudLoginPage.qml | 2 +- nymea-app/ui/connection/ConnectPage.qml | 12 +- nymea-app/ui/connection/ManualConnectPage.qml | 2 +- .../wifisetup/BluetoothDiscoveryPage.qml | 65 +- .../ui/connection/wifisetup/BoxInfoPage.qml | 12 +- .../connection/wifisetup/ConnectWiFiPage.qml | 32 +- .../wifisetup/NetworkSettingsPage.qml | 14 +- .../wifisetup/WirelessSetupPage.qml | 41 +- 18 files changed, 232 insertions(+), 1572 deletions(-) delete mode 100644 libnymea-app/wifisetup/networkmanagercontroller.cpp delete mode 100644 libnymea-app/wifisetup/networkmanagercontroller.h delete mode 100644 libnymea-app/wifisetup/wirelesssetupmanager.cpp delete mode 100644 libnymea-app/wifisetup/wirelesssetupmanager.h diff --git a/libnymea-app/libnymea-app-core.h b/libnymea-app/libnymea-app-core.h index 89a9edd9..2d539388 100644 --- a/libnymea-app/libnymea-app-core.h +++ b/libnymea-app/libnymea-app-core.h @@ -74,7 +74,6 @@ #include "configuration/mqttpolicy.h" #include "configuration/mqttpolicies.h" #include "wifisetup/btwifisetup.h" -#include "wifisetup/networkmanagercontroller.h" #include "types/wirelessaccesspoint.h" #include "types/wirelessaccesspoints.h" #include "models/wirelessaccesspointsproxy.h" @@ -272,11 +271,9 @@ void registerQmlTypes() { qmlRegisterType(uri, 1, 0, "TagListProxyModel"); qmlRegisterType(uri, 1, 0, "BtWiFiSetup"); - qmlRegisterType(uri, 1, 0, "NetworkManagerController"); qmlRegisterType(uri, 1, 0, "BluetoothDiscovery"); qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDeviceInfo", "Can't create this in QML. Get it from the DeviceInfos."); qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDeviceInfos", "Can't create this in QML. Get it from the BluetoothDiscovery."); - qmlRegisterUncreatableType(uri, 1, 0, "WirelessSetupManager", "Can't create this in QML. Get it from the NetworkManagerControler."); qmlRegisterUncreatableType(uri, 1, 0, "WirelessAccessPoint", "Can't create this in QML. Get it from the WirelessAccessPoints."); qmlRegisterUncreatableType(uri, 1, 0, "WirelessAccessPoints", "Can't create this in QML. Get it from the Engine instance."); qmlRegisterType(uri, 1, 0, "WirelessAccessPointsProxy"); diff --git a/libnymea-app/libnymea-app.pri b/libnymea-app/libnymea-app.pri index d3a0f5cf..7f82e210 100644 --- a/libnymea-app/libnymea-app.pri +++ b/libnymea-app/libnymea-app.pri @@ -134,8 +134,6 @@ SOURCES += \ $${PWD}/wifisetup/bluetoothdeviceinfo.cpp \ $${PWD}/wifisetup/bluetoothdeviceinfos.cpp \ $${PWD}/wifisetup/bluetoothdiscovery.cpp \ - $${PWD}/wifisetup/wirelesssetupmanager.cpp \ - $${PWD}/wifisetup/networkmanagercontroller.cpp \ $${PWD}/models/logsmodelng.cpp \ $${PWD}/models/interfacesproxy.cpp \ $${PWD}/models/tagsproxymodel.cpp \ @@ -281,8 +279,6 @@ HEADERS += \ $${PWD}/wifisetup/bluetoothdeviceinfo.h \ $${PWD}/wifisetup/bluetoothdeviceinfos.h \ $${PWD}/wifisetup/bluetoothdiscovery.h \ - $${PWD}/wifisetup/wirelesssetupmanager.h \ - $${PWD}/wifisetup/networkmanagercontroller.h \ $${PWD}/libnymea-app-core.h \ $${PWD}/models/logsmodelng.h \ $${PWD}/models/interfacesproxy.h \ diff --git a/libnymea-app/wifisetup/btwifisetup.cpp b/libnymea-app/wifisetup/btwifisetup.cpp index c04837c8..849b8a65 100644 --- a/libnymea-app/wifisetup/btwifisetup.cpp +++ b/libnymea-app/wifisetup/btwifisetup.cpp @@ -9,6 +9,7 @@ static QBluetoothUuid wifiServiceUuid = QBluetoothUuid(QUuid("e0 static QBluetoothUuid wifiCommanderCharacteristicUuid = QBluetoothUuid(QUuid("e081fec1-f757-4449-b9c9-bfa83133f7fc")); static QBluetoothUuid wifiResponseCharacteristicUuid = QBluetoothUuid(QUuid("e081fec2-f757-4449-b9c9-bfa83133f7fc")); static QBluetoothUuid wifiStatusCharacteristicUuid = QBluetoothUuid(QUuid("e081fec3-f757-4449-b9c9-bfa83133f7fc")); +static QBluetoothUuid wifiModeCharacteristicUuid = QBluetoothUuid(QUuid("e081fec4-f757-4449-b9c9-bfa83133f7fc")); static QBluetoothUuid networkServiceUuid = QBluetoothUuid(QUuid("ef6d6610-b8af-49e0-9eca-ab343513641c")); static QBluetoothUuid networkStatusCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6611-b8af-49e0-9eca-ab343513641c")); @@ -32,6 +33,9 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) qDebug() << "device" << device; if (m_btController) { delete m_btController; + m_currentConnection = nullptr; + currentConnectionChanged(); + m_accessPoints->clearModel(); m_status = StatusDisconnected; emit statusChanged(m_status); @@ -47,6 +51,11 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) qDebug() << "Bluetooth disconnected"; m_status = StatusDisconnected; emit statusChanged(m_status); + m_btController->deleteLater(); + m_btController = nullptr; + m_currentConnection = nullptr; + emit currentConnectionChanged(); + m_accessPoints->clearModel(); }); typedef void (QLowEnergyController::*errorsSignal)(QLowEnergyController::Error); @@ -69,8 +78,6 @@ void BtWiFiSetup::disconnectFromDevice() { if (m_btController) { m_btController->disconnectFromDevice(); - m_btController->deleteLater(); - m_btController = nullptr; } } @@ -86,7 +93,39 @@ void BtWiFiSetup::connectDeviceToWiFi(const QString &ssid, const QString &passwo parameters.insert("e", ssid); parameters.insert("p", password); request.insert("p", parameters); - streamData(request); + streamData(m_wifiService, wifiCommanderCharacteristicUuid, request); +} + +void BtWiFiSetup::disconnectDeviceFromWiFi() +{ + if (m_status != StatusConnectedToWiFi) { + qWarning() << "Cannot disconnect from wifi in state" << m_status; + } + QLowEnergyCharacteristic ssidCharacteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); + QVariantMap request; + request.insert("c", (int)WirelessServiceCommandDisconnect); + streamData(m_wifiService, wifiCommanderCharacteristicUuid, request); +} + +void BtWiFiSetup::scanWiFi() +{ + QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); + QVariantMap request; + request.insert("c", (int)WirelessServiceCommandScan); + streamData(m_wifiService, wifiCommanderCharacteristicUuid, request); +} + +void BtWiFiSetup::pressPushButton() +{ + if (!m_systemService) { + qDebug() << "System service not available. Cannot perform push button pairing"; + return; + } + QLowEnergyCharacteristic commanderCharacteristic = m_systemService->characteristic(systemCommanderCharacteristicUuid); + QVariantMap request; + request.insert("c", (int)SystemServiceCommandPushAuthentication); + + streamData(m_systemService, systemCommanderCharacteristicUuid, request); } BtWiFiSetup::Status BtWiFiSetup::status() const @@ -134,11 +173,23 @@ bool BtWiFiSetup::networkingEnabled() const return m_networkingEnabled; } +void BtWiFiSetup::setNetworkingEnabled(bool networkingEnabled) +{ + QLowEnergyCharacteristic characteristic = m_networkService->characteristic(networkCommanderCharacteristicUuid); + m_networkService->writeCharacteristic(characteristic, networkingEnabled ? QByteArray::fromHex("00") : QByteArray::fromHex("01")); +} + bool BtWiFiSetup::wirelessEnabled() const { return m_wirelessEnabled; } +void BtWiFiSetup::setWirelessEnabled(bool wirelessEnabled) const +{ + QLowEnergyCharacteristic characteristic = m_networkService->characteristic(networkCommanderCharacteristicUuid); + m_networkService->writeCharacteristic(characteristic, wirelessEnabled ? QByteArray::fromHex("02") : QByteArray::fromHex("03")); +} + WirelessAccessPoints *BtWiFiSetup::accessPoints() const { return m_accessPoints; @@ -151,6 +202,7 @@ WirelessAccessPoint *BtWiFiSetup::currentConnection() const void BtWiFiSetup::setupServices() { + qDebug() << "Setting up services"; m_deviceInformationService = m_btController->createServiceObject(QBluetoothUuid::DeviceInformation, m_btController); m_networkService = m_btController->createServiceObject(networkServiceUuid, m_btController); m_wifiService = m_btController->createServiceObject(wifiServiceUuid, m_btController); @@ -223,6 +275,7 @@ void BtWiFiSetup::setupServices() m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiStatusCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); + qDebug() << "Fetching networks after init"; loadNetworks(); }); connect(m_wifiService, &QLowEnergyService::characteristicChanged, this, &BtWiFiSetup::characteristicChanged); @@ -236,22 +289,15 @@ void BtWiFiSetup::setupServices() return; qDebug() << "System service discovered"; m_systemService->writeDescriptor(m_systemService->characteristic(systemResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - }); m_systemService->discoverDetails(); } } -void BtWiFiSetup::streamData(const QVariantMap &request) +void BtWiFiSetup::streamData(QLowEnergyService *service, const QUuid &characteristicUuid, const QVariantMap &request) { - QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); - if (!characteristic.isValid()) { - qWarning() << "WifiSetupManager: WirelessService: Wireless commander characteristic not valid"; - return; - } - + QLowEnergyCharacteristic characteristic = service->characteristic(characteristicUuid); QByteArray data = QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact) + '\n'; - qDebug() << "WifiSetupManager: WirelessService: Start streaming response data:" << data.count() << "bytes"; int sentDataLength = 0; QByteArray remainingData = data; @@ -261,8 +307,6 @@ void BtWiFiSetup::streamData(const QVariantMap &request) m_wifiService->writeCharacteristic(characteristic, package); remainingData = remainingData.remove(0, package.count()); } - - qDebug() << "WifiSetupManager: WirelessService: Finished streaming request data"; } void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) @@ -278,11 +322,19 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) switch (command) { case WirelessServiceCommandGetNetworks: - m_accessPoints->clearModel(); - foreach (const QVariant &data, data.value("p").toList()) { - WirelessAccessPoint *accessPoint = new WirelessAccessPoint(this); + bool found = false; + for (int i = 0; i < m_accessPoints->rowCount(); i++) { + WirelessAccessPoint *existingAp = m_accessPoints->get(i); + if (existingAp->macAddress() == data.toMap().value("m").toString()) { + found = true; + } + } + if (found) { + continue; + } + WirelessAccessPoint *accessPoint = new WirelessAccessPoint(this); accessPoint->setSsid(data.toMap().value("e").toString()); accessPoint->setMacAddress(data.toMap().value("m").toString()); accessPoint->setSignalStrength(data.toMap().value("s").toInt()); @@ -296,6 +348,29 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) m_status = StatusConnectingToWiFi; emit statusChanged(m_status); break; + case WirelessServiceCommandGetCurrentConnection: + // Find current network + m_currentConnection = nullptr; + foreach (WirelessAccessPoint *accessPoint, m_accessPoints->wirelessAccessPoints()) { + QVariantMap currentConnection = data.value("p").toMap(); + QString macAddress = currentConnection.value("m").toString(); + if (accessPoint->macAddress() == macAddress) { + // Set the current network + m_currentConnection = accessPoint; + accessPoint->setHostAddress(currentConnection.value("i").toString()); + } + } + qDebug() << "current connection is:" << m_currentConnection; + emit currentConnectionChanged(); + break; + case WirelessServiceCommandScan: + if (responseCode == WirelessServiceResponseSuccess) { + qDebug() << "Fetching networks after wifi scan"; + loadNetworks(); + } + break; + default: + qWarning() << "Unhandled command reply"; } } @@ -303,28 +378,29 @@ void BtWiFiSetup::loadNetworks() { QVariantMap request; request.insert("c", (int)WirelessServiceCommandGetNetworks); - streamData(request); + streamData(m_wifiService, wifiCommanderCharacteristicUuid, request); } void BtWiFiSetup::loadCurrentConnection() { QVariantMap request; request.insert("c", (int)WirelessServiceCommandGetCurrentConnection); - streamData(request); + streamData(m_wifiService, wifiCommanderCharacteristicUuid, request); } -void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &data) +void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) { if (characteristic.uuid() == wifiResponseCharacteristicUuid) { - m_inputBuffers[characteristic.uuid()].append(data); + + m_inputBuffers[characteristic.uuid()].append(value); if (!m_inputBuffers[characteristic.uuid()].endsWith("\n")) { return; } QByteArray data = m_inputBuffers.take(characteristic.uuid()); QJsonParseError error; - QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error); + QJsonDocument jsonDoc = QJsonDocument::fromJson(data.trimmed(), &error); if (error.error != QJsonParseError::NoError) { - qWarning() << "Invalid json data received:" << error.errorString() << data << "from characteristic:" << characteristic.uuid(); + qWarning() << "Invalid json data received:" << error.errorString() << data.trimmed() << "from characteristic:" << characteristic.uuid(); m_btController->disconnectFromDevice(); return; } @@ -332,7 +408,7 @@ void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characte } else if (characteristic.uuid() == wifiStatusCharacteristicUuid) { - m_wirelessStatus = static_cast(data.toHex().toInt(nullptr, 16)); + m_wirelessStatus = static_cast(value.toHex().toInt(nullptr, 16)); qDebug() << "Wireless status changed" << m_wirelessStatus; emit wirelessStatusChanged(); @@ -342,8 +418,14 @@ void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characte loadCurrentConnection(); } + // Note: wirelessEnabled characterristic seems broken server-side. Let's check the wifiStatus for it being enabled or not + if (m_wirelessEnabled != (m_wirelessStatus != WirelessStatusUnavailable)) { + m_wirelessEnabled = m_wirelessStatus != WirelessStatusUnavailable; + emit wirelessEnabledChanged(); + } + } else if (characteristic.uuid() == networkStatusCharacteristicUuid) { - m_networkStatus = static_cast(data.toHex().toInt(nullptr, 16)); + m_networkStatus = static_cast(value.toHex().toInt(nullptr, 16)); qDebug() << "Network status changed:" << m_networkStatus; if (m_networkStatus == NetworkStatusGlobal) { m_status = StatusConnectedToWiFi; @@ -352,6 +434,12 @@ void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characte loadCurrentConnection(); } + // Note: networkingEnabled characterristic seems broken server-side. Let's check the networkStatus for it being enabled or not + if (m_wirelessEnabled != (m_networkStatus != NetworkStatusAsleep)) { + m_networkingEnabled = m_networkStatus != NetworkStatusAsleep; + emit wirelessEnabledChanged(); + } + } else { qWarning() << "Unhandled packet from characteristic" << characteristic.uuid(); } diff --git a/libnymea-app/wifisetup/btwifisetup.h b/libnymea-app/wifisetup/btwifisetup.h index c7fa8607..49b8f778 100644 --- a/libnymea-app/wifisetup/btwifisetup.h +++ b/libnymea-app/wifisetup/btwifisetup.h @@ -22,8 +22,8 @@ class BtWiFiSetup : public QObject Q_PROPERTY(NetworkStatus networkStatus READ networkStatus NOTIFY networkStatusChanged) Q_PROPERTY(WirelessStatus wirelessStatus READ wirelessStatus NOTIFY wirelessStatusChanged) - Q_PROPERTY(bool networkingEnabled READ networkingEnabled NOTIFY networkingEnabledChanged) - Q_PROPERTY(bool wirelessEnabled READ wirelessEnabled NOTIFY wirelessEnabledChanged) + Q_PROPERTY(bool networkingEnabled READ networkingEnabled WRITE setNetworkingEnabled NOTIFY networkingEnabledChanged) + Q_PROPERTY(bool wirelessEnabled READ wirelessEnabled WRITE setWirelessEnabled NOTIFY wirelessEnabledChanged) Q_PROPERTY(WirelessAccessPoints *accessPoints READ accessPoints CONSTANT) Q_PROPERTY(WirelessAccessPoint *currentConnection READ currentConnection NOTIFY currentConnectionChanged) @@ -128,6 +128,9 @@ public: Q_INVOKABLE void connectToDevice(const BluetoothDeviceInfo *device); Q_INVOKABLE void disconnectFromDevice(); Q_INVOKABLE void connectDeviceToWiFi(const QString &ssid, const QString &password); + Q_INVOKABLE void disconnectDeviceFromWiFi(); + Q_INVOKABLE void scanWiFi(); + Q_INVOKABLE void pressPushButton(); Status status() const; @@ -140,7 +143,10 @@ public: NetworkStatus networkStatus() const; WirelessStatus wirelessStatus() const; bool networkingEnabled() const; + void setNetworkingEnabled(bool networkingEnabled); + bool wirelessEnabled() const; + void setWirelessEnabled(bool wirelessEnabled) const; WirelessAccessPoints *accessPoints() const; WirelessAccessPoint *currentConnection() const; @@ -165,14 +171,14 @@ signals: private: void setupServices(); - void streamData(const QVariantMap &request); + void streamData(QLowEnergyService *service, const QUuid &characteristicUuid, const QVariantMap &request); void processWiFiPacket(const QVariantMap &data); void loadNetworks(); void loadCurrentConnection(); private slots: - void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &data); + void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); private: Status m_status = StatusDisconnected; diff --git a/libnymea-app/wifisetup/networkmanagercontroller.cpp b/libnymea-app/wifisetup/networkmanagercontroller.cpp deleted file mode 100644 index 9e2943dd..00000000 --- a/libnymea-app/wifisetup/networkmanagercontroller.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* -* Copyright 2013 - 2020, nymea GmbH -* Contact: contact@nymea.io -* -* This file is part of nymea. -* This project including source code and documentation is protected by -* copyright law, and remains the property of nymea GmbH. All rights, including -* reproduction, publication, editing and translation, are reserved. The use of -* this project is subject to the terms of a license agreement to be concluded -* with nymea GmbH in accordance with the terms of use of nymea GmbH, available -* under https://nymea.io/license -* -* GNU General Public License Usage -* Alternatively, this project may be redistributed and/or modified under the -* terms of the GNU General Public License as published by the Free Software -* Foundation, GNU version 3. This project is distributed in the hope that it -* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty -* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General -* Public License for more details. -* -* You should have received a copy of the GNU General Public License along with -* this project. If not, see . -* -* For any further details and any questions please contact us under -* contact@nymea.io or see our FAQ/Licensing Information on -* https://nymea.io/license/faq -* -* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "networkmanagercontroller.h" - -NetworkManagerController::NetworkManagerController(QObject *parent) : QObject(parent) -{ - -} - -BluetoothDeviceInfo *NetworkManagerController::bluetoothDeviceInfo() const -{ - return m_bluetoothDeviceInfo; -} - -void NetworkManagerController::setBluetoothDeviceInfo(BluetoothDeviceInfo *bluetoothDeviceInfo) -{ - if (m_bluetoothDeviceInfo != bluetoothDeviceInfo) { - m_bluetoothDeviceInfo = bluetoothDeviceInfo; - emit bluetoothDeviceInfoChanged(); - } -} - -WirelessSetupManager *NetworkManagerController::manager() -{ - return m_wirelessSetupManager; -} - -void NetworkManagerController::connectDevice() -{ - if (!m_bluetoothDeviceInfo) { - qWarning() << "Can't connect to device. bluetoothDeviceInfo not set."; - return; - } - - if (m_wirelessSetupManager) { - delete m_wirelessSetupManager; - m_wirelessSetupManager = nullptr; - emit managerChanged(); - } - - if (!m_bluetoothDeviceInfo) { - qDebug() << "Could not connect to device. There is no device info for" << m_bluetoothDeviceInfo->name() << m_bluetoothDeviceInfo->address(); - return; - } - - m_wirelessSetupManager = new WirelessSetupManager(m_bluetoothDeviceInfo->bluetoothDeviceInfo(), this); - emit managerChanged(); - - m_wirelessSetupManager->connectDevice(); -} diff --git a/libnymea-app/wifisetup/networkmanagercontroller.h b/libnymea-app/wifisetup/networkmanagercontroller.h deleted file mode 100644 index 3889a2eb..00000000 --- a/libnymea-app/wifisetup/networkmanagercontroller.h +++ /dev/null @@ -1,68 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* -* Copyright 2013 - 2020, nymea GmbH -* Contact: contact@nymea.io -* -* This file is part of nymea. -* This project including source code and documentation is protected by -* copyright law, and remains the property of nymea GmbH. All rights, including -* reproduction, publication, editing and translation, are reserved. The use of -* this project is subject to the terms of a license agreement to be concluded -* with nymea GmbH in accordance with the terms of use of nymea GmbH, available -* under https://nymea.io/license -* -* GNU General Public License Usage -* Alternatively, this project may be redistributed and/or modified under the -* terms of the GNU General Public License as published by the Free Software -* Foundation, GNU version 3. This project is distributed in the hope that it -* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty -* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General -* Public License for more details. -* -* You should have received a copy of the GNU General Public License along with -* this project. If not, see . -* -* For any further details and any questions please contact us under -* contact@nymea.io or see our FAQ/Licensing Information on -* https://nymea.io/license/faq -* -* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef NETWORKMANAGERCONTROLLER_H -#define NETWORKMANAGERCONTROLLER_H - -#include -#include - -#include "bluetoothdeviceinfo.h" -#include "wirelesssetupmanager.h" - -class NetworkManagerController : public QObject -{ - Q_OBJECT - Q_PROPERTY(BluetoothDeviceInfo* bluetoothDeviceInfo READ bluetoothDeviceInfo WRITE setBluetoothDeviceInfo) - Q_PROPERTY(WirelessSetupManager *manager READ manager NOTIFY managerChanged) - -public: - explicit NetworkManagerController(QObject *parent = nullptr); - - BluetoothDeviceInfo* bluetoothDeviceInfo() const; - void setBluetoothDeviceInfo(BluetoothDeviceInfo* bluetoothDeviceInfo); - - WirelessSetupManager *manager(); - - Q_INVOKABLE void connectDevice(); - -private: - BluetoothDeviceInfo* m_bluetoothDeviceInfo = nullptr; - - WirelessSetupManager *m_wirelessSetupManager = nullptr; - -signals: - void managerChanged(); - void nameChanged(); - void bluetoothDeviceInfoChanged(); - -}; - -#endif // NETWORKMANAGERCONTROLLER_H diff --git a/libnymea-app/wifisetup/wirelesssetupmanager.cpp b/libnymea-app/wifisetup/wirelesssetupmanager.cpp deleted file mode 100644 index 514b9d4f..00000000 --- a/libnymea-app/wifisetup/wirelesssetupmanager.cpp +++ /dev/null @@ -1,1028 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* -* Copyright 2013 - 2020, nymea GmbH -* Contact: contact@nymea.io -* -* This file is part of nymea. -* This project including source code and documentation is protected by -* copyright law, and remains the property of nymea GmbH. All rights, including -* reproduction, publication, editing and translation, are reserved. The use of -* this project is subject to the terms of a license agreement to be concluded -* with nymea GmbH in accordance with the terms of use of nymea GmbH, available -* under https://nymea.io/license -* -* GNU General Public License Usage -* Alternatively, this project may be redistributed and/or modified under the -* terms of the GNU General Public License as published by the Free Software -* Foundation, GNU version 3. This project is distributed in the hope that it -* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty -* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General -* Public License for more details. -* -* You should have received a copy of the GNU General Public License along with -* this project. If not, see . -* -* For any further details and any questions please contact us under -* contact@nymea.io or see our FAQ/Licensing Information on -* https://nymea.io/license/faq -* -* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "wirelesssetupmanager.h" -#include "types/wirelessaccesspoint.h" -#include "types/wirelessaccesspoints.h" -#include "models/wirelessaccesspointsproxy.h" - -#include - -static QBluetoothUuid wifiServiceUuid = QBluetoothUuid(QUuid("e081fec0-f757-4449-b9c9-bfa83133f7fc")); -static QBluetoothUuid wifiCommanderCharacteristicUuid = QBluetoothUuid(QUuid("e081fec1-f757-4449-b9c9-bfa83133f7fc")); -static QBluetoothUuid wifiResponseCharacteristicUuid = QBluetoothUuid(QUuid("e081fec2-f757-4449-b9c9-bfa83133f7fc")); -static QBluetoothUuid wifiStatusCharacteristicUuid = QBluetoothUuid(QUuid("e081fec3-f757-4449-b9c9-bfa83133f7fc")); - -static QBluetoothUuid networkServiceUuid = QBluetoothUuid(QUuid("ef6d6610-b8af-49e0-9eca-ab343513641c")); -static QBluetoothUuid networkStatusCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6611-b8af-49e0-9eca-ab343513641c")); -static QBluetoothUuid networkCommanderCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6612-b8af-49e0-9eca-ab343513641c")); -static QBluetoothUuid networkResponseCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6613-b8af-49e0-9eca-ab343513641c")); -static QBluetoothUuid networkingEnabledCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6614-b8af-49e0-9eca-ab343513641c")); -static QBluetoothUuid wirelessEnabledCharacteristicUuid = QBluetoothUuid(QUuid("ef6d6615-b8af-49e0-9eca-ab343513641c")); - -static QBluetoothUuid systemServiceUuid = QBluetoothUuid(QUuid("e081fed0-f757-4449-b9c9-bfa83133f7fc")); -static QBluetoothUuid systemCommanderCharacteristicUuid = QBluetoothUuid(QUuid("e081fed1-f757-4449-b9c9-bfa83133f7fc")); -static QBluetoothUuid systemResponseCharacteristicUuid = QBluetoothUuid(QUuid("e081fed2-f757-4449-b9c9-bfa83133f7fc")); - - -WirelessSetupManager::WirelessSetupManager(const QBluetoothDeviceInfo &deviceInfo, QObject *parent) : - BluetoothDevice(deviceInfo, parent), - m_accessPoints(new WirelessAccessPoints(this)) -{ - connect(this, &WirelessSetupManager::connectedChanged, this, &WirelessSetupManager::onConnectedChanged); - connect(this, &WirelessSetupManager::serviceDiscoveryFinished, this, &WirelessSetupManager::onServiceDiscoveryFinished); -} - -QString WirelessSetupManager::modelNumber() const -{ - return m_modelNumber; -} - -QString WirelessSetupManager::manufacturer() const -{ - return m_manufacturer; -} - -QString WirelessSetupManager::softwareRevision() const -{ - return m_softwareRevision; -} - -QString WirelessSetupManager::firmwareRevision() const -{ - return m_firmwareRevision; -} - -QString WirelessSetupManager::hardwareRevision() const -{ - return m_hardwareRevision; -} - -bool WirelessSetupManager::initialized() const -{ - return m_initialized; -} - -bool WirelessSetupManager::working() const -{ - return m_working; -} - -WirelessSetupManager::NetworkStatus WirelessSetupManager::networkStatus() const -{ - return m_networkStatus; -} - -WirelessSetupManager::WirelessStatus WirelessSetupManager::wirelessStatus() const -{ - return m_wirelessStatus; -} - -bool WirelessSetupManager::networkingEnabled() const -{ - return m_networkingEnabled; -} - -bool WirelessSetupManager::wirelessEnabled() const -{ - return m_wirelessEnabled; -} - -WirelessAccessPoints *WirelessSetupManager::accessPoints() const -{ - return m_accessPoints; -} - -WirelessAccessPoint *WirelessSetupManager::currentConnection() const -{ - return m_currentConnection; -} - -void WirelessSetupManager::reloadData() -{ - loadNetworks(); -} - -void WirelessSetupManager::loadNetworks() -{ - qDebug() << "WifiSetupManager: Start loading wifi networks"; - - if (!m_wifiService) { - qWarning() << "WifiSetupManager: Could not send command. Service not valid"; - return; - } - - QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); - if (!characteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not send command. Characteristic is not valid"; - return; - } - - m_readingResponse = true; - m_inputDataStream.clear(); - - setStatusText("Loading wifi network list..."); - m_working = true; - emit workingChanged(); - - QVariantMap request; - request.insert("c", (int)WirelessServiceCommandGetNetworks); - streamData(request); -} - -void WirelessSetupManager::loadCurrentConnection() -{ - qDebug() << "WifiSetupManager: Start loading current connection data"; - - if (!m_wifiService) { - qWarning() << "WifiSetupManager: Could not send command. Service not valid"; - return; - } - - QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); - if (!characteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not send command. Characteristic is not valid"; - return; - } - - m_readingResponse = false; - m_inputDataStream.clear(); - - setStatusText("Loading current connection data"); - m_working = true; - emit workingChanged(); - - QVariantMap request; - request.insert("c", (int)WirelessServiceCommandGetCurrentConnection); - streamData(request); -} - -void WirelessSetupManager::performWifiScan() -{ - qDebug() << "WifiSetupManager: Start loading wifi networks"; - - if (!m_wifiService) { - qWarning() << "WifiSetupManager: Could not send command. Service not valid"; - return; - } - - QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); - if (!characteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not send command. Characteristic is not valid"; - return; - } - - setStatusText("Perform refresh..."); - m_working = true; - emit workingChanged(); - - QVariantMap request; - request.insert("c", (int)WirelessServiceCommandScan); - streamData(request); -} - -void WirelessSetupManager::enableNetworking(bool enable) -{ - qDebug() << "WifiSetupManager: Send networking" << enable; - - if (!m_netwokService) { - qWarning() << "WifiSetupManager: Could not set networking. Service not valid"; - return; - } - - QLowEnergyCharacteristic characteristic = m_netwokService->characteristic(networkCommanderCharacteristicUuid); - if (!characteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not set networking. Characteristic is not valid"; - return; - } - - m_netwokService->writeCharacteristic(characteristic, enable ? QByteArray::fromHex("00") : QByteArray::fromHex("01")); -} - -void WirelessSetupManager::enableWireless(bool enable) -{ - qDebug() << "WifiSetupManager: Send wireless networking" << enable; - - if (!m_netwokService) { - qWarning() << "WifiSetupManager: Could not enable/disable wireless. Service not valid"; - return; - } - - QLowEnergyCharacteristic characteristic = m_netwokService->characteristic(networkCommanderCharacteristicUuid); - if (!characteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not enable/disable wireless. Characteristic is not valid"; - return; - } - - m_netwokService->writeCharacteristic(characteristic, enable ? QByteArray::fromHex("02") : QByteArray::fromHex("03")); -} - -void WirelessSetupManager::connectWirelessNetwork(const QString &ssid, const QString &password) -{ - qDebug() << "WifiSetupManager: Connect wireless network" << ssid << password; - - m_ssid = ssid; - m_password = password; - - if (!m_wifiService) { - qWarning() << "WifiSetupManager: Could not set wireless network. Service not valid"; - return; - } - - QLowEnergyCharacteristic ssidCharacteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); - if (!ssidCharacteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not connect. Characteristic is not valid"; - return; - } - - QVariantMap request; - request.insert("c", (int)WirelessServiceCommandConnect); - QVariantMap parameters; - parameters.insert("e", ssid); - parameters.insert("p", password); - request.insert("p", parameters); - - streamData(request); -} - -void WirelessSetupManager::disconnectWirelessNetwork() -{ - qDebug() << "WifiSetupManager: Disconnect wireless network"; - - if (!m_wifiService) { - qWarning() << "WifiSetupManager: Could not disconnect wireless network. Service not valid"; - return; - } - - QLowEnergyCharacteristic ssidCharacteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); - if (!ssidCharacteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not disconnect wireless ssid. Characteristic is not valid"; - return; - } - - QVariantMap request; - request.insert("c", (int)WirelessServiceCommandDisconnect); - streamData(request); -} - -void WirelessSetupManager::pressPushButton() -{ - qDebug() << "WifiSetupManager: Press push button"; - - if (!m_systemService) { - qWarning() << "WifiSetupManager: Could not press push button. Service not valid"; - return; - } - - QLowEnergyCharacteristic commanderCharacteristic = m_systemService->characteristic(systemCommanderCharacteristicUuid); - if (!commanderCharacteristic.isValid()) { - qWarning() << "WifiSetupManager: Could not press push button. Characteristic is not valid"; - return; - } - - QVariantMap request; - request.insert("c", (int)SystemServiceCommandPushAuthentication); - - QByteArray data = QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact) + '\n'; - qDebug() << "WifiSetupManager: SystemService: Start streaming response data:" << data.count() << "bytes"; - - int sentDataLength = 0; - QByteArray remainingData = data; - while (!remainingData.isEmpty()) { - QByteArray package = remainingData.left(20); - sentDataLength += package.count(); - m_systemService->writeCharacteristic(commanderCharacteristic, package); - remainingData = remainingData.remove(0, package.count()); - } - - qDebug() << "WifiSetupManager: SystemService: Finished streaming request data"; -} - -void WirelessSetupManager::checkInitialized() -{ - bool initialized = false; - - if (m_systemService) { - initialized = m_deviceInformationService->state() == QLowEnergyService::ServiceDiscovered - && m_netwokService->state() == QLowEnergyService::ServiceDiscovered - && m_wifiService->state() == QLowEnergyService::ServiceDiscovered - && m_systemService->state() == QLowEnergyService::ServiceDiscovered; - } else { - initialized = m_deviceInformationService->state() == QLowEnergyService::ServiceDiscovered - && m_netwokService->state() == QLowEnergyService::ServiceDiscovered - && m_wifiService->state() == QLowEnergyService::ServiceDiscovered; - } - - if (initialized && m_wirelessEnabled && m_networkingEnabled) { - loadNetworks(); - } -} - -void WirelessSetupManager::setModelNumber(const QString &modelNumber) -{ - m_modelNumber = modelNumber; - emit modelNumberChanged(); -} - -void WirelessSetupManager::setManufacturer(const QString &manufacturer) -{ - m_manufacturer = manufacturer; - emit manufacturerChanged(); -} - -void WirelessSetupManager::setSoftwareRevision(const QString &softwareRevision) -{ - m_softwareRevision = softwareRevision; - emit softwareRevisionChanged(); -} - -void WirelessSetupManager::setFirmwareRevision(const QString &firmwareRevision) -{ - m_firmwareRevision = firmwareRevision; - emit firmwareRevisionChanged(); -} - -void WirelessSetupManager::setHardwareRevision(const QString &hardwareRevision) -{ - m_hardwareRevision = hardwareRevision; - emit hardwareRevisionChanged(); -} - -void WirelessSetupManager::setNetworkStatus(int networkStatus) -{ - if (m_networkStatus == networkStatus) - return; - - m_networkStatus = static_cast(networkStatus); - qDebug() << "-->" << m_networkStatus; - emit networkStatusChanged(); -} - -void WirelessSetupManager::setWirelessStatus(int wirelessStatus) -{ - if (m_wirelessStatus == wirelessStatus) - return; - - m_wirelessStatus = static_cast(wirelessStatus); - qDebug() << "-->" << m_wirelessStatus; - emit wirelessStatusChanged(); -} - -void WirelessSetupManager::setNetworkingEnabled(bool networkingEnabled) -{ - if (m_networkingEnabled == networkingEnabled) - return; - - qDebug() << "WifiSetupManager: Networking enabled changed" << networkingEnabled; - m_networkingEnabled = networkingEnabled; - emit networkingEnabledChanged(); - - if (!m_networkingEnabled) - m_accessPoints->clearModel(); - -} - -void WirelessSetupManager::setWirelessEnabled(bool wirelessEnabled) -{ - if (m_wirelessEnabled == wirelessEnabled) - return; - - qDebug() << "WifiSetupManager: Wireless enabled changed" << wirelessEnabled; - m_wirelessEnabled = wirelessEnabled; - emit wirelessEnabledChanged(); - - if (!m_wirelessEnabled) - m_accessPoints->clearModel(); - -} - -void WirelessSetupManager::streamData(const QVariantMap &request) -{ - QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); - if (!characteristic.isValid()) { - qWarning() << "WifiSetupManager: WirelessService: Wireless commander characteristic not valid"; - return; - } - - QByteArray data = QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact) + '\n'; - qDebug() << "WifiSetupManager: WirelessService: Start streaming response data:" << data.count() << "bytes"; - - int sentDataLength = 0; - QByteArray remainingData = data; - while (!remainingData.isEmpty()) { - QByteArray package = remainingData.left(20); - sentDataLength += package.count(); - m_wifiService->writeCharacteristic(characteristic, package); - remainingData = remainingData.remove(0, package.count()); - } - - qDebug() << "WifiSetupManager: WirelessService: Finished streaming request data"; -} - -void WirelessSetupManager::processNetworkResponse(const QVariantMap &response) -{ - if (!response.contains("c") || !response.contains("r")) { - qWarning() << "WifiSetupManager: Got invalid response map."; - return; - } - - NetworkServiceCommand command = (NetworkServiceCommand)response.value("c").toInt(); - NetworkServiceResponse responseCode = (NetworkServiceResponse)response.value("r").toInt(); - - if (responseCode != NetworkServiceResponseSuccess) { - qWarning() << "WifiSetupManager: Got error for command" << command << responseCode; - - - switch (responseCode) { - case NetworkServiceResponseIvalidValue: - emit errorOccurred(tr("Invalid value.")); - break; - case NetworkServiceResponseNetworkManagerNotAvailable: - emit errorOccurred(tr("There is no networkmanager available on the device.")); - break; - case NetworkServiceResponseWirelessNotAvailable: - emit errorOccurred(tr("There is no wireless device available on the device.")); - break; - default: - emit errorOccurred(tr("Unknown error occured.")); - break; - } - - return; - } - - qDebug() << "WifiSetupManager: Network command response" << command << responseCode; -} - -void WirelessSetupManager::processWifiResponse(const QVariantMap &response) -{ - m_working = false; - emit workingChanged(); - - if (!response.contains("c") || !response.contains("r")) { - qWarning() << "WifiSetupManager: Got invalid response map."; - return; - } - - WirelessServiceCommand command = (WirelessServiceCommand)response.value("c").toInt(); - WirelessServiceResponse responseCode = (WirelessServiceResponse)response.value("r").toInt(); - - if (responseCode != WirelessServiceResponseSuccess) { - qWarning() << "WifiSetupManager: Got error for command" << command << responseCode; - - switch (responseCode) { - case WirelessServiceResponseIvalidCommand: - emit errorOccurred(tr("Invalid command.")); - break; - case WirelessServiceResponseIvalidParameters: - emit errorOccurred(tr("Invalid parameters.")); - break; - case WirelessServiceResponseNetworkManagerNotAvailable: - emit errorOccurred(tr("There is no networkmanager available on the device.")); - break; - case WirelessServiceResponseWirelessNotAvailable: - emit errorOccurred(tr("There is no wireless device available on the device.")); - break; - case WirelessServiceResponseWirelessNotEnabled: - emit errorOccurred(tr("The wireless networking is disabled on the device.")); - break; - case WirelessServiceResponseNetworkingNotEnabled: - emit errorOccurred(tr("The networking is disabled on the device.")); - break; - default: - emit errorOccurred(tr("Unknown error occured.")); - break; - } - - return; - } - - switch (command) { - case WirelessServiceCommandGetNetworks: { - if (!response.contains("p")) { - qWarning() << "WifiSetupManager: Missing parameters in response."; - return; - } - - m_accessPointsVariantList = response.value("p").toList(); - m_accessPoints->clearModel(); - - foreach (const QVariant &accessPointVariant, m_accessPointsVariantList) { - QVariantMap accessPointVariantMap = accessPointVariant.toMap(); - WirelessAccessPoint *accessPoint = new WirelessAccessPoint(this); - accessPoint->setSsid(accessPointVariantMap.value("e").toString()); - accessPoint->setMacAddress(accessPointVariantMap.value("m").toString()); - accessPoint->setSignalStrength(accessPointVariantMap.value("s").toInt()); - accessPoint->setProtected(accessPointVariantMap.value("p").toBool()); - accessPoint->setHostAddress(""); - - m_accessPoints->addWirelessAccessPoint(accessPoint); - } - - loadCurrentConnection(); - - break; - } - case WirelessServiceCommandConnect: - - break; - case WirelessServiceCommandConnectHidden: - - break; - case WirelessServiceCommandDisconnect: - - break; - case WirelessServiceCommandGetCurrentConnection: { - - qDebug() << "Current network connection" << response; - QVariantMap currentConnection = response.value("p").toMap();; - - // Find current network - m_currentConnection = nullptr; - QString macAddress = currentConnection.value("m").toString(); - foreach (WirelessAccessPoint *accessPoint, m_accessPoints->wirelessAccessPoints()) { - if (accessPoint->macAddress() == macAddress) { - // Set the current network - m_currentConnection = accessPoint; - accessPoint->setHostAddress(currentConnection.value("i").toString()); - } - } - qDebug() << "current connection is:" << m_currentConnection; - emit currentConnectionChanged(); - - m_initialized = true; - emit initializedChanged(); - - break; - } - default: - break; - } -} - -void WirelessSetupManager::processSystemResponse(const QVariantMap &response) -{ - m_working = false; - emit workingChanged(); - - if (!response.contains("c") || !response.contains("r")) { - qWarning() << "WifiSetupManager: Got invalid response map."; - return; - } - - SystemServiceCommand command = (SystemServiceCommand)response.value("c").toInt(); - SystemServiceResponse responseCode = (SystemServiceResponse)response.value("r").toInt(); - - if (responseCode != SystemServiceResponseSuccess) { - qWarning() << "WifiSetupManager: Got error for command" << command << responseCode; - return; - } - - qDebug() << "WifiSetupManager: System command response" << command << responseCode; -} - -void WirelessSetupManager::onConnectedChanged() -{ - if (!connected()) { - // Clean up - qDebug() << "WifiSetupManager: Clean up services"; - if (m_deviceInformationService) { - m_deviceInformationService->deleteLater(); - } - if (m_netwokService) { - m_netwokService->deleteLater(); - } - if (m_wifiService) { - m_wifiService->deleteLater(); - } - if (m_systemService) { - m_systemService->deleteLater(); - } - - m_deviceInformationService = nullptr; - m_netwokService = nullptr; - m_wifiService = nullptr; - m_systemService = nullptr; - - m_accessPoints->clearModel(); - - m_initialized = false; - emit initializedChanged(); - m_working = false; - emit workingChanged(); - - setManufacturer(""); - setModelNumber(""); - setSoftwareRevision(""); - setFirmwareRevision(""); - setHardwareRevision(""); - } -} - -void WirelessSetupManager::onServiceDiscoveryFinished() -{ - foreach (const QBluetoothUuid &serviceUuid, controller()->services()) { - qDebug() << "WifiSetupManager: -->" << serviceUuid.toString(); - } - - if (!controller()->services().contains(QBluetoothUuid::DeviceInformation)) { - qWarning() << "WifiSetupManager: Could not find device information service"; - controller()->disconnectFromDevice(); - return; - } - - if (!controller()->services().contains(networkServiceUuid)) { - qWarning() << "WifiSetupManager: Could not find network service"; - controller()->disconnectFromDevice(); - return; - } - - if (!controller()->services().contains(wifiServiceUuid)) { - qWarning() << "WifiSetupManager: Could not find wifi service"; - controller()->disconnectFromDevice(); - return; - } - - // Device Informantion service - if (!m_deviceInformationService) { - m_deviceInformationService = controller()->createServiceObject(QBluetoothUuid::DeviceInformation, this); - if (!m_deviceInformationService) { - qWarning() << "WifiSetupManager: Could not create temperature service."; - controller()->disconnectFromDevice(); - return; - } - - connect(m_deviceInformationService, &QLowEnergyService::stateChanged, this, &WirelessSetupManager::onDeviceInformationStateChanged); - connect(m_deviceInformationService, &QLowEnergyService::characteristicChanged, this, &WirelessSetupManager::onDeviceInformationCharacteristicChanged); - connect(m_deviceInformationService, &QLowEnergyService::characteristicRead, this, &WirelessSetupManager::onDeviceInformationCharacteristicChanged); - - if (m_deviceInformationService->state() == QLowEnergyService::DiscoveryRequired) { - qDebug() << "WifiSetupManager: start discovering device information service..."; - m_deviceInformationService->discoverDetails(); - } - - } -} - -void WirelessSetupManager::onDeviceInformationStateChanged(const QLowEnergyService::ServiceState &state) -{ - - if (state != QLowEnergyService::ServiceDiscovered) - return; - - qDebug() << "WifiSetupManager: Device information service discovered."; - - foreach (const QLowEnergyCharacteristic &characteristic, m_deviceInformationService->characteristics()) { - qDebug() << " -->" << characteristic.name() << characteristic.uuid().toString() << characteristic.value(); - foreach (const QLowEnergyDescriptor &descriptor, characteristic.descriptors()) { - qDebug() << " -->" << descriptor.name() << descriptor.uuid().toString() << descriptor.value(); - } - } - - setManufacturer(QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::ManufacturerNameString).value())); - setModelNumber(QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::ModelNumberString).value())); - setSoftwareRevision(QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::SoftwareRevisionString).value())); - setFirmwareRevision(QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::FirmwareRevisionString).value())); - setHardwareRevision(QString::fromUtf8(m_deviceInformationService->characteristic(QBluetoothUuid::HardwareRevisionString).value())); - - // Network service - if (!m_netwokService) { - m_netwokService = controller()->createServiceObject(networkServiceUuid, this); - if (!m_netwokService) { - qWarning() << "WifiSetupManager: Could not create network service."; - controller()->disconnectFromDevice(); - return; - } - - connect(m_netwokService, &QLowEnergyService::stateChanged, this, &WirelessSetupManager::onNetworkServiceStateChanged); - connect(m_netwokService, &QLowEnergyService::characteristicChanged, this, &WirelessSetupManager::onNetworkServiceCharacteristicChanged); - connect(m_netwokService, &QLowEnergyService::characteristicRead, this, &WirelessSetupManager::onNetworkServiceReadFinished); - - if (m_netwokService->state() == QLowEnergyService::DiscoveryRequired) { - qDebug() << "WifiSetupManager: start discovering network service..."; - m_netwokService->discoverDetails(); - } - } - checkInitialized(); -} - -void WirelessSetupManager::onDeviceInformationCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - Q_UNUSED(characteristic) - Q_UNUSED(value) -} - -void WirelessSetupManager::onDeviceInformationReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - Q_UNUSED(characteristic) - Q_UNUSED(value) -} - -void WirelessSetupManager::onNetworkServiceStateChanged(const QLowEnergyService::ServiceState &state) -{ - if (state != QLowEnergyService::ServiceDiscovered) - return; - - qDebug() << "WifiSetupManager: Network service discovered."; - - foreach (const QLowEnergyCharacteristic &characteristic, m_netwokService->characteristics()) { - qDebug() << " -->" << characteristic.name() << characteristic.uuid().toString() << characteristic.value(); - foreach (const QLowEnergyDescriptor &descriptor, characteristic.descriptors()) { - qDebug() << " -->" << descriptor.name() << descriptor.uuid().toString() << descriptor.value(); - } - } - - QLowEnergyCharacteristic networkCharacteristic = m_netwokService->characteristic(networkStatusCharacteristicUuid); - if (!networkCharacteristic.isValid()) { - qWarning() << "Invalud networking status characteristic"; - return; - } - - QLowEnergyCharacteristic networkingEnabledCharacteristic = m_netwokService->characteristic(networkingEnabledCharacteristicUuid); - if (!networkingEnabledCharacteristic.isValid()) { - qWarning() << "Invalud networking enabled characteristic"; - return; - } - - QLowEnergyCharacteristic wirelessEnabledCharacteristic = m_netwokService->characteristic(wirelessEnabledCharacteristicUuid); - if (!wirelessEnabledCharacteristic.isValid()) { - qWarning() << "Invalud wireless enabled characteristic"; - return; - } - - - // Enable notifications - qDebug() << "Enable notifications of network service"; - m_netwokService->writeDescriptor(networkCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - m_netwokService->writeDescriptor(networkingEnabledCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - m_netwokService->writeDescriptor(wirelessEnabledCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - - setStatusText("Connected and ready"); - m_working = false; - emit workingChanged(); - - // Done with discovery - setNetworkStatus(networkCharacteristic.value().toHex().toUInt(0, 16)); - setNetworkingEnabled((bool)networkingEnabledCharacteristic.value().toHex().toUInt(0, 16)); - setWirelessEnabled((bool)wirelessEnabledCharacteristic.value().toHex().toUInt(0, 16)); - - // Wifi service - if (!m_wifiService) { - m_wifiService = controller()->createServiceObject(wifiServiceUuid, this); - if (!m_wifiService) { - qWarning() << "WifiSetupManager: Could not create wifi service."; - controller()->disconnectFromDevice(); - return; - } - - connect(m_wifiService, &QLowEnergyService::stateChanged, this, &WirelessSetupManager::onWifiServiceStateChanged); - connect(m_wifiService, &QLowEnergyService::characteristicChanged, this, &WirelessSetupManager::onWifiServiceCharacteristicChanged); - connect(m_wifiService, &QLowEnergyService::characteristicRead, this, &WirelessSetupManager::onWifiServiceReadFinished); - - if (m_wifiService->state() == QLowEnergyService::DiscoveryRequired) { - qDebug() << "WifiSetupManager: start discovering wifi service..."; - m_wifiService->discoverDetails(); - } - - } - - checkInitialized(); -} - -void WirelessSetupManager::onNetworkServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - if (characteristic.uuid() == networkStatusCharacteristicUuid) { - qDebug() << "Network status changed:" << value; - setNetworkStatus(value.toHex().toInt(nullptr, 16)); - if (m_networkStatus == NetworkStatusGlobal) { - loadCurrentConnection(); - } - return; - } else if (characteristic.uuid() == networkResponseCharacteristicUuid) { - // Check if currently reading - if (m_readingResponse) { - m_inputDataStream.append(value); - } else { - m_inputDataStream.clear(); - m_readingResponse = true; - m_inputDataStream.append(value); - } - - // If command finished - if (value.endsWith('\n')) { - QJsonParseError error; - QJsonDocument jsonDocument = QJsonDocument::fromJson(m_inputDataStream, &error); - if (error.error != QJsonParseError::NoError) { - qWarning() << "Got invalid json object" << m_inputDataStream; - m_inputDataStream.clear(); - m_readingResponse = false; - return; - } - - qDebug() << "Got command stream" << qUtf8Printable(jsonDocument.toJson()); - - processNetworkResponse(jsonDocument.toVariant().toMap()); - - m_inputDataStream.clear(); - m_readingResponse = false; - return; - } - } else if (characteristic.uuid() == networkingEnabledCharacteristicUuid) { - qDebug() << "Networking enabled changed" << (bool)value.toHex().toUInt(0, 16); - setNetworkingEnabled((bool)value.toHex().toUInt(0, 16)); - return; - } else if (characteristic.uuid() == wirelessEnabledCharacteristicUuid) { - qDebug() << "Wireless enabled changed" << (bool)value.toHex().toUInt(0, 16); - setWirelessEnabled((bool)value.toHex().toUInt(0, 16)); - return; - } - - qWarning() << "Bluetooth: Unhandled service characteristic changed" << characteristic.uuid() << value; -} - -void WirelessSetupManager::onNetworkServiceReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - Q_UNUSED(characteristic) - Q_UNUSED(value) -} - -void WirelessSetupManager::onWifiServiceStateChanged(const QLowEnergyService::ServiceState &state) -{ - if (state != QLowEnergyService::ServiceDiscovered) - return; - - qDebug() << "WifiSetupManager: Wifi service discovered."; - - foreach (const QLowEnergyCharacteristic &characteristic, m_wifiService->characteristics()) { - qDebug() << " -->" << characteristic.name() << characteristic.uuid().toString() << characteristic.value(); - foreach (const QLowEnergyDescriptor &descriptor, characteristic.descriptors()) { - qDebug() << " -->" << descriptor.name() << descriptor.uuid().toString() << descriptor.value(); - } - } - - // Enable notifications - m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiStatusCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - - setWirelessStatus(m_wifiService->characteristic(wifiStatusCharacteristicUuid).value().toHex().toUInt(0, 16)); - - // System service - if (!m_systemService) { - m_systemService = controller()->createServiceObject(systemServiceUuid, this); - if (!m_systemService) { - qWarning() << "WifiSetupManager: Could not create system service. Looks like this networkmanager has not implemented that."; - //controller()->disconnectFromDevice(); - } else { - connect(m_systemService, &QLowEnergyService::stateChanged, this, &WirelessSetupManager::onSystemServiceStateChanged); - connect(m_systemService, &QLowEnergyService::characteristicChanged, this, &WirelessSetupManager::onSystemServiceCharacteristicChanged); - connect(m_systemService, &QLowEnergyService::characteristicRead, this, &WirelessSetupManager::onSystemServiceReadFinished); - - if (m_systemService->state() == QLowEnergyService::DiscoveryRequired) { - qDebug() << "WifiSetupManager: start discovering system service..."; - m_systemService->discoverDetails(); - } - } - } - - checkInitialized(); -} - -void WirelessSetupManager::onWifiServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - Q_UNUSED(characteristic) - - if (characteristic.uuid() == wifiResponseCharacteristicUuid) { - // Check if currently reading - if (m_readingResponse) { - m_inputDataStream.append(value); - } else { - m_inputDataStream.clear(); - m_readingResponse = true; - m_inputDataStream.append(value); - } - - // If command finished - if (value.endsWith('\n')) { - QJsonParseError error; - QJsonDocument jsonDocument = QJsonDocument::fromJson(m_inputDataStream, &error); - if (error.error != QJsonParseError::NoError) { - qWarning() << "Got invalid json object" << m_inputDataStream; - m_inputDataStream.clear(); - m_readingResponse = false; - return; - } - - qDebug() << "Got command stream" << qUtf8Printable(jsonDocument.toJson()); - - processWifiResponse(jsonDocument.toVariant().toMap()); - - m_inputDataStream.clear(); - m_readingResponse = false; - } - return; - } - - if (characteristic.uuid() == wifiStatusCharacteristicUuid) { - qDebug() << "Wireless status changed:" << value.toHex().toUInt(nullptr, 16) << "Old status:" << m_wirelessStatus; - setWirelessStatus(value.toHex().toInt(nullptr, 16)); - if (m_wirelessStatus == WirelessStatusActivated) { - loadCurrentConnection(); - } - return; - } - - qWarning() << "Bluetooth: Unhandled service characteristic changed" << characteristic.uuid() << value; - -} - -void WirelessSetupManager::onWifiServiceReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - Q_UNUSED(characteristic) - Q_UNUSED(value) -} - -void WirelessSetupManager::onSystemServiceStateChanged(const QLowEnergyService::ServiceState &state) -{ - if (state != QLowEnergyService::ServiceDiscovered) - return; - - qDebug() << "WifiSetupManager: System service discovered."; - - foreach (const QLowEnergyCharacteristic &characteristic, m_systemService->characteristics()) { - qDebug() << " -->" << characteristic.name() << characteristic.uuid().toString() << characteristic.value(); - foreach (const QLowEnergyDescriptor &descriptor, characteristic.descriptors()) { - qDebug() << " -->" << descriptor.name() << descriptor.uuid().toString() << descriptor.value(); - } - } - - // Enable notifications - m_systemService->writeDescriptor(m_systemService->characteristic(systemResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); - - checkInitialized(); -} - -void WirelessSetupManager::onSystemServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - Q_UNUSED(characteristic) - - // Check if currently reading - if (m_readingResponse) { - m_inputDataStream.append(value); - } else { - m_inputDataStream.clear(); - m_readingResponse = true; - m_inputDataStream.append(value); - } - - // If command finished - if (value.endsWith('\n')) { - QJsonParseError error; - QJsonDocument jsonDocument = QJsonDocument::fromJson(m_inputDataStream, &error); - if (error.error != QJsonParseError::NoError) { - qWarning() << "Got invalid json object" << m_inputDataStream; - m_inputDataStream.clear(); - m_readingResponse = false; - return; - } - - qDebug() << "Got command stream" << qUtf8Printable(jsonDocument.toJson()); - - processSystemResponse(jsonDocument.toVariant().toMap()); - - m_inputDataStream.clear(); - m_readingResponse = false; - } -} - -void WirelessSetupManager::onSystemServiceReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - Q_UNUSED(characteristic) - Q_UNUSED(value) -} diff --git a/libnymea-app/wifisetup/wirelesssetupmanager.h b/libnymea-app/wifisetup/wirelesssetupmanager.h deleted file mode 100644 index a56ccfd7..00000000 --- a/libnymea-app/wifisetup/wirelesssetupmanager.h +++ /dev/null @@ -1,275 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -* -* Copyright 2013 - 2020, nymea GmbH -* Contact: contact@nymea.io -* -* This file is part of nymea. -* This project including source code and documentation is protected by -* copyright law, and remains the property of nymea GmbH. All rights, including -* reproduction, publication, editing and translation, are reserved. The use of -* this project is subject to the terms of a license agreement to be concluded -* with nymea GmbH in accordance with the terms of use of nymea GmbH, available -* under https://nymea.io/license -* -* GNU General Public License Usage -* Alternatively, this project may be redistributed and/or modified under the -* terms of the GNU General Public License as published by the Free Software -* Foundation, GNU version 3. This project is distributed in the hope that it -* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty -* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General -* Public License for more details. -* -* You should have received a copy of the GNU General Public License along with -* this project. If not, see . -* -* For any further details and any questions please contact us under -* contact@nymea.io or see our FAQ/Licensing Information on -* https://nymea.io/license/faq -* -* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef WIRELESSSETUPMANAGER_H -#define WIRELESSSETUPMANAGER_H - -#include -#include - -#include "bluetoothdevice.h" - -class WirelessAccessPoint; -class WirelessAccessPoints; -class WirelessAccessPointsProxy; - -class WirelessSetupManager : public BluetoothDevice -{ - Q_OBJECT - Q_PROPERTY(bool working READ working NOTIFY workingChanged) - Q_PROPERTY(bool initialized READ initialized NOTIFY initializedChanged) - - Q_PROPERTY(WirelessAccessPoints *accessPoints READ accessPoints CONSTANT) - Q_PROPERTY(WirelessAccessPoint *currentConnection READ currentConnection NOTIFY currentConnectionChanged) - - Q_PROPERTY(QString modelNumber READ modelNumber NOTIFY modelNumberChanged) - Q_PROPERTY(QString manufacturer READ manufacturer NOTIFY manufacturerChanged) - Q_PROPERTY(QString softwareRevision READ softwareRevision NOTIFY softwareRevisionChanged) - Q_PROPERTY(QString firmwareRevision READ firmwareRevision NOTIFY firmwareRevisionChanged) - Q_PROPERTY(QString hardwareRevision READ hardwareRevision NOTIFY hardwareRevisionChanged) - - Q_PROPERTY(NetworkStatus networkStatus READ networkStatus NOTIFY networkStatusChanged) - Q_PROPERTY(WirelessStatus wirelessStatus READ wirelessStatus NOTIFY wirelessStatusChanged) - Q_PROPERTY(bool networkingEnabled READ networkingEnabled NOTIFY networkingEnabledChanged) - Q_PROPERTY(bool wirelessEnabled READ wirelessEnabled NOTIFY wirelessEnabledChanged) - -public: - - enum WirelessServiceCommand { - WirelessServiceCommandInvalid = -1, - WirelessServiceCommandGetNetworks = 0x00, - WirelessServiceCommandConnect = 0x01, - WirelessServiceCommandConnectHidden = 0x02, - WirelessServiceCommandDisconnect = 0x03, - WirelessServiceCommandScan = 0x04, - WirelessServiceCommandGetCurrentConnection = 0x05 - }; - Q_ENUM(WirelessServiceCommand) - - enum WirelessServiceResponse { - WirelessServiceResponseSuccess = 0x00, - WirelessServiceResponseIvalidCommand = 0x01, - WirelessServiceResponseIvalidParameters = 0x02, - WirelessServiceResponseNetworkManagerNotAvailable = 0x03, - WirelessServiceResponseWirelessNotAvailable = 0x04, - WirelessServiceResponseWirelessNotEnabled = 0x05, - WirelessServiceResponseNetworkingNotEnabled = 0x06, - WirelessServiceResponseUnknownError = 0x07 - }; - Q_ENUM(WirelessServiceResponse) - - enum NetworkServiceCommand { - NetworkServiceCommandInvalid = -1, - NetworkServiceCommandEnableNetworking = 0x00, - NetworkServiceCommandDisableNetworking = 0x01, - NetworkServiceCommandEnableWireless = 0x02, - NetworkServiceCommandDisableWireless = 0x03 - }; - Q_ENUM(NetworkServiceCommand) - - enum NetworkServiceResponse { - NetworkServiceResponseSuccess = 0x00, - NetworkServiceResponseIvalidValue = 0x01, - NetworkServiceResponseNetworkManagerNotAvailable = 0x02, - NetworkServiceResponseWirelessNotAvailable = 0x03, - NetworkServiceResponseUnknownError = 0x04, - }; - Q_ENUM(NetworkServiceResponse) - - enum SystemServiceCommand { - SystemServiceCommandInvalid = -1, - SystemServiceCommandPushAuthentication = 0x00 - }; - Q_ENUM(SystemServiceCommand) - - enum SystemServiceResponse { - SystemServiceResponseSuccess = 0x00, - SystemServiceResponseUnknownError = 0x01, - SystemServiceResponseInvalidCommand = 0x02, - SystemServiceResponseInvalidValue = 0x03, - SystemServiceResponsePushServiceUnavailable = 0x04, - }; - Q_ENUM(SystemServiceResponse) - - enum NetworkStatus { - NetworkStatusUnknown = 0x00, - NetworkStatusAsleep = 0x01, - NetworkStatusDisconnected = 0x02, - NetworkStatusDisconnecting = 0x03, - NetworkStatusConnecting = 0x04, - NetworkStatusLocal = 0x05, - NetworkStatusConnectedSite = 0x06, - NetworkStatusGlobal = 0x07 - }; - Q_ENUM(NetworkStatus) - - enum WirelessStatus { - WirelessStatusUnknown = 0x00, - WirelessStatusUnmanaged = 0x01, - WirelessStatusUnavailable = 0x02, - WirelessStatusDisconnected = 0x03, - WirelessStatusPrepare = 0x04, - WirelessStatusConfig = 0x05, - WirelessStatusNeedAuth = 0x06, - WirelessStatusIpConfig = 0x07, - WirelessStatusIpCheck = 0x08, - WirelessStatusSecondaries = 0x09, - WirelessStatusActivated = 0x0A, - WirelessStatusDeactivating = 0x0B, - WirelessStatusFailed = 0x0C - }; - Q_ENUM(WirelessStatus) - - explicit WirelessSetupManager(const QBluetoothDeviceInfo &deviceInfo, QObject *parent = nullptr); - - QString modelNumber() const; - QString manufacturer() const; - QString softwareRevision() const; - QString firmwareRevision() const; - QString hardwareRevision() const; - - bool initialized() const; - bool working() const; - - NetworkStatus networkStatus() const; - WirelessSetupManager::WirelessStatus wirelessStatus() const; - - bool networkingEnabled() const; - bool wirelessEnabled() const; - - WirelessAccessPoints *accessPoints() const; - WirelessAccessPoint *currentConnection() const; - - void reloadData(); - - // Wireless commands - Q_INVOKABLE void loadNetworks(); - Q_INVOKABLE void loadCurrentConnection(); - Q_INVOKABLE void performWifiScan(); - Q_INVOKABLE void enableNetworking(bool enable); - Q_INVOKABLE void enableWireless(bool enable); - Q_INVOKABLE void connectWirelessNetwork(const QString &ssid, const QString &password = QString()); - Q_INVOKABLE void disconnectWirelessNetwork(); - Q_INVOKABLE void pressPushButton(); - -signals: - void modelNumberChanged(); - void manufacturerChanged(); - void softwareRevisionChanged(); - void firmwareRevisionChanged(); - void hardwareRevisionChanged(); - - void initializedChanged(); - void workingChanged(); - - void networkStatusChanged(); - void wirelessStatusChanged(); - void networkingEnabledChanged(); - void wirelessEnabledChanged(); - - void currentConnectionChanged(); - - void errorOccurred(const QString &errorMessage); - -private slots: - void onConnectedChanged(); - void onServiceDiscoveryFinished(); - - void onDeviceInformationStateChanged(const QLowEnergyService::ServiceState &state); - void onDeviceInformationCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - void onDeviceInformationReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - - void onNetworkServiceStateChanged(const QLowEnergyService::ServiceState &state); - void onNetworkServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - void onNetworkServiceReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - - void onWifiServiceStateChanged(const QLowEnergyService::ServiceState &state); - void onWifiServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - void onWifiServiceReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - - void onSystemServiceStateChanged(const QLowEnergyService::ServiceState &state); - void onSystemServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - void onSystemServiceReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - -private: - QLowEnergyService *m_deviceInformationService = nullptr; - QLowEnergyService *m_netwokService = nullptr; - QLowEnergyService *m_wifiService = nullptr; - QLowEnergyService *m_systemService = nullptr; - - WirelessAccessPoints *m_accessPoints = nullptr; - WirelessAccessPoint *m_currentConnection = nullptr; - - QString m_modelNumber; - QString m_manufacturer; - QString m_softwareRevision; - QString m_firmwareRevision; - QString m_hardwareRevision; - - bool m_networkingEnabled = false; - bool m_wirelessEnabled = false; - - bool m_working = false; - bool m_initialized = false; - - NetworkStatus m_networkStatus = NetworkStatusUnknown; - WirelessStatus m_wirelessStatus = WirelessStatusUnknown; - - bool m_readingResponse = false; - QByteArray m_inputDataStream; - - QString m_ssid; - QString m_password; - - QVariantList m_accessPointsVariantList; - - void checkInitialized(); - - // Private set methods for read only properties - void setModelNumber(const QString &modelNumber); - void setManufacturer(const QString &manufacturer); - void setSoftwareRevision(const QString &softwareRevision); - void setFirmwareRevision(const QString &firmwareRevision); - void setHardwareRevision(const QString &hardwareRevision); - - void setNetworkStatus(int networkStatus); - void setWirelessStatus(int wirelessStatus); - void setNetworkingEnabled(bool networkingEnabled); - void setWirelessEnabled(bool wirelessEnabled); - - // Data methods - void streamData(const QVariantMap &request); - void processNetworkResponse(const QVariantMap &response); - void processWifiResponse(const QVariantMap &response); - void processSystemResponse(const QVariantMap &response); - -}; - -#endif // WIRELESSSETUPMANAGER_H diff --git a/nymea-app/ui/Nymea.qml b/nymea-app/ui/Nymea.qml index 41494ca3..3cb99172 100644 --- a/nymea-app/ui/Nymea.qml +++ b/nymea-app/ui/Nymea.qml @@ -130,13 +130,11 @@ ApplicationWindow { anchors.bottomMargin: keyboardRect.height } - NymeaDiscovery { - id: discovery + property NymeaDiscovery nymeaDiscovery: NymeaDiscovery { objectName: "discovery" awsClient: AWSClient // discovering: pageStack.currentItem.objectName === "discoveryPage" } - property alias _discovery: discovery property var supportedInterfaces: [ "light", diff --git a/nymea-app/ui/RootItem.qml b/nymea-app/ui/RootItem.qml index 0b39375e..572b0900 100644 --- a/nymea-app/ui/RootItem.qml +++ b/nymea-app/ui/RootItem.qml @@ -84,7 +84,7 @@ Item { d.pushSettingsPage("connection/wifisetup/BluetoothDiscoveryPage.qml"); } function startDemoMode() { - var host = discovery.nymeaHosts.createWanHost("Demo server", "nymea://nymea.nymea.io:2222") + var host = nymeaDiscovery.nymeaHosts.createWanHost("Demo server", "nymea://nymea.nymea.io:2222") root.currentEngine.jsonRpcClient.connectToHost(host) } @@ -164,7 +164,7 @@ Item { } Binding { - target: _discovery + target: nymeaDiscovery property: "discovering" value: engine.jsonRpcClient.currentHost === null } @@ -180,11 +180,11 @@ Item { Component.onCompleted: { setupPushNotifications(); if (autoConnectHost.length > 0) { - var host = discovery.nymeaHosts.createLanHost("Manual connection", autoConnectHost); + var host = nymeaDiscovery.nymeaHosts.createLanHost("Manual connection", autoConnectHost); engine.jsonRpcClient.connectToHost(host) } else if (tabSettings.lastConnectedHost.length > 0) { print("Last connected host was", tabSettings.lastConnectedHost) - var cachedHost = discovery.nymeaHosts.find(tabSettings.lastConnectedHost); + var cachedHost = nymeaDiscovery.nymeaHosts.find(tabSettings.lastConnectedHost); if (cachedHost) { engine.jsonRpcClient.connectToHost(cachedHost) return; @@ -343,14 +343,14 @@ Item { var popup = certDialogComponent.createObject(root); popup.accepted.connect(function(){ engine.jsonRpcClient.acceptCertificate(serverUuid, pem); - engine.jsonRpcClient.connectToHost(discovery.nymeaHosts.find(serverUuid)); + engine.jsonRpcClient.connectToHost(nymeaDiscovery.nymeaHosts.find(serverUuid)); }) popup.open(); } onConnectedChanged: { print("json client connected changed", engine.jsonRpcClient.connected) if (engine.jsonRpcClient.connected) { - discovery.cacheHost(engine.jsonRpcClient.currentHost) + nymeaDiscovery.cacheHost(engine.jsonRpcClient.currentHost) tabSettings.lastConnectedHost = engine.jsonRpcClient.serverUuid } init(); diff --git a/nymea-app/ui/appsettings/CloudLoginPage.qml b/nymea-app/ui/appsettings/CloudLoginPage.qml index 916d736b..912bd19a 100644 --- a/nymea-app/ui/appsettings/CloudLoginPage.qml +++ b/nymea-app/ui/appsettings/CloudLoginPage.qml @@ -126,7 +126,7 @@ SettingsPageBase { onClicked: { print("clicked, connected:", engine.jsonRpcClient.connected, model.id) if (!engine.jsonRpcClient.connected) { - var host = discovery.nymeaHosts.find(model.id) + var host = nymeaDiscovery.nymeaHosts.find(model.id) engine.jsonRpcClient.connectToHost(host); } } diff --git a/nymea-app/ui/connection/ConnectPage.qml b/nymea-app/ui/connection/ConnectPage.qml index 70dd5ae0..63ce0d59 100644 --- a/nymea-app/ui/connection/ConnectPage.qml +++ b/nymea-app/ui/connection/ConnectPage.qml @@ -70,7 +70,7 @@ Page { NymeaHostsFilterModel { id: hostsProxy - discovery: _discovery + discovery: nymeaDiscovery showUnreachableBearers: false jsonRpcClient: engine.jsonRpcClient showUnreachableHosts: false @@ -220,16 +220,16 @@ Page { Layout.leftMargin: app.margins Layout.rightMargin: app.margins wrapMode: Text.WordWrap - visible: discovery.nymeaHosts.count === 0 + visible: nymeaDiscovery.nymeaHosts.count === 0 text: qsTr("Do you have a %1:core but it's not connected to your network yet? Use the wireless setup to connect it!").arg(app.systemName) } Button { Layout.fillWidth: true Layout.leftMargin: app.margins Layout.rightMargin: app.margins - visible: discovery.nymeaHosts.count === 0 + visible: nymeaDiscovery.nymeaHosts.count === 0 text: qsTr("Start wireless setup") - onClicked: pageStack.push(Qt.resolvedUrl("wifisetup/BluetoothDiscoveryPage.qml"), {nymeaDiscovery: discovery}) + onClicked: pageStack.push(Qt.resolvedUrl("wifisetup/BluetoothDiscoveryPage.qml")) } Button { Layout.fillWidth: true @@ -245,10 +245,10 @@ Page { Layout.leftMargin: app.margins Layout.rightMargin: app.margins Layout.bottomMargin: app.margins - visible: discovery.nymeaHosts.count === 0 + visible: nymeaDiscovery.nymeaHosts.count === 0 text: qsTr("Demo mode (online)") onClicked: { - var host = discovery.nymeaHosts.createWanHost("Demo server", "nymea://nymea.nymea.io:2222") + var host = nymeaDiscovery.nymeaHosts.createWanHost("Demo server", "nymea://nymea.nymea.io:2222") engine.jsonRpcClient.connectToHost(host) } } diff --git a/nymea-app/ui/connection/ManualConnectPage.qml b/nymea-app/ui/connection/ManualConnectPage.qml index bf6c73e9..6ae36884 100644 --- a/nymea-app/ui/connection/ManualConnectPage.qml +++ b/nymea-app/ui/connection/ManualConnectPage.qml @@ -124,7 +124,7 @@ Page { } print("Try to connect ", rpcUrl) - var host = discovery.nymeaHosts.createLanHost("Manual connection", rpcUrl); + var host = nymeaDiscovery.nymeaHosts.createLanHost("Manual connection", rpcUrl); engine.jsonRpcClient.connectToHost(host) } } diff --git a/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml b/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml index 4d1f3a3a..c8ef605d 100644 --- a/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml +++ b/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml @@ -41,57 +41,70 @@ Page { onBackPressed: pageStack.pop() } - property var nymeaDiscovery: null - BluetoothDiscovery { id: bluetoothDiscovery discoveryEnabled: pageStack.currentItem === root } - NetworkManagerController { - id: networkManager + BtWiFiSetup { + id: wifiSetup + + onStatusChanged: { + print("status changed", status) + switch (status) { + case BtWiFiSetup.StatusDisconnected: + pageStack.pop(root) + break; + case BtWiFiSetup.StatusConnectingToBluetooth: + break; + case BtWiFiSetup.StatusConnectedToBluetooth: + if (!wifiSetup.networkingEnabled) { + wifiSetup.networkingEnabled = true; + } + if (!wifiSetup.wirelessEnabled) { + wifiSetup.wirelessEnabled = true; + } + setupDevice() + break; + case BtWiFiSetup.StatusConnectingToWiFi: + break; + case BtWiFiSetup.StatusConnectedToWiFi: + print("Connected to wifi!") + } + } + onBluetoothConnectionError: { + print("Error") + pageStack.pop(root) + } + onWirelessEnabledChanged: { + if (wirelessEnabled) { + scanWiFi(); + } + } } function connectDevice(btDeviceInfo) { - networkManager.bluetoothDeviceInfo = btDeviceInfo - networkManager.connectDevice(); + wifiSetup.connectToDevice(btDeviceInfo) print("**** connecting") pageStack.push(connectingPageComponent, {deviceName: btDeviceInfo.name}) } function setupDevice() { pageStack.pop(root, StackView.Immediate) - if (networkManager.manager.currentConnection) { - print("***** pushing WirelessSetupPage with networkManager:", networkManager) - var page = pageStack.push(Qt.resolvedUrl("WirelessSetupPage.qml"), { networkManagerController: networkManager, nymeaDiscovery: root.nymeaDiscovery } ) + if (wifiSetup.currentConnection) { + var page = pageStack.push(Qt.resolvedUrl("WirelessSetupPage.qml"), { wifiSetup: wifiSetup } ) page.done.connect(function() { pageStack.pop(root, StackView.Immediate); pageStack.pop(); }) } else { - var page = pageStack.push(Qt.resolvedUrl("ConnectWiFiPage.qml"), { networkManagerController: networkManager } ) + var page = pageStack.push(Qt.resolvedUrl("ConnectWiFiPage.qml"), { wifiSetup: wifiSetup } ) page.connected.connect(function() { setupDevice(); }) } } - Connections { - target: networkManager.manager - onInitializedChanged: { - if (networkManager.manager.initialized) { - setupDevice() - } else { - pageStack.pop(root) - } - } - - onConnectedChanged: { - if (!networkManager.manager.connected) { - pageStack.pop(root) - } - } - } ColumnLayout { anchors.fill: parent diff --git a/nymea-app/ui/connection/wifisetup/BoxInfoPage.qml b/nymea-app/ui/connection/wifisetup/BoxInfoPage.qml index 824aa7ca..4113db27 100644 --- a/nymea-app/ui/connection/wifisetup/BoxInfoPage.qml +++ b/nymea-app/ui/connection/wifisetup/BoxInfoPage.qml @@ -41,7 +41,7 @@ Page { onBackPressed: pageStack.pop() } - property var networkManagerController: null + property BtWiFiSetup wifiSetup: null ColumnLayout { anchors { left: parent.left; top: parent.top; right: parent.right } @@ -50,31 +50,31 @@ Page { Layout.fillWidth: true progressive: false text: qsTr("System UUID") - subText: networkManagerController.manager.modelNumber + subText: wifiSetup.modelNumber } NymeaSwipeDelegate { Layout.fillWidth: true progressive: false text: qsTr("Manufacturer") - subText: networkManagerController.manager.manufacturer + subText: wifiSetup.manufacturer } NymeaSwipeDelegate { Layout.fillWidth: true progressive: false text: qsTr("Software revision") - subText: networkManagerController.manager.softwareRevision + subText: wifiSetup.softwareRevision } NymeaSwipeDelegate { Layout.fillWidth: true progressive: false text: qsTr("Firmware revision") - subText: networkManagerController.manager.firmwareRevision + subText: wifiSetup.firmwareRevision } NymeaSwipeDelegate { Layout.fillWidth: true progressive: false text: qsTr("Hardware revision") - subText: networkManagerController.manager.hardwareRevision + subText: wifiSetup.hardwareRevision } } } diff --git a/nymea-app/ui/connection/wifisetup/ConnectWiFiPage.qml b/nymea-app/ui/connection/wifisetup/ConnectWiFiPage.qml index 2c1025fa..b1939d49 100644 --- a/nymea-app/ui/connection/wifisetup/ConnectWiFiPage.qml +++ b/nymea-app/ui/connection/wifisetup/ConnectWiFiPage.qml @@ -37,7 +37,7 @@ import Nymea 1.0 Page { id: root - property var networkManagerController: null + property BtWiFiSetup wifiSetup: null signal connected(); @@ -50,13 +50,13 @@ Page { HeaderButton { imageSource: "../images/info.svg" onClicked: { - pageStack.push(Qt.resolvedUrl("BoxInfoPage.qml"), {networkManagerController: root.networkManagerController}) + pageStack.push(Qt.resolvedUrl("BoxInfoPage.qml"), {wifiSetup: root.wifiSetup}) } } HeaderButton { imageSource: "../images/settings.svg" onClicked: { - pageStack.push(Qt.resolvedUrl("NetworkSettingsPage.qml"), {networkManagerController: root.networkManagerController}) + pageStack.push(Qt.resolvedUrl("NetworkSettingsPage.qml"), {wifiSetup: root.wifiSetup}) } } } @@ -69,14 +69,26 @@ Page { Layout.fillHeight: true model: WirelessAccessPointsProxy { - accessPoints: networkManagerController.manager.accessPoints + accessPoints: wifiSetup.accessPoints } clip: true + Timer { + interval: 5000 + repeat: true + onTriggered: wifiSetup.scanWiFi() + running: wifiSetup.accessPoints.count === 0 + } + + BusyIndicator { + anchors.centerIn: parent + visible: wifiSetup.accessPoints.count === 0 + running: visible + } + delegate: NymeaSwipeDelegate { width: parent.width text: model.ssid !== "" ? model.ssid : qsTr("Hidden Network") - enabled: !networkManagerController.manager.working subText: model.hostAddress iconColor: model.selectedNetwork ? Style.accentColor : "#808080" @@ -136,16 +148,16 @@ Page { property string macAddress Connections { - target: root.networkManagerController.manager + target: root.wifiSetup onCurrentConnectionChanged: { - if (root.networkManagerController.manager.currentConnection && root.networkManagerController.manager.currentConnection.ssid === authenticationPage.ssid) { + if (root.wifiSetup.currentConnection && root.wifiSetup.currentConnection.ssid === authenticationPage.ssid) { print("**** connected!") root.connected(); } } onWirelessStatusChanged: { - print("Wireless status changed:", networkManagerController.manager.networkStatus) - if (networkManagerController.manager.wirelessStatus === WirelessSetupManager.WirelessStatusFailed) { + print("Wireless status changed:", wifiSetup.networkStatus) + if (wifiSetup.wirelessStatus === BtWiFiSetup.WirelessStatusFailed) { wrongPasswordText.visible = true pageStack.pop(authenticationPage) } @@ -199,7 +211,7 @@ Page { text: qsTr("OK") enabled: passwordTextField.displayText.length >= 8 onClicked: { - root.networkManagerController.manager.connectWirelessNetwork(authenticationPage.ssid, passwordTextField.text) + root.wifiSetup.connectDeviceToWiFi(authenticationPage.ssid, passwordTextField.text) pageStack.push(connectingWifiWaitPageComponent, {ssid: authenticationPage.ssid }) } } diff --git a/nymea-app/ui/connection/wifisetup/NetworkSettingsPage.qml b/nymea-app/ui/connection/wifisetup/NetworkSettingsPage.qml index 27eef97a..fca42e47 100644 --- a/nymea-app/ui/connection/wifisetup/NetworkSettingsPage.qml +++ b/nymea-app/ui/connection/wifisetup/NetworkSettingsPage.qml @@ -41,7 +41,7 @@ Page { onBackPressed: pageStack.pop() } - property var networkManagerController: null + property BtWiFiSetup wifiSetup: null ColumnLayout { anchors { left: parent.left; top: parent.top; right: parent.right } @@ -49,17 +49,17 @@ Page { SwitchDelegate { Layout.fillWidth: true text: qsTr("Networking") - checked: networkManagerController.manager.networkingEnabled - onClicked: networkManagerController.manager.enableNetworking(checked) + checked: wifiSetup.networkingEnabled + onClicked: wifiSetup.networkingEnabled = checked } SwitchDelegate { Layout.fillWidth: true - enabled: networkManagerController.manager.networkingEnabled + enabled: wifiSetup.networkingEnabled text: qsTr("Wireless network") - checked: networkManagerController.manager.wirelessEnabled + checked: wifiSetup.wirelessEnabled onClicked: { - networkManagerController.manager.enableWireless(checked) + wifiSetup.wirelessEnabled = checked } } @@ -68,7 +68,7 @@ Page { Layout.leftMargin: app.margins Layout.rightMargin: app.margins text: qsTr("Trigger a wireless scan on the device.") - onClicked: networkManagerController.manager.performWifiScan() + onClicked: wifiSetup.scanWiFi() } } } diff --git a/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml b/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml index 00b0943d..abb1ce5a 100644 --- a/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml +++ b/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml @@ -37,8 +37,7 @@ import Nymea 1.0 Page { id: root - property var networkManagerController: null - property var nymeaDiscovery: null + property BtWiFiSetup wifiSetup: null signal done() @@ -51,25 +50,24 @@ Page { HeaderButton { imageSource: "../images/info.svg" onClicked: { - pageStack.push(Qt.resolvedUrl("BoxInfoPage.qml"), {networkManagerController: root.networkManagerController}) + pageStack.push(Qt.resolvedUrl("BoxInfoPage.qml"), {wifiSetup: root.wifiSetup}) } } HeaderButton { imageSource: "../images/settings.svg" onClicked: { - pageStack.push(Qt.resolvedUrl("NetworkSettingsPage.qml"), {networkManagerController: root.networkManagerController}) + pageStack.push(Qt.resolvedUrl("NetworkSettingsPage.qml"), {wifiSetup: root.wifiSetup}) } } } Component.onCompleted: { - print("created with networkmanagercontroller:", root.networkManagerController) updateConnectButton(); } Connections { - target: root.networkManagerController.manager - onErrorOccurred: { + target: root.wifiSetup + onWifiSetupError: { print("Error occurred", errorMessage) var errorDialog = Qt.createComponent(Qt.resolvedUrl("../components/ErrorDialog.qml")); var popup = errorDialog.createObject(app, {text: errorMessage}) @@ -82,25 +80,25 @@ Page { } Connections { - target: root.nymeadiscovery.nymeaHosts + target: nymeaDiscovery.nymeaHosts onCountChanged: updateConnectButton(); } function updateConnectButton() { - if (!root.networkManagerController.manager.currentConnection) { + if (!root.wifiSetup.currentConnection) { connectButton.url = ""; return; } // FIXME: We should rather look for the UUID here, but nymea-networkmanager doesn't support getting us the nymea uuid (yet) - for (var i = 0; i < root.nymeadiscovery.nymeaHosts.count; i++) { - for (var j = 0; j < root.nymeadiscovery.nymeaHosts.get(i).connections.count; j++) { - if (root.nymeadiscovery.nymeaHosts.get(i).connections.get(j).url.toString().indexOf(root.networkManagerController.manager.currentConnection.hostAddress) >= 0) { - connectButton.url = root.nymeadiscovery.nymeaHosts.get(i).connections.get(j).url + for (var i = 0; i < nymeaDiscovery.nymeaHosts.count; i++) { + for (var j = 0; j < nymeaDiscovery.nymeaHosts.get(i).connections.count; j++) { + if (nymeaDiscovery.nymeaHosts.get(i).connections.get(j).url.toString().indexOf(root.wifiSetup.currentConnection.hostAddress) >= 0) { + connectButton.host = nymeaDiscovery.nymeaHosts.get(i) return; } } - root.nymeadiscovery.nymeaHosts.get(i).connections.countChanged.connect(function() { + nymeaDiscovery.nymeaHosts.get(i).connections.countChanged.connect(function() { updateConnectButton(); }) } @@ -121,15 +119,15 @@ Page { Layout.fillWidth: true Layout.leftMargin: app.margins; Layout.rightMargin: app.margins wrapMode: Text.WordWrap - text: root.networkManagerController.manager.currentConnection - ? qsTr("Your %1:core is connected to %2").arg(app.systemName).arg(root.networkManagerController.manager.currentConnection.ssid) + text: root.wifiSetup.currentConnection + ? qsTr("Your %1:core is connected to %2").arg(app.systemName).arg(root.wifiSetup.currentConnection.ssid) : "" } Label { Layout.fillWidth: true Layout.leftMargin: app.margins; Layout.rightMargin: app.margins - text: qsTr("IP address: %1").arg(root.networkManagerController.manager.currentConnection.hostAddress) + text: qsTr("IP address: %1").arg(root.wifiSetup.currentConnection.hostAddress) elide: Text.ElideRight } @@ -147,13 +145,14 @@ Page { Button { id: connectButton - visible: url != "" + visible: host !== null Layout.fillWidth: true Layout.leftMargin: app.margins; Layout.rightMargin: app.margins text: qsTr("Connect to %1:core").arg(app.systemName) - property string url + property NymeaHost host: null onClicked: { - engine.jsonRpcClient.connectToHost(url) + print("connecting to", host) + engine.jsonRpcClient.connectToHost(host) } } @@ -162,7 +161,7 @@ Page { Layout.leftMargin: app.margins; Layout.rightMargin: app.margins text: qsTr("Change network") onClicked: { - var page = pageStack.push(Qt.resolvedUrl("ConnectWiFiPage.qml"), {networkManagerController: root.networkManagerController}) + var page = pageStack.push(Qt.resolvedUrl("ConnectWiFiPage.qml"), {wifiSetup: root.wifiSetup}) page.connected.connect(function() { pageStack.pop(root) }) From 14fe5bb610b3df8cc89ad183555c661ad75afe69 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 12 Jan 2021 19:10:45 +0100 Subject: [PATCH 6/9] Some more finishing touches --- libnymea-app/wifisetup/btwifisetup.cpp | 59 +++++++++++-------- libnymea-app/wifisetup/btwifisetup.h | 79 +++++++++++++------------- 2 files changed, 73 insertions(+), 65 deletions(-) diff --git a/libnymea-app/wifisetup/btwifisetup.cpp b/libnymea-app/wifisetup/btwifisetup.cpp index 849b8a65..8cb3107f 100644 --- a/libnymea-app/wifisetup/btwifisetup.cpp +++ b/libnymea-app/wifisetup/btwifisetup.cpp @@ -36,8 +36,8 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) m_currentConnection = nullptr; currentConnectionChanged(); m_accessPoints->clearModel(); - m_status = StatusDisconnected; - emit statusChanged(m_status); + m_bluetoothStatus = BluetoothStatusDisconnected; + emit bluetoothStatusChanged(m_bluetoothStatus); } @@ -45,12 +45,14 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) connect(m_btController, &QLowEnergyController::connected, this, [this](){ qDebug() << "Bluetooth connected"; m_btController->discoverServices(); + m_bluetoothStatus = BluetoothStatusConnectedToBluetooth; + emit bluetoothStatusChanged(m_bluetoothStatus); }); connect(m_btController, &QLowEnergyController::disconnected, this, [this](){ qDebug() << "Bluetooth disconnected"; - m_status = StatusDisconnected; - emit statusChanged(m_status); + m_bluetoothStatus = BluetoothStatusDisconnected; + emit bluetoothStatusChanged(m_bluetoothStatus); m_btController->deleteLater(); m_btController = nullptr; m_currentConnection = nullptr; @@ -69,8 +71,8 @@ void BtWiFiSetup::connectToDevice(const BluetoothDeviceInfo *device) setupServices(); }); - m_status = StatusConnectingToBluetooth; - emit statusChanged(m_status); + m_bluetoothStatus = BluetoothStatusConnectingToBluetooth; + emit bluetoothStatusChanged(m_bluetoothStatus); m_btController->connectToDevice(); } @@ -83,8 +85,8 @@ void BtWiFiSetup::disconnectFromDevice() void BtWiFiSetup::connectDeviceToWiFi(const QString &ssid, const QString &password) { - if (m_status != StatusConnectedToBluetooth) { - qWarning() << "Cannot connect to wifi in state" << m_status; + if (m_bluetoothStatus != BluetoothStatusConnectedToBluetooth) { + qWarning() << "Cannot connect to wifi in state" << m_bluetoothStatus; } QVariantMap request; @@ -98,10 +100,9 @@ void BtWiFiSetup::connectDeviceToWiFi(const QString &ssid, const QString &passwo void BtWiFiSetup::disconnectDeviceFromWiFi() { - if (m_status != StatusConnectedToWiFi) { - qWarning() << "Cannot disconnect from wifi in state" << m_status; + if (m_bluetoothStatus != BluetoothStatusConnectedToBluetooth) { + qWarning() << "Cannot disconnect from wifi in state" << m_bluetoothStatus; } - QLowEnergyCharacteristic ssidCharacteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); QVariantMap request; request.insert("c", (int)WirelessServiceCommandDisconnect); streamData(m_wifiService, wifiCommanderCharacteristicUuid, request); @@ -109,28 +110,30 @@ void BtWiFiSetup::disconnectDeviceFromWiFi() void BtWiFiSetup::scanWiFi() { - QLowEnergyCharacteristic characteristic = m_wifiService->characteristic(wifiCommanderCharacteristicUuid); + if (m_bluetoothStatus != BluetoothStatusConnectedToBluetooth) { + qWarning() << "Cannot disconnect from wifi in state" << m_bluetoothStatus; + } QVariantMap request; request.insert("c", (int)WirelessServiceCommandScan); streamData(m_wifiService, wifiCommanderCharacteristicUuid, request); } -void BtWiFiSetup::pressPushButton() +bool BtWiFiSetup::pressPushButton() { if (!m_systemService) { qDebug() << "System service not available. Cannot perform push button pairing"; - return; + return false; } - QLowEnergyCharacteristic commanderCharacteristic = m_systemService->characteristic(systemCommanderCharacteristicUuid); QVariantMap request; request.insert("c", (int)SystemServiceCommandPushAuthentication); streamData(m_systemService, systemCommanderCharacteristicUuid, request); + return true; } -BtWiFiSetup::Status BtWiFiSetup::status() const +BtWiFiSetup::BluetoothStatus BtWiFiSetup::bluetoothStatus() const { - return m_status; + return m_bluetoothStatus; } QString BtWiFiSetup::modelNumber() const @@ -175,6 +178,9 @@ bool BtWiFiSetup::networkingEnabled() const void BtWiFiSetup::setNetworkingEnabled(bool networkingEnabled) { + if (m_bluetoothStatus != BluetoothStatusConnectedToBluetooth) { + qWarning() << "Cannot disconnect from wifi in state" << m_bluetoothStatus; + } QLowEnergyCharacteristic characteristic = m_networkService->characteristic(networkCommanderCharacteristicUuid); m_networkService->writeCharacteristic(characteristic, networkingEnabled ? QByteArray::fromHex("00") : QByteArray::fromHex("01")); } @@ -186,6 +192,9 @@ bool BtWiFiSetup::wirelessEnabled() const void BtWiFiSetup::setWirelessEnabled(bool wirelessEnabled) const { + if (m_bluetoothStatus != BluetoothStatusConnectedToBluetooth) { + qWarning() << "Cannot disconnect from wifi in state" << m_bluetoothStatus; + } QLowEnergyCharacteristic characteristic = m_networkService->characteristic(networkCommanderCharacteristicUuid); m_networkService->writeCharacteristic(characteristic, wirelessEnabled ? QByteArray::fromHex("02") : QByteArray::fromHex("03")); } @@ -202,7 +211,7 @@ WirelessAccessPoint *BtWiFiSetup::currentConnection() const void BtWiFiSetup::setupServices() { - qDebug() << "Setting up services"; + qDebug() << "Setting up Bluetooth services"; m_deviceInformationService = m_btController->createServiceObject(QBluetoothUuid::DeviceInformation, m_btController); m_networkService = m_btController->createServiceObject(networkServiceUuid, m_btController); m_wifiService = m_btController->createServiceObject(wifiServiceUuid, m_btController); @@ -268,8 +277,6 @@ void BtWiFiSetup::setupServices() return; qDebug() << "Wifi service discovered"; - m_status = StatusConnectedToBluetooth; - emit statusChanged(m_status); // Enable notifations m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiResponseCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100")); @@ -345,11 +352,15 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) break; case WirelessServiceCommandConnect: qDebug() << "Connect call succeeded"; - m_status = StatusConnectingToWiFi; - emit statusChanged(m_status); break; case WirelessServiceCommandGetCurrentConnection: // Find current network + if (!data.value("p").toMap().value("m").toString().isEmpty() && data.value("p").toMap().value("i").toString().isEmpty()) { + // There's a bug in libnymea-networkmanager that sometimes it emits current connection before it actually obtained the IP address + qDebug() << "Retring to fetch the current connection because IP is not set yet."; + loadCurrentConnection(); + return; + } m_currentConnection = nullptr; foreach (WirelessAccessPoint *accessPoint, m_accessPoints->wirelessAccessPoints()) { QVariantMap currentConnection = data.value("p").toMap(); @@ -357,6 +368,7 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) if (accessPoint->macAddress() == macAddress) { // Set the current network m_currentConnection = accessPoint; + accessPoint->setHostAddress(currentConnection.value("i").toString()); } } @@ -428,9 +440,6 @@ void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characte m_networkStatus = static_cast(value.toHex().toInt(nullptr, 16)); qDebug() << "Network status changed:" << m_networkStatus; if (m_networkStatus == NetworkStatusGlobal) { - m_status = StatusConnectedToWiFi; - emit statusChanged(m_status); - loadCurrentConnection(); } diff --git a/libnymea-app/wifisetup/btwifisetup.h b/libnymea-app/wifisetup/btwifisetup.h index 49b8f778..bfbf316c 100644 --- a/libnymea-app/wifisetup/btwifisetup.h +++ b/libnymea-app/wifisetup/btwifisetup.h @@ -12,7 +12,7 @@ class WirelessAccessPoint; class BtWiFiSetup : public QObject { Q_OBJECT - Q_PROPERTY(Status status READ status NOTIFY statusChanged) + Q_PROPERTY(BluetoothStatus bluetoothStatus READ bluetoothStatus NOTIFY bluetoothStatusChanged) Q_PROPERTY(QString modelNumber READ modelNumber NOTIFY modelNumberChanged) Q_PROPERTY(QString manufacturer READ manufacturer NOTIFY manufacturerChanged) @@ -29,14 +29,41 @@ class BtWiFiSetup : public QObject Q_PROPERTY(WirelessAccessPoint *currentConnection READ currentConnection NOTIFY currentConnectionChanged) public: - enum Status { - StatusDisconnected, - StatusConnectingToBluetooth, - StatusConnectedToBluetooth, - StatusConnectingToWiFi, - StatusConnectedToWiFi + enum BluetoothStatus { + BluetoothStatusDisconnected, + BluetoothStatusConnectingToBluetooth, + BluetoothStatusConnectedToBluetooth }; - Q_ENUM(Status) + Q_ENUM(BluetoothStatus) + + enum NetworkStatus { + NetworkStatusUnknown = 0x00, + NetworkStatusAsleep = 0x01, + NetworkStatusDisconnected = 0x02, + NetworkStatusDisconnecting = 0x03, + NetworkStatusConnecting = 0x04, + NetworkStatusLocal = 0x05, + NetworkStatusConnectedSite = 0x06, + NetworkStatusGlobal = 0x07 + }; + Q_ENUM(NetworkStatus) + + enum WirelessStatus { + WirelessStatusUnknown = 0x00, + WirelessStatusUnmanaged = 0x01, + WirelessStatusUnavailable = 0x02, + WirelessStatusDisconnected = 0x03, + WirelessStatusPrepare = 0x04, + WirelessStatusConfig = 0x05, + WirelessStatusNeedAuth = 0x06, + WirelessStatusIpConfig = 0x07, + WirelessStatusIpCheck = 0x08, + WirelessStatusSecondaries = 0x09, + WirelessStatusActivated = 0x0A, + WirelessStatusDeactivating = 0x0B, + WirelessStatusFailed = 0x0C + }; + Q_ENUM(WirelessStatus) enum WirelessServiceCommand { WirelessServiceCommandInvalid = -1, @@ -94,34 +121,6 @@ public: }; Q_ENUM(SystemServiceResponse) - enum NetworkStatus { - NetworkStatusUnknown = 0x00, - NetworkStatusAsleep = 0x01, - NetworkStatusDisconnected = 0x02, - NetworkStatusDisconnecting = 0x03, - NetworkStatusConnecting = 0x04, - NetworkStatusLocal = 0x05, - NetworkStatusConnectedSite = 0x06, - NetworkStatusGlobal = 0x07 - }; - Q_ENUM(NetworkStatus) - - enum WirelessStatus { - WirelessStatusUnknown = 0x00, - WirelessStatusUnmanaged = 0x01, - WirelessStatusUnavailable = 0x02, - WirelessStatusDisconnected = 0x03, - WirelessStatusPrepare = 0x04, - WirelessStatusConfig = 0x05, - WirelessStatusNeedAuth = 0x06, - WirelessStatusIpConfig = 0x07, - WirelessStatusIpCheck = 0x08, - WirelessStatusSecondaries = 0x09, - WirelessStatusActivated = 0x0A, - WirelessStatusDeactivating = 0x0B, - WirelessStatusFailed = 0x0C - }; - Q_ENUM(WirelessStatus) explicit BtWiFiSetup(QObject *parent = nullptr); @@ -130,9 +129,9 @@ public: Q_INVOKABLE void connectDeviceToWiFi(const QString &ssid, const QString &password); Q_INVOKABLE void disconnectDeviceFromWiFi(); Q_INVOKABLE void scanWiFi(); - Q_INVOKABLE void pressPushButton(); + Q_INVOKABLE bool pressPushButton(); - Status status() const; + BluetoothStatus bluetoothStatus() const; QString modelNumber() const; QString manufacturer() const; @@ -152,7 +151,7 @@ public: WirelessAccessPoint *currentConnection() const; signals: - void statusChanged(Status status); + void bluetoothStatusChanged(BluetoothStatus status); void bluetoothConnectionError(); void wifiSetupError(); @@ -181,7 +180,7 @@ private slots: void characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); private: - Status m_status = StatusDisconnected; + BluetoothStatus m_bluetoothStatus = BluetoothStatusDisconnected; QLowEnergyController *m_btController = nullptr; QLowEnergyService *m_deviceInformationService = nullptr; From 94483e92c5a8d50faeb0d580c37ba84b2dafb3b4 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Wed, 13 Jan 2021 12:34:33 +0100 Subject: [PATCH 7/9] Update UI to latest changes --- libnymea-app/wifisetup/btwifisetup.cpp | 10 +++++++++- libnymea-app/wifisetup/btwifisetup.h | 3 ++- .../wifisetup/BluetoothDiscoveryPage.qml | 18 ++++++++++-------- .../connection/wifisetup/WirelessSetupPage.qml | 4 ++-- .../ThingClassDetailsPage.qml | 4 ++++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/libnymea-app/wifisetup/btwifisetup.cpp b/libnymea-app/wifisetup/btwifisetup.cpp index 8cb3107f..d4bcc187 100644 --- a/libnymea-app/wifisetup/btwifisetup.cpp +++ b/libnymea-app/wifisetup/btwifisetup.cpp @@ -348,7 +348,9 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) accessPoint->setProtected(data.toMap().value("p").toBool()); accessPoint->setHostAddress(""); m_accessPoints->addWirelessAccessPoint(accessPoint); + } + loadCurrentConnection(); break; case WirelessServiceCommandConnect: qDebug() << "Connect call succeeded"; @@ -374,6 +376,12 @@ void BtWiFiSetup::processWiFiPacket(const QVariantMap &data) } qDebug() << "current connection is:" << m_currentConnection; emit currentConnectionChanged(); + + if (m_bluetoothStatus != BluetoothStatusLoaded) { + m_bluetoothStatus = BluetoothStatusLoaded; + emit bluetoothStatusChanged(m_bluetoothStatus); + } + break; case WirelessServiceCommandScan: if (responseCode == WirelessServiceResponseSuccess) { @@ -439,7 +447,7 @@ void BtWiFiSetup::characteristicChanged(const QLowEnergyCharacteristic &characte } else if (characteristic.uuid() == networkStatusCharacteristicUuid) { m_networkStatus = static_cast(value.toHex().toInt(nullptr, 16)); qDebug() << "Network status changed:" << m_networkStatus; - if (m_networkStatus == NetworkStatusGlobal) { + if (m_networkStatus == NetworkStatusGlobal || m_networkStatus == NetworkStatusLocal || m_networkStatus == NetworkStatusConnectedSite) { loadCurrentConnection(); } diff --git a/libnymea-app/wifisetup/btwifisetup.h b/libnymea-app/wifisetup/btwifisetup.h index bfbf316c..ac23e4d8 100644 --- a/libnymea-app/wifisetup/btwifisetup.h +++ b/libnymea-app/wifisetup/btwifisetup.h @@ -32,7 +32,8 @@ public: enum BluetoothStatus { BluetoothStatusDisconnected, BluetoothStatusConnectingToBluetooth, - BluetoothStatusConnectedToBluetooth + BluetoothStatusConnectedToBluetooth, + BluetoothStatusLoaded, }; Q_ENUM(BluetoothStatus) diff --git a/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml b/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml index c8ef605d..bd24fa24 100644 --- a/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml +++ b/nymea-app/ui/connection/wifisetup/BluetoothDiscoveryPage.qml @@ -49,15 +49,17 @@ Page { BtWiFiSetup { id: wifiSetup - onStatusChanged: { + onBluetoothStatusChanged: { print("status changed", status) switch (status) { - case BtWiFiSetup.StatusDisconnected: + case BtWiFiSetup.BluetoothStatusDisconnected: pageStack.pop(root) break; - case BtWiFiSetup.StatusConnectingToBluetooth: + case BtWiFiSetup.BluetoothStatusConnectingToBluetooth: break; - case BtWiFiSetup.StatusConnectedToBluetooth: + case BtWiFiSetup.BluetoothStatusConnectedToBluetooth: + break; + case BtWiFiSetup.BluetoothStatusLoaded: if (!wifiSetup.networkingEnabled) { wifiSetup.networkingEnabled = true; } @@ -66,12 +68,12 @@ Page { } setupDevice() break; - case BtWiFiSetup.StatusConnectingToWiFi: - break; - case BtWiFiSetup.StatusConnectedToWiFi: - print("Connected to wifi!") } } + onWirelessStatusChanged: { + + } + onBluetoothConnectionError: { print("Error") pageStack.pop(root) diff --git a/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml b/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml index abb1ce5a..8aa9159f 100644 --- a/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml +++ b/nymea-app/ui/connection/wifisetup/WirelessSetupPage.qml @@ -86,7 +86,7 @@ Page { function updateConnectButton() { if (!root.wifiSetup.currentConnection) { - connectButton.url = ""; + connectButton.host = null; return; } @@ -102,7 +102,7 @@ Page { updateConnectButton(); }) } - connectButton.url = ""; + connectButton.host = null; } ColumnLayout { diff --git a/nymea-app/ui/thingconfiguration/ThingClassDetailsPage.qml b/nymea-app/ui/thingconfiguration/ThingClassDetailsPage.qml index 75fbbe88..fdc08bbe 100644 --- a/nymea-app/ui/thingconfiguration/ThingClassDetailsPage.qml +++ b/nymea-app/ui/thingconfiguration/ThingClassDetailsPage.qml @@ -54,6 +54,10 @@ SettingsPageBase { text: root.thingClass.displayName subText: root.thingClass.id.toString().replace(/[{}]/g, "") progressive: false + onClicked: { + PlatformHelper.toClipBoard(subText); + ToolTip.show(qsTr("ID copied to clipboard"), 500); + } } SettingsPageSectionHeader { From 9a08e50ae335e29a600f01f5f70d917c6db40e9a Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Wed, 13 Jan 2021 12:11:01 +0100 Subject: [PATCH 8/9] Rename DeviceDiscovery to ThingDiscovery --- libnymea-app/libnymea-app-core.h | 6 ++-- libnymea-app/libnymea-app.pri | 4 +-- ...devicediscovery.cpp => thingdiscovery.cpp} | 36 +++++++++---------- .../{devicediscovery.h => thingdiscovery.h} | 22 ++++++------ 4 files changed, 34 insertions(+), 34 deletions(-) rename libnymea-app/{devicediscovery.cpp => thingdiscovery.cpp} (87%) rename libnymea-app/{devicediscovery.h => thingdiscovery.h} (87%) diff --git a/libnymea-app/libnymea-app-core.h b/libnymea-app/libnymea-app-core.h index 2d539388..f71165c0 100644 --- a/libnymea-app/libnymea-app-core.h +++ b/libnymea-app/libnymea-app-core.h @@ -39,7 +39,7 @@ #include "deviceclassesproxy.h" #include "devicesproxy.h" #include "pluginsproxy.h" -#include "devicediscovery.h" +#include "thingdiscovery.h" #include "interfacesmodel.h" #include "rulemanager.h" #include "models/rulesfiltermodel.h" @@ -199,8 +199,8 @@ void registerQmlTypes() { qmlRegisterUncreatableType(uri, 1, 0, "DeviceClass", "Can't create this in QML. Get it from the DeviceClasses."); qmlRegisterUncreatableType(uri, 1, 0, "DeviceClasses", "Can't create this in QML. Get it from the DeviceManager."); qmlRegisterType(uri, 1, 0, "DeviceClassesProxy"); - qmlRegisterType(uri, 1, 0, "DeviceDiscovery"); - qmlRegisterType(uri, 1, 0, "ThingDiscovery"); + qmlRegisterType(uri, 1, 0, "DeviceDiscovery"); + qmlRegisterType(uri, 1, 0, "ThingDiscovery"); qmlRegisterType(uri, 1, 0, "DeviceDiscoveryProxy"); qmlRegisterType(uri, 1, 0, "ThingDiscoveryProxy"); qmlRegisterUncreatableType(uri, 1, 0, "DeviceDescriptor", "Get it from DeviceDiscovery"); diff --git a/libnymea-app/libnymea-app.pri b/libnymea-app/libnymea-app.pri index 7f82e210..3cec8124 100644 --- a/libnymea-app/libnymea-app.pri +++ b/libnymea-app/libnymea-app.pri @@ -115,7 +115,7 @@ SOURCES += \ $${PWD}/devicesproxy.cpp \ $${PWD}/deviceclasses.cpp \ $${PWD}/deviceclassesproxy.cpp \ - $${PWD}/devicediscovery.cpp \ + $${PWD}/thingdiscovery.cpp \ $${PWD}/models/packagesfiltermodel.cpp \ $${PWD}/models/taglistmodel.cpp \ $${PWD}/scripting/codecompletion.cpp \ @@ -260,7 +260,7 @@ HEADERS += \ $${PWD}/devicesproxy.h \ $${PWD}/deviceclasses.h \ $${PWD}/deviceclassesproxy.h \ - $${PWD}/devicediscovery.h \ + $${PWD}/thingdiscovery.h \ $${PWD}/models/packagesfiltermodel.h \ $${PWD}/models/taglistmodel.h \ $${PWD}/scripting/codecompletion.h \ diff --git a/libnymea-app/devicediscovery.cpp b/libnymea-app/thingdiscovery.cpp similarity index 87% rename from libnymea-app/devicediscovery.cpp rename to libnymea-app/thingdiscovery.cpp index 8576ee45..5cf3dcf5 100644 --- a/libnymea-app/devicediscovery.cpp +++ b/libnymea-app/thingdiscovery.cpp @@ -28,22 +28,22 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include "devicediscovery.h" +#include "thingdiscovery.h" #include "engine.h" -DeviceDiscovery::DeviceDiscovery(QObject *parent) : +ThingDiscovery::ThingDiscovery(QObject *parent) : QAbstractListModel(parent) { } -int DeviceDiscovery::rowCount(const QModelIndex &parent) const +int ThingDiscovery::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent) return m_foundDevices.count(); } -QVariant DeviceDiscovery::data(const QModelIndex &index, int role) const +QVariant ThingDiscovery::data(const QModelIndex &index, int role) const { switch (role) { case RoleId: @@ -59,7 +59,7 @@ QVariant DeviceDiscovery::data(const QModelIndex &index, int role) const return QVariant(); } -QHash DeviceDiscovery::roleNames() const +QHash ThingDiscovery::roleNames() const { QHash roles; roles.insert(RoleId, "id"); @@ -69,7 +69,7 @@ QHash DeviceDiscovery::roleNames() const return roles; } -void DeviceDiscovery::discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams) +void ThingDiscovery::discoverThings(const QUuid &deviceClassId, const QVariantList &discoveryParams) { if (m_busy) { qWarning() << "Busy... not restarting discovery"; @@ -94,13 +94,13 @@ void DeviceDiscovery::discoverDevices(const QUuid &deviceClassId, const QVariant if (!discoveryParams.isEmpty()) { params.insert("discoveryParams", discoveryParams); } - m_engine->jsonRpcClient()->sendCommand("Devices.GetDiscoveredDevices", params, this, "discoverDevicesResponse"); + m_engine->jsonRpcClient()->sendCommand("Devices.GetDiscoveredDevices", params, this, "discoverThingsResponse"); m_busy = true; m_displayMessage.clear(); emit busyChanged(); } -DeviceDescriptor *DeviceDiscovery::get(int index) const +DeviceDescriptor *ThingDiscovery::get(int index) const { if (index < 0 || index >= m_foundDevices.count()) { return nullptr; @@ -108,12 +108,12 @@ DeviceDescriptor *DeviceDiscovery::get(int index) const return m_foundDevices.at(index); } -Engine *DeviceDiscovery::engine() const +Engine *ThingDiscovery::engine() const { return m_engine; } -void DeviceDiscovery::setEngine(Engine *engine) +void ThingDiscovery::setEngine(Engine *engine) { if (m_engine != engine) { m_engine = engine; @@ -121,19 +121,19 @@ void DeviceDiscovery::setEngine(Engine *engine) } } -bool DeviceDiscovery::busy() const +bool ThingDiscovery::busy() const { return m_busy; } -QString DeviceDiscovery::displayMessage() const +QString ThingDiscovery::displayMessage() const { return m_displayMessage; } -void DeviceDiscovery::discoverDevicesResponse(int /*commandId*/, const QVariantMap ¶ms) +void ThingDiscovery::discoverThingsResponse(int /*commandId*/, const QVariantMap ¶ms) { -// qDebug() << "response received" << params; + qDebug() << "response received" << params; QVariantList descriptors = params.value("deviceDescriptors").toList(); foreach (const QVariant &descriptorVariant, descriptors) { qDebug() << "Found device. Descriptor:" << descriptorVariant; @@ -159,7 +159,7 @@ void DeviceDiscovery::discoverDevicesResponse(int /*commandId*/, const QVariantM emit busyChanged(); } -bool DeviceDiscovery::contains(const QUuid &deviceDescriptorId) const +bool ThingDiscovery::contains(const QUuid &deviceDescriptorId) const { foreach (DeviceDescriptor *descriptor, m_foundDevices) { if (descriptor->id() == deviceDescriptorId) { @@ -211,19 +211,19 @@ DeviceDiscoveryProxy::DeviceDiscoveryProxy(QObject *parent): } -DeviceDiscovery *DeviceDiscoveryProxy::deviceDiscovery() const +ThingDiscovery *DeviceDiscoveryProxy::deviceDiscovery() const { return m_deviceDiscovery; } -void DeviceDiscoveryProxy::setDeviceDiscovery(DeviceDiscovery *deviceDiscovery) +void DeviceDiscoveryProxy::setDeviceDiscovery(ThingDiscovery *deviceDiscovery) { if (m_deviceDiscovery != deviceDiscovery) { m_deviceDiscovery = deviceDiscovery; setSourceModel(deviceDiscovery); emit deviceDiscoveryChanged(); emit countChanged(); - connect(m_deviceDiscovery, &DeviceDiscovery::countChanged, this, &DeviceDiscoveryProxy::countChanged); + connect(m_deviceDiscovery, &ThingDiscovery::countChanged, this, &DeviceDiscoveryProxy::countChanged); invalidateFilter(); } } diff --git a/libnymea-app/devicediscovery.h b/libnymea-app/thingdiscovery.h similarity index 87% rename from libnymea-app/devicediscovery.h rename to libnymea-app/thingdiscovery.h index 95e014aa..b70e724a 100644 --- a/libnymea-app/devicediscovery.h +++ b/libnymea-app/thingdiscovery.h @@ -28,8 +28,8 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#ifndef DEVICEDISCOVERY_H -#define DEVICEDISCOVERY_H +#ifndef THINGDISCOVERY_H +#define THINGDISCOVERY_H #include #include @@ -60,7 +60,7 @@ private: Params *m_params = nullptr; }; -class DeviceDiscovery : public QAbstractListModel +class ThingDiscovery : public QAbstractListModel { Q_OBJECT Q_PROPERTY(Engine* engine READ engine WRITE setEngine) @@ -75,14 +75,14 @@ public: RoleDescription }; - DeviceDiscovery(QObject *parent = nullptr); + ThingDiscovery(QObject *parent = nullptr); int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role) const override; QHash roleNames() const override; - Q_INVOKABLE void discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams = {}); + Q_INVOKABLE void discoverThings(const QUuid &thingClassId, const QVariantList &discoveryParams = {}); Q_INVOKABLE DeviceDescriptor* get(int index) const; @@ -93,7 +93,7 @@ public: QString displayMessage() const; private slots: - void discoverDevicesResponse(int commandId, const QVariantMap ¶ms); + void discoverThingsResponse(int commandId, const QVariantMap ¶ms); signals: void busyChanged(); @@ -113,7 +113,7 @@ class DeviceDiscoveryProxy: public QSortFilterProxyModel { Q_OBJECT Q_PROPERTY(int count READ rowCount NOTIFY countChanged) - Q_PROPERTY(DeviceDiscovery* deviceDiscovery READ deviceDiscovery WRITE setDeviceDiscovery NOTIFY deviceDiscoveryChanged) + Q_PROPERTY(ThingDiscovery* deviceDiscovery READ deviceDiscovery WRITE setDeviceDiscovery NOTIFY deviceDiscoveryChanged) Q_PROPERTY(bool showAlreadyAdded READ showAlreadyAdded WRITE setShowAlreadyAdded NOTIFY showAlreadyAddedChanged) Q_PROPERTY(bool showNew READ showNew WRITE setShowNew NOTIFY showNewChanged) Q_PROPERTY(QUuid filterDeviceId READ filterDeviceId WRITE setFilterDeviceId NOTIFY filterDeviceIdChanged) @@ -121,8 +121,8 @@ class DeviceDiscoveryProxy: public QSortFilterProxyModel public: DeviceDiscoveryProxy(QObject *parent = nullptr); - DeviceDiscovery* deviceDiscovery() const; - void setDeviceDiscovery(DeviceDiscovery* deviceDiscovery); + ThingDiscovery* deviceDiscovery() const; + void setDeviceDiscovery(ThingDiscovery* deviceDiscovery); bool showAlreadyAdded() const; void setShowAlreadyAdded(bool showAlreadyAdded); @@ -146,10 +146,10 @@ protected: bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; private: - DeviceDiscovery* m_deviceDiscovery = nullptr; + ThingDiscovery* m_deviceDiscovery = nullptr; bool m_showAlreadyAdded = false; bool m_showNew = true; QUuid m_filterDeviceId; }; -#endif // DEVICEDISCOVERY_H +#endif // THINGDISCOVERY_H From 76954a14032edc54c9fdb7aba047c8454865aacc Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 14 Jan 2021 00:12:56 +0100 Subject: [PATCH 9/9] complete discovery renaming --- libnymea-app/libnymea-app-core.h | 8 +- libnymea-app/thingdiscovery.cpp | 76 +++++++++---------- libnymea-app/thingdiscovery.h | 40 +++++----- .../ui/thingconfiguration/SetupWizard.qml | 35 +++++---- 4 files changed, 78 insertions(+), 81 deletions(-) diff --git a/libnymea-app/libnymea-app-core.h b/libnymea-app/libnymea-app-core.h index f71165c0..682e49af 100644 --- a/libnymea-app/libnymea-app-core.h +++ b/libnymea-app/libnymea-app-core.h @@ -199,12 +199,10 @@ void registerQmlTypes() { qmlRegisterUncreatableType(uri, 1, 0, "DeviceClass", "Can't create this in QML. Get it from the DeviceClasses."); qmlRegisterUncreatableType(uri, 1, 0, "DeviceClasses", "Can't create this in QML. Get it from the DeviceManager."); qmlRegisterType(uri, 1, 0, "DeviceClassesProxy"); - qmlRegisterType(uri, 1, 0, "DeviceDiscovery"); qmlRegisterType(uri, 1, 0, "ThingDiscovery"); - qmlRegisterType(uri, 1, 0, "DeviceDiscoveryProxy"); - qmlRegisterType(uri, 1, 0, "ThingDiscoveryProxy"); - qmlRegisterUncreatableType(uri, 1, 0, "DeviceDescriptor", "Get it from DeviceDiscovery"); - qmlRegisterUncreatableType(uri, 1, 0, "ThingDescriptor", "Get it from ThingDiscovery"); + qmlRegisterType(uri, 1, 0, "ThingDiscoveryProxy"); + qmlRegisterUncreatableType(uri, 1, 0, "DeviceDescriptor", "Get it from DeviceDiscovery"); + qmlRegisterUncreatableType(uri, 1, 0, "ThingDescriptor", "Get it from ThingDiscovery"); qmlRegisterType(uri, 1, 0, "DeviceModel"); diff --git a/libnymea-app/thingdiscovery.cpp b/libnymea-app/thingdiscovery.cpp index 5cf3dcf5..e380ef9a 100644 --- a/libnymea-app/thingdiscovery.cpp +++ b/libnymea-app/thingdiscovery.cpp @@ -53,7 +53,7 @@ QVariant ThingDiscovery::data(const QModelIndex &index, int role) const case RoleDescription: return m_foundDevices.at(index.row())->description(); case RoleDeviceId: - return m_foundDevices.at(index.row())->deviceId(); + return m_foundDevices.at(index.row())->thingId(); } return QVariant(); @@ -100,7 +100,7 @@ void ThingDiscovery::discoverThings(const QUuid &deviceClassId, const QVariantLi emit busyChanged(); } -DeviceDescriptor *ThingDiscovery::get(int index) const +ThingDescriptor *ThingDiscovery::get(int index) const { if (index < 0 || index >= m_foundDevices.count()) { return nullptr; @@ -139,7 +139,7 @@ void ThingDiscovery::discoverThingsResponse(int /*commandId*/, const QVariantMap qDebug() << "Found device. Descriptor:" << descriptorVariant; if (!contains(descriptorVariant.toMap().value("id").toUuid())) { beginInsertRows(QModelIndex(), m_foundDevices.count(), m_foundDevices.count()); - DeviceDescriptor *descriptor = new DeviceDescriptor(descriptorVariant.toMap().value("id").toUuid(), + ThingDescriptor *descriptor = new ThingDescriptor(descriptorVariant.toMap().value("id").toUuid(), descriptorVariant.toMap().value("deviceId").toString(), descriptorVariant.toMap().value("title").toString(), descriptorVariant.toMap().value("description").toString()); @@ -161,7 +161,7 @@ void ThingDiscovery::discoverThingsResponse(int /*commandId*/, const QVariantMap bool ThingDiscovery::contains(const QUuid &deviceDescriptorId) const { - foreach (DeviceDescriptor *descriptor, m_foundDevices) { + foreach (ThingDescriptor *descriptor, m_foundDevices) { if (descriptor->id() == deviceDescriptorId) { return true; } @@ -169,10 +169,10 @@ bool ThingDiscovery::contains(const QUuid &deviceDescriptorId) const return false; } -DeviceDescriptor::DeviceDescriptor(const QUuid &id, const QUuid &deviceId, const QString &name, const QString &description, QObject *parent): +ThingDescriptor::ThingDescriptor(const QUuid &id, const QUuid &thingId, const QString &name, const QString &description, QObject *parent): QObject(parent), m_id(id), - m_deviceId(deviceId), + m_thingId(thingId), m_name(name), m_description(description), m_params(new Params(this)) @@ -180,60 +180,60 @@ DeviceDescriptor::DeviceDescriptor(const QUuid &id, const QUuid &deviceId, const } -QUuid DeviceDescriptor::id() const +QUuid ThingDescriptor::id() const { return m_id; } -QUuid DeviceDescriptor::deviceId() const +QUuid ThingDescriptor::thingId() const { - return m_deviceId; + return m_thingId; } -QString DeviceDescriptor::name() const +QString ThingDescriptor::name() const { return m_name; } -QString DeviceDescriptor::description() const +QString ThingDescriptor::description() const { return m_description; } -Params* DeviceDescriptor::params() const +Params* ThingDescriptor::params() const { return m_params; } -DeviceDiscoveryProxy::DeviceDiscoveryProxy(QObject *parent): +ThingDiscoveryProxy::ThingDiscoveryProxy(QObject *parent): QSortFilterProxyModel (parent) { } -ThingDiscovery *DeviceDiscoveryProxy::deviceDiscovery() const +ThingDiscovery *ThingDiscoveryProxy::thingDiscovery() const { - return m_deviceDiscovery; + return m_thingDiscovery; } -void DeviceDiscoveryProxy::setDeviceDiscovery(ThingDiscovery *deviceDiscovery) +void ThingDiscoveryProxy::setThingDiscovery(ThingDiscovery *thingDiscovery) { - if (m_deviceDiscovery != deviceDiscovery) { - m_deviceDiscovery = deviceDiscovery; - setSourceModel(deviceDiscovery); - emit deviceDiscoveryChanged(); + if (m_thingDiscovery != thingDiscovery) { + m_thingDiscovery = thingDiscovery; + setSourceModel(thingDiscovery); + emit thingDiscoveryChanged(); emit countChanged(); - connect(m_deviceDiscovery, &ThingDiscovery::countChanged, this, &DeviceDiscoveryProxy::countChanged); + connect(m_thingDiscovery, &ThingDiscovery::countChanged, this, &ThingDiscoveryProxy::countChanged); invalidateFilter(); } } -bool DeviceDiscoveryProxy::showAlreadyAdded() const +bool ThingDiscoveryProxy::showAlreadyAdded() const { return m_showAlreadyAdded; } -void DeviceDiscoveryProxy::setShowAlreadyAdded(bool showAlreadyAdded) +void ThingDiscoveryProxy::setShowAlreadyAdded(bool showAlreadyAdded) { if (m_showAlreadyAdded != showAlreadyAdded) { m_showAlreadyAdded = showAlreadyAdded; @@ -243,12 +243,12 @@ void DeviceDiscoveryProxy::setShowAlreadyAdded(bool showAlreadyAdded) } } -bool DeviceDiscoveryProxy::showNew() const +bool ThingDiscoveryProxy::showNew() const { return m_showNew; } -void DeviceDiscoveryProxy::setShowNew(bool showNew) +void ThingDiscoveryProxy::setShowNew(bool showNew) { if (m_showNew != showNew) { m_showNew = showNew; @@ -258,37 +258,37 @@ void DeviceDiscoveryProxy::setShowNew(bool showNew) } } -QUuid DeviceDiscoveryProxy::filterDeviceId() const +QUuid ThingDiscoveryProxy::filterThingId() const { - return m_filterDeviceId; + return m_filterThingId; } -void DeviceDiscoveryProxy::setFilterDeviceId(const QUuid &filterDeviceId) +void ThingDiscoveryProxy::setFilterThingId(const QUuid &filterDeviceId) { - if (m_filterDeviceId != filterDeviceId) { - m_filterDeviceId = filterDeviceId; - emit filterDeviceIdChanged(); + if (m_filterThingId != filterDeviceId) { + m_filterThingId = filterDeviceId; + emit filterThingIdChanged(); invalidateFilter(); emit countChanged(); } } -DeviceDescriptor *DeviceDiscoveryProxy::get(int index) const +ThingDescriptor *ThingDiscoveryProxy::get(int index) const { - return m_deviceDiscovery->get(mapToSource(this->index(index, 0)).row()); + return m_thingDiscovery->get(mapToSource(this->index(index, 0)).row()); } -bool DeviceDiscoveryProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +bool ThingDiscoveryProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { Q_UNUSED(sourceParent) - DeviceDescriptor* dev = m_deviceDiscovery->get(sourceRow); - if (!m_showAlreadyAdded && !dev->deviceId().isNull()) { + ThingDescriptor* dev = m_thingDiscovery->get(sourceRow); + if (!m_showAlreadyAdded && !dev->thingId().isNull()) { return false; } - if (!m_showNew && dev->deviceId().isNull()) { + if (!m_showNew && dev->thingId().isNull()) { return false; } - if (!m_filterDeviceId.isNull() && dev->deviceId() != m_filterDeviceId) { + if (!m_filterThingId.isNull() && dev->thingId() != m_filterThingId) { return false; } return true; diff --git a/libnymea-app/thingdiscovery.h b/libnymea-app/thingdiscovery.h index b70e724a..5cd4daf5 100644 --- a/libnymea-app/thingdiscovery.h +++ b/libnymea-app/thingdiscovery.h @@ -36,25 +36,25 @@ #include "engine.h" -class DeviceDescriptor: public QObject { +class ThingDescriptor: public QObject { Q_OBJECT Q_PROPERTY(QUuid id READ id CONSTANT) - Q_PROPERTY(QUuid deviceId READ deviceId CONSTANT) + Q_PROPERTY(QUuid thingId READ thingId CONSTANT) Q_PROPERTY(QString name READ name CONSTANT) Q_PROPERTY(QString description READ description CONSTANT) Q_PROPERTY(Params* params READ params CONSTANT) public: - DeviceDescriptor(const QUuid &id, const QUuid &deviceId, const QString &name, const QString &description, QObject *parent = nullptr); + ThingDescriptor(const QUuid &id, const QUuid &thingId, const QString &name, const QString &description, QObject *parent = nullptr); QUuid id() const; - QUuid deviceId() const; + QUuid thingId() const; QString name() const; QString description() const; Params* params() const; private: QUuid m_id; - QUuid m_deviceId; + QUuid m_thingId; QString m_name; QString m_description; Params *m_params = nullptr; @@ -84,7 +84,7 @@ public: Q_INVOKABLE void discoverThings(const QUuid &thingClassId, const QVariantList &discoveryParams = {}); - Q_INVOKABLE DeviceDescriptor* get(int index) const; + Q_INVOKABLE ThingDescriptor* get(int index) const; Engine* engine() const; void setEngine(Engine *jsonRpcClient); @@ -106,23 +106,23 @@ private: QString m_displayMessage; bool contains(const QUuid &deviceDescriptorId) const; - QList m_foundDevices; + QList m_foundDevices; }; -class DeviceDiscoveryProxy: public QSortFilterProxyModel +class ThingDiscoveryProxy: public QSortFilterProxyModel { Q_OBJECT Q_PROPERTY(int count READ rowCount NOTIFY countChanged) - Q_PROPERTY(ThingDiscovery* deviceDiscovery READ deviceDiscovery WRITE setDeviceDiscovery NOTIFY deviceDiscoveryChanged) + Q_PROPERTY(ThingDiscovery* thingDiscovery READ thingDiscovery WRITE setThingDiscovery NOTIFY thingDiscoveryChanged) Q_PROPERTY(bool showAlreadyAdded READ showAlreadyAdded WRITE setShowAlreadyAdded NOTIFY showAlreadyAddedChanged) Q_PROPERTY(bool showNew READ showNew WRITE setShowNew NOTIFY showNewChanged) - Q_PROPERTY(QUuid filterDeviceId READ filterDeviceId WRITE setFilterDeviceId NOTIFY filterDeviceIdChanged) + Q_PROPERTY(QUuid filterThingId READ filterThingId WRITE setFilterThingId NOTIFY filterThingIdChanged) public: - DeviceDiscoveryProxy(QObject *parent = nullptr); + ThingDiscoveryProxy(QObject *parent = nullptr); - ThingDiscovery* deviceDiscovery() const; - void setDeviceDiscovery(ThingDiscovery* deviceDiscovery); + ThingDiscovery* thingDiscovery() const; + void setThingDiscovery(ThingDiscovery* thingDiscovery); bool showAlreadyAdded() const; void setShowAlreadyAdded(bool showAlreadyAdded); @@ -130,26 +130,26 @@ public: bool showNew() const; void setShowNew(bool showNew); - QUuid filterDeviceId() const; - void setFilterDeviceId(const QUuid &filterDeviceId); + QUuid filterThingId() const; + void setFilterThingId(const QUuid &filterDeviceId); - Q_INVOKABLE DeviceDescriptor* get(int index) const; + Q_INVOKABLE ThingDescriptor* get(int index) const; signals: void countChanged(); - void deviceDiscoveryChanged(); + void thingDiscoveryChanged(); void showAlreadyAddedChanged(); void showNewChanged(); - void filterDeviceIdChanged(); + void filterThingIdChanged(); protected: bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; private: - ThingDiscovery* m_deviceDiscovery = nullptr; + ThingDiscovery* m_thingDiscovery = nullptr; bool m_showAlreadyAdded = false; bool m_showNew = true; - QUuid m_filterDeviceId; + QUuid m_filterThingId; }; #endif // THINGDISCOVERY_H diff --git a/nymea-app/ui/thingconfiguration/SetupWizard.qml b/nymea-app/ui/thingconfiguration/SetupWizard.qml index a5cbef5c..f6588254 100644 --- a/nymea-app/ui/thingconfiguration/SetupWizard.qml +++ b/nymea-app/ui/thingconfiguration/SetupWizard.qml @@ -69,7 +69,6 @@ Page { id: d property var vendorId: null property ThingDescriptor thingDescriptor: null - property alias deviceDescriptor: d.thingDescriptor property var discoveryParams: [] property string deviceName: "" property int pairRequestId: 0 @@ -86,7 +85,7 @@ Page { internalPageStack.push(discoveryParamsPage) } else { print("Starting discovery...") - discovery.discoverDevices(deviceClass.id) + discovery.discoverThings(deviceClass.id) internalPageStack.push(discoveryPage, {deviceClass: deviceClass}) } } else if (root.deviceClass.createMethods.indexOf("CreateMethodUser") !== -1) { @@ -171,7 +170,7 @@ Page { } } - DeviceDiscovery { + ThingDiscovery { id: discovery engine: _engine } @@ -218,7 +217,7 @@ Page { param["value"] = paramRepeater.itemAt(i).value d.discoveryParams.push(param); } - discovery.discoverDevices(root.deviceClass.id, d.discoveryParams) + discovery.discoverThings(root.deviceClass.id, d.discoveryParams) internalPageStack.push(discoveryPage, {deviceClass: root.deviceClass}) } } @@ -259,12 +258,12 @@ Page { Layout.fillWidth: true Layout.fillHeight: true clip: true - model: DeviceDiscoveryProxy { + model: ThingDiscoveryProxy { id: discoveryProxy - deviceDiscovery: discovery + thingDiscovery: discovery showAlreadyAdded: root.device !== null showNew: root.device === null - filterDeviceId: root.device ? root.device.id : "" + filterThingId: root.device ? root.device.id : "" } delegate: NymeaSwipeDelegate { width: parent.width @@ -273,7 +272,7 @@ Page { subText: model.description iconName: app.interfacesToIcon(discoveryView.deviceClass.interfaces) onClicked: { - d.deviceDescriptor = discoveryProxy.get(index); + d.thingDescriptor = discoveryProxy.get(index); d.deviceName = model.name; internalPageStack.push(paramsPage) } @@ -284,7 +283,7 @@ Page { Layout.fillWidth: true Layout.leftMargin: app.margins; Layout.rightMargin: app.margins text: qsTr("Search again") - onClicked: discovery.discoverDevices(root.deviceClass.id, d.discoveryParams) + onClicked: discovery.discoverThings(root.deviceClass.id, d.discoveryParams) visible: !discovery.busy } @@ -384,7 +383,7 @@ Page { Repeater { id: paramRepeater - model: engine.jsonRpcClient.ensureServerVersion("1.12") || d.deviceDescriptor == null ? root.deviceClass.paramTypes : null + model: engine.jsonRpcClient.ensureServerVersion("1.12") || d.thingDescriptor == null ? root.deviceClass.paramTypes : null delegate: ParamDelegate { // Layout.preferredHeight: 60 Layout.fillWidth: true @@ -441,14 +440,14 @@ Page { switch (root.deviceClass.setupMethod) { case 0: if (root.device) { - if (d.deviceDescriptor) { - engine.deviceManager.reconfigureDiscoveredDevice(root.device.id, d.deviceDescriptor.id, params); + if (d.thingDescriptor) { + engine.deviceManager.reconfigureDiscoveredDevice(root.device.id, d.thingDescriptor.id, params); } else { engine.deviceManager.reconfigureDevice(root.device.id, params); } } else { - if (d.deviceDescriptor) { - engine.deviceManager.addDiscoveredDevice(root.deviceClass.id, d.deviceDescriptor.id, nameTextField.text, params); + if (d.thingDescriptor) { + engine.deviceManager.addDiscoveredDevice(root.deviceClass.id, d.thingDescriptor.id, nameTextField.text, params); } else { engine.deviceManager.addDevice(root.deviceClass.id, nameTextField.text, params); } @@ -460,15 +459,15 @@ Page { case 4: case 5: if (root.device) { - if (d.deviceDescriptor) { - engine.deviceManager.pairDiscoveredDevice(root.deviceClass.id, d.deviceDescriptor.id, params, nameTextField.text); + if (d.thingDescriptor) { + engine.deviceManager.pairDiscoveredDevice(root.deviceClass.id, d.thingDescriptor.id, params, nameTextField.text); } else { engine.deviceManager.rePairDevice(root.device.id, params, nameTextField.text); } return; } else { - if (d.deviceDescriptor) { - engine.deviceManager.pairDiscoveredDevice(root.deviceClass.id, d.deviceDescriptor.id, params, nameTextField.text); + if (d.thingDescriptor) { + engine.deviceManager.pairDiscoveredDevice(root.deviceClass.id, d.thingDescriptor.id, params, nameTextField.text); } else { engine.deviceManager.pairDevice(root.deviceClass.id, params, nameTextField.text); }