netd: Add Softap controller skeleton functions

Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
diff --git a/Android.mk b/Android.mk
index 2f2422e..ce73697 100644
--- a/Android.mk
+++ b/Android.mk
@@ -11,7 +11,7 @@
 
 LOCAL_SRC_FILES:=                                      \
                   main.cpp                             \
-		  CommandListener.cpp                  \
+                  CommandListener.cpp                  \
                   NetdCommand.cpp                      \
                   NetlinkManager.cpp                   \
                   NetlinkHandler.cpp                   \
@@ -19,7 +19,8 @@
                   TetherController.cpp                 \
                   NatController.cpp                    \
                   PppController.cpp                    \
-                  PanController.cpp
+                  PanController.cpp                    \
+                  SoftapController.cpp
 
 LOCAL_MODULE:= netd
 
diff --git a/CommandListener.cpp b/CommandListener.cpp
index eba88de..0bbab15 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -44,6 +44,7 @@
 NatController *CommandListener::sNatCtrl = NULL;
 PppController *CommandListener::sPppCtrl = NULL;
 PanController *CommandListener::sPanCtrl = NULL;
+SoftapController *CommandListener::sSoftapCtrl = NULL;
 
 CommandListener::CommandListener() :
                  FrameworkListener("netd") {
@@ -54,6 +55,7 @@
     registerCmd(new ListTtysCmd());
     registerCmd(new PppdCmd());
     registerCmd(new PanCmd());
+    registerCmd(new SoftapCmd());
 
     if (!sTetherCtrl)
         sTetherCtrl = new TetherController();
@@ -63,6 +65,8 @@
         sPppCtrl = new PppController();
     if (!sPanCtrl)
         sPanCtrl = new PanController();
+    if (!sSoftapCtrl)
+        sSoftapCtrl = new SoftapController();
 }
 
 CommandListener::InterfaceCmd::InterfaceCmd() :
@@ -471,3 +475,44 @@
 
     return 0;
 }
+
+CommandListener::SoftapCmd::SoftapCmd() :
+                 NetdCommand("softap") {
+}
+
+int CommandListener::SoftapCmd::runCommand(SocketClient *cli,
+                                        int argc, char **argv) {
+    int rc = 0;
+
+    if (argc < 2) {
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Softap Missing argument", false);
+        return 0;
+    }
+
+    if (!strcmp(argv[1], "start")) {
+        rc = sSoftapCtrl->startSoftap();
+    } else if (!strcmp(argv[1], "stop")) {
+        rc = sSoftapCtrl->stopSoftap();
+    } else if (!strcmp(argv[1], "status")) {
+        char *tmp = NULL;
+
+        asprintf(&tmp, "Softap service %s",
+                 (sSoftapCtrl->isSoftapStarted() ? "started" : "stopped"));
+        cli->sendMsg(ResponseCode::SoftapStatusResult, tmp, false);
+        free(tmp);
+        return 0;
+    } else if (!strcmp(argv[1], "set")) {
+        rc = sSoftapCtrl->setSoftap(argc, argv);
+    } else {
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Softap Unknown cmd", false);
+        return 0;
+    }
+
+    if (!rc) {
+        cli->sendMsg(ResponseCode::CommandOkay, "Softap operation succeeded", false);
+    } else {
+        cli->sendMsg(ResponseCode::OperationFailed, "Softap operation failed", true);
+    }
+
+    return 0;
+}
diff --git a/CommandListener.h b/CommandListener.h
index 84476dd..82bebdd 100644
--- a/CommandListener.h
+++ b/CommandListener.h
@@ -24,12 +24,14 @@
 #include "NatController.h"
 #include "PppController.h"
 #include "PanController.h"
+#include "SoftapController.h"
 
 class CommandListener : public FrameworkListener {
     static TetherController *sTetherCtrl;
     static NatController *sNatCtrl;
     static PppController *sPppCtrl;
     static PanController *sPanCtrl;
+    static SoftapController *sSoftapCtrl;
 
 public:
     CommandListener();
@@ -37,6 +39,13 @@
 
 private:
 
+    class SoftapCmd : public NetdCommand {
+    public:
+        SoftapCmd();
+        virtual ~SoftapCmd() {}
+        int runCommand(SocketClient *c, int argc, char ** argv);
+    };
+
     class InterfaceCmd : public NetdCommand {
     public:
         InterfaceCmd();
diff --git a/ResponseCode.h b/ResponseCode.h
index de54e8b..f622254 100644
--- a/ResponseCode.h
+++ b/ResponseCode.h
@@ -34,6 +34,7 @@
     static const int IpFwdStatusResult        = 211;
     static const int PanStatusResult          = 212;
     static const int InterfaceGetCfgResult    = 213;
+    static const int SoftapStatusResult       = 214;
 
     // 400 series - The command was accepted but the requested action
     // did not take place.
diff --git a/SoftapController.cpp b/SoftapController.cpp
new file mode 100644
index 0000000..88bae5c
--- /dev/null
+++ b/SoftapController.cpp
@@ -0,0 +1,89 @@
+/*
+ * 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>
+
+#define LOG_TAG "SoftapController"
+#include <cutils/log.h>
+
+#include "SoftapController.h"
+
+SoftapController::SoftapController() {
+    mPid = 0;
+}
+
+SoftapController::~SoftapController() {
+}
+
+int SoftapController::startSoftap() {
+    pid_t pid = 1;
+
+    LOGD("Softap start");
+    if (mPid) {
+        LOGE("Softap already started");
+        errno = EBUSY;
+        return -1;
+    }
+#if 0
+   if ((pid = fork()) < 0) {
+        LOGE("fork failed (%s)", strerror(errno));
+        return -1;
+    }
+#endif
+    if (!pid) {
+        LOGE("Softap Started");
+        return 0;
+    } else {
+        mPid = pid;
+    }
+    return 0;
+
+}
+
+int SoftapController::stopSoftap() {
+    LOGD("Softap stop");
+    if (mPid == 0) {
+        LOGE("Softap already stopped");
+        return 0;
+    }
+#if 0
+    LOGD("Stopping Softap service");
+    kill(mPid, SIGTERM);
+    waitpid(mPid, NULL, 0);
+#endif
+    mPid = 0;
+    LOGD("Softap service stopped");
+    return 0;
+}
+
+bool SoftapController::isSoftapStarted() {
+    return (mPid != 0 ? true : false);
+}
+
+int SoftapController::setSoftap(int argc, char *argv[]) {
+    LOGD("Softap set");
+    return 0;
+}
diff --git a/SoftapController.h b/SoftapController.h
new file mode 100644
index 0000000..1af113d
--- /dev/null
+++ b/SoftapController.h
@@ -0,0 +1,37 @@
+/*
+ * 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 _SOFTAP_CONTROLLER_H
+#define _SOFTAP_CONTROLLER_H
+
+#include <linux/in.h>
+
+#include <utils/List.h>
+
+class SoftapController {
+    pid_t mPid;
+
+public:
+    SoftapController();
+    virtual ~SoftapController();
+
+    int startSoftap();
+    int stopSoftap();
+    bool isSoftapStarted();
+    int setSoftap(int argc, char *argv[]);
+};
+
+#endif