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 #define TIME_UPDATE_INTERVAL (30*60*1000)
16 static time_t prev_time;
18 void setRTC(time_t time)
20 FILE *f = fopen("/proc/stb/fp/rtc", "w");
23 if (fprintf(f, "%u", (unsigned int)time))
26 eDebug("write /proc/stb/fp/rtc failed (%m)");
31 int fd = open("/dev/dbox/fp0", O_RDWR);
34 if ( ::ioctl(fd, FP_IOCTL_SET_RTC, (void*)&time ) < 0 )
35 eDebug("FP_IOCTL_SET_RTC failed(%m)");
46 FILE *f = fopen("/proc/stb/fp/rtc", "r");
49 // sanity check to detect corrupt atmel firmware
51 if (fscanf(f, "%u", &tmp) != 1)
52 eDebug("read /proc/stb/fp/rtc failed (%m)");
59 int fd = open("/dev/dbox/fp0", O_RDWR);
62 if ( ::ioctl(fd, FP_IOCTL_GET_RTC, (void*)&rtc_time ) < 0 )
63 eDebug("FP_IOCTL_GET_RTC failed(%m)");
67 return rtc_time != prev_time ? rtc_time : 0;
70 time_t parseDVBtime(__u8 t1, __u8 t2, __u8 t3, __u8 t4, __u8 t5)
75 t.tm_hour=fromBCD(t3);
79 t.tm_year = (int) ((mjd - 15078.2) / 365.25);
80 t.tm_mon = (int) ((mjd - 14956.1 - (int)(t.tm_year * 365.25)) / 30.6001);
81 t.tm_mday = (int) (mjd - 14956 - (int)(t.tm_year * 365.25) - (int)(t.tm_mon * 30.6001));
82 k = (t.tm_mon == 14 || t.tm_mon == 15) ? 1 : 0;
83 t.tm_year = t.tm_year + k;
84 t.tm_mon = t.tm_mon - 1 - k * 12;
93 TDT::TDT(eDVBChannel *chan, int update_count)
94 :chan(chan), m_interval_timer(eTimer::create()), update_count(update_count)
96 CONNECT(tableReady, TDT::ready);
97 CONNECT(m_interval_timer->timeout, TDT::start);
99 chan->getDemux(demux, 0);
102 void TDT::ready(int error)
104 eDVBLocalTimeHandler::getInstance()->updateTime(error, chan, ++update_count);
107 int TDT::createTable(unsigned int nr, const __u8 *data, unsigned int max)
109 if ( data && data[0] == 0x70 || data[0] == 0x73 )
111 int length = ((data[1] & 0x0F) << 8) | data[2];
114 time_t tptime = parseDVBtime(data[3], data[4], data[5], data[6], data[7]);
115 if (tptime && tptime != -1)
116 eDVBLocalTimeHandler::getInstance()->updateTime(tptime, chan, update_count);
129 spec.pid = TimeAndDateSection::PID;
130 spec.tid = TimeAndDateSection::TID;
131 spec.tid_mask = 0xFC;
132 spec.timeout = TimeAndDateSection::TIMEOUT;
133 spec.flags= eDVBTableSpec::tfAnyVersion |
134 eDVBTableSpec::tfHaveTID |
135 eDVBTableSpec::tfHaveTIDMask |
136 eDVBTableSpec::tfHaveTimeout;
138 eGTable::start( demux, spec );
142 void TDT::startTimer( int interval )
144 m_interval_timer->start(interval, true);
147 eDVBLocalTimeHandler *eDVBLocalTimeHandler::instance;
148 DEFINE_REF(eDVBLocalTimeHandler);
150 eDVBLocalTimeHandler::eDVBLocalTimeHandler()
151 :m_use_dvb_time(false), m_updateNonTunedTimer(eTimer::create(eApp)), m_time_ready(false)
155 ePtr<eDVBResourceManager> res_mgr;
156 eDVBResourceManager::getInstance(res_mgr);
158 eDebug("[eDVBLocalTimerHandler] no resource manager !!!!!!!");
161 res_mgr->connectChannelAdded(slot(*this,&eDVBLocalTimeHandler::DVBChannelAdded), m_chanAddedConn);
162 time_t now = time(0);
163 if ( now < 1072224000 ) // 01.01.2004
164 eDebug("RTC not ready... wait for transponder time");
165 else // inform all who's waiting for valid system time..
167 eDebug("Use valid Linux Time :) (RTC?)");
169 /*emit*/ m_timeUpdated();
172 CONNECT(m_updateNonTunedTimer->timeout, eDVBLocalTimeHandler::updateNonTuned);
175 eDVBLocalTimeHandler::~eDVBLocalTimeHandler()
180 eDebug("set RTC to previous valid time");
185 void eDVBLocalTimeHandler::readTimeOffsetData( const char* filename )
187 m_timeOffsetMap.clear();
188 FILE *f=fopen(filename, "r");
195 if (!fgets( line, 256, f ))
197 if (strstr(line, "Transponder UTC Time Offsets\n"))
199 int dvbnamespace,tsid,onid,offs;
200 if ( sscanf( line, "%08x,%04x,%04x:%d\n",&dvbnamespace,&tsid,&onid,&offs ) == 4 )
201 m_timeOffsetMap[eDVBChannelID(dvbnamespace,tsid,onid)]=offs;
206 void eDVBLocalTimeHandler::writeTimeOffsetData( const char* filename )
208 FILE *f=fopen(filename, "w+");
211 fprintf(f, "Transponder UTC Time Offsets\n");
212 for ( std::map<eDVBChannelID,int>::iterator it ( m_timeOffsetMap.begin() ); it != m_timeOffsetMap.end(); ++it )
213 fprintf(f, "%08x,%04x,%04x:%d\n",
214 it->first.dvbnamespace.get(),
215 it->first.transport_stream_id.get(), it->first.original_network_id.get(), it->second );
220 void eDVBLocalTimeHandler::setUseDVBTime(bool b)
222 if (m_use_dvb_time != b) {
223 if (m_use_dvb_time) {
224 eDebug("[eDVBLocalTimeHandler] disable sync local time with transponder time!");
225 std::map<iDVBChannel*, channel_data>::iterator it =
226 m_knownChannels.begin();
227 for (; it != m_knownChannels.end(); ++it) {
228 if (it->second.m_prevChannelState == iDVBChannel::state_ok)
233 eDebug("[eDVBLocalTimeHandler] enable sync local time with transponder time!");
234 std::map<iDVBChannel*, channel_data>::iterator it =
235 m_knownChannels.begin();
236 for (; it != m_knownChannels.end(); ++it) {
237 if (it->second.m_prevChannelState == iDVBChannel::state_ok) {
238 it->second.tdt = new TDT(it->second.channel);
239 it->second.tdt->start();
247 void eDVBLocalTimeHandler::updateNonTuned()
249 updateTime(-1, 0, 0);
250 m_updateNonTunedTimer->start(TIME_UPDATE_INTERVAL, true);
253 void eDVBLocalTimeHandler::updateTime( time_t tp_time, eDVBChannel *chan, int update_count )
256 bool restart_tdt = false;
259 else if (tp_time == -1)
262 /*if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 ||
263 ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
264 && eSystemInfo::getInstance()->hasStandbyWakeupTimer() ) ) TODO !!!!!!! */
266 eDebug("[eDVBLocalTimerHandler] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
267 time_t rtc_time = getRTC();
268 if ( rtc_time ) // RTC Ready?
271 localtime_r(&rtc_time, &now);
272 eDebug("[eDVBLocalTimerHandler] RTC time is %02d:%02d:%02d",
276 time_t linuxTime=time(0);
277 localtime_r(&linuxTime, &now);
278 eDebug("[eDVBLocalTimerHandler] Receiver time is %02d:%02d:%02d",
282 time_difference = rtc_time - linuxTime;
283 eDebug("[eDVBLocalTimerHandler] RTC to Receiver time difference is %ld seconds", linuxTime - rtc_time );
284 if ( time_difference )
286 eDebug("[eDVBLocalTimerHandler] set Linux Time to RTC Time");
288 gettimeofday(&tnow,0);
289 tnow.tv_sec=rtc_time;
290 settimeofday(&tnow,0);
292 else if ( !time_difference )
293 eDebug("[eDVBLocalTimerHandler] no change needed");
295 eDebug("[eDVBLocalTimerHandler] set to RTC time");
296 /*emit*/ m_timeUpdated();
299 eDebug("[eDVBLocalTimerHandler] shit RTC not ready :(");
304 std::map< eDVBChannelID, int >::iterator it( m_timeOffsetMap.find( chan->getChannelID() ) );
306 // current linux time
307 time_t linuxTime = time(0);
309 // difference between current enigma time and transponder time
310 int enigma_diff = tp_time-linuxTime;
314 bool updated = m_time_ready;
316 if ( m_time_ready ) // ref time ready?
318 // difference between reference time (current enigma time)
319 // and the transponder time
320 eDebug("[eDVBLocalTimerHandler] diff is %d", enigma_diff);
321 if ( abs(enigma_diff) < 120 )
323 eDebug("[eDVBLocalTimerHandler] diff < 120 .. use Transponder Time");
324 m_timeOffsetMap[chan->getChannelID()] = 0;
325 new_diff = enigma_diff;
327 else if ( it != m_timeOffsetMap.end() ) // correction saved?
329 eDebug("[eDVBLocalTimerHandler] we have correction %d", it->second);
330 time_t CorrectedTpTime = tp_time+it->second;
331 int ddiff = CorrectedTpTime-linuxTime;
332 eDebug("[eDVBLocalTimerHandler] diff after add correction is %d", ddiff);
333 if ( abs(it->second) < 300 ) // stored correction < 5 min
335 eDebug("[eDVBLocalTimerHandler] use stored correction(<5 min)");
341 m_timeOffsetMap[chan->getChannelID()] = rtc-tp_time;
342 new_diff = rtc-linuxTime; // set enigma time to rtc
343 eDebug("[eDVBLocalTimerHandler] update stored correction to %ld (calced against RTC time)", rtc-tp_time );
345 else if ( abs(ddiff) <= 120 )
347 // with stored correction calced time difference is lower 2 min
348 // this don't help when a transponder have a clock running to slow or to fast
349 // then its better to have a DM7020 with always running RTC
350 eDebug("[eDVBLocalTimerHandler] use stored correction(corr < 2 min)");
353 else // big change in calced correction.. hold current time and update correction
355 eDebug("[eDVBLocalTimerHandler] update stored correction to %d", -enigma_diff);
356 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
361 eDebug("[eDVBLocalTimerHandler] no correction found... store calced correction(%d)",-enigma_diff);
362 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
365 else // no time setted yet
367 if ( it != m_timeOffsetMap.end() )
369 enigma_diff += it->second;
370 eDebug("[eDVBLocalTimerHandler] we have correction (%d)... use", it->second );
373 eDebug("[eDVBLocalTimerHandler] dont have correction.. set Transponder Diff");
374 new_diff=enigma_diff;
378 time_t t = linuxTime+new_diff;
379 m_last_tp_time_difference=tp_time-t;
382 updated) // overrride this check on first received TDT
384 eDebug("[eDVBLocalTimerHandler] not changed");
390 // set rtc to calced transponder time when the first tdt is received on this
393 eDebug("[eDVBLocalTimerHandler] update RTC");
397 if (abs(getRTC() - t) > 60)
399 eDebug("[eDVBLocalTimerHandler] difference between new linux time and RTC time is > 60 sec... transponder time looks not ok... use rtc time");
403 eDebug("[eDVBLocalTimerHandler] difference between linux time and RTC time is < 60 sec... so the transponder time looks ok");
406 eDebug("[eDVBLocalTimerHandler] no RTC available :(");
409 localtime_r(&t, &now);
410 eDebug("[eDVBLocalTimerHandler] time update to %02d:%02d:%02d",
415 time_difference = t - linuxTime; // calc our new linux_time -> enigma_time correction
416 eDebug("[eDVBLocalTimerHandler] m_time_difference is %d", time_difference );
418 if ( time_difference )
420 eDebug("[eDVBLocalTimerHandler] set Linux Time");
422 gettimeofday(&tnow,0);
424 settimeofday(&tnow,0);
427 /*emit*/ m_timeUpdated();
432 std::map<iDVBChannel*, channel_data>::iterator it =
433 m_knownChannels.find(chan);
434 if ( it != m_knownChannels.end() )
436 int updateCount = it->second.tdt->getUpdateCount();
438 it->second.tdt = new TDT(chan, updateCount);
439 it->second.tdt->startTimer(TIME_UPDATE_INTERVAL); // restart TDT for this transponder in 30min
444 void eDVBLocalTimeHandler::DVBChannelAdded(eDVBChannel *chan)
448 // eDebug("[eDVBLocalTimerHandler] add channel %p", chan);
449 std::pair<std::map<iDVBChannel*, channel_data>::iterator, bool> tmp =
450 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data>(chan, channel_data()) );
451 tmp.first->second.tdt = NULL;
452 tmp.first->second.channel = chan;
453 tmp.first->second.m_prevChannelState = -1;
454 chan->connectStateChange(slot(*this, &eDVBLocalTimeHandler::DVBChannelStateChanged), tmp.first->second.m_stateChangedConn);
458 void eDVBLocalTimeHandler::DVBChannelStateChanged(iDVBChannel *chan)
460 std::map<iDVBChannel*, channel_data>::iterator it =
461 m_knownChannels.find(chan);
462 if ( it != m_knownChannels.end() )
465 chan->getState(state);
466 if ( state != it->second.m_prevChannelState )
470 case iDVBChannel::state_ok:
471 eDebug("[eDVBLocalTimerHandler] channel %p running", chan);
472 m_updateNonTunedTimer->stop();
473 if (m_use_dvb_time) {
474 it->second.tdt = new TDT(it->second.channel);
475 it->second.tdt->start();
478 case iDVBChannel::state_release:
479 eDebug("[eDVBLocalTimerHandler] remove channel %p", chan);
480 m_knownChannels.erase(it);
481 if (m_knownChannels.empty())
482 m_updateNonTunedTimer->start(TIME_UPDATE_INTERVAL, true);
484 default: // ignore all other events
487 it->second.m_prevChannelState = state;