Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and 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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/*
 *
 *  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 <glib.h>

#include <gisi/message.h>
#include <gisi/client.h>
#include <gisi/iter.h>

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

#include "isimodem.h"
#include "isiutil.h"
#include "network.h"
#include "debug.h"

struct reg_info {
	uint8_t status;
	uint8_t mode;
};

struct gsm_info {
	uint16_t lac;
	uint32_t ci;
	uint8_t egprs;
	uint8_t hsdpa;
	uint8_t hsupa;
};

struct rat_info {
	uint8_t rat;
	uint8_t compact;
};

struct network_time {
	uint8_t year;
	uint8_t mon;
	uint8_t mday;
	uint8_t hour;
	uint8_t min;
	uint8_t sec;
	uint8_t utc;
	uint8_t dst;
};

struct netreg_data {
	GIsiClient *client;
	struct reg_info reg;
	struct gsm_info gsm;
	struct rat_info rat;
};

static inline guint8 *mccmnc_to_bcd(const char *mcc, const char *mnc,
						guint8 *bcd)
{
	bcd[0] = (mcc[0] - '0') | (mcc[1] - '0') << 4;
	bcd[1] = (mcc[2] - '0');
	bcd[1] |= (mnc[2] == '\0' ? 0x0f : (mnc[2] - '0')) << 4;
	bcd[2] = (mnc[0] - '0') | (mnc[1] - '0') << 4;
	return bcd;
}

static inline int isi_status_to_at_status(struct reg_info *reg)
{
	switch (reg->status) {
	case NET_REG_STATUS_NOSERV:
	case NET_REG_STATUS_NOSERV_NOTSEARCHING:
	case NET_REG_STATUS_NOSERV_NOSIM:
	case NET_REG_STATUS_POWER_OFF:
	case NET_REG_STATUS_NSPS:
	case NET_REG_STATUS_NSPS_NO_COVERAGE:
		return 0;

	case NET_REG_STATUS_HOME:
		return 1;

	case NET_REG_STATUS_NOSERV_SEARCHING:
		return 2;

	case NET_REG_STATUS_NOSERV_SIM_REJECTED_BY_NW:
		return 3;

	case NET_REG_STATUS_ROAM_BLINK:
	case NET_REG_STATUS_ROAM:
		return 5;
	}
	return 4;
}

static inline int isi_to_at_tech(struct rat_info *rat, struct gsm_info *gsm)
{
	int tech = -1;

	if (rat == NULL || gsm == NULL)
		return -1;

	if (rat->rat == NET_GSM_RAT)
		tech = 0;

	if (rat->compact)
		tech = 1;

	if (rat->rat == NET_UMTS_RAT)
		tech = 2;

	if (gsm->egprs)
		tech = 3;

	if (gsm->hsdpa)
		tech = 4;

	if (gsm->hsupa)
		tech = 5;

	if (gsm->hsdpa && gsm->hsupa)
		tech = 6;

	return tech;
}

static gboolean check_response_status(const GIsiMessage *msg, uint8_t msgid)
{
	uint8_t cause;

	if (g_isi_msg_error(msg) < 0) {
		DBG("Error: %s", strerror(-g_isi_msg_error(msg)));
		return FALSE;
	}

	if (g_isi_msg_id(msg) != msgid) {
		DBG("Unexpected msg: %s",
			net_message_id_name(g_isi_msg_id(msg)));
		return FALSE;
	}

	if (!g_isi_msg_data_get_byte(msg, 0, &cause))
		return FALSE;

	if (cause != NET_CAUSE_OK) {
		DBG("Request failed: %s", net_isi_cause_name(cause));
		return FALSE;
	}
	return TRUE;
}

static gboolean parse_common_info(GIsiSubBlockIter *iter, struct reg_info *reg)
{
	return reg && g_isi_sb_iter_get_byte(iter, &reg->status, 2) &&
		g_isi_sb_iter_get_byte(iter, &reg->mode, 3);
}

static gboolean parse_gsm_info(GIsiSubBlockIter *iter, struct gsm_info *gsm)
{
	return gsm && g_isi_sb_iter_get_word(iter, &gsm->lac, 2) &&
			g_isi_sb_iter_get_dword(iter, &gsm->ci, 4) &&
			g_isi_sb_iter_get_byte(iter, &gsm->egprs, 17) &&
			g_isi_sb_iter_get_byte(iter, &gsm->hsdpa, 20) &&
			g_isi_sb_iter_get_byte(iter, &gsm->hsupa, 21);
}

static void reg_status_ind_cb(const GIsiMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	GIsiSubBlockIter iter;

	if (netreg == NULL || nd == NULL)
		return;

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

	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 NET_REG_INFO_COMMON:

			if (!parse_common_info(&iter, &nd->reg))
				return;
			break;

		case NET_GSM_REG_INFO:

			if (!parse_gsm_info(&iter, &nd->gsm))
				return;
			break;
		}
	}

	ofono_netreg_status_notify(netreg, isi_status_to_at_status(&nd->reg),
					nd->gsm.lac, nd->gsm.ci,
					isi_to_at_tech(&nd->rat, &nd->gsm));
}

static gboolean parse_rat_info(GIsiSubBlockIter *iter, struct rat_info *rat)
{
	uint8_t len;

	if (!g_isi_sb_iter_get_byte(iter, &rat->rat, 2))
		return FALSE;

	if (!g_isi_sb_iter_get_byte(iter, &len, 3))
		return FALSE;

	if (len != 0 && !g_isi_sb_iter_get_byte(iter, &rat->compact, 4))
		return FALSE;

	return TRUE;
}

static void rat_ind_cb(const GIsiMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);

	GIsiSubBlockIter iter;

	if (nd == NULL || g_isi_msg_id(msg) != NET_RAT_IND)
		return;

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

		if (g_isi_sb_iter_get_id(&iter) != NET_RAT_INFO)
			continue;

		if (!parse_rat_info(&iter, &nd->rat))
			return;
	}

	ofono_netreg_status_notify(netreg, isi_status_to_at_status(&nd->reg),
					nd->gsm.lac, nd->gsm.ci,
					isi_to_at_tech(&nd->rat, &nd->gsm));
}

static void reg_status_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	struct ofono_netreg *netreg = cbd->user;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	ofono_netreg_status_cb_t cb = cbd->cb;

	GIsiSubBlockIter iter;

	if (!check_response_status(msg, NET_REG_STATUS_GET_RESP))
		goto error;

	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 NET_REG_INFO_COMMON:

			if (!parse_common_info(&iter, &nd->reg))
				goto error;
			break;

		case NET_GSM_REG_INFO:

			if (!parse_gsm_info(&iter, &nd->gsm))
				goto error;
			break;
		}
	}

	CALLBACK_WITH_SUCCESS(cb, isi_status_to_at_status(&nd->reg),
				nd->gsm.lac, nd->gsm.ci,
				isi_to_at_tech(&nd->rat, &nd->gsm),
				cbd->data);
	g_free(cbd);
	return;

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

static void rat_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	struct ofono_netreg *netreg = cbd->user;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	ofono_netreg_status_cb_t cb = cbd->cb;

	const uint8_t req[] = {
		NET_REG_STATUS_GET_REQ
	};
	GIsiSubBlockIter iter;

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

	if (!check_response_status(msg, NET_RAT_RESP))
		goto error;

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

		if (g_isi_sb_iter_get_id(&iter) != NET_RAT_INFO)
			continue;

		if (!parse_rat_info(&iter, &nd->rat))
			goto error;
	}

	if (g_isi_client_send(nd->client, req, sizeof(req),
				reg_status_resp_cb, cbd, NULL))
		return;

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

static void isi_registration_status(struct ofono_netreg *netreg,
					ofono_netreg_status_cb_t cb, void *data)
{
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct isi_cb_data *cbd = isi_cb_data_new(netreg, cb, data);

	/*
	 * Current technology depends on the current RAT as well as
	 * the services reported by the current cell.  Therefore we
	 * need a pair of queries to deduce the full registration
	 * status: first query for the RAT then the actual
	 * registration status.
	 */
	const uint8_t rat[] = {
		NET_RAT_REQ,
		NET_CURRENT_RAT
	};

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

	if (g_isi_client_send(nd->client, rat, sizeof(rat),
				rat_resp_cb, cbd, NULL))
		return;

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

static void name_get_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	ofono_netreg_operator_cb_t cb = cbd->cb;
	struct ofono_network_operator op;

	GIsiSubBlockIter iter;
	uint8_t len = 0;
	char *tag = NULL;

	memset(&op, 0, sizeof(struct ofono_network_operator));

	if (!check_response_status(msg, NET_OPER_NAME_READ_RESP))
		goto error;

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

		switch (g_isi_sb_iter_get_id(&iter)) {
		case NET_GSM_OPERATOR_INFO:

			if (!g_isi_sb_iter_get_oper_code(&iter, op.mcc,
								op.mnc, 2))
				goto error;
			break;

		case NET_OPER_NAME_INFO:

			if (!g_isi_sb_iter_get_byte(&iter, &len, 3))
				goto error;

			/* Name is UCS-2 encoded */
			len *= 2;

			if (!g_isi_sb_iter_get_alpha_tag(&iter, &tag, len, 4))
				goto error;

			strncpy(op.name, tag, OFONO_MAX_OPERATOR_NAME_LENGTH);
			op.name[OFONO_MAX_OPERATOR_NAME_LENGTH] = '\0';
			g_free(tag);
			break;
		}
	}

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

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


static void isi_current_operator(struct ofono_netreg *netreg,
					ofono_netreg_operator_cb_t cb,
					void *data)
{
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct isi_cb_data *cbd = isi_cb_data_new(netreg, cb, data);

	const uint8_t msg[] = {
		NET_OPER_NAME_READ_REQ,
		NET_HARDCODED_LATIN_OPER_NAME,
		OFONO_MAX_OPERATOR_NAME_LENGTH,
		0x00, 0x00,  /* Index not used */
		0x00,  /* Filler */
		0x00  /* No sub-blocks */
	};

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

	if (g_isi_client_send(nd->client, msg, sizeof(msg),
				name_get_resp_cb, cbd, g_free))
		return;

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


static void available_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	ofono_netreg_operator_list_cb_t cb = cbd->cb;
	struct ofono_network_operator *list = NULL;

	GIsiSubBlockIter iter;
	uint8_t sb_count;

	int total = 0;
	int common = 0;
	int detail = 0;

	if (!check_response_status(msg, NET_AVAILABLE_GET_RESP))
		goto error;

	if (!g_isi_msg_data_get_byte(msg, 1, &sb_count))
		goto error;

	/* Each description of an operator has a pair of sub-blocks */
	total = sb_count / 2;
	list = alloca(total * sizeof(struct ofono_network_operator));

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

		struct ofono_network_operator *op;
		char *tag = NULL;
		uint8_t taglen = 0;
		uint8_t status = 0;
		uint8_t umts = 0;

		switch (g_isi_sb_iter_get_id(&iter)) {

		case NET_AVAIL_NETWORK_INFO_COMMON:

			if (!g_isi_sb_iter_get_byte(&iter, &status, 2)
				|| !g_isi_sb_iter_get_byte(&iter, &taglen, 5)
				|| !g_isi_sb_iter_get_alpha_tag(&iter, &tag,
						taglen * 2, 6))
				goto error;

			op = list + common++;
			op->status = status;

			strncpy(op->name, tag, OFONO_MAX_OPERATOR_NAME_LENGTH);
			op->name[OFONO_MAX_OPERATOR_NAME_LENGTH] = '\0';
			g_free(tag);
			break;

		case NET_DETAILED_NETWORK_INFO:
			op = list + detail++;

			if (!g_isi_sb_iter_get_oper_code(&iter, op->mcc,
								op->mnc, 2)
				|| !g_isi_sb_iter_get_byte(&iter, &umts, 7))
				goto error;

			op->tech = umts ? 2 : 3;
			break;
		}
	}

	if (common == detail && detail == total) {
		CALLBACK_WITH_SUCCESS(cb, total, list, cbd->data);
		return;
	}

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

static void isi_list_operators(struct ofono_netreg *netreg,
				ofono_netreg_operator_list_cb_t cb,
				void *data)
{
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct isi_cb_data *cbd = isi_cb_data_new(netreg, cb, data);

	const unsigned char msg[] = {
		NET_AVAILABLE_GET_REQ,
		NET_MANUAL_SEARCH,
		0x01,  /* Sub-block count */
		NET_GSM_BAND_INFO,
		0x04,  /* Sub-block length */
		NET_GSM_BAND_ALL_SUPPORTED_BANDS,
		0x00
	};

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

	if (g_isi_client_send_with_timeout(nd->client, msg, sizeof(msg),
				NETWORK_SCAN_TIMEOUT, available_resp_cb, cbd,
				g_free))
		return;

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

static void set_auto_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	struct netreg_data *nd = cbd->user;
	ofono_netreg_register_cb_t cb = cbd->cb;

	if (!check_response_status(msg, NET_SET_RESP)) {
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	nd->reg.mode = NET_SELECT_MODE_AUTOMATIC;
	CALLBACK_WITH_SUCCESS(cb, cbd->data);
}

static void isi_register_auto(struct ofono_netreg *netreg,
				ofono_netreg_register_cb_t cb,
				void *data)
{
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct isi_cb_data *cbd = isi_cb_data_new(netreg, cb, data);

	const unsigned char msg[] = {
		NET_SET_REQ,
		0x00,  /* Registered in another protocol? */
		0x01,  /* Sub-block count */
		NET_OPERATOR_INFO_COMMON,
		0x04,  /* Sub-block length */
		nd->reg.mode == NET_SELECT_MODE_AUTOMATIC
			? NET_SELECT_MODE_USER_RESELECTION
			: NET_SELECT_MODE_AUTOMATIC,
		0x00  /* Index not used */
	};

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

	if (g_isi_client_send_with_timeout(nd->client, msg, sizeof(msg),
				NETWORK_SET_TIMEOUT,
				set_auto_resp_cb, cbd, g_free))
		return;

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

static void set_manual_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	struct ofono_netreg *netreg = cbd->user;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	ofono_netreg_register_cb_t cb = cbd->cb;

	if (!check_response_status(msg, NET_SET_RESP)) {
		CALLBACK_WITH_FAILURE(cb, cbd->data);
		return;
	}

	nd->reg.mode = NET_SELECT_MODE_MANUAL;
	CALLBACK_WITH_SUCCESS(cb, cbd->data);
}

static void isi_register_manual(struct ofono_netreg *netreg,
				const char *mcc, const char *mnc,
				ofono_netreg_register_cb_t cb, void *data)
{
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct isi_cb_data *cbd = isi_cb_data_new(netreg, cb, data);

	guint8 buffer[3] = { 0 };
	guint8 *bcd = mccmnc_to_bcd(mcc, mnc, buffer);

	const unsigned char msg[] = {
		NET_SET_REQ,
		0x00,  /* Registered in another protocol? */
		0x02,  /* Sub-block count */
		NET_OPERATOR_INFO_COMMON,
		0x04,  /* Sub-block length */
		NET_SELECT_MODE_MANUAL,
		0x00,  /* Index not used */
		NET_GSM_OPERATOR_INFO,
		0x08,  /* Sub-block length */
		bcd[0], bcd[1], bcd[2],
		NET_GSM_BAND_INFO_NOT_AVAIL,  /* Pick any supported band */
		0x00, 0x00  /* Filler */
	};

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

	if (g_isi_client_send_with_timeout(nd->client, msg, sizeof(msg),
				NETWORK_SET_TIMEOUT,
				set_manual_resp_cb, cbd, g_free))
		return;

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

static void isi_deregister(struct ofono_netreg *netreg,
				ofono_netreg_register_cb_t cb,
				void *data)
{
	DBG("Not implemented.");
	CALLBACK_WITH_FAILURE(cb, data);
}

static void rssi_ind_cb(const GIsiMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;
	uint8_t rssi;

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

	if (!g_isi_msg_data_get_byte(msg, 0, &rssi))
		return;

	ofono_netreg_strength_notify(netreg, rssi ? rssi : -1);
}

static gboolean parse_nettime(GIsiSubBlockIter *iter,
				struct ofono_network_time *info)
{
	struct network_time *time;
	size_t len = sizeof(struct network_time);

	if (!g_isi_sb_iter_get_struct(iter, (void **) &time, len, 2))
		return FALSE;

	/* Value is years since last turn of century */
	info->year = time->year != NET_INVALID_TIME ? time->year : -1;
	info->year += 2000;

	info->mon = time->mon != NET_INVALID_TIME ? time->mon : -1;
	info->mday = time->mday != NET_INVALID_TIME ? time->mday : -1;
	info->hour = time->hour != NET_INVALID_TIME ? time->hour : -1;
	info->min = time->min != NET_INVALID_TIME ? time->min : -1;
	info->sec = time->sec != NET_INVALID_TIME ? time->sec : -1;

	/* Most significant bit set indicates negative offset. The
	 * second most significant bit is 'reserved'. The value is the
	 * offset from UTCin a count of 15min intervals, possibly
	 * including the current DST adjustment. */
	info->utcoff = (time->utc & 0x3F) * 15 * 60;
	if (time->utc & 0x80)
		info->utcoff *= -1;

	info->dst = time->dst != NET_INVALID_TIME ? time->dst : -1;
	return TRUE;
}

static void time_ind_cb(const GIsiMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;
	struct ofono_network_time info;
	GIsiSubBlockIter iter;

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

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

		if (g_isi_sb_iter_get_id(&iter) != NET_TIME_INFO)
			continue;

		if (!parse_nettime(&iter, &info))
			return;

		ofono_netreg_time_notify(netreg, &info);
		return;
	}
}

static void rssi_resp_cb(const GIsiMessage *msg, void *data)
{
	struct isi_cb_data *cbd = data;
	ofono_netreg_strength_cb_t cb = cbd->cb;
	uint8_t rssi;

	GIsiSubBlockIter iter;

	if (!check_response_status(msg, NET_RSSI_GET_RESP)) {
		CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
		return;
	}

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

		if (g_isi_sb_iter_get_id(&iter) != NET_RSSI_CURRENT)
			continue;

		if (!g_isi_sb_iter_get_byte(&iter, &rssi, 2))
			break;

		CALLBACK_WITH_SUCCESS(cb, rssi ? rssi : -1, cbd->data);
		return;
	}
}

static void isi_strength(struct ofono_netreg *netreg,
				ofono_netreg_strength_cb_t cb,
				void *data)
{
	struct netreg_data *nd = ofono_netreg_get_data(netreg);
	struct isi_cb_data *cbd = isi_cb_data_new(netreg, cb, data);

	const uint8_t msg[] = {
		NET_RSSI_GET_REQ,
		NET_CS_GSM,
		NET_CURRENT_CELL_RSSI,
	};

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

	if (g_isi_client_send(nd->client, msg, sizeof(msg),
				rssi_resp_cb, cbd, g_free))
		return;

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

static void reachable_cb(const GIsiMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;
	struct netreg_data *nd = ofono_netreg_get_data(netreg);

	if (g_isi_msg_error(msg) < 0)
		return;

	ISI_VERSION_DBG(msg);

	g_isi_client_ind_subscribe(nd->client, NET_RSSI_IND, rssi_ind_cb,
					netreg);
	g_isi_client_ind_subscribe(nd->client, NET_REG_STATUS_IND,
					reg_status_ind_cb, netreg);
	g_isi_client_ind_subscribe(nd->client, NET_RAT_IND, rat_ind_cb, netreg);
	g_isi_client_ind_subscribe(nd->client, NET_TIME_IND, time_ind_cb,
					netreg);

	ofono_netreg_register(netreg);
}

static int isi_netreg_probe(struct ofono_netreg *netreg, unsigned int vendor,
				void *user)
{
	GIsiModem *modem = user;
	struct netreg_data *nd;

	nd = g_try_new0(struct netreg_data, 1);
	if (nd == NULL)
		return -ENOMEM;

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

	ofono_netreg_set_data(netreg, nd);

	g_isi_client_verify(nd->client, reachable_cb, netreg, NULL);

	return 0;
}

static void isi_netreg_remove(struct ofono_netreg *netreg)
{
	struct netreg_data *data = ofono_netreg_get_data(netreg);

	ofono_netreg_set_data(netreg, NULL);

	if (data == NULL)
		return;

	g_isi_client_destroy(data->client);
	g_free(data);
}

static struct ofono_netreg_driver driver = {
	.name			= "isimodem",
	.probe			= isi_netreg_probe,
	.remove			= isi_netreg_remove,
	.registration_status	= isi_registration_status,
	.current_operator	= isi_current_operator,
	.list_operators		= isi_list_operators,
	.register_auto		= isi_register_auto,
	.register_manual	= isi_register_manual,
	.deregister		= isi_deregister,
	.strength		= isi_strength,
};

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

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