// ScbcCopyKey.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "ScbcCopyKey.h"
#include "OTA.h"
#include "CurlClient.h"
#include "cJSON.h"
#include "CharEncoding.h"
#ifdef __MAKE_PYD__
#include "Python.h"
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CSIACP g_objSiacp;
std::string g_strData;
std::string g_httpData;

bool ReadKeyFile(const char *lpfile, std::string& data)
{
	if (!lpfile || _taccess(lpfile, 0) == -1)
		return false;

	FILE* pf = NULL;
	if (_tfopen_s(&pf, lpfile, _T("rb")) == 0)
	{
		if (pf)
		{
			fseek(pf, 0, SEEK_END);
			size_t fs = ftell(pf);
			fseek(pf, 0, SEEK_SET);

			byte* pdata = (byte*)malloc(fs);
			if (pdata)
			{
				fread(pdata, fs, 1, pf);
				fclose(pf);

				data.append((char*)pdata, fs);
				free(pdata);

				return true;
			}

			fclose(pf);
		}
	}

	return false;
}

// 唯一的应用程序对象
#ifdef __CONSOLE__
CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// 初始化 MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{
		std::string token = HTTP_Login("http://test.dsp.server.qhmoka.com/login", "auto", "123456", TRUE);
		if ( token.size() )
		{
			int userId = HTTP_GetUserId("http://test.dsp.server.qhmoka.com/api/automation/getUserId", token.c_str(), "MOKA-AF-MS6586-0000L", "fe6015c2966118d45e745d3bdeabc052f87e7847", "5c:ad:76:e0:55:79");
		}
		// TODO: 在此处为应用程序的行为编写代码。
		if ( Open(5, 115200, 8, 0, 1) )
		{
			if ( EnterFactory() )
			{
				/*printf("%d\n",GetProjectId());
				printf("%s\n",GetSoftVersion());
				printf("%s\n",GetDeviceId());
				printf("%s\n",GetClientType());
				printf("%s\n",GetMAC());
				printf("%s\n",GetHDCPKey());
				printf("%s\n",GetHDCPKey22());
				printf("%s\n",GetWidi());
				printf("%s\n",GetNetflixESN());
				printf("%s\n",GetWidevine());
				printf("%s\n",GetCiKey());
				printf("%s\n",GetOSDLanguage());
				printf("%s\n",GetShopLanguage());
				printf("%s\n",GetChannel());

				ShowFactoryMenu();
				HideFactoryMenu();

				ShowFactoryInformation();
				HideFactoryInformation();

				int min = 0;
				ReadAgingTime(min);
				*/

				SetRedGainRegister(100);
				SetRedGainRegister(300);
				

				LeaveFactory();
			}
			else
			{
				printf("进入工厂模式失败\n");
			}
			
			Close();
		}
		else
		{
			printf("打开串口失败\n");
		}
	}

	system("pause");

	return nRetCode;
}
#endif

#ifndef __MAKE_PYD__

SCBCCOPYKEY_API BOOL Open(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
{
	return g_objSiacp.OpenComm(nPort, dwBaudrate);
}

SCBCCOPYKEY_API void Close()
{
	g_objSiacp.CloseComm();
}

SCBCCOPYKEY_API BOOL IsOpen()
{
	return g_objSiacp.IsOpen();
}

SCBCCOPYKEY_API BOOL EnterFactory()
{
	return g_objSiacp.SCBC_EnterFactory();
}

SCBCCOPYKEY_API BOOL LeaveFactory()
{
	return g_objSiacp.SCBC_LeaveFactory();
}

//////////////////////////////////////////////////////////////////////////
// 读取;
SCBCCOPYKEY_API INT GetProjectId()
{
	int pid = 0;
	if ( g_objSiacp.SCBC_GetProjectId(pid) )
		return pid;

	// 获取失败返回-1;
	return -1;
}

SCBCCOPYKEY_API LPCTSTR GetSoftVersion()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetSoftVersion(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetDeviceId()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetDeviceId(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetClientType()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetClientType(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetMAC()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetMAC(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetHDCPKey()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetHDCPKey(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetHDCPKey22()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetHDCPKey22(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetWidi()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetWidi(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetNetflixESN()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetNetflixESN(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetWidevine()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetWidevine(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetCiKey()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetCiKey(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetOSDLanguage()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetOSDLanguage(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetShopLanguage()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetShopLanguage(g_strData) )
		return g_strData.c_str();

	return _T("");
}

SCBCCOPYKEY_API LPCTSTR GetChannel()
{
	g_strData.clear();
	if ( g_objSiacp.SCBC_GetChannel(g_strData) )
		return g_strData.c_str();

	return _T("");
}

//////////////////////////////////////////////////////////////////////////
// 设置;
SCBCCOPYKEY_API BOOL SetProjectId(int pid)
{
	return g_objSiacp.SCBC_SetProjectId(pid);
}

SCBCCOPYKEY_API BOOL SetProjectId(LPCTSTR lpPid)
{
	if ( lpPid == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetProjectId(lpPid);
}

SCBCCOPYKEY_API BOOL SetProjectId(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetProjectId(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetDeviceId(LPCTSTR lpDeviceId)
{
	if ( lpDeviceId == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetDeviceId(lpDeviceId);
}

SCBCCOPYKEY_API BOOL SetDeviceId(const byte* pBuffer, const int &nLen)
{
	return g_objSiacp.SCBC_SetDeviceId(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetMAC(LPCTSTR lpMac)
{
	if ( lpMac == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetMAC(lpMac);
}

SCBCCOPYKEY_API BOOL SetMAC(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetMAC(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetHDCPKey(LPCTSTR lpHDCP, BOOL bHasSpace)
{
	if ( lpHDCP == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetHDCPKey(lpHDCP, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetHDCPKey(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetHDCPKey(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetHDCPKey22(LPCTSTR lpHDCP22, BOOL bHasSpace)
{
	if ( lpHDCP22 == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetHDCPKey22(lpHDCP22, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetHDCPKey22(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetHDCPKey22(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetNetflixESN(LPCTSTR lpESN, BOOL bHasSpace)
{
	if ( lpESN == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetNetflixESN(lpESN, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetNetflixESN(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetNetflixESN(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetWidi(LPCTSTR lpWidi, BOOL bHasSpace)
{
	if ( lpWidi == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetWidi(lpWidi, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetWidi(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetWidi(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetWidevine(LPCTSTR lpWidevine, BOOL bHasSpace)
{
	if ( lpWidevine == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetWidevine(lpWidevine, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetWidevine(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetWidevine(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetCiKey(LPCTSTR lpCiKey, BOOL bHasSpace)
{
	if ( lpCiKey == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetCiKey(lpCiKey, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetCiKey(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetCiKey(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetOSDLanguage(LPCTSTR lan, BOOL bHasSpace)
{
	if ( lan == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetOSDLanguage(lan, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetOSDLanguage(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetOSDLanguage(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetShopLanguage(LPCTSTR lan, BOOL bHasSpace)
{
	if ( lan == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetShopLanguage(lan, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetShopLanguage(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetShopLanguage(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetChannel(LPCTSTR channel, BOOL bHasSpace)
{
	if ( channel == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetChannel(channel, bHasSpace);
}

SCBCCOPYKEY_API BOOL SetChannel(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetChannel(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetWBNormal(LPCTSTR data)
{
	if ( data == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetWBNormal(data);
}

SCBCCOPYKEY_API BOOL SetWBNormal(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetWBNormal(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetWBCool(LPCTSTR data)
{
	if ( data == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetWBCool(data);
}

SCBCCOPYKEY_API BOOL SetWBCool(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetWBCool(pBuffer, nLen);
}

SCBCCOPYKEY_API BOOL SetWBWarm(LPCTSTR data)
{
	if ( data == NULL)
		return FALSE;
	return g_objSiacp.SCBC_SetWBWarm(data);
}

SCBCCOPYKEY_API BOOL SetWBWarm(const byte* pBuffer, const int& nLen)
{
	return g_objSiacp.SCBC_SetWBWarm(pBuffer, nLen);
}

//////////////////////////////////////////////////////////////////////////
// 检验;
SCBCCOPYKEY_API BOOL CheckDeviceId()
{
	return g_objSiacp.SCBC_CheckDeviceId();
}

SCBCCOPYKEY_API BOOL CheckMAC()
{
	return g_objSiacp.SCBC_CheckMAC();
}

SCBCCOPYKEY_API BOOL CheckHDCP()
{
	return g_objSiacp.SCBC_CheckHDCP();
}

SCBCCOPYKEY_API BOOL CheckHDCP22()
{
	return g_objSiacp.SCBC_CheckHDCP22();
}

SCBCCOPYKEY_API BOOL CheckNetflixESN()
{
	return g_objSiacp.SCBC_CheckNetflixESN();
}

SCBCCOPYKEY_API BOOL CheckWidi()
{
	return g_objSiacp.SCBC_CheckWidi();
}

SCBCCOPYKEY_API BOOL CheckWidevine()
{
	return g_objSiacp.SCBC_CheckWidevine();
}

SCBCCOPYKEY_API BOOL CheckCikey()
{
	return g_objSiacp.SCBC_CheckCikey();
}

SCBCCOPYKEY_API BOOL StarWarmUpMode()
{
	return g_objSiacp.SCBC_StarWarmUpMode();
}

SCBCCOPYKEY_API BOOL StopWarmUpMode()
{
	return g_objSiacp.SCBC_StopWarmUpMode();
}

SCBCCOPYKEY_API BOOL ShowFactoryMenu()
{
	return g_objSiacp.SCBC_ShowFactoryMenu();
}

SCBCCOPYKEY_API BOOL HideFactoryMenu()
{
	return g_objSiacp.SCBC_HideFactoryMenu();
}

SCBCCOPYKEY_API BOOL ShowFactoryInformation()
{
	return g_objSiacp.SCBC_ShowFactoryInformation();
}

SCBCCOPYKEY_API BOOL HideFactoryInformation()
{
	return g_objSiacp.SCBC_HideFactoryInformation();
}

SCBCCOPYKEY_API BOOL EnterAgingModel()
{
	return g_objSiacp.SCBC_EnterAgingModel();
}

SCBCCOPYKEY_API BOOL LeaveAgingModel()
{
	return g_objSiacp.SCBC_LeaveAgingModel();
}

SCBCCOPYKEY_API BOOL ReadAgingTime(int &min)
{
	return g_objSiacp.SCBC_ReadAgingTime(min);
}

SCBCCOPYKEY_API BOOL SetRedGainRegister(int value)
{
	return g_objSiacp.SCBC_SetRedGainRegister(value);
}

SCBCCOPYKEY_API BOOL SetGreenGainRegister(int value)
{
	return g_objSiacp.SCBC_SetGreenGainRegister(value);
}

SCBCCOPYKEY_API BOOL SetBlueGainRegister(int value)
{
	return g_objSiacp.SCBC_SetBlueGainRegister(value);
}

SCBCCOPYKEY_API BOOL SetRedOffsetRegister(int value)
{
	return g_objSiacp.SCBC_SetRedOffsetRegister(value);
}

SCBCCOPYKEY_API BOOL SetGreenOffsetRegister(int value)
{
	return g_objSiacp.SCBC_SetGreenOffsetRegister(value);
}

SCBCCOPYKEY_API BOOL SetBlueOffsetRegister(int value)
{
	return g_objSiacp.SCBC_SetBlueOffsetRegister(value);
}

#else

static PyObject* Open(PyObject* self, PyObject* args)
{
	int nPort;
	int Baudrate;
	int ByteSize, Parity, StopBits;
	if (!PyArg_ParseTuple(args, "iiiii", &nPort, &Baudrate, &ByteSize, &Parity, &StopBits ))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.OpenComm(nPort, Baudrate));
}

static PyObject* Close(PyObject* self, PyObject* args)
{
	g_objSiacp.CloseComm();
	return Py_None;
}

static PyObject* IsOpen(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.IsOpen());
}

static PyObject* EnterFactory(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_EnterFactory());
}

static PyObject* LeaveFactory(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_LeaveFactory());
}

//////////////////////////////////////////////////////////////////////////
// 读取;
static PyObject* GetProjectId(PyObject* self, PyObject* args)
{
	int pid = -1;
	g_objSiacp.SCBC_GetProjectId(pid);
	// 获取失败返回-1;
	return Py_BuildValue("i", pid);
}

static PyObject* GetSoftVersion(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetSoftVersion(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetDeviceId(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetDeviceId(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetClientType(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetClientType(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetMAC(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetMAC(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetHDCPKey(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetHDCPKey(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetHDCPKey22(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetHDCPKey22(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetWidi(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetWidi(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetNetflixESN(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetNetflixESN(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetWidevine(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetWidevine(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetCiKey(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetCiKey(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetOSDLanguage(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetOSDLanguage(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetShopLanguage(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetShopLanguage(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

static PyObject* GetChannel(PyObject* self, PyObject* args)
{
	g_strData.clear();
	g_objSiacp.SCBC_GetChannel(g_strData);
	// 获取失败返回"";
	return Py_BuildValue("s", g_strData.c_str());
}

//////////////////////////////////////////////////////////////////////////
// 设置;
static PyObject* SetProjectId(PyObject* self, PyObject* args)
{
	int pid = 0;
	if (!PyArg_ParseTuple(args, "i", &pid))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetProjectId(pid));
}

static PyObject* SetDeviceId(PyObject* self, PyObject* args)
{
	const char* pszdata= 0;
	if (!PyArg_ParseTuple(args, "s", &pszdata))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetDeviceId(pszdata));
}

static PyObject* SetDeviceIdBF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetDeviceId((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetMAC(PyObject* self, PyObject* args)
{
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "s", &pszdata))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetMAC(pszdata));
}

static PyObject* SetMACBF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetMAC((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetHDCPKey(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetHDCPKey(pszdata, bHasSpace));
}

static PyObject* SetHDCPKeyBF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetHDCPKey((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetHDCPKey22(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetHDCPKey22(pszdata, bHasSpace));
}

static PyObject* SetHDCPKey22BF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetHDCPKey22((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetNetflixESN(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetNetflixESN(pszdata, bHasSpace));
}

static PyObject* SetNetflixESNBF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetNetflixESN((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetWidi(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetWidi(pszdata, bHasSpace));
}

static PyObject* SetWidiBF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetWidi((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetWidevine(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetWidevine(pszdata, bHasSpace));
}

static PyObject* SetWidevineBF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetWidevine((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetCiKey(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetCiKey(pszdata, bHasSpace));
}

static PyObject* SetCiKeyBF(PyObject* self, PyObject* args)
{
	const char* pszfile = 0;
	if (!PyArg_ParseTuple(args, "s", &pszfile))
		return NULL;

	// 打开文件;
	std::string data;
	if ( ReadKeyFile(pszfile, data) )
	{
		return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetCiKey((BYTE*)data.c_str(), data.size()));
	}

	return Py_BuildValue("b", false);
}

static PyObject* SetOSDLanguage(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetOSDLanguage(pszdata, bHasSpace));
}

static PyObject* SetShopLanguage(PyObject* self, PyObject* args)
{
	BOOL bHasSpace; 
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "sb", &pszdata, &bHasSpace))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetShopLanguage(pszdata, bHasSpace));
}

static PyObject* SetChannel(PyObject* self, PyObject* args)
{
	int value = 0;
	if (!PyArg_ParseTuple(args, "i", &value))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetChannel(value));
}

static PyObject* SetWBNormal(PyObject* self, PyObject* args)
{
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "s", &pszdata))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetWBNormal(pszdata));
}

static PyObject* SetWBCool(PyObject* self, PyObject* args)
{
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "s", &pszdata))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetWBCool(pszdata));
}

static PyObject* SetWBWarm(PyObject* self, PyObject* args)
{
	const char* pszdata = 0;
	if (!PyArg_ParseTuple(args, "s", &pszdata))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetWBWarm(pszdata));
}
//////////////////////////////////////////////////////////////////////////
// 校验;
static PyObject* CheckDeviceId(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckDeviceId());
}

static PyObject* CheckMAC(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckMAC());
}

static PyObject* CheckHDCP(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckHDCP());
}

static PyObject* CheckHDCP22(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckHDCP22());
}

static PyObject* CheckNetflixESN(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckNetflixESN());
}

static PyObject* CheckWidi(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckWidi());
}

static PyObject* CheckWidevine(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckWidevine());
}

static PyObject* CheckCikey(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_CheckCikey());
}

static PyObject* StarWarmUpMode(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_StarWarmUpMode());
}

static PyObject* StopWarmUpMode(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_StopWarmUpMode());
}

//
static PyObject* ShowFactoryMenu(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_ShowFactoryMenu());
}

static PyObject* HideFactoryMenu(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_HideFactoryMenu());
}

static PyObject* ShowFactoryInformation(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_ShowFactoryInformation());
}

static PyObject* HideFactoryInformation(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_HideFactoryInformation());
}

static PyObject* EnterAgingModel(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_EnterAgingModel());
}

static PyObject* LeaveAgingModel(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_LeaveAgingModel());
}

static PyObject* ReadAgingTime(PyObject* self, PyObject* args)
{
	int min = 0;
	if ( !g_objSiacp.SCBC_ReadAgingTime(min) )
		return Py_BuildValue("i", -1);
	return Py_BuildValue("i", min);
}

static PyObject* SetRedGainRegister(PyObject* self, PyObject* args)
{
	int value = 0;
	if (!PyArg_ParseTuple(args, "i", &value))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetRedGainRegister(value));
}

static PyObject* SetGreenGainRegister(PyObject* self, PyObject* args)
{
	int value = 0;
	if (!PyArg_ParseTuple(args, "i", &value))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetGreenGainRegister(value));
}

static PyObject* SetBlueGainRegister(PyObject* self, PyObject* args)
{
	int value = 0;
	if (!PyArg_ParseTuple(args, "i", &value))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetBlueGainRegister(value));
}

static PyObject* SetRedOffsetRegister(PyObject* self, PyObject* args)
{
	int value = 0;
	if (!PyArg_ParseTuple(args, "i", &value))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetRedOffsetRegister(value));
}

static PyObject* SetGreenOffsetRegister(PyObject* self, PyObject* args)
{
	int value = 0;
	if (!PyArg_ParseTuple(args, "i", &value))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetGreenOffsetRegister(value));
}

static PyObject* SetBlueOffsetRegister(PyObject* self, PyObject* args)
{
	int value = 0;
	if (!PyArg_ParseTuple(args, "i", &value))
		return NULL;

	return Py_BuildValue("b", (bool)g_objSiacp.SCBC_SetBlueOffsetRegister(value));
}

static PyObject* HTTPLogin(PyObject* self, PyObject* args)
{
	int rememberMe = 0;
	const char* pszhost = NULL;
	const char* pszUserName = NULL;
	const char* pszPassword = NULL;
	
	if (!PyArg_ParseTuple(args, "sssb", &pszhost, &pszUserName, &pszPassword, &rememberMe))
		return NULL;

	HTTP_Login(pszhost, pszUserName, pszPassword, rememberMe); 

	return Py_BuildValue("s", g_httpData.c_str());
}

static PyObject* HTTPGetUserId(PyObject* self, PyObject* args)
{
	const char* pszhost = NULL;
	const char* pszToken = NULL;
	const char* pszClientType = NULL;
	const char* pszDeviceId = NULL;
	const char* pszMAC = NULL;
	if (!PyArg_ParseTuple(args, "sssss", &pszhost, &pszToken, &pszClientType, &pszDeviceId, &pszMAC))
		return NULL;

	int userId = HTTP_GetUserId(pszhost, pszToken, pszClientType, pszDeviceId, pszMAC); 

	return Py_BuildValue("i", userId);
}



// 描述方法,暴露给python的函数;
static PyMethodDef ScbcCopyKey_Methods[] = {
	{"Open",Open,METH_VARARGS,"打开串口"},
	{"Close", Close, METH_VARARGS, "关闭串口"},
	{"IsOpen", IsOpen, METH_VARARGS, "串口是否打开"},
	{"EnterFactory", EnterFactory, METH_VARARGS, "进入工厂模式"},
	{"LeaveFactory", LeaveFactory, METH_VARARGS, "离开工厂模式"},
	//////////////////////////////////////////////////////////////////////////
	// 读取;
	{"GetProjectId", GetProjectId, METH_VARARGS, "获取pid"},
	{"GetSoftVersion", GetSoftVersion, METH_VARARGS, "获取版本号"},
	{"GetDeviceId", GetDeviceId, METH_VARARGS, "获取DeviceID"},
	{"GetClientType", GetClientType, METH_VARARGS, "获取ClientType"},
	{"GetMAC", GetMAC, METH_VARARGS, "获取MAC"},
	{"GetHDCPKey", GetHDCPKey, METH_VARARGS, "获取HDCP"},
	{"GetHDCPKey22", GetHDCPKey22, METH_VARARGS, "获取HDCP22"},
	{"GetWidi", GetWidi, METH_VARARGS, "获取WIDI"},
	{"GetNetflixESN", GetNetflixESN, METH_VARARGS, "获取ESN"},
	{"GetWidevine", GetWidevine, METH_VARARGS, "获取WIDEVINE"},
	{"GetCiKey", GetCiKey, METH_VARARGS, "获取CIKEY"},
	{"GetOSDLanguage", GetOSDLanguage, METH_VARARGS, "获取OSDLAN"},
	{"GetShopLanguage", GetShopLanguage, METH_VARARGS, "获取SHOPLAN"},
	{"GetChannel", GetChannel, METH_VARARGS, "获取频道号"},
	//////////////////////////////////////////////////////////////////////////
	// 设置;
	{"SetProjectId", SetProjectId, METH_VARARGS, "设置pid"},
	{"SetDeviceId", SetDeviceId, METH_VARARGS, "设置DeviceId"},
	{"SetDeviceIdBF", SetDeviceIdBF, METH_VARARGS, "设置DeviceId"},
	{"SetMAC", SetMAC, METH_VARARGS, "设置MAC"},
	{"SetMACBF", SetMACBF, METH_VARARGS, "设置MAC"},
	{"SetHDCPKey", SetHDCPKey, METH_VARARGS, "设置HDCP"},
	{"SetHDCPKeyBF", SetHDCPKeyBF, METH_VARARGS, "设置HDCP"},
	{"SetHDCPKey22", SetHDCPKey22, METH_VARARGS, "设置HDCP22"},
	{"SetHDCPKey22BF", SetHDCPKey22BF, METH_VARARGS, "设置HDCP22"},
	{"SetWidi", SetWidi, METH_VARARGS, "设置WIDI"},
	{"SetWidiBF", SetWidiBF, METH_VARARGS, "设置WIDI"},
	{"SetNetflixESN", SetNetflixESN, METH_VARARGS, "设置ESN"},
	{"SetNetflixESNBF", SetNetflixESNBF, METH_VARARGS, "设置ESN"},
	{"SetWidevine", SetWidevine, METH_VARARGS, "设置WIDEVINE"},
	{"SetWidevineBF", SetWidevineBF, METH_VARARGS, "设置WIDEVINE"},
	{"SetCiKey", SetCiKey, METH_VARARGS, "设置CIKEY"},
	{"SetCiKeyBF", SetCiKeyBF, METH_VARARGS, "设置CIKEY"},
	{"SetOSDLanguage", SetOSDLanguage, METH_VARARGS, "设置OSDLAN"},
	{"SetShopLanguage", SetShopLanguage, METH_VARARGS, "设置SHOPLAN"},
	{"SetChannel", SetChannel, METH_VARARGS, "设置频道号"},

	{"SetWBNormal", SetWBNormal, METH_VARARGS, "设置OSDLAN"},
	{"SetWBCool", SetWBCool, METH_VARARGS, "设置SHOPLAN"},
	{"SetWBWarm", SetWBWarm, METH_VARARGS, "设置频道号"},
	//////////////////////////////////////////////////////////////////////////
	// 校验;
	{"CheckDeviceId", CheckDeviceId, METH_VARARGS, "检验pid"},
	{"CheckMAC", CheckMAC, METH_VARARGS, "检验MAC"},
	{"CheckHDCP", CheckHDCP, METH_VARARGS, "检验HDCP"},
	{"CheckHDCP22", CheckHDCP22, METH_VARARGS, "检验HDCP22"},
	{"CheckWidi", CheckWidi, METH_VARARGS, "检验WIDI"},
	{"CheckNetflixESN", CheckNetflixESN, METH_VARARGS, "检验ESN"},
	{"CheckWidevine", CheckWidevine, METH_VARARGS, "检验WIDEVINE"},
	{"CheckCikey", CheckCikey, METH_VARARGS, "检验CIKEY"},
	{"StarWarmUpMode", StarWarmUpMode, METH_VARARGS, "检验OSDLAN"},
	{"StopWarmUpMode", StopWarmUpMode, METH_VARARGS, "检验SHOPLAN"},
	// others;
	{"ShowFactoryMenu", ShowFactoryMenu, METH_VARARGS, "显示工厂菜单"},
	{"HideFactoryMenu", HideFactoryMenu, METH_VARARGS, "隐藏工厂菜单"},
	{"ShowFactoryInformation", ShowFactoryInformation, METH_VARARGS, "工厂信息内容显示"},
	{"HideFactoryInformation", HideFactoryInformation, METH_VARARGS, "工厂信息内容隐藏"},
	{"EnterAgingModel", EnterAgingModel, METH_VARARGS, "老化模式开"},
	{"LeaveAgingModel", LeaveAgingModel, METH_VARARGS, "老化模式关"},
	{"ReadAgingTime", ReadAgingTime, METH_VARARGS, "老化时间读取"},
	{"SetRedGainRegister", SetRedGainRegister, METH_VARARGS, "红增益"},
	{"SetGreenGainRegister", SetGreenGainRegister, METH_VARARGS, "绿增益"},
	{"SetBlueGainRegister", SetBlueGainRegister, METH_VARARGS, "蓝增益"},
	{"SetRedOffsetRegister", SetRedOffsetRegister, METH_VARARGS, "红偏移"},
	{"SetGreenOffsetRegister", SetGreenOffsetRegister, METH_VARARGS, "绿偏移"},
	{"SetBlueOffsetRegister", SetBlueOffsetRegister, METH_VARARGS, "蓝偏移"},
	// HTTP
	{"HTTPLogin", HTTPLogin, METH_VARARGS, "登录服务器"},
	{"HTTPGetUserId", HTTPGetUserId, METH_VARARGS, "获取UserId"},
	{NULL,NULL}
};

// 初始模块;//格式：init<模块名称>
PyMODINIT_FUNC initScbcCopyKey()
{
	// 初始化pyd函数列表;
	PyObject* m, * d;
	m = Py_InitModule("ScbcCopyKey", ScbcCopyKey_Methods);
	d = PyModule_GetDict(m);
}

#endif

SCBCCOPYKEY_API LPCTSTR HTTP_Login(LPCTSTR lpHost, LPCTSTR lpUserName, LPCTSTR lpPassword, BOOL bRemember)
{
	if ( !lpHost || !lpUserName || !lpPassword )
		return _T("");

	CCurlClient curl;
	if ( curl.Initialize() ==  CURLE_OK)
	{
		curl.SetHeaders("Content-Type: application/json;charset=utf-8");
		// Json数据;
		cJSON *pJson = cJSON_CreateObject();
		cJSON_AddStringToObject(pJson, "username", lpUserName);
		cJSON_AddStringToObject(pJson, "password", lpPassword);
		cJSON_AddStringToObject(pJson, "rememberMe", bRemember ? "true" : "false");
		char *pJsonText = cJSON_Print(pJson);

		std::string result;
		CURLcode curlCode = CURLE_OK;
		if (_tcsstr(lpHost, "https://") || _tcsstr(lpHost, "HTTPS://") )
			curlCode = curl.Posts(lpHost, pJsonText, result);
		else
			curlCode = curl.Posts(lpHost, pJsonText, result);

		// 释放堆内存;
		if (pJsonText)
			delete pJsonText;
		pJsonText = NULL;

		if ( pJson )
			cJSON_Delete(pJson);
		pJson = NULL;		

		if ( curlCode == CURLE_OK )
		{
			result = CharEncoding::DeCode_URLUTF8(result.c_str());

			// 解析返回值;
			pJson = cJSON_Parse(result.c_str());
			if (pJson != NULL)
			{
				int code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valueint : -1;
				std::string msg = cJSON_GetObjectItem(pJson, "msg") ? cJSON_GetObjectItem(pJson, "msg")->valuestring : "";
				if ( code == 0 )
				{				
					cJSON *pObj = cJSON_GetObjectItem(pJson, "data");
					if (pObj != NULL)
					{
						g_httpData = cJSON_GetObjectItem(pObj, "token") ? cJSON_GetObjectItem(pObj, "token")->valuestring : "";
						// 释放内存;
						cJSON_Delete(pJson);

						// 返回结果;
						return g_httpData.c_str();
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pJson);	
			}
		}
	}

	return _T("");
}

SCBCCOPYKEY_API int HTTP_GetUserId(LPCTSTR lpHost, LPCTSTR lpLoginToken, LPCTSTR lpClientType, LPCTSTR lpDeviceId, LPCTSTR lpMAC)
{
	if ( !lpHost || !lpLoginToken || !lpClientType || !lpDeviceId || !lpMAC)
		return -1;

	CCurlClient curl;
	if ( curl.Initialize() ==  CURLE_OK)
	{
		std::string header = "Authorization:";
		header.append(lpLoginToken);
		curl.SetHeaders(header);
		curl.SetHeaders("Content-Type: application/json;charset=utf-8");
		// Json数据;
		cJSON *pJson = cJSON_CreateObject();
		cJSON_AddStringToObject(pJson, "clientType", lpClientType);
		cJSON_AddStringToObject(pJson, "deviceId", lpDeviceId);
		cJSON_AddStringToObject(pJson, "mac", lpMAC);
		char *pJsonText = cJSON_Print(pJson);

		std::string result;
		CURLcode curlCode = CURLE_OK;
		if (_tcsstr(lpHost, "https://") || _tcsstr(lpHost, "HTTPS://") )
			curlCode = curl.Posts(lpHost, pJsonText, result);
		else
			curlCode = curl.Posts(lpHost, pJsonText, result);

		// 释放堆内存;
		if (pJsonText)
			delete pJsonText;
		pJsonText = NULL;

		if ( pJson )
			cJSON_Delete(pJson);
		pJson = NULL;		

		if ( curlCode == CURLE_OK )
		{
			result = CharEncoding::DeCode_URLUTF8(result.c_str());

			// 解析返回值;
			pJson = cJSON_Parse(result.c_str());
			if (pJson != NULL)
			{
				int code = cJSON_GetObjectItem(pJson, "code") ? cJSON_GetObjectItem(pJson, "code")->valueint : -1;
				std::string msg = cJSON_GetObjectItem(pJson, "msg") ? cJSON_GetObjectItem(pJson, "msg")->valuestring : "";
				if ( code == 0 )
				{				
					cJSON *pObj = cJSON_GetObjectItem(pJson, "data");
					if (pObj != NULL)
					{
						int uiserId = cJSON_GetObjectItem(pObj, "userId") ? cJSON_GetObjectItem(pObj, "userId")->valueint : -1;
						// 释放内存;
						cJSON_Delete(pJson);

						// 返回结果;
						return uiserId;
					}
				}

				// 错误产生,可输出msg方便查询;
				cJSON_Delete(pJson);	
			}
		}
	}

	return -1;
}