summaryrefslogtreecommitdiff
path: root/filter.c
blob: e1abd574b8b3fc1ab40fd4fadf320f91ad18866c (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
/*
 * filter.c: Section filter
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: filter.c 5.1 2021/03/16 15:10:54 kls Exp $
 */

#include "filter.h"
#include "sections.h"

// --- cSectionSyncer --------------------------------------------------------

cSectionSyncer::cSectionSyncer(bool Random)
{
  random = Random;
  Reset();
}

void cSectionSyncer::Reset(void)
{
  currentVersion = -1;
  currentSection = -1;
  synced = false;
  complete = false;
  segments = 0;
  memset(sections, 0x00, sizeof(sections));
}

bool cSectionSyncer::Check(uchar Version, int SectionNumber)
{
  if (Version != currentVersion) {
     Reset();
     currentVersion = Version;
     }
  if (complete)
     return false;
  if (!random) {
     if (!synced) {
        if (SectionNumber == 0) {
           currentSection = 0;
           synced = true;
           }
        else
           return false;
        }
     if (SectionNumber != currentSection)
        return false;
     }
  return !GetSectionFlag(SectionNumber);
}

bool cSectionSyncer::Processed(int SectionNumber, int LastSectionNumber, int SegmentLastSectionNumber)
{
  SetSectionFlag(SectionNumber, true); // the flag for this section
  if (!random)
     currentSection++;           // expect the next section
  int Index = SectionNumber / 8; // the segment (byte) in which this section lies
  uchar b = 0xFF;                // all sections in this segment
  if (SegmentLastSectionNumber < 0 && Index == LastSectionNumber / 8)
     SegmentLastSectionNumber = LastSectionNumber;
  if (SegmentLastSectionNumber >= 0) {
     b >>= 7 - (SegmentLastSectionNumber & 0x07); // limits them up to the last section in this segment
     if (!random && SectionNumber == SegmentLastSectionNumber)
        currentSection = (SectionNumber + 8) & ~0x07; // expect first section of next segment
     }
  if (sections[Index] == b)           // all expected sections in this segment have been received
     segments |= 1 << Index;          // so we set the respective bit in the segments flags
  uint32_t s = 0xFFFFFFFF;            // all segments
  s >>= 31 - (LastSectionNumber / 8); // limits them up to the last expected segment
  complete = segments == s;
  return complete;
}

#if DEPRECATED_SECTIONSYNCER_SYNC_REPEAT
void cSectionSyncer::Repeat(void)
{
  SetSectionFlag(currentSection, false);
  synced = false;
  complete = false;
}

bool cSectionSyncer::Sync(uchar Version, int Number, int LastNumber)
{
  if (Version != currentVersion) {
     Reset();
     currentVersion = Version;
     }
  if (!synced) {
     if (Number != 0)
        return false;
     else
        synced = true;
     }
  currentSection = Number;
  bool Result = !GetSectionFlag(Number);
  SetSectionFlag(Number, true);
  if (Number == LastNumber)
     complete = true;
  return Result;
}
#endif

// --- cFilterData -----------------------------------------------------------

cFilterData::cFilterData(void)
{
  pid = 0;
  tid = 0;
  mask = 0;
  sticky = false;
}

cFilterData::cFilterData(u_short Pid, u_char Tid, u_char Mask, bool Sticky)
{
  pid = Pid;
  tid = Tid;
  mask = Mask;
  sticky = Sticky;
}

cFilterData& cFilterData::operator= (const cFilterData &FilterData)
{
  pid = FilterData.pid;
  tid = FilterData.tid;
  mask = FilterData.mask;
  sticky = FilterData.sticky;
  return *this;
}

bool cFilterData::Is(u_short Pid, u_char Tid, u_char Mask)
{
  return pid == Pid && tid == Tid && mask == Mask;
}

bool cFilterData::Matches(u_short Pid, u_char Tid)
{
  return pid == Pid && tid == (Tid & mask);
}

// --- cFilter ---------------------------------------------------------------

cFilter::cFilter(void)
{
  sectionHandler = NULL;
  on = false;
}

cFilter::cFilter(u_short Pid, u_char Tid, u_char Mask)
{
  sectionHandler = NULL;
  on = false;
  Set(Pid, Tid, Mask);
}

cFilter::~cFilter()
{
  if (sectionHandler)
     sectionHandler->Detach(this);
}

int cFilter::Source(void)
{
  return sectionHandler ? sectionHandler->Source() : 0;
}

int cFilter::Transponder(void)
{
  return sectionHandler ? sectionHandler->Transponder() : 0;
}

const cChannel *cFilter::Channel(void)
{
  return sectionHandler ? sectionHandler->Channel() : NULL;
}

void cFilter::SetStatus(bool On)
{
  if (sectionHandler && on != On) {
     cFilterData *fd = data.First();
     while (fd) {
           if (On)
              sectionHandler->Add(fd);
           else {
              sectionHandler->Del(fd);
              if (!fd->sticky) {
                 cFilterData *next = data.Next(fd);
                 data.Del(fd);
                 fd = next;
                 continue;
                 }
              }
           fd = data.Next(fd);
           }
     on = On;
     }
}

bool cFilter::Matches(u_short Pid, u_char Tid)
{
  if (on) {
     for (cFilterData *fd = data.First(); fd; fd = data.Next(fd)) {
         if (fd->Matches(Pid, Tid))
            return true;
         }
     }
  return false;
}

void cFilter::Set(u_short Pid, u_char Tid, u_char Mask)
{
  Add(Pid, Tid, Mask, true);
}

void cFilter::Add(u_short Pid, u_char Tid, u_char Mask, bool Sticky)
{
  cFilterData *fd = new cFilterData(Pid, Tid, Mask, Sticky);
  data.Add(fd);
  if (sectionHandler && on)
     sectionHandler->Add(fd);
}

void cFilter::Del(u_short Pid, u_char Tid, u_char Mask)
{
  for (cFilterData *fd = data.First(); fd; fd = data.Next(fd)) {
      if (fd->Is(Pid, Tid, Mask)) {
         if (sectionHandler && on)
            sectionHandler->Del(fd);
         data.Del(fd);
         return;
         }
      }
}