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 FILE *f = fopen("/proc/stb/fp/rtc", "w");
21 if (fprintf(f, "%u", time))
24 eDebug("write /proc/stb/fp/rtc failed (%m)");
29 int fd = open("/dev/dbox/fp0", O_RDWR);
32 if ( ::ioctl(fd, FP_IOCTL_SET_RTC, (void*)&time ) < 0 )
33 eDebug("FP_IOCTL_SET_RTC failed(%m)");
44 FILE *f = fopen("/proc/stb/fp/rtc", "r");
47 // sanity check to detect corrupt atmel firmware
48 if (fscanf(f, "%u", &rtc_time) != 1)
49 eDebug("read /proc/stb/fp/rtc failed (%m)");
54 int fd = open("/dev/dbox/fp0", O_RDWR);
57 if ( ::ioctl(fd, FP_IOCTL_GET_RTC, (void*)&rtc_time ) < 0 )
58 eDebug("FP_IOCTL_GET_RTC failed(%m)");
62 return rtc_time != prev_time ? rtc_time : 0;
65 time_t parseDVBtime(__u8 t1, __u8 t2, __u8 t3, __u8 t4, __u8 t5)
70 t.tm_hour=fromBCD(t3);
74 t.tm_year = (int) ((mjd - 15078.2) / 365.25);
75 t.tm_mon = (int) ((mjd - 14956.1 - (int)(t.tm_year * 365.25)) / 30.6001);
76 t.tm_mday = (int) (mjd - 14956 - (int)(t.tm_year * 365.25) - (int)(t.tm_mon * 30.6001));
77 k = (t.tm_mon == 14 || t.tm_mon == 15) ? 1 : 0;
78 t.tm_year = t.tm_year + k;
79 t.tm_mon = t.tm_mon - 1 - k * 12;
88 TDT::TDT(eDVBChannel *chan, int update_count)
89 :chan(chan), update_count(update_count)
91 CONNECT(tableReady, TDT::ready);
92 CONNECT(m_interval_timer.timeout, TDT::start);
94 chan->getDemux(demux, 0);
97 void TDT::ready(int error)
99 eDVBLocalTimeHandler::getInstance()->updateTime(error, chan, ++update_count);
102 int TDT::createTable(unsigned int nr, const __u8 *data, unsigned int max)
104 if ( data && data[0] == 0x70 || data[0] == 0x73 )
106 int length = ((data[1] & 0x0F) << 8) | data[2];
109 time_t tptime = parseDVBtime(data[3], data[4], data[5], data[6], data[7]);
110 if (tptime && tptime != -1)
111 eDVBLocalTimeHandler::getInstance()->updateTime(tptime, chan, update_count);
124 spec.pid = TimeAndDateSection::PID;
125 spec.tid = TimeAndDateSection::TID;
126 spec.tid_mask = 0xFC;
127 spec.timeout = TimeAndDateSection::TIMEOUT;
128 spec.flags= eDVBTableSpec::tfAnyVersion |
129 eDVBTableSpec::tfHaveTID |
130 eDVBTableSpec::tfHaveTIDMask |
131 eDVBTableSpec::tfHaveTimeout;
133 eGTable::start( demux, spec );
137 void TDT::startTimer( int interval )
139 m_interval_timer.start(interval, true);
142 eDVBLocalTimeHandler *eDVBLocalTimeHandler::instance;
143 DEFINE_REF(eDVBLocalTimeHandler);
145 eDVBLocalTimeHandler::eDVBLocalTimeHandler()
150 ePtr<eDVBResourceManager> res_mgr;
151 eDVBResourceManager::getInstance(res_mgr);
153 eDebug("[eDVBLocalTimerHandler] no resource manager !!!!!!!");
156 res_mgr->connectChannelAdded(slot(*this,&eDVBLocalTimeHandler::DVBChannelAdded), m_chanAddedConn);
157 time_t now = time(0);
158 if ( now < 1072224000 ) // 01.01.2004
159 eDebug("RTC not ready... wait for transponder time");
160 else // inform all who's waiting for valid system time..
162 eDebug("Use valid Linux Time :) (RTC?)");
164 /*emit*/ m_timeUpdated();
169 eDVBLocalTimeHandler::~eDVBLocalTimeHandler()
172 for (std::map<iDVBChannel*, channel_data>::iterator it=m_knownChannels.begin(); it != m_knownChannels.end(); ++it)
173 delete it->second.tdt;
176 eDebug("set RTC to previous valid time");
181 void eDVBLocalTimeHandler::readTimeOffsetData( const char* filename )
183 m_timeOffsetMap.clear();
184 FILE *f=fopen(filename, "r");
191 if (!fgets( line, 256, f ))
193 if (strstr(line, "Transponder UTC Time Offsets\n"))
195 int dvbnamespace,tsid,onid,offs;
196 if ( sscanf( line, "%08x,%04x,%04x:%d\n",&dvbnamespace,&tsid,&onid,&offs ) == 4 )
197 m_timeOffsetMap[eDVBChannelID(dvbnamespace,tsid,onid)]=offs;
202 void eDVBLocalTimeHandler::writeTimeOffsetData( const char* filename )
204 FILE *f=fopen(filename, "w+");
207 fprintf(f, "Transponder UTC Time Offsets\n");
208 for ( std::map<eDVBChannelID,int>::iterator it ( m_timeOffsetMap.begin() ); it != m_timeOffsetMap.end(); ++it )
209 fprintf(f, "%08x,%04x,%04x:%d\n",
210 it->first.dvbnamespace.get(),
211 it->first.transport_stream_id.get(), it->first.original_network_id.get(), it->second );
216 void eDVBLocalTimeHandler::updateTime( time_t tp_time, eDVBChannel *chan, int update_count )
219 bool restart_tdt = false;
222 else if (tp_time == -1)
225 /*if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 ||
226 ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
227 && eSystemInfo::getInstance()->hasStandbyWakeupTimer() ) ) TODO !!!!!!! */
229 eDebug("[eDVBLocalTimerHandler] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
230 time_t rtc_time = getRTC();
231 if ( rtc_time ) // RTC Ready?
234 localtime_r(&rtc_time, &now);
235 eDebug("[eDVBLocalTimerHandler] RTC time is %02d:%02d:%02d",
239 time_t linuxTime=time(0);
240 localtime_r(&linuxTime, &now);
241 eDebug("[eDVBLocalTimerHandler] Receiver time is %02d:%02d:%02d",
245 time_difference = rtc_time - linuxTime;
246 eDebug("[eDVBLocalTimerHandler] RTC to Receiver time difference is %ld seconds", linuxTime - rtc_time );
247 if ( time_difference )
249 eDebug("[eDVBLocalTimerHandler] set Linux Time to RTC Time");
251 gettimeofday(&tnow,0);
252 tnow.tv_sec=rtc_time;
253 settimeofday(&tnow,0);
255 else if ( !time_difference )
256 eDebug("[eDVBLocalTimerHandler] no change needed");
258 eDebug("[eDVBLocalTimerHandler] set to RTC time");
259 /*emit*/ m_timeUpdated();
262 eDebug("[eDVBLocalTimerHandler] shit RTC not ready :(");
267 std::map< eDVBChannelID, int >::iterator it( m_timeOffsetMap.find( chan->getChannelID() ) );
269 // current linux time
270 time_t linuxTime = time(0);
272 // difference between current enigma time and transponder time
273 int enigma_diff = tp_time-linuxTime;
277 bool updated = m_time_ready;
279 if ( m_time_ready ) // ref time ready?
281 // difference between reference time (current enigma time)
282 // and the transponder time
283 eDebug("[eDVBLocalTimerHandler] diff is %d", enigma_diff);
284 if ( abs(enigma_diff) < 120 )
286 eDebug("[eDVBLocalTimerHandler] diff < 120 .. use Transponder Time");
287 m_timeOffsetMap[chan->getChannelID()] = 0;
288 new_diff = enigma_diff;
290 else if ( it != m_timeOffsetMap.end() ) // correction saved?
292 eDebug("[eDVBLocalTimerHandler] we have correction %d", it->second);
293 time_t CorrectedTpTime = tp_time+it->second;
294 int ddiff = CorrectedTpTime-linuxTime;
295 eDebug("[eDVBLocalTimerHandler] diff after add correction is %d", ddiff);
296 if ( abs(it->second) < 300 ) // stored correction < 5 min
298 eDebug("[eDVBLocalTimerHandler] use stored correction(<5 min)");
304 m_timeOffsetMap[chan->getChannelID()] = rtc-tp_time;
305 new_diff = rtc-linuxTime; // set enigma time to rtc
306 eDebug("[eDVBLocalTimerHandler] update stored correction to %ld (calced against RTC time)", rtc-tp_time );
308 else if ( abs(ddiff) <= 120 )
310 // with stored correction calced time difference is lower 2 min
311 // this don't help when a transponder have a clock running to slow or to fast
312 // then its better to have a DM7020 with always running RTC
313 eDebug("[eDVBLocalTimerHandler] use stored correction(corr < 2 min)");
316 else // big change in calced correction.. hold current time and update correction
318 eDebug("[eDVBLocalTimerHandler] update stored correction to %d", -enigma_diff);
319 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
324 eDebug("[eDVBLocalTimerHandler] no correction found... store calced correction(%d)",-enigma_diff);
325 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
328 else // no time setted yet
330 if ( it != m_timeOffsetMap.end() )
332 enigma_diff += it->second;
333 eDebug("[eDVBLocalTimerHandler] we have correction (%d)... use", it->second );
336 eDebug("[eDVBLocalTimerHandler] dont have correction.. set Transponder Diff");
337 new_diff=enigma_diff;
341 time_t t = linuxTime+new_diff;
342 m_last_tp_time_difference=tp_time-t;
345 updated) // overrride this check on first received TDT
347 eDebug("[eDVBLocalTimerHandler] not changed");
353 // set rtc to calced transponder time when the first tdt is received on this
356 eDebug("[eDVBLocalTimerHandler] update RTC");
360 if (abs(getRTC() - t) > 60)
362 eDebug("[eDVBLocalTimerHandler] difference between new linux time and RTC time is > 60 sec... transponder time looks not ok... use rtc time");
366 eDebug("[eDVBLocalTimerHandler] difference between linux time and RTC time is < 60 sec... so the transponder time looks ok");
369 eDebug("[eDVBLocalTimerHandler] no RTC available :(");
372 localtime_r(&t, &now);
373 eDebug("[eDVBLocalTimerHandler] time update to %02d:%02d:%02d",
378 time_difference = t - linuxTime; // calc our new linux_time -> enigma_time correction
379 eDebug("[eDVBLocalTimerHandler] m_time_difference is %d", time_difference );
381 if ( time_difference )
383 eDebug("[eDVBLocalTimerHandler] set Linux Time");
385 gettimeofday(&tnow,0);
387 settimeofday(&tnow,0);
390 /*emit*/ m_timeUpdated();
395 std::map<iDVBChannel*, channel_data>::iterator it =
396 m_knownChannels.find(chan);
397 if ( it != m_knownChannels.end() )
399 TDT *prev_tdt = it->second.tdt;
400 it->second.tdt = new TDT(chan, prev_tdt->getUpdateCount());
401 it->second.tdt->startTimer(60*60*1000); // restart TDT for this transponder in 60min
407 void eDVBLocalTimeHandler::DVBChannelAdded(eDVBChannel *chan)
411 // eDebug("[eDVBLocalTimerHandler] add channel %p", chan);
412 std::pair<std::map<iDVBChannel*, channel_data>::iterator, bool> tmp =
413 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data>(chan, channel_data()) );
414 tmp.first->second.tdt = NULL;
415 tmp.first->second.channel = chan;
416 tmp.first->second.m_prevChannelState = -1;
417 chan->connectStateChange(slot(*this, &eDVBLocalTimeHandler::DVBChannelStateChanged), tmp.first->second.m_stateChangedConn);
421 void eDVBLocalTimeHandler::DVBChannelStateChanged(iDVBChannel *chan)
423 std::map<iDVBChannel*, channel_data>::iterator it =
424 m_knownChannels.find(chan);
425 if ( it != m_knownChannels.end() )
428 chan->getState(state);
429 if ( state != it->second.m_prevChannelState )
433 case iDVBChannel::state_ok:
434 eDebug("[eDVBLocalTimerHandler] channel %p running", chan);
435 it->second.tdt = new TDT(it->second.channel);
436 it->second.tdt->start();
438 case iDVBChannel::state_release:
439 eDebug("[eDVBLocalTimerHandler] remove channel %p", chan);
440 delete it->second.tdt;
441 m_knownChannels.erase(it);
443 default: // ignore all other events
446 it->second.m_prevChannelState = state;