/
sailfish_manager.c
1529 lines (1326 loc) · 40.2 KB
/
sailfish_manager.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
*
* Copyright (C) 2017-2020 Jolla Ltd.
* Copyright (C) 2019-2020 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gutil_log.h>
#include <gutil_strv.h>
#include <gutil_macros.h>
#include <string.h>
#include <ofono/storage.h>
#include <ofono/watch.h>
#include "src/ofono.h"
#include "src/storage.h"
#include <sailfish_manager.h>
#include <sailfish_cell_info.h>
#include "sailfish_manager_dbus.h"
#include "sailfish_cell_info_dbus.h"
#include "sailfish_sim_info.h"
/* How long we wait for all drivers to register (number of idle loops) */
#define SF_INIT_IDLE_COUNT (5)
enum ofono_watch_events {
WATCH_EVENT_MODEM,
WATCH_EVENT_ONLINE,
WATCH_EVENT_IMSI,
WATCH_EVENT_COUNT
};
enum sim_auto_select {
SIM_AUTO_SELECT_OFF,
SIM_AUTO_SELECT_ON,
SIM_AUTO_SELECT_ONCE
};
struct sailfish_manager_priv {
struct sailfish_manager pub; /* Public part */
struct sailfish_slot_driver_reg *drivers;
struct sailfish_manager_dbus *dbus;
struct sailfish_slot_priv *voice_slot;
struct sailfish_slot_priv *data_slot;
struct sailfish_slot_priv *mms_slot;
sailfish_slot_ptr *slots;
enum sim_auto_select auto_data_sim;
gboolean auto_data_sim_done;
int slot_count;
guint init_countdown;
guint init_id;
char *default_voice_imsi;
char *default_data_imsi;
char *mms_imsi;
GKeyFile *storage;
GHashTable *errors;
};
struct sailfish_slot_driver_reg {
struct sailfish_slot_driver_reg *next;
const struct sailfish_slot_driver *driver;
struct sailfish_manager_priv *plugin;
struct sailfish_slot_manager *manager;
guint init_id;
};
struct sailfish_slot_manager {
const struct sailfish_slot_driver *driver;
struct sailfish_manager_priv *plugin;
struct sailfish_slot_manager_impl *impl;
struct sailfish_slot_priv *slots;
gboolean started;
guint start_id;
};
struct sailfish_slot_priv {
struct sailfish_slot pub;
struct sailfish_slot_priv *next;
struct sailfish_slot_manager *manager;
struct sailfish_slot_impl *impl;
struct ofono_watch *watch;
struct sailfish_sim_info *siminfo;
struct sailfish_sim_info_dbus *siminfo_dbus;
struct sailfish_cell_info *cellinfo;
struct sailfish_cell_info_dbus *cellinfo_dbus;
enum sailfish_sim_state sim_state;
enum sailfish_slot_flags flags;
gulong watch_event_id[WATCH_EVENT_COUNT];
char *imei;
char *imeisv;
gboolean enabled_changed;
GHashTable *errors;
int index;
};
/* Read-only config */
#define SF_CONFIG_FILE "main.conf"
#define SF_CONFIG_GROUP "ModemManager"
#define SF_CONFIG_KEY_AUTO_DATA_SIM "AutoSelectDataSim"
/* "ril" is used for historical reasons */
#define SF_STORE "ril"
#define SF_STORE_GROUP "Settings"
#define SF_STORE_ENABLED_SLOTS "EnabledSlots"
#define SF_STORE_DEFAULT_VOICE_SIM "DefaultVoiceSim"
#define SF_STORE_DEFAULT_DATA_SIM "DefaultDataSim"
#define SF_STORE_SLOTS_SEP ","
#define SF_STORE_AUTO_DATA_SIM_DONE "AutoSelectDataSimDone"
/* The file where error statistics is stored. Again "rilerror" is historical */
#define SF_ERROR_STORAGE "rilerror" /* File name */
#define SF_ERROR_COMMON_SECTION "common" /* Modem independent section */
/* Path always starts with a slash, skip it */
#define sailfish_slot_debug_prefix(s) ((s)->pub.path + 1)
static int sailfish_manager_update_modem_paths(struct sailfish_manager_priv *);
static gboolean sailfish_manager_update_ready(struct sailfish_manager_priv *p);
static inline struct sailfish_manager_priv *sailfish_manager_priv_cast
(struct sailfish_manager *m)
{
return G_CAST(m, struct sailfish_manager_priv, pub);
}
static inline struct sailfish_slot_priv *sailfish_slot_priv_cast
(struct sailfish_slot *s)
{
return G_CAST(s, struct sailfish_slot_priv, pub);
}
static inline const struct sailfish_slot_priv *sailfish_slot_priv_cast_const
(const struct sailfish_slot *s)
{
return G_CAST(s, struct sailfish_slot_priv, pub);
}
static inline void sailfish_slot_set_data_role(struct sailfish_slot_priv *s,
enum sailfish_data_role role)
{
const struct sailfish_slot_driver *d = s->manager->driver;
if (d->slot_set_data_role) {
d->slot_set_data_role(s->impl, role);
}
}
static gboolean sailfish_config_get_enum(GKeyFile *file, const char *group,
const char *key, int *result,
const char *name, int value, ...)
{
char *str = g_key_file_get_string(file, group, key, NULL);
if (str) {
/*
* Some people are thinking that # is a comment
* anywhere on the line, not just at the beginning
*/
char *comment = strchr(str, '#');
if (comment) *comment = 0;
g_strstrip(str);
if (strcasecmp(str, name)) {
va_list args;
va_start(args, value);
while ((name = va_arg(args, char*)) != NULL) {
value = va_arg(args, int);
if (!strcasecmp(str, name)) {
break;
}
}
va_end(args);
}
if (!name) {
ofono_error("Invalid %s config value (%s)", key, str);
}
g_free(str);
if (name) {
if (result) {
*result = value;
}
return TRUE;
}
}
return FALSE;
}
/* Update modem paths and emit D-Bus signal if necessary */
static void sailfish_manager_update_modem_paths_full
(struct sailfish_manager_priv *p)
{
sailfish_manager_dbus_signal(p->dbus,
sailfish_manager_update_modem_paths(p));
}
/*
* sailfish_manager_foreach_driver() and sailfish_manager_foreach_slot()
* terminate the loop and return TRUE if the callback returns TRUE. If all
* callbacks return FALSE, they returns FALSE. It there are no drivers/slots,
* they return FALSE too.
*/
#define SF_LOOP_CONTINUE (FALSE)
#define SF_LOOP_DONE (TRUE)
static gboolean sailfish_manager_foreach_driver(struct sailfish_manager_priv *p,
gboolean (*fn)(struct sailfish_slot_driver_reg *r, void *user_data),
void *user_data)
{
struct sailfish_slot_driver_reg *r = p->drivers;
gboolean done = FALSE;
while (r && !done) {
struct sailfish_slot_driver_reg *rnext = r->next;
/* The callback returns TRUE to terminate the loop */
done = fn(r, user_data);
r = rnext;
}
return done;
}
static gboolean sailfish_manager_foreach_slot
(struct sailfish_manager_priv *p,
gboolean (*fn)(struct sailfish_slot_priv *s, void *user_data),
void *user_data)
{
struct sailfish_slot_driver_reg *r = p->drivers;
gboolean done = FALSE;
while (r && !done) {
struct sailfish_slot_manager *m = r->manager;
struct sailfish_slot_driver_reg *rnext = r->next;
if (m) {
struct sailfish_slot_priv *s = m->slots;
while (s) {
struct sailfish_slot_priv *snext = s->next;
/* The callback returns TRUE to terminate
* the loop */
if (fn(s, user_data)) {
done = TRUE;
break;
}
s = snext;
}
}
r = rnext;
}
return done;
}
static void sailfish_manager_slot_update_cell_info_dbus
(struct sailfish_slot_priv *s)
{
struct ofono_modem *modem = s->watch->modem;
if (modem && s->cellinfo) {
if (!s->cellinfo_dbus) {
s->cellinfo_dbus = sailfish_cell_info_dbus_new(modem,
s->cellinfo);
}
} else {
if (s->cellinfo_dbus) {
sailfish_cell_info_dbus_free(s->cellinfo_dbus);
s->cellinfo_dbus = NULL;
}
}
}
static void sailfish_manager_slot_modem_changed(struct ofono_watch *w,
void *user_data)
{
struct sailfish_slot_priv *s = user_data;
struct sailfish_manager_priv *p = s->manager->plugin;
sailfish_manager_slot_update_cell_info_dbus(s);
sailfish_manager_update_modem_paths_full(p);
sailfish_manager_update_ready(p);
}
static void sailfish_manager_slot_imsi_changed(struct ofono_watch *w,
void *user_data)
{
struct sailfish_slot_priv *slot = user_data;
struct sailfish_manager_priv *plugin = slot->manager->plugin;
struct sailfish_slot_priv *voice_slot = plugin->voice_slot;
struct sailfish_slot_priv *data_slot = plugin->data_slot;
int signal_mask;
/*
* We want the first slot to be selected by default.
* However, things may become available in pretty much
* any order, so reset the slot pointers to NULL and let
* sailfish_manager_update_modem_paths() to pick them again.
*/
plugin->voice_slot = NULL;
plugin->data_slot = NULL;
plugin->pub.default_voice_path = NULL;
plugin->pub.default_data_path = NULL;
signal_mask = sailfish_manager_update_modem_paths(plugin);
if (voice_slot != plugin->voice_slot) {
if (!plugin->voice_slot) {
DBG("No default voice SIM");
}
signal_mask |= SAILFISH_MANAGER_SIGNAL_VOICE_PATH;
}
if (data_slot != plugin->data_slot) {
if (!plugin->data_slot) {
DBG("No default data SIM");
}
signal_mask |= SAILFISH_MANAGER_SIGNAL_DATA_PATH;
}
sailfish_manager_dbus_signal(plugin->dbus, signal_mask);
}
static gboolean sailfish_manager_count_slot(struct sailfish_slot_priv *s,
void *user_data)
{
(*((int *)user_data))++;
return SF_LOOP_CONTINUE;
}
static gboolean sailfish_manager_index_slot(struct sailfish_slot_priv *s,
void *user_data)
{
struct sailfish_manager_priv *p = user_data;
s->index = p->slot_count;
p->slots[p->slot_count++] = &s->pub;
return SF_LOOP_CONTINUE;
}
static void sailfish_manager_reindex_slots(struct sailfish_manager_priv *p)
{
int count = 0;
sailfish_manager_foreach_slot(p, sailfish_manager_count_slot, &count);
g_free(p->slots);
p->pub.slots = p->slots = g_new0(sailfish_slot_ptr, count + 1);
/* p->slot_count is the index for sailfish_manager_index_slot */
p->slot_count = 0;
sailfish_manager_foreach_slot(p, sailfish_manager_index_slot, p);
p->slots[p->slot_count] = NULL;
GASSERT(p->slot_count == count);
}
static gboolean sailfish_manager_check_slot_name(struct sailfish_slot_priv *s,
void *path)
{
return strcmp(s->pub.path, path) ? SF_LOOP_CONTINUE : SF_LOOP_DONE;
}
struct sailfish_slot *sailfish_manager_slot_add
(struct sailfish_slot_manager *m, struct sailfish_slot_impl *impl,
const char *path, enum ofono_radio_access_mode techs,
const char *imei, const char *imeisv,
enum sailfish_sim_state sim_state)
{
return sailfish_manager_slot_add2(m, impl, path, techs, imei, imeisv,
sim_state, SAILFISH_SLOT_NO_FLAGS);
}
struct sailfish_slot *sailfish_manager_slot_add2
(struct sailfish_slot_manager *m, struct sailfish_slot_impl *impl,
const char *path, enum ofono_radio_access_mode techs,
const char *imei, const char *imeisv,
enum sailfish_sim_state sim_state,
enum sailfish_slot_flags flags)
{
/* Only accept these calls when we are starting! We have been
* assuming all along that the number of slots is known right
* from startup. Perhaps it wasn't a super bright idea because
* there are USB modems which can appear (and disappear) pretty
* much at any time. This has to be dealt with somehow at some
* point but for now let's leave it as is. */
if (path && m && !m->started && !sailfish_manager_foreach_slot
(m->plugin, sailfish_manager_check_slot_name,
(char*)path)) {
char *enabled_slots;
struct sailfish_manager_priv *p = m->plugin;
struct sailfish_slot_priv *s =
g_slice_new0(struct sailfish_slot_priv);
DBG("%s", path);
s->impl = impl;
s->manager = m;
s->sim_state = sim_state;
s->flags = flags;
s->watch = ofono_watch_new(path);
s->siminfo = sailfish_sim_info_new(path);
s->siminfo_dbus = sailfish_sim_info_dbus_new(s->siminfo);
s->pub.path = s->watch->path;
s->pub.imei = s->imei = g_strdup(imei);
s->pub.imeisv = s->imeisv = g_strdup(imeisv);
s->pub.sim_present = (sim_state == SAILFISH_SIM_STATE_PRESENT);
/* Check if it's enabled */
enabled_slots = g_key_file_get_string(p->storage,
SF_STORE_GROUP, SF_STORE_ENABLED_SLOTS, NULL);
if (enabled_slots) {
char **strv = g_strsplit(enabled_slots,
SF_STORE_SLOTS_SEP, 0);
DBG("Enabled slots: %s", enabled_slots);
s->pub.enabled = gutil_strv_contains(strv, path);
g_strfreev(strv);
g_free(enabled_slots);
} else {
/* All slots are enabled by default */
s->pub.enabled = TRUE;
}
/* Add it to the list */
if (!m->slots) {
/* The first one */
m->slots = s;
} else if (strcmp(m->slots->pub.path, path) > 0) {
/* This one becomes the head of the list */
s->next = m->slots;
m->slots = s;
} else {
/* Need to do some sorting */
struct sailfish_slot_priv *prev = m->slots;
struct sailfish_slot_priv *slot = m->slots->next;
while (slot && strcmp(slot->pub.path, path) < 0) {
prev = slot;
slot = prev->next;
}
s->next = prev->next;
prev->next = s;
}
sailfish_manager_reindex_slots(m->plugin);
/* Register for events */
s->watch_event_id[WATCH_EVENT_MODEM] =
ofono_watch_add_modem_changed_handler(s->watch,
sailfish_manager_slot_modem_changed, s);
s->watch_event_id[WATCH_EVENT_ONLINE] =
ofono_watch_add_online_changed_handler(s->watch,
sailfish_manager_slot_modem_changed, s);
s->watch_event_id[WATCH_EVENT_IMSI] =
ofono_watch_add_imsi_changed_handler(s->watch,
sailfish_manager_slot_imsi_changed, s);
return &s->pub;
} else {
ofono_error("Refusing to register slot %s", path);
}
return NULL;
}
static void sailfish_slot_free(struct sailfish_slot_priv *s)
{
struct sailfish_slot_manager *m = s->manager;
struct sailfish_manager_priv *p = m->plugin;
if (s->impl) {
const struct sailfish_slot_driver *d = m->driver;
if (d->slot_free) {
d->slot_free(s->impl);
s->impl = NULL;
}
}
if (s->errors) {
g_hash_table_destroy(s->errors);
}
sailfish_sim_info_unref(s->siminfo);
sailfish_sim_info_dbus_free(s->siminfo_dbus);
sailfish_cell_info_dbus_free(s->cellinfo_dbus);
sailfish_cell_info_unref(s->cellinfo);
ofono_watch_remove_all_handlers(s->watch, s->watch_event_id);
ofono_watch_unref(s->watch);
g_free(s->imei);
g_free(s->imeisv);
s->next = NULL;
s->manager = NULL;
g_slice_free(struct sailfish_slot_priv, s);
sailfish_manager_reindex_slots(p);
}
void sailfish_manager_set_cell_info(struct sailfish_slot *s,
struct sailfish_cell_info *info)
{
if (s) {
struct sailfish_slot_priv *slot = sailfish_slot_priv_cast(s);
if (slot->cellinfo != info) {
sailfish_cell_info_dbus_free(slot->cellinfo_dbus);
sailfish_cell_info_unref(slot->cellinfo);
slot->cellinfo = sailfish_cell_info_ref(info);
slot->cellinfo_dbus = NULL;
sailfish_manager_slot_update_cell_info_dbus(slot);
}
}
}
static gboolean sailfish_manager_update_dbus_block_proc
(struct sailfish_slot_driver_reg *r, void *data)
{
enum sailfish_manager_dbus_block *block = data;
struct sailfish_slot_manager *m;
struct sailfish_slot_priv *s;
if (r->init_id) {
/* Driver is being initialized */
(*block) |= SAILFISH_MANAGER_DBUS_BLOCK_ALL;
return SF_LOOP_DONE;
}
m = r->manager;
if (!m) {
return SF_LOOP_CONTINUE;
}
if (!m->started) {
/* Slots are being initialized */
(*block) |= SAILFISH_MANAGER_DBUS_BLOCK_ALL;
return SF_LOOP_DONE;
}
for (s = m->slots; s && s->imei; s = s->next);
if (s) {
/* IMEI is not available (yet) */
(*block) |= SAILFISH_MANAGER_DBUS_BLOCK_IMEI;
}
return SF_LOOP_CONTINUE;
}
static void sailfish_manager_update_dbus_block(struct sailfish_manager_priv *p)
{
enum sailfish_manager_dbus_block block =
SAILFISH_MANAGER_DBUS_BLOCK_NONE;
if (p->init_countdown) {
/* Plugin is being initialized */
block |= SAILFISH_MANAGER_DBUS_BLOCK_ALL;
} else {
sailfish_manager_foreach_driver(p,
sailfish_manager_update_dbus_block_proc, &block);
}
sailfish_manager_dbus_set_block(p->dbus, block);
}
static void sailfish_manager_set_config_string
(struct sailfish_manager_priv *p, const char *key,
const char *value)
{
if (value) {
g_key_file_set_string(p->storage, SF_STORE_GROUP, key, value);
} else {
g_key_file_remove_key(p->storage, SF_STORE_GROUP, key, NULL);
}
storage_sync(NULL, SF_STORE, p->storage);
}
struct sailfish_manager_slot_imsi_data {
struct sailfish_slot_priv *slot;
const char *imsi;
};
static gboolean sailfish_manager_find_slot_imsi_proc
(struct sailfish_slot_priv *s, void *user_data)
{
struct sailfish_manager_slot_imsi_data *data = user_data;
const char *slot_imsi = s->watch->imsi;
if (slot_imsi && !strcmp(slot_imsi, data->imsi)) {
data->slot = s;
return SF_LOOP_DONE;
} else {
return SF_LOOP_CONTINUE;
}
}
struct sailfish_manager_any_slot_data {
struct sailfish_slot_priv *slot;
};
static gboolean sailfish_manager_find_any_slot_proc
(struct sailfish_slot_priv *s, void *user_data)
{
struct sailfish_manager_any_slot_data *data = user_data;
const char *slot_imsi = s->watch->imsi;
if (slot_imsi) {
data->slot = s;
return SF_LOOP_DONE;
} else {
return SF_LOOP_CONTINUE;
}
}
static struct sailfish_slot_priv *sailfish_manager_find_slot_imsi
(struct sailfish_manager_priv *p,
const char *imsi)
{
if (imsi) {
/* We are looking for the specific sim */
struct sailfish_manager_slot_imsi_data data;
memset(&data, 0, sizeof(data));
data.imsi = imsi;
sailfish_manager_foreach_slot(p,
sailfish_manager_find_slot_imsi_proc, &data);
return data.slot;
} else {
/* We are looking for any slot with a sim */
struct sailfish_manager_any_slot_data data;
memset(&data, 0, sizeof(data));
sailfish_manager_foreach_slot(p,
sailfish_manager_find_any_slot_proc, &data);
return data.slot;
}
}
static gboolean sailfish_manager_all_sims_are_initialized_proc
(struct sailfish_slot_priv *s, void *user_data)
{
if (s->pub.sim_present && s->pub.enabled && !s->watch->imsi) {
*((gboolean*)user_data) = FALSE;
return SF_LOOP_DONE;
} else {
return SF_LOOP_CONTINUE;
}
}
static gboolean sailfish_manager_all_sims_are_initialized
(struct sailfish_manager_priv *p)
{
gboolean result = TRUE;
sailfish_manager_foreach_slot(p,
sailfish_manager_all_sims_are_initialized_proc, &result);
return result;
}
/* Returns the event mask to be passed to sailfish_manager_dbus_signal.
* The caller has a chance to OR it with other bits */
static int sailfish_manager_update_modem_paths(struct sailfish_manager_priv *p)
{
int mask = 0;
struct sailfish_slot_priv *slot = NULL;
struct sailfish_slot_priv *mms_slot = NULL;
struct sailfish_slot_priv *old_data_slot = NULL;
struct sailfish_slot_priv *new_data_slot = NULL;
/* Voice */
if (p->default_voice_imsi) {
slot = sailfish_manager_find_slot_imsi(p,
p->default_voice_imsi);
} else if (p->voice_slot) {
/* Make sure that the slot is enabled and SIM is in */
slot = sailfish_manager_find_slot_imsi(p,
p->voice_slot->watch->imsi);
}
/*
* If there's no default voice SIM, we will find any SIM instead.
* One should always be able to make and receive a phone call
* if there's a working SIM in the phone. However if the
* previously selected voice SIM is inserted, we will switch
* back to it.
*
* A similar behavior can be configured for data SIM too.
*/
if (!slot) {
slot = sailfish_manager_find_slot_imsi(p, NULL);
}
if (p->voice_slot != slot) {
mask |= SAILFISH_MANAGER_SIGNAL_VOICE_PATH;
p->voice_slot = slot;
if (slot) {
const char *path = slot->pub.path;
DBG("Default voice SIM at %s", path);
p->pub.default_voice_path = path;
} else {
DBG("No default voice SIM");
p->pub.default_voice_path = NULL;
}
}
/* Data */
if (p->default_data_imsi) {
slot = sailfish_manager_find_slot_imsi(p,
p->default_data_imsi);
} else if (p->slot_count < 2) {
if (p->data_slot) {
/* Make sure that the slot is enabled and SIM is in */
slot = sailfish_manager_find_slot_imsi(p,
p->data_slot->watch->imsi);
} else {
/* Check if anything is available */
slot = sailfish_manager_find_slot_imsi(p, NULL);
}
} else {
slot = NULL;
}
/* Check if we need to auto-select data SIM (always or once) */
if (!slot && (p->auto_data_sim == SIM_AUTO_SELECT_ON ||
(p->auto_data_sim == SIM_AUTO_SELECT_ONCE &&
!p->auto_data_sim_done))) {
/*
* To actually make a selection we need all present SIMs
* to be initialized. Otherwise we may end up endlessly
* switching data SIMs back and forth.
*/
if (sailfish_manager_all_sims_are_initialized(p)) {
slot = sailfish_manager_find_slot_imsi(p, NULL);
if (slot && slot->watch->online &&
p->auto_data_sim == SIM_AUTO_SELECT_ONCE) {
/*
* Data SIM only needs to be auto-selected
* once and it's done. Write that down.
*/
p->auto_data_sim_done = TRUE;
g_key_file_set_boolean(p->storage,
SF_STORE_GROUP,
SF_STORE_AUTO_DATA_SIM_DONE,
p->auto_data_sim_done);
storage_sync(NULL, SF_STORE, p->storage);
}
} else {
DBG("Skipping auto-selection of data SIM");
}
}
if (slot && !slot->watch->online) {
slot = NULL;
}
if (p->mms_imsi) {
mms_slot = sailfish_manager_find_slot_imsi(p, p->mms_imsi);
}
if (mms_slot && (mms_slot != slot ||
(slot->flags & SAILFISH_SLOT_SINGLE_CONTEXT))) {
/*
* Reset default data SIM if
* a) another SIM is temporarily selected for MMS; or
* b) this slot can't have more than one context active.
*/
slot = NULL;
}
/* Are we actually switching data SIMs? */
old_data_slot = p->mms_slot ? p->mms_slot : p->data_slot;
new_data_slot = mms_slot ? mms_slot : slot;
if (p->data_slot != slot) {
mask |= SAILFISH_MANAGER_SIGNAL_DATA_PATH;
p->data_slot = slot;
if (slot) {
const char *path = slot->pub.path;
DBG("Default data SIM at %s", path);
p->pub.default_data_path = path;
} else {
DBG("No default data SIM");
p->pub.default_data_path = NULL;
}
}
if (p->mms_slot != mms_slot) {
mask |= SAILFISH_MANAGER_SIGNAL_MMS_PATH;
p->mms_slot = mms_slot;
if (mms_slot) {
const char *path = mms_slot->pub.path;
DBG("MMS data SIM at %s", path);
p->pub.mms_path = path;
} else {
DBG("No MMS data SIM");
p->pub.mms_path = NULL;
}
}
if (old_data_slot != new_data_slot) {
/* Yes we are switching data SIMs */
if (old_data_slot) {
sailfish_slot_set_data_role(old_data_slot,
SAILFISH_DATA_ROLE_NONE);
}
if (new_data_slot) {
sailfish_slot_set_data_role(new_data_slot,
(new_data_slot == p->data_slot) ?
SAILFISH_DATA_ROLE_INTERNET :
SAILFISH_DATA_ROLE_MMS);
}
}
return mask;
}
static gboolean sailfish_manager_update_ready_driver_proc
(struct sailfish_slot_driver_reg *r, void *unused)
{
struct sailfish_slot_manager *m = r->manager;
if (!m || m->started) {
/* This one is either missing or ready */
return SF_LOOP_CONTINUE;
} else {
/* This one is not */
return SF_LOOP_DONE;
}
}
static gboolean sailfish_manager_update_ready_slot_proc
(struct sailfish_slot_priv *s, void *unused)
{
if (s->imei && s->sim_state != SAILFISH_SIM_STATE_UNKNOWN) {
/* This one is ready */
return SF_LOOP_CONTINUE;
} else {
/* This one is not */
return SF_LOOP_DONE;
}
}
static gboolean sailfish_manager_update_ready(struct sailfish_manager_priv *p)
{
/*
* sailfish_manager_foreach_driver and sailfish_manager_foreach_slot
* return FALSE if either all callbacks returned SF_LOOP_CONTINUE or
* there are no drivers/slots. In either case we are ready. */
const gboolean ready =
!sailfish_manager_foreach_driver
(p,sailfish_manager_update_ready_driver_proc, NULL) &&
!sailfish_manager_foreach_slot
(p, sailfish_manager_update_ready_slot_proc, NULL);
if (p->pub.ready != ready) {
p->pub.ready = ready;
sailfish_manager_update_dbus_block(p);
DBG("%sready", ready ? "" : "not ");
sailfish_manager_dbus_signal(p->dbus,
SAILFISH_MANAGER_SIGNAL_READY);
return TRUE;
} else {
return FALSE;
}
}
void sailfish_manager_imei_obtained(struct sailfish_slot *s, const char *imei)
{
if (s) {
struct sailfish_slot_priv *slot = sailfish_slot_priv_cast(s);
/* We assume that IMEI never changes */
GASSERT(imei);
GASSERT(!slot->imei || !g_strcmp0(slot->imei, imei));
g_free(slot->imei); /* Just in case */
slot->pub.imei = slot->imei = g_strdup(imei);
sailfish_manager_update_ready(slot->manager->plugin);
}
}
void sailfish_manager_imeisv_obtained(struct sailfish_slot *s,
const char *imeisv)
{
if (s) {
struct sailfish_slot_priv *slot = sailfish_slot_priv_cast(s);
/* We assume that IMEISV never changes */
GASSERT(imeisv);
GASSERT(!slot->imeisv || !g_strcmp0(slot->imeisv, imeisv));
g_free(slot->imeisv); /* Just in case */
slot->pub.imeisv = slot->imeisv = g_strdup(imeisv);
sailfish_manager_update_ready(slot->manager->plugin);
}
}
void sailfish_manager_set_sim_state(struct sailfish_slot *s,
enum sailfish_sim_state state)
{
if (s) {
struct sailfish_slot_priv *slot = sailfish_slot_priv_cast(s);
struct sailfish_manager_priv *p = slot->manager->plugin;
const gboolean present = (state == SAILFISH_SIM_STATE_PRESENT);
if (slot->pub.sim_present != present) {
slot->pub.sim_present = present;
sailfish_manager_dbus_signal_sim(p->dbus,
slot->index, present);
sailfish_manager_update_modem_paths_full(p);
}
if (slot->sim_state != state) {
slot->sim_state = state;
sailfish_manager_update_ready(p);
}
}
}
static gboolean sailfish_manager_update_enabled_slot
(struct sailfish_slot_priv *s, void *unused)
{
if (s->pub.enabled && s->enabled_changed) {
const struct sailfish_slot_driver *d = s->manager->driver;
DBG("%s enabled", sailfish_slot_debug_prefix(s));
s->enabled_changed = TRUE;
if (d->slot_enabled_changed) {
d->slot_enabled_changed(s->impl);
}
}
return SF_LOOP_CONTINUE;
}
static gboolean sailfish_manager_update_disabled_slot
(struct sailfish_slot_priv *s, void *unused)
{
if (!s->pub.enabled && s->enabled_changed) {
struct sailfish_slot_manager *m = s->manager;
const struct sailfish_slot_driver *d = m->driver;
DBG("%s disabled", sailfish_slot_debug_prefix(s));
s->enabled_changed = FALSE;
if (d->slot_enabled_changed) {
d->slot_enabled_changed(s->impl);
}
sailfish_manager_update_modem_paths_full(m->plugin);
}
return SF_LOOP_CONTINUE;
}
static void sailfish_manager_update_slots(struct sailfish_manager_priv *p)
{
sailfish_manager_foreach_slot(p, sailfish_manager_update_disabled_slot,
NULL);
sailfish_manager_foreach_slot(p, sailfish_manager_update_enabled_slot,
NULL);
sailfish_manager_update_modem_paths_full(p);
}
static gboolean sailfish_manager_enabled_slots_proc
(struct sailfish_slot_priv *slot, void *user_data)
{
if (slot->pub.enabled) {
char ***list = user_data;
*list = gutil_strv_add(*list, slot->pub.path);
}
return SF_LOOP_CONTINUE;
}
struct sailfish_manager_set_enabled_slots_data {
gchar * const * enabled;
gboolean all_enabled;
gboolean changed;
};
static gboolean sailfish_manager_set_enabled_slots_proc
(struct sailfish_slot_priv *slot, void *user_data)
{
struct sailfish_manager_set_enabled_slots_data *data = user_data;
struct sailfish_slot *s = &slot->pub;
const gboolean was_enabled = s->enabled;
s->enabled = gutil_strv_contains(data->enabled, s->path);
if ((was_enabled && !s->enabled) || (!was_enabled && s->enabled)) {
slot->enabled_changed = TRUE;
data->changed = TRUE;
}