power: supply: power_supply_show_enum_with_available(): Replace spaces with '_'
Some enum style power-supply properties have text-values / labels for some of the enum values containing a space, e.g. "Long Life" for POWER_SUPPLY_CHARGE_TYPE_LONGLIFE. Make power_supply_show_enum_with_available() replace these spaces with '_' when showing the available text-values. After this the output for a battery which supports "Long Life" will be e.g.: Fast [Standard] Long_Life or: Fast Standard [Long_Life] Modify power_supply_store_property() to accept both the original text-value with space and the alternative value with the spaces replaced by '_'. This allows users to write the value with '_' after seeing this on reading the property. Signed-off-by: Hans de Goede <hdegoede@redhat.com> Reviewed-by: Thomas Weißschuh <linux@weissschuh.net> Link: https://lore.kernel.org/r/20241209204051.8786-2-hdegoede@redhat.com Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
This commit is contained in:
committed by
Sebastian Reichel
parent
72ad51a623
commit
cc4c34577d
@@ -237,23 +237,52 @@ static enum power_supply_property dev_attr_psp(struct device_attribute *attr)
|
||||
return to_ps_attr(attr) - power_supply_attrs;
|
||||
}
|
||||
|
||||
static void power_supply_escape_spaces(const char *str, char *buf, size_t bufsize)
|
||||
{
|
||||
strscpy(buf, str, bufsize);
|
||||
strreplace(buf, ' ', '_');
|
||||
}
|
||||
|
||||
static int power_supply_match_string(const char * const *array, size_t n, const char *s)
|
||||
{
|
||||
int ret;
|
||||
|
||||
/* First try an exact match */
|
||||
ret = __sysfs_match_string(array, n, s);
|
||||
if (ret >= 0)
|
||||
return ret;
|
||||
|
||||
/* Second round, try matching with spaces replaced by '_' */
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char buf[32];
|
||||
|
||||
power_supply_escape_spaces(array[i], buf, sizeof(buf));
|
||||
if (sysfs_streq(buf, s))
|
||||
return i;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static ssize_t power_supply_show_enum_with_available(
|
||||
struct device *dev, const char * const labels[], int label_count,
|
||||
unsigned int available_values, int value, char *buf)
|
||||
{
|
||||
bool match = false, available, active;
|
||||
char escaped_label[32];
|
||||
ssize_t count = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < label_count; i++) {
|
||||
available = available_values & BIT(i);
|
||||
active = i == value;
|
||||
power_supply_escape_spaces(labels[i], escaped_label, sizeof(escaped_label));
|
||||
|
||||
if (available && active) {
|
||||
count += sysfs_emit_at(buf, count, "[%s] ", labels[i]);
|
||||
count += sysfs_emit_at(buf, count, "[%s] ", escaped_label);
|
||||
match = true;
|
||||
} else if (available) {
|
||||
count += sysfs_emit_at(buf, count, "%s ", labels[i]);
|
||||
count += sysfs_emit_at(buf, count, "%s ", escaped_label);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,8 +373,8 @@ static ssize_t power_supply_store_property(struct device *dev,
|
||||
|
||||
ret = -EINVAL;
|
||||
if (ps_attr->text_values_len > 0) {
|
||||
ret = __sysfs_match_string(ps_attr->text_values,
|
||||
ps_attr->text_values_len, buf);
|
||||
ret = power_supply_match_string(ps_attr->text_values,
|
||||
ps_attr->text_values_len, buf);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user