summaryrefslogtreecommitdiff
path: root/global.cpp
blob: 0cf65ba495b6ecc1fa6b3c997f85470ad10c521c (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
#include <vdr/channels.h>
#include "global.h"

// --- cGlobalInfosatdata
cGlobalInfosatdata::cGlobalInfosatdata()
{
    file[0]=0;
    Init(NULL,0,0,0);
}

bool cGlobalInfosatdata::NeverSeen(int Day, int Month, int Packetcount)
{
    if ((day!=Day) || (month!=Month) || (pktcnt!=Packetcount))
    {
        return true;
    }
    else
    {
        return false;
    }
}

void cGlobalInfosatdata::Init(char *File, int Day, int Month, int Packetcount)
{
    if (access(file,R_OK)==0)
    {
        dsyslog("infosatepg: deleting old %s",file);
        unlink(file);
    }
    ready=false;
    processed=false;
    day=Day;
    month=Month;
    pktcnt=Packetcount;
    if (File) snprintf(file,sizeof(file),"%s",File);
    else file[0]=0;
    memset(bitfield,0,sizeof(bitfield));
}

int cGlobalInfosatdata::Save(int fd)
{
    if (fd==-1) return -1;
    ssize_t ret=write(fd,&ready,sizeof(ready));
    if (ret!=sizeof(ready)) return -1;
    ret=write(fd,&processed,sizeof(processed));
    if (ret!=sizeof(processed)) return -1;
    ret=write(fd,&day,sizeof(day));
    if (ret!=sizeof(day)) return -1;
    ret=write(fd,&month,sizeof(month));
    if (ret!=sizeof(month)) return -1;
    ret=write(fd,&pktcnt,sizeof(pktcnt));
    if (ret!=sizeof(pktcnt)) return -1;
    ret=write(fd,&bitfield,sizeof(bitfield));
    if (ret!=sizeof(bitfield)) return -1;
    ret=write(fd,&file,sizeof(file));
    if (ret!=sizeof(file)) return -1;
    return ret;
}

int cGlobalInfosatdata::Load(int fd)
{
    if (fd==-1) return -1;
    ssize_t ret=read(fd,&ready,sizeof(ready));
    if (ret!=sizeof(ready)) return -1;
    ret=read(fd,&processed,sizeof(processed));
    if (ret!=sizeof(processed)) return -1;
    ret=read(fd,&day,sizeof(day));
    if (ret!=sizeof(day)) return -1;
    ret=read(fd,&month,sizeof(month));
    if (ret!=sizeof(month)) return -1;
    ret=read(fd,&pktcnt,sizeof(pktcnt));
    if (ret!=sizeof(pktcnt)) return -1;
    ret=read(fd,&bitfield,sizeof(bitfield));
    if (ret!=sizeof(bitfield)) return -1;
    ret=read(fd,&file,sizeof(file));
    if (ret!=sizeof(file)) return -1;
    dsyslog("infosatepg: loaded file=*%s*",file);
    return ret;
}

#ifdef INFOSATEPG_DEBUG
void cGlobalInfosatdata::Debug(const char *Directory)
{
    char file[1024];
    snprintf(file,sizeof(file),"%s/infosatepg9999_999.dat",Directory);
    Init(file,99,99,999);
    ready=true;
}
#endif

int cGlobalInfosatdata::ReceivedPercent()
{
    if (pktcnt==0) return 0;
    int donecnt=0;
    for (int i=0; i<pktcnt; i++)
    {
        if (GetBit(i)==true) donecnt++;
    }
    return ((donecnt*100)/pktcnt);
}

bool cGlobalInfosatdata::ReceivedAll()
{
    if (pktcnt==0) return false;
    for (int i=0; i<pktcnt; i++)
    {
        if (GetBit(i)==false) return false;
    }
    ready=true;
    return true;
}

// --- cGlobalInfosatepg -----------------------------------------------------
cGlobalInfosatepg::cGlobalInfosatepg()
{
    // set default values
    Pid=1809;
    EventTimeDiff=480; // default 8 minutes
    Channel=1;
    MAC[0]=0x01;
    MAC[1]=0x00;
    MAC[2]=0x5e;
    MAC[3]=0x02;
    MAC[4]=0x02;
    WaitTime=10; // default 10 seconds
    Switched=false;
    directory=NULL;
    SetDirectory("/tmp");
    infosatchannels=NULL;
    numinfosatchannels=0;
    this_day=-1;
    this_month=-1;
}

cGlobalInfosatepg::~cGlobalInfosatepg()
{
    free((void *) directory);
    free(infosatchannels);
}

bool cGlobalInfosatepg::SetDirectory(const char *Directory)
{
    if (access(Directory,W_OK | R_OK | X_OK)==-1)
    {
        // cannot access, maybe create?
        if (mkdir(Directory,0775)==-1)
        {
            // even if the Directory exists, we cannot access
            return false;
        }
        if (access(Directory,W_OK | R_OK| X_OK)==-1) return false;
    }
    free((void *) directory);
    directory = Directory ? strdup(Directory) : NULL;
    return true;
}

bool cGlobalInfosatepg::CheckMAC(struct ethhdr *eth_hdr)
{
    if (!eth_hdr) return false;
    if (eth_hdr->h_dest[0]!=MAC[0]) return false;
    if (eth_hdr->h_dest[1]!=MAC[1]) return false;
    if (eth_hdr->h_dest[2]!=MAC[2]) return false;
    if (eth_hdr->h_dest[3]!=MAC[3]) return false;
    if (eth_hdr->h_dest[4]!=MAC[4]) return false;

    if (eth_hdr->h_dest[5]<EPG_FIRST_DAY_MAC) return false;
    if (eth_hdr->h_dest[5]>EPG_LAST_DAY_MAC) return false;

    return true;
}

void cGlobalInfosatepg::AddChannel(tChannelID ChannelID,int Usage)
{
    infosatchannels=(struct infosatchannels *) realloc(infosatchannels,
                    (numinfosatchannels+1)*sizeof(struct infosatchannels));
    if (!infosatchannels) return;
    infosatchannels[numinfosatchannels].ChannelID=ChannelID;
    infosatchannels[numinfosatchannels].Usage=Usage;
    numinfosatchannels++;
}

tChannelID cGlobalInfosatepg::GetChannelID(int Index)
{
    if (numinfosatchannels==0) return tChannelID::InvalidID;
    if ((Index<0) || (Index>numinfosatchannels-1))
        return tChannelID::InvalidID;
    return infosatchannels[Index].ChannelID;
}

bool cGlobalInfosatepg::ChannelExists(tChannelID ChannelID,int *Index)
{
    if (numinfosatchannels==0) return false;
    for (int i=0; i<numinfosatchannels; i++)
    {
        if (infosatchannels[i].ChannelID==tChannelID::InvalidID) continue;
        if (infosatchannels[i].ChannelID==ChannelID)
        {
            if (Index) *Index=i;
            return true;
        }
    }
    return false;
}

bool cGlobalInfosatepg::SetChannelUse(int Index,int Usage)
{
    if (numinfosatchannels==0) return false;
    if ((Index<0) || (Index>numinfosatchannels-1)) return false;
    bool ret=false;
    if (infosatchannels[Index].Usage!=Usage) ret=true;
    infosatchannels[Index].Usage=Usage;
    return ret;
}

int cGlobalInfosatepg::GetChannelUse(int Index)
{
    if (numinfosatchannels==0) return USE_NOTHING;
    if ((Index<0) || (Index>numinfosatchannels-1)) return USE_NOTHING;
    return infosatchannels[Index].Usage;
}

int cGlobalInfosatepg::Save()
{
    char file[1024];
    snprintf(file,sizeof(file),"%s/infosatepg.dat",directory);

    int f=open(file,O_RDWR|O_CREAT|O_TRUNC,0664);
    if (f==-1)
    {
        if (errno!=ENOSPC)
        {
            esyslog("infosatepg: unable to create file '%s'", file);
        }
        return -1;
    }

    int ret;
    for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
    {
        ret=Infosatdata[mac].Save(f);
        if (ret==-1) break;
    }

    if (ret!=-1)
    {
        ret=write(f,&this_day,sizeof(this_day));
        if (ret!=sizeof(this_day)) ret=-1;
    }
    if (ret!=-1)
    {
        ret=write(f,&this_month,sizeof(this_month));
        if (ret!=sizeof(this_month)) ret=-1;
    }

    close(f);
    if (ret==-1) unlink(file);
    return ret;
}

int cGlobalInfosatepg::Load()
{
    char file[1024];
    snprintf(file,sizeof(file),"%s/infosatepg.dat",directory);

    int f=open(file,O_RDONLY);
    if (f==-1) return 0; // it's ok if the file doesn't exist

    int ret;
    for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
    {
        ret=Infosatdata[mac].Load(f);
        if (ret==-1) break;
    }
    if (ret!=-1)
    {
        ret=read(f,&this_day,sizeof(this_day));
        if (ret!=sizeof(this_day)) ret=-1;
    }
    if (ret!=-1)
    {
        ret=read(f,&this_month,sizeof(this_month));
        if (ret!=sizeof(this_month)) ret=-1;
    }

    close(f);
    if (ret==-1)
    {
        unlink(file); // but it's not ok if the read failed!
        for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
        {
            Infosatdata[mac].Init(NULL,0,0,0);
        }

    }
    return ret;
}

void cGlobalInfosatepg::ResetProcessedFlags(void)
{
    dsyslog("infosatepg: reprocess files (later)");
    for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
    {
        Infosatdata[mac].ResetProcessed();
    }
}

void cGlobalInfosatepg::Lock(time_t Now)
{
    struct tm tm;
    if (!localtime_r(&Now,&tm))
    {
        this_day=-1;
        this_month=-1;
    }
    else
    {
        if ((tm.tm_mday!=this_day) || ((tm.tm_mon+1)!=this_month))
            isyslog("infosatepg: all data processed");
        this_day=tm.tm_mday;
        this_month=tm.tm_mon+1;
    }
}

bool cGlobalInfosatepg::isLocked(int *Day, int *Month)
{
    if (Day) *Day=this_day;
    if (Month) *Month=this_month;
    time_t Now=time(NULL);
    struct tm tm;
    if (!localtime_r(&Now,&tm)) return false;
    if ((tm.tm_mday==this_day) && ((tm.tm_mon+1)==this_month)) return true;
    return false;
}