typecd: Register SOP' number_of_alternate_modes
The number of alternate modes supported by a cable plug (SOP') is
exposed via sysfs. Modify the cable plug (SOP') registration code to
read and store this value.
BUG=b:172097194
TEST=All unit tests still pass. Build and boot on volteer and ensure
that we don't see any error prints associated with Cable Plug
registration when a cable is connected.
Change-Id: Ifbf44a502e46a64fbd385f21659db92d94195cb5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2600259
Reviewed-by: Mengqi Guo <mqg@chromium.org>
Reviewed-by: Prashant Malani <pmalani@chromium.org>
Commit-Queue: Prashant Malani <pmalani@chromium.org>
Tested-by: Prashant Malani <pmalani@chromium.org>
diff --git a/typecd/cable.cc b/typecd/cable.cc
index 051f371..8b90617 100644
--- a/typecd/cable.cc
+++ b/typecd/cable.cc
@@ -4,7 +4,11 @@
#include "typecd/cable.h"
+#include <string>
+
#include <base/files/file_enumerator.h>
+#include <base/strings/string_number_conversions.h>
+#include <base/strings/string_util.h>
#include <re2/re2.h>
#include "typecd/pd_vdo_constants.h"
@@ -23,6 +27,28 @@
base::FileEnumerator iter(syspath, false, base::FileEnumerator::DIRECTORIES);
for (auto path = iter.Next(); !path.empty(); path = iter.Next())
AddAltMode(path);
+
+ if (GetNumAltModes() != -1)
+ return;
+
+ auto num_altmodes_path = syspath.Append("number_of_alternate_modes");
+
+ std::string val_str;
+ if (!base::ReadFileToString(num_altmodes_path, &val_str)) {
+ LOG(WARNING) << "Number of alternate modes not available for syspath "
+ << syspath;
+ return;
+ }
+
+ base::TrimWhitespaceASCII(val_str, base::TRIM_TRAILING, &val_str);
+
+ int num_altmodes;
+ if (!base::StringToInt(val_str, &num_altmodes)) {
+ LOG(ERROR) << "Couldn't parse num_altmodes from string: " << val_str;
+ return;
+ }
+
+ SetNumAltModes(num_altmodes);
}
bool Cable::AddAltMode(const base::FilePath& mode_syspath) {