Debian Codegraph @

src/libqmi-glib/qmi-endpoint.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * libqmi-glib -- GLib/GIO based library to control QMI devices
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
19 *
20 * Copyright (C) 2012 Lanedo GmbH
21 * Copyright (C) 2012-2017 Aleksander Morgado <[email protected]>
22 * Copyright (C) 2019 Eric Caruso <[email protected]>
23 */
24
25 #include "qmi-endpoint.h"
26
27 #include "qmi-common.h"
28 #include "qmi-error-types.h"
29 #include "qmi-errors.h"
30
31 G_DEFINE_TYPE (QmiEndpoint, qmi_endpoint, G_TYPE_OBJECT)
32
33 struct _QmiEndpointPrivate {
34 GByteArray *buffer;
35 QmiFile *file;
36 };
37
38 enum {
39 PROP_0,
40 PROP_FILE,
41 PROP_LAST
42 };
43
44 enum {
45 SIGNAL_NEW_DATA,
46 SIGNAL_HANGUP,
47 SIGNAL_LAST
48 };
49
50 static GParamSpec *properties[PROP_LAST];
51 static guint signals[SIGNAL_LAST] = { 0 };
52
53 /*****************************************************************************/
54
55 gboolean
56 qmi_endpoint_parse_buffer (QmiEndpoint *self,
57 QmiMessageHandler handler,
58 gpointer user_data,
59 GError **error)
60 {
61 do {
62 GError *inner_error = NULL;
63 QmiMessage *message;
64
65 /* Every message received must start with the QMUX or QRTR marker.
66 * If it doesn't, we broke framing :-/
67 * If we broke framing, an error should be reported and the device
68 * should get closed */
69 if (self->priv->buffer->len > 0 &&
70 self->priv->buffer->data[0] != QMI_MESSAGE_QMUX_MARKER &&
71 self->priv->buffer->data[0] != QMI_MESSAGE_QRTR_MARKER) {
72 g_set_error (error,
73 QMI_PROTOCOL_ERROR,
74 QMI_PROTOCOL_ERROR_MALFORMED_MESSAGE,
75 "QMI framing error detected");
76 g_signal_emit (self, signals[SIGNAL_HANGUP], 0);
77 return FALSE;
78 }
79
80 message = qmi_message_new_from_raw (self->priv->buffer, &inner_error);
81 if (!message) {
82 if (!inner_error)
83 /* More data we need */
84 return TRUE;
85
86 /* Warn about the issue */
87 g_warning ("[%s] invalid message received: '%s'",
88 qmi_file_get_path_display (self->priv->file),
89 inner_error->message);
90 g_error_free (inner_error);
91
92 if (qmi_utils_get_traces_enabled ()) {
93 gchar *printable;
94 guint len = MIN (self->priv->buffer->len, 2048);
95
96 printable = qmi_common_str_hex (self->priv->buffer->data, len, ':');
97 g_debug ("<<<<<< RAW INVALID MESSAGE:\n"
98 "<<<<<< length = %u\n"
99 "<<<<<< data = %s\n",
100 self->priv->buffer->len, /* show full buffer len */
101 printable);
102 g_free (printable);
103 }
104
105 } else {
106 /* Play with the received message */
107 handler (message, user_data);
108 qmi_message_unref (message);
109 }
110 } while (self->priv->buffer->len > 0);
111
112 return TRUE;
113 }
114
115 void
116 qmi_endpoint_add_message (QmiEndpoint *self,
117 const guint8 *data,
118 guint len)
119 {
120 self->priv->buffer = g_byte_array_append (self->priv->buffer, data, len);
121 g_signal_emit (self, signals[SIGNAL_NEW_DATA], 0);
122 }
123
124 /*****************************************************************************/
125
126 static gboolean
127 endpoint_setup_indications_finish (QmiEndpoint *self,
128 GAsyncResult *res,
129 GError **error)
130 {
131 return g_task_propagate_boolean (G_TASK (res), error);
132 }
133
134 static void
135 endpoint_setup_indications (QmiEndpoint *self,
136 guint timeout,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
139 gpointer user_data)
140 {
141 GTask *task;
142
143 task = g_task_new (self, cancellable, callback, user_data);
144
145 g_task_return_boolean (task, TRUE);
146 g_object_unref (task);
147 }
148
149 /*****************************************************************************/
150
151 const gchar *
152 qmi_endpoint_get_name (QmiEndpoint *self)
153 {
154 return qmi_file_get_path_display (self->priv->file);
155 }
156
157 gboolean
158 qmi_endpoint_open_finish (QmiEndpoint *self,
159 GAsyncResult *res,
160 GError **error)
161 {
162 return QMI_ENDPOINT_GET_CLASS (self)->open_finish (self, res, error);
163 }
164
165 void
166 qmi_endpoint_open (QmiEndpoint *self,
167 gboolean use_proxy,
168 guint timeout,
169 GCancellable *cancellable,
170 GAsyncReadyCallback callback,
171 gpointer user_data)
172 {
173 g_assert (QMI_ENDPOINT_GET_CLASS (self)->open &&
174 QMI_ENDPOINT_GET_CLASS (self)->open_finish);
175
176 return QMI_ENDPOINT_GET_CLASS (self)->open (self,
177 use_proxy,
178 timeout,
179 cancellable,
180 callback,
181 user_data);
182 }
183
184 gboolean
185 qmi_endpoint_is_open (QmiEndpoint *self)
186 {
187 g_assert (QMI_ENDPOINT_GET_CLASS (self)->is_open);
188
189 return QMI_ENDPOINT_GET_CLASS (self)->is_open (self);
190 }
191
192 gboolean
193 qmi_endpoint_setup_indications_finish (QmiEndpoint *self,
194 GAsyncResult *res,
195 GError **error)
196 {
197 return QMI_ENDPOINT_GET_CLASS (self)->setup_indications_finish (self, res, error);
198 }
199
200 void
201 qmi_endpoint_setup_indications (QmiEndpoint *self,
202 guint timeout,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
205 gpointer user_data)
206 {
207 g_assert (QMI_ENDPOINT_GET_CLASS (self)->setup_indications &&
208 QMI_ENDPOINT_GET_CLASS (self)->setup_indications_finish);
209
210 return QMI_ENDPOINT_GET_CLASS (self)->setup_indications (self,
211 timeout,
212 cancellable,
213 callback,
214 user_data);
215 }
216
217 gboolean
218 qmi_endpoint_send (QmiEndpoint *self,
219 QmiMessage *message,
220 guint timeout,
221 GCancellable *cancellable,
222 GError **error)
223 {
224 g_assert (QMI_ENDPOINT_GET_CLASS (self)->send);
225
226 return QMI_ENDPOINT_GET_CLASS (self)->send (self, message, timeout, cancellable, error);
227 }
228
229 gboolean
230 qmi_endpoint_close_finish (QmiEndpoint *self,
231 GAsyncResult *res,
232 GError **error)
233 {
234 return QMI_ENDPOINT_GET_CLASS (self)->close_finish (self, res, error);
235 }
236
237 void
238 qmi_endpoint_close (QmiEndpoint *self,
239 guint timeout,
240 GCancellable *cancellable,
241 GAsyncReadyCallback callback,
242 gpointer user_data)
243 {
244 g_assert (QMI_ENDPOINT_GET_CLASS (self)->close &&
245 QMI_ENDPOINT_GET_CLASS (self)->close_finish);
246
247 return QMI_ENDPOINT_GET_CLASS (self)->close (self,
248 timeout,
249 cancellable,
250 callback,
251 user_data);
252 }
253
254 /*****************************************************************************/
255
256 static void
257 set_property (GObject *object,
258 guint prop_id,
259 const GValue *value,
260 GParamSpec *pspec)
261 {
262 QmiEndpoint *self = QMI_ENDPOINT (object);
263
264 switch (prop_id) {
265 case PROP_FILE:
266 g_assert (self->priv->file == NULL);
267 self->priv->file = g_value_dup_object (value);
268 break;
269 default:
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271 break;
272 }
273 }
274
275 static void
276 get_property (GObject *object,
277 guint prop_id,
278 GValue *value,
279 GParamSpec *pspec)
280 {
281 QmiEndpoint *self = QMI_ENDPOINT (object);
282
283 switch (prop_id) {
284 case PROP_FILE:
285 g_value_set_object (value, self->priv->file);
286 break;
287 default:
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 break;
290 }
291 }
292
293 /*****************************************************************************/
294
295 static void
296 qmi_endpoint_init (QmiEndpoint *self)
297 {
298 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
299 QMI_TYPE_ENDPOINT,
300 QmiEndpointPrivate);
301
302 self->priv->buffer = g_byte_array_new ();
303 }
304
305 static void
306 dispose (GObject *object)
307 {
308 QmiEndpoint *self = QMI_ENDPOINT (object);
309
310 g_clear_pointer (&self->priv->buffer, g_byte_array_unref);
311 g_clear_object (&self->priv->file);
312
313 G_OBJECT_CLASS (qmi_endpoint_parent_class)->dispose (object);
314 }
315
316 static void
317 qmi_endpoint_class_init (QmiEndpointClass *klass)
318 {
319 GObjectClass *object_class = G_OBJECT_CLASS (klass);
320
321 g_type_class_add_private (object_class, sizeof (QmiEndpointPrivate));
322
323 object_class->get_property = get_property;
324 object_class->set_property = set_property;
325 object_class->dispose = dispose;
326
327 klass->setup_indications = endpoint_setup_indications;
328 klass->setup_indications_finish = endpoint_setup_indications_finish;
329
330 properties[PROP_FILE] =
331 g_param_spec_object (QMI_ENDPOINT_FILE,
332 "Device file",
333 "File to the underlying QMI device",
334 QMI_TYPE_FILE,
335 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
336 g_object_class_install_property (object_class, PROP_FILE, properties[PROP_FILE]);
337
338 signals[SIGNAL_NEW_DATA] =
339 g_signal_new (QMI_ENDPOINT_SIGNAL_NEW_DATA,
340 G_OBJECT_CLASS_TYPE (G_OBJECT_CLASS (klass)),
341 G_SIGNAL_RUN_LAST,
342 0,
343 NULL,
344 NULL,
345 NULL,
346 G_TYPE_NONE,
347 0);
348
349 signals[SIGNAL_HANGUP] =
350 g_signal_new (QMI_ENDPOINT_SIGNAL_HANGUP,
351 G_OBJECT_CLASS_TYPE (G_OBJECT_CLASS (klass)),
352 G_SIGNAL_RUN_LAST,
353 0,
354 NULL,
355 NULL,
356 NULL,
357 G_TYPE_NONE,
358 0);
359 }