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

cDynamiteStatus *cDynamiteStatus::status = NULL;

#define SETINITIALCHANNELTIMEOUT 60 // 60 seconds

cDynamiteStatus::cDynamiteStatus(int InitialChannel)
{
  init = time(NULL);
  initialChannel = InitialChannel;
  initialChannelSet = false;
  switchCount = 0;
  isyslog("dynamite: initial channel is %d", initialChannel);
}

void cDynamiteStatus::ChannelSwitch(const cDevice *Device, int ChannelNumber)
{
  if ((ChannelNumber == 0) || initialChannelSet || (initialChannel < 0) || (switchCount > 1))
     return;
  if ((cDevice::PrimaryDevice() != cDevice::ActualDevice()) && (cDevice::PrimaryDevice() == Device))
     return;
  if (ChannelNumber == initialChannel) {
     initialChannelSet = true;
     return;
     }
  if ((time(NULL) - init) > SETINITIALCHANNELTIMEOUT) {
     isyslog("dynamite: no devices within %d seconds for receiving initial channel %d, giving up", SETINITIALCHANNELTIMEOUT, initialChannel);
     initialChannelSet = 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 initial channel on device attach");
}

void cDynamiteStatus::Init(void)
{
  if (status)
     return;
  if (*Setup.InitialChannel) {
     cString cid = Setup.InitialChannel;
     if (isnumber(cid)) { // for compatibility with old setup.conf files
        if (cChannel *Channel = Channels.GetByNumber(atoi(cid)))
           cid = Channel->GetChannelID().ToString();
        }
     if (cChannel *Channel = Channels.GetByChannelID(tChannelID::FromString(cid)))
        status = new cDynamiteStatus(Channel->Number());
     }
}

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

void cDynamiteStatus::SetInitialChannel(void)
{
  if (status == NULL)
     return;
  if (status->initialChannelSet) {
     DeInit();
     return;
     }
  if (!Channels.SwitchTo(status->initialChannel))
     status->switchCount--;
}