summaryrefslogtreecommitdiff
path: root/src/libqmi-glib/qmi-helpers.h
blob: 48b9f6df32edf34a61c49f16e4afe6efe4af7902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 * libqmi-glib -- GLib/GIO based library to control QMI devices
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2012-2015 Dan Williams <dcbw@redhat.com>
 * Copyright (C) 2012-2017 Aleksander Morgado <aleksander@aleksander.es>
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 * Copyright (C) 2000 Wim Taymans <wtay@chello.be>
 * Copyright (C) 2002 Thomas Vander Stichele <thomas@apestaart.org>

 */

#ifndef _LIBQMI_GLIB_QMI_HELPERS_H_
#define _LIBQMI_GLIB_QMI_HELPERS_H_

#include <glib.h>
#include <gio/gio.h>

G_BEGIN_DECLS

G_GNUC_INTERNAL
gchar *qmi_helpers_str_hex (gconstpointer mem,
                            gsize         size,
                            gchar         delimiter);
G_GNUC_INTERNAL
gboolean qmi_helpers_check_user_allowed  (uid_t    uid,
                                          GError **error);

G_GNUC_INTERNAL
gboolean qmi_helpers_string_utf8_validate_printable (const guint8 *utf8,
                                                     gsize         utf8_len);

G_GNUC_INTERNAL
gchar *qmi_helpers_string_utf8_from_gsm7 (const guint8 *gsm,
                                          gsize         gsm_len);

G_GNUC_INTERNAL
gchar *qmi_helpers_string_utf8_from_ucs2le (const guint8 *ucs2le,
                                            gsize         ucs2le_len);

typedef enum {
    QMI_HELPERS_TRANSPORT_TYPE_UNKNOWN,
    QMI_HELPERS_TRANSPORT_TYPE_QMUX,
    QMI_HELPERS_TRANSPORT_TYPE_MBIM,
} QmiHelpersTransportType;

/*
 * Symbol defining the file name for a unix domain socket used instead of a device file.
 * The unix domain socket is addressed with the QMUX protocol.
 */
#define QMI_QMUX_SOCKET_FILE_NAME "qmux_socket"

G_GNUC_INTERNAL
QmiHelpersTransportType qmi_helpers_get_transport_type (const gchar  *path,
                                                        GError      **error);

G_GNUC_INTERNAL
gchar *qmi_helpers_get_devpath (const gchar  *cdc_wdm_path,
                                GError      **error);

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, /* caller allocates */
                                      guint         max_read_size,
                                      GError      **error);

G_GNUC_INTERNAL
gboolean qmi_helpers_write_sysfs_file (const gchar  *sysfs_path,
                                       const gchar  *value,
                                       GError      **error);

G_GNUC_INTERNAL
gboolean qmi_helpers_list_links (GFile         *sysfs_file,
                                 GCancellable  *cancellable,
                                 GPtrArray     *previous_links,
                                 GPtrArray    **out_links,
                                 GError       **error);

G_GNUC_INTERNAL
void qmi_helpers_clear_string (gchar **value);

static inline gfloat
QMI_GFLOAT_SWAP_LE_BE (gfloat in)
{
  union
  {
    guint32 i;
    gfloat f;
  } u;

  u.f = in;
  u.i = GUINT32_SWAP_LE_BE (u.i);
  return u.f;
}

static inline gdouble
QMI_GDOUBLE_SWAP_LE_BE (gdouble in)
{
  union
  {
    guint64 i;
    gdouble d;
  } u;

  u.d = in;
  u.i = GUINT64_SWAP_LE_BE (u.i);
  return u.d;
}

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define QMI_ENDIAN_HOST          QMI_ENDIAN_LITTLE
#define QMI_GFLOAT_TO_LE(val)    ((gfloat) (val))
#define QMI_GFLOAT_TO_BE(val)    (QMI_GFLOAT_SWAP_LE_BE (val))
#define QMI_GDOUBLE_TO_LE(val)   ((gdouble) (val))
#define QMI_GDOUBLE_TO_BE(val)   (QMI_GDOUBLE_SWAP_LE_BE (val))

#elif G_BYTE_ORDER == G_BIG_ENDIAN
#define QMI_ENDIAN_HOST          QMI_ENDIAN_BIG
#define QMI_GFLOAT_TO_LE(val)    (QMI_GFLOAT_SWAP_LE_BE (val))
#define QMI_GFLOAT_TO_BE(val)    ((gfloat) (val))
#define QMI_GDOUBLE_TO_LE(val)   (QMI_GDOUBLE_SWAP_LE_BE (val))
#define QMI_GDOUBLE_TO_BE(val)   ((gdouble) (val))

#else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
#error unknown ENDIAN type
#endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */

#define QMI_GFLOAT_FROM_LE(val)  (QMI_GFLOAT_TO_LE (val))
#define QMI_GFLOAT_FROM_BE(val)  (QMI_GFLOAT_TO_BE (val))
#define QMI_GDOUBLE_FROM_LE(val) (QMI_GDOUBLE_TO_LE (val))
#define QMI_GDOUBLE_FROM_BE(val) (QMI_GDOUBLE_TO_BE (val))



#if !GLIB_CHECK_VERSION(2,54,0)

/* Pointer Array lookup with a GEqualFunc, imported from GLib 2.54 */
#define g_ptr_array_find_with_equal_func qmi_ptr_array_find_with_equal_func
G_GNUC_INTERNAL
gboolean qmi_ptr_array_find_with_equal_func (GPtrArray     *haystack,
                                             gconstpointer  needle,
                                             GEqualFunc     equal_func,
                                             guint         *index_);

#endif

G_END_DECLS

#endif /* _LIBQMI_GLIB_QMI_UTILS_PRIVATE_H_ */