aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmi-message-ctl.c
blob: 35f8c346fa23c01e4b1894bfb7142d4ef203d8de (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* -*- 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) 2011 - 2012 Red Hat, Inc.
 * Copyright (C) 2012 Aleksander Morgado <aleksander@lanedo.com>
 */

#include <endian.h>

#include "qmi-message-ctl.h"
#include "qmi-enums.h"
#include "qmi-error-types.h"

/*****************************************************************************/
/* Version info */

/**
 * QmiCtlVersionInfo:
 *
 * An opaque type specifying a supported service.
 */
struct _QmiCtlVersionInfo {
    volatile gint ref_count;
    QmiService service;
    guint16 major_version;
    guint16 minor_version;
};

/**
 * qmi_ctl_version_info_get_service:
 * @info: a #QmiCtlVersionInfo.
 *
 * Get the QMI service being reported.
 *
 * Returns: A #QmiService.
 */
QmiService
qmi_ctl_version_info_get_service (QmiCtlVersionInfo *info)
{
    g_return_val_if_fail (info != NULL, QMI_SERVICE_UNKNOWN);

    return info->service;
}

/**
 * qmi_ctl_version_info_get_major_version:
 * @info: a #QmiCtlVersionInfo.
 *
 * Get the major version of the QMI service being reported.
 *
 * Returns: the major version.
 */
guint16
qmi_ctl_version_info_get_major_version (QmiCtlVersionInfo *info)
{
    g_return_val_if_fail (info != NULL, 0);

    return info->major_version;
}

/**
 * qmi_ctl_version_info_get_minor_version:
 * @info: a #QmiCtlVersionInfo.
 *
 * Get the minor version of the QMI service being reported.
 *
 * Returns: the minor version.
 */
guint16
qmi_ctl_version_info_get_minor_version (QmiCtlVersionInfo *info)
{
    g_return_val_if_fail (info != NULL, 0);

    return info->minor_version;
}

/**
 * qmi_ctl_version_info_ref:
 * @info: a #QmiCtlVersionInfo.
 *
 * Atomically increments the reference count of @info by one.
 *
 * Returns: the new reference to @info.
 */
QmiCtlVersionInfo *
qmi_ctl_version_info_ref (QmiCtlVersionInfo *info)
{
    g_return_val_if_fail (info != NULL, NULL);

    g_atomic_int_inc (&info->ref_count);
    return info;
}

/**
 * qmi_ctl_version_info_unref:
 * @info: a #QmiCtlVersionInfo.
 *
 * Atomically decrements the reference count of array by one.
 * If the reference count drops to 0, @info is completely disposed.
 */
void
qmi_ctl_version_info_unref (QmiCtlVersionInfo *info)
{
    g_return_if_fail (info != NULL);

    if (g_atomic_int_dec_and_test (&info->ref_count)) {
        g_slice_free (QmiCtlVersionInfo, info);
    }
}

QmiMessage *
qmi_message_ctl_version_info_new (guint8 transaction_id)
{
    return qmi_message_new (QMI_SERVICE_CTL,
                            0,
                            transaction_id,
                            QMI_CTL_MESSAGE_GET_VERSION_INFO);
}

struct qmi_ctl_version_info_list_service {
	guint8 service_type;
	guint16 major_version;
	guint16 minor_version;
} __attribute__((__packed__));

struct qmi_tlv_ctl_version_info_list {
	guint8 count;
	struct qmi_ctl_version_info_list_service services[0];
}__attribute__((__packed__));

GPtrArray *
qmi_message_ctl_version_info_reply_parse (QmiMessage *self,
                                          GError **error)
{
    struct qmi_tlv_ctl_version_info_list *service_list;
    struct qmi_ctl_version_info_list_service *svc;
    guint8 svcbuf[100];
    guint16 svcbuflen = 100;
    GPtrArray *result;
    guint i;

    g_assert (qmi_message_get_message_id (self) == QMI_CTL_MESSAGE_GET_VERSION_INFO);

    if (!qmi_message_tlv_get_varlen (self,
                                     0x01,
                                     &svcbuflen,
                                     svcbuf,
                                     error)) {
        g_prefix_error (error, "Couldn't get services TLV: ");
        return NULL;
    }

    service_list = (struct qmi_tlv_ctl_version_info_list *) svcbuf;
    if (svcbuflen < (service_list->count * sizeof (struct qmi_ctl_version_info_list_service))) {
        g_set_error (error,
                     QMI_CORE_ERROR,
                     QMI_CORE_ERROR_FAILED,
                     "Couldn't read the whole services list (%u < %u)",
                     svcbuflen,
                     (service_list->count * sizeof (struct qmi_ctl_version_info_list_service)));
        return NULL;
    }

    result = g_ptr_array_sized_new (service_list->count);
    g_ptr_array_set_free_func (result, (GDestroyNotify)qmi_ctl_version_info_unref);

    for (i = 0, svc = &(service_list->services[0]);
         i < service_list->count;
         i++, svc++) {
        QmiCtlVersionInfo *info;

        info = g_slice_new (QmiCtlVersionInfo);
        info->service = (QmiService)svc->service_type;
        info->major_version = le16toh (svc->major_version);
        info->minor_version = le16toh (svc->minor_version);

        g_ptr_array_add (result, info);
    }

    return result;
}

/*****************************************************************************/
/* Allocate CID */

QmiMessage *
qmi_message_ctl_allocate_cid_new (guint8 transaction_id,
                                  QmiService service)
{
    QmiMessage *message;
    guint8 service_id;
    GError *error = NULL;

    g_assert (service != QMI_SERVICE_UNKNOWN);
    service_id = service;

    message = qmi_message_new (QMI_SERVICE_CTL,
                               0,
                               transaction_id,
                               QMI_CTL_MESSAGE_ALLOCATE_CLIENT_ID);

    qmi_message_tlv_add (message,
                         (guint8)0x01,
                         sizeof (service_id),
                         &service_id,
                         &error);
    g_assert_no_error (error);

    return message;
}

struct qmi_ctl_cid {
	guint8 service_type;
    guint8 cid;
} __attribute__((__packed__));

gboolean
qmi_message_ctl_allocate_cid_reply_parse (QmiMessage *self,
                                          guint8 *cid,
                                          QmiService *service,
                                          GError **error)
{
    struct qmi_ctl_cid id;

    g_assert (qmi_message_get_message_id (self) == QMI_CTL_MESSAGE_ALLOCATE_CLIENT_ID);

    if (!qmi_message_tlv_get (self, 0x01, sizeof (id), &id, error)) {
        g_prefix_error (error, "Couldn't get TLV: ");
        return FALSE;
    }

    if (cid)
        *cid = id.cid;
    if (service)
        *service = (QmiService)id.service_type;

    return TRUE;
}

/*****************************************************************************/
/* Release CID */

QmiMessage *
qmi_message_ctl_release_cid_new (guint8 transaction_id,
                                 QmiService service,
                                 guint8 cid)
{
    QmiMessage *message;
    GError *error = NULL;
    struct qmi_ctl_cid id;

    g_assert (service != QMI_SERVICE_UNKNOWN);
    id.service_type = (guint8)service;
    id.cid = cid;

    message = qmi_message_new (QMI_SERVICE_CTL,
                               0,
                               transaction_id,
                               QMI_CTL_MESSAGE_RELEASE_CLIENT_ID);

    qmi_message_tlv_add (message,
                         (guint8)0x01,
                         sizeof (id),
                         &id,
                         &error);
    g_assert_no_error (error);

    return message;
}

gboolean
qmi_message_ctl_release_cid_reply_parse (QmiMessage *self,
                                         guint8 *cid,
                                         QmiService *service,
                                         GError **error)
{
    struct qmi_ctl_cid id;

    g_assert (qmi_message_get_message_id (self) == QMI_CTL_MESSAGE_RELEASE_CLIENT_ID);

    if (!qmi_message_tlv_get (self, 0x01, sizeof (id), &id, error)) {
        g_prefix_error (error, "Couldn't get TLV: ");
        return FALSE;
    }

    if (cid)
        *cid = id.cid;
    if (service)
        *service = (QmiService)id.service_type;

    return TRUE;
}

/*****************************************************************************/
/* Sync */

QmiMessage *
qmi_message_ctl_sync_new (guint8 transaction_id)
{
    return qmi_message_new (QMI_SERVICE_CTL,
                            0,
                            transaction_id,
                            QMI_CTL_MESSAGE_SYNC);
}