-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.c
3441 lines (2916 loc) · 84.4 KB
/
scan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Simple MPEG parser to achieve network/service information.
*
* refered standards:
*
* ETSI EN 300 468
* ETSI TR 101 211
* ETSI ETR 211
* ITU-T H.222.0
*
* 2005-05-10 - Basic ATSC PSIP parsing support added
* ATSC Standard Revision B (A65/B)
*
* Thanks to Sean Device from Triveni for providing access to ATSC signals
* and to Kevin Fowlks for his independent ATSC scanning tool.
*
* Please contribute: It is possible that some descriptors for ATSC are
* not parsed yet and thus the result won't be complete.
*/
#include <stdlib.h>
#include <stdio.h>
#include <iconv.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include <glob.h>
#include <ctype.h>
#include "list.h"
#include "diseqc.h"
#include "dump-zap.h"
#include "dump-vdr.h"
#include "scan.h"
#include "lnb.h"
#include "bouquet.h"
#include "atsc_psip_section.h"
#define CRC_LEN 4
enum pid {
PID_PAT = 0x0000,
PID_CAT = 0x0001,
PID_TSDT = 0x0002,
PID_NIT_ST = 0x0010,
PID_SDT_BAT_ST = 0x0011,
PID_EIT_STCIT = 0x0012,
PID_RST_ST = 0x0013,
PID_TDT_TOT_ST = 0x0014,
PID_NET_SYNC = 0x0015,
PID_RNT = 0x0016,
PID_INBAND_SIG = 0x001C,
PID_MEASUREMENT = 0x001D,
PID_DIT = 0x001E,
PID_SIT = 0x001F,
};
enum table_id {
TID_PAT = 0x00, // Program association table
TID_CAT = 0x01, // Conditional access table
TID_PMT = 0x02, // Program map table
TID_SDT = 0x03, // Stream description table
// 0x04 .. 0x3F - Reserved
TID_NIT_ACTUAL = 0x40, // Network information table - actual network
TID_NIT_OTHER = 0x41, // Network information table - other network
TID_SDT_ACTUAL = 0x42, // Service description table - actual stream
// 0x43 .. 0x45 - Reserved
TID_SDT_OTHER = 0x46, // Service description table - other stream
// 0x47 .. 0x49 - Reserved
TID_BAT = 0x4A, // Bouquet association table
// 0x4B .. 0x4D - Reserved
TID_EIT_ACTUAL = 0x4E, // Event information table - actual stream - present/following
TID_EIT_OTHER = 0x4F, // Event information table - other stream - present/following
// 0x50 .. 0x5F // Event information table - actual stream - schedule
// 0x60 .. 0x6F // Event information table - other stream - schedule
TID_TDT = 0x70, // Time date table
TID_RST = 0x71, // Running status table
TID_ST = 0x72, // Stuffing table
TID_TOT = 0x73, // Time offset table
TID_AIT = 0x74, // Application information table
TID_CT = 0x75, // Container table
TID_RCT = 0x76, // Related content table
TID_CIT = 0x77, // Content identifier table
TID_MPE_FEC = 0x78, // MPE-FEC table
TID_RNT = 0x79, // Resolution notification table
// 0x7A .. 0x7D - Reserved
TID_DIT = 0x7E, // Discountinuity information table
TID_SIT = 0x7F, // Selection information table
// 0x80 .. 0xFE - User defined
TID_ATSC_CVT1 = 0xC8,
TID_ATSC_CVT2 = 0xC9,
// 0xFF - Reserved
};
enum table_type {
PAT,
PMT,
SDT,
NIT
};
static char demux_devname[80];
// Configuration parameters
int verbosity = 2;
static int scan_iterations = 10;
static int skip_count = 0;
static int long_timeout;
static int current_tp_only;
static int get_other_nits;
static int noauto=0;
static int vdr_dump_provider;
static int vdr_dump_channum;
static int no_ATSC_PSIP;
static int ATSC_type=1;
static int ca_select = -1;
static int serv_select = 7;
static struct lnb_types_st lnb_type;
static int unique_anon_services;
static fe_spectral_inversion_t spectral_inversion = INVERSION_AUTO;
static int switch_pos = 0;
static int uncommitted_switch_pos = 0;
static int rotor_pos = 0;
static int curr_rotor_pos = 0;
static char rotor_pos_name[16] = "";
static char override_orbital_pos[16] = "";
enum format output_format = OUTPUT_VDR;
static int output_format_set = 0;
static int disable_s1 = FALSE;
static int disable_s2 = FALSE;
static int use_bouquets = 0;
static rotorslot_t rotor[49];
struct section_buf {
struct list_head list;
const char *dmx_devname;
unsigned int run_once : 1;
unsigned int segmented : 1; /* segmented by table_id_ext */
int fd;
enum pid pid;
enum table_id table_id;
int table_id_ext;
int section_version_number;
uint8_t section_done[32];
int sectionfilter_done;
time_t timeout;
time_t start_time;
time_t running_time;
struct section_buf *next_seg; /* this is used to handle
* segmented tables (like NIT-other)
*/
int skip_count;
};
static LIST_HEAD(scanned_transponders);
static LIST_HEAD(new_transponders);
static struct transponder *current_tp;
static struct bouquet_ctx *bouquets = NULL;
static void dump_dvb_parameters (FILE *f, struct transponder *p);
static void setup_filter (struct section_buf* s, const char *dmx_devname,
enum pid pid, enum table_id tid, int tid_ext,
int run_once, int segmented, int timeout);
static void add_filter (struct section_buf *s);
int rotor_nn(int orbital_pos, int we_flag);
/* According to the DVB standards, the combination of network_id and
* transport_stream_id should be unique, but in real life the satellite
* operators and broadcasters don't care enough to coordinate
* the numbering. Thus we identify TPs by frequency (dvbscan handles only
* one satellite at a time). Further complication: Different NITs on
* one satellite sometimes list the same TP with slightly different
* frequencies, so we have to search within some bandwidth.
*/
static struct transponder *alloc_transponder(uint32_t frequency)
{
struct transponder *tp = calloc(1, sizeof(*tp));
memset(tp, 0, sizeof(*tp));
tp->frequency = frequency;
INIT_LIST_HEAD(&tp->list);
INIT_LIST_HEAD(&tp->services);
list_add_tail(&tp->list, &new_transponders);
return tp;
}
static int is_same_frequency(uint32_t f1, uint32_t f2)
{
uint32_t diff;
if (f1 == f2)
return 1;
diff = (f1 > f2) ? (f1 - f2) : (f2 - f1);
//FIXME: use symbolrate etc. to estimate bandwidth
if (diff < 2000) {
debug("f1 = %u is same TP as f2 = %u\n", f1, f2);
return 1;
}
return 0;
}
static int is_same_transponder(struct transponder *t1, struct transponder *t2)
{
if(is_same_frequency(t1->frequency, t2->frequency) && t1->polarisation == t2->polarisation && t1->stream_id == t2->stream_id) {
return 1;
}
else {
return 0;
}
}
static struct transponder *find_transponder_by_freq(uint32_t frequency)
{
struct list_head *pos;
struct transponder *tp;
list_for_each(pos, &scanned_transponders) {
tp = list_entry(pos, struct transponder, list);
if (current_tp_only)
return tp;
if (is_same_frequency(tp->frequency, frequency))
return tp;
}
list_for_each(pos, &new_transponders) {
tp = list_entry(pos, struct transponder, list);
if (is_same_frequency(tp->frequency, frequency))
return tp;
}
return NULL;
}
static struct transponder *find_transponder(uint32_t frequency, enum polarisation pol)
{
struct list_head *pos;
struct transponder *tp;
list_for_each(pos, &scanned_transponders) {
tp = list_entry(pos, struct transponder, list);
if (current_tp_only)
return tp;
if (is_same_frequency(tp->frequency, frequency) && tp->polarisation == pol)
return tp;
}
list_for_each(pos, &new_transponders) {
tp = list_entry(pos, struct transponder, list);
if (is_same_frequency(tp->frequency, frequency) && tp->polarisation == pol)
return tp;
}
return NULL;
}
static void remove_duplicate_transponder(struct transponder *t)
{
struct list_head *pos;
struct transponder *tp;
list_for_each(pos, &new_transponders) {
tp = list_entry(pos, struct transponder, list);
if (is_same_transponder(tp, t) && tp != t) {
pos = tp->list.prev;
list_del_init(&tp->list);
}
}
}
static void copy_transponder(struct transponder *d, struct transponder *s, int isOverride)
{
d->network_id = s->network_id;
d->original_network_id = s->original_network_id;
d->transport_stream_id = s->transport_stream_id;
d->frequency = s->frequency;
d->symbol_rate = s->symbol_rate;
d->inversion = s->inversion;
if(isOverride || d->rolloff == ROLLOFF_AUTO) {
d->rolloff = s->rolloff;
}
if(isOverride || d->fec == FEC_AUTO) {
d->fec = s->fec;
}
if(isOverride || d->fecHP == FEC_AUTO) {
d->fecHP = s->fecHP;
}
if(isOverride || d->fecLP == FEC_AUTO) {
d->fecLP = s->fecLP;
}
if(isOverride || d->modulation == QAM_AUTO) {
d->modulation = s->modulation;
}
if(isOverride || d->bandwidth == BANDWIDTH_AUTO) {
d->bandwidth = s->bandwidth;
}
if(isOverride || d->hierarchy == HIERARCHY_AUTO) {
d->hierarchy = s->hierarchy;
}
if(isOverride || d->guard_interval == GUARD_INTERVAL_AUTO) {
d->guard_interval = s->guard_interval;
}
if(isOverride || d->transmission_mode == TRANSMISSION_MODE_AUTO) {
d->transmission_mode = s->transmission_mode;
}
d->polarisation = s->polarisation;
d->orbital_pos = s->orbital_pos;
d->delivery_system = s->delivery_system;
d->we_flag = s->we_flag;
d->scan_done = s->scan_done;
d->last_tuning_failed = s->last_tuning_failed;
d->other_frequency_flag = s->other_frequency_flag;
d->n_other_f = s->n_other_f;
if (d->n_other_f) {
d->other_f = calloc(d->n_other_f, sizeof(uint32_t));
memcpy(d->other_f, s->other_f, d->n_other_f * sizeof(uint32_t));
}
else
d->other_f = NULL;
}
/* service_ids are guaranteed to be unique within one TP
* (the DVB standards say theay should be unique within one
* network, but in real life...)
*/
static struct service *alloc_service(struct transponder *tp, int service_id)
{
struct service *s = calloc(1, sizeof(*s));
INIT_LIST_HEAD(&s->list);
s->service_id = service_id;
list_add_tail(&s->list, &tp->services);
return s;
}
static struct service *find_service(struct transponder *tp, int service_id)
{
struct list_head *pos;
struct service *s;
list_for_each(pos, &tp->services) {
s = list_entry(pos, struct service, list);
if (s->service_id == service_id)
return s;
}
return NULL;
}
static void parse_ca_identifier_descriptor (const unsigned char *buf, struct service *s)
{
unsigned char len = buf [1];
unsigned int i;
buf += 2;
if (len > sizeof(s->ca_id)) {
len = sizeof(s->ca_id);
warning("too many CA system ids\n");
}
s->ca_num = 0;
memcpy(s->ca_id, buf, len);
for (i = 0; i < len / sizeof(s->ca_id[0]); i++) {
int id = ((s->ca_id[i] & 0x00FF) << 8) + ((s->ca_id[i] & 0xFF00) >> 8);
s->ca_id[i] = id;
info(" CA ID 0x%04X\n", s->ca_id[i]);
s->ca_num++;
}
}
static void parse_iso639_language_descriptor (const unsigned char *buf, struct service *s)
{
unsigned char len = buf [1];
buf += 2;
if (len >= 4) {
debug(" LANG=%.3s %d\n", buf, buf[3]);
memcpy(s->audio_lang[s->audio_num], buf, 3);
s->audio_lang[s->audio_num][3] = '\0';
#if 0
/* seems like the audio_type is wrong all over the place */
//if (buf[3] == 0) -> normal
if (buf[3] == 1)
s->audio_lang[s->audio_num][3] = '!'; /* clean effects (no language) */
else if (buf[3] == 2)
s->audio_lang[s->audio_num][3] = '?'; /* for the hearing impaired */
else if (buf[3] == 3)
s->audio_lang[s->audio_num][3] = '+'; /* visually impaired commentary */
#endif
}
}
static void parse_network_name_descriptor (const unsigned char *buf, void *dummy)
{
(void)dummy;
unsigned char len = buf [1];
info("Network Name '%.*s'\n", len, buf + 2);
}
static void parse_terrestrial_uk_channel_number (const unsigned char *buf, void *dummy)
{
(void)dummy;
int i, n, channel_num, service_id;
struct list_head *p1, *p2;
struct transponder *t;
struct service *s;
// 32 bits per record
n = buf[1] / 4;
if (n < 1)
return;
// desc id, desc len, (service id, service number)
buf += 2;
for (i = 0; i < n; i++) {
service_id = (buf[0]<<8)|(buf[1]&0xff);
channel_num = ((buf[2]&0x03)<<8)|(buf[3]&0xff);
debug("Service ID 0x%X has channel number %d ", service_id, channel_num);
list_for_each(p1, &scanned_transponders) {
t = list_entry(p1, struct transponder, list);
list_for_each(p2, &t->services) {
s = list_entry(p2, struct service, list);
if (s->service_id == service_id)
s->channel_num = channel_num;
}
}
buf += 4;
}
}
static long bcd32_to_cpu (const int b0, const int b1, const int b2, const int b3)
{
return ((b0 >> 4) & 0x0f) * 10000000 + (b0 & 0x0f) * 1000000 +
((b1 >> 4) & 0x0f) * 100000 + (b1 & 0x0f) * 10000 +
((b2 >> 4) & 0x0f) * 1000 + (b2 & 0x0f) * 100 +
((b3 >> 4) & 0x0f) * 10 + (b3 & 0x0f);
}
static void parse_cable_delivery_system_descriptor (const unsigned char *buf, struct transponder *t)
{
static const fe_code_rate_t fec_tab [8] = {
FEC_AUTO, FEC_1_2, FEC_2_3, FEC_3_4,
FEC_5_6, FEC_7_8, FEC_NONE, FEC_NONE
};
static const fe_modulation_t qam_tab [6] = {
QAM_AUTO, QAM_16, QAM_32, QAM_64, QAM_128, QAM_256
};
if (!t) {
warning("cable_delivery_system_descriptor outside transport stream definition (ignored)\n");
return;
}
t->delivery_system = SYS_DVBC_ANNEX_AC;
t->frequency = bcd32_to_cpu (buf[2], buf[3], buf[4], buf[5]);
t->frequency *= 100;
t->fec = fec_tab[buf[12] & 0x07];
t->symbol_rate = 10 * bcd32_to_cpu (buf[9], buf[10], buf[11], buf[12] & 0xf0);
if ((buf[8] & 0x0f) > 5)
t->modulation = QAM_AUTO;
else
t->modulation = qam_tab[buf[8] & 0x0f];
t->inversion = spectral_inversion;
if (verbosity >= 5) {
debug("%#04x/%#04x ", t->network_id, t->transport_stream_id);
dump_dvb_parameters (stderr, t);
if (t->scan_done)
dprintf(5, " (done)");
if (t->last_tuning_failed)
dprintf(5, " (tuning failed)");
dprintf(5, "\n");
}
}
static void parse_s2_satellite_delivery_system_descriptor (const unsigned char *buf, struct transponder *t)
{
if (!t) {
warning("satellite_delivery_system_descriptor outside transport stream definition (ignored)\n");
return;
}
t->delivery_system = SYS_DVBS2;
}
static void parse_satellite_delivery_system_descriptor (const unsigned char *buf, struct transponder *t)
{
if (!t) {
warning("satellite_delivery_system_descriptor outside transport stream definition (ignored)\n");
return;
}
switch ( getBits(buf,69,1) )
{
case 0: t->delivery_system = SYS_DVBS; break;
case 1: t->delivery_system = SYS_DVBS2; break;
}
if (t->delivery_system == SYS_DVBS2)
{
switch ( getBits(buf,67,2) )
{
case 0 : t->rolloff = ROLLOFF_35; break;
case 1 : t->rolloff = ROLLOFF_25; break;
case 2 : t->rolloff = ROLLOFF_20; break;
}
}
else {
if (noauto) t->rolloff = ROLLOFF_35;
}
t->frequency = 10 * bcd32_to_cpu (buf[2], buf[3], buf[4], buf[5]);
switch ( getBits(buf,100,4) )
{
case 0 : t->fec = FEC_AUTO; break;
case 1 : t->fec = FEC_1_2; break;
case 2 : t->fec = FEC_2_3; break;
case 3 : t->fec = FEC_3_4; break;
case 4 : t->fec = FEC_5_6; break;
case 5 : t->fec = FEC_7_8; break;
case 6 : t->fec = FEC_8_9; break;
case 7 : t->fec = FEC_3_5; break;
case 8 : t->fec = FEC_4_5; break;
case 9 : t->fec = FEC_9_10; break;
case 15 : t->fec = FEC_NONE; break;
}
t->symbol_rate = 10 * bcd32_to_cpu (buf[9], buf[10], buf[11], buf[12] & 0xf0);
t->inversion = spectral_inversion;
t->polarisation = (buf[8] >> 5) & 0x03;
t->orbital_pos = bcd32_to_cpu (0x00, 0x00, buf[6], buf[7]);
t->we_flag = buf[8] >> 7;
switch ( getBits(buf,70,2) )
{
case 0 : t->modulation = QAM_AUTO; break;
case 1 : t->modulation = QPSK; break;
case 2 : t->modulation = PSK_8; break;
case 3 : t->modulation = QAM_16; break;
}
if (verbosity >= 5) {
debug("%#04x/%#04x ", t->network_id, t->transport_stream_id);
dump_dvb_parameters (stderr, t);
dprintf(5, "\n");
if (t->scan_done)
dprintf(5, " (done)");
if (t->last_tuning_failed)
dprintf(5, " (tuning failed)");
dprintf(5, "\n");
}
}
static void parse_terrestrial_delivery_system_descriptor (const unsigned char *buf, struct transponder *t)
{
static const fe_modulation_t m_tab [] = { QPSK, QAM_16, QAM_64, QAM_AUTO };
static const fe_code_rate_t ofec_tab [8] = { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_7_8 };
if (!t) {
warning("terrestrial_delivery_system_descriptor outside transport stream definition (ignored)\n");
return;
}
t->delivery_system = SYS_DVBT;
t->frequency = (buf[2] << 24) | (buf[3] << 16);
t->frequency |= (buf[4] << 8) | buf[5];
t->frequency *= 10;
t->inversion = spectral_inversion;
t->bandwidth = BANDWIDTH_8_MHZ + ((buf[6] >> 5) & 0x3);
t->modulation = m_tab[(buf[7] >> 6) & 0x3];
t->hierarchy = HIERARCHY_NONE + ((buf[7] >> 3) & 0x3);
if ((buf[7] & 0x7) > 4)
t->fecHP = FEC_AUTO;
else
t->fecHP = ofec_tab [buf[7] & 0x7];
if (((buf[8] >> 5) & 0x7) > 4)
t->fecLP = FEC_AUTO;
else
t->fecLP = ofec_tab [(buf[8] >> 5) & 0x7];
t->guard_interval = GUARD_INTERVAL_1_32 + ((buf[8] >> 3) & 0x3);
t->transmission_mode = (buf[8] & 0x2) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
t->other_frequency_flag = (buf[8] & 0x01);
if (verbosity >= 5) {
debug("%#04x/%#04x ", t->network_id, t->transport_stream_id);
dump_dvb_parameters (stderr, t);
if (t->scan_done)
dprintf(5, " (done)");
if (t->last_tuning_failed)
dprintf(5, " (tuning failed)");
dprintf(5, "\n");
}
}
static void parse_frequency_list_descriptor (const unsigned char *buf, struct transponder *t)
{
int n, i;
typeof(*t->other_f) f;
if (!t) {
warning("frequency_list_descriptor outside transport stream definition (ignored)\n");
return;
}
if (t->other_f)
return;
n = (buf[1] - 1) / 4;
if (n < 1 || (buf[2] & 0x03) != 3)
return;
t->other_f = calloc(n, sizeof(*t->other_f));
t->n_other_f = n;
buf += 3;
for (i = 0; i < n; i++) {
f = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
t->other_f[i] = f * 10;
buf += 4;
}
}
char * dvbtext2utf8(char* dvbtext, int dvbtextlen)
{
unsigned char *src, *dest;
char *utf8buf;
char *utf8out;
char *utf8in;
char *utf8res;
size_t inlen, outlen;
int old_style_conv=0;
int skip_char=0;
iconv_t code_desc;
switch (dvbtext[0]) {
case 0x01: /* ISO-8859-5 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-5");
break;
case 0x02: /* ISO-8859-6 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-6");
break;
case 0x03: /* ISO-8859-7 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-7");
break;
case 0x04: /* ISO-8859-8 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-8");
break;
case 0x05: /* ISO-8859-9 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-9");
break;
case 0x06: /* ISO-8859-10 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-10");
break;
case 0x07: /* ISO-8859-11 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-11");
break;
case 0x08: /* ISO-8859-12 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-12");
break;
case 0x09: /* ISO-8859-13 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-13");
break;
case 0x0a: /* ISO-8859-14 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-14");
break;
case 0x0b: /* ISO-8859-15 */
skip_char = 1;
code_desc = iconv_open("UTF-8","ISO8859-15");
break;
case 0x0c: /* 0x0C - 0x0F - reserverd for future use */
case 0x0d:
case 0x0e:
case 0x0f:
skip_char = 1;
code_desc = iconv_open("UTF-8","LATIN1");
break;
case 0x10:
skip_char = 3;
if ( dvbtext[1] != 0x00 ) {
old_style_conv = 1;
} else {
switch (dvbtext[2]) {
case 0x01: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-1");
break;
case 0x02: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-2");
break;
case 0x03: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-3");
break;
case 0x04: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-4");
break;
case 0x05: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-5");
break;
case 0x06: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-6");
break;
case 0x07: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-7");
break;
case 0x08: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-8");
break;
case 0x09: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-9");
break;
case 0x0a: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-10");
break;
case 0x0b: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-11");
break;
case 0x0c: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-12");
break;
case 0x0d: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-13");
break;
case 0x0e: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-14");
break;
case 0x0f: /* ISO-8859-1 */
code_desc = iconv_open("UTF-8","ISO8859-15");
break;
default:
skip_char = 3;
old_style_conv = 1;
break;
}
}
break;
case 0x11: /* ISO/IEC 10646 Basic Multilingual Plane (BMP) */
skip_char = 1;
old_style_conv = 1;
break;
case 0x12: /* KSX1001-2004 Korean Character Set */
skip_char = 1;
old_style_conv = 1;
break;
case 0x13: /* GB-2312-1980 Simplified Chinese Character */
skip_char = 1;
old_style_conv = 1;
break;
case 0x14: /* Big5 subset of ISO/IEC 10646 Traditional Chinese */
skip_char = 1;
old_style_conv = 1;
break;
case 0x15: /* UTF-8 encoding of ISO/IEC 10646 Basic Multilingual Plane (BMP) */
skip_char = 1;
old_style_conv = 1;
break;
case 0x16: /* 0x16 - 0x1E - reserverd for future use */
case 0x17:
case 0x18:
case 0x19:
case 0x1a:
case 0x1b:
case 0x1c:
case 0x1d:
case 0x1e:
skip_char = 1;
old_style_conv = 1;
break;
case 0x1F: /* described by encoding_type_id. TBD */
skip_char = 1;
old_style_conv = 1;
break;
default:
skip_char = 0;
old_style_conv = 1;
break;
}
if ( skip_char > 0 )
{
memmove(dvbtext,&dvbtext[skip_char],dvbtextlen-skip_char);
dvbtext[dvbtextlen-skip_char]='\0';
}
if ( old_style_conv == 1)
{
utf8buf = malloc(dvbtextlen);
memset( utf8buf, 0, dvbtextlen );
memcpy ( utf8buf, dvbtext, dvbtextlen );
for (src = dest = (unsigned char *) utf8buf; *src; src++)
if (*src >= 0x20 && (*src < 0x80 || *src > 0x9f))
*dest++ = *src;
*dest = '\0';
if (!utf8buf[0]) {
/* zap zero length names */
if ( utf8buf )
free(utf8buf);
utf8res = strdup("\0");
} else {
code_desc = iconv_open("UTF-8","LATIN1");
if ( code_desc != (iconv_t)(-1) )
{
utf8in = strdup(utf8buf);
inlen = strlen(utf8buf);
if ( utf8buf )
free(utf8buf);
outlen = inlen*2;
utf8buf = malloc(outlen);
utf8out = utf8buf;
memset( utf8buf, 0, outlen);
errno = 0;
utf8in = dvbtext;
iconv( code_desc, &utf8in, &inlen, &utf8out, &outlen);
utf8res = strdup(utf8buf);
if ( utf8buf )
free(utf8buf);
iconv_close(code_desc);
}
}
}
else
{
if ( code_desc != (iconv_t)(-1) )
{
inlen = dvbtextlen-skip_char;
outlen = inlen*2;
utf8buf = malloc(outlen);
utf8out = utf8buf;
memset( utf8buf, 0, outlen);
errno = 0;
utf8in = dvbtext;
iconv( code_desc, &utf8in, &inlen, &utf8out, &outlen);
utf8res = strdup(utf8buf);
if ( utf8buf )
free(utf8buf);
iconv_close(code_desc);
}
}
return utf8res;
}
static void parse_service_descriptor (const unsigned char *buf, struct service *s)
{
unsigned char len;
char* dvbtext;
// s->type = buf[2];
buf += 3;
len = *buf;
buf++;
if (s->provider_name == NULL)
free (s->provider_name);
dvbtext = malloc (len + 1);
memcpy (dvbtext, buf, len);
dvbtext[len]='\0';
s->provider_name = dvbtext2utf8(dvbtext,len + 1);
if (dvbtext == NULL)
free(dvbtext);
if (s->service_name)
free (s->service_name);
buf += len;
len = *buf;
buf++;
dvbtext = malloc (len + 1);
memcpy (dvbtext, buf, len);
dvbtext[len]='\0';
s->service_name = dvbtext2utf8(dvbtext,len + 1);
if (dvbtext == NULL)
free(dvbtext);
info("0x%04X 0x%04X: pmt_pid 0x%04X %s -- %s (%s%s)\n",
current_tp->transport_stream_id,
s->service_id,
s->pmt_pid,
s->provider_name, s->service_name,
s->running == RM_NOT_RUNNING ? "not running" :
s->running == RM_STARTS_SOON ? "starts soon" :
s->running == RM_PAUSING ? "pausing" :
s->running == RM_RUNNING ? "running" : "???",
s->scrambled ? ", scrambled" : "");
}
static void parse_ca_descriptor (const unsigned char *buf, struct service *s)
{
unsigned char descriptor_length = buf [1];
int CA_system_ID;
int found=0;
int i;
buf += 2;
if (descriptor_length < 4) return;
CA_system_ID = (buf[0] << 8) | buf[1];
for (i=0; i<s->ca_num; i++)
if (s->ca_id[i] == CA_system_ID)
found++;
if (!found) {
if (s->ca_num + 1 >= CA_SYSTEM_ID_MAX)
warning("TOO MANY CA SYSTEM IDs.\n");
else {
info(" CA ID : PID 0x%04X\n", CA_system_ID);
s->ca_id[s->ca_num]=CA_system_ID;
s->ca_num++;
}
}
}
static int find_descriptor(uint8_t tag, const unsigned char *buf,
int descriptors_loop_len,
const unsigned char **desc, int *desc_len)
{
while (descriptors_loop_len > 0) {
unsigned char descriptor_tag = buf[0];
unsigned char descriptor_len = buf[1] + 2;
if (!descriptor_len) {
warning("descriptor_tag == 0x%02X, len is 0\n", descriptor_tag);
break;
}
if (tag == descriptor_tag) {
if (desc)
*desc = buf;
if (desc_len)
*desc_len = descriptor_len;
return 1;
}