netd: Add preliminary support for BT PAN

Signed-off-by: San Mehat <san@google.com>
diff --git a/Android.mk b/Android.mk
index 678a5b9..4b9b6c9 100644
--- a/Android.mk
+++ b/Android.mk
@@ -18,15 +18,18 @@
                   logwrapper.c                         \
                   TetherController.cpp                 \
                   NatController.cpp                    \
-                  PppController.cpp
+                  PppController.cpp                    \
+                  PanController.cpp
 
 LOCAL_MODULE:= netd
 
-LOCAL_C_INCLUDES := $(KERNEL_HEADERS) -I../../frameworks/base/include/
+LOCAL_C_INCLUDES := $(KERNEL_HEADERS) -I../../frameworks/base/include/ \
+                    $(LOCAL_PATH)/../bluetooth/bluedroid/include \
+                    $(LOCAL_PATH)/../bluetooth/bluez-clean-headers
 
 LOCAL_CFLAGS := 
 
-LOCAL_SHARED_LIBRARIES := libsysutils libcutils
+LOCAL_SHARED_LIBRARIES := libsysutils libcutils libbluedroid
 
 include $(BUILD_EXECUTABLE)
 
diff --git a/CommandListener.cpp b/CommandListener.cpp
index 774d018..448ae1d 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -33,6 +33,7 @@
 TetherController *CommandListener::sTetherCtrl = NULL;
 NatController *CommandListener::sNatCtrl = NULL;
 PppController *CommandListener::sPppCtrl = NULL;
+PanController *CommandListener::sPanCtrl = NULL;
 
 CommandListener::CommandListener() :
                  FrameworkListener("netd") {
@@ -42,6 +43,7 @@
     registerCmd(new NatCmd());
     registerCmd(new ListTtysCmd());
     registerCmd(new PppdCmd());
+    registerCmd(new PanCmd());
 
     if (!sTetherCtrl)
         sTetherCtrl = new TetherController();
@@ -49,6 +51,8 @@
         sNatCtrl = new NatController();
     if (!sPppCtrl)
         sPppCtrl = new PppController();
+    if (!sPanCtrl)
+        sPanCtrl = new PanController();
 }
 
 CommandListener::ListInterfacesCmd::ListInterfacesCmd() :
@@ -279,3 +283,42 @@
 
     return 0;
 }
+
+CommandListener::PanCmd::PanCmd() :
+                 NetdCommand("pan") {
+}
+
+int CommandListener::PanCmd::runCommand(SocketClient *cli,
+                                        int argc, char **argv) {
+    int rc = 0;
+
+    if (argc < 2) {
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing argument", false);
+        return 0;
+    }
+
+    if (!strcmp(argv[1], "start")) {
+        rc = sPanCtrl->startPan();
+    } else if (!strcmp(argv[1], "stop")) {
+        rc = sPanCtrl->stopPan();
+    } else if (!strcmp(argv[1], "status")) {
+        char *tmp = NULL;
+
+        asprintf(&tmp, "Pan services %s",
+                 (sPanCtrl->isPanStarted() ? "started" : "stopped"));
+        cli->sendMsg(ResponseCode::PanStatusResult, tmp, false);
+        free(tmp);
+        return 0;
+    } else {
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown pan cmd", false);
+        return 0;
+    }
+
+    if (!rc) {
+        cli->sendMsg(ResponseCode::CommandOkay, "Pan operation succeeded", false);
+    } else {
+        cli->sendMsg(ResponseCode::OperationFailed, "Pan operation failed", true);
+    }
+
+    return 0;
+}
diff --git a/CommandListener.h b/CommandListener.h
index 55a039d..c005c59 100644
--- a/CommandListener.h
+++ b/CommandListener.h
@@ -23,11 +23,13 @@
 #include "TetherController.h"
 #include "NatController.h"
 #include "PppController.h"
+#include "PanController.h"
 
 class CommandListener : public FrameworkListener {
     static TetherController *sTetherCtrl;
     static NatController *sNatCtrl;
     static PppController *sPppCtrl;
+    static PanController *sPanCtrl;
 
 public:
     CommandListener();
@@ -76,6 +78,13 @@
         virtual ~PppdCmd() {}
         int runCommand(SocketClient *c, int argc, char ** argv);
     };
+
+    class PanCmd : public NetdCommand {
+    public:
+        PanCmd();
+        virtual ~PanCmd() {}
+        int runCommand(SocketClient *c, int argc, char ** argv);
+    };
 };
 
 #endif
diff --git a/PanController.cpp b/PanController.cpp
new file mode 100644
index 0000000..f4ae8ee
--- /dev/null
+++ b/PanController.cpp
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <bluedroid/bluetooth.h>
+
+#define LOG_TAG "PanController"
+#include <cutils/log.h>
+
+#include "PanController.h"
+
+extern "C" int bt_is_enabled();
+
+PanController::PanController() {
+    mPid = 0;
+}
+
+PanController::~PanController() {
+}
+
+int PanController::startPan() {
+    pid_t pid;
+
+    if (!bt_is_enabled()) {
+        LOGE("Cannot start PAN services - Bluetooth not running");
+        errno = ENODEV;
+        return -1;
+    }
+
+    if (mPid) {
+        LOGE("PAN already started");
+        errno = EBUSY;
+        return -1;
+    }
+
+   if ((pid = fork()) < 0) {
+        LOGE("fork failed (%s)", strerror(errno));
+        return -1;
+    }
+
+    if (!pid) {
+        if (execl("/system/bin/pand", "/system/bin/pand", "--nodetach", "--listen",
+                  "--role", "NAP", (char *) NULL)) {
+            LOGE("execl failed (%s)", strerror(errno));
+        }
+        LOGE("Should never get here!");
+        return 0;
+    } else {
+        mPid = pid;
+    }
+    return 0;
+
+}
+
+int PanController::stopPan() {
+    if (mPid == 0) {
+        LOGE("PAN already stopped");
+        return 0;
+    }
+
+    LOGD("Stopping PAN services");
+    kill(mPid, SIGTERM);
+    waitpid(mPid, NULL, 0);
+    mPid = 0;
+    LOGD("PAN services stopped");
+    return 0;
+}
+
+bool PanController::isPanStarted() {
+    return (mPid != 0 ? true : false);
+}
diff --git a/PanController.h b/PanController.h
new file mode 100644
index 0000000..4f52fee
--- /dev/null
+++ b/PanController.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _PAN_CONTROLLER_H
+#define _PAN_CONTROLLER_H
+
+#include <linux/in.h>
+
+#include <utils/List.h>
+
+class PanController {
+    pid_t mPid;
+
+public:
+    PanController();
+    virtual ~PanController();
+
+    int startPan();
+    int stopPan();
+    bool isPanStarted();
+};
+
+#endif
diff --git a/ResponseCode.h b/ResponseCode.h
index 6c1c238..cf818ae 100644
--- a/ResponseCode.h
+++ b/ResponseCode.h
@@ -32,6 +32,7 @@
     static const int CommandOkay              = 200;
     static const int TetherStatusResult       = 210;
     static const int IpFwdStatusResult        = 211;
+    static const int PanStatusResult          = 212;
 
     // 400 series - The command was accepted but the requested action
     // did not take place.