Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #62 from lpotter/newtether
[connectionagent] add tethering functionality
  • Loading branch information
lpotter committed May 2, 2014
2 parents e6292a7 + eaada24 commit 84f6000
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 21 deletions.
9 changes: 9 additions & 0 deletions connd/com.jollamobile.Connectiond.xml
Expand Up @@ -28,6 +28,9 @@
<signal name="roamingAskChanged">
<arg name="askRoaming" type="b" direction="out"/>
</signal>
<signal name="tetheringFinished">
<arg name="success" type="b" direction="out"/>
</signal>
<method name="connectToType">
<arg name="in0" type="s" direction="in"/>
</method>
Expand All @@ -39,5 +42,11 @@
<arg name="input" type="a{sv}" direction="in"/>
<annotation name="org.qtproject.QtDBus.QtTypeName.In0" value="QVariantMap"/>
</method>
<method name="startTethering">
<arg name="in0" type="s" direction="in"/>
</method>
<method name="stopTethering">
</method>
</interface>
</node>

171 changes: 154 additions & 17 deletions connd/qconnectionagent.cpp
Expand Up @@ -234,18 +234,35 @@ void QConnectionAgent::serviceStateChanged(const QString &state)
if (state == "disconnect") {
ua->sendConnectReply("Clear");
}
// if (state == "failure") {
if (state == "failure") {
if (delayedTethering && service->type() == "cellular" && tetheringWifiTech->tethering()) {
Q_EMIT tetheringFinished(false);
}
// serviceInProgress.clear();
// // service->requestDisconnect();
// }
}

if (delayedTethering && service->type() == "wifi" && state == "association") {
service->requestDisconnect();
}
if (state == "online") {
Q_EMIT connectionState(state, service->type());
}

if (service->type() == "wifi" && delayedTethering) {
netman->getTechnology(service->type())->setTethering(true);
}
if (service->type() == "cellular" && delayedTethering) {
if (!tetheringWifiTech->tethering()) {
tetheringWifiTech->setTethering(true);
}
}
}
//auto migrate
if (state == "idle") {
} else {
if (service->type() == "wifi" && delayedTethering) {
netman->getTechnology(service->type())->setTethering(true);
}
} else {
updateServicesMap();
}
currentNetworkState = state;
Expand Down Expand Up @@ -358,10 +375,26 @@ void QConnectionAgent::networkStateChanged(const QString &state)

if ((state == "online" && netman->defaultRoute()->type() == "cellular")
|| (state == "idle")) {

if (tetheringWifiTech && tetheringWifiTech->powered()
&& !tetheringWifiTech->tethering())
tetheringWifiTech->scan();
// on gprs, scan wifi every scanTimeoutInterval minutes
if (scanTimeoutInterval != 0)
scanTimer->start(scanTimeoutInterval * 60 * 1000);
}

if (delayedTethering && state == "online") {

if (tetheringWifiTech->tethering()) {
if (netman->defaultRoute()->type() == "cellular") {
delayedTethering = false;
Q_EMIT tetheringFinished(true);
}
} else {
tetheringWifiTech->setTethering(true);
}
}
}

bool QConnectionAgent::askRoaming() const
Expand Down Expand Up @@ -457,10 +490,13 @@ void QConnectionAgent::technologyPowerChanged(bool powered)
{
NetworkTechnology *tech = static_cast<NetworkTechnology *>(sender());
qDebug() << tech->name() << powered;
if (tech->type() == "wifi") {
if (powered) {
tetheringWifiTech->scan();
} else {

if (netman && tech->type() == "wifi" && powered && delayedTethering) {
tech->setTethering(true);
}

if (!delayedTethering && tech->type() == "wifi") {
if (!powered) {
removeAllTypes("wifi"); //dont wait for connman, he's too slow at this
QString bestService = findBestConnectableService();
if (!bestService.isEmpty()) {
Expand All @@ -473,7 +509,8 @@ void QConnectionAgent::technologyPowerChanged(bool powered)

void QConnectionAgent::techChanged()
{
qDebug() << netman->getTechnology("wifi");
if (!netman)
return;
if (netman->getTechnologies().isEmpty()) {
knownTechnologies.clear();
}
Expand All @@ -483,9 +520,6 @@ void QConnectionAgent::techChanged()
}
if (tetheringWifiTech) {
tetheringEnabled = tetheringWifiTech->tethering();
qDebug() << "tethering is" << tetheringEnabled;
QObject::connect(tetheringWifiTech, SIGNAL(tetheringChanged(bool)),
this,SLOT(techTetheringChanged(bool)), Qt::UniqueConnection);
}

Q_FOREACH(NetworkTechnology *technology,netman->getTechnologies()) {
Expand All @@ -495,6 +529,8 @@ void QConnectionAgent::techChanged()
tetheringWifiTech = technology;
connect(tetheringWifiTech,SIGNAL(poweredChanged(bool)),this,SLOT(technologyPowerChanged(bool)));
connect(tetheringWifiTech,SIGNAL(scanFinished()),this,SLOT(onScanFinished()));
connect(tetheringWifiTech, SIGNAL(tetheringChanged(bool)),
this,SLOT(techTetheringChanged(bool)), Qt::UniqueConnection);
}
} else {
knownTechnologies.removeOne(technology->path());
Expand All @@ -518,10 +554,28 @@ bool QConnectionAgent::isStateOnline(const QString &state)
return false;
}

void QConnectionAgent::techTetheringChanged(bool b)
void QConnectionAgent::techTetheringChanged(bool on)
{
qDebug() << b;
tetheringEnabled = b;
qDebug() << on;
tetheringEnabled = on;
NetworkTechnology *technology = static_cast<NetworkTechnology *>(sender());

if (on && delayedTethering && technology) {
QVector <NetworkService *> services = netman->getServices("cellular");
if (services.isEmpty())
return;
NetworkService* cellService = services.at(0);
if (cellService) {
if (cellService->state() == "idle"|| cellService->state() == "failure") {
cellService->requestConnect();
} else if (cellService->connected()) {
delayedTethering = false;
Q_EMIT tetheringFinished(true);
}
} else {
stopTethering();
}
}
}

void QConnectionAgent::offlineModeChanged(bool b)
Expand All @@ -542,7 +596,7 @@ void QConnectionAgent::displayStateChanged(const QString &state)
{
if (state == "on") {
NetworkTechnology *wifiTech = netman->getTechnology("wifi");
if (wifiTech && wifiTech->powered() && !wifiTech->connected()) {
if (wifiTech && wifiTech->powered() && !wifiTech->connected() && !wifiTech->tethering()) {
wifiTech->scan();
}
}
Expand Down Expand Up @@ -603,7 +657,10 @@ bool QConnectionAgent::isBestService(NetworkService *service)

void QConnectionAgent::scanTimeout()
{
if (tetheringWifiTech && tetheringWifiTech->powered() && !tetheringWifiTech->connected() && netman->defaultRoute()->type() != "wifi" ) {
if (!tetheringWifiTech || tetheringWifiTech->tethering())
return;

if (tetheringWifiTech->powered() && !tetheringWifiTech->connected() && netman->defaultRoute()->type() != "wifi" ) {
tetheringWifiTech->scan();
qDebug() << "start scanner" << scanTimeoutInterval;
if (scanTimeoutInterval != 0) {
Expand Down Expand Up @@ -722,3 +779,83 @@ void QConnectionAgent::openConnectionDialog(const QString &type)
QDBusMessage reply = connSelectorInterface->callWithArgumentList(QDBus::NoBlock,
QStringLiteral("openConnection"), args);
}

void QConnectionAgent::startTethering(const QString &type)
{
if (!netman | (type != "wifi")) { //we only support wifi for now
Q_EMIT tetheringFinished(false);
return;
}

NetworkTechnology *tetherTech = netman->getTechnology(type);
if (!tetherTech) {
Q_EMIT tetheringFinished(false);
return;
}

QVector <NetworkService *> services = netman->getServices("cellular");
if (services.isEmpty()) {
Q_EMIT tetheringFinished(false);
return;
}

NetworkService *cellService = services.at(0);
if (!cellService || netman->offlineMode()) {
Q_EMIT tetheringFinished(false);
return;
}

QSettings confFile;
confFile.beginGroup("Connectionagent");
bool cellConnected = cellService->connected();
bool cellAutoconnect = cellService->autoConnect();

// save cellular connection state
confFile.setValue("tetheringCellularConnected",cellConnected);
confFile.setValue("tetheringCellularAutoconnect",cellAutoconnect);

bool techPowered = tetherTech->powered();

// save wifi powered state
confFile.setValue("tetheringTechPowered",techPowered);
confFile.setValue("tetheringType",type);

delayedTethering = true;
if (!techPowered) {
tetherTech->setPowered(true);
} else {
tetherTech->setTethering(true);
}
}

void QConnectionAgent::stopTethering()
{
delayedTethering = false;
QSettings confFile;
confFile.beginGroup("Connectionagent");

NetworkTechnology *tetherTech = netman->getTechnology(confFile.value("tetheringType","wifi").toString());
if (tetherTech && tetherTech->tethering()) {
tetherTech->setTethering(false);
}
bool b = confFile.value("tetheringCellularConnected").toBool();
bool ab = confFile.value("tetheringCellularAutoconnect").toBool();

Q_FOREACH (const QString &path, servicesMap.keys()) {
if (path.contains("cellular")) {
if (isStateOnline(servicesMap.value(path)->state())) {
qDebug() << "disconnect mobile data";
if (!b)
servicesMap.value(path)->requestDisconnect();
if (!ab)
servicesMap.value(path)->setAutoConnect(false);
}
}
}

b = confFile.value("tetheringTechPowered").toBool();
if (!b && tetherTech) {
tetherTech->setPowered(false);
}
Q_EMIT tetheringFinished(false);
}
6 changes: 6 additions & 0 deletions connd/qconnectionagent.h
Expand Up @@ -61,6 +61,7 @@ class QConnectionAgent : public QObject
void connectNow(const QString &path);

void requestBrowser(const QString &url);
void tetheringFinished(bool);

public Q_SLOTS:

Expand All @@ -75,6 +76,9 @@ public Q_SLOTS:

void connectToType(const QString &type);

void startTethering(const QString &type);
void stopTethering();

private:
explicit QConnectionAgent(QObject *parent = 0);
static QConnectionAgent *self;
Expand Down Expand Up @@ -107,6 +111,8 @@ public Q_SLOTS:
bool isBestService(NetworkService *service);
QString findBestConnectableService();
void removeAllTypes(const QString &type);
bool tetheringStarted;
bool delayedTethering;

private slots:
void onScanFinished();
Expand Down
19 changes: 18 additions & 1 deletion connectionagentplugin/connectionagentplugin.cpp
Expand Up @@ -87,10 +87,13 @@ void ConnectionAgentPlugin::connectToConnectiond(QString)

connect(connManagerInterface,SIGNAL(userInputRequested(QString,QVariantMap)),
this,SLOT(onUserInputRequested(QString,QVariantMap)), Qt::UniqueConnection);

connect(connManagerInterface,SIGNAL(tetheringFinished(bool)),
this,SLOT(onTetheringFinished(bool)));
}

void ConnectionAgentPlugin::sendUserReply(const QVariantMap &input)
{
{
if (!connManagerInterface || !connManagerInterface->isValid()) {
Q_EMIT errorReported("","ConnectionAgent not available");
return;
Expand Down Expand Up @@ -179,3 +182,17 @@ void ConnectionAgentPlugin::setAskRoaming(bool value)
connManagerInterface->setAskRoaming(value);
}

void ConnectionAgentPlugin::startTethering(const QString &type)
{
connManagerInterface->startTethering(type);
}

void ConnectionAgentPlugin::onTetheringFinished(bool success)
{
Q_EMIT tetheringFinished(success);
}

void ConnectionAgentPlugin::stopTethering()
{
connManagerInterface->stopTethering();
}
6 changes: 5 additions & 1 deletion connectionagentplugin/connectionagentplugin.h
Expand Up @@ -27,7 +27,7 @@ class ConnectionAgentPlugin : public QObject
Q_PROPERTY(bool askRoaming READ askRoaming WRITE setAskRoaming)

Q_DISABLE_COPY(ConnectionAgentPlugin)

public:
explicit ConnectionAgentPlugin(QObject *parent = 0);
~ConnectionAgentPlugin();
Expand All @@ -38,6 +38,8 @@ public slots:
void sendUserReply(const QVariantMap &input);
void sendConnectReply(const QString &replyMessage, int timeout = 120);
void connectToType(const QString &type);
void startTethering(const QString &type);
void stopTethering();

signals:
void userInputRequested(const QString &servicePath, const QVariantMap &fields);
Expand All @@ -47,6 +49,7 @@ public slots:
void configurationNeeded(const QString &type);
void connectionState(const QString &state, const QString &type);
void browserRequested(const QString &url);
void tetheringFinished(bool);

private:
com::jolla::Connectiond *connManagerInterface;
Expand All @@ -58,6 +61,7 @@ private slots:
void onUserInputRequested(const QString &service, const QVariantMap &fields);
void onConnectionRequested();
void onConnectionState(const QString &state, const QString &type);
void onTetheringFinished(bool);

void connectToConnectiond(const QString = QString());
void connectiondUnregistered(const QString = QString());
Expand Down
14 changes: 14 additions & 0 deletions connectionagentplugin/connectionamanagerinterface.h
Expand Up @@ -63,6 +63,19 @@ public Q_SLOTS: // METHODS
return asyncCallWithArgumentList(QLatin1String("sendUserReply"), argumentList);
}

inline QDBusPendingReply<> startTethering(const QString &in0)
{
QList<QVariant> argumentList;
argumentList << QVariant::fromValue(in0);
return asyncCallWithArgumentList(QLatin1String("startTethering"), argumentList);
}

inline QDBusPendingReply<> stopTethering()
{
return asyncCall(QLatin1String("stopTethering"));
}


Q_SIGNALS: // SIGNALS
void configurationNeeded(const QString &type);
void connectionRequest();
Expand All @@ -72,6 +85,7 @@ public Q_SLOTS: // METHODS
void roamingAskChanged(bool askRoaming);
void userInputCanceled();
void userInputRequested(const QString &service, const QVariantMap &fields);
void tetheringFinished(bool);
};

namespace com {
Expand Down

0 comments on commit 84f6000

Please sign in to comment.