summaryrefslogtreecommitdiff
path: root/src/qmicli/qmicli-loc.c
blob: 946af2ea79fce4b93656e7747430498173446148 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * qmicli -- Command line interface to control QMI devices
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2018 Thomas Weißschuh <thomas@weissschuh.net>
 * Copyright (C) 2018 Aleksander Morgado <aleksander@aleksander.es>
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>

#include <glib.h>
#include <gio/gio.h>

#include <libqmi-glib.h>

#include "qmicli.h"
#include "qmicli-helpers.h"

#if defined HAVE_QMI_SERVICE_LOC

#undef VALIDATE_MASK_NONE
#define VALIDATE_MASK_NONE(str) (str ? str : "none")

/* Context */

typedef enum {
    MONITORING_STEP_FIRST,
    MONITORING_STEP_REGISTER_EVENTS,
    MONITORING_STEP_SETUP_TIMEOUT,
    MONITORING_STEP_ONGOING,
} MonitoringStep;

typedef struct {
    QmiDevice      *device;
    QmiClientLoc   *client;
    GCancellable   *cancellable;
    guint           timeout_id;
    MonitoringStep  monitoring_step;
    guint           position_report_indication_id;
    guint           nmea_indication_id;
    guint           gnss_sv_info_indication_id;
    guint           delete_assistance_data_indication_id;
    guint           get_nmea_types_indication_id;
    guint           set_nmea_types_indication_id;
    guint           get_operation_mode_indication_id;
    guint           set_operation_mode_indication_id;
    guint           get_engine_lock_indication_id;
    guint           set_engine_lock_indication_id;
} Context;
static Context *ctx;

/* Options */
static gint     session_id;
static gboolean start_flag;
static gboolean stop_flag;
static gboolean get_position_report_flag;
static gboolean get_gnss_sv_info_flag;
static gint     timeout;
static gboolean follow_position_report_flag;
static gboolean follow_gnss_sv_info_flag;
static gboolean follow_nmea_flag;
static gboolean delete_assistance_data_flag;
static gboolean get_nmea_types_flag;
static gchar   *set_nmea_types_str;
static gboolean get_operation_mode_flag;
static gchar   *set_operation_mode_str;
static gboolean get_engine_lock_flag;
static gchar   *set_engine_lock_str;
static gboolean noop_flag;

#define DEFAULT_LOC_TIMEOUT_SECS 30

static GOptionEntry entries[] = {
#if defined HAVE_QMI_MESSAGE_LOC_START || defined HAVE_QMI_MESSAGE_LOC_STOP
    {
        "loc-session-id", 0, 0, G_OPTION_ARG_INT, &session_id,
        "Session ID for the LOC session",
        "[ID]",
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_START
    {
        "loc-start", 0, 0, G_OPTION_ARG_NONE, &start_flag,
        "Start location gathering",
        NULL,
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_STOP
    {
        "loc-stop", 0, 0, G_OPTION_ARG_NONE, &stop_flag,
        "Stop location gathering",
        NULL,
    },
#endif
#if defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
    {
        "loc-get-position-report", 0, 0, G_OPTION_ARG_NONE, &get_position_report_flag,
        "Get position reported by the location module",
        NULL,
    },
#endif
#if defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
    {
        "loc-get-gnss-sv-info", 0, 0, G_OPTION_ARG_NONE, &get_gnss_sv_info_flag,
        "Show GNSS space vehicle info",
        NULL,
    },
#endif
#if (defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT || defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO) && \
    defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
    {
        "loc-timeout", 0, 0, G_OPTION_ARG_INT, &timeout,
        "Maximum time to wait for information in `--loc-get-position-report' and `--loc-get-gnss-sv-info' (default 30s)",
        "[SECS]",
    },
#endif
#if defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
    {
        "loc-follow-position-report", 0, 0, G_OPTION_ARG_NONE, &follow_position_report_flag,
        "Follow all position updates reported by the location module indefinitely",
        NULL,
    },
#endif
#if defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
    {
        "loc-follow-gnss-sv-info", 0, 0, G_OPTION_ARG_NONE, &follow_gnss_sv_info_flag,
        "Follow all GNSS space vehicle info updates reported by the location module indefinitely",
        NULL,
    },
#endif
#if defined HAVE_QMI_INDICATION_LOC_NMEA && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
    {
        "loc-follow-nmea", 0, 0, G_OPTION_ARG_NONE, &follow_nmea_flag,
        "Follow all NMEA trace updates reported by the location module indefinitely",
        NULL,
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_DELETE_ASSISTANCE_DATA
    {
        "loc-delete-assistance-data", 0, 0, G_OPTION_ARG_NONE, &delete_assistance_data_flag,
        "Delete positioning assistance data",
        NULL,
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_GET_NMEA_TYPES
    { "loc-get-nmea-types", 0, 0, G_OPTION_ARG_NONE, &get_nmea_types_flag,
      "Get list of enabled NMEA traces",
      NULL
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_SET_NMEA_TYPES
    { "loc-set-nmea-types", 0, 0, G_OPTION_ARG_STRING, &set_nmea_types_str,
      "Set list of enabled NMEA traces",
      "[type1|type2|type3...]"
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_GET_OPERATION_MODE
    { "loc-get-operation-mode", 0, 0, G_OPTION_ARG_NONE, &get_operation_mode_flag,
      "Get operation mode",
      NULL
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_SET_OPERATION_MODE
    { "loc-set-operation-mode", 0, 0, G_OPTION_ARG_STRING, &set_operation_mode_str,
      "Set operation mode",
      "[default|msb|msa|standalone|cellid|wwan]"
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_GET_ENGINE_LOCK
    { "loc-get-engine-lock", 0, 0, G_OPTION_ARG_NONE, &get_engine_lock_flag,
      "Get engine lock status",
      NULL
    },
#endif
#if defined HAVE_QMI_MESSAGE_LOC_SET_ENGINE_LOCK
    { "loc-set-engine-lock", 0, 0, G_OPTION_ARG_STRING, &set_engine_lock_str,
      "Set engine lock status",
      "[none|mi|mt|all]"
    },
#endif
    { "loc-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag,
      "Just allocate or release a LOC client. Use with `--client-no-release-cid' and/or `--client-cid'",
      NULL
    },
    { NULL, 0, 0, 0, NULL, NULL, NULL }
};

GOptionGroup *
qmicli_loc_get_option_group (void)
{
    GOptionGroup *group;

    group = g_option_group_new ("loc",
                                "LOC options:",
                                "Show location options", NULL, NULL);
    g_option_group_add_entries (group, entries);

    return group;
}

gboolean
qmicli_loc_options_enabled (void)
{
    static guint n_actions = 0;
    static gboolean checked = FALSE;
    gboolean follow_action;

    if (checked)
        return !!n_actions;

    /* Let's define the following actions:
     *  - Start location engine
     *  - Stop location engine
     *  - Show current position (oneshot).
     *  - Show current satellite info (oneshot).
     *  - Follow updates indefinitely, including either position, satellite info or NMEA traces.
     *  - Other single-request operations.
     */
    follow_action = !!(follow_position_report_flag + follow_gnss_sv_info_flag + follow_nmea_flag);
    n_actions = (start_flag +
                 stop_flag +
                 get_position_report_flag +
                 get_gnss_sv_info_flag +
                 follow_action +
                 delete_assistance_data_flag +
                 get_nmea_types_flag +
                 !!set_nmea_types_str +
                 get_operation_mode_flag +
                 !!set_operation_mode_str +
                 get_engine_lock_flag +
                 !!set_engine_lock_str +
                 noop_flag);

    if (n_actions > 1) {
        g_printerr ("error: too many LOC actions requested\n");
        exit (EXIT_FAILURE);
    }

    if (session_id < 0 || session_id > G_MAXUINT8) {
        g_printerr ("error: invalid session ID: %d [0,%u]\n", session_id, G_MAXUINT8);
        exit (EXIT_FAILURE);
    }

    if (timeout < 0) {
        g_printerr ("error: invalid timeout: %d", timeout);
        exit (EXIT_FAILURE);
    }

    if (timeout > 0 && !(get_position_report_flag || get_gnss_sv_info_flag)) {
        g_printerr ("error: `--loc-timeout' is only applicable with `--loc-get-position-report' or `--loc-get-gnss-sv-info'\n");
        exit (EXIT_FAILURE);
    }

    /* Actions that require receiving QMI indication messages must specify that
     * indications are expected. */
    if (get_position_report_flag ||
        get_gnss_sv_info_flag ||
        follow_action ||
        delete_assistance_data_flag ||
        get_nmea_types_flag ||
        set_nmea_types_str ||
        get_operation_mode_flag ||
        set_operation_mode_str ||
        get_engine_lock_flag ||
        set_engine_lock_str)
        qmicli_expect_indications ();

    checked = TRUE;
    return !!n_actions;
}

static void
context_free (Context *context)
{
    if (!context)
        return;

    if (context->timeout_id)
        g_source_remove (context->timeout_id);

    if (context->position_report_indication_id)
        g_signal_handler_disconnect (context->client, context->position_report_indication_id);

    if (context->gnss_sv_info_indication_id)
        g_signal_handler_disconnect (context->client, context->gnss_sv_info_indication_id);

    if (context->nmea_indication_id)
        g_signal_handler_disconnect (context->client, context->nmea_indication_id);

    if (context->delete_assistance_data_indication_id)
        g_signal_handler_disconnect (context->client, context->delete_assistance_data_indication_id);

    if (context->get_nmea_types_indication_id)
        g_signal_handler_disconnect (context->client, context->get_nmea_types_indication_id);

    if (context->set_nmea_types_indication_id)
        g_signal_handler_disconnect (context->client, context->set_nmea_types_indication_id);

    if (context->get_operation_mode_indication_id)
        g_signal_handler_disconnect (context->client, context->get_operation_mode_indication_id);

    if (context->set_operation_mode_indication_id)
        g_signal_handler_disconnect (context->client, context->set_operation_mode_indication_id);

    if (context->get_engine_lock_indication_id)
        g_signal_handler_disconnect (context->client, context->get_engine_lock_indication_id);

    if (context->set_engine_lock_indication_id)
        g_signal_handler_disconnect (context->client, context->set_engine_lock_indication_id);

    g_clear_object (&context->cancellable);
    g_clear_object (&context->client);
    g_clear_object (&context->device);
    g_slice_free (Context, context);
}

static void
operation_shutdown (gboolean operation_status)
{
    context_free (ctx);
    qmicli_async_operation_done (operation_status, FALSE);
}

#if (defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT || \
     defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO ||    \
     defined HAVE_QMI_INDICATION_LOC_NMEA) &&           \
    defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS

static void monitoring_step_run (void);

static gboolean
monitoring_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
monitoring_cancelled (GCancellable *cancellable)
{
    /* For GET operations, this is a failure */
    if (get_position_report_flag || get_gnss_sv_info_flag) {
        g_printerr ("error: operation failed: cancelled\n");
        operation_shutdown (FALSE);
        return;
    }

    /* For FOLLOW operations, silently exit */
    if (follow_position_report_flag || follow_gnss_sv_info_flag || follow_nmea_flag) {
        operation_shutdown (TRUE);
        return;
    }

    g_assert_not_reached ();
}

#endif /* HAVE_QMI_INDICATION_LOC_POSITION_REPORT
        * HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO
        * HAVE_QMI_INDICATION_LOC_NMEA */

#if defined HAVE_QMI_INDICATION_LOC_NMEA && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS

static void
nmea_received (QmiClientLoc               *client,
               QmiIndicationLocNmeaOutput *output)
{
    const gchar *nmea = NULL;

    qmi_indication_loc_nmea_output_get_nmea_string (output, &nmea, NULL);
    if (nmea)
        /* Note: NMEA traces already have an EOL */
        g_print ("%s", nmea);
}

#endif /* HAVE_QMI_INDICATION_LOC_NMEA */

#if defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS

static void
gnss_sv_info_received (QmiClientLoc                     *client,
                       QmiIndicationLocGnssSvInfoOutput *output)
{
    GArray   *satellite_infos = NULL;
    guint     i, num_satellite_infos;
    gboolean  altitude_assumed;

    if (qmi_indication_loc_gnss_sv_info_output_get_altitude_assumed (output, &altitude_assumed, NULL))
        g_print ("[gnss sv info] Altitude assumed: %s\n", altitude_assumed ? "yes" : "no");
    else
        g_print ("[gnss sv info] Altitude assumed: n/a\n");

    qmi_indication_loc_gnss_sv_info_output_get_list (output, &satellite_infos, NULL);

    num_satellite_infos = satellite_infos ? satellite_infos->len : 0;
    g_print ("[gnss sv info] %d satellites detected:\n", num_satellite_infos);
    for (i = 0; i < num_satellite_infos; i++) {
        QmiIndicationLocGnssSvInfoOutputListElement *element;

        element = &g_array_index (satellite_infos, QmiIndicationLocGnssSvInfoOutputListElement, i);
        g_print ("   [satellite #%u]\n", i);
        g_print ("      system:           %s\n", (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_SYSTEM) ? qmi_loc_system_get_string (element->system) : "n/a");
        if (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_GNSS_SATELLITE_ID)
            g_print ("      satellite id:     %u\n", element->gnss_satellite_id);
        else
            g_print ("      satellite id:     n/a\n");
        g_print ("      health status:    %s\n", (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_HEALTH_STATUS) ? qmi_loc_health_status_get_string (element->health_status) : "n/a");
        g_print ("      satellite status: %s\n", (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_PROCESS_STATUS) ? qmi_loc_satellite_status_get_string (element->satellite_status) : "n/a");
        g_print ("      navigation data:  %s\n", (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_SATELLITE_INFO_MASK) ? qmi_loc_navigation_data_get_string (element->navigation_data) : "n/a");

        if (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_ELEVATION)
            g_print ("      elevation:        %lf\n", (gdouble)element->elevation_degrees);
        else
            g_print ("      elevation:        n/a\n");

        if (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_AZIMUTH)
            g_print ("      azimuth:          %lf\n", (gdouble)element->azimuth_degrees);
        else
            g_print ("      azimuth:          n/a\n");

        if (element->valid_information & QMI_LOC_SATELLITE_VALID_INFORMATION_SIGNAL_TO_NOISE_RATIO)
            g_print ("      SNR:              %lf\n", (gdouble)element->signal_to_noise_ratio_bhz);
        else
            g_print ("      SNR:              n/a\n");
    }

    /* Terminate GET request */
    if (get_gnss_sv_info_flag)
        operation_shutdown (TRUE);
}

#endif /* HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO */

#if defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT && defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS

static void
position_report_received (QmiClientLoc                         *client,
                          QmiIndicationLocPositionReportOutput *output)
{
    QmiLocSessionStatus status;

    qmi_indication_loc_position_report_output_get_session_status (output, &status, NULL);
    g_print ("[position report] status: %s\n", qmi_loc_session_status_get_string (status));

    if (status == QMI_LOC_SESSION_STATUS_SUCCESS || status == QMI_LOC_SESSION_STATUS_IN_PROGRESS) {
        gdouble auxd;
        gfloat auxf;
        guint8 aux8;
        guint32 aux32;
        guint64 aux64;
        QmiLocReliability reliability;
        QmiLocTechnologyUsed technology;
        QmiLocTimeSource time_source;
        QmiLocSensorDataUsage sensor_data_usage;
        gfloat pdop;
        gfloat hdop;
        gfloat vdop;
        guint16 gps_weeks;
        guint32 gps_time_of_week_milliseconds;
        gboolean auxb;
        GArray *array;

        if (qmi_indication_loc_position_report_output_get_latitude (output, &auxd, NULL))
            g_print ("   latitude:  %lf degrees\n", auxd);
        else
            g_print ("   latitude:  n/a\n");

        if (qmi_indication_loc_position_report_output_get_longitude (output, &auxd, NULL))
            g_print ("   longitude: %lf degrees\n", auxd);
        else
            g_print ("   longitude: n/a\n");

        if (qmi_indication_loc_position_report_output_get_horizontal_uncertainty_circular (output, &auxf, NULL))
            g_print ("   circular horizontal position uncertainty:            %lf meters\n", (gdouble)auxf);
        else
            g_print ("   circular horizontal position uncertainty:            n/a\n");

        if (qmi_indication_loc_position_report_output_get_horizontal_uncertainty_elliptical_minor (output, &auxf, NULL))
            g_print ("   horizontal elliptical uncertainty (semi-minor axis): %lf meters\n", (gdouble)auxf);
        else
            g_print ("   horizontal elliptical uncertainty (semi-minor axis): n/a\n");

        if (qmi_indication_loc_position_report_output_get_horizontal_uncertainty_elliptical_major (output, &auxf, NULL))
            g_print ("   horizontal elliptical uncertainty (semi-major axis): %lf meters\n", (gdouble)auxf);
        else
            g_print ("   horizontal elliptical uncertainty (semi-major axis): n/a\n");

        if (qmi_indication_loc_position_report_output_get_horizontal_uncertainty_elliptical_azimuth (output, &auxf, NULL))
            g_print ("   horizontal elliptical uncertainty azimuth:           %lf meters\n", (gdouble)auxf);
        else
            g_print ("   horizontal elliptical uncertainty azimuth:           n/a\n");

        if (qmi_indication_loc_position_report_output_get_horizontal_confidence (output, &aux8, NULL))
            g_print ("   horizontal confidence: %u%%\n", aux8);
        else
            g_print ("   horizontal confidence: n/a\n");

        if (qmi_indication_loc_position_report_output_get_horizontal_reliability (output, &reliability, NULL))
            g_print ("   horizontal reliability: %s\n", qmi_loc_reliability_get_string (reliability));
        else
            g_print ("   horizontal reliability: n/a\n");

        if (qmi_indication_loc_position_report_output_get_horizontal_speed (output, &auxf, NULL))
            g_print ("   horizontal speed: %lf m/s\n", (gdouble)auxf);
        else
            g_print ("   horizontal speed: n/a\n");

        if (qmi_indication_loc_position_report_output_get_speed_uncertainty (output, &auxf, NULL))
            g_print ("   speed uncertainty: %lf m/s\n", (gdouble)auxf);
        else
            g_print ("   speed uncertainty: n/a\n");

        if (qmi_indication_loc_position_report_output_get_altitude_from_ellipsoid (output, &auxf, NULL))
            g_print ("   altitude w.r.t. ellipsoid: %lf meters\n", (gdouble)auxf);
        else
            g_print ("   altitude w.r.t. ellipsoid: n/a\n");

        if (qmi_indication_loc_position_report_output_get_altitude_from_sealevel (output, &auxf, NULL))
            g_print ("   altitude w.r.t. mean sea level: %lf meters\n", (gdouble)auxf);
        else
            g_print ("   altitude w.r.t. mean sea level: n/a\n");

        if (qmi_indication_loc_position_report_output_get_vertical_uncertainty (output, &auxf, NULL))
            g_print ("   vertical uncertainty: %lf meters\n", (gdouble)auxf);
        else
            g_print ("   vertical uncertainty: n/a\n");

        if (qmi_indication_loc_position_report_output_get_vertical_confidence (output, &aux8, NULL))
            g_print ("   vertical confidence: %u%%\n", aux8);
        else
            g_print ("   vertical confidence: n/a\n");

        if (qmi_indication_loc_position_report_output_get_vertical_reliability (output, &reliability, NULL))
            g_print ("   vertical reliability: %s\n", qmi_loc_reliability_get_string (reliability));
        else
            g_print ("   vertical reliability: n/a\n");

        if (qmi_indication_loc_position_report_output_get_vertical_speed (output, &auxf, NULL))
            g_print ("   vertical speed: %lf m/s\n", (gdouble)auxf);
        else
            g_print ("   vertical speed: n/a\n");

        if (qmi_indication_loc_position_report_output_get_heading (output, &auxf, NULL))
            g_print ("   heading: %lf degrees\n", (gdouble)auxf);
        else
            g_print ("   heading: n/a\n");

        if (qmi_indication_loc_position_report_output_get_heading_uncertainty (output, &auxf, NULL))
            g_print ("   heading uncertainty: %lf meters\n", (gdouble)auxf);
        else
            g_print ("   heading uncertainty: n/a\n");

        if (qmi_indication_loc_position_report_output_get_magnetic_deviation (output, &auxf, NULL))
            g_print ("   magnetic deviation: %lf degrees\n", (gdouble)auxf);
        else
            g_print ("   magnetic deviation: n/a\n");

        if (qmi_indication_loc_position_report_output_get_technology_used (output, &technology, NULL)) {
            g_autofree gchar *technology_str = NULL;

            technology_str = qmi_loc_technology_used_build_string_from_mask (technology);
            g_print ("   technology: %s\n", VALIDATE_MASK_NONE (technology_str));
        } else
            g_print ("   technology: n/a\n");

        if (qmi_indication_loc_position_report_output_get_dop (output, &pdop, &hdop, &vdop, NULL)) {
            g_print ("   position DOP:   %lf\n", (gdouble)pdop);
            g_print ("   horizontal DOP: %lf\n", (gdouble)hdop);
            g_print ("   vertical DOP:   %lf\n", (gdouble)vdop);
        } else {
            g_print ("   position DOP:   n/a\n");
            g_print ("   horizontal DOP: n/a\n");
            g_print ("   vertical DOP:   n/a\n");
        }

        if (qmi_indication_loc_position_report_output_get_utc_timestamp (output, &aux64, NULL))
            g_print ("   UTC timestamp: %" G_GUINT64_FORMAT " ms\n", aux64);
        else
            g_print ("   UTC timestamp: n/a\n");

        if (qmi_indication_loc_position_report_output_get_leap_seconds (output, &aux8, NULL))
            g_print ("   Leap seconds: %u\n", aux8);
        else
            g_print ("   Leap seconds: n/a\n");

        if (qmi_indication_loc_position_report_output_get_gps_date_time (output, &gps_weeks, &gps_time_of_week_milliseconds, NULL))
            g_print ("   GPS time: %u weeks and %ums\n", gps_weeks, gps_time_of_week_milliseconds);
        else
            g_print ("   GPS time: n/a\n");

        if (qmi_indication_loc_position_report_output_get_time_uncertainty (output, &auxf, NULL))
            g_print ("   time uncertainty: %lf ms\n", (gdouble)auxf);
        else
            g_print ("   time uncertainty: n/a\n");

        if (qmi_indication_loc_position_report_output_get_time_source (output, &time_source, NULL))
            g_print ("   time source: %s\n", qmi_loc_time_source_get_string (time_source));
        else
            g_print ("   time source: n/a\n");

        if (qmi_indication_loc_position_report_output_get_sensor_data_usage (output, &sensor_data_usage, NULL)) {
            g_autofree gchar *sensor_data_usage_str = NULL;

            sensor_data_usage_str = qmi_loc_sensor_data_usage_build_string_from_mask (sensor_data_usage);
            g_print ("   sensor data usage: %s\n", VALIDATE_MASK_NONE (sensor_data_usage_str));
        } else
            g_print ("   sensor data usage: n/a\n");

        if (qmi_indication_loc_position_report_output_get_session_fix_count (output, &aux32, NULL))
            g_print ("   Fix count: %u\n", aux32);
        else
            g_print ("   Fix count: n/a\n");

        if (qmi_indication_loc_position_report_output_get_satellites_used (output, &array, NULL)) {
            guint i;

            g_print ("   Satellites used: ");
            for (i = 0; i < array->len; i++) {
                guint16 sv_id;

                /*
                 * - For GPS:     1 to 32
                 * - For SBAS:    33 to 64
                 * - For GLONASS: 65 to 96
                 * - For QZSS:    193 to 197
                 * - For BDS:     201 to 237
                 */
                sv_id = g_array_index (array, guint16, i);
                g_print ("%u%s", sv_id, i == array->len - 1 ? "" : ",");
            }
            g_print ("\n");
        } else
            g_print ("   Satellites used: n/a\n");

        if (qmi_indication_loc_position_report_output_get_altitude_assumed (output, &auxb, NULL))
            g_print ("   Altitude assumed: %s\n", auxb ? "yes" : "no");
        else
            g_print ("   Altitude assumed: n/a\n");

        /* Terminate GET request */
        if (get_position_report_flag)
            operation_shutdown (TRUE);

        return;
    }

    /* Otherwise, treat as error */
    g_printerr ("[position report] error: %s\n", qmi_loc_session_status_get_string (status));
    if (get_position_report_flag)
        operation_shutdown (FALSE);
}

#endif /* HAVE_QMI_INDICATION_LOC_POSITION_REPORT */

#if (defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT || \
     defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO ||    \
     defined HAVE_QMI_INDICATION_LOC_NMEA) && \
    defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS

static void
monitoring_step_ongoing (void)
{
#if defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT
    if (get_position_report_flag || follow_position_report_flag)
        ctx->position_report_indication_id = g_signal_connect (ctx->client,
                                                               "position-report",
                                                               G_CALLBACK (position_report_received),
                                                               NULL);
#endif

#if defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO
    if (get_gnss_sv_info_flag || follow_gnss_sv_info_flag)
        ctx->gnss_sv_info_indication_id = g_signal_connect (ctx->client,
                                                            "gnss-sv-info",
                                                            G_CALLBACK (gnss_sv_info_received),
                                                            NULL);
#endif

#if defined HAVE_QMI_INDICATION_LOC_NMEA
    if (follow_nmea_flag)
        ctx->nmea_indication_id = g_signal_connect (ctx->client,
                                                    "nmea",
                                                    G_CALLBACK (nmea_received),
                                                    NULL);
#endif

    g_assert (ctx->position_report_indication_id ||
              ctx->gnss_sv_info_indication_id ||
              ctx->nmea_indication_id);
}

static void
monitoring_step_setup_timeout (void)
{
    /* User can use Ctrl+C to cancel the monitoring at any time */
    g_cancellable_connect (ctx->cancellable,
                           G_CALLBACK (monitoring_cancelled),
                           NULL,
                           NULL);

    /* For non-follow requests, we also setup a timeout */
    if (get_position_report_flag || get_gnss_sv_info_flag)
        ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                                 (GSourceFunc) monitoring_timed_out,
                                                 NULL);

    /* Go on */
    ctx->monitoring_step++;
    monitoring_step_run ();
}

static void
register_events_ready (QmiClientLoc *client,
                       GAsyncResult *res)
{
   QmiMessageLocRegisterEventsOutput *output;
   GError                            *error = NULL;

   output = qmi_client_loc_register_events_finish (client, res, &error);
   if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
   }

    if (!qmi_message_loc_register_events_output_get_result (output, &error)) {
        g_printerr ("error: could not register location tracking events: %s\n", error->message);
        qmi_message_loc_register_events_output_unref (output);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    g_debug ("Registered location tracking events...");

    /* Go on */
    ctx->monitoring_step++;
    monitoring_step_run ();

    qmi_message_loc_register_events_output_unref (output);
}

static void
monitoring_step_register_events (void)
{
    QmiMessageLocRegisterEventsInput *re_input;
    QmiLocEventRegistrationFlag       indication_mask = 0;

    /* Configure events to enable */

    if (get_position_report_flag || follow_position_report_flag)
        indication_mask |= QMI_LOC_EVENT_REGISTRATION_FLAG_POSITION_REPORT;

    if (get_gnss_sv_info_flag || follow_gnss_sv_info_flag)
        indication_mask |= QMI_LOC_EVENT_REGISTRATION_FLAG_GNSS_SATELLITE_INFO;

    if (follow_nmea_flag)
        indication_mask |= QMI_LOC_EVENT_REGISTRATION_FLAG_NMEA;

    g_assert (indication_mask);

    re_input = qmi_message_loc_register_events_input_new ();
    qmi_message_loc_register_events_input_set_event_registration_mask (
        re_input, indication_mask, NULL);
    qmi_client_loc_register_events (ctx->client,
                                    re_input,
                                    10,
                                    ctx->cancellable,
                                    (GAsyncReadyCallback) register_events_ready,
                                    NULL);
    qmi_message_loc_register_events_input_unref (re_input);
}

static void
monitoring_step_run (void)
{
    switch (ctx->monitoring_step) {
    case MONITORING_STEP_FIRST:
        ctx->monitoring_step++;
        /* fall through */

    case MONITORING_STEP_REGISTER_EVENTS:
        monitoring_step_register_events ();
        return;

    case MONITORING_STEP_SETUP_TIMEOUT:
        monitoring_step_setup_timeout ();
        return;

    case MONITORING_STEP_ONGOING:
        monitoring_step_ongoing ();
        return;

    default:
        g_assert_not_reached();
    }
}

#endif /* HAVE_QMI_INDICATION_LOC_POSITION_REPORT
        * HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO
        * HAVE_QMI_INDICATION_LOC_NMEA */

#if defined HAVE_QMI_MESSAGE_LOC_DELETE_ASSISTANCE_DATA

static gboolean
delete_assistance_data_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
delete_assistance_data_received (QmiClientLoc                               *client,
                                 QmiIndicationLocDeleteAssistanceDataOutput *output)
{
    QmiLocIndicationStatus  status;
    GError                 *error = NULL;

    if (!qmi_indication_loc_delete_assistance_data_output_get_indication_status (output, &status, &error)) {
        g_printerr ("error: couldn't delete assistance data: %s\n", error->message);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    g_print ("Successfully deleted assistance data\n");
    operation_shutdown (TRUE);
}

static void
delete_assistance_data_ready (QmiClientLoc *client,
                              GAsyncResult *res)
{
    QmiMessageLocDeleteAssistanceDataOutput *output;
    GError                                  *error = NULL;

    output = qmi_client_loc_delete_assistance_data_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_delete_assistance_data_output_get_result (output, &error)) {
        g_printerr ("error: could not delete assistance data: %s\n", error->message);
        qmi_message_loc_delete_assistance_data_output_unref (output);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    /* Wait for response asynchronously */
    ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                             (GSourceFunc) delete_assistance_data_timed_out,
                                             NULL);

    ctx->delete_assistance_data_indication_id = g_signal_connect (ctx->client,
                                                                  "delete-assistance-data",
                                                                  G_CALLBACK (delete_assistance_data_received),
                                                                  NULL);

    qmi_message_loc_delete_assistance_data_output_unref (output);
}

#endif /* HAVE_QMI_MESSAGE_LOC_DELETE_ASSISTANCE_DATA */

#if defined HAVE_QMI_MESSAGE_LOC_GET_NMEA_TYPES

static gboolean
get_nmea_types_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
get_nmea_types_received (QmiClientLoc                       *client,
                         QmiIndicationLocGetNmeaTypesOutput *output)
{
    QmiLocIndicationStatus  status;
    QmiLocNmeaType          nmea_types_mask;
    g_autoptr(GError)       error = NULL;
    g_autofree gchar       *nmea_types_str = NULL;

    if (!qmi_indication_loc_get_nmea_types_output_get_indication_status (output, &status, &error)) {
        g_printerr ("error: couldn't get NMEA types: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_indication_loc_get_nmea_types_output_get_nmea_types (output, &nmea_types_mask, NULL)) {
        g_printerr ("error: couldn't get NMEA types: missing\n");
        operation_shutdown (FALSE);
        return;
    }

    nmea_types_str = qmi_loc_nmea_type_build_string_from_mask (nmea_types_mask);
    g_print ("Successfully retrieved NMEA types: %s\n", VALIDATE_MASK_NONE (nmea_types_str));
    operation_shutdown (TRUE);
}

static void
get_nmea_types_ready (QmiClientLoc *client,
                      GAsyncResult *res)
{
    g_autoptr(QmiMessageLocGetNmeaTypesOutput) output = NULL;
    g_autoptr(GError)                          error = NULL;

    output = qmi_client_loc_get_nmea_types_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_get_nmea_types_output_get_result (output, &error)) {
        g_printerr ("error: could not get NMEA types: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    /* Wait for response asynchronously */
    ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                             (GSourceFunc) get_nmea_types_timed_out,
                                             NULL);

    ctx->get_nmea_types_indication_id = g_signal_connect (ctx->client,
                                                          "get-nmea-types",
                                                          G_CALLBACK (get_nmea_types_received),
                                                          NULL);
}

#endif /* HAVE_QMI_MESSAGE_LOC_GET_NMEA_TYPES */

#if defined HAVE_QMI_MESSAGE_LOC_SET_NMEA_TYPES

static gboolean
set_nmea_types_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
set_nmea_types_received (QmiClientLoc                       *client,
                         QmiIndicationLocSetNmeaTypesOutput *output)
{
    QmiLocIndicationStatus status;
    g_autoptr(GError)      error = NULL;

    if (!qmi_indication_loc_set_nmea_types_output_get_indication_status (output, &status, &error)) {
        g_printerr ("error: couldn't set NMEA types: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    g_print ("Successfully set NMEA types\n");
    operation_shutdown (TRUE);
}

static void
set_nmea_types_ready (QmiClientLoc *client,
                      GAsyncResult *res)
{
    g_autoptr(QmiMessageLocSetNmeaTypesOutput) output = NULL;
    g_autoptr(GError)                          error = NULL;

    output = qmi_client_loc_set_nmea_types_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_set_nmea_types_output_get_result (output, &error)) {
        g_printerr ("error: could not set NMEA types: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    /* Wait for response asynchronously */
    ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                             (GSourceFunc) set_nmea_types_timed_out,
                                             NULL);

    ctx->set_nmea_types_indication_id = g_signal_connect (ctx->client,
                                                          "set-nmea-types",
                                                          G_CALLBACK (set_nmea_types_received),
                                                          NULL);
}

static QmiMessageLocSetNmeaTypesInput *
set_nmea_types_input_create (const gchar *str)
{
    g_autoptr(QmiMessageLocSetNmeaTypesInput) input = NULL;
    g_autoptr(GError)                         error = NULL;
    QmiLocNmeaType                            nmea_type_mask;

    if (!qmicli_read_loc_nmea_type_from_string (str, &nmea_type_mask)) {
        g_printerr ("error: couldn't parse input string as NMEA types: '%s'\n", str);
        return NULL;
    }

    input = qmi_message_loc_set_nmea_types_input_new ();
    if (!qmi_message_loc_set_nmea_types_input_set_nmea_types (input, nmea_type_mask, &error)) {
        g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
        return NULL;
    }

    return g_steal_pointer (&input);
}

#endif /* HAVE_QMI_MESSAGE_LOC_SET_NMEA_TYPES */

#if defined HAVE_QMI_MESSAGE_LOC_GET_OPERATION_MODE

static gboolean
get_operation_mode_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
get_operation_mode_received (QmiClientLoc                           *client,
                             QmiIndicationLocGetOperationModeOutput *output)
{
    QmiLocIndicationStatus  status;
    QmiLocOperationMode     operation_mode;
    g_autoptr(GError)       error = NULL;

    if (!qmi_indication_loc_get_operation_mode_output_get_indication_status (output, &status, &error)) {
        g_printerr ("error: couldn't get operation mode %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_indication_loc_get_operation_mode_output_get_operation_mode (output, &operation_mode, NULL)) {
        g_printerr ("error: couldn't get operation mode: missing\n");
        operation_shutdown (FALSE);
        return;
    }

    g_print ("Successfully retrieved operation mode: %s\n", qmi_loc_operation_mode_get_string (operation_mode));
    operation_shutdown (TRUE);
}

static void
get_operation_mode_ready (QmiClientLoc *client,
                          GAsyncResult *res)
{
    g_autoptr(QmiMessageLocGetOperationModeOutput) output = NULL;
    g_autoptr(GError)                              error = NULL;

    output = qmi_client_loc_get_operation_mode_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_get_operation_mode_output_get_result (output, &error)) {
        g_printerr ("error: could not get operation mode: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    /* Wait for response asynchronously */
    ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                             (GSourceFunc) get_operation_mode_timed_out,
                                             NULL);

    ctx->get_operation_mode_indication_id = g_signal_connect (ctx->client,
                                                              "get-operation-mode",
                                                              G_CALLBACK (get_operation_mode_received),
                                                              NULL);
}
#endif /* HAVE_QMI_MESSAGE_LOC_GET_OPERATION_MODE */

#if defined HAVE_QMI_MESSAGE_LOC_SET_OPERATION_MODE

static gboolean
set_operation_mode_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
set_operation_mode_received (QmiClientLoc                           *client,
                             QmiIndicationLocSetOperationModeOutput *output)
{
    QmiLocIndicationStatus status;
    g_autoptr(GError)      error = NULL;

    if (!qmi_indication_loc_set_operation_mode_output_get_indication_status (output, &status, &error)) {
        g_printerr ("error: couldn't set operation mode: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    g_print ("Successfully set operation mode\n");
    operation_shutdown (TRUE);
}

static void
set_operation_mode_ready (QmiClientLoc *client,
                          GAsyncResult *res)
{
    g_autoptr(QmiMessageLocSetOperationModeOutput) output = NULL;
    g_autoptr(GError)                              error = NULL;

    output = qmi_client_loc_set_operation_mode_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_set_operation_mode_output_get_result (output, &error)) {
        g_printerr ("error: could not set operation mode: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    /* Wait for response asynchronously */
    ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                             (GSourceFunc) set_operation_mode_timed_out,
                                             NULL);

    ctx->set_operation_mode_indication_id = g_signal_connect (ctx->client,
                                                              "set-operation-mode",
                                                              G_CALLBACK (set_operation_mode_received),
                                                              NULL);
}

static QmiMessageLocSetOperationModeInput *
set_operation_mode_input_create (const gchar *str)
{
    g_autoptr(QmiMessageLocSetOperationModeInput) input = NULL;
    g_autoptr(GError)                             error = NULL;
    QmiLocOperationMode                           operation_mode;

    if (!qmicli_read_loc_operation_mode_from_string (str, &operation_mode))
        return NULL;

    input = qmi_message_loc_set_operation_mode_input_new ();
    if (!qmi_message_loc_set_operation_mode_input_set_operation_mode (input, operation_mode, &error)) {
        g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
        return NULL;
    }

    return g_steal_pointer (&input);
}

#endif /* HAVE_QMI_MESSAGE_LOC_SET_OPERATION_MODE */

#if defined HAVE_QMI_MESSAGE_LOC_GET_ENGINE_LOCK

static gboolean
get_engine_lock_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
get_engine_lock_received (QmiClientLoc                        *client,
                          QmiIndicationLocGetEngineLockOutput *output)
{
    QmiLocIndicationStatus  status;
    QmiLocLockType          type;
    g_autoptr(GError)       error = NULL;

    if (!qmi_indication_loc_get_engine_lock_output_get_indication_status (output, &status, &error)) {
        g_printerr ("error: couldn't get engine lock %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_indication_loc_get_engine_lock_output_get_lock_type (output, &type, NULL)) {
        g_printerr ("error: couldn't get engine lock: missing\n");
        operation_shutdown (FALSE);
        return;
    }

    g_print ("Successfully retrieved engine lock: %s\n", qmi_loc_lock_type_get_string (type));
    operation_shutdown (TRUE);
}

static void
get_engine_lock_ready (QmiClientLoc *client,
                       GAsyncResult *res)
{
    g_autoptr(QmiMessageLocGetEngineLockOutput) output = NULL;
    g_autoptr(GError)                           error = NULL;

    output = qmi_client_loc_get_engine_lock_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_get_engine_lock_output_get_result (output, &error)) {
        g_printerr ("error: could not get engine lock: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    /* Wait for response asynchronously */
    ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                             (GSourceFunc) get_engine_lock_timed_out,
                                             NULL);

    ctx->get_engine_lock_indication_id = g_signal_connect (ctx->client,
                                                           "get-engine-lock",
                                                           G_CALLBACK (get_engine_lock_received),
                                                           NULL);
}
#endif /* HAVE_QMI_MESSAGE_LOC_GET_ENGINE_LOCK */

#if defined HAVE_QMI_MESSAGE_LOC_SET_ENGINE_LOCK

static gboolean
set_engine_lock_timed_out (void)
{
    ctx->timeout_id = 0;
    g_printerr ("error: operation failed: timeout\n");
    operation_shutdown (FALSE);
    return G_SOURCE_REMOVE;
}

static void
set_engine_lock_received (QmiClientLoc                        *client,
                          QmiIndicationLocSetEngineLockOutput *output)
{
    QmiLocIndicationStatus status;
    g_autoptr(GError)      error = NULL;

    if (!qmi_indication_loc_set_engine_lock_output_get_indication_status (output, &status, &error)) {
        g_printerr ("error: couldn't set engine lock: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    g_print ("Successfully set engine lock\n");
    operation_shutdown (TRUE);
}

static void
set_engine_lock_ready (QmiClientLoc *client,
                          GAsyncResult *res)
{
    g_autoptr(QmiMessageLocSetEngineLockOutput) output = NULL;
    g_autoptr(GError)                           error = NULL;

    output = qmi_client_loc_set_engine_lock_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_set_engine_lock_output_get_result (output, &error)) {
        g_printerr ("error: could not set engine lock: %s\n", error->message);
        operation_shutdown (FALSE);
        return;
    }

    /* Wait for response asynchronously */
    ctx->timeout_id = g_timeout_add_seconds (timeout > 0 ? timeout : DEFAULT_LOC_TIMEOUT_SECS,
                                             (GSourceFunc) set_engine_lock_timed_out,
                                             NULL);

    ctx->set_engine_lock_indication_id = g_signal_connect (ctx->client,
                                                           "set-engine-lock",
                                                           G_CALLBACK (set_engine_lock_received),
                                                           NULL);
}

static QmiMessageLocSetEngineLockInput *
set_engine_lock_input_create (const gchar *str)
{
    g_autoptr(QmiMessageLocSetEngineLockInput) input = NULL;
    g_autoptr(GError)                          error = NULL;
    QmiLocLockType                             type;

    if (!qmicli_read_loc_lock_type_from_string (str, &type))
        return NULL;

    input = qmi_message_loc_set_engine_lock_input_new ();
    if (!qmi_message_loc_set_engine_lock_input_set_lock_type (input, type, &error)) {
        g_printerr ("error: couldn't create input data bundle: '%s'\n", error->message);
        return NULL;
    }

    return g_steal_pointer (&input);
}

#endif /* HAVE_QMI_MESSAGE_LOC_SET_ENGINE_LOCK */

#if defined HAVE_QMI_MESSAGE_LOC_STOP

static void
stop_ready (QmiClientLoc *client,
            GAsyncResult *res)
{
    QmiMessageLocStopOutput *output;
    GError                  *error = NULL;

    output = qmi_client_loc_stop_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_stop_output_get_result (output, &error)) {
        g_printerr ("error: could not stop location tracking: %s\n", error->message);
        qmi_message_loc_stop_output_unref (output);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    g_print ("[%s] Successfully stopped location tracking (session id %u)\n",
             qmi_device_get_path_display (ctx->device), session_id);

    qmi_message_loc_stop_output_unref (output);
    operation_shutdown (TRUE);
}

#endif /* HAVE_QMI_MESSAGE_LOC_STOP */

#if defined HAVE_QMI_MESSAGE_LOC_START

static void
start_ready (QmiClientLoc *client,
             GAsyncResult *res)
{
    QmiMessageLocStartOutput *output;
    GError                   *error = NULL;

    output = qmi_client_loc_start_finish (client, res, &error);
    if (!output) {
        g_printerr ("error: operation failed: %s\n", error->message);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    if (!qmi_message_loc_start_output_get_result (output, &error)) {
        g_printerr ("error: could not start location tracking: %s\n", error->message);
        qmi_message_loc_start_output_unref (output);
        g_error_free (error);
        operation_shutdown (FALSE);
        return;
    }

    g_print ("[%s] Successfully started location tracking (session id %u)\n",
             qmi_device_get_path_display (ctx->device), session_id);

    qmi_message_loc_start_output_unref (output);
    operation_shutdown (TRUE);
}

#endif /* HAVE_QMI_MESSAGE_LOC_START */

static gboolean
noop_cb (gpointer unused)
{
    operation_shutdown (TRUE);
    return G_SOURCE_REMOVE;
}

void
qmicli_loc_run (QmiDevice    *device,
                QmiClientLoc *client,
                GCancellable *cancellable)
{
    /* Initialize context */
    ctx = g_slice_new0 (Context);
    ctx->device = g_object_ref (device);
    ctx->client = g_object_ref (client);
    ctx->cancellable = g_object_ref (cancellable);

#if defined HAVE_QMI_MESSAGE_LOC_START
    if (start_flag) {
        QmiMessageLocStartInput *input;

        input = qmi_message_loc_start_input_new ();
        qmi_message_loc_start_input_set_session_id (input, (guint8) session_id, NULL);
        qmi_message_loc_start_input_set_intermediate_report_state (input, QMI_LOC_INTERMEDIATE_REPORT_STATE_ENABLE, NULL);
        qmi_message_loc_start_input_set_minimum_interval_between_position_reports (input, 1000, NULL);
        qmi_message_loc_start_input_set_fix_recurrence_type (input, QMI_LOC_FIX_RECURRENCE_TYPE_REQUEST_PERIODIC_FIXES, NULL);
        qmi_client_loc_start (ctx->client,
                              input,
                              10,
                              ctx->cancellable,
                              (GAsyncReadyCallback) start_ready,
                              NULL);
        qmi_message_loc_start_input_unref (input);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_STOP
    if (stop_flag) {
        QmiMessageLocStopInput *input;

        input = qmi_message_loc_stop_input_new ();
        qmi_message_loc_stop_input_set_session_id (input, (guint8) session_id, NULL);
        qmi_client_loc_stop (ctx->client,
                             input,
                             10,
                             ctx->cancellable,
                             (GAsyncReadyCallback) stop_ready,
                             NULL);
        qmi_message_loc_stop_input_unref (input);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_DELETE_ASSISTANCE_DATA
    if (delete_assistance_data_flag) {
        QmiMessageLocDeleteAssistanceDataInput *input;

        input = qmi_message_loc_delete_assistance_data_input_new ();
        qmi_message_loc_delete_assistance_data_input_set_delete_all (input, TRUE, NULL);
        qmi_client_loc_delete_assistance_data (ctx->client,
                                               input,
                                               10,
                                               ctx->cancellable,
                                               (GAsyncReadyCallback) delete_assistance_data_ready,
                                               NULL);
        qmi_message_loc_delete_assistance_data_input_unref (input);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_GET_NMEA_TYPES
    if (get_nmea_types_flag) {
        qmi_client_loc_get_nmea_types (ctx->client,
                                       NULL,
                                       10,
                                       ctx->cancellable,
                                       (GAsyncReadyCallback) get_nmea_types_ready,
                                       NULL);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_SET_NMEA_TYPES
    if (set_nmea_types_str) {
        g_autoptr(QmiMessageLocSetNmeaTypesInput) input = NULL;

        g_debug ("Asynchronously setting APN type...");
        input = set_nmea_types_input_create (set_nmea_types_str);
        if (!input) {
            operation_shutdown (FALSE);
            return;
        }
        qmi_client_loc_set_nmea_types (ctx->client,
                                       input,
                                       10,
                                       ctx->cancellable,
                                       (GAsyncReadyCallback)set_nmea_types_ready,
                                       NULL);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_GET_OPERATION_MODE
    if (get_operation_mode_flag) {
        qmi_client_loc_get_operation_mode (ctx->client,
                                           NULL,
                                           10,
                                           ctx->cancellable,
                                           (GAsyncReadyCallback) get_operation_mode_ready,
                                           NULL);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_SET_OPERATION_MODE
    if (set_operation_mode_str) {
        g_autoptr(QmiMessageLocSetOperationModeInput) input = NULL;

        g_debug ("Asynchronously setting operation mode...");
        input = set_operation_mode_input_create (set_operation_mode_str);
        if (!input) {
            operation_shutdown (FALSE);
            return;
        }
        qmi_client_loc_set_operation_mode (ctx->client,
                                           input,
                                           10,
                                           ctx->cancellable,
                                           (GAsyncReadyCallback)set_operation_mode_ready,
                                           NULL);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_GET_ENGINE_LOCK
    if (get_engine_lock_flag) {
        qmi_client_loc_get_engine_lock (ctx->client,
                                        NULL,
                                        10,
                                        ctx->cancellable,
                                        (GAsyncReadyCallback) get_engine_lock_ready,
                                        NULL);
        return;
    }
#endif

#if defined HAVE_QMI_MESSAGE_LOC_SET_ENGINE_LOCK
    if (set_engine_lock_str) {
        g_autoptr(QmiMessageLocSetEngineLockInput) input = NULL;

        g_debug ("Asynchronously setting engine lock...");
        input = set_engine_lock_input_create (set_engine_lock_str);
        if (!input) {
            operation_shutdown (FALSE);
            return;
        }
        qmi_client_loc_set_engine_lock (ctx->client,
                                        input,
                                        10,
                                        ctx->cancellable,
                                        (GAsyncReadyCallback)set_engine_lock_ready,
                                        NULL);
        return;
    }
#endif

#if (defined HAVE_QMI_INDICATION_LOC_POSITION_REPORT || \
     defined HAVE_QMI_INDICATION_LOC_GNSS_SV_INFO ||    \
     defined HAVE_QMI_INDICATION_LOC_NMEA) &&           \
    defined HAVE_QMI_MESSAGE_LOC_REGISTER_EVENTS
    if (get_position_report_flag || get_gnss_sv_info_flag || follow_position_report_flag || follow_gnss_sv_info_flag || follow_nmea_flag) {
        /* All the remaining actions require monitoring */
        ctx->monitoring_step = MONITORING_STEP_FIRST;
        monitoring_step_run ();
        return;
    }
#endif

    /* Just client allocate/release? */
    if (noop_flag) {
        g_idle_add (noop_cb, NULL);
        return;
    }

    g_warn_if_reached ();
}

#endif /* HAVE_QMI_SERVICE_LOC */