1 #include <lib/dvb/dvbtime.h>
2 #include <lib/dvb/dvb.h>
10 // defines for DM7000 / DM7020
11 #define FP_IOCTL_SET_RTC 0x101
12 #define FP_IOCTL_GET_RTC 0x102
14 static time_t prev_time;
16 void setRTC(time_t time)
18 int fd = open("/dev/dbox/fp0", O_RDWR);
21 if ( ::ioctl(fd, FP_IOCTL_SET_RTC, (void*)&time ) < 0 )
22 eDebug("FP_IOCTL_SET_RTC failed(%m)");
32 int fd = open("/dev/dbox/fp0", O_RDWR);
35 if ( ::ioctl(fd, FP_IOCTL_GET_RTC, (void*)&rtc_time ) < 0 )
36 eDebug("FP_IOCTL_GET_RTC failed(%m)");
39 return rtc_time != prev_time ? rtc_time : 0;
42 time_t parseDVBtime(__u8 t1, __u8 t2, __u8 t3, __u8 t4, __u8 t5)
47 t.tm_hour=fromBCD(t3);
51 t.tm_year = (int) ((mjd - 15078.2) / 365.25);
52 t.tm_mon = (int) ((mjd - 14956.1 - (int)(t.tm_year * 365.25)) / 30.6001);
53 t.tm_mday = (int) (mjd - 14956 - (int)(t.tm_year * 365.25) - (int)(t.tm_mon * 30.6001));
54 k = (t.tm_mon == 14 || t.tm_mon == 15) ? 1 : 0;
55 t.tm_year = t.tm_year + k;
56 t.tm_mon = t.tm_mon - 1 - k * 12;
65 TDT::TDT(eDVBChannel *chan, int update_count)
66 :chan(chan), update_count(update_count)
68 CONNECT(tableReady, TDT::ready);
69 CONNECT(m_interval_timer.timeout, TDT::start);
71 chan->getDemux(demux, 0);
74 void TDT::ready(int error)
76 eDVBLocalTimeHandler::getInstance()->updateTime(error, chan, ++update_count);
79 int TDT::createTable(unsigned int nr, const __u8 *data, unsigned int max)
81 if ( data && data[0] == 0x70 || data[0] == 0x73 )
83 int length = ((data[1] & 0x0F) << 8) | data[2];
86 time_t tptime = parseDVBtime(data[3], data[4], data[5], data[6], data[7]);
87 if (tptime && tptime != -1)
88 eDVBLocalTimeHandler::getInstance()->updateTime(tptime, chan, update_count);
101 spec.pid = TimeAndDateSection::PID;
102 spec.tid = TimeAndDateSection::TID;
103 spec.tid_mask = 0xFC;
104 spec.timeout = TimeAndDateSection::TIMEOUT;
105 spec.flags= eDVBTableSpec::tfAnyVersion |
106 eDVBTableSpec::tfHaveTID |
107 eDVBTableSpec::tfHaveTIDMask |
108 eDVBTableSpec::tfHaveTimeout;
110 eGTable::start( demux, spec );
114 void TDT::startTimer( int interval )
116 m_interval_timer.start(interval, true);
119 eDVBLocalTimeHandler *eDVBLocalTimeHandler::instance;
120 DEFINE_REF(eDVBLocalTimeHandler);
122 eDVBLocalTimeHandler::eDVBLocalTimeHandler()
123 :m_time_ready(false), m_time_difference(0)
127 ePtr<eDVBResourceManager> res_mgr;
128 eDVBResourceManager::getInstance(res_mgr);
130 eDebug("[eDVBLocalTimerHandler] no resource manager !!!!!!!");
133 res_mgr->connectChannelAdded(slot(*this,&eDVBLocalTimeHandler::DVBChannelAdded), m_chanAddedConn);
134 time_t now = time(0);
135 if ( now < 1072224000 ) // 01.01.2004
136 eDebug("RTC not ready... wait for transponder time");
137 else // inform all who's waiting for valid system time..
139 eDebug("Use valid Linux Time :) (RTC?)");
141 /*emit*/ m_timeUpdated();
146 eDVBLocalTimeHandler::~eDVBLocalTimeHandler()
149 for (std::map<iDVBChannel*, channel_data>::iterator it=m_knownChannels.begin(); it != m_knownChannels.end(); ++it)
150 delete it->second.tdt;
153 eDebug("set RTC to previous valid time");
158 void eDVBLocalTimeHandler::readTimeOffsetData( const char* filename )
160 m_timeOffsetMap.clear();
161 FILE *f=fopen(filename, "r");
168 if (!fgets( line, 256, f ))
170 if (strstr(line, "Transponder UTC Time Offsets\n"))
172 int dvbnamespace,tsid,onid,offs;
173 if ( sscanf( line, "%08x,%04x,%04x:%d\n",&dvbnamespace,&tsid,&onid,&offs ) == 4 )
174 m_timeOffsetMap[eDVBChannelID(dvbnamespace,tsid,onid)]=offs;
179 void eDVBLocalTimeHandler::writeTimeOffsetData( const char* filename )
181 FILE *f=fopen(filename, "w+");
184 fprintf(f, "Transponder UTC Time Offsets\n");
185 for ( std::map<eDVBChannelID,int>::iterator it ( m_timeOffsetMap.begin() ); it != m_timeOffsetMap.end(); ++it )
186 fprintf(f, "%08x,%04x,%04x:%d\n",
187 it->first.dvbnamespace.get(),
188 it->first.transport_stream_id.get(), it->first.original_network_id.get(), it->second );
193 void eDVBLocalTimeHandler::updateTime( time_t tp_time, eDVBChannel *chan, int update_count )
195 bool restart_tdt = false;
198 else if (tp_time == -1)
201 /*if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 ||
202 ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
203 && eSystemInfo::getInstance()->hasStandbyWakeupTimer() ) ) TODO !!!!!!! */
205 eDebug("[eDVBLocalTimerHandler] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
206 time_t rtc_time = getRTC();
207 if ( rtc_time ) // RTC Ready?
210 localtime_r(&rtc_time, &now);
211 eDebug("[eDVBLocalTimerHandler] RTC time is %02d:%02d:%02d",
215 time_t linuxTime=time(0);
216 time_t nowTime=linuxTime+m_time_difference;
217 localtime_r(&nowTime, &now);
218 eDebug("[eDVBLocalTimerHandler] Receiver time is %02d:%02d:%02d",
222 m_time_difference = rtc_time - linuxTime;
223 eDebug("[eDVBLocalTimerHandler] RTC to Receiver time difference is %ld seconds", nowTime - rtc_time );
224 if ( m_time_difference )
226 eDebug("[eDVBLocalTimerHandler] set Linux Time to RTC Time");
228 gettimeofday(&tnow,0);
229 tnow.tv_sec=rtc_time;
230 settimeofday(&tnow,0);
233 else if ( !m_time_difference )
234 eDebug("[eDVBLocalTimerHandler] no change needed");
236 eDebug("[eDVBLocalTimerHandler] set to RTC time");
237 /*emit*/ m_timeUpdated();
240 eDebug("[eDVBLocalTimerHandler] shit RTC not ready :(");
245 std::map< eDVBChannelID, int >::iterator it( m_timeOffsetMap.find( chan->getChannelID() ) );
247 // current linux time
248 time_t linuxTime = time(0);
250 // current enigma time
251 time_t nowTime=linuxTime+m_time_difference;
253 // difference between current enigma time and transponder time
254 int enigma_diff = tp_time-nowTime;
258 bool updated = m_time_ready;
260 if ( m_time_ready ) // ref time ready?
262 // difference between reference time (current enigma time)
263 // and the transponder time
264 eDebug("[eDVBLocalTimerHandler] diff is %d", enigma_diff);
265 if ( abs(enigma_diff) < 120 )
267 eDebug("[eDVBLocalTimerHandler] diff < 120 .. use Transponder Time");
268 m_timeOffsetMap[chan->getChannelID()] = 0;
269 new_diff = enigma_diff;
271 else if ( it != m_timeOffsetMap.end() ) // correction saved?
273 eDebug("[eDVBLocalTimerHandler] we have correction %d", it->second);
274 time_t CorrectedTpTime = tp_time+it->second;
275 int ddiff = CorrectedTpTime-nowTime;
276 eDebug("[eDVBLocalTimerHandler] diff after add correction is %d", ddiff);
277 if ( abs(it->second) < 300 ) // stored correction < 5 min
279 eDebug("[eDVBLocalTimerHandler] use stored correction(<5 min)");
285 m_timeOffsetMap[chan->getChannelID()] = rtc-tp_time;
286 new_diff = rtc-nowTime; // set enigma time to rtc
287 eDebug("[eDVBLocalTimerHandler] update stored correction to %ld (calced against RTC time)", rtc-tp_time );
289 else if ( abs(ddiff) <= 120 )
291 // with stored correction calced time difference is lower 2 min
292 // this don't help when a transponder have a clock running to slow or to fast
293 // then its better to have a DM7020 with always running RTC
294 eDebug("[eDVBLocalTimerHandler] use stored correction(corr < 2 min)");
297 else // big change in calced correction.. hold current time and update correction
299 eDebug("[eDVBLocalTimerHandler] update stored correction to %d", -enigma_diff);
300 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
305 eDebug("[eDVBLocalTimerHandler] no correction found... store calced correction(%d)",-enigma_diff);
306 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
309 else // no time setted yet
311 if ( it != m_timeOffsetMap.end() )
313 enigma_diff += it->second;
314 eDebug("[eDVBLocalTimerHandler] we have correction (%d)... use", it->second );
317 eDebug("[eDVBLocalTimerHandler] dont have correction.. set Transponder Diff");
318 new_diff=enigma_diff;
322 time_t t = nowTime+new_diff;
323 m_last_tp_time_difference=tp_time-t;
326 updated) // overrride this check on first received TDT
328 eDebug("[eDVBLocalTimerHandler] not changed");
334 // set rtc to calced transponder time when the first tdt is received on this
337 eDebug("[eDVBLocalTimerHandler] update RTC");
341 if (abs(getRTC() - t) > 60)
343 eDebug("[eDVBLocalTimerHandler] difference between new linux time and RTC time is > 60 sec... transponder time looks not ok... use rtc time");
347 eDebug("[eDVBLocalTimerHandler] difference between linux time and RTC time is < 60 sec... so the transponder time looks ok");
350 eDebug("[eDVBLocalTimerHandler] no RTC available :(");
353 localtime_r(&t, &now);
354 eDebug("[eDVBLocalTimerHandler] time update to %02d:%02d:%02d",
359 m_time_difference = t - linuxTime; // calc our new linux_time -> enigma_time correction
360 eDebug("[eDVBLocalTimerHandler] m_time_difference is %d", m_time_difference );
362 if ( m_time_difference )
364 eDebug("[eDVBLocalTimerHandler] set Linux Time");
366 gettimeofday(&tnow,0);
368 settimeofday(&tnow,0);
372 /*emit*/ m_timeUpdated();
377 std::map<iDVBChannel*, channel_data>::iterator it =
378 m_knownChannels.find(chan);
379 if ( it != m_knownChannels.end() )
381 TDT *prev_tdt = it->second.tdt;
382 it->second.tdt = new TDT(chan, prev_tdt->getUpdateCount());
383 it->second.tdt->startTimer(60*60*1000); // restart TDT for this transponder in 60min
389 void eDVBLocalTimeHandler::DVBChannelAdded(eDVBChannel *chan)
393 // eDebug("[eDVBLocalTimerHandler] add channel %p", chan);
394 std::pair<std::map<iDVBChannel*, channel_data>::iterator, bool> tmp =
395 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data>(chan, channel_data()) );
396 tmp.first->second.tdt = NULL;
397 tmp.first->second.channel = chan;
398 tmp.first->second.m_prevChannelState = -1;
399 chan->connectStateChange(slot(*this, &eDVBLocalTimeHandler::DVBChannelStateChanged), tmp.first->second.m_stateChangedConn);
403 void eDVBLocalTimeHandler::DVBChannelStateChanged(iDVBChannel *chan)
405 std::map<iDVBChannel*, channel_data>::iterator it =
406 m_knownChannels.find(chan);
407 if ( it != m_knownChannels.end() )
410 chan->getState(state);
411 if ( state != it->second.m_prevChannelState )
415 case iDVBChannel::state_ok:
416 eDebug("[eDVBLocalTimerHandler] channel %p running", chan);
417 it->second.tdt = new TDT(it->second.channel);
418 it->second.tdt->start();
420 case iDVBChannel::state_release:
421 eDebug("[eDVBLocalTimerHandler] remove channel %p", chan);
422 delete it->second.tdt;
423 m_knownChannels.erase(it);
425 default: // ignore all other events
428 it->second.m_prevChannelState = state;