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

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.  *   bt_hci.cpp
  15.  *
  16.  * Project:
  17.  * --------
  18.  *   Maui META APP
  19.  *
  20.  * Description:
  21.  * ------------
  22.  *   BT HCI command test 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<iostream>
  42. #include <fstream>
  43. #pragma hdrstop
  44. #ifndef  _BT_COMMON_H_
  45. #include "bt_common.h"
  46. #endif
  47. #ifndef  _BT_HCI_COMMON_H_
  48. #include "bt_hci_common.h"
  49. #endif
  50. #ifndef  _BT_HCI_H_
  51. #include "bt_hci.h"
  52. #endif
  53. // man
  54. #ifndef  _MAN_ACTIVE_H_
  55. #include "man_active.h"
  56. #endif
  57. // misc
  58. #ifndef  _FILE_UTILS_H_
  59. #include "file_utils.h"
  60. #endif
  61. #ifndef  _FT_UTILS_H_
  62. #include "ft_utils.h"
  63. #endif
  64. #ifndef  _MISC_BT_H_
  65. #include "misc_bt.h"
  66. #endif
  67. // message
  68. #ifndef _META_LAB_MSG_H_
  69. #include "meta_lab_msg.h"
  70. #endif
  71. #define INVALID_TAG  -1
  72. const AnsiString KEY_WORD[]=
  73. {
  74.     "[GROUP]",
  75.     "[COMMAND]",
  76.     "[DEFINE]",
  77.     "[CATEGORY]"
  78. };
  79. //----------------------------------------------------------------------------
  80. static CBTHCI*  gs_bt_hci_ptr;
  81. static bool g_bIsRunning = false;
  82. //==============================================================================
  83. static void REQ_Read_From_File(void)
  84. {
  85.     gs_bt_hci_ptr->REQ_Read_From_File();
  86. }
  87. //----------------------------------------------------------------------------
  88. static void  REQ_Send_HCI_Command(void)
  89. {
  90.     gs_bt_hci_ptr->REQ_Send_HCI_Command();
  91. }
  92. //------------------------------------------------------------------------------
  93. static void __stdcall CNF_HCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  94. {
  95.     gs_bt_hci_ptr->CNF_HCIEvent( cnf, token, usrData );
  96. }
  97. //===========================================================================
  98. CBTHCI::CBTHCI(void)
  99. {
  100.     g_bIsRunning = false;
  101.     ConfirmCallback = 0;
  102.     m_fp = NULL;
  103.     AllocateVector();
  104. }
  105. //---------------------------------------------------------------------------
  106. CBTHCI::~CBTHCI(void)
  107. {
  108.     DeAllocateVector();
  109. }
  110. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. void CBTHCI::AllocateVector(void)
  112. {
  113.     if (NULL == m_pvHCIGroup)
  114.     {
  115.         m_pvHCIGroup = new vector<S_BT_HCI_GROUP>;
  116.     }
  117.     if (NULL == m_pvHCICategory)
  118.     {
  119.         m_pvHCICategory  = new vector<S_BT_HCI_CATEGORY>;
  120.     }
  121. }
  122. //---------------------------------------------------------------------------
  123. void CBTHCI::DeAllocateVector(void)
  124. {
  125.     if (NULL != m_pvHCIGroup)
  126.     {
  127.         if (!m_pvHCIGroup->empty())
  128.         {
  129.             m_pvHCIGroup->erase(m_pvHCIGroup->begin(), m_pvHCIGroup->end());
  130.         }
  131.         delete m_pvHCIGroup;
  132.         m_pvHCIGroup = NULL;
  133.     }
  134.     if (NULL != m_pvHCICategory)
  135.     {
  136.         if (!m_pvHCICategory->empty())
  137.         {
  138.             m_pvHCICategory->erase(m_pvHCICategory->begin(), m_pvHCICategory->end());
  139.         }
  140.         delete m_pvHCICategory;
  141.         m_pvHCICategory = NULL;
  142.     }
  143. }
  144. //---------------------------------------------------------------------------
  145. void CBTHCI::EraseVector(void)
  146. {
  147.     if (!m_pvHCIGroup->empty())
  148.     {
  149.         m_pvHCIGroup->erase(m_pvHCIGroup->begin(), m_pvHCIGroup->end());
  150.     }
  151.     if (!m_pvHCICategory->empty())
  152.     {
  153.         m_pvHCICategory->erase(m_pvHCICategory->begin(), m_pvHCICategory->end());
  154.     }
  155. }
  156. //---------------------------------------------------------------------------
  157. void CBTHCI::REQ_Stop(void)
  158. {
  159.     if (!g_bIsRunning)
  160.     {
  161.         return;
  162.     }
  163.     Confirm(METAAPP_STOP);
  164. }
  165. //---------------------------------------------------------------------------
  166. void CBTHCI::Confirm(E_METAAPP_RESULT_T confirm_state)
  167. {
  168.     if (!g_bIsRunning)
  169.     {
  170.         return;
  171.     }
  172.     g_bIsRunning = false;
  173.  //  CloseFile();
  174.     if (NULL == ConfirmCallback)
  175.     {
  176.         return;
  177.     }
  178.     m_eConfirmState = confirm_state;
  179.     ActiveMan->SetActiveFunction(ConfirmCallback);
  180. }
  181. //============================================================================
  182. void CBTHCI::REQ_Start( S_BT_HCI  &bt_hci )
  183. {
  184.     gs_bt_hci_ptr  = this;
  185.     g_bIsRunning    = true;
  186.     m_sBTHCI = bt_hci;
  187.     ActiveMan->SetActiveFunction( ::REQ_Send_HCI_Command );
  188. }
  189. //---------------------------------------------------------------------------
  190. void CBTHCI::REQ_Send_HCI_Command(void)
  191. {
  192.     if (!g_bIsRunning)
  193.     {
  194.         return;
  195.     }
  196.     AddTxLogToStringList(m_sBTHCI.s_hci_cmd);
  197.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), m_sBTHCI.ui_ms_timeout, &m_sBTHCI.s_hci_cmd, ::CNF_HCIEvent, NULL, m_sBTHCI.uc_last_event );
  198.     if (META_SUCCESS != MetaResult)
  199.     {
  200.         if (META_TIMEOUT == MetaResult)
  201.         {
  202.             Confirm(METAAPP_TIMEOUT);
  203.             return;
  204.         }
  205.         else if (META_CANCEL == MetaResult)
  206.         {
  207.             Confirm(METAAPP_CANCEL);
  208.             return;
  209.         }
  210.         else
  211.         {
  212.             Confirm(METAAPP_FAIL);
  213.             return;
  214.         }
  215.     }
  216.     Confirm(METAAPP_SUCCESS);
  217. }
  218. //---------------------------------------------------------------------------
  219. void __stdcall CBTHCI::CNF_HCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  220. {
  221.    // if(!g_bIsRunning)  return;
  222.     
  223.     m_sHCIEvent = *cnf;
  224.     AddRxLogToStringList(*cnf);
  225. }
  226. //===========================================================================
  227. void CBTHCI::REQ_Read_From_File_Start( S_BT_HCI  &bt_hci )
  228. {
  229.     gs_bt_hci_ptr  = this;
  230.     g_bIsRunning    = true;
  231.     m_sBTHCI = bt_hci;
  232.     //m_asFileName = filename;
  233.     EraseVector();
  234.     ActiveMan->SetActiveFunction( ::REQ_Read_From_File );
  235. }
  236. //----------------------------------------------------------------------------
  237. bool CBTHCI::REQ_Read_Last_HCI_File_Start( S_BT_HCI  &bt_hci )
  238. {
  239.      m_sBTHCI = bt_hci;
  240.      EraseVector();
  241.     if( ! OpenFile() )
  242.     {
  243.         return false;
  244.     }
  245.     if( ! ParseFile() )
  246.     {
  247.         return false;
  248.     }
  249.     CloseFile();
  250.     
  251.     return true;
  252. }
  253. //----------------------------------------------------------------------------
  254. void  CBTHCI::REQ_Read_From_File( void )
  255. {
  256.     if( ! g_bIsRunning )  return;
  257.     
  258.     if( ! OpenFile() )
  259.     {
  260.         CloseFile();
  261.         Confirm( METAAPP_FAIL );
  262.         return;
  263.     }
  264.     if( ! ParseFile() )
  265.     {
  266.         CloseFile();
  267.         Confirm(METAAPP_FAIL);
  268.         return;
  269.     }
  270.     CloseFile();
  271.     Confirm(METAAPP_SUCCESS);
  272. }
  273. //----------------------------------------------------------------------------
  274. bool  CBTHCI::OpenFile( void )
  275. {
  276.    // if( m_ifs.is_open() )
  277.    // {
  278.    //     CloseFile();
  279.    // }
  280.     if( ! CheckFileExist(m_sBTHCI.filename) )
  281.     {
  282.         return false;
  283.     }
  284. #if 0
  285.     m_pifs = new ifstream;
  286.     m_pifs->open( m_sBTHCI.filename.c_str(), ios::in );
  287.     if( 0 == m_pifs->is_open() )
  288.         return false;
  289. #endif
  290.     m_fp = fopen(m_sBTHCI.filename.c_str(), "r");
  291.     if( NULL == m_fp )
  292.     return false;
  293.     //m_ifs.seekg( ios::beg );
  294.    // m_pifs->setmode( filebuf::text ); // set to text mode
  295.     //m_pFBuf = m_ifs.rdbuf();
  296.     return true;
  297. }
  298. //---------------------------------------------------------------------------
  299. void CBTHCI::CloseFile( void )
  300. {
  301.     #if 0
  302.     if( m_pifs->is_open() )
  303.     {   m_pifs->close();
  304.         delete m_pifs;
  305.     }
  306.     #endif
  307.     if( NULL != m_fp )
  308.     {
  309.         fclose(m_fp);
  310.         m_fp = NULL;
  311.     }
  312. }
  313. //---------------------------------------------------------------------------
  314. bool CBTHCI::ParseFile( void )
  315. {
  316.     const int str_size = 256;
  317.     char str[str_size];
  318.     m_eHCISec = BT_HCI_UNKNOW;
  319.    // while( ! m_pifs->eof() )
  320.     while( !feof(m_fp) )
  321.     {
  322.        // m_pifs->getline( str, str_size, 'n' );
  323.         fgets( str, str_size, m_fp );
  324.         if( ! ParseCommandLine( str ) )
  325.         {
  326.             AnsiString as_str = str;
  327.             AddParseLogToStringList(as_str + " error");
  328.             return false;
  329.         }    
  330.        // streampos pos = m_ifs.tellg();
  331.        // m_ifs.tellg();
  332.     }    
  333.     return true;
  334. }
  335. //---------------------------------------------------------------------------
  336. bool CBTHCI::ParseCommandLine( AnsiString as_cmd )
  337. {
  338.     as_cmd = as_cmd.UpperCase();
  339.     if( as_cmd.AnsiCompareIC("") == 0 )  return true;  // NULL
  340.     if( as_cmd.AnsiPos("//") != 0 )  return true; // note
  341.     for( int i=0; i<sizeof(KEY_WORD)/sizeof(KEY_WORD[0]); i++ )
  342.     {
  343.         if( as_cmd.AnsiPos( KEY_WORD[i] ) != 0 )
  344.         {
  345.             m_eHCISec = (E_BT_HCI_TAG) i;
  346.             return true;
  347.         }
  348.     }
  349.     switch( m_eHCISec )
  350.     {
  351.         case BT_HCI_GROUP_TAG:
  352.         {
  353.              if( ! ParseHCIGroup( as_cmd ) )
  354.                 return false;
  355.         }
  356.         break;
  357.         case BT_HCI_COMMAND_TAG:
  358.         {
  359.              if( ! ParseHCICommand( as_cmd ) )
  360.                 return false;
  361.         }
  362.         break;
  363.         case BT_HCI_DEFINE_TAG:
  364.         {
  365.              if( ! ParseHCICmdDefine( as_cmd ) )
  366.                 return false;
  367.         }
  368.         break;
  369.         case BT_HCI_CATEGORY_TAG:
  370.         {
  371.              if( ! ParseHCICategory( as_cmd ) )
  372.                 return false;
  373.         }
  374.         break;
  375.     }
  376.     return true;
  377. }
  378. //---------------------------------------------------------------------------
  379. bool CBTHCI::ParseHCIGroup( AnsiString as_cmd )
  380. {
  381.     AnsiString as_group_tag;
  382.     AnsiString as_group_name;
  383.     int iLD_r, iLD_l;
  384.     iLD_l = as_cmd.LastDelimiter("[");
  385.     iLD_r = as_cmd.LastDelimiter("]");
  386.     as_group_tag  = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1);
  387.     as_group_name = as_cmd.SubString( iLD_r+2, as_cmd.Length() );
  388.     S_BT_HCI_GROUP group;
  389.     group.as_group_name = as_group_name;
  390.     group.as_group_tag  = as_group_tag;
  391.     m_pvHCIGroup->push_back( group );
  392.     return true;
  393. }
  394. //---------------------------------------------------------------------------
  395. bool CBTHCI::ParseHCICategory( AnsiString as_cmd )
  396. {
  397.     AnsiString as_category_tag;
  398.     AnsiString as_category_name;
  399.     int iLD_r, iLD_l;
  400.     // category header
  401.     if( as_cmd.AnsiPos("[S") != 0 || as_cmd.AnsiPos("[s") != 0 )
  402.     {
  403.         m_bFirstCategoryBody = true;
  404.         iLD_l = as_cmd.LastDelimiter("[");
  405.         iLD_r = as_cmd.LastDelimiter("]");
  406.         as_category_tag  = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1);
  407.         as_category_name = as_cmd.SubString( iLD_r+2, as_cmd.Length() );
  408.         S_BT_HCI_CATEGORY category;
  409.         category.as_category_name = as_category_name;
  410.         category.as_category_tag  = as_category_tag;
  411.         m_pvHCICategory->push_back( category );
  412. //        m_iHCICategory =
  413.     }
  414.     else  // category body
  415.     {
  416.         AnsiString as_cmd_tag;
  417.         AnsiString as_cmd_name;
  418.         //int iLD_r, iLD_l;
  419.         iLD_l = as_cmd.LastDelimiter("[");
  420.         iLD_r = as_cmd.LastDelimiter("]");
  421.         as_cmd_tag   = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1);
  422.         as_cmd_name  = as_cmd.SubString( iLD_r+2, as_cmd.Length() );
  423.        // iLD_l = as_cmd_tag.LastDelimiter("C");
  424.       //  as_category_tag = as_cmd_tag.SubString( 1, iLD_l-1 );
  425.         vector<S_BT_HCI_CATEGORY>:: iterator iter_category;
  426.         vector<S_BT_HCI_GROUP>:: iterator iter_group;
  427.         vector<S_BT_HCI_COMMAND>:: iterator iter_cmd;
  428.         if( m_bFirstCategoryBody )
  429.         {
  430.             m_bFirstCategoryBody = false;
  431.             for( iter_category = m_pvHCICategory->begin(); iter_category != m_pvHCICategory->end(); iter_category++ )
  432.             {
  433.                 m_vLastCateogryIter = iter_category;
  434.             }
  435.         }
  436.         S_BT_HCI_COMMAND command;
  437.        // command.as_cmd_name = as_cmd_name;
  438.         command.as_cmd_tag  = as_cmd_tag;
  439.         bool found = false;
  440.         for( iter_group = m_pvHCIGroup->begin(); (false==found) && (iter_group != m_pvHCIGroup->end()); iter_group++ )
  441.         {
  442.             for( iter_cmd = iter_group->v_cmd.begin(); iter_cmd != iter_group->v_cmd.end(); iter_cmd++ )
  443.             {
  444.                 if( iter_cmd->as_cmd_tag.AnsiCompareIC(as_cmd_tag) == 0 )
  445.                 {
  446.                      command = *iter_cmd;
  447.                      found = true;
  448.                      break;
  449.                 }
  450.             }
  451.         }
  452.         m_vLastCateogryIter->v_cmd.push_back( command );
  453.     }
  454.     return true;
  455. }
  456. //---------------------------------------------------------------------------
  457. bool CBTHCI::ParseHCICommand( AnsiString as_cmd )
  458. {
  459.     AnsiString as_group_tag;
  460.     AnsiString as_cmd_tag;
  461.     AnsiString as_cmd_name;
  462.     int iLD_r, iLD_l;
  463.     iLD_l = as_cmd.LastDelimiter("[");
  464.     iLD_r = as_cmd.LastDelimiter("]");
  465.     as_cmd_tag   = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1);
  466.     as_cmd_name  = as_cmd.SubString( iLD_r+2, as_cmd.Length() );
  467.     iLD_l = as_cmd_tag.LastDelimiter("C");
  468.     as_group_tag = as_cmd_tag.SubString( 1, iLD_l-1 );
  469.     vector<S_BT_HCI_GROUP>:: iterator iter;
  470.     for( iter = m_pvHCIGroup->begin(); iter != m_pvHCIGroup->end(); iter++ )
  471.     {
  472.         S_BT_HCI_GROUP group;
  473.         group = *iter;
  474.         if( group.as_group_tag.AnsiCompareIC( as_group_tag ) == 0 )
  475.         {
  476.             S_BT_HCI_COMMAND command;
  477.             command.as_cmd_name = as_cmd_name;
  478.             command.as_cmd_tag  = as_cmd_tag;
  479.             iter->v_cmd.push_back( command );
  480.             break;
  481.         }
  482.     }
  483.     return true;
  484. }
  485. //---------------------------------------------------------------------------
  486. bool CBTHCI::ParseHCICmdDefine( AnsiString as_cmd )
  487. {
  488.    // static AnsiString as_last_cmd_tag=""; // last parsing command tag
  489.     static AnsiString as_cur_cmd;
  490.     static AnsiString as_cmd_tag=""; // currently parsing command tag
  491.     AnsiString as_group_tag;
  492.    // static unsigned int send_par_num=0;
  493.    // static unsigned int receive_par_num=0;
  494.    // static vector<S_BT_HCI_GROUP>:: iterator last_iter;
  495.     static vector<S_BT_HCI_GROUP>:: iterator iter_group;
  496.     static vector<S_BT_HCI_COMMAND>:: iterator iter_cmd;
  497.     int iLD_r, iLD_l, iLD_c;
  498.     iLD_l = as_cmd.AnsiPos("(G");
  499.     iLD_r = as_cmd.LastDelimiter(")");
  500.     if( (0 != iLD_l) && (0 != iLD_r) ) // command define header
  501.     {
  502.         as_cur_cmd = as_cmd;
  503.         //as_last_cmd_tag = as_cur_cmd_tag;
  504.         as_cmd_tag  = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1);
  505.         iLD_c           = as_cmd_tag.AnsiPos("C");
  506.         as_group_tag    = as_cmd_tag.SubString( 1, iLD_c-1 );
  507.         S_BT_HCI_GROUP group;
  508.         S_BT_HCI_COMMAND command;
  509.         for( iter_group = m_pvHCIGroup->begin(); iter_group != m_pvHCIGroup->end(); iter_group++ )
  510.         {
  511.             group = *iter_group;
  512.             if( group.as_group_tag.AnsiCompareIC( as_group_tag ) == 0 )
  513.             {
  514.                 for( iter_cmd = iter_group->v_cmd.begin(); iter_cmd != iter_group->v_cmd.end(); iter_cmd++ )
  515.                 {
  516.                     command = *iter_cmd;
  517.                     if( command.as_cmd_tag.AnsiCompareIC( as_cmd_tag ) == 0 )
  518.                     {
  519.                         iter_cmd->ui_send_num = 0;
  520.                         iter_cmd->ui_receive_num = 0;
  521.                         break;
  522.                     }
  523.                 }
  524.                // last_iter    = curr_iter;
  525.                // curr_iter    = iter;
  526.             }
  527.         }
  528.        // if( as_last_cmd_tag.AnsiCompareIC("") != 0 ) // not first command define header
  529.        // {
  530.        //    // curr_iter->ui_send_num    = send_par_num;
  531.        //    // curr_iter->ui_receive_num = receive_par_num;
  532.        // }
  533.        // vector<S_BT_HCI_COMMAND>:: iterator iter;
  534.        // S_BT_HCI_COMMAND command;
  535.       //  for( iter = m_pvHCICmd->begin(); iter != m_pvHCICmd->end(); iter++ )
  536.        // {
  537.        //     command = *iter;
  538.        //     if( command.as_cmd_tag.AnsiCompareIC( as_cur_cmd_tag ) == 0 )
  539.        //     {
  540.        ////         last_iter    = curr_iter;
  541.        //        // curr_iter    = iter;
  542.        //        // break;
  543.        //     }
  544.        // }
  545.        //
  546.        // send_par_num     = 0;
  547.        // receive_par_num  = 0;
  548.     }
  549.     else // command define parameter
  550.     {
  551.         if( (iLD_l=as_cmd.AnsiPos("#")) != 0 ) // opcode
  552.         {
  553.             iLD_r = as_cmd.AnsiPos(" ");
  554.             if( 0 == iLD_r )
  555.             {
  556.                 if( as_cmd.AnsiPos("0X") == 0 )
  557.                 {
  558.                     iter_cmd->us_opcode = as_cmd.SubString( iLD_l+1, as_cmd.Length() ).ToInt();
  559.                 }
  560.                 else
  561.                 {
  562.                     iLD_r = as_cmd.AnsiPos("0X");
  563.                     AnsiString_Hex_To_UnsignedShort( as_cmd.SubString( iLD_r+2, as_cmd.Length()-iLD_r-2 ), iter_cmd->us_opcode );
  564.                 }
  565.             }
  566.             else
  567.             {
  568.                 if( as_cmd.AnsiPos("0X") == 0 )
  569.                 {
  570.                     iter_cmd->us_opcode = as_cmd.SubString( iLD_r+1, as_cmd.Length() ).ToInt();
  571.                 }
  572.                 else
  573.                 {
  574.                     iLD_r = as_cmd.AnsiPos("0X");
  575.                     AnsiString_Hex_To_UnsignedShort( as_cmd.SubString( iLD_r+2, as_cmd.Length()-iLD_r-2 ), iter_cmd->us_opcode );
  576.                 }
  577.             }
  578.         }
  579.         else if( (iLD_l=as_cmd.AnsiPos("$")) != 0 ) // last event
  580.         {
  581.             iLD_r = as_cmd.AnsiPos(" ");
  582.             if( 0 == iLD_r )
  583.             {
  584.                 if( as_cmd.AnsiPos("0X") == 0 )
  585.                 {
  586.                     iter_cmd->uc_last_event = as_cmd.SubString( iLD_l+1, as_cmd.Length() ).ToInt();
  587.                 }
  588.                 else
  589.                 {
  590.                     iLD_r = as_cmd.AnsiPos("0X");
  591.                     AnsiString_Hex_To_UnsignedChar( as_cmd.SubString( iLD_r+2, as_cmd.Length()-iLD_r-2 ), iter_cmd->uc_last_event );
  592.                 }
  593.             }
  594.             else
  595.             {
  596.                 if( as_cmd.AnsiPos("0X") == 0 )
  597.                 {
  598.                     iter_cmd->uc_last_event = as_cmd.SubString( iLD_r+1, as_cmd.Length() ).ToInt();
  599.                 }
  600.                 else
  601.                 {
  602.                     iLD_r = as_cmd.AnsiPos("0X");
  603.                     AnsiString_Hex_To_UnsignedChar( as_cmd.SubString( iLD_r+2, as_cmd.Length()-iLD_r-2 ), iter_cmd->uc_last_event );
  604.                 }
  605.             }
  606.         }
  607.         else if( (iLD_l=as_cmd.AnsiPos("@")) != 0 )   // send parameter
  608.         {
  609.             iLD_r = as_cmd.AnsiPos(" ");
  610.             S_BT_HCI_PARAMETER par;
  611.             if( 0 != iLD_r )
  612.             {
  613.                 par.ui_bytes  = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1 ).ToInt();
  614.                 if( 0 == as_cmd.AnsiPos("(") )
  615.                 {
  616.                     par.as_name = as_cmd.SubString( iLD_r+1, as_cmd.Length() );
  617.                 }
  618.                 else
  619.                 {
  620.                     par.as_name = as_cmd.SubString( iLD_r+1, as_cmd.AnsiPos("(")-iLD_r-2 );
  621.                 }
  622.             }
  623.             else
  624.             {
  625.                 AnsiString as_log = "parsing " +  as_cur_cmd + " command " + as_cmd + " parameter fail";
  626.                 AddParseLogToStringList( as_log );
  627.                 return false;
  628.             }
  629.             // default value, range
  630.             iLD_l = as_cmd.AnsiPos("(");
  631.             if( 0 == iLD_l ) // default value & range not exist
  632.             {
  633.                 par.b_rangecheck = false;
  634.                 par.s_value.uc_nz_byte     = 0;
  635.                 par.s_min_value.uc_nz_byte = 0;
  636.                 par.s_max_value.uc_nz_byte = 0;
  637.                 for( int i=0; i<BT_HCI_VALUE_SIZE; i++ )
  638.                 {
  639.                     par.s_value.uc_value[i]     = 0;
  640.                     par.s_min_value.uc_value[i] = 0;
  641.                     par.s_max_value.uc_value[i] = 0;
  642.                 }
  643.             }
  644.             else // default value or range exist
  645.             {
  646.                 iLD_r = as_cmd.AnsiPos(",");
  647.                 if( iLD_r != 0 ) // default value & range exist
  648.                 {
  649.                     if( as_cmd.AnsiPos("0X") ) // Hex
  650.                     {
  651.                         iLD_l = as_cmd.AnsiPos("0X");
  652.                         AnsiString_Hex_To_BTHCIValue( as_cmd.SubString(iLD_l+2, iLD_r-iLD_l-2), par.s_value );
  653.                     }
  654.                     else // Dec
  655.                     {   AnsiString_To_BTHCIValue( as_cmd.SubString(iLD_l+1, iLD_r-iLD_l-1), par.s_value );
  656.                     }
  657.                     iLD_l = iLD_r;
  658.                     iLD_r = as_cmd.AnsiPos(")");
  659.                     AnsiString as_range = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1 );
  660.                     iLD_r = as_range.AnsiPos("-");
  661.                     if( iLD_r != 0 )
  662.                     {
  663.                         par.b_rangecheck = true;
  664.                         // min range
  665.                         AnsiString as_min_range = as_range.SubString( 1, iLD_r-1 );
  666.                         if( as_min_range.AnsiPos("0X") ) // Hex
  667.                         {
  668.                             iLD_l = as_min_range.AnsiPos("0X");
  669.                             AnsiString_Hex_To_BTHCIValue( as_min_range.SubString(iLD_l+2, as_min_range.Length()), par.s_min_value );
  670.                         }
  671.                         else // Dec
  672.                         {   AnsiString_To_BTHCIValue( as_min_range.SubString(1, as_min_range.Length()), par.s_min_value );
  673.                         }
  674.                         // max range
  675.                         AnsiString as_max_range = as_range.SubString( iLD_r+1, as_range.Length()-iLD_r );
  676.                         if( as_max_range.AnsiPos("0X") ) // Hex
  677.                         {
  678.                             iLD_l = as_max_range.AnsiPos("0X");
  679.                             AnsiString_Hex_To_BTHCIValue( as_max_range.SubString(iLD_l+2, as_max_range.Length()), par.s_max_value );
  680.                         }
  681.                         else // Dec
  682.                         {   AnsiString_To_BTHCIValue( as_max_range.SubString(1, as_max_range.Length()), par.s_max_value );
  683.                         }
  684.                     }
  685.                     else // no range check
  686.                     {
  687.                         par.b_rangecheck = false;
  688.                     }
  689.                 }
  690.                 else // default value or range exist
  691.                 {
  692.                     if( as_cmd.AnsiPos("-") != 0 ) // default value not exist, range exist
  693.                     {
  694.                         par.b_rangecheck = true;
  695.                         par.s_value.uc_nz_byte     = 0;
  696.                         for( int i=0; i<BT_HCI_VALUE_SIZE; i++ )
  697.                         {
  698.                             par.s_value.uc_value[i]     = 0;
  699.                         }
  700.                         iLD_l = as_cmd.AnsiPos("(");
  701.                         iLD_r = as_cmd.AnsiPos(")");
  702.                         AnsiString as_range = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1 );
  703.                         
  704.                         // min range
  705.                         iLD_r = as_range.AnsiPos("-");
  706.                         AnsiString as_min_range = as_range.SubString( 1, iLD_r-1 );
  707.                         if( as_min_range.AnsiPos("0X") ) // Hex
  708.                         {
  709.                             iLD_l = as_min_range.AnsiPos("0X");
  710.                             AnsiString_Hex_To_BTHCIValue( as_min_range.SubString(iLD_l+2, as_min_range.Length()), par.s_min_value );
  711.                         }
  712.                         else // Dec
  713.                         {   AnsiString_To_BTHCIValue( as_min_range.SubString(1, as_min_range.Length()), par.s_min_value );
  714.                         }
  715.                         // max range
  716.                         AnsiString as_max_range = as_range.SubString( iLD_r+1, as_range.Length()-iLD_r );
  717.                         if( as_max_range.AnsiPos("0X") ) // Hex
  718.                         {
  719.                             iLD_l = as_max_range.AnsiPos("0X");
  720.                             AnsiString_Hex_To_BTHCIValue( as_max_range.SubString(iLD_l+2, as_max_range.Length()), par.s_max_value );
  721.                         }
  722.                         else // Dec
  723.                         {   AnsiString_To_BTHCIValue( as_max_range.SubString(1, as_max_range.Length()), par.s_max_value );
  724.                         }
  725.                     }
  726.                     else // default value exist, range not exist
  727.                     {
  728.                         par.b_rangecheck = false;
  729.                         par.s_min_value.uc_nz_byte = 0;
  730.                         par.s_max_value.uc_nz_byte = 0;
  731.                         for( int i=0; i<BT_HCI_VALUE_SIZE; i++ )
  732.                         {
  733.                             par.s_min_value.uc_value[i] = 0;
  734.                             par.s_max_value.uc_value[i] = 0;
  735.                         }
  736.                         iLD_l = as_cmd.AnsiPos("(");
  737.                         iLD_r = as_cmd.AnsiPos(")");
  738.                         AnsiString as_default = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1 );
  739.                         if( as_default.AnsiPos("0X") ) // Hex
  740.                         {
  741.                             iLD_l = as_default.AnsiPos("0X");
  742.                             AnsiString_Hex_To_BTHCIValue( as_default.SubString(iLD_l+2, as_default.Length()), par.s_value );
  743.                         }
  744.                         else // Dec
  745.                         {   AnsiString_To_BTHCIValue( as_default.SubString(1, as_default.Length()), par.s_value );
  746.                         }
  747.                     }
  748.                 }
  749.             }
  750.             iter_cmd->v_send_par.push_back( par );
  751.             iter_cmd->ui_send_num++;
  752.         }
  753.         else if( (iLD_l=as_cmd.AnsiPos("~")) != 0 )   // receive parameter
  754.         {
  755.             S_BT_HCI_PARAMETER par;
  756.             iLD_r = as_cmd.AnsiPos("*");
  757.             if( 0 == iLD_r )
  758.             {
  759.                 iLD_r = as_cmd.AnsiPos(" ");
  760.                 if( 0 != iLD_r )
  761.                 {
  762.                     par.b_show   = true;
  763.                     par.ui_bytes = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1 ).ToInt();
  764.                     par.as_name  = as_cmd.SubString( iLD_r+1, as_cmd.Length() ).Trim();
  765.                 }
  766.                 else
  767.                 {
  768.                     AnsiString as_log = "parsing " +  as_cur_cmd + " command " + as_cmd + " parameter fail";
  769.                     AddParseLogToStringList( as_log );
  770.                     return false;
  771.                 }
  772.             }
  773.             else
  774.             {
  775.                 iLD_l = as_cmd.AnsiPos("*");
  776.                 iLD_r = as_cmd.AnsiPos(" ");
  777.                 if( 0 != iLD_r )
  778.                 {
  779.                     par.b_show   = false;
  780.                     par.ui_bytes = as_cmd.SubString( iLD_l+1, iLD_r-iLD_l-1 ).ToInt();
  781.                     par.as_name  = as_cmd.SubString( iLD_r+1, as_cmd.Length() ).Trim();
  782.                 }
  783.                 else
  784.                 {
  785.                     AnsiString as_log = "parsing " +  as_cur_cmd + " command " + as_cmd + " parameter fail";
  786.                     AddParseLogToStringList( as_log );
  787.                     return false;
  788.                 }
  789.             }
  790.             iter_cmd->v_receive_par.push_back( par );
  791.             iter_cmd->ui_receive_num++;
  792.         }
  793.         if( (iLD_l=as_cmd.AnsiPos("&")) != 0 )   // timeout
  794.         {
  795.             iLD_r = as_cmd.AnsiPos(" ");
  796.             if( 0 == iLD_r )
  797.             {
  798.                 if( as_cmd.AnsiPos("0X") == 0 )
  799.                 {
  800.                     iter_cmd->ui_ms_timeout = as_cmd.SubString( iLD_l+1, as_cmd.Length() ).Trim().ToInt()*1000;
  801.                 }
  802.                 else
  803.                 {
  804.                     iLD_r = as_cmd.AnsiPos("0X");
  805.                     AnsiString_Hex_To_UnsignedInt( as_cmd.SubString( iLD_r+2, as_cmd.Length()-iLD_r-2 ), iter_cmd->ui_ms_timeout );
  806.                     iter_cmd->ui_ms_timeout *= 1000;
  807.                 }
  808.             }
  809.             else
  810.             {
  811.                 if( as_cmd.AnsiPos("0X") == 0 )
  812.                 {
  813.                     AnsiString as_timeout = as_cmd.SubString( iLD_r+1, as_cmd.Length()-iLD_r ).Trim();
  814.                     iter_cmd->ui_ms_timeout = as_timeout.ToInt()*1000;
  815.                 }
  816.                 else
  817.                 {
  818.                     iLD_r = as_cmd.AnsiPos("0X");
  819.                     AnsiString_Hex_To_UnsignedInt( as_cmd.SubString( iLD_r+2, as_cmd.Length()-iLD_r-2 ), iter_cmd->ui_ms_timeout );
  820.                     iter_cmd->ui_ms_timeout *= 1000;
  821.                 }
  822.             }
  823.         }
  824.         else
  825.         {   iter_cmd->ui_ms_timeout = HCI_DEFAULT_TIMEOUT_MS;
  826.         }
  827.     }
  828.     return true;
  829. }
  830. //===========================================================================
  831. //////////////////////////   Send/receive HCI command   /////////////////////
  832. //===========================================================================
  833. //void  CBTHCI::Compose_HCI_TX_Command( BT_HCI_COMMAND &hci_cmd )
  834. //{
  835. //    vector<S_BT_HCI_PARAMETER>:: iterator iter_par;
  836. //    int idx;
  837. //
  838. //    hci_cmd.m_cmd[0] = 0x01; // fixed pattern
  839. //    hci_cmd.m_cmd[1] = m_sTxCmd.us_opcode & 0x00FF;   // low byte of opcode
  840. //    hci_cmd.m_cmd[2] = (m_sTxCmd.us_opcode & 0xFF00) >> 16; // high byte of opcode
  841. //    hci_cmd.m_cmd[3] = m_sTxCmd.uc_total_par_size; // total parameter size
  842. //    idx = 4;
  843. //    for( iter_par = m_sTxCmd.v_par.begin(); iter_par != m_sTxCmd.v_par.end(); iter_par++ )
  844. //    {
  845. //        for(int i=0; i<iter_par->ui_bytes; i++ )
  846. //        {
  847. //            hci_cmd.m_cmd[idx] = iter_par->s_value.uc_value[i];
  848. //            idx++;
  849. //        }
  850. //    }
  851. //    hci_cmd.m_len = idx + 1;
  852. //}
  853. //---------------------------------------------------------------------------
  854. //void  CBTHCI::DeCompose_HCI_RX_Event( void )
  855. //{
  856. //    vector<S_BT_HCI_PARAMETER>:: iterator iter_par;
  857. //    int idx = 5;
  858. //
  859. //    m_sRxEvent.uc_event_code     = m_sRxEventFmt.uc_event[0];
  860. //    m_sRxEvent.uc_total_par_size = m_sRxEventFmt.uc_event[1];
  861. //    m_sRxEvent.uc_packet_num     = m_sRxEventFmt.uc_event[2];
  862. //    m_sRxEvent.us_opcode         = m_sRxEventFmt.uc_event[4];
  863. //    m_sRxEvent.us_opcode         = (m_sRxEvent.us_opcode << 8) | m_sRxEventFmt.uc_event[3];
  864. //
  865. //    for( iter_par = m_sRxEvent.v_par.begin(); iter_par != m_sRxEvent.v_par.end(); iter_par++ )
  866. //    {
  867. //        for(int i=0; i<iter_par->ui_bytes; i++ )
  868. //        {
  869. //            iter_par->s_value.uc_value[i] =  m_sRxEventFmt.uc_event[idx];
  870. //            idx++;
  871. //        }
  872. //    }
  873. //}
  874. //===========================================================================
  875. ////////////////////////////   Log  information   ////////////////////////
  876. //===========================================================================
  877. void  CBTHCI::AddParseLogToStringList(AnsiString as_log)
  878. {
  879.     m_sBTHCI.log->Add(as_log);
  880.     PostMessage(
  881.                          m_sBTHCI.hPostMsgDestHandle,
  882.                          WM_ML_BT_PRINT_MSG,
  883.                          PAGEIDX_BT_HCI,
  884.                          0
  885.                         );
  886. }
  887. //----------------------------------------------------------------------------
  888. void  CBTHCI::AddTxLogToStringList(BT_HCI_COMMAND hci_cmd)
  889. {
  890.     char        str[1024];
  891.     AnsiString as_log;
  892.     sprintf( str, "%X,", hci_cmd.m_opcode & 0x00FF );
  893.     AnsiString as_opcode_low(str);
  894.     sprintf( str, "%X,", (hci_cmd.m_opcode & 0xFF00)>>8 );
  895.     AnsiString as_opcode_high(str);
  896.     Array_To_Hex_String_UnsignedChar( str, hci_cmd.m_cmd, hci_cmd.m_len, ',' );
  897.     AnsiString as_cmd( (const char*)str );
  898.     if( 0 == hci_cmd.m_len )
  899.        as_cmd = "";
  900.     sprintf( str, "%X,", hci_cmd.m_len );
  901.     AnsiString as_length(str);
  902.    
  903.     as_log = "[" + TimeToStr(Time()) + "] TX: " + as_opcode_low + as_opcode_high + as_length  + as_cmd;
  904.     m_sBTHCI.log->Add( as_log );
  905.     PostMessage(
  906.                          m_sBTHCI.hPostMsgDestHandle,
  907.                          WM_ML_BT_PRINT_MSG,
  908.                          PAGEIDX_BT_HCI,
  909.                          0
  910.                         );
  911. }
  912. //--------------------------------------------------------------------------
  913. void  CBTHCI::AddRxLogToStringList(BT_HCI_EVENT hci_event)
  914. {
  915.     unsigned char str[1024];
  916.     AnsiString as_log;
  917.     sprintf( str, "%X,", hci_event.m_event );
  918.     AnsiString as_event( (const char*) str );
  919.     sprintf( str, "%X,", hci_event.m_len );
  920.     AnsiString as_length( (const char*) str );
  921.     Array_To_Hex_String_UnsignedChar( str, hci_event.m_parms, hci_event.m_len, ',' );
  922.     AnsiString as_param( (const char*) str );
  923.     if(  0 == hci_event.m_len )
  924.         as_param = "";
  925.         
  926.     as_log = "[" + TimeToStr(Time()) + "] RX: " + as_event + as_length + as_param;
  927.     
  928.     m_sBTHCI.log->Add( as_log );
  929.     PostMessage(
  930.                          m_sBTHCI.hPostMsgDestHandle,
  931.                          WM_ML_BT_PRINT_MSG,
  932.                          PAGEIDX_BT_HCI,
  933.                          0
  934.                         );
  935. }
  936. //===========================================================================
  937. ////////////////////////////   Export  information   ////////////////////////
  938. //===========================================================================
  939. E_METAAPP_RESULT_T CBTHCI::Get_ConfirmState(void)
  940. {
  941.     return  m_eConfirmState;
  942. }
  943. //---------------------------------------------------------------------------
  944. vector<S_BT_HCI_CATEGORY>*  CBTHCI::Get_HciCategoryVector( void )
  945. {   return m_pvHCICategory;
  946. }
  947. //---------------------------------------------------------------------------
  948. vector<S_BT_HCI_GROUP>*  CBTHCI::Get_HciGroupVector( void )
  949. {
  950.     return m_pvHCIGroup;
  951. }
  952. //---------------------------------------------------------------------------
  953. BT_HCI_EVENT  CBTHCI::Get_HCIEvent( void )
  954. {   return m_sHCIEvent;
  955. }