summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2014-03-05 08:56:13 +0100
committerAleksander Morgado <aleksander@aleksander.es>2014-03-05 08:56:13 +0100
commit378d71b61de6bd910659c02a52360262662a06bf (patch)
tree9fc4ae749fbaaf6528bae74caf222435d6cf7d2a
parentf493cf758320e0d49cc95ffc28ef7554d277ab3e (diff)
downloadlibmbim-378d71b61de6bd910659c02a52360262662a06bf.tar.gz
mbimcli: new helper method to read uints from strings
-rw-r--r--src/mbimcli/Makefile.am4
-rw-r--r--src/mbimcli/mbimcli-dss.c18
-rw-r--r--src/mbimcli/mbimcli-helpers.c49
-rw-r--r--src/mbimcli/mbimcli-helpers.h31
-rw-r--r--src/mbimcli/mbimcli-phonebook.c9
-rw-r--r--src/mbimcli/mbimcli.c25
6 files changed, 88 insertions, 48 deletions
diff --git a/src/mbimcli/Makefile.am b/src/mbimcli/Makefile.am
index 224feb0..4c2d1df 100644
--- a/src/mbimcli/Makefile.am
+++ b/src/mbimcli/Makefile.am
@@ -10,8 +10,8 @@ mbimcli_CPPFLAGS = \
-I$(top_builddir)/src/libmbim-glib/generated
mbimcli_SOURCES = \
- mbimcli.h \
- mbimcli.c \
+ mbimcli.h mbimcli.c \
+ mbimcli-helpers.h mbimcli-helpers.c \
mbimcli-basic-connect.c \
mbimcli-phonebook.c \
mbimcli-ms-firmware-id.c \
diff --git a/src/mbimcli/mbimcli-dss.c b/src/mbimcli/mbimcli-dss.c
index f6dc92c..a674dd6 100644
--- a/src/mbimcli/mbimcli-dss.c
+++ b/src/mbimcli/mbimcli-dss.c
@@ -32,6 +32,7 @@
#include <libmbim-glib.h>
#include "mbimcli.h"
+#include "mbimcli-helpers.h"
/* Context */
typedef struct {
@@ -161,21 +162,6 @@ set_dss_ready (MbimDevice *device,
}
static gboolean
-parse_uint (const gchar *str,
- guint *u)
-{
- gulong num;
-
- errno = 0;
- num = strtoul (str, NULL, 10);
- if (errno || num > G_MAXUINT)
- return FALSE;
-
- *u = (guint)num;
- return TRUE;
-}
-
-static gboolean
common_parse (const gchar *str,
MbimUuid *dsid,
guint32 *ssid)
@@ -197,7 +183,7 @@ common_parse (const gchar *str,
g_printerr ("error: couldn't parse input string, missing arguments\n");
else if (!mbim_uuid_from_printable (split[0], dsid))
g_printerr ("error: couldn't parse UUID, should be xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n");
- else if (!parse_uint (split[1], ssid))
+ else if (!mbimcli_read_uint_from_string (split[1], ssid))
g_printerr ("error: couldn't parse Session ID, should be a number\n");
else
status = TRUE;
diff --git a/src/mbimcli/mbimcli-helpers.c b/src/mbimcli/mbimcli-helpers.c
new file mode 100644
index 0000000..aa37d5a
--- /dev/null
+++ b/src/mbimcli/mbimcli-helpers.c
@@ -0,0 +1,49 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * mbimcli -- Command line interface to control MBIM devices
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) 2014 Aleksander Morgado <aleksander@aleksander.es>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "mbimcli-helpers.h"
+
+gboolean
+mbimcli_read_uint_from_string (const gchar *str,
+ guint *out)
+{
+ gulong num;
+
+ if (!str || !str[0])
+ return FALSE;
+
+ for (num = 0; str[num]; num++) {
+ if (!g_ascii_isdigit (str[num]))
+ return FALSE;
+ }
+
+ errno = 0;
+ num = strtoul (str, NULL, 10);
+ if (!errno && num <= G_MAXUINT) {
+ *out = (guint)num;
+ return TRUE;
+ }
+ return FALSE;
+}
diff --git a/src/mbimcli/mbimcli-helpers.h b/src/mbimcli/mbimcli-helpers.h
new file mode 100644
index 0000000..d7dabdc
--- /dev/null
+++ b/src/mbimcli/mbimcli-helpers.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * mbimcli -- Command line interface to control QMI devices
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright (C) 2014 Aleksander Morgado <aleksander@aleksander.es>
+ */
+
+#include <glib.h>
+
+#include <libmbim-glib.h>
+
+#ifndef __MBIMCLI_HELPERS_H__
+#define __MBIMCLI_HELPERS_H__
+
+gboolean mbimcli_read_uint_from_string (const gchar *str,
+ guint *out);
+
+#endif /* __MBIMCLI_H__ */
diff --git a/src/mbimcli/mbimcli-phonebook.c b/src/mbimcli/mbimcli-phonebook.c
index 0a544d6..eb0b054 100644
--- a/src/mbimcli/mbimcli-phonebook.c
+++ b/src/mbimcli/mbimcli-phonebook.c
@@ -32,6 +32,7 @@
#include <libmbim-glib.h>
#include "mbimcli.h"
+#include "mbimcli-helpers.h"
/* Context */
typedef struct {
@@ -169,17 +170,11 @@ phonebook_write_input_parse (const gchar *str,
/* Check whether we have the optional Index item */
if (split[2]) {
- gulong num;
-
- errno = 0;
- num = strtoul (split[2], NULL, 10);
- if (errno || num > G_MAXUINT) {
+ if (!mbimcli_read_uint_from_string (split[2], idx)) {
g_printerr ("error: couldn't parse input string, invalid index '%s'\n", split[2]);
g_strfreev (split);
return FALSE;
}
-
- *idx = (guint)num;
} else {
/* Default to index 0, which is an invalid one */
*idx = 0;
diff --git a/src/mbimcli/mbimcli.c b/src/mbimcli/mbimcli.c
index e99f543..46d6a42 100644
--- a/src/mbimcli/mbimcli.c
+++ b/src/mbimcli/mbimcli.c
@@ -33,6 +33,7 @@
#include <libmbim-glib.h>
#include "mbimcli.h"
+#include "mbimcli-helpers.h"
#define PROGRAM_NAME "mbimcli"
#define PROGRAM_VERSION PACKAGE_VERSION
@@ -268,27 +269,6 @@ device_open_ready (MbimDevice *dev,
}
}
-static guint
-read_transaction_id (const gchar *str)
-{
- gulong num;
-
- if (!str || !str[0])
- return 0;
-
- for (num = 0; str[num]; num++) {
- if (!g_ascii_isdigit (str[num]))
- return 0;
- }
-
- errno = 0;
- num = strtoul (str, NULL, 10);
- if (!errno && num <= G_MAXUINT) {
- return (guint)num;
- }
- return 0;
-}
-
static void
device_new_ready (GObject *unused,
GAsyncResult *res)
@@ -306,8 +286,7 @@ device_new_ready (GObject *unused,
if (no_open_str) {
guint transaction_id;
- transaction_id = read_transaction_id (no_open_str);
- if (!transaction_id) {
+ if (!mbimcli_read_uint_from_string (no_open_str, &transaction_id)) {
g_printerr ("error: invalid transaction ID specified: %s\n",
no_open_str);
exit (EXIT_FAILURE);