trikRuntime
trikWiFiWorker.h
Go to the documentation of this file.
1 /* Copyright 2015 Yurii Litvinov and CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <QtCore/QObject>
18 #include <QtCore/QString>
19 #include <QtCore/QScopedPointer>
20 #include <QtCore/QSocketNotifier>
21 #include <QtCore/QHash>
22 
24 
25 #include "networkStructs.h"
26 
27 namespace trikWiFi {
28 
29 class WpaSupplicantCommunicator;
30 
32 class TrikWiFiWorker : public QObject
33 {
34  Q_OBJECT
35 
36 public:
42  TrikWiFiWorker(const QString &interfaceFilePrefix, const QString &daemonFile);
43 
44  ~TrikWiFiWorker() override;
45 
47  Q_INVOKABLE void connect(const QString &ssid);
48 
50  Q_INVOKABLE void disconnect();
51 
53  Q_INVOKABLE void reinit();
54 
56  Q_INVOKABLE void dispose();
57 
60  Q_INVOKABLE void statusRequest();
61 
64 
67  Q_INVOKABLE void scanRequest();
68 
70  QList<ScanResult> scanResult();
71 
72 signals:
75  void scanFinished();
76 
78  void connected();
79 
82 
85  void statusReady();
86 
88  void error(const QString &message);
89 
90 private slots:
92  void receiveMessages();
93 
94 private:
96  struct NetworkConfiguration
97  {
99  int id;
100 
102  QString ssid;
103  };
104 
106  static QHash<QString, QString> parseReply(const QString &reply);
107 
109  void processMessage(const QString &message);
110 
112  void processScanResults();
113 
115  int addOpenNetwork(const QString &ssid);
116 
118  int findNetworkId(const QString &ssid) const;
119 
121  void listKnownNetworks();
122 
123  QString mInterfaceFile;
124  QString mDaemonFile;
125  QScopedPointer<WpaSupplicantCommunicator> mControlInterface;
126  QScopedPointer<WpaSupplicantCommunicator> mMonitorInterface;
127  QScopedPointer<QSocketNotifier> mMonitorFileSocketNotifier;
128 
130  QHash<QString, NetworkConfiguration> mNetworkConfiguration;
131 
133 
136 
139  bool mIgnoreScanResults = true;
140 
142  bool mPlannedDisconnect = false;
143 };
144 
145 }
Q_INVOKABLE void scanRequest()
Scans for available WiFi networks.
Definition: trikWiFiWorker.cpp:123
Q_INVOKABLE void reinit()
Disposes an old connection to wpa_supplicant and creates a new one.
Definition: trikWiFiWorker.cpp:39
DisconnectReason
Enum with possible reasons of "disconnect" message.
Definition: networkStructs.h:35
void error(const QString &message)
Emitted when something goes wrong.
Contains info about current connection.
Definition: networkStructs.h:53
Q_INVOKABLE void statusRequest()
Gets conection status and connection information such as SSID and IP.
Definition: trikWiFiWorker.cpp:134
Q_INVOKABLE void dispose()
Closes the connection to wpa_supplicant.
Definition: trikWiFiWorker.cpp:70
QList< ScanResult > scanResult()
Returns a list of currently known available WiFi networks. Use scanRequest() method to refresh...
Definition: trikWiFiWorker.cpp:213
Helper template for syncing reader and writer.
Definition: synchronizedVar.h:34
void connected()
Emitted when wpa_supplicant connects to WiFi network. SSID of this network can be retrieved by status...
TrikWiFiWorker(const QString &interfaceFilePrefix, const QString &daemonFile)
Constructor.
Definition: trikWiFiWorker.cpp:26
Q_INVOKABLE void connect(const QString &ssid)
Connect to a network with given ssid.
Definition: trikWiFiWorker.cpp:80
void scanFinished()
Emitted when scanning for available networks initiated by scan() is finished and results are availabl...
Q_INVOKABLE void disconnect()
Disconnect from network if we are currently connected to one.
Definition: trikWiFiWorker.cpp:112
Status statusResult()
Returns last known connection status. To refresh, use statusRequest() method.
Definition: trikWiFiWorker.cpp:159
void disconnected(trikWiFi::DisconnectReason reason)
Emitted when wpa_supplicant disconnects from current network.
Worker object for TrikWiFi, supposed to be runned in its own thread.
Definition: trikWiFiWorker.h:32
Definition: controller.h:25
void statusReady()
Emitted when connection status requested by statusRequest() is ready and results can be obtained by s...
~TrikWiFiWorker() override
Definition: trikWiFiWorker.cpp:34