Linux preempt-rt

Check our new training course

Real-Time Linux with PREEMPT_RT

Check our new training course
with Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2009-2010  Nokia Corporation and/or its subsidiary(-ies).
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>

#include <glib.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/gprs.h>
#include <ofono/gprs-context.h>
#include <gisi/client.h>
#include <gisi/iter.h>

#include "isimodem.h"
#include "isiutil.h"
#include "gpds.h"
#include "info.h"
#include "debug.h"

/* 27.007 Section 10.1.20 <stat> */
enum network_registration_status {
	GPRS_STAT_NOT_REGISTERED =	0,
	GPRS_STAT_REGISTERED =		1,
	GPRS_STAT_SEARCHING =		2,
	GPRS_STAT_DENIED =		3,
	GPRS_STAT_UNKNOWN =		4,
	GPRS_STAT_ROAMING =		5,
};

struct gprs_data {
	GIsiClient *client;
	GIsiClient *info_client;
};

static void configure_resp_cb(const GIsiMessage *msg, void *opaque)
{
	const uint8_t *data = g_isi_msg_data(msg);

	if (g_isi_msg_error(msg) < 0) {
		DBG("ISI message error: %d", g_isi_msg_error(msg));
		return;
	}

	if (g_isi_msg_id(msg) != GPDS_CONFIGURE_RESP)
		return;

	if (g_isi_msg_data_len(msg) < 1)
		return;

	if (data[0] != GPDS_OK)
		DBG("GPDS configure failed: %s", gpds_status_name(data[0]));
}

static void set_attach_mode(struct ofono_gprs *gprs, int attached)
{
	struct gprs_data *gd = ofono_gprs_get_data(gprs);

	const unsigned char msg[] = {
		GPDS_CONFIGURE_REQ,
		attached ? GPDS_ATTACH_MODE_AUTOMATIC : GPDS_ATTACH_MODE_MANUAL,
		GPDS_MT_ACT_MODE_REJECT,
		GPDS_CLASSC_MODE_DEFAULT,
		GPDS_AOL_CTX_DEFAULT,
		0x00,
		0x00
	};

	g_isi_client_send(gd->client, msg, sizeof(msg),
				configure_resp_cb, gprs, NULL);
}

static void detach_ind_cb(const GIsiMessage *msg, void *opaque)
{
	struct ofono_gprs *gprs = opaque;
	const uint8_t *data = g_isi_msg_data(msg);

	if (g_isi_msg_error(msg) < 0)
		return;

	if (g_isi_msg_id(msg) != GPDS_DETACH_IND)
		return;

	if (g_isi_msg_data_len(msg) < 2)
		return;

	DBG("detached: %s (0x%02"PRIx8")",
		gpds_isi_cause_name(data[0]), data[0]);

	set_attach_mode(gprs, FALSE);

	ofono_gprs_detached_notify(gprs);
}

static void suspend_notify(struct ofono_gprs *gprs, uint8_t suspend_status,
			uint8_t suspend_cause)
{
	int cause;

	DBG("transfer status: %s (0x%02"PRIx8") cause %s (0x%02"PRIx8")",
		gpds_transfer_status_name(suspend_status), suspend_status,
		gpds_transfer_cause_name(suspend_cause), suspend_cause);

	if (suspend_status == GPDS_TRANSFER_AVAIL) {
		ofono_gprs_resume_notify(gprs);
		return;
	}

	switch (suspend_cause) {
	case GPDS_TRANSFER_CAUSE_SUSPENDED_NO_COVERAGE:
		cause = GPRS_SUSPENDED_NO_COVERAGE;
		break;

	case GPDS_TRANSFER_CAUSE_SUSPENDED_CALL:
		cause = GPRS_SUSPENDED_CALL;
		break;

	case GPDS_TRANSFER_CAUSE_SUSPENDED_CALL_SMS:
	case GPDS_TRANSFER_CAUSE_SUSPENDED_RAU:
	case GPDS_TRANSFER_CAUSE_SUSPENDED_LU:
		cause = GPRS_SUSPENDED_SIGNALLING;
		break;

	default:
		return;
	}

	ofono_gprs_suspend_notify(gprs, cause);
}

static void transfer_status_ind_cb(const GIsiMessage *msg, void *opaque)
{
	struct ofono_gprs *gprs = opaque;
	const uint8_t *data = g_isi_msg_data(msg);

	if (g_isi_msg_error(msg) < 0)
		return;

	if (g_isi_msg_id(msg) != GPDS_TRANSFER_STATUS_IND)
		return;

	if (g_isi_msg_data_len(msg) < 2)
		return;

	suspend_notify(gprs, data[0], data[1]);
}

static void create_contexts(struct ofono_gprs *gprs, int count)
{
	struct gprs_data *gd = ofono_gprs_get_data(gprs);
	GIsiModem *modem = g_isi_client_modem(gd->client);
	struct ofono_modem *omodem = g_isi_modem_get_userdata(modem);
	struct ofono_gprs_context *gc;
	int i;

	for (i = 0; i < count; i++) {
		gc = ofono_gprs_context_create(omodem, 0, "isimodem", modem);
		if (gc == NULL)
			break;

		ofono_gprs_add_context(gprs, gc);
	}

	ofono_gprs_set_cid_range(gprs, 1, i);

	DBG("%d GPRS contexts created", count);
}

static void info_pp_read_resp_cb(const GIsiMessage *msg, void *opaque)
{
	struct ofono_gprs *gprs = opaque;
	uint8_t count = GPDS_MAX_CONTEXT_COUNT;
	GIsiSubBlockIter iter;

	if (g_isi_msg_error(msg) == -ESHUTDOWN)
		return;

	if (g_isi_msg_error(msg) < 0)
		goto out;

	if (g_isi_msg_id(msg) != INFO_PP_READ_RESP)
		goto out;

	for (g_isi_sb_iter_init(&iter, msg, 2); g_isi_sb_iter_is_valid(&iter);
			g_isi_sb_iter_next(&iter)) {

		switch (g_isi_sb_iter_get_id(&iter)) {
		case INFO_SB_PP: {
			guint16 fea;
			guint8 n;
			unsigned pp;

			if (!g_isi_sb_iter_get_byte(&iter, &n, 1))
				goto out;

			for (pp = 4; n--; pp += 2) {

				if (!g_isi_sb_iter_get_word(&iter, &fea, pp))
					goto out;

				if ((fea >> 8) != INFO_PP_MAX_PDP_CONTEXTS)
					goto out;

				count = fea & 0xff;
				break;
			}
			break;
		}

		default:
			break;
		}
	}

out:
	create_contexts(gprs, count);
}

static void gpds_reachable_cb(const GIsiMessage *msg, void *opaque)
{
	struct ofono_gprs *gprs = opaque;
	struct gprs_data *gd = ofono_gprs_get_data(gprs);
	GIsiModem *modem = g_isi_client_modem(gd->client);

	const unsigned char req[] = {
		INFO_PP_READ_REQ,
		0,				/* filler */
		1,				/* subblocks */
		INFO_SB_PP,
		8,				/* subblock length */
		0,
		1,				/* N */
		INFO_PP_MAX_PDP_CONTEXTS,	/* PP feature */
		0,				/* PP value */
		0,				/* filler */
		0				/* filler */
	};

	if (g_isi_msg_error(msg) < 0) {
		DBG("unable to bootstrap gprs driver");
		ofono_gprs_remove(gprs);
		return;
	}

	ISI_RESOURCE_DBG(msg);

	g_isi_client_ind_subscribe(gd->client, GPDS_DETACH_IND,
					detach_ind_cb, gprs);
	g_isi_client_ind_subscribe(gd->client, GPDS_TRANSFER_STATUS_IND,
					transfer_status_ind_cb, gprs);

	ofono_gprs_register(gprs);

	gd->info_client = g_isi_client_create(modem, PN_PHONE_INFO);
	if (gd->info_client == NULL) {
		create_contexts(gprs, GPDS_MAX_CONTEXT_COUNT);
		return;
	}

	g_isi_client_send(gd->info_client, req, sizeof(req),
				info_pp_read_resp_cb, gprs, NULL);
}

static int isi_gprs_probe(struct ofono_gprs *gprs,
				unsigned int vendor, void *user)
{
	GIsiModem *modem = user;
	struct gprs_data *gd = g_try_new0(struct gprs_data, 1);

	if (gd == NULL)
		return -ENOMEM;

	gd->client = g_isi_client_create(modem, PN_GPDS);
	if (gd->client == NULL) {
		g_free(gd);
		return -ENOMEM;
	}

	ofono_gprs_set_data(gprs, gd);

	g_isi_client_set_timeout(gd->client, GPDS_TIMEOUT);
	g_isi_client_verify(gd->client, gpds_reachable_cb, gprs, NULL);

	return 0;
}

static void isi_gprs_remove(struct ofono_gprs *gprs)
{
	struct gprs_data *gd = ofono_gprs_get_data(gprs);

	ofono_gprs_set_data(gprs, NULL);

	if (gd == NULL)
		return;

	g_isi_client_destroy(gd->client);
	g_isi_client_destroy(gd->info_client);
	g_free(gd);
}

static void attach_resp_cb(const GIsiMessage *msg, void *opaque)
{
	struct isi_cb_data *cbd = opaque;
	ofono_gprs_cb_t cb = cbd->cb;
	const uint8_t *data = g_isi_msg_data(msg);

	if (g_isi_msg_error(msg) < 0) {
		DBG("ISI message error: %d", g_isi_msg_error(msg));
		goto error;
	}

	if (g_isi_msg_id(msg) != GPDS_ATTACH_RESP)
		return;

	if (g_isi_msg_data_len(msg) < 2)
		goto error;

	if (data[0] != GPDS_OK) {
		DBG("attach failed: %s", gpds_status_name(data[0]));
		goto error;
	}

	set_attach_mode(cbd->user, TRUE);

	CALLBACK_WITH_SUCCESS(cb, cbd->data);
	return;

error:
	CALLBACK_WITH_FAILURE(cb, cbd->data);
}

static void detach_resp_cb(const GIsiMessage *msg, void *opaque)
{
	struct isi_cb_data *cbd = opaque;
	ofono_gprs_cb_t cb = cbd->cb;
	const uint8_t *data = g_isi_msg_data(msg);

	if (g_isi_msg_error(msg) < 0) {
		DBG("ISI client error: %d", g_isi_msg_error(msg));
		goto error;
	}

	if (g_isi_msg_id(msg) != GPDS_DETACH_RESP)
		return;

	if (g_isi_msg_data_len(msg) < 2)
		goto error;

	if (data[0] != GPDS_OK) {
		DBG("detach failed: %s", gpds_status_name(data[0]));
		goto error;
	}

	set_attach_mode(cbd->user, FALSE);

	CALLBACK_WITH_SUCCESS(cb, cbd->data);
	return;

error:
	CALLBACK_WITH_FAILURE(cb, cbd->data);
}

static void isi_gprs_set_attached(struct ofono_gprs *gprs, int attached,
					ofono_gprs_cb_t cb, void *data)
{
	struct gprs_data *gd = ofono_gprs_get_data(gprs);
	struct isi_cb_data *cbd = isi_cb_data_new(gprs, cb, data);

	if (cbd == NULL || gd == NULL)
		goto error;

	if (attached) {
		const unsigned char msg[] = {
			GPDS_ATTACH_REQ,
			GPDS_FOLLOW_OFF
		};

		if (g_isi_client_send_with_timeout(gd->client,
				msg, sizeof(msg),
				GPDS_ATTACH_TIMEOUT, attach_resp_cb,
				cbd, g_free))
			return;
	} else {
		const unsigned char msg[] = {
			GPDS_DETACH_REQ,
			0x00, /* filler */
			0x00  /* sub-blocks */
		};

		if (g_isi_client_send_with_timeout(gd->client,
				msg, sizeof(msg),
				GPDS_DETACH_TIMEOUT, detach_resp_cb,
				cbd, g_free))
			return;
	}

error:
	CALLBACK_WITH_FAILURE(cb, data);
	g_free(cbd);
}

static void status_resp_cb(const GIsiMessage *msg, void *opaque)
{
	struct isi_cb_data *cbd = opaque;
	ofono_gprs_status_cb_t cb = cbd->cb;
	struct ofono_gprs *gprs = cbd->data;
	int status;
	const uint8_t *data = g_isi_msg_data(msg);

	if (g_isi_msg_error(msg) < 0) {
		DBG("ISI message error: %d", g_isi_msg_error(msg));
		goto error;
	}

	if (g_isi_msg_id(msg) != GPDS_STATUS_RESP)
		return;

	if (g_isi_msg_data_len(msg) < 12)
		goto error;

	/* FIXME: the core still expects reg status, and not a boolean
	 * attached status here.*/
	switch (data[0]) {
	case GPDS_ATTACHED:
		status = GPRS_STAT_REGISTERED;
		break;
	case GPDS_DETACHED:
		status = GPRS_STAT_NOT_REGISTERED;
		break;
	default:
		status = GPRS_STAT_UNKNOWN;
	}

	suspend_notify(gprs, data[10], data[11]);

	CALLBACK_WITH_SUCCESS(cb, status, cbd->data);
	return;

error:
	CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
}

static void isi_gprs_attached_status(struct ofono_gprs *gprs,
						ofono_gprs_status_cb_t cb,
						void *data)
{
	struct gprs_data *gd = ofono_gprs_get_data(gprs);
	struct isi_cb_data *cbd = isi_cb_data_new(NULL, cb, data);

	const unsigned char msg[] = {
		GPDS_STATUS_REQ,
	};

	if (cbd == NULL || gd == NULL)
		goto error;

	if (g_isi_client_send(gd->client, msg, sizeof(msg),
				status_resp_cb, cbd, g_free))
		return;

error:
	CALLBACK_WITH_FAILURE(cb, -1, data);
	g_free(cbd);
}

static struct ofono_gprs_driver driver = {
	.name			= "isimodem",
	.probe			= isi_gprs_probe,
	.remove			= isi_gprs_remove,
	.set_attached		= isi_gprs_set_attached,
	.attached_status	= isi_gprs_attached_status,
};

void isi_gprs_init(void)
{
	ofono_gprs_driver_register(&driver);
}

void isi_gprs_exit(void)
{
	ofono_gprs_driver_unregister(&driver);
}