+ singleLock s(cache_lock);
+ const eventData *data=0;
+ RESULT ret = lookupEventTime(service, t, data, direction);
+ if ( !ret && data )
+ result = data->get();
+ return ret;
+}
+
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, Event *& result, int direction)
+{
+ singleLock s(cache_lock);
+ const eventData *data=0;
+ RESULT ret = lookupEventTime(service, t, data, direction);
+ if ( !ret && data )
+ result = new Event((uint8_t*)data->get());
+ return ret;
+}
+
+RESULT eEPGCache::lookupEventTime(const eServiceReference &service, time_t t, ePtr<eServiceEvent> &result, int direction)
+{
+ singleLock s(cache_lock);
+ const eventData *data=0;
+ RESULT ret = lookupEventTime(service, t, data, direction);
+ if ( !ret && data )
+ {
+ Event ev((uint8_t*)data->get());
+ result = new eServiceEvent();
+ const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
+ ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
+ }
+ return ret;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eventData *&result )
+{
+ singleLock s(cache_lock);
+ uniqueEPGKey key(handleGroup(service));
+
+ eventCache::iterator It = eventDB.find( key );
+ if ( It != eventDB.end() && !It->second.first.empty() ) // entrys cached?
+ {
+ eventMap::iterator i( It->second.first.find( event_id ));
+ if ( i != It->second.first.end() )
+ {
+ result = i->second;
+ return 0;
+ }
+ else
+ {
+ result = 0;
+ eDebug("[EPGC] event %04x not found in epgcache", event_id);
+ }
+ }
+ return -1;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, const eit_event_struct *&result)
+{
+ singleLock s(cache_lock);
+ const eventData *data=0;
+ RESULT ret = lookupEventId(service, event_id, data);
+ if ( !ret && data )
+ result = data->get();
+ return ret;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, Event *& result)
+{
+ singleLock s(cache_lock);
+ const eventData *data=0;
+ RESULT ret = lookupEventId(service, event_id, data);
+ if ( !ret && data )
+ result = new Event((uint8_t*)data->get());
+ return ret;
+}
+
+RESULT eEPGCache::lookupEventId(const eServiceReference &service, int event_id, ePtr<eServiceEvent> &result)
+{
+ singleLock s(cache_lock);
+ const eventData *data=0;
+ RESULT ret = lookupEventId(service, event_id, data);
+ if ( !ret && data )
+ {
+ Event ev((uint8_t*)data->get());
+ result = new eServiceEvent();
+ const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)service;
+ ret = result->parseFrom(&ev, (ref.getTransportStreamID().get()<<16)|ref.getOriginalNetworkID().get());
+ }
+ return ret;
+}
+
+RESULT eEPGCache::startTimeQuery(const eServiceReference &service, time_t begin, int minutes)
+{
+ eventCache::iterator It = eventDB.find(handleGroup(service));
+ if ( It != eventDB.end() && It->second.second.size() )
+ {
+ m_timemap_end = minutes != -1 ? It->second.second.upper_bound(begin+minutes*60) : It->second.second.end();
+ if ( begin != -1 )
+ {
+ m_timemap_cursor = It->second.second.lower_bound(begin);
+ if ( m_timemap_cursor != It->second.second.end() )
+ {
+ if ( m_timemap_cursor->second->getStartTime() != begin )
+ {
+ timeMap::iterator x = m_timemap_cursor;
+ --x;
+ if ( x != It->second.second.end() )
+ {
+ time_t start_time = x->second->getStartTime();
+ if ( begin > start_time && begin < (start_time+x->second->getDuration()))
+ m_timemap_cursor = x;
+ }
+ }
+ }
+ }
+ else
+ m_timemap_cursor = It->second.second.begin();
+ const eServiceReferenceDVB &ref = (const eServiceReferenceDVB&)handleGroup(service);
+ currentQueryTsidOnid = (ref.getTransportStreamID().get()<<16) | ref.getOriginalNetworkID().get();
+ return 0;
+ }
+ return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(const eventData *& result)
+{
+ if ( m_timemap_cursor != m_timemap_end )
+ {
+ result = m_timemap_cursor++->second;
+ return 0;
+ }
+ return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(const eit_event_struct *&result)
+{
+ if ( m_timemap_cursor != m_timemap_end )
+ {
+ result = m_timemap_cursor++->second->get();
+ return 0;
+ }
+ return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(Event *&result)
+{
+ if ( m_timemap_cursor != m_timemap_end )
+ {
+ result = new Event((uint8_t*)m_timemap_cursor++->second->get());
+ return 0;
+ }
+ return -1;
+}
+
+RESULT eEPGCache::getNextTimeEntry(ePtr<eServiceEvent> &result)
+{
+ if ( m_timemap_cursor != m_timemap_end )
+ {
+ Event ev((uint8_t*)m_timemap_cursor++->second->get());
+ result = new eServiceEvent();
+ return result->parseFrom(&ev, currentQueryTsidOnid);
+ }
+ return -1;
+}
+
+void fillTuple(ePyObject tuple, char *argstring, int argcount, ePyObject service, ePtr<eServiceEvent> &ptr, ePyObject nowTime, ePyObject service_name )
+{
+ ePyObject tmp;
+ int pos=0;
+ while(pos < argcount)
+ {
+ bool inc_refcount=false;
+ switch(argstring[pos])
+ {
+ case '0': // PyLong 0
+ tmp = PyLong_FromLong(0);
+ break;
+ case 'I': // Event Id
+ tmp = ptr ? PyLong_FromLong(ptr->getEventId()) : ePyObject();
+ break;
+ case 'B': // Event Begin Time
+ tmp = ptr ? PyLong_FromLong(ptr->getBeginTime()) : ePyObject();
+ break;
+ case 'D': // Event Duration
+ tmp = ptr ? PyLong_FromLong(ptr->getDuration()) : ePyObject();
+ break;
+ case 'T': // Event Title
+ tmp = ptr ? PyString_FromString(ptr->getEventName().c_str()) : ePyObject();
+ break;
+ case 'S': // Event Short Description
+ tmp = ptr ? PyString_FromString(ptr->getShortDescription().c_str()) : ePyObject();
+ break;
+ case 'E': // Event Extended Description
+ tmp = ptr ? PyString_FromString(ptr->getExtendedDescription().c_str()) : ePyObject();
+ break;
+ case 'C': // Current Time
+ tmp = nowTime;
+ inc_refcount = true;
+ break;
+ case 'R': // service reference string
+ tmp = service;
+ inc_refcount = true;
+ break;
+ case 'N': // service name
+ tmp = service_name;
+ inc_refcount = true;
+ }
+ if (!tmp)
+ {
+ tmp = Py_None;
+ inc_refcount = true;
+ }
+ if (inc_refcount)
+ Py_INCREF(tmp);
+ PyTuple_SET_ITEM(tuple, pos++, tmp);
+ }
+}
+
+int handleEvent(ePtr<eServiceEvent> &ptr, ePyObject dest_list, char* argstring, int argcount, ePyObject service, ePyObject nowTime, ePyObject service_name, ePyObject convertFunc, ePyObject convertFuncArgs)
+{
+ if (convertFunc)
+ {
+ fillTuple(convertFuncArgs, argstring, argcount, service, ptr, nowTime, service_name);
+ ePyObject result = PyObject_CallObject(convertFunc, convertFuncArgs);
+ if (result)
+ {
+ if (service_name)
+ Py_DECREF(service_name);
+ if (nowTime)
+ Py_DECREF(nowTime);
+ Py_DECREF(convertFuncArgs);
+ Py_DECREF(dest_list);
+ PyErr_SetString(PyExc_StandardError,
+ "error in convertFunc execute");
+ eDebug("error in convertFunc execute");
+ return -1;
+ }
+ PyList_Append(dest_list, result);
+ Py_DECREF(result);
+ }
+ else
+ {
+ ePyObject tuple = PyTuple_New(argcount);
+ fillTuple(tuple, argstring, argcount, service, ptr, nowTime, service_name);
+ PyList_Append(dest_list, tuple);
+ Py_DECREF(tuple);
+ }
+ return 0;
+}
+
+// here we get a python list
+// the first entry in the list is a python string to specify the format of the returned tuples (in a list)
+// 0 = PyLong(0)
+// I = Event Id
+// B = Event Begin Time
+// D = Event Duration
+// T = Event Title
+// S = Event Short Description
+// E = Event Extended Description
+// C = Current Time
+// R = Service Reference
+// N = Service Name
+// then for each service follows a tuple
+// first tuple entry is the servicereference (as string... use the ref.toString() function)
+// the second is the type of query
+// 2 = event_id
+// -1 = event before given start_time
+// 0 = event intersects given start_time
+// +1 = event after given start_time
+// the third
+// when type is eventid it is the event_id
+// when type is time then it is the start_time ( 0 for now_time )
+// the fourth is the end_time .. ( optional .. for query all events in time range)
+
+PyObject *eEPGCache::lookupEvent(ePyObject list, ePyObject convertFunc)
+{
+ ePyObject convertFuncArgs;
+ int argcount=0;
+ char *argstring=NULL;
+ if (!PyList_Check(list))
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("no list");
+ return NULL;
+ }
+ int listIt=0;
+ int listSize=PyList_Size(list);
+ if (!listSize)
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "not params given");
+ eDebug("not params given");
+ return NULL;
+ }
+ else
+ {
+ ePyObject argv=PyList_GET_ITEM(list, 0); // borrowed reference!
+ if (PyString_Check(argv))
+ {
+ argstring = PyString_AS_STRING(argv);
+ ++listIt;
+ }
+ else
+ argstring = "I"; // just event id as default
+ argcount = strlen(argstring);
+// eDebug("have %d args('%s')", argcount, argstring);
+ }
+ if (convertFunc)
+ {
+ if (!PyCallable_Check(convertFunc))
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "convertFunc must be callable");
+ eDebug("convertFunc is not callable");
+ return NULL;
+ }
+ convertFuncArgs = PyTuple_New(argcount);
+ }
+
+ ePyObject nowTime = strchr(argstring, 'C') ?
+ PyLong_FromLong(eDVBLocalTimeHandler::getInstance()->nowTime()) :
+ ePyObject();
+
+ bool must_get_service_name = strchr(argstring, 'N') ? true : false;
+
+ // create dest list
+ ePyObject dest_list=PyList_New(0);
+ while(listSize > listIt)
+ {
+ ePyObject item=PyList_GET_ITEM(list, listIt++); // borrowed reference!
+ if (PyTuple_Check(item))
+ {
+ bool service_changed=false;
+ int type=0;
+ long event_id=-1;
+ time_t stime=-1;
+ int minutes=0;
+ int tupleSize=PyTuple_Size(item);
+ int tupleIt=0;
+ ePyObject service;
+ while(tupleSize > tupleIt) // parse query args
+ {
+ ePyObject entry=PyTuple_GET_ITEM(item, tupleIt); // borrowed reference!
+ switch(tupleIt++)
+ {
+ case 0:
+ {
+ if (!PyString_Check(entry))
+ {
+ eDebug("tuple entry 0 is no a string");
+ goto skip_entry;
+ }
+ service = entry;
+ break;
+ }
+ case 1:
+ type=PyInt_AsLong(entry);
+ if (type < -1 || type > 2)
+ {
+ eDebug("unknown type %d", type);
+ goto skip_entry;
+ }
+ break;
+ case 2:
+ event_id=stime=PyInt_AsLong(entry);
+ break;
+ case 3:
+ minutes=PyInt_AsLong(entry);
+ break;
+ default:
+ eDebug("unneeded extra argument");
+ break;
+ }
+ }
+ eServiceReference ref(handleGroup(eServiceReference(PyString_AS_STRING(service))));
+ if (ref.type != eServiceReference::idDVB)
+ {
+ eDebug("service reference for epg query is not valid");
+ continue;
+ }
+
+ // redirect subservice querys to parent service
+ eServiceReferenceDVB &dvb_ref = (eServiceReferenceDVB&)ref;
+ if (dvb_ref.getParentTransportStreamID().get()) // linkage subservice
+ {
+ eServiceCenterPtr service_center;
+ if (!eServiceCenter::getPrivInstance(service_center))
+ {
+ dvb_ref.setTransportStreamID( dvb_ref.getParentTransportStreamID() );
+ dvb_ref.setServiceID( dvb_ref.getParentServiceID() );
+ dvb_ref.setParentTransportStreamID(eTransportStreamID(0));
+ dvb_ref.setParentServiceID(eServiceID(0));
+ dvb_ref.name="";
+ service = PyString_FromString(dvb_ref.toString().c_str());
+ service_changed = true;
+ }
+ }
+
+ ePyObject service_name;
+ if (must_get_service_name)
+ {
+ ePtr<iStaticServiceInformation> sptr;
+ eServiceCenterPtr service_center;
+ eServiceCenter::getPrivInstance(service_center);
+ if (service_center)
+ {
+ service_center->info(ref, sptr);
+ if (sptr)
+ {
+ std::string name;
+ sptr->getName(ref, name);
+ if (name.length())
+ service_name = PyString_FromString(name.c_str());
+ }
+ }
+ if (!service_name)
+ service_name = PyString_FromString("<n/a>");
+ }
+ if (minutes)
+ {
+ Lock();
+ if (!startTimeQuery(ref, stime, minutes))
+ {
+ ePtr<eServiceEvent> ptr;
+ while (!getNextTimeEntry(ptr))
+ {
+ if (handleEvent(ptr, dest_list, argstring, argcount, service, nowTime, service_name, convertFunc, convertFuncArgs))
+ {
+ Unlock();
+ return 0; // error
+ }
+ }
+ }
+ Unlock();
+ }
+ else
+ {
+ ePtr<eServiceEvent> ptr;
+ if (stime)
+ {
+ if (type == 2)
+ lookupEventId(ref, event_id, ptr);
+ else
+ lookupEventTime(ref, stime, ptr, type);
+ }
+ if (handleEvent(ptr, dest_list, argstring, argcount, service, nowTime, service_name, convertFunc, convertFuncArgs))
+ return 0; // error
+ }
+ if (service_changed)
+ Py_DECREF(service);
+ if (service_name)
+ Py_DECREF(service_name);
+ }
+skip_entry:
+ ;
+ }
+ if (convertFuncArgs)
+ Py_DECREF(convertFuncArgs);
+ if (nowTime)
+ Py_DECREF(nowTime);
+ return dest_list;
+}
+
+void fillTuple2(ePyObject tuple, const char *argstring, int argcount, eventData *evData, ePtr<eServiceEvent> &ptr, ePyObject service_name, ePyObject service_reference)
+{
+ ePyObject tmp;
+ int pos=0;
+ while(pos < argcount)
+ {
+ bool inc_refcount=false;
+ switch(argstring[pos])
+ {
+ case '0': // PyLong 0
+ tmp = PyLong_FromLong(0);
+ break;
+ case 'I': // Event Id
+ tmp = PyLong_FromLong(evData->getEventID());
+ break;
+ case 'B': // Event Begin Time
+ if (ptr)
+ tmp = ptr ? PyLong_FromLong(ptr->getBeginTime()) : ePyObject();
+ else
+ tmp = PyLong_FromLong(evData->getStartTime());
+ break;
+ case 'D': // Event Duration
+ if (ptr)
+ tmp = ptr ? PyLong_FromLong(ptr->getDuration()) : ePyObject();
+ else
+ tmp = PyLong_FromLong(evData->getDuration());
+ break;
+ case 'T': // Event Title
+ tmp = ptr ? PyString_FromString(ptr->getEventName().c_str()) : ePyObject();
+ break;
+ case 'S': // Event Short Description
+ tmp = ptr ? PyString_FromString(ptr->getShortDescription().c_str()) : ePyObject();
+ break;
+ case 'E': // Event Extended Description
+ tmp = ptr ? PyString_FromString(ptr->getExtendedDescription().c_str()) : ePyObject();
+ break;
+ case 'R': // service reference string
+ tmp = service_reference;
+ inc_refcount = true;
+ break;
+ case 'N': // service name
+ tmp = service_name;
+ inc_refcount = true;
+ break;
+ }
+ if (!tmp)
+ {
+ tmp = Py_None;
+ inc_refcount = true;
+ }
+ if (inc_refcount)
+ Py_INCREF(tmp);
+ PyTuple_SET_ITEM(tuple, pos++, tmp);
+ }
+}
+
+// here we get a python tuple
+// the first entry in the tuple is a python string to specify the format of the returned tuples (in a list)
+// I = Event Id
+// B = Event Begin Time
+// D = Event Duration
+// T = Event Title
+// S = Event Short Description
+// E = Event Extended Description
+// R = Service Reference
+// N = Service Name
+// the second tuple entry is the MAX matches value
+// the third tuple entry is the type of query
+// 0 = search for similar broadcastings (SIMILAR_BROADCASTINGS_SEARCH)
+// 1 = search events with exactly title name (EXAKT_TITLE_SEARCH)
+// 2 = search events with text in title name (PARTIAL_TITLE_SEARCH)
+// when type is 0 (SIMILAR_BROADCASTINGS_SEARCH)
+// the fourth is the servicereference string
+// the fifth is the eventid
+// when type is 1 or 2 (EXAKT_TITLE_SEARCH or PARTIAL_TITLE_SEARCH)
+// the fourth is the search text
+// the fifth is
+// 0 = case sensitive (CASE_CHECK)
+// 1 = case insensitive (NO_CASECHECK)
+
+PyObject *eEPGCache::search(ePyObject arg)
+{
+ ePyObject ret;
+ int descridx = -1;
+ __u32 descr[512];
+ int eventid = -1;
+ const char *argstring=0;
+ char *refstr=0;
+ int argcount=0;
+ int querytype=-1;
+ bool needServiceEvent=false;
+ int maxmatches=0;
+
+ if (PyTuple_Check(arg))
+ {
+ int tuplesize=PyTuple_Size(arg);
+ if (tuplesize > 0)
+ {
+ ePyObject obj = PyTuple_GET_ITEM(arg,0);
+ if (PyString_Check(obj))
+ {
+ argcount = PyString_GET_SIZE(obj);
+ argstring = PyString_AS_STRING(obj);
+ for (int i=0; i < argcount; ++i)
+ switch(argstring[i])
+ {
+ case 'S':
+ case 'E':
+ case 'T':
+ needServiceEvent=true;
+ default:
+ break;
+ }
+ }
+ else
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("tuple arg 0 is not a string");
+ return NULL;
+ }
+ }
+ if (tuplesize > 1)
+ maxmatches = PyLong_AsLong(PyTuple_GET_ITEM(arg, 1));
+ if (tuplesize > 2)
+ {
+ querytype = PyLong_AsLong(PyTuple_GET_ITEM(arg, 2));
+ if (tuplesize > 4 && querytype == 0)
+ {
+ ePyObject obj = PyTuple_GET_ITEM(arg, 3);
+ if (PyString_Check(obj))
+ {
+ refstr = PyString_AS_STRING(obj);
+ eServiceReferenceDVB ref(refstr);
+ if (ref.valid())
+ {
+ eventid = PyLong_AsLong(PyTuple_GET_ITEM(arg, 4));
+ singleLock s(cache_lock);
+ const eventData *evData = 0;
+ lookupEventId(ref, eventid, evData);
+ if (evData)
+ {
+ __u8 *data = evData->EITdata;
+ int tmp = evData->ByteSize-12;
+ __u32 *p = (__u32*)(data+12);
+ // search short and extended event descriptors
+ while(tmp>0)
+ {
+ __u32 crc = *p++;
+ descriptorMap::iterator it =
+ eventData::descriptors.find(crc);
+ if (it != eventData::descriptors.end())
+ {
+ __u8 *descr_data = it->second.second;
+ switch(descr_data[0])
+ {
+ case 0x4D ... 0x4E:
+ descr[++descridx]=crc;
+ default:
+ break;
+ }
+ }
+ tmp-=4;
+ }
+ }
+ if (descridx<0)
+ eDebug("event not found");
+ }
+ else
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("tuple arg 4 is not a valid service reference string");
+ return NULL;
+ }
+ }
+ else
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("tuple arg 4 is not a string");
+ return NULL;
+ }
+ }
+ else if (tuplesize > 4 && (querytype == 1 || querytype == 2) )
+ {
+ ePyObject obj = PyTuple_GET_ITEM(arg, 3);
+ if (PyString_Check(obj))
+ {
+ int casetype = PyLong_AsLong(PyTuple_GET_ITEM(arg, 4));
+ const char *str = PyString_AS_STRING(obj);
+ int textlen = PyString_GET_SIZE(obj);
+ if (querytype == 1)
+ eDebug("lookup for events with '%s' as title(%s)", str, casetype?"ignore case":"case sensitive");
+ else
+ eDebug("lookup for events with '%s' in title(%s)", str, casetype?"ignore case":"case sensitive");
+ singleLock s(cache_lock);
+ for (descriptorMap::iterator it(eventData::descriptors.begin());
+ it != eventData::descriptors.end() && descridx < 511; ++it)
+ {
+ __u8 *data = it->second.second;
+ if ( data[0] == 0x4D ) // short event descriptor
+ {
+ int title_len = data[5];
+ if ( querytype == 1 )
+ {
+ if (title_len > textlen)
+ continue;
+ else if (title_len < textlen)
+ continue;
+ if ( casetype )
+ {
+ if ( !strncasecmp((const char*)data+6, str, title_len) )
+ {
+// std::string s((const char*)data+6, title_len);
+// eDebug("match1 %s %s", str, s.c_str() );
+ descr[++descridx] = it->first;
+ }
+ }
+ else if ( !strncmp((const char*)data+6, str, title_len) )
+ {
+// std::string s((const char*)data+6, title_len);
+// eDebug("match2 %s %s", str, s.c_str() );
+ descr[++descridx] = it->first;
+ }
+ }
+ else
+ {
+ int idx=0;
+ while((title_len-idx) >= textlen)
+ {
+ if (casetype)
+ {
+ if (!strncasecmp((const char*)data+6+idx, str, textlen) )
+ {
+ descr[++descridx] = it->first;
+// std::string s((const char*)data+6, title_len);
+// eDebug("match 3 %s %s", str, s.c_str() );
+ break;
+ }
+ else if (!strncmp((const char*)data+6+idx, str, textlen) )
+ {
+ descr[++descridx] = it->first;
+// std::string s((const char*)data+6, title_len);
+// eDebug("match 4 %s %s", str, s.c_str() );
+ break;
+ }
+ }
+ ++idx;
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("tuple arg 4 is not a string");
+ return NULL;
+ }
+ }
+ else
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("tuple arg 3(%d) is not a known querytype(0, 1, 2)", querytype);
+ return NULL;
+ }
+ }
+ else
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("not enough args in tuple");
+ return NULL;
+ }
+ }
+ else
+ {
+ PyErr_SetString(PyExc_StandardError,
+ "type error");
+ eDebug("arg 0 is not a tuple");
+ return NULL;
+ }
+
+ if (descridx > -1)
+ {
+ int maxcount=maxmatches;
+ eServiceReferenceDVB ref(refstr?(const eServiceReferenceDVB&)handleGroup(eServiceReference(refstr)):eServiceReferenceDVB(""));
+ // ref is only valid in SIMILAR_BROADCASTING_SEARCH
+ // in this case we start searching with the base service
+ bool first = ref.valid() ? true : false;
+ singleLock s(cache_lock);
+ eventCache::iterator cit(ref.valid() ? eventDB.find(ref) : eventDB.begin());
+ while(cit != eventDB.end() && maxcount)
+ {
+ if ( ref.valid() && !first && cit->first == ref )
+ {
+ // do not scan base service twice ( only in SIMILAR BROADCASTING SEARCH )
+ ++cit;
+ continue;
+ }
+ ePyObject service_name;
+ ePyObject service_reference;
+ timeMap &evmap = cit->second.second;
+ // check all events
+ for (timeMap::iterator evit(evmap.begin()); evit != evmap.end() && maxcount; ++evit)
+ {
+ int evid = evit->second->getEventID();
+ if ( evid == eventid)
+ continue;
+ __u8 *data = evit->second->EITdata;
+ int tmp = evit->second->ByteSize-12;
+ __u32 *p = (__u32*)(data+12);
+ // check if any of our descriptor used by this event
+ int cnt=-1;
+ while(tmp>0)
+ {
+ __u32 crc32 = *p++;
+ for ( int i=0; i <= descridx; ++i)
+ {
+ if (descr[i] == crc32) // found...
+ ++cnt;
+ }
+ tmp-=4;
+ }
+ if ( (querytype == 0 && cnt == descridx) ||
+ ((querytype == 1 || querytype == 2) && cnt != -1) )
+ {
+ const uniqueEPGKey &service = cit->first;
+ eServiceReference ref =
+ eDVBDB::getInstance()->searchReference(service.tsid, service.onid, service.sid);
+ if (ref.valid())
+ {
+ // create servive event
+ ePtr<eServiceEvent> ptr;
+ if (needServiceEvent)
+ {
+ lookupEventId(ref, evid, ptr);
+ if (!ptr)
+ eDebug("event not found !!!!!!!!!!!");
+ }
+ // create service name
+ if (!service_name && strchr(argstring,'N'))
+ {
+ ePtr<iStaticServiceInformation> sptr;
+ eServiceCenterPtr service_center;
+ eServiceCenter::getPrivInstance(service_center);
+ if (service_center)
+ {
+ service_center->info(ref, sptr);
+ if (sptr)
+ {
+ std::string name;
+ sptr->getName(ref, name);
+ if (name.length())
+ service_name = PyString_FromString(name.c_str());
+ }
+ }
+ if (!service_name)
+ service_name = PyString_FromString("<n/a>");
+ }
+ // create servicereference string
+ if (!service_reference && strchr(argstring,'R'))
+ service_reference = PyString_FromString(ref.toString().c_str());
+ // create list
+ if (!ret)
+ ret = PyList_New(0);
+ // create tuple
+ ePyObject tuple = PyTuple_New(argcount);
+ // fill tuple
+ fillTuple2(tuple, argstring, argcount, evit->second, ptr, service_name, service_reference);
+ PyList_Append(ret, tuple);
+ Py_DECREF(tuple);
+ --maxcount;
+ }
+ }
+ }
+ if (service_name)
+ Py_DECREF(service_name);
+ if (service_reference)
+ Py_DECREF(service_reference);
+ if (first)
+ {
+ // now start at first service in epgcache database ( only in SIMILAR BROADCASTING SEARCH )
+ first=false;
+ cit=eventDB.begin();
+ }
+ else
+ ++cit;
+ }
+ }
+
+ if (!ret)
+ {
+ Py_INCREF(Py_None);
+ ret=Py_None;
+ }
+
+ return ret;
+}
+
+#ifdef ENABLE_PRIVATE_EPG
+#include <dvbsi++/descriptor_tag.h>
+#include <dvbsi++/unknown_descriptor.h>
+#include <dvbsi++/private_data_specifier_descriptor.h>
+
+void eEPGCache::PMTready(eDVBServicePMTHandler *pmthandler)
+{
+ ePtr<eTable<ProgramMapSection> > ptr;
+ if (!pmthandler->getPMT(ptr) && ptr)
+ {
+ std::vector<ProgramMapSection*>::const_iterator i;
+ for (i = ptr->getSections().begin(); i != ptr->getSections().end(); ++i)
+ {
+ const ProgramMapSection &pmt = **i;
+
+ ElementaryStreamInfoConstIterator es;
+ for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
+ {
+ int tmp=0;
+ switch ((*es)->getType())
+ {
+ case 0x05: // private
+ for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
+ desc != (*es)->getDescriptors()->end(); ++desc)
+ {
+ switch ((*desc)->getTag())
+ {
+ case PRIVATE_DATA_SPECIFIER_DESCRIPTOR:
+ if (((PrivateDataSpecifierDescriptor*)(*desc))->getPrivateDataSpecifier() == 190)
+ tmp |= 1;
+ break;
+ case 0x90:
+ {
+ UnknownDescriptor *descr = (UnknownDescriptor*)*desc;
+ int descr_len = descr->getLength();
+ if (descr_len == 4)
+ {
+ uint8_t data[descr_len+2];
+ descr->writeToBuffer(data);
+ if ( !data[2] && !data[3] && data[4] == 0xFF && data[5] == 0xFF )
+ tmp |= 2;
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ }
+ default:
+ break;
+ }
+ if (tmp==3)
+ {
+ eServiceReferenceDVB ref;
+ if (!pmthandler->getServiceReference(ref))
+ {
+ int pid = (*es)->getPid();
+ messages.send(Message(Message::got_private_pid, ref, pid));
+ return;
+ }
+ }
+ }
+ }
+ }
+ else
+ eDebug("PMTready but no pmt!!");
+}
+
+struct date_time
+{
+ __u8 data[5];
+ time_t tm;
+ date_time( const date_time &a )
+ {
+ memcpy(data, a.data, 5);
+ tm = a.tm;
+ }
+ date_time( const __u8 data[5])
+ {
+ memcpy(this->data, data, 5);
+ tm = parseDVBtime(data[0], data[1], data[2], data[3], data[4]);
+ }
+ date_time()