summaryrefslogtreecommitdiffstats
path: root/src/qconnectivity
diff options
context:
space:
mode:
authorGatis Paeglis <gatis.paeglis@digia.com>2014-02-11 17:05:27 +0100
committerGatis Paeglis <gatis.paeglis@digia.com>2014-02-14 11:27:48 +0200
commit2ae4b46d8e117aa645a05dd150050bb851c2333e (patch)
tree1350875bb1af96a213aee4d31c69d0b1b63a817b /src/qconnectivity
parent69e4deb9dc7432d6c3cbff371f5fa3016d517c93 (diff)
Enable internet on eAndroid emulator
Change-Id: If357f7c1ea090165cb0a3738156a66f5961d66a6 Reviewed-by: Rainer Keller <rainer.keller@digia.com>
Diffstat (limited to 'src/qconnectivity')
-rw-r--r--src/qconnectivity/main.cpp47
1 files changed, 25 insertions, 22 deletions
diff --git a/src/qconnectivity/main.cpp b/src/qconnectivity/main.cpp
index fb8b857..046375e 100644
--- a/src/qconnectivity/main.cpp
+++ b/src/qconnectivity/main.cpp
@@ -38,7 +38,8 @@ static bool QT_USE_EXPIRED_LEASE = !qgetenv("QT_USE_EXPIRED_LEASE").isEmpty();
// this might indicate a badly configured DHCP server
static int MIN_RENEWAL_TIME_SECS = 300; // 5 min
-#define ETH_INTERFACE "eth0"
+#define ETH_INTERFACE_HW "eth0"
+#define ETH_INTERFACE_EMULATOR "eth1"
// this function is defined in android/system/core/libnetutils/dhcp_utils.c
extern "C" {
@@ -67,6 +68,7 @@ protected:
bool startDhcp(bool renew, const char *interface);
void stopDhcp(const char *interface);
bool ethernetSupported() const;
+ bool isEmulator() const;
protected slots:
void initNetdConnection();
@@ -76,7 +78,6 @@ protected slots:
void sendReply(QLocalSocket *requester, const QByteArray &reply) const;
void updateLease();
void handleError(QLocalSocket::LocalSocketError /*socketError*/) const;
- bool isEmulator() const;
private:
friend class LeaseTimer;
@@ -86,6 +87,8 @@ private:
QLocalServer *m_serverSocket;
bool m_linkUp;
LeaseTimer *m_leaseTimer;
+ bool m_isEmulator;
+ QByteArray m_ethInterface;
};
class LeaseTimer : public QTimer
@@ -116,13 +119,11 @@ private:
};
QConnectivityDaemon::QConnectivityDaemon()
- : m_netdSocket(0), m_serverSocket(0), m_linkUp(false), m_leaseTimer(0)
+ : m_netdSocket(0), m_serverSocket(0), m_linkUp(false), m_leaseTimer(0), m_isEmulator(isEmulator())
{
qDebug() << "starting QConnectivityDaemon...";
- if (isEmulator()) {
- // ### TODO - init Internet setup on emulator
- } else {
- initNetdConnection();
+ if (!m_isEmulator) {
+ m_ethInterface = ETH_INTERFACE_HW;
m_leaseTimer = new LeaseTimer(this);
m_leaseTimer->setSingleShot(true);
connect(m_leaseTimer, SIGNAL(timeout()), this, SLOT(updateLease()));
@@ -137,29 +138,30 @@ QConnectivityDaemon::QConnectivityDaemon()
} else {
qWarning() << "QConnectivityDaemon: failed to open qconnectivity server socket";
}
+ } else {
+ m_ethInterface = ETH_INTERFACE_EMULATOR;
}
+ initNetdConnection();
}
bool QConnectivityDaemon::isEmulator() const
{
bool isEmulator = false;
- QString content;
QFile conf("/system/bin/appcontroller.conf");
if (conf.open(QIODevice::ReadOnly)) {
- QTextStream stream(&conf);
- content = stream.readAll();
+ QByteArray content = conf.readAll();
isEmulator = content.contains("platform=emulator");
+ conf.close();
} else {
qWarning() << "Failed to read appcontroller.conf";
}
- conf.close();
return isEmulator;
}
void QConnectivityDaemon::initNetdConnection()
{
- static int attemptCount = 12;
if (ethernetSupported()) {
+ static int attemptCount = 12;
int netdFd = socket_local_client("netd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
if (netdFd != -1) {
qDebug() << "QConnectivityDaemon: connected to netd socket";
@@ -170,9 +172,8 @@ void QConnectivityDaemon::initNetdConnection()
this, SLOT(handleError(QLocalSocket::LocalSocketError)));
// down-up sequence generates "linkstate" events, which we can use to setup
// our daemon on initial startup or on daemon restarts
- stopDhcp(ETH_INTERFACE);
- sendCommand("0 interface setcfg " ETH_INTERFACE " down");
- sendCommand("0 interface setcfg " ETH_INTERFACE " up");
+ sendCommand(QByteArray("0 interface setcfg ").append(m_ethInterface).append(" down").constData());
+ sendCommand(QByteArray("0 interface setcfg ").append(m_ethInterface).append(" up").constData());
} else {
qWarning() << "QConnectivityDaemon: failed to connect to netd socket";
if (--attemptCount != 0)
@@ -218,18 +219,19 @@ void QConnectivityDaemon::handleInterfaceChange(const QList<QByteArray> &message
qWarning() << "QConnectivityDaemon: broken command";
return;
}
- if (message.at(2) == "linkstate" && message.at(3) == ETH_INTERFACE) {
+
+ if (message.at(2) == "linkstate" && message.at(3) == m_ethInterface) {
if (message.at(4) == "up") {
// ethernet cable has been plugged in
if (!m_linkUp) {
m_linkUp = true;
- startDhcp(false, ETH_INTERFACE);
+ startDhcp(false, m_ethInterface);
}
} else {
// .. plugged out
if (m_linkUp) {
m_linkUp = false;
- stopDhcp(ETH_INTERFACE);
+ stopDhcp(m_ethInterface);
}
}
}
@@ -284,7 +286,7 @@ bool QConnectivityDaemon::startDhcp(bool renew, const char *interface)
property_set("net.dns2", dns2);
}
- if (lease >= 0) {
+ if (!m_isEmulator && lease >= 0) {
if (lease < MIN_RENEWAL_TIME_SECS) {
qWarning() << "QConnectivityDaemon: DHCP server proposes lease time " << lease
<< "seconds. We will use" << MIN_RENEWAL_TIME_SECS << " seconds instead.";
@@ -317,20 +319,21 @@ void QConnectivityDaemon::stopDhcp(const char *interface)
qDebug() << "QConnectivityDaemon: stopDhcp: " << interface;
ifc_clear_addresses(interface);
dhcp_stop(interface);
- if (m_leaseTimer->isActive())
+ if (!m_isEmulator && m_leaseTimer->isActive())
m_leaseTimer->stop();
}
bool QConnectivityDaemon::ethernetSupported() const
{
// standard linux kernel path
- return QDir().exists("/sys/class/net/" ETH_INTERFACE);
+ return QDir().exists(QString("/sys/class/net/").append(m_ethInterface));
}
void QConnectivityDaemon::handleNetdEvent()
{
QByteArray data = m_netdSocket->readAll();
- if (QT_CONNECTIVITY_DEBUG) qDebug() << "QConnectivityDaemon: netd event: " << data;
+ if (QT_CONNECTIVITY_DEBUG)
+ qDebug() << "QConnectivityDaemon: netd event: " << data;
if (data.endsWith('\0'))
data.chop(1);