netipmid: Manage and expose session object

Session (RMCP+) is managed by net-ipmid directly, but
session commands has to be supported in both LAN & other
session-less interfaces. In order to make session commands
to work in other interfaces, session objects must be
exposed as D-Bus objects, so that ipmi-providers can
query the same.

Tested:
 1. Verified that RMCP+ session are perfectly working
 2. Verified RMCP+ session establishment fails for wrong
    password
 3. Verified that session privilege level are maintained
    and access are restricted accordingly
 4. Verified session timeout and sessions are destroyed
    accordingly after timeout
 5. verified max session count working behavior
 6. verified ipmi-providers responding with proper response for this
    (or D-Bus objects are exposed correctly during session creation,
    session deletion,session update,
    (like privilege, - say even set session privilege level command)
 7.Session objects are created dynamically.

Change-Id: I78a8449359877ef6cc4cd8161d8c67e6e54eb52b
Signed-off-by: Suryakanth Sekar <suryakanth.sekar@linux.intel.com>
diff --git a/session.hpp b/session.hpp
index 7b02221..4d4bf19 100644
--- a/session.hpp
+++ b/session.hpp
@@ -9,12 +9,18 @@
 
 #include <chrono>
 #include <exception>
+#include <ipmid/api.hpp>
+#include <ipmid/sessiondef.hpp>
 #include <list>
 #include <memory>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
 #include <string>
+#include <unordered_map>
 #include <user_channel/channel_layer.hpp>
 #include <user_channel/user_layer.hpp>
 #include <vector>
+#include <xyz/openbmc_project/Ipmi/SessionInfo/server.hpp>
 
 namespace session
 {
@@ -32,14 +38,6 @@
     OEM,
 };
 
-enum class State
-{
-    INACTIVE,              // Session is not in use
-    SETUP_IN_PROGRESS,     // Session Setup Sequence is progressing
-    ACTIVE,                // Session is active
-    TEAR_DOWN_IN_PROGRESS, // When Closing Session
-};
-
 // Seconds of inactivity allowed during session setup stage
 constexpr auto SESSION_SETUP_TIMEOUT = 5s;
 // Seconds of inactivity allowed when session is active
@@ -98,7 +96,11 @@
  * active sessions established with the BMC. It is recommended that a BMC
  * implementation support at least four simultaneous sessions
  */
-class Session
+
+using SessionIface = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Ipmi::server::SessionInfo>;
+
+class Session : public SessionIface
 {
   public:
     Session() = default;
@@ -117,10 +119,14 @@
      * @param[in] inRemoteConsoleSessID - Remote Console Session ID
      * @param[in] priv - Privilege Level requested in the Command
      */
-    Session(SessionID inRemoteConsoleSessID, Privilege priv) :
-        reqMaxPrivLevel(priv), bmcSessionID(crypto::prng::rand()),
-        remoteConsoleSessionID(inRemoteConsoleSessID)
+    Session(sdbusplus::bus::bus& bus, const char* path,
+            SessionID inRemoteConsoleSessID, SessionID BMCSessionID,
+            char priv) :
+        SessionIface(bus, path)
     {
+        reqMaxPrivLevel = static_cast<session::Privilege>(priv);
+        bmcSessionID = BMCSessionID;
+        remoteConsoleSessionID = inRemoteConsoleSessID;
     }
 
     auto getBMCSessionID() const
@@ -238,21 +244,23 @@
      * transaction time is compared against the session inactivity timeout.
      *
      */
-    bool isSessionActive()
+    bool isSessionActive(uint8_t sessionState)
     {
         auto currentTime = std::chrono::steady_clock::now();
         auto elapsedSeconds = std::chrono::duration_cast<std::chrono::seconds>(
             currentTime - lastTime);
 
+        State state = static_cast<session::State>(sessionState);
+
         switch (state)
         {
-            case State::SETUP_IN_PROGRESS:
+            case State::setupInProgress:
                 if (elapsedSeconds < SESSION_SETUP_TIMEOUT)
                 {
                     return true;
                 }
                 break;
-            case State::ACTIVE:
+            case State::active:
                 if (elapsedSeconds < SESSION_INACTIVITY_TIMEOUT)
                 {
                     return true;
@@ -265,11 +273,6 @@
     }
 
     /**
-     * @brief Session's Current Privilege Level
-     */
-    Privilege curPrivLevel = Privilege::CALLBACK;
-
-    /**
      * @brief Session's Requested Maximum Privilege Level
      */
     Privilege reqMaxPrivLevel;
@@ -280,13 +283,11 @@
     ipmi::PrivAccess sessionUserPrivAccess{};
     ipmi::ChannelAccess sessionChannelAccess{};
 
-    SequenceNumbers sequenceNums;  // Session Sequence Numbers
-    State state = State::INACTIVE; // Session State
-    std::string userName{};        // User Name
+    SequenceNumbers sequenceNums; // Session Sequence Numbers
+    std::string userName{};       // User Name
 
     /** @brief Socket channel for communicating with the remote client.*/
     std::shared_ptr<udpsocket::Channel> channelPtr;
-    uint8_t chNum;
 
   private:
     SessionID bmcSessionID = 0;           // BMC Session ID