/
ril_plugin.c
2299 lines (1986 loc) · 64.7 KB
/
ril_plugin.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
/*
* oFono - Open Source Telephony - RIL-based devices
*
* Copyright (C) 2015-2019 Jolla Ltd.
* Copyright (C) 2019 Open Mobile Platform LLC.
*
* 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.
*/
#include "ril_plugin.h"
#include "ril_config.h"
#include "ril_sim_card.h"
#include "ril_sim_settings.h"
#include "ril_cell_info.h"
#include "ril_network.h"
#include "ril_radio.h"
#include "ril_radio_caps.h"
#include "ril_data.h"
#include "ril_util.h"
#include "ril_vendor.h"
#include "ril_devmon.h"
#include "ril_log.h"
#include "ofono.h"
#include "sailfish_manager.h"
#include <ofono/storage.h>
#include <ofono/watch.h>
#include <grilio_transport.h>
#include <gutil_ints.h>
#include <gutil_macros.h>
#include <gutil_misc.h>
#include <mce_log.h>
#include <linux/capability.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/plugin.h>
#include <ofono/storage.h>
#include <ofono/ril-transport.h>
#define OFONO_RADIO_ACCESS_MODE_ALL (OFONO_RADIO_ACCESS_MODE_GSM |\
OFONO_RADIO_ACCESS_MODE_UMTS |\
OFONO_RADIO_ACCESS_MODE_LTE)
#define RIL_DEVICE_IDENTITY_RETRIES_LAST 2
#define RIL_SUB_SIZE 4
#define RILMODEM_CONF_FILE "ril_subscription.conf"
#define RILMODEM_DEFAULT_IDENTITY "radio:radio"
#define RILMODEM_DEFAULT_SOCK "/dev/socket/rild"
#define RILMODEM_DEFAULT_SOCK2 "/dev/socket/rild2"
#define RILMODEM_DEFAULT_SUB "SUB1"
#define RILMODEM_DEFAULT_TECHS OFONO_RADIO_ACCESS_MODE_ALL
#define RILMODEM_DEFAULT_LTE_MODE PREF_NET_TYPE_LTE_GSM_WCDMA
#define RILMODEM_DEFAULT_UMTS_MODE PREF_NET_TYPE_GSM_WCDMA_AUTO
#define RILMODEM_DEFAULT_NETWORK_MODE_TIMEOUT (20*1000) /* ms */
#define RILMODEM_DEFAULT_ENABLE_VOICECALL TRUE
#define RILMODEM_DEFAULT_ENABLE_CBS TRUE
#define RILMODEM_DEFAULT_ENABLE_STK TRUE
#define RILMODEM_DEFAULT_SLOT 0xffffffff
#define RILMODEM_DEFAULT_TIMEOUT 0 /* No timeout */
#define RILMODEM_DEFAULT_SIM_FLAGS RIL_SIM_CARD_V9_UICC_SUBSCRIPTION_WORKAROUND
#define RILMODEM_DEFAULT_DATA_OPT RIL_ALLOW_DATA_AUTO
#define RILMODEM_DEFAULT_DM_FLAGS (RIL_DATA_MANAGER_3GLTE_HANDOVER | \
RIL_DATA_MANAGER_FORCE_GSM_ON_OTHER_SLOTS)
#define RILMODEM_DEFAULT_START_TIMEOUT 20000 /* ms */
#define RILMODEM_DEFAULT_DATA_CALL_FORMAT RIL_DATA_CALL_FORMAT_AUTO
#define RILMODEM_DEFAULT_DATA_CALL_RETRY_LIMIT 4
#define RILMODEM_DEFAULT_DATA_CALL_RETRY_DELAY 200 /* ms */
#define RILMODEM_DEFAULT_EMPTY_PIN_QUERY TRUE /* optimistic */
#define RILMODEM_DEFAULT_QUERY_AVAILABLE_BAND_MODE TRUE /* Qualcomm */
#define RILMODEM_DEFAULT_LEGACY_IMEI_QUERY FALSE
#define RILMODEM_DEFAULT_RADIO_POWER_CYCLE TRUE
#define RILMODEM_DEFAULT_CONFIRM_RADIO_POWER_ON TRUE
#define RILMODEM_DEFAULT_NETWORK_SELECTION_MANUAL_0 TRUE
#define RILMODEM_DEFAULT_FORCE_GSM_WHEN_RADIO_OFF TRUE
#define RILMODEM_DEFAULT_USE_DATA_PROFILES FALSE
#define RILMODEM_DEFAULT_MMS_DATA_PROFILE_ID RIL_DATA_PROFILE_IMS
#define RILMODEM_DEFAULT_SLOT_FLAGS SAILFISH_SLOT_NO_FLAGS
/* RIL socket transport name and parameters */
#define RIL_TRANSPORT_MODEM "modem"
#define RIL_TRANSPORT_SOCKET "socket"
#define RIL_TRANSPORT_SOCKET_PATH "path"
#define RIL_TRANSPORT_SOCKET_SUB "sub"
/*
* The convention is that the keys which can only appear in the [Settings]
* section start with the upper case, those which appear in the [ril_*]
* modem section (OR in the [Settings] if they apply to all modems) start
* with lower case.
*/
#define RILCONF_SETTINGS_EMPTY "EmptyConfig"
#define RILCONF_SETTINGS_IDENTITY "Identity"
#define RILCONF_SETTINGS_3GHANDOVER "3GLTEHandover"
#define RILCONF_SETTINGS_GSM_NON_DATA_SLOTS "ForceGsmForNonDataSlots"
#define RILCONF_SETTINGS_SET_RADIO_CAP "SetRadioCapability"
#define RILCONF_MODEM_PREFIX "ril_"
#define RILCONF_PATH_PREFIX "/" RILCONF_MODEM_PREFIX
#define RILCONF_TRANSPORT "transport"
#define RILCONF_NAME "name"
#define RILCONF_SOCKET "socket"
#define RILCONF_SLOT "slot"
#define RILCONF_SUB "sub"
#define RILCONF_START_TIMEOUT "startTimeout"
#define RILCONF_TIMEOUT "timeout"
#define RILCONF_4G "enable4G" /* Deprecated */
#define RILCONF_ENABLE_VOICECALL "enableVoicecall"
#define RILCONF_ENABLE_CBS "enableCellBroadcast"
#define RILCONF_ENABLE_STK "enableSimToolkit"
#define RILCONF_TECHNOLOGIES "technologies"
#define RILCONF_LTE_MODE "lteNetworkMode"
#define RILCONF_UMTS_MODE "umtsNetworkMode"
#define RILCONF_NETWORK_MODE_TIMEOUT "networkModeTimeout"
#define RILCONF_UICC_WORKAROUND "uiccWorkaround"
#define RILCONF_ECCLIST_FILE "ecclistFile"
#define RILCONF_ALLOW_DATA_REQ "allowDataReq"
#define RILCONF_EMPTY_PIN_QUERY "emptyPinQuery"
#define RILCONF_DATA_CALL_FORMAT "dataCallFormat"
#define RILCONF_VENDOR_DRIVER "vendorDriver"
#define RILCONF_DATA_CALL_RETRY_LIMIT "dataCallRetryLimit"
#define RILCONF_DATA_CALL_RETRY_DELAY "dataCallRetryDelay"
#define RILCONF_LOCAL_HANGUP_REASONS "localHangupReasons"
#define RILCONF_REMOTE_HANGUP_REASONS "remoteHangupReasons"
#define RILCONF_LEGACY_IMEI_QUERY "legacyImeiQuery"
#define RILCONF_RADIO_POWER_CYCLE "radioPowerCycle"
#define RILCONF_CONFIRM_RADIO_POWER_ON "confirmRadioPowerOn"
#define RILCONF_SINGLE_DATA_CONTEXT "singleDataContext"
#define RILCONF_NETWORK_SELECTION_MANUAL_0 "networkSelectionManual0"
#define RILCONF_FORCE_GSM_WHEN_RADIO_OFF "forceGsmWhenRadioOff"
#define RILCONF_USE_DATA_PROFILES "useDataProfiles"
#define RILCONF_MMS_DATA_PROFILE_ID "mmsDataProfileId"
#define RILCONF_DEVMON "deviceStateTracking"
/* Modem error ids */
#define RIL_ERROR_ID_RILD_RESTART "rild-restart"
#define RIL_ERROR_ID_CAPS_SWITCH_ABORTED "ril-caps-switch-aborted"
enum ril_plugin_io_events {
IO_EVENT_CONNECTED,
IO_EVENT_ERROR,
IO_EVENT_EOF,
IO_EVENT_RADIO_STATE_CHANGED,
IO_EVENT_COUNT
};
enum ril_plugin_watch_events {
WATCH_EVENT_MODEM,
WATCH_EVENT_COUNT
};
enum ril_set_radio_cap_opt {
RIL_SET_RADIO_CAP_AUTO,
RIL_SET_RADIO_CAP_ENABLED,
RIL_SET_RADIO_CAP_DISABLED
};
enum ril_devmon_opt {
RIL_DEVMON_NONE,
RIL_DEVMON_AUTO,
RIL_DEVMON_SS,
RIL_DEVMON_DS
};
struct ril_plugin_identity {
uid_t uid;
gid_t gid;
};
struct ril_plugin_settings {
int dm_flags;
enum ril_set_radio_cap_opt set_radio_cap;
struct ril_plugin_identity identity;
};
typedef struct sailfish_slot_manager_impl {
struct sailfish_slot_manager *handle;
struct ril_data_manager *data_manager;
struct ril_radio_caps_manager *caps_manager;
struct ril_plugin_settings settings;
gulong caps_manager_event_id;
guint start_timeout_id;
GSList *slots;
} ril_plugin;
typedef struct sailfish_slot_impl {
ril_plugin* plugin;
struct sailfish_slot *handle;
struct sailfish_cell_info *cell_info;
struct ofono_watch *watch;
gulong watch_event_id[WATCH_EVENT_COUNT];
char *path;
char *imei;
char *imeisv;
char *name;
char *transport_name;
GHashTable *transport_params;
char *ecclist_file;
int timeout; /* RIL timeout, in milliseconds */
int index;
int sim_flags;
struct ril_data_options data_opt;
struct ril_slot_config config;
struct ril_modem *modem;
struct ril_radio *radio;
struct ril_radio_caps *caps;
struct ril_network *network;
struct ril_sim_card *sim_card;
struct ril_sim_settings *sim_settings;
struct ril_oem_raw *oem_raw;
const struct ril_vendor_driver *vendor_driver;
struct ril_vendor *vendor;
struct ril_data *data;
gboolean legacy_imei_query;
enum sailfish_slot_flags slot_flags;
guint start_timeout;
guint start_timeout_id;
struct ril_devmon *devmon;
struct ril_devmon_io *devmon_io;
GRilIoChannel *io;
gulong io_event_id[IO_EVENT_COUNT];
gulong sim_card_state_event_id;
gboolean received_sim_status;
guint serialize_id;
guint caps_check_id;
guint imei_req_id;
guint trace_id;
guint dump_id;
guint retry_id;
} ril_slot;
typedef void (*ril_plugin_slot_cb_t)(ril_slot *slot);
typedef void (*ril_plugin_slot_param_cb_t)(ril_slot *slot, void *param);
static void ril_debug_trace_notify(struct ofono_debug_desc *desc);
static void ril_debug_dump_notify(struct ofono_debug_desc *desc);
static void ril_debug_grilio_notify(struct ofono_debug_desc *desc);
static void ril_debug_mce_notify(struct ofono_debug_desc *desc);
static void ril_plugin_debug_notify(struct ofono_debug_desc *desc);
static void ril_plugin_drop_orphan_slots(ril_plugin *plugin);
static void ril_plugin_retry_init_io(ril_slot *slot);
static void ril_plugin_check_modem(ril_slot *slot);
GLOG_MODULE_DEFINE("rilmodem");
static const char ril_debug_trace_name[] = "ril_trace";
static GLogModule ril_debug_trace_module = {
.name = ril_debug_trace_name,
.max_level = GLOG_LEVEL_VERBOSE,
.level = GLOG_LEVEL_VERBOSE,
.flags = GLOG_FLAG_HIDE_NAME
};
static struct ofono_debug_desc ril_debug_trace OFONO_DEBUG_ATTR = {
.name = ril_debug_trace_name,
.flags = OFONO_DEBUG_FLAG_DEFAULT | OFONO_DEBUG_FLAG_HIDE_NAME,
.notify = ril_debug_trace_notify
};
static struct ofono_debug_desc ril_debug_dump OFONO_DEBUG_ATTR = {
.name = "ril_dump",
.flags = OFONO_DEBUG_FLAG_DEFAULT | OFONO_DEBUG_FLAG_HIDE_NAME,
.notify = ril_debug_dump_notify
};
static struct ofono_debug_desc grilio_debug OFONO_DEBUG_ATTR = {
.name = "grilio",
.flags = OFONO_DEBUG_FLAG_DEFAULT,
.notify = ril_debug_grilio_notify
};
static struct ofono_debug_desc mce_debug OFONO_DEBUG_ATTR = {
.name = "mce",
.flags = OFONO_DEBUG_FLAG_DEFAULT,
.notify = ril_debug_mce_notify
};
static struct ofono_debug_desc ril_plugin_debug OFONO_DEBUG_ATTR = {
.name = "ril_plugin",
.flags = OFONO_DEBUG_FLAG_DEFAULT,
.notify = ril_plugin_debug_notify
};
static inline const char *ril_slot_debug_prefix(const ril_slot *slot)
{
/* slot->path always starts with a slash, skip it */
return slot->path + 1;
}
static gboolean ril_plugin_multisim(ril_plugin *plugin)
{
return plugin->slots && plugin->slots->next;
}
static void ril_plugin_foreach_slot_param(ril_plugin *plugin,
ril_plugin_slot_param_cb_t fn, void *param)
{
GSList *l = plugin->slots;
while (l) {
GSList *next = l->next;
fn((ril_slot *)l->data, param);
l = next;
}
}
static void ril_plugin_foreach_slot_proc(gpointer data, gpointer user_data)
{
((ril_plugin_slot_cb_t)user_data)(data);
}
static void ril_plugin_foreach_slot(ril_plugin *plugin, ril_plugin_slot_cb_t fn)
{
g_slist_foreach(plugin->slots, ril_plugin_foreach_slot_proc, fn);
}
static void ril_plugin_foreach_slot_manager_proc(ril_plugin *plugin, void *data)
{
ril_plugin_foreach_slot(plugin, (ril_plugin_slot_cb_t)data);
}
static void ril_plugin_foreach_slot_manager(struct sailfish_slot_driver_reg *r,
ril_plugin_slot_cb_t fn)
{
sailfish_manager_foreach_slot_manager(r,
ril_plugin_foreach_slot_manager_proc, fn);
}
static void ril_plugin_remove_slot_handler(ril_slot *slot, int id)
{
GASSERT(id >= 0 && id<IO_EVENT_COUNT);
if (slot->io_event_id[id]) {
grilio_channel_remove_handler(slot->io, slot->io_event_id[id]);
slot->io_event_id[id] = 0;
}
}
static void ril_plugin_shutdown_slot(ril_slot *slot, gboolean kill_io)
{
if (slot->modem) {
ril_modem_delete(slot->modem);
/* The above call is expected to result in
* ril_plugin_modem_removed getting called
* which will set slot->modem to NULL */
GASSERT(!slot->modem);
}
if (kill_io) {
if (slot->retry_id) {
g_source_remove(slot->retry_id);
slot->retry_id = 0;
}
if (slot->devmon_io) {
ril_devmon_io_free(slot->devmon_io);
slot->devmon_io = NULL;
}
if (slot->cell_info) {
sailfish_cell_info_unref(slot->cell_info);
slot->cell_info = NULL;
}
if (slot->caps) {
ril_radio_caps_unref(slot->caps);
slot->caps = NULL;
}
if (slot->data) {
ril_data_allow(slot->data, RIL_DATA_ROLE_NONE);
ril_data_unref(slot->data);
slot->data = NULL;
}
if (slot->radio) {
ril_radio_unref(slot->radio);
slot->radio = NULL;
}
if (slot->network) {
ril_network_unref(slot->network);
slot->network = NULL;
}
if (slot->sim_card) {
ril_sim_card_remove_handler(slot->sim_card,
slot->sim_card_state_event_id);
ril_sim_card_unref(slot->sim_card);
slot->sim_card_state_event_id = 0;
slot->sim_card = NULL;
slot->received_sim_status = FALSE;
}
if (slot->vendor) {
ril_vendor_unref(slot->vendor);
slot->vendor = NULL;
}
if (slot->io) {
int i;
grilio_channel_remove_logger(slot->io, slot->trace_id);
grilio_channel_remove_logger(slot->io, slot->dump_id);
slot->trace_id = 0;
slot->dump_id = 0;
if (slot->caps_check_id) {
grilio_channel_cancel_request(slot->io,
slot->caps_check_id, FALSE);
slot->caps_check_id = 0;
}
if (slot->imei_req_id) {
grilio_channel_cancel_request(slot->io,
slot->imei_req_id, FALSE);
slot->imei_req_id = 0;
}
if (slot->serialize_id) {
grilio_channel_deserialize(slot->io,
slot->serialize_id);
slot->serialize_id = 0;
}
for (i=0; i<IO_EVENT_COUNT; i++) {
ril_plugin_remove_slot_handler(slot, i);
}
grilio_channel_shutdown(slot->io, FALSE);
grilio_channel_unref(slot->io);
slot->io = NULL;
}
}
}
static void ril_plugin_check_ready(ril_slot *slot)
{
if (slot->serialize_id && slot->imei && slot->sim_card &&
slot->sim_card->status) {
grilio_channel_deserialize(slot->io, slot->serialize_id);
slot->serialize_id = 0;
}
}
static void ril_plugin_get_imeisv_cb(GRilIoChannel *io, int status,
const void *data, guint len, void *user_data)
{
ril_slot *slot = user_data;
char *imeisv = NULL;
GASSERT(slot->imei_req_id);
slot->imei_req_id = 0;
if (status == RIL_E_SUCCESS) {
GRilIoParser rilp;
grilio_parser_init(&rilp, data, len);
imeisv = grilio_parser_get_utf8(&rilp);
DBG("%s", imeisv);
/*
* slot->imei should be either NULL (when we get connected
* to rild the very first time) or match the already known
* IMEI (if rild crashed and we have reconnected)
*/
if (slot->imeisv && imeisv && strcmp(slot->imeisv, imeisv)) {
ofono_warn("IMEISV has changed \"%s\" -> \"%s\"",
slot->imeisv, imeisv);
}
} else {
ofono_error("Slot %u IMEISV query error: %s",
slot->config.slot, ril_error_to_string(status));
}
if (slot->imeisv) {
/* We assume that IMEISV never changes */
g_free(imeisv);
} else {
slot->imeisv = (imeisv ? imeisv : g_strdup(""));
sailfish_manager_imeisv_obtained(slot->handle, slot->imeisv);
}
ril_plugin_check_modem(slot);
}
static void ril_plugin_get_imei_cb(GRilIoChannel *io, int status,
const void *data, guint len, void *user_data)
{
ril_slot *slot = user_data;
char *imei = NULL;
GASSERT(slot->imei_req_id);
slot->imei_req_id = 0;
if (status == RIL_E_SUCCESS) {
GRilIoParser rilp;
grilio_parser_init(&rilp, data, len);
imei = grilio_parser_get_utf8(&rilp);
DBG("%s", imei);
/*
* slot->imei should be either NULL (when we get connected
* to rild the very first time) or match the already known
* IMEI (if rild crashed and we have reconnected)
*/
if (slot->imei && imei && strcmp(slot->imei, imei)) {
ofono_warn("IMEI has changed \"%s\" -> \"%s\"",
slot->imei, imei);
}
if (imei) {
/* IMEI query was successful, fetch IMEISV too */
GRilIoRequest *req = grilio_request_new();
slot->imei_req_id =
grilio_channel_send_request_full(slot->io,
req, RIL_REQUEST_GET_IMEISV,
ril_plugin_get_imeisv_cb, NULL, slot);
grilio_request_unref(req);
}
} else {
ofono_error("Slot %u IMEI query error: %s", slot->config.slot,
ril_error_to_string(status));
}
if (slot->imei) {
/* We assume that IMEI never changes */
g_free(imei);
} else {
slot->imei = imei ? imei : g_strdup_printf("%d", slot->index);
sailfish_manager_imei_obtained(slot->handle, slot->imei);
}
ril_plugin_check_modem(slot);
ril_plugin_check_ready(slot);
}
static void ril_plugin_device_identity_cb(GRilIoChannel *io, int status,
const void *data, guint len, void *user_data)
{
ril_slot *slot = user_data;
char *imei = NULL;
char *imeisv = NULL;
GASSERT(slot->imei_req_id);
slot->imei_req_id = 0;
if (status == RIL_E_SUCCESS) {
GRilIoParser rilp;
guint32 n;
/*
* RIL_REQUEST_DEVICE_IDENTITY
*
* "response" is const char **
* ((const char **)response)[0] is IMEI (for GSM)
* ((const char **)response)[1] is IMEISV (for GSM)
* ((const char **)response)[2] is ESN (for CDMA)
* ((const char **)response)[3] is MEID (for CDMA)
*/
grilio_parser_init(&rilp, data, len);
if (grilio_parser_get_uint32(&rilp, &n) && n >= 2) {
imei = grilio_parser_get_utf8(&rilp);
imeisv = grilio_parser_get_utf8(&rilp);
DBG("%s %s", imei, imeisv);
} else {
DBG("parsing failure!");
}
/*
* slot->imei should be either NULL (when we get connected
* to rild the very first time) or match the already known
* IMEI (if rild crashed and we have reconnected)
*/
if (slot->imei && imei && strcmp(slot->imei, imei)) {
ofono_warn("IMEI has changed \"%s\" -> \"%s\"",
slot->imei, imei);
}
} else {
ofono_error("Slot %u IMEI query error: %s", slot->config.slot,
ril_error_to_string(status));
}
if (slot->imei) {
/* We assume that IMEI never changes */
g_free(imei);
} else {
slot->imei = imei ? imei : g_strdup_printf("%d", slot->index);
sailfish_manager_imei_obtained(slot->handle, slot->imei);
}
if (slot->imeisv) {
g_free(imeisv);
} else {
slot->imeisv = (imeisv ? imeisv : g_strdup(""));
sailfish_manager_imeisv_obtained(slot->handle, slot->imeisv);
}
ril_plugin_check_modem(slot);
ril_plugin_check_ready(slot);
}
static void ril_plugin_start_imei_query(ril_slot *slot, gboolean blocking,
int retries)
{
GRilIoRequest *req = grilio_request_new();
/* There was a bug in libgrilio which was making request blocking
* regardless of what we pass to grilio_request_set_blocking(),
* that's why we don't call grilio_request_set_blocking() if
* blocking is FALSE */
if (blocking) grilio_request_set_blocking(req, TRUE);
grilio_request_set_retry(req, RIL_RETRY_MS, retries);
grilio_channel_cancel_request(slot->io, slot->imei_req_id, FALSE);
slot->imei_req_id = (slot->legacy_imei_query ?
grilio_channel_send_request_full(slot->io, req,
RIL_REQUEST_GET_IMEI,
ril_plugin_get_imei_cb, NULL, slot) :
grilio_channel_send_request_full(slot->io, req,
RIL_REQUEST_DEVICE_IDENTITY,
ril_plugin_device_identity_cb, NULL, slot));
grilio_request_unref(req);
}
static enum sailfish_sim_state ril_plugin_sim_state(ril_slot *slot)
{
const struct ril_sim_card_status *status = slot->sim_card->status;
if (status) {
switch (status->card_state) {
case RIL_CARDSTATE_PRESENT:
return SAILFISH_SIM_STATE_PRESENT;
case RIL_CARDSTATE_ABSENT:
return SAILFISH_SIM_STATE_ABSENT;
case RIL_CARDSTATE_ERROR:
return SAILFISH_SIM_STATE_ERROR;
default:
break;
}
}
return SAILFISH_SIM_STATE_UNKNOWN;
}
static void ril_plugin_sim_state_changed(struct ril_sim_card *card, void *data)
{
ril_slot *slot = data;
const enum sailfish_sim_state sim_state = ril_plugin_sim_state(slot);
if (card->status) {
switch (sim_state) {
case SAILFISH_SIM_STATE_PRESENT:
DBG("SIM found in slot %u", slot->config.slot);
break;
case SAILFISH_SIM_STATE_ABSENT:
DBG("No SIM in slot %u", slot->config.slot);
break;
default:
break;
}
if (!slot->received_sim_status && slot->imei_req_id) {
/*
* We have received the SIM status but haven't yet
* got IMEI from the modem. Some RILs behave this
* way if the modem doesn't have IMEI initialized
* yet. Cancel the current request (with unlimited
* number of retries) and give a few more tries
* (this time, limited number).
*
* Some RILs fail RIL_REQUEST_DEVICE_IDENTITY until
* the modem has been properly initialized.
*/
DBG("Giving slot %u last chance", slot->config.slot);
ril_plugin_start_imei_query(slot, FALSE,
RIL_DEVICE_IDENTITY_RETRIES_LAST);
}
slot->received_sim_status = TRUE;
}
sailfish_manager_set_sim_state(slot->handle, sim_state);
ril_plugin_check_ready(slot);
}
static void ril_plugin_handle_error(ril_slot *slot, const char *message)
{
ofono_error("%s %s", ril_slot_debug_prefix(slot), message);
sailfish_manager_slot_error(slot->handle, RIL_ERROR_ID_RILD_RESTART,
message);
ril_plugin_shutdown_slot(slot, TRUE);
ril_plugin_retry_init_io(slot);
}
static void ril_plugin_slot_error(GRilIoChannel *io, const GError *error,
void *data)
{
ril_plugin_handle_error((ril_slot *)data, GERRMSG(error));
}
static void ril_plugin_slot_disconnected(GRilIoChannel *io, void *data)
{
ril_plugin_handle_error((ril_slot *)data, "disconnected");
}
static void ril_plugin_caps_switch_aborted(struct ril_radio_caps_manager *mgr,
void *data)
{
ril_plugin *plugin = data;
DBG("radio caps switch aborted");
sailfish_manager_error(plugin->handle,
RIL_ERROR_ID_CAPS_SWITCH_ABORTED,
"Capability switch transaction aborted");
}
static void ril_plugin_trace(GRilIoChannel *io, GRILIO_PACKET_TYPE type,
guint id, guint code, const void *data, guint data_len, void *user_data)
{
ril_slot *slot = user_data;
struct ril_vendor *vendor = slot->vendor;
static const GLogModule* log_module = &ril_debug_trace_module;
const char *prefix = io->name ? io->name : "";
const char dir = (type == GRILIO_PACKET_REQ) ? '<' : '>';
const char *scode = NULL;
switch (type) {
case GRILIO_PACKET_REQ:
if (io->ril_version <= 9 &&
code == RIL_REQUEST_V9_SET_UICC_SUBSCRIPTION) {
scode = "V9_SET_UICC_SUBSCRIPTION";
} else {
scode = ril_vendor_request_to_string(vendor, code);
if (!scode) {
/* Not a vendor specific request */
scode = ril_request_to_string(code);
}
}
gutil_log(log_module, GLOG_LEVEL_VERBOSE, "%s%c [%08x] %s",
prefix, dir, id, scode);
break;
case GRILIO_PACKET_ACK:
gutil_log(log_module, GLOG_LEVEL_VERBOSE, "%s%c [%08x] ACK",
prefix, dir, id);
break;
case GRILIO_PACKET_RESP:
case GRILIO_PACKET_RESP_ACK_EXP:
gutil_log(log_module, GLOG_LEVEL_VERBOSE, "%s%c [%08x] %s",
prefix, dir, id, ril_error_to_string(code));
break;
case GRILIO_PACKET_UNSOL:
case GRILIO_PACKET_UNSOL_ACK_EXP:
scode = ril_vendor_event_to_string(vendor, code);
if (!scode) {
/* Not a vendor specific event */
scode = ril_unsol_event_to_string(code);
}
gutil_log(log_module, GLOG_LEVEL_VERBOSE, "%s%c %s",
prefix, dir, scode);
break;
}
}
static void ril_debug_dump_update(ril_slot *slot)
{
if (slot->io) {
if (ril_debug_dump.flags & OFONO_DEBUG_FLAG_PRINT) {
if (!slot->dump_id) {
slot->dump_id =
grilio_channel_add_default_logger(
slot->io, GLOG_LEVEL_VERBOSE);
}
} else if (slot->dump_id) {
grilio_channel_remove_logger(slot->io, slot->dump_id);
slot->dump_id = 0;
}
}
}
static void ril_debug_trace_update(ril_slot *slot)
{
if (slot->io) {
if (ril_debug_trace.flags & OFONO_DEBUG_FLAG_PRINT) {
if (!slot->trace_id) {
slot->trace_id =
grilio_channel_add_logger(slot->io,
ril_plugin_trace, slot);
/*
* Loggers are invoked in the order they have
* been registered. Make sure that dump logger
* is invoked after ril_plugin_trace.
*/
if (slot->dump_id) {
grilio_channel_remove_logger(slot->io,
slot->dump_id);
slot->dump_id = 0;
}
ril_debug_dump_update(slot);
}
} else if (slot->trace_id) {
grilio_channel_remove_logger(slot->io, slot->trace_id);
slot->trace_id = 0;
}
}
}
static const char *ril_plugin_log_prefix(ril_slot *slot)
{
return ril_plugin_multisim(slot->plugin) ?
ril_slot_debug_prefix(slot) : "";
}
static void ril_plugin_create_modem(ril_slot *slot)
{
struct ril_modem *modem;
const char *log_prefix = ril_plugin_log_prefix(slot);
DBG("%s", ril_slot_debug_prefix(slot));
GASSERT(slot->io && slot->io->connected);
GASSERT(!slot->modem);
modem = ril_modem_create(slot->io, log_prefix, slot->path, slot->imei,
slot->imeisv, slot->ecclist_file, &slot->config, slot->radio,
slot->network, slot->sim_card, slot->data, slot->sim_settings,
slot->vendor, slot->cell_info);
if (modem) {
slot->modem = modem;
slot->oem_raw = ril_oem_raw_new(modem, log_prefix);
} else {
ril_plugin_shutdown_slot(slot, TRUE);
}
}
static void ril_plugin_check_modem(ril_slot *slot)
{
if (!slot->modem && slot->handle->enabled &&
slot->io && slot->io->connected &&
!slot->imei_req_id && slot->imei) {
ril_plugin_create_modem(slot);
}
}
/*
* It seems to be necessary to kick (with RIL_REQUEST_RADIO_POWER) the
* modems with power on after one of the modems has been powered off.
* Otherwise bad things may happen (like the modem never registering
* on the network).
*/
static void ril_plugin_power_check(ril_slot *slot)
{
ril_radio_confirm_power_on(slot->radio);
}
static void ril_plugin_radio_state_changed(GRilIoChannel *io, guint code,
const void *data, guint len, void *user_data)
{
if (ril_radio_state_parse(data, len) == RADIO_STATE_OFF) {
ril_slot *slot = user_data;
DBG("power off for slot %u", slot->config.slot);
ril_plugin_foreach_slot(slot->plugin, ril_plugin_power_check);
}
}
static void ril_plugin_radio_caps_cb(const struct ril_radio_capability *cap,
void *user_data)
{
ril_slot *slot = user_data;
DBG("radio caps %s", cap ? "ok" : "NOT supported");
GASSERT(slot->caps_check_id);
slot->caps_check_id = 0;
if (cap) {
ril_plugin *plugin = slot->plugin;
if (!plugin->caps_manager) {
plugin->caps_manager = ril_radio_caps_manager_new
(plugin->data_manager);
plugin->caps_manager_event_id =
ril_radio_caps_manager_add_aborted_handler(
plugin->caps_manager,
ril_plugin_caps_switch_aborted,
plugin);
}
GASSERT(!slot->caps);
slot->caps = ril_radio_caps_new(plugin->caps_manager,
ril_plugin_log_prefix(slot), slot->io, slot->data,
slot->radio, slot->sim_card, slot->network,
&slot->config, cap);
}
}
static void ril_plugin_manager_started(ril_plugin *plugin)
{
ril_plugin_drop_orphan_slots(plugin);
ril_data_manager_check_data(plugin->data_manager);
sailfish_slot_manager_started(plugin->handle);
}
static void ril_plugin_all_slots_started_cb(ril_slot *slot, void *param)
{
if (!slot->handle) {
(*((gboolean*)param)) = FALSE; /* Not all */
}
}
static void ril_plugin_check_if_started(ril_plugin* plugin)
{
if (plugin->start_timeout_id) {
gboolean all = TRUE;
ril_plugin_foreach_slot_param(plugin,
ril_plugin_all_slots_started_cb, &all);
if (all) {
DBG("Startup done!");
g_source_remove(plugin->start_timeout_id);
/* id is zeroed by ril_plugin_manager_start_done */
GASSERT(!plugin->start_timeout_id);
ril_plugin_manager_started(plugin);
}
}
}
static void ril_plugin_slot_connected(ril_slot *slot)
{
ril_plugin *plugin = slot->plugin;
const struct ril_plugin_settings *ps = &plugin->settings;
const char *log_prefix = ril_plugin_log_prefix(slot);
ofono_debug("%s version %u", (slot->name && slot->name[0]) ?
slot->name : "RIL", slot->io->ril_version);
GASSERT(slot->io->connected);
GASSERT(!slot->io_event_id[IO_EVENT_CONNECTED]);
/*
* Modem will be registered after RIL_REQUEST_DEVICE_IDENTITY
* successfully completes. By the time ofono starts, rild may
* not be completely functional. Waiting until it responds to
* RIL_REQUEST_DEVICE_IDENTITY (or RIL_REQUEST_GET_IMEI/SV)
* and retrying the request on failure, (hopefully) gives rild
* enough time to finish whatever it's doing during initialization.
*/
ril_plugin_start_imei_query(slot, TRUE, -1);
GASSERT(!slot->radio);
slot->radio = ril_radio_new(slot->io);
GASSERT(!slot->io_event_id[IO_EVENT_RADIO_STATE_CHANGED]);
if (slot->config.confirm_radio_power_on) {
slot->io_event_id[IO_EVENT_RADIO_STATE_CHANGED] =
grilio_channel_add_unsol_event_handler(slot->io,
ril_plugin_radio_state_changed,
RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED, slot);
}
GASSERT(!slot->sim_card);
slot->sim_card = ril_sim_card_new(slot->io, slot->config.slot,
slot->sim_flags);
slot->sim_card_state_event_id = ril_sim_card_add_state_changed_handler(
slot->sim_card, ril_plugin_sim_state_changed, slot);
/* ril_sim_card is expected to perform RIL_REQUEST_GET_SIM_STATUS
* asynchronously and report back when request has completed: */
GASSERT(!slot->sim_card->status);
GASSERT(!slot->received_sim_status);
GASSERT(!slot->vendor);
slot->vendor = ril_vendor_create(slot->vendor_driver, slot->io,
slot->path, &slot->config);
GASSERT(!slot->network);
slot->network = ril_network_new(slot->path, slot->io, log_prefix,
slot->radio, slot->sim_card, slot->sim_settings,
&slot->config, slot->vendor);
GASSERT(!slot->data);
slot->data = ril_data_new(plugin->data_manager, log_prefix,
slot->radio, slot->network, slot->io, &slot->data_opt,