sdm: Use fstream to read sysfs entries.

- Replace FILE operations with fstream templates to read sysfs entries.

CRs-Fixed: 1029997
Change-Id: I5603617cbada01a4651394577bed56de38f9f060
diff --git a/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index 2dabe6c..aad3999 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/sdm/libs/core/fb/hw_device.cpp
@@ -45,12 +45,17 @@
 #include <utils/sys.h>
 #include <vector>
 #include <algorithm>
+#include <string>
 
 #include "hw_device.h"
 #include "hw_info_interface.h"
 
 #define __CLASS__ "HWDevice"
 
+using std::string;
+using std::to_string;
+using std::fstream;
+
 namespace sdm {
 
 HWDevice::HWDevice(BufferSyncHandler *buffer_sync_handler)
@@ -731,61 +736,43 @@
 }
 
 void HWDevice::GetHWPanelNameByNode(int device_node, HWPanelInfo *panel_info) {
-  if (!panel_info) {
-    DLOGE("PanelInfo pointer in invalid.");
-    return;
-  }
-  char *string_buffer = reinterpret_cast<char*>(malloc(sizeof(char) * kMaxStringLength));
-  if (!string_buffer) {
-    DLOGE("Failed to allocated string_buffer memory");
-    return;
-  }
-  snprintf(string_buffer, kMaxStringLength, "%s%d/msm_fb_panel_info", fb_path_, device_node);
-  FILE *fileptr = Sys::fopen_(string_buffer, "r");
-  if (!fileptr) {
-    DLOGW("Failed to open msm_fb_panel_info node device node %d", device_node);
-  } else {
-    size_t len = kMaxStringLength;
+  string file_name = fb_path_ + to_string(device_node) + "/msm_fb_panel_info";
 
-    while ((Sys::getline_(&string_buffer, &len, fileptr)) != -1) {
-      uint32_t token_count = 0;
-      const uint32_t max_count = 10;
-      char *tokens[max_count] = { NULL };
-      if (!ParseLine(string_buffer, "=\n", tokens, max_count, &token_count)) {
-        if (!strncmp(tokens[0], "panel_name", strlen("panel_name"))) {
-          snprintf(panel_info->panel_name, sizeof(panel_info->panel_name), "%s", tokens[1]);
-          break;
-        }
-      }
-    }
-    Sys::fclose_(fileptr);
-  }
-  free(string_buffer);
-}
-
-void HWDevice::GetHWPanelInfoByNode(int device_node, HWPanelInfo *panel_info) {
-  if (!panel_info) {
-    DLOGE("PanelInfo pointer in invalid.");
-    return;
-  }
-  char stringbuffer[kMaxStringLength];
-  FILE *fileptr = NULL;
-  snprintf(stringbuffer, sizeof(stringbuffer), "%s%d/msm_fb_panel_info", fb_path_, device_node);
-  fileptr = Sys::fopen_(stringbuffer, "r");
-  if (!fileptr) {
+  Sys::fstream fs(file_name, fstream::in);
+  if (!fs.is_open()) {
     DLOGW("Failed to open msm_fb_panel_info node device node %d", device_node);
     return;
   }
 
-  char *line = stringbuffer;
-  size_t len = kMaxStringLength;
-  ssize_t read;
-
-  while ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
+  string line;
+  while (Sys::getline_(fs, line)) {
     uint32_t token_count = 0;
     const uint32_t max_count = 10;
     char *tokens[max_count] = { NULL };
-    if (!ParseLine(line, tokens, max_count, &token_count)) {
+    if (!ParseLine(line.c_str(), "=\n", tokens, max_count, &token_count)) {
+      if (!strncmp(tokens[0], "panel_name", strlen("panel_name"))) {
+        snprintf(panel_info->panel_name, sizeof(panel_info->panel_name), "%s", tokens[1]);
+        break;
+      }
+    }
+  }
+}
+
+void HWDevice::GetHWPanelInfoByNode(int device_node, HWPanelInfo *panel_info) {
+  string file_name = fb_path_ + to_string(device_node) + "/msm_fb_panel_info";
+
+  Sys::fstream fs(file_name, fstream::in);
+  if (!fs.is_open()) {
+    DLOGW("Failed to open msm_fb_panel_info node device node %d", device_node);
+    return;
+  }
+
+  string line;
+  while (Sys::getline_(fs, line)) {
+    uint32_t token_count = 0;
+    const uint32_t max_count = 10;
+    char *tokens[max_count] = { NULL };
+    if (!ParseLine(line.c_str(), tokens, max_count, &token_count)) {
       if (!strncmp(tokens[0], "pu_en", strlen("pu_en"))) {
         panel_info->partial_update = atoi(tokens[1]);
       } else if (!strncmp(tokens[0], "xstart", strlen("xstart"))) {
@@ -815,7 +802,7 @@
       }
     }
   }
-  Sys::fclose_(fileptr);
+
   GetHWDisplayPortAndMode(device_node, &panel_info->port, &panel_info->mode);
   GetSplitInfo(device_node, panel_info);
   GetHWPanelNameByNode(device_node, panel_info);
@@ -825,81 +812,64 @@
 void HWDevice::GetHWDisplayPortAndMode(int device_node, HWDisplayPort *port, HWDisplayMode *mode) {
   *port = kPortDefault;
   *mode = kModeDefault;
-  char *stringbuffer = reinterpret_cast<char*>(malloc(sizeof(char) * kMaxStringLength));
-  if (!stringbuffer) {
-    DLOGE("Failed to allocated string_buffer memory");
+
+  string file_name = fb_path_ + to_string(device_node) + "/msm_fb_type";
+
+  Sys::fstream fs(file_name, fstream::in);
+  if (!fs.is_open()) {
+    DLOGW("File not found %s", file_name.c_str());
     return;
   }
 
-  snprintf(stringbuffer, kMaxStringLength, "%s%d/msm_fb_type", fb_path_, device_node);
-  FILE *fileptr = Sys::fopen_(stringbuffer, "r");
-  if (!fileptr) {
-    DLOGW("File not found %s", stringbuffer);
-    free(stringbuffer);
+  string line;
+  if (!Sys::getline_(fs, line)) {
     return;
   }
 
-  size_t len = kMaxStringLength;
-  ssize_t read = Sys::getline_(&stringbuffer, &len, fileptr);
-  if (read == -1) {
-    Sys::fclose_(fileptr);
-    free(stringbuffer);
-    return;
-  }
-  if ((strncmp(stringbuffer, "mipi dsi cmd panel", strlen("mipi dsi cmd panel")) == 0)) {
+  if ((strncmp(line.c_str(), "mipi dsi cmd panel", strlen("mipi dsi cmd panel")) == 0)) {
     *port = kPortDSI;
     *mode = kModeCommand;
-  } else if ((strncmp(stringbuffer, "mipi dsi video panel", strlen("mipi dsi video panel")) == 0)) {
+  } else if ((strncmp(line.c_str(), "mipi dsi video panel", strlen("mipi dsi video panel")) == 0)) {
     *port = kPortDSI;
     *mode = kModeVideo;
-  } else if ((strncmp(stringbuffer, "lvds panel", strlen("lvds panel")) == 0)) {
+  } else if ((strncmp(line.c_str(), "lvds panel", strlen("lvds panel")) == 0)) {
     *port = kPortLVDS;
     *mode = kModeVideo;
-  } else if ((strncmp(stringbuffer, "edp panel", strlen("edp panel")) == 0)) {
+  } else if ((strncmp(line.c_str(), "edp panel", strlen("edp panel")) == 0)) {
     *port = kPortEDP;
     *mode = kModeVideo;
-  } else if ((strncmp(stringbuffer, "dtv panel", strlen("dtv panel")) == 0)) {
+  } else if ((strncmp(line.c_str(), "dtv panel", strlen("dtv panel")) == 0)) {
     *port = kPortDTv;
     *mode = kModeVideo;
-  } else if ((strncmp(stringbuffer, "writeback panel", strlen("writeback panel")) == 0)) {
+  } else if ((strncmp(line.c_str(), "writeback panel", strlen("writeback panel")) == 0)) {
     *port = kPortWriteBack;
     *mode = kModeCommand;
   }
-  Sys::fclose_(fileptr);
-  free(stringbuffer);
 
   return;
 }
 
 void HWDevice::GetSplitInfo(int device_node, HWPanelInfo *panel_info) {
-  char stringbuffer[kMaxStringLength];
-  FILE *fileptr = NULL;
-  uint32_t token_count = 0;
-  const uint32_t max_count = 10;
-  char *tokens[max_count] = { NULL };
-
   // Split info - for MDSS Version 5 - No need to check version here
-  snprintf(stringbuffer , sizeof(stringbuffer), "%s%d/msm_fb_split", fb_path_, device_node);
-  fileptr = Sys::fopen_(stringbuffer, "r");
-  if (!fileptr) {
-    DLOGW("File not found %s", stringbuffer);
+  string file_name = fb_path_ + to_string(device_node) + "/msm_fb_split";
+
+  Sys::fstream fs(file_name, fstream::in);
+  if (!fs.is_open()) {
+    DLOGW("File not found %s", file_name.c_str());
     return;
   }
 
-  char *line = stringbuffer;
-  size_t len = kMaxStringLength;
-  ssize_t read;
-
   // Format "left right" space as delimiter
-  read = Sys::getline_(&line, &len, fileptr);
-  if (read > 0) {
-    if (!ParseLine(line, tokens, max_count, &token_count)) {
+  uint32_t token_count = 0;
+  const uint32_t max_count = 10;
+  char *tokens[max_count] = { NULL };
+  string line;
+  if (Sys::getline_(fs, line)) {
+    if (!ParseLine(line.c_str(), tokens, max_count, &token_count)) {
       panel_info->split_info.left_split = UINT32(atoi(tokens[0]));
       panel_info->split_info.right_split = UINT32(atoi(tokens[1]));
     }
   }
-
-  Sys::fclose_(fileptr);
 }
 
 void HWDevice::GetHWPanelMaxBrightnessFromNode(HWPanelInfo *panel_info) {
@@ -926,7 +896,8 @@
   Sys::close_(fd);
 }
 
-int HWDevice::ParseLine(char *input, char *tokens[], const uint32_t max_token, uint32_t *count) {
+int HWDevice::ParseLine(const char *input, char *tokens[], const uint32_t max_token,
+                        uint32_t *count) {
   char *tmp_token = NULL;
   char *temp_ptr;
   uint32_t index = 0;
@@ -934,7 +905,7 @@
   if (!input) {
     return -1;
   }
-  tmp_token = strtok_r(input, delim, &temp_ptr);
+  tmp_token = strtok_r(const_cast<char *>(input), delim, &temp_ptr);
   while (tmp_token && index < max_token) {
     tokens[index++] = tmp_token;
     tmp_token = strtok_r(NULL, delim, &temp_ptr);
@@ -944,7 +915,7 @@
   return 0;
 }
 
-int HWDevice::ParseLine(char *input, const char *delim, char *tokens[],
+int HWDevice::ParseLine(const char *input, const char *delim, char *tokens[],
                         const uint32_t max_token, uint32_t *count) {
   char *tmp_token = NULL;
   char *temp_ptr;
@@ -952,7 +923,7 @@
   if (!input) {
     return -1;
   }
-  tmp_token = strtok_r(input, delim, &temp_ptr);
+  tmp_token = strtok_r(const_cast<char *>(input), delim, &temp_ptr);
   while (tmp_token && index < max_token) {
     tokens[index++] = tmp_token;
     tmp_token = strtok_r(NULL, delim, &temp_ptr);
diff --git a/sdm/libs/core/fb/hw_device.h b/sdm/libs/core/fb/hw_device.h
index a63c4a0..9967175 100644
--- a/sdm/libs/core/fb/hw_device.h
+++ b/sdm/libs/core/fb/hw_device.h
@@ -116,8 +116,8 @@
   void GetHWDisplayPortAndMode(int device_node, HWDisplayPort *port, HWDisplayMode *mode);
   void GetSplitInfo(int device_node, HWPanelInfo *panel_info);
   void GetHWPanelMaxBrightnessFromNode(HWPanelInfo *panel_info);
-  int ParseLine(char *input, char *tokens[], const uint32_t max_token, uint32_t *count);
-  int ParseLine(char *input, const char *delim, char *tokens[],
+  int ParseLine(const char *input, char *tokens[], const uint32_t max_token, uint32_t *count);
+  int ParseLine(const char *input, const char *delim, char *tokens[],
                 const uint32_t max_token, uint32_t *count);
   void ResetDisplayParams();
   void SetCSC(const LayerCSC source, mdp_color_space *color_space);
diff --git a/sdm/libs/core/fb/hw_events.cpp b/sdm/libs/core/fb/hw_events.cpp
old mode 100755
new mode 100644
diff --git a/sdm/libs/core/fb/hw_info.cpp b/sdm/libs/core/fb/hw_info.cpp
index a1a928c..9dbb1c8 100644
--- a/sdm/libs/core/fb/hw_info.cpp
+++ b/sdm/libs/core/fb/hw_info.cpp
@@ -49,7 +49,7 @@
 using std::vector;
 using std::map;
 using std::string;
-using std::ifstream;
+using std::fstream;
 using std::to_string;
 
 namespace sdm {
@@ -67,15 +67,15 @@
   { 0x3F, 0xF4, 0x10, 0x1E, 0x20, 0xFF, 0x01, 0x00, 0xAA, 0x16 },  // kHWWBIntfOutput
 };
 
-int HWInfo::ParseString(char *input, char *tokens[], const uint32_t max_token, const char *delim,
-                        uint32_t *count) {
+int HWInfo::ParseString(const char *input, char *tokens[], const uint32_t max_token,
+                        const char *delim, uint32_t *count) {
   char *tmp_token = NULL;
   char *temp_ptr;
   uint32_t index = 0;
   if (!input) {
     return -1;
   }
-  tmp_token = strtok_r(input, delim, &temp_ptr);
+  tmp_token = strtok_r(const_cast<char *>(input), delim, &temp_ptr);
   while (tmp_token && index < max_token) {
     tokens[index++] = tmp_token;
     tmp_token = strtok_r(NULL, delim, &temp_ptr);
@@ -107,15 +107,9 @@
 }
 
 DisplayError HWInfo::GetDynamicBWLimits(HWResourceInfo *hw_resource) {
-  const char *bw_info_node = "/sys/devices/virtual/graphics/fb0/mdp/bw_mode_bitmap";
-  FILE *fileptr = NULL;
-  uint32_t token_count = 0;
-  const uint32_t max_count = kBwModeMax;
-  char *tokens[max_count] = { NULL };
-  fileptr = Sys::fopen_(bw_info_node, "r");
-
-  if (!fileptr) {
-    DLOGE("File '%s' not found", bw_info_node);
+  Sys::fstream fs(kBWModeBitmap, fstream::in);
+  if (!fs.is_open()) {
+    DLOGE("File '%s' not found", kBWModeBitmap);
     return kErrorHardware;
   }
 
@@ -125,17 +119,12 @@
     bw_info->pipe_bw_limit[index] = hw_resource->max_pipe_bw;
   }
 
-  char *stringbuffer = reinterpret_cast<char *>(malloc(kMaxStringLength));
-  if (stringbuffer == NULL) {
-    DLOGE("Failed to allocate stringbuffer");
-    return kErrorMemory;
-  }
-
-  size_t len = kMaxStringLength;
-  ssize_t read;
-  char *line = stringbuffer;
-  while ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
-    if (!ParseString(line, tokens, max_count, ":, =\n", &token_count)) {
+  uint32_t token_count = 0;
+  const uint32_t max_count = kBwModeMax;
+  char *tokens[max_count] = { NULL };
+  string line;
+  while (Sys::getline_(fs, line)) {
+    if (!ParseString(line.c_str(), tokens, max_count, ":, =\n", &token_count)) {
       if (!strncmp(tokens[0], "default_pipe", strlen("default_pipe"))) {
         bw_info->pipe_bw_limit[kBwDefault] = UINT32(atoi(tokens[1]));
       } else if (!strncmp(tokens[0], "camera_pipe", strlen("camera_pipe"))) {
@@ -155,47 +144,30 @@
       }
     }
   }
-  free(stringbuffer);
-  Sys::fclose_(fileptr);
 
   return kErrorNone;
 }
 
 DisplayError HWInfo::GetHWResourceInfo(HWResourceInfo *hw_resource) {
-  if (!hw_resource) {
-    DLOGE("HWResourceInfo pointer in invalid.");
-    return kErrorParameters;
-  }
-  const char *fb_path = "/sys/devices/virtual/graphics/fb";
-  FILE *fileptr = NULL;
-  uint32_t token_count = 0;
-  const uint32_t max_count = 256;
-  char *tokens[max_count] = { NULL };
-  char *stringbuffer = reinterpret_cast<char *>(malloc(kMaxStringLength));
+  string fb_path = "/sys/devices/virtual/graphics/fb"
+                      + to_string(kHWCapabilitiesNode) + "/mdp/caps";
 
-  if (stringbuffer == NULL) {
-    DLOGE("Failed to allocate stringbuffer");
-    return kErrorMemory;
-  }
-
-  snprintf(stringbuffer , kMaxStringLength, "%s%d/mdp/caps", fb_path, kHWCapabilitiesNode);
-  fileptr = Sys::fopen_(stringbuffer, "r");
-
-  if (!fileptr) {
-    DLOGE("File '%s' not found", stringbuffer);
-    free(stringbuffer);
+  Sys::fstream fs(fb_path, fstream::in);
+  if (!fs.is_open()) {
+    DLOGE("File '%s' not found", fb_path.c_str());
     return kErrorHardware;
   }
 
   InitSupportedFormatMap(hw_resource);
-
-  size_t len = kMaxStringLength;
-  ssize_t read;
-  char *line = stringbuffer;
   hw_resource->hw_version = kHWMdssVersion5;
-  while ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
+
+  uint32_t token_count = 0;
+  const uint32_t max_count = 256;
+  char *tokens[max_count] = { NULL };
+  string line;
+  while (Sys::getline_(fs, line)) {
     // parse the line and update information accordingly
-    if (!ParseString(line, tokens, max_count, ":, =\n", &token_count)) {
+    if (!ParseString(line.c_str(), tokens, max_count, ":, =\n", &token_count)) {
       if (!strncmp(tokens[0], "hw_rev", strlen("hw_rev"))) {
         hw_resource->hw_revision = UINT32(atoi(tokens[1]));  // HW Rev, v1/v2
       } else if (!strncmp(tokens[0], "rot_input_fmts", strlen("rot_input_fmts"))) {
@@ -281,8 +253,8 @@
       } else if (!strncmp(tokens[0], "pipe_count", strlen("pipe_count"))) {
         uint32_t pipe_count = UINT8(atoi(tokens[1]));
         for (uint32_t i = 0; i < pipe_count; i++) {
-          read = Sys::getline_(&line, &len, fileptr);
-          if (!ParseString(line, tokens, max_count, ": =\n", &token_count)) {
+          Sys::getline_(fs, line);
+          if (!ParseString(line.c_str(), tokens, max_count, ": =\n", &token_count)) {
             HWPipeCaps pipe_caps;
             pipe_caps.type = kPipeTypeUnused;
             for (uint32_t j = 0; j < token_count; j += 2) {
@@ -335,8 +307,6 @@
     ::dlclose(extension_lib);
   }
 
-  Sys::fclose_(fileptr);
-
   DLOGI("SDE Version = %d, SDE Revision = %x, RGB = %d, VIG = %d, DMA = %d, Cursor = %d",
         hw_resource->hw_version, hw_resource->hw_revision, hw_resource->num_rgb_pipe,
         hw_resource->num_vig_pipe, hw_resource->num_dma_pipe, hw_resource->num_cursor_pipe);
@@ -390,26 +360,20 @@
 }
 
 DisplayError HWInfo::GetMDSSRotatorInfo(HWResourceInfo *hw_resource) {
-  FILE *fileptr = NULL;
-  char *stringbuffer = reinterpret_cast<char *>(malloc(sizeof(char) * kMaxStringLength));
-  uint32_t token_count = 0;
-  const uint32_t max_count = 10;
-  char *tokens[max_count] = { NULL };
-  size_t len = kMaxStringLength;
-  ssize_t read = 0;
-
-  snprintf(stringbuffer, sizeof(char) * kMaxStringLength, "%s", kRotatorCapsPath);
-  fileptr = Sys::fopen_(stringbuffer, "r");
-
-  if (!fileptr) {
-    DLOGW("File '%s' not found", stringbuffer);
-    free(stringbuffer);
+  Sys::fstream fs(kRotatorCapsPath, fstream::in);
+  if (!fs.is_open()) {
+    DLOGW("File '%s' not found", kRotatorCapsPath);
     return kErrorNotSupported;
   }
 
+  uint32_t token_count = 0;
+  const uint32_t max_count = 10;
+  char *tokens[max_count] = { NULL };
+  string line;
+
   hw_resource->hw_rot_info.type = HWRotatorInfo::ROT_TYPE_MDSS;
-  while ((read = Sys::getline_(&stringbuffer, &len, fileptr)) != -1) {
-    if (!ParseString(stringbuffer, tokens, max_count, ":, =\n", &token_count)) {
+  while (Sys::getline_(fs, line)) {
+    if (!ParseString(line.c_str(), tokens, max_count, ":, =\n", &token_count)) {
       if (!strncmp(tokens[0], "wb_count", strlen("wb_count"))) {
         hw_resource->hw_rot_info.num_rotator = UINT8(atoi(tokens[1]));
         hw_resource->hw_rot_info.device_path = "/dev/mdss_rotator";
@@ -419,9 +383,6 @@
     }
   }
 
-  Sys::fclose_(fileptr);
-  free(stringbuffer);
-
   DLOGI("MDSS Rotator: Count = %d, Downscale = %d", hw_resource->hw_rot_info.num_rotator,
         hw_resource->hw_rot_info.has_downscale);
 
@@ -430,28 +391,27 @@
 
 DisplayError HWInfo::GetV4L2RotatorInfo(HWResourceInfo *hw_resource) {
   const uint32_t kMaxV4L2Nodes = 64;
-  size_t len = kMaxStringLength;
-  char *line = reinterpret_cast<char *>(malloc(sizeof(char) * len));
   bool found = false;
 
   for (uint32_t i = 0; (i < kMaxV4L2Nodes) && (false == found); i++) {
     string path = "/sys/class/video4linux/video" + to_string(i) + "/name";
-    FILE *fileptr = Sys::fopen_(path.c_str(), "r");
-    if (fileptr) {
-      if ((Sys::getline_(&line, &len, fileptr) != -1) &&
-          (!strncmp(line, "sde_rotator", strlen("sde_rotator")))) {
-         hw_resource->hw_rot_info.device_path = string("/dev/video" + to_string(i));
-         hw_resource->hw_rot_info.num_rotator++;
-         hw_resource->hw_rot_info.type = HWRotatorInfo::ROT_TYPE_V4L2;
-         hw_resource->hw_rot_info.has_downscale = true;
-         // We support only 1 rotator
-         found = true;
-      }
-      Sys::fclose_(fileptr);
+    Sys::fstream fs(path, fstream::in);
+    if (!fs.is_open()) {
+      continue;
+    }
+
+    string line;
+    if (Sys::getline_(fs, line) &&
+        (!strncmp(line.c_str(), "sde_rotator", strlen("sde_rotator")))) {
+       hw_resource->hw_rot_info.device_path = string("/dev/video" + to_string(i));
+       hw_resource->hw_rot_info.num_rotator++;
+       hw_resource->hw_rot_info.type = HWRotatorInfo::ROT_TYPE_V4L2;
+       hw_resource->hw_rot_info.has_downscale = true;
+       // We support only 1 rotator
+       found = true;
     }
   }
 
-  free(line);
   DLOGI("V4L2 Rotator: Count = %d, Downscale = %d", hw_resource->hw_rot_info.num_rotator,
         hw_resource->hw_rot_info.has_downscale);
 
@@ -544,54 +504,35 @@
 }
 
 DisplayError HWInfo::GetFirstDisplayInterfaceType(HWDisplayInterfaceInfo *hw_disp_info) {
-  char *stringbuffer = reinterpret_cast<char *>(malloc(kMaxStringLength));
-  if (stringbuffer == NULL) {
-    DLOGE("Failed to allocate Stringbuffer");
-    return kErrorMemory;
-  }
-
-  char *line = stringbuffer;
-  size_t len = kMaxStringLength;
-  ssize_t read;
-
-  FILE *fileptr = Sys::fopen_("/sys/devices/virtual/graphics/fb0/msm_fb_type", "r");
-  if (!fileptr) {
-    free(stringbuffer);
+  Sys::fstream fs("/sys/devices/virtual/graphics/fb0/msm_fb_type", fstream::in);
+  if (!fs.is_open()) {
     return kErrorHardware;
   }
 
-  if ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
-    if (!strncmp(line, "dtv panel", strlen("dtv panel"))) {
-      hw_disp_info->type = kHDMI;
-      DLOGI("First display is HDMI");
-    } else {
-      hw_disp_info->type = kPrimary;
-      DLOGI("First display is internal display");
-    }
+  string line;
+  if (!Sys::getline_(fs, line)) {
+    return kErrorHardware;
+  }
+
+  if (!strncmp(line.c_str(), "dtv panel", strlen("dtv panel"))) {
+    hw_disp_info->type = kHDMI;
+    DLOGI("First display is HDMI");
   } else {
-    free(stringbuffer);
-    Sys::fclose_(fileptr);
-    return kErrorHardware;
+    hw_disp_info->type = kPrimary;
+    DLOGI("First display is internal display");
   }
 
-  Sys::fclose_(fileptr);
-
-  fileptr = Sys::fopen_("/sys/devices/virtual/graphics/fb0/connected", "r");
-  if (!fileptr) {
-    // If fb0 is for a DSI/connected panel, then connected node will not exist
+  fs.open("/sys/devices/virtual/graphics/fb0/connected", fstream::in);
+  if (!fs.is_open()) {
     hw_disp_info->is_connected = true;
   } else {
-    if ((read = Sys::getline_(&line, &len, fileptr)) != -1) {
-        hw_disp_info->is_connected =  (!strncmp(line, "1", strlen("1")));
-    } else {
-        Sys::fclose_(fileptr);
-        free(stringbuffer);
-        return kErrorHardware;
+    if (!Sys::getline_(fs, line)) {
+      return kErrorHardware;
     }
-    Sys::fclose_(fileptr);
+
+    hw_disp_info->is_connected =  (!strncmp(line.c_str(), "1", strlen("1")));
   }
 
-  free(stringbuffer);
   return kErrorNone;
 }
 
diff --git a/sdm/libs/core/fb/hw_info.h b/sdm/libs/core/fb/hw_info.h
index f22acb0..1f18616 100644
--- a/sdm/libs/core/fb/hw_info.h
+++ b/sdm/libs/core/fb/hw_info.h
@@ -56,9 +56,11 @@
   static const int kHWCapabilitiesNode = 0;
   static const uint8_t kDefaultFormatSupport[kHWSubBlockMax][BITS_TO_BYTES(MDP_IMGTYPE_LIMIT1)];
   static constexpr const char *kRotatorCapsPath = "/sys/devices/virtual/rotator/mdss_rotator/caps";
+  static constexpr const char *kBWModeBitmap
+                                  = "/sys/devices/virtual/graphics/fb0/mdp/bw_mode_bitmap";
 
-  static int ParseString(char *input, char *tokens[], const uint32_t max_token, const char *delim,
-                         uint32_t *count);
+  static int ParseString(const char *input, char *tokens[], const uint32_t max_token,
+                         const char *delim, uint32_t *count);
   DisplayError GetDynamicBWLimits(HWResourceInfo *hw_resource);
   LayerBufferFormat GetSDMFormat(int mdp_format);
   void InitSupportedFormatMap(HWResourceInfo *hw_resource);
diff --git a/sdm/libs/core/fb/hw_primary.cpp b/sdm/libs/core/fb/hw_primary.cpp
index c082c04..8c71274 100644
--- a/sdm/libs/core/fb/hw_primary.cpp
+++ b/sdm/libs/core/fb/hw_primary.cpp
@@ -60,6 +60,8 @@
 namespace sdm {
 
 using std::string;
+using std::to_string;
+using std::fstream;
 
 DisplayError HWPrimary::Create(HWInterface **intf, HWInfoInterface *hw_info_intf,
                                BufferSyncHandler *buffer_sync_handler) {
@@ -122,37 +124,28 @@
 
 bool HWPrimary::GetCurrentModeFromSysfs(size_t *curr_x_pixels, size_t *curr_y_pixels) {
   bool ret = false;
-  size_t len = kPageSize;
-  string mode_path = string(fb_path_) + string("0/mode");
+  string mode_path = fb_path_ + string("0/mode");
 
-  FILE *fd = Sys::fopen_(mode_path.c_str(), "r");
-  if (fd) {
-    char *buffer = static_cast<char *>(calloc(len, sizeof(char)));
+  Sys::fstream fs(mode_path, fstream::in);
+  if (!fs.is_open()) {
+    return false;
+  }
 
-    if (buffer == NULL) {
-      DLOGW("Failed to allocate memory");
-      Sys::fclose_(fd);
-      return false;
+  string line;
+  if (Sys::getline_(fs, line)) {
+    // String is of form "U:1600x2560p-0". Documentation/fb/modedb.txt in
+    // kernel has more info on the format.
+    size_t xpos = line.find(':');
+    size_t ypos = line.find('x');
+
+    if (xpos == string::npos || ypos == string::npos) {
+      DLOGI("Resolution switch not supported");
+    } else {
+      *curr_x_pixels = static_cast<size_t>(atoi(line.c_str() + xpos + 1));
+      *curr_y_pixels = static_cast<size_t>(atoi(line.c_str() + ypos + 1));
+      DLOGI("Current Config: %u x %u", *curr_x_pixels, *curr_y_pixels);
+      ret = true;
     }
-
-    if (Sys::getline_(&buffer, &len, fd) > 0) {
-      // String is of form "U:1600x2560p-0". Documentation/fb/modedb.txt in
-      // kernel has more info on the format.
-      size_t xpos = string(buffer).find(':');
-      size_t ypos = string(buffer).find('x');
-
-      if (xpos == string::npos || ypos == string::npos) {
-        DLOGI("Resolution switch not supported");
-      } else {
-        *curr_x_pixels = static_cast<size_t>(atoi(buffer + xpos + 1));
-        *curr_y_pixels = static_cast<size_t>(atoi(buffer + ypos + 1));
-        DLOGI("Current Config: %u x %u", *curr_x_pixels, *curr_y_pixels);
-        ret = true;
-      }
-    }
-
-    free(buffer);
-    Sys::fclose_(fd);
   }
 
   return ret;
@@ -161,48 +154,39 @@
 void HWPrimary::InitializeConfigs() {
   size_t curr_x_pixels = 0;
   size_t curr_y_pixels = 0;
-  size_t len = kPageSize;
-  string modes_path = string(fb_path_) + string("0/modes");
 
   if (!GetCurrentModeFromSysfs(&curr_x_pixels, &curr_y_pixels)) {
     return;
   }
 
-  FILE *fd = Sys::fopen_(modes_path.c_str(), "r");
-  if (fd) {
-    char *buffer = static_cast<char *>(calloc(len, sizeof(char)));
+  string modes_path = string(fb_path_) + string("0/modes");
 
-    if (buffer == NULL) {
-      DLOGW("Failed to allocate memory");
-      Sys::fclose_(fd);
-      return;
-    }
-
-    while (Sys::getline_(&buffer, &len, fd) > 0) {
-      DisplayConfigVariableInfo config;
-      size_t xpos = string(buffer).find(':');
-      size_t ypos = string(buffer).find('x');
-
-      if (xpos == string::npos || ypos == string::npos) {
-        continue;
-      }
-
-      config.x_pixels = UINT32(atoi(buffer + xpos + 1));
-      config.y_pixels = UINT32(atoi(buffer + ypos + 1));
-      DLOGI("Found mode %d x %d", config.x_pixels, config.y_pixels);
-      display_configs_.push_back(config);
-      display_config_strings_.push_back(string(buffer));
-
-      if (curr_x_pixels == config.x_pixels && curr_y_pixels == config.y_pixels) {
-        active_config_index_ = UINT32(display_configs_.size() - 1);
-        DLOGI("Active config index %u", active_config_index_);
-      }
-    }
-
-    free(buffer);
-    Sys::fclose_(fd);
-  } else {
+  Sys::fstream fs(modes_path, fstream::in);
+  if (!fs.is_open()) {
     DLOGI("Unable to process modes");
+    return;
+  }
+
+  string line;
+  while (Sys::getline_(fs, line)) {
+    DisplayConfigVariableInfo config;
+    size_t xpos = line.find(':');
+    size_t ypos = line.find('x');
+
+    if (xpos == string::npos || ypos == string::npos) {
+      continue;
+    }
+
+    config.x_pixels = UINT32(atoi(line.c_str() + xpos + 1));
+    config.y_pixels = UINT32(atoi(line.c_str() + ypos + 1));
+    DLOGI("Found mode %d x %d", config.x_pixels, config.y_pixels);
+    display_configs_.push_back(config);
+    display_config_strings_.push_back(string(line.c_str()));
+
+    if (curr_x_pixels == config.x_pixels && curr_y_pixels == config.y_pixels) {
+      active_config_index_ = UINT32(display_configs_.size() - 1);
+      DLOGI("Active config index %u", active_config_index_);
+    }
   }
 }
 
diff --git a/sdm/libs/utils/sys.cpp b/sdm/libs/utils/sys.cpp
index a622b9e..6dddcfb 100644
--- a/sdm/libs/utils/sys.cpp
+++ b/sdm/libs/utils/sys.cpp
@@ -31,6 +31,7 @@
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <string>
 
 #define __CLASS__ "Sys"
 
@@ -49,46 +50,15 @@
 Sys::poll Sys::poll_ = ::poll;
 Sys::pread Sys::pread_ = ::pread;
 Sys::pwrite Sys::pwrite_ = ::pwrite;
-Sys::fopen Sys::fopen_ = ::fopen;
-Sys::fclose Sys::fclose_ = ::fclose;
-Sys::getline Sys::getline_ = ::getline;
 Sys::pthread_cancel Sys::pthread_cancel_ = PthreadCancel;
 Sys::dup Sys::dup_ = ::dup;
 Sys::read Sys::read_ = ::read;
 Sys::write Sys::write_ = ::write;
 Sys::eventfd Sys::eventfd_ = ::eventfd;
 
-#else
-
-// Point to virtual driver interfaces.
-extern int virtual_ioctl(int fd, int cmd, ...);
-extern int virtual_open(const char *file_name, int access, ...);
-extern int virtual_close(int fd);
-extern int virtual_poll(struct pollfd *fds,  nfds_t num, int timeout);
-extern ssize_t virtual_pread(int fd, void *data, size_t count, off_t offset);
-extern ssize_t virtual_pwrite(int fd, const void *data, size_t count, off_t offset);
-extern FILE* virtual_fopen(const char *fname, const char *mode);
-extern int virtual_fclose(FILE* fileptr);
-extern ssize_t virtual_getline(char **lineptr, size_t *linelen, FILE *stream);
-extern int virtual_dup(int fd);
-extern ssize_t virtual_read(int fd, void *data, size_t count);
-extern ssize_t virtual_write(int fd, const void *data, size_t count);
-extern int virtual_eventfd(unsigned int initval, int flags);
-
-Sys::ioctl Sys::ioctl_ = virtual_ioctl;
-Sys::open Sys::open_ = virtual_open;
-Sys::close Sys::close_ = virtual_close;
-Sys::poll Sys::poll_ = virtual_poll;
-Sys::pread Sys::pread_ = virtual_pread;
-Sys::pwrite Sys::pwrite_ = virtual_pwrite;
-Sys::fopen Sys::fopen_ = virtual_fopen;
-Sys::fclose Sys::fclose_ = virtual_fclose;
-Sys::getline Sys::getline_ = virtual_getline;
-Sys::pthread_cancel Sys::pthread_cancel_ = ::pthread_cancel;
-Sys::dup Sys::dup_ = virtual_dup;
-Sys::read Sys::read_ = virtual_read;
-Sys::write Sys::write_ = virtual_write;
-Sys::eventfd Sys::eventfd_ = virtual_eventfd;
+bool Sys::getline_(fstream &fs, std::string &line) {
+  return std::getline(fs, line) ? true : false;
+}
 
 #endif  // SDM_VIRTUAL_DRIVER