forked from kristrev/data-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_input_netlink.c
804 lines (664 loc) · 29.8 KB
/
metadata_input_netlink.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
/* Copyright (c) 2015, Celerway, Kristian Evensen <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <libmnl/libmnl.h>
#include JSON_LOC
#include <getopt.h>
#include <sys/time.h>
#include "metadata_exporter.h"
#include "metadata_input_netlink.h"
#include "netlink_helpers.h"
#include "backend_event_loop.h"
#include "lib/minmea.h"
#include "metadata_exporter_log.h"
static uint8_t md_input_netlink_parse_conn_event(struct md_input_netlink *min,
struct json_object *meta_obj)
{
struct md_conn_event *mce = min->mce;
json_object_object_foreach(meta_obj, key, val) {
if (!strcmp(key, "md_seq"))
mce->sequence = (uint16_t) json_object_get_int(val);
if (!strcmp(key, "timestamp"))
mce->tstamp = json_object_get_int64(val);
if (!strcmp(key, "event_type"))
mce->event_type = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "event_param"))
mce->event_param = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "event_value"))
mce->event_value = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "event_value_str"))
mce->event_value_str = json_object_get_string(val);
if (!strcmp(key, "interface_id_type"))
mce->interface_id_type = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "interface_id"))
mce->interface_id = json_object_get_string(val);
if (!strcmp(key, "imei"))
mce->imei = json_object_get_string(val);
if (!strcmp(key, "imsi"))
mce->imsi = json_object_get_string(val);
if (!strcmp(key, "interface_name"))
mce->interface_name = json_object_get_string(val);
if (!strcmp(key, "interface_type"))
mce->interface_type = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "network_address_family"))
mce->network_address_family = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "network_address"))
mce->network_address = json_object_get_string(val);
if (!strcmp(key, "network_provider_type"))
mce->network_provider_type = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "network_provider"))
mce->network_provider = json_object_get_int(val);
if (!strcmp(key, "l3_session_id"))
mce->l3_session_id = (uint64_t) json_object_get_int64(val);
if (!strcmp(key, "l4_session_id"))
mce->l4_session_id = (uint64_t) json_object_get_int64(val);
if (!strcmp(key, "signal_strength"))
mce->signal_strength = (int8_t) json_object_get_int(val);
if (!strcmp(key, "rx_bytes"))
mce->rx_bytes = (uint64_t) json_object_get_int64(val);
if (!strcmp(key, "tx_bytes"))
mce->tx_bytes = (uint64_t) json_object_get_int64(val);
}
if (mce->event_param == CONN_EVENT_DATA_USAGE_UPDATE) {
if (!mce->tstamp || !mce->event_param || !mce->interface_id || (mce->imei && !mce->imsi) ||
(mce->imsi && !mce->imei)) {
META_PRINT_SYSLOG(min->parent, LOG_ERR, "Missing required argument in usage JSON\n");
return RETVAL_FAILURE;
} else {
return RETVAL_SUCCESS;
}
}
if (!mce->tstamp || !mce->sequence ||
!mce->l3_session_id || !mce->event_param ||
!mce->interface_type || !mce->network_address_family ||
!mce->network_address || !mce->interface_id ||
!mce->interface_id_type) {
META_PRINT_SYSLOG(min->parent, LOG_ERR, "Missing required argument in JSON\n");
return RETVAL_FAILURE;
}
//We need to update the value in case of a connection event update, since it
//is a string
//TODO: Implement a more elegant technique if we get more cases like this
if (mce->event_param == CONN_EVENT_META_UPDATE && !mce->event_value_str) {
META_PRINT_SYSLOG(min->parent, LOG_ERR, "Missing event value for connection update\n");
return RETVAL_FAILURE;
}
return RETVAL_SUCCESS;
}
static uint8_t md_input_netlink_parse_iface_event(struct md_input_netlink *min,
struct json_object *meta_obj, struct md_iface_event *mie)
{
json_object_object_foreach(meta_obj, key, val) {
if (!strcmp(key, "md_seq"))
mie->sequence = (uint16_t) json_object_get_int(val);
if (!strcmp(key, "timestamp"))
mie->tstamp = json_object_get_int64(val);
if (!strcmp(key, "event_param"))
mie->event_param = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "event_type"))
mie->event_type = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "iccid"))
mie->iccid = json_object_get_string(val);
if (!strcmp(key, "imsi"))
mie->imsi = json_object_get_string(val);
if (!strcmp(key, "imei"))
mie->imei = json_object_get_string(val);
if (!strcmp(key, "ip_addr"))
mie->ip_addr = json_object_get_string(val);
if (!strcmp(key, "internal_ip_addr"))
mie->internal_ip_addr = json_object_get_string(val);
if (!strcmp(key, "isp_name"))
mie->isp_name = json_object_get_string(val);
if (!strcmp(key, "ifname"))
mie->ifname = json_object_get_string(val);
if (!strcmp(key, "imsi_mccmnc"))
mie->imsi_mccmnc = (uint32_t) json_object_get_int(val);
if (!strcmp(key, "network_mccmnc"))
mie->nw_mccmnc = (uint32_t) json_object_get_int(val);
if (!strcmp(key, "cid"))
mie->cid = json_object_get_int(val);
if (!strcmp(key, "device_mode"))
mie->device_mode = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "device_sub_mode"))
mie->device_submode = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "rssi"))
mie->rssi = (int8_t) json_object_get_int(val);
if (!strcmp(key, "ecio"))
mie->ecio = (int8_t) json_object_get_int(val);
if (!strcmp(key, "rscp"))
mie->rscp = (int16_t) json_object_get_int(val);
if (!strcmp(key, "lte_rssi"))
mie->lte_rssi = (int8_t) json_object_get_int(val);
if (!strcmp(key, "lte_rsrp"))
mie->lte_rsrp = (int16_t) json_object_get_int(val);
if (!strcmp(key, "lte_rsrq"))
mie->lte_rsrq = (int8_t) json_object_get_int(val);
if (!strcmp(key, "lac"))
mie->lac = (uint16_t) json_object_get_int(val);
if (!strcmp(key, "lte_band"))
mie->lte_band = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "lte_freq"))
mie->lte_freq = (uint16_t) json_object_get_int(val);
if (!strcmp(key, "lte_pci"))
mie->lte_pci = (uint16_t) json_object_get_int(val);
if (!strcmp(key, "device_state"))
mie->device_state = (uint8_t) json_object_get_int(val);
if (!strcmp(key, "enodeb_id"))
mie->enodeb_id = json_object_get_int(val);
}
return RETVAL_SUCCESS;
}
static void md_input_netlink_handle_iface_event(struct md_input_netlink *min,
struct json_object *obj)
{
memset(min->mie, 0, sizeof(struct md_iface_event));
min->mie->md_type = META_TYPE_INTERFACE;
min->mie->lac = -1;
min->mie->cid = -1;
min->mie->rscp = (int16_t) META_IFACE_INVALID;
min->mie->lte_rsrp = (int16_t) META_IFACE_INVALID;
min->mie->rssi = (int8_t) META_IFACE_INVALID;
min->mie->ecio = (int8_t) META_IFACE_INVALID;
min->mie->lte_rssi = (int8_t) META_IFACE_INVALID;
min->mie->lte_rsrq = (int8_t) META_IFACE_INVALID;
min->mie->lte_pci = 0xFFFF;
min->mie->enodeb_id = -1;
if (md_input_netlink_parse_iface_event(min, obj, min->mie) == RETVAL_FAILURE)
return;
mde_publish_event_obj(min->parent, (struct md_event*) min->mie);
}
static void md_input_netlink_radio_cell_loc_geran(struct md_input_netlink *min,
struct json_object *obj)
{
struct md_radio_cell_loc_geran_event *event = calloc(sizeof(struct md_radio_cell_loc_geran_event), 1);
if (!event)
return;
json_object_object_foreach(obj, key, val) {
if (!strcmp(key, "md_seq"))
event->sequence = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "timestamp"))
event->tstamp = json_object_get_int64(val);
else if (!strcmp(key, "event_param"))
event->event_param = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "event_type"))
event->md_type = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "iccid"))
event->iccid = json_object_get_string(val);
else if (!strcmp(key, "imsi"))
event->imsi = json_object_get_string(val);
else if (!strcmp(key, "imei"))
event->imei = json_object_get_string(val);
else if (!strcmp(key, "cell_id"))
event->cell_id = (uint32_t) json_object_get_int(val);
else if (!strcmp(key, "plmn"))
event->plmn = json_object_get_string(val);
else if (!strcmp(key, "lac"))
event->lac = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "arfcn"))
event->arfcn = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "bsic"))
event->bsic = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "timing_advance"))
event->timing_advance = (uint32_t) json_object_get_int(val);
else if (!strcmp(key, "rx_lev"))
event->rx_lev = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "cell_geran_info_nmr"))
event->cell_geran_info_nmr = json_object_to_json_string_ext(val, JSON_C_TO_STRING_PLAIN);
}
mde_publish_event_obj(min->parent, (struct md_event*) event);
free(event);
}
static void md_input_netlink_radio_grr_cell_resel(struct md_input_netlink *min,
struct json_object *obj)
{
struct md_radio_grr_cell_resel_event *event = calloc(sizeof(struct md_radio_grr_cell_resel_event), 1);
struct json_object *obj_tmp;
uint8_t neigh_count = 0;
if (!event)
return;
json_object_object_foreach(obj, key, val) {
if (!strcmp(key, "md_seq"))
event->sequence = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "timestamp"))
event->tstamp = json_object_get_int64(val);
else if (!strcmp(key, "event_param"))
event->event_param = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "event_type"))
event->md_type = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "iccid"))
event->iccid = json_object_get_string(val);
else if (!strcmp(key, "imsi"))
event->imsi = json_object_get_string(val);
else if (!strcmp(key, "imei"))
event->imei = json_object_get_string(val);
else if (!strcmp(key, "serving_bcch_arfcn"))
event->serving_bcch_arfcn = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "serving_pbcch_arfcn"))
event->serving_pbcch_arfcn = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "serving_priority_class"))
event->serving_priority_class = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "serving_rxlev_avg"))
event->serving_rxlev_avg = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "serving_c1"))
event->serving_c1 = (uint32_t) json_object_get_int(val);
else if (!strcmp(key, "serving_c2"))
event->serving_c2 = (uint32_t) json_object_get_int(val);
else if (!strcmp(key, "serving_c31"))
event->serving_c31 = (uint32_t) json_object_get_int(val);
else if (!strcmp(key, "serving_c32"))
event->serving_c32 = (uint32_t) json_object_get_int(val);
else if (!strcmp(key, "serving_five_second_timer"))
event->serving_five_second_timer = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "cell_reselet_status"))
event->cell_reselet_status = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "recent_cell_selection"))
event->recent_cell_selection = (uint8_t) json_object_get_int(val);
}
json_object_object_get_ex(obj, "neighbor_cell_count", &obj_tmp);
if (obj_tmp)
neigh_count = json_object_get_int(obj_tmp);
if (neigh_count) {
json_object_object_get_ex(obj, "grr_cell_neighbor", &obj_tmp);
if (obj_tmp)
event->neighbors = json_object_to_json_string_ext(obj_tmp, JSON_C_TO_STRING_PLAIN);
}
mde_publish_event_obj(min->parent, (struct md_event*) event);
free(event);
}
static void md_input_netlink_radio_gsm_rr_cell_sel_reset_param(struct md_input_netlink *min,
struct json_object *obj)
{
struct md_radio_gsm_rr_cell_sel_reset_param_event *event = calloc(sizeof(struct md_radio_gsm_rr_cell_sel_reset_param_event), 1);
if (!event)
return;
json_object_object_foreach(obj, key, val) {
if (!strcmp(key, "md_seq"))
event->sequence = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "timestamp"))
event->tstamp = json_object_get_int64(val);
else if (!strcmp(key, "event_param"))
event->event_param = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "event_type"))
event->md_type = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "iccid"))
event->iccid = json_object_get_string(val);
else if (!strcmp(key, "imsi"))
event->imsi = json_object_get_string(val);
else if (!strcmp(key, "imei"))
event->imei = json_object_get_string(val);
else if (!strcmp(key, "cell_reselect_hysteresis"))
event->cell_reselect_hysteresis = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "ms_txpwr_max_cch"))
event->ms_txpwr_max_cch = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "rxlev_access_min"))
event->rxlev_access_min = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "power_offset_valid"))
event->power_offset_valid = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "power_offset"))
event->power_offset = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "neci"))
event->neci = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "acs"))
event->acs = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "opt_reselect_param_ind"))
event->opt_reselect_param_ind = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "cell_bar_qualify"))
event->cell_bar_qualify = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "cell_reselect_offset"))
event->cell_reselect_offset = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "temporary_offset"))
event->temporary_offset = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "penalty_time"))
event->penalty_time = (uint8_t) json_object_get_int(val);
}
mde_publish_event_obj(min->parent, (struct md_event*) event);
free(event);
}
static void md_input_netlink_radio_gsm_rr_cipher_mode(struct md_input_netlink *min,
struct json_object *obj)
{
struct md_radio_gsm_rr_cipher_mode_event *event = calloc(sizeof(struct md_radio_gsm_rr_cipher_mode_event), 1);
if (!event)
return;
json_object_object_foreach(obj, key, val) {
if (!strcmp(key, "md_seq"))
event->sequence = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "timestamp"))
event->tstamp = json_object_get_int64(val);
else if (!strcmp(key, "event_param"))
event->event_param = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "event_type"))
event->md_type = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "iccid"))
event->iccid = json_object_get_string(val);
else if (!strcmp(key, "imsi"))
event->imsi = json_object_get_string(val);
else if (!strcmp(key, "imei"))
event->imei = json_object_get_string(val);
else if (!strcmp(key, "ciphering_state"))
event->ciphering_state = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "ciphering_algorithm"))
event->ciphering_algorithm = (uint8_t) json_object_get_int(val);
}
mde_publish_event_obj(min->parent, (struct md_event*) event);
free(event);
}
static void md_input_netlink_radio_gsm_rr_channel_conf(struct md_input_netlink *min,
struct json_object *obj)
{
struct md_radio_gsm_rr_channel_conf_event *event = calloc(sizeof(struct md_radio_gsm_rr_channel_conf_event), 1);
if (!event)
return;
json_object_object_foreach(obj, key, val) {
if (!strcmp(key, "md_seq"))
event->sequence = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "timestamp"))
event->tstamp = json_object_get_int64(val);
else if (!strcmp(key, "event_param"))
event->event_param = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "event_type"))
event->md_type = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "iccid"))
event->iccid = json_object_get_string(val);
else if (!strcmp(key, "imsi"))
event->imsi = json_object_get_string(val);
else if (!strcmp(key, "imei"))
event->imei = json_object_get_string(val);
else if (!strcmp(key, "num_ded_chans;"))
event->num_ded_chans = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "dtx_indicator"))
event->dtx_indicator = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "power_level"))
event->power_level = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "starting_time_valid"))
event->starting_time_valid = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "starting_time"))
event->starting_time = (uint16_t) json_object_get_int(val);
else if (!strcmp(key, "cipher_flag"))
event->cipher_flag = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "cipher_algorithm"))
event->cipher_algorithm = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "channel_mode_1"))
event->channel_mode_1 = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "channel_mode_2"))
event->channel_mode_2 = (uint8_t) json_object_get_int(val);
else if (!strcmp(key, "after_channel_config"))
event->after_channel_config = json_object_get_string(val);
else if (!strcmp(key, "before_channel_config"))
event->before_channel_config = json_object_get_string(val);
}
mde_publish_event_obj(min->parent, (struct md_event*) event);
free(event);
}
static void md_input_netlink_handle_radio_event(struct md_input_netlink *min,
struct json_object *obj)
{
//struct md_iface_event mie;
uint8_t retval = 0;
json_object *event_param_json;
uint8_t event_param;
if (!json_object_object_get_ex(obj, "event_param", &event_param_json)) {
META_PRINT_SYSLOG(min->parent, LOG_ERR, "Missing event type\n");
return;
}
event_param = (uint8_t) json_object_get_int(event_param_json);
switch (event_param) {
case RADIO_EVENT_GSM_RR_CIPHER_MODE:
META_PRINT_SYSLOG(min->parent, LOG_ERR, "GSM_RR_CIPHER_MODE\n");
md_input_netlink_radio_gsm_rr_cipher_mode(min, obj);
break;
case RADIO_EVENT_GSM_RR_CHANNEL_CONF:
META_PRINT_SYSLOG(min->parent, LOG_ERR, "GSM_RR_CHANNEL_CONF\n");
md_input_netlink_radio_gsm_rr_channel_conf(min, obj);
break;
case RADIO_EVENT_CELL_LOCATION_GERAN:
META_PRINT_SYSLOG(min->parent, LOG_ERR, "CELL_LOCATION_GERAN\n");
md_input_netlink_radio_cell_loc_geran(min, obj);
break;
case RADIO_EVENT_GSM_RR_CELL_SEL_RESEL_PARAM:
META_PRINT_SYSLOG(min->parent, LOG_ERR, "GSM_RR_CELL_SEL_RESEL_PARAM\n");
md_input_netlink_radio_gsm_rr_cell_sel_reset_param(min, obj);
break;
case RADIO_EVENT_GRR_CELL_RESEL:
META_PRINT_SYSLOG(min->parent, LOG_ERR, "GRR_CELL_RESEL\n");
md_input_netlink_radio_grr_cell_resel(min, obj);
break;
default:
break;
}
}
static void md_input_netlink_handle_conn_event(struct md_input_netlink *min,
struct json_object *obj)
{
uint8_t retval = 0;
memset(min->mce, 0, sizeof(struct md_conn_event));
min->mce->md_type = META_TYPE_CONNECTION;
//255 is reserved value used to indicate that there is no value to export
//(look at writers)
min->mce->event_value = UINT8_MAX;
retval = md_input_netlink_parse_conn_event(min, obj);
if (retval == RETVAL_FAILURE)
return;
mde_publish_event_obj(min->parent, (struct md_event*) min->mce);
}
static void md_input_netlink_handle_gps_event(struct md_input_netlink *min,
struct json_object *json_obj)
{
struct md_gps_event gps_event;
uint8_t retval = 0;
int8_t sentence_id = 0;
union {
struct minmea_sentence_gga gga;
struct minmea_sentence_rmc rmc;
} gps;
memset(&gps_event, 0, sizeof(struct md_gps_event));
gps_event.md_type = META_TYPE_POS;
json_object_object_foreach(json_obj, key, val) {
if (!strcmp(key, "md_seq"))
gps_event.sequence = (uint16_t) json_object_get_int(val);
if (!strcmp(key, "timestamp"))
gps_event.tstamp_tv.tv_sec = json_object_get_int64(val);
if (!strcmp(key, "nmea_string"))
gps_event.nmea_raw = json_object_get_string(val);
}
if (!gps_event.sequence || !gps_event.nmea_raw)
return;
sentence_id = minmea_sentence_id(gps_event.nmea_raw, 0);
if (sentence_id <= 0)
return;
gps_event.minmea_id = sentence_id;
//We can ignore NMEA checksum
switch (sentence_id) {
case MINMEA_SENTENCE_GGA:
retval = minmea_parse_gga(&gps.gga, gps_event.nmea_raw);
if (retval && !gps.gga.fix_quality) {
retval = 0;
} else {
gps_event.time = gps.gga.time;
gps_event.latitude = minmea_tocoord(&gps.gga.latitude);
gps_event.longitude = minmea_tocoord(&gps.gga.longitude);
gps_event.altitude = minmea_tofloat(&gps.gga.altitude);
gps_event.satellites_tracked = gps.gga.satellites_tracked;
}
break;
case MINMEA_SENTENCE_RMC:
retval = minmea_parse_rmc(&gps.rmc, gps_event.nmea_raw);
if (retval && !gps.rmc.valid) {
retval = 0;
} else {
gps_event.time = gps.rmc.time;
gps_event.latitude = minmea_tocoord(&gps.rmc.latitude);
gps_event.longitude = minmea_tocoord(&gps.rmc.longitude);
gps_event.speed = minmea_tofloat(&gps.rmc.speed);
}
break;
default:
break;
}
if (!retval)
return;
mde_publish_event_obj(min->parent, (struct md_event *) &gps_event);
}
static uint8_t md_input_netlink_add_json_key_value(const char *key,
int32_t value, struct json_object *obj)
{
struct json_object *obj_add = NULL;
obj_add = json_object_new_int(value);
if (!obj_add)
return RETVAL_FAILURE;
json_object_object_add(obj, key, obj_add);
return RETVAL_SUCCESS;
}
static void md_input_netlink_handle_event(void *ptr, int32_t fd, uint32_t events)
{
struct md_input_netlink *min = ptr;
uint8_t rcv_buf[MNL_SOCKET_BUFFER_SIZE];
ssize_t retval;
struct nlmsghdr *nlh = (struct nlmsghdr*) rcv_buf;
const char *nlh_payload;
struct json_object *nlh_obj = NULL, *json_event = NULL;
uint8_t event_type = 0;
memset(rcv_buf, 0, sizeof(rcv_buf));
//TODO: Consider adding support for fragmented data
retval = mnl_socket_recvfrom(min->metadata_sock, rcv_buf,
MNL_SOCKET_BUFFER_SIZE);
if (retval <= 0)
return;
nlh_payload = mnl_nlmsg_get_payload(nlh);
nlh_obj = json_tokener_parse(nlh_payload);
if (!nlh_obj) {
META_PRINT_SYSLOG(min->parent, LOG_ERR, "Received invalid JSON object on Netlink socket\n");
return;
}
//We are inserting version and sequence number. Version is so that the
//application handling this data knows if it supports the format or not.
//Sequence is so that we can see the order in which events arrived at the
//metadata exporter, making it easier to correlate events between
//applications. The different applications publishing data might also insert
//their own sequence number
if (md_input_netlink_add_json_key_value("md_seq", mde_inc_seq(min->parent), nlh_obj) ||
md_input_netlink_add_json_key_value("md_ver", MDE_VERSION, nlh_obj)) {
json_object_put(nlh_obj);
return;
}
if (!json_object_object_get_ex(nlh_obj, "event_type", &json_event)) {
META_PRINT_SYSLOG(min->parent, LOG_ERR, "Missing event type\n");
json_object_put(nlh_obj);
return;
}
event_type = (uint8_t) json_object_get_int(json_event);
if (!(event_type & min->md_nl_mask)) {
json_object_put(nlh_obj);
return;
}
META_PRINT(min->parent->logfile, "Got JSON %s\n", json_object_to_json_string(nlh_obj));
switch (event_type) {
case META_TYPE_INTERFACE:
md_input_netlink_handle_iface_event(min, nlh_obj);
break;
case META_TYPE_CONNECTION:
md_input_netlink_handle_conn_event(min, nlh_obj);
break;
case META_TYPE_POS:
md_input_netlink_handle_gps_event(min, nlh_obj);
break;
case META_TYPE_RADIO:
md_input_netlink_handle_radio_event(min, nlh_obj);
break;
default:
META_PRINT(min->parent->logfile, "Unknown event type\n");
break;
}
json_object_put(nlh_obj);
}
static uint8_t md_input_netlink_config(struct md_input_netlink *min)
{
//TODO: This code will be refactored to different importers, similar to
//outputs
min->metadata_sock = nlhelper_create_socket(NETLINK_USERSOCK,
(1 << (METADATA_NL_GROUP - 1)));
if (min->metadata_sock == NULL)
return RETVAL_FAILURE;
if(!(min->event_handle = backend_create_epoll_handle(min,
mnl_socket_get_fd(min->metadata_sock),
md_input_netlink_handle_event)))
return RETVAL_FAILURE;
backend_event_loop_update(min->parent->event_loop, EPOLLIN, EPOLL_CTL_ADD,
mnl_socket_get_fd(min->metadata_sock), min->event_handle);
//TODO: Move to handler
min->mce = calloc(sizeof(struct md_conn_event), 1);
if (min->mce == NULL)
return RETVAL_FAILURE;
min->mie = calloc(sizeof(struct md_iface_event), 1);
if (min->mie == NULL)
return RETVAL_FAILURE;
min->mre = calloc(sizeof(struct md_radio_event), 1);
if (min->mre == NULL)
return RETVAL_FAILURE;
return RETVAL_SUCCESS;
}
static uint8_t md_input_netlink_init(void *ptr, json_object* config)
{
struct md_input_netlink *min = ptr;
uint32_t md_nl_mask = 0;
json_object* subconfig;
if (json_object_object_get_ex(config, "netlink", &subconfig)) {
json_object_object_foreach(subconfig, key, val) {
if (!strcmp(key, "conn"))
md_nl_mask |= META_TYPE_CONNECTION;
if (!strcmp(key, "pos"))
md_nl_mask |= META_TYPE_POS;
if (!strcmp(key, "iface"))
md_nl_mask |= META_TYPE_INTERFACE;
if (!strcmp(key, "radio"))
md_nl_mask |= META_TYPE_RADIO;
}
}
if (!md_nl_mask) {
META_PRINT_SYSLOG(min->parent, LOG_ERR, "At least one netlink event type must be present\n");
return RETVAL_FAILURE;
}
min->md_nl_mask = md_nl_mask;
//Netlink has no arguments (for now), so just go right to config
return md_input_netlink_config(min);
}
void md_netlink_usage()
{
fprintf(stderr, "\"netlink\": {\t\tNetlink input (at least one event type must be present)\n");
fprintf(stderr, " \"conn\":\t\tReceive netlink connection events\n");
fprintf(stderr, " \"pos\":\t\tReceive netlink position events\n");
fprintf(stderr, " \"iface\":\t\tReceive netlink interface events\n");
fprintf(stderr, " \"radio\":\t\tReceive netlink radio events (QXDM + neigh. cells)\n");
fprintf(stderr, "},\n");
}
void md_netlink_setup(struct md_exporter *mde, struct md_input_netlink *min)
{
min->parent = mde;
min->init = md_input_netlink_init;
}