summaryrefslogtreecommitdiffstats
path: root/net.c
blob: 87f8c85c7cde0a9c963fd1a2fae7acb58ba76ea4 (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
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/*
 * This file is part of Samsung-RIL.
 *
 * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
 * Copyright (C) 2011-2013 Paul Kocialkowski <contact@paulk.fr>
 *
 * Samsung-RIL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsung-RIL 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 Samsung-RIL.  If not, see <http://www.gnu.org/licenses/>.
 */

#define LOG_TAG "RIL-NET"
#include <utils/Log.h>

#include "samsung-ril.h"
#include "util.h"

#include <plmn_list.h>

unsigned char ipc2ril_reg_state(unsigned char reg_state)
{
	switch (reg_state) {
		case IPC_NET_REGISTRATION_STATE_NONE:
			return 0;
		case IPC_NET_REGISTRATION_STATE_HOME:
			return 1;
		case IPC_NET_REGISTRATION_STATE_SEARCHING:
			return 2;
		case IPC_NET_REGISTRATION_STATE_EMERGENCY:
			return 10;
		case IPC_NET_REGISTRATION_STATE_ROAMING:
			return 5;
		case IPC_NET_REGISTRATION_STATE_UNKNOWN:
			return 4;
		default:
			LOGE("%s: invalid reg_state: %d", __func__, reg_state);
			return 0;
	}
}

unsigned char ipc2ril_act(unsigned char act)
{
	switch (act) {
		case IPC_NET_ACCESS_TECHNOLOGY_GPRS:
			return 1;
		case IPC_NET_ACCESS_TECHNOLOGY_EDGE:
			return 2;
		case IPC_NET_ACCESS_TECHNOLOGY_UMTS:
			return 3;
		case IPC_NET_ACCESS_TECHNOLOGY_GSM:
		case IPC_NET_ACCESS_TECHNOLOGY_GSM2:
		default:
			return 0;
	}
}

unsigned char ipc2ril_gprs_act(unsigned char act)
{
	switch (act) {
		case IPC_NET_ACCESS_TECHNOLOGY_GPRS:
			return 1;
		case IPC_NET_ACCESS_TECHNOLOGY_EDGE:
			return 2;
		case IPC_NET_ACCESS_TECHNOLOGY_UMTS:
			return 3;
		case IPC_NET_ACCESS_TECHNOLOGY_GSM:
		case IPC_NET_ACCESS_TECHNOLOGY_GSM2:
		default:
			return 0;
	}
}

int ipc2ril_mode_sel(unsigned char mode)
{
	switch (mode) {
		case 0:
			return 7; // auto mode
		case IPC_NET_MODE_SEL_GSM_UMTS:
			return 0;
		case IPC_NET_MODE_SEL_GSM_ONLY:
			return 1;
		case IPC_NET_MODE_SEL_UMTS_ONLY:
			return 2;
		default:
			return 0;
	}
}

unsigned char ril2ipc_mode_sel(int mode)
{
	switch (mode) {
		case 1: // GSM only
			return IPC_NET_MODE_SEL_GSM_ONLY;
		case 2: // WCDMA only
			return IPC_NET_MODE_SEL_UMTS_ONLY;
		case 0:
		default: // GSM/WCDMA + the rest
			return IPC_NET_MODE_SEL_GSM_UMTS;
	}
}

int ipc2ril_plmn_sel(unsigned char mode)
{
	switch (mode) {
		case IPC_NET_PLMN_SEL_MANUAL:
			return 1;
		case IPC_NET_PLMN_SEL_AUTO:
			return 0;
		default:
			return 0;
	}
}

unsigned char ril2ipc_plmn_sel(int mode)
{
	switch (mode) {
		case 0:
			return IPC_NET_PLMN_SEL_AUTO;
		case 1:
			return IPC_NET_PLMN_SEL_MANUAL;
		default:
			return 0;
	}
}

void ipc2ril_reg_state_resp(struct ipc_net_regist_response *netinfo, char *response[15])
{
	unsigned char reg_state;
	unsigned char act;

	if (netinfo == NULL || response == NULL)
		return;

	reg_state = ipc2ril_reg_state(netinfo->reg_state);
	act = ipc2ril_act(netinfo->act);

	memset(response, 0, sizeof(response));

	asprintf(&response[0], "%d", reg_state);
	asprintf(&response[1], "%x", netinfo->lac);
	asprintf(&response[2], "%x", netinfo->cid);
	asprintf(&response[3], "%d", act);
}

void ipc2ril_gprs_reg_state_resp(struct ipc_net_regist_response *netinfo, char *response[4])
{
	unsigned char reg_state;
	unsigned char act;

	if (netinfo == NULL || response == NULL)
		return;

	reg_state = ipc2ril_reg_state(netinfo->reg_state);
	act = ipc2ril_gprs_act(netinfo->act);

	memset(response, 0, sizeof(response));

	asprintf(&response[0], "%d", reg_state);
	asprintf(&response[1], "%x", netinfo->lac);
	asprintf(&response[2], "%x", netinfo->cid);
	asprintf(&response[3], "%d", act);
}

/*
 * Set all the tokens to data waiting.
 * For instance when only operator is updated by modem NOTI, we don't need
 * to ask the modem new NET Regist and GPRS Net Regist states so act like we got
 * these from modem NOTI too so we don't have to make the requests
 */
void ril_tokens_net_set_data_waiting(void)
{
	ril_data.tokens.registration_state = RIL_TOKEN_DATA_WAITING;
	ril_data.tokens.gprs_registration_state = RIL_TOKEN_DATA_WAITING;
	ril_data.tokens.operator = RIL_TOKEN_DATA_WAITING;
}

/*
 * Returns 1 if unsol data is waiting, 0 if not
 */
int ril_tokens_net_get_data_waiting(void)
{
	return ril_data.tokens.registration_state == RIL_TOKEN_DATA_WAITING || ril_data.tokens.gprs_registration_state == RIL_TOKEN_DATA_WAITING || ril_data.tokens.operator == RIL_TOKEN_DATA_WAITING;
}

void ril_tokens_net_state_dump(void)
{
	LOGD("ril_tokens_net_state_dump:\n\
	\tril_data.tokens.registration_state = %p\n\
	\tril_data.tokens.gprs_registration_state = %p\n\
	\tril_data.tokens.operator = %p\n", ril_data.tokens.registration_state, ril_data.tokens.gprs_registration_state, ril_data.tokens.operator);
}

void ril_plmn_split(char *plmn_data, char **plmn, unsigned int *mcc, unsigned int *mnc)
{
	char plmn_t[7];
	int i;

	memset(plmn_t, 0, sizeof(plmn_t));
	memcpy(plmn_t, plmn_data, 6);

	if (plmn_t[5] == '#')
		plmn_t[5] = '\0';

	if (plmn != NULL) {
		*plmn = malloc(sizeof(plmn_t));
		memcpy(*plmn, plmn_t, sizeof(plmn_t));
	}

	if (mcc == NULL || mnc == NULL)
		return;

	sscanf(plmn_t, "%3u%2u", mcc, mnc);
}

void ril_plmn_string(char *plmn_data, char *response[3])
{
	unsigned int mcc, mnc;
	char *plmn = NULL;

	int plmn_entries;
	int i;

	if (plmn_data == NULL || response == NULL)
		return;

	ril_plmn_split(plmn_data, &plmn, &mcc, &mnc);

	asprintf(&response[2], "%s", plmn);

	if (plmn != NULL)
		free(plmn);

	plmn_entries = sizeof(plmn_list) / sizeof(struct plmn_list_entry);

	LOGD("Found %d plmn records", plmn_entries);

	for (i = 0 ; i < plmn_entries ; i++) {
		if (plmn_list[i].mcc == mcc && plmn_list[i].mnc == mnc) {
			asprintf(&response[0], "%s", plmn_list[i].operator_long);
			asprintf(&response[1], "%s", plmn_list[i].operator_short);
			return;	
		}
	}

	response[0] = NULL;
	response[1] = NULL;
}

/*
 * How to handle NET unsol data from modem:
 * 1- Rx UNSOL (NOTI) data from modem
 * 2- copy data in a sized variable stored in radio
 * 3- make sure no SOL request is going on for this token
 * 4- copy data to radio structure
 * 5- if no UNSOL data is already waiting for a token, tell RILJ NETWORK_STATE_CHANGED
 * 6- set all the net tokens to RIL_TOKEN_DATA_WAITING
 * 7- RILJ will ask for OPERATOR, GPRS_REG_STATE and REG_STATE
 * for each request:
 * 8- if token is RIL_TOKEN_DATA_WAITING it's SOL request for modem UNSOL data
 * 9- send back modem data and tell E_SUCCESS to RILJ request
 * 10- set token to 0x00
 *
 * How to handle NET sol requests from RILJ:
 * 1- if token is 0x00 it's UNSOL RILJ request for modem data
 * 2- put RIL_Token in token
 * 3- request data to the modem
 * 4- Rx SOL (RESP) data from modem
 * 5- copy data to radio structure
 * 6- send back data to RILJ with token from modem message
 * 7- if token != RIL_TOKEN_DATA_WAITING, reset token to 0x00
 *
 * What if both are appening at the same time?
 * 1- RILJ requests modem data (UNSOL)
 * 2- token is 0x00 so send request to modem
 * 3- UNSOL data arrives from modem
 * 4- set all tokens to RIL_TOKEN_DATA_WAITING
 * 5- store data, tell RILJ NETWORK_STATE_CHANGED
 * 6- Rx requested data from modem
 * 7- copy data to radio structure
 * 8- token mismatch (is now RIL_TOKEN_DATA_WAITING)
 * 9- send back data to RIL with token from IPC message
 * 10- don't reset token to 0x00
 * 11- RILJ does SOL request for modem data (we know it's SOL because we didn't reset token)
 * 12- send back last data we have (from UNSOL RILJ request here)
 */

void ril_request_operator(RIL_Token t)
{
	char *response[3];
	size_t i;

	// IPC_NET_REGISTRATION_STATE_ROAMING is the biggest valid value
	if (ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_NONE ||
	ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_SEARCHING ||
	ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_UNKNOWN ||
	ril_data.state.netinfo.reg_state > IPC_NET_REGISTRATION_STATE_ROAMING) {
		ril_request_complete(t, RIL_E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW, NULL, 0);

		ril_data.tokens.operator = RIL_TOKEN_NULL;
		return;
	}

	if (ril_data.tokens.operator == RIL_TOKEN_DATA_WAITING) {
		LOGD("Got RILJ request for UNSOL data");

		/* Send back the data we got UNSOL */
		ril_plmn_string(ril_data.state.plmndata.plmn, response);

		ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

		for (i = 0; i < sizeof(response) / sizeof(char *) ; i++) {
			if (response[i] != NULL)
				free(response[i]);
		}

		ril_data.tokens.operator = RIL_TOKEN_NULL;
	} else if (ril_data.tokens.operator == RIL_TOKEN_NULL) {
		LOGD("Got RILJ request for SOL data");
		/* Request data to the modem */
		ril_data.tokens.operator = t;

		ipc_fmt_send_get(IPC_NET_CURRENT_PLMN, ril_request_get_id(t));
	} else {
		LOGE("Another request is going on, returning UNSOL data");

		/* Send back the data we got UNSOL */
		ril_plmn_string(ril_data.state.plmndata.plmn, response);

		ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

		for (i = 0; i < sizeof(response) / sizeof(char *) ; i++) {
			if (response[i] != NULL)
				free(response[i]);
		}
	}

	ril_tokens_net_state_dump();
}

void ipc_net_current_plmn(struct ipc_message_info *info)
{
	struct ipc_net_current_plmn_response *plmndata;
	RIL_Token t;

	char *response[3];
	size_t i;

	if (info->data == NULL || info->length < sizeof(struct ipc_net_current_plmn_response))
		goto error;

	plmndata = (struct ipc_net_current_plmn_response *) info->data;
	t = ril_request_get_token(info->aseq);

	switch (info->type) {
		case IPC_TYPE_NOTI:
			LOGD("Got UNSOL Operator message");

			// IPC_NET_REGISTRATION_STATE_ROAMING is the biggest valid value
			if (ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_NONE ||
			ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_SEARCHING ||
			ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_UNKNOWN ||
			ril_data.state.netinfo.reg_state > IPC_NET_REGISTRATION_STATE_ROAMING) {
				/* Better keeping it up to date */
				memcpy(&(ril_data.state.plmndata), plmndata, sizeof(struct ipc_net_current_plmn_response));

				return;
			} else {
				if (ril_data.tokens.operator != RIL_TOKEN_NULL && ril_data.tokens.operator != RIL_TOKEN_DATA_WAITING) {
					LOGE("Another Operator Req is in progress, skipping");
					return;
				}

				memcpy(&(ril_data.state.plmndata), plmndata, sizeof(struct ipc_net_current_plmn_response));

				/* we already told RILJ to get the new data but it wasn't done yet */
				if (ril_tokens_net_get_data_waiting() && ril_data.tokens.operator == RIL_TOKEN_DATA_WAITING) {
					LOGD("Updating Operator data in background");
				} else {
					ril_tokens_net_set_data_waiting();
#if RIL_VERSION >= 6
					ril_request_unsolicited(RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED, NULL, 0);
#else
					ril_request_unsolicited(RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED, NULL, 0);
#endif
				}
			}
			break;
		case IPC_TYPE_RESP:
			LOGD("Got SOL Operator message");

			// IPC_NET_REGISTRATION_STATE_ROAMING is the biggest valid value
			if (ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_NONE ||
			ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_SEARCHING ||
			ril_data.state.netinfo.reg_state == IPC_NET_REGISTRATION_STATE_UNKNOWN ||
			ril_data.state.netinfo.reg_state > IPC_NET_REGISTRATION_STATE_ROAMING) {
				/* Better keeping it up to date */
				memcpy(&(ril_data.state.plmndata), plmndata, sizeof(struct ipc_net_current_plmn_response));

				ril_request_complete(t, RIL_E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW, NULL, 0);

				if (ril_data.tokens.operator != RIL_TOKEN_DATA_WAITING)
					ril_data.tokens.operator = RIL_TOKEN_NULL;
				return;
			} else {
				if (ril_data.tokens.operator != t)
					LOGE("Operator tokens mismatch");

				/* Better keeping it up to date */
				memcpy(&(ril_data.state.plmndata), plmndata, sizeof(struct ipc_net_current_plmn_response));

				ril_plmn_string(plmndata->plmn, response);

				ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

				for (i = 0; i < sizeof(response) / sizeof(char *) ; i++) {
					if (response[i] != NULL)
						free(response[i]);
				}

				if (ril_data.tokens.operator != RIL_TOKEN_DATA_WAITING)
					ril_data.tokens.operator = RIL_TOKEN_NULL;
			}
			break;
		default:
			LOGE("%s: unhandled ipc method: %d", __func__, info->type);
			break;
	}

	ril_tokens_net_state_dump();

	return;

error:
	if (info != NULL)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

#if RIL_VERSION >= 6
void ril_request_voice_registration_state(RIL_Token t)
#else
void ril_request_registration_state(RIL_Token t)
#endif
{
	struct ipc_net_regist_get regist_req;
	char *response[4];
	int i;

	if (ril_data.tokens.registration_state == RIL_TOKEN_DATA_WAITING) {
		LOGD("Got RILJ request for UNSOL data");

		/* Send back the data we got UNSOL */
		ipc2ril_reg_state_resp(&(ril_data.state.netinfo), response);

		ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

		for (i = 0; i < (int) (sizeof(response) / sizeof(char *)) ; i++) {
			if (response[i] != NULL)
				free(response[i]);
		}

		ril_data.tokens.registration_state = RIL_TOKEN_NULL;
	} else if (ril_data.tokens.registration_state == RIL_TOKEN_NULL) {
		LOGD("Got RILJ request for SOL data");
		/* Request data to the modem */
		ril_data.tokens.registration_state = t;

		ipc_net_regist_get_setup(&regist_req, IPC_NET_SERVICE_DOMAIN_GSM);
		ipc_fmt_send(IPC_NET_REGIST, IPC_TYPE_GET, (void *)&regist_req, sizeof(struct ipc_net_regist_get), ril_request_get_id(t));
	} else {
		LOGE("Another request is going on, returning UNSOL data");

		/* Send back the data we got UNSOL */
		ipc2ril_reg_state_resp(&(ril_data.state.netinfo), response);

		ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

		for (i = 0; i < (int) (sizeof(response) / sizeof(char *)) ; i++) {
			if (response[i] != NULL)
				free(response[i]);
		}
	}

	ril_tokens_net_state_dump();
}

#if RIL_VERSION >= 6
void ril_request_data_registration_state(RIL_Token t)
#else
void ril_request_gprs_registration_state(RIL_Token t)
#endif
{
	struct ipc_net_regist_get regist_req;
	char *response[4];
	size_t i;

	if (ril_data.tokens.gprs_registration_state == RIL_TOKEN_DATA_WAITING) {
		LOGD("Got RILJ request for UNSOL data");

		/* Send back the data we got UNSOL */
		ipc2ril_gprs_reg_state_resp(&(ril_data.state.gprs_netinfo), response);

		ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

		for (i = 0; i < sizeof(response) / sizeof(char *) ; i++) {
			if (response[i] != NULL)
				free(response[i]);
		}

		ril_data.tokens.gprs_registration_state = RIL_TOKEN_NULL;
	} else if (ril_data.tokens.gprs_registration_state == RIL_TOKEN_NULL) {
		LOGD("Got RILJ request for SOL data");

		/* Request data to the modem */
		ril_data.tokens.gprs_registration_state = t;

		ipc_net_regist_get_setup(&regist_req, IPC_NET_SERVICE_DOMAIN_GPRS);
		ipc_fmt_send(IPC_NET_REGIST, IPC_TYPE_GET, (void *)&regist_req, sizeof(struct ipc_net_regist_get), ril_request_get_id(t));
	} else {
		LOGE("Another request is going on, returning UNSOL data");

		/* Send back the data we got UNSOL */
		ipc2ril_gprs_reg_state_resp(&(ril_data.state.gprs_netinfo), response);

		ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

		for (i = 0; i < sizeof(response) / sizeof(char *) ; i++) {
			if (response[i] != NULL)
				free(response[i]);
		}
	}

	ril_tokens_net_state_dump();
}

void ipc_net_regist_unsol(struct ipc_message_info *info)
{
	struct ipc_net_regist_response *netinfo;

	if (info->data == NULL || info->length < sizeof(struct ipc_net_regist_response))
		goto error;

	netinfo = (struct ipc_net_regist_response *) info->data;

	LOGD("Got UNSOL NetRegist message");

	switch (netinfo->domain) {
		case IPC_NET_SERVICE_DOMAIN_GSM:
			if (ril_data.tokens.registration_state != RIL_TOKEN_NULL && ril_data.tokens.registration_state != RIL_TOKEN_DATA_WAITING) {
				LOGE("Another NetRegist Req is in progress, skipping");
				return;
			}

			memcpy(&(ril_data.state.netinfo), netinfo, sizeof(struct ipc_net_regist_response));

			/* we already told RILJ to get the new data but it wasn't done yet */
			if (ril_tokens_net_get_data_waiting() && ril_data.tokens.registration_state == RIL_TOKEN_DATA_WAITING) {
				LOGD("Updating NetRegist data in background");
			} else {
				ril_tokens_net_set_data_waiting();
#if RIL_VERSION >= 6
				ril_request_unsolicited(RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED, NULL, 0);
#else
				ril_request_unsolicited(RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED, NULL, 0);
#endif
			}
			break;

		case IPC_NET_SERVICE_DOMAIN_GPRS:
			if (ril_data.tokens.gprs_registration_state != RIL_TOKEN_NULL && ril_data.tokens.gprs_registration_state != RIL_TOKEN_DATA_WAITING) {
				LOGE("Another GPRS NetRegist Req is in progress, skipping");
				return;
			}

			memcpy(&(ril_data.state.gprs_netinfo), netinfo, sizeof(struct ipc_net_regist_response));

			/* we already told RILJ to get the new data but it wasn't done yet */
			if (ril_tokens_net_get_data_waiting() && ril_data.tokens.gprs_registration_state == RIL_TOKEN_DATA_WAITING) {
				LOGD("Updating GPRSNetRegist data in background");
			} else {
				ril_tokens_net_set_data_waiting();
#if RIL_VERSION >= 6
				ril_request_unsolicited(RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED, NULL, 0);
#else
				ril_request_unsolicited(RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED, NULL, 0);
#endif
			}
			break;
		default:
			LOGE("%s: unhandled service domain: %d", __func__, netinfo->domain);
			break;
	}

	ril_tokens_net_state_dump();

	return;

error:
	if (info != NULL)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ipc_net_regist_sol(struct ipc_message_info *info)
{
	struct ipc_net_regist_response *netinfo;
	RIL_Token t;

	char *response[4];
	size_t i;

	if (info->data == NULL || info->length < sizeof(struct ipc_net_regist_response))
		goto error;

	netinfo = (struct ipc_net_regist_response *) info->data;
	t = ril_request_get_token(info->aseq);

	LOGD("Got SOL NetRegist message");

	switch (netinfo->domain) {
		case IPC_NET_SERVICE_DOMAIN_GSM:
			if (ril_data.tokens.registration_state != t)
				LOGE("Registration state tokens mismatch");

			/* Better keeping it up to date */
			memcpy(&(ril_data.state.netinfo), netinfo, sizeof(struct ipc_net_regist_response));

			ipc2ril_reg_state_resp(netinfo, response);

			ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

			for (i = 0; i < sizeof(response) / sizeof(char *) ; i++) {
				if (response[i] != NULL)
					free(response[i]);
			}

			if (ril_data.tokens.registration_state != RIL_TOKEN_DATA_WAITING)
				ril_data.tokens.registration_state = RIL_TOKEN_NULL;
			break;
		case IPC_NET_SERVICE_DOMAIN_GPRS:
			if (ril_data.tokens.gprs_registration_state != t)
				LOGE("GPRS registration state tokens mismatch");

			/* Better keeping it up to date */
			memcpy(&(ril_data.state.gprs_netinfo), netinfo, sizeof(struct ipc_net_regist_response));

			ipc2ril_gprs_reg_state_resp(netinfo, response);

			ril_request_complete(t, RIL_E_SUCCESS, response, sizeof(response));

			for (i = 0; i < sizeof(response) / sizeof(char *) ; i++) {
				if (response[i] != NULL)
					free(response[i]);
			}
			if (ril_data.tokens.registration_state != RIL_TOKEN_DATA_WAITING)
				ril_data.tokens.gprs_registration_state = RIL_TOKEN_NULL;
			break;
		default:
			LOGE("%s: unhandled service domain: %d", __func__, netinfo->domain);
			break;
	}

	ril_tokens_net_state_dump();

	return;

error:
	if (info != NULL)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ipc_net_regist(struct ipc_message_info *info)
{
	if (info == NULL)
		return;

	/* Don't consider this if modem isn't in normal power mode. */
	if (ril_data.state.power_state != IPC_PWR_PHONE_STATE_NORMAL)
		return;

	switch (info->type) {
		case IPC_TYPE_NOTI:
			ipc_net_regist_unsol(info);
			break;
		case IPC_TYPE_RESP:
			ipc_net_regist_sol(info);
			break;
		default:
			LOGE("%s: unhandled ipc method: %d", __func__, info->type);
			break;
	}

}

void ril_request_query_available_networks(RIL_Token t)
{
	ipc_fmt_send_get(IPC_NET_PLMN_LIST, ril_request_get_id(t));
}

void ipc_net_plmn_list(struct ipc_message_info *info)
{
	struct ipc_net_plmn_entries *entries_info;
	struct ipc_net_plmn_entry *entries;

	char **response;
	int length;
	int count;

	int index;
	int i;

	if (info->data == NULL || info->length < sizeof(struct ipc_net_plmn_entries))
		goto error;

	entries_info = (struct ipc_net_plmn_entries *) info->data;
	entries = (struct ipc_net_plmn_entry *) (info->data + sizeof(struct ipc_net_plmn_entries));

	LOGD("Listed %d PLMNs\n", entries_info->num);

	length = sizeof(char *) * 4 * entries_info->num;
	response = (char **) calloc(1, length);

	count = 0;
	for (i = 0 ; i < entries_info->num ; i++) {
		// Assumed type for 'emergency only' PLMNs
		if (entries[i].type == 0x01)
			continue;

		index = count * 4;
		ril_plmn_string(entries[i].plmn, &response[index]);

		index = count * 4 + 3;
		switch (entries[i].status) {
			case IPC_NET_PLMN_STATUS_AVAILABLE:
				response[index] = strdup("available");
				break;
			case IPC_NET_PLMN_STATUS_CURRENT:
				response[index] = strdup("current");
				break;
			case IPC_NET_PLMN_STATUS_FORBIDDEN:
				response[index] = strdup("forbidden");
				break;
			default:
				response[index] = strdup("unknown");
				break;
		}

		count++;
	}

	length = sizeof(char *) * 4 * count;
	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_SUCCESS, response, length);

	for (i = 0 ; i < entries_info->num ; i++)
		if (response[i] != NULL)
			free(response[i]);

	free(response);

	return;

error:
	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ril_request_get_preferred_network_type(RIL_Token t)
{
	ipc_fmt_send_get(IPC_NET_MODE_SEL, ril_request_get_id(t));
}

void ril_request_set_preferred_network_type(RIL_Token t, void *data, size_t length)
{
	int ril_mode;
	struct ipc_net_mode_sel mode_sel;

	if (data == NULL || length < (int) sizeof(int))
		goto error;

	ril_mode = *((int *) data);

	mode_sel.mode_sel = ril2ipc_mode_sel(ril_mode);

	ipc_gen_phone_res_expect_to_complete(ril_request_get_id(t), IPC_NET_MODE_SEL);

	ipc_fmt_send(IPC_NET_MODE_SEL, IPC_TYPE_SET, (unsigned char *) &mode_sel, sizeof(mode_sel), ril_request_get_id(t));

	return;

error:
	ril_request_complete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ipc_net_mode_sel(struct ipc_message_info *info)
{
	struct ipc_net_mode_sel *mode_sel;
	int ril_mode;

	if (info->data == NULL || info->length < sizeof(struct ipc_net_mode_sel))
		goto error;

	mode_sel = (struct ipc_net_mode_sel *) info->data;
	ril_mode = ipc2ril_mode_sel(mode_sel->mode_sel);

	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_SUCCESS, &ril_mode, sizeof(ril_mode));

	return;

error:
	if (info != NULL)
		ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ril_request_query_network_selection_mode(RIL_Token t)
{
	ipc_fmt_send_get(IPC_NET_PLMN_SEL, ril_request_get_id(t));
}

void ipc_net_plmn_sel(struct ipc_message_info *info)
{
	struct ipc_net_plmn_sel_get *plmn_sel;
	int ril_mode;

	if (info->data == NULL || info->length < sizeof(struct ipc_net_plmn_sel_get))
		goto error;

	plmn_sel = (struct ipc_net_plmn_sel_get *) info->data;
	ril_mode = ipc2ril_plmn_sel(plmn_sel->plmn_sel);

	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_SUCCESS, &ril_mode, sizeof(ril_mode));

	return;

error:
	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
}

void ipc_net_plmn_sel_complete(struct ipc_message_info *info)
{
	struct ipc_gen_phone_res *phone_res;
	int rc;

	phone_res = (struct ipc_gen_phone_res *) info->data;

	rc = ipc_gen_phone_res_check(phone_res);
	if (rc < 0) {
		if ((phone_res->code & 0x00ff) == 0x6f) {
			LOGE("Not authorized to register to this network!");
			ril_request_complete(ril_request_get_token(info->aseq), RIL_E_ILLEGAL_SIM_OR_ME, NULL, 0);
		} else {
			LOGE("There was an error during operator selection!");
			ril_request_complete(ril_request_get_token(info->aseq), RIL_E_GENERIC_FAILURE, NULL, 0);
		}
		return;
	}

	ril_request_complete(ril_request_get_token(info->aseq), RIL_E_SUCCESS, NULL, 0);
}

void ril_request_set_network_selection_automatic(RIL_Token t)
{
	struct ipc_net_plmn_sel_set plmn_sel;

	ipc_net_plmn_sel_set_setup(&plmn_sel, IPC_NET_PLMN_SEL_AUTO, NULL, IPC_NET_ACCESS_TECHNOLOGY_UNKNOWN);

	ipc_gen_phone_res_expect_to_func(ril_request_get_id(t), IPC_NET_PLMN_SEL, ipc_net_plmn_sel_complete);

	ipc_fmt_send(IPC_NET_PLMN_SEL, IPC_TYPE_SET, (unsigned char *) &plmn_sel, sizeof(plmn_sel), ril_request_get_id(t));
}

void ril_request_set_network_selection_manual(RIL_Token t, void *data, size_t length)
{
	struct ipc_net_plmn_sel_set plmn_sel;

	if (data == NULL || length < (int) sizeof(char *))
		return;

	// FIXME: We always assume UMTS capability
	ipc_net_plmn_sel_set_setup(&plmn_sel, IPC_NET_PLMN_SEL_MANUAL, data, IPC_NET_ACCESS_TECHNOLOGY_UMTS);

	ipc_gen_phone_res_expect_to_func(ril_request_get_id(t), IPC_NET_PLMN_SEL, ipc_net_plmn_sel_complete);

	ipc_fmt_send(IPC_NET_PLMN_SEL, IPC_TYPE_SET, (unsigned char *) &plmn_sel, sizeof(plmn_sel), ril_request_get_id(t));
}