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

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) 2001
  8. *
  9. *****************************************************************************/
  10. /*****************************************************************************
  11.  *
  12.  * Filename:
  13.  * ---------
  14.  *   wifi_ee.cpp
  15.  *
  16.  * Project:
  17.  * --------
  18.  *   Maui META APP
  19.  *
  20.  * Description:
  21.  * ------------
  22.  *   WiFi EEPROM source
  23.  *
  24.  * Author:
  25.  * -------
  26.  *  Andy Ueng (mtk00490)
  27.  *
  28.  *============================================================================
  29.  *             HISTORY
  30.  * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  31.  *------------------------------------------------------------------------------
  32.  * $Revision$
  33.  * $Modtime$
  34.  * $Log$
  35.  *
  36.  *------------------------------------------------------------------------------
  37.  * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
  38.  *============================================================================
  39.  ****************************************************************************/
  40. //===========================================================================
  41. #include <IniFiles.hpp>
  42. #pragma hdrstop
  43. #ifndef  _WIFI_EE_H_
  44. #include "wifi_ee.h"
  45. #endif
  46. // man
  47. #ifndef  _MAN_ACTIVE_H_
  48. #include "man_active.h"
  49. #endif
  50. // misc
  51. #ifndef  _FT_UTILS_H_
  52. #include "ft_utils.h"
  53. #endif
  54. #ifndef  _MISC_WIFI_H_
  55. #include "misc_wifi.h"
  56. #endif
  57. static CWIFIEE*  gs_wifi_ee_ptr;
  58. static bool g_bIsRunning = false;
  59. //===========================================================================
  60. static  void  Read_From_EE( void )
  61. {   gs_wifi_ee_ptr->Read_From_EE();
  62. }
  63. //---------------------------------------------------------------------------
  64. static  void  Write_To_EE( void )
  65. {   gs_wifi_ee_ptr->Write_To_EE();
  66. }
  67. //===========================================================================
  68. CWIFIEE::CWIFIEE( void )
  69. {
  70.     g_bIsRunning = false;
  71.     ConfirmCallback = NULL;
  72. }
  73. //---------------------------------------------------------------------------
  74. CWIFIEE::~CWIFIEE( )
  75. {
  76.     g_bIsRunning = false;
  77.     ConfirmCallback = NULL;
  78. }
  79. //---------------------------------------------------------------------------
  80. void CWIFIEE::REQ_Stop(void)
  81. {
  82.     if (!g_bIsRunning)
  83.     {
  84.         return;
  85.     }
  86.     
  87.     Confirm(METAAPP_STOP);
  88. }
  89. //---------------------------------------------------------------------------
  90. void CWIFIEE::Confirm(E_METAAPP_RESULT_T confirm_state)
  91. {
  92.     if (!g_bIsRunning)
  93.     {
  94.         return;
  95.     }
  96.     g_bIsRunning = false;
  97.     if (NULL == ConfirmCallback)
  98.     {
  99.         return;
  100.     }
  101.     if (confirm_state != METAAPP_SUCCESS)
  102.     {
  103.         META_CancelAllBlockingCall_r(m_META_HANDLE_Obj.Get_MainHandle());
  104.     }
  105.     m_eConfirmState = confirm_state;
  106.     ActiveMan->SetActiveFunction(ConfirmCallback);
  107. }
  108. //===========================================================================
  109. void  CWIFIEE::REQ_Read_From_EE_Start(S_WIFI_EEPROM eeprom)
  110. {
  111.     gs_wifi_ee_ptr = this;
  112.     g_bIsRunning   = true;
  113.     m_sEE          = eeprom;
  114.     ActiveMan->SetActiveFunction(::Read_From_EE);
  115. }
  116. //------------------------------------------------------------
  117. void CWIFIEE::Read_From_EE(void)
  118. {
  119.     if (!g_bIsRunning)
  120.     {   Confirm( METAAPP_FAIL );  return;  }
  121.     META_RESULT  MetaResult = META_WiFi_EEPROM_Read_r( m_META_HANDLE_Obj.Get_MainHandle(), 100*m_sEE.count, m_sEE.eeprom, m_sEE.count );
  122.     if(MetaResult!=META_SUCCESS)
  123.     {
  124.         if( MetaResult!=META_TIMEOUT )
  125.         {   Confirm( METAAPP_FAIL );  return;  }
  126.         else
  127.         {   Confirm( METAAPP_TIMEOUT );  return;   }
  128.     }
  129.     Confirm(METAAPP_SUCCESS);
  130. }
  131. //===========================================================================
  132. void  CWIFIEE::REQ_Write_To_EE_Start(S_WIFI_EEPROM eeprom)
  133. {
  134.     gs_wifi_ee_ptr = this;
  135.     g_bIsRunning   = true;
  136.     m_sEE          = eeprom;
  137.     ActiveMan->SetActiveFunction( ::Write_To_EE );
  138. }
  139. //------------------------------------------------------------
  140. void  CWIFIEE::Write_To_EE( void )
  141. {
  142.     if( ! g_bIsRunning )
  143.     {   Confirm( METAAPP_FAIL );  return;  }
  144.     META_RESULT  MetaResult = META_WiFi_EEPROM_Write_r( m_META_HANDLE_Obj.Get_MainHandle(), 100*m_sEE.count, m_sEE.eeprom, m_sEE.count );
  145.     if(MetaResult!=META_SUCCESS)
  146.     {
  147.         if( MetaResult!=META_TIMEOUT )
  148.         {   Confirm( METAAPP_FAIL );  return;  }
  149.         else
  150.         {   Confirm( METAAPP_TIMEOUT );  return;   }
  151.     }
  152.     Confirm(METAAPP_SUCCESS);
  153. }
  154. //===========================================================================
  155. static AnsiString SECTION_NAME_GENERAL = "WiFi General Information";
  156. static AnsiString KEY_NAME_GENERAL[] =
  157. {
  158.     "WiFi BB chip ID",
  159.     "WiFi RF chip ID",
  160.     "WiFi serial number",
  161.     "WiFi MAC address",
  162.     "WiFi checksum"
  163. };
  164. typedef enum
  165. {
  166.     WIFI_BB_CHIP_ID_IDX=0,
  167.     WIFI_RF_CHIP_ID_IDX,
  168.     WIFI_SN_IDX,
  169.     WIFI_MAC_ADDR_IDX,
  170.     WIFI_CHECKSUM_IDX
  171. }E_WIFI_EE_GENERAL;
  172. //---------------------------------------------------------------------------
  173. bool  CWIFIEE::REQ_Read_General_Info_From_File( char *filename )
  174. {
  175.     TIniFile   *ini_file;
  176.     ini_file = new TIniFile( filename );
  177.     if( ini_file == NULL )  return false;
  178.     m_sEEGeneral.usBBChipID = ini_file->ReadInteger( SECTION_NAME_GENERAL,
  179.                                      KEY_NAME_GENERAL[WIFI_BB_CHIP_ID_IDX],
  180.                                      0);
  181.     m_sEEGeneral.usRFChipID = ini_file->ReadInteger( SECTION_NAME_GENERAL,
  182.                                      KEY_NAME_GENERAL[WIFI_RF_CHIP_ID_IDX],
  183.                                      0);
  184.     m_sEEGeneral.uiSN = ini_file->ReadInteger( SECTION_NAME_GENERAL,
  185.                                      KEY_NAME_GENERAL[WIFI_SN_IDX],
  186.                                      0);
  187.     AnsiString as_mac_addr;
  188.     as_mac_addr = ini_file->ReadString( SECTION_NAME_GENERAL,
  189.                                      KEY_NAME_GENERAL[WIFI_MAC_ADDR_IDX],
  190.                                      0);
  191.     if( ! Str_To_MACAddress( as_mac_addr, m_sEEGeneral.sMACAddr ) )
  192.     {
  193.         delete  ini_file;
  194.         return false;
  195.     }
  196.     delete  ini_file;
  197.     return true;
  198. }
  199. //-----------------------------------------------------------------------------
  200. bool  CWIFIEE::REQ_Write_General_Info_To_File( char *filename )
  201. {
  202.     TIniFile   *ini_file;
  203.     ini_file = new TIniFile( filename );
  204.     if( NULL == ini_file )  return  false;
  205.     ini_file->WriteInteger( SECTION_NAME_GENERAL,
  206.                             KEY_NAME_GENERAL[WIFI_BB_CHIP_ID_IDX],
  207.                             m_sEEGeneral.usBBChipID  );
  208.     ini_file->WriteInteger( SECTION_NAME_GENERAL,
  209.                             KEY_NAME_GENERAL[WIFI_RF_CHIP_ID_IDX],
  210.                             m_sEEGeneral.usRFChipID  );
  211.     ini_file->WriteInteger( SECTION_NAME_GENERAL,
  212.                             KEY_NAME_GENERAL[WIFI_SN_IDX],
  213.                             m_sEEGeneral.uiSN  );
  214.     AnsiString as_mac_addr;
  215.     if( ! MACAddress_To_Str( m_sEEGeneral.sMACAddr , as_mac_addr )  )
  216.     {
  217.         delete  ini_file;
  218.         return false;
  219.     }
  220.     ini_file->WriteString( SECTION_NAME_GENERAL,
  221.                             KEY_NAME_GENERAL[WIFI_MAC_ADDR_IDX],
  222.                             as_mac_addr  );
  223.     ini_file->WriteInteger( SECTION_NAME_GENERAL,
  224.                             KEY_NAME_GENERAL[WIFI_CHECKSUM_IDX],
  225.                             m_sEEGeneral.ucChecksum  );
  226.     delete  ini_file;
  227.     return  true;
  228. }
  229. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  230. static AnsiString SECTION_NAME_TXDC = "WiFi TX DC Offset";
  231. static AnsiString KEY_NAME_TXDC[] =
  232. {
  233.     "I_DC",
  234.     "Q_DC"
  235. };
  236. //---------------------------------------------------------------------------
  237. bool  CWIFIEE::REQ_Read_TX_DC_Offset_From_File( char *filename )
  238. {
  239.     TIniFile   *ini_file;
  240.     ini_file = new TIniFile( filename );
  241.     if( ini_file == NULL )  return false;
  242.     m_sEEGeneral.sTxDcOffset.i_ch_offset = ini_file->ReadInteger( SECTION_NAME_TXDC,
  243.                                      KEY_NAME_TXDC[WIFI_IDX_IDC],
  244.                                      0);
  245.     m_sEEGeneral.sTxDcOffset.q_ch_offset = ini_file->ReadInteger( SECTION_NAME_TXDC,
  246.                                      KEY_NAME_TXDC[WIFI_IDX_QDC],
  247.                                      0);
  248.     delete  ini_file;
  249.     return true;
  250. }
  251. //-----------------------------------------------------------------------------
  252. bool  CWIFIEE::REQ_Write_TX_DC_Offset_To_File( char *filename )
  253. {
  254.     TIniFile   *ini_file;
  255.     ini_file = new TIniFile( filename );
  256.     if( NULL == ini_file )  return  false;
  257.     ini_file->WriteInteger( SECTION_NAME_TXDC,
  258.                             KEY_NAME_TXDC[WIFI_IDX_IDC],
  259.                             m_sEEGeneral.sTxDcOffset.i_ch_offset  );
  260.     ini_file->WriteInteger( SECTION_NAME_TXDC,
  261.                             KEY_NAME_TXDC[WIFI_IDX_QDC],
  262.                             m_sEEGeneral.sTxDcOffset.q_ch_offset  );
  263.     delete  ini_file;
  264.     return  true;
  265. }
  266. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  267. static const AnsiString SECTION_NAME_2400M = "WiFi TX power 2.4G";
  268. static const AnsiString SECTION_NAME_5000M = "WiFi TX power 5G";
  269. static const AnsiString KEY_NAME_2400M[] =
  270. {
  271.     "TX power CCK",
  272.     "TX power OFDM"
  273. };
  274. static const AnsiString KEY_NAME_5000M = "TX power";
  275. //----------------------------------------------------------------------------
  276. bool  CWIFIEE::REQ_Read_TxPower2400M_From_File( char *filename )
  277. {
  278.     TIniFile   *ini_file;
  279.     AnsiString  as_data;
  280.     char        str[2048];
  281.     unsigned char      ucdata[NUM_TX_POWER_2400M_CH];
  282.     int         i, j;
  283.     ini_file = new TIniFile( filename );
  284.     if( ini_file == NULL )  return false;
  285.     for( i=0; i<2; i++ )
  286.     {
  287.         as_data = ini_file->ReadString( SECTION_NAME_2400M,
  288.                                         KEY_NAME_2400M[i],
  289.                                         "0,0,0,0,0,0,0,0,0,0,0,0,0,0");
  290.         strcpy( str, as_data.c_str() );
  291.         String_To_Array_UnsignedChar( str, ucdata, NUM_TX_POWER_2400M_CH );
  292.         if( WIFI_MOD_CCK == i )
  293.         {
  294.             for( j=0; j<NUM_TX_POWER_2400M_CH; j++ )
  295.             {
  296.                 m_sTxChannelDac2400M.CCKTxPWR[j] = ucdata[j];
  297.             }
  298.         }
  299.         else
  300.         {
  301.             for( j=0; j<NUM_TX_POWER_2400M_CH; j++ )
  302.             {
  303.                 m_sTxChannelDac2400M.OFDMTxPWR[j] = ucdata[j];
  304.             }
  305.         }
  306.     }
  307.     delete  ini_file;
  308.     return true;
  309. }
  310. //-----------------------------------------------------------------------------
  311. bool  CWIFIEE::REQ_Write_TxPower2400M_To_File( char *filename )
  312. {
  313.     TIniFile   *ini_file;
  314.     char        str[2048];
  315.     unsigned char         ucdata[NUM_TX_POWER_2400M_CH];
  316.     int         i, j;
  317.     ini_file = new TIniFile( filename );
  318.     if( ini_file == NULL )  return  false;
  319.     for( i=0; i<2; i++ )
  320.     {
  321.         if( WIFI_MOD_CCK == i )
  322.         {
  323.             for( j=0; j<NUM_TX_POWER_2400M_CH; j++ )
  324.             {  ucdata[i] = m_sTxChannelDac2400M.CCKTxPWR[i];  }
  325.         }
  326.         else
  327.         {
  328.             for( j=0; j<NUM_TX_POWER_2400M_CH; j++ )
  329.             {  ucdata[i] = m_sTxChannelDac2400M.OFDMTxPWR[i];  }
  330.         }
  331.         Array_To_String_UnsignedChar( str, ucdata, NUM_TX_POWER_2400M_CH, ',' );
  332.         ini_file->WriteString( SECTION_NAME_2400M,
  333.                                KEY_NAME_2400M[i],
  334.                                str );
  335.     }
  336.     delete  ini_file;
  337.     return  true;
  338. }
  339. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  340. bool  CWIFIEE::REQ_Read_TxPower5000M_From_File( char *filename )
  341. {
  342.     TIniFile   *ini_file;
  343.     AnsiString  as_data;
  344.     char        str[2048];
  345.     unsigned char      ucdata[NUM_TX_POWER_5000M_CH];
  346.     int         i;
  347.     ini_file = new TIniFile( filename );
  348.     if( ini_file == NULL )  return false;
  349.     as_data = ini_file->ReadString( SECTION_NAME_5000M,
  350.                                      KEY_NAME_5000M,
  351.                                      "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
  352.     strcpy( str, as_data.c_str() );
  353.     String_To_Array_UnsignedChar( str, ucdata, NUM_TX_POWER_5000M_CH );
  354.     for( i=0; i<NUM_TX_POWER_5000M_CH; i++ )
  355.     {  m_sTxChannelDac5000M.TxPWR[i] = ucdata[i];
  356.     }
  357.     delete  ini_file;
  358.     return true;
  359. }
  360. //-----------------------------------------------------------------------------
  361. bool  CWIFIEE::REQ_Write_TxPower5000M_To_File( char *filename )
  362. {
  363.     TIniFile   *ini_file;
  364.     char        str[2048];
  365.     unsigned char         ucdata[NUM_TX_POWER_5000M_CH];
  366.     int         i;
  367.     ini_file = new TIniFile( filename );
  368.     if( ini_file == NULL )  return  false;
  369.     for( i=0; i<NUM_TX_POWER_5000M_CH; i++ )
  370.     {  ucdata[i] = m_sTxChannelDac5000M.TxPWR[i];  }
  371.     Array_To_String_UnsignedChar( str, ucdata, NUM_TX_POWER_5000M_CH, ',' );
  372.     ini_file->WriteString( SECTION_NAME_5000M,
  373.                            KEY_NAME_5000M,
  374.                            str );
  375.     delete  ini_file;
  376.     return  true;
  377. }
  378. //---------------------------------------------------------------------------
  379. unsigned char CWIFIEE::Calculate_Checksum( S_WIFI_EE_GENERAL &ee_gnrl )
  380. {
  381.    // m_sEEGeneral = ee_gnrl;
  382.     unsigned char xor_sum=0;
  383.     unsigned char checksum;
  384.     // BB chip ID
  385.     xor_sum ^= (ee_gnrl.usBBChipID & 0x00FF);
  386.     xor_sum ^= ((ee_gnrl.usBBChipID & 0xFF00)>>8);
  387.     // RF chip ID
  388.     xor_sum ^= (ee_gnrl.usRFChipID & 0x00FF);
  389.     xor_sum ^= ((ee_gnrl.usRFChipID & 0xFF00)>>8);
  390.     // SN
  391.     xor_sum ^= (ee_gnrl.usRFChipID & 0x000000FF);
  392.     xor_sum ^= ((ee_gnrl.usRFChipID & 0x0000FF00)>>8);
  393.     xor_sum ^= ((ee_gnrl.usRFChipID & 0x00FF0000)>>16);
  394.     xor_sum ^= ((ee_gnrl.usRFChipID & 0xFF000000)>>24);
  395.     // MAC address
  396.     for( int i=0; i< sizeof(ee_gnrl.sMACAddr.mac_addr)/sizeof(ee_gnrl.sMACAddr.mac_addr[0]); i=i+2 )
  397.     {
  398.         xor_sum ^= ee_gnrl.sMACAddr.mac_addr[i];
  399.     }
  400.     // reserved 0
  401.     xor_sum ^= (ee_gnrl.usReserved0 & 0x00FF);
  402.     xor_sum ^= ((ee_gnrl.usReserved0 & 0xFF00)>>8);
  403.     // TX DC offset
  404.     xor_sum ^= (ee_gnrl.sTxDcOffset.i_ch_offset);
  405.     xor_sum ^= (ee_gnrl.sTxDcOffset.q_ch_offset);
  406.     // reserved 1
  407.     xor_sum ^= (ee_gnrl.usReserved1 & 0x00FF);
  408.     xor_sum ^= ((ee_gnrl.usReserved1 & 0xFF00)>>8);
  409.     // reserved 2
  410.     xor_sum ^= (ee_gnrl.ucReserved2);
  411.     checksum = 0xFF ^ xor_sum;
  412.     return checksum;
  413. }
  414. //===========================================================================
  415. ////////////////////////////   Global  information   ////////////////////////
  416. //===========================================================================
  417. E_METAAPP_RESULT_T CWIFIEE::Get_ConfirmState(void)
  418. {
  419.     return  m_eConfirmState;
  420. }
  421. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  422. S_WIFI_EEPROM   CWIFIEE::Get_EE( void )
  423. {
  424.     return  m_sEE;
  425. }
  426. //---------------------------------------------------------------------------
  427. void   CWIFIEE::Set_EE( S_WIFI_EEPROM &eeprom )
  428. {
  429.     m_sEE = eeprom;
  430. }
  431. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  432. S_WIFI_EE_GENERAL CWIFIEE::Get_EEGeneral( void )
  433. {   return  m_sEEGeneral;
  434. }
  435. //---------------------------------------------------------------------------
  436. void CWIFIEE::Set_EEGeneral( S_WIFI_EE_GENERAL &ee_general )
  437. {   m_sEEGeneral = ee_general;
  438. }
  439. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  440. WiFi_DAC_DC_Offset_S CWIFIEE::Get_TxDcOffset( void )
  441. {   return  m_sEEGeneral.sTxDcOffset;
  442. }
  443. //--------------------------------------------------------------------------
  444. void CWIFIEE::Set_TxDcOffset( WiFi_DAC_DC_Offset_S tx_dc_offset )
  445. {   m_sEEGeneral.sTxDcOffset = tx_dc_offset;
  446. }
  447. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  448. unsigned char CWIFIEE::Get_Checksum( void )
  449. {
  450.     return m_sEEGeneral.ucChecksum;
  451. }
  452. //--------------------------------------------------------------------------
  453. void CWIFIEE::Set_Checksum( unsigned char checksum )
  454. {
  455.     m_sEEGeneral.ucChecksum = checksum;
  456. }
  457. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  458. WiFi_TxPower_2400M_S CWIFIEE::Get_TxChannelDac2400M( void )
  459. {
  460.     return m_sTxChannelDac2400M;
  461. }
  462. //---------------------------------------------------------------------------
  463. void CWIFIEE::Set_TxChannelDac2400M( WiFi_TxPower_2400M_S &txpwr )
  464. {
  465.     m_sTxChannelDac2400M = txpwr;
  466. }
  467. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  468. WiFi_TxPower_5000M_S CWIFIEE::Get_TxChannelDac5000M( void )
  469. {
  470.     return m_sTxChannelDac5000M;
  471. }
  472. //---------------------------------------------------------------------------
  473. void CWIFIEE::Set_TxChannelDac5000M( WiFi_TxPower_5000M_S &txpwr )
  474. {
  475.     m_sTxChannelDac5000M = txpwr;
  476. }