aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2021-02-04 22:04:03 +0100
committerAleksander Morgado <aleksander@aleksander.es>2021-02-15 15:01:23 +0000
commit4fe938621d528799b31b32952cd1db619df5a9e3 (patch)
treefbedac62f68e14a8108803c83314d1e031d2ff22 /src
parent46a3facf497af52afbd4ea2ab5e98e4b0229d6b5 (diff)
libqmi-glib,helpers: move sysfs R/W operations to the internal helpers
Diffstat (limited to 'src')
-rw-r--r--src/libqmi-glib/qmi-device.c82
-rw-r--r--src/libqmi-glib/qmi-helpers.c68
-rw-r--r--src/libqmi-glib/qmi-helpers.h10
3 files changed, 86 insertions, 74 deletions
diff --git a/src/libqmi-glib/qmi-device.c b/src/libqmi-glib/qmi-device.c
index f1dc83d..dad407a 100644
--- a/src/libqmi-glib/qmi-device.c
+++ b/src/libqmi-glib/qmi-device.c
@@ -766,72 +766,6 @@ qmi_device_get_wwan_iface (QmiDevice *self)
/* Expected data format */
static gboolean
-read_sysfs_file (const gchar *sysfs_path,
- gchar *out_value,
- GError **error)
-{
- FILE *f;
- gboolean status = FALSE;
-
- if (!(f = fopen (sysfs_path, "r"))) {
- g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
- "Failed to open sysfs file '%s': %s",
- sysfs_path, g_strerror (errno));
- goto out;
- }
-
- if (fread (out_value, 1, 1, f) != 1) {
- g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
- "Failed to read from sysfs file '%s': %s",
- sysfs_path, g_strerror (errno));
- goto out;
- }
-
- if (*out_value != 'Y' && *out_value != 'N') {
- g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
- "Unexpected sysfs file contents: %c", *out_value);
- goto out;
- }
-
- status = TRUE;
-
- out:
- if (f)
- fclose (f);
- return status;
-}
-
-static gboolean
-write_sysfs_file (const gchar *sysfs_path,
- gchar value,
- GError **error)
-{
- gboolean status = FALSE;
- FILE *f;
-
- if (!(f = fopen (sysfs_path, "w"))) {
- g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
- "Failed to open sysfs file '%s' for R/W: %s",
- sysfs_path, g_strerror (errno));
- goto out;
- }
-
- if (fwrite (&value, 1, 1, f) != 1) {
- g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
- "Failed to write to sysfs file '%s': %s",
- sysfs_path, g_strerror (errno));
- goto out;
- }
-
- status = TRUE;
-
- out:
- if (f)
- fclose (f);
- return status;
-}
-
-static gboolean
get_expected_data_format (QmiDevice *self,
const gchar *raw_ip_sysfs_path,
const gchar *pass_through_sysfs_path,
@@ -840,13 +774,13 @@ get_expected_data_format (QmiDevice *self,
gchar raw_ip_value = '\0';
gchar pass_through_value = '\0';
- if (!read_sysfs_file (raw_ip_sysfs_path, &raw_ip_value, error))
+ if (!qmi_helpers_read_sysfs_file (raw_ip_sysfs_path, &raw_ip_value, error))
return QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN;
if (raw_ip_value == 'N')
return QMI_DEVICE_EXPECTED_DATA_FORMAT_802_3;
- if (read_sysfs_file (pass_through_sysfs_path, &pass_through_value, NULL) && (pass_through_value == 'Y'))
+ if (qmi_helpers_read_sysfs_file (pass_through_sysfs_path, &pass_through_value, NULL) && (pass_through_value == 'Y'))
return QMI_DEVICE_EXPECTED_DATA_FORMAT_QMAP_PASS_THROUGH;
return QMI_DEVICE_EXPECTED_DATA_FORMAT_RAW_IP;
@@ -860,18 +794,18 @@ set_expected_data_format (QmiDevice *self,
GError **error)
{
if (requested == QMI_DEVICE_EXPECTED_DATA_FORMAT_802_3) {
- write_sysfs_file (pass_through_sysfs_path, 'N', NULL);
- return write_sysfs_file (raw_ip_sysfs_path, 'N', error);
+ qmi_helpers_write_sysfs_file (pass_through_sysfs_path, 'N', NULL);
+ return qmi_helpers_write_sysfs_file (raw_ip_sysfs_path, 'N', error);
}
if (requested == QMI_DEVICE_EXPECTED_DATA_FORMAT_RAW_IP) {
- write_sysfs_file (pass_through_sysfs_path, 'N', NULL);
- return write_sysfs_file (raw_ip_sysfs_path, 'Y', error);
+ qmi_helpers_write_sysfs_file (pass_through_sysfs_path, 'N', NULL);
+ return qmi_helpers_write_sysfs_file (raw_ip_sysfs_path, 'Y', error);
}
if (requested == QMI_DEVICE_EXPECTED_DATA_FORMAT_QMAP_PASS_THROUGH) {
- return (write_sysfs_file (raw_ip_sysfs_path, 'Y', error) &&
- write_sysfs_file (pass_through_sysfs_path, 'Y', error));
+ return (qmi_helpers_write_sysfs_file (raw_ip_sysfs_path, 'Y', error) &&
+ qmi_helpers_write_sysfs_file (pass_through_sysfs_path, 'Y', error));
}
g_assert_not_reached ();
diff --git a/src/libqmi-glib/qmi-helpers.c b/src/libqmi-glib/qmi-helpers.c
index acb8e22..b9fa348 100644
--- a/src/libqmi-glib/qmi-helpers.c
+++ b/src/libqmi-glib/qmi-helpers.c
@@ -32,6 +32,8 @@
#include <pwd.h>
#include <errno.h>
+#include <gio/gio.h>
+
#include "qmi-helpers.h"
#include "qmi-error-types.h"
@@ -561,3 +563,69 @@ qmi_helpers_get_devname (const gchar *cdc_wdm_path,
return devname;
}
+
+gboolean
+qmi_helpers_read_sysfs_file (const gchar *sysfs_path,
+ gchar *out_value,
+ GError **error)
+{
+ FILE *f;
+ gboolean status = FALSE;
+
+ if (!(f = fopen (sysfs_path, "r"))) {
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
+ "Failed to open sysfs file '%s': %s",
+ sysfs_path, g_strerror (errno));
+ goto out;
+ }
+
+ if (fread (out_value, 1, 1, f) != 1) {
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
+ "Failed to read from sysfs file '%s': %s",
+ sysfs_path, g_strerror (errno));
+ goto out;
+ }
+
+ if (*out_value != 'Y' && *out_value != 'N') {
+ g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED,
+ "Unexpected sysfs file contents: %c", *out_value);
+ goto out;
+ }
+
+ status = TRUE;
+
+ out:
+ if (f)
+ fclose (f);
+ return status;
+}
+
+gboolean
+qmi_helpers_write_sysfs_file (const gchar *sysfs_path,
+ gchar value,
+ GError **error)
+{
+ gboolean status = FALSE;
+ FILE *f;
+
+ if (!(f = fopen (sysfs_path, "w"))) {
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
+ "Failed to open sysfs file '%s' for R/W: %s",
+ sysfs_path, g_strerror (errno));
+ goto out;
+ }
+
+ if (fwrite (&value, 1, 1, f) != 1) {
+ g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
+ "Failed to write to sysfs file '%s': %s",
+ sysfs_path, g_strerror (errno));
+ goto out;
+ }
+
+ status = TRUE;
+
+ out:
+ if (f)
+ fclose (f);
+ return status;
+}
diff --git a/src/libqmi-glib/qmi-helpers.h b/src/libqmi-glib/qmi-helpers.h
index f80e334..3bda581 100644
--- a/src/libqmi-glib/qmi-helpers.h
+++ b/src/libqmi-glib/qmi-helpers.h
@@ -71,6 +71,16 @@ G_GNUC_INTERNAL
gchar *qmi_helpers_get_devname (const gchar *cdc_wdm_path,
GError **error);
+G_GNUC_INTERNAL
+gboolean qmi_helpers_read_sysfs_file (const gchar *sysfs_path,
+ gchar *out_value,
+ GError **error);
+
+G_GNUC_INTERNAL
+gboolean qmi_helpers_write_sysfs_file (const gchar *sysfs_path,
+ gchar value,
+ GError **error);
+
static inline gfloat
QMI_GFLOAT_SWAP_LE_BE (gfloat in)
{