All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Ser <simon.ser@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: martin.peres@intel.com
Subject: [igt-dev] [PATCH i-g-t 2/5] lib/igt_eld: consolidate ELD parsing
Date: Fri, 31 May 2019 14:21:57 +0300	[thread overview]
Message-ID: <20190531112200.22403-3-simon.ser@intel.com> (raw)
In-Reply-To: <20190531112200.22403-1-simon.ser@intel.com>

Make the ELD enumeration more robust, and implement proper parsing for ELD
fields. This will become useful when other ELD fields (formats, sample rates,
sample sizes) will be parsed and checked.

Signed-off-by: Simon Ser <simon.ser@intel.com>
---
 lib/igt_eld.c | 94 +++++++++++++++++++++++++++++++--------------------
 lib/igt_eld.h |  5 +++
 2 files changed, 63 insertions(+), 36 deletions(-)

diff --git a/lib/igt_eld.c b/lib/igt_eld.c
index 8e0dcc306e85..a198001a56d7 100644
--- a/lib/igt_eld.c
+++ b/lib/igt_eld.c
@@ -30,8 +30,12 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "igt_core.h"
 #include "igt_eld.h"
 
+#define ELD_PREFIX "eld#"
+#define ELD_DELIM " \t"
+
 /**
  * EDID-Like Data (ELD) is metadata parsed and exposed by ALSA for HDMI and
  * DisplayPort connectors supporting audio. This includes the monitor name and
@@ -39,39 +43,49 @@
  * on).
  */
 
-/** eld_entry_is_igt: checks whether an ELD entry is mapped to the IGT EDID */
-static bool eld_entry_is_igt(const char *path)
+static bool eld_parse_entry(const char *path, struct eld_entry *eld)
 {
-	FILE *in;
+	FILE *f;
 	char buf[1024];
-	uint8_t eld_valid = 0;
-	uint8_t mon_valid = 0;
-
-	in = fopen(path, "r");
-	if (!in)
-		return false;
+	char *key, *value;
+	size_t len;
+	bool monitor_present;
 
-	memset(buf, 0, 1024);
+	memset(eld, 0, sizeof(*eld));
 
-	while ((fgets(buf, 1024, in)) != NULL) {
-		char *line = buf;
+	f = fopen(path, "r");
+	if (!f) {
+		igt_debug("Failed to open ELD file: %s\n", path);
+		return false;
+	}
 
-		if (!strncasecmp(line, "eld_valid", 9) &&
-				strstr(line, "1")) {
-			eld_valid++;
-		}
+	while ((fgets(buf, sizeof(buf), f)) != NULL) {
+		len = strlen(buf);
+		if (buf[len - 1] == '\n')
+			buf[len - 1] = '\0';
+
+		key = strtok(buf, ELD_DELIM);
+		value = strtok(NULL, "");
+		/* Skip whitespace at the beginning */
+		value += strspn(value, ELD_DELIM);
+
+		if (strcmp(key, "monitor_present") == 0)
+			monitor_present = strcmp(value, "1") == 0;
+		else if (strcmp(key, "eld_valid") == 0)
+			eld->valid = strcmp(value, "1") == 0;
+		else if (strcmp(key, "monitor_name") == 0)
+			snprintf(eld->monitor_name, sizeof(eld->monitor_name),
+				 "%s", value);
+	}
 
-		if (!strncasecmp(line, "monitor_name", 12) &&
-				strstr(line, "IGT")) {
-			mon_valid++;
-		}
+	if (ferror(f) != 0) {
+		igt_debug("Failed to read ELD file: %d\n", ferror(f));
+		return false;
 	}
 
-	fclose(in);
-	if (mon_valid && eld_valid)
-		return true;
+	fclose(f);
 
-	return false;
+	return monitor_present;
 }
 
 /** eld_has_igt: check whether ALSA has detected the audio-capable IGT EDID by
@@ -79,27 +93,35 @@ static bool eld_entry_is_igt(const char *path)
 bool eld_has_igt(void)
 {
 	DIR *dir;
-	struct dirent *snd_hda;
+	struct dirent *dirent;
 	int i;
+	char card[64];
+	char path[PATH_MAX];
+	struct eld_entry eld;
 
 	for (i = 0; i < 8; i++) {
-		char cards[128];
-
-		snprintf(cards, sizeof(cards), "/proc/asound/card%d", i);
-		dir = opendir(cards);
+		snprintf(card, sizeof(card), "/proc/asound/card%d", i);
+		dir = opendir(card);
 		if (!dir)
 			continue;
 
-		while ((snd_hda = readdir(dir))) {
-			char fpath[PATH_MAX];
+		while ((dirent = readdir(dir))) {
+			if (strncmp(dirent->d_name, ELD_PREFIX,
+				    strlen(ELD_PREFIX)) != 0)
+				continue;
+
+			snprintf(path, sizeof(path), "%s/%s", card,
+				 dirent->d_name);
+			if (!eld_parse_entry(path, &eld)) {
+				continue;
+			}
 
-			if (*snd_hda->d_name == '.' ||
-			    strstr(snd_hda->d_name, "eld") == 0)
+			if (!eld.valid) {
+				igt_debug("Skipping invalid ELD: %s\n", path);
 				continue;
+			}
 
-			snprintf(fpath, sizeof(fpath), "%s/%s", cards,
-				 snd_hda->d_name);
-			if (eld_entry_is_igt(fpath)) {
+			if (strcmp(eld.monitor_name, "IGT") == 0) {
 				closedir(dir);
 				return true;
 			}
diff --git a/lib/igt_eld.h b/lib/igt_eld.h
index 27f876d9f631..21fe3537acf9 100644
--- a/lib/igt_eld.h
+++ b/lib/igt_eld.h
@@ -30,6 +30,11 @@
 
 #include <stdbool.h>
 
+struct eld_entry {
+	bool valid;
+	char monitor_name[16];
+};
+
 bool eld_has_igt(void);
 
 #endif
-- 
2.21.0

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

  parent reply	other threads:[~2019-05-31 11:21 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-31 11:21 [igt-dev] [PATCH i-g-t 0/5] Add audio EDID tests to Chamelium Simon Ser
2019-05-31 11:21 ` [igt-dev] [PATCH i-g-t 1/5] lib/igt_eld: introduce an ELD library Simon Ser
2019-06-03 12:27   ` Martin Peres
2019-05-31 11:21 ` Simon Ser [this message]
2019-06-03 12:42   ` [igt-dev] [PATCH i-g-t 2/5] lib/igt_eld: consolidate ELD parsing Martin Peres
2019-06-03 14:34     ` Ser, Simon
2019-06-04  8:02       ` Peres, Martin
2019-05-31 11:21 ` [igt-dev] [PATCH i-g-t 3/5] lib/igt_eld: parse Short Audio Descriptors Simon Ser
2019-06-03 12:57   ` Martin Peres
2019-06-03 15:06     ` Ser, Simon
2019-05-31 11:21 ` [igt-dev] [PATCH i-g-t 4/5] lib/igt_eld: allow retrieving an ELD entry per monitor name Simon Ser
2019-06-03 13:04   ` Martin Peres
2019-06-03 15:10     ` Ser, Simon
2019-05-31 11:22 ` [igt-dev] [PATCH i-g-t 5/5] tests/kms_chamelium: add an audio EDID test Simon Ser
2019-05-31 13:18   ` Arkadiusz Hiler
2019-05-31 13:25     ` Arkadiusz Hiler
2019-05-31 14:06     ` Ser, Simon
2019-06-03 12:56     ` Ser, Simon
2019-05-31 16:02 ` [igt-dev] ✓ Fi.CI.BAT: success for Add audio EDID tests to Chamelium Patchwork
2019-06-01 18:54 ` [igt-dev] ✗ Fi.CI.IGT: failure " Patchwork
2019-06-03  6:38   ` Ser, Simon
2019-06-03 12:12     ` Peres, Martin
2019-06-03 12:17 ` [igt-dev] ✓ Fi.CI.IGT: success " Patchwork
2019-06-03 13:24 ` [igt-dev] ✓ Fi.CI.BAT: success for Add audio EDID tests to Chamelium (rev2) Patchwork
2019-06-03 19:53 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190531112200.22403-3-simon.ser@intel.com \
    --to=simon.ser@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=martin.peres@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.