summaryrefslogtreecommitdiff
path: root/eit.c
blob: eebe82c7e3d9b676cd26580bd4e20ec1f15159ce (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
/***************************************************************************
                          eit.c  -  description
                             -------------------
    begin                : Fri Aug 25 2000
    copyright            : (C) 2000 by Robert Schneider
    email                : Robert.Schneider@web.de

    2001-08-15: Adapted to 'libdtv' by Rolf Hakenes <hakenes@hippomi.de>

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 * $Id: eit.c 1.65 2003/02/02 15:41:03 kls Exp $
 ***************************************************************************/

#include "eit.h"
#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/dvb/dmx.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "channels.h"
#include "config.h"
#include "libdtv/libdtv.h"
#include "videodir.h"

// --- cMJD ------------------------------------------------------------------

class cMJD {
public:
   cMJD();
   cMJD(u_char date_hi, u_char date_lo);
   cMJD(u_char date_hi, u_char date_lo, u_char timehr, u_char timemi, u_char timese);
   ~cMJD();
  /**  */
  void ConvertToTime();
  /**  */
  bool SetSystemTime();
  /**  */
  time_t GetTime_t();
protected: // Protected attributes
  /**  */
  time_t mjdtime;
protected: // Protected attributes
  /**  */
  u_char time_second;
protected: // Protected attributes
  /**  */
  u_char time_minute;
protected: // Protected attributes
  /**  */
  u_char time_hour;
protected: // Protected attributes
  /**  */
  u_short mjd;
};

cMJD::cMJD()
{
}

cMJD::cMJD(u_char date_hi, u_char date_lo)
{
   mjd = date_hi << 8 | date_lo;
   time_hour = time_minute = time_second = 0;
   ConvertToTime();
}

cMJD::cMJD(u_char date_hi, u_char date_lo, u_char timehr, u_char timemi, u_char timese)
{
   mjd = date_hi << 8 | date_lo;
   time_hour = timehr;
   time_minute = timemi;
   time_second = timese;
   ConvertToTime();
}

cMJD::~cMJD()
{
}

/**  */
void cMJD::ConvertToTime()
{
   struct tm t;

   t.tm_sec = time_second;
   t.tm_min = time_minute;
   t.tm_hour = time_hour;
   int k;

   t.tm_year = (int) ((mjd - 15078.2) / 365.25);
   t.tm_mon = (int) ((mjd - 14956.1 - (int)(t.tm_year * 365.25)) / 30.6001);
   t.tm_mday = (int) (mjd - 14956 - (int)(t.tm_year * 365.25) - (int)(t.tm_mon * 30.6001));
   k = (t.tm_mon == 14 || t.tm_mon == 15) ? 1 : 0;
   t.tm_year = t.tm_year + k;
   t.tm_mon = t.tm_mon - 1 - k * 12;
   t.tm_mon--;

   t.tm_isdst = -1;
   t.tm_gmtoff = 0;

   mjdtime = timegm(&t);

   //isyslog("Time parsed = %s\n", ctime(&mjdtime));
}

/**  */
bool cMJD::SetSystemTime()
{
   struct tm *ptm;
   time_t loctim;

   struct tm tm_r;
   ptm = localtime_r(&mjdtime, &tm_r);
   loctim = time(NULL);

   if (abs(mjdtime - loctim) > 2)
   {
      isyslog("System Time = %s (%ld)\n", ctime(&loctim), loctim);
      isyslog("Local Time  = %s (%ld)\n", ctime(&mjdtime), mjdtime);
      if (stime(&mjdtime) < 0)
         esyslog("ERROR while setting system time: %m");
      return true;
   }

   return false;
}
/**  */
time_t cMJD::GetTime_t()
{
   return mjdtime;
}

// --- cTDT ------------------------------------------------------------------

class cTDT {
public:
   cTDT(tdt_t *ptdt);
   ~cTDT();
  /**  */
  bool SetSystemTime();
protected: // Protected attributes
  /**  */
  tdt_t tdt;
  /**  */
  cMJD mjd; // kls 2001-03-02: made this a member instead of a pointer (it wasn't deleted in the destructor!)
};

#define BCD2DEC(b) (((b >> 4) & 0x0F) * 10 + (b & 0x0F))

cTDT::cTDT(tdt_t *ptdt)
:tdt(*ptdt)
,mjd(tdt.utc_mjd_hi, tdt.utc_mjd_lo, BCD2DEC(tdt.utc_time_h), BCD2DEC(tdt.utc_time_m), BCD2DEC(tdt.utc_time_s))
{
}

cTDT::~cTDT()
{
}
/**  */
bool cTDT::SetSystemTime()
{
   return mjd.SetSystemTime();
}

// --- cEventInfo ------------------------------------------------------------

cEventInfo::cEventInfo(tChannelID channelid, unsigned short eventid)
{
   pTitle = NULL;
   pSubtitle = NULL;
   pExtendedDescription = NULL;
   bIsPresent = bIsFollowing = false;
   lDuration = 0;
   tTime = 0;
   uTableID = 0;
   uEventID = eventid;
   channelID = channelid;
   nChannelNumber = 0;
}

cEventInfo::~cEventInfo()
{
   free(pTitle);
   free(pSubtitle);
   free(pExtendedDescription);
}

/**  */
const char * cEventInfo::GetTitle() const
{
   return pTitle;
}
/**  */
const char * cEventInfo::GetSubtitle() const
{
   return pSubtitle;
}
/**  */
const char * cEventInfo::GetExtendedDescription() const
{
   return pExtendedDescription;
}
/**  */
bool cEventInfo::IsPresent() const
{
   return bIsPresent;
}
/**  */
void cEventInfo::SetPresent(bool pres)
{
   bIsPresent = pres;
}
/**  */
bool cEventInfo::IsFollowing() const
{
   return bIsFollowing;
}

void cEventInfo::SetTableID(unsigned char tableid)
{
   uTableID = tableid;
}

/**  */
void cEventInfo::SetFollowing(bool foll)
{
   bIsFollowing = foll;
}
/**  */
const char * cEventInfo::GetDate() const
{
   static char szDate[25];

   struct tm tm_r;
   strftime(szDate, sizeof(szDate), "%d.%m.%Y", localtime_r(&tTime, &tm_r));

   return szDate;
}

const unsigned char cEventInfo::GetTableID(void) const
{
   return uTableID;
}

/**  */
const char * cEventInfo::GetTimeString() const
{
   static char szTime[25];

   struct tm tm_r;
   strftime(szTime, sizeof(szTime), "%R", localtime_r(&tTime, &tm_r));

   return szTime;
}
/**  */
const char * cEventInfo::GetEndTimeString() const
{
   static char szEndTime[25];
   time_t tEndTime = tTime + lDuration;

   struct tm tm_r;
   strftime(szEndTime, sizeof(szEndTime), "%R", localtime_r(&tEndTime, &tm_r));

   return szEndTime;
}
/**  */
time_t cEventInfo::GetTime() const
{
   return tTime;
}
/**  */
long cEventInfo::GetDuration() const
{
   return lDuration;
}
/**  */
unsigned short cEventInfo::GetEventID() const
{
   return uEventID;
}
/**  */
void cEventInfo::SetTitle(const char *string)
{
   pTitle = strcpyrealloc(pTitle, string);
}
/**  */
void cEventInfo::SetSubtitle(const char *string)
{
   pSubtitle = strcpyrealloc(pSubtitle, string);
}
/**  */
void cEventInfo::SetExtendedDescription(const char *string)
{
   pExtendedDescription = strcpyrealloc(pExtendedDescription, string);
}
/**  */
void cEventInfo::SetTime(time_t t)
{
   tTime = t;
}
/**  */
void cEventInfo::SetDuration(long l)
{
   lDuration = l;
}
/**  */
void cEventInfo::SetEventID(unsigned short evid)
{
   uEventID = evid;
}
/**  */
void cEventInfo::SetChannelID(tChannelID channelid)
{
   channelID = channelid;
}

/**  */
tChannelID cEventInfo::GetChannelID() const
{
   return channelID;
}

/**  */
void cEventInfo::Dump(FILE *f, const char *Prefix) const
{
   if (tTime + lDuration >= time(NULL)) {
      fprintf(f, "%sE %u %ld %ld %X\n", Prefix, uEventID, tTime, lDuration, uTableID);
      if (!isempty(pTitle))
         fprintf(f, "%sT %s\n", Prefix, pTitle);
      if (!isempty(pSubtitle))
         fprintf(f, "%sS %s\n", Prefix, pSubtitle);
      if (!isempty(pExtendedDescription))
         fprintf(f, "%sD %s\n", Prefix, pExtendedDescription);
      fprintf(f, "%se\n", Prefix);
      }
}

bool cEventInfo::Read(FILE *f, cSchedule *Schedule)
{
  if (Schedule) {
     cEventInfo *pEvent = NULL;
     char *s;
     while ((s = readline(f)) != NULL) {
           char *t = skipspace(s + 1);
           switch (*s) {
             case 'E': if (!pEvent) {
                          unsigned int uEventID;
                          time_t tTime;
                          long lDuration;
                          unsigned int uTableID = 0;
                          int n = sscanf(t, "%u %ld %ld %X", &uEventID, &tTime, &lDuration, &uTableID);
                          if (n == 3 || n == 4) {
                             pEvent = (cEventInfo *)Schedule->GetEvent(uEventID, tTime);
                             if (!pEvent)
                                pEvent = Schedule->AddEvent(new cEventInfo(Schedule->GetChannelID(), uEventID));
                             if (pEvent) {
                                pEvent->SetTableID(uTableID);
                                pEvent->SetTime(tTime);
                                pEvent->SetDuration(lDuration);
                                }
                             }
                          }
                       break;
             case 'T': if (pEvent)
                          pEvent->SetTitle(t);
                       break;
             case 'S': if (pEvent)
                          pEvent->SetSubtitle(t);
                       break;
             case 'D': if (pEvent)
                          pEvent->SetExtendedDescription(t);
                       break;
             case 'e': pEvent = NULL;
                       break;
             case 'c': // to keep things simple we react on 'c' here
                       return true;
             default:  esyslog("ERROR: unexpected tag while reading EPG data: %s", s);
                       return false;
             }
           }
     esyslog("ERROR: unexpected end of file while reading EPG data");
     }
  return false;
}

#define MAXEPGBUGFIXSTATS 7
#define MAXEPGBUGFIXCHANS 100
struct tEpgBugFixStats {
  int hits;
  int n;
  tChannelID channelIDs[MAXEPGBUGFIXCHANS];
  tEpgBugFixStats(void) { hits = n = 0; }
  };

tEpgBugFixStats EpgBugFixStats[MAXEPGBUGFIXSTATS];

static void EpgBugFixStat(int Number, tChannelID ChannelID)
{
  if (0 <= Number && Number < MAXEPGBUGFIXSTATS) {
     tEpgBugFixStats *p = &EpgBugFixStats[Number];
     p->hits++;
     int i = 0;
     for (; i < p->n; i++) {
         if (p->channelIDs[i] == ChannelID)
            break;
         }
     if (i == p->n && p->n < MAXEPGBUGFIXCHANS)
        p->channelIDs[p->n++] = ChannelID;
     }
}

static void ReportEpgBugFixStats(bool Reset = false)
{
  if (Setup.EPGBugfixLevel > 0) {
     bool GotHits = false;
     char buffer[1024];
     for (int i = 0; i < MAXEPGBUGFIXSTATS; i++) {
         const char *delim = "\t";
         tEpgBugFixStats *p = &EpgBugFixStats[i];
         if (p->hits) {
            if (!GotHits) {
               dsyslog("=====================");
               dsyslog("EPG bugfix statistics");
               dsyslog("=====================");
               dsyslog("IF SOMEBODY WHO IS IN CHARGE OF THE EPG DATA FOR ONE OF THE LISTED");
               dsyslog("CHANNELS READS THIS: PLEASE TAKE A LOOK AT THE FUNCTION cEventInfo::FixEpgBugs()");
               dsyslog("IN VDR/eit.c TO LEARN WHAT'S WRONG WITH YOUR DATA, AND FIX IT!");
               dsyslog("=====================");
               dsyslog("Fix\tHits\tChannels");
               GotHits = true;
               }
            char *q = buffer;
            q += snprintf(q, sizeof(buffer) - (q - buffer), "%d\t%d", i, p->hits);
            for (int c = 0; c < p->n; c++) {
                cChannel *channel = Channels.GetByChannelID(p->channelIDs[c], true);
                if (channel) {
                   q += snprintf(q, sizeof(buffer) - (q - buffer), "%s%s", delim, channel->Name());
                   delim = ", ";
                   }
                }
            dsyslog("%s", buffer);
            }
         if (Reset)
            p->hits = p->n = 0;
         }
     if (GotHits)
        dsyslog("=====================");
     }
}

void cEventInfo::FixEpgBugs(void)
{
  // VDR can't usefully handle newline characters in the EPG data, so let's
  // always convert them to blanks (independent of the setting of EPGBugfixLevel):
  strreplace(pTitle, '\n', ' ');
  strreplace(pSubtitle, '\n', ' ');
  strreplace(pExtendedDescription, '\n', ' ');

  if (Setup.EPGBugfixLevel == 0)
     return;

  // Some TV stations apparently have their own idea about how to fill in the
  // EPG data. Let's fix their bugs as good as we can:
  if (pTitle) {

     // VOX puts too much information into the Subtitle and leaves the Extended
     // Description empty:
     //
     // Title
     // (NAT, Year Min')[ ["Subtitle". ]Extended Description]
     //
     if (pSubtitle && !pExtendedDescription) {
        if (*pSubtitle == '(') {
           char *e = strchr(pSubtitle + 1, ')');
           if (e) {
              if (*(e + 1)) {
                 if (*++e == ' ')
                    if (*(e + 1) == '"')
                       e++;
                 }
              else
                 e = NULL;
              char *s = e ? strdup(e) : NULL;
              free(pSubtitle);
              pSubtitle = s;
              EpgBugFixStat(0, GetChannelID());
              // now the fixes #1 and #2 below will handle the rest
              }
           }
        }

     // VOX and VIVA put the Subtitle in quotes and use either the Subtitle
     // or the Extended Description field, depending on how long the string is:
     //
     // Title
     // "Subtitle". Extended Description
     //
     if ((pSubtitle == NULL) != (pExtendedDescription == NULL)) {
        char *p = pSubtitle ? pSubtitle : pExtendedDescription;
        if (*p == '"') {
           const char *delim = "\".";
           char *e = strstr(p + 1, delim);
           if (e) {
              *e = 0;
              char *s = strdup(p + 1);
              char *d = strdup(e + strlen(delim));
              free(pSubtitle);
              free(pExtendedDescription);
              pSubtitle = s;
              pExtendedDescription = d;
              EpgBugFixStat(1, GetChannelID());
              }
           }
        }

     // VOX and VIVA put the Extended Description into the Subtitle (preceeded
     // by a blank) if there is no actual Subtitle and the Extended Description
     // is short enough:
     //
     // Title
     //  Extended Description
     //
     if (pSubtitle && !pExtendedDescription) {
        if (*pSubtitle == ' ') {
           memmove(pSubtitle, pSubtitle + 1, strlen(pSubtitle));
           pExtendedDescription = pSubtitle;
           pSubtitle = NULL;
           EpgBugFixStat(2, GetChannelID());
           }
        }

     // Pro7 sometimes repeats the Title in the Subtitle:
     //
     // Title
     // Title
     //
     if (pSubtitle && strcmp(pTitle, pSubtitle) == 0) {
        free(pSubtitle);
        pSubtitle = NULL;
        EpgBugFixStat(3, GetChannelID());
        }

     // ZDF.info puts the Subtitle between double quotes, which is nothing
     // but annoying (some even put a '.' after the closing '"'):
     //
     // Title
     // "Subtitle"[.]
     //
     if (pSubtitle && *pSubtitle == '"') {
        int l = strlen(pSubtitle);
        if (l > 2 && (pSubtitle[l - 1] == '"' || (pSubtitle[l - 1] == '.' && pSubtitle[l - 2] == '"'))) {
           memmove(pSubtitle, pSubtitle + 1, l);
           char *p = strrchr(pSubtitle, '"');
           if (p)
              *p = 0;
           EpgBugFixStat(4, GetChannelID());
           }
        }

     if (Setup.EPGBugfixLevel <= 1)
        return;

     // Some channels apparently try to do some formatting in the texts,
     // which is a bad idea because they have no way of knowing the width
     // of the window that will actually display the text.
     // Remove excess whitespace:
     pTitle = compactspace(pTitle);
     pSubtitle = compactspace(pSubtitle);
     pExtendedDescription = compactspace(pExtendedDescription);
     // Remove superfluous hyphens:
     if (pExtendedDescription) {
        char *p = pExtendedDescription;
        while (*p && *(p + 1) && *(p + 2)) {
              if (*p == '-' && *(p + 1) == ' ' && *(p + 2) && islower(*(p - 1)) && islower(*(p + 2))) {
                 if (!startswith(p + 2, "und ")) { // special case in German, as in "Lach- und Sachgeschichten"
                    memmove(p, p + 2, strlen(p + 2) + 1);
                    EpgBugFixStat(5, GetChannelID());
                    }
                 }
              p++;
              }
        }

#define MAX_USEFUL_SUBTITLE_LENGTH 40
     // Some channels put a whole lot of information in the Subtitle and leave
     // the Extended Description totally empty. So if the Subtitle length exceeds
     // MAX_USEFUL_SUBTITLE_LENGTH, let's put this into the Extended Description
     // instead:
     if (!isempty(pSubtitle) && isempty(pExtendedDescription)) {
        if (strlen(pSubtitle) > MAX_USEFUL_SUBTITLE_LENGTH) {
           free(pExtendedDescription);
           pExtendedDescription = pSubtitle;
           pSubtitle = NULL;
           EpgBugFixStat(5, GetChannelID());
           }
        }

     // Some channels use the ` ("backtick") character, where a ' (single quote)
     // would be normally used. Actually, "backticks" in normal text don't make
     // much sense, so let's replace them:
     strreplace(pTitle, '`', '\'');
     strreplace(pSubtitle, '`', '\'');
     strreplace(pExtendedDescription, '`', '\'');
     }
}

// --- cSchedule -------------------------------------------------------------

cSchedule::cSchedule(tChannelID channelid)
{
   pPresent = pFollowing = NULL;
   channelID = channelid;
}


cSchedule::~cSchedule()
{
}

cEventInfo *cSchedule::AddEvent(cEventInfo *EventInfo)
{
  Events.Add(EventInfo);
  return EventInfo;
}

const cEventInfo *cSchedule::GetPresentEvent(void) const
{
  return GetEventAround(time(NULL));
}

const cEventInfo *cSchedule::GetFollowingEvent(void) const
{
  const cEventInfo *pe = NULL;
  time_t now = time(NULL);
  time_t delta = INT_MAX;
  for (cEventInfo *p = Events.First(); p; p = Events.Next(p)) {
      time_t dt = p->GetTime() - now;
      if (dt > 0 && dt < delta) {
         delta = dt;
         pe = p;
         }
      }
  return pe;
}

void cSchedule::SetChannelID(tChannelID channelid)
{
   channelID = channelid;
}
/**  */
tChannelID cSchedule::GetChannelID() const
{
   return channelID;
}
/**  */
const cEventInfo * cSchedule::GetEvent(unsigned short uEventID, time_t tTime) const
{
   // Returns either the event info with the given uEventID or, if that one can't
   // be found, the one with the given tTime (or NULL if neither can be found)
   cEventInfo *pe = Events.First();
   cEventInfo *pt = NULL;
   while (pe != NULL)
   {
      if (pe->GetEventID() == uEventID)
         return pe;
      if (tTime > 0 && pe->GetTime() == tTime) // 'tTime < 0' is apparently used with NVOD channels
         pt = pe;

      pe = Events.Next(pe);
   }

   return pt;
}

const cEventInfo *cSchedule::GetEventAround(time_t Time) const
{
  const cEventInfo *pe = NULL;
  time_t delta = INT_MAX;
  for (cEventInfo *p = Events.First(); p; p = Events.Next(p)) {
      time_t dt = Time - p->GetTime();
      if (dt >= 0 && dt < delta && p->GetTime() + p->GetDuration() >= Time) {
         delta = dt;
         pe = p;
         }
      }
  return pe;
}

bool cSchedule::SetPresentEvent(cEventInfo *pEvent)
{
   if (pPresent != NULL)
      pPresent->SetPresent(false);
   pPresent = pEvent;
   pPresent->SetPresent(true);

   return true;
}

/**  */
bool cSchedule::SetFollowingEvent(cEventInfo *pEvent)
{
   if (pFollowing != NULL)
      pFollowing->SetFollowing(false);
   pFollowing = pEvent;
   pFollowing->SetFollowing(true);

   return true;
}

/**  */
void cSchedule::Cleanup()
{
   Cleanup(time(NULL));
}

/**  */
void cSchedule::Cleanup(time_t tTime)
{
   cEventInfo *pEvent;
   for (int a = 0; true ; a++)
   {
      pEvent = Events.Get(a);
      if (pEvent == NULL)
         break;
      if (pEvent->GetTime() + pEvent->GetDuration() + 3600 < tTime) // adding one hour for safety
      {
         Events.Del(pEvent);
         a--;
      }
   }
}

/**  */
void cSchedule::Dump(FILE *f, const char *Prefix) const
{
   cChannel *channel = Channels.GetByChannelID(channelID, true);
   if (channel)
   {
      fprintf(f, "%sC %s %s\n", Prefix, channel->GetChannelID().ToString(), channel->Name());
      for (cEventInfo *p = Events.First(); p; p = Events.Next(p))
         p->Dump(f, Prefix);
      fprintf(f, "%sc\n", Prefix);
   }
}

bool cSchedule::Read(FILE *f, cSchedules *Schedules)
{
  if (Schedules) {
     char *s;
     while ((s = readline(f)) != NULL) {
           if (*s == 'C') {
              s = skipspace(s + 1);
              char *p = strchr(s, ' ');
              if (p)
                 *p = 0; // strips optional channel name
              if (*s) {
                 tChannelID channelID = tChannelID::FromString(s);
                 if (channelID.Valid()) {
                    cSchedule *p = (cSchedule *)Schedules->AddChannelID(channelID);
                    if (p) {
                       if (!cEventInfo::Read(f, p))
                          return false;
                       }
                    }
                 else {
                    esyslog("ERROR: illegal channel ID: %s", s);
                    return false;
                    }
                 }
              }
           else {
              esyslog("ERROR: unexpected tag while reading EPG data: %s", s);
              return false;
              }
           }
     return true;
     }
  return false;
}

// --- cSchedules ------------------------------------------------------------

cSchedules::cSchedules()
{
   pCurrentSchedule = NULL;
}

cSchedules::~cSchedules()
{
}
/**  */
const cSchedule *cSchedules::AddChannelID(tChannelID channelid)
{
  const cSchedule *p = GetSchedule(channelid);
  if (!p) {
     Add(new cSchedule(channelid));
     p = GetSchedule(channelid);
     }
  return p;
}
/**  */
const cSchedule *cSchedules::SetCurrentChannelID(tChannelID channelid)
{
  channelid.ClrRid();
  pCurrentSchedule = AddChannelID(channelid);
  if (pCurrentSchedule)
     currentChannelID = channelid;
  return pCurrentSchedule;
}
/**  */
const cSchedule * cSchedules::GetSchedule() const
{
   return pCurrentSchedule;
}
/**  */
const cSchedule * cSchedules::GetSchedule(tChannelID channelid) const
{
   cSchedule *p;

   channelid.ClrRid();
   p = First();
   while (p != NULL)
   {
      if (p->GetChannelID() == channelid)
         return p;
      p = Next(p);
   }

   return NULL;
}

/**  */
void cSchedules::Cleanup()
{
   cSchedule *p;

   p = First();
   while (p != NULL)
   {
      p->Cleanup(time(NULL));
      p = Next(p);
   }
}

/**  */
void cSchedules::Dump(FILE *f, const char *Prefix) const
{
   for (cSchedule *p = First(); p; p = Next(p))
      p->Dump(f, Prefix);
}

/**  */
bool cSchedules::Read(FILE *f)
{
   cMutexLock MutexLock;
   return cSchedule::Read(f, (cSchedules *)cSIProcessor::Schedules(MutexLock));
}

// --- cEIT ------------------------------------------------------------------

class cEIT {
private:
   cSchedules *schedules;
public:
   cEIT(unsigned char *buf, int length, cSchedules *Schedules);
   ~cEIT();
  /**  */
  int ProcessEIT(unsigned char *buffer, int CurrentSource);

protected: // Protected methods
  /** returns true if this EIT covers a
present/following information, false if it's
schedule information */
  bool IsPresentFollowing();
protected: // Protected attributes
  /** Table ID of this EIT struct */
  u_char tid;
};

cEIT::cEIT(unsigned char * buf, int length, cSchedules *Schedules)
{
   tid = buf[0];
   schedules = Schedules;
}

cEIT::~cEIT()
{
}

/**  */
int cEIT::ProcessEIT(unsigned char *buffer, int CurrentSource)
{
   cEventInfo *pEvent, *rEvent = NULL;
   cSchedule *pSchedule, *rSchedule = NULL;
   struct LIST *VdrProgramInfos;
   struct VdrProgramInfo *VdrProgramInfo;

   if (!buffer)
      return -1;

   VdrProgramInfos = createVdrProgramInfos(buffer);

   if (VdrProgramInfos) {
      for (VdrProgramInfo = (struct VdrProgramInfo *) VdrProgramInfos->Head; VdrProgramInfo; VdrProgramInfo = (struct VdrProgramInfo *) xSucc (VdrProgramInfo)) {
          //XXX TODO use complete channel ID
          cChannel *channel = Channels.GetByServiceID(CurrentSource, VdrProgramInfo->ServiceID);
          tChannelID channelID = channel ? channel->GetChannelID() : tChannelID(CurrentSource, 0, 0, VdrProgramInfo->ServiceID);
          channelID.ClrRid();
          //XXX
          pSchedule = (cSchedule *)schedules->GetSchedule(channelID);
          if (!pSchedule) {
             schedules->Add(new cSchedule(channelID));
             pSchedule = (cSchedule *)schedules->GetSchedule(channelID);
             if (!pSchedule)
                break;
             }
          if (VdrProgramInfo->ReferenceServiceID) {
             rSchedule = (cSchedule *)schedules->GetSchedule(tChannelID(CurrentSource, 0, 0, VdrProgramInfo->ReferenceServiceID));
             if (!rSchedule)
                break;
             rEvent = (cEventInfo *)rSchedule->GetEvent((unsigned short)VdrProgramInfo->ReferenceEventID);
             if (!rEvent)
                break;
             }
          pEvent = (cEventInfo *)pSchedule->GetEvent((unsigned short)VdrProgramInfo->EventID, VdrProgramInfo->StartTime);
          if (!pEvent) {
             // If we don't have that event ID yet, we create a new one.
             // Otherwise we copy the information into the existing event anyway, because the data might have changed.
             pEvent = pSchedule->AddEvent(new cEventInfo(channelID, VdrProgramInfo->EventID));
             if (!pEvent)
                break;
             pEvent->SetTableID(tid);
             }
          else {
             // We have found an existing event, either through its event ID or its start time.
             // If the existing event has a zero table ID it was defined externally and shall
             // not be overwritten.
             if (pEvent->GetTableID() == 0x00)
                continue;
             // If the new event comes from a table that belongs to an "other TS" and the existing
             // one comes from an "actual TS" table, lets skip it.
             if ((tid == 0x4F || tid == 0x60 || tid == 0x61) && (pEvent->GetTableID() == 0x4E || pEvent->GetTableID() == 0x50 || pEvent->GetTableID() == 0x51))
                continue;
             }
          if (rEvent) {
             pEvent->SetTitle(rEvent->GetTitle());
             pEvent->SetSubtitle(rEvent->GetSubtitle());
             pEvent->SetExtendedDescription(rEvent->GetExtendedDescription());
             }
          else {
             pEvent->SetTableID(tid);
             pEvent->SetTitle(VdrProgramInfo->ShortName);
             pEvent->SetSubtitle(VdrProgramInfo->ShortText);
             pEvent->SetExtendedDescription(VdrProgramInfo->ExtendedName);
             //XXX kls 2001-09-22:
             //XXX apparently this never occurred, so I have simpified ExtendedDescription handling
             //XXX pEvent->AddExtendedDescription(VdrProgramInfo->ExtendedText);
             }
          pEvent->SetTime(VdrProgramInfo->StartTime);
          pEvent->SetDuration(VdrProgramInfo->Duration);
          pEvent->FixEpgBugs();
          if (IsPresentFollowing()) {
             if ((GetRunningStatus(VdrProgramInfo->Status) == RUNNING_STATUS_PAUSING) || (GetRunningStatus(VdrProgramInfo->Status) == RUNNING_STATUS_RUNNING))
                pSchedule->SetPresentEvent(pEvent);
             else if (GetRunningStatus(VdrProgramInfo->Status) == RUNNING_STATUS_AWAITING)
                pSchedule->SetFollowingEvent(pEvent);
             }
          }
      }

   xMemFreeAll(NULL);
   return 0;
}

/** returns true if this EIT covers a
present/following information, false if it's
schedule information */
bool cEIT::IsPresentFollowing()
{
   if (tid == 0x4e || tid == 0x4f)
      return true;

   return false;
}

// --- cCaDescriptor ---------------------------------------------------------

class cCaDescriptor : public cListObject {
  friend class cCaDescriptors;
private:
  int source;
  int transponder;
  int serviceId;
  int caSystem;
  int length;
  uchar *data;
public:
  cCaDescriptor(int Source, int Transponder, int ServiceId, int CaSystem, int Length, uchar *Data);
  virtual ~cCaDescriptor();
  int Length(void) const { return length; }
  const uchar *Data(void) const { return data; }
  };

cCaDescriptor::cCaDescriptor(int Source, int Transponder, int ServiceId, int CaSystem, int Length, uchar *Data)
{
  source = Source;
  transponder = Transponder;
  serviceId = ServiceId;
  caSystem = CaSystem;
  length = Length;
  data = MALLOC(uchar, length);
  memcpy(data, Data, length);
  /*//XXX just while debugging...
  char buffer[1024];
  char *q = buffer;
  q += sprintf(q, "CAM: %04X %5d %4d", source, transponder, serviceId);
  for (int i = 0; i < length; i++)
      q += sprintf(q, " %02X", data[i]);
  dsyslog(buffer);
  *///XXX
}

cCaDescriptor::~cCaDescriptor()
{
  free(data);
}

// --- cCaDescriptors --------------------------------------------------------

class cCaDescriptors : public cList<cCaDescriptor> {
public:
  const cCaDescriptor *Get(int Source, int Transponder, int ServiceId, int CaSystem);
  };

const cCaDescriptor *cCaDescriptors::Get(int Source, int Transponder, int ServiceId, int CaSystem)
{
  for (cCaDescriptor *ca = First(); ca; ca = Next(ca)) {
      if (ca->source == Source && ca->transponder == Transponder && ca->serviceId == ServiceId) {
         if (CaSystem == -1 || ca->caSystem == CaSystem)
            return ca;
         if (CaSystem < 0)
            CaSystem++;
         }
      }
  return NULL;
}

// --- cSIProcessor ----------------------------------------------------------

#define MAX_FILTERS 20
#define EPGDATAFILENAME "epg.data"

int cSIProcessor::numSIProcessors = 0;
cSchedules *cSIProcessor::schedules = NULL;
cMutex cSIProcessor::schedulesMutex;
cCaDescriptors *cSIProcessor::caDescriptors = NULL;
cMutex cSIProcessor::caDescriptorsMutex;
const char *cSIProcessor::epgDataFileName = EPGDATAFILENAME;
time_t cSIProcessor::lastDump = time(NULL);

/**  */
cSIProcessor::cSIProcessor(const char *FileName)
{
   fileName = strdup(FileName);
   masterSIProcessor = numSIProcessors == 0; // the first one becomes the 'master'
   currentSource = 0;
   currentTransponder = 0;
   pmtIndex = 0;
   pmtPid = 0;
   filters = NULL;
   if (!numSIProcessors++) { // the first one creates them
      schedules = new cSchedules;
      caDescriptors = new cCaDescriptors;
      }
   filters = (SIP_FILTER *)calloc(MAX_FILTERS, sizeof(SIP_FILTER));
   SetStatus(true);
   Start();
}

cSIProcessor::~cSIProcessor()
{
   if (masterSIProcessor)
      ReportEpgBugFixStats();
   active = false;
   Cancel(3);
   ShutDownFilters();
   free(filters);
   if (!--numSIProcessors) { // the last one deletes them
      delete schedules;
      delete caDescriptors;
      }
   free(fileName);
}

const cSchedules *cSIProcessor::Schedules(cMutexLock &MutexLock)
{
  if (MutexLock.Lock(&schedulesMutex))
     return schedules;
  return NULL;
}

bool cSIProcessor::Read(FILE *f)
{
  bool OwnFile = f == NULL;
  if (OwnFile) {
     const char *FileName = GetEpgDataFileName();
     if (access(FileName, R_OK) == 0) {
        dsyslog("reading EPG data from %s", FileName);
        if ((f = fopen(FileName, "r")) == NULL) {
           LOG_ERROR;
           return false;
           }
        }
     else
        return false;
     }
  bool result = cSchedules::Read(f);
  if (OwnFile)
     fclose(f);
  return result;
}

void cSIProcessor::Clear(void)
{
  cMutexLock MutexLock(&schedulesMutex);
  delete schedules;
  schedules = new cSchedules;
}

void cSIProcessor::SetEpgDataFileName(const char *FileName)
{
  epgDataFileName = NULL;
  if (FileName)
     epgDataFileName = strdup(DirectoryOk(FileName) ? AddDirectory(FileName, EPGDATAFILENAME) : FileName);
}

const char *cSIProcessor::GetEpgDataFileName(void)
{
  if (epgDataFileName)
     return *epgDataFileName == '/' ? epgDataFileName : AddDirectory(VideoDirectory, epgDataFileName);
  return NULL;
}

void cSIProcessor::SetStatus(bool On)
{
   LOCK_THREAD;
   ShutDownFilters();
   pmtIndex = 0;
   pmtPid = 0;
   if (On)
   {
      AddFilter(0x00, 0x00);  // PAT
      AddFilter(0x14, 0x70);  // TDT
      AddFilter(0x14, 0x73);  // TOT
      AddFilter(0x12, 0x4e);  // event info, actual TS, present/following
      AddFilter(0x12, 0x4f);  // event info, other TS, present/following
      AddFilter(0x12, 0x50);  // event info, actual TS, schedule
      AddFilter(0x12, 0x60);  // event info, other TS, schedule
      AddFilter(0x12, 0x51);  // event info, actual TS, schedule for another 4 days
      AddFilter(0x12, 0x61);  // event info, other TS, schedule for another 4 days
   }
}

#define PMT_SCAN_TIMEOUT  10 // seconds

/** use the vbi device to parse all relevant SI
information and let the classes corresponding
to the tables write their information to the disk */
void cSIProcessor::Action()
{
   dsyslog("EIT processing thread started (pid=%d)%s", getpid(), masterSIProcessor ? " - master" : "");

   time_t lastCleanup = time(NULL);
   time_t lastPmtScan = time(NULL);

   active = true;

   while(active)
   {
      if (masterSIProcessor)
      {
         time_t now = time(NULL);
         struct tm tm_r;
         struct tm *ptm = localtime_r(&now, &tm_r);
         if (now - lastCleanup > 3600 && ptm->tm_hour == 5)
         {
            cMutexLock MutexLock(&schedulesMutex);
            isyslog("cleaning up schedules data");
            schedules->Cleanup();
            lastCleanup = now;
            ReportEpgBugFixStats(true);
         }
         if (epgDataFileName && now - lastDump > 600)
         {
            cMutexLock MutexLock(&schedulesMutex);
            cSafeFile f(GetEpgDataFileName());
            if (f.Open()) {
               schedules->Dump(f);
               f.Close();
               }
            else
               LOG_ERROR;
            lastDump = now;
         }
      }

      // set up pfd structures for all active filter
      pollfd pfd[MAX_FILTERS];
      int NumUsedFilters = 0;
      for (int a = 0; a < MAX_FILTERS ; a++)
      {
         if (filters[a].inuse)
         {
            pfd[NumUsedFilters].fd = filters[a].handle;
            pfd[NumUsedFilters].events = POLLIN;
            NumUsedFilters++;
         }
      }

      // wait until data becomes ready from the bitfilter
      if (poll(pfd, NumUsedFilters, 1000) != 0)
      {
         for (int a = 0; a < NumUsedFilters ; a++)
         {
            if (pfd[a].revents & POLLIN)
            {
               // read section
               unsigned char buf[4096]; // max. allowed size for any EIT section
               int r = safe_read(filters[a].handle, buf, sizeof(buf));
               if (r > 3) // minimum number of bytes necessary to get section length
               {
                  int seclen = ((buf[1] & 0x0F) << 8) | (buf[2] & 0xFF) + 3;
                  int pid = filters[a].pid;
                  if (seclen == r)
                  {
                     //dsyslog("Received pid 0x%04X with table ID 0x%02X and length of %4d\n", pid, buf[0], seclen);
                     switch (pid)
                     {
                        case 0x00:
                           if (buf[0] == 0x00)
                           {
                              LOCK_THREAD;
                              if (pmtPid && time(NULL) - lastPmtScan > PMT_SCAN_TIMEOUT) {
                                 DelFilter(pmtPid, 0x02);
                                 pmtPid = 0;
                                 pmtIndex++;
                                 lastPmtScan = time(NULL);
                                 }
                              if (!pmtPid) {
                                 cMutexLock MutexLock(&schedulesMutex); // since the xMem... stuff is not thread safe, we need to use a "global" mutex
                                 struct LIST *pat = siParsePAT(buf);
                                 if (pat) {
                                    int Index = 0;
                                    for (struct Program *prg = (struct Program *)pat->Head; prg; prg = (struct Program *)xSucc(prg)) {
                                        if (prg->ProgramID) {
                                           if (Index++ == pmtIndex) {
                                              pmtPid = prg->NetworkPID;
                                              AddFilter(pmtPid, 0x02);
                                              break;
                                              }
                                           }
                                        }
                                    if (!pmtPid)
                                       pmtIndex = 0;
                                    }
                                 xMemFreeAll(NULL);
                                 }
                           }
                           break;
                        case 0x14:
                           if (buf[0] == 0x70)
                           {
                              if (Setup.SetSystemTime && Setup.TimeTransponder && ISTRANSPONDER(currentTransponder, Setup.TimeTransponder))
                              {
                                 cTDT ctdt((tdt_t *)buf);
                                 ctdt.SetSystemTime();
                              }
                           }
                              /*XXX this comes pretty often:
                           else
                              dsyslog("Time packet was not 0x70 but 0x%02x\n", (int)buf[0]);
                              XXX*/
                           break;

                        case 0x12:
                           if (buf[0] != 0x72)
                           {
                              cMutexLock MutexLock(&schedulesMutex);
                              cEIT ceit(buf, seclen, schedules);
                              ceit.ProcessEIT(buf, currentSource);
                           }
                           else
                              dsyslog("Received stuffing section in EIT\n");
                           break;

                        default: {
                           LOCK_THREAD;
                           if (pid == pmtPid && buf[0] == 0x02 && currentSource && currentTransponder) {
                              cMutexLock MutexLock(&schedulesMutex); // since the xMem... stuff is not thread safe, we need to use a "global" mutex
                              struct Pid *pi = siParsePMT(buf);
                              if (pi) {
                                 for (struct LIST *d = (struct LIST *)pi->Descriptors; d; d = (struct LIST *)xSucc(d)) {
                                     if (DescriptorTag(d) == DESCR_CA) {
                                        uchar *Data = ((ConditionalAccessDescriptor *)d)->Data;
                                        int CaSystem = (Data[2] << 8) | Data[3];
                                        if (!caDescriptors->Get(currentSource, currentTransponder, pi->ProgramID, CaSystem)) {
                                           cMutexLock MutexLock(&caDescriptorsMutex);
                                           caDescriptors->Add(new cCaDescriptor(currentSource, currentTransponder, pi->ProgramID, CaSystem, ((ConditionalAccessDescriptor *)d)->Amount, Data));
                                           }
                                        //XXX update???
                                        }
                                     }
                                 }
                              xMemFreeAll(NULL);
                              lastPmtScan = 0; // this triggers the next scan
                              }
                           }
                           break;
                     }
                  }
                  /*
                  else
                     dsyslog("read incomplete section - seclen = %d, r = %d", seclen, r);
                  */
               }
            }
         }
      }
   }

   dsyslog("EIT processing thread ended (pid=%d)%s", getpid(), masterSIProcessor ? " - master" : "");
}

/** Add a filter with packet identifier pid and
table identifer tid */
bool cSIProcessor::AddFilter(unsigned short pid, u_char tid)
{
   dmx_sct_filter_params sctFilterParams;
   memset(&sctFilterParams, 0, sizeof(sctFilterParams));
   sctFilterParams.pid = pid;
   sctFilterParams.timeout = 0;
   sctFilterParams.flags = DMX_IMMEDIATE_START;
   sctFilterParams.filter.filter[0] = tid;
   sctFilterParams.filter.mask[0] = 0xFF;

   for (int a = 0; a < MAX_FILTERS; a++)
   {
      if (!filters[a].inuse)
      {
         filters[a].pid = pid;
         filters[a].tid = tid;
         if ((filters[a].handle = open(fileName, O_RDWR | O_NONBLOCK)) >= 0)
         {
            if (ioctl(filters[a].handle, DMX_SET_FILTER, &sctFilterParams) >= 0)
               filters[a].inuse = true;
            else
            {
               esyslog("ERROR: can't set filter (pid=%d, tid=%02X)", pid, tid);
               close(filters[a].handle);
               return false;
            }
            // dsyslog("Registered filter handle %04x, pid = %02d, tid = %02d", filters[a].handle, filters[a].pid, filters[a].tid);
         }
         else
         {
            esyslog("ERROR: can't open filter handle");
            return false;
         }
         return true;
      }
   }
   esyslog("ERROR: too many filters");

   return false;
}

bool cSIProcessor::DelFilter(unsigned short pid, u_char tid)
{
   for (int a = 0; a < MAX_FILTERS; a++)
   {
      if (filters[a].inuse && filters[a].pid == pid && filters[a].tid == tid)
      {
         close(filters[a].handle);
         // dsyslog("Deregistered filter handle %04x, pid = %02d, tid = %02d", filters[a].handle, filters[a].pid, filters[a].tid);
         filters[a].inuse = false;
         return true;
      }
   }
   return false;
}

/**  */
bool cSIProcessor::ShutDownFilters(void)
{
   for (int a = 0; a < MAX_FILTERS; a++)
   {
      if (filters[a].inuse)
      {
         close(filters[a].handle);
         // dsyslog("Deregistered filter handle %04x, pid = %02d, tid = %02d", filters[a].handle, filters[a].pid, filters[a].tid);
         filters[a].inuse = false;
      }
   }

   return true; // there's no real 'boolean' to return here...
}

/** */
void cSIProcessor::SetCurrentTransponder(int CurrentSource, int CurrentTransponder)
{
  currentSource = CurrentSource;
  currentTransponder = CurrentTransponder;
}

/** */
bool cSIProcessor::SetCurrentChannelID(tChannelID channelid)
{
  cMutexLock MutexLock(&schedulesMutex);
  return schedules ? schedules->SetCurrentChannelID(channelid) : false;
}

void cSIProcessor::TriggerDump(void)
{
  cMutexLock MutexLock(&schedulesMutex);
  lastDump = 0;
}

int cSIProcessor::GetCaDescriptors(int Source, int Transponder, int ServiceId, int BufSize, uchar *Data)
{
  if (BufSize > 0 && Data) {
     cMutexLock MutexLock(&caDescriptorsMutex);
     int length = 0;
     for (int i = -1; ; i--) {
         const cCaDescriptor *d = caDescriptors->Get(Source, Transponder, ServiceId, i);
         if (d) {
            if (length + d->Length() <= BufSize) {
               memcpy(Data + length, d->Data(), d->Length());
               length += d->Length();
               }
            else
               return -1;
            }
         else
            break;
         }
     return length;
     }
  return -1;
}