// BcasCard.h: CBcasCardM クラスのインターフェイス
//
//////////////////////////////////////////////////////////////////////

#pragma once

#include "BcasCard.h"
#include "CasProxy.h"

class CBcasCardM
{
public:
	CBcasCardM();
	~CBcasCardM();

	const DWORD GetCardReaderNum(void) const;
	LPCTSTR GetCardReaderName(const DWORD dwIndex = 0) const;

	const bool OpenCard(LPCTSTR lpszReader = NULL);
	void CloseCard(void);

	const BYTE * GetBcasCardID(void);
	const BYTE * GetInitialCbc(void);
	const BYTE * GetSystemKey(void);
	const BYTE * GetKsFromEcm(const BYTE *pEcmData, const DWORD dwEcmSize);

	const DWORD GetLastError(void) const;

	#define CARD_INNER_ONLY		0
	#define CARD_OUTER_ENABLE	1
	#define CARD_PROXY_ENABLE	2
	
	void SetBcasStatus(DWORD outer_card) { m_ReaderMethod = outer_card; }
	void SetBcasProxyAdr(LPCTSTR host,DWORD port){ m_CasProxy.SetAddress(host,port); }

protected:
	const bool EnumCardReader(void);
	const bool TransmitCommand(const BYTE *pSendData, const DWORD dwSendSize, BYTE *pRecvData, const DWORD dwMaxRecv, DWORD *pdwRecvSize = NULL);
	const bool InitialSetting(void);


	struct TAG_BCASCARDINFO
	{
		BYTE BcasCardID[6];		// Card ID
		BYTE SystemKey[32];		// Descrambling system key
		BYTE InitialCbc[8];		// Descrambler CBC initial value
	} m_BcasCardInfo;
	
	struct TAG_ECMSTATUS
	{
		DWORD dwLastEcmSize;	// 最後に問い合わせのあったECMサイズ
		BYTE LastEcmData[256];	// 最後に問い合わせのあったECMデータ
		BYTE KsData[16];		// Ks Odd + Even	
	} m_EcmStatus;
	
	DWORD m_dwLastError;

private:
	const bool StoreEcmData(const BYTE *pEcmData, const DWORD dwEcmSize);

	BOOL m_UseOuterReader;
	BOOL m_UseProxyReader;
	CBcasCard m_Reader;
	DWORD m_ReaderMethod;
	CCasProxy m_CasProxy;
};
