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)
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);
79 int TDT::createTable(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 eDVBLocalTimeHandler::getInstance()->updateTime(tptime, chan);
100 spec.pid = TimeAndDateSection::PID;
101 spec.tid = TimeAndDateSection::TID;
102 spec.tid_mask = 0xFC;
103 spec.timeout = TimeAndDateSection::TIMEOUT;
104 spec.flags= eDVBTableSpec::tfAnyVersion |
105 eDVBTableSpec::tfHaveTID |
106 eDVBTableSpec::tfHaveTIDMask |
107 eDVBTableSpec::tfHaveTimeout;
109 eGTable::start( demux, spec );
113 void TDT::startTimer( int interval )
115 m_interval_timer.start(interval, true);
118 eDVBLocalTimeHandler *eDVBLocalTimeHandler::instance;
119 DEFINE_REF(eDVBLocalTimeHandler);
121 eDVBLocalTimeHandler::eDVBLocalTimeHandler()
122 :m_time_ready(false), m_time_difference(0)
126 ePtr<eDVBResourceManager> res_mgr;
127 eDVBResourceManager::getInstance(res_mgr);
129 eDebug("[eDVBLocalTimerHandler] no resource manager !!!!!!!");
131 res_mgr->connectChannelAdded(slot(*this,&eDVBLocalTimeHandler::DVBChannelAdded), m_chanAddedConn);
134 eDVBLocalTimeHandler::~eDVBLocalTimeHandler()
137 for (std::map<iDVBChannel*, channel_data>::iterator it=m_knownChannels.begin(); it != m_knownChannels.end(); ++it)
138 delete it->second.tdt;
141 void eDVBLocalTimeHandler::readTimeOffsetData( const char* filename )
143 m_timeOffsetMap.clear();
144 FILE *f=fopen(filename, "r");
151 if (!fgets( line, 256, f ))
153 if (strstr(line, "Transponder UTC Time Offsets\n"))
155 int dvbnamespace,tsid,onid,offs;
156 if ( sscanf( line, "%08x,%04x,%04x:%d\n",&dvbnamespace,&tsid,&onid,&offs ) == 4 )
157 m_timeOffsetMap[eDVBChannelID(dvbnamespace,tsid,onid)]=offs;
162 void eDVBLocalTimeHandler::writeTimeOffsetData( const char* filename )
164 FILE *f=fopen(filename, "w+");
167 fprintf(f, "Transponder UTC Time Offsets\n");
168 for ( std::map<eDVBChannelID,int>::iterator it ( m_timeOffsetMap.begin() ); it != m_timeOffsetMap.end(); ++it )
169 fprintf(f, "%08x,%04x,%04x:%d\n",
170 it->first.dvbnamespace.get(),
171 it->first.transport_stream_id.get(), it->first.original_network_id.get(), it->second );
176 void eDVBLocalTimeHandler::updateTime( time_t tp_time, eDVBChannel *chan )
178 bool restart_tdt = false;
181 else if (tp_time == -1)
184 /*if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 ||
185 ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7000
186 && eSystemInfo::getInstance()->hasStandbyWakeupTimer() ) ) TODO !!!!!!! */
188 eDebug("[eDVBLocalTimerHandler] no transponder tuned... or no TDT/TOT avail .. try to use RTC :)");
189 time_t rtc_time = getRTC();
190 if ( rtc_time ) // RTC Ready?
192 tm now = *localtime(&rtc_time);
193 eDebug("[eDVBLocalTimerHandler] RTC time is %02d:%02d:%02d",
197 time_t linuxTime=time(0);
198 time_t nowTime=linuxTime+m_time_difference;
199 now = *localtime(&nowTime);
200 eDebug("[eDVBLocalTimerHandler] Receiver time is %02d:%02d:%02d",
204 m_time_difference = rtc_time - linuxTime;
205 eDebug("[eDVBLocalTimerHandler] RTC to Receiver time difference is %d seconds", nowTime - rtc_time );
206 if ( abs(m_time_difference) > 59 )
208 eDebug("[eDVBLocalTimerHandler] set Linux Time to RTC Time");
210 gettimeofday(&tnow,0);
211 tnow.tv_sec=rtc_time;
212 settimeofday(&tnow,0);
213 for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
214 ;it != eMainloop::existing_loops.end(); ++it)
215 it->addTimeOffset(m_time_difference);
218 else if ( !m_time_difference )
219 eDebug("[eDVBLocalTimerHandler] no change needed");
221 eDebug("[eDVBLocalTimerHandler] set to RTC time");
222 /*emit*/ m_timeUpdated();
225 eDebug("[eDVBLocalTimerHandler] shit RTC not ready :(");
230 std::map< eDVBChannelID, int >::iterator it( m_timeOffsetMap.find( chan->getChannelID() ) );
232 // current linux time
233 time_t linuxTime = time(0);
235 // current enigma time
236 time_t nowTime=linuxTime+m_time_difference;
238 // difference between current enigma time and transponder time
239 int enigma_diff = tp_time-nowTime;
243 if ( m_time_ready ) // ref time ready?
245 // difference between reference time (current enigma time)
246 // and the transponder time
247 eDebug("[eDVBLocalTimerHandler] diff is %d", enigma_diff);
248 if ( abs(enigma_diff) < 120 )
250 eDebug("[eDVBLocalTimerHandler] diff < 120 .. use Transponder Time");
251 m_timeOffsetMap[chan->getChannelID()] = 0;
252 new_diff = enigma_diff;
254 else if ( it != m_timeOffsetMap.end() ) // correction saved?
256 eDebug("[eDVBLocalTimerHandler] we have correction %d", it->second);
257 time_t CorrectedTpTime = tp_time+it->second;
258 int ddiff = CorrectedTpTime-nowTime;
259 eDebug("[eDVBLocalTimerHandler] diff after add correction is %d", ddiff);
260 if ( abs(it->second) < 300 ) // stored correction < 5 min
262 eDebug("[eDVBLocalTimerHandler] use stored correction(<5 min)");
265 else if ( /*eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 && TODO !!!*/
269 m_timeOffsetMap[chan->getChannelID()] = rtc-tp_time;
270 new_diff = rtc-nowTime; // set enigma time to rtc
271 eDebug("[eDVBLocalTimerHandler] update stored correction to %d (calced against RTC time)", rtc-tp_time );
273 else if ( abs(ddiff) <= 120 )
275 // with stored correction calced time difference is lower 2 min
276 // this don't help when a transponder have a clock running to slow or to fast
277 // then its better to have a DM7020 with always running RTC
278 eDebug("[eDVBLocalTimerHandler] use stored correction(corr < 2 min)");
281 else // big change in calced correction.. hold current time and update correction
283 eDebug("[eDVBLocalTimerHandler] update stored correction to %d", -enigma_diff);
284 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
289 eDebug("[eDVBLocalTimerHandler] no correction found... store calced correction(%d)",-enigma_diff);
290 m_timeOffsetMap[chan->getChannelID()] = -enigma_diff;
293 else // no time setted yet
295 if ( it != m_timeOffsetMap.end() )
297 enigma_diff += it->second;
298 eDebug("[eDVBLocalTimerHandler] we have correction (%d)... use", it->second );
301 eDebug("[eDVBLocalTimerHandler] dont have correction.. set Transponder Diff");
302 new_diff=enigma_diff;
306 time_t t = nowTime+new_diff;
307 m_last_tp_time_difference=tp_time-t;
311 eDebug("[eDVBLocalTimerHandler] not changed");
315 tm now = *localtime(&t);
316 eDebug("[eDVBLocalTimerHandler] time update to %02d:%02d:%02d",
321 m_time_difference = t - linuxTime; // calc our new linux_time -> enigma_time correction
322 eDebug("[eDVBLocalTimerHandler] m_time_difference is %d", m_time_difference );
324 // if ( eSystemInfo::getInstance()->getHwType() == eSystemInfo::DM7020 ) TODO !!
327 if ( abs(m_time_difference) > 59 )
329 eDebug("[eDVBLocalTimerHandler] set Linux Time");
331 gettimeofday(&tnow,0);
333 settimeofday(&tnow,0);
334 for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
335 ;it != eMainloop::existing_loops.end(); ++it)
336 it->addTimeOffset(m_time_difference);
340 /*emit*/ m_timeUpdated();
345 std::map<iDVBChannel*, channel_data>::iterator it =
346 m_knownChannels.find(chan);
347 if ( it != m_knownChannels.end() )
349 delete it->second.tdt;
350 it->second.tdt = new TDT(chan);
351 it->second.tdt->startTimer(60*60*1000); // restart TDT for this transponder in 60min
356 void eDVBLocalTimeHandler::DVBChannelAdded(eDVBChannel *chan)
360 // eDebug("[eDVBLocalTimerHandler] add channel %p", chan);
361 std::pair<std::map<iDVBChannel*, channel_data>::iterator, bool> tmp =
362 m_knownChannels.insert( std::pair<iDVBChannel*, channel_data>(chan, channel_data()) );
363 tmp.first->second.tdt = new TDT(chan);
364 tmp.first->second.channel = chan;
365 chan->connectStateChange(slot(*this, &eDVBLocalTimeHandler::DVBChannelStateChanged), tmp.first->second.m_stateChangedConn);
369 void eDVBLocalTimeHandler::DVBChannelStateChanged(iDVBChannel *chan)
371 std::map<iDVBChannel*, channel_data>::iterator it =
372 m_knownChannels.find(chan);
373 if ( it != m_knownChannels.end() )
376 chan->getState(state);
379 case iDVBChannel::state_ok:
380 eDebug("[eDVBLocalTimerHandler] channel %p running", chan);
381 it->second.tdt->start();
383 case iDVBChannel::state_release:
384 eDebug("[eDVBLocalTimerHandler] remove channel %p", chan);
385 delete it->second.tdt;
386 m_knownChannels.erase(it);