#include "env.h"
#ifdef HAVE_DRM_EDID_PARSER
#include "p8-platform/os.h"
#include "drm-edid.h"
#include <dirent.h>
#include <fstream>
using namespace P8PLATFORM;
uint16_t CDRMEdidParser::GetPhysicalAddress(void)
{
uint16_t iPA(0);
std::string baseDir = "/sys/class/drm/";
DIR *dir = opendir(baseDir.c_str());
if (dir == NULL)
{
return iPA;
}
struct dirent *entry = readdir(dir);
std::string enablededid;
std::string line;
while (entry != NULL && enablededid.empty())
{
if (entry->d_type == DT_LNK)
{
std::string path, enabledPath, edidPath;
path = baseDir + entry->d_name;
enabledPath = path + "/enabled";
edidPath = path + "/edid";
std::ifstream f(enabledPath.c_str());
if (f.is_open())
{
if (f.good() && !f.eof())
{
getline(f, line);
if (line == "enabled")
{
std::ifstream edid(edidPath.c_str());
if (edid.is_open())
{
if (edid.good()) {
enablededid = edidPath;
}
edid.close();
}
}
}
f.close();
}
}
entry = readdir(dir);
}
closedir(dir);
if (!enablededid.empty())
{
FILE *fp = fopen(enablededid.c_str(), "r");
if (fp)
{
char* buf = (char*)calloc(4096, sizeof(char));
int iPtr(0);
int c(0);
if (buf)
{
while (c != EOF)
{
c = fgetc(fp);
if (c != EOF)
buf[iPtr++] = c;
}
iPA = CEDIDParser::GetPhysicalAddressFromEDID(buf, iPtr);
free(buf);
}
fclose(fp);
}
}
return iPA;
}
#endif