+void eDVBCISlot::setAppManager( eDVBCIApplicationManagerSession *session )
+{
+ application_manager=session;
+}
+
+void eDVBCISlot::setMMIManager( eDVBCIMMISession *session )
+{
+ mmi_session = session;
+}
+
+void eDVBCISlot::setCAManager( eDVBCICAManagerSession *session )
+{
+ ca_manager = session;
+}
+
+int eDVBCISlot::getSlotID()
+{
+ return slotid;
+}
+
+int eDVBCISlot::reset()
+{
+ printf("edvbcislot: reset requested\n");
+
+ if (state == stateInvalid)
+ {
+ unsigned char buf[256];
+ eDebug("ci flush");
+ while(::read(fd, buf, 256)>0);
+ state = stateResetted;
+ }
+
+ while(sendqueue.size())
+ {
+ delete [] sendqueue.top().data;
+ sendqueue.pop();
+ }
+
+ ioctl(fd, 0);
+
+ return 0;
+}
+
+int eDVBCISlot::startMMI()
+{
+ printf("edvbcislot: startMMI()\n");
+
+ if(application_manager)
+ application_manager->startMMI();
+
+ return 0;
+}
+
+int eDVBCISlot::stopMMI()
+{
+ printf("edvbcislot: stopMMI()\n");
+
+ if(mmi_session)
+ mmi_session->stopMMI();
+
+ return 0;
+}
+
+int eDVBCISlot::answerText(int answer)
+{
+ printf("edvbcislot: answerText(%d)\n", answer);
+
+ if(mmi_session)
+ mmi_session->answerText(answer);
+
+ return 0;
+}
+
+int eDVBCISlot::getMMIState()
+{
+ if(mmi_session)
+ return 1;
+
+ return 0;
+}
+
+int eDVBCISlot::answerEnq(char *value)
+{
+ printf("edvbcislot: answerENQ(%s)\n", value);
+
+ if(mmi_session)
+ mmi_session->answerEnq(value);
+
+ return 0;
+}
+
+int eDVBCISlot::cancelEnq()
+{
+ printf("edvbcislot: cancelENQ\n");
+
+ if(mmi_session)
+ mmi_session->cancelEnq();
+
+ return 0;
+}
+
+int eDVBCISlot::sendCAPMT(eDVBServicePMTHandler *pmthandler, const std::vector<uint16_t> &ids)
+{
+ if (!ca_manager)
+ {
+ eDebug("no ca_manager (no CI plugged?)");
+ return -1;
+ }
+ const std::vector<uint16_t> &caids = ids.empty() ? ca_manager->getCAIDs() : ids;
+ ePtr<eTable<ProgramMapSection> > ptr;
+ if (pmthandler->getPMT(ptr))
+ return -1;
+ else
+ {
+ eDVBTableSpec table_spec;
+ ptr->getSpec(table_spec);
+ int pmt_version = table_spec.version & 0x1F; // just 5 bits
+
+ eServiceReferenceDVB ref;
+ pmthandler->getServiceReference(ref);
+ uint16_t program_number = ref.getServiceID().get();
+ std::map<uint16_t, uint8_t>::iterator it =
+ running_services.find(program_number);
+
+ if ( it != running_services.end() &&
+ (pmt_version == it->second) &&
+ !(caids.size() == 1 && caids[0] == 0xFFFF) )
+ {
+ eDebug("[eDVBCISlot] dont sent self capmt version twice");
+ return -1;
+ }
+
+ std::vector<ProgramMapSection*>::const_iterator i=ptr->getSections().begin();
+ if ( i == ptr->getSections().end() )
+ return -1;
+ else
+ {
+ unsigned char raw_data[2048];
+
+// eDebug("send %s capmt for service %04x",
+// it != running_services.end() ? "UPDATE" : running_services.empty() ? "ONLY" : "ADD",
+// program_number);
+
+ CaProgramMapSection capmt(*i++,
+ it != running_services.end() ? 0x05 /*update*/ : running_services.empty() ? 0x03 /*only*/ : 0x04 /*add*/, 0x01, caids );
+ while( i != ptr->getSections().end() )
+ {
+ // eDebug("append");
+ capmt.append(*i++);
+ }
+ capmt.writeToBuffer(raw_data);
+#if 1
+// begin calc capmt length
+ int wp=0;
+ int hlen;
+ if ( raw_data[3] & 0x80 )
+ {
+ int i=0;
+ int lenbytes = raw_data[3] & ~0x80;
+ while(i < lenbytes)
+ wp = (wp << 8) | raw_data[4 + i++];
+ wp+=4;
+ wp+=lenbytes;
+ hlen = 4 + lenbytes;
+ }
+ else
+ {
+ wp = raw_data[3];
+ wp+=4;
+ hlen = 4;
+ }
+// end calc capmt length
+// eDebug("ca_manager %p dump capmt:", ca_manager);
+// for(int i=0;i<wp;i++)
+// eDebugNoNewLine("%02x ", raw_data[i]);
+// eDebug("");
+#endif
+ if (caids.size() == 1 && caids[0] == 0xFFFF)
+ {
+// eDebugNoNewLine("SEND EMPTY CAPMT.. old version is %02x", raw_data[hlen+3]);
+ raw_data[hlen+3] &= ~0x3E;
+ raw_data[hlen+3] |= ((pmt_version+1) & 0x1F) << 1;
+// eDebug(" new version is %02x", raw_data[hlen+3]);
+ }
+
+ //dont need tag and lenfield
+ ca_manager->sendCAPMT(raw_data + hlen, wp - hlen);
+ running_services[program_number] = pmt_version;
+ }
+ }
+ return 0;
+}
+
+void eDVBCISlot::removeService(uint16_t program_number)
+{
+ if (program_number == 0xFFFF)
+ running_services.clear(); // remove all
+ else
+ running_services.erase(program_number); // remove single service
+}
+
+int eDVBCISlot::enableTS(int enable, int tuner)
+{
+// printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
+// printf("eDVBCISlot::enableTS(%d %d)\n", enable, tuner);
+
+ FILE *input0, *input1, *ci;
+ if((input0 = fopen("/proc/stb/tsmux/input0", "wb")) == NULL) {
+ printf("cannot open /proc/stb/tsmux/input0\n");
+ return 0;
+ }
+ if((input1 = fopen("/proc/stb/tsmux/input1", "wb")) == NULL) {
+ printf("cannot open /proc/stb/tsmux/input1\n");
+ return 0;
+ }
+ if((ci = fopen("/proc/stb/tsmux/input2", "wb")) == NULL) {
+ printf("cannot open /proc/stb/tsmux/input2\n");
+ return 0;
+ }
+
+ fprintf(ci, "%s", tuner==0 ? "A" : "B"); // configure CI data source (TunerA, TunerB)
+ fprintf(input0, "%s", tuner==0 && enable ? "CI" : "A"); // configure ATI input 0 data source
+ fprintf(input1, "%s", tuner==1 && enable ? "CI" : "B"); // configure ATI input 1 data source
+
+ fclose(input0);
+ fclose(input1);
+ fclose(ci);
+ return 0;
+}
+