Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wireless access point mode #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions libnymea-app-core/wifisetup/wirelessaccesspoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ QVariant WirelessAccessPoints::data(const QModelIndex &index, int role) const
return QVariant();
}

int WirelessAccessPoints::count() const
{
return m_wirelessAccessPoints.count();
}

WirelessAccessPoint *WirelessAccessPoints::getAccessPoint(const QString &ssid) const
{
foreach (WirelessAccessPoint *accessPoint, m_wirelessAccessPoints) {
Expand Down
1 change: 0 additions & 1 deletion libnymea-app-core/wifisetup/wirelessaccesspoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class WirelessAccessPoints : public QAbstractListModel
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

int count() const;
Q_INVOKABLE WirelessAccessPoint *getAccessPoint(const QString &ssid) const;
Q_INVOKABLE WirelessAccessPoint *get(int index);

Expand Down
161 changes: 124 additions & 37 deletions libnymea-app-core/wifisetup/wirelesssetupmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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"));
Expand All @@ -43,7 +44,6 @@ static QBluetoothUuid systemServiceUuid = QBluetoothUuid(QUuid("
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))
Expand Down Expand Up @@ -87,6 +87,11 @@ bool WirelessSetupManager::working() const
return m_working;
}

bool WirelessSetupManager::accessPointModeAvailable() const
{
return m_accessPointModeAvailable;
}

WirelessSetupManager::NetworkStatus WirelessSetupManager::networkStatus() const
{
return m_networkStatus;
Expand All @@ -97,6 +102,11 @@ WirelessSetupManager::WirelessStatus WirelessSetupManager::wirelessStatus() cons
return m_wirelessStatus;
}

WirelessSetupManager::WirelessDeviceMode WirelessSetupManager::wirelessDeviceMode() const
{
return m_wirelessDeviceMode;
}

bool WirelessSetupManager::networkingEnabled() const
{
return m_networkingEnabled;
Expand Down Expand Up @@ -145,7 +155,7 @@ void WirelessSetupManager::loadNetworks()
emit workingChanged();

QVariantMap request;
request.insert("c", (int)WirelessServiceCommandGetNetworks);
request.insert("c", static_cast<int>(WirelessServiceCommandGetNetworks));
streamData(request);
}

Expand All @@ -172,7 +182,7 @@ void WirelessSetupManager::loadCurrentConnection()
emit workingChanged();

QVariantMap request;
request.insert("c", (int)WirelessServiceCommandGetCurrentConnection);
request.insert("c", static_cast<int>(WirelessServiceCommandGetCurrentConnection));
streamData(request);
}

Expand All @@ -196,7 +206,7 @@ void WirelessSetupManager::performWifiScan()
emit workingChanged();

QVariantMap request;
request.insert("c", (int)WirelessServiceCommandScan);
request.insert("c", static_cast<int>(WirelessServiceCommandScan));
streamData(request);
}

Expand Down Expand Up @@ -255,7 +265,35 @@ void WirelessSetupManager::connectWirelessNetwork(const QString &ssid, const QSt
}

QVariantMap request;
request.insert("c", (int)WirelessServiceCommandConnect);
request.insert("c", static_cast<int>(WirelessServiceCommandConnect));
QVariantMap parameters;
parameters.insert("e", ssid);
parameters.insert("p", password);
request.insert("p", parameters);

streamData(request);
}

void WirelessSetupManager::startAccessPoint(const QString &ssid, const QString &password)
{
qDebug() << "WifiSetupManager: Start wireless access point using SSID:" << ssid << " password:" << 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", static_cast<int>(WirelessServiceCommandStartAccessPoint));
QVariantMap parameters;
parameters.insert("e", ssid);
parameters.insert("p", password);
Expand All @@ -280,7 +318,7 @@ void WirelessSetupManager::disconnectWirelessNetwork()
}

QVariantMap request;
request.insert("c", (int)WirelessServiceCommandDisconnect);
request.insert("c", static_cast<int>(WirelessServiceCommandDisconnect));
streamData(request);
}

Expand All @@ -300,7 +338,7 @@ void WirelessSetupManager::pressPushButton()
}

QVariantMap request;
request.insert("c", (int)SystemServiceCommandPushAuthentication);
request.insert("c", static_cast<int>(SystemServiceCommandPushAuthentication));

QByteArray data = QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact) + '\n';
qDebug() << "WifiSetupManager: SystemService: Start streaming response data:" << data.count() << "bytes";
Expand Down Expand Up @@ -332,9 +370,8 @@ void WirelessSetupManager::checkInitialized()
&& m_wifiService->state() == QLowEnergyService::ServiceDiscovered;
}

if (initialized && m_wirelessEnabled && m_networkingEnabled) {
if (initialized)
loadNetworks();
}
}

void WirelessSetupManager::setModelNumber(const QString &modelNumber)
Expand Down Expand Up @@ -387,6 +424,27 @@ void WirelessSetupManager::setWirelessStatus(int wirelessStatus)
emit wirelessStatusChanged();
}

void WirelessSetupManager::setWirelessDeviceMode(int wirelessDeviceMode)
{
if (m_wirelessDeviceMode == wirelessDeviceMode)
return;

m_wirelessDeviceMode = static_cast<WirelessDeviceMode>(wirelessDeviceMode);
qDebug() << "-->" << m_wirelessDeviceMode;
emit wirelessDeviceModeChanged();

switch (m_wirelessDeviceMode) {
case WirelessDeviceModeAccessPoint:
loadCurrentConnection();
break;
case WirelessDeviceModeInfrastructure:
loadNetworks();
break;
default:
break;
}
}

void WirelessSetupManager::setNetworkingEnabled(bool networkingEnabled)
{
if (m_networkingEnabled == networkingEnabled)
Expand Down Expand Up @@ -445,13 +503,12 @@ void WirelessSetupManager::processNetworkResponse(const QVariantMap &response)
return;
}

NetworkServiceCommand command = (NetworkServiceCommand)response.value("c").toInt();
NetworkServiceResponse responseCode = (NetworkServiceResponse)response.value("r").toInt();
NetworkServiceCommand command = static_cast<NetworkServiceCommand>(response.value("c").toInt());
NetworkServiceResponse responseCode = static_cast<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."));
Expand Down Expand Up @@ -483,8 +540,8 @@ void WirelessSetupManager::processWifiResponse(const QVariantMap &response)
return;
}

WirelessServiceCommand command = (WirelessServiceCommand)response.value("c").toInt();
WirelessServiceResponse responseCode = (WirelessServiceResponse)response.value("r").toInt();
WirelessServiceCommand command = static_cast<WirelessServiceCommand>(response.value("c").toInt());
WirelessServiceResponse responseCode = static_cast<WirelessServiceResponse>(response.value("r").toInt());

if (responseCode != WirelessServiceResponseSuccess) {
qWarning() << "WifiSetupManager: Got error for command" << command << responseCode;
Expand Down Expand Up @@ -556,21 +613,31 @@ void WirelessSetupManager::processWifiResponse(const QVariantMap &response)
qDebug() << "Current network connection" << response;
QVariantMap currentConnection = response.value("p").toMap();;

// Find current network
m_currentConnection = nullptr;
WirelessAccessPoint *oldCurrent = m_currentConnection;
if (m_currentConnection) {
m_currentConnection->deleteLater();
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());
}
if (!macAddress.isEmpty()) {
// Looks like we are connected to a network which doesn't show up in the wifi scan. perhaps we opened up our own ap or connected to a hidden network. Let's add it now
m_currentConnection = new WirelessAccessPoint(this);
m_currentConnection->setSsid(currentConnection.value("e").toString());
m_currentConnection->setMacAddress(macAddress);
m_currentConnection->setHostAddress(currentConnection.value("i").toString());
m_currentConnection->setSignalStrength(currentConnection.value("s").toInt());
m_currentConnection->setProtected(currentConnection.value("p").toBool());
}
qDebug() << "Current connection is:" << m_currentConnection << (m_currentConnection ? m_currentConnection->hostAddress() : "");
if (oldCurrent != m_currentConnection) {
emit currentConnectionChanged();
}
qDebug() << "current connection is:" << m_currentConnection;
emit currentConnectionChanged();

m_initialized = true;
emit initializedChanged();
if (!m_initialized) {
m_initialized = true;
emit initializedChanged();
}

break;
}
Expand All @@ -589,8 +656,8 @@ void WirelessSetupManager::processSystemResponse(const QVariantMap &response)
return;
}

SystemServiceCommand command = (SystemServiceCommand)response.value("c").toInt();
SystemServiceResponse responseCode = (SystemServiceResponse)response.value("r").toInt();
SystemServiceCommand command = static_cast<SystemServiceCommand>(response.value("c").toInt());
SystemServiceResponse responseCode = static_cast<SystemServiceResponse>(response.value("r").toInt());

if (responseCode != SystemServiceResponseSuccess) {
qWarning() << "WifiSetupManager: Got error for command" << command << responseCode;
Expand Down Expand Up @@ -781,9 +848,9 @@ void WirelessSetupManager::onNetworkServiceStateChanged(const QLowEnergyService:
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));
setNetworkStatus(static_cast<int>(networkCharacteristic.value().toHex().toUInt(nullptr, 16)));
setNetworkingEnabled(static_cast<bool>(networkingEnabledCharacteristic.value().toHex().toUInt(nullptr, 16)));
setWirelessEnabled(static_cast<bool>(wirelessEnabledCharacteristic.value().toHex().toUInt(nullptr, 16)));

// Wifi service
if (!m_wifiService) {
Expand Down Expand Up @@ -847,12 +914,14 @@ void WirelessSetupManager::onNetworkServiceCharacteristicChanged(const QLowEnerg
return;
}
} else if (characteristic.uuid() == networkingEnabledCharacteristicUuid) {
qDebug() << "Networking enabled changed" << (bool)value.toHex().toUInt(0, 16);
setNetworkingEnabled((bool)value.toHex().toUInt(0, 16));
bool networkingEnabled = static_cast<bool>(value.toHex().toUInt(nullptr, 16));
qDebug() << "Networking enabled changed" << networkingEnabled;
setNetworkingEnabled(networkingEnabled);
return;
} else if (characteristic.uuid() == wirelessEnabledCharacteristicUuid) {
qDebug() << "Wireless enabled changed" << (bool)value.toHex().toUInt(0, 16);
setWirelessEnabled((bool)value.toHex().toUInt(0, 16));
bool wirelessEnabled = static_cast<bool>(value.toHex().toUInt(nullptr, 16));
qDebug() << "Wireless enabled changed" << wirelessEnabled;
setWirelessEnabled(wirelessEnabled);
return;
}

Expand All @@ -874,16 +943,29 @@ void WirelessSetupManager::onWifiServiceStateChanged(const QLowEnergyService::Se

foreach (const QLowEnergyCharacteristic &characteristic, m_wifiService->characteristics()) {
qDebug() << " -->" << characteristic.name() << characteristic.uuid().toString() << characteristic.value();

if (characteristic.uuid() == wifiModeCharacteristicUuid) {
m_accessPointModeAvailable = true;
emit accessPointModeAvailableChanged();
}

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));
// Note: For compatibility
if (m_accessPointModeAvailable) {
m_wifiService->writeDescriptor(m_wifiService->characteristic(wifiModeCharacteristicUuid).descriptor(QBluetoothUuid::ClientCharacteristicConfiguration), QByteArray::fromHex("0100"));
setWirelessDeviceMode(static_cast<int>(m_wifiService->characteristic(wifiModeCharacteristicUuid).value().toHex().toUInt(nullptr, 16)));
}

setWirelessStatus(static_cast<int>(m_wifiService->characteristic(wifiStatusCharacteristicUuid).value().toHex().toUInt(nullptr, 16)));

// System service
if (!m_systemService) {
Expand Down Expand Up @@ -950,8 +1032,13 @@ void WirelessSetupManager::onWifiServiceCharacteristicChanged(const QLowEnergyCh
return;
}

qWarning() << "Bluetooth: Unhandled service characteristic changed" << characteristic.uuid() << value;
if (characteristic.uuid() == wifiModeCharacteristicUuid) {
qDebug() << "Wireless mode changed:" << value.toHex().toUInt(nullptr, 16); // << "Old status:" << m_wirelessStatus;
setWirelessDeviceMode(value.toHex().toInt(nullptr, 16));
return;
}

qWarning() << "Bluetooth: Unhandled service characteristic changed" << characteristic.uuid() << value;
}

void WirelessSetupManager::onWifiServiceReadFinished(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
Expand Down
Loading