vdr 2.6.1
include/vdr/ci.h
Go to the documentation of this file.
1/*
2 * ci.h: Common Interface
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * $Id: ci.h 4.14 2019/05/28 14:58:08 kls Exp $
8 */
9
10#ifndef __CI_H
11#define __CI_H
12
13#include <stdint.h>
14#include <stdio.h>
15#include "channels.h"
16#include "ringbuffer.h"
17#include "thread.h"
18#include "tools.h"
19
20#define MAX_CAM_SLOTS_PER_ADAPTER 16 // maximum possible value is 255 (same value as MAXDEVICES!)
21#define MAX_CONNECTIONS_PER_CAM_SLOT 8 // maximum possible value is 254
22#define CAM_READ_TIMEOUT 50 // ms
23
25class cCamSlot;
26
27// VDR's Common Interface functions implement only the features that are absolutely
28// necessary to control a CAM. If a plugin wants to implement additional functionality
29// (i.e. "resources"), it can do so by deriving from cCiResourceHandler, cCiSession
30// and (if necessary) from cCiApplicationInformation.
31
32class cCiSession {
33private:
34 uint16_t sessionId;
35 uint32_t resourceId;
37protected:
41 void SetResourceId(uint32_t Id);
45 int GetTag(int &Length, const uint8_t **Data);
46 const uint8_t *GetData(const uint8_t *Data, int &Length);
47 void SendData(int Tag, int Length = 0, const uint8_t *Data = NULL);
48 cCiTransportConnection *Tc(void) { return tc; }
49public:
51 virtual ~cCiSession();
52 uint16_t SessionId(void) { return sessionId; }
53 uint32_t ResourceId(void) { return resourceId; }
55 virtual bool HasUserIO(void) { return false; }
56 virtual void Process(int Length = 0, const uint8_t *Data = NULL);
57 virtual bool TsPostProcess(uint8_t *TsPacket) { return false; }
66 };
67
69protected:
70 int state;
71 uint8_t applicationType;
73 uint16_t manufacturerCode;
74 char *menuString;
75public:
78 virtual void Process(int Length = 0, const uint8_t *Data = NULL);
79 bool EnterMenu(void);
80 const char *GetMenuString(void) { return menuString; }
81 };
82
83class cCiResourceHandler : public cListObject {
84public:
92 virtual const uint32_t *ResourceIds(void) const = 0;
95 virtual cCiSession *GetNewCiSession(uint32_t ResourceId, uint16_t SessionId, cCiTransportConnection *Tc) = 0;
97 };
98
99class cCiResourceHandlers : public cList<cCiResourceHandler> {
100private:
102public:
105 void Register(cCiResourceHandler *ResourceHandler);
110 const uint32_t *Ids(void) { return &resourceIds[0]; }
111 int NumIds(void) { return resourceIds.Size(); }
112 cCiSession *GetNewCiSession(uint32_t ResourceId, uint16_t SessionId, cCiTransportConnection *Tc);
113 };
114
116
117class cCiMMI;
118
119class cCiMenu {
120 friend class cCamSlot;
121 friend class cCiMMI;
122private:
123 enum { MAX_CIMENU_ENTRIES = 64 };
124 cCiMMI *mmi;
125 cMutex *mutex;
126 bool selectable;
127 char *titleText;
128 char *subTitleText;
129 char *bottomText;
131 int numEntries;
132 bool AddEntry(char *s);
134public:
136 const char *TitleText(void) { return titleText; }
137 const char *SubTitleText(void) { return subTitleText; }
138 const char *BottomText(void) { return bottomText; }
139 const char *Entry(int n) { return n < numEntries ? entries[n] : NULL; }
140 int NumEntries(void) { return numEntries; }
141 bool Selectable(void) { return selectable; }
142 void Select(int Index);
143 void Cancel(void);
144 void Abort(void);
145 bool HasUpdate(void);
146 };
147
148class cCiEnquiry {
149 friend class cCamSlot;
150 friend class cCiMMI;
151private:
152 cCiMMI *mmi;
153 cMutex *mutex;
154 char *text;
155 bool blind;
156 int expectedLength;
158public:
160 const char *Text(void) { return text; }
161 bool Blind(void) { return blind; }
162 int ExpectedLength(void) { return expectedLength; }
163 void Reply(const char *s);
164 void Cancel(void);
165 void Abort(void);
166 };
167
168class cDevice;
169
171
172class cCiAdapter : public cThread {
173 friend class cCamSlot;
174private:
176 void AddCamSlot(cCamSlot *CamSlot);
178protected:
179 cCamSlot *ItCamSlot(int &Iter);
183 virtual void Action(void);
187 virtual int Read(uint8_t *Buffer, int MaxLength) { return 0; }
192 virtual void Write(const uint8_t *Buffer, int Length) {}
194 virtual bool Reset(int Slot) { return false; }
197 virtual eModuleStatus ModuleStatus(int Slot) { return msNone; }
199 virtual bool Assign(cDevice *Device, bool Query = false) { return false; }
208public:
210 virtual ~cCiAdapter();
212 };
213
214class cTPDU;
216class cCiSession;
217class cCiCaProgramData;
218class cCaPidReceiver;
220class cMtdHandler;
221class cMtdMapper;
222class cMtdCamSlot;
223class cCiCaPmt;
224
225struct cCiCaPmtList {
228 cCiCaPmt *Add(uint8_t CmdId, int Source, int Transponder, int ProgramNumber, const int *CaSystemIds);
229 void Del(cCiCaPmt *CaPmt);
230 };
231
232class cCamSlot : public cListObject {
233 friend class cCiAdapter;
234 friend class cCiTransportConnection;
235 friend class cCiConditionalAccessSupport;
236 friend class cMtdCamSlot;
237private:
245 int slotIndex;
246 int slotNumber;
247 cCiTransportConnection *tc[MAX_CONNECTIONS_PER_CAM_SLOT + 1]; // connection numbering starts with 1
249 time_t resetTime;
251 bool resendPmt;
252 int source;
253 int transponder;
255 bool mtdAvailable;
257 void KeepSharedCaPids(int ProgramNumber, const int *CaSystemIds, int *CaPids);
258 void NewConnection(void);
260 void Process(cTPDU *TPDU = NULL);
261 void Write(cTPDU *TPDU);
262 cCiSession *GetSessionByResourceId(uint32_t ResourceId);
263 void MtdActivate(bool On);
265protected:
266 virtual const int *GetCaSystemIds(void);
267 virtual void SendCaPmt(uint8_t CmdId);
268 virtual bool RepliesToQuery(void);
271 void BuildCaPmts(uint8_t CmdId, cCiCaPmtList &CaPmtList, cMtdMapper *MtdMapper = NULL);
274 void SendCaPmts(cCiCaPmtList &CaPmtList);
276 void MtdEnable(void);
279 int MtdPutData(uchar *Data, int Count);
283public:
284 bool McdAvailable(void) { return RepliesToQuery(); }
286 bool MtdAvailable(void) { return mtdAvailable; }
288 bool MtdActive(void) { return mtdHandler != NULL; }
290public:
291 cCamSlot(cCiAdapter *CiAdapter, bool WantsTsData = false, cCamSlot *MasterSlot = NULL);
305 virtual ~cCamSlot();
306 bool IsMasterSlot(void) { return !masterSlot; }
309 cCamSlot *MasterSlot(void) { return masterSlot ? masterSlot : this; }
317 void TriggerResendPmt(void) { resendPmt = true; }
319 virtual bool Assign(cDevice *Device, bool Query = false);
332 cDevice *Device(void) { return assignedDevice; }
334 bool Devices(cVector<int> &DeviceNumbers);
338 bool WantsTsData(void) const { return caPidReceiver != NULL; }
341 int SlotIndex(void) { return slotIndex; }
344 int SlotNumber(void) { return slotNumber; }
350 virtual bool Reset(void);
353 virtual bool CanActivate(void);
356 virtual void StartActivation(void);
366 virtual void CancelActivation(void);
368 virtual bool IsActivating(void);
372 virtual const char *GetCamName(void);
375 virtual bool Ready(void);
377 virtual bool HasMMI(void);
379 virtual bool HasUserIO(void);
382 virtual bool EnterMenu(void);
384 virtual cCiMenu *GetMenu(void);
386 virtual cCiEnquiry *GetEnquiry(void);
388 int Priority(void);
391 virtual bool ProvidesCa(const int *CaSystemIds);
398 virtual void AddPid(int ProgramNumber, int Pid, int StreamType);
401 virtual void SetPid(int Pid, bool Active);
405 virtual void AddChannel(const cChannel *Channel);
410 virtual bool CanDecrypt(const cChannel *Channel, cMtdMapper *MtdMapper = NULL);
422 virtual void StartDecrypting(void);
428 virtual void StopDecrypting(void);
433 virtual bool IsDecrypting(void);
435 virtual uchar *Decrypt(uchar *Data, int &Count);
473 virtual bool TsPostProcess(uchar *Data);
479 virtual bool Inject(uchar *Data, int Count);
485 virtual void InjectEit(int Sid);
491 };
492
493class cCamSlots : public cList<cCamSlot> {
494public:
498 bool WaitForAllCamSlotsReady(int Timeout = 0);
504 };
505
506extern cCamSlots CamSlots;
507
509
510class cChannelCamRelations : public cList<cChannelCamRelation> {
511private:
516 time_t lastCleanup;
517 void Cleanup(void);
518public:
520 void Reset(int CamSlotNumber);
521 bool CamChecked(tChannelID ChannelID, int CamSlotNumber);
522 bool CamDecrypt(tChannelID ChannelID, int CamSlotNumber);
523 void SetChecked(tChannelID ChannelID, int CamSlotNumber);
524 void SetDecrypt(tChannelID ChannelID, int CamSlotNumber);
525 void ClrChecked(tChannelID ChannelID, int CamSlotNumber);
526 void ClrDecrypt(tChannelID ChannelID, int CamSlotNumber);
527 void Load(const char *FileName);
528 void Save(void);
529 };
530
532
533bool CamResponsesLoad(const char *FileName, bool AllowComments = false, bool MustExist = false);
534
535#endif //__CI_H
eModuleStatus
Definition: ci.h:170
Definition: ci.h:232
bool Devices(cVector< int > &DeviceNumbers)
Adds the numbers of any devices that currently use this CAM to the given DeviceNumbers.
virtual bool RepliesToQuery(void)
Returns true if the CAM in this slot replies to queries and thus supports MCD ("Multi Channel Decrypt...
Definition: ci.c:2520
bool MtdActive(void)
Returns true if MTD is currently active.
int Priority(void)
Returns the priority of the device this slot is currently assigned to, or IDLEPRIORITY if it is not a...
cCamSlot * MasterSlot(void)
Returns this CAM slot's master slot, or a pointer to itself if it is a master slot.
Definition: ci.h:309
cMutex mutex
Definition: ci.h:238
void DeleteAllConnections(void)
int source
Definition: ci.h:252
int slotIndex
Definition: ci.h:245
cCiSession * GetSessionByResourceId(uint32_t ResourceId)
void KeepSharedCaPids(int ProgramNumber, const int *CaSystemIds, int *CaPids)
void TriggerResendPmt(void)
Tells this CAM slot to resend the list of CA_PMTs to the CAM.
virtual ~cCamSlot()
virtual bool TsPostProcess(uchar *Data)
If there is a cCiSession that needs to do additional processing on TS packets (after the CAM has done...
void MtdEnable(void)
Enables MTD support for this CAM.
bool McdAvailable(void)
Returns true if this CAM supports MCD ("Multi Channel Decyption").
cCiAdapter * ciAdapter
Definition: ci.h:240
virtual bool Ready(void)
Returns 'true' if the CAM in this slot is ready to decrypt.
cMtdHandler * mtdHandler
Definition: ci.h:256
int MasterSlotNumber(void)
Returns the number of this CAM's master slot within the whole system.
virtual bool Inject(uchar *Data, int Count)
Sends all Count bytes of the given Data to the CAM, and returns true if this was possible.
bool mtdAvailable
Definition: ci.h:255
void Process(cTPDU *TPDU=NULL)
cCamSlot * masterSlot
Definition: ci.h:241
void MtdActivate(bool On)
Activates (On == true) or deactivates (On == false) MTD.
virtual cCiEnquiry * GetEnquiry(void)
Gets a pending enquiry, or NULL if there is no enquiry.
eModuleStatus lastModuleStatus
Definition: ci.h:248
virtual bool RepliesToQuery(void)
Returns true if the CAM in this slot replies to queries and thus supports MCD ("Multi Channel Decrypt...
void NewConnection(void)
bool resendPmt
Definition: ci.h:251
virtual bool IsActivating(void)
Returns true if this CAM slot is currently activating a smart card.
virtual void StartDecrypting(void)
Sends all CA_PMT entries to the CAM that have been modified since the last call to this function.
virtual void AddPid(int ProgramNumber, int Pid, int StreamType)
Adds the given PID information to the list of PIDs.
virtual bool HasUserIO(void)
Returns true if there is a pending user interaction, which shall be retrieved via GetMenu() or GetEnq...
int transponder
Definition: ci.h:253
virtual bool EnterMenu(void)
Requests the CAM in this slot to start its menu.
virtual void SendCaPmt(uint8_t CmdId)
time_t resetTime
Definition: ci.h:249
bool WantsTsData(void) const
Returns true if this CAM slot wants to receive the TS data through its Decrypt() function.
Definition: ci.h:338
cCaActivationReceiver * caActivationReceiver
Definition: ci.h:244
cTimeMs moduleCheckTimer
Definition: ci.h:250
cDevice * Device(void)
Returns the device this CAM slot is currently assigned to.
Definition: ci.h:332
cCondVar processed
Definition: ci.h:239
virtual void StopDecrypting(void)
Clears the list of CA_PMT entries and tells the CAM to stop decrypting.
virtual bool CanActivate(void)
Returns true if there is a CAM in this slot that can be put into activation mode.
virtual cCiMenu * GetMenu(void)
Gets a pending menu, or NULL if there is no menu.
virtual bool HasMMI(void)
Returns 'true' if the CAM in this slot has an active MMI.
int slotNumber
Definition: ci.h:246
virtual bool IsDecrypting(void)
Returns true if the CAM in this slot is currently used for decrypting.
bool MtdAvailable(void)
Returns true if this CAM supports MTD ("Multi Transponder Decryption").
virtual eModuleStatus ModuleStatus(void)
Returns the status of the CAM in this slot.
virtual void SetPid(int Pid, bool Active)
Sets the given Pid (which has previously been added through a call to AddPid()) to Active.
int MtdPutData(uchar *Data, int Count)
Sends at most Count bytes of the given Data to the individual MTD CAM slots that are using this CAM.
virtual bool Assign(cDevice *Device, bool Query=false)
Assigns this CAM slot to the given Device, if this is possible.
void Write(cTPDU *TPDU)
int SlotIndex(void)
Returns the index of this CAM slot within its CI adapter.
cList< cCiCaProgramData > caProgramList
Definition: ci.h:254
virtual bool Reset(void)
Resets the CAM in this slot.
virtual const char * GetCamName(void)
Returns the name of the CAM in this slot, or NULL if there is no ready CAM in this slot.
virtual void StartActivation(void)
Puts the CAM in this slot into a mode where an inserted smart card can be activated.
virtual void InjectEit(int Sid)
Injects a generated EIT with a "present event" for the given Sid into the TS data stream sent to the ...
virtual bool CanDecrypt(const cChannel *Channel, cMtdMapper *MtdMapper=NULL)
Returns true if there is a CAM in this slot that is able to decrypt the given Channel (or at least cl...
virtual void CancelActivation(void)
Cancels a previously started activation (if any).
virtual uchar * Decrypt(uchar *Data, int &Count)
If this is a CAM slot that can be freely assigned to any device, but will not be directly inserted in...
void SendCaPmts(cCiCaPmtList &CaPmtList)
Sends the given list of CA_PMTs to the CAM.
virtual bool ProvidesCa(const int *CaSystemIds)
Returns true if the CAM in this slot provides one of the given CaSystemIds.
cCamSlot(cCiAdapter *CiAdapter, bool WantsTsData=false, cCamSlot *MasterSlot=NULL)
Creates a new CAM slot for the given CiAdapter.
cCamSlot * MtdSpawn(void)
If this CAM slot can do MTD ("Multi Transponder Decryption"), a call to this function returns a cMtdC...
void BuildCaPmts(uint8_t CmdId, cCiCaPmtList &CaPmtList, cMtdMapper *MtdMapper=NULL)
Generates all CA_PMTs with the given CmdId and stores them in the given CaPmtList.
cCiTransportConnection * tc[MAX_CONNECTIONS_PER_CAM_SLOT+1]
Definition: ci.h:247
cCaPidReceiver * caPidReceiver
Definition: ci.h:243
virtual void AddChannel(const cChannel *Channel)
Adds all PIDs of the given Channel to the current list of PIDs.
int SlotNumber(void)
Returns the number of this CAM slot within the whole system.
cDevice * assignedDevice
Definition: ci.h:242
virtual const int * GetCaSystemIds(void)
bool IsMasterSlot(void)
Returns true if this CAM slot itself is a master slot (which means that it doesn't have a pointer to ...
Definition: ci.h:493
bool WaitForAllCamSlotsReady(int Timeout=0)
Waits until all CAM slots have become ready, or the given Timeout (seconds) has expired.
int NumReadyMasterSlots(void)
Returns the number of master CAM slots in the system that are ready to decrypt.
void ClrDecrypt(tChannelID ChannelID, int CamSlotNumber)
void Load(const char *FileName)
void SetChecked(tChannelID ChannelID, int CamSlotNumber)
cMutex mutex
Definition: ci.h:512
bool CamDecrypt(tChannelID ChannelID, int CamSlotNumber)
bool CamChecked(tChannelID ChannelID, int CamSlotNumber)
time_t lastCleanup
Definition: ci.h:516
cChannelCamRelation * GetEntry(tChannelID ChannelID)
cChannelCamRelation * AddEntry(tChannelID ChannelID)
void SetDecrypt(tChannelID ChannelID, int CamSlotNumber)
cString fileName
Definition: ci.h:513
void ClrChecked(tChannelID ChannelID, int CamSlotNumber)
void Reset(int CamSlotNumber)
Definition: ci.h:172
cCamSlot * camSlots[MAX_CAM_SLOTS_PER_ADAPTER]
Definition: ci.h:175
virtual bool Reset(int Slot)
Resets the CAM in the given Slot.
virtual ~cCiAdapter()
The derived class must call Cancel(3) in its destructor.
cCiAdapter(void)
virtual void Action(void)
Handles the attached CAM slots in a separate thread.
virtual eModuleStatus ModuleStatus(int Slot)
Returns the status of the CAM in the given Slot.
cCamSlot * ItCamSlot(int &Iter)
Iterates over all added CAM slots of this adapter.
virtual bool Assign(cDevice *Device, bool Query=false)
Assigns this adapter to the given Device, if this is possible.
void AddCamSlot(cCamSlot *CamSlot)
Adds the given CamSlot to this CI adapter.
virtual void Write(const uint8_t *Buffer, int Length)
Writes Length bytes of the given Buffer.
virtual int Read(uint8_t *Buffer, int MaxLength)
Reads one chunk of data into the given Buffer, up to MaxLength bytes.
const char * GetMenuString(void)
virtual void Process(int Length=0, const uint8_t *Data=NULL)
uint16_t manufacturerCode
Definition: ci.h:73
virtual ~cCiApplicationInformation()
cCiApplicationInformation(uint16_t SessionId, cCiTransportConnection *Tc)
uint8_t applicationType
Definition: ci.h:71
uint16_t applicationManufacturer
Definition: ci.h:72
Definition: ci.c:935
Definition: ci.h:148
char * text
Definition: ci.h:154
void Cancel(void)
int ExpectedLength(void)
cCiEnquiry(cCiMMI *MMI)
const char * Text(void)
bool Blind(void)
cMutex * mutex
Definition: ci.h:153
void Abort(void)
cCiMMI * mmi
Definition: ci.h:152
void Reply(const char *s)
bool blind
Definition: ci.h:155
int expectedLength
Definition: ci.h:156
Definition: ci.c:1405
Definition: ci.h:119
bool selectable
Definition: ci.h:126
void Abort(void)
bool HasUpdate(void)
const char * TitleText(void)
const char * BottomText(void)
int NumEntries(void)
void Cancel(void)
bool Selectable(void)
Definition: ci.h:141
int numEntries
Definition: ci.h:131
bool AddEntry(char *s)
const char * SubTitleText(void)
cCiMenu(cCiMMI *MMI, bool Selectable)
cMutex * mutex
Definition: ci.h:125
@ MAX_CIMENU_ENTRIES
Definition: ci.h:123
char * entries[MAX_CIMENU_ENTRIES]
Definition: ci.h:130
cCiMMI * mmi
Definition: ci.h:124
char * bottomText
Definition: ci.h:129
char * titleText
Definition: ci.h:127
void Select(int Index)
const char * Entry(int n)
char * subTitleText
Definition: ci.h:128
cCiResourceHandler(void)
Creates a new resource handler, through which the available resources can be provides.
virtual const uint32_t * ResourceIds(void) const =0
Returns a pointer to an array of resource identifiers, where the last value is zero.
virtual ~cCiResourceHandler()
virtual cCiSession * GetNewCiSession(uint32_t ResourceId, uint16_t SessionId, cCiTransportConnection *Tc)=0
Returns a new cCiSession, according to the given ResourceId.
cVector< uint32_t > resourceIds
Definition: ci.h:101
cCiResourceHandlers(void)
Creates the default list of resourceIds.
const uint32_t * Ids(void)
void Register(cCiResourceHandler *ResourceHandler)
Adds the given ResourceHandler to the list of resource handlers and appends its ResourceIds to the gl...
cCiSession * GetNewCiSession(uint32_t ResourceId, uint16_t SessionId, cCiTransportConnection *Tc)
Definition: ci.h:32
virtual ~cCiSession()
uint16_t SessionId(void)
Definition: ci.h:52
int GetTag(int &Length, const uint8_t **Data)
void SendData(int Tag, int Length=0, const uint8_t *Data=NULL)
uint32_t resourceId
Definition: ci.h:35
cCiSession(uint16_t SessionId, uint32_t ResourceId, cCiTransportConnection *Tc)
void SetResourceId(uint32_t Id)
If this is a class that has been derived from an existing cCiSession class, but implements a differen...
uint32_t ResourceId(void)
Definition: ci.h:53
virtual bool TsPostProcess(uint8_t *TsPacket)
If this cCiSession needs to do additional processing on TS packets (after the CAM has done the decryp...
cCamSlot * CamSlot(void)
const uint8_t * GetData(const uint8_t *Data, int &Length)
virtual bool HasUserIO(void)
virtual void Process(int Length=0, const uint8_t *Data=NULL)
void SetTsPostProcessor(void)
If this cCiSession implements the TsPostProcess() function, it shall call SetTsPostProcessor() to reg...
uint16_t sessionId
Definition: ci.h:34
cCiTransportConnection * tc
Definition: ci.h:36
cCiTransportConnection * Tc(void)
cMtdMapper * MtdMapper(void)
Definition: ci.c:505
int Size(void) const
cChannelCamRelations ChannelCamRelations
Definition: ci.c:2947
@ msReady
@ msPresent
@ msNone
@ msReset
cCamSlots CamSlots
Definition: ci.c:2838
cCiResourceHandlers CiResourceHandlers
Definition: ci.c:1777
#define MAX_CAM_SLOTS_PER_ADAPTER
bool CamResponsesLoad(const char *FileName, bool AllowComments=false, bool MustExist=false)
Definition: ci.c:481
#define MAX_CONNECTIONS_PER_CAM_SLOT
unsigned char uchar
void Del(cCiCaPmt *CaPmt)
cVector< cCiCaPmt * > caPmts
Definition: ci.h:226
cCiCaPmt * Add(uint8_t CmdId, int Source, int Transponder, int ProgramNumber, const int *CaSystemIds)