ncsi: implement the main function

This commit adds the functionality to
call the specific function depends on the
command given.

eg: Force a channel to be used.
ncsi-netlink --set -x 2 -p 0 -c 0
x: Interface Index
p: Package number
c: channel number

eg: Gets the info of a given interface
ncsi-netlink --info -x 2 -p 0
x: Interface Index
p: Package number

eg: Clear all the package/channel
ncsi-netlink --clear -x 2
x: Interface Index

This commit also removes the unnecessary
console messages.

Change-Id: I682f91452a3ab6362be0de8ac2658ffc9e160c29
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/ncsi_netlink_main.cpp b/ncsi_netlink_main.cpp
index 2441383..aa54956 100644
--- a/ncsi_netlink_main.cpp
+++ b/ncsi_netlink_main.cpp
@@ -14,9 +14,99 @@
  * limitations under the License.
  */
 
+#include "argument.hpp"
+#include "ncsi_util.hpp"
+
+#include <iostream>
+#include <string>
+
+static void exitWithError(const char* err, char** argv)
+{
+    phosphor::network::ncsi::ArgumentParser::usage(argv);
+    std::cerr << "ERROR: " << err << "\n";
+    exit(EXIT_FAILURE);
+}
+
 int main(int argc, char** argv)
 {
-    //TODO will implement later
+    using namespace phosphor::network;
+    using namespace phosphor::network::ncsi;
+    // Read arguments.
+    auto options = ArgumentParser(argc, argv);
+    int packageInt {};
+    int channelInt {};
+    int indexInt {};
+
+    // Parse out interface argument.
+    auto ifIndex = (options)["index"];
+    try
+    {
+        indexInt = stoi(ifIndex, nullptr);
+    }
+    catch (const std::exception& e)
+    {
+        exitWithError("Interface not specified.", argv);
+    }
+
+    if (indexInt < 0)
+    {
+        exitWithError("Interface value should be greater than equal to 0", argv);
+    }
+
+    // Parse out package argument.
+    auto package = (options)["package"];
+    try
+    {
+        packageInt = stoi(package, nullptr);
+    }
+    catch (const std::exception& e)
+    {
+         packageInt = DEFAULT_VALUE;
+    }
+
+    if (packageInt < 0)
+    {
+        packageInt = DEFAULT_VALUE;
+    }
+
+    // Parse out channel argument.
+    auto channel = (options)["channel"];
+    try
+    {
+        channelInt = stoi(channel, nullptr);
+    }
+    catch (const std::exception& e)
+    {
+         channelInt = DEFAULT_VALUE;
+    }
+
+    if (channelInt < 0)
+    {
+        channelInt = DEFAULT_VALUE;
+    }
+
+    auto setCmd = (options)["set"];
+    if (setCmd == "true")
+    {
+        // Can not perform set operation without pacakge.
+        if (packageInt == DEFAULT_VALUE)
+        {
+            exitWithError("Package not specified.", argv);
+        }
+        return ncsi::setChannel(indexInt, packageInt, channelInt);
+    }
+    else if ((options)["info"] == "true")
+    {
+        return ncsi::getInfo(indexInt, packageInt);
+    }
+    else if ((options)["clear"] == "true")
+    {
+        return  ncsi::clearInterface(indexInt);
+    }
+    else
+    {
+        exitWithError("No Command specified", argv);
+    }
     return 0;
 }
 
diff --git a/ncsi_util.cpp b/ncsi_util.cpp
index bf19ee3..77224f7 100644
--- a/ncsi_util.cpp
+++ b/ncsi_util.cpp
@@ -19,8 +19,6 @@
 using namespace phosphor::logging;
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 
-constexpr auto DEFAULT_VALUE = -1;
-constexpr auto NONE = 0;
 using CallBack = int(*)(struct nl_msg* msg, void* arg);
 
 namespace internal
@@ -131,21 +129,20 @@
                 }
                 else
                 {
-                    log<level::DEBUG>("Channel not Active",
-                            entry("CHANNEL=%x", channel));
-
+                    log<level::DEBUG>("Channel Not Active",
+                                      entry("CHANNEL=%x", channel));
                 }
 
                 if (channeltb[NCSI_CHANNEL_ATTR_FORCED])
                 {
                     log<level::DEBUG>("Channel is forced");
                 }
-
             }
             else
             {
                 log<level::DEBUG>("Channel with no ID");
             }
+
             if (channeltb[NCSI_CHANNEL_ATTR_VERSION_MAJOR])
             {
                 auto major = nla_get_u32(channeltb[NCSI_CHANNEL_ATTR_VERSION_MAJOR]);
@@ -166,9 +163,10 @@
             }
             if (channeltb[NCSI_CHANNEL_ATTR_LINK_STATE])
             {
+
                 auto link = nla_get_u32(channeltb[NCSI_CHANNEL_ATTR_LINK_STATE]);
                 log<level::DEBUG>("Channel Link State",
-                                  entry("STATE=%d", link));
+                                  entry("STATE=%x", link));
             }
             if (channeltb[NCSI_CHANNEL_ATTR_VLAN_LIST])
             {
@@ -181,7 +179,6 @@
                     auto id = nla_get_u16(vid);
                     log<level::DEBUG>("VID",
                                       entry("VID=%d", id));
-
                     vid = nla_next(vid, &len);
                 }
             }
@@ -287,18 +284,27 @@
 
 int setChannel(int ifindex, int package, int channel)
 {
+    log<level::DEBUG>("Set Channel",
+                      entry("CHANNEL=%x", channel),
+                      entry("PACKAGE=%x", package),
+                      entry("IFINDEX=%x", ifindex));
     return internal::applyCmd(ifindex, ncsi_nl_commands::NCSI_CMD_SET_INTERFACE,
                               package, channel);
 }
 
 int clearInterface(int ifindex)
 {
+    log<level::DEBUG>("ClearInterface",
+                      entry("IFINDEX=%x", ifindex));
     return internal::applyCmd(ifindex,
                               ncsi_nl_commands::NCSI_CMD_CLEAR_INTERFACE);
 }
 
 int getInfo(int ifindex, int package)
 {
+    log<level::DEBUG>("Get Info",
+                      entry("PACKAGE=%x", package),
+                      entry("IFINDEX=%x", ifindex));
     if (package == DEFAULT_VALUE)
     {
         return internal::applyCmd(ifindex, ncsi_nl_commands::NCSI_CMD_PKG_INFO,
diff --git a/ncsi_util.hpp b/ncsi_util.hpp
index 570152b..db92269 100644
--- a/ncsi_util.hpp
+++ b/ncsi_util.hpp
@@ -5,6 +5,9 @@
 namespace ncsi
 {
 
+constexpr auto DEFAULT_VALUE = -1;
+constexpr auto NONE = 0;
+
 /* @brief  This function will ask underlying NCSI driver
  *         to set a specific  package or package/channel
  *         combination as the preferred choice.