/
dvbci.cpp
1834 lines (1595 loc) · 46.8 KB
/
dvbci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sstream>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <ios>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <lib/base/init.h>
#include <lib/base/init_num.h>
#include <lib/base/cfile.h>
#include <lib/base/ebase.h>
#include <lib/base/eerror.h>
#include <lib/base/nconfig.h> // access to python config
#include <lib/base/esimpleconfig.h>
#include <lib/dvb/db.h>
#include <lib/dvb/pmt.h>
#include <lib/dvb_ci/dvbci.h>
#include <lib/dvb_ci/dvbci_session.h>
#include <lib/dvb_ci/dvbci_camgr.h>
#include <lib/dvb_ci/dvbci_ui.h>
#include <lib/dvb_ci/dvbci_appmgr.h>
#include <lib/dvb_ci/dvbci_mmi.h>
#include <lib/dvb_ci/dvbci_ccmgr.h>
#include <dvbsi++/ca_program_map_section.h>
eDVBCIInterfaces *eDVBCIInterfaces::instance = 0;
pthread_mutex_t eDVBCIInterfaces::m_pmt_handler_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
pthread_mutex_t eDVBCIInterfaces::m_slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static char* readInputCI(int NimNumber)
{
char id1[] = "NIM Socket";
char id2[] = "Input_Name";
char keys1[] = "1234567890";
char keys2[] = "12ABCDabcd";
char *inputName = 0;
char buf[256];
FILE *f;
f = fopen("/proc/bus/nim_sockets", "rt");
if (f)
{
while (fgets(buf, sizeof(buf), f))
{
char *p = strcasestr(buf, id1);
if (!p)
continue;
p += strlen(id1);
p += strcspn(p, keys1);
if (*p && strtol(p, 0, 0) == NimNumber)
break;
}
while (fgets(buf, sizeof(buf), f))
{
if (strcasestr(buf, id1))
break;
char *p = strcasestr(buf, id2);
if (!p)
continue;
p = strchr(p + strlen(id2), ':');
if (!p)
continue;
p++;
p += strcspn(p, keys2);
size_t len = strspn(p, keys2);
if (len > 0)
{
inputName = strndup(p, len);
break;
}
}
fclose(f);
}
return inputName;
}
static std::string getTunerLetterDM(int NimNumber)
{
char *srcCI = readInputCI(NimNumber);
if (srcCI) {
std::string ret = std::string(srcCI);
free(srcCI);
return ret;
}
return eDVBCISlot::getTunerLetter(NimNumber);
}
eDVBCIInterfaces::eDVBCIInterfaces()
: m_messagepump_thread(this,1, "dvbci"), m_messagepump_main(eApp,1, "dvbci"), m_runTimer(eTimer::create(this))
{
int num_ci = 0;
std::stringstream path;
instance = this;
m_stream_interface = interface_none;
m_stream_finish_mode = finish_none;
CONNECT(m_messagepump_thread.recv_msg, eDVBCIInterfaces::gotMessageThread);
CONNECT(m_messagepump_main.recv_msg, eDVBCIInterfaces::gotMessageMain);
m_runTimer->start(750, false);
eDebug("[CI] scanning for common interfaces..");
singleLock s(m_slot_lock);
for (;;)
{
path.str("");
path.clear();
path << "/dev/ci" << num_ci;
if(::access(path.str().c_str(), R_OK) < 0)
break;
ePtr<eDVBCISlot> cislot;
cislot = new eDVBCISlot(this, num_ci);
m_slots.push_back(cislot);
++num_ci;
}
for (eSmartPtrList<eDVBCISlot>::iterator it(m_slots.begin()); it != m_slots.end(); ++it)
#ifdef DREAMBOX_DUAL_TUNER
it->setSource(getTunerLetterDM(0));
#else
it->setSource("A");
#endif
for (int tuner_no = 0; tuner_no < 26; ++tuner_no) // NOTE: this assumes tuners are A .. Z max.
{
path.str("");
path.clear();
path << "/proc/stb/tsmux/input" << tuner_no << "_choices";
if(::access(path.str().c_str(), R_OK) < 0)
break;
#ifdef DREAMBOX_DUAL_TUNER
setInputSource(tuner_no, getTunerLetterDM(tuner_no));
#else
setInputSource(tuner_no, eDVBCISlot::getTunerLetter(tuner_no));
#endif
}
eDebug("[CI] done, found %d common interface slots", num_ci);
if (num_ci)
{
static const char *proc_ci_choices = "/proc/stb/tsmux/ci0_input_choices";
if (CFile::contains_word(proc_ci_choices, "PVR")) // lowest prio = PVR
m_stream_interface = interface_use_pvr;
if (CFile::contains_word(proc_ci_choices, "DVR")) // low prio = DVR
m_stream_interface = interface_use_dvr;
if (CFile::contains_word(proc_ci_choices, "DVR0")) // high prio = DVR0
m_stream_interface = interface_use_dvr;
if (m_stream_interface == interface_none) // fallback = DVR
{
m_stream_interface = interface_use_dvr;
eDebug("[CI] Streaming CI routing interface not advertised, assuming DVR method");
}
if (CFile::contains_word(proc_ci_choices, "PVR_NONE")) // low prio = PVR_NONE
m_stream_finish_mode = finish_use_pvr_none;
if (CFile::contains_word(proc_ci_choices, "NONE")) // high prio = NONE
m_stream_finish_mode = finish_use_none;
if (m_stream_finish_mode == finish_none) // fallback = "tuner"
{
m_stream_finish_mode = finish_use_tuner_a;
eDebug("[CI] Streaming CI finish interface not advertised, assuming \"tuner\" method");
}
}
run();
}
eDVBCIInterfaces::~eDVBCIInterfaces()
{
m_messagepump_thread.send(1); // stop thread
kill(); // join
}
eDVBCIInterfaces *eDVBCIInterfaces::getInstance()
{
return instance;
}
void eDVBCIInterfaces::thread()
{
hasStarted();
if (nice(4) == -1)
{
eDebug("[CI] thread failed to modify scheduling priority (%m)");
}
runLoop();
}
// runs in the thread
void eDVBCIInterfaces::gotMessageThread(const int &message)
{
quit(0); // quit thread
}
// runs in the e2 mainloop
void eDVBCIInterfaces::gotMessageMain(const int &message)
{
recheckPMTHandlers();
}
eDVBCISlot *eDVBCIInterfaces::getSlot(int slotid)
{
singleLock s(m_slot_lock);
for(eSmartPtrList<eDVBCISlot>::iterator i(m_slots.begin()); i != m_slots.end(); ++i)
if(i->getSlotID() == slotid)
return i;
eWarning("[CI] FIXME: request for unknown slot");
return 0;
}
int eDVBCIInterfaces::getSlotState(int slotid)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return eDVBCISlot::stateInvalid;
return slot->getState();
}
int eDVBCIInterfaces::reset(int slotid)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
return slot->reset();
}
int eDVBCIInterfaces::initialize(int slotid)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
slot->removeService();
return sendCAPMT(slotid);
}
int eDVBCIInterfaces::sendCAPMT(int slotid)
{
eDVBCISlot *slot;
singleLock s1(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
singleLock s2(m_pmt_handler_lock);
PMTHandlerList::iterator it = m_pmt_handlers.begin();
while (it != m_pmt_handlers.end())
{
eDVBCISlot *tmp = it->cislot;
while (tmp && tmp != slot)
tmp = tmp->linked_next;
if (tmp)
{
tmp->sendCAPMT(it->pmthandler); // send capmt
break;
}
++it;
}
return 0;
}
int eDVBCIInterfaces::startMMI(int slotid)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
return slot->startMMI();
}
int eDVBCIInterfaces::stopMMI(int slotid)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
return slot->stopMMI();
}
int eDVBCIInterfaces::answerText(int slotid, int answer)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
return slot->answerText(answer);
}
int eDVBCIInterfaces::answerEnq(int slotid, char *value)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
return slot->answerEnq(value);
}
int eDVBCIInterfaces::cancelEnq(int slotid)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
return slot->cancelEnq();
}
void eDVBCIInterfaces::ciRemoved(eDVBCISlot *slot)
{
if (slot->use_count)
{
singleLock s1(m_pmt_handler_lock);
singleLock s2(m_slot_lock);
eDebug("[CI] Slot %d: removed... usecount %d", slot->getSlotID(), slot->use_count);
for (PMTHandlerList::iterator it(m_pmt_handlers.begin());
it != m_pmt_handlers.end(); ++it)
{
if (it->cislot == slot) // remove the base slot
it->cislot = slot->linked_next;
else if (it->cislot)
{
eDVBCISlot *prevSlot = it->cislot, *hSlot = it->cislot->linked_next;
while (hSlot)
{
if (hSlot == slot) {
prevSlot->linked_next = slot->linked_next;
break;
}
prevSlot = hSlot;
hSlot = hSlot->linked_next;
}
}
}
if (slot->linked_next)
slot->linked_next->setSource(slot->current_source);
else // last CI in chain
setInputSource(slot->current_tuner, eDVBCISlot::getTunerLetter(slot->current_tuner));
slot->linked_next = 0;
slot->use_count=0;
slot->plugged=true;
slot->user_mapped=false;
slot->removeService(0xFFFF);
executeRecheckPMTHandlersInMainloop(); // calls recheckPMTHandlers in the e2 mainloop
}
}
bool eDVBCIInterfaces::canDescrambleMultipleServices(eDVBCISlot* slot)
{
singleLock s(m_slot_lock);
char configStr[255];
snprintf(configStr, 255, "config.ci.%d.canDescrambleMultipleServices", slot->getSlotID());
std::string str = eSimpleConfig::getString(configStr, "auto");
if ( str == "auto" )
{
if (slot->getAppManager())
{
std::string appname = slot->getAppManager()->getAppName();
if (appname.find("AlphaCrypt") != std::string::npos || appname.find("Multi") != std::string::npos)
return true;
}
}
else if (str == "yes")
return true;
return false;
}
// executes recheckPMTHandlers in the e2 mainloop
void eDVBCIInterfaces::executeRecheckPMTHandlersInMainloop()
{
m_messagepump_main.send(1);
}
// has to run in the e2 mainloop to be able to access the pmt handler
void eDVBCIInterfaces::recheckPMTHandlers()
{
singleLock s1(m_pmt_handler_lock);
singleLock s2(m_slot_lock);
eTrace("[CI] recheckPMTHAndlers()");
for (PMTHandlerList::iterator it(m_pmt_handlers.begin());
it != m_pmt_handlers.end(); ++it)
{
CAID_LIST caids;
ePtr<eDVBService> service;
eServiceReferenceDVB ref;
eDVBCISlot *tmp = it->cislot;
eDVBServicePMTHandler *pmthandler = it->pmthandler;
eDVBServicePMTHandler::program p;
bool plugged_cis_exist = false;
pmthandler->getServiceReference(ref);
pmthandler->getService(service);
eTrace("[CI] recheck %p %s", pmthandler, ref.toString().c_str());
for (eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin()); ci_it != m_slots.end(); ++ci_it)
if (ci_it->plugged && ci_it->getCAManager())
{
eDebug("[CI] Slot %d plugged", ci_it->getSlotID());
ci_it->plugged = false;
plugged_cis_exist = true;
}
// check if this pmt handler has already assigned CI(s) .. and this CI(s) are already running
if (!plugged_cis_exist)
{
while(tmp)
{
if (!tmp->running_services.empty())
break;
tmp=tmp->linked_next;
}
if (tmp) // we dont like to change tsmux for running services
{
eTrace("[CI] already assigned and running CI!\n");
continue;
}
}
if (!pmthandler->getProgramInfo(p))
{
int cnt=0;
std::set<eDVBServicePMTHandler::program::capid_pair> set(p.caids.begin(), p.caids.end());
for (std::set<eDVBServicePMTHandler::program::capid_pair>::reverse_iterator x(set.rbegin()); x != set.rend(); ++x, ++cnt)
caids.push_front(x->caid);
if (service && cnt)
service->m_ca = caids;
}
if (service)
caids = service->m_ca;
if (caids.empty())
continue; // unscrambled service
for (eSmartPtrList<eDVBCISlot>::iterator ci_it(m_slots.begin()); ci_it != m_slots.end(); ++ci_it)
{
eTrace("[CI] check Slot %d", ci_it->getSlotID());
bool useThis=false;
bool user_mapped=true;
eDVBCICAManagerSession *ca_manager = ci_it->getCAManager();
if (ca_manager)
{
int mask=0;
if (!ci_it->possible_services.empty())
{
mask |= 1;
serviceSet::iterator it = ci_it->possible_services.find(ref);
if (it != ci_it->possible_services.end())
{
eDebug("[CI] '%s' is in service list of slot %d... so use it", ref.toString().c_str(), ci_it->getSlotID());
useThis = true;
}
else // check parent
{
eServiceReferenceDVB parent_ref = ref.getParentServiceReference();
if (parent_ref)
{
it = ci_it->possible_services.find(ref);
if (it != ci_it->possible_services.end())
{
eDebug("[CI] parent '%s' of '%s' is in service list of slot %d... so use it",
parent_ref.toString().c_str(), ref.toString().c_str(), ci_it->getSlotID());
useThis = true;
}
}
}
}
if (!useThis && !ci_it->possible_providers.empty())
{
eDVBNamespace ns = ref.getDVBNamespace();
mask |= 2;
if (!service) // subservice?
{
eServiceReferenceDVB parent_ref = ref.getParentServiceReference();
eDVBDB::getInstance()->getService(parent_ref, service);
}
if (service)
{
providerSet::iterator it = ci_it->possible_providers.find(providerPair(service->m_provider_name, ns.get()));
if (it != ci_it->possible_providers.end())
{
eDebug("[CI] '%s/%08x' is in provider list of slot %d... so use it", service->m_provider_name.c_str(), ns.get(), ci_it->getSlotID());
useThis = true;
}
}
}
if (!useThis && !ci_it->possible_caids.empty())
{
mask |= 4;
for (CAID_LIST::iterator ca(caids.begin()); ca != caids.end(); ++ca)
{
caidSet::iterator it = ci_it->possible_caids.find(*ca);
if (it != ci_it->possible_caids.end())
{
eDebug("[CI] caid '%04x' is in caid list of slot %d... so use it", *ca, ci_it->getSlotID());
useThis = true;
break;
}
}
}
if (!useThis && !mask)
{
const std::vector<uint16_t> &ci_caids = ca_manager->getCAIDs();
for (CAID_LIST::iterator ca(caids.begin()); ca != caids.end(); ++ca)
{
std::vector<uint16_t>::const_iterator z =
std::lower_bound(ci_caids.begin(), ci_caids.end(), *ca);
if ( z != ci_caids.end() && *z == *ca )
{
eDebug("[CI] The CI in Slot %d has said it can handle caid %04x... so use it", ci_it->getSlotID(), *z);
useThis = true;
user_mapped = false;
break;
}
}
}
}
if (useThis)
{
// check if this CI is already assigned to this pmthandler
eDVBCISlot *tmp = it->cislot;
while(tmp)
{
if (tmp == ci_it)
break;
tmp=tmp->linked_next;
}
if (tmp) // ignore already assigned cislots...
{
eTrace("[CI] already assigned!");
continue;
}
eTrace("[CI] current slot %d usecount %d", ci_it->getSlotID(), ci_it->use_count);
if (ci_it->use_count) // check if this CI can descramble more than one service
{
bool found = false;
useThis = false;
PMTHandlerList::iterator tmp = m_pmt_handlers.begin();
while (!found && tmp != m_pmt_handlers.end())
{
eTrace("[CI] .");
eDVBCISlot *tmp_cislot = tmp->cislot;
while (!found && tmp_cislot)
{
eTrace("[CI] ..");
eServiceReferenceDVB ref2;
tmp->pmthandler->getServiceReference(ref2);
if ( tmp_cislot == ci_it && it->pmthandler != tmp->pmthandler )
{
eTrace("[CI] check pmthandler %s for same service/tp", ref2.toString().c_str());
eDVBChannelID s1, s2;
if (ref != ref2)
{
eTrace("[CI] different services!");
ref.getChannelID(s1);
ref2.getChannelID(s2);
}
if (ref == ref2 || (s1 == s2 && canDescrambleMultipleServices(tmp_cislot)))
{
found = true;
eTrace("[CI] found!");
eDVBCISlot *tmpci = it->cislot = tmp->cislot;
while(tmpci)
{
++tmpci->use_count;
eTrace("[CI] (2)CISlot %d, usecount now %d", tmpci->getSlotID(), tmpci->use_count);
tmpci=tmpci->linked_next;
}
}
}
tmp_cislot=tmp_cislot->linked_next;
}
eTrace("[CI] ...");
++tmp;
}
}
if (useThis)
{
if (ci_it->user_mapped) // we dont like to link user mapped CIs
{
eTrace("[CI] user mapped CI already in use... dont link!");
continue;
}
++ci_it->use_count;
eDebug("[CI] (1)Slot %d, usecount now %d", ci_it->getSlotID(), ci_it->use_count);
std::stringstream ci_source;
ci_source << "CI" << ci_it->getSlotID();
if (!it->cislot)
{
int tunernum = -1;
eUsePtr<iDVBChannel> channel;
if (!pmthandler->getChannel(channel))
{
ePtr<iDVBFrontend> frontend;
if (!channel->getFrontend(frontend))
{
eDVBFrontend *fe = (eDVBFrontend*) &(*frontend);
tunernum = fe->getSlotID();
}
if (tunernum != -1)
{
setInputSource(tunernum, ci_source.str());
#ifdef DREAMBOX_DUAL_TUNER
ci_it->setSource(getTunerLetterDM(tunernum));
#else
ci_it->setSource(eDVBCISlot::getTunerLetter(tunernum));
#endif
}
else
{
/*
* No associated frontend, this must be a DVR source
*
* No need to set tuner input (setInputSource), because we have no tuner.
*/
switch(m_stream_interface)
{
case interface_use_dvr:
{
std::stringstream source;
source << "DVR" << channel->getDvrId();
ci_it->setSource(source.str());
break;
}
case interface_use_pvr:
{
ci_it->setSource("PVR");
break;
}
default:
{
eDebug("[CI] warning: no valid CI streaming interface");
break;
}
}
}
}
ci_it->current_tuner = tunernum;
}
else
{
ci_it->current_tuner = it->cislot->current_tuner;
ci_it->linked_next = it->cislot;
ci_it->setSource(ci_it->linked_next->current_source);
ci_it->linked_next->setSource(ci_source.str());
}
it->cislot = ci_it;
it->cislot->setCamMgrRoutingActive(true);
eTrace("[CI] assigned!");
gotPMT(pmthandler);
}
if (it->cislot && user_mapped) // CI assigned to this pmthandler in this run.. and user mapped? then we break here.. we dont like to link other CIs to user mapped CIs
{
eTrace("[CI] user mapped CI assigned... dont link CIs!");
break;
}
}
}
}
}
void eDVBCIInterfaces::addPMTHandler(eDVBServicePMTHandler *pmthandler)
{
singleLock s(m_pmt_handler_lock);
// check if this pmthandler is already registered
PMTHandlerList::iterator it = m_pmt_handlers.begin();
while (it != m_pmt_handlers.end())
{
if ( *it++ == pmthandler )
return;
}
eServiceReferenceDVB ref;
pmthandler->getServiceReference(ref);
eDebug("[eDVBCIInterfaces] addPMTHandler %s", ref.toString().c_str());
m_pmt_handlers.push_back(CIPmtHandler(pmthandler));
recheckPMTHandlers();
}
void eDVBCIInterfaces::removePMTHandler(eDVBServicePMTHandler *pmthandler)
{
singleLock s1(m_pmt_handler_lock);
singleLock s2(m_slot_lock);
PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(),m_pmt_handlers.end(),pmthandler);
if (it != m_pmt_handlers.end())
{
eDVBCISlot *slot = it->cislot;
eDVBCISlot *base_slot = slot;
eDVBServicePMTHandler *pmthandler = it->pmthandler;
m_pmt_handlers.erase(it);
eServiceReferenceDVB service_to_remove;
pmthandler->getServiceReference(service_to_remove);
bool sameServiceExist=false;
for (PMTHandlerList::iterator i=m_pmt_handlers.begin(); i != m_pmt_handlers.end(); ++i)
{
if (i->cislot)
{
eServiceReferenceDVB ref;
i->pmthandler->getServiceReference(ref);
if ( ref == service_to_remove )
{
sameServiceExist=true;
break;
}
}
}
while(slot)
{
eDVBCISlot *next = slot->linked_next;
if (!sameServiceExist)
{
eDebug("[eDVBCIInterfaces] remove last pmt handler for service %s send empty capmt",
service_to_remove.toString().c_str());
std::vector<uint16_t> caids;
caids.push_back(0xFFFF);
slot->sendCAPMT(pmthandler, caids); // send a capmt without caids to remove a running service
slot->removeService(service_to_remove.getServiceID().get());
if (slot->current_tuner == -1)
{
// no previous tuner to go back to, signal to CI interface CI action is finished
std::string finish_source;
switch (m_stream_finish_mode)
{
case finish_use_tuner_a:
{
#ifdef DREAMBOX_DUAL_TUNER
finish_source = getTunerLetterDM(0);
#else
finish_source = "A";
#endif
break;
}
case finish_use_pvr_none:
{
finish_source = "PVR_NONE";
break;
}
case finish_use_none:
{
finish_source = "NONE";
break;
}
default:
(void)0;
}
if(finish_source == "")
{
eDebug("[CI] warning: CI streaming finish mode not set, assuming \"tuner A\"");
#ifdef DREAMBOX_DUAL_TUNER
finish_source = getTunerLetterDM(0);
#else
finish_source = "A";
#endif
}
slot->setSource(finish_source);
}
}
if (!--slot->use_count)
{
if (slot->linked_next)
slot->linked_next->setSource(slot->current_source);
else
setInputSource(slot->current_tuner, eDVBCISlot::getTunerLetter(slot->current_tuner));
if (base_slot != slot)
{
eDVBCISlot *tmp = it->cislot;
while(tmp->linked_next != slot)
tmp = tmp->linked_next;
ASSERT(tmp);
if (slot->linked_next)
tmp->linked_next = slot->linked_next;
else
tmp->linked_next = 0;
}
else // removed old base slot.. update ptr
base_slot = slot->linked_next;
slot->linked_next = 0;
slot->user_mapped = false;
}
eDebug("[CI] (3) slot %d usecount is now %d", slot->getSlotID(), slot->use_count);
slot = next;
}
// check if another service is waiting for the CI
recheckPMTHandlers();
}
}
void eDVBCIInterfaces::gotPMT(eDVBServicePMTHandler *pmthandler)
{
// language config can only be accessed by e2 mainloop
if (m_language == "")
m_language = eConfigManager::getConfigValue("config.osd.language");
singleLock s1(m_pmt_handler_lock);
singleLock s2(m_slot_lock);
eDebug("[eDVBCIInterfaces] gotPMT");
PMTHandlerList::iterator it=std::find(m_pmt_handlers.begin(), m_pmt_handlers.end(), pmthandler);
if (it != m_pmt_handlers.end() && it->cislot)
{
eDVBCISlot *tmp = it->cislot;
while(tmp)
{
eTrace("[CI] check slot %d %d %d", tmp->getSlotID(), tmp->running_services.empty(), canDescrambleMultipleServices(tmp));
if (tmp->running_services.empty() || canDescrambleMultipleServices(tmp))
{
tmp->setCaParameter(pmthandler);
tmp->sendCAPMT(pmthandler);
}
tmp = tmp->linked_next;
}
}
}
int eDVBCIInterfaces::getMMIState(int slotid)
{
eDVBCISlot *slot;
singleLock s(m_slot_lock);
if( (slot = getSlot(slotid)) == 0 )
return -1;
return slot->getMMIState();
}
int eDVBCIInterfaces::setInputSource(int tuner_no, const std::string &source)
{
if (tuner_no >= 0)
{
char buf[64];
snprintf(buf, sizeof(buf), "/proc/stb/tsmux/input%d", tuner_no);
if (CFile::write(buf, source.c_str()) == -1)
{
eDebug("[CI] eDVBCIInterfaces setInputSource for input %s failed!", source.c_str());
return 0;
}
eDebug("[CI] eDVBCIInterfaces setInputSource(%d, %s)", tuner_no, source.c_str());
}
return 0;
}
PyObject *eDVBCIInterfaces::getDescrambleRules(int slotid)
{
singleLock s(m_slot_lock);
eDVBCISlot *slot = getSlot(slotid);
if (!slot)
{
char tmp[255];
snprintf(tmp, 255, "eDVBCIInterfaces::getDescrambleRules try to get rules for CI Slot %d... but just %zd slots are available", slotid, m_slots.size());
PyErr_SetString(PyExc_ValueError, tmp);
return 0;
}
ePyObject tuple = PyTuple_New(3);
int caids = slot->possible_caids.size();
int services = slot->possible_services.size();
int providers = slot->possible_providers.size();
ePyObject caid_list = PyList_New(caids);
ePyObject service_list = PyList_New(services);
ePyObject provider_list = PyList_New(providers);
caidSet::iterator caid_it(slot->possible_caids.begin());
while(caids)
{
--caids;
PyList_SET_ITEM(caid_list, caids, PyLong_FromLong(*caid_it));
++caid_it;
}
serviceSet::iterator ref_it(slot->possible_services.begin());
while(services)
{
--services;
PyList_SET_ITEM(service_list, services, PyUnicode_FromString(ref_it->toString().c_str()));
++ref_it;
}
providerSet::iterator provider_it(slot->possible_providers.begin());
while(providers)
{
ePyObject tuple = PyTuple_New(2);
PyTuple_SET_ITEM(tuple, 0, PyUnicode_FromString(provider_it->first.c_str()));
PyTuple_SET_ITEM(tuple, 1, PyLong_FromUnsignedLong(provider_it->second));
--providers;
PyList_SET_ITEM(provider_list, providers, tuple);
++provider_it;
}
PyTuple_SET_ITEM(tuple, 0, service_list);
PyTuple_SET_ITEM(tuple, 1, provider_list);
PyTuple_SET_ITEM(tuple, 2, caid_list);
return tuple;
}
const char *PyObject_TypeStr(PyObject *o)
{
return o->ob_type && o->ob_type->tp_name ? o->ob_type->tp_name : "unknown object type";
}
RESULT eDVBCIInterfaces::setDescrambleRules(int slotid, SWIG_PYOBJECT(ePyObject) obj )
{
singleLock s(m_slot_lock);
eDVBCISlot *slot = getSlot(slotid);
if (!slot)
{
char tmp[255];
snprintf(tmp, 255, "eDVBCIInterfaces::setDescrambleRules try to set rules for CI Slot %d... but just %zd slots are available", slotid, m_slots.size());
PyErr_SetString(PyExc_ValueError, tmp);
return -1;
}
if (!PyTuple_Check(obj))
{
char tmp[255];
snprintf(tmp, 255, "2nd argument of setDescrambleRules is not a tuple.. it is a '%s'!!", PyObject_TypeStr(obj));
PyErr_SetString(PyExc_TypeError, tmp);
return -1;
}
if (PyTuple_Size(obj) != 3)
{
const char *errstr = "eDVBCIInterfaces::setDescrambleRules not enough entrys in argument tuple!!\n"
"first argument should be a pythonlist with possible services\n"
"second argument should be a pythonlist with possible providers/dvbnamespace tuples\n"
"third argument should be a pythonlist with possible caids";
PyErr_SetString(PyExc_TypeError, errstr);
return -1;
}
ePyObject service_list = PyTuple_GET_ITEM(obj, 0);
ePyObject provider_list = PyTuple_GET_ITEM(obj, 1);
ePyObject caid_list = PyTuple_GET_ITEM(obj, 2);
if (!PyList_Check(service_list) || !PyList_Check(provider_list) || !PyList_Check(caid_list))
{
char errstr[512];