Add transaction support to Set Lan Cmd and Set Channel Access Cmd

* Ipmid will cache single setting to memory by Set Lan cmd
* Add Set Channel Access cmd to ipmid
* Set Channel Access cmd is used to apply the previous cached settings

TODO: parse the request data of Set Channel Access cmd if needed.

Resolves openbmc/openbmc#444

Change-Id: Ia7ac57a551731920ffe69dad5c69c995521fed02
Signed-off-by: Nan Li <william.bjlinan@hotmail.com>
diff --git a/transporthandler.cpp b/transporthandler.cpp
index 32ed993..8ee2dbe 100644
--- a/transporthandler.cpp
+++ b/transporthandler.cpp
@@ -23,9 +23,7 @@
 
 const int SIZE_MAC = 18; //xx:xx:xx:xx:xx:xx
 
-char new_ipaddr  [INET_ADDRSTRLEN] = "";
-char new_netmask [INET_ADDRSTRLEN] = "";
-char new_gateway [INET_ADDRSTRLEN] = "";
+struct channel_config_t channel_config;
 
 const uint8_t SET_COMPLETE = 0;
 const uint8_t SET_IN_PROGRESS = 1;
@@ -39,8 +37,8 @@
 
 void register_netfn_transport_functions() __attribute__((constructor));
 
-// Helper Function to get IP Address/NetMask/Gateway from Network Manager or Cache
-// based on Set-In-Progress State
+// Helper Function to get IP Address/NetMask/Gateway from Network Manager or
+// Cache based on Set-In-Progress State
 ipmi_ret_t getNetworkData(uint8_t lan_param, uint8_t * data)
 {
     sd_bus *bus = ipmid_get_sd_bus_connection();
@@ -59,6 +57,7 @@
     if (r < 0) {
         fprintf(stderr, "Failed to get %s bus name: %s\n",
                 obj, strerror(-r));
+        rc = IPMI_CC_UNSPECIFIED_ERROR;
         goto cleanup;
     }
     r = sd_bus_call_method(bus, app, obj, ifc, "GetAddress4", &error,
@@ -70,7 +69,8 @@
         goto cleanup;
     }
 
-    r = sd_bus_message_read(reply, "iyss", &family, &prefixlen, &ipaddr, &gateway);
+    r = sd_bus_message_read(reply, "iyss",
+                            &family, &prefixlen, &ipaddr, &gateway);
     if(r < 0)
     {
         fprintf(stderr, "Failed to get a response: %s\n", strerror(-rc));
@@ -78,8 +78,12 @@
         goto cleanup;
     }
 
-    printf("N/W data from HW %s:%d:%s:%s\n", family==AF_INET?"IPv4":"IPv6", prefixlen, ipaddr,gateway);
-    printf("N/W data from Cache: %s:%s:%s\n", new_ipaddr, new_netmask, new_gateway);
+    printf("N/W data from HW %s:%d:%s:%s\n",
+            family==AF_INET?"IPv4":"IPv6", prefixlen, ipaddr,gateway);
+    printf("N/W data from Cache: %s:%s:%s\n",
+            channel_config.new_ipaddr.c_str(),
+            channel_config.new_netmask.c_str(),
+            channel_config.new_gateway.c_str());
 
     if(lan_param == LAN_PARM_IP)
     {
@@ -90,7 +94,7 @@
         }
         else if(lan_set_in_progress == SET_IN_PROGRESS)
         {
-            inet_pton(AF_INET, new_ipaddr, (void *)data);
+            inet_pton(AF_INET, channel_config.new_ipaddr.c_str(), (void *)data);
         }
     }
     else if(lan_param == LAN_PARM_SUBNET)
@@ -102,7 +106,7 @@
          }
          else if(lan_set_in_progress == SET_IN_PROGRESS)
          {
-             inet_pton(AF_INET, new_netmask, (void *)data);
+             inet_pton(AF_INET, channel_config.new_netmask.c_str(), (void *)data);
          }
     }
     else if(lan_param == LAN_PARM_GATEWAY)
@@ -114,7 +118,7 @@
          }
          else if(lan_set_in_progress == SET_IN_PROGRESS)
          {
-             inet_pton(AF_INET, new_gateway,(void *)data);
+             inet_pton(AF_INET, channel_config.new_gateway.c_str(),(void *)data);
          }
     }
     else
@@ -154,26 +158,24 @@
     ipmi_ret_t rc = IPMI_CC_OK;
     *data_len = 0;
     sd_bus *bus = ipmid_get_sd_bus_connection();
-    sd_bus_message *reply = NULL;
+    sd_bus_message *reply = nullptr;
     sd_bus_error error = SD_BUS_ERROR_NULL;
     int r = 0;
-    char *app = NULL;
+    char *app = nullptr;
+
+    char tmp_ipaddr[INET_ADDRSTRLEN];
+    char tmp_netmask[INET_ADDRSTRLEN];
+    char tmp_gateway[INET_ADDRSTRLEN];
 
     printf("IPMI SET_LAN\n");
 
     set_lan_t *reqptr = (set_lan_t*) request;
 
-    // TODO Use dbus interface once available. For now use cmd line.
-    // TODO Add the rest of the parameters like setting auth type
-    // TODO Add error handling
-
-    if (reqptr->parameter == LAN_PARM_IP)
-    {
-        snprintf(new_ipaddr, INET_ADDRSTRLEN, "%d.%d.%d.%d",
+    if (reqptr->parameter == LAN_PARM_IP) {
+        snprintf(tmp_ipaddr, INET_ADDRSTRLEN, "%d.%d.%d.%d",
             reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
-    }
-    else if (reqptr->parameter == LAN_PARM_MAC)
-    {
+        channel_config.new_ipaddr.assign(tmp_ipaddr);
+    } else if (reqptr->parameter == LAN_PARM_MAC) {
         char mac[SIZE_MAC];
 
         snprintf(mac, SIZE_MAC, "%02x:%02x:%02x:%02x:%02x:%02x",
@@ -192,63 +194,36 @@
         }
         r = sd_bus_call_method(bus, app, obj, ifc, "SetHwAddress", &error,
                                 &reply, "ss", nwinterface, mac);
-        if(r < 0)
-        {
+        if (r < 0) {
             fprintf(stderr, "Failed to call the method: %s\n", strerror(-r));
             rc = IPMI_CC_UNSPECIFIED_ERROR;
         }
-    }
-    else if (reqptr->parameter == LAN_PARM_SUBNET)
+    } else if (reqptr->parameter == LAN_PARM_SUBNET)
     {
-        snprintf(new_netmask, INET_ADDRSTRLEN, "%d.%d.%d.%d",
+        snprintf(tmp_netmask, INET_ADDRSTRLEN, "%d.%d.%d.%d",
             reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
-    }
-    else if (reqptr->parameter == LAN_PARM_GATEWAY)
+        channel_config.new_netmask.assign(tmp_netmask);
+    } else if (reqptr->parameter == LAN_PARM_GATEWAY)
     {
-        snprintf(new_gateway, INET_ADDRSTRLEN, "%d.%d.%d.%d",
+        snprintf(tmp_gateway, INET_ADDRSTRLEN, "%d.%d.%d.%d",
             reqptr->data[0], reqptr->data[1], reqptr->data[2], reqptr->data[3]);
-    }
-    else if (reqptr->parameter == LAN_PARM_INPROGRESS)
+        channel_config.new_gateway.assign(tmp_gateway);
+    } else if (reqptr->parameter == LAN_PARM_INPROGRESS)
     {
-        if(reqptr->data[0] == SET_COMPLETE) // Set Complete
-        {
+        if(reqptr->data[0] == SET_COMPLETE) {
             lan_set_in_progress = SET_COMPLETE;
-            // Apply the IP settings once IP Address, Netmask and Gateway  is set
-            if (!strcmp(new_ipaddr, "") || !strcmp (new_netmask, "") || !strcmp (new_gateway, ""))
-            {
-                printf("ERROR: Incomplete LAN Parameters\n");
-            }
-            else
-            {
 
-                r = sd_bus_call_method(bus,            // On the System Bus
-                                        app,            // Service to contact
-                                        obj,            // Object path
-                                        ifc,            // Interface name
-                                        "SetAddress4",  // Method to be called
-                                        &error,         // object to return error
-                                        &reply,         // Response message on success
-                                        "ssss",         // input message (Interface, IP Address, Netmask, Gateway)
-                                        nwinterface,    // eth0
-                                        new_ipaddr,
-                                        new_netmask,
-                                        new_gateway);
-                if(r < 0)
-                {
-                    fprintf(stderr, "Failed to set network data %s:%s:%s %s\n", new_ipaddr, new_netmask, new_gateway, error.message);
-                    rc = IPMI_CC_UNSPECIFIED_ERROR;
-                }
-                memset(new_ipaddr, 0, INET_ADDRSTRLEN);
-                memset(new_netmask, 0, INET_ADDRSTRLEN);
-                memset(new_gateway, 0, INET_ADDRSTRLEN);
-            }
-        }
-        else if(reqptr->data[0] == SET_IN_PROGRESS) // Set In Progress
+            printf("N/W data from Cache: %s:%s:%s\n",
+                    channel_config.new_ipaddr.c_str(),
+                    channel_config.new_netmask.c_str(),
+                    channel_config.new_gateway.c_str());
+            printf("Use Set Channel Access command to apply them\n");
+
+        } else if(reqptr->data[0] == SET_IN_PROGRESS) // Set In Progress
         {
             lan_set_in_progress = SET_IN_PROGRESS;
         }
-    }
-    else
+    } else
     {
         fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
         rc = IPMI_CC_PARM_NOT_SUPPORTED;