sdm: Identify device node path before opening.

- Check existence of /dev/graphic/fb* and /dev/fb* path
  and open appropriate device node.

CRs-Fixed: 1062882
Change-Id: Icd1f2a3f444a748eb05a7abbe9f98eae25cf254a
diff --git a/sdm/include/utils/sys.h b/sdm/include/utils/sys.h
index b90007a..6b40df4 100644
--- a/sdm/include/utils/sys.h
+++ b/sdm/include/utils/sys.h
@@ -54,6 +54,7 @@
 #else
   typedef int (*ioctl)(int, int, ...);
 #endif
+  typedef int (*access)(const char *, int);
   typedef int (*open)(const char *, int, ...);
   typedef int (*close)(int);
   typedef int (*poll)(struct pollfd *, nfds_t, int);
@@ -68,6 +69,7 @@
   static bool getline_(fstream &fs, std::string &line);  // NOLINT
 
   static ioctl ioctl_;
+  static access access_;
   static open open_;
   static close close_;
   static poll poll_;
diff --git a/sdm/libs/core/fb/hw_device.cpp b/sdm/libs/core/fb/hw_device.cpp
index f75e92a..414c6e9 100644
--- a/sdm/libs/core/fb/hw_device.cpp
+++ b/sdm/libs/core/fb/hw_device.cpp
@@ -107,12 +107,28 @@
 }
 
 DisplayError HWDevice::Init() {
-  char device_name[64] = {0};
-
   // Read the fb node index
   fb_node_index_ = GetFBNodeIndex(device_type_);
   if (fb_node_index_ == -1) {
-    DLOGE("%s should be present", device_name_);
+    DLOGE("device type = %d should be present", device_type_);
+    return kErrorHardware;
+  }
+
+  const char *dev_name = NULL;
+  vector<string> dev_paths = {"/dev/graphics/fb", "/dev/fb"};
+  for (size_t i = 0; i < dev_paths.size(); i++) {
+    dev_paths[i] += to_string(fb_node_index_);
+    if (Sys::access_(dev_paths[i].c_str(), F_OK) >= 0) {
+      dev_name = dev_paths[i].c_str();
+      DLOGI("access(%s) successful", dev_name);
+      break;
+    }
+
+    DLOGI("access(%s), errno = %d, error = %s", dev_paths[i].c_str(), errno, strerror(errno));
+  }
+
+  if (!dev_name) {
+    DLOGE("access() failed for all possible paths");
     return kErrorHardware;
   }
 
@@ -122,10 +138,9 @@
   hw_resource_ = HWResourceInfo();
   hw_info_intf_->GetHWResourceInfo(&hw_resource_);
 
-  snprintf(device_name, sizeof(device_name), "%s%d", "/dev/graphics/fb", fb_node_index_);
-  device_fd_ = Sys::open_(device_name, O_RDWR);
+  device_fd_ = Sys::open_(dev_name, O_RDWR);
   if (device_fd_ < 0) {
-    DLOGE("open %s failed err = %d errstr = %s", device_name, errno,  strerror(errno));
+    DLOGE("open %s failed errno = %d, error = %s", dev_name, errno, strerror(errno));
     return kErrorResources;
   }
 
diff --git a/sdm/libs/utils/sys.cpp b/sdm/libs/utils/sys.cpp
index 0d1ab0e..f5e0f29 100644
--- a/sdm/libs/utils/sys.cpp
+++ b/sdm/libs/utils/sys.cpp
@@ -45,6 +45,7 @@
 
 // Pointer to actual driver interfaces.
 Sys::ioctl Sys::ioctl_ = ::ioctl;
+Sys::access Sys::access_ = ::access;
 Sys::open Sys::open_ = ::open;
 Sys::close Sys::close_ = ::close;
 Sys::poll Sys::poll_ = ::poll;