summaryrefslogtreecommitdiff
path: root/drivers/ubloxmodem/network-registration.c
blob: 25f239a606c5186584fa166f6248ed304d3bd6d4 (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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
 *  Copyright (C) 2010  ST-Ericsson AB.
 *  Copyright (C) 2019  Norrbonn AB
 *
 *  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

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#include <glib.h>

#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/netreg.h>

#include "gatchat.h"
#include "gatresult.h"

#include "common.h"
#include "ubloxmodem.h"
#include "drivers/atmodem/vendor.h"

#include "drivers/atmodem/network-registration.h"

static const char *none_prefix[] = { NULL };
static const char *cmer_prefix[] = { "+CMER:", NULL };
static const char *ureg_prefix[] = { "+UREG:", NULL };
static const char *creg_prefix[] = { "+CREG:", NULL };

struct netreg_data {
	struct at_netreg_data at_data;

	const struct ublox_model *model;
	bool updating_status : 1;
};

struct tech_query {
	int status;
	int lac;
	int ci;
	int tech;
	struct ofono_netreg *netreg;
};

static void ciev_notify(GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
	int strength, ind;
	GAtResultIter iter;

	g_at_result_iter_init(&iter, result);

	if (!g_at_result_iter_next(&iter, "+CIEV:"))
		return;

	if (!g_at_result_iter_next_number(&iter, &ind))
		return;

	if (ind != nd->signal_index)
		return;

	if (!g_at_result_iter_next_number(&iter, &strength))
		return;

	if (strength == nd->signal_invalid)
		strength = -1;
	else
		strength = (strength * 100) / (nd->signal_max - nd->signal_min);

	ofono_netreg_strength_notify(netreg, strength);
}

static gboolean notify_time(gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);

	nd->nitz_timeout = 0;

	ofono_netreg_time_notify(netreg, &nd->time);

	return FALSE;
}

static void ctzdst_notify(GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
	int dst;
	GAtResultIter iter;

	g_at_result_iter_init(&iter, result);

	if (!g_at_result_iter_next(&iter, "+CTZDST:"))
		return;

	if (!g_at_result_iter_next_number(&iter, &dst))
		return;

	DBG("dst %d", dst);

	nd->time.dst = dst;

	if (nd->nitz_timeout > 0) {
		g_source_remove(nd->nitz_timeout);
		nd->nitz_timeout = 0;
	}

	ofono_netreg_time_notify(netreg, &nd->time);
}

static void ctzv_notify(GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
	int year, mon, mday, hour, min, sec;
	const char *tz, *time;
	GAtResultIter iter;

	g_at_result_iter_init(&iter, result);

	if (!g_at_result_iter_next(&iter, "+CTZV:"))
		return;

	if (!g_at_result_iter_next_unquoted_string(&iter, &tz))
		return;

	if (!g_at_result_iter_next_string(&iter, &time))
		return;

	DBG("tz %s time %s", tz, time);

	if (sscanf(time, "%u/%u/%u,%u:%u:%u", &year, &mon, &mday,
						&hour, &min, &sec) != 6)
		return;

	nd->time.sec = sec;
	nd->time.min = min;
	nd->time.hour = hour;
	nd->time.mday = mday;
	nd->time.mon = mon;
	nd->time.year = 2000 + year;

	nd->time.utcoff = atoi(tz) * 15 * 60;

	/* Delay notification in case there's a DST update coming */
	if (nd->nitz_timeout > 0)
		g_source_remove(nd->nitz_timeout);

	nd->nitz_timeout = g_timeout_add_seconds(1, notify_time, user_data);
}

static void ctze_notify(GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
	int year, mon, mday, hour, min, sec;
	int dst;
	const char *tz, *time;
	GAtResultIter iter;

	g_at_result_iter_init(&iter, result);

	if (!g_at_result_iter_next(&iter, "+CTZE:"))
		return;

	if (!g_at_result_iter_next_unquoted_string(&iter, &tz))
		return;

	if (!g_at_result_iter_next_number(&iter, &dst))
		return;

	if (!g_at_result_iter_next_string(&iter, &time))
		return;

	DBG("tz %s dst %d time %s", tz, dst, time);

	if (sscanf(time, "%u/%u/%u,%u:%u:%u", &year, &mon, &mday,
						&hour, &min, &sec) != 6)
		return;

	nd->time.sec = sec;
	nd->time.min = min;
	nd->time.hour = hour;
	nd->time.mday = mday;
	nd->time.mon = mon;
	nd->time.year = 2000 + year;

	nd->time.utcoff = atoi(tz) * 15 * 60;
	nd->time.dst = dst;

	ofono_netreg_time_notify(netreg, &nd->time);
}

static int ublox_ureg_state_to_tech(int state)
{
	switch (state) {
	case 1:
		return ACCESS_TECHNOLOGY_GSM;
	case 2:
		return ACCESS_TECHNOLOGY_GSM_EGPRS;
	case 3:
		return ACCESS_TECHNOLOGY_UTRAN;
	case 4:
		return ACCESS_TECHNOLOGY_UTRAN_HSDPA;
	case 5:
		return ACCESS_TECHNOLOGY_UTRAN_HSUPA;
	case 6:
		return ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
	case 7:
		return ACCESS_TECHNOLOGY_EUTRAN;
	case 8:
		return ACCESS_TECHNOLOGY_GSM;
	case 9:
		return ACCESS_TECHNOLOGY_GSM_EGPRS;
	default:
		/* Not registered for PS (0) or something unknown (>9)... */
		return -1;
	}
}

static gboolean is_registered(int status)
{
	return status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
		status == NETWORK_REGISTRATION_STATUS_ROAMING;
}

static void ublox_creg_cb(gboolean ok, GAtResult *result,
				gpointer user_data)
{
	struct tech_query *tq = user_data;
	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
	int status;
	int lac;
	int ci;
	int tech;

	nd->updating_status = false;

	if (!ok)
		return;

	if (at_util_parse_reg(result, "+CREG:", NULL, &status,
				&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
		return;

	/* The query provided a tech, use that */
	if (is_registered(status) && tq->tech != -1)
		tech = tq->tech;

	ofono_netreg_status_notify(tq->netreg, status, lac, ci, tech);
}

static void ublox_ureg_cb(gboolean ok, GAtResult *result,
							gpointer user_data)
{
	struct tech_query *tq = user_data;
	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
	GAtResultIter iter;
	gint enabled, state;
	int tech = tq->tech;

	nd->updating_status = false;

	if (!ok)
		goto error;

	g_at_result_iter_init(&iter, result);

	if (!g_at_result_iter_next(&iter, "+UREG:"))
		return;

	if (!g_at_result_iter_next_number(&iter, &enabled))
		return;

	if (!g_at_result_iter_next_number(&iter, &state))
		return;

	tech = ublox_ureg_state_to_tech(state);
	if (tech < 0)
		/* No valid UREG status, we have to trust CREG... */
		tech = tq->tech;

error:
	ofono_netreg_status_notify(tq->netreg,
			tq->status, tq->lac, tq->ci, tech);
}

static void ureg_notify(GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct tech_query *tq;
	GAtResultIter iter;
	int state;

	if (nd->updating_status)
		return;

	g_at_result_iter_init(&iter, result);

	if (!g_at_result_iter_next(&iter, "+UREG:"))
		return;

	if (!g_at_result_iter_next_number(&iter, &state))
		return;

	tq = g_new0(struct tech_query, 1);

	tq->tech = ublox_ureg_state_to_tech(state);
	tq->netreg = netreg;

	if (g_at_chat_send(nd->at_data.chat, "AT+CREG?", creg_prefix,
			ublox_creg_cb, tq, g_free) > 0) {
		nd->updating_status = true;
		return;
	}

	g_free(tq);
}

static void creg_notify(GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct tech_query *tq;
	int status;
	int lac;
	int ci;
	int tech;

	if (nd->updating_status)
		return;

	if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
			&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
		return;

	if (!is_registered(status))
		goto notify;

	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
		tq = g_new0(struct tech_query, 1);

		tq->status = status;
		tq->lac = lac;
		tq->ci = ci;
		tq->tech = tech;
		tq->netreg = netreg;

		if (g_at_chat_send(nd->at_data.chat, "AT+UREG?", ureg_prefix,
				ublox_ureg_cb, tq, g_free) > 0) {
			nd->updating_status = true;
			return;
		}

		g_free(tq);
	}

	if (tech == -1)
		tech = nd->at_data.tech;

notify:
	ofono_netreg_status_notify(netreg, status, lac, ci, tech);
}

static void at_cmer_not_supported(struct ofono_netreg *netreg)
{
	ofono_error("+CMER not supported by this modem.  If this is an error"
			" please submit patches to support this hardware");

	ofono_netreg_remove(netreg);
}

static void ublox_finish_registration(struct ofono_netreg *netreg)
{
	struct netreg_data *nd = ofono_netreg_get_data(netreg);

	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model))
		g_at_chat_register(nd->at_data.chat, "+UREG:",
					ureg_notify, FALSE, netreg, NULL);

	g_at_chat_register(nd->at_data.chat, "+CIEV:",
			ciev_notify, FALSE, netreg, NULL);

	g_at_chat_register(nd->at_data.chat, "+CREG:",
				creg_notify, FALSE, netreg, NULL);

	ofono_netreg_register(netreg);
}

static void ublox_ureg_set_cb(gboolean ok,
				GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;

	if (!ok) {
		ofono_error("Unable to initialize Network Registration");
		ofono_netreg_remove(netreg);
		return;
	}

	ublox_finish_registration(netreg);
}

static void ublox_cmer_set_cb(gboolean ok,
				GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);

	if (!ok) {
		at_cmer_not_supported(netreg);
		return;
	}

	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
		g_at_chat_send(nd->at_data.chat, "AT+UREG=1", none_prefix,
			ublox_ureg_set_cb, netreg, NULL);

		return;
	}

	ublox_finish_registration(netreg);
}

static void ublox_creg_set_cb(gboolean ok,
				GAtResult *result, gpointer user_data)
{
	struct ofono_netreg *netreg = user_data;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);

	if (!ok) {
		ofono_error("Unable to initialize Network Registration");
		ofono_netreg_remove(netreg);
		return;
	}

	if (ublox_is_toby_l4(nd->model))
		/* FIXME */
		ofono_error("TOBY L4 requires polling of ECSQ");

	/* Register for network time update reports */
	if (ublox_is_toby_l2(nd->model)) {
		/* TOBY L2 does not support CTZDST */
		g_at_chat_register(nd->at_data.chat, "+CTZE:", ctze_notify,
						FALSE, netreg, NULL);
		g_at_chat_send(nd->at_data.chat, "AT+CTZR=2", none_prefix,
						NULL, NULL, NULL);
	} else {
		g_at_chat_register(nd->at_data.chat, "+CTZV:", ctzv_notify,
						FALSE, netreg, NULL);
		g_at_chat_register(nd->at_data.chat, "+CTZDST:", ctzdst_notify,
						FALSE, netreg, NULL);
		g_at_chat_send(nd->at_data.chat, "AT+CTZR=1", none_prefix,
						NULL, NULL, NULL);
	}

	/* AT+CMER NOTES:
	 * - For all u-blox models, mode 3 is equivalent to mode 1;
	 * since some models do not support setting modes 2 nor 3
	 * (see UBX-13002752), we prefer mode 1 for all models.
	 * - The TOBY L4 does not support ind=2
	 */
	g_at_chat_send(nd->at_data.chat, "AT+CMER=1,0,0,1", cmer_prefix,
			ublox_cmer_set_cb, netreg, NULL);
}

/*
 * uBlox netreg atom probe.
 * - takes uBlox model ID parameter instead of AT vendor ID
 */
static int ublox_netreg_probe(struct ofono_netreg *netreg,
				unsigned int model_id,
				void *data)
{
	GAtChat *chat = data;
	struct netreg_data *nd;

	nd = g_new0(struct netreg_data, 1);

	nd->model = ublox_model_from_id(model_id);

	/* There should be no uBlox-specific quirks in the 'generic'
	 * AT driver
	 */
	nd->at_data.vendor = OFONO_VENDOR_GENERIC;

	nd->at_data.chat = g_at_chat_clone(chat);
	nd->at_data.tech = -1;
	nd->at_data.time.sec = -1;
	nd->at_data.time.min = -1;
	nd->at_data.time.hour = -1;
	nd->at_data.time.mday = -1;
	nd->at_data.time.mon = -1;
	nd->at_data.time.year = -1;
	nd->at_data.time.dst = 0;
	nd->at_data.time.utcoff = 0;
	ofono_netreg_set_data(netreg, nd);

	/* All uBlox devices support n=2 so no need to query this */
	g_at_chat_send(nd->at_data.chat, "AT+CREG=2", none_prefix,
			ublox_creg_set_cb, netreg, NULL);

	return 0;
}

static const struct ofono_netreg_driver driver = {
	.name				= "ubloxmodem",
	.probe				= ublox_netreg_probe,
	.remove				= at_netreg_remove,
	.registration_status		= at_registration_status,
	.current_operator		= at_current_operator,
	.list_operators			= at_list_operators,
	.register_auto			= at_register_auto,
	.register_manual		= at_register_manual,
	.strength			= at_signal_strength,
};

void ublox_netreg_init(void)
{
	ofono_netreg_driver_register(&driver);
}

void ublox_netreg_exit(void)
{
	ofono_netreg_driver_unregister(&driver);
}