+/*
+ * Sets path to the hwmon attribute if this channel group
+ * supports shunt resistance setting. The caller has to supply
+ * a valid GString.
+ */
+static int get_shunt_path(const struct sr_channel_group *cg, GString *path)
+{
+ struct channel_group_priv *cgp;
+ int ret = SR_OK, status;
+
+ cgp = cg->priv;
+
+ if (cgp->probe_type != PROBE_ENRG) {
+ sr_err("Probe doesn't support shunt resistance setting");
+ return SR_ERR_ARG;
+ }
+
+ g_string_append_printf(path,
+ "/sys/class/hwmon/hwmon%d/shunt_resistor",
+ cgp->hwmon_num);
+
+ /*
+ * The shunt_resistor sysfs attribute is available
+ * in the Linux kernel since version 3.20. We have
+ * to notify the user if this attribute is not present.
+ */
+ status = g_file_test(path->str, G_FILE_TEST_EXISTS);
+ if (!status) {
+ sr_err("shunt_resistance attribute not present, please update "
+ "your kernel to version >=3.20");
+ return SR_ERR_NA;
+ }
+
+ return ret;
+}
+
+SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg,
+ uint64_t *shunt)
+{
+ GString *path = g_string_sized_new(64);
+ gchar *contents;
+ int status, ret = SR_OK;
+ GError *err = NULL;
+
+ status = get_shunt_path(cg, path);
+ if (status != SR_OK) {
+ ret = status;
+ goto out;
+ }
+
+ status = g_file_get_contents(path->str, &contents, NULL, &err);
+ if (!status) {
+ sr_err("Error reading shunt resistance: %s", err->message);
+ ret = SR_ERR_IO;
+ goto out;
+ }
+
+ *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10));
+
+out:
+ g_string_free(path, TRUE);
+ return ret;
+}
+
+SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
+{
+ GString *path = g_string_sized_new(64);;
+ int status, ret = SR_OK;
+ FILE *fd;
+
+ status = get_shunt_path(cg, path);
+ if (status != SR_OK) {
+ ret = status;
+ goto out;
+ }
+
+ /*
+ * Can't use g_file_set_contents() here, as it calls open() with
+ * O_EXEC flag in a sysfs directory thus failing with EACCES.
+ */
+ fd = g_fopen(path->str, "w");
+ if (!fd) {
+ sr_err("Error opening %s: %s", path->str, strerror(errno));
+ ret = SR_ERR_IO;
+ goto out;
+ }
+
+ g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
+ fclose(fd);
+
+out:
+ g_string_free(path, TRUE);
+ return ret;
+}
+
+SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg,
+ gboolean *off)
+{
+ struct channel_group_priv *cgp;
+ int val;
+
+ cgp = cg->priv;
+
+ val = sr_gpio_getval_export(pws_info_gpios[cgp->index]);
+ if (val != 1) {
+ sr_err("Probe has no power-switch");
+ return SR_ERR_ARG;
+ }
+
+ val = sr_gpio_getval_export(pws_gpios[cgp->index]);
+ *off = val ? FALSE : TRUE;
+
+ return SR_OK;
+}
+
+SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
+ gboolean off)
+{
+ struct channel_group_priv *cgp;
+ int val;
+
+ cgp = cg->priv;
+
+ val = sr_gpio_getval_export(pws_info_gpios[cgp->index]);
+ if (val != 1) {
+ sr_err("Probe has no power-switch");
+ return SR_ERR_ARG;
+ }
+
+ val = sr_gpio_setval_export(pws_gpios[cgp->index], off ? 0 : 1);
+
+ return SR_OK;
+}
+