88fd677a4ed4aef644897eaca0e8eee6ec3e95cc
[enigma2.git] / lib / dvb / epgcache.cpp
1 #include <lib/dvb/epgcache.h>
2 #include <lib/dvb/dvb.h>
3
4 #undef EPG_DEBUG  
5
6 #include <time.h>
7 #include <unistd.h>  // for usleep
8 #include <sys/vfs.h> // for statfs
9 // #include <libmd5sum.h>
10 #include <lib/base/eerror.h>
11 #include <Python.h>
12
13 int eventData::CacheSize=0;
14 descriptorMap eventData::descriptors;
15 __u8 eventData::data[4108];
16 extern const uint32_t crc32_table[256];
17
18 eventData::eventData(const eit_event_struct* e, int size, int type)
19         :ByteSize(size&0xFF), type(type&0xFF)
20 {
21         if (!e)
22                 return;
23
24         __u32 descr[65];
25         __u32 *pdescr=descr;
26
27         __u8 *data = (__u8*)e;
28         int ptr=10;
29         int descriptors_length = (data[ptr++]&0x0F) << 8;
30         descriptors_length |= data[ptr++];
31         while ( descriptors_length > 0 )
32         {
33                 __u8 *descr = data+ptr;
34                 int descr_len = descr[1]+2;
35
36                 __u32 crc = 0;
37                 int cnt=0;
38                 while(cnt++ < descr_len)
39                         crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ data[ptr++]) & 0xFF];
40
41                 descriptorMap::iterator it =
42                         descriptors.find(crc);
43                 if ( it == descriptors.end() )
44                 {
45                         CacheSize+=descr_len;
46                         __u8 *d = new __u8[descr_len];
47                         memcpy(d, descr, descr_len);
48                         descriptors[crc] = descriptorPair(1, d);
49                 }
50                 else
51                         ++it->second.first;
52
53                 *pdescr++=crc;
54                 descriptors_length -= descr_len;
55         }
56         ByteSize = 12+((pdescr-descr)*4);
57         EITdata = new __u8[ByteSize];
58         CacheSize+=ByteSize;
59         memcpy(EITdata, (__u8*) e, 12);
60         memcpy(EITdata+12, descr, ByteSize-12);
61 }
62
63 const eit_event_struct* eventData::get() const
64 {
65         int pos = 12;
66         int tmp = ByteSize-12;
67
68         memcpy(data, EITdata, 12);
69         __u32 *p = (__u32*)(EITdata+12);
70         while(tmp>0)
71         {
72                 descriptorMap::iterator it =
73                         descriptors.find(*p++);
74                 if ( it != descriptors.end() )
75                 {
76                         int b = it->second.second[1]+2;
77                         memcpy(data+pos, it->second.second, b );
78                         pos += b;
79                 }
80                 tmp-=4;
81         }
82
83         return (const eit_event_struct*)data;
84 }
85
86 eventData::~eventData()
87 {
88         if ( ByteSize )
89         {
90                 CacheSize-=ByteSize;
91                 ByteSize-=12;
92                 __u32 *d = (__u32*)(EITdata+12);
93                 while(ByteSize)
94                 {
95                         descriptorMap::iterator it =
96                                 descriptors.find(*d++);
97                         if ( it != descriptors.end() )
98                         {
99                                 descriptorPair &p = it->second;
100                                 if (!--p.first) // no more used descriptor
101                                 {
102                                         CacheSize -= it->second.second[1];
103                                         delete [] it->second.second;    // free descriptor memory
104                                         descriptors.erase(it);  // remove entry from descriptor map
105                                 }
106                         }
107                         ByteSize-=4;
108                 }
109                 delete [] EITdata;
110         }
111 }
112
113 void eventData::load(FILE *f)
114 {
115         int size=0;
116         int id=0;
117         __u8 header[2];
118         descriptorPair p;
119         fread(&size, sizeof(int), 1, f);
120         while(size)
121         {
122                 fread(&id, sizeof(__u32), 1, f);
123                 fread(&p.first, sizeof(int), 1, f);
124                 fread(header, 2, 1, f);
125                 int bytes = header[1]+2;
126                 p.second = new __u8[bytes];
127                 p.second[0] = header[0];
128                 p.second[1] = header[1];
129                 fread(p.second+2, bytes-2, 1, f);
130                 descriptors[id]=p;
131                 --size;
132                 CacheSize+=bytes;
133         }
134 }
135
136 void eventData::save(FILE *f)
137 {
138         int size=descriptors.size();
139         descriptorMap::iterator it(descriptors.begin());
140         fwrite(&size, sizeof(int), 1, f);
141         while(size)
142         {
143                 fwrite(&it->first, sizeof(__u32), 1, f);
144                 fwrite(&it->second.first, sizeof(int), 1, f);
145                 fwrite(it->second.second, it->second.second[1]+2, 1, f);
146                 ++it;
147                 --size;
148         }
149 }
150
151 eEPGCache* eEPGCache::instance;
152 pthread_mutex_t eEPGCache::cache_lock=
153         PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
154 pthread_mutex_t eEPGCache::channel_map_lock=
155         PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
156
157 DEFINE_REF(eEPGCache)
158
159 eEPGCache::eEPGCache()
160         :messages(this,1), cleanTimer(this)//, paused(0)
161 {
162         eDebug("[EPGC] Initialized EPGCache");
163
164         CONNECT(messages.recv_msg, eEPGCache::gotMessage);
165         CONNECT(eDVBLocalTimeHandler::getInstance()->m_timeUpdated, eEPGCache::timeUpdated);
166         CONNECT(cleanTimer.timeout, eEPGCache::cleanLoop);
167
168         ePtr<eDVBResourceManager> res_mgr;
169         eDVBResourceManager::getInstance(res_mgr);
170         if (!res_mgr)
171                 eDebug("[eEPGCache] no resource manager !!!!!!!");
172         else
173                 res_mgr->connectChannelAdded(slot(*this,&eEPGCache::DVBChannelAdded), m_chanAddedConn);
174         instance=this;
175 }
176
177 void eEPGCache::timeUpdated()
178 {
179         if ( !thread_running() )
180         {
181                 eDebug("[EPGC] time updated.. start EPG Mainloop");
182                 run();
183         }
184         else
185                 messages.send(Message(Message::timeChanged));
186 }
187
188 void eEPGCache::DVBChannelAdded(eDVBChannel *chan)
189 {
190         if ( chan )
191         {
192 //              eDebug("[eEPGCache] add channel %p", chan);
193                 channel_data *data = new channel_data(this);
194                 data->channel = chan;
195                 data->prevChannelState = -1;
196                 singleLock s(channel_map_lock);
197                 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data* >(chan, data) );
198                 chan->connectStateChange(slot(*this, &eEPGCache::DVBChannelStateChanged), data->m_stateChangedConn);
199         }
200 }
201
202 void eEPGCache::DVBChannelRunning(iDVBChannel *chan)
203 {
204         singleLock s(channel_map_lock);
205         channelMapIterator it =
206                 m_knownChannels.find(chan);
207         if ( it == m_knownChannels.end() )
208                 eDebug("[eEPGCache] will start non existing channel %p !!!", chan);
209         else
210         {
211                 channel_data &data = *it->second;
212                 ePtr<eDVBResourceManager> res_mgr;
213                 if ( eDVBResourceManager::getInstance( res_mgr ) )
214                         eDebug("[eEPGCache] no res manager!!");
215                 else
216                 {
217                         ePtr<iDVBDemux> demux;
218                         if ( data.channel->getDemux(demux, 0) )
219                         {
220                                 eDebug("[eEPGCache] no demux!!");
221                                 return;
222                         }
223                         else
224                         {
225                                 RESULT res = demux->createSectionReader( this, data.m_NowNextReader );
226                                 if ( res )
227                                 {
228                                         eDebug("[eEPGCache] couldnt initialize nownext reader!!");
229                                         return;
230                                 }
231
232                                 res = demux->createSectionReader( this, data.m_ScheduleReader );
233                                 if ( res )
234                                 {
235                                         eDebug("[eEPGCache] couldnt initialize schedule reader!!");
236                                         return;
237                                 }
238
239                                 res = demux->createSectionReader( this, data.m_ScheduleOtherReader );
240                                 if ( res )
241                                 {
242                                         eDebug("[eEPGCache] couldnt initialize schedule other reader!!");
243                                         return;
244                                 }
245
246                                 messages.send(Message(Message::startChannel, chan));
247                                 // -> gotMessage -> changedService
248                         }
249                 }
250         }
251 }
252
253 void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
254 {
255         channelMapIterator it =
256                 m_knownChannels.find(chan);
257         if ( it != m_knownChannels.end() )
258         {
259                 int state=0;
260                 chan->getState(state);
261                 if ( it->second->prevChannelState != state )
262                 {
263                         switch (state)
264                         {
265                                 case iDVBChannel::state_ok:
266                                 {
267                                         eDebug("[eEPGCache] channel %p running", chan);
268                                         DVBChannelRunning(chan);
269                                         break;
270                                 }
271                                 case iDVBChannel::state_release:
272                                 {
273                                         eDebug("[eEPGCache] remove channel %p", chan);
274                                         messages.send(Message(Message::leaveChannel, chan));
275                                         while(!it->second->can_delete)
276                                                 usleep(1000);
277                                         delete it->second;
278                                         m_knownChannels.erase(it);
279                                         // -> gotMessage -> abortEPG
280                                         break;
281                                 }
282                                 default: // ignore all other events
283                                         return;
284                         }
285                         it->second->prevChannelState = state;
286                 }
287         }
288 }
289
290 void eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
291 {
292         eit_t *eit = (eit_t*) data;
293
294         int len=HILO(eit->section_length)-1;//+3-4;
295         int ptr=EIT_SIZE;
296         if ( ptr >= len )
297                 return;
298
299         // This fixed the EPG on the Multichoice irdeto systems
300         // the EIT packet is non-compliant.. their EIT packet stinks
301         if ( data[ptr-1] < 0x40 )
302                 --ptr;
303
304         uniqueEPGKey service( HILO(eit->service_id), HILO(eit->original_network_id), HILO(eit->transport_stream_id) );
305         eit_event_struct* eit_event = (eit_event_struct*) (data+ptr);
306         int eit_event_size;
307         int duration;
308
309         time_t TM = parseDVBtime( eit_event->start_time_1, eit_event->start_time_2,     eit_event->start_time_3, eit_event->start_time_4, eit_event->start_time_5);
310         time_t now = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
311
312         if ( TM != 3599 && TM > -1)
313                 channel->haveData |= source;
314
315         singleLock s(cache_lock);
316         // hier wird immer eine eventMap zurück gegeben.. entweder eine vorhandene..
317         // oder eine durch [] erzeugte
318         std::pair<eventMap,timeMap> &servicemap = eventDB[service];
319         eventMap::iterator prevEventIt = servicemap.first.end();
320         timeMap::iterator prevTimeIt = servicemap.second.end();
321
322         while (ptr<len)
323         {
324                 eit_event_size = HILO(eit_event->descriptors_loop_length)+EIT_LOOP_SIZE;
325
326                 duration = fromBCD(eit_event->duration_1)*3600+fromBCD(eit_event->duration_2)*60+fromBCD(eit_event->duration_3);
327                 TM = parseDVBtime(
328                         eit_event->start_time_1,
329                         eit_event->start_time_2,
330                         eit_event->start_time_3,
331                         eit_event->start_time_4,
332                         eit_event->start_time_5);
333
334                 if ( TM == 3599 )
335                         goto next;
336
337                 if ( TM != 3599 && (TM+duration < now || TM > now+14*24*60*60) )
338                         goto next;
339
340                 if ( now <= (TM+duration) || TM == 3599 /*NVOD Service*/ )  // old events should not be cached
341                 {
342                         __u16 event_id = HILO(eit_event->event_id);
343 //                      eDebug("event_id is %d sid is %04x", event_id, service.sid);
344
345                         eventData *evt = 0;
346                         int ev_erase_count = 0;
347                         int tm_erase_count = 0;
348
349                         // search in eventmap
350                         eventMap::iterator ev_it =
351                                 servicemap.first.find(event_id);
352
353                         // entry with this event_id is already exist ?
354                         if ( ev_it != servicemap.first.end() )
355                         {
356                                 if ( source > ev_it->second->type )  // update needed ?
357                                         goto next; // when not.. the skip this entry
358
359                                 // search this event in timemap
360                                 timeMap::iterator tm_it_tmp = 
361                                         servicemap.second.find(ev_it->second->getStartTime());
362
363                                 if ( tm_it_tmp != servicemap.second.end() )
364                                 {
365                                         if ( tm_it_tmp->first == TM ) // correct eventData
366                                         {
367                                                 // exempt memory
368                                                 delete ev_it->second;
369                                                 evt = new eventData(eit_event, eit_event_size, source);
370                                                 ev_it->second=evt;
371                                                 tm_it_tmp->second=evt;
372                                                 goto next;
373                                         }
374                                         else
375                                         {
376                                                 tm_erase_count++;
377                                                 // delete the found record from timemap
378                                                 servicemap.second.erase(tm_it_tmp);
379                                                 prevTimeIt=servicemap.second.end();
380                                         }
381                                 }
382                         }
383
384                         // search in timemap, for check of a case if new time has coincided with time of other event 
385                         // or event was is not found in eventmap
386                         timeMap::iterator tm_it =
387                                 servicemap.second.find(TM);
388
389                         if ( tm_it != servicemap.second.end() )
390                         {
391                                 // i think, if event is not found on eventmap, but found on timemap updating nevertheless demands
392 #if 0
393                                 if ( source > tm_it->second->type && tm_erase_count == 0 ) // update needed ?
394                                         goto next; // when not.. the skip this entry
395 #endif
396
397                                 // search this time in eventmap
398                                 eventMap::iterator ev_it_tmp = 
399                                         servicemap.first.find(tm_it->second->getEventID());
400
401                                 if ( ev_it_tmp != servicemap.first.end() )
402                                 {
403                                         ev_erase_count++;                               
404                                         // delete the found record from eventmap
405                                         servicemap.first.erase(ev_it_tmp);
406                                         prevEventIt=servicemap.first.end();
407                                 }
408                         }
409                         
410                         evt = new eventData(eit_event, eit_event_size, source);
411 #if EPG_DEBUG
412                         bool consistencyCheck=true;
413 #endif
414                         if (ev_erase_count > 0 && tm_erase_count > 0) // 2 different pairs have been removed
415                         {
416                                 // exempt memory
417                                 delete ev_it->second; 
418                                 delete tm_it->second;
419                                 ev_it->second=evt;
420                                 tm_it->second=evt;
421                         }
422                         else if (ev_erase_count == 0 && tm_erase_count > 0) 
423                         {
424                                 // exempt memory
425                                 delete ev_it->second;
426                                 tm_it=prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
427                                 ev_it->second=evt;
428                         }
429                         else if (ev_erase_count > 0 && tm_erase_count == 0)
430                         {
431                                 // exempt memory
432                                 delete tm_it->second;
433                                 ev_it=prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
434                                 tm_it->second=evt;
435                         }
436                         else // added new eventData
437                         {
438 #if EPG_DEBUG
439                                 consistencyCheck=false;
440 #endif
441                                 prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
442                                 prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
443                         }
444 #if EPG_DEBUG
445                         if ( consistencyCheck )
446                         {
447                                 if ( tm_it->second != evt || ev_it->second != evt )
448                                         eFatal("tm_it->second != ev_it->second");
449                                 else if ( tm_it->second->getStartTime() != tm_it->first )
450                                         eFatal("event start_time(%d) non equal timemap key(%d)", 
451                                                 tm_it->second->getStartTime(), tm_it->first );
452                                 else if ( tm_it->first != TM )
453                                         eFatal("timemap key(%d) non equal TM(%d)", 
454                                                 tm_it->first, TM);
455                                 else if ( ev_it->second->getEventID() != ev_it->first )
456                                         eFatal("event_id (%d) non equal event_map key(%d)",
457                                                 ev_it->second->getEventID(), ev_it->first);
458                                 else if ( ev_it->first != event_id )
459                                         eFatal("eventmap key(%d) non equal event_id(%d)", 
460                                                 ev_it->first, event_id );
461                         }
462 #endif
463                 }
464 next:
465 #if EPG_DEBUG
466                 if ( servicemap.first.size() != servicemap.second.size() )
467                 {
468                         FILE *f = fopen("/hdd/event_map.txt", "w+");
469                         int i=0;
470                         for (eventMap::iterator it(servicemap.first.begin())
471                                 ; it != servicemap.first.end(); ++it )
472                                 fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n", 
473                                         i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
474                         fclose(f);
475                         f = fopen("/hdd/time_map.txt", "w+");
476                         i=0;
477                         for (timeMap::iterator it(servicemap.second.begin())
478                                 ; it != servicemap.second.end(); ++it )
479                                         fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n", 
480                                                 i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
481                         fclose(f);
482
483                         eFatal("(1)map sizes not equal :( sid %04x tsid %04x onid %04x size %d size2 %d", 
484                                 service.sid, service.tsid, service.onid, 
485                                 servicemap.first.size(), servicemap.second.size() );
486                 }
487 #endif
488                 ptr += eit_event_size;
489                 eit_event=(eit_event_struct*)(((__u8*)eit_event)+eit_event_size);
490         }
491 }
492
493 void eEPGCache::flushEPG(const uniqueEPGKey & s)
494 {
495         eDebug("[EPGC] flushEPG %d", (int)(bool)s);
496         singleLock l(cache_lock);
497         if (s)  // clear only this service
498         {
499                 eventCache::iterator it = eventDB.find(s);
500                 if ( it != eventDB.end() )
501                 {
502                         eventMap &evMap = it->second.first;
503                         timeMap &tmMap = it->second.second;
504                         tmMap.clear();
505                         for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
506                                 delete i->second;
507                         evMap.clear();
508                         eventDB.erase(it);
509
510                         // TODO .. search corresponding channel for removed service and remove this channel from lastupdated map
511                 }
512         }
513         else // clear complete EPG Cache
514         {
515                 for (eventCache::iterator it(eventDB.begin());
516                         it != eventDB.end(); ++it)
517                 {
518                         eventMap &evMap = it->second.first;
519                         timeMap &tmMap = it->second.second;
520                         for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
521                                 delete i->second;
522                         evMap.clear();
523                         tmMap.clear();
524                 }
525                 eventDB.clear();
526                 channelLastUpdated.clear();
527                 singleLock m(channel_map_lock);
528                 for (channelMapIterator it(m_knownChannels.begin()); it != m_knownChannels.end(); ++it)
529                         it->second->startEPG();
530         }
531         eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
532 }
533
534 void eEPGCache::cleanLoop()
535 {
536         singleLock s(cache_lock);
537         if (!eventDB.empty())
538         {
539                 eDebug("[EPGC] start cleanloop");
540
541                 time_t now = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
542
543                 for (eventCache::iterator DBIt = eventDB.begin(); DBIt != eventDB.end(); DBIt++)
544                 {
545                         for (timeMap::iterator It = DBIt->second.second.begin(); It != DBIt->second.second.end() && It->first < now;)
546                         {
547                                 if ( now > (It->first+It->second->getDuration()) )  // outdated normal entry (nvod references to)
548                                 {
549                                         // remove entry from eventMap
550                                         eventMap::iterator b(DBIt->second.first.find(It->second->getEventID()));
551                                         if ( b != DBIt->second.first.end() )
552                                         {
553                                                 // release Heap Memory for this entry   (new ....)
554 //                                              eDebug("[EPGC] delete old event (evmap)");
555                                                 DBIt->second.first.erase(b);
556                                         }
557
558                                         // remove entry from timeMap
559 //                                      eDebug("[EPGC] release heap mem");
560                                         delete It->second;
561                                         DBIt->second.second.erase(It++);
562 //                                      eDebug("[EPGC] delete old event (timeMap)");
563                                 }
564                                 else
565                                         ++It;
566                         }
567                 }
568                 eDebug("[EPGC] stop cleanloop");
569                 eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
570         }
571         cleanTimer.start(CLEAN_INTERVAL,true);
572 }
573
574 eEPGCache::~eEPGCache()
575 {
576         messages.send(Message::quit);
577         kill(); // waiting for thread shutdown
578         singleLock s(cache_lock);
579         for (eventCache::iterator evIt = eventDB.begin(); evIt != eventDB.end(); evIt++)
580                 for (eventMap::iterator It = evIt->second.first.begin(); It != evIt->second.first.end(); It++)
581                         delete It->second;
582 }
583
584 void eEPGCache::gotMessage( const Message &msg )
585 {
586         switch (msg.type)
587         {
588                 case Message::flush:
589                         flushEPG(msg.service);
590                         break;
591                 case Message::startChannel:
592                 {
593                         singleLock s(channel_map_lock);
594                         channelMapIterator channel =
595                                 m_knownChannels.find(msg.channel);
596                         if ( channel != m_knownChannels.end() )
597                                 channel->second->startChannel();
598                         break;
599                 }
600                 case Message::leaveChannel:
601                 {
602                         singleLock s(channel_map_lock);
603                         channelMapIterator channel =
604                                 m_knownChannels.find(msg.channel);
605                         if ( channel != m_knownChannels.end() )
606                                 channel->second->abortEPG();
607                         break;
608                 }
609                 case Message::quit:
610                         quit(0);
611                         break;
612                 case Message::timeChanged:
613                         cleanLoop();
614                         break;
615                 default:
616                         eDebug("unhandled EPGCache Message!!");
617                         break;
618         }
619 }
620
621 void eEPGCache::thread()
622 {
623         nice(4);
624         load();
625         cleanLoop();
626         exec();
627         save();
628 }
629
630 void eEPGCache::load()
631 {
632         FILE *f = fopen("/hdd/epg.dat", "r");
633         if (f)
634         {
635                 unsigned char md5_saved[16];
636                 unsigned char md5[16];
637                 int size=0;
638                 int cnt=0;
639                 bool md5ok=false;
640 #if 0
641                 if (!md5_file("/hdd/epg.dat", 1, md5))
642                 {
643                         FILE *f = fopen("/hdd/epg.dat.md5", "r");
644                         if (f)
645                         {
646                                 fread( md5_saved, 16, 1, f);
647                                 fclose(f);
648                                 if ( !memcmp(md5_saved, md5, 16) )
649                                         md5ok=true;
650                         }
651                 }
652                 if ( md5ok )
653 #endif
654                 {
655                         char text1[13];
656                         fread( text1, 13, 1, f);
657                         if ( !strncmp( text1, "ENIGMA_EPG_V4", 13) )
658                         {
659                                 fread( &size, sizeof(int), 1, f);
660                                 while(size--)
661                                 {
662                                         uniqueEPGKey key;
663                                         eventMap evMap;
664                                         timeMap tmMap;
665                                         int size=0;
666                                         fread( &key, sizeof(uniqueEPGKey), 1, f);
667                                         fread( &size, sizeof(int), 1, f);
668                                         while(size--)
669                                         {
670                                                 __u8 len=0;
671                                                 __u8 type=0;
672                                                 eventData *event=0;
673                                                 fread( &type, sizeof(__u8), 1, f);
674                                                 fread( &len, sizeof(__u8), 1, f);
675                                                 event = new eventData(0, len, type);
676                                                 event->EITdata = new __u8[len];
677                                                 eventData::CacheSize+=len;
678                                                 fread( event->EITdata, len, 1, f);
679                                                 evMap[ event->getEventID() ]=event;
680                                                 tmMap[ event->getStartTime() ]=event;
681                                                 ++cnt;
682                                         }
683                                         eventDB[key]=std::pair<eventMap,timeMap>(evMap,tmMap);
684                                 }
685                                 eventData::load(f);
686                                 eDebug("%d events read from /hdd/epg.dat", cnt);
687                         }
688                         else
689                                 eDebug("[EPGC] don't read old epg database");
690                         fclose(f);
691                 }
692         }
693 }
694
695 void eEPGCache::save()
696 {
697         struct statfs s;
698         off64_t tmp;
699         if (statfs("/hdd", &s)<0)
700                 tmp=0;
701         else
702         {
703                 tmp=s.f_blocks;
704                 tmp*=s.f_bsize;
705         }
706
707         // prevent writes to builtin flash
708         if ( tmp < 1024*1024*50 ) // storage size < 50MB
709                 return;
710
711         // check for enough free space on storage
712         tmp=s.f_bfree;
713         tmp*=s.f_bsize;
714         if ( tmp < (eventData::CacheSize*12)/10 ) // 20% overhead
715                 return;
716
717         FILE *f = fopen("/hdd/epg.dat", "w");
718         int cnt=0;
719         if ( f )
720         {
721                 const char *text = "ENIGMA_EPG_V4";
722                 fwrite( text, 13, 1, f );
723                 int size = eventDB.size();
724                 fwrite( &size, sizeof(int), 1, f );
725                 for (eventCache::iterator service_it(eventDB.begin()); service_it != eventDB.end(); ++service_it)
726                 {
727                         timeMap &timemap = service_it->second.second;
728                         fwrite( &service_it->first, sizeof(uniqueEPGKey), 1, f);
729                         size = timemap.size();
730                         fwrite( &size, sizeof(int), 1, f);
731                         for (timeMap::iterator time_it(timemap.begin()); time_it != timemap.end(); ++time_it)
732                         {
733                                 __u8 len = time_it->second->ByteSize;
734                                 fwrite( &time_it->second->type, sizeof(__u8), 1, f );
735                                 fwrite( &len, sizeof(__u8), 1, f);
736                                 fwrite( time_it->second->EITdata, len, 1, f);
737                                 ++cnt;
738                         }
739                 }
740                 eDebug("%d events written to /hdd/epg.dat", cnt);
741                 eventData::save(f);
742                 fclose(f);
743 #if 0
744                 unsigned char md5[16];
745                 if (!md5_file("/hdd/epg.dat", 1, md5))
746                 {
747                         FILE *f = fopen("/hdd/epg.dat.md5", "w");
748                         if (f)
749                         {
750                                 fwrite( md5, 16, 1, f);
751                                 fclose(f);
752                         }
753                 }
754 #endif
755         }
756 }
757
758 eEPGCache::channel_data::channel_data(eEPGCache *ml)
759         :cache(ml)
760         ,abortTimer(ml), zapTimer(ml)
761         ,state(0), isRunning(0), haveData(0), can_delete(1)
762 {
763         CONNECT(zapTimer.timeout, eEPGCache::channel_data::startEPG);
764         CONNECT(abortTimer.timeout, eEPGCache::channel_data::abortNonAvail);
765 }
766
767 bool eEPGCache::channel_data::finishEPG()
768 {
769         if (!isRunning)  // epg ready
770         {
771                 eDebug("[EPGC] stop caching events(%d)", time(0)+eDVBLocalTimeHandler::getInstance()->difference());
772                 zapTimer.start(UPDATE_INTERVAL, 1);
773                 eDebug("[EPGC] next update in %i min", UPDATE_INTERVAL / 60000);
774                 for (int i=0; i < 3; ++i)
775                 {
776                         seenSections[i].clear();
777                         calcedSections[i].clear();
778                 }
779                 singleLock l(cache->cache_lock);
780                 cache->channelLastUpdated[channel->getChannelID()] = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
781                 can_delete=1;
782                 return true;
783         }
784         return false;
785 }
786
787 void eEPGCache::channel_data::startEPG()
788 {
789         eDebug("[EPGC] start caching events(%d)", eDVBLocalTimeHandler::getInstance()->difference()+time(0));
790         state=0;
791         haveData=0;
792         can_delete=0;
793         for (int i=0; i < 3; ++i)
794         {
795                 seenSections[i].clear();
796                 calcedSections[i].clear();
797         }
798
799         eDVBSectionFilterMask mask;
800         memset(&mask, 0, sizeof(mask));
801         mask.pid = 0x12;
802         mask.flags = eDVBSectionFilterMask::rfCRC;
803
804         mask.data[0] = 0x4E;
805         mask.mask[0] = 0xFE;
806         m_NowNextReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_NowNextConn);
807         m_NowNextReader->start(mask);
808         isRunning |= NOWNEXT;
809
810         mask.data[0] = 0x50;
811         mask.mask[0] = 0xF0;
812         m_ScheduleReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_ScheduleConn);
813         m_ScheduleReader->start(mask);
814         isRunning |= SCHEDULE;
815
816         mask.data[0] = 0x60;
817         mask.mask[0] = 0xF0;
818         m_ScheduleOtherReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_ScheduleOtherConn);
819         m_ScheduleOtherReader->start(mask);
820         isRunning |= SCHEDULE_OTHER;
821
822         abortTimer.start(7000,true);
823 }
824
825 void eEPGCache::channel_data::abortNonAvail()
826 {
827         if (!state)
828         {
829                 if ( !(haveData&eEPGCache::NOWNEXT) && (isRunning&eEPGCache::NOWNEXT) )
830                 {
831                         eDebug("[EPGC] abort non avail nownext reading");
832                         isRunning &= ~eEPGCache::NOWNEXT;
833                         m_NowNextReader->stop();
834                         m_NowNextConn=0;
835                 }
836                 if ( !(haveData&eEPGCache::SCHEDULE) && (isRunning&eEPGCache::SCHEDULE) )
837                 {
838                         eDebug("[EPGC] abort non avail schedule reading");
839                         isRunning &= ~SCHEDULE;
840                         m_ScheduleReader->stop();
841                         m_ScheduleConn=0;
842                 }
843                 if ( !(haveData&eEPGCache::SCHEDULE_OTHER) && (isRunning&eEPGCache::SCHEDULE_OTHER) )
844                 {
845                         eDebug("[EPGC] abort non avail schedule_other reading");
846                         isRunning &= ~SCHEDULE_OTHER;
847                         m_ScheduleOtherReader->stop();
848                         m_ScheduleOtherConn=0;
849                 }
850                 if ( isRunning )
851                         abortTimer.start(90000, true);
852                 else
853                 {
854                         ++state;
855                         for (int i=0; i < 3; ++i)
856                         {
857                                 seenSections[i].clear();
858                                 calcedSections[i].clear();
859                         }
860                         can_delete=1;
861                 }
862         }
863         ++state;
864 }
865
866 void eEPGCache::channel_data::startChannel()
867 {
868         updateMap::iterator It = cache->channelLastUpdated.find( channel->getChannelID() );
869
870         int update = ( It != cache->channelLastUpdated.end() ? ( UPDATE_INTERVAL - ( (time(0)+eDVBLocalTimeHandler::getInstance()->difference()-It->second) * 1000 ) ) : ZAP_DELAY );
871
872         if (update < ZAP_DELAY)
873                 update = ZAP_DELAY;
874
875         zapTimer.start(update, 1);
876         if (update >= 60000)
877                 eDebug("[EPGC] next update in %i min", update/60000);
878         else if (update >= 1000)
879                 eDebug("[EPGC] next update in %i sec", update/1000);
880 }
881
882 void eEPGCache::channel_data::abortEPG()
883 {
884         for (int i=0; i < 3; ++i)
885         {
886                 seenSections[i].clear();
887                 calcedSections[i].clear();
888         }
889         abortTimer.stop();
890         zapTimer.stop();
891         if (isRunning)
892         {
893                 eDebug("[EPGC] abort caching events !!");
894                 if (isRunning & eEPGCache::SCHEDULE)
895                 {
896                         isRunning &= ~eEPGCache::SCHEDULE;
897                         m_ScheduleReader->stop();
898                         m_ScheduleConn=0;
899                 }
900                 if (isRunning & eEPGCache::NOWNEXT)
901                 {
902                         isRunning &= ~eEPGCache::NOWNEXT;
903                         m_NowNextReader->stop();
904                         m_NowNextConn=0;
905                 }
906                 if (isRunning & SCHEDULE_OTHER)
907                 {
908                         isRunning &= ~eEPGCache::SCHEDULE_OTHER;
909                         m_ScheduleOtherReader->stop();
910                         m_ScheduleOtherConn=0;
911                 }
912                 can_delete=1;
913         }
914 }
915
916 void eEPGCache::channel_data::readData( const __u8 *data)
917 {
918         if (!data)
919                 eDebug("get Null pointer from section reader !!");
920         else
921         {
922                 int source;
923                 int map;
924                 iDVBSectionReader *reader=NULL;
925                 switch(data[0])
926                 {
927                         case 0x4E ... 0x4F:
928                                 reader=m_NowNextReader;
929                                 source=eEPGCache::NOWNEXT;
930                                 map=0;
931                                 break;
932                         case 0x50 ... 0x5F:
933                                 reader=m_ScheduleReader;
934                                 source=eEPGCache::SCHEDULE;
935                                 map=1;
936                                 break;
937                         case 0x60 ... 0x6F:
938                                 reader=m_ScheduleOtherReader;
939                                 source=eEPGCache::SCHEDULE_OTHER;
940                                 map=2;
941                                 break;
942                         default:
943                                 eDebug("[EPGC] unknown table_id !!!");
944                                 return;
945                 }
946                 tidMap &seenSections = this->seenSections[map];
947                 tidMap &calcedSections = this->calcedSections[map];
948                 if ( state == 1 && calcedSections == seenSections || state > 1 )
949                 {
950                         eDebugNoNewLine("[EPGC] ");
951                         switch (source)
952                         {
953                                 case eEPGCache::NOWNEXT:
954                                         m_NowNextConn=0;
955                                         eDebugNoNewLine("nownext");
956                                         break;
957                                 case eEPGCache::SCHEDULE:
958                                         m_ScheduleConn=0;
959                                         eDebugNoNewLine("schedule");
960                                         break;
961                                 case eEPGCache::SCHEDULE_OTHER:
962                                         m_ScheduleOtherConn=0;
963                                         eDebugNoNewLine("schedule other");
964                                         break;
965                                 default: eDebugNoNewLine("unknown");break;
966                         }
967                         eDebug(" finished(%d)", time(0)+eDVBLocalTimeHandler::getInstance()->difference());
968                         if ( reader )
969                                 reader->stop();
970                         isRunning &= ~source;
971                         if (!isRunning)
972                                 finishEPG();
973                 }
974                 else
975                 {
976                         eit_t *eit = (eit_t*) data;
977                         __u32 sectionNo = data[0] << 24;
978                         sectionNo |= data[3] << 16;
979                         sectionNo |= data[4] << 8;
980                         sectionNo |= eit->section_number;
981
982                         tidMap::iterator it =
983                                 seenSections.find(sectionNo);
984
985                         if ( it == seenSections.end() )
986                         {
987                                 seenSections.insert(sectionNo);
988                                 calcedSections.insert(sectionNo);
989                                 __u32 tmpval = sectionNo & 0xFFFFFF00;
990                                 __u8 incr = source == NOWNEXT ? 1 : 8;
991                                 for ( int i = 0; i <= eit->last_section_number; i+=incr )
992                                 {
993                                         if ( i == eit->section_number )
994                                         {
995                                                 for (int x=i; x <= eit->segment_last_section_number; ++x)
996                                                         calcedSections.insert(tmpval|(x&0xFF));
997                                         }
998                                         else
999                                                 calcedSections.insert(tmpval|(i&0xFF));
1000                                 }
1001                                 cache->sectionRead(data, source, this);
1002                         }
1003                 }
1004         }
1005 }
1006
1007 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eventData *&result )
1008 // if t == -1 we search the current event...
1009 {
1010         singleLock s(cache_lock);
1011         uniqueEPGKey key(service);
1012
1013         // check if EPG for this service is ready...
1014         eventCache::iterator It = eventDB.find( key );
1015         if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached ?
1016         {
1017                 if (t==-1)
1018                         t = time(0)+eDVBLocalTimeHandler::getInstance()->difference();
1019                 timeMap::iterator i = It->second.second.lower_bound(t);  // find > or equal
1020                 if ( i != It->second.second.end() )
1021                 {
1022                         if ( i->second->getStartTime() != t )
1023                         {
1024                                 timeMap::iterator x = i;
1025                                 --x;
1026                                 if ( x != It->second.second.end() )
1027                                 {
1028                                         time_t start_time = x->second->getStartTime();
1029                                         if (t < start_time)
1030                                                 return -1;
1031                                         if (t > (start_time+x->second->getDuration()))
1032                                                 return -1;
1033                                         i = x;
1034                                 }
1035                                 else
1036                                         return -1;
1037                         }
1038                         result = i->second;
1039                         return 0;
1040                 }
1041         }
1042         return -1;
1043 }
1044
1045 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eit_event_struct *&result )
1046 {
1047         singleLock s(cache_lock);
1048         const eventData *data=0;
1049         RESULT ret = lookupEventTime(service, t, data);
1050         if ( !ret && data )
1051                 result = data->get();
1052         return ret;
1053 }
1054
1055 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, Event *& result )
1056 {
1057         singleLock s(cache_lock);
1058         const eventData *data=0;
1059         RESULT ret = lookupEventTime(service, t, data);
1060         if ( !ret && data )
1061                 result = new Event((uint8_t*)data->get());
1062         return ret;
1063 }
1064
1065 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, ePtr<eServiceEvent> &result )
1066 {
1067         singleLock s(cache_lock);
1068         const eventData *data=0;
1069         RESULT ret = lookupEventTime(service, t, data);
1070         if ( !ret && data )
1071         {
1072                 Event ev((uint8_t*)data->get());
1073                 result = new eServiceEvent();
1074                 const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
1075                 ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
1076         }
1077         return ret;
1078 }
1079
1080 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eventData *&result )
1081 {
1082         singleLock s(cache_lock);
1083         uniqueEPGKey key( service );
1084
1085         eventCache::iterator It = eventDB.find( key );
1086         if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached?
1087         {
1088                 eventMap::iterator i( It->second.first.find( event_id ));
1089                 if ( i != It->second.first.end() )
1090                 {
1091                         result = i->second;
1092                         return 0;
1093                 }
1094                 else
1095                 {
1096                         result = 0;
1097                         eDebug("event %04x not found in epgcache", event_id);
1098                 }
1099         }
1100         return -1;
1101 }
1102
1103 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&result)
1104 {
1105         singleLock s(cache_lock);
1106         const eventData *data=0;
1107         RESULT ret = lookupEventId(service, event_id, data);
1108         if ( !ret && data )
1109                 result = data->get();
1110         return ret;
1111 }
1112
1113 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, Event *& result)
1114 {
1115         singleLock s(cache_lock);
1116         const eventData *data=0;
1117         RESULT ret = lookupEventId(service, event_id, data);
1118         if ( !ret && data )
1119                 result = new Event((uint8_t*)data->get());
1120         return ret;
1121 }
1122
1123 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &result)
1124 {
1125         singleLock s(cache_lock);
1126         const eventData *data=0;
1127         RESULT ret = lookupEventId(service, event_id, data);
1128         if ( !ret && data )
1129         {
1130                 Event ev((uint8_t*)data->get());
1131                 result = new eServiceEvent();
1132                 const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
1133                 ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
1134         }
1135         return ret;
1136 }
1137
1138 RESULT eEPGCache::startTimeQuery(const eServiceReference &service, time_t begin, int minutes)
1139 {
1140         eventCache::iterator It = eventDB.find( service );
1141         if ( It != eventDB.end() && It->second.second.size() )
1142         {
1143                 m_timemap_end = minutes != -1 ? It->second.second.upper_bound(begin+minutes*60) : It->second.second.end();
1144                 if ( begin != -1 )
1145                 {
1146                         m_timemap_cursor = It->second.second.lower_bound(begin);
1147                         if ( m_timemap_cursor != It->second.second.end() )
1148                         {
1149                                 if ( m_timemap_cursor->second->getStartTime() != begin )
1150                                 {
1151                                         timeMap::iterator x = m_timemap_cursor;
1152                                         --x;
1153                                         if ( x != It->second.second.end() )
1154                                         {
1155                                                 time_t start_time = x->second->getStartTime();
1156                                                 if ( begin > start_time && begin < (start_time+x->second->getDuration()))
1157                                                         m_timemap_cursor = x;
1158                                         }
1159                                 }
1160                         }
1161                 }
1162                 else
1163                         m_timemap_cursor = It->second.second.begin();
1164                 const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
1165                 currentQueryTsidOnid = (ref.getTransportStreamID().get()<<16) | ref.getOriginalNetworkID().get();
1166                 return 0;
1167         }
1168         return -1;
1169 }
1170
1171 RESULT eEPGCache::getNextTimeEntry(const eventData *& result)
1172 {
1173         if ( m_timemap_cursor != m_timemap_end )
1174         {
1175                 result = m_timemap_cursor++->second;
1176                 return 0;
1177         }
1178         return -1;
1179 }
1180
1181 RESULT eEPGCache::getNextTimeEntry(const eit_event_struct *&result)
1182 {
1183         if ( m_timemap_cursor != m_timemap_end )
1184         {
1185                 result = m_timemap_cursor++->second->get();
1186                 return 0;
1187         }
1188         return -1;
1189 }
1190
1191 RESULT eEPGCache::getNextTimeEntry(Event *&result)
1192 {
1193         if ( m_timemap_cursor != m_timemap_end )
1194         {
1195                 result = new Event((uint8_t*)m_timemap_cursor++->second->get());
1196                 return 0;
1197         }
1198         return -1;
1199 }
1200
1201 RESULT eEPGCache::getNextTimeEntry(ePtr<eServiceEvent> &result)
1202 {
1203         if ( m_timemap_cursor != m_timemap_end )
1204         {
1205                 Event ev((uint8_t*)m_timemap_cursor++->second->get());
1206                 result = new eServiceEvent();
1207                 return result->parseFrom(&ev, currentQueryTsidOnid);
1208         }
1209         return -1;
1210 }
1211
1212 void fillTuple(PyObject *tuple, char *argstring, int argcount, PyObject *service, ePtr<eServiceEvent> &ptr, PyObject *nowTime, PyObject *service_name )
1213 {
1214         PyObject *tmp=NULL;
1215         int pos=0;
1216         while(pos < argcount)
1217         {
1218                 bool inc_refcount=false;
1219                 switch(argstring[pos])
1220                 {
1221                         case 'I': // Event Id
1222                                 tmp = ptr ? PyLong_FromLong(ptr->getEventId()) : NULL;
1223                                 break;
1224                         case 'B': // Event Begin Time
1225                                 tmp = ptr ? PyLong_FromLong(ptr->getBeginTime()) : NULL;
1226                                 break;
1227                         case 'D': // Event Duration
1228                                 tmp = ptr ? PyLong_FromLong(ptr->getDuration()) : NULL;
1229                                 break;
1230                         case 'T': // Event Title
1231                                 tmp = ptr ? PyString_FromString(ptr->getEventName().c_str()) : NULL;
1232                                 break;
1233                         case 'S': // Event Short Description
1234                                 tmp = ptr ? PyString_FromString(ptr->getShortDescription().c_str()) : NULL;
1235                                 break;
1236                         case 'E': // Event Extended Description
1237                                 tmp = ptr ? PyString_FromString(ptr->getExtendedDescription().c_str()) : NULL;
1238                                 break;
1239                         case 'C': // Current Time
1240                                 tmp = nowTime;
1241                                 inc_refcount = true;
1242                                 break;
1243                         case 'R': // service reference string
1244                                 tmp = service;
1245                                 inc_refcount = true;
1246                                 break;
1247                         case 'N': // service name
1248                                 tmp = service_name;
1249                                 inc_refcount = true;
1250                 }
1251                 if (!tmp)
1252                 {
1253                         tmp = Py_None;
1254                         inc_refcount = true;
1255                 }
1256                 if (inc_refcount)
1257                         Py_INCREF(tmp);
1258                 PyTuple_SET_ITEM(tuple, pos++, tmp);
1259         }
1260 }
1261
1262 PyObject *handleEvent(ePtr<eServiceEvent> &ptr, PyObject *dest_list, char* argstring, int argcount, PyObject *service, PyObject *nowTime, PyObject *service_name, PyObject *convertFunc, PyObject *convertFuncArgs)
1263 {
1264         if (convertFunc)
1265         {
1266                 fillTuple(convertFuncArgs, argstring, argcount, service, ptr, nowTime, service_name);
1267                 PyObject *result = PyEval_CallObject(convertFunc, convertFuncArgs);
1268                 if (result == NULL)
1269                 {
1270                         if (nowTime)
1271                                 Py_DECREF(nowTime);
1272                         Py_DECREF(convertFuncArgs);
1273                         return result;
1274                 }
1275                 PyList_Append(dest_list, result);
1276         }
1277         else
1278         {
1279                 PyObject *tuple = PyTuple_New(argcount);
1280                 fillTuple(tuple, argstring, argcount, service, ptr, nowTime, service_name);
1281                 PyList_Append(dest_list, tuple);
1282         }
1283         return 0;
1284 }
1285
1286 // here we get a list with tuples
1287 // first tuple entry is the servicereference
1288 // the second is the type of query (0 = time, 1 = event_id)
1289 // the third
1290 //              when type is eventid it is the event_id
1291 //              when type is time then it is the start_time ( 0 for now_time )
1292 // the fourth is the end_time .. ( optional )
1293
1294 /* argv is a python string
1295    I = Event Id
1296    B = Event Begin Time
1297    D = Event Duration
1298    T = Event Title
1299    S = Event Short Description
1300    E = Event Extended Description
1301    C = Current Time
1302    R = Service Reference
1303    N = Service Name
1304 */
1305
1306 PyObject *eEPGCache::lookupEvent(PyObject *list, PyObject *convertFunc)
1307 {
1308         PyObject *convertFuncArgs=NULL;
1309         int argcount=0;
1310         char *argstring=NULL;
1311         if (!PyList_Check(list))
1312         {
1313                 PyErr_SetString(PyExc_StandardError,
1314                         "type error");
1315                 eDebug("no list");
1316                 return NULL;
1317         }
1318         int listIt=0;
1319         int listSize=PyList_Size(list);
1320         if (!listSize)
1321         {
1322                 PyErr_SetString(PyExc_StandardError,
1323                         "not params given");
1324                 eDebug("not params given");
1325                 return NULL;
1326         }
1327         else 
1328         {
1329                 PyObject *argv=PyList_GET_ITEM(list, 0); // borrowed reference!
1330                 if (PyString_Check(argv))
1331                 {
1332                         argstring = PyString_AS_STRING(argv);
1333                         ++listIt;
1334                 }
1335                 else
1336                         argstring = "I"; // just event id as default
1337                 argcount = strlen(argstring);
1338 //              eDebug("have %d args('%s')", argcount, argstring);
1339         }
1340         if (convertFunc)
1341         {
1342                 if (!PyCallable_Check(convertFunc))
1343                 {
1344                         PyErr_SetString(PyExc_StandardError,
1345                                 "convertFunc must be callable");
1346                         eDebug("convertFunc is not callable");
1347                         return NULL;
1348                 }
1349                 convertFuncArgs = PyTuple_New(argcount);
1350         }
1351
1352         PyObject *nowTime = strchr(argstring, 'C') ?
1353                 PyLong_FromLong(time(0)+eDVBLocalTimeHandler::getInstance()->difference()) :
1354                 NULL;
1355
1356         bool must_get_service_name = strchr(argstring, 'N') ? true : false;
1357
1358         // create dest list
1359         PyObject *dest_list=PyList_New(0);
1360         while(listSize > listIt)
1361         {
1362                 PyObject *item=PyList_GET_ITEM(list, listIt++); // borrowed reference!
1363                 if (PyTuple_Check(item))
1364                 {
1365                         int type=0;
1366                         long event_id=-1;
1367                         time_t stime=-1;
1368                         int minutes=0;
1369                         int tupleSize=PyTuple_Size(item);
1370                         int tupleIt=0;
1371                         PyObject *service=NULL;
1372                         while(tupleSize > tupleIt)  // parse query args
1373                         {
1374                                 PyObject *entry=PyTuple_GET_ITEM(item, tupleIt); // borrowed reference!
1375                                 switch(tupleIt++)
1376                                 {
1377                                         case 0:
1378                                         {
1379                                                 if (!PyString_Check(entry))
1380                                                 {
1381                                                         eDebug("tuple entry 0 is no a string");
1382                                                         continue;
1383                                                 }
1384                                                 service = entry;
1385                                                 break;
1386                                         }
1387                                         case 1:
1388                                                 type=PyInt_AsLong(entry);
1389                                                 if (type < 0 || type > 1)
1390                                                 {
1391                                                         eDebug("unknown type %d", type);
1392                                                         continue;
1393                                                 }
1394                                                 break;
1395                                         case 2:
1396                                                 event_id=stime=PyInt_AsLong(entry);
1397                                                 break;
1398                                         case 3:
1399                                                 minutes=PyInt_AsLong(entry);
1400                                                 break;
1401                                         default:
1402                                                 eDebug("unneeded extra argument");
1403                                                 break;
1404                                 }
1405                         }
1406                         eServiceReference ref(PyString_AS_STRING(service));
1407                         if (ref.type != eServiceReference::idDVB)
1408                         {
1409                                 eDebug("service reference for epg query is not valid");
1410                                 continue;
1411                         }
1412                         PyObject *service_name=NULL;
1413                         if (must_get_service_name)
1414                         {
1415                                 ePtr<iStaticServiceInformation> sptr;
1416                                 eServiceCenterPtr service_center;
1417                                 eServiceCenter::getPrivInstance(service_center);
1418                                 if (service_center)
1419                                 {
1420                                         service_center->info(ref, sptr);
1421                                         if (sptr)
1422                                         {
1423                                                 std::string name;
1424                                                 sptr->getName(ref, name);
1425                                                 if (name.length())
1426                                                         service_name = PyString_FromString(name.c_str());
1427                                         }
1428                                 }
1429                                 if (!service_name)
1430                                         service_name = PyString_FromString("<n/a>");
1431                         }
1432                         if (minutes)
1433                         {
1434                                 Lock();
1435                                 if (!startTimeQuery(ref, stime, minutes))
1436                                 {
1437                                         ePtr<eServiceEvent> ptr;
1438                                         while (!getNextTimeEntry(ptr))
1439                                         {
1440                                                 PyObject *ret = handleEvent(ptr, dest_list, argstring, argcount, service, nowTime, service_name, convertFunc, convertFuncArgs);
1441                                                 if (ret)
1442                                                         return ret;
1443                                         }
1444                                 }
1445                                 Unlock();
1446                         }
1447                         else
1448                         {
1449                                 ePtr<eServiceEvent> ptr;
1450                                 if (stime)
1451                                 {
1452                                         if (type == 0)
1453                                                 lookupEventTime(ref, stime, ptr);
1454                                         else // type == 1
1455                                                 lookupEventId(ref, event_id, ptr);
1456                                 }
1457                                 PyObject *ret = handleEvent(ptr, dest_list, argstring, argcount, service, nowTime, service_name, convertFunc, convertFuncArgs);
1458                                 if (ret)
1459                                         return ret;
1460                         }
1461                         if (service_name)
1462                                 Py_DECREF(service_name);
1463                 }
1464         }
1465         if (convertFuncArgs)
1466                 Py_DECREF(convertFuncArgs);
1467         if (nowTime)
1468                 Py_DECREF(nowTime);
1469         return dest_list;
1470 }
1471
1472
1473
1474