/*
 * Softcam plugin to VDR (C++)
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#ifndef ___SYSTEM_CC_H
#define ___SYSTEM_CC_H

#include "vdr/thread.h"
#include "system.h"
#include "misc.h"

#define DEBUG_CC

#ifdef DEBUG_CC
#define dc(x) { (x); }
#else
#define dc(x) ; 
#endif

// ----------------------------------------------------------------

class cEcmInfo;
class cNetSocket;
class cCardClients;
class cSystemCardClient;

// ----------------------------------------------------------------

#define MAX_CC_CAID 16

class cCardClient : public cSimpleItem, protected cMutex {
friend class cSystemCardClient;
protected:
	const char *name;
	char hostname[64];
	int port;
	bool emmAllowed;
	int emmCaid[MAX_CC_CAID], emmMask[MAX_CC_CAID], numCaid;
	cMsgCache msECM, msEMM;
	//
	bool ParseStdConfig(const char *config, int *num=0);
	virtual bool SendMsg(cNetSocket *so, const unsigned char *data, int len);
	virtual int RecvMsg(cNetSocket *so, unsigned char *data, int len, int to=-1);
	virtual bool Login(void) { return false; }
	bool Immediate(void);
public:
	cCardClient(const char *Name);
	virtual bool Init(const char *config)=0;
	virtual bool CanHandle(unsigned short SysId);
	virtual bool ProcessECM(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw)=0;
	const char *Name(void) { return name; }
};

// ----------------------------------------------------------------

class cCardClientLink {
friend class cCardClients;
private:
	cCardClientLink *next;
protected:
	const char *name;
public:
	cCardClientLink(const char *Name);
	virtual ~cCardClientLink() {};
	virtual cCardClient *Create(void)=0;
};

// ----------------------------------------------------------------

template<class CC> class cCardClientLinkReg : public cCardClientLink {
public:
	cCardClientLinkReg(const char *Name):cCardClientLink(Name) {}
	virtual cCardClient *Create(void) { return new CC(name); }
};

#endif //___SYSTEM_CC_H