forked from qdk0901/sxx-ril
/
sxx-ril-network.c
executable file
·1379 lines (1141 loc) · 36 KB
/
sxx-ril-network.c
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <telephony/ril.h>
#include <pthread.h>
#include <cutils/properties.h>
#include <assert.h>
#include "sxx-ril.h"
#define REPOLL_OPERATOR_SELECTED 30 /* 30 * 2 = 1M = ok? */
static const struct timeval TIMEVAL_OPERATOR_SELECT_POLL = { 2, 0 };
static int net_mode = 0;
static int sys_mode = 0;
int creg_value, mode_value;
int getZmdsValue();
volatile int sig_ok = 0;
//------------------------private function -----------------------------
static int get_reg_stat(int registered,int roaming)
{
if(registered == 0){
return 0; //0 - Not registered, MT is not currently searching a new operator to register
}else if(registered == 4){
return 2; // 2 - Not registered, but MT is currently searching a new operator to register
}else if(roaming == 1){
return 5;// Registered, roaming
}else{
return 1;// Registered, home network
}
}
/*
0 - Unknown, 1 - GPRS, 2 - EDGE, 3 - UMTS,
* 4 - IS95A, 5 - IS95B, 6 - 1xRTT,
* 7 - EvDo Rev. 0, 8 - EvDo Rev. A,
* 9 - HSDPA, 10 - HSUPA, 11 - HSPA,
* 12 - EVDO Rev B
<sys_mode>
0 no service
1 AMPS
2 CDMA
3 GSM/GPRS
4 HDR\u6a21\u5f0f
5 WCDMA\u6a21\u5f0f
6 GPS\u6a21\u5f0f
7 GSM/WCDMA
8 CDMA/HDR HYBRID
*/
static int get_sys_mode( int sysmode)
{
int map[] = {0,8,6,1,8,3,3,3,8};
if( sysmode >= 9 || sysmode < 0){
return 0;
}
return map[sysmode];
}
/** do post-AT+CFUN=1 initialization */
static void onRadioPowerOn()
{
pollSIMState(NULL);
}
static int is_cdma_net( int sysmode){
int ret = 0;
switch(sysmode){
case 0:
case 1:
case 6:
ret = 1;
break;
case 8:
case 3:
ret = 0;
break;
}
return ret;
}
/**
* RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL
*
* Manually select a specified network.
*
* The radio baseband/RIL implementation is expected to fall back to
* automatic selection mode if the manually selected network should go
* out of range in the future.
*/
void requestSetNetworkSelectionManual(void *data, size_t datalen,
RIL_Token t)
{
_requestSetNetworkSelectionManualWCDMA(data, datalen, t);
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL, requestSetNetworkSelectionManual)
/**
* RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE
*
* Requests to set the preferred network type for searching and registering
* (CS/PS domain, RAT, and operation mode).
*/
void requestSetPreferredNetworkType(void *data, size_t datalen,
RIL_Token t)
{
_requestSetPreferredNetworkTypeWCDMA(data, datalen, t);
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE, requestSetPreferredNetworkType)
/**
* RIL_REQUEST_SIGNAL_STRENGTH
*
* Requests current signal strength and bit error rate.
*
* Must succeed if radio is on.
*/
void requestSignalStrength(void *data, size_t datalen, RIL_Token t)
{
_requestSignalStrengthWCDMA(data, datalen, t);
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_SIGNAL_STRENGTH, requestSignalStrength)
void requestSetNetworkSelectionAutomatic(void *data, size_t datalen,
RIL_Token t)
{
int err = 0;
err = at_send_command("AT+COPS=0", NULL);
if (err < 0)
goto error;
RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
return;
error:
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
return;
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC, requestSetNetworkSelectionAutomatic)
void _requestSetNetworkSelectionManualWCDMA(void *data, size_t datalen,
RIL_Token t)
{
/*
* AT+COPS=[<mode>[,<format>[,<oper>[,<AcT>]]]]
* <mode> = 4 = Manual (<oper> field shall be present and AcT optionally) with fallback to automatic if manual fails.
* <format> = 2 = Numeric <oper>, the number has structure:
* (country code digit 3)(country code digit 2)(country code digit 1)
* (network code digit 2)(network code digit 1)
*/
int err = 0;
char *cmd = NULL;
ATResponse *atresponse = NULL;
const char *mccMnc = (const char *) data;
/* Check inparameter. */
if (mccMnc == NULL) {
goto error;
}
/* Build and send command. */
asprintf(&cmd, "AT+COPS=4,2,\"%s\"", mccMnc);
err = at_send_command(cmd, &atresponse);
if (err < 0 || atresponse->success == 0)
goto error;
RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
finally:
at_response_free(atresponse);
if (cmd != NULL)
free(cmd);
return;
error:
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
goto finally;
}
void select_tech(int t)
{
char *cmd;
asprintf(&cmd, "AT+COPS=0,,,%d", t);
at_send_command(cmd,NULL);
free(cmd);
}
void _requestSetPreferredNetworkTypeWCDMA(void *data, size_t datalen,
RIL_Token t)
{
ATResponse *atresponse = NULL;
int err = 0;
int rat;
int arg;
char *cmd = NULL;
RIL_Errno errno = RIL_E_GENERIC_FAILURE;
rat = ((int *) data)[0];
if(sState == RADIO_STATE_OFF){
RIL_onRequestComplete(t,RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
return;
}
switch (rat) {
case 0:
case 3:
case 4:
case 7:
arg = 4; // atuo
break;
case 1:
case 5:
arg = 13; // GSM
break;
case 2:
case 6:
arg = 14; // WCDMA
break;
default:
RIL_onRequestComplete(t,RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
return;
}
int zv = getZmdsValue();
if(zv == arg){
RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
return;
}
asprintf(&cmd, "AT+ZMDS=%d", arg);
err = at_send_command(cmd, &atresponse);
if (err < 0 || atresponse->success == 0){
errno = RIL_E_GENERIC_FAILURE;
goto error;
}
if(arg == 14)
select_tech(2);
else if(arg == 13)
select_tech(0);
else{
at_send_command("AT+COPS=0,,,",NULL);
}
RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
finally:
free(cmd);
at_response_free(atresponse);
return;
error:
RIL_onRequestComplete(t, errno, NULL, 0);
goto finally;
}
void reportSignalStrength(void *param)
{
ATResponse *p_response = NULL;
int err, tmp;
char *line;
RIL_SignalStrength_v6 resp;
memset(&resp,0,sizeof(resp));
resp.GW_SignalStrength.signalStrength = -1;
resp.GW_SignalStrength.bitErrorRate = -1;
resp.LTE_SignalStrength.signalStrength = -1;
resp.LTE_SignalStrength.rsrp = -1;
resp.LTE_SignalStrength.rsrq = -1;
resp.LTE_SignalStrength.rssnr = -1;
resp.LTE_SignalStrength.cqi = -1;
err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
if (err < 0 || p_response->success == 0)
{
goto error;
}
line = p_response->p_intermediates->line;
err = at_tok_start(&line);
if (err < 0) goto error;
err = at_tok_nextint(&line, &(resp.GW_SignalStrength.signalStrength));
if (err < 0) goto error;
err = at_tok_nextint(&line, &(resp.GW_SignalStrength.bitErrorRate));
if (err < 0) goto error;
at_response_free(p_response);
p_response = NULL;
if (param != NULL)
resp.GW_SignalStrength.signalStrength = *(int*)param;
resp.LTE_SignalStrength.signalStrength = -1;
resp.LTE_SignalStrength.rsrp = -1;
resp.LTE_SignalStrength.rsrq = -1;
resp.LTE_SignalStrength.rssnr = -1;
resp.LTE_SignalStrength.cqi = -1;
RIL_onUnsolicitedResponse (
RIL_UNSOL_SIGNAL_STRENGTH,
& resp, sizeof(resp));
return;
error:
at_response_free(p_response);
}
void _requestSignalStrengthWCDMA(void *data, size_t datalen, RIL_Token t)
{
ATResponse *p_response = NULL;
int err;
RIL_SignalStrength_v6 resp;
char *line;
memset(&resp,0,sizeof(resp));
err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
if (err < 0 || p_response->success == 0) {
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
goto error;
}
line = p_response->p_intermediates->line;
err = at_tok_start(&line);
if (err < 0) goto error;
err = at_tok_nextint(&line, &(resp.GW_SignalStrength.signalStrength));
if (err < 0) goto error;
err = at_tok_nextint(&line, &(resp.GW_SignalStrength.bitErrorRate));
if (err < 0) goto error;
resp.LTE_SignalStrength.signalStrength = -1;
resp.LTE_SignalStrength.rsrp = -1;
resp.LTE_SignalStrength.rsrq = -1;
resp.LTE_SignalStrength.rssnr = -1;
resp.LTE_SignalStrength.cqi = -1;
RIL_onRequestComplete(t, RIL_E_SUCCESS, &resp, sizeof(resp));
at_response_free(p_response);
return;
error:
LOGE("requestSignalStrength must never return an error when radio is on");
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
at_response_free(p_response);
}
/**
* RIL_REQUEST_QUERY_AVAILABLE_NETWORKS
*
* Scans for available networks.
ok
*/
static void requestQueryAvailableNetworks(void *data, size_t datalen, RIL_Token t)
{
/*
* AT+COPS=?
* +COPS: [list of supported (<stat>,long alphanumeric <oper>
* ,short alphanumeric <oper>,numeric <oper>[,<AcT>])s]
* [,,(list of supported <mode>s),(list of supported <format>s)]
*
* <stat>
* 0 = unknown
* 1 = available
* 2 = current
* 3 = forbidden
*/
int err = 0;
ATResponse *atresponse = NULL;
const char *statusTable[] =
{ "unknown", "available", "current", "forbidden" };
char **responseArray = NULL;
char *p;
int n = 0;
int i = 0;
err = at_send_command_multiline("AT+COPS=?", "+COPS:", &atresponse);
if (err < 0 ||
atresponse->success == 0 ||
atresponse->p_intermediates == NULL)
goto error;
p = atresponse->p_intermediates->line;
while (*p != '\0') {
if (*p == '(')
n++;
if(*p == ',' && *(p+1) == ',')
break;
p++;
}
/* Allocate array of strings, blocks of 4 strings. */
responseArray = alloca(n * 4 * sizeof(char *));
p = atresponse->p_intermediates->line;
/* Loop and collect response information into the response array. */
for (i = 0; i < n; i++) {
int status = 0;
char *line = NULL;
char *s = NULL;
char *longAlphaNumeric = NULL;
char *shortAlphaNumeric = NULL;
char *numeric = NULL;
char *remaining = NULL;
s = line = getFirstElementValue(p, "(", ")", &remaining);
p = remaining;
if (line == NULL) {
LOGE("Null pointer while parsing COPS response. This should not happen.");
break;
}
/* <stat> */
err = at_tok_nextint(&line, &status);
if (err < 0)
goto error;
/* long alphanumeric <oper> */
err = at_tok_nextstr(&line, &longAlphaNumeric);
if (err < 0)
goto error;
/* short alphanumeric <oper> */
err = at_tok_nextstr(&line, &shortAlphaNumeric);
if (err < 0)
goto error;
/* numeric <oper> */
err = at_tok_nextstr(&line, &numeric);
if (err < 0)
goto error;
responseArray[i * 4 + 0] = alloca(strlen(longAlphaNumeric) + 1);
strcpy(responseArray[i * 4 + 0], longAlphaNumeric);
responseArray[i * 4 + 1] = alloca(strlen(shortAlphaNumeric) + 1);
strcpy(responseArray[i * 4 + 1], shortAlphaNumeric);
responseArray[i * 4 + 2] = alloca(strlen(numeric) + 1);
strcpy(responseArray[i * 4 + 2], numeric);
free(s);
/*
* Check if modem returned an empty string, and fill it with MNC/MMC
* if that's the case.
*/
if (responseArray[i * 4 + 0] && strlen(responseArray[i * 4 + 0]) == 0) {
responseArray[i * 4 + 0] = alloca(strlen(responseArray[i * 4 + 2])
+ 1);
strcpy(responseArray[i * 4 + 0], responseArray[i * 4 + 2]);
}
if (responseArray[i * 4 + 1] && strlen(responseArray[i * 4 + 1]) == 0) {
responseArray[i * 4 + 1] = alloca(strlen(responseArray[i * 4 + 2])
+ 1);
strcpy(responseArray[i * 4 + 1], responseArray[i * 4 + 2]);
}
responseArray[i * 4 + 3] = alloca(strlen(statusTable[status]) + 1);
sprintf(responseArray[i * 4 + 3], "%s", statusTable[status]);
}
RIL_onRequestComplete(t, RIL_E_SUCCESS, responseArray,
i * 4 * sizeof(char *));
finally:
at_response_free(atresponse);
return;
error:
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
goto finally;
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_QUERY_AVAILABLE_NETWORKS, requestQueryAvailableNetworks)
/**
* RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE
*
* Query current network selectin mode.
ok
*/
void requestQueryNetworkSelectionMode(
void *data, size_t datalen, RIL_Token t)
{
int err;
ATResponse *p_response = NULL;
int response = 0;
char *line;
err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
if (err < 0 || p_response->success == 0) {
goto error;
}
line = p_response->p_intermediates->line;
err = at_tok_start(&line);
if (err < 0) {
goto error;
}
err = at_tok_nextint(&line, &response);
if (err < 0) {
goto error;
}
RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
at_response_free(p_response);
return;
error:
at_response_free(p_response);
LOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE, requestQueryNetworkSelectionMode)
int switch_signal_strength(int rssi,int evdo)
{
int dbm;
if(evdo){//evdo
if(rssi >= 80 ) return 60;
if(rssi == 60) return 75;
if(rssi == 40) return 90;
if(rssi == 20) return 105;
return 125;
}else{//cdma
if(16<=rssi && rssi <= 31 ) return 75;
if(8<=rssi && rssi < 16) return 85;
if(4<=rssi && rssi< 8) return 95;
if(1<=rssi && rssi < 4) return 100;
return 125;
}
}
/**
* RIL_REQUEST_VOICE_REGISTRATION_STATE
*
* Request current registration state
*
* "data" is NULL
* "response" is a "char **"
* ((const char **)response)[0] is registration state 0-6,
* 0 - Not registered, MT is not currently searching
* a new operator to register
* 1 - Registered, home network
* 2 - Not registered, but MT is currently searching
* a new operator to register
* 3 - Registration denied
* 4 - Unknown
* 5 - Registered, roaming
* 10 - Same as 0, but indicates that emergency calls
* are enabled.
* 12 - Same as 2, but indicates that emergency calls
* are enabled.
* 13 - Same as 3, but indicates that emergency calls
* are enabled.
* 14 - Same as 4, but indicates that emergency calls
* are enabled.
*
* ((const char **)response)[1] is LAC if registered on a GSM/WCDMA system or
* NULL if not.Valid LAC are 0x0000 - 0xffff
* ((const char **)response)[2] is CID if registered on a * GSM/WCDMA or
* NULL if not.
* Valid CID are 0x00000000 - 0xffffffff
* In GSM, CID is Cell ID (see TS 27.007)
* in 16 bits
* In UMTS, CID is UMTS Cell Identity
* (see TS 25.331) in 28 bits
* ((const char **)response)[3] indicates the available radio technology 0-7,
* 0 - Unknown, 1 - GPRS, 2 - EDGE, 3 - UMTS,
* 4 - IS95A, 5 - IS95B, 6 - 1xRTT,
* 7 - EvDo Rev. 0, 8 - EvDo Rev. A,
* 9 - HSDPA, 10 - HSUPA, 11 - HSPA,
* 12 - EVDO Rev B
* ((const char **)response)[4] is Base Station ID if registered on a CDMA
* system or NULL if not. Base Station ID in
* decimal format
* ((const char **)response)[5] is Base Station latitude if registered on a
* CDMA system or NULL if not. Base Station
* latitude is a decimal number as specified in
* 3GPP2 C.S0005-A v6.0. It is represented in
* units of 0.25 seconds and ranges from -1296000
* to 1296000, both values inclusive (corresponding
* to a range of -90° to +90°).
* ((const char **)response)[6] is Base Station longitude if registered on a
* CDMA system or NULL if not. Base Station
* longitude is a decimal number as specified in
* 3GPP2 C.S0005-A v6.0. It is represented in
* units of 0.25 seconds and ranges from -2592000
* to 2592000, both values inclusive (corresponding
* to a range of -180° to +180°).
* ((const char **)response)[7] is concurrent services support indicator if
* registered on a CDMA system 0-1.
* 0 - Concurrent services not supported,
* 1 - Concurrent services supported
* ((const char **)response)[8] is System ID if registered on a CDMA system or
* NULL if not. Valid System ID are 0 - 32767
* ((const char **)response)[9] is Network ID if registered on a CDMA system or
* NULL if not. Valid System ID are 0 - 65535
* ((const char **)response)[10] is the TSB-58 Roaming Indicator if registered
* on a CDMA or EVDO system or NULL if not. Valid values
* are 0-255.
* ((const char **)response)[11] indicates whether the current system is in the
* PRL if registered on a CDMA or EVDO system or NULL if
* not. 0=not in the PRL, 1=in the PRL
* ((const char **)response)[12] is the default Roaming Indicator from the PRL,
* if registered on a CDMA or EVDO system or NULL if not.
* Valid values are 0-255.
* ((const char **)response)[13] if registration state is 3 (Registration
* denied) this is an enumerated reason why
* registration was denied. See 3GPP TS 24.008,
* 10.5.3.6 and Annex G.
* 0 - General
* 1 - Authentication Failure
* 2 - IMSI unknown in HLR
* 3 - Illegal MS
* 4 - Illegal ME
* 5 - PLMN not allowed
* 6 - Location area not allowed
* 7 - Roaming not allowed
* 8 - No Suitable Cells in this Location Area
* 9 - Network failure
* 10 - Persistent location update reject
*
* Please note that registration state 4 ("unknown") is treated
* as "out of service" in the Android telephony system
*
* Registration state 3 can be returned if Location Update Reject
* (with cause 17 - Network Failure) is received repeatedly from the network,
* to facilitate "managed roaming"
*
* Valid errors:
* SUCCESS
* RADIO_NOT_AVAILABLE
* GENERIC_FAILURE
*/
void requestVoiceRegistrationState(void *data, size_t datalen, RIL_Token t)
{
_requestRegistrationStateWCDMA(data, datalen, t);
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_VOICE_REGISTRATION_STATE, requestVoiceRegistrationState)
/**
* RIL_REQUEST_OPERATOR
*
* Request current operator ONS or EONS
*
* "data" is NULL
* "response" is a "const char **"
* ((const char **)response)[0] is long alpha ONS or EONS
* or NULL if unregistered
*
* ((const char **)response)[1] is short alpha ONS or EONS
* or NULL if unregistered
* ((const char **)response)[2] is 5 or 6 digit numeric code (MCC + MNC)
* or NULL if unregistered
*
* Valid errors:
* SUCCESS
* RADIO_NOT_AVAILABLE
* GENERIC_FAILURE
*/
void requestOperator(void *data, size_t datalen, RIL_Token t)
{
_requestOperatorWCDMA(data, datalen, t);
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_OPERATOR, requestOperator)
/**
* RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE
*
* Query the preferred network type (CS/PS domain, RAT, and operation mode)
* for searching and registering.
*
* "data" is NULL
*
* "response" is int *
* ((int *)response)[0] is == 0 for GSM/WCDMA (WCDMA preferred)
* ((int *)response)[0] is == 1 for GSM only
* ((int *)response)[0] is == 2 for WCDMA only
* ((int *)response)[0] is == 3 for GSM/WCDMA (auto mode, according to PRL)
* ((int *)response)[0] is == 4 for CDMA and EvDo (auto mode, according to PRL)
* ((int *)response)[0] is == 5 for CDMA only
* ((int *)response)[0] is == 6 for EvDo only
* ((int *)response)[0] is == 7 for GSM/WCDMA, CDMA, and EvDo (auto mode, according to PRL)
*
* Valid errors:
* SUCCESS
* RADIO_NOT_AVAILABLE
* GENERIC_FAILURE
*
* See also: RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE
*/
void requestGetPreferredNetworkType(void *data, size_t datalen,
RIL_Token t)
{
_requestGetPreferredNetworkTypeWCDMA(data, datalen, t);
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE, requestGetPreferredNetworkType)
static int getNetworkType()
{
int err;
ATResponse *p_response = NULL;
int type = 0;
char *line;
err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
if (err < 0 || p_response->success == 0) {
return type;
}
line = p_response->p_intermediates->line;
err = at_tok_start(&line);
if (err < 0) {
goto out;
}
err = at_tok_nextint(&line, &type);
err = at_tok_nextint(&line, &type);
err = at_tok_nextint(&line, &type);
err = at_tok_nextint(&line, &type);
switch(type){
case 0:
case 1:
type = 2;
break;
case 2:
type = 3;
break;
default:
type = 3;
break;
}
out:
at_response_free(p_response);
return type;
}
void reportSignalStrength(void *param);
static int _requestRegistrationStateWCDMASub(void *data,int request, size_t datalen, RIL_Token t)
{
int err;
int response[4]={0,};
char * responseStr[4]={NULL,};
ATResponse *p_response = NULL;
const char *cmd;
const char *prefix;
char *line, *p;
int commas;
int skip;
int count = 3;
int i = 0;
if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
cmd = "AT+CREG?";
prefix = "+CREG:";
} else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
cmd = "AT+CGREG?";
prefix = "+CGREG:";
} else {
assert(0);
goto error;
}
err = at_send_command_singleline(cmd, prefix, &p_response);
if (err != 0 ||p_response->success == 0 ) goto error;
line = p_response->p_intermediates->line;
err = at_tok_start(&line);
if (err < 0) goto error;
/* Ok you have to be careful here
* The solicited version of the CREG response is
* +CREG: n, stat, [lac, cid]
* and the unsolicited version is
* +CREG: stat, [lac, cid]
* The <n> parameter is basically "is unsolicited creg on?"
* which it should always be
*
* Now we should normally get the solicited version here,
* but the unsolicited version could have snuck in
* so we have to handle both
*
* Also since the LAC and CID are only reported when registered,
* we can have 1, 2, 3, or 4 arguments here
*
* finally, a +CGREG: answer may have a fifth value that corresponds
* to the network type, as in;
*
* +CGREG: n, stat [,lac, cid [,networkType]]
*/
/* count number of commas */
commas = 0;
for (p = line ; *p != '\0' ;p++) {
if (*p == ',') commas++;
}
switch (commas) {
case 0: /* +CREG: <stat> */
err = at_tok_nextint(&line, &response[0]);
if (err < 0) goto error;
break;
case 1: /* +CREG: <n>, <stat> */
err = at_tok_nextint(&line, &skip);
if (err < 0) goto error;
err = at_tok_nextint(&line, &response[0]);
if (err < 0) goto error;
break;
case 2: /* +CREG: <stat>, <lac>, <cid> */
err = at_tok_nextint(&line, &response[0]);
if (err < 0) goto error;
err = at_tok_nexthexint(&line, &response[1]);
if (err < 0) goto error;
err = at_tok_nexthexint(&line, &response[2]);
if (err < 0) goto error;
break;
case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
err = at_tok_nextint(&line, &skip);
if (err < 0) goto error;
err = at_tok_nextint(&line, &response[0]);
if (err < 0) goto error;
err = at_tok_nexthexint(&line, &response[1]);
if (err < 0) goto error;
err = at_tok_nexthexint(&line, &response[2]);
if (err < 0) goto error;
break;
/* special case for CGREG, there is a fourth parameter
* that is the network type (unknown/gprs/edge/umts)
*/
case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
err = at_tok_nextint(&line, &skip);
if (err < 0) goto error;
err = at_tok_nextint(&line, &response[0]);
if (err < 0) goto error;
err = at_tok_nexthexint(&line, &response[1]);
if (err < 0) goto error;
err = at_tok_nexthexint(&line, &response[2]);
if (err < 0) goto error;
err = at_tok_nexthexint(&line, &response[3]);
if (err < 0) goto error;
count = 4;
break;
default:
goto error;
}
if(response[0] != 1 && response[0] != 5 && response[0] != 3) goto error;
//if(response[0] == 5) response[0] = 1;
int type = getNetworkType();
asprintf(&responseStr[0], "%d", response[0]);
asprintf(&responseStr[1], "%x", response[1]);
asprintf(&responseStr[2], "%x", response[2]);
asprintf(&responseStr[3], "%d", type);
creg_value=response[0];
RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, 4*sizeof(char*));
at_response_free(p_response);
for(i=0;i<4;i++){
if(responseStr[i]) free(responseStr[i]);
}
return 0;
error:
LOGE("requestRegistrationState must never return an error when radio is on");
RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
at_response_free(p_response);
for(i=0;i<4;i++){
if(responseStr[i]) free(responseStr[i]);
}
return -1;
}
extern SIM_Status getSIMStatus(int falce);
void _requestRegistrationStateWCDMA(void *data, size_t datalen, RIL_Token t)
{
int wait = 20,ret = -1;
int sim_status = getSIMStatus(0);
int is_lock = 0;
if(sim_status == SIM_PIN || sim_status == SIM_PUK){
is_lock = 1;
}else if(sim_status == SIM_ABSENT) wait = 2;
while(wait-- && ret == -1){
ret = _requestRegistrationStateWCDMASub(data,RIL_REQUEST_VOICE_REGISTRATION_STATE,datalen,t);
if(is_lock) break;
LOGD("_requestRegistrationStateWCDMA wait = %d",wait);
if(ret ) sleep(1);
}
}
void requestDataRegistrationState(void *data, size_t datalen, RIL_Token t)
{
int wait = 20,ret = -1;
int sim_status = getSIMStatus(0);
int is_lock = 0;
if(sim_status == SIM_PIN || sim_status == SIM_PUK){
is_lock = 1;
}else if(sim_status == SIM_ABSENT)
wait = 2;
while(wait-- && ret == -1){
ret = _requestRegistrationStateWCDMASub(data,RIL_REQUEST_DATA_REGISTRATION_STATE,datalen,t);
if(is_lock) break;
LOGD("requestGPRSRegistrationState wait = %d",wait);
if(ret ) sleep(1);
}
}
REGISTER_DEFAULT_ITEM(RIL_REQUEST_DATA_REGISTRATION_STATE, requestDataRegistrationState)
void _requestOperatorWCDMA(void *data, size_t datalen, RIL_Token t)
{
int err;
int i;
int skip;
ATLine *p_cur;
char *response[3];
memset(response, 0, sizeof(response));
ATResponse *p_response = NULL;
err = at_send_command_multiline(
"AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
"+COPS:", &p_response);
/* we expect 3 lines here:
* +COPS: 0,0,"T - Mobile"
* +COPS: 0,1,"TMO"
* +COPS: 0,2,"310170"
*/
if (err != 0) goto error;
for (i = 0, p_cur = p_response->p_intermediates
; p_cur != NULL
; p_cur = p_cur->p_next, i++
) {
char *line = p_cur->line;
err = at_tok_start(&line);
if (err < 0) goto error;
err = at_tok_nextint(&line, &skip);
if (err < 0) goto error;
// If we're unregistered, we may just get
// a "+COPS: 0" response