OSDN Git Service

Added bluetooth reverse tether start/stop cmd
authorzzy <zhenye@broadcom.com>
Sun, 15 Apr 2012 00:24:24 +0000 (17:24 -0700)
committerMatthew Xie <mattx@google.com>
Sat, 14 Jul 2012 09:17:41 +0000 (02:17 -0700)
CommandListener.cpp
TetherController.cpp
TetherController.h

index 510f09c..110dba6 100644 (file)
@@ -611,6 +611,12 @@ int CommandListener::TetherCmd::runCommand(SocketClient *cli,
 
     if (!strcmp(argv[1], "stop")) {
         rc = sTetherCtrl->stopTethering();
+    } else if(!strcmp(argv[1], "start-reverse")) {
+        LOGD("CommandListener::TetherCmd::run, call startReverseTethering, iface:%s", argv[2]);
+        sTetherCtrl->startReverseTethering(argv[2]);
+    } else if (!strcmp(argv[1], "stop-reverse")) {
+        LOGD("CommandListener::TetherCmd::run, call stopReverseTethering");
+        rc = sTetherCtrl->stopReverseTethering();
     } else if (!strcmp(argv[1], "status")) {
         char *tmp = NULL;
 
index 8d14a14..7a470c7 100644 (file)
@@ -38,6 +38,7 @@ TetherController::TetherController() {
     mDnsForwarders = new NetAddressCollection();
     mDaemonFd = -1;
     mDaemonPid = 0;
+    mDhcpcdPid = 0;
 }
 
 TetherController::~TetherController() {
@@ -186,7 +187,69 @@ int TetherController::stopTethering() {
     ALOGD("Tethering services stopped");
     return 0;
 }
+int TetherController::startReverseTethering(const char* iface) {
+    if (mDhcpcdPid != 0) {
+        LOGE("Reverse tethering already started");
+        errno = EBUSY;
+        return -1;
+    }
+
+    LOGD("TetherController::startReverseTethering, Starting reverse tethering");
+
+    /*
+     * TODO: Create a monitoring thread to handle and restart
+     * the daemon if it exits prematurely
+     */
+    //cleanup the dhcp result
+    char dhcp_result_name[64];
+    snprintf(dhcp_result_name, sizeof(dhcp_result_name) - 1, "dhcp.%s.result", iface);
+    property_set(dhcp_result_name, "");
+
+    pid_t pid;
+    if ((pid = fork()) < 0) {
+        LOGE("fork failed (%s)", strerror(errno));
+        return -1;
+    }
+
+    if (!pid) {
 
+        char *args[10];
+        int argc = 0;
+        args[argc++] = "/system/bin/dhcpcd";
+        char host_name[128];
+        if (property_get("net.hostname", host_name, NULL) && (host_name[0] != '\0'))
+        {
+            args[argc++] = "-h";
+            args[argc++] = host_name;
+        }
+        args[argc++] = (char*)iface;
+        args[argc] = NULL;
+        if (execv(args[0], args)) {
+            LOGE("startReverseTethering, execv failed (%s)", strerror(errno));
+        }
+        LOGE("startReverseTethering, Should never get here!");
+        return 0;
+    } else {
+        mDhcpcdPid = pid;
+        LOGD("Reverse Tethering running, pid:%d", pid);
+    }
+    return 0;
+}
+int TetherController::stopReverseTethering() {
+
+    if (mDhcpcdPid == 0) {
+        LOGE("Tethering already stopped");
+        return 0;
+    }
+
+    LOGD("Stopping tethering services");
+
+    kill(mDhcpcdPid, SIGTERM);
+    waitpid(mDhcpcdPid, NULL, 0);
+    mDhcpcdPid = 0;
+    LOGD("Tethering services stopped");
+    return 0;
+}
 bool TetherController::isTetheringStarted() {
     return (mDaemonPid == 0 ? false : true);
 }
index eef94fe..463774b 100644 (file)
@@ -28,6 +28,7 @@ class TetherController {
     InterfaceCollection  *mInterfaces;
     NetAddressCollection *mDnsForwarders;
     pid_t                 mDaemonPid;
+    pid_t                 mDhcpcdPid;
     int                   mDaemonFd;
 
 public:
@@ -42,6 +43,9 @@ public:
     int stopTethering();
     bool isTetheringStarted();
 
+    int startReverseTethering(const char* iface);
+    int stopReverseTethering();
+
     int setDnsForwarders(char **servers, int numServers);
     NetAddressCollection *getDnsForwarders();