| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 3 |
/* |
| 4 |
* libmbim-glib -- GLib/GIO based library to control MBIM devices |
| 5 |
* |
| 6 |
* Copyright (C) 2014 Aleksander Morgado <[email protected]> |
| 7 |
* Copyright (C) 2014 Smith Micro Software, Inc. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
#include "mbim-proxy-helpers.h" |
| 12 |
#include "mbim-message-private.h" |
| 13 |
#include "mbim-message.h" |
| 14 |
#include "mbim-error-types.h" |
| 15 |
#include "mbim-cid.h" |
| 16 |
#include "mbim-uuid.h" |
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
|
| 20 |
static gboolean |
| 21 |
cmp_event_entry_contents (const MbimEventEntry *in, |
| 22 |
const MbimEventEntry *out) |
| 23 |
{ |
| 24 |
guint i, o; |
| 25 |
|
| 26 |
g_assert (mbim_uuid_cmp (&(in->device_service_id), &(out->device_service_id))); |
| 27 |
|
| 28 |
/* First, compare number of cids in the array */ |
| 29 |
if (in->cids_count != out->cids_count) |
| 30 |
return FALSE; |
| 31 |
|
| 32 |
if (in->cids_count == 0) |
| 33 |
g_assert (in->cids == NULL); |
| 34 |
if (out->cids_count == 0) |
| 35 |
g_assert (out->cids == NULL); |
| 36 |
|
| 37 |
for (i = 0; i < in->cids_count; i++) { |
| 38 |
for (o = 0; o < out->cids_count; o++) { |
| 39 |
if (in->cids[i] == out->cids[o]) |
| 40 |
break; |
| 41 |
} |
| 42 |
if (o == out->cids_count) |
| 43 |
return FALSE; |
| 44 |
} |
| 45 |
|
| 46 |
return TRUE; |
| 47 |
} |
| 48 |
|
| 49 |
gboolean |
| 50 |
_mbim_proxy_helper_service_subscribe_list_cmp (const MbimEventEntry * const *a, |
| 51 |
gsize a_size, |
| 52 |
const MbimEventEntry * const *b, |
| 53 |
gsize b_size) |
| 54 |
{ |
| 55 |
gsize i, o; |
| 56 |
|
| 57 |
/* First, compare number of entries a the array */ |
| 58 |
if (a_size != b_size) |
| 59 |
return FALSE; |
| 60 |
|
| 61 |
/* Now compare each service one by one */ |
| 62 |
for (i = 0; i < a_size; i++) { |
| 63 |
/* Look for this same service a the other array */ |
| 64 |
for (o = 0; o < b_size; o++) { |
| 65 |
/* When service found, compare contents */ |
| 66 |
if (mbim_uuid_cmp (&(a[i]->device_service_id), &(b[o]->device_service_id))) { |
| 67 |
if (!cmp_event_entry_contents (a[i], b[o])) |
| 68 |
return FALSE; |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
/* Service not found! */ |
| 73 |
if (!b[o]) |
| 74 |
return FALSE; |
| 75 |
} |
| 76 |
|
| 77 |
return TRUE; |
| 78 |
} |
| 79 |
|
| 80 |
/*****************************************************************************/ |
| 81 |
|
| 82 |
void |
| 83 |
_mbim_proxy_helper_service_subscribe_list_debug (const MbimEventEntry * const *list, |
| 84 |
gsize list_size) |
| 85 |
{ |
| 86 |
gsize i; |
| 87 |
|
| 88 |
for (i = 0; i < list_size; i++) { |
| 89 |
const MbimEventEntry *entry = list[i]; |
| 90 |
MbimService service; |
| 91 |
gchar *str; |
| 92 |
|
| 93 |
service = mbim_uuid_to_service (&entry->device_service_id); |
| 94 |
str = mbim_uuid_get_printable (&entry->device_service_id); |
| 95 |
g_debug ("[service %u] %s (%s)", |
| 96 |
(guint)i, str, mbim_service_lookup_name (service)); |
| 97 |
g_free (str); |
| 98 |
|
| 99 |
if (entry->cids_count == 0) |
| 100 |
g_debug ("[service %u] No CIDs explicitly enabled", (guint)i); |
| 101 |
else { |
| 102 |
guint j; |
| 103 |
|
| 104 |
g_debug ("[service %u] %u CIDs enabled", (guint)i, entry->cids_count); |
| 105 |
for (j = 0; j < entry->cids_count; j++) { |
| 106 |
const gchar *cid_str; |
| 107 |
|
| 108 |
cid_str = mbim_cid_get_printable (service, entry->cids[j]); |
| 109 |
g_debug ("[service %u] [cid %u] %u (%s)", |
| 110 |
(guint)i, j, entry->cids[j], cid_str ? cid_str : "unknown"); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/*****************************************************************************/ |
| 117 |
|
| 118 |
MbimEventEntry ** |
| 119 |
_mbim_proxy_helper_service_subscribe_request_parse (MbimMessage *message, |
| 120 |
gsize *out_size, |
| 121 |
GError **error) |
| 122 |
{ |
| 123 |
MbimEventEntry **array = NULL; |
| 124 |
guint32 i; |
| 125 |
guint32 element_count; |
| 126 |
guint32 offset = 0; |
| 127 |
guint32 array_offset; |
| 128 |
GError *inner_error = NULL; |
| 129 |
|
| 130 |
g_assert (message != NULL); |
| 131 |
g_assert (out_size != NULL); |
| 132 |
|
| 133 |
if (mbim_message_get_message_type (message) != MBIM_MESSAGE_TYPE_COMMAND) { |
| 134 |
g_set_error (error, |
| 135 |
MBIM_CORE_ERROR, |
| 136 |
MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 137 |
"Message is not a request"); |
| 138 |
return FALSE; |
| 139 |
} |
| 140 |
|
| 141 |
if (!mbim_message_command_get_raw_information_buffer (message, NULL)) { |
| 142 |
g_set_error (error, |
| 143 |
MBIM_CORE_ERROR, |
| 144 |
MBIM_CORE_ERROR_INVALID_MESSAGE, |
| 145 |
"Message does not have information buffer"); |
| 146 |
return FALSE; |
| 147 |
} |
| 148 |
|
| 149 |
if (!_mbim_message_read_guint32 (message, offset, &element_count, error)) |
| 150 |
return NULL; |
| 151 |
|
| 152 |
if (element_count) { |
| 153 |
array = g_new0 (MbimEventEntry *, element_count + 1); |
| 154 |
|
| 155 |
offset += 4; |
| 156 |
for (i = 0; i < element_count; i++) { |
| 157 |
MbimUuid uuid; |
| 158 |
|
| 159 |
if (!_mbim_message_read_guint32 (message, offset, &array_offset, &inner_error)) |
| 160 |
break; |
| 161 |
if (!_mbim_message_read_uuid (message, array_offset, NULL, &uuid, &inner_error)) |
| 162 |
break; |
| 163 |
|
| 164 |
array[i] = g_new0 (MbimEventEntry, 1); |
| 165 |
memcpy (&(array[i]->device_service_id), &uuid, 16); |
| 166 |
array_offset += 16; |
| 167 |
|
| 168 |
if (!_mbim_message_read_guint32 (message, array_offset, &(array[i])->cids_count, &inner_error)) |
| 169 |
break; |
| 170 |
array_offset += 4; |
| 171 |
|
| 172 |
if (array[i]->cids_count && !_mbim_message_read_guint32_array (message, array[i]->cids_count, array_offset, &array[i]->cids, &inner_error)) |
| 173 |
break; |
| 174 |
offset += 8; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if (inner_error) { |
| 179 |
mbim_event_entry_array_free (array); |
| 180 |
g_propagate_error (error, inner_error); |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
|
| 184 |
*out_size = element_count; |
| 185 |
return array; |
| 186 |
} |
| 187 |
|
| 188 |
/*****************************************************************************/ |
| 189 |
|
| 190 |
MbimEventEntry ** |
| 191 |
_mbim_proxy_helper_service_subscribe_list_merge (MbimEventEntry **in, |
| 192 |
gsize in_size, |
| 193 |
MbimEventEntry **merge, |
| 194 |
gsize merge_size, |
| 195 |
gsize *out_size) |
| 196 |
{ |
| 197 |
gsize m; |
| 198 |
|
| 199 |
g_assert (out_size != NULL); |
| 200 |
|
| 201 |
*out_size = in_size; |
| 202 |
|
| 203 |
if (!merge || !merge_size) |
| 204 |
return in; |
| 205 |
|
| 206 |
for (m = 0; m < merge_size; m++) { |
| 207 |
MbimEventEntry *entry = NULL; |
| 208 |
MbimService id; |
| 209 |
|
| 210 |
/* ignore all merge additions for standard services */ |
| 211 |
id = mbim_uuid_to_service (&merge[m]->device_service_id); |
| 212 |
if (id >= MBIM_SERVICE_BASIC_CONNECT && id <= MBIM_SERVICE_DSS) |
| 213 |
continue; |
| 214 |
|
| 215 |
/* look for matching uuid */ |
| 216 |
if (in && in_size) { |
| 217 |
gsize i; |
| 218 |
|
| 219 |
for (i = 0; i < in_size; i++) { |
| 220 |
if (mbim_uuid_cmp (&merge[m]->device_service_id, &in[i]->device_service_id)) { |
| 221 |
entry = in[i]; |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/* matching uuid not found in merge array, add it */ |
| 228 |
if (!entry) { |
| 229 |
gsize o; |
| 230 |
|
| 231 |
/* Index of the new element to add... */ |
| 232 |
o = *out_size; |
| 233 |
|
| 234 |
/* Increase number of events in the output array */ |
| 235 |
(*out_size)++; |
| 236 |
in = g_realloc (in, sizeof (MbimEventEntry *) * (*out_size + 1)); |
| 237 |
in[o] = g_memdup (merge[m], sizeof (MbimEventEntry)); |
| 238 |
if (merge[m]->cids_count) |
| 239 |
in[o]->cids = g_memdup (merge[m]->cids, sizeof (guint32) * merge[m]->cids_count); |
| 240 |
else |
| 241 |
in[o]->cids = NULL; |
| 242 |
in[*out_size] = NULL; |
| 243 |
} else { |
| 244 |
gsize cm, co; |
| 245 |
|
| 246 |
/* matching uuid found, add cids */ |
| 247 |
if (!entry->cids_count) |
| 248 |
/* all cids already enabled for uuid */ |
| 249 |
continue; |
| 250 |
|
| 251 |
/* If we're adding all enabled cids, directly apply that */ |
| 252 |
if (merge[m]->cids_count == 0) { |
| 253 |
g_free (entry->cids); |
| 254 |
entry->cids = NULL; |
| 255 |
entry->cids_count = 0; |
| 256 |
} |
| 257 |
|
| 258 |
for (cm = 0; cm < merge[m]->cids_count; cm++) { |
| 259 |
for (co = 0; co < entry->cids_count; co++) { |
| 260 |
if (merge[m]->cids[cm] == entry->cids[co]) { |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (co == entry->cids_count) { |
| 266 |
/* cid not found in merge array, add it */ |
| 267 |
entry->cids = g_realloc (entry->cids, sizeof (guint32) * (++entry->cids_count)); |
| 268 |
entry->cids[co] = merge[m]->cids[cm]; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return in; |
| 275 |
} |
| 276 |
|
| 277 |
/*****************************************************************************/ |
| 278 |
|
| 279 |
MbimEventEntry ** |
| 280 |
_mbim_proxy_helper_service_subscribe_list_dup (MbimEventEntry **in, |
| 281 |
gsize in_size, |
| 282 |
gsize *out_size) |
| 283 |
{ |
| 284 |
MbimEventEntry **out; |
| 285 |
guint i; |
| 286 |
|
| 287 |
g_assert (out_size != NULL); |
| 288 |
|
| 289 |
out = g_new0 (MbimEventEntry *, in_size + 1); |
| 290 |
for (i = 0; i < in_size; i++) { |
| 291 |
MbimEventEntry *entry_in; |
| 292 |
MbimEventEntry *entry_out; |
| 293 |
|
| 294 |
entry_in = in[i]; |
| 295 |
entry_out = g_new (MbimEventEntry, 1); |
| 296 |
memcpy (&entry_out->device_service_id, &entry_in->device_service_id, sizeof (MbimUuid)); |
| 297 |
entry_out->cids_count = entry_in->cids_count; |
| 298 |
entry_out->cids = g_new (guint32, entry_out->cids_count); |
| 299 |
memcpy (entry_out->cids, entry_in->cids, sizeof (guint32) * entry_out->cids_count); |
| 300 |
out[i] = entry_out; |
| 301 |
} |
| 302 |
|
| 303 |
*out_size = in_size; |
| 304 |
return out; |
| 305 |
} |
| 306 |
|
| 307 |
/*****************************************************************************/ |
| 308 |
|
| 309 |
MbimEventEntry ** |
| 310 |
_mbim_proxy_helper_service_subscribe_list_new_standard (gsize *out_size) |
| 311 |
{ |
| 312 |
MbimEventEntry **out; |
| 313 |
guint i = 0; |
| 314 |
MbimEventEntry *entry; |
| 315 |
|
| 316 |
g_assert (out_size != NULL); |
| 317 |
|
| 318 |
#define STANDARD_SERVICES_LIST_SIZE 5 |
| 319 |
out = g_new0 (MbimEventEntry *, STANDARD_SERVICES_LIST_SIZE + 1); |
| 320 |
|
| 321 |
/* Basic connect service */ |
| 322 |
{ |
| 323 |
static const guint32 notify_cids[] = { |
| 324 |
MBIM_CID_BASIC_CONNECT_SUBSCRIBER_READY_STATUS, |
| 325 |
MBIM_CID_BASIC_CONNECT_RADIO_STATE, |
| 326 |
MBIM_CID_BASIC_CONNECT_PREFERRED_PROVIDERS, |
| 327 |
MBIM_CID_BASIC_CONNECT_REGISTER_STATE, |
| 328 |
MBIM_CID_BASIC_CONNECT_PACKET_SERVICE, |
| 329 |
MBIM_CID_BASIC_CONNECT_SIGNAL_STATE, |
| 330 |
MBIM_CID_BASIC_CONNECT_CONNECT, |
| 331 |
MBIM_CID_BASIC_CONNECT_PROVISIONED_CONTEXTS, |
| 332 |
MBIM_CID_BASIC_CONNECT_IP_CONFIGURATION, |
| 333 |
MBIM_CID_BASIC_CONNECT_EMERGENCY_MODE, |
| 334 |
MBIM_CID_BASIC_CONNECT_MULTICARRIER_PROVIDERS, |
| 335 |
}; |
| 336 |
|
| 337 |
entry = g_new (MbimEventEntry, 1); |
| 338 |
memcpy (&entry->device_service_id, mbim_uuid_from_service (MBIM_SERVICE_BASIC_CONNECT), sizeof (MbimUuid)); |
| 339 |
entry->cids_count = G_N_ELEMENTS (notify_cids); |
| 340 |
entry->cids = g_memdup (notify_cids, sizeof (guint32) * entry->cids_count); |
| 341 |
out[i++] = entry; |
| 342 |
} |
| 343 |
|
| 344 |
/* SMS service */ |
| 345 |
{ |
| 346 |
static const guint32 notify_cids[] = { |
| 347 |
MBIM_CID_SMS_CONFIGURATION, |
| 348 |
MBIM_CID_SMS_READ, |
| 349 |
MBIM_CID_SMS_MESSAGE_STORE_STATUS, |
| 350 |
}; |
| 351 |
|
| 352 |
entry = g_new (MbimEventEntry, 1); |
| 353 |
memcpy (&entry->device_service_id, mbim_uuid_from_service (MBIM_SERVICE_SMS), sizeof (MbimUuid)); |
| 354 |
entry->cids_count = G_N_ELEMENTS (notify_cids); |
| 355 |
entry->cids = g_memdup (notify_cids, sizeof (guint32) * entry->cids_count); |
| 356 |
out[i++] = entry; |
| 357 |
} |
| 358 |
|
| 359 |
/* USSD service */ |
| 360 |
{ |
| 361 |
static const guint32 notify_cids[] = { |
| 362 |
MBIM_CID_USSD, |
| 363 |
}; |
| 364 |
|
| 365 |
entry = g_new (MbimEventEntry, 1); |
| 366 |
memcpy (&entry->device_service_id, mbim_uuid_from_service (MBIM_SERVICE_USSD), sizeof (MbimUuid)); |
| 367 |
entry->cids_count = G_N_ELEMENTS (notify_cids); |
| 368 |
entry->cids = g_memdup (notify_cids, sizeof (guint32) * entry->cids_count); |
| 369 |
out[i++] = entry; |
| 370 |
} |
| 371 |
|
| 372 |
/* Phonebook service */ |
| 373 |
{ |
| 374 |
static const guint32 notify_cids[] = { |
| 375 |
MBIM_CID_PHONEBOOK_CONFIGURATION, |
| 376 |
}; |
| 377 |
|
| 378 |
entry = g_new (MbimEventEntry, 1); |
| 379 |
memcpy (&entry->device_service_id, mbim_uuid_from_service (MBIM_SERVICE_PHONEBOOK), sizeof (MbimUuid)); |
| 380 |
entry->cids_count = G_N_ELEMENTS (notify_cids); |
| 381 |
entry->cids = g_memdup (notify_cids, sizeof (guint32) * entry->cids_count); |
| 382 |
out[i++] = entry; |
| 383 |
} |
| 384 |
|
| 385 |
/* STK service */ |
| 386 |
{ |
| 387 |
static const guint32 notify_cids[] = { |
| 388 |
MBIM_CID_STK_PAC, |
| 389 |
}; |
| 390 |
|
| 391 |
entry = g_new (MbimEventEntry, 1); |
| 392 |
memcpy (&entry->device_service_id, mbim_uuid_from_service (MBIM_SERVICE_STK), sizeof (MbimUuid)); |
| 393 |
entry->cids_count = G_N_ELEMENTS (notify_cids); |
| 394 |
entry->cids = g_memdup (notify_cids, sizeof (guint32) * entry->cids_count); |
| 395 |
out[i++] = entry; |
| 396 |
} |
| 397 |
|
| 398 |
g_assert_cmpuint (i, ==, STANDARD_SERVICES_LIST_SIZE); |
| 399 |
*out_size = i; |
| 400 |
return out; |
| 401 |
} |