/*
 * 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
 */

#ifdef CONSTCW

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <typeinfo>

//#include <vdr/channels.h>
//#include <vdr/sources.h>

#include "common.h"
#include "system.h"
#include "system-common.h"
#include "data.h"
#include "misc.h"
#include "..\\main.h"
//#include "vdr-compat.h"

#define SYSTEM_NAME          "ConstCW"
#define SYSTEM_PRI           -20

// -- cPlainKeyConstCw ----------------------------------------------------------------

#define PLAINLEN_CW 16

class cPlainKeyConstCw : public cHexKey {
private:
  int prgId, source, transponder;
public:
  cPlainKeyConstCw(bool Super);
  virtual bool Parse(char *line);
  virtual bool Save(FILE *f);
  bool Matches(const cEcmInfo *ecm);
  };

static cPlainKeyTypeReg<cPlainKeyConstCw,'X'> KeyReg;

cPlainKeyConstCw::cPlainKeyConstCw(bool Super)
:cHexKey(Super)
{}

bool cPlainKeyConstCw::Matches(const cEcmInfo *ecm)
{
  return ecm->prgId==prgId && ecm->source==source && ecm->transponder==transponder;
}

bool cPlainKeyConstCw::Parse(char *line)
{
  unsigned char caid[2], skey[PLAINLEN_CW];

  const char *sline=line;
  if(GetChar(line,&type,1) && GetHex(line,caid,2)) {
    int freq, num;
    char pol, srcBuf[16];
    if(sscanf(line," %d:%c:%15[^:]:%d%n",&freq,&pol,srcBuf,&prgId,&num)==4) {
     // source=cSource::FromString(srcBuf);
     source =0;
      while(freq>20000) freq/=1000;
      transponder=freq;
      line+=num;
      if(GetHex(line,skey,PLAINLEN_CW)) {
        type=toupper(type);
        keynr=Bin2Int(caid,2);
        id=0;
        SetKeyFromBin(skey,PLAINLEN_CW);
        return true;
        }
      }
    }
  //d(printf("constcw-key: bad key format '%s'\n",sline))
  m_DLLInstance.Log("[ConstCW] Key format error. \nLine:'%s'",sline);
  return false;
}

bool cPlainKeyConstCw::Save(FILE *f)
{
  return false;
}

// -- cSystemConstCw ------------------------------------------------------------------

class cSystemConstCw : public cSystem {
public:
  cSystemConstCw(void):cSystem(SYSTEM_NAME,SYSTEM_PRI) {}
  virtual bool ProcessECM(const cEcmInfo *ecm, unsigned char *source);
  };

bool cSystemConstCw::ProcessECM(const cEcmInfo *ecm, unsigned char *source)
{
  cKeySnoop ks(this,'X',0,ecm->caId);
  cPlainKey *pk=0;
  while((pk=keys.FindKey('X',0,ecm->caId,pk))) {
    if(typeid(*pk)==typeid(cPlainKeyConstCw) && ((cPlainKeyConstCw *)pk)->Matches(ecm)) {
      pk->Get(cw);
      ks.OK(pk);
      //m_DLLInstance.StopECMFilter();
      return true;
      }
    }
  return false;
}

// -- cSystemLinkConstCw ------------------------------------------------------------

class cSystemLinkConstCw : public cSystemLink {
public:
  cSystemLinkConstCw(void);
  virtual bool CanHandle(unsigned short SysId);
  virtual cSystem *Create(void) { return new cSystemConstCw; }
  };

static cSystemLinkConstCw staticInit;

cSystemLinkConstCw::cSystemLinkConstCw(void)
:cSystemLink(SYSTEM_NAME,SYSTEM_PRI)
{}

bool cSystemLinkConstCw::CanHandle(unsigned short SysId)
{
  return keys.FindKey('X',0,SysId,0)!=0;
}

#else //CONSTCW

// add dummy loader for CWs
#include "system-common.h"
static cPlainKeyTypeReg<cPlainKeyDummy,'X'> KeyReg;

#endif //CONSTCW
