T_TXPCL_common.cpp
上传用户:gelin96
上传日期:2017-01-08
资源大小:20993k
文件大小:268k
源码类别:

MTK

开发平台:

C++ Builder

  1. /*****************************************************************************
  2. *  Copyright Statement:
  3. *  --------------------
  4. *  This software is protected by Copyright and the information contained
  5. *  herein is confidential. The software may not be copied and the information
  6. *  contained herein may not be used or disclosed except with the written
  7. *  permission of MediaTek Inc. (C) 2005
  8. *
  9. *  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  10. *  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  11. *  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
  12. *  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  13. *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  14. *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  15. *  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  16. *  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  17. *  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
  18. *  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
  19. *  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
  20. *  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
  21. *
  22. *  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
  23. *  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  24. *  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  25. *  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
  26. *  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  27. *
  28. *  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
  29. *  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
  30. *  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
  31. *  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
  32. *  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
  33. *
  34. *****************************************************************************/
  35. /*****************************************************************************
  36.  *
  37.  * Filename:
  38.  * ---------
  39.  *   T_TXPCL_common.cpp
  40.  *
  41.  * Project:
  42.  * --------
  43.  *   Maui META APP
  44.  *
  45.  * Description:
  46.  * ------------
  47.  *  TX PCL calibration common source
  48.  *
  49.  * Author:
  50.  * -------
  51.  *  Andy Ueng (mtk00490)
  52.  *
  53.  *============================================================================
  54.  *             HISTORY
  55.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  56.  *------------------------------------------------------------------------------
  57.  * $Revision$
  58.  * $Modtime$
  59.  * $Log$
  60.  *
  61.  *------------------------------------------------------------------------------
  62.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  63.  *============================================================================
  64. ****************************************************************************/
  65. #include <assert.h>
  66. #include <math.h>
  67. #pragma hdrstop
  68. #ifndef _T_META_FACTORY_CALIBRATION_H_
  69. #include "T_META_factory_calibration.H"
  70. #endif
  71. #ifndef _APCCAL_COMMON_H_
  72. #include "apccal_common.h"
  73. #endif
  74. #ifndef  _RF_APC_H_
  75. #include "rf_apc.h"
  76. #endif
  77. // callback
  78. #ifndef _META_FACTORY_RF_CB_H_
  79. #include "meta_factory_rf_cb.h"
  80. #endif
  81. #ifndef _META_FACTORY_NVRAM_CB_H_
  82. #include "meta_factory_nvram_cb.h"
  83. #endif
  84. // form
  85. #ifndef _META_FACTORY_H_
  86. #include "META_Factory.h"
  87. #endif
  88. // equipment
  89. #ifndef _AGECOMMON_H_
  90. #include "agecommon.h"
  91. #endif
  92. #ifndef _AGE1968A_H_
  93. #include "age1968a.h"
  94. #endif
  95. // misc
  96. #ifndef  _FT_UTILS_H_
  97. #include "ft_utils.h"
  98. #endif
  99. #ifndef  _BAND_UTILS_H_
  100. #include "band_utils.h"
  101. #endif
  102. #ifndef  _GSM_UTILS_H_
  103. #include "gsm_utils.h"
  104. #endif
  105. #ifndef  _TIME_UTILS_H_
  106. #include "time_utils.h"
  107. #endif
  108. //----------------------------------------------------------------------------
  109. // equipment
  110. extern CMU_VAR cmu;
  111. extern CRFAPC*  MF_rf_apc_ptr;
  112. extern RfNbtx_Req *MF_rf_tx_level_req;
  113. extern bool MF_rf_tx_level_cnf;
  114. extern bool  is_suspend_cal; // in T_META_factory_calibration.cpp
  115. //============================================================================
  116. bool __fastcall T_META_factory_calibration::TXPCLCal_2cal(void)
  117. {
  118.     int FIRST_PCL=0;
  119.         AnsiString  as_tx_pcl;
  120.         AnsiString  as_tx_2cal_pcl;
  121.         AnsiString  as_sub_tx_2cal_pcl;
  122.         AnsiString  as_max_p;
  123.         AnsiString  as_wanted_p;
  124.         AnsiString  as_min_p;
  125.         AnsiString  as_c;
  126.         AnsiString  as_sub_tx_pcl;
  127.         AnsiString  as_sub_max_p;
  128.         AnsiString  as_sub_wanted_p;
  129.         AnsiString  as_sub_min_p;
  130.         AnsiString  as_sub_c;
  131.         log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  132.              " ============ GMSK TX power control level calibration begin ============ n "
  133.            );
  134.         if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  135.         {
  136.             if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GSM_BCH_TCH ) )
  137.             {
  138.                 CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  139.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  140.                               " FAIL: "+
  141.                               m_pRct->as_RCT + " < Operation mode = GSM BCH+TCH"
  142.                             );
  143.                 return false;
  144.             }
  145.         }
  146.         else
  147.         {
  148.             if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GPRS_BCH_PDTCH) )
  149.             {
  150.                 CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  151.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  152.                               " FAIL: "+
  153.                               m_pRct->as_RCT + " < Operation mode = GPRS BCH+PDTCH"
  154.                             );
  155.                 return false;
  156.             }
  157.         }
  158.         short s_ARFCN;
  159.         FrequencyBand eFreqBand = FrequencyBand850;
  160.         i_N_TX = 2000; // TX frames
  161.         //---------------------------------------------------------------------------
  162.         while (eFreqBand < FrequencyBandCount)
  163.         {
  164.             if ( this->Terminated )
  165.             {
  166.                 this->OnTerminate = neByUser;
  167.                 return false;
  168.             }
  169.             switch (eFreqBand)
  170.             {
  171.                 case FrequencyBand850:
  172.                 {
  173.                     if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  174.                     {
  175.                         eFreqBand++;
  176.                         continue;
  177.                     }
  178.                     FIRST_PCL = GSM850_FIRST_PCL;
  179.                     s_ARFCN = m_sARFCN_C0_GSM850;
  180.                     i_TX_level_count = RF_TX_LEVEL_2_CAL_COUNT;
  181.                     as_tx_2cal_pcl = m_as_gsm850_tx_2cal_pcl;
  182.                     as_tx_pcl = m_as_gsm850_tx_pcl;
  183.                     as_max_p = m_as_gsm850_max_p;
  184.                     as_wanted_p = m_as_gsm850_wanted_p;
  185.                     as_min_p = m_as_gsm850_min_p;
  186.                     as_c = m_as_gsm850_c;
  187.                 }
  188.                 break;
  189.                 case FrequencyBand900:
  190.                 {
  191.                     if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  192.                     {
  193.                         eFreqBand++;
  194.                         continue;
  195.                     }
  196.                     FIRST_PCL = GSM900_FIRST_PCL;
  197.                     s_ARFCN = m_sARFCN_C0_GSM;
  198.                     i_TX_level_count = RF_TX_LEVEL_2_CAL_COUNT;
  199.                     as_tx_2cal_pcl = m_as_gsm900_tx_2cal_pcl;
  200.                     as_tx_pcl = m_as_gsm900_tx_pcl;
  201.                     as_max_p = m_as_gsm900_max_p;
  202.                     as_wanted_p = m_as_gsm900_wanted_p;
  203.                     as_min_p = m_as_gsm900_min_p;
  204.                     as_c = m_as_gsm900_c;
  205.                 }
  206.                 break;
  207.                 case FrequencyBand1800:
  208.                 {
  209.                     if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  210.                     {
  211.                         eFreqBand++;
  212.                         continue;
  213.                     }
  214.                     FIRST_PCL = DCS1800_FIRST_PCL;
  215.                     s_ARFCN = m_sARFCN_C0_DCS;
  216.                     i_TX_level_count = RF_TX_LEVEL_2_CAL_COUNT;
  217.                     as_tx_2cal_pcl = m_as_dcs1800_tx_2cal_pcl;
  218.                     as_tx_pcl = m_as_dcs1800_tx_pcl;
  219.                     as_max_p = m_as_dcs1800_max_p;
  220.                     as_wanted_p = m_as_dcs1800_wanted_p;
  221.                     as_min_p = m_as_dcs1800_min_p;
  222.                     as_c = m_as_dcs1800_c;
  223.                 }
  224.                 break;
  225.                 case FrequencyBand1900:
  226.                 {
  227.                     if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  228.                     {
  229.                         eFreqBand++;
  230.                         continue;
  231.                     }
  232.                     FIRST_PCL = PCS1900_FIRST_PCL;
  233.                     s_ARFCN = m_sARFCN_C0_PCS;
  234.              i_TX_level_count = RF_TX_LEVEL_2_CAL_COUNT;
  235.                     as_tx_2cal_pcl = m_as_pcs1900_tx_2cal_pcl;
  236.                     as_tx_pcl = m_as_pcs1900_tx_pcl;
  237.                     as_max_p = m_as_pcs1900_max_p;
  238.                     as_wanted_p = m_as_pcs1900_wanted_p;
  239.                     as_min_p = m_as_pcs1900_min_p;
  240.                     as_c = m_as_pcs1900_c;
  241.                 }
  242.                 break;
  243.                 default:
  244.                 {
  245.                     assert(false);
  246.                 }
  247.                 break;
  248.             } // switch
  249.             E_BANDSEL band_index = FreqBand_To_BandIdx(eFreqBand);
  250.             int vi_Band = FreqBand_To_AgeBand(eFreqBand);
  251.             
  252.             if (this->Terminated)
  253.             {
  254.                 this->OnTerminate = neByUser;
  255.                 return false;
  256.             }
  257.             log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  258.                          " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  259.                        );
  260.             if (!MF_rf_apc_ptr->ApcSectionExist(m_pCal->as_IniFile.c_str(), band_index))
  261.             {
  262.                 CalErrorHandler(WM_MF_RF_TX_LEVEL_INI_FAIL);
  263.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  264.                               " FAIL :"+
  265.                               " APC section is not in initial file: " + FreqBand_To_Str(eFreqBand)
  266.                             );
  267.                 return false;
  268.             }
  269.             if (IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  270.             {
  271.                 if(eFreqBand == FrequencyBand1900)
  272.                 {
  273.                     if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 1, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  274.                     {
  275.                         CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  276.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  277.                                   " FAIL: target < set Band1900 flag = 1"
  278.                                 );
  279.                         return false;
  280.                     }
  281.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  282.                               " target < set Band1900 flag = 1"
  283.                             );
  284.                 }
  285.                 else
  286.                 {
  287.                     if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 0, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  288.                     {
  289.                         CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  290.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  291.                                   " FAIL: target < set Band1900 flag = 0"
  292.                                 );
  293.                         return false;
  294.                     }
  295.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  296.                               " target < set Band1900 flag = 0"
  297.                             );
  298.                 }
  299.                 is_suspend_cal = true;
  300.                 this->Suspend();
  301.                 is_suspend_cal = false;
  302.             }
  303.             if( ! m_rct_ctrl.RCT_cellBand( m_pRct, vi_Band ) )
  304.             {
  305.                 CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  306.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  307.                               " FAIL: "+
  308.                               m_pRct->as_RCT + " < Band = " + ViBand_To_Str( vi_Band )
  309.                             );
  310.                 return false;
  311.             }
  312.             Sleep( 100 );  // wait Agilent 8960 cell band setup ready!
  313.                    // if Agilent 8960 not read, target will get error power!
  314.             if ( this->Terminated )
  315.             {
  316.                 this->OnTerminate = neByUser;
  317.                 return false;
  318.             }
  319.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  320.                           m_pRct->as_RCT + " < Band = " + ViBand_To_Str( vi_Band )
  321.                         );
  322.             short bch_arfcn = ::Get_SeperateChannel( vi_Band, s_ARFCN );
  323.             if( ! m_rct_ctrl.RCT_BCHARFCN( m_pRct, bch_arfcn) )
  324.             {
  325.                 CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  326.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  327.                               " FAIL: "+
  328.                               m_pRct->as_RCT + " < BCH ARFCN = " + IntToStr( bch_arfcn )
  329.                         );
  330.                 return false;
  331.             }
  332.             if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  333.             {
  334.                 if( ! m_rct_ctrl.RCT_TCHARFCN( m_pRct, s_ARFCN ) )
  335.                 {
  336.                     CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  337.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  338.                                   " FAIL: "+
  339.                                   m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr( s_ARFCN )
  340.                                 );
  341.                     return false;
  342.                 }
  343.             }
  344.             else
  345.             {
  346.                 if( ! m_rct_ctrl.RCT_PDTCHARFCN( m_pRct, s_ARFCN ) )
  347.                 {
  348.                     CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  349.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  350.                                   " FAIL: "+
  351.                                   m_pRct->as_RCT + " < PDTCH ARFCN = " + IntToStr( s_ARFCN )
  352.                                 );
  353.                     return false;
  354.                 }
  355.             }
  356.             if ( this->Terminated )
  357.             {
  358.                 this->OnTerminate = neByUser;
  359.                 return false;
  360.             }
  361.             if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  362.             {
  363.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  364.                           m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr( s_ARFCN )
  365.                         );
  366.             }
  367.             else
  368.             {
  369.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  370.                           m_pRct->as_RCT + " < PDTCH ARFCN = " + IntToStr( s_ARFCN )
  371.                         );
  372.             }
  373.             // ----------------------- set Agilent 8960 < TSC -------------------------
  374.             if( ! m_rct_ctrl.RCT_ConfigTSC( m_pRct, m_cTSC ) )
  375.             {
  376.                 CalErrorHandler( WM_MF_AGE8960_SET_TSC_FAIL );
  377.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  378.                               "FAIL: " + m_pRct->as_RCT + " < set TSC fail."
  379.                             );
  380.                 return false;
  381.             }
  382.             if ( this->Terminated )
  383.             {
  384.                 this->OnTerminate = neByUser;
  385.                 return false;
  386.             }
  387.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  388.                           m_pRct->as_RCT + " < TSC = " + IntToStr( m_cTSC)
  389.                         );
  390.           //  log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  391.           //                " TimerCal->Enabled 8= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  392.           //              );
  393.             RestartTimerCal( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  394.             if (!m_rct_ctrl.RCT_Config_ExpectedTxPower(m_pRct, true, 0))
  395.             {
  396.                 RestartTimerCal(WM_MF_AGE8960_CONFIG_TX_POWER_FAIL);
  397.                 log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  398.                                   " FAIL : " + m_pRct->as_RCT + " < config expected TX power to auto fail"
  399.                                 );
  400.                 return false;
  401.             }
  402.             if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  403.             {
  404.                 if( ! m_rct_ctrl.RCT_confTXPower( m_pRct ) )
  405.                 {
  406.                     CalErrorHandler( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  407.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  408.                                   " FAIL: " + m_pRct->as_RCT + " < config TX power ."
  409.                                 );
  410.                     return false;
  411.                 }
  412.             }
  413.             else
  414.             {
  415.                 if( ! m_rct_ctrl.RCT_confMultiSlotTxPower( m_pRct, AGE1960_TX_POWER_MEASURE_COUNT, MOD_GMSK ) )
  416.                 {
  417.                     CalErrorHandler( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  418.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  419.                                   " FAIL: " + m_pRct->as_RCT + " < config TX power 1."
  420.                                 );
  421.                     return false;
  422.                 }
  423.             }
  424.             if( ! m_rct_ctrl.RCT_TXPowerContOff( m_pRct ) )
  425.             {
  426.                 CalErrorHandler( WM_MF_AGE8960_SET_TX_POWER_CONT_OFF_FAIL );
  427.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  428.                               " FAIL: " + m_pRct->as_RCT + " < set TX power continuous OFF "
  429.                             );
  430.                 return false;
  431.             }
  432.             if ( this->Terminated )
  433.             {
  434.                 this->OnTerminate = neByUser;
  435.                 return false;
  436.             }
  437.             // calibration from high power to low power
  438.             // index from big to little.
  439.             // PCL string in config file from right to left.
  440.             int iRealRecursiveTimes = 0;
  441.             if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  442.             {
  443.                 for ( int N=0; N<i_TX_level_count; N++)  // DCS,PCS:16~1 or GSM:15~1
  444.                 {
  445.                     log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  446.                           " ------------------------------------------------------------------ "
  447.                         );
  448.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  449.                           " N = " + N
  450.                         );
  451.                     if (! getAnsiStrSubItem( as_tx_2cal_pcl, N+1, DEFAULT_SEP_CHAR, as_sub_tx_2cal_pcl) )
  452.                     {
  453.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  454.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  455.                               " FAIL: Read config file fail(sub_tx_cal_pcl)."
  456.                             );
  457.                         return false;
  458.                     }
  459.                     for( iRealRecursiveTimes=0; iRealRecursiveTimes<i_TXlevelRecusiveTimes;
  460.                          iRealRecursiveTimes++) // marked by Andy Ueng for no iteration test
  461.                     {
  462.                         log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  463.                               " RecursiveTimes = " + IntToStr(iRealRecursiveTimes)
  464.                             );
  465.                         if( 0 == iRealRecursiveTimes )
  466.                         {
  467.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  468.                              // " band_index = " + IntToStr(band_index) + "," +
  469.                              // " power_index = " + IntToStr(FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()) + "," +
  470.                               " MF_rf_apc_ptr->ApcProfile[" + IntToStr(band_index) + "].rampData.power[" +
  471.                                 IntToStr(FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()) + "] = " +
  472.                                // IntToStr(MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()])
  473.                                 IntToStr(MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() ) )
  474.                             );
  475.                             // not support GSM400
  476.                             // m_usPCLDacValue[band_index][N] = MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()];
  477.                             m_usPCLDacValue[band_index][N] = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() );
  478.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  479.                                 //  " band_index = " + IntToStr(band_index) + "," +
  480.                                 //  " N = " + IntToStr(N) + "," +
  481.                                   " m_usPCLDacValue[" +IntToStr(band_index) + "][" + IntToStr(N) + "] = " +
  482.                                     IntToStr(m_usPCLDacValue[band_index][N])
  483.                                 );
  484.                         }
  485.                         // index start from 1
  486.                         if (! getAnsiStrSubItem( as_tx_pcl, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_tx_pcl) )
  487.                         {
  488.                             CalErrorHandler( WM_MF_CFG_READ_FAIL );
  489.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  490.                                       " FAIL: Read config file fail(sub_tx_cal_pcl 2)."
  491.                                     );
  492.                             return false;
  493.                         }
  494.                         if (! getAnsiStrSubItem( as_max_p, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_max_p) )
  495.                         {
  496.                             CalErrorHandler( WM_MF_CFG_READ_FAIL );
  497.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  498.                               " FAIL: Read config file fail(sub_max_p)."
  499.                             );
  500.                         return false;
  501.                     }
  502.                     if (! getAnsiStrSubItem( as_min_p, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_min_p) )
  503.                     {
  504.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  505.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  506.                               " FAIL: Read config file fail(sub_min_p)."
  507.                             );
  508.                         return false;
  509.                     }
  510.                     if (! getAnsiStrSubItem( as_wanted_p, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_wanted_p) )
  511.                     {
  512.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  513.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  514.                               " FAIL: Read config file fail(sub_wanted_p)."
  515.                             );
  516.                         return false;
  517.                     }
  518.                     if (! getAnsiStrSubItem( as_c, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_c) )
  519.                     {
  520.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  521.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  522.                               " FAIL: Read config file fail(c)."
  523.                             );
  524.                         return false;
  525.                     }
  526.                     if ( this->Terminated )
  527.                     {
  528.                         this->OnTerminate = neByUser;
  529.                         return false;
  530.                     }
  531.                     if( ! m_rct_ctrl.RCT_MSTXLevel( m_pRct, as_sub_tx_pcl.ToInt()) )
  532.                     {
  533.                         CalErrorHandler( WM_MF_AGE8960_SET_MS_TX_LEVEL_FAIL );
  534.                         if ( frmFatcory->rbAgilent8960->Checked )
  535.                         {
  536.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  537.                                   " FAIL : Agilent 8960< MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  538.                                 );
  539.                         }
  540.                         else
  541.                         {
  542.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  543.                                   " FAIL : cmu200 > MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  544.                                 );
  545.                         }
  546.                         return false;
  547.                     }
  548.                     if ( this->Terminated )
  549.                     {
  550.                         this->OnTerminate = neByUser;
  551.                         return false;
  552.                     }
  553.                     if ( frmFatcory->rbAgilent8960->Checked )
  554.                     {
  555.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  556.                               " Agilent 8960< MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  557.                             );
  558.                     }
  559.                     else
  560.                     {
  561.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  562.                               " CMU200 < MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  563.                             );
  564.                     }
  565.                     RestartTimerCal( WM_MF_RF_STOP_FAIL );
  566.                     if (!MF_rf_stop.REQ_Start())
  567.                     {
  568.                         CalErrorHandler( WM_MF_RF_STOP_FAIL );
  569.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  570.                           " FAIL: Target < stop RF fail."
  571.                         );
  572.                         return false;
  573.                     }
  574.                     MF_rf_tx_level_req->arfcn = s_ARFCN;
  575.                     MF_rf_tx_level_req->bsic  =  m_cTSC;
  576.                     MF_rf_tx_level_req->power    = as_sub_tx_pcl.ToInt();
  577.                     MF_rf_tx_level_req->frames   = i_N_TX;
  578.                     MF_rf_tx_level_req->dacValue = m_sDefault_value;
  579.                     MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC; // NB burst
  580.                     if ( META_Rf_NB_TX_r( m_pCal->i_MainMETAHandle,
  581.                      MF_rf_tx_level_req,
  582.                      MF_rf_tx_level_cb,
  583.                      &MF_rf_tx_level_token,
  584.                                      NULL
  585.                                   ) != META_SUCCESS)
  586.                     {
  587.                         CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  588.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  589.                               " FAIL : Target< ARFCN = " + IntToStr( s_ARFCN ) +
  590.                               ", TSC = " + IntToStr( m_cTSC ) +
  591.                               ", power = " + as_sub_tx_pcl +
  592.                               ", frames = " + IntToStr(i_N_TX) +
  593.                               ", dac value = " + IntToStr( m_sDefault_value )
  594.                             );
  595.                         return false;
  596.                     }
  597.                     if ( this->Terminated )
  598.                     {
  599.                         this->OnTerminate = neByUser;
  600.                         return false;
  601.                     }
  602.                     #ifdef _MYDEBUG_
  603.                        TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  604.                     #endif
  605.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  606.                     #ifdef _MYDEBUG_
  607.                     ":" + AnsiString( ms ) +
  608.                     #endif
  609.                          " Target< TX: ARFCN = " + IntToStr( s_ARFCN ) +
  610.                          ", TSC = " + IntToStr( m_cTSC ) +
  611.                          ", power = " + as_sub_tx_pcl +
  612.                          ", frames = " + IntToStr(i_N_TX) +
  613.                          ", dac value = " + IntToStr( m_sDefault_value )
  614.                     );
  615.                     #ifdef _MYDEBUG_
  616.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  617.                     log->Add( " before suspend " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  618.                           ":" + AnsiString( ms )
  619.                         );
  620.                     #endif
  621.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  622.                           " TimerCal->Enabled 7= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  623.                         );
  624.                     RestartTimerCal( WM_MF_RF_TX_LEVEL_FAIL );
  625.                     is_suspend_cal = true;
  626.                     this->Suspend();
  627.                     is_suspend_cal = false;
  628.                     #ifdef _MYDEBUG_
  629.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  630.                     log->Add( " after resume " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  631.                           ":" + AnsiString( ms )
  632.                          );
  633.                     #endif
  634.                     if( this->Terminated )
  635.                     {
  636.                         this->OnTerminate = neByUser;
  637.                         return false;
  638.                     }
  639.                     if (! MF_rf_tx_level_cnf )
  640.                     {
  641.                         CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  642.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  643.                               " FAIL : ! MF_rf_tx_level_cnf"
  644.                             );
  645.                         return false;
  646.                     }
  647.                     #ifdef _MYDEBUG_
  648.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  649.                     log->Add( " before read TX power Q " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  650.                           ":" + AnsiString( ms )
  651.                         );
  652.                     #endif
  653.                     RestartTimerCal( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  654.                     if( ! m_rct_ctrl.RCT_ReadTxPower( m_pRct, m_virTX ) )
  655.                     {
  656.                         CalErrorHandler( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  657.                         if ( frmFatcory->rbAgilent8960->Checked )
  658.                         {
  659.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  660.                                   " FAIL : Agilent 8960< read TX power 1."
  661.                                 );
  662.                         }
  663.                         else
  664.                         {
  665.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  666.                                   " FAIL : CMU 200< read TX power 1."
  667.                                 );
  668.                         }
  669.                         return false;
  670.                     }
  671.                     frmFatcory->DisableAllCalTimer();
  672.                     m_dPCL_dBm[band_index][N] = m_virTX ;
  673.                     m_dPCL_power[band_index][N] =  pow(10.0, m_dPCL_dBm[band_index][N]/10.0)/1000.0;
  674.                     #ifdef _MYDEBUG_
  675.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  676.                     log->Add( " after read TX power Q " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  677.                           ":" + AnsiString( ms )
  678.                         );
  679.                     #endif
  680.                     if ( this->Terminated )
  681.                     {
  682.                         this->OnTerminate = neByUser;
  683.                         return false;
  684.                     }
  685.                     if ( frmFatcory->rbAgilent8960->Checked )
  686.                     {
  687.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  688.                               " Agilent 8960< read TX power = "+ Double_To_AnsiString( m_virTX )
  689.                             );
  690.                     }
  691.                     else
  692.                     {
  693.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  694.                               " CMU 200< read TX power = "+ Double_To_AnsiString( m_virTX )
  695.                             );
  696.                     }
  697.                     signed short  ss1;
  698.                     double d1;
  699.                     if (iRealRecursiveTimes != i_TXlevelRecusiveTimes-1) //iteration
  700.                     {
  701.                         if ( m_virTX > (atof(as_sub_wanted_p.c_str())+ m_dAPC_Delta) ||
  702.                              m_virTX < (atof(as_sub_wanted_p.c_str())- m_dAPC_Delta))
  703.                         {
  704.                             d1 = atof(as_sub_c.c_str()) * (atof(as_sub_wanted_p.c_str()) - m_virTX );
  705.                             // modified by Andy Ueng for always increment 1
  706.                             //  ss1 = (signed short) (d1>0) ? floor(d1+0.5): ceil(d1-0.5);  // to round up or down
  707.                             ss1 = (signed short) (d1>0) ? floor(d1+1.0): ceil(d1-1.0);
  708.                             unsigned short us_org_apc_dac = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() );
  709.                             unsigned short us_new_apc_dac;
  710.                             if ((us_org_apc_dac + ss1) <= MIN_APC_DAC )
  711.                             {
  712.                                 us_new_apc_dac = 1;
  713.                                 //MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt(), 1 );
  714.                             }
  715.                             else if ((us_org_apc_dac + ss1) > MAX_APC_DAC)
  716.                             {
  717.                                 us_new_apc_dac = MAX_APC_DAC;
  718.                                 //MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt(), MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() )+ss1 );
  719.                             }
  720.                             else
  721.                             {
  722.                                 us_new_apc_dac = us_org_apc_dac + ss1;
  723.                             }
  724.                             MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt(), us_new_apc_dac );
  725.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  726.                                   " band_index = " + IntToStr(band_index) + "," +
  727.                                   " power_index = " + IntToStr(FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()) + "," +
  728.                                   " MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[power_index] = " + IntToStr(us_new_apc_dac )
  729.                                 );
  730.                             // m_usPCLDacValue[band_index][N] = MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()];
  731.                             m_usPCLDacValue[band_index][N] = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() );
  732.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  733.                                   //" band_index = " + IntToStr(band_index) + "," +
  734.                                   //" N = " + IntToStr(N) + "," +
  735.                                   " m_usPCLDacValue[" + IntToStr(band_index) +"][" + IntToStr(N) +"] = " +
  736.                                    IntToStr(m_usPCLDacValue[band_index][N])
  737.                                 );
  738.                             //  log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  739.                             //          //  " MF_rf_apc_ptr->ApcDcOffset[band_index] = " + IntToStr(MF_rf_apc_ptr->ApcDcOffset[band_index])
  740.                             //           " MF_rf_apc_ptr->HighApcDcOffset[band_index] = " + IntToStr(MF_rf_apc_ptr->Get_HighApcDcOffset(band_index))
  741.                             //          );
  742.                             //m_dV_set[band_index][N] = (MF_rf_apc_ptr->ApcDcOffset[band_index] + m_usPCLDacValue[band_index][N]) / 1024.0 * 2.8 * 15.0 / 25.0; // added by Andy Ueng
  743.                             //log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  744.                             //         " band_index = " + IntToStr(band_index) + "," +
  745.                             //         " N = " + IntToStr(N) + "," +
  746.                             //         " m_dV_set[band_index][N] = " + Double_To_AnsiString(m_dV_set[band_index][N])
  747.                             //       );
  748.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  749.                                   " new S = "+
  750.                                  // AnsiString( MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()]  )
  751.                                   AnsiString( MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() )  )
  752.                                 );
  753.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  754.                                   " delta s = "+
  755.                                   AnsiString( ss1 )
  756.                                 );
  757.                             MF_rf_apc_ptr->ConfirmCallback = ccb_set_apc_level;
  758.                             //  MF_rf_apc_ptr->Req_Rf_SetRampApcLevel(eFreqBand, as_sub_tx_2cal_pcl.ToInt(), MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()]);
  759.                             MF_rf_apc_ptr->Req_Rf_SetRampApcLevel(eFreqBand, as_sub_tx_2cal_pcl.ToInt(), MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() ));
  760.                             if ( this->Terminated )
  761.                             {
  762.                                 this->OnTerminate = neByUser;
  763.                                 return false;
  764.                             }
  765.                             RestartTimerCal( WM_MF_SET_APC_DAC_FAIL );
  766.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  767.                                   " TimerCal->Enabled 9= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  768.                                 );
  769.                             is_suspend_cal = true;
  770.                             this->Suspend();
  771.                             is_suspend_cal = false;
  772.                             E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  773.                             if (state != METAAPP_SUCCESS)
  774.                             {
  775.                                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  776.                                 {
  777.                                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RAMPTABLE_xxx_LID_VERNO_FAIL);
  778.                                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  779.                                              " FAIL : NVRAM_EF_L1_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  780.                                             );
  781.                                 }
  782.                                 else
  783.                                 {
  784.                                     CalErrorHandler(WM_MF_SET_APC_DAC_FAIL);
  785.                                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  786.                                              " FAIL: Target< set APC level fail. PCL = "+ as_sub_tx_2cal_pcl +
  787.                                              " APC DAC = " +  IntToStr(MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() ))  + "n"
  788.                                             );
  789.                                     return false;
  790.                                 }
  791.                             }
  792.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  793.                                       " Target< set APC level ok. PCL = "+ as_sub_tx_2cal_pcl + "," +
  794.                                       " APC DAC = " +  IntToStr(MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt() ))  + "n"
  795.                                     );
  796.                             if ( this->Terminated )
  797.                             {
  798.                                 this->OnTerminate = neByUser;
  799.                                 return false;
  800.                             }
  801.                         }
  802.                         else
  803.                         {
  804.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  805.                                   " delta s = 0 "
  806.                                 );
  807.                             break; // for iRealRecursiveTimes
  808.                         }
  809.                     } // if no iteration
  810.                     else if( iRealRecursiveTimes == i_TXlevelRecusiveTimes-1 )
  811.                     {
  812.                         double d_max_p = atof(as_sub_max_p.c_str());
  813.                         double d_min_p = atof(as_sub_min_p.c_str());
  814.                         if ( m_virTX > d_max_p || m_virTX < d_min_p )
  815.                         {
  816.                             CalErrorHandler( WM_MF_RF_TX_LEVEL_CHECK_FAIL );
  817.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +  " " +
  818.                                       m_pRct->as_RCT+ " > read TX power = "+ Double_To_AnsiString( m_virTX ) +
  819.                                       " ( Out of Range ), min TX power = " + Double_To_AnsiString( d_min_p ) + ", max TX power = " + Double_To_AnsiString( d_max_p )
  820.                         );
  821.                         return false;
  822.                     }
  823.                     else
  824.                     {
  825.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  826.                           m_pRct->as_RCT+ " > read TX power = "+ Double_To_AnsiString( m_virTX ) +
  827.                           " ( Pass ), min TX power = " + Double_To_AnsiString( d_min_p ) + ", max TX power = " + Double_To_AnsiString( d_max_p )
  828.                           );
  829.                     }
  830.                 }
  831.                 if ( this->Terminated )
  832.                 {
  833.                     this->OnTerminate = neByUser;
  834.                     return false;
  835.                 }
  836.                 } // for iRealRecursiveTimes
  837.             } // for N
  838.         } // single slot
  839.         else // multi-slot
  840.         {
  841.             unsigned int ui_tx_2cal_pcl[RF_TX_LEVEL_2_CAL_COUNT];
  842.             double d_max_p[RF_TX_LEVEL_2_CAL_COUNT];
  843.             double d_min_p[RF_TX_LEVEL_2_CAL_COUNT];
  844.             double d_wanted_p[RF_TX_LEVEL_2_CAL_COUNT];
  845.             S_MULTISLOT_BURST_POWER virTX_p;
  846.             unsigned int ui_c[RF_TX_LEVEL_2_CAL_COUNT];
  847.             unsigned short us_apc_dac[RF_TX_LEVEL_2_CAL_COUNT];
  848.             for( int N=0; N<i_TX_level_count; N++)  // DCS,PCS:16~1 or GSM:15~1
  849.             {
  850.                 if (! getAnsiStrSubItem( as_tx_2cal_pcl, N+1, DEFAULT_SEP_CHAR, as_sub_tx_2cal_pcl) )
  851.                 {
  852.                     CalErrorHandler( WM_MF_CFG_READ_FAIL );
  853.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  854.                               " FAIL: Read config file fail(sub_tx_cal_pcl)."
  855.                             );
  856.                     return false;
  857.                 }
  858.                 ui_tx_2cal_pcl[N] = as_sub_tx_2cal_pcl.ToInt();
  859.                 if (! getAnsiStrSubItem( as_max_p, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_max_p) )
  860.                 {
  861.                     CalErrorHandler( WM_MF_CFG_READ_FAIL );
  862.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  863.                               " FAIL: Read config file fail(sub_max_p)."
  864.                             );
  865.                     return false;
  866.                 }
  867.                 d_max_p[N] = atof(as_sub_max_p.c_str());
  868.                 if (! getAnsiStrSubItem( as_min_p, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_min_p) )
  869.                 {
  870.                     CalErrorHandler( WM_MF_CFG_READ_FAIL );
  871.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  872.                               " FAIL: Read config file fail(sub_min_p)."
  873.                             );
  874.                     return false;
  875.                 }
  876.                 d_min_p[N] = atof(as_sub_min_p.c_str());
  877.                 if (! getAnsiStrSubItem( as_wanted_p, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_wanted_p) )
  878.                 {
  879.                     CalErrorHandler( WM_MF_CFG_READ_FAIL );
  880.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  881.                               " FAIL: Read config file fail(sub_wanted_p)."
  882.                             );
  883.                     return false;
  884.                 }
  885.                 d_wanted_p[N] = atof(as_sub_wanted_p.c_str());
  886.                 if (! getAnsiStrSubItem( as_c, FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_c) )
  887.                 {
  888.                     CalErrorHandler( WM_MF_CFG_READ_FAIL );
  889.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  890.                               " FAIL: Read config file fail(c)."
  891.                             );
  892.                     return false;
  893.                 }
  894.                 ui_c[N] = as_sub_c.ToInt();
  895.             } // for(N)
  896.             for( iRealRecursiveTimes=0; iRealRecursiveTimes<i_TXlevelRecusiveTimes;
  897.                          iRealRecursiveTimes++) // marked by Andy Ueng for no iteration test
  898.             {
  899.                 log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  900.                               " RecursiveTimes = " + IntToStr(iRealRecursiveTimes)
  901.                             );
  902.                 if( 0 == iRealRecursiveTimes )
  903.                 {
  904.                     m_usPCLDacValue[band_index][0] = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[0] );
  905.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  906.                                   " m_usPCLDacValue[" +IntToStr(band_index) + "][" + IntToStr(0) + "] = " +
  907.                                     IntToStr(m_usPCLDacValue[band_index][0])
  908.                             );
  909.                 }
  910.                 RestartTimerCal( WM_MF_RF_STOP_FAIL );
  911.                 if (!MF_rf_stop.REQ_Start())
  912.                 {
  913.                     CalErrorHandler( WM_MF_RF_STOP_FAIL );
  914.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  915.                           " FAIL: Target < stop RF fail."
  916.                         );
  917.                     return false;
  918.                 }
  919.                 int   pcl[4];
  920.                 CodingScheme cs[4];
  921.                 cs[0]    = CodingSchemeMCS5;
  922.                 cs[1]    = CodingSchemeMCS5;
  923.                 pcl[0]   = ui_tx_2cal_pcl[0];
  924.                 pcl[1]   = ui_tx_2cal_pcl[1];
  925.                 S_MULTI_SLOT_TX_T multi_slot_tx;
  926.                 multi_slot_tx.b_MultiSlotTXExSupport = m_pCal->b_MultiSlotTxExSupport;
  927.                 multi_slot_tx.e_bandsel = band_index;
  928.                 multi_slot_tx.req.arfcn = s_ARFCN;
  929.                 multi_slot_tx.req.bsic  = m_cTSC;
  930.                 multi_slot_tx.req.timeSlotmask = 0x03;
  931.                 for (int i=0; i<4; i++)
  932.                 {
  933.                     multi_slot_tx.req.powerLev[i] = pcl[i];
  934.                     multi_slot_tx.req.cs[i]       = cs[i];
  935.                 }
  936.                 multi_slot_tx.req.ta = 0;
  937.                 multi_slot_tx.req.frames = -99;
  938.                 multi_slot_tx.req.dacValue = m_sDefault_value;
  939.                 multi_slot_tx.req.pattern = NB_TX_RANDOM_WITH_TSC;
  940.                 multi_slot_tx.req.pattern_data = 0;
  941.                 RF_MULTI_SLOT_TX_Obj.ConfirmCallback = ::ConfirmCallback_MultiSlotTX;
  942.                 RF_MULTI_SLOT_TX_Obj.REQ_Start(multi_slot_tx);
  943.                 if ( this->Terminated )
  944.                 {
  945.                     this->OnTerminate = neByUser;
  946.                     return false;
  947.                 }
  948.                 RestartTimerCal( WM_MF_RF_TX_LEVEL_FAIL );
  949.                 SUSPEND_CAL_THREAD
  950.                 if( RF_MULTI_SLOT_TX_Obj.Get_ConfirmState() != METAAPP_SUCCESS )
  951.                 {
  952.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  953.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  954.                           " FAIL: Target < multislot TX fail."
  955.                     );
  956.                     return false;
  957.                 }
  958.                 if( ! m_rct_ctrl.RCT_readMultiSlotAverageTxPower( m_pRct, &virTX_p ) )
  959.                 {
  960.                     CalErrorHandler( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  961.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  962.                                   " FAIL: " + m_pRct->as_RCT + " < read multislot TX power ."
  963.                                 );
  964.                     return false;
  965.                 }
  966.                 frmFatcory->DisableAllCalTimer();
  967.                 for ( int N=0; N<i_TX_level_count; N++)  // DCS,PCS:16~1 or GSM:15~1
  968.                 {
  969.                     m_dPCL_dBm[band_index][N] = virTX_p.d_burstpower[N] ;
  970.                     m_dPCL_power[band_index][N] =  pow(10.0, m_dPCL_dBm[band_index][N]/10.0)/1000.0;
  971.                     if ( this->Terminated )
  972.                     {
  973.                         this->OnTerminate = neByUser;
  974.                         return false;
  975.                     }
  976.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  977.                           m_pRct->as_RCT + " < read slot " + IntToStr(N) + " TX power = " + Double_To_AnsiString( virTX_p.d_burstpower[N] )
  978.                          );
  979.                     signed short  ss1;
  980.                     double d1;
  981.                     if (iRealRecursiveTimes != i_TXlevelRecusiveTimes-1) //iteration
  982.                     {
  983.                         if ( virTX_p.d_burstpower[N] > (d_wanted_p[N]+ m_dAPC_Delta) ||
  984.                              virTX_p.d_burstpower[N] < (d_wanted_p[N]- m_dAPC_Delta))
  985.                         {
  986.                             d1 = ui_c[N] * ( d_wanted_p[N] - virTX_p.d_burstpower[N] );
  987.                             ss1 = (signed short) (d1>0) ? floor(d1+1.0): ceil(d1-1.0);
  988.                             unsigned short us_org_apc_dac = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N] );
  989.                             unsigned short us_new_apc_dac;
  990.                             if ( (us_org_apc_dac + ss1) <= 0 )
  991.                             {
  992.                                 us_new_apc_dac = 1;
  993.                                 //MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N], 1 );
  994.                             }
  995.                             else if ( (us_org_apc_dac + ss1) > MAX_APC_DAC )
  996.                             {
  997.                                 us_new_apc_dac = MAX_APC_DAC;
  998.                                 //MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N], MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N] )+ss1 );
  999.                             }
  1000.                             else
  1001.                             {
  1002.                                 us_new_apc_dac = us_org_apc_dac + ss1;
  1003.                             }
  1004.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1005.                                   " band_index = " + IntToStr(band_index) + "," +
  1006.                                   " power_index = " + IntToStr(FIRST_PCL-as_sub_tx_2cal_pcl.ToInt()) + "," +
  1007.                                   " MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[power_index] = " + IntToStr( us_new_apc_dac )
  1008.                                 );
  1009.                             m_usPCLDacValue[band_index][N] = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N] );
  1010.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1011.                                   " m_usPCLDacValue[" + IntToStr(band_index) +"][" + IntToStr(N) +"] = " +
  1012.                                    IntToStr(m_usPCLDacValue[band_index][N])
  1013.                                 );
  1014.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1015.                                   " new S = "+
  1016.                                   AnsiString( MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N] )  )
  1017.                                 );
  1018.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1019.                                   " delta s = "+
  1020.                                   AnsiString( ss1 )
  1021.                                 );
  1022.                             MF_rf_apc_ptr->ConfirmCallback = ccb_set_apc_level;
  1023.                             MF_rf_apc_ptr->Req_Rf_SetRampApcLevel(eFreqBand, ui_tx_2cal_pcl[N], MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N] ));
  1024.                             if ( this->Terminated )
  1025.                             {
  1026.                                 this->OnTerminate = neByUser;
  1027.                                 return false;
  1028.                             }
  1029.                             RestartTimerCal( WM_MF_SET_APC_DAC_FAIL );
  1030.                            // log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1031.                            //       " TimerCal->Enabled 9= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1032.                            //     );
  1033.                             is_suspend_cal = true;
  1034.                             this->Suspend();
  1035.                             is_suspend_cal = false;
  1036.                             E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  1037.                             if (state != METAAPP_SUCCESS)
  1038.                             {
  1039.                                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  1040.                                 {
  1041.                                     CalErrorHandler(WM_MF_NVRAM_EF_L1_RAMPTABLE_xxx_LID_VERNO_FAIL);
  1042.                                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1043.                                              " FAIL : NVRAM_EF_L1_RAMPTABLE_xxx_LID version is not support, please update META to latest version. "
  1044.                                             );
  1045.                                 }
  1046.                                 else
  1047.                                 {
  1048.                                     CalErrorHandler( WM_MF_SET_APC_DAC_FAIL );
  1049.                                     log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1050.                                              " FAIL: Target< set APC level fail. PCL = "+ as_sub_tx_2cal_pcl +
  1051.                                              " APC DAC = " +  IntToStr(MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N] ))  + "n"
  1052.                                             );
  1053.                                 }
  1054.                                 return false;
  1055.                             }
  1056.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1057.                                       " Target< set APC level ok. PCL = "+ as_sub_tx_2cal_pcl + "," +
  1058.                                       " APC DAC = " +  IntToStr(MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-ui_tx_2cal_pcl[N] ))  + "n"
  1059.                                     );
  1060.                             if ( this->Terminated )
  1061.                             {
  1062.                                 this->OnTerminate = neByUser;
  1063.                                 return false;
  1064.                             }
  1065.                         }
  1066.                         else
  1067.                         {
  1068.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1069.                                   " delta s = 0 "
  1070.                                 );
  1071.                             break; // for iRealRecursiveTimes
  1072.                         }
  1073.                     } // if no iteration
  1074.                     else if( iRealRecursiveTimes == i_TXlevelRecusiveTimes-1 )
  1075.                     {
  1076.                         if( virTX_p.d_burstpower[N] > d_max_p[N] || virTX_p.d_burstpower[N] < d_min_p[N] )
  1077.                         {
  1078.                             CalErrorHandler( WM_MF_RF_TX_LEVEL_CHECK_FAIL );
  1079.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  1080.                                       m_pRct->as_RCT + " > read TX power = "+ Double_To_AnsiString( virTX_p.d_burstpower[N] ) +
  1081.                                       " ( Out of Range ), min TX power = " + Double_To_AnsiString( d_min_p[N] ) + ", max TX power = " + Double_To_AnsiString( d_max_p[N] )
  1082.                                     );
  1083.                             return false;
  1084.                         }
  1085.                         else
  1086.                         {
  1087.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  1088.                                       m_pRct->as_RCT + " > read TX power = "+ Double_To_AnsiString( virTX_p.d_burstpower[N] ) +
  1089.                                       " ( Pass ), min TX power = " + Double_To_AnsiString( d_min_p[N] ) + ", max TX power = " + Double_To_AnsiString( d_max_p[N] )
  1090.                                     );
  1091.                         }
  1092.                     }
  1093.                 } // for(N)
  1094.                 if ( this->Terminated )
  1095.                 {
  1096.                     this->OnTerminate = neByUser;
  1097.                     return false;
  1098.                 }
  1099.             } // for(iRealRecursiveTimes)
  1100.         } // multi-slot
  1101.         eFreqBand++;
  1102.         if ( this->Terminated )
  1103.         {
  1104.             this->OnTerminate = neByUser;
  1105.             return false;
  1106.         }
  1107.     } // while
  1108.         // Sleep( i_N_TX*4.615);
  1109.         if( ! m_rct_ctrl.RCT_TXPowerContOff( m_pRct ) )
  1110.         {
  1111.                 CalErrorHandler( WM_MF_AGE8960_SET_TX_POWER_CONT_OFF_FAIL );
  1112.                 if ( frmFatcory->rbAgilent8960->Checked )
  1113.                 {
  1114.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1115.                               " FAIL : Agilent 8960< set continuous TX power OFF "
  1116.                             );
  1117.                 }
  1118.                 else
  1119.                 {
  1120.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1121.                           " FAIL : cmu200 < set continuous TX power OFF "
  1122.                         );
  1123.                 }
  1124.                 return false;
  1125.         }
  1126.         if ( this->Terminated )
  1127.         {
  1128.             this->OnTerminate = neByUser;
  1129.             return false;
  1130.         }
  1131.        
  1132.         log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1133.              " ============= TX power control level calibration end ============= n "
  1134.            );
  1135.     RestartTimerCal(WM_MF_RF_TX_LEVEL_FAIL);
  1136.     if (!MF_rf_stop.REQ_Start())
  1137.     {
  1138.         CalErrorHandler( WM_MF_RF_STOP_FAIL );
  1139.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1140.                           " FAIL: Target < stop RF fail."
  1141.                         );
  1142.         return false;
  1143.     }
  1144.     frmFatcory->DisableAllCalTimer();
  1145.     log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1146.              " ============= GMSK TX power control level calibration end ============= n "
  1147.            );
  1148.            
  1149.     if( m_asAPC_Check.AnsiCompareIC("yes") != 0 )
  1150.     {
  1151.         PostMessage(
  1152.                      ctrl.hPostMsgDestHandle,
  1153.                      WM_MF_RF_TX_LEVEL_CALIBRATION_DONE_NO_CHECK,
  1154.                      0,
  1155.                      0
  1156.                     );
  1157.         frmFatcory->DisableAllCalTimer();
  1158.         log->Add( "n" + DateToStr(Date()) + " "+ CurrentTimeStr() +
  1159.                " TX PCL calibration done (without TX PCL check procedure)! n"
  1160.             );
  1161.     }
  1162.     
  1163.     return true;
  1164. }
  1165. //=========================================================================
  1166. bool __fastcall T_META_factory_calibration::TXPCLCal_3cal(void)
  1167. {
  1168.     AnsiString  as_tx_pcl;
  1169.     AnsiString  as_tx_cal_pcl;
  1170.     AnsiString  as_max_p;
  1171.     AnsiString  as_wanted_p;
  1172.     AnsiString  as_min_p;
  1173.     AnsiString  as_c;
  1174.     AnsiString  as_sub_tx_pcl;
  1175.     AnsiString  as_sub_tx_cal_pcl;
  1176.     AnsiString  as_sub_max_p;
  1177.     AnsiString  as_sub_wanted_p;
  1178.     AnsiString  as_sub_min_p;
  1179.     AnsiString  as_sub_c;
  1180.     int FIRST_PCL=0;
  1181.     log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1182.              " ============ GMSK TX power control level calibration begin ============ n "
  1183.            );
  1184.     if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  1185.     {
  1186.         if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GSM_BCH_TCH ) )
  1187.         {
  1188.             CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  1189.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1190.                               " FAIL: "+
  1191.                               m_pRct->as_RCT + " < Operation mode = GSM BCH+TCH"
  1192.                             );
  1193.             return false;
  1194.         }
  1195.     }
  1196.     else
  1197.     {
  1198.         if( ! m_rct_ctrl.RCT_operatingMode( m_pRct, OPERATING_MODE_GPRS_BCH_PDTCH) )
  1199.         {
  1200.             CalErrorHandler( WM_MF_AGE8960_SET_OPERATION_MODE_FAIL );
  1201.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1202.                               " FAIL: "+
  1203.                               m_pRct->as_RCT + " < Operation mode = GPRS BCH+PDTCH"
  1204.                             );
  1205.             return false;
  1206.         }
  1207.     }
  1208.     short s_ARFCN;
  1209.     FrequencyBand eFreqBand = FrequencyBand850;
  1210.     i_N_TX = -99; // TX frames
  1211.     //---------------------------------------------------------------------------
  1212.     while (eFreqBand < FrequencyBandCount)
  1213.     {
  1214.         switch (eFreqBand)
  1215.         {
  1216.             case FrequencyBand850:
  1217.             {
  1218.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  1219.                 {
  1220.                     eFreqBand++;
  1221.                     continue;
  1222.                 }
  1223.                 FIRST_PCL = GSM850_FIRST_PCL;
  1224.                 s_ARFCN = m_sARFCN_C0_GSM850;
  1225.                 i_TX_level_count = RF_TX_LEVEL_3_CAL_COUNT;
  1226.                 as_tx_cal_pcl    = m_as_gsm850_tx_cal_pcl;
  1227.                 as_tx_pcl        = m_as_gsm850_tx_pcl;
  1228.                 as_max_p         = m_as_gsm850_max_p;
  1229.                 as_wanted_p      = m_as_gsm850_wanted_p;
  1230.                 as_min_p         = m_as_gsm850_min_p;
  1231.                 as_c             = m_as_gsm850_c;
  1232.             }
  1233.             break;
  1234.             case FrequencyBand900:
  1235.             {
  1236.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  1237.                 {
  1238.                     eFreqBand++;
  1239.                     continue;
  1240.                 }
  1241.                 FIRST_PCL = GSM900_FIRST_PCL;
  1242.                 s_ARFCN = m_sARFCN_C0_GSM;
  1243.                 i_TX_level_count = RF_TX_LEVEL_3_CAL_COUNT;
  1244.                 as_tx_cal_pcl    = m_as_gsm900_tx_cal_pcl;
  1245.                 as_tx_pcl        = m_as_gsm900_tx_pcl;
  1246.                 as_max_p         = m_as_gsm900_max_p;
  1247.                 as_wanted_p      = m_as_gsm900_wanted_p;
  1248.                 as_min_p         = m_as_gsm900_min_p;
  1249.                 as_c             = m_as_gsm900_c;
  1250.             }
  1251.             break;
  1252.             case FrequencyBand1800:
  1253.             {
  1254.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  1255.                 {
  1256.                     eFreqBand++;
  1257.                     continue;
  1258.                 }
  1259.                 FIRST_PCL = DCS1800_FIRST_PCL;
  1260.                 s_ARFCN = m_sARFCN_C0_DCS;
  1261.                 i_TX_level_count = RF_TX_LEVEL_3_CAL_COUNT;
  1262.                 as_tx_cal_pcl    = m_as_dcs1800_tx_cal_pcl;
  1263.                 as_tx_pcl        = m_as_dcs1800_tx_pcl;
  1264.                 as_max_p         = m_as_dcs1800_max_p;
  1265.                 as_wanted_p      = m_as_dcs1800_wanted_p;
  1266.                 as_min_p         = m_as_dcs1800_min_p;
  1267.                 as_c             = m_as_dcs1800_c;
  1268.             }
  1269.             break;
  1270.             case FrequencyBand1900:
  1271.             {
  1272.                 if (!IsFreqBankSupported(m_asFreqBank, eFreqBand))
  1273.                 {
  1274.                     eFreqBand++;
  1275.                     continue;
  1276.                 }
  1277.                 FIRST_PCL = PCS1900_FIRST_PCL;
  1278.                 s_ARFCN = m_sARFCN_C0_PCS;
  1279.                 i_TX_level_count = RF_TX_LEVEL_3_CAL_COUNT;
  1280.                 as_tx_cal_pcl    = m_as_pcs1900_tx_cal_pcl;
  1281.                 as_tx_pcl        = m_as_pcs1900_tx_pcl;
  1282.                 as_max_p         = m_as_pcs1900_max_p;
  1283.                 as_wanted_p      = m_as_pcs1900_wanted_p;
  1284.                 as_min_p         = m_as_pcs1900_min_p;
  1285.                 as_c             = m_as_pcs1900_c;
  1286.             }
  1287.             break;
  1288.             default:
  1289.             {
  1290.                 assert(false);
  1291.             }
  1292.             break;
  1293.         } // switch
  1294.         E_BANDSEL band_index = FreqBand_To_BandIdx(eFreqBand);
  1295.         int vi_Band = FreqBand_To_AgeBand(eFreqBand);
  1296.         if ( this->Terminated )
  1297.         {
  1298.             this->OnTerminate = neByUser;
  1299.             return false;
  1300.         }
  1301.         log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1302.                          " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  1303.                        );
  1304.         if (!MF_rf_apc_ptr->ApcSectionExist(m_pCal->as_IniFile.c_str(), band_index))
  1305.         {
  1306.             CalErrorHandler(WM_MF_RF_TX_LEVEL_INI_FAIL);
  1307.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1308.                      " FAIL :"+
  1309.                      " APC section is not in initial file: " + FreqBand_To_Str(eFreqBand)
  1310.                      );
  1311.             return false;
  1312.         }
  1313.         if (IsFreqBankSupported(  m_asFreqBank, eFreqBand))
  1314.         {
  1315.             if( FrequencyBand1900 == eFreqBand )
  1316.             {
  1317.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 1, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  1318.                 {
  1319.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1320.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1321.                                   " FAIL: target < target < set Band1900 flag = 1"
  1322.                                 );
  1323.                     return false;
  1324.                 }
  1325.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1326.                               " target < set Band1900 flag = 1"
  1327.                             );
  1328.             }
  1329.             else
  1330.             {
  1331.                 if(META_Rf_SelectFrequencyBand1900_r( m_pCal->i_MainMETAHandle, 0, MF_SelectBand_cnf_cb, &MF_SelectBand_token, NULL) !=  META_SUCCESS)
  1332.                 {
  1333.                     CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1334.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1335.                                   " FAIL: target < set Band1900 flag = 0"
  1336.                                 );
  1337.                     return false;
  1338.                 }
  1339.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1340.                               " target < set Band1900 flag = 0"
  1341.                             );
  1342.             }
  1343.             is_suspend_cal = true;
  1344.             this->Suspend();
  1345.             is_suspend_cal = false;
  1346.         }
  1347.         if( ! m_rct_ctrl.RCT_cellBand( m_pRct, vi_Band ) )
  1348.         {
  1349.             CalErrorHandler( WM_MF_AGE8960_SET_BAND_FAIL );
  1350.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1351.                               " FAIL: " +
  1352.                               m_pRct->as_RCT + " < Band = " + ViBand_To_Str( vi_Band )
  1353.                             );
  1354.             return false;
  1355.         }
  1356.   #ifdef _MYDEBUG_
  1357.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1358.                       " Sleep 100 " + AnsiString(__FILE__)
  1359.                     );
  1360.   #endif
  1361.         Sleep( 100 );  // wait Agilent 8960 cell band setup ready!
  1362.                    // if Agilent 8960 not read, target will get error power!
  1363.         if ( this->Terminated )
  1364.         {
  1365.             this->OnTerminate = neByUser;
  1366.             return false;
  1367.         }
  1368.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  1369.                           m_pRct->as_RCT + " < Band = " + ViBand_To_Str( vi_Band )
  1370.                         );
  1371.         short bch_arfcn = ::Get_SeperateChannel( vi_Band, s_ARFCN );
  1372.         if( ! m_rct_ctrl.RCT_BCHARFCN( m_pRct, bch_arfcn) )
  1373.         {
  1374.             CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  1375.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1376.                               " FAIL: " +
  1377.                               m_pRct->as_RCT + " < BCH ARFCN = " + IntToStr( bch_arfcn )
  1378.                     );
  1379.             return false;
  1380.         }
  1381.         if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  1382.         {
  1383.             if( ! m_rct_ctrl.RCT_TCHARFCN( m_pRct, s_ARFCN ) )
  1384.             {
  1385.                 CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  1386.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1387.                               " FAIL: "+
  1388.                               m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr( s_ARFCN )
  1389.                             );
  1390.                 return false;
  1391.             }
  1392.         }
  1393.         else
  1394.         {
  1395.             if( ! m_rct_ctrl.RCT_PDTCHARFCN( m_pRct, s_ARFCN ) )
  1396.             {
  1397.                 CalErrorHandler( WM_MF_AGE8960_SET_TCH_ARFCN_FAIL );
  1398.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1399.                                   " FAIL: "+
  1400.                                   m_pRct->as_RCT + " < PDTCH ARFCN = " + IntToStr( s_ARFCN )
  1401.                                 );
  1402.                 return false;
  1403.             }
  1404.         }
  1405.         if( this->Terminated )
  1406.         {
  1407.             this->OnTerminate = neByUser;
  1408.             return false;
  1409.         }
  1410.         if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  1411.         {
  1412.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  1413.                           m_pRct->as_RCT + " < TCH ARFCN = " + IntToStr( s_ARFCN )
  1414.                         );
  1415.         }
  1416.         else
  1417.         {
  1418.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  1419.                           m_pRct->as_RCT + " < PDTCH ARFCN = " + IntToStr( s_ARFCN )
  1420.                         );
  1421.         }
  1422.         if( ! m_rct_ctrl.RCT_ConfigTSC( m_pRct, m_cTSC ) )
  1423.         {
  1424.             CalErrorHandler( WM_MF_AGE8960_SET_TSC_FAIL );
  1425.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() + " " +
  1426.                               "FAIL: " + m_pRct->as_RCT + " < set TSC fail."
  1427.                             );
  1428.             return false;
  1429.         }
  1430.         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +  " " +
  1431.                       m_pRct->as_RCT + " < TSC = " + IntToStr( m_cTSC)
  1432.                     );
  1433.             // log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1434.             //              " TimerCal->Enabled 8= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1435.             //            );
  1436.         RestartTimerCal( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  1437.         if (!m_rct_ctrl.RCT_Config_ExpectedTxPower(m_pRct, true, 0))
  1438.         {
  1439.             RestartTimerCal(WM_MF_AGE8960_CONFIG_TX_POWER_FAIL);
  1440.             log->Add(DateToStr(Date()) +  " " + CurrentTimeStr() +
  1441.                                   " FAIL : " + m_pRct->as_RCT + " < config expected TX power to auto fail"
  1442.                                 );
  1443.             return false;
  1444.         }
  1445.         if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  1446.         {
  1447.             if( ! m_rct_ctrl.RCT_confTXPower( m_pRct ) )
  1448.             {
  1449.                 CalErrorHandler( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  1450.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1451.                                   " FAIL: " + m_pRct->as_RCT + " < config TX power 1."
  1452.                                 );
  1453.                 return false;
  1454.             }
  1455.         }
  1456.         else
  1457.         {
  1458.             if( ! m_rct_ctrl.RCT_confMultiSlotTxPower( m_pRct, AGE1960_TX_POWER_MEASURE_COUNT, MOD_GMSK ) )
  1459.             {
  1460.                 CalErrorHandler( WM_MF_AGE8960_CONFIG_TX_POWER_FAIL );
  1461.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1462.                                   " FAIL: " + m_pRct->as_RCT + " < config TX power 1."
  1463.                                 );
  1464.                 return false;
  1465.             }
  1466.         }
  1467.         if( ! m_rct_ctrl.RCT_TXPowerContOff( m_pRct ) )
  1468.         {
  1469.             CalErrorHandler( WM_MF_AGE8960_SET_TX_POWER_CONT_OFF_FAIL );
  1470.             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1471.                               " FAIL: " + m_pRct->as_RCT + " < set TX power continuous OFF "
  1472.                             );
  1473.             return false;
  1474.         }
  1475.         if ( this->Terminated )
  1476.         {
  1477.             this->OnTerminate = neByUser;
  1478.             return false;
  1479.         }
  1480.         // calibration from high power to low power
  1481.         // index from big to little.
  1482.         // PCL string in config file from right to left.
  1483.         int iRealRecursiveTimes = 0;
  1484.         if( TX_SLOT_SINGLE == m_pCal->e_tx_slot )
  1485.         {
  1486.             for ( int N=0; N<i_TX_level_count; N++)  // DCS,PCS:16~1 or GSM:15~1
  1487.             {
  1488.                 log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1489.                        " ------------------------------------------------------------------ "
  1490.                     );
  1491.                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1492.                       " N = " + N
  1493.                     );
  1494.                 if (! getAnsiStrSubItem( as_tx_cal_pcl, N+1, DEFAULT_SEP_CHAR, as_sub_tx_cal_pcl) )
  1495.                 {
  1496.                     CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1497.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1498.                           " FAIL: Read config file fail(sub_tx_cal_pcl)."
  1499.                         );
  1500.                     return false;
  1501.                 }
  1502.                 for( iRealRecursiveTimes=0; iRealRecursiveTimes<i_TXlevelRecusiveTimes;
  1503.                      iRealRecursiveTimes++) // marked by Andy Ueng for no iteration test
  1504.                 {
  1505.                     log->Add( "n" + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1506.                           " RecursiveTimes = " + IntToStr(iRealRecursiveTimes)
  1507.                         );
  1508.                     if( 0 == iRealRecursiveTimes )
  1509.                     {
  1510.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1511.                               " band_index = " + IntToStr(band_index) + "," +
  1512.                               " power_index = " + IntToStr(FIRST_PCL-as_sub_tx_cal_pcl.ToInt()) + "," +
  1513.                               " MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[power_index] = " + IntToStr(MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt() ))
  1514.                             );
  1515.                         m_usPCLDacValue[band_index][N] = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt() );
  1516.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1517.                               " band_index = " + IntToStr(band_index) + "," +
  1518.                               " N = " + IntToStr(N) + "," +
  1519.                               " m_usPCLDacValue[band_index][N] = " + IntToStr(m_usPCLDacValue[band_index][N])
  1520.                             );
  1521.                         if( ! MF_rf_apc_ptr->Get_RealApcDcOffset( m_pCal->b_TADOSupport, band_index, as_sub_tx_cal_pcl.ToInt(), m_iApcDcOffset ) )
  1522.                         {
  1523.                             CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1524.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1525.                                       " FAIL: Get APC DC offset " +
  1526.                                       " TADO support = " + IntToStr( m_pCal->b_TADOSupport ) +
  1527.                                       " eFreqBand = " + IntToStr( eFreqBand ) +
  1528.                                       " pcl = " + as_sub_tx_cal_pcl
  1529.                                     );
  1530.                              return false;
  1531.                         }
  1532.                         m_dV_set[band_index][N] = (m_iApcDcOffset + m_usPCLDacValue[band_index][N]) / 1024.0 * 2.8 * 15.0 / 25.0;
  1533.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1534.                               " band_index = " + IntToStr(band_index) + "," +
  1535.                               " N  = " + IntToStr(N) + "," +
  1536.                               " m_dV_set[band_index][N] = " + Double_To_AnsiString(m_dV_set[band_index][N])
  1537.                             );
  1538.                     }
  1539.                     // index start from 1
  1540.                     if (! getAnsiStrSubItem( as_tx_pcl, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_tx_pcl) )
  1541.                     {
  1542.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1543.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1544.                               " FAIL: Read config file fail(sub_tx_cal_pcl 2)."
  1545.                             );
  1546.                         return false;
  1547.                     }
  1548.                     if (! getAnsiStrSubItem( as_max_p, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_max_p) )
  1549.                     {
  1550.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1551.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1552.                               " FAIL: Read config file fail(sub_max_p)."
  1553.                             );
  1554.                         return false;
  1555.                     }
  1556.                     if (! getAnsiStrSubItem( as_min_p, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_min_p) )
  1557.                     {
  1558.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1559.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1560.                               " FAIL: Read config file fail(sub_min_p)."
  1561.                             );
  1562.                         return false;
  1563.                     }
  1564.                     if(! getAnsiStrSubItem( as_wanted_p, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_wanted_p) )
  1565.                     {
  1566.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1567.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1568.                               " FAIL: Read config file fail(sub_wanted_p)."
  1569.                             );
  1570.                         return false;
  1571.                     }
  1572.                     if(! getAnsiStrSubItem( as_c, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()+1, DEFAULT_SEP_CHAR, as_sub_c) )
  1573.                     {
  1574.                         CalErrorHandler( WM_MF_CFG_READ_FAIL );
  1575.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1576.                               " FAIL: Read config file fail(c)."
  1577.                             );
  1578.                         return false;
  1579.                     }
  1580.                     if( ! m_rct_ctrl.RCT_MSTXLevel( m_pRct, as_sub_tx_pcl.ToInt()) )
  1581.                     {
  1582.                         CalErrorHandler( WM_MF_AGE8960_SET_MS_TX_LEVEL_FAIL );
  1583.                         if ( frmFatcory->rbAgilent8960->Checked )
  1584.                         {
  1585.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1586.                                   " FAIL : Agilent 8960< MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  1587.                                 );
  1588.                         }
  1589.                         else
  1590.                         {
  1591.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1592.                                   " FAIL : cmu200 > MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  1593.                                 );
  1594.                         }
  1595.                         return false;
  1596.                     }
  1597.                     if( frmFatcory->rbAgilent8960->Checked )
  1598.                     {
  1599.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1600.                               " Agilent 8960< MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  1601.                             );
  1602.                     }
  1603.                     else
  1604.                     {
  1605.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1606.                               " CMU200 < MS TX level = " + IntToStr( as_sub_tx_pcl.ToInt())
  1607.                             );
  1608.                     }
  1609.                     if ( this->Terminated )
  1610.                     {
  1611.                         this->OnTerminate = neByUser;
  1612.                         return false;
  1613.                     }
  1614.                     RestartTimerCal( WM_MF_RF_TX_LEVEL_FAIL );
  1615.                     if (!MF_rf_stop.REQ_Start())
  1616.                     {
  1617.                         CalErrorHandler( WM_MF_RF_STOP_FAIL );
  1618.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1619.                                   " FAIL: Target < stop RF fail."
  1620.                                 );
  1621.                         return false;
  1622.                     }
  1623.                     MF_rf_tx_level_req->arfcn = s_ARFCN;
  1624.                     MF_rf_tx_level_req->bsic  =  m_cTSC;
  1625.                     MF_rf_tx_level_req->power    = as_sub_tx_pcl.ToInt();
  1626.                     MF_rf_tx_level_req->frames   = i_N_TX;
  1627.                     MF_rf_tx_level_req->dacValue = m_sDefault_value;
  1628.                     MF_rf_tx_level_req->burstTypeNB = NB_TX_RANDOM_WITH_TSC; // NB burst
  1629.                     if ( META_Rf_NB_TX_r( m_pCal->i_MainMETAHandle,
  1630.                      MF_rf_tx_level_req,
  1631.                      MF_rf_tx_level_cb,
  1632.                      &MF_rf_tx_level_token,
  1633.                                      NULL
  1634.                                   ) != META_SUCCESS)
  1635.                     {
  1636.                         CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1637.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1638.                               " FAIL : Target< ARFCN = " + IntToStr( s_ARFCN ) +
  1639.                               ", TSC = " + IntToStr( m_cTSC ) +
  1640.                               ", power = " + as_sub_tx_pcl +
  1641.                               ", frames = " + IntToStr(i_N_TX) +
  1642.                               ", dac value = " + IntToStr( m_sDefault_value )
  1643.                             );
  1644.                         return false;
  1645.                     }
  1646.                     if ( this->Terminated )
  1647.                     {
  1648.                         this->OnTerminate = neByUser;
  1649.                         return false;
  1650.                     }
  1651.                 #ifdef _MYDEBUG_
  1652.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  1653.                 #endif
  1654.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1655.                 #ifdef _MYDEBUG_
  1656.                 ":" + AnsiString( ms ) +
  1657.                 #endif
  1658.                          " Target< TX: ARFCN = " + IntToStr( s_ARFCN ) +
  1659.                          ", TSC = " + IntToStr( m_cTSC ) +
  1660.                          ", power = " + as_sub_tx_pcl +
  1661.                          ", frames = " + IntToStr(i_N_TX) +
  1662.                          ", dac value = " + IntToStr( m_sDefault_value )
  1663.                     );
  1664.                     #ifdef _MYDEBUG_
  1665.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  1666.                     log->Add( " before suspend " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1667.                           ":" + AnsiString( ms )
  1668.                         );
  1669.                     #endif
  1670.                     log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1671.                           " TimerCal->Enabled 7= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1672.                         );
  1673.                     RestartTimerCal( WM_MF_RF_TX_LEVEL_FAIL );
  1674.                     is_suspend_cal = true;
  1675.                     this->Suspend();
  1676.                     is_suspend_cal = false;
  1677.                     #ifdef _MYDEBUG_
  1678.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  1679.                     log->Add( " after resume " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1680.                           ":" + AnsiString( ms )
  1681.                          );
  1682.                     #endif
  1683.                     if ( this->Terminated )
  1684.                     {
  1685.                         this->OnTerminate = neByUser;
  1686.                         return false;
  1687.                     }
  1688.                     if (! MF_rf_tx_level_cnf )
  1689.                     {
  1690.                         CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1691.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1692.                               " FAIL : ! MF_rf_tx_level_cnf"
  1693.                             );
  1694.                         return false;
  1695.                     }
  1696.                     #ifdef _MYDEBUG_
  1697.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  1698.                     log->Add( " before read TX power Q " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1699.                           ":" + AnsiString( ms )
  1700.                         );
  1701.                     #endif
  1702.                     RestartTimerCal( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  1703.                     if( ! m_rct_ctrl.RCT_ReadTxPower( m_pRct, m_virTX ) )
  1704.                     {
  1705.                         CalErrorHandler( WM_MF_AGE8960_READ_TX_POWER_FAIL );
  1706.                         if ( frmFatcory->rbAgilent8960->Checked )
  1707.                         {
  1708.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1709.                                   " FAIL : Agilent 8960< read TX power 1."
  1710.                                 );
  1711.                         }
  1712.                         else
  1713.                         {
  1714.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1715.                                   " FAIL : CMU 200< read TX power 1."
  1716.                                 );
  1717.                         }
  1718.                         return false;
  1719.                     }
  1720.                     frmFatcory->DisableAllCalTimer();
  1721.                     m_dPCL_dBm[band_index][N] = m_virTX ;
  1722.                     m_dPCL_power[band_index][N] =  pow(10.0, m_dPCL_dBm[band_index][N]/10.0)/1000.0;
  1723.                     #ifdef _MYDEBUG_
  1724.                     TDateTime::CurrentTime().DecodeTime(&hr, &min, &sec, &ms );
  1725.                     log->Add( " after read TX power Q " + DateToStr(Date()) +  " " + CurrentTimeStr() +
  1726.                           ":" + AnsiString( ms )
  1727.                         );
  1728.                     #endif
  1729.                     if( frmFatcory->rbAgilent8960->Checked )
  1730.                     {
  1731.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1732.                               " Agilent 8960< read TX power = "+ Double_To_AnsiString( m_virTX )
  1733.                             );
  1734.                     }
  1735.                     else
  1736.                     {
  1737.                         log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1738.                               " CMU 200< read TX power = "+ Double_To_AnsiString( m_virTX )
  1739.                             );
  1740.                     }
  1741.                     if( this->Terminated )
  1742.                     {
  1743.                         this->OnTerminate = neByUser;
  1744.                         return false;
  1745.                     }
  1746.                     signed short  ss1;
  1747.                     double d1;
  1748.                     if (iRealRecursiveTimes != i_TXlevelRecusiveTimes-1) //iteration
  1749.                     {
  1750.                         if ( m_virTX > (atof(as_sub_wanted_p.c_str())+ m_dAPC_Delta) ||
  1751.                              m_virTX < (atof(as_sub_wanted_p.c_str())- m_dAPC_Delta))
  1752.                         {
  1753.                             d1 = atof(as_sub_c.c_str()) * (atof(as_sub_wanted_p.c_str()) - m_virTX );
  1754.                             // modified by Andy Ueng for always increment 1
  1755.                             //  ss1 = (signed short) (d1>0) ? floor(d1+0.5): ceil(d1-0.5);  // to round up or down
  1756.                             ss1 = (signed short) (d1>0) ? floor(d1+1.0): ceil(d1-1.0);
  1757.                             if ((MF_rf_apc_ptr->Get_ApcProfilePower(band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()) + ss1) <= 0 )
  1758.                             {
  1759.                                 MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt(), 1 );
  1760.                             }
  1761.                             else if ((MF_rf_apc_ptr->Get_ApcProfilePower(band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt() ) + ss1) > MAX_APC_DAC)
  1762.                             {
  1763.                                 MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt(), MAX_APC_DAC);
  1764.                             }
  1765.                             else
  1766.                             {
  1767.                                 MF_rf_apc_ptr->Set_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt(), MF_rf_apc_ptr->Get_ApcProfilePower(band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()) + ss1 );
  1768.                             }
  1769.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1770.                                   " band_index = " + IntToStr(band_index) + "," +
  1771.                                   " power_index = " + IntToStr(FIRST_PCL-as_sub_tx_cal_pcl.ToInt()) + "," +
  1772.                                   " MF_rf_apc_ptr->ApcProfile[band_index].rampData.power[power_index] = " + IntToStr(MF_rf_apc_ptr->Get_ApcProfilePower(band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt()))
  1773.                                 );
  1774.                             m_usPCLDacValue[band_index][N] = MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt() );
  1775.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1776.                                   " band_index = " + IntToStr(band_index) + "," +
  1777.                                   " N = " + IntToStr(N) + "," +
  1778.                                   " m_usPCLDacValue[band_index][N] = " + IntToStr(m_usPCLDacValue[band_index][N])
  1779.                                 );
  1780.                             if( ! MF_rf_apc_ptr->Get_RealApcDcOffset( m_pCal->b_TADOSupport, band_index, as_sub_tx_cal_pcl.ToInt(), m_iApcDcOffset ) )
  1781.                             {
  1782.                                 CalErrorHandler( WM_MF_RF_TX_LEVEL_FAIL );
  1783.                                 log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1784.                                           " FAIL: Get APC DC offset " +
  1785.                                           " TADO support = " + IntToStr( m_pCal->b_TADOSupport ) +
  1786.                                           " eFreqBand = " + IntToStr( eFreqBand ) +
  1787.                                           " pcl = " + as_sub_tx_cal_pcl
  1788.                                         );
  1789.                                 return false;
  1790.                             }
  1791.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1792.                                   " MF_rf_apc_ptr->ApcDcOffset[band_index] = " + IntToStr(m_iApcDcOffset)
  1793.                                 );
  1794.                             m_dV_set[band_index][N] = (m_iApcDcOffset + m_usPCLDacValue[band_index][N]) / 1024.0 * 2.8 * 15.0 / 25.0;
  1795.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1796.                                  " band_index = " + IntToStr(band_index) + "," +
  1797.                                  " N = " + IntToStr(N) + "," +
  1798.                                  " m_dV_set[band_index][N] = " + Double_To_AnsiString(m_dV_set[band_index][N])
  1799.                                );
  1800.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1801.                                   " new S = "+
  1802.                                   IntToStr( MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt() )  )
  1803.                                 );
  1804.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1805.                                   " delta s = "+
  1806.                                   AnsiString( ss1 )
  1807.                                 );
  1808.                             MF_rf_apc_ptr->ConfirmCallback = ::ccb_set_apc_level;
  1809.                             MF_rf_apc_ptr->Req_Rf_SetRampApcLevel(eFreqBand, as_sub_tx_cal_pcl.ToInt(), MF_rf_apc_ptr->Get_ApcProfilePower( band_index, FIRST_PCL-as_sub_tx_cal_pcl.ToInt() ) );
  1810.                             if ( this->Terminated )
  1811.                             {
  1812.                                 this->OnTerminate = neByUser;
  1813.                                 return false;
  1814.                             }
  1815.                             RestartTimerCal( WM_MF_SET_APC_DAC_FAIL );
  1816.                             log->Add( DateToStr(Date()) +  " " + CurrentTimeStr() +
  1817.                                   " TimerCal->Enabled 9= " + IntToStr((int)frmFatcory->TimerCal->Enabled)
  1818.                                 );
  1819.                             is_suspend_cal = true;
  1820.                             this->Suspend();
  1821.                             is_suspend_cal = false;
  1822.                             E_METAAPP_RESULT_T state = MF_rf_apc_ptr->Get_ConfirmState();
  1823.                             if (state != METAAPP_SUCCESS)
  1824.                             {
  1825.                                 if (METAAPP_NVRAM_LID_VER_NOT_SUPPORT == state)
  1826.                                 {