summaryrefslogtreecommitdiff
path: root/eitscan.c
blob: 4273f773af2fbb40da5feb15c7fc1c0cfc2c5aaa (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
/*
 * eitscan.c: EIT scanner
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: eitscan.c 1.17 2004/01/10 16:50:51 kls Exp $
 */

#include "eitscan.h"
#include <stdlib.h>
#include "channels.h"
#include "dvbdevice.h"

// --- cScanData -------------------------------------------------------------

class cScanData : public cListObject {
private:
  int source;
  int transponder;
public:
  cScanData(int Source, int Transponder);
  virtual bool operator< (const cListObject &ListObject);
  int Source(void) { return source; }
  int Transponder(void) { return transponder; }
  cChannel *GetChannel(void);
  };

cScanData::cScanData(int Source, int Transponder)
{
  source = Source;
  transponder = Transponder;
}

bool cScanData::operator< (const cListObject &ListObject)
{
  cScanData *sd = (cScanData *)&ListObject;
  return source < sd->source || source == sd->source && transponder < sd->transponder;
}

//XXX this might be done differently later...
cChannel *cScanData::GetChannel(void)
{
  for (cChannel *Channel = Channels.First(); Channel; Channel = Channels.Next(Channel)) {
      if (!Channel->GroupSep() && Channel->Source() == source && ISTRANSPONDER(Channel->Transponder(), transponder))
         return Channel;
      }
  return NULL;
}

// --- cScanList -------------------------------------------------------------

class cScanList : public cList<cScanData> {
public:
  cScanList(void);
  void AddTransponder(const cChannel *Channel);
  };

cScanList::cScanList(void)
{
  for (cChannel *ch = Channels.First(); ch; ch = Channels.Next(ch))
      AddTransponder(ch);
  Sort();
}

void cScanList::AddTransponder(const cChannel *Channel)
{
  for (cScanData *sd = First(); sd; sd = Next(sd)) {
      if (sd->Source() == Channel->Source() && sd->Transponder() == Channel->Transponder())
         return;
      }
  Add(new cScanData(Channel->Source(), Channel->Transponder()));
}

// --- cEITScanner -----------------------------------------------------------

cEITScanner EITScanner;

cEITScanner::cEITScanner(void)
{
  lastScan = lastActivity = time(NULL);
  currentDevice = NULL;
  currentChannel = 0;
  memset(lastChannel, 0, sizeof(lastChannel));
  scanList = NULL;
}

cEITScanner::~cEITScanner()
{
  delete scanList;
}

void cEITScanner::Activity(void)
{
  if (currentChannel) {
     Channels.SwitchTo(currentChannel);
     currentChannel = 0;
     }
  lastActivity = time(NULL);
}

void cEITScanner::Process(void)
{
  if (Setup.EPGScanTimeout && Channels.MaxNumber() > 1) {
     time_t now = time(NULL);
     if (now - lastScan > ScanTimeout && now - lastActivity > ActivityTimeout) {
        if (Channels.Lock(false, 10)) {
           if (!scanList)
              scanList = new cScanList();
           for (bool AnyDeviceSwitched = false; !AnyDeviceSwitched; ) {
               cScanData *ScanData = NULL;
               for (int i = 0; i < cDevice::NumDevices(); i++) {
                   if (ScanData || (ScanData = scanList->First()) != NULL) {
                      cDevice *Device = cDevice::GetDevice(i);
                      if (Device) {
                         if (Device != cDevice::PrimaryDevice() || (cDevice::NumDevices() == 1 && Setup.EPGScanTimeout && now - lastActivity > Setup.EPGScanTimeout * 3600)) {
                            if (!(Device->Receiving(true) || Device->Replaying())) {
                               cChannel *Channel = ScanData->GetChannel();
                               if (Channel) {
                                  //XXX if (Device->ProvidesTransponder(Channel)) {
                                  if ((!Channel->Ca() || Channel->Ca() == Device->DeviceNumber() + 1 || Channel->Ca() >= 0x0100) && Device->ProvidesTransponder(Channel)) { //XXX temporary for the 'sky' plugin
                                     if (Device == cDevice::PrimaryDevice() && !currentChannel)
                                        currentChannel = Device->CurrentChannel();
                                     currentDevice = Device;//XXX see also dvbdevice.c!!!
                                     Device->SwitchChannel(Channel, false);
                                     currentDevice = NULL;
                                     scanList->Del(ScanData);
                                     ScanData = NULL;
                                     AnyDeviceSwitched = true;
                                     }
                                  }
                               }
                            }
                         }
                      }
                   else
                      break;
                   }
               if (ScanData && !AnyDeviceSwitched) {
                  scanList->Del(ScanData);
                  ScanData = NULL;
                  }
               if (!scanList->Count()) {
                  delete scanList;
                  scanList = NULL;
                  break;
                  }
               }
           }
        lastScan = time(NULL);
        Channels.Unlock();
        }
     }
}