#include "bt_mp_dll_build.h"
#include "bt_mp_calibration_base.h"
#include "RtlBluetoothMP_scriptExec_base.h"

static pfBTMPAPI_BuildBluetoothModule hfBTMPAPI_BuildBluetoothModule=NULL;
static pfBTMPAPI_BuildInterfaceVendor hfBTMPAPI_BuildInterfaceVendor=NULL;
static pfBTMPAPI_BuildInterfaceRTK hfBTMPAPI_BuildInterfaceRTK=NULL;
static pfBTMPAPI_BluetoothVerify hfBTMPAPI_BluetoothVerify= NULL;
static pfBTMPAPI_ExternFunctionRTK hfBTMPAPI_ExternFunctionRTK=NULL;
//----------------------------- rd mode begin-----------------------------------
static BASE_FP_SCRIPT_EXE_FILE   hfBASE_FP_SCRIPT_EXE_FILE = NULL;
static BASE_FP_CALLBACK_SHOW     hfBASE_FP_CALLBACK_SHOW= NULL;
//----------------------------- rd mode end-----------------------------------
static pfApi_calculate_XtalBoundary hfApi_calculate_XtalBoundary = NULL;
static pfApi_calculate_XtalNormal hfApi_calculate_XtalNormal = NULL;

#ifndef BCB_MODE
static HMODULE hRTKBTMPDLL_Object=NULL;
#else
#include <vcl.h>
#pragma hdrstop
static HINSTANCE hRTKBTMPDLL_Object=NULL;
#endif




static int LoadRTKBTMPDLLObject(char *DllModulePathBuffer);
static int LoadRTKBTMPDLLObjectForActionsBumble(char *DllModulePathBuffer);
static void UnLoadLoadRTKBTMPDLLObject();

static int LoadRTKBTMPDLLObject(char *DllModulePathBuffer)
{
	char DLLPathBuffer[700];
	//int szLen=0;
	memset(DLLPathBuffer,0,700);

//endif ACTSBluetoothRF

        #ifdef _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
	if (strlen(DllModulePathBuffer)== 0)
		sprintf(DLLPathBuffer,_T"RTLBlueToothMP.dll");
	else
		sprintf(DLLPathBuffer,_T"%s\\RTLBlueToothMP.dll",DllModulePathBuffer);
        #else

	if (strlen(DllModulePathBuffer)== 0)
		sprintf(DLLPathBuffer,"RTLBlueToothMP.dll");
	else
		sprintf(DLLPathBuffer,"%s\\RTLBlueToothMP.dll",DllModulePathBuffer);
        #endif

	hRTKBTMPDLL_Object = LoadLibrary(DLLPathBuffer);
	if (hRTKBTMPDLL_Object == NULL)
		return -1;

	hfBTMPAPI_BuildBluetoothModule	=(pfBTMPAPI_BuildBluetoothModule)	GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BuildBluetoothModule");
	hfBTMPAPI_BuildInterfaceVendor	=(pfBTMPAPI_BuildInterfaceVendor)	        GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BuildInterfaceVendor");
	hfBTMPAPI_BuildInterfaceRTK	=(pfBTMPAPI_BuildInterfaceRTK)	        GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BuildInterfaceRTK");
        hfBTMPAPI_BluetoothVerify=      (pfBTMPAPI_BluetoothVerify) GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BluetoothVerify");
        hfBTMPAPI_ExternFunctionRTK=      (pfBTMPAPI_ExternFunctionRTK) GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_ExternFunctionRTK");

        //----------------------------- rd mode begin-----------------------------------
        hfBASE_FP_SCRIPT_EXE_FILE = (BASE_FP_SCRIPT_EXE_FILE) GetProcAddress(hRTKBTMPDLL_Object,"Api_Script_Excute_File");
        hfBASE_FP_CALLBACK_SHOW  = (BASE_FP_CALLBACK_SHOW) GetProcAddress(hRTKBTMPDLL_Object,"Api_Script_Excute_CallBackShow");
        //----------------------------- rd mode end-----------------------------------
        hfApi_calculate_XtalBoundary = (pfApi_calculate_XtalBoundary) GetProcAddress(hRTKBTMPDLL_Object,"Api_calculate_XtalBoundary");
        hfApi_calculate_XtalNormal = (pfApi_calculate_XtalNormal) GetProcAddress(hRTKBTMPDLL_Object,"Api_calculate_XtalNormal");
#ifndef _IsVCType_
	atexit(UnLoadLoadRTKBTMPDLLObject);
#endif
	if ((hfBTMPAPI_BuildBluetoothModule== NULL) ||
		(hfBTMPAPI_BuildInterfaceVendor== NULL)||
		(hfBTMPAPI_BuildInterfaceRTK== NULL) ||
                (hfBTMPAPI_BluetoothVerify == NULL) ||
                (hfBTMPAPI_ExternFunctionRTK == NULL) 
		)
	{
		return -1;
	}
	return 0;
}
static int LoadRTKBTMPDLLObjectForActionsBumble(char *DllModulePathBuffer)
{
	char DLLPathBuffer[700];
	//int szLen=0;
	memset(DLLPathBuffer,0,700);

        #ifdef _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
	if (strlen(DllModulePathBuffer)== 0)
		sprintf(DLLPathBuffer,_T"ACTSBluetoothRF.dll");
	else
		sprintf(DLLPathBuffer,_T"%s\\ACTSBluetoothRF.dll",DllModulePathBuffer);
        #else

	if (strlen(DllModulePathBuffer)== 0)
		sprintf(DLLPathBuffer,"ACTSBluetoothRF.dll");
	else
		sprintf(DLLPathBuffer,"%s\\ACTSBluetoothRF.dll",DllModulePathBuffer);
        #endif


	hRTKBTMPDLL_Object = LoadLibrary(DLLPathBuffer);
	if (hRTKBTMPDLL_Object == NULL)
		return -1;

	hfBTMPAPI_BuildBluetoothModule	=(pfBTMPAPI_BuildBluetoothModule)	GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BuildBluetoothModule");
	hfBTMPAPI_BuildInterfaceVendor	=(pfBTMPAPI_BuildInterfaceVendor)	        GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BuildInterfaceVendor");
	hfBTMPAPI_BuildInterfaceRTK	=(pfBTMPAPI_BuildInterfaceRTK)	        GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BuildInterfaceRTK");
        hfBTMPAPI_BluetoothVerify=      (pfBTMPAPI_BluetoothVerify) GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_BluetoothVerify");
        hfBTMPAPI_ExternFunctionRTK=      (pfBTMPAPI_ExternFunctionRTK) GetProcAddress(hRTKBTMPDLL_Object,"BTMPAPI_ExternFunctionRTK");
        //----------------------------- rd mode begin-----------------------------------
        hfBASE_FP_SCRIPT_EXE_FILE = (BASE_FP_SCRIPT_EXE_FILE) GetProcAddress(hRTKBTMPDLL_Object,"Api_Script_Excute_File");
        hfBASE_FP_CALLBACK_SHOW  = (BASE_FP_CALLBACK_SHOW) GetProcAddress(hRTKBTMPDLL_Object,"Api_Script_Excute_CallBackShow");
        //----------------------------- rd mode end-----------------------------------
        hfApi_calculate_XtalBoundary = (pfApi_calculate_XtalBoundary) GetProcAddress(hRTKBTMPDLL_Object,"Api_calculate_XtalBoundary");
        hfApi_calculate_XtalNormal = (pfApi_calculate_XtalNormal) GetProcAddress(hRTKBTMPDLL_Object,"Api_calculate_XtalNormal");
#ifndef _IsVCType_
	atexit(UnLoadLoadRTKBTMPDLLObject);
#endif
	if ((hfBTMPAPI_BuildBluetoothModule== NULL) ||
		(hfBTMPAPI_BuildInterfaceVendor== NULL)||
		(hfBTMPAPI_BuildInterfaceRTK== NULL) ||
                (hfBTMPAPI_BluetoothVerify == NULL) ||
                (hfBTMPAPI_ExternFunctionRTK == NULL) 
		)
	{
		return -1;
	}
	return 0;
}



static void UnLoadLoadRTKBTMPDLLObject(void)
{
	if (hRTKBTMPDLL_Object != NULL)
	FreeLibrary(hRTKBTMPDLL_Object);
	return;
}



void ReleaseBTPMPDLLObject(BASE_BTMPDLLObject *Modlue)
{
      	if ((NULL == hRTKBTMPDLL_Object ) || (NULL == Modlue ))
	{
                         return ;
	}

	else
	{
		FreeLibrary(hRTKBTMPDLL_Object);
		hRTKBTMPDLL_Object=NULL;
                Modlue = NULL;
	}

	return;

}





int BuildBTMPDLLObject(BASE_BTMPDLLObject *Modlue,char *SettingPath,int VendorVersion)
{
	char ModulePathBuffer[256];
	int SettingPathLen=0;
	int n=0;
        int rtn=0;
	memset(ModulePathBuffer,0,256);
	if (Modlue == NULL)
		return -1;

	if (SettingPath != NULL)
		SettingPathLen=strlen(SettingPath);

	if (SettingPathLen>0)
	{
		//memcpy(ModulePathBuffer,SettingPath,SettingPathLen);
		for (n=0;n<SettingPathLen;n++)
		{
			ModulePathBuffer[n]= SettingPath[n];
		}
	}
	if (hRTKBTMPDLL_Object == NULL){
                if (VendorVersion == 1)  //Actions
                {

                       rtn= LoadRTKBTMPDLLObjectForActionsBumble(ModulePathBuffer);
                       if (rtn !=0)
                       {
                          rtn= LoadRTKBTMPDLLObject(ModulePathBuffer);
                       }
                }
                else
                        rtn= LoadRTKBTMPDLLObject(ModulePathBuffer);

		if (rtn !=0)
		{
			ReleaseBTPMPDLLObject(Modlue);
			return -1;
		}
	}

	Modlue->BTMPAPI_BuildBluetoothModule= hfBTMPAPI_BuildBluetoothModule;
	Modlue->BTMPAPI_BuildInterfaceRTK=  hfBTMPAPI_BuildInterfaceRTK;
	Modlue->BTMPAPI_BuildInterfaceVendor= hfBTMPAPI_BuildInterfaceVendor;
        Modlue->BTMPAPI_BluetoothVerify =  hfBTMPAPI_BluetoothVerify;
        Modlue->ExternFunctionRTK =  hfBTMPAPI_ExternFunctionRTK;
	return 0;
}
//----------------------------- rd mode begin-----------------------------------
int rd_mode_scriptfile_Excute(BASE_BTMPDLL_MODULE *pDLLBtBaseModule,char *pFileName,char *saveFileName)
{
    int rtn=0;
    if (hfBASE_FP_SCRIPT_EXE_FILE == NULL)
       return -1;

    rtn =  hfBASE_FP_SCRIPT_EXE_FILE(pDLLBtBaseModule,pFileName,saveFileName);
    return rtn;

}

void rd_mode_callbackshow(void *fp)
{
    if (hfBASE_FP_CALLBACK_SHOW != NULL)
    {
         // hfBASE_FP_CALLBACK_SHOW(
         hfBASE_FP_CALLBACK_SHOW((BASE_FP_CALLBACK_SHOW)fp);
    }
    return ;
}
//----------------------------- rd mode end-----------------------------------

int calibration_Excute_XtalBoundary(
        BASE_BTMPDLL_MODULE *pDLLBtBaseModule,
        CAL_PARAMETER *pParameter,
	int cal_freq_MHz,
	double MeasureFreq_MHz,
	unsigned int *pData)
{
    int rtn=0;
    if (hfApi_calculate_XtalBoundary == NULL)
        return -1;

    rtn = hfApi_calculate_XtalBoundary(pDLLBtBaseModule,pParameter,cal_freq_MHz,MeasureFreq_MHz,pData);
    return rtn;

}

int calibration_Excute_XtalNormal(
        BASE_BTMPDLL_MODULE *pDLLBtBaseModule,
        CAL_PARAMETER *pParameter,
	double MeasureFreqOffset_KHz,
        unsigned int InitCFCapValue,
        unsigned int *pNewCFCapValue)
{
    int rtn=0;
    if (hfApi_calculate_XtalNormal == NULL)
        return -1;

    rtn = hfApi_calculate_XtalNormal(pDLLBtBaseModule,pParameter,MeasureFreqOffset_KHz,InitCFCapValue,pNewCFCapValue);
    return rtn;

}

