+
+std::map<eServiceReferenceDVB, eDVBCAService*> eDVBCAService::exist;
+
+eDVBCAService::eDVBCAService()
+ :m_prev_build_hash(0), m_sendstate(0), m_retryTimer(eApp)
+{
+ memset(m_used_demux, 0xFF, sizeof(m_used_demux));
+ CONNECT(m_retryTimer.timeout, eDVBCAService::sendCAPMT);
+ Connect();
+}
+
+eDVBCAService::~eDVBCAService()
+{
+ eDebug("[eDVBCAService] free service %s", m_service.toString().c_str());
+ ::close(m_sock);
+}
+
+RESULT eDVBCAService::register_service( const eServiceReferenceDVB &ref, int demux_num, eDVBCAService *&caservice )
+{
+ CAServiceMap::iterator it = exist.find(ref);
+ if ( it != exist.end() )
+ caservice = it->second;
+ else
+ {
+ caservice = (exist[ref]=new eDVBCAService());
+ caservice->m_service = ref;
+ eDebug("[eDVBCAService] new service %s", ref.toString().c_str() );
+ }
+// search free demux entry
+ int iter=0, max_demux_slots = sizeof(caservice->m_used_demux);
+
+ while ( iter < max_demux_slots && caservice->m_used_demux[iter] != 0xFF )
+ ++iter;
+
+ if ( iter < max_demux_slots )
+ {
+ caservice->m_used_demux[iter] = demux_num & 0xFF;
+ eDebug("[eDVBCAService] add demux %d to slot %d service %s", demux_num, iter, ref.toString().c_str());
+ }
+ else
+ {
+ eDebug("[eDVBCAService] no more demux slots free for service %s!!", ref.toString().c_str());
+ return -1;
+ }
+ return 0;
+}
+
+RESULT eDVBCAService::unregister_service( const eServiceReferenceDVB &ref, int demux_num, eTable<ProgramMapSection> *ptr )
+{
+ CAServiceMap::iterator it = exist.find(ref);
+ if ( it == exist.end() )
+ {
+ eDebug("[eDVBCAService] try to unregister non registered %s", ref.toString().c_str());
+ return -1;
+ }
+ else
+ {
+ eDVBCAService *caservice = it->second;
+ bool freed = false;
+ int iter = 0,
+ used_demux_slots = 0,
+ max_demux_slots = sizeof(caservice->m_used_demux)/sizeof(int);
+ while ( iter < max_demux_slots )
+ {
+ if ( caservice->m_used_demux[iter] != 0xFF )
+ {
+ if ( !freed && caservice->m_used_demux[iter] == demux_num )
+ {
+ eDebug("[eDVBCAService] free slot %d demux %d for service %s", iter, caservice->m_used_demux[iter], caservice->m_service.toString().c_str() );
+ caservice->m_used_demux[iter] = 0xFF;
+ freed=true;
+ }
+ else
+ ++used_demux_slots;
+ }
+ ++iter;
+ }
+ if (!freed)
+ {
+ eDebug("[eDVBCAService] couldn't free demux slot for demux %d", demux_num);
+ return -1;
+ }
+ if (!used_demux_slots) // no more used.. so we remove it
+ {
+ delete it->second;
+ exist.erase(it);
+ }
+ else
+ {
+ if (ptr)
+ it->second->buildCAPMT(ptr);
+ else
+ eDebug("[eDVBCAService] can not send updated demux info");
+ }
+ }
+ return 0;
+}
+
+void eDVBCAService::Connect()
+{
+ memset(&m_servaddr, 0, sizeof(struct sockaddr_un));
+ m_servaddr.sun_family = AF_UNIX;
+ strcpy(m_servaddr.sun_path, "/tmp/camd.socket");
+ m_clilen = sizeof(m_servaddr.sun_family) + strlen(m_servaddr.sun_path);
+ m_sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ connect(m_sock, (struct sockaddr *) &m_servaddr, m_clilen);
+ fcntl(m_sock, F_SETFL, O_NONBLOCK);
+ int val=1;
+ setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &val, 4);
+}
+
+void eDVBCAService::buildCAPMT(eTable<ProgramMapSection> *ptr)
+{
+ if (!ptr)
+ return;
+
+ eDVBTableSpec table_spec;
+ ptr->getSpec(table_spec);
+
+ int pmtpid = table_spec.pid,
+ pmt_version = table_spec.version;
+
+ uint8_t demux_mask = 0;
+ uint8_t first_demux_num = 0xFF;
+
+#if 1
+ int iter=0, max_demux_slots = sizeof(m_used_demux);
+ while ( iter < max_demux_slots )
+ {
+ if ( m_used_demux[iter] != 0xFF )
+ {
+ if ( first_demux_num == 0xFF )
+ first_demux_num = m_used_demux[iter];
+ demux_mask |= (1 << m_used_demux[iter]);
+ }
+ ++iter;
+ }
+#else
+ demux_mask = 3;
+ first_demux_num = 0;
+#endif
+
+ if ( first_demux_num == 0xFF )
+ {
+ eDebug("[eDVBCAService] no demux found for service %s", m_service.toString().c_str() );
+ return;
+ }
+
+ eDebug("demux %d mask %02x prevhash %08x", first_demux_num, demux_mask, m_prev_build_hash);
+
+ unsigned int build_hash = (pmtpid << 16);
+ build_hash |= (demux_mask << 8);
+ build_hash |= (pmt_version&0xFF);
+
+ if ( build_hash == m_prev_build_hash )
+ {
+ eDebug("[eDVBCAService] don't build/send the same CA PMT twice");
+ return;
+ }
+
+ std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
+ if ( i != ptr->getSections().end() )
+ {
+ CaProgramMapSection capmt(*i++, m_prev_build_hash ? 0x05 /*update*/ : 0x03 /*only*/, 0x01 );
+
+ while( i != ptr->getSections().end() )
+ {
+// eDebug("append");
+ capmt.append(*i++);
+ }
+
+ // add our private descriptors to capmt
+ uint8_t tmp[10];
+
+ tmp[0]=0x84; // pmt pid
+ tmp[1]=0x02;
+ tmp[2]=pmtpid>>8;
+ tmp[3]=pmtpid&0xFF;
+ capmt.injectDescriptor(tmp, false);
+
+ tmp[0] = 0x82; // demux
+ tmp[1] = 0x02;
+ tmp[2] = demux_mask; // descramble bitmask
+ tmp[3] = first_demux_num; // read section data from demux number
+ capmt.injectDescriptor(tmp, false);
+
+ tmp[0] = 0x81; // dvbnamespace
+ tmp[1] = 0x08;
+ tmp[2] = m_service.getDVBNamespace().get()>>24;
+ tmp[3]=(m_service.getDVBNamespace().get()>>16)&0xFF;
+ tmp[4]=(m_service.getDVBNamespace().get()>>8)&0xFF;
+ tmp[5]=m_service.getDVBNamespace().get()&0xFF;
+ tmp[6]=m_service.getTransportStreamID().get()>>8;
+ tmp[7]=m_service.getTransportStreamID().get()&0xFF;
+ tmp[8]=m_service.getOriginalNetworkID().get()>>8;
+ tmp[9]=m_service.getOriginalNetworkID().get()&0xFF;
+ capmt.injectDescriptor(tmp, false);
+
+ capmt.writeToBuffer(m_capmt);
+ }
+
+ m_prev_build_hash = build_hash;
+
+ if ( m_sendstate != 0xFFFFFFFF )
+ m_sendstate=0;
+ sendCAPMT();
+}
+
+void eDVBCAService::sendCAPMT()
+{
+ if ( m_sendstate && m_sendstate != 0xFFFFFFFF ) // broken pipe retry
+ {
+ ::close(m_sock);
+ Connect();
+ }
+
+ int wp=0;
+ if ( m_capmt[3] & 0x80 )
+ {
+ int i=0;
+ int lenbytes = m_capmt[3] & ~0x80;
+ while(i < lenbytes)
+ wp = (wp << 8) | m_capmt[4 + i++];
+ wp+=4;
+ wp+=lenbytes;
+ }
+ else
+ {
+ wp = m_capmt[3];
+ wp+=4;
+ }
+
+ if ( write(m_sock, m_capmt, wp) == wp )
+ {
+ m_sendstate=0xFFFFFFFF;
+ eDebug("[eDVBCAService] send %d bytes",wp);
+#if 1
+ for(int i=0;i<wp;i++)
+ eDebugNoNewLine("%02x ", m_capmt[i]);
+ eDebug("");
+#endif
+ }
+ else
+ {
+ switch(m_sendstate)
+ {
+ case 0xFFFFFFFF:
+ ++m_sendstate;
+ m_retryTimer.start(0,true);
+// eDebug("[eDVBCAService] send failed .. immediate retry");
+ break;
+ default:
+ m_retryTimer.start(5000,true);
+// eDebug("[eDVBCAService] send failed .. retry in 5 sec");
+ break;
+ }
+ ++m_sendstate;
+ }
+}