summaryrefslogtreecommitdiff
path: root/status.c
blob: 45db455f10f56ccc46fa91768702dcbdd0690b4d (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
#include "status.h"

cDynamiteStatus *cDynamiteStatus::status = NULL;

#define SETSTARTUPCHANNELTIMEOUT 60 // 60 seconds

cDynamiteStatus::cDynamiteStatus(int StartupChannel)
{
  init = time(NULL);
  startupChannel = StartupChannel;
  startupChannelSet = false;
  switchCount = 0;
  isyslog("dynamite: startup channel is %d", startupChannel);
}

void cDynamiteStatus::ChannelSwitch(const cDevice *Device, int ChannelNumber, bool LiveView)
{
  if ((ChannelNumber == 0) || startupChannelSet || (startupChannel < 0) || (switchCount > 1))
     return;
  if ((cDevice::PrimaryDevice() != cDevice::ActualDevice()) && (cDevice::PrimaryDevice() == Device))
     return;
  if (ChannelNumber == startupChannel) {
     startupChannelSet = true;
     return;
     }
  if ((time(NULL) - init) > SETSTARTUPCHANNELTIMEOUT) {
     isyslog("dynamite: no devices within %d seconds for receiving startup channel %d, giving up", SETSTARTUPCHANNELTIMEOUT, startupChannel);
     startupChannelSet = true;
     return;
     }
  isyslog("dynamite: device %d switches channel to %d", Device->DeviceNumber() + 1, ChannelNumber);
  switchCount++;
  if (switchCount > 1)
     isyslog("dynamite: assuming manual channel switch, so give up trying to set startup channel on device attach");
}

void cDynamiteStatus::Init(void)
{
  if (status)
     return;
  int startupChannel = Setup.CurrentChannel;
  if (*Setup.InitialChannel) {
     cString cid = Setup.InitialChannel;
     if (isnumber(cid)) // for compatibility with old setup.conf files
        startupChannel = atoi(cid);
     else {
#if VDRVERSNUM > 20300
        LOCK_CHANNELS_READ;
        const cChannels *vdrchannels = Channels;
#else
        cChannels *vdrchannels = &Channels;
#endif
        if (const cChannel *Channel = vdrchannels->GetByChannelID(tChannelID::FromString(cid))) {
           status = new cDynamiteStatus(Channel->Number());
           return;
           }
        }
     }
#if VDRVERSNUM > 20300
  LOCK_CHANNELS_READ;
  const cChannels *vdrchannels = Channels;
#else
  cChannels *vdrchannels = &Channels;
#endif
  if (const cChannel *Channel = vdrchannels->GetByNumber(startupChannel))
     status = new cDynamiteStatus(Channel->Number());
}

void cDynamiteStatus::DeInit(void)
{
  if (status == NULL)
     return;
  delete status;
  status = NULL;
}

void cDynamiteStatus::SetStartupChannel(void)
{
  if (status == NULL)
     return;
  if (status->startupChannelSet) {
     DeInit();
     return;
     }
  isyslog("dynamite: new device attached, retry switching to startup channel %d", status->startupChannel);
#if VDRVERSNUM > 20300
  LOCK_CHANNELS_READ;
  const cChannels *vdrchannels = Channels;
#else
  cChannels *vdrchannels = &Channels;
#endif
  if (!vdrchannels->SwitchTo(status->startupChannel))
     status->switchCount--;
}