8089afa146fcef3222a67b7d7a6a2f2a78229701
[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 #ifdef EPG_DEBUG
7 #include <lib/service/event.h>
8 #endif
9
10 #include <time.h>
11 #include <unistd.h>  // for usleep
12 #include <sys/vfs.h> // for statfs
13 // #include <libmd5sum.h>
14 #include <lib/base/eerror.h>
15 #include <lib/dvb/pmt.h>
16 #include <lib/dvb/db.h>
17 #include <lib/python/python.h>
18
19 int eventData::CacheSize=0;
20 descriptorMap eventData::descriptors;
21 __u8 eventData::data[4108];
22 extern const uint32_t crc32_table[256];
23
24 const eServiceReference &handleGroup(const eServiceReference &ref)
25 {
26         if (ref.flags & eServiceReference::isGroup)
27         {
28                 ePtr<eDVBResourceManager> res;
29                 if (!eDVBResourceManager::getInstance(res))
30                 {
31                         ePtr<iDVBChannelList> db;
32                         if (!res->getChannelList(db))
33                         {
34                                 eBouquet *bouquet=0;
35                                 if (!db->getBouquet(ref, bouquet))
36                                 {
37                                         std::list<eServiceReference>::iterator it(bouquet->m_services.begin());
38                                         if (it != bouquet->m_services.end())
39                                                 return *it;
40                                 }
41                         }
42                 }
43         }
44         return ref;
45 }
46
47
48 eventData::eventData(const eit_event_struct* e, int size, int type)
49         :ByteSize(size&0xFF), type(type&0xFF)
50 {
51         if (!e)
52                 return;
53
54         __u32 descr[65];
55         __u32 *pdescr=descr;
56
57         __u8 *data = (__u8*)e;
58         int ptr=10;
59         int descriptors_length = (data[ptr++]&0x0F) << 8;
60         descriptors_length |= data[ptr++];
61         while ( descriptors_length )
62         {
63                 __u8 *descr = data+ptr;
64                 int descr_len = descr[1]+2;
65
66                 __u32 crc = 0;
67                 int cnt=0;
68                 while(cnt++ < descr_len)
69                         crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ data[ptr++]) & 0xFF];
70
71                 descriptorMap::iterator it =
72                         descriptors.find(crc);
73                 if ( it == descriptors.end() )
74                 {
75                         CacheSize+=descr_len;
76                         __u8 *d = new __u8[descr_len];
77                         memcpy(d, descr, descr_len);
78                         descriptors[crc] = descriptorPair(1, d);
79                 }
80                 else
81                         ++it->second.first;
82
83                 *pdescr++=crc;
84                 descriptors_length -= descr_len;
85         }
86         ASSERT(pdescr <= &descr[65]);
87         ByteSize = 12+((pdescr-descr)*4);
88         EITdata = new __u8[ByteSize];
89         CacheSize+=ByteSize;
90         memcpy(EITdata, (__u8*) e, 12);
91         memcpy(EITdata+12, descr, ByteSize-12);
92 }
93
94 const eit_event_struct* eventData::get() const
95 {
96         int pos = 10;
97         int tmp = ByteSize-12;
98         memcpy(data, EITdata, 12);
99         int descriptors_length = (data[pos++]&0x0F) << 8;
100         descriptors_length |= data[pos++];
101         __u32 *p = (__u32*)(EITdata+12);
102         while(tmp>0)
103         {
104                 descriptorMap::iterator it =
105                         descriptors.find(*p++);
106                 if ( it != descriptors.end() )
107                 {
108                         int b = it->second.second[1]+2;
109                         memcpy(data+pos, it->second.second, b );
110                         pos += b;
111                 }
112                 else
113                         eFatal("LINE %d descriptor not found in descriptor cache %08x!!!!!!", __LINE__, *(p-1));
114                 tmp-=4;
115         }
116         ASSERT(pos <= 4108);
117         ASSERT((pos-12) == descriptors_length);
118         return (const eit_event_struct*)data;
119 }
120
121 eventData::~eventData()
122 {
123         if ( ByteSize )
124         {
125                 CacheSize-=ByteSize;
126                 int descriptors_length = (EITdata[10]&0x0F) << 8;
127                 descriptors_length |= EITdata[11];
128                 __u32 *d = (__u32*)(EITdata+12);
129                 while(descriptors_length)
130                 {
131                         descriptorMap::iterator it =
132                                 descriptors.find(*d++);
133                         if ( it != descriptors.end() )
134                         {
135                                 descriptorPair &p = it->second;
136                                 descriptors_length -= (it->second.second[1]+2);
137                                 if (!--p.first) // no more used descriptor
138                                 {
139                                         CacheSize -= it->second.second[1];
140                                         delete [] it->second.second;    // free descriptor memory
141                                         descriptors.erase(it);  // remove entry from descriptor map
142                                 }
143                         }
144                         else
145                                 eFatal("LINE %d descriptor not found in descriptor cache %08x!!!!!!", __LINE__, *(d-1));
146                 }
147                 delete [] EITdata;
148                 ASSERT(!descriptors_length);
149         }
150 }
151
152 void eventData::load(FILE *f)
153 {
154         int size=0;
155         int id=0;
156         __u8 header[2];
157         descriptorPair p;
158         fread(&size, sizeof(int), 1, f);
159         while(size)
160         {
161                 fread(&id, sizeof(__u32), 1, f);
162                 fread(&p.first, sizeof(int), 1, f);
163                 fread(header, 2, 1, f);
164                 int bytes = header[1]+2;
165                 p.second = new __u8[bytes];
166                 p.second[0] = header[0];
167                 p.second[1] = header[1];
168                 fread(p.second+2, bytes-2, 1, f);
169                 descriptors[id]=p;
170                 --size;
171                 CacheSize+=bytes;
172         }
173 }
174
175 void eventData::save(FILE *f)
176 {
177         int size=descriptors.size();
178         descriptorMap::iterator it(descriptors.begin());
179         fwrite(&size, sizeof(int), 1, f);
180         while(size)
181         {
182                 fwrite(&it->first, sizeof(__u32), 1, f);
183                 fwrite(&it->second.first, sizeof(int), 1, f);
184                 fwrite(it->second.second, it->second.second[1]+2, 1, f);
185                 ++it;
186                 --size;
187         }
188 }
189
190 eEPGCache* eEPGCache::instance;
191 pthread_mutex_t eEPGCache::cache_lock=
192         PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
193 pthread_mutex_t eEPGCache::channel_map_lock=
194         PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
195
196 DEFINE_REF(eEPGCache)
197
198 eEPGCache::eEPGCache()
199         :messages(this,1), cleanTimer(this)//, paused(0)
200 {
201         eDebug("[EPGC] Initialized EPGCache");
202
203         CONNECT(messages.recv_msg, eEPGCache::gotMessage);
204         CONNECT(eDVBLocalTimeHandler::getInstance()->m_timeUpdated, eEPGCache::timeUpdated);
205         CONNECT(cleanTimer.timeout, eEPGCache::cleanLoop);
206
207         ePtr<eDVBResourceManager> res_mgr;
208         eDVBResourceManager::getInstance(res_mgr);
209         if (!res_mgr)
210                 eDebug("[eEPGCache] no resource manager !!!!!!!");
211         else
212                 res_mgr->connectChannelAdded(slot(*this,&eEPGCache::DVBChannelAdded), m_chanAddedConn);
213         instance=this;
214 }
215
216 void eEPGCache::timeUpdated()
217 {
218         if (!sync())
219         {
220                 eDebug("[EPGC] time updated.. start EPG Mainloop");
221                 run();
222         } else
223                 messages.send(Message(Message::timeChanged));
224 }
225
226 void eEPGCache::DVBChannelAdded(eDVBChannel *chan)
227 {
228         if ( chan )
229         {
230 //              eDebug("[eEPGCache] add channel %p", chan);
231                 channel_data *data = new channel_data(this);
232                 data->channel = chan;
233                 data->prevChannelState = -1;
234 #ifdef ENABLE_PRIVATE_EPG
235                 data->m_PrivatePid = -1;
236 #endif
237                 singleLock s(channel_map_lock);
238                 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data* >(chan, data) );
239                 chan->connectStateChange(slot(*this, &eEPGCache::DVBChannelStateChanged), data->m_stateChangedConn);
240         }
241 }
242
243 void eEPGCache::DVBChannelRunning(iDVBChannel *chan)
244 {
245         channelMapIterator it =
246                 m_knownChannels.find(chan);
247         if ( it == m_knownChannels.end() )
248                 eDebug("[eEPGCache] will start non existing channel %p !!!", chan);
249         else
250         {
251                 channel_data &data = *it->second;
252                 ePtr<eDVBResourceManager> res_mgr;
253                 if ( eDVBResourceManager::getInstance( res_mgr ) )
254                         eDebug("[eEPGCache] no res manager!!");
255                 else
256                 {
257                         ePtr<iDVBDemux> demux;
258                         if ( data.channel->getDemux(demux, 0) )
259                         {
260                                 eDebug("[eEPGCache] no demux!!");
261                                 return;
262                         }
263                         else
264                         {
265                                 RESULT res = demux->createSectionReader( this, data.m_NowNextReader );
266                                 if ( res )
267                                 {
268                                         eDebug("[eEPGCache] couldnt initialize nownext reader!!");
269                                         return;
270                                 }
271
272                                 res = demux->createSectionReader( this, data.m_ScheduleReader );
273                                 if ( res )
274                                 {
275                                         eDebug("[eEPGCache] couldnt initialize schedule reader!!");
276                                         return;
277                                 }
278
279                                 res = demux->createSectionReader( this, data.m_ScheduleOtherReader );
280                                 if ( res )
281                                 {
282                                         eDebug("[eEPGCache] couldnt initialize schedule other reader!!");
283                                         return;
284                                 }
285 #ifdef ENABLE_PRIVATE_EPG
286                                 res = demux->createSectionReader( this, data.m_PrivateReader );
287                                 if ( res )
288                                 {
289                                         eDebug("[eEPGCache] couldnt initialize private reader!!");
290                                         return;
291                                 }
292 #endif
293 #ifdef ENABLE_MHW_EPG
294                                 res = demux->createSectionReader( this, data.m_MHWReader );
295                                 if ( res )
296                                 {
297                                         eDebug("[eEPGCache] couldnt initialize mhw reader!!");
298                                         return;
299                                 }
300                                 res = demux->createSectionReader( this, data.m_MHWReader2 );
301                                 if ( res )
302                                 {
303                                         eDebug("[eEPGCache] couldnt initialize mhw reader!!");
304                                         return;
305                                 }
306 #endif
307                                 messages.send(Message(Message::startChannel, chan));
308                                 // -> gotMessage -> changedService
309                         }
310                 }
311         }
312 }
313
314 void eEPGCache::DVBChannelStateChanged(iDVBChannel *chan)
315 {
316         channelMapIterator it =
317                 m_knownChannels.find(chan);
318         if ( it != m_knownChannels.end() )
319         {
320                 int state=0;
321                 chan->getState(state);
322                 if ( it->second->prevChannelState != state )
323                 {
324                         switch (state)
325                         {
326                                 case iDVBChannel::state_ok:
327                                 {
328                                         eDebug("[eEPGCache] channel %p running", chan);
329                                         DVBChannelRunning(chan);
330                                         break;
331                                 }
332                                 case iDVBChannel::state_release:
333                                 {
334                                         eDebug("[eEPGCache] remove channel %p", chan);
335                                         messages.send(Message(Message::leaveChannel, chan));
336                                         pthread_mutex_lock(&it->second->channel_active);
337                                         singleLock s(channel_map_lock);
338                                         m_knownChannels.erase(it);
339                                         pthread_mutex_unlock(&it->second->channel_active);
340                                         delete it->second;
341                                         it->second=0;
342                                         // -> gotMessage -> abortEPG
343                                         break;
344                                 }
345                                 default: // ignore all other events
346                                         return;
347                         }
348                         if (it->second)
349                                 it->second->prevChannelState = state;
350                 }
351         }
352 }
353
354 void eEPGCache::FixOverlapping(std::pair<eventMap,timeMap> &servicemap, time_t TM, int duration, const timeMap::iterator &tm_it, const uniqueEPGKey &service)
355 {
356         timeMap::iterator tmp = tm_it;
357         while ((tmp->first+tmp->second->getDuration()-300) > TM)
358         {
359                 if(tmp->first != TM 
360 #ifdef ENABLE_PRIVATE_EPG
361                         && tmp->second->type != PRIVATE 
362 #endif
363 #ifdef ENABLE_MHW
364                         && tmp->second->type != MHW
365 #endif
366                         )
367                 {
368                         __u16 event_id = tmp->second->getEventID();
369                         servicemap.first.erase(event_id);
370 #ifdef EPG_DEBUG
371                         Event evt((uint8_t*)tmp->second->get());
372                         eServiceEvent event;
373                         event.parseFrom(&evt, service.sid<<16|service.onid);
374                         eDebug("(1)erase no more used event %04x %d\n%s %s\n%s",
375                                 service.sid, event_id,
376                                 event.getBeginTimeString().c_str(),
377                                 event.getEventName().c_str(),
378                                 event.getExtendedDescription().c_str());
379 #endif
380                         delete tmp->second;
381                         if (tmp == servicemap.second.begin())
382                         {
383                                 servicemap.second.erase(tmp);
384                                 break;
385                         }
386                         else
387                                 servicemap.second.erase(tmp--);
388                 }
389                 else
390                 {
391                         if (tmp == servicemap.second.begin())
392                                 break;
393                         --tmp;
394                 }
395         }
396
397         tmp = tm_it;
398         while(tmp->first < (TM+duration-300))
399         {
400                 if (tmp->first != TM && tmp->second->type != PRIVATE)
401                 {
402                         __u16 event_id = tmp->second->getEventID();
403                         servicemap.first.erase(event_id);
404 #ifdef EPG_DEBUG  
405                         Event evt((uint8_t*)tmp->second->get());
406                         eServiceEvent event;
407                         event.parseFrom(&evt, service.sid<<16|service.onid);
408                         eDebug("(2)erase no more used event %04x %d\n%s %s\n%s",
409                                 service.sid, event_id,
410                                 event.getBeginTimeString().c_str(),
411                                 event.getEventName().c_str(),
412                                 event.getExtendedDescription().c_str());
413 #endif
414                         delete tmp->second;
415                         servicemap.second.erase(tmp++);
416                 }
417                 else
418                         ++tmp;
419                 if (tmp == servicemap.second.end())
420                         break;
421         }
422 }
423
424 void eEPGCache::sectionRead(const __u8 *data, int source, channel_data *channel)
425 {
426         eit_t *eit = (eit_t*) data;
427
428         int len=HILO(eit->section_length)-1;//+3-4;
429         int ptr=EIT_SIZE;
430         if ( ptr >= len )
431                 return;
432
433         // This fixed the EPG on the Multichoice irdeto systems
434         // the EIT packet is non-compliant.. their EIT packet stinks
435         if ( data[ptr-1] < 0x40 )
436                 --ptr;
437
438         uniqueEPGKey service( HILO(eit->service_id), HILO(eit->original_network_id), HILO(eit->transport_stream_id) );
439         eit_event_struct* eit_event = (eit_event_struct*) (data+ptr);
440         int eit_event_size;
441         int duration;
442
443         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);
444         time_t now = eDVBLocalTimeHandler::getInstance()->nowTime();
445
446         if ( TM != 3599 && TM > -1)
447                 channel->haveData |= source;
448
449         singleLock s(cache_lock);
450         // hier wird immer eine eventMap zurück gegeben.. entweder eine vorhandene..
451         // oder eine durch [] erzeugte
452         std::pair<eventMap,timeMap> &servicemap = eventDB[service];
453         eventMap::iterator prevEventIt = servicemap.first.end();
454         timeMap::iterator prevTimeIt = servicemap.second.end();
455
456         while (ptr<len)
457         {
458                 eit_event_size = HILO(eit_event->descriptors_loop_length)+EIT_LOOP_SIZE;
459
460                 duration = fromBCD(eit_event->duration_1)*3600+fromBCD(eit_event->duration_2)*60+fromBCD(eit_event->duration_3);
461                 TM = parseDVBtime(
462                         eit_event->start_time_1,
463                         eit_event->start_time_2,
464                         eit_event->start_time_3,
465                         eit_event->start_time_4,
466                         eit_event->start_time_5);
467
468                 if ( TM == 3599 )
469                         goto next;
470
471                 if ( TM != 3599 && (TM+duration < now || TM > now+14*24*60*60) )
472                         goto next;
473
474                 if ( now <= (TM+duration) || TM == 3599 /*NVOD Service*/ )  // old events should not be cached
475                 {
476                         __u16 event_id = HILO(eit_event->event_id);
477 //                      eDebug("event_id is %d sid is %04x", event_id, service.sid);
478
479                         eventData *evt = 0;
480                         int ev_erase_count = 0;
481                         int tm_erase_count = 0;
482
483                         // search in eventmap
484                         eventMap::iterator ev_it =
485                                 servicemap.first.find(event_id);
486
487                         // entry with this event_id is already exist ?
488                         if ( ev_it != servicemap.first.end() )
489                         {
490                                 if ( source > ev_it->second->type )  // update needed ?
491                                         goto next; // when not.. then skip this entry
492
493                                 // search this event in timemap
494                                 timeMap::iterator tm_it_tmp =
495                                         servicemap.second.find(ev_it->second->getStartTime());
496
497                                 if ( tm_it_tmp != servicemap.second.end() )
498                                 {
499                                         if ( tm_it_tmp->first == TM ) // just update eventdata
500                                         {
501                                                 // exempt memory
502                                                 delete ev_it->second;
503                                                 ev_it->second = tm_it_tmp->second =
504                                                         new eventData(eit_event, eit_event_size, source);
505                                                 FixOverlapping(servicemap, TM, duration, tm_it_tmp, service);
506                                                 goto next;
507                                         }
508                                         else  // event has new event begin time
509                                         {
510                                                 tm_erase_count++;
511                                                 // delete the found record from timemap
512                                                 servicemap.second.erase(tm_it_tmp);
513                                                 prevTimeIt=servicemap.second.end();
514                                         }
515                                 }
516                         }
517
518                         // search in timemap, for check of a case if new time has coincided with time of other event
519                         // or event was is not found in eventmap
520                         timeMap::iterator tm_it =
521                                 servicemap.second.find(TM);
522
523                         if ( tm_it != servicemap.second.end() )
524                         {
525                                 // event with same start time but another event_id...
526                                 if ( source > tm_it->second->type &&
527                                         ev_it == servicemap.first.end() )
528                                         goto next; // when not.. then skip this entry
529
530                                 // search this time in eventmap
531                                 eventMap::iterator ev_it_tmp =
532                                         servicemap.first.find(tm_it->second->getEventID());
533
534                                 if ( ev_it_tmp != servicemap.first.end() )
535                                 {
536                                         ev_erase_count++;
537                                         // delete the found record from eventmap
538                                         servicemap.first.erase(ev_it_tmp);
539                                         prevEventIt=servicemap.first.end();
540                                 }
541                         }
542
543                         evt = new eventData(eit_event, eit_event_size, source);
544 #ifdef EPG_DEBUG
545                         bool consistencyCheck=true;
546 #endif
547                         if (ev_erase_count > 0 && tm_erase_count > 0) // 2 different pairs have been removed
548                         {
549                                 // exempt memory
550                                 delete ev_it->second;
551                                 delete tm_it->second;
552                                 ev_it->second=evt;
553                                 tm_it->second=evt;
554                         }
555                         else if (ev_erase_count == 0 && tm_erase_count > 0)
556                         {
557                                 // exempt memory
558                                 delete ev_it->second;
559                                 tm_it=prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
560                                 ev_it->second=evt;
561                         }
562                         else if (ev_erase_count > 0 && tm_erase_count == 0)
563                         {
564                                 // exempt memory
565                                 delete tm_it->second;
566                                 ev_it=prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
567                                 tm_it->second=evt;
568                         }
569                         else // added new eventData
570                         {
571 #ifdef EPG_DEBUG
572                                 consistencyCheck=false;
573 #endif
574                                 ev_it=prevEventIt=servicemap.first.insert( prevEventIt, std::pair<const __u16, eventData*>( event_id, evt) );
575                                 tm_it=prevTimeIt=servicemap.second.insert( prevTimeIt, std::pair<const time_t, eventData*>( TM, evt ) );
576                         }
577
578                         FixOverlapping(servicemap, TM, duration, tm_it, service);
579
580 #ifdef EPG_DEBUG
581                         if ( consistencyCheck )
582                         {
583                                 if ( tm_it->second != evt || ev_it->second != evt )
584                                         eFatal("tm_it->second != ev_it->second");
585                                 else if ( tm_it->second->getStartTime() != tm_it->first )
586                                         eFatal("event start_time(%d) non equal timemap key(%d)",
587                                                 tm_it->second->getStartTime(), tm_it->first );
588                                 else if ( tm_it->first != TM )
589                                         eFatal("timemap key(%d) non equal TM(%d)",
590                                                 tm_it->first, TM);
591                                 else if ( ev_it->second->getEventID() != ev_it->first )
592                                         eFatal("event_id (%d) non equal event_map key(%d)",
593                                                 ev_it->second->getEventID(), ev_it->first);
594                                 else if ( ev_it->first != event_id )
595                                         eFatal("eventmap key(%d) non equal event_id(%d)",
596                                                 ev_it->first, event_id );
597                         }
598 #endif
599                 }
600 next:
601 #ifdef EPG_DEBUG
602                 if ( servicemap.first.size() != servicemap.second.size() )
603                 {
604                         FILE *f = fopen("/hdd/event_map.txt", "w+");
605                         int i=0;
606                         for (eventMap::iterator it(servicemap.first.begin())
607                                 ; it != servicemap.first.end(); ++it )
608                                 fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n", 
609                                         i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
610                         fclose(f);
611                         f = fopen("/hdd/time_map.txt", "w+");
612                         i=0;
613                         for (timeMap::iterator it(servicemap.second.begin())
614                                 ; it != servicemap.second.end(); ++it )
615                                         fprintf(f, "%d(key %d) -> time %d, event_id %d, data %p\n", 
616                                                 i++, (int)it->first, (int)it->second->getStartTime(), (int)it->second->getEventID(), it->second );
617                         fclose(f);
618
619                         eFatal("(1)map sizes not equal :( sid %04x tsid %04x onid %04x size %d size2 %d", 
620                                 service.sid, service.tsid, service.onid, 
621                                 servicemap.first.size(), servicemap.second.size() );
622                 }
623 #endif
624                 ptr += eit_event_size;
625                 eit_event=(eit_event_struct*)(((__u8*)eit_event)+eit_event_size);
626         }
627 }
628
629 void eEPGCache::flushEPG(const uniqueEPGKey & s)
630 {
631         eDebug("[EPGC] flushEPG %d", (int)(bool)s);
632         singleLock l(cache_lock);
633         if (s)  // clear only this service
634         {
635                 eventCache::iterator it = eventDB.find(s);
636                 if ( it != eventDB.end() )
637                 {
638                         eventMap &evMap = it->second.first;
639                         timeMap &tmMap = it->second.second;
640                         tmMap.clear();
641                         for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
642                                 delete i->second;
643                         evMap.clear();
644                         eventDB.erase(it);
645
646                         // TODO .. search corresponding channel for removed service and remove this channel from lastupdated map
647 #ifdef ENABLE_PRIVATE_EPG
648                         contentMaps::iterator it =
649                                 content_time_tables.find(s);
650                         if ( it != content_time_tables.end() )
651                         {
652                                 it->second.clear();
653                                 content_time_tables.erase(it);
654                         }
655 #endif
656                 }
657         }
658         else // clear complete EPG Cache
659         {
660                 for (eventCache::iterator it(eventDB.begin());
661                         it != eventDB.end(); ++it)
662                 {
663                         eventMap &evMap = it->second.first;
664                         timeMap &tmMap = it->second.second;
665                         for (eventMap::iterator i = evMap.begin(); i != evMap.end(); ++i)
666                                 delete i->second;
667                         evMap.clear();
668                         tmMap.clear();
669                 }
670                 eventDB.clear();
671 #ifdef ENABLE_PRIVATE_EPG
672                 content_time_tables.clear();
673 #endif
674                 channelLastUpdated.clear();
675                 singleLock m(channel_map_lock);
676                 for (channelMapIterator it(m_knownChannels.begin()); it != m_knownChannels.end(); ++it)
677                         it->second->startEPG();
678         }
679         eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
680 }
681
682 void eEPGCache::cleanLoop()
683 {
684         singleLock s(cache_lock);
685         if (!eventDB.empty())
686         {
687                 eDebug("[EPGC] start cleanloop");
688
689                 time_t now = eDVBLocalTimeHandler::getInstance()->nowTime();
690
691                 for (eventCache::iterator DBIt = eventDB.begin(); DBIt != eventDB.end(); DBIt++)
692                 {
693                         bool updated = false;
694                         for (timeMap::iterator It = DBIt->second.second.begin(); It != DBIt->second.second.end() && It->first < now;)
695                         {
696                                 if ( now > (It->first+It->second->getDuration()) )  // outdated normal entry (nvod references to)
697                                 {
698                                         // remove entry from eventMap
699                                         eventMap::iterator b(DBIt->second.first.find(It->second->getEventID()));
700                                         if ( b != DBIt->second.first.end() )
701                                         {
702                                                 // release Heap Memory for this entry   (new ....)
703 //                                              eDebug("[EPGC] delete old event (evmap)");
704                                                 DBIt->second.first.erase(b);
705                                         }
706
707                                         // remove entry from timeMap
708 //                                      eDebug("[EPGC] release heap mem");
709                                         delete It->second;
710                                         DBIt->second.second.erase(It++);
711 //                                      eDebug("[EPGC] delete old event (timeMap)");
712                                         updated = true;
713                                 }
714                                 else
715                                         ++It;
716                         }
717 #ifdef ENABLE_PRIVATE_EPG
718                         if ( updated )
719                         {
720                                 contentMaps::iterator x =
721                                         content_time_tables.find( DBIt->first );
722                                 if ( x != content_time_tables.end() )
723                                 {
724                                         timeMap &tmMap = DBIt->second.second;
725                                         for ( contentMap::iterator i = x->second.begin(); i != x->second.end(); )
726                                         {
727                                                 for ( contentTimeMap::iterator it(i->second.begin());
728                                                         it != i->second.end(); )
729                                                 {
730                                                         if ( tmMap.find(it->second.first) == tmMap.end() )
731                                                                 i->second.erase(it++);
732                                                         else
733                                                                 ++it;
734                                                 }
735                                                 if ( i->second.size() )
736                                                         ++i;
737                                                 else
738                                                         x->second.erase(i++);
739                                         }
740                                 }
741                         }
742 #endif
743                 }
744                 eDebug("[EPGC] stop cleanloop");
745                 eDebug("[EPGC] %i bytes for cache used", eventData::CacheSize);
746         }
747         cleanTimer.start(CLEAN_INTERVAL,true);
748 }
749
750 eEPGCache::~eEPGCache()
751 {
752         messages.send(Message::quit);
753         kill(); // waiting for thread shutdown
754         singleLock s(cache_lock);
755         for (eventCache::iterator evIt = eventDB.begin(); evIt != eventDB.end(); evIt++)
756                 for (eventMap::iterator It = evIt->second.first.begin(); It != evIt->second.first.end(); It++)
757                         delete It->second;
758 }
759
760 void eEPGCache::gotMessage( const Message &msg )
761 {
762         switch (msg.type)
763         {
764                 case Message::flush:
765                         flushEPG(msg.service);
766                         break;
767                 case Message::startChannel:
768                 {
769                         singleLock s(channel_map_lock);
770                         channelMapIterator channel =
771                                 m_knownChannels.find(msg.channel);
772                         if ( channel != m_knownChannels.end() )
773                                 channel->second->startChannel();
774                         break;
775                 }
776                 case Message::leaveChannel:
777                 {
778                         singleLock s(channel_map_lock);
779                         channelMapIterator channel =
780                                 m_knownChannels.find(msg.channel);
781                         if ( channel != m_knownChannels.end() )
782                                 channel->second->abortEPG();
783                         break;
784                 }
785                 case Message::quit:
786                         quit(0);
787                         break;
788 #ifdef ENABLE_PRIVATE_EPG
789                 case Message::got_private_pid:
790                 {
791                         singleLock s(channel_map_lock);
792                         for (channelMapIterator it(m_knownChannels.begin()); it != m_knownChannels.end(); ++it)
793                         {
794                                 eDVBChannel *channel = (eDVBChannel*) it->first;
795                                 channel_data *data = it->second;
796                                 eDVBChannelID chid = channel->getChannelID();
797                                 if ( chid.transport_stream_id.get() == msg.service.tsid &&
798                                         chid.original_network_id.get() == msg.service.onid &&
799                                         data->m_PrivatePid == -1 )
800                                 {
801                                         data->m_PrevVersion = -1;
802                                         data->m_PrivatePid = msg.pid;
803                                         data->m_PrivateService = msg.service;
804                                         int onid = chid.original_network_id.get();
805                                         onid |= 0x80000000;  // we use highest bit as private epg indicator
806                                         chid.original_network_id = onid;
807                                         updateMap::iterator It = channelLastUpdated.find( chid );
808                                         int update = ( It != channelLastUpdated.end() ? ( UPDATE_INTERVAL - ( (eDVBLocalTimeHandler::getInstance()->nowTime()-It->second) * 1000 ) ) : ZAP_DELAY );
809                                         if (update < ZAP_DELAY)
810                                                 update = ZAP_DELAY;
811                                         data->startPrivateTimer.start(update, 1);
812                                         if (update >= 60000)
813                                                 eDebug("[EPGC] next private update in %i min", update/60000);
814                                         else if (update >= 1000)
815                                                 eDebug("[EPGC] next private update in %i sec", update/1000);
816                                         break;
817                                 }
818                         }
819                         break;
820                 }
821 #endif
822                 case Message::timeChanged:
823                         cleanLoop();
824                         break;
825                 default:
826                         eDebug("unhandled EPGCache Message!!");
827                         break;
828         }
829 }
830
831 void eEPGCache::thread()
832 {
833         hasStarted();
834         nice(4);
835         load();
836         cleanLoop();
837         runLoop();
838         save();
839 }
840
841 void eEPGCache::load()
842 {
843         singleLock s(cache_lock);
844         FILE *f = fopen("/hdd/epg.dat", "r");
845         if (f)
846         {
847                 int size=0;
848                 int cnt=0;
849 #if 0
850                 unsigned char md5_saved[16];
851                 unsigned char md5[16];
852                 bool md5ok=false;
853
854                 if (!md5_file("/hdd/epg.dat", 1, md5))
855                 {
856                         FILE *f = fopen("/hdd/epg.dat.md5", "r");
857                         if (f)
858                         {
859                                 fread( md5_saved, 16, 1, f);
860                                 fclose(f);
861                                 if ( !memcmp(md5_saved, md5, 16) )
862                                         md5ok=true;
863                         }
864                 }
865                 if ( md5ok )
866 #endif
867                 {
868                         unsigned int magic=0;
869                         fread( &magic, sizeof(int), 1, f);
870                         if (magic != 0x98765432)
871                         {
872                                 eDebug("[EPGC] epg file has incorrect byte order.. dont read it");
873                                 fclose(f);
874                                 return;
875                         }
876                         char text1[13];
877                         fread( text1, 13, 1, f);
878                         if ( !strncmp( text1, "ENIGMA_EPG_V6", 13) )
879                         {
880                                 fread( &size, sizeof(int), 1, f);
881                                 while(size--)
882                                 {
883                                         uniqueEPGKey key;
884                                         eventMap evMap;
885                                         timeMap tmMap;
886                                         int size=0;
887                                         fread( &key, sizeof(uniqueEPGKey), 1, f);
888                                         fread( &size, sizeof(int), 1, f);
889                                         while(size--)
890                                         {
891                                                 __u8 len=0;
892                                                 __u8 type=0;
893                                                 eventData *event=0;
894                                                 fread( &type, sizeof(__u8), 1, f);
895                                                 fread( &len, sizeof(__u8), 1, f);
896                                                 event = new eventData(0, len, type);
897                                                 event->EITdata = new __u8[len];
898                                                 eventData::CacheSize+=len;
899                                                 fread( event->EITdata, len, 1, f);
900                                                 evMap[ event->getEventID() ]=event;
901                                                 tmMap[ event->getStartTime() ]=event;
902                                                 ++cnt;
903                                         }
904                                         eventDB[key]=std::pair<eventMap,timeMap>(evMap,tmMap);
905                                 }
906                                 eventData::load(f);
907                                 eDebug("[EPGC] %d events read from /hdd/epg.dat", cnt);
908 #ifdef ENABLE_PRIVATE_EPG
909                                 char text2[11];
910                                 fread( text2, 11, 1, f);
911                                 if ( !strncmp( text2, "PRIVATE_EPG", 11) )
912                                 {
913                                         size=0;
914                                         fread( &size, sizeof(int), 1, f);
915                                         while(size--)
916                                         {
917                                                 int size=0;
918                                                 uniqueEPGKey key;
919                                                 fread( &key, sizeof(uniqueEPGKey), 1, f);
920                                                 eventMap &evMap=eventDB[key].first;
921                                                 fread( &size, sizeof(int), 1, f);
922                                                 while(size--)
923                                                 {
924                                                         int size;
925                                                         int content_id;
926                                                         fread( &content_id, sizeof(int), 1, f);
927                                                         fread( &size, sizeof(int), 1, f);
928                                                         while(size--)
929                                                         {
930                                                                 time_t time1, time2;
931                                                                 __u16 event_id;
932                                                                 fread( &time1, sizeof(time_t), 1, f);
933                                                                 fread( &time2, sizeof(time_t), 1, f);
934                                                                 fread( &event_id, sizeof(__u16), 1, f);
935                                                                 content_time_tables[key][content_id][time1]=std::pair<time_t, __u16>(time2, event_id);
936                                                                 eventMap::iterator it =
937                                                                         evMap.find(event_id);
938                                                                 if (it != evMap.end())
939                                                                         it->second->type = PRIVATE;
940                                                         }
941                                                 }
942                                         }
943                                 }
944 #endif // ENABLE_PRIVATE_EPG
945                         }
946                         else
947                                 eDebug("[EPGC] don't read old epg database");
948                         fclose(f);
949                 }
950         }
951 }
952
953 void eEPGCache::save()
954 {
955         struct statfs s;
956         off64_t tmp;
957         if (statfs("/hdd", &s)<0)
958                 tmp=0;
959         else
960         {
961                 tmp=s.f_blocks;
962                 tmp*=s.f_bsize;
963         }
964
965         // prevent writes to builtin flash
966         if ( tmp < 1024*1024*50 ) // storage size < 50MB
967                 return;
968
969         // check for enough free space on storage
970         tmp=s.f_bfree;
971         tmp*=s.f_bsize;
972         if ( tmp < (eventData::CacheSize*12)/10 ) // 20% overhead
973                 return;
974
975         FILE *f = fopen("/hdd/epg.dat", "w");
976         int cnt=0;
977         if ( f )
978         {
979                 unsigned int magic = 0x98765432;
980                 fwrite( &magic, sizeof(int), 1, f);
981                 const char *text = "ENIGMA_EPG_V6";
982                 fwrite( text, 13, 1, f );
983                 int size = eventDB.size();
984                 fwrite( &size, sizeof(int), 1, f );
985                 for (eventCache::iterator service_it(eventDB.begin()); service_it != eventDB.end(); ++service_it)
986                 {
987                         timeMap &timemap = service_it->second.second;
988                         fwrite( &service_it->first, sizeof(uniqueEPGKey), 1, f);
989                         size = timemap.size();
990                         fwrite( &size, sizeof(int), 1, f);
991                         for (timeMap::iterator time_it(timemap.begin()); time_it != timemap.end(); ++time_it)
992                         {
993                                 __u8 len = time_it->second->ByteSize;
994                                 fwrite( &time_it->second->type, sizeof(__u8), 1, f );
995                                 fwrite( &len, sizeof(__u8), 1, f);
996                                 fwrite( time_it->second->EITdata, len, 1, f);
997                                 ++cnt;
998                         }
999                 }
1000                 eDebug("[EPGC] %d events written to /hdd/epg.dat", cnt);
1001                 eventData::save(f);
1002 #ifdef ENABLE_PRIVATE_EPG
1003                 const char* text3 = "PRIVATE_EPG";
1004                 fwrite( text3, 11, 1, f );
1005                 size = content_time_tables.size();
1006                 fwrite( &size, sizeof(int), 1, f);
1007                 for (contentMaps::iterator a = content_time_tables.begin(); a != content_time_tables.end(); ++a)
1008                 {
1009                         contentMap &content_time_table = a->second;
1010                         fwrite( &a->first, sizeof(uniqueEPGKey), 1, f);
1011                         int size = content_time_table.size();
1012                         fwrite( &size, sizeof(int), 1, f);
1013                         for (contentMap::iterator i = content_time_table.begin(); i != content_time_table.end(); ++i )
1014                         {
1015                                 int size = i->second.size();
1016                                 fwrite( &i->first, sizeof(int), 1, f);
1017                                 fwrite( &size, sizeof(int), 1, f);
1018                                 for ( contentTimeMap::iterator it(i->second.begin());
1019                                         it != i->second.end(); ++it )
1020                                 {
1021                                         fwrite( &it->first, sizeof(time_t), 1, f);
1022                                         fwrite( &it->second.first, sizeof(time_t), 1, f);
1023                                         fwrite( &it->second.second, sizeof(__u16), 1, f);
1024                                 }
1025                         }
1026                 }
1027 #endif
1028                 fclose(f);
1029 #if 0
1030                 unsigned char md5[16];
1031                 if (!md5_file("/hdd/epg.dat", 1, md5))
1032                 {
1033                         FILE *f = fopen("/hdd/epg.dat.md5", "w");
1034                         if (f)
1035                         {
1036                                 fwrite( md5, 16, 1, f);
1037                                 fclose(f);
1038                         }
1039                 }
1040 #endif
1041         }
1042 }
1043
1044 eEPGCache::channel_data::channel_data(eEPGCache *ml)
1045         :cache(ml)
1046         ,abortTimer(ml), zapTimer(ml), state(0)
1047         ,isRunning(0), haveData(0)
1048 #ifdef ENABLE_PRIVATE_EPG
1049         ,startPrivateTimer(ml)
1050 #endif
1051 #ifdef ENABLE_MHW_EPG
1052         ,m_MHWTimeoutTimer(ml)
1053 #endif
1054 {
1055 #ifdef ENABLE_MHW_EPG
1056         CONNECT(m_MHWTimeoutTimer.timeout, eEPGCache::channel_data::MHWTimeout);
1057 #endif
1058         CONNECT(zapTimer.timeout, eEPGCache::channel_data::startEPG);
1059         CONNECT(abortTimer.timeout, eEPGCache::channel_data::abortNonAvail);
1060 #ifdef ENABLE_PRIVATE_EPG
1061         CONNECT(startPrivateTimer.timeout, eEPGCache::channel_data::startPrivateReader);
1062 #endif
1063         pthread_mutex_init(&channel_active, 0);
1064 }
1065
1066 bool eEPGCache::channel_data::finishEPG()
1067 {
1068         if (!isRunning)  // epg ready
1069         {
1070                 eDebug("[EPGC] stop caching events(%ld)", eDVBLocalTimeHandler::getInstance()->nowTime());
1071                 zapTimer.start(UPDATE_INTERVAL, 1);
1072                 eDebug("[EPGC] next update in %i min", UPDATE_INTERVAL / 60000);
1073                 for (int i=0; i < 3; ++i)
1074                 {
1075                         seenSections[i].clear();
1076                         calcedSections[i].clear();
1077                 }
1078                 singleLock l(cache->cache_lock);
1079                 cache->channelLastUpdated[channel->getChannelID()] = eDVBLocalTimeHandler::getInstance()->nowTime();
1080 #ifdef ENABLE_MHW_EPG
1081                 cleanup();
1082 #endif
1083                 return true;
1084         }
1085         return false;
1086 }
1087
1088 void eEPGCache::channel_data::startEPG()
1089 {
1090         eDebug("[EPGC] start caching events(%ld)", eDVBLocalTimeHandler::getInstance()->nowTime());
1091         state=0;
1092         haveData=0;
1093         for (int i=0; i < 3; ++i)
1094         {
1095                 seenSections[i].clear();
1096                 calcedSections[i].clear();
1097         }
1098
1099         eDVBSectionFilterMask mask;
1100         memset(&mask, 0, sizeof(mask));
1101
1102 #ifdef ENABLE_MHW_EPG
1103         mask.pid = 0xD3;
1104         mask.data[0] = 0x91;
1105         mask.mask[0] = 0xFF;
1106         m_MHWReader->connectRead(slot(*this, &eEPGCache::channel_data::readMHWData), m_MHWConn);
1107         m_MHWReader->start(mask);
1108         isRunning |= MHW;
1109         memcpy(&m_MHWFilterMask, &mask, sizeof(eDVBSectionFilterMask));
1110
1111         mask.pid = 0x231;
1112         mask.data[0] = 0xC8;
1113         mask.mask[0] = 0xFF;
1114         mask.data[1] = 0;
1115         mask.mask[1] = 0xFF;
1116         m_MHWReader2->connectRead(slot(*this, &eEPGCache::channel_data::readMHWData2), m_MHWConn2);
1117         m_MHWReader2->start(mask);
1118         isRunning |= MHW;
1119         memcpy(&m_MHWFilterMask2, &mask, sizeof(eDVBSectionFilterMask));
1120         mask.data[1] = 0;
1121         mask.mask[1] = 0;
1122 #endif
1123
1124         mask.pid = 0x12;
1125         mask.flags = eDVBSectionFilterMask::rfCRC;
1126
1127         mask.data[0] = 0x4E;
1128         mask.mask[0] = 0xFE;
1129         m_NowNextReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_NowNextConn);
1130         m_NowNextReader->start(mask);
1131         isRunning |= NOWNEXT;
1132
1133         mask.data[0] = 0x50;
1134         mask.mask[0] = 0xF0;
1135         m_ScheduleReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_ScheduleConn);
1136         m_ScheduleReader->start(mask);
1137         isRunning |= SCHEDULE;
1138
1139         mask.data[0] = 0x60;
1140         m_ScheduleOtherReader->connectRead(slot(*this, &eEPGCache::channel_data::readData), m_ScheduleOtherConn);
1141         m_ScheduleOtherReader->start(mask);
1142         isRunning |= SCHEDULE_OTHER;
1143
1144         abortTimer.start(7000,true);
1145 }
1146
1147 void eEPGCache::channel_data::abortNonAvail()
1148 {
1149         if (!state)
1150         {
1151                 if ( !(haveData&NOWNEXT) && (isRunning&NOWNEXT) )
1152                 {
1153                         eDebug("[EPGC] abort non avail nownext reading");
1154                         isRunning &= ~NOWNEXT;
1155                         m_NowNextReader->stop();
1156                         m_NowNextConn=0;
1157                 }
1158                 if ( !(haveData&SCHEDULE) && (isRunning&SCHEDULE) )
1159                 {
1160                         eDebug("[EPGC] abort non avail schedule reading");
1161                         isRunning &= ~SCHEDULE;
1162                         m_ScheduleReader->stop();
1163                         m_ScheduleConn=0;
1164                 }
1165                 if ( !(haveData&SCHEDULE_OTHER) && (isRunning&SCHEDULE_OTHER) )
1166                 {
1167                         eDebug("[EPGC] abort non avail schedule_other reading");
1168                         isRunning &= ~SCHEDULE_OTHER;
1169                         m_ScheduleOtherReader->stop();
1170                         m_ScheduleOtherConn=0;
1171                 }
1172 #ifdef ENABLE_MHW_EPG
1173                 if ( !(haveData&MHW) && (isRunning&MHW) )
1174                 {
1175                         eDebug("[EPGC] abort non avail mhw reading");
1176                         isRunning &= ~MHW;
1177                         m_MHWReader->stop();
1178                         m_MHWConn=0;
1179                         m_MHWReader2->stop();
1180                         m_MHWConn2=0;
1181                 }
1182 #endif
1183                 if ( isRunning )
1184                         abortTimer.start(90000, true);
1185                 else
1186                 {
1187                         ++state;
1188                         for (int i=0; i < 3; ++i)
1189                         {
1190                                 seenSections[i].clear();
1191                                 calcedSections[i].clear();
1192                         }
1193                 }
1194         }
1195         ++state;
1196 }
1197
1198 void eEPGCache::channel_data::startChannel()
1199 {
1200         pthread_mutex_lock(&channel_active);
1201         updateMap::iterator It = cache->channelLastUpdated.find( channel->getChannelID() );
1202
1203         int update = ( It != cache->channelLastUpdated.end() ? ( UPDATE_INTERVAL - ( (eDVBLocalTimeHandler::getInstance()->nowTime()-It->second) * 1000 ) ) : ZAP_DELAY );
1204
1205         if (update < ZAP_DELAY)
1206                 update = ZAP_DELAY;
1207
1208         zapTimer.start(update, 1);
1209         if (update >= 60000)
1210                 eDebug("[EPGC] next update in %i min", update/60000);
1211         else if (update >= 1000)
1212                 eDebug("[EPGC] next update in %i sec", update/1000);
1213 }
1214
1215 void eEPGCache::channel_data::abortEPG()
1216 {
1217         for (int i=0; i < 3; ++i)
1218         {
1219                 seenSections[i].clear();
1220                 calcedSections[i].clear();
1221         }
1222         abortTimer.stop();
1223         zapTimer.stop();
1224         if (isRunning)
1225         {
1226                 eDebug("[EPGC] abort caching events !!");
1227                 if (isRunning & SCHEDULE)
1228                 {
1229                         isRunning &= ~SCHEDULE;
1230                         m_ScheduleReader->stop();
1231                         m_ScheduleConn=0;
1232                 }
1233                 if (isRunning & NOWNEXT)
1234                 {
1235                         isRunning &= ~NOWNEXT;
1236                         m_NowNextReader->stop();
1237                         m_NowNextConn=0;
1238                 }
1239                 if (isRunning & SCHEDULE_OTHER)
1240                 {
1241                         isRunning &= ~SCHEDULE_OTHER;
1242                         m_ScheduleOtherReader->stop();
1243                         m_ScheduleOtherConn=0;
1244                 }
1245 #ifdef ENABLE_MHW_EPG
1246                 if (isRunning & MHW)
1247                 {
1248                         isRunning &= ~MHW;
1249                         m_MHWReader->stop();
1250                         m_MHWConn=0;
1251                         m_MHWReader2->stop();
1252                         m_MHWConn2=0;
1253                 }
1254 #endif
1255         }
1256 #ifdef ENABLE_PRIVATE_EPG
1257         if (m_PrivateReader)
1258                 m_PrivateReader->stop();
1259         if (m_PrivateConn)
1260                 m_PrivateConn=0;
1261 #endif
1262         pthread_mutex_unlock(&channel_active);
1263 }
1264
1265 void eEPGCache::channel_data::readData( const __u8 *data)
1266 {
1267         int source;
1268         int map;
1269         iDVBSectionReader *reader=NULL;
1270         switch(data[0])
1271         {
1272                 case 0x4E ... 0x4F:
1273                         reader=m_NowNextReader;
1274                         source=NOWNEXT;
1275                         map=0;
1276                         break;
1277                 case 0x50 ... 0x5F:
1278                         reader=m_ScheduleReader;
1279                         source=SCHEDULE;
1280                         map=1;
1281                         break;
1282                 case 0x60 ... 0x6F:
1283                         reader=m_ScheduleOtherReader;
1284                         source=SCHEDULE_OTHER;
1285                         map=2;
1286                         break;
1287                 default:
1288                         eDebug("[EPGC] unknown table_id !!!");
1289                         return;
1290         }
1291         tidMap &seenSections = this->seenSections[map];
1292         tidMap &calcedSections = this->calcedSections[map];
1293         if ( state == 1 && calcedSections == seenSections || state > 1 )
1294         {
1295                 eDebugNoNewLine("[EPGC] ");
1296                 switch (source)
1297                 {
1298                         case NOWNEXT:
1299                                 m_NowNextConn=0;
1300                                 eDebugNoNewLine("nownext");
1301                                 break;
1302                         case SCHEDULE:
1303                                 m_ScheduleConn=0;
1304                                 eDebugNoNewLine("schedule");
1305                                 break;
1306                         case SCHEDULE_OTHER:
1307                                 m_ScheduleOtherConn=0;
1308                                 eDebugNoNewLine("schedule other");
1309                                 break;
1310                         default: eDebugNoNewLine("unknown");break;
1311                 }
1312                 eDebug(" finished(%ld)", eDVBLocalTimeHandler::getInstance()->nowTime());
1313                 if ( reader )
1314                         reader->stop();
1315                 isRunning &= ~source;
1316                 if (!isRunning)
1317                         finishEPG();
1318         }
1319         else
1320         {
1321                 eit_t *eit = (eit_t*) data;
1322                 __u32 sectionNo = data[0] << 24;
1323                 sectionNo |= data[3] << 16;
1324                 sectionNo |= data[4] << 8;
1325                 sectionNo |= eit->section_number;
1326
1327                 tidMap::iterator it =
1328                         seenSections.find(sectionNo);
1329
1330                 if ( it == seenSections.end() )
1331                 {
1332                         seenSections.insert(sectionNo);
1333                         calcedSections.insert(sectionNo);
1334                         __u32 tmpval = sectionNo & 0xFFFFFF00;
1335                         __u8 incr = source == NOWNEXT ? 1 : 8;
1336                         for ( int i = 0; i <= eit->last_section_number; i+=incr )
1337                         {
1338                                 if ( i == eit->section_number )
1339                                 {
1340                                         for (int x=i; x <= eit->segment_last_section_number; ++x)
1341                                                 calcedSections.insert(tmpval|(x&0xFF));
1342                                 }
1343                                 else
1344                                         calcedSections.insert(tmpval|(i&0xFF));
1345                         }
1346                         cache->sectionRead(data, source, this);
1347                 }
1348         }
1349 }
1350
1351 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eventData *&result, int direction)
1352 // if t == -1 we search the current event...
1353 {
1354         singleLock s(cache_lock);
1355         uniqueEPGKey key(handleGroup(service));
1356
1357         // check if EPG for this service is ready...
1358         eventCache::iterator It = eventDB.find( key );
1359         if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached ?
1360         {
1361                 if (t==-1)
1362                         t = eDVBLocalTimeHandler::getInstance()->nowTime();
1363                 timeMap::iterator i = direction <= 0 ? It->second.second.lower_bound(t) :  // find > or equal
1364                         It->second.second.upper_bound(t); // just >
1365                 if ( i != It->second.second.end() )
1366                 {
1367                         if ( direction < 0 || (direction == 0 && i->second->getStartTime() > t) )
1368                         {
1369                                 timeMap::iterator x = i;
1370                                 --x;
1371                                 if ( x != It->second.second.end() )
1372                                 {
1373                                         time_t start_time = x->second->getStartTime();
1374                                         if (direction >= 0)
1375                                         {
1376                                                 if (t < start_time)
1377                                                         return -1;
1378                                                 if (t > (start_time+x->second->getDuration()))
1379                                                         return -1;
1380                                         }
1381                                         i = x;
1382                                 }
1383                                 else
1384                                         return -1;
1385                         }
1386                         result = i->second;
1387                         return 0;
1388                 }
1389         }
1390         return -1;
1391 }
1392
1393 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, const eit_event_struct *&result, int direction)
1394 {
1395         singleLock s(cache_lock);
1396         const eventData *data=0;
1397         RESULT ret = lookupEventTime(service, t, data, direction);
1398         if ( !ret && data )
1399                 result = data->get();
1400         return ret;
1401 }
1402
1403 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, Event *& result, int direction)
1404 {
1405         singleLock s(cache_lock);
1406         const eventData *data=0;
1407         RESULT ret = lookupEventTime(service, t, data, direction);
1408         if ( !ret && data )
1409                 result = new Event((uint8_t*)data->get());
1410         return ret;
1411 }
1412
1413 RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, ePtr<eServiceEvent> &result, int direction)
1414 {
1415         singleLock s(cache_lock);
1416         const eventData *data=0;
1417         RESULT ret = lookupEventTime(service, t, data, direction);
1418         if ( !ret && data )
1419         {
1420                 Event ev((uint8_t*)data->get());
1421                 result = new eServiceEvent();
1422                 const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
1423                 ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
1424         }
1425         return ret;
1426 }
1427
1428 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eventData *&result )
1429 {
1430         singleLock s(cache_lock);
1431         uniqueEPGKey key(handleGroup(service));
1432
1433         eventCache::iterator It = eventDB.find( key );
1434         if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached?
1435         {
1436                 eventMap::iterator i( It->second.first.find( event_id ));
1437                 if ( i != It->second.first.end() )
1438                 {
1439                         result = i->second;
1440                         return 0;
1441                 }
1442                 else
1443                 {
1444                         result = 0;
1445                         eDebug("[EPGC] event %04x not found in epgcache", event_id);
1446                 }
1447         }
1448         return -1;
1449 }
1450
1451 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&result)
1452 {
1453         singleLock s(cache_lock);
1454         const eventData *data=0;
1455         RESULT ret = lookupEventId(service, event_id, data);
1456         if ( !ret && data )
1457                 result = data->get();
1458         return ret;
1459 }
1460
1461 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, Event *& result)
1462 {
1463         singleLock s(cache_lock);
1464         const eventData *data=0;
1465         RESULT ret = lookupEventId(service, event_id, data);
1466         if ( !ret && data )
1467                 result = new Event((uint8_t*)data->get());
1468         return ret;
1469 }
1470
1471 RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &result)
1472 {
1473         singleLock s(cache_lock);
1474         const eventData *data=0;
1475         RESULT ret = lookupEventId(service, event_id, data);
1476         if ( !ret && data )
1477         {
1478                 Event ev((uint8_t*)data->get());
1479                 result = new eServiceEvent();
1480                 const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
1481                 ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
1482         }
1483         return ret;
1484 }
1485
1486 RESULT eEPGCache::startTimeQuery(const eServiceReference &service, time_t begin, int minutes)
1487 {
1488         Lock();
1489         eventCache::iterator It = eventDB.find(handleGroup(service));
1490         if ( It != eventDB.end() && It->second.second.size() )
1491         {
1492                 m_timemap_end = minutes != -1 ? It->second.second.upper_bound(begin+minutes*60) : It->second.second.end();
1493                 if ( begin != -1 )
1494                 {
1495                         m_timemap_cursor = It->second.second.lower_bound(begin);
1496                         if ( m_timemap_cursor != It->second.second.end() )
1497                         {
1498                                 if ( m_timemap_cursor->second->getStartTime() != begin )
1499                                 {
1500                                         timeMap::iterator x = m_timemap_cursor;
1501                                         --x;
1502                                         if ( x != It->second.second.end() )
1503                                         {
1504                                                 time_t start_time = x->second->getStartTime();
1505                                                 if ( begin > start_time && begin < (start_time+x->second->getDuration()))
1506                                                         m_timemap_cursor = x;
1507                                         }
1508                                 }
1509                         }
1510                 }
1511                 else
1512                         m_timemap_cursor = It->second.second.begin();
1513                 const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)handleGroup(service);
1514                 currentQueryTsidOnid = (ref.getTransportStreamID().get()<<16) | ref.getOriginalNetworkID().get();
1515                 Unlock();
1516                 return 0;
1517         }
1518         Unlock();
1519         return -1;
1520 }
1521
1522 RESULT eEPGCache::getNextTimeEntry(const eventData *& result)
1523 {
1524         if ( m_timemap_cursor != m_timemap_end )
1525         {
1526                 result = m_timemap_cursor++->second;
1527                 return 0;
1528         }
1529         return -1;
1530 }
1531
1532 RESULT eEPGCache::getNextTimeEntry(const eit_event_struct *&result)
1533 {
1534         if ( m_timemap_cursor != m_timemap_end )
1535         {
1536                 result = m_timemap_cursor++->second->get();
1537                 return 0;
1538         }
1539         return -1;
1540 }
1541
1542 RESULT eEPGCache::getNextTimeEntry(Event *&result)
1543 {
1544         if ( m_timemap_cursor != m_timemap_end )
1545         {
1546                 result = new Event((uint8_t*)m_timemap_cursor++->second->get());
1547                 return 0;
1548         }
1549         return -1;
1550 }
1551
1552 RESULT eEPGCache::getNextTimeEntry(ePtr<eServiceEvent> &result)
1553 {
1554         if ( m_timemap_cursor != m_timemap_end )
1555         {
1556                 Event ev((uint8_t*)m_timemap_cursor++->second->get());
1557                 result = new eServiceEvent();
1558                 return result->parseFrom(&ev, currentQueryTsidOnid);
1559         }
1560         return -1;
1561 }
1562
1563 void fillTuple(ePyObject tuple, char *argstring, int argcount, ePyObject service, ePtr<eServiceEvent> &ptr, ePyObject nowTime, ePyObject service_name )
1564 {
1565         ePyObject tmp;
1566         int pos=0;
1567         while(pos < argcount)
1568         {
1569                 bool inc_refcount=false;
1570                 switch(argstring[pos])
1571                 {
1572                         case '0': // PyLong 0
1573                                 tmp = PyLong_FromLong(0);
1574                                 break;
1575                         case 'I': // Event Id
1576                                 tmp = ptr ? PyLong_FromLong(ptr->getEventId()) : ePyObject();
1577                                 break;
1578                         case 'B': // Event Begin Time
1579                                 tmp = ptr ? PyLong_FromLong(ptr->getBeginTime()) : ePyObject();
1580                                 break;
1581                         case 'D': // Event Duration
1582                                 tmp = ptr ? PyLong_FromLong(ptr->getDuration()) : ePyObject();
1583                                 break;
1584                         case 'T': // Event Title
1585                                 tmp = ptr ? PyString_FromString(ptr->getEventName().c_str()) : ePyObject();
1586                                 break;
1587                         case 'S': // Event Short Description
1588                                 tmp = ptr ? PyString_FromString(ptr->getShortDescription().c_str()) : ePyObject();
1589                                 break;
1590                         case 'E': // Event Extended Description
1591                                 tmp = ptr ? PyString_FromString(ptr->getExtendedDescription().c_str()) : ePyObject();
1592                                 break;
1593                         case 'C': // Current Time
1594                                 tmp = nowTime;
1595                                 inc_refcount = true;
1596                                 break;
1597                         case 'R': // service reference string
1598                                 tmp = service;
1599                                 inc_refcount = true;
1600                                 break;
1601                         case 'N': // service name
1602                                 tmp = service_name;
1603                                 inc_refcount = true;
1604                 }
1605                 if (!tmp)
1606                 {
1607                         tmp = Py_None;
1608                         inc_refcount = true;
1609                 }
1610                 if (inc_refcount)
1611                         Py_INCREF(tmp);
1612                 PyTuple_SET_ITEM(tuple, pos++, tmp);
1613         }
1614 }
1615
1616 int handleEvent(ePtr<eServiceEvent> &ptr, ePyObject dest_list, char* argstring, int argcount, ePyObject service, ePyObject nowTime, ePyObject service_name, ePyObject convertFunc, ePyObject convertFuncArgs)
1617 {
1618         if (convertFunc)
1619         {
1620                 fillTuple(convertFuncArgs, argstring, argcount, service, ptr, nowTime, service_name);
1621                 ePyObject result = PyObject_CallObject(convertFunc, convertFuncArgs);
1622                 if (result)
1623                 {
1624                         if (service_name)
1625                                 Py_DECREF(service_name);
1626                         if (nowTime)
1627                                 Py_DECREF(nowTime);
1628                         Py_DECREF(convertFuncArgs);
1629                         Py_DECREF(dest_list);
1630                         PyErr_SetString(PyExc_StandardError,
1631                                 "error in convertFunc execute");
1632                         eDebug("error in convertFunc execute");
1633                         return -1;
1634                 }
1635                 PyList_Append(dest_list, result);
1636                 Py_DECREF(result);
1637         }
1638         else
1639         {
1640                 ePyObject tuple = PyTuple_New(argcount);
1641                 fillTuple(tuple, argstring, argcount, service, ptr, nowTime, service_name);
1642                 PyList_Append(dest_list, tuple);
1643                 Py_DECREF(tuple);
1644         }
1645         return 0;
1646 }
1647
1648 // here we get a python list
1649 // the first entry in the list is a python string to specify the format of the returned tuples (in a list)
1650 //   0 = PyLong(0)
1651 //   I = Event Id
1652 //   B = Event Begin Time
1653 //   D = Event Duration
1654 //   T = Event Title
1655 //   S = Event Short Description
1656 //   E = Event Extended Description
1657 //   C = Current Time
1658 //   R = Service Reference
1659 //   N = Service Name
1660 // then for each service follows a tuple
1661 //   first tuple entry is the servicereference (as string... use the ref.toString() function)
1662 //   the second is the type of query
1663 //     2 = event_id
1664 //    -1 = event before given start_time
1665 //     0 = event intersects given start_time
1666 //    +1 = event after given start_time
1667 //   the third
1668 //      when type is eventid it is the event_id
1669 //      when type is time then it is the start_time ( 0 for now_time )
1670 //   the fourth is the end_time .. ( optional .. for query all events in time range)
1671
1672 PyObject *eEPGCache::lookupEvent(ePyObject list, ePyObject convertFunc)
1673 {
1674         ePyObject convertFuncArgs;
1675         int argcount=0;
1676         char *argstring=NULL;
1677         if (!PyList_Check(list))
1678         {
1679                 PyErr_SetString(PyExc_StandardError,
1680                         "type error");
1681                 eDebug("no list");
1682                 return NULL;
1683         }
1684         int listIt=0;
1685         int listSize=PyList_Size(list);
1686         if (!listSize)
1687         {
1688                 PyErr_SetString(PyExc_StandardError,
1689                         "not params given");
1690                 eDebug("not params given");
1691                 return NULL;
1692         }
1693         else 
1694         {
1695                 ePyObject argv=PyList_GET_ITEM(list, 0); // borrowed reference!
1696                 if (PyString_Check(argv))
1697                 {
1698                         argstring = PyString_AS_STRING(argv);
1699                         ++listIt;
1700                 }
1701                 else
1702                         argstring = "I"; // just event id as default
1703                 argcount = strlen(argstring);
1704 //              eDebug("have %d args('%s')", argcount, argstring);
1705         }
1706         if (convertFunc)
1707         {
1708                 if (!PyCallable_Check(convertFunc))
1709                 {
1710                         PyErr_SetString(PyExc_StandardError,
1711                                 "convertFunc must be callable");
1712                         eDebug("convertFunc is not callable");
1713                         return NULL;
1714                 }
1715                 convertFuncArgs = PyTuple_New(argcount);
1716         }
1717
1718         ePyObject nowTime = strchr(argstring, 'C') ?
1719                 PyLong_FromLong(eDVBLocalTimeHandler::getInstance()->nowTime()) :
1720                 ePyObject();
1721
1722         bool must_get_service_name = strchr(argstring, 'N') ? true : false;
1723
1724         // create dest list
1725         ePyObject dest_list=PyList_New(0);
1726         while(listSize > listIt)
1727         {
1728                 ePyObject item=PyList_GET_ITEM(list, listIt++); // borrowed reference!
1729                 if (PyTuple_Check(item))
1730                 {
1731                         bool service_changed=false;
1732                         int type=0;
1733                         long event_id=-1;
1734                         time_t stime=-1;
1735                         int minutes=0;
1736                         int tupleSize=PyTuple_Size(item);
1737                         int tupleIt=0;
1738                         ePyObject service;
1739                         while(tupleSize > tupleIt)  // parse query args
1740                         {
1741                                 ePyObject entry=PyTuple_GET_ITEM(item, tupleIt); // borrowed reference!
1742                                 switch(tupleIt++)
1743                                 {
1744                                         case 0:
1745                                         {
1746                                                 if (!PyString_Check(entry))
1747                                                 {
1748                                                         eDebug("tuple entry 0 is no a string");
1749                                                         goto skip_entry;
1750                                                 }
1751                                                 service = entry;
1752                                                 break;
1753                                         }
1754                                         case 1:
1755                                                 type=PyInt_AsLong(entry);
1756                                                 if (type < -1 || type > 2)
1757                                                 {
1758                                                         eDebug("unknown type %d", type);
1759                                                         goto skip_entry;
1760                                                 }
1761                                                 break;
1762                                         case 2:
1763                                                 event_id=stime=PyInt_AsLong(entry);
1764                                                 break;
1765                                         case 3:
1766                                                 minutes=PyInt_AsLong(entry);
1767                                                 break;
1768                                         default:
1769                                                 eDebug("unneeded extra argument");
1770                                                 break;
1771                                 }
1772                         }
1773                         eServiceReference ref(handleGroup(eServiceReference(PyString_AS_STRING(service))));
1774                         if (ref.type != eServiceReference::idDVB)
1775                         {
1776                                 eDebug("service reference for epg query is not valid");
1777                                 continue;
1778                         }
1779
1780                         // redirect subservice querys to parent service
1781                         eServiceReferenceDVB &dvb_ref = (eServiceReferenceDVB&)ref;
1782                         if (dvb_ref.getParentTransportStreamID().get()) // linkage subservice
1783                         {
1784                                 eServiceCenterPtr service_center;
1785                                 if (!eServiceCenter::getPrivInstance(service_center))
1786                                 {
1787                                         dvb_ref.setTransportStreamID( dvb_ref.getParentTransportStreamID() );
1788                                         dvb_ref.setServiceID( dvb_ref.getParentServiceID() );
1789                                         dvb_ref.setParentTransportStreamID(eTransportStreamID(0));
1790                                         dvb_ref.setParentServiceID(eServiceID(0));
1791                                         dvb_ref.name="";
1792                                         service = PyString_FromString(dvb_ref.toString().c_str());
1793                                         service_changed = true;
1794                                 }
1795                         }
1796
1797                         ePyObject service_name;
1798                         if (must_get_service_name)
1799                         {
1800                                 ePtr<iStaticServiceInformation> sptr;
1801                                 eServiceCenterPtr service_center;
1802                                 eServiceCenter::getPrivInstance(service_center);
1803                                 if (service_center)
1804                                 {
1805                                         service_center->info(ref, sptr);
1806                                         if (sptr)
1807                                         {
1808                                                 std::string name;
1809                                                 sptr->getName(ref, name);
1810                                                 if (name.length())
1811                                                         service_name = PyString_FromString(name.c_str());
1812                                         }
1813                                 }
1814                                 if (!service_name)
1815                                         service_name = PyString_FromString("<n/a>");
1816                         }
1817                         if (minutes)
1818                         {
1819                                 Lock();
1820                                 if (!startTimeQuery(ref, stime, minutes))
1821                                 {
1822                                         ePtr<eServiceEvent> ptr;
1823                                         while (!getNextTimeEntry(ptr))
1824                                         {
1825                                                 if (handleEvent(ptr, dest_list, argstring, argcount, service, nowTime, service_name, convertFunc, convertFuncArgs))
1826                                                 {
1827                                                         Unlock();
1828                                                         return 0;  // error
1829                                                 }
1830                                         }
1831                                 }
1832                                 Unlock();
1833                         }
1834                         else
1835                         {
1836                                 ePtr<eServiceEvent> ptr;
1837                                 if (stime)
1838                                 {
1839                                         if (type == 2)
1840                                                 lookupEventId(ref, event_id, ptr);
1841                                         else
1842                                                 lookupEventTime(ref, stime, ptr, type);
1843                                 }
1844                                 if (handleEvent(ptr, dest_list, argstring, argcount, service, nowTime, service_name, convertFunc, convertFuncArgs))
1845                                         return 0; // error
1846                         }
1847                         if (service_changed)
1848                                 Py_DECREF(service);
1849                         if (service_name)
1850                                 Py_DECREF(service_name);
1851                 }
1852 skip_entry:
1853                 ;
1854         }
1855         if (convertFuncArgs)
1856                 Py_DECREF(convertFuncArgs);
1857         if (nowTime)
1858                 Py_DECREF(nowTime);
1859         return dest_list;
1860 }
1861
1862 void fillTuple2(ePyObject tuple, const char *argstring, int argcount, eventData *evData, ePtr<eServiceEvent> &ptr, ePyObject service_name, ePyObject service_reference)
1863 {
1864         ePyObject tmp;
1865         int pos=0;
1866         while(pos < argcount)
1867         {
1868                 bool inc_refcount=false;
1869                 switch(argstring[pos])
1870                 {
1871                         case '0': // PyLong 0
1872                                 tmp = PyLong_FromLong(0);
1873                                 break;
1874                         case 'I': // Event Id
1875                                 tmp = PyLong_FromLong(evData->getEventID());
1876                                 break;
1877                         case 'B': // Event Begin Time
1878                                 if (ptr)
1879                                         tmp = ptr ? PyLong_FromLong(ptr->getBeginTime()) : ePyObject();
1880                                 else
1881                                         tmp = PyLong_FromLong(evData->getStartTime());
1882                                 break;
1883                         case 'D': // Event Duration
1884                                 if (ptr)
1885                                         tmp = ptr ? PyLong_FromLong(ptr->getDuration()) : ePyObject();
1886                                 else
1887                                         tmp = PyLong_FromLong(evData->getDuration());
1888                                 break;
1889                         case 'T': // Event Title
1890                                 tmp = ptr ? PyString_FromString(ptr->getEventName().c_str()) : ePyObject();
1891                                 break;
1892                         case 'S': // Event Short Description
1893                                 tmp = ptr ? PyString_FromString(ptr->getShortDescription().c_str()) : ePyObject();
1894                                 break;
1895                         case 'E': // Event Extended Description
1896                                 tmp = ptr ? PyString_FromString(ptr->getExtendedDescription().c_str()) : ePyObject();
1897                                 break;
1898                         case 'R': // service reference string
1899                                 tmp = service_reference;
1900                                 inc_refcount = true;
1901                                 break;
1902                         case 'N': // service name
1903                                 tmp = service_name;
1904                                 inc_refcount = true;
1905                                 break;
1906                 }
1907                 if (!tmp)
1908                 {
1909                         tmp = Py_None;
1910                         inc_refcount = true;
1911                 }
1912                 if (inc_refcount)
1913                         Py_INCREF(tmp);
1914                 PyTuple_SET_ITEM(tuple, pos++, tmp);
1915         }
1916 }
1917
1918 // here we get a python tuple
1919 // the first entry in the tuple is a python string to specify the format of the returned tuples (in a list)
1920 //   I = Event Id
1921 //   B = Event Begin Time
1922 //   D = Event Duration
1923 //   T = Event Title
1924 //   S = Event Short Description
1925 //   E = Event Extended Description
1926 //   R = Service Reference
1927 //   N = Service Name
1928 //  the second tuple entry is the MAX matches value
1929 //  the third tuple entry is the type of query
1930 //     0 = search for similar broadcastings (SIMILAR_BROADCASTINGS_SEARCH)
1931 //     1 = search events with exactly title name (EXAKT_TITLE_SEARCH)
1932 //     2 = search events with text in title name (PARTIAL_TITLE_SEARCH)
1933 //  when type is 0 (SIMILAR_BROADCASTINGS_SEARCH)
1934 //   the fourth is the servicereference string
1935 //   the fifth is the eventid
1936 //  when type is 1 or 2 (EXAKT_TITLE_SEARCH or PARTIAL_TITLE_SEARCH)
1937 //   the fourth is the search text
1938 //   the fifth is
1939 //     0 = case sensitive (CASE_CHECK)
1940 //     1 = case insensitive (NO_CASECHECK)
1941
1942 PyObject *eEPGCache::search(ePyObject arg)
1943 {
1944         ePyObject ret;
1945         int descridx = -1;
1946         __u32 descr[512];
1947         int eventid = -1;
1948         const char *argstring=0;
1949         char *refstr=0;
1950         int argcount=0;
1951         int querytype=-1;
1952         bool needServiceEvent=false;
1953         int maxmatches=0;
1954
1955         if (PyTuple_Check(arg))
1956         {
1957                 int tuplesize=PyTuple_Size(arg);
1958                 if (tuplesize > 0)
1959                 {
1960                         ePyObject obj = PyTuple_GET_ITEM(arg,0);
1961                         if (PyString_Check(obj))
1962                         {
1963                                 argcount = PyString_GET_SIZE(obj);
1964                                 argstring = PyString_AS_STRING(obj);
1965                                 for (int i=0; i < argcount; ++i)
1966                                         switch(argstring[i])
1967                                         {
1968                                         case 'S':
1969                                         case 'E':
1970                                         case 'T':
1971                                                 needServiceEvent=true;
1972                                         default:
1973                                                 break;
1974                                         }
1975                         }
1976                         else
1977                         {
1978                                 PyErr_SetString(PyExc_StandardError,
1979                                         "type error");
1980                                 eDebug("tuple arg 0 is not a string");
1981                                 return NULL;
1982                         }
1983                 }
1984                 if (tuplesize > 1)
1985                         maxmatches = PyLong_AsLong(PyTuple_GET_ITEM(arg, 1));
1986                 if (tuplesize > 2)
1987                 {
1988                         querytype = PyLong_AsLong(PyTuple_GET_ITEM(arg, 2));
1989                         if (tuplesize > 4 && querytype == 0)
1990                         {
1991                                 ePyObject obj = PyTuple_GET_ITEM(arg, 3);
1992                                 if (PyString_Check(obj))
1993                                 {
1994                                         refstr = PyString_AS_STRING(obj);
1995                                         eServiceReferenceDVB ref(refstr);
1996                                         if (ref.valid())
1997                                         {
1998                                                 eventid = PyLong_AsLong(PyTuple_GET_ITEM(arg, 4));
1999                                                 singleLock s(cache_lock);
2000                                                 const eventData *evData = 0;
2001                                                 lookupEventId(ref, eventid, evData);
2002                                                 if (evData)
2003                                                 {
2004                                                         __u8 *data = evData->EITdata;
2005                                                         int tmp = evData->ByteSize-12;
2006                                                         __u32 *p = (__u32*)(data+12);
2007                                                                 // search short and extended event descriptors
2008                                                         while(tmp>0)
2009                                                         {
2010                                                                 __u32 crc = *p++;
2011                                                                 descriptorMap::iterator it =
2012                                                                         eventData::descriptors.find(crc);
2013                                                                 if (it != eventData::descriptors.end())
2014                                                                 {
2015                                                                         __u8 *descr_data = it->second.second;
2016                                                                         switch(descr_data[0])
2017                                                                         {
2018                                                                         case 0x4D ... 0x4E:
2019                                                                                 descr[++descridx]=crc;
2020                                                                         default:
2021                                                                                 break;
2022                                                                         }
2023                                                                 }
2024                                                                 tmp-=4;
2025                                                         }
2026                                                 }
2027                                                 if (descridx<0)
2028                                                         eDebug("event not found");
2029                                         }
2030                                         else
2031                                         {
2032                                                 PyErr_SetString(PyExc_StandardError,
2033                                                         "type error");
2034                                                 eDebug("tuple arg 4 is not a valid service reference string");
2035                                                 return NULL;
2036                                         }
2037                                 }
2038                                 else
2039                                 {
2040                                         PyErr_SetString(PyExc_StandardError,
2041                                         "type error");
2042                                         eDebug("tuple arg 4 is not a string");
2043                                         return NULL;
2044                                 }
2045                         }
2046                         else if (tuplesize > 4 && (querytype == 1 || querytype == 2) )
2047                         {
2048                                 ePyObject obj = PyTuple_GET_ITEM(arg, 3);
2049                                 if (PyString_Check(obj))
2050                                 {
2051                                         int casetype = PyLong_AsLong(PyTuple_GET_ITEM(arg, 4));
2052                                         const char *str = PyString_AS_STRING(obj);
2053                                         int textlen = PyString_GET_SIZE(obj);
2054                                         if (querytype == 1)
2055                                                 eDebug("lookup for events with '%s' as title(%s)", str, casetype?"ignore case":"case sensitive");
2056                                         else
2057                                                 eDebug("lookup for events with '%s' in title(%s)", str, casetype?"ignore case":"case sensitive");
2058                                         singleLock s(cache_lock);
2059                                         for (descriptorMap::iterator it(eventData::descriptors.begin());
2060                                                 it != eventData::descriptors.end() && descridx < 511; ++it)
2061                                         {
2062                                                 __u8 *data = it->second.second;
2063                                                 if ( data[0] == 0x4D ) // short event descriptor
2064                                                 {
2065                                                         int title_len = data[5];
2066                                                         if ( querytype == 1 )
2067                                                         {
2068                                                                 if (title_len > textlen)
2069                                                                         continue;
2070                                                                 else if (title_len < textlen)
2071                                                                         continue;
2072                                                                 if ( casetype )
2073                                                                 {
2074                                                                         if ( !strncasecmp((const char*)data+6, str, title_len) )
2075                                                                         {
2076 //                                                                              std::string s((const char*)data+6, title_len);
2077 //                                                                              eDebug("match1 %s %s", str, s.c_str() );
2078                                                                                 descr[++descridx] = it->first;
2079                                                                         }
2080                                                                 }
2081                                                                 else if ( !strncmp((const char*)data+6, str, title_len) )
2082                                                                 {
2083 //                                                                      std::string s((const char*)data+6, title_len);
2084 //                                                                      eDebug("match2 %s %s", str, s.c_str() );
2085                                                                         descr[++descridx] = it->first;
2086                                                                 }
2087                                                         }
2088                                                         else
2089                                                         {
2090                                                                 int idx=0;
2091                                                                 while((title_len-idx) >= textlen)
2092                                                                 {
2093                                                                         if (casetype)
2094                                                                         {
2095                                                                                 if (!strncasecmp((const char*)data+6+idx, str, textlen) )
2096                                                                                 {
2097                                                                                         descr[++descridx] = it->first;
2098 //                                                                                      std::string s((const char*)data+6, title_len);
2099 //                                                                                      eDebug("match 3 %s %s", str, s.c_str() );
2100                                                                                         break;
2101                                                                                 }
2102                                                                                 else if (!strncmp((const char*)data+6+idx, str, textlen) )
2103                                                                                 {
2104                                                                                         descr[++descridx] = it->first;
2105 //                                                                                      std::string s((const char*)data+6, title_len);
2106 //                                                                                      eDebug("match 4 %s %s", str, s.c_str() );
2107                                                                                         break;
2108                                                                                 }
2109                                                                         }
2110                                                                         ++idx;
2111                                                                 }
2112                                                         }
2113                                                 }
2114                                         }
2115                                 }
2116                                 else
2117                                 {
2118                                         PyErr_SetString(PyExc_StandardError,
2119                                                 "type error");
2120                                         eDebug("tuple arg 4 is not a string");
2121                                         return NULL;
2122                                 }
2123                         }
2124                         else
2125                         {
2126                                 PyErr_SetString(PyExc_StandardError,
2127                                         "type error");
2128                                 eDebug("tuple arg 3(%d) is not a known querytype(0, 1, 2)", querytype);
2129                                 return NULL;
2130                         }
2131                 }
2132                 else
2133                 {
2134                         PyErr_SetString(PyExc_StandardError,
2135                                 "type error");
2136                         eDebug("not enough args in tuple");
2137                         return NULL;
2138                 }
2139         }
2140         else
2141         {
2142                 PyErr_SetString(PyExc_StandardError,
2143                         "type error");
2144                 eDebug("arg 0 is not a tuple");
2145                 return NULL;
2146         }
2147
2148         ASSERT(descridx <= 512);
2149
2150         if (descridx > -1)
2151         {
2152                 int maxcount=maxmatches;
2153                 eServiceReferenceDVB ref(refstr?(const eServiceReferenceDVB&)handleGroup(eServiceReference(refstr)):eServiceReferenceDVB(""));
2154                 // ref is only valid in SIMILAR_BROADCASTING_SEARCH
2155                 // in this case we start searching with the base service
2156                 bool first = ref.valid() ? true : false;
2157                 singleLock s(cache_lock);
2158                 eventCache::iterator cit(ref.valid() ? eventDB.find(ref) : eventDB.begin());
2159                 while(cit != eventDB.end() && maxcount)
2160                 {
2161                         if ( ref.valid() && !first && cit->first == ref )
2162                         {
2163                                 // do not scan base service twice ( only in SIMILAR BROADCASTING SEARCH )
2164                                 ++cit;
2165                                 continue;
2166                         }
2167                         ePyObject service_name;
2168                         ePyObject service_reference;
2169                         timeMap &evmap = cit->second.second;
2170                         // check all events
2171                         for (timeMap::iterator evit(evmap.begin()); evit != evmap.end() && maxcount; ++evit)
2172                         {
2173                                 int evid = evit->second->getEventID();
2174                                 if ( evid == eventid)
2175                                         continue;
2176                                 __u8 *data = evit->second->EITdata;
2177                                 int tmp = evit->second->ByteSize-12;
2178                                 __u32 *p = (__u32*)(data+12);
2179                                 // check if any of our descriptor used by this event
2180                                 int cnt=-1;
2181                                 while(tmp>0)
2182                                 {
2183                                         __u32 crc32 = *p++;
2184                                         for ( int i=0; i <= descridx; ++i)
2185                                         {
2186                                                 if (descr[i] == crc32)  // found...
2187                                                         ++cnt;
2188                                         }
2189                                         tmp-=4;
2190                                 }
2191                                 if ( (querytype == 0 && cnt == descridx) ||
2192                                          ((querytype == 1 || querytype == 2) && cnt != -1) )
2193                                 {
2194                                         const uniqueEPGKey &service = cit->first;
2195                                         eServiceReference ref =
2196                                                 eDVBDB::getInstance()->searchReference(service.tsid, service.onid, service.sid);
2197                                         if (ref.valid())
2198                                         {
2199                                         // create servive event
2200                                                 ePtr<eServiceEvent> ptr;
2201                                                 if (needServiceEvent)
2202                                                 {
2203                                                         lookupEventId(ref, evid, ptr);
2204                                                         if (!ptr)
2205                                                                 eDebug("event not found !!!!!!!!!!!");
2206                                                 }
2207                                         // create service name
2208                                                 if (!service_name && strchr(argstring,'N'))
2209                                                 {
2210                                                         ePtr<iStaticServiceInformation> sptr;
2211                                                         eServiceCenterPtr service_center;
2212                                                         eServiceCenter::getPrivInstance(service_center);
2213                                                         if (service_center)
2214                                                         {
2215                                                                 service_center->info(ref, sptr);
2216                                                                 if (sptr)
2217                                                                 {
2218                                                                         std::string name;
2219                                                                         sptr->getName(ref, name);
2220                                                                         if (name.length())
2221                                                                                 service_name = PyString_FromString(name.c_str());
2222                                                                 }
2223                                                         }
2224                                                         if (!service_name)
2225                                                                 service_name = PyString_FromString("<n/a>");
2226                                                 }
2227                                         // create servicereference string
2228                                                 if (!service_reference && strchr(argstring,'R'))
2229                                                         service_reference = PyString_FromString(ref.toString().c_str());
2230                                         // create list
2231                                                 if (!ret)
2232                                                         ret = PyList_New(0);
2233                                         // create tuple
2234                                                 ePyObject tuple = PyTuple_New(argcount);
2235                                         // fill tuple
2236                                                 fillTuple2(tuple, argstring, argcount, evit->second, ptr, service_name, service_reference);
2237                                                 PyList_Append(ret, tuple);
2238                                                 Py_DECREF(tuple);
2239                                                 --maxcount;
2240                                         }
2241                                 }
2242                         }
2243                         if (service_name)
2244                                 Py_DECREF(service_name);
2245                         if (service_reference)
2246                                 Py_DECREF(service_reference);
2247                         if (first)
2248                         {
2249                                 // now start at first service in epgcache database ( only in SIMILAR BROADCASTING SEARCH )
2250                                 first=false;
2251                                 cit=eventDB.begin();
2252                         }
2253                         else
2254                                 ++cit;
2255                 }
2256         }
2257
2258         if (!ret)
2259                 Py_RETURN_NONE;
2260
2261         return ret;
2262 }
2263
2264 #ifdef ENABLE_PRIVATE_EPG
2265 #include <dvbsi++/descriptor_tag.h>
2266 #include <dvbsi++/unknown_descriptor.h>
2267 #include <dvbsi++/private_data_specifier_descriptor.h>
2268
2269 void eEPGCache::PMTready(eDVBServicePMTHandler *pmthandler)
2270 {
2271         ePtr<eTable<ProgramMapSection> > ptr;
2272         if (!pmthandler->getPMT(ptr) && ptr)
2273         {
2274                 std::vector<ProgramMapSection*>::const_iterator i;
2275                 for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
2276                 {
2277                         const ProgramMapSection &pmt = **i;
2278
2279                         ElementaryStreamInfoConstIterator es;
2280                         for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
2281                         {
2282                                 int tmp=0;
2283                                 switch ((*es)->getType())
2284                                 {
2285                                 case 0x05: // private
2286                                         for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
2287                                                 desc != (*es)->getDescriptors()->end(); ++desc)
2288                                         {
2289                                                 switch ((*desc)->getTag())
2290                                                 {
2291                                                         case PRIVATE_DATA_SPECIFIER_DESCRIPTOR:
2292                                                                 if (((PrivateDataSpecifierDescriptor*)(*desc))->getPrivateDataSpecifier() == 190)
2293                                                                         tmp |= 1;
2294                                                                 break;
2295                                                         case 0x90:
2296                                                         {
2297                                                                 UnknownDescriptor *descr = (UnknownDescriptor*)*desc;
2298                                                                 int descr_len = descr->getLength();
2299                                                                 if (descr_len == 4)
2300                                                                 {
2301                                                                         uint8_t data[descr_len+2];
2302                                                                         descr->writeToBuffer(data);
2303                                                                         if ( !data[2] && !data[3] && data[4] == 0xFF && data[5] == 0xFF )
2304                                                                                 tmp |= 2;
2305                                                                 }
2306                                                                 break;
2307                                                         }
2308                                                         default:
2309                                                                 break;
2310                                                 }
2311                                         }
2312                                 default:
2313                                         break;
2314                                 }
2315                                 if (tmp==3)
2316                                 {
2317                                         eServiceReferenceDVB ref;
2318                                         if (!pmthandler->getServiceReference(ref))
2319                                         {
2320                                                 int pid = (*es)->getPid();
2321                                                 messages.send(Message(Message::got_private_pid, ref, pid));
2322                                                 return;
2323                                         }
2324                                 }
2325                         }
2326                 }
2327         }
2328         else
2329                 eDebug("PMTready but no pmt!!");
2330 }
2331
2332 struct date_time
2333 {
2334         __u8 data[5];
2335         time_t tm;
2336         date_time( const date_time &a )
2337         {
2338                 memcpy(data, a.data, 5);
2339                 tm = a.tm;
2340         }
2341         date_time( const __u8 data[5])
2342         {
2343                 memcpy(this->data, data, 5);
2344                 tm = parseDVBtime(data[0], data[1], data[2], data[3], data[4]);
2345         }
2346         date_time()
2347         {
2348         }
2349         const __u8& operator[](int pos) const
2350         {
2351                 return data[pos];
2352         }
2353 };
2354
2355 struct less_datetime
2356 {
2357         bool operator()( const date_time &a, const date_time &b ) const
2358         {
2359                 return abs(a.tm-b.tm) < 360 ? false : a.tm < b.tm;
2360         }
2361 };
2362
2363 void eEPGCache::privateSectionRead(const uniqueEPGKey &current_service, const __u8 *data)
2364 {
2365         contentMap &content_time_table = content_time_tables[current_service];
2366         singleLock s(cache_lock);
2367         std::map< date_time, std::list<uniqueEPGKey>, less_datetime > start_times;
2368         eventMap &evMap = eventDB[current_service].first;
2369         timeMap &tmMap = eventDB[current_service].second;
2370         int ptr=8;
2371         int content_id = data[ptr++] << 24;
2372         content_id |= data[ptr++] << 16;
2373         content_id |= data[ptr++] << 8;
2374         content_id |= data[ptr++];
2375
2376         contentTimeMap &time_event_map =
2377                 content_time_table[content_id];
2378         for ( contentTimeMap::iterator it( time_event_map.begin() );
2379                 it != time_event_map.end(); ++it )
2380         {
2381                 eventMap::iterator evIt( evMap.find(it->second.second) );
2382                 if ( evIt != evMap.end() )
2383                 {
2384                         delete evIt->second;
2385                         evMap.erase(evIt);
2386                 }
2387                 tmMap.erase(it->second.first);
2388         }
2389         time_event_map.clear();
2390
2391         __u8 duration[3];
2392         memcpy(duration, data+ptr, 3);
2393         ptr+=3;
2394         int duration_sec =
2395                 fromBCD(duration[0])*3600+fromBCD(duration[1])*60+fromBCD(duration[2]);
2396
2397         const __u8 *descriptors[65];
2398         const __u8 **pdescr = descriptors;
2399
2400         int descriptors_length = (data[ptr++]&0x0F) << 8;
2401         descriptors_length |= data[ptr++];
2402         while ( descriptors_length > 0 )
2403         {
2404                 int descr_type = data[ptr];
2405                 int descr_len = data[ptr+1];
2406                 descriptors_length -= (descr_len+2);
2407                 if ( descr_type == 0xf2 )
2408                 {
2409                         ptr+=2;
2410                         int tsid = data[ptr++] << 8;
2411                         tsid |= data[ptr++];
2412                         int onid = data[ptr++] << 8;
2413                         onid |= data[ptr++];
2414                         int sid = data[ptr++] << 8;
2415                         sid |= data[ptr++];
2416
2417 // WORKAROUND for wrong transmitted epg data (01.08.2006)
2418                         if ( onid == 0x85 )
2419                         {
2420                                 switch( (tsid << 16) | sid )
2421                                 {
2422                                         case 0x01030b: sid = 0x1b; tsid = 4; break;  // Premiere Win
2423                                         case 0x0300f0: sid = 0xe0; tsid = 2; break;
2424                                         case 0x0300f1: sid = 0xe1; tsid = 2; break;
2425                                         case 0x0300f5: sid = 0xdc; break;
2426                                         case 0x0400d2: sid = 0xe2; tsid = 0x11; break;
2427                                         case 0x1100d3: sid = 0xe3; break;
2428                                 }
2429                         }
2430 ////////////////////////////////////////////
2431
2432                         uniqueEPGKey service( sid, onid, tsid );
2433                         descr_len -= 6;
2434                         while( descr_len > 0 )
2435                         {
2436                                 __u8 datetime[5];
2437                                 datetime[0] = data[ptr++];
2438                                 datetime[1] = data[ptr++];
2439                                 int tmp_len = data[ptr++];
2440                                 descr_len -= 3;
2441                                 while( tmp_len > 0 )
2442                                 {
2443                                         memcpy(datetime+2, data+ptr, 3);
2444                                         ptr+=3;
2445                                         descr_len -= 3;
2446                                         tmp_len -= 3;
2447                                         start_times[datetime].push_back(service);
2448                                 }
2449                         }
2450                 }
2451                 else
2452                 {
2453                         *pdescr++=data+ptr;
2454                         ptr += 2;
2455                         ptr += descr_len;
2456                 }
2457         }
2458         ASSERT(pdescr <= &descriptors[65])
2459         __u8 event[4098];
2460         eit_event_struct *ev_struct = (eit_event_struct*) event;
2461         ev_struct->running_status = 0;
2462         ev_struct->free_CA_mode = 1;
2463         memcpy(event+7, duration, 3);
2464         ptr = 12;
2465         const __u8 **d=descriptors;
2466         while ( d < pdescr )
2467         {
2468                 memcpy(event+ptr, *d, ((*d)[1])+2);
2469                 ptr+=(*d++)[1];
2470                 ptr+=2;
2471         }
2472         ASSERT(ptr <= 4098);
2473         for ( std::map< date_time, std::list<uniqueEPGKey> >::iterator it(start_times.begin()); it != start_times.end(); ++it )
2474         {
2475                 time_t now = eDVBLocalTimeHandler::getInstance()->nowTime();
2476                 if ( (it->first.tm + duration_sec) < now )
2477                         continue;
2478                 memcpy(event+2, it->first.data, 5);
2479                 int bptr = ptr;
2480                 int cnt=0;
2481                 for (std::list<uniqueEPGKey>::iterator i(it->second.begin()); i != it->second.end(); ++i)
2482                 {
2483                         event[bptr++] = 0x4A;
2484                         __u8 *len = event+(bptr++);
2485                         event[bptr++] = (i->tsid & 0xFF00) >> 8;
2486                         event[bptr++] = (i->tsid & 0xFF);
2487                         event[bptr++] = (i->onid & 0xFF00) >> 8;
2488                         event[bptr++] = (i->onid & 0xFF);
2489                         event[bptr++] = (i->sid & 0xFF00) >> 8;
2490                         event[bptr++] = (i->sid & 0xFF);
2491                         event[bptr++] = 0xB0;
2492                         bptr += sprintf((char*)(event+bptr), "Option %d", ++cnt);
2493                         *len = ((event+bptr) - len)-1;
2494                 }
2495                 int llen = bptr - 12;
2496                 ev_struct->descriptors_loop_length_hi = (llen & 0xF00) >> 8;
2497                 ev_struct->descriptors_loop_length_lo = (llen & 0xFF);
2498
2499                 time_t stime = it->first.tm;
2500                 while( tmMap.find(stime) != tmMap.end() )
2501                         ++stime;
2502                 event[6] += (stime - it->first.tm);
2503                 __u16 event_id = 0;
2504                 while( evMap.find(event_id) != evMap.end() )
2505                         ++event_id;
2506                 event[0] = (event_id & 0xFF00) >> 8;
2507                 event[1] = (event_id & 0xFF);
2508                 time_event_map[it->first.tm]=std::pair<time_t, __u16>(stime, event_id);
2509                 eventData *d = new eventData( ev_struct, bptr, PRIVATE );
2510                 evMap[event_id] = d;
2511                 tmMap[stime] = d;
2512         }
2513 }
2514
2515 void eEPGCache::channel_data::startPrivateReader()
2516 {
2517         eDVBSectionFilterMask mask;
2518         memset(&mask, 0, sizeof(mask));
2519         mask.pid = m_PrivatePid;
2520         mask.flags = eDVBSectionFilterMask::rfCRC;
2521         mask.data[0] = 0xA0;
2522         mask.mask[0] = 0xFF;
2523         eDebug("[EPGC] start privatefilter for pid %04x and version %d", m_PrivatePid, m_PrevVersion);
2524         if (m_PrevVersion != -1)
2525         {
2526                 mask.data[3] = m_PrevVersion << 1;
2527                 mask.mask[3] = 0x3E;
2528                 mask.mode[3] = 0x3E;
2529         }
2530         seenPrivateSections.clear();
2531         if (!m_PrivateConn)
2532                 m_PrivateReader->connectRead(slot(*this, &eEPGCache::channel_data::readPrivateData), m_PrivateConn);
2533         m_PrivateReader->start(mask);
2534 }
2535
2536 void eEPGCache::channel_data::readPrivateData( const __u8 *data)
2537 {
2538         if ( seenPrivateSections.find(data[6]) == seenPrivateSections.end() )
2539         {
2540                 cache->privateSectionRead(m_PrivateService, data);
2541                 seenPrivateSections.insert(data[6]);
2542         }
2543         if ( seenPrivateSections.size() == (unsigned int)(data[7] + 1) )
2544         {
2545                 eDebug("[EPGC] private finished");
2546                 eDVBChannelID chid = channel->getChannelID();
2547                 int tmp = chid.original_network_id.get();
2548                 tmp |= 0x80000000; // we use highest bit as private epg indicator
2549                 chid.original_network_id = tmp;
2550                 cache->channelLastUpdated[chid] = eDVBLocalTimeHandler::getInstance()->nowTime();
2551                 m_PrevVersion = (data[5] & 0x3E) >> 1;
2552                 startPrivateReader();
2553         }
2554 }
2555
2556 #endif // ENABLE_PRIVATE_EPG
2557
2558 #ifdef ENABLE_MHW_EPG
2559 void eEPGCache::channel_data::cleanup()
2560 {
2561         m_channels.clear();
2562         m_themes.clear();
2563         m_titles.clear();
2564         m_program_ids.clear();
2565 }
2566
2567 __u8 *eEPGCache::channel_data::delimitName( __u8 *in, __u8 *out, int len_in )
2568 {
2569         // Names in mhw structs are not strings as they are not '\0' terminated.
2570         // This function converts the mhw name into a string.
2571         // Constraint: "length of out" = "length of in" + 1.
2572         int i;
2573         for ( i=0; i < len_in; i++ )
2574                 out[i] = in[i];
2575
2576         i = len_in - 1;
2577         while ( ( i >=0 ) && ( out[i] == 0x20 ) )
2578                 i--;
2579
2580         out[i+1] = 0;
2581         return out;
2582 }
2583
2584 void eEPGCache::channel_data::timeMHW2DVB( u_char hours, u_char minutes, u_char *return_time)
2585 // For time of day
2586 {
2587         return_time[0] = toBCD( hours );
2588         return_time[1] = toBCD( minutes );
2589         return_time[2] = 0;
2590 }
2591
2592 void eEPGCache::channel_data::timeMHW2DVB( int minutes, u_char *return_time)
2593 {
2594         timeMHW2DVB( int(minutes/60), minutes%60, return_time );
2595 }
2596
2597 void eEPGCache::channel_data::timeMHW2DVB( u_char day, u_char hours, u_char minutes, u_char *return_time)
2598 // For date plus time of day
2599 {
2600         // Remove offset in mhw time.
2601         __u8 local_hours = hours;
2602         if ( hours >= 16 )
2603                 local_hours -= 4;
2604         else if ( hours >= 8 )
2605                 local_hours -= 2;
2606
2607         // As far as we know all mhw time data is sent in central Europe time zone.
2608         // So, temporarily set timezone to western europe
2609         time_t dt = eDVBLocalTimeHandler::getInstance()->nowTime();
2610
2611         char *old_tz = getenv( "TZ" );
2612         putenv("TZ=CET-1CEST,M3.5.0/2,M10.5.0/3");
2613         tzset();
2614
2615         tm localnow;
2616         localtime_r(&dt, &localnow);
2617
2618         if (day == 7)
2619                 day = 0;
2620         if ( day + 1 < localnow.tm_wday )               // day + 1 to prevent old events to show for next week.
2621                 day += 7;
2622         if (local_hours <= 5)
2623                 day++;
2624
2625         dt += 3600*24*(day - localnow.tm_wday); // Shift dt to the recording date (local time zone).
2626         dt += 3600*(local_hours - localnow.tm_hour);  // Shift dt to the recording hour.
2627
2628         tm recdate;
2629         gmtime_r( &dt, &recdate );   // This will also take care of DST.
2630
2631         if ( old_tz == NULL )
2632                 unsetenv( "TZ" );
2633         else
2634                 putenv( old_tz );
2635         tzset();
2636
2637         // Calculate MJD according to annex in ETSI EN 300 468
2638         int l=0;
2639         if ( recdate.tm_mon <= 1 )      // Jan or Feb
2640                 l=1;
2641         int mjd = 14956 + recdate.tm_mday + int( (recdate.tm_year - l) * 365.25) +
2642                 int( (recdate.tm_mon + 2 + l * 12) * 30.6001);
2643
2644         return_time[0] = (mjd & 0xFF00)>>8;
2645         return_time[1] = mjd & 0xFF;
2646
2647         timeMHW2DVB( recdate.tm_hour, minutes, return_time+2 );
2648 }
2649
2650 void eEPGCache::channel_data::storeTitle(std::map<__u32, mhw_title_t>::iterator itTitle, std::string sumText, const __u8 *data)
2651 // data is borrowed from calling proc to save memory space.
2652 {
2653         __u8 name[34];
2654         // For each title a separate EIT packet will be sent to eEPGCache::sectionRead()
2655         bool isMHW2 = itTitle->second.mhw2_mjd_hi || itTitle->second.mhw2_mjd_lo ||
2656                 itTitle->second.mhw2_duration_hi || itTitle->second.mhw2_duration_lo;
2657
2658         eit_t *packet = (eit_t *) data;
2659         packet->table_id = 0x50;
2660         packet->section_syntax_indicator = 1;
2661         packet->service_id_hi = m_channels[ itTitle->second.channel_id - 1 ].channel_id_hi;
2662         packet->service_id_lo = m_channels[ itTitle->second.channel_id - 1 ].channel_id_lo;
2663         packet->version_number = 0;     // eEPGCache::sectionRead() will dig this for the moment
2664         packet->current_next_indicator = 0;
2665         packet->section_number = 0;     // eEPGCache::sectionRead() will dig this for the moment
2666         packet->last_section_number = 0;        // eEPGCache::sectionRead() will dig this for the moment
2667         packet->transport_stream_id_hi = m_channels[ itTitle->second.channel_id - 1 ].transport_stream_id_hi;
2668         packet->transport_stream_id_lo = m_channels[ itTitle->second.channel_id - 1 ].transport_stream_id_lo;
2669         packet->original_network_id_hi = m_channels[ itTitle->second.channel_id - 1 ].network_id_hi;
2670         packet->original_network_id_lo = m_channels[ itTitle->second.channel_id - 1 ].network_id_lo;
2671         packet->segment_last_section_number = 0; // eEPGCache::sectionRead() will dig this for the moment
2672         packet->segment_last_table_id = 0x50;
2673
2674         __u8 *title = isMHW2 ? ((__u8*)(itTitle->second.title))-4 : (__u8*)itTitle->second.title;
2675         std::string prog_title = (char *) delimitName( title, name, isMHW2 ? 33 : 23 );
2676         int prog_title_length = prog_title.length();
2677
2678         int packet_length = EIT_SIZE + EIT_LOOP_SIZE + EIT_SHORT_EVENT_DESCRIPTOR_SIZE +
2679                 prog_title_length + 1;
2680
2681         eit_event_t *event_data = (eit_event_t *) (data + EIT_SIZE);
2682         event_data->event_id_hi = (( itTitle->first ) >> 8 ) & 0xFF;
2683         event_data->event_id_lo = ( itTitle->first ) & 0xFF;
2684
2685         if (isMHW2)
2686         {
2687                 u_char *data = (u_char*) event_data;
2688                 data[2] = itTitle->second.mhw2_mjd_hi;
2689                 data[3] = itTitle->second.mhw2_mjd_lo;
2690                 data[4] = itTitle->second.mhw2_hours;
2691                 data[5] = itTitle->second.mhw2_minutes;
2692                 data[6] = itTitle->second.mhw2_seconds;
2693                 timeMHW2DVB( HILO(itTitle->second.mhw2_duration), data+7 );
2694         }
2695         else
2696         {
2697                 timeMHW2DVB( itTitle->second.dh.day, itTitle->second.dh.hours, itTitle->second.ms.minutes,
2698                 (u_char *) event_data + 2 );
2699                 timeMHW2DVB( HILO(itTitle->second.duration), (u_char *) event_data+7 );
2700         }
2701
2702         event_data->running_status = 0;
2703         event_data->free_CA_mode = 0;
2704         int descr_ll = EIT_SHORT_EVENT_DESCRIPTOR_SIZE + 1 + prog_title_length;
2705
2706         eit_short_event_descriptor_struct *short_event_descriptor =
2707                 (eit_short_event_descriptor_struct *) ( (u_char *) event_data + EIT_LOOP_SIZE);
2708         short_event_descriptor->descriptor_tag = EIT_SHORT_EVENT_DESCRIPTOR;
2709         short_event_descriptor->descriptor_length = EIT_SHORT_EVENT_DESCRIPTOR_SIZE +
2710                 prog_title_length - 1;
2711         short_event_descriptor->language_code_1 = 'e';
2712         short_event_descriptor->language_code_2 = 'n';
2713         short_event_descriptor->language_code_3 = 'g';
2714         short_event_descriptor->event_name_length = prog_title_length;
2715         u_char *event_name = (u_char *) short_event_descriptor + EIT_SHORT_EVENT_DESCRIPTOR_SIZE;
2716         memcpy(event_name, prog_title.c_str(), prog_title_length);
2717
2718         // Set text length
2719         event_name[prog_title_length] = 0;
2720
2721         if ( sumText.length() > 0 )
2722         // There is summary info
2723         {
2724                 unsigned int sum_length = sumText.length();
2725                 if ( sum_length + short_event_descriptor->descriptor_length <= 0xff )
2726                 // Store summary in short event descriptor
2727                 {
2728                         // Increase all relevant lengths
2729                         event_name[prog_title_length] = sum_length;
2730                         short_event_descriptor->descriptor_length += sum_length;
2731                         packet_length += sum_length;
2732                         descr_ll += sum_length;
2733                         sumText.copy( (char *) event_name+prog_title_length+1, sum_length );
2734                 }
2735                 else
2736                 // Store summary in extended event descriptors
2737                 {
2738                         int remaining_sum_length = sumText.length();
2739                         int nbr_descr = int(remaining_sum_length/247) + 1;
2740                         for ( int i=0; i < nbr_descr; i++)
2741                         // Loop once per extended event descriptor
2742                         {
2743                                 eit_extended_descriptor_struct *ext_event_descriptor = (eit_extended_descriptor_struct *) (data + packet_length);
2744                                 sum_length = remaining_sum_length > 247 ? 247 : remaining_sum_length;
2745                                 remaining_sum_length -= sum_length;
2746                                 packet_length += 8 + sum_length;
2747                                 descr_ll += 8 + sum_length;
2748
2749                                 ext_event_descriptor->descriptor_tag = EIT_EXTENDED_EVENT_DESCRIPOR;
2750                                 ext_event_descriptor->descriptor_length = sum_length + 6;
2751                                 ext_event_descriptor->descriptor_number = i;
2752                                 ext_event_descriptor->last_descriptor_number = nbr_descr - 1;
2753                                 ext_event_descriptor->iso_639_2_language_code_1 = 'e';
2754                                 ext_event_descriptor->iso_639_2_language_code_2 = 'n';
2755                                 ext_event_descriptor->iso_639_2_language_code_3 = 'g';
2756                                 u_char *the_text = (u_char *) ext_event_descriptor + 8;
2757                                 the_text[-2] = 0;
2758                                 the_text[-1] = sum_length;
2759                                 sumText.copy( (char *) the_text, sum_length, sumText.length() - sum_length - remaining_sum_length );
2760                         }
2761                 }
2762         }
2763
2764         if (!isMHW2)
2765         {
2766                 // Add content descriptor
2767                 u_char *descriptor = (u_char *) data + packet_length;
2768                 packet_length += 4;
2769                 descr_ll += 4;
2770
2771                 int content_id = 0;
2772                 std::string content_descr = (char *) delimitName( m_themes[itTitle->second.theme_id].name, name, 15 );
2773                 if ( content_descr.find( "FILM" ) != std::string::npos )
2774                         content_id = 0x10;
2775                 else if ( content_descr.find( "SPORT" ) != std::string::npos )
2776                         content_id = 0x40;
2777
2778                 descriptor[0] = 0x54;
2779                 descriptor[1] = 2;
2780                 descriptor[2] = content_id;
2781                 descriptor[3] = 0;
2782         }
2783
2784         event_data->descriptors_loop_length_hi = (descr_ll & 0xf00)>>8;
2785         event_data->descriptors_loop_length_lo = (descr_ll & 0xff);
2786
2787         packet->section_length_hi =  ((packet_length - 3)&0xf00)>>8;
2788         packet->section_length_lo =  (packet_length - 3)&0xff;
2789
2790         // Feed the data to eEPGCache::sectionRead()
2791         cache->sectionRead( data, MHW, this );
2792 }
2793
2794 void eEPGCache::channel_data::startTimeout(int msec)
2795 {
2796         m_MHWTimeoutTimer.start(msec,true);
2797         m_MHWTimeoutet=false;
2798 }
2799
2800 void eEPGCache::channel_data::startMHWReader(__u16 pid, __u8 tid)
2801 {
2802         m_MHWFilterMask.pid = pid;
2803         m_MHWFilterMask.data[0] = tid;
2804         m_MHWReader->start(m_MHWFilterMask);
2805 //      eDebug("start 0x%02x 0x%02x", pid, tid);
2806 }
2807
2808 void eEPGCache::channel_data::startMHWReader2(__u16 pid, __u8 tid, int ext)
2809 {
2810         m_MHWFilterMask2.pid = pid;
2811         m_MHWFilterMask2.data[0] = tid;
2812         if (ext != -1)
2813         {
2814                 m_MHWFilterMask2.data[1] = ext;
2815                 m_MHWFilterMask2.mask[1] = 0xFF;
2816 //              eDebug("start 0x%03x 0x%02x 0x%02x", pid, tid, ext);
2817         }
2818         else
2819         {
2820                 m_MHWFilterMask2.data[1] = 0;
2821                 m_MHWFilterMask2.mask[1] = 0;
2822 //              eDebug("start 0x%02x 0x%02x", pid, tid);
2823         }
2824         m_MHWReader2->start(m_MHWFilterMask2);
2825 }
2826
2827 void eEPGCache::channel_data::readMHWData(const __u8 *data)
2828 {
2829         if ( m_MHWReader2 )
2830                 m_MHWReader2->stop();
2831
2832         if ( state > 1 || // aborted
2833                 // have si data.. so we dont read mhw data
2834                 (haveData & (SCHEDULE|SCHEDULE_OTHER)) )
2835         {
2836                 eDebug("[EPGC] mhw aborted %d", state);
2837         }
2838         else if (m_MHWFilterMask.pid == 0xD3 && m_MHWFilterMask.data[0] == 0x91)
2839         // Channels table
2840         {
2841                 int len = ((data[1]&0xf)<<8) + data[2] - 1;
2842                 int record_size = sizeof( mhw_channel_name_t );
2843                 int nbr_records = int (len/record_size);
2844
2845                 for ( int i = 0; i < nbr_records; i++ )
2846                 {
2847                         mhw_channel_name_t *channel = (mhw_channel_name_t*) &data[4 + i*record_size];
2848                         m_channels.push_back( *channel );
2849                 }
2850                 haveData |= MHW;
2851
2852                 eDebug("[EPGC] mhw %d channels found", m_channels.size());
2853
2854                 // Channels table has been read, start reading the themes table.
2855                 startMHWReader(0xD3, 0x92);
2856                 return;
2857         }
2858         else if (m_MHWFilterMask.pid == 0xD3 && m_MHWFilterMask.data[0] == 0x92)
2859         // Themes table
2860         {
2861                 int len = ((data[1]&0xf)<<8) + data[2] - 16;
2862                 int record_size = sizeof( mhw_theme_name_t );
2863                 int nbr_records = int (len/record_size);
2864                 int idx_ptr = 0;
2865                 __u8 next_idx = (__u8) *(data + 3 + idx_ptr);
2866                 __u8 idx = 0;
2867                 __u8 sub_idx = 0;
2868                 for ( int i = 0; i < nbr_records; i++ )
2869                 {
2870                         mhw_theme_name_t *theme = (mhw_theme_name_t*) &data[19 + i*record_size];
2871                         if ( i >= next_idx )
2872                         {
2873                                 idx = (idx_ptr<<4);
2874                                 idx_ptr++;
2875                                 next_idx = (__u8) *(data + 3 + idx_ptr);
2876                                 sub_idx = 0;
2877                         }
2878                         else
2879                                 sub_idx++;
2880
2881                         m_themes[idx+sub_idx] = *theme;
2882                 }
2883                 eDebug("[EPGC] mhw %d themes found", m_themes.size());
2884                 // Themes table has been read, start reading the titles table.
2885                 startMHWReader(0xD2, 0x90);
2886                 startTimeout(4000);
2887                 return;
2888         }
2889         else if (m_MHWFilterMask.pid == 0xD2 && m_MHWFilterMask.data[0] == 0x90)
2890         // Titles table
2891         {
2892                 mhw_title_t *title = (mhw_title_t*) data;
2893
2894                 if ( title->channel_id == 0xFF )        // Separator
2895                         return; // Continue reading of the current table.
2896                 else
2897                 {
2898                         // Create unique key per title
2899                         __u32 title_id = ((title->channel_id)<<16)|((title->dh.day)<<13)|((title->dh.hours)<<8)|
2900                                 (title->ms.minutes);
2901                         __u32 program_id = ((title->program_id_hi)<<24)|((title->program_id_mh)<<16)|
2902                                 ((title->program_id_ml)<<8)|(title->program_id_lo);
2903
2904                         if ( m_titles.find( title_id ) == m_titles.end() )
2905                         {
2906                                 startTimeout(4000);
2907                                 title->mhw2_mjd_hi = 0;
2908                                 title->mhw2_mjd_lo = 0;
2909                                 title->mhw2_duration_hi = 0;
2910                                 title->mhw2_duration_lo = 0;
2911                                 m_titles[ title_id ] = *title;
2912                                 if ( (title->ms.summary_available) && (m_program_ids.find(program_id) == m_program_ids.end()) )
2913                                         // program_ids will be used to gather summaries.
2914                                         m_program_ids[ program_id ] = title_id;
2915                                 return; // Continue reading of the current table.
2916                         }
2917                         else if (!checkTimeout())
2918                                 return;
2919                 }
2920                 if ( !m_program_ids.empty())
2921                 {
2922                         // Titles table has been read, there are summaries to read.
2923                         // Start reading summaries, store corresponding titles on the fly.
2924                         startMHWReader(0xD3, 0x90);
2925                         eDebug("[EPGC] mhw %d titles(%d with summary) found",
2926                                 m_titles.size(),
2927                                 m_program_ids.size());
2928                         startTimeout(4000);
2929                         return;
2930                 }
2931         }
2932         else if (m_MHWFilterMask.pid == 0xD3 && m_MHWFilterMask.data[0] == 0x90)
2933         // Summaries table
2934         {
2935                 mhw_summary_t *summary = (mhw_summary_t*) data;
2936
2937                 // Create unique key per record
2938                 __u32 program_id = ((summary->program_id_hi)<<24)|((summary->program_id_mh)<<16)|
2939                         ((summary->program_id_ml)<<8)|(summary->program_id_lo);
2940                 int len = ((data[1]&0xf)<<8) + data[2];
2941
2942                 // ugly workaround to convert const __u8* to char*
2943                 char *tmp=0;
2944                 memcpy(&tmp, &data, sizeof(void*));
2945                 tmp[len+3] = 0; // Terminate as a string.
2946
2947                 std::map<__u32, __u32>::iterator itProgid( m_program_ids.find( program_id ) );
2948                 if ( itProgid == m_program_ids.end() )
2949                 { /*    This part is to prevent to looping forever if some summaries are not received yet.
2950                         There is a timeout of 4 sec. after the last successfully read summary. */
2951                         if (!m_program_ids.empty() && !checkTimeout())
2952                                 return; // Continue reading of the current table.
2953                 }
2954                 else
2955                 {
2956                         std::string the_text = (char *) (data + 11 + summary->nb_replays * 7);
2957
2958                         unsigned int pos=0;
2959                         while((pos = the_text.find("\r\n")) != std::string::npos)
2960                                 the_text.replace(pos, 2, " ");
2961
2962                         // Find corresponding title, store title and summary in epgcache.
2963                         std::map<__u32, mhw_title_t>::iterator itTitle( m_titles.find( itProgid->second ) );
2964                         if ( itTitle != m_titles.end() )
2965                         {
2966                                 startTimeout(4000);
2967                                 storeTitle( itTitle, the_text, data );
2968                                 m_titles.erase( itTitle );
2969                         }
2970                         m_program_ids.erase( itProgid );
2971                         if ( !m_program_ids.empty() )
2972                                 return; // Continue reading of the current table.
2973                 }
2974         }
2975         eDebug("[EPGC] mhw finished(%ld) %d summaries not found",
2976                 eDVBLocalTimeHandler::getInstance()->nowTime(),
2977                 m_program_ids.size());
2978         // Summaries have been read, titles that have summaries have been stored.
2979         // Now store titles that do not have summaries.
2980         for (std::map<__u32, mhw_title_t>::iterator itTitle(m_titles.begin()); itTitle != m_titles.end(); itTitle++)
2981                 storeTitle( itTitle, "", data );
2982         isRunning &= ~MHW;
2983         m_MHWConn=0;
2984         if ( m_MHWReader )
2985                 m_MHWReader->stop();
2986         if (haveData)
2987                 finishEPG();
2988 }
2989
2990 void eEPGCache::channel_data::readMHWData2(const __u8 *data)
2991 {
2992         int dataLen = (((data[1]&0xf) << 8) | data[2]) + 3;
2993
2994         if ( m_MHWReader )
2995                 m_MHWReader->stop();
2996
2997         if ( state > 1 || // aborted
2998                 // have si data.. so we dont read mhw data
2999                 (haveData & (eEPGCache::SCHEDULE|eEPGCache::SCHEDULE_OTHER)) )
3000         {
3001                 eDebug("[EPGC] mhw2 aborted %d", state);
3002         }
3003         else if (m_MHWFilterMask2.pid == 0x231 && m_MHWFilterMask2.data[0] == 0xC8 && m_MHWFilterMask2.data[1] == 0)
3004         // Channels table
3005         {
3006                 int num_channels = data[120];
3007                 if(dataLen > 120)
3008                 {
3009                         int ptr = 121 + 6 * num_channels;
3010                         if( dataLen > ptr )
3011                         {
3012                                 for( int chid = 0; chid < num_channels; ++chid )
3013                                 {
3014                                         ptr += ( data[ptr] & 0x0f ) + 1;
3015                                         if( dataLen < ptr )
3016                                                 goto abort;
3017                                 }
3018                         }
3019                         else
3020                                 goto abort;
3021                 }
3022                 else
3023                         goto abort;
3024                 // data seems consistent...
3025                 const __u8 *tmp = data+121;
3026                 for (int i=0; i < num_channels; ++i)
3027                 {
3028                         mhw_channel_name_t channel;
3029                         channel.transport_stream_id_hi = *(tmp++);
3030                         channel.transport_stream_id_lo = *(tmp++);
3031                         channel.channel_id_hi = *(tmp++);
3032                         channel.channel_id_lo = *(tmp++);
3033 #warning FIXME hardcoded network_id in mhw2 epg
3034                         channel.network_id_hi = 0; // hardcoded astra 19.2
3035                         channel.network_id_lo = 1;
3036                         m_channels.push_back(channel);
3037                         tmp+=2;
3038                 }
3039                 for (int i=0; i < num_channels; ++i)
3040                 {
3041                         mhw_channel_name_t &channel = m_channels[i];
3042                         int channel_name_len=*(tmp++)&0x0f;
3043                         int x=0;
3044                         for (; x < channel_name_len; ++x)
3045                                 channel.name[x]=*(tmp++);
3046                         channel.name[x+1]=0;
3047                 }
3048                 haveData |= MHW;
3049                 eDebug("[EPGC] mhw2 %d channels found", m_channels.size());
3050         }
3051         else if (m_MHWFilterMask2.pid == 0x231 && m_MHWFilterMask2.data[0] == 0xC8 && m_MHWFilterMask2.data[1] == 1)
3052         {
3053                 // Themes table
3054                 eDebug("[EPGC] mhw2 themes nyi");
3055         }
3056         else if (m_MHWFilterMask2.pid == 0x234 && m_MHWFilterMask2.data[0] == 0xe6)
3057         // Titles table
3058         {
3059                 int pos=18;
3060                 bool valid=true;
3061                 int len = ((data[1]&0xf)<<8) + data[2] - 16;
3062                 bool finish=false;
3063                 if(data[dataLen-1] != 0xff)
3064                         return;
3065                 while( pos < dataLen )
3066                 {
3067                         valid = false;
3068                         pos += 7;
3069                         if( pos < dataLen )
3070                         {
3071                                 pos += 3;
3072                                 if( pos < dataLen )
3073                                 {
3074                                         if( data[pos] > 0xc0 )
3075                                         {
3076                                                 pos += ( data[pos] - 0xc0 );
3077                                                 pos += 4;
3078                                                 if( pos < dataLen )
3079                                                 {
3080                                                         if( data[pos] == 0xff )
3081                                                         {
3082                                                                 ++pos;
3083                                                                 valid = true;
3084                                                         }
3085                                                 }
3086                                         }
3087                                 }
3088                         }
3089                         if( !valid )
3090                         {
3091                                 if (checkTimeout())
3092                                         goto start_summary;
3093                                 return;
3094                         }
3095                 }
3096                 // data seems consistent...
3097                 mhw_title_t title;
3098                 pos = 18;
3099                 while (pos < len)
3100                 {
3101                         title.channel_id = data[pos]+1;
3102                         title.program_id_ml = data[pos+1];
3103                         title.program_id_lo = data[pos+2];
3104                         title.mhw2_mjd_hi = data[pos+3];
3105                         title.mhw2_mjd_lo = data[pos+4];
3106                         title.mhw2_hours = data[pos+5];
3107                         title.mhw2_minutes = data[pos+6];
3108                         title.mhw2_seconds = data[pos+7];
3109                         int duration = ((data[pos+8] << 8)|data[pos+9]) >> 4;
3110                         title.mhw2_duration_hi = (duration&0xFF00) >> 8;
3111                         title.mhw2_duration_lo = duration&0xFF;
3112                         __u8 slen = data[pos+10] & 0x3f;
3113                         __u8 *dest = ((__u8*)title.title)-4;
3114                         memcpy(dest, &data[pos+11], slen>33 ? 33 : slen);
3115                         memset(dest+slen, 0x20, 33-slen);
3116                         pos += 11 + slen;
3117 //                      not used theme id (data[7] & 0x3f) + (data[pos] & 0x3f);
3118                         __u32 summary_id = (data[pos+1] << 8) | data[pos+2];
3119
3120                         // Create unique key per title
3121                         __u32 title_id = (title.channel_id<<16) | (title.program_id_ml<<8) | title.program_id_lo;
3122
3123 //                      eDebug("program_id: %08x, %s", program_id,
3124 //                              std::string((const char *)title.title, (int)(slen > 23 ? 23 : slen)).c_str());
3125
3126                         pos += 4;
3127
3128                         if ( m_titles.find( title_id ) == m_titles.end() )
3129                         {
3130                                 startTimeout(4000);
3131                                 m_titles[ title_id ] = title;
3132                                 if (summary_id != 0xFFFF &&  // no summary avail
3133                                         m_program_ids.find(summary_id) == m_program_ids.end())
3134                                 {
3135                                         m_program_ids[ summary_id ] = title_id;
3136                                 }
3137                         }
3138                         else
3139                         {
3140                                 if ( !checkTimeout() )
3141                                         continue;       // Continue reading of the current table.
3142                                 finish=true;
3143                                 break;
3144                         }
3145                 }
3146 start_summary:
3147                 if (finish)
3148                 {
3149                         eDebug("[EPGC] mhw2 %d titles(%d with summary) found", m_titles.size(), m_program_ids.size());
3150                         if (!m_program_ids.empty())
3151                         {
3152                                 // Titles table has been read, there are summaries to read.
3153                                 // Start reading summaries, store corresponding titles on the fly.
3154                                 startMHWReader2(0x236, 0x96);
3155                                 startTimeout(4000);
3156                                 return;
3157                         }
3158                 }
3159                 else
3160                         return;
3161         }
3162         else if (m_MHWFilterMask2.pid == 0x236 && m_MHWFilterMask2.data[0] == 0x96)
3163         // Summaries table
3164         {
3165                 int len, loop, pos, lenline;
3166                 bool valid;
3167                 valid = true;
3168                 if( dataLen > 18 )
3169                 {
3170                         loop = data[12];
3171                         pos = 13 + loop;
3172                         if( dataLen > pos )
3173                         {
3174                                 loop = data[pos] & 0x0f;
3175                                 pos += 1;
3176                                 if( dataLen > pos )
3177                                 {
3178                                         len = 0;
3179                                         for( ; loop > 0; --loop )
3180                                         {
3181                                                 if( dataLen > (pos+len) )
3182                                                 {
3183                                                         lenline = data[pos+len];
3184                                                         len += lenline + 1;
3185                                                 }
3186                                                 else
3187                                                         valid=false;
3188                                         }
3189                                 }
3190                         }
3191                 }
3192                 else if (!checkTimeout())
3193                         return;  // continue reading
3194                 if (valid && !checkTimeout())
3195                 {
3196                         // data seems consistent...
3197                         __u32 summary_id = (data[3]<<8)|data[4];
3198
3199                         // ugly workaround to convert const __u8* to char*
3200                         char *tmp=0;
3201                         memcpy(&tmp, &data, sizeof(void*));
3202
3203                         len = 0;
3204                         loop = data[12];
3205                         pos = 13 + loop;
3206                         loop = tmp[pos] & 0x0f;
3207                         pos += 1;
3208                         for( ; loop > 0; loop -- )
3209                         {
3210                                 lenline = tmp[pos+len];
3211                                 tmp[pos+len] = ' ';
3212                                 len += lenline + 1;
3213                         }
3214                         if( len > 0 )
3215                             tmp[pos+len] = 0;
3216                         else
3217                                 tmp[pos+1] = 0;
3218
3219                         std::map<__u32, __u32>::iterator itProgid( m_program_ids.find( summary_id ) );
3220                         if ( itProgid == m_program_ids.end() )
3221                         { /*    This part is to prevent to looping forever if some summaries are not received yet.
3222                                 There is a timeout of 4 sec. after the last successfully read summary. */
3223         
3224                                 if ( !m_program_ids.empty() && !checkTimeout() )
3225                                         return; // Continue reading of the current table.
3226                         }
3227                         else
3228                         {
3229                                 startTimeout(4000);
3230                                 std::string the_text = (char *) (data + pos + 1);
3231
3232                                 // Find corresponding title, store title and summary in epgcache.
3233                                 std::map<__u32, mhw_title_t>::iterator itTitle( m_titles.find( itProgid->second ) );
3234                                 if ( itTitle != m_titles.end() )
3235                                 {
3236                                         storeTitle( itTitle, the_text, data );
3237                                         m_titles.erase( itTitle );
3238                                 }
3239                                 m_program_ids.erase( itProgid );
3240                                 if ( !m_program_ids.empty() )
3241                                         return; // Continue reading of the current table.
3242                         }
3243                 }
3244         }
3245         if (isRunning & eEPGCache::MHW)
3246         {
3247                 if ( m_MHWFilterMask2.pid == 0x231 && m_MHWFilterMask2.data[0] == 0xC8 && m_MHWFilterMask2.data[1] == 0)
3248                 {
3249                         // Channels table has been read, start reading the themes table.
3250                         startMHWReader2(0x231, 0xC8, 1);
3251                         return;
3252                 }
3253                 else if ( m_MHWFilterMask2.pid == 0x231 && m_MHWFilterMask2.data[0] == 0xC8 && m_MHWFilterMask2.data[1] == 1)
3254                 {
3255                         // Themes table has been read, start reading the titles table.
3256                         startMHWReader2(0x234, 0xe6);
3257                         return;
3258                 }
3259                 else
3260                 {
3261                         // Summaries have been read, titles that have summaries have been stored.
3262                         // Now store titles that do not have summaries.
3263                         for (std::map<__u32, mhw_title_t>::iterator itTitle(m_titles.begin()); itTitle != m_titles.end(); itTitle++)
3264                                 storeTitle( itTitle, "", data );
3265                         eDebug("[EPGC] mhw2 finished(%ld) %d summaries not found",
3266                                 eDVBLocalTimeHandler::getInstance()->nowTime(),
3267                                 m_program_ids.size());
3268                 }
3269         }
3270 abort:
3271         isRunning &= ~MHW;
3272         m_MHWConn2=0;
3273         if ( m_MHWReader2 )
3274                 m_MHWReader2->stop();
3275         if (haveData)
3276                 finishEPG();
3277 }
3278 #endif