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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
#include <vdr/channels.h>
#include <ctype.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 | W_OK)==0)
{
dsyslog("infosatepg: deleting old %s",file);
unlink(file);
}
Processed=false;
receivedall=false;
missed=0;
Unlocated=0;
lastpkt=-1;
receivedpercent=0;
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,&receivedall,sizeof(receivedall));
if (ret!=sizeof(receivedall)) return -1;
ret=write(fd,&receivedpercent,sizeof(receivedpercent));
if (ret!=sizeof(receivedpercent)) 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,&missed,sizeof (missed));
if (ret!=sizeof (missed)) return -1;
ret=write (fd,&Unlocated,sizeof (Unlocated));
if (ret!=sizeof (Unlocated)) 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,&receivedall,sizeof (receivedall));
if (ret!=sizeof (receivedall)) return -1;
ret=read (fd,&receivedpercent,sizeof(receivedpercent));
if (ret!=sizeof (receivedpercent)) 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,&missed,sizeof (missed));
if (ret!=sizeof (missed)) return -1;
ret=read (fd,&Unlocated,sizeof (Unlocated));
if (ret!=sizeof (Unlocated)) 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;
// check file
if (file[0]!=0)
{
if (access(file,R_OK | W_OK)==-1)
{
// file doesnt exist -> receive it again
esyslog("infosatepg: cannot access %s",file);
Init(file,day,month,pktcnt);
}
}
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
void cGlobalInfosatdata::CheckMissed(int ActualPacket)
{
if (receivedall) return; // count missed packets while receiving
if ((ActualPacket!=(lastpkt+1)) && (lastpkt!=-1))
{
missed++;
}
lastpkt=ActualPacket;
}
bool cGlobalInfosatdata::CheckReceivedAll()
{
int donecnt=0;
receivedpercent=0;
if (pktcnt==0) return false;
bool retval=true;
for (int i=0; i<pktcnt; i++)
{
if (GetBit(i)==true) donecnt++;
else retval=false;
}
receivedpercent=((donecnt*100)/pktcnt);
if (retval==true) receivedall=true;
return retval;
}
// --- cGlobalInfosatepg -----------------------------------------------------
cGlobalInfosatepg::cGlobalInfosatepg()
{
// set default values
// set private member
switched=false;
directory=NULL;
infosatchannels=NULL;
this_day=-1;
this_month=-1;
numinfosatchannels=0;
wakeuptime=-1;
// set public member
LastCurrentChannel=-1;
Pid=1809;
Srate = 22000;
Frequency = 12604;
Polarization ='h';
channel=-1;
MAC[0]=0x01;
MAC[1]=0x00;
MAC[2]=0x5e;
MAC[3]=0x02;
MAC[4]=0x02;
WaitTime=10; // default 10 seconds
SetDirectory ("/tmp");
NoWakeup=false;
NoDeferredShutdown=false;
ActualMac=0;
HideMainMenu=true;
}
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(ether_header *eth_hdr)
{
if (!eth_hdr) return false;
if (eth_hdr->ether_dhost[0]!=MAC[0]) return false;
if (eth_hdr->ether_dhost[1]!=MAC[1]) return false;
if (eth_hdr->ether_dhost[2]!=MAC[2]) return false;
if (eth_hdr->ether_dhost[3]!=MAC[3]) return false;
if (eth_hdr->ether_dhost[4]!=MAC[4]) return false;
if (eth_hdr->ether_dhost[5]<EPG_FIRST_DAY_MAC) return false;
if (eth_hdr->ether_dhost[5]>EPG_LAST_DAY_MAC) return false;
return true;
}
void cGlobalInfosatepg::AddChannel(tChannelID ChannelID,int Usage,int Days)
{
infosatchannels= (struct infosatchannels *) realloc (infosatchannels,
(numinfosatchannels+1) *sizeof (struct infosatchannels));
if (!infosatchannels) return;
infosatchannels[numinfosatchannels].ChannelID=ChannelID;
infosatchannels[numinfosatchannels].Usage=Usage & 0xFFFF;
if (Days<=0) Days=1;
if (Days>EPG_DAYS) Days=EPG_DAYS;
infosatchannels[numinfosatchannels].Days=Days;
numinfosatchannels++;
}
void cGlobalInfosatepg::RemoveChannel(int Index)
{
if ((Index<0) || (Index>numinfosatchannels)) return;
int i;
for (i=Index; i<(numinfosatchannels-1); i++)
{
infosatchannels[i].ChannelID=infosatchannels[i+1].ChannelID;
infosatchannels[i].Usage=infosatchannels[i+1].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::SetChannelOptions(int Index,int Usage,int Days)
{
if (numinfosatchannels==0) return false;
if ((Index<0) || (Index>numinfosatchannels-1)) return false;
bool ret=false;
if ((infosatchannels[Index].Usage!=Usage) | (infosatchannels[Index].Days!=Days)) ret=true;
infosatchannels[Index].Usage=Usage;
infosatchannels[Index].Days=Days;
return ret;
}
int cGlobalInfosatepg::GetChannelUsage(int Index)
{
if (numinfosatchannels==0) return USE_NOTHING;
if ((Index<0) || (Index>numinfosatchannels-1)) return USE_NOTHING;
return infosatchannels[Index].Usage;
}
int cGlobalInfosatepg::GetChannelDays(int Index)
{
if (numinfosatchannels==0) return 0;
if ((Index<0) || (Index>numinfosatchannels-1)) return 0;
return infosatchannels[Index].Days;
}
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;
}
if (ret!=-1)
{
ret=write (f,&wakeuptime,sizeof(wakeuptime));
if (ret!=sizeof (wakeuptime)) 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;
}
if (ret!=-1)
{
ret=read (f,&wakeuptime,sizeof (wakeuptime));
if (ret!=sizeof (wakeuptime)) ret=-1;
}
close (f);
if (ret==-1)
{
unlink (file); // but it's not ok if the read failed!
ResetReceivedAll();
}
return ret;
}
void cGlobalInfosatepg::ResetReceivedAll(void)
{
for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
{
Infosatdata[mac].ResetReceivedAll();
}
wakeuptime=-1;
this_day=-1;
this_month=-1;
}
void cGlobalInfosatepg::ResetProcessed (void)
{
for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
{
Infosatdata[mac].Processed=false;
}
wakeuptime=-1;
this_day=-1;
this_month=-1;
}
bool cGlobalInfosatepg::ProcessedAll()
{
int numProcessed=0;
for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
{
if (Infosatdata[mac].Processed) numProcessed++;
}
// all days processed
if (numProcessed==EPG_DAYS) return true;
return false;
}
bool cGlobalInfosatepg::ReceivedAll(int *Day, int *Month)
{
bool res=false;
time_t Now=time (NULL);
struct tm tm;
if (!localtime_r (&Now,&tm)) return false;
// First check if we have received all data
int numReceived=0;
for (int mac=EPG_FIRST_DAY_MAC; mac<=EPG_LAST_DAY_MAC; mac++)
{
if (Infosatdata[mac].ReceivedAll()) numReceived++;
}
// All days fully received
if (numReceived==EPG_DAYS)
{
if ((this_day==-1) && (this_month==-1))
{
isyslog ("infosatepg: all data received");
this_day=tm.tm_mday;
this_month=tm.tm_mon+1;
}
// From today?
if ((this_day==tm.tm_mday) && (this_month==tm.tm_mon+1))
{
res=true;
}
else
{
// New day, but new data is ready only after wakeup-time
time_t Time=WakeupTime();
if (Time==-1)
{
// no wakeuptime set, just wait
res=true;
}
else
{
Time-=300;
if (Now>=Time)
{
// new day and new data should be available
ResetReceivedAll();
res=false;
}
else
{
// wait till we are after wakeuptime
res=true;
}
}
}
}
if (Day) *Day=this_day;
if (Month) *Month=this_month;
return res;
}
bool cGlobalInfosatepg::FindReceiverChannel()
{
cChannel *chan;
int source = cSource::FromString("S19.2E"); // only from astra 19.2E
// Try to find a channel with the exact frequency
for (int i=0; i<=Channels.MaxNumber(); i++)
{
chan = Channels.GetByNumber(i,0);
if (chan)
{
if (chan->Source()!=source) continue;
if (chan->Srate()!=Srate) continue;
if (chan->Frequency()!=Frequency) continue;
#if VDRVERSNUM < 10713
if (chan->Polarization()!=Polarization) continue;
#else
const char *polarization=strpbrk(chan->Parameters(),"HVLRhvlr");
if (!polarization) continue;
if (toupper(*polarization)!=toupper(Polarization)) continue;
#endif
channel=i;
return true;
}
}
// None found? Then try to find a channel with frequency + 1
for (int i=0; i<=Channels.MaxNumber(); i++)
{
chan = Channels.GetByNumber(i,0);
if (chan)
{
if (chan->Source()!=source) continue;
if (chan->Srate()!=Srate) continue;
if (chan->Frequency()!=(Frequency+1)) continue;
#if VDRVERSNUM < 10713
if (chan->Polarization()!=Polarization) continue;
#else
const char *polarization=strpbrk(chan->Parameters(),"HVLRhvlr");
if (!polarization) continue;
if (toupper(*polarization)!=toupper(Polarization)) continue;
#endif
channel=i;
return true;
}
}
// Still none found? Then try to find a channel with frequency - 1
for (int i=0; i<=Channels.MaxNumber(); i++)
{
chan = Channels.GetByNumber(i,0);
if (chan)
{
if (chan->Source()!=source) continue;
if (chan->Srate()!=Srate) continue;
if (chan->Frequency()!=(Frequency-1)) continue;
#if VDRVERSNUM < 10713
if (chan->Polarization()!=Polarization) continue;
#else
const char *polarization=strpbrk(chan->Parameters(),"HVLRhvlr");
if (!polarization) continue;
if (toupper(*polarization)!=toupper(Polarization)) continue;
#endif
channel=i;
return true;
}
}
// No channel available
channel=-1;
return false;
}
|