Debian Codegraph @

src/libqmi-glib/qmi-client.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-2017 Aleksander Morgado <[email protected]>
21 */
22
23 #include <gio/gio.h>
24
25 #include "qmi-error-types.h"
26 #include "qmi-enum-types.h"
27 #include "qmi-flag-types.h"
28 #include "qmi-device.h"
29 #include "qmi-client.h"
30 #include "qmi-ctl.h"
31
32 G_DEFINE_ABSTRACT_TYPE (QmiClient, qmi_client, G_TYPE_OBJECT)
33
34 enum {
35 PROP_0,
36 PROP_DEVICE,
37 PROP_SERVICE,
38 PROP_CID,
39 PROP_VERSION_MAJOR,
40 PROP_VERSION_MINOR,
41 PROP_VALID,
42 PROP_LAST
43 };
44
45 static GParamSpec *properties[PROP_LAST];
46
47 struct _QmiClientPrivate {
48 QmiDevice *device;
49 QmiService service;
50 guint8 cid;
51 guint version_major;
52 guint version_minor;
53
54 guint16 transaction_id;
55 };
56
57 /*****************************************************************************/
58
59 GObject *
60 qmi_client_get_device (QmiClient *self)
61 {
62 GObject *device;
63
64 g_return_val_if_fail (QMI_IS_CLIENT (self), NULL);
65
66 g_object_get (G_OBJECT (self),
67 QMI_CLIENT_DEVICE, &device,
68 NULL);
69
70 return device;
71 }
72
73 GObject *
74 qmi_client_peek_device (QmiClient *self)
75 {
76 g_return_val_if_fail (QMI_IS_CLIENT (self), NULL);
77
78 return G_OBJECT (self->priv->device);
79 }
80
81 QmiService
82 qmi_client_get_service (QmiClient *self)
83 {
84 g_return_val_if_fail (QMI_IS_CLIENT (self), QMI_SERVICE_UNKNOWN);
85
86 return self->priv->service;
87 }
88
89 guint8
90 qmi_client_get_cid (QmiClient *self)
91 {
92 g_return_val_if_fail (QMI_IS_CLIENT (self), QMI_CID_NONE);
93
94 return self->priv->cid;
95 }
96
97 gboolean
98 qmi_client_is_valid (QmiClient *self)
99 {
100 g_return_val_if_fail (QMI_IS_CLIENT (self), FALSE);
101
102 return (self->priv->service != QMI_SERVICE_UNKNOWN &&
103 QMI_IS_DEVICE (self->priv->device) &&
104 ((self->priv->cid != QMI_CID_NONE || self->priv->service == QMI_SERVICE_CTL)));
105 }
106
107 gboolean
108 qmi_client_get_version (QmiClient *self,
109 guint *major,
110 guint *minor)
111 {
112 g_return_val_if_fail (QMI_IS_CLIENT (self), FALSE);
113
114 /* If the major version is greater than zero, assume it was
115 * set properly */
116 if (!self->priv->version_major)
117 return FALSE;
118
119 *major = self->priv->version_major;
120 *minor = self->priv->version_minor;
121 return TRUE;
122 }
123
124 gboolean
125 qmi_client_check_version (QmiClient *self,
126 guint major,
127 guint minor)
128 {
129 g_return_val_if_fail (QMI_IS_CLIENT (self), FALSE);
130
131 /* If the major version is greater than zero, assume it was
132 * set properly */
133 if (!self->priv->version_major)
134 return FALSE;
135
136 if (self->priv->version_major > major ||
137 (self->priv->version_major == major &&
138 self->priv->version_minor >= minor))
139 return TRUE;
140
141 return FALSE;
142 }
143
144 guint16
145 qmi_client_get_next_transaction_id (QmiClient *self)
146 {
147 guint16 next;
148
149 g_return_val_if_fail (QMI_IS_CLIENT (self), 0);
150
151 next = self->priv->transaction_id;
152
153 /* Don't go further than 8bits in the CTL service */
154 if ((self->priv->service == QMI_SERVICE_CTL &&
155 self->priv->transaction_id == G_MAXUINT8) ||
156 self->priv->transaction_id == G_MAXUINT16)
157 /* Reset! */
158 self->priv->transaction_id = 0x01;
159 else
160 self->priv->transaction_id++;
161
162 return next;
163 }
164
165 /*****************************************************************************/
166
167 void
168 __qmi_client_process_indication (QmiClient *self,
169 QmiMessage *message)
170 {
171 if (QMI_CLIENT_GET_CLASS (self)->process_indication)
172 QMI_CLIENT_GET_CLASS (self)->process_indication (self, message);
173 }
174
175 /*****************************************************************************/
176
177 static void
178 set_property (GObject *object,
179 guint prop_id,
180 const GValue *value,
181 GParamSpec *pspec)
182 {
183 QmiClient *self = QMI_CLIENT (object);
184
185 switch (prop_id) {
186 case PROP_DEVICE:
187 /* NOTE!! We do NOT keep a reference to the device here.
188 * Clients are OWNED by the device */
189 self->priv->device = g_value_get_object (value);
190 break;
191 case PROP_SERVICE:
192 self->priv->service = g_value_get_enum (value);
193 break;
194 case PROP_CID:
195 self->priv->cid = (guint8)g_value_get_uint (value);
196 break;
197 case PROP_VERSION_MAJOR:
198 self->priv->version_major = g_value_get_uint (value);
199 break;
200 case PROP_VERSION_MINOR:
201 self->priv->version_minor = g_value_get_uint (value);
202 break;
203 default:
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 break;
206 }
207 }
208
209 static void
210 get_property (GObject *object,
211 guint prop_id,
212 GValue *value,
213 GParamSpec *pspec)
214 {
215 QmiClient *self = QMI_CLIENT (object);
216
217 switch (prop_id) {
218 case PROP_DEVICE:
219 g_value_set_object (value, self->priv->device);
220 break;
221 case PROP_SERVICE:
222 g_value_set_enum (value, self->priv->service);
223 break;
224 case PROP_CID:
225 g_value_set_uint (value, (guint)self->priv->cid);
226 break;
227 case PROP_VERSION_MAJOR:
228 g_value_set_uint (value, self->priv->version_major);
229 break;
230 case PROP_VERSION_MINOR:
231 g_value_set_uint (value, self->priv->version_minor);
232 break;
233 case PROP_VALID:
234 g_value_set_boolean (value, qmi_client_is_valid (self));
235 break;
236 default:
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 break;
239 }
240 }
241
242 static void
243 qmi_client_init (QmiClient *self)
244 {
245 self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
246 QMI_TYPE_CLIENT,
247 QmiClientPrivate);
248
249 /* Defaults */
250 self->priv->service = QMI_SERVICE_UNKNOWN;
251 self->priv->transaction_id = 0x01;
252 self->priv->cid = QMI_CID_NONE;
253 self->priv->version_major = 0;
254 self->priv->version_minor = 0;
255 }
256
257 static void
258 qmi_client_class_init (QmiClientClass *klass)
259 {
260 GObjectClass *object_class = G_OBJECT_CLASS (klass);
261
262 g_type_class_add_private (object_class, sizeof (QmiClientPrivate));
263
264 object_class->get_property = get_property;
265 object_class->set_property = set_property;
266
267 /**
268 * QmiClient:client-device:
269 *
270 * Since: 1.0
271 */
272 properties[PROP_DEVICE] =
273 g_param_spec_object (QMI_CLIENT_DEVICE,
274 "Device",
275 "The QMI device",
276 QMI_TYPE_DEVICE,
277 G_PARAM_READWRITE);
278 g_object_class_install_property (object_class, PROP_DEVICE, properties[PROP_DEVICE]);
279
280 /**
281 * QmiClient:client-service:
282 *
283 * Since: 1.0
284 */
285 properties[PROP_SERVICE] =
286 g_param_spec_enum (QMI_CLIENT_SERVICE,
287 "Service",
288 "QMI service this client is using",
289 QMI_TYPE_SERVICE,
290 QMI_SERVICE_UNKNOWN,
291 G_PARAM_READWRITE);
292 g_object_class_install_property (object_class, PROP_SERVICE, properties[PROP_SERVICE]);
293
294 /**
295 * QmiClient:client-cid:
296 *
297 * Since: 1.0
298 */
299 properties[PROP_CID] =
300 g_param_spec_uint (QMI_CLIENT_CID,
301 "Client ID",
302 "ID of the client registered into the QMI device",
303 0,
304 G_MAXUINT8,
305 QMI_CID_NONE,
306 G_PARAM_READWRITE);
307 g_object_class_install_property (object_class, PROP_CID, properties[PROP_CID]);
308
309 /**
310 * QmiClient:client-version-major:
311 *
312 * Since: 1.0
313 */
314 properties[PROP_VERSION_MAJOR] =
315 g_param_spec_uint (QMI_CLIENT_VERSION_MAJOR,
316 "Version major",
317 "Major version of the service handled by this client",
318 0,
319 G_MAXUINT,
320 0,
321 G_PARAM_READWRITE);
322 g_object_class_install_property (object_class, PROP_VERSION_MAJOR, properties[PROP_VERSION_MAJOR]);
323
324 /**
325 * QmiClient:client-version-minor:
326 *
327 * Since: 1.0
328 */
329 properties[PROP_VERSION_MINOR] =
330 g_param_spec_uint (QMI_CLIENT_VERSION_MINOR,
331 "Version minor",
332 "Minor version of the service handled by this client",
333 0,
334 G_MAXUINT,
335 0,
336 G_PARAM_READWRITE);
337 g_object_class_install_property (object_class, PROP_VERSION_MINOR, properties[PROP_VERSION_MINOR]);
338
339 /**
340 * QmiClient:client-valid:
341 *
342 * Since: 1.20
343 */
344 properties[PROP_VALID] =
345 g_param_spec_boolean (QMI_CLIENT_VALID,
346 "Valid",
347 "Whether the client is valid and usable",
348 FALSE,
349 G_PARAM_READABLE);
350 g_object_class_install_property (object_class, PROP_VALID, properties[PROP_VALID]);
351 }