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

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_con.cpp
  15.  *
  16.  * Project:
  17.  * --------
  18.  *   Maui META APP
  19.  *
  20.  * Description:
  21.  * ------------
  22.  *   BT connection 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. // BT
  45. #ifndef  _BT_HCI_COMMON_H_
  46. #include "bt_hci_common.h"
  47. #endif
  48. #ifndef  _BT_CON_H_
  49. #include "bt_con.h"
  50. #endif
  51. // man
  52. #ifndef  _MAN_TIMER_H_
  53. #include "man_timer.h"
  54. #endif
  55. #ifndef  _MAN_ACTIVE_H_
  56. #include "man_active.h"
  57. #endif
  58. // misc
  59. #ifndef  _FT_UTILS_H_
  60. #include "ft_utils.h"
  61. #endif
  62. // message
  63. #ifndef _META_LAB_MSG_H_
  64. #include "meta_lab_msg.h"
  65. #endif
  66. //==============================================================================
  67. static CBTCT* gs_bt_ct_ptr;
  68. static bool g_bIsRunning = false;
  69. //==============================================================================
  70. static void REQ_Stop(void)
  71. {   gs_bt_ct_ptr->REQ_Stop();
  72. }
  73. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. static void  REQ_SendResetHCICommand( void )
  75. {   gs_bt_ct_ptr->REQ_SendResetHCICommand();
  76. }
  77. //---------------------------------------------------------------------------
  78. static void __stdcall CNF_ResetHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  79. {   gs_bt_ct_ptr->CNF_ResetHCIEvent( cnf, token, usrData );
  80. }
  81. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. static void  REQ_Send_Read_Local_BDAdrress_HCI_command( void )
  83. {   gs_bt_ct_ptr->REQ_Send_Read_Local_BDAdrress_HCI_command();
  84. }
  85. //-----------------------------------------------------------------------------
  86. static void __stdcall CNF_ReadLocalBDAddressHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  87. {   gs_bt_ct_ptr->CNF_ReadLocalBDAddressHCIEvent( cnf, token, usrData );
  88. }
  89. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. static void  REQ_Send_Write_Local_BDAdrress_HCI_command( void )
  91. {   gs_bt_ct_ptr->REQ_Send_Write_Local_BDAdrress_HCI_command();
  92. }
  93. //---------------------------------------------------------------------------
  94. static void __stdcall CNF_WriteLocalBDAddressHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  95. {   gs_bt_ct_ptr->CNF_WriteLocalBDAddressHCIEvent( cnf, token, usrData );
  96. }
  97. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. static void  REQ_Send_Scan_HCI_command( void )
  99. {   gs_bt_ct_ptr->REQ_Send_Scan_HCI_command();
  100. }
  101. //---------------------------------------------------------------------------
  102. static void __stdcall CNF_ScanHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  103. {   gs_bt_ct_ptr->CNF_ScanHCIEvent( cnf, token, usrData );
  104. }
  105. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106. static void REQ_Send_Inquiry_Scan_Interval_HCI_command(void)
  107. {
  108.     gs_bt_ct_ptr->REQ_Send_Inquiry_Scan_Interval_HCI_command();
  109. }
  110. //---------------------------------------------------------------------------
  111. static void __stdcall CNF_InquiryScanIntervalHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  112. {
  113.     gs_bt_ct_ptr->CNF_InquiryScanIntervalHCIEvent(cnf, token, usrData);
  114. }
  115. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  116. static void REQ_Send_Page_Scan_Interval_HCI_command(void)
  117. {
  118.     gs_bt_ct_ptr->REQ_Send_Page_Scan_Interval_HCI_command();
  119. }
  120. //---------------------------------------------------------------------------
  121. static void __stdcall CNF_PageScanIntervalHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  122. {
  123.     gs_bt_ct_ptr->CNF_PageScanIntervalHCIEvent(cnf, token, usrData);
  124. }
  125. static void  REQ_Send_Auto_Accept_HCI_command( void )
  126. {   gs_bt_ct_ptr->REQ_Send_Auto_Accept_HCI_command();
  127. }
  128. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  129. static void  REQ_Send_Inquiry_HCI_command( void )
  130. {   gs_bt_ct_ptr->REQ_Send_Inquiry_HCI_command();
  131. }
  132. //---------------------------------------------------------------------------
  133. static void __stdcall CNF_InquiryHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  134. {   gs_bt_ct_ptr->CNF_InquiryHCIEvent( cnf, token, usrData );
  135. }
  136. //---------------------------------------------------------------------------
  137. static void  REQ_Send_Inquiry_Cancel_HCI_command( void )
  138. {   gs_bt_ct_ptr->REQ_Send_Inquiry_Cancel_HCI_command();
  139. }
  140. //---------------------------------------------------------------------------
  141. static void __stdcall CNF_InquiryCancelHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  142. {   gs_bt_ct_ptr->CNF_InquiryCancelHCIEvent( cnf, token, usrData );
  143. }
  144. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  145. static void  REQ_Send_ConnectACL_HCI_command( void )
  146. {   gs_bt_ct_ptr->REQ_Send_ConnectACL_HCI_command();
  147. }
  148. //---------------------------------------------------------------------------
  149. static void __stdcall CNF_ConnectACLHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  150. {   gs_bt_ct_ptr->CNF_ConnectACLHCIEvent( cnf, token, usrData );
  151. }
  152. //---------------------------------------------------------------------------
  153. static void __stdcall CNF_DisconnectACLAutoHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  154. {   gs_bt_ct_ptr->CNF_DisconnectACLAutoHCIEvent( cnf, token, usrData );
  155. }
  156. //---------------------------------------------------------------------------
  157. static void __stdcall CNF_LinkKeyNegReplyHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  158. {   gs_bt_ct_ptr->CNF_LinkKeyNegReplyHCIEvent( cnf, token, usrData );
  159. }
  160. //---------------------------------------------------------------------------
  161. static void  REQ_Send_DisconnectACL_HCI_command( void )
  162. {   gs_bt_ct_ptr->REQ_Send_DisconnectACL_HCI_command();
  163. }
  164. //---------------------------------------------------------------------------
  165. static void __stdcall CNF_DisconnectACLHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  166. {   gs_bt_ct_ptr->CNF_DisconnectACLHCIEvent( cnf, token, usrData );
  167. }
  168. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169. static void  REQ_Send_Change_ACL_Packet_Type_HCI_command( void )
  170. {   gs_bt_ct_ptr->REQ_Send_Change_ACL_Packet_Type_HCI_command();
  171. }
  172. //---------------------------------------------------------------------------
  173. static void __stdcall CNF_ChangeACLPacketTypeHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  174. {   gs_bt_ct_ptr->CNF_ChangeACLPacketTypeHCIEvent( cnf, token, usrData );
  175. }
  176. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  177. static void  REQ_Send_EnterPIN_HCI_command( void )
  178. {   gs_bt_ct_ptr->REQ_Send_EnterPIN_HCI_command();
  179. }
  180. //---------------------------------------------------------------------------
  181. static void __stdcall CNF_EnterPINHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  182. {   gs_bt_ct_ptr->CNF_EnterPINHCIEvent( cnf, token, usrData );
  183. }
  184. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  185. static void  REQ_Send_ConnectSCO_HCI_command( void )
  186. {   gs_bt_ct_ptr->REQ_Send_ConnectSCO_HCI_command();
  187. }
  188. //---------------------------------------------------------------------------
  189. static void __stdcall CNF_ConnectSCOHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  190. {   gs_bt_ct_ptr->CNF_ConnectSCOHCIEvent( cnf, token, usrData );
  191. }
  192. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  193. static void  REQ_Send_DisconnectSCO_HCI_command( void )
  194. {   gs_bt_ct_ptr->REQ_Send_DisconnectSCO_HCI_command();
  195. }
  196. //---------------------------------------------------------------------------
  197. static void __stdcall CNF_DisconnectSCOHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  198. {   gs_bt_ct_ptr->CNF_DisconnectSCOHCIEvent( cnf, token, usrData );
  199. }
  200. //===========================================================================
  201. CBTCT::CBTCT( void )
  202. {
  203.     g_bIsRunning = false;
  204.     ConfirmCallback = 0;
  205.     AllocateVector();
  206. }
  207. //---------------------------------------------------------------------------
  208. CBTCT::~CBTCT( void )
  209. {
  210.     g_bIsRunning = false;
  211.     ConfirmCallback = 0;
  212.     DeAllocateVector();
  213. }
  214. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  215. void CBTCT::AllocateVector( void )
  216. {
  217.     if( NULL == m_pvCTRes )
  218.         m_pvCTRes     = new vector<S_BT_CT_RES>;
  219. }
  220. //---------------------------------------------------------------------------
  221. void CBTCT::DeAllocateVector( void )
  222. {
  223.     if( NULL != m_pvCTRes )
  224.     {
  225.         if( ! m_pvCTRes->empty() )
  226.         {
  227.             m_pvCTRes->erase( m_pvCTRes->begin(), m_pvCTRes->end() );
  228.         }
  229.         delete m_pvCTRes;
  230.         m_pvCTRes = NULL;
  231.     }
  232. }
  233. //---------------------------------------------------------------------------
  234. void  CBTCT::EraseVector( void )
  235. {
  236.     if( ! m_pvCTRes->empty() )
  237.     {   m_pvCTRes->erase( m_pvCTRes->begin(), m_pvCTRes->end() );
  238.     }
  239. }
  240. //---------------------------------------------------------------------------
  241. //---------------------------------------------------------------------------
  242. void  CBTCT::REQ_Stop( void )
  243. {
  244.     if (!g_bIsRunning)
  245.     {
  246.         return;
  247.     }
  248.     
  249.     Confirm( METAAPP_STOP );
  250. }
  251. //---------------------------------------------------------------------------
  252. void  CBTCT::Confirm(E_METAAPP_RESULT_T confirm_state)
  253. {
  254.     if (!g_bIsRunning)
  255.     {
  256.         return;
  257.     }
  258.     g_bIsRunning = false;
  259.     if (NULL == ConfirmCallback)
  260.     {
  261.         return;
  262.     }    
  263.     m_eConfirmState = confirm_state;
  264.     ActiveMan->SetActiveFunction( ConfirmCallback );
  265. }
  266. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  267. void  CBTCT::Reset_HCI_Command( BT_HCI_COMMAND &hci_cmd )
  268. {
  269.     for( int i=0; i<(sizeof(hci_cmd.m_cmd)/sizeof(hci_cmd.m_cmd[0])); i++ )
  270.     {
  271.         hci_cmd.m_cmd[i] = 0;
  272.     }
  273. }
  274. //==========================================================================
  275. void  CBTCT::REQ_Stop_Start( void )
  276. {
  277.     gs_bt_ct_ptr  = this;
  278.     g_bIsRunning  = true;
  279.     META_CancelAllBlockingCall_r( m_META_HANDLE_Obj.Get_MainHandle() );
  280.     ActiveMan->SetActiveFunction( ::REQ_Stop );
  281.    
  282. }
  283. //==========================================================================
  284. void  CBTCT::REQ_Reset_Start( S_BT_CT bt_ct )
  285. {
  286.     gs_bt_ct_ptr  = this;
  287.     g_bIsRunning  = true;
  288.     m_sBTCT = bt_ct;
  289.     EraseVector();
  290.     ActiveMan->SetActiveFunction( ::REQ_SendResetHCICommand );
  291. }
  292. //---------------------------------------------------------------
  293. void CBTCT::REQ_SendResetHCICommand( void )
  294. {
  295.     if(!g_bIsRunning)  return;
  296.     BT_HCI_COMMAND hci_cmd;
  297.     Compose_Reset_HCI_Command( hci_cmd );
  298.     AddTxLogToStringList( hci_cmd );
  299.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_ResetHCIEvent, NULL, HCE_COMMAND_COMPLETE);
  300.     if (!m_bEventOk)
  301.     {
  302.         Confirm( METAAPP_FAIL );
  303.         return;
  304.     }
  305.     if (META_SUCCESS != MetaResult)
  306.     {
  307.         if( META_TIMEOUT == MetaResult )
  308.         {
  309.             Confirm( METAAPP_TIMEOUT );
  310.             return;
  311.         }
  312.         else if( META_CANCEL == MetaResult )
  313.         {
  314.             Confirm( METAAPP_CANCEL );
  315.             return;
  316.         }
  317.         else
  318.         {
  319.             Confirm( METAAPP_FAIL );
  320.             return;
  321.         }
  322.     }
  323.     Confirm(METAAPP_SUCCESS);
  324. }
  325. //---------------------------------------------------------------------------
  326. void __stdcall CBTCT::CNF_ResetHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  327. {
  328.     // 0E,04,01,03,0C,00
  329.     if (!g_bIsRunning)
  330.     {
  331.         return;
  332.     }
  333.     
  334.     AddRxLogToStringList(*cnf);
  335.     if ((cnf->m_len      != 0x04) ||
  336.         (cnf->m_parms[0] != 0x01) ||
  337.         (cnf->m_parms[1] != 0x03) ||
  338.         (cnf->m_parms[2] != 0x0C) ||
  339.         (cnf->m_parms[3] != 0x00)
  340.       )
  341.     {
  342.         m_bEventOk = false;
  343.     }
  344.     else
  345.     {
  346.         m_bEventOk = true;
  347.     }
  348. }
  349. //---------------------------------------------------------------------------
  350. void CBTCT::Compose_Reset_HCI_Command( BT_HCI_COMMAND &hci_cmd )
  351. {
  352.     hci_cmd.m_opcode = 0x0C03;
  353.     hci_cmd.m_len    = 0x00;
  354. }
  355. //===========================================================================
  356. //////////////////////////// Read local BD address //////////////////////////
  357. //===========================================================================
  358. void  CBTCT::REQ_ReadLocalBDAddress_Start( S_BT_CT bt_ct )
  359. {
  360.     gs_bt_ct_ptr  = this;
  361.     g_bIsRunning  = true;
  362.     m_sBTCT = bt_ct;
  363.     ActiveMan->SetActiveFunction( ::REQ_Send_Read_Local_BDAdrress_HCI_command );
  364. }
  365. //---------------------------------------------------------------------------
  366. void  CBTCT::REQ_Send_Read_Local_BDAdrress_HCI_command( void )
  367. {
  368.     if(!g_bIsRunning)  return;
  369.     m_bEventOk = false;
  370.     BT_HCI_COMMAND hci_cmd;
  371.     Compose_Read_Local_BD_Addr_HCI_Command( hci_cmd );
  372.     AddTxLogToStringList( hci_cmd );
  373.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_ReadLocalBDAddressHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  374.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  375.     {
  376.         if( META_TIMEOUT == MetaResult )
  377.         {
  378.             Confirm( METAAPP_TIMEOUT );
  379.             return;
  380.         }
  381.         else if( META_CANCEL == MetaResult )
  382.         {
  383.             Confirm( METAAPP_CANCEL );
  384.             return;
  385.         }
  386.         else
  387.         {
  388.             Confirm( METAAPP_FAIL );
  389.             return;
  390.         }
  391.     }
  392.     Confirm(METAAPP_SUCCESS);
  393. }
  394. //---------------------------------------------------------------------------
  395. void __stdcall CBTCT::CNF_ReadLocalBDAddressHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  396. {
  397.     if(!g_bIsRunning)  return;
  398.     if( 0xFF == cnf->m_event )  return; // LMP
  399.     AddRxLogToStringList( *cnf );
  400.     m_bEventOk = DeCompose_Read_Local_BD_Addr_HCI_Event( *cnf, m_sLocalBdAddr );
  401. }
  402. //--------------------------------------------------------------------------
  403. void  CBTCT::Compose_Read_Local_BD_Addr_HCI_Command( BT_HCI_COMMAND &hci_cmd )
  404. {
  405.     hci_cmd.m_opcode = 0x1009;
  406.     hci_cmd.m_len    = 0;
  407.     Reset_HCI_Command( hci_cmd );
  408. }
  409. //--------------------------------------------------------------------------
  410. bool  CBTCT::DeCompose_Read_Local_BD_Addr_HCI_Event( BT_HCI_EVENT hci_event, S_BD_ADDRS &s_bd_addrs )
  411. {
  412.     if(
  413.         0x09 != hci_event.m_parms[1] ||
  414.         0x10 != hci_event.m_parms[2] ||
  415.         0x00 != hci_event.m_parms[3]
  416.        )
  417.      {  return false;
  418.      }
  419.      S_BD_ADDR bd_addr;
  420.      for( int i=0; i<BD_ADDRESS_LEN; i++ )
  421.         bd_addr.uc_BD_Addr[i] = hci_event.m_parms[4+i];
  422.     s_bd_addrs.us_NAP = (bd_addr.uc_BD_Addr[5] << 8) | bd_addr.uc_BD_Addr[4];
  423.     s_bd_addrs.uc_UAP = bd_addr.uc_BD_Addr[3];
  424.     s_bd_addrs.ui_LAP = (bd_addr.uc_BD_Addr[2] << 16) | (bd_addr.uc_BD_Addr[1] << 8) | bd_addr.uc_BD_Addr[0];
  425.     return true;
  426. }
  427. //===========================================================================
  428. ///////////////////////////  write local BD address  ////////////////////////
  429. //===========================================================================
  430. void  CBTCT::REQ_WriteLocalBDAddress_Start( S_BT_CT bt_ct )
  431. {
  432.     gs_bt_ct_ptr  = this;
  433.     g_bIsRunning  = true;
  434.     m_sBTCT = bt_ct;
  435.     ActiveMan->SetActiveFunction( ::REQ_Send_Write_Local_BDAdrress_HCI_command );
  436. }
  437. //---------------------------------------------------------------------------
  438. void  CBTCT::REQ_Send_Write_Local_BDAdrress_HCI_command( void )
  439. {
  440.     if(!g_bIsRunning)  return;
  441.     m_bEventOk = false;
  442.     BT_HCI_COMMAND hci_cmd;
  443.     Compose_Write_Local_BD_Addr_HCI_Command( hci_cmd, m_sLocalBdAddr );
  444.     AddTxLogToStringList( hci_cmd );
  445.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_WriteLocalBDAddressHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  446.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  447.     {
  448.         if( META_TIMEOUT == MetaResult )
  449.         {
  450.             Confirm( METAAPP_TIMEOUT );
  451.             return;
  452.         }
  453.         else if( META_CANCEL == MetaResult )
  454.         {
  455.             Confirm( METAAPP_CANCEL );
  456.             return;
  457.         }
  458.         else
  459.         {
  460.             Confirm( METAAPP_FAIL );
  461.             return;
  462.         }
  463.     }
  464.     Confirm(METAAPP_SUCCESS);
  465. }
  466. //---------------------------------------------------------------------------
  467. void __stdcall CBTCT::CNF_WriteLocalBDAddressHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  468. {
  469.     if(!g_bIsRunning)  return;
  470.     if( 0xFF == cnf->m_event )  return; // LMP
  471.     AddRxLogToStringList( *cnf );
  472.     m_bEventOk = DeCompose_Write_Local_BD_Addr_HCI_Event( *cnf );
  473. }
  474. //--------------------------------------------------------------------------
  475. void  CBTCT::Compose_Write_Local_BD_Addr_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BD_ADDRS s_bd_addrs )
  476. {
  477.     hci_cmd.m_opcode = 0xFC1A;
  478.     hci_cmd.m_len    = 6;
  479.     Reset_HCI_Command( hci_cmd );
  480.     hci_cmd.m_cmd[0] = m_sLocalBdAddr.ui_LAP & 0x000000FF;
  481.     hci_cmd.m_cmd[1] = (m_sLocalBdAddr.ui_LAP & 0x0000FF00) >>  8;
  482.     hci_cmd.m_cmd[2] = (m_sLocalBdAddr.ui_LAP & 0x00FF0000) >> 16;
  483.     hci_cmd.m_cmd[3] =  m_sLocalBdAddr.uc_UAP;
  484.     hci_cmd.m_cmd[4] =  m_sLocalBdAddr.us_NAP & 0x00FF;
  485.     hci_cmd.m_cmd[5] = (m_sLocalBdAddr.us_NAP & 0xFF00) >> 8;
  486. }
  487. //--------------------------------------------------------------------------
  488. bool  CBTCT::DeCompose_Write_Local_BD_Addr_HCI_Event( BT_HCI_EVENT hci_event )
  489. {
  490.     if(
  491.         0x1A != hci_event.m_parms[1] ||
  492.         0xFC != hci_event.m_parms[2] ||
  493.         0x00 != hci_event.m_parms[3]
  494.        )
  495.      {  return false;
  496.      }
  497.     return true;
  498. }
  499. //===========================================================================
  500. ////////////////////////////       Scan            //////////////////////////
  501. //===========================================================================
  502. void  CBTCT::REQ_Scan_Start( S_BT_CT bt_ct )
  503. {
  504.     gs_bt_ct_ptr  = this;
  505.     g_bIsRunning  = true;
  506.     m_sBTCT = bt_ct;
  507.     ActiveMan->SetActiveFunction( ::REQ_Send_Scan_HCI_command );
  508. }
  509. //---------------------------------------------------------------------------
  510. void  CBTCT::REQ_Send_Scan_HCI_command( void )
  511. {
  512.     if(!g_bIsRunning)  return;
  513.     m_bEventOk = false;
  514.     BT_HCI_COMMAND hci_cmd;
  515.     // interlaced inquiry scan
  516.     Compose_Inquiry_Scan_Type_HCI_Command( hci_cmd, m_sScan );
  517.     AddTxLogToStringList( hci_cmd );
  518.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_ScanHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  519.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  520.     {
  521.         if( META_TIMEOUT == MetaResult )
  522.         {
  523.             Confirm( METAAPP_TIMEOUT );
  524.             return;
  525.         }
  526.         else if( META_CANCEL == MetaResult )
  527.         {
  528.             Confirm( METAAPP_CANCEL );
  529.             return;
  530.         }
  531.         else
  532.         {
  533.             Confirm( METAAPP_FAIL );
  534.             return;
  535.         }
  536.     }
  537.     // interlaced page scan
  538.     Compose_Page_Scan_Type_HCI_Command( hci_cmd, m_sScan );
  539.     AddTxLogToStringList( hci_cmd );
  540.     MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_ScanHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  541.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  542.     {
  543.         if( META_TIMEOUT == MetaResult )
  544.         {
  545.             Confirm( METAAPP_TIMEOUT );
  546.             return;
  547.         }
  548.         else if( META_CANCEL == MetaResult )
  549.         {
  550.             Confirm( METAAPP_CANCEL );
  551.             return;
  552.         }
  553.         else
  554.         {
  555.             Confirm( METAAPP_FAIL );
  556.             return;
  557.         }
  558.     }
  559.     //  scan type
  560.     Compose_Scan_Enable_HCI_Command( hci_cmd, m_sScan );
  561.     AddTxLogToStringList( hci_cmd );
  562.     MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_ScanHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  563.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  564.     {
  565.         if( META_TIMEOUT == MetaResult )
  566.         {
  567.             Confirm( METAAPP_TIMEOUT );
  568.             return;
  569.         }
  570.         else if( META_CANCEL == MetaResult )
  571.         {
  572.             Confirm( METAAPP_CANCEL );
  573.             return;
  574.         }
  575.         else
  576.         {
  577.             Confirm( METAAPP_FAIL );
  578.             return;
  579.         }
  580.     }
  581.     Confirm(METAAPP_SUCCESS);
  582. }
  583. //---------------------------------------------------------------------------
  584. void __stdcall CBTCT::CNF_ScanHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  585. {
  586.     if(!g_bIsRunning)  return;
  587.     if( 0xFF == cnf->m_event )  return; // LMP
  588.     AddRxLogToStringList( *cnf );
  589.     m_bEventOk = DeCompose_Scan_HCI_Event( *cnf );
  590. }
  591. //--------------------------------------------------------------------------
  592. void  CBTCT::Compose_Inquiry_Scan_Type_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_SCAN scan )
  593. {
  594.     hci_cmd.m_opcode = 0x0C43;
  595.     hci_cmd.m_len    = 1;
  596.     Reset_HCI_Command( hci_cmd );
  597.     if( scan.b_interlaced_inquiry_scan )
  598.         hci_cmd.m_cmd[0] = 1;
  599.     else
  600.         hci_cmd.m_cmd[0] = 0;
  601. }
  602. //--------------------------------------------------------------------------
  603. void  CBTCT::Compose_Page_Scan_Type_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_SCAN scan )
  604. {
  605.     hci_cmd.m_opcode = 0x0C47;
  606.     hci_cmd.m_len    = 1;
  607.     Reset_HCI_Command( hci_cmd );
  608.     if( scan.b_interlaced_page_scan )
  609.         hci_cmd.m_cmd[0] = 1;
  610.     else
  611.         hci_cmd.m_cmd[0] = 0;
  612. }
  613. //--------------------------------------------------------------------------
  614. void  CBTCT::Compose_Scan_Enable_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_SCAN scan )
  615. {
  616.     hci_cmd.m_opcode = 0x0C1A;
  617.     hci_cmd.m_len    = 1;
  618.     Reset_HCI_Command( hci_cmd );
  619.     hci_cmd.m_cmd[0] = (unsigned char) scan.e_scan_type;
  620. }
  621. //--------------------------------------------------------------------------
  622. bool  CBTCT::DeCompose_Scan_HCI_Event( BT_HCI_EVENT hci_event )
  623. {
  624.     if(
  625.         0x00 != hci_event.m_parms[3]
  626.        )
  627.      {  return false;
  628.      }
  629.     return true;
  630. }
  631. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  632. void CBTCT::REQ_Set_Inquiry_Scan_Interval_Start(S_BT_CT bt_ct)
  633. {
  634.     gs_bt_ct_ptr  = this;
  635.     g_bIsRunning  = true;
  636.     m_sBTCT = bt_ct;
  637.     ActiveMan->SetActiveFunction(::REQ_Send_Inquiry_Scan_Interval_HCI_command);
  638. }
  639. //---------------------------------------------------------------------------
  640. void CBTCT::REQ_Send_Inquiry_Scan_Interval_HCI_command(void)
  641. {
  642.     if (!g_bIsRunning)
  643.     {
  644.         return;
  645.     }
  646.     m_bEventOk = false;
  647.     BT_HCI_COMMAND hci_cmd;
  648.     // interlaced inquiry scan
  649.     Compose_Inquiry_Scan_Interval_HCI_Command(hci_cmd);
  650.     AddTxLogToStringList(hci_cmd);
  651.     META_RESULT MetaResult = META_BT_SendHCICommand_r(m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_InquiryScanIntervalHCIEvent, NULL, HCE_COMMAND_COMPLETE);
  652.     if ((META_SUCCESS != MetaResult) || (!m_bEventOk))
  653.     {
  654.         if (META_TIMEOUT == MetaResult)
  655.         {
  656.             Confirm(METAAPP_TIMEOUT);
  657.             return;
  658.         }
  659.         else if (META_CANCEL == MetaResult)
  660.         {
  661.             Confirm(METAAPP_CANCEL);
  662.             return;
  663.         }
  664.         else
  665.         {
  666.             Confirm(METAAPP_FAIL);
  667.             return;
  668.         }
  669.     }
  670.     Confirm(METAAPP_SUCCESS);
  671. }
  672. //---------------------------------------------------------------------------
  673. void __stdcall CBTCT::CNF_InquiryScanIntervalHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  674. {
  675.     if (!g_bIsRunning)
  676.     {
  677.         return;
  678.     }
  679.     if (0xFF == cnf->m_event)
  680.     {
  681.         return; // LMP
  682.     }
  683.     AddRxLogToStringList(*cnf);
  684.     m_bEventOk = DeCompose_Inquiry_Scan_Interval_HCI_Event(*cnf);
  685. }
  686. //--------------------------------------------------------------------------
  687. void CBTCT::Compose_Inquiry_Scan_Interval_HCI_Command(BT_HCI_COMMAND &hci_cmd)
  688. {
  689.     hci_cmd.m_opcode = 0x0C1E;
  690.     hci_cmd.m_len    = 4;
  691.     Reset_HCI_Command(hci_cmd);
  692.     hci_cmd.m_cmd[0] = 0x64;
  693.     hci_cmd.m_cmd[1] = 0x00;
  694.     hci_cmd.m_cmd[2] = 0x11;
  695.     hci_cmd.m_cmd[3] = 0x00;
  696. }
  697. //--------------------------------------------------------------------------
  698. bool CBTCT::DeCompose_Inquiry_Scan_Interval_HCI_Event(BT_HCI_EVENT hci_event)
  699. {
  700.     if(
  701.         0x00 != hci_event.m_parms[3]
  702.        )
  703.      {  return false;
  704.      }
  705.     return true;
  706. }
  707. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  708. void CBTCT::REQ_Set_Page_Scan_Interval_Start(S_BT_CT bt_ct)
  709. {
  710.     gs_bt_ct_ptr  = this;
  711.     g_bIsRunning  = true;
  712.     m_sBTCT = bt_ct;
  713.     
  714.     ActiveMan->SetActiveFunction(::REQ_Send_Page_Scan_Interval_HCI_command);
  715. }
  716. //---------------------------------------------------------------------------
  717. void CBTCT::REQ_Send_Page_Scan_Interval_HCI_command(void)
  718. {
  719.     if (!g_bIsRunning)
  720.     {
  721.         return;
  722.     }
  723.     m_bEventOk = false;
  724.     BT_HCI_COMMAND hci_cmd;
  725.     // interlaced inquiry scan
  726.     Compose_Page_Scan_Interval_HCI_Command(hci_cmd);
  727.     AddTxLogToStringList(hci_cmd);
  728.     META_RESULT MetaResult = META_BT_SendHCICommand_r(m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_PageScanIntervalHCIEvent, NULL, HCE_COMMAND_COMPLETE);
  729.     if ((META_SUCCESS != MetaResult) || (!m_bEventOk))
  730.     {
  731.         if (META_TIMEOUT == MetaResult)
  732.         {
  733.             Confirm(METAAPP_TIMEOUT);
  734.             return;
  735.         }
  736.         else if (META_CANCEL == MetaResult)
  737.         {
  738.             Confirm(METAAPP_CANCEL);
  739.             return;
  740.         }
  741.         else
  742.         {
  743.             Confirm(METAAPP_FAIL);
  744.             return;
  745.         }
  746.     }
  747.     Confirm(METAAPP_SUCCESS);
  748. }
  749. //---------------------------------------------------------------------------
  750. void __stdcall CBTCT::CNF_PageScanIntervalHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  751. {
  752.     if (!g_bIsRunning)
  753.     {
  754.         return;
  755.     }
  756.     if (0xFF == cnf->m_event)
  757.     {
  758.         return; // LMP
  759.     }
  760.     AddRxLogToStringList(*cnf);
  761.     m_bEventOk = DeCompose_Page_Scan_Interval_HCI_Event(*cnf);
  762. }
  763. //--------------------------------------------------------------------------
  764. void CBTCT::Compose_Page_Scan_Interval_HCI_Command(BT_HCI_COMMAND &hci_cmd)
  765. {
  766.     hci_cmd.m_opcode = 0x0C1C;
  767.     hci_cmd.m_len    = 4;
  768.     Reset_HCI_Command(hci_cmd);
  769.     hci_cmd.m_cmd[0] = 0x64;
  770.     hci_cmd.m_cmd[1] = 0x00;
  771.     hci_cmd.m_cmd[2] = 0x11;
  772.     hci_cmd.m_cmd[3] = 0x00;
  773. }
  774. //--------------------------------------------------------------------------
  775. bool CBTCT::DeCompose_Page_Scan_Interval_HCI_Event(BT_HCI_EVENT hci_event)
  776. {
  777.     if(
  778.         0x00 != hci_event.m_parms[3]
  779.        )
  780.      {  return false;
  781.      }
  782.     return true;
  783. }
  784. //===========================================================================
  785. ////////////////////////////       auto accept     //////////////////////////
  786. //===========================================================================
  787. void  CBTCT::REQ_Auto_Accept_Start( S_BT_CT bt_ct )
  788. {
  789.     gs_bt_ct_ptr  = this;
  790.     g_bIsRunning  = true;
  791.     m_sBTCT = bt_ct;
  792.     ActiveMan->SetActiveFunction( ::REQ_Send_Auto_Accept_HCI_command );
  793. }
  794. //---------------------------------------------------------------------------
  795. void  CBTCT::REQ_Send_Auto_Accept_HCI_command( void )
  796. {
  797.     if(!g_bIsRunning)  return;
  798.     m_bEventOk = false;
  799.     BT_HCI_COMMAND hci_cmd;
  800.     // interlaced inquiry scan
  801.     Compose_Auto_Accept_HCI_Command( hci_cmd );
  802.     AddTxLogToStringList( hci_cmd );
  803.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_ScanHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  804.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  805.     {
  806.         if( META_TIMEOUT == MetaResult )
  807.         {
  808.             Confirm( METAAPP_TIMEOUT );
  809.             return;
  810.         }
  811.         else if( META_CANCEL == MetaResult )
  812.         {
  813.             Confirm( METAAPP_CANCEL );
  814.             return;
  815.         }
  816.         else
  817.         {
  818.             Confirm( METAAPP_FAIL );
  819.             return;
  820.         }
  821.     }
  822.     Confirm(METAAPP_SUCCESS);
  823. }
  824. //---------------------------------------------------------------------------
  825. void __stdcall CBTCT::CNF_AutoAcceptHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  826. {
  827.     if(!g_bIsRunning)  return;
  828.     if( 0xFF == cnf->m_event )  return; // LMP
  829.     AddRxLogToStringList( *cnf );
  830.     m_bEventOk = DeCompose_Scan_HCI_Event( *cnf );
  831. }
  832. //--------------------------------------------------------------------------
  833. void  CBTCT::Compose_Auto_Accept_HCI_Command( BT_HCI_COMMAND &hci_cmd )
  834. {
  835.     hci_cmd.m_opcode = 0x0C05;
  836.     hci_cmd.m_len    = 3;
  837.     Reset_HCI_Command( hci_cmd );
  838.     hci_cmd.m_cmd[0] = 0x02;
  839.     hci_cmd.m_cmd[1] = 0x00;
  840.     hci_cmd.m_cmd[2] = 0x02;
  841. }
  842. //--------------------------------------------------------------------------
  843. bool  CBTCT::DeCompose_Auto_Accept_HCI_Event( BT_HCI_EVENT hci_event )
  844. {
  845.     if(
  846.         0x00 != hci_event.m_parms[3]
  847.        )
  848.      {  return false;
  849.      }
  850.     return true;
  851. }
  852. //===========================================================================
  853. ///////////////////////////        Inquiry           ////////////////////////
  854. //===========================================================================
  855. void  CBTCT::REQ_Inquiry_Start( S_BT_CT bt_ct )
  856. {
  857.     gs_bt_ct_ptr  = this;
  858.     g_bIsRunning  = true;
  859.     m_sBTCT = bt_ct;
  860.   //  EraseVector();
  861.     ActiveMan->SetActiveFunction( ::REQ_Send_Inquiry_HCI_command );
  862. }
  863. //---------------------------------------------------------------------------
  864. void  CBTCT::REQ_Send_Inquiry_HCI_command( void )
  865. {
  866.     if(!g_bIsRunning)  return;
  867.     m_bEventOk = false;
  868.     BT_HCI_COMMAND hci_cmd;
  869.     m_uiEventCount = 0;
  870.     Compose_Inquiry_HCI_Command( hci_cmd );
  871.     AddTxLogToStringList( hci_cmd );
  872.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), m_uiInquiryTimeout, &hci_cmd, ::CNF_InquiryHCIEvent, NULL, 0x01 );
  873.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  874.     {
  875.         if( META_TIMEOUT == MetaResult )
  876.         {
  877.             Confirm( METAAPP_TIMEOUT );
  878.             return;
  879.         }
  880.         else if( META_CANCEL == MetaResult )
  881.         {
  882.             Confirm( METAAPP_CANCEL );
  883.             return;
  884.         }
  885.         else
  886.         {
  887.             Confirm( METAAPP_FAIL );
  888.             return;
  889.         }
  890.     }
  891.     Confirm(METAAPP_SUCCESS);
  892. }
  893. //---------------------------------------------------------------------------
  894. void __stdcall CBTCT::CNF_InquiryHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  895. {
  896.     if(!g_bIsRunning)  return;
  897.     if( 0xFF == cnf->m_event )  return; // LMP
  898.     m_uiEventCount++;
  899.     AddRxLogToStringList( *cnf );
  900.     m_bEventOk = DeCompose_Inquiry_HCI_Event( *cnf );
  901. }
  902. //--------------------------------------------------------------------------
  903. void  CBTCT::Compose_Inquiry_HCI_Command( BT_HCI_COMMAND &hci_cmd )
  904. {
  905.     // 33 8B 9E 30 0A
  906.     hci_cmd.m_opcode = 0x0401;
  907.     hci_cmd.m_len    = 5;
  908.     Reset_HCI_Command( hci_cmd );
  909.     hci_cmd.m_cmd[0] = 0x33;
  910.     hci_cmd.m_cmd[1] = 0x8B;
  911.     hci_cmd.m_cmd[2] = 0x9E;
  912.     hci_cmd.m_cmd[3] = 0x30;
  913.     hci_cmd.m_cmd[4] = 0x0A;
  914.     
  915. }
  916. //--------------------------------------------------------------------------
  917. bool  CBTCT::DeCompose_Inquiry_HCI_Event( BT_HCI_EVENT hci_event )
  918. {
  919.     if( 0x02 == hci_event.m_event )
  920.     {
  921.         int num_of_response = hci_event.m_parms[0];
  922.         for( int i=0; i<num_of_response; i++ )
  923.         {
  924.             S_BT_CT_RES res;
  925.             res.e_acl_status = BT_ACL_STATUS_NONE;
  926.             for( int j=0; j<BD_ADDRESS_LEN; j++ )
  927.             {
  928.                 res.s_bd_addr.uc_BD_Addr[j] = hci_event.m_parms[i*14+j+1];
  929.             }
  930.             vector<S_BT_CT_RES>:: iterator iter;
  931.             bool match = false;
  932.             for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  933.             {
  934.                 match = true;
  935.                 for( int i=0; i<BD_ADDRESS_LEN; i++ )
  936.                 {
  937.                     if( iter->s_bd_addr.uc_BD_Addr[i] != res.s_bd_addr.uc_BD_Addr[i] )
  938.                     {
  939.                         match = false;
  940.                         break;
  941.                     }
  942.                 }
  943.                 if( match )
  944.                    break;
  945.             }
  946.             if( ! match )
  947.             {
  948.                 res.e_acl_status = BT_ACL_STATUS_NONE;
  949.                 res.e_sco_status = BT_CT_SCO_STATUS_NONE;
  950.                 res.uc_h1_acl    = 0xFF;
  951.                 res.uc_h2_acl    = 0xFF;
  952.                 res.uc_h1_sco    = 0xFF;
  953.                 res.uc_h2_sco    = 0xFF;
  954.                 m_pvCTRes->push_back( res );
  955.                 char str[3];
  956.                 AnsiString as_bd_addr;
  957.                 for( int j=BD_ADDRESS_LEN-1; j>=0; j-- )
  958.                 {
  959.                     if( res.s_bd_addr.uc_BD_Addr[j] <= 0x0F )
  960.                         sprintf( str, "0%X", res.s_bd_addr.uc_BD_Addr[j] );
  961.                     else
  962.                         sprintf( str, "%X", res.s_bd_addr.uc_BD_Addr[j] );
  963.                     AnsiString as_str(str);
  964.                     if( j!=0 ) as_str+= ":";
  965.                         as_bd_addr+= as_str;
  966.                 }
  967.                 AddBDAddressToDiscovedList( as_bd_addr );
  968.             }
  969.         }
  970.     }
  971.     return true;
  972. }
  973. //---------------------------------------------------------------------------
  974. void  CBTCT::AddBDAddressToDiscovedList( AnsiString as_bd_addr )
  975. {
  976.     m_sBTCT.list->Add( as_bd_addr );
  977. }
  978. //===========================================================================
  979. ///////////////////////////        Inquiry Cancel          //////////////////
  980. //===========================================================================
  981. void  CBTCT::REQ_Inquiry_Cancel_Start( S_BT_CT bt_ct )
  982. {
  983.     gs_bt_ct_ptr  = this;
  984.     g_bIsRunning  = true;
  985.     m_sBTCT = bt_ct;
  986.     //ActiveMan->SetActiveFunction2( ::REQ_Send_Inquiry_Cancel_HCI_command );
  987.     ActiveMan->SetActiveFunction(::REQ_Send_Inquiry_Cancel_HCI_command);
  988. }
  989. //---------------------------------------------------------------------------
  990. void  CBTCT::REQ_Send_Inquiry_Cancel_HCI_command( void )
  991. {
  992.     if(!g_bIsRunning)  return;
  993.     m_bEventOk = false;
  994.     BT_HCI_COMMAND hci_cmd;
  995.     Compose_Inquiry_Cancel_HCI_Command( hci_cmd );
  996.     AddTxLogToStringList( hci_cmd );
  997.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 10000, &hci_cmd, ::CNF_InquiryCancelHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  998.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  999.     {
  1000.         if( META_TIMEOUT == MetaResult )
  1001.         {
  1002.             Confirm( METAAPP_TIMEOUT );
  1003.             return;
  1004.         }
  1005.         else if( META_CANCEL == MetaResult )
  1006.         {
  1007.             Confirm( METAAPP_CANCEL );
  1008.             return;
  1009.         }
  1010.         else
  1011.         {
  1012.             Confirm( METAAPP_FAIL );
  1013.             return;
  1014.         }
  1015.     }
  1016.     //ActiveMan->SetActiveFunction2( ::REQ_Finish );
  1017.     Confirm(METAAPP_SUCCESS);
  1018. }
  1019. //---------------------------------------------------------------------------
  1020. void __stdcall CBTCT::CNF_InquiryCancelHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1021. {
  1022.     if(!g_bIsRunning)  return;
  1023.     if( 0xFF == cnf->m_event )  return; // LMP
  1024.     m_uiEventCount++;
  1025.     AddRxLogToStringList( *cnf );
  1026.     m_bEventOk = DeCompose_Inquiry_Cancel_HCI_Event( *cnf );
  1027. }
  1028. //--------------------------------------------------------------------------
  1029. void  CBTCT::Compose_Inquiry_Cancel_HCI_Command( BT_HCI_COMMAND &hci_cmd )
  1030. {
  1031.     // 33 8B 9E 30 0A
  1032.     hci_cmd.m_opcode = 0x0402;
  1033.     hci_cmd.m_len    = 0;
  1034.     Reset_HCI_Command( hci_cmd );
  1035.     
  1036. }
  1037. //--------------------------------------------------------------------------
  1038. bool  CBTCT::DeCompose_Inquiry_Cancel_HCI_Event( BT_HCI_EVENT hci_event )
  1039. {
  1040.     if(
  1041.         0x00 != hci_event.m_parms[3]
  1042.        )
  1043.      {  return false;
  1044.      }
  1045.     return true;
  1046. }
  1047. //===========================================================================
  1048. ///////////////////////////        Connect           ////////////////////////
  1049. //===========================================================================
  1050. void  CBTCT::REQ_ConnectACL_Start( S_BT_CT bt_ct )
  1051. {
  1052.     gs_bt_ct_ptr  = this;
  1053.     g_bIsRunning  = true;
  1054.     m_sBTCT = bt_ct;
  1055.     ActiveMan->SetActiveFunction( ::REQ_Send_ConnectACL_HCI_command );
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. void  CBTCT::REQ_Send_ConnectACL_HCI_command( void )
  1059. {
  1060.     if(!g_bIsRunning)  return;
  1061.     m_bEventOk = false;
  1062.     BT_HCI_COMMAND hci_cmd;
  1063.     //m_uiEventCount = 0;
  1064.     Compose_ConnectACL_HCI_Command( hci_cmd, m_sConnectACL );
  1065.     AddTxLogToStringList( hci_cmd );
  1066.     META_RESULT MetaResult = META_BT_RegisterAutoCallback_r( m_META_HANDLE_Obj.Get_MainHandle(), ::CNF_DisconnectACLAutoHCIEvent );
  1067.     MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 40000, &hci_cmd, ::CNF_ConnectACLHCIEvent, NULL, 0x1D );
  1068.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  1069.     {
  1070.         if( META_TIMEOUT == MetaResult )
  1071.         {
  1072.             Confirm( METAAPP_TIMEOUT );
  1073.             return;
  1074.         }
  1075.         else if( META_CANCEL == MetaResult )
  1076.         {
  1077.             Confirm( METAAPP_CANCEL );
  1078.             return;
  1079.         }
  1080.         else
  1081.         {
  1082.             Confirm( METAAPP_FAIL );
  1083.             return;
  1084.         }
  1085.     }
  1086.     Confirm(METAAPP_SUCCESS);
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __stdcall CBTCT::CNF_ConnectACLHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1090. {
  1091.     if(!g_bIsRunning)  return;
  1092.     if( 0xFF == cnf->m_event )  return; // LMP
  1093.    // m_uiEventCount++;
  1094.     AddRxLogToStringList( *cnf );
  1095.     m_bEventOk = DeCompose_ConnectACL_HCI_Event( *cnf );
  1096. }
  1097. //--------------------------------------------------------------------------
  1098. void __stdcall CBTCT::CNF_DisconnectACLAutoHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1099. {
  1100.     AddRxLogToStringList( *cnf );
  1101.     if( (0x05 == cnf->m_event) && (0x00 == cnf->m_parms[0]) )
  1102.     {
  1103.         vector<S_BT_CT_RES>:: iterator iter;
  1104.         bool match;
  1105.         for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1106.         {
  1107.             match = true;
  1108.             if( iter->uc_h1_acl != cnf->m_parms[1] ||
  1109.                 iter->uc_h2_acl != cnf->m_parms[2]
  1110.               )
  1111.             {
  1112.                     match = false;
  1113.                     break;
  1114.             }
  1115.             if( match )
  1116.             {
  1117.                 iter->e_acl_status = BT_ACL_STATUS_NONE;
  1118.                 break;
  1119.             }
  1120.         }
  1121.     }
  1122.     if (NULL != m_sBTCT.log)
  1123.     {
  1124.         PostMessage(m_sBTCT.hPostMsgDestHandle,
  1125.                     WM_ML_BT_DISCONNECT_MSG,
  1126.                     PAGEIDX_BT_CT,
  1127.                     0
  1128.                     );
  1129.     }
  1130. }
  1131. //--------------------------------------------------------------------------
  1132. void  CBTCT::Compose_ConnectACL_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_CONNECT_ACL connect )
  1133. {
  1134.     // 33 8B 9E 30 0A
  1135.     hci_cmd.m_opcode = 0x0405;
  1136.     hci_cmd.m_len    = 13;
  1137.     Reset_HCI_Command( hci_cmd );
  1138.     for(int i=0; i<BD_ADDRESS_LEN; i++ )
  1139.         hci_cmd.m_cmd[i] = connect.s_bd_addr.uc_BD_Addr[i];
  1140.     hci_cmd.m_cmd[6] = connect.us_acl_pkt_type & 0x00FF;
  1141.     hci_cmd.m_cmd[7] = (connect.us_acl_pkt_type & 0xFF00) >> 8;
  1142.     hci_cmd.m_cmd[8] = 0x01;
  1143.     hci_cmd.m_cmd[9] = 0x00;
  1144.     hci_cmd.m_cmd[10]= 0x00;
  1145.     hci_cmd.m_cmd[11]= 0x00;
  1146.     hci_cmd.m_cmd[12]= 0x01;
  1147. }
  1148. //--------------------------------------------------------------------------
  1149. bool  CBTCT::DeCompose_ConnectACL_HCI_Event( BT_HCI_EVENT hci_event )
  1150. {
  1151.     if( 0x03 == hci_event.m_event )
  1152.     {
  1153.         S_BT_CT_RES ct_res;
  1154.         // status
  1155.         ct_res.e_acl_status = (E_BT_ACL_STATUS) hci_event.m_parms[0];
  1156.         if( (ct_res.e_acl_status != BT_ACL_STATUS_SUCCESS) &&
  1157.             (ct_res.e_acl_status != BT_ACL_STATUS_PAGE_TIMEOUT)
  1158.           )
  1159.         {   ct_res.e_acl_status = BT_ACL_STATUS_FAIL;
  1160.         }
  1161.         // connection handle
  1162.         ct_res.uc_h1_acl = hci_event.m_parms[1];
  1163.         ct_res.uc_h2_acl = hci_event.m_parms[2];
  1164.         // bd address
  1165.         for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1166.         {
  1167.             ct_res.s_bd_addr.uc_BD_Addr[i] = hci_event.m_parms[3+i];
  1168.         }
  1169.         vector<S_BT_CT_RES>:: iterator iter;
  1170.         for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1171.         {
  1172.             bool match = true;
  1173.             for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1174.             {
  1175.                 if( iter->s_bd_addr.uc_BD_Addr[i] != ct_res.s_bd_addr.uc_BD_Addr[i] )
  1176.                 {
  1177.                     match = false;
  1178.                     break;
  1179.                 }
  1180.             }
  1181.             if( match )
  1182.             {
  1183.                 *iter = ct_res;
  1184.                 break;
  1185.             }
  1186.         }
  1187.     }
  1188.     else if( 0x17 == hci_event.m_event )
  1189.     {
  1190. #if 0
  1191.         BT_HCI_COMMAND hci_cmd;
  1192.         Compose_Link_Key_Negative_Reply_HCI_Command( hci_cmd, m_sConnectACL );
  1193.         AddTxLogToStringList( hci_cmd );
  1194.         m_bEvent2Ok = false;
  1195.         META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_LinkKeyNegReplyHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  1196.         if( (META_SUCCESS != MetaResult) || (!m_bEvent2Ok) )
  1197.         {
  1198.             if( META_TIMEOUT == MetaResult )
  1199.             {
  1200.                 Confirm( METAAPP_TIMEOUT );
  1201.                 return false;
  1202.             }
  1203.             else if( META_CANCEL == MetaResult )
  1204.             {
  1205.                 Confirm( METAAPP_CANCEL );
  1206.                 return false;
  1207.             }
  1208.             else
  1209.             {
  1210.                 Confirm( METAAPP_FAIL );
  1211.                 return false;
  1212.             }
  1213.         }
  1214. #endif
  1215.     }
  1216.     else if( 0x16 == hci_event.m_event )
  1217.     {
  1218.         if (NULL != m_sBTCT.log)
  1219.         {
  1220.             PostMessage(m_sBTCT.hPostMsgDestHandle,
  1221.                         WM_ML_BT_PIN_MSG,
  1222.                         PAGEIDX_BT_CT,
  1223.                         0
  1224.                        );
  1225.         }               
  1226.     }
  1227.     return true;
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void __stdcall CBTCT::CNF_LinkKeyNegReplyHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1231. {
  1232.     if(!g_bIsRunning)  return;
  1233.     if( 0xFF == cnf->m_event )  return; // LMP
  1234.    // m_uiEventCount++;
  1235.     AddRxLogToStringList( *cnf );
  1236.     m_bEvent2Ok = DeCompose_Link_Key_Negative_Reply_HCI_Event( *cnf, m_sConnectACL );
  1237. }
  1238. //--------------------------------------------------------------------------
  1239. void  CBTCT::Compose_Link_Key_Negative_Reply_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_CONNECT_ACL connect )
  1240. {
  1241.     // 33 8B 9E 30 0A
  1242.     hci_cmd.m_opcode = 0x040C;
  1243.     hci_cmd.m_len    = 6;
  1244.     Reset_HCI_Command( hci_cmd );
  1245.     for(int i=0; i<BD_ADDRESS_LEN; i++ )
  1246.         hci_cmd.m_cmd[i] = connect.s_bd_addr.uc_BD_Addr[i];
  1247.     
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. bool CBTCT::DeCompose_Link_Key_Negative_Reply_HCI_Event( BT_HCI_EVENT hci_event, S_BT_CONNECT_ACL connect )
  1251. {
  1252.     if( 0x00 != hci_event.m_parms[3] )
  1253.      {  return false;
  1254.      }
  1255.     for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1256.     {
  1257.         if( connect.s_bd_addr.uc_BD_Addr[i] != hci_event.m_parms[4+i] )
  1258.             return false;
  1259.     }        
  1260.     return true;
  1261. }
  1262. //===========================================================================
  1263. ///////////////////////////  Change ACL packet type  ////////////////////////
  1264. //===========================================================================
  1265. void  CBTCT::REQ_Change_ACL_Packet_Type_Start( S_BT_CT bt_ct )
  1266. {
  1267.     gs_bt_ct_ptr  = this;
  1268.     g_bIsRunning  = true;
  1269.     m_sBTCT = bt_ct;
  1270.     ActiveMan->SetActiveFunction( ::REQ_Send_Change_ACL_Packet_Type_HCI_command );
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void  CBTCT::REQ_Send_Change_ACL_Packet_Type_HCI_command( void )
  1274. {
  1275.     if(!g_bIsRunning)  return;
  1276.     m_bEventOk = false;
  1277.     BT_HCI_COMMAND hci_cmd;
  1278.     //m_uiEventCount = 0;
  1279.     Compose_Change_ACL_Packet_Type_HCI_Command( hci_cmd, m_sConnectACL );
  1280.     AddTxLogToStringList( hci_cmd );
  1281.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 10000, &hci_cmd, ::CNF_ChangeACLPacketTypeHCIEvent, NULL, 0x1D );
  1282.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  1283.     {
  1284.         if( META_TIMEOUT == MetaResult )
  1285.         {
  1286.             Confirm( METAAPP_TIMEOUT );
  1287.             return;
  1288.         }
  1289.         else if( META_CANCEL == MetaResult )
  1290.         {
  1291.             Confirm( METAAPP_CANCEL );
  1292.             return;
  1293.         }
  1294.         else
  1295.         {
  1296.             Confirm( METAAPP_FAIL );
  1297.             return;
  1298.         }
  1299.     }
  1300.     Confirm(METAAPP_SUCCESS);
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. void __stdcall CBTCT::CNF_ChangeACLPacketTypeHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1304. {
  1305.     if(!g_bIsRunning)  return;
  1306.     if( 0xFF == cnf->m_event )  return; // LMP
  1307.    // m_uiEventCount++;
  1308.     AddRxLogToStringList( *cnf );
  1309.     m_bEventOk = DeCompose_Change_ACL_Packet_Type_HCI_Event( *cnf );
  1310. }
  1311. //--------------------------------------------------------------------------
  1312. void  CBTCT::Compose_Change_ACL_Packet_Type_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_CONNECT_ACL connect )
  1313. {
  1314.     hci_cmd.m_opcode = 0x040F;
  1315.     hci_cmd.m_len    = 4;
  1316.     Reset_HCI_Command( hci_cmd );
  1317.     vector<S_BT_CT_RES>:: iterator iter;
  1318.     for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1319.     {
  1320.         bool match = true;
  1321.         for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1322.         {
  1323.             if( iter->s_bd_addr.uc_BD_Addr[i] != connect.s_bd_addr.uc_BD_Addr[i] )
  1324.             {
  1325.                 match = false;
  1326.                 break;
  1327.             }
  1328.         }
  1329.         if( match )
  1330.         {
  1331.             break;
  1332.         }
  1333.     }
  1334.     hci_cmd.m_cmd[0] = iter->uc_h1_acl;
  1335.     hci_cmd.m_cmd[1] = iter->uc_h2_acl;
  1336.     hci_cmd.m_cmd[2] = connect.us_acl_pkt_type & 0x00FF;
  1337.     hci_cmd.m_cmd[3] = (connect.us_acl_pkt_type & 0xFF00) >> 8;
  1338. }
  1339. //--------------------------------------------------------------------------
  1340. bool  CBTCT::DeCompose_Change_ACL_Packet_Type_HCI_Event( BT_HCI_EVENT hci_event )
  1341. {
  1342.     return true;
  1343. }
  1344. //===========================================================================
  1345. ///////////////////////////  Enter PIN  ////////////////////////
  1346. //===========================================================================
  1347. bool  CBTCT::REQ_EnterPIN_Start( S_BT_CT bt_ct )
  1348. {
  1349.     gs_bt_ct_ptr  = this;
  1350.     g_bIsRunning  = true;
  1351.     m_sBTCT = bt_ct;
  1352.     if( REQ_Send_EnterPIN_HCI_command() ) return false;
  1353.     return true;
  1354. }
  1355. //---------------------------------------------------------------------------
  1356. bool  CBTCT::REQ_Send_EnterPIN_HCI_command( void )
  1357. {
  1358.     if(!g_bIsRunning)  return false;
  1359.     m_bEventOk = false;
  1360.     BT_HCI_COMMAND hci_cmd;
  1361.     //m_uiEventCount = 0;
  1362.     Compose_EnterPIN_HCI_Command( hci_cmd, m_sEnterPIN );
  1363.     AddTxLogToStringList( hci_cmd );
  1364.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_EnterPINHCIEvent, NULL, HCE_COMMAND_COMPLETE );
  1365.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  1366.     {
  1367.         return false;
  1368.     }
  1369.     return true;
  1370. }
  1371. //---------------------------------------------------------------------------
  1372. void __stdcall CBTCT::CNF_EnterPINHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1373. {
  1374.     if(!g_bIsRunning)  return;
  1375.     if( 0xFF == cnf->m_event )  return; // LMP
  1376.    // m_uiEventCount++;
  1377.     AddRxLogToStringList( *cnf );
  1378.     m_bEventOk = DeCompose_EnterPIN_HCI_Event( *cnf );
  1379. }
  1380. //--------------------------------------------------------------------------
  1381. void  CBTCT::Compose_EnterPIN_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_ENTER_PIN enter_pin )
  1382. {
  1383.     hci_cmd.m_opcode = 0x040D;
  1384.     hci_cmd.m_len    = 23;
  1385.     Reset_HCI_Command( hci_cmd );
  1386.     for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1387.     {
  1388.         hci_cmd.m_cmd[i] = enter_pin.s_bd_addr.uc_BD_Addr[i];
  1389.     }
  1390.     hci_cmd.m_cmd[6] = enter_pin.s_bt_pin.uc_pin_len;
  1391.     for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1392.     {
  1393.         hci_cmd.m_cmd[7+i] = enter_pin.s_bt_pin.uc_PIN[i];
  1394.     }
  1395.     
  1396. }
  1397. //--------------------------------------------------------------------------
  1398. bool  CBTCT::DeCompose_EnterPIN_HCI_Event( BT_HCI_EVENT hci_event )
  1399. {
  1400.     if(
  1401.         0x00 != hci_event.m_parms[3]
  1402.        )
  1403.      {  return false;
  1404.      }
  1405.     return true;
  1406. }
  1407. //===========================================================================
  1408. ///////////////////////////      Disonnect  ACL      ////////////////////////
  1409. //===========================================================================
  1410. void  CBTCT::REQ_DisconnectACL_Start( S_BT_CT bt_ct )
  1411. {
  1412.     gs_bt_ct_ptr  = this;
  1413.     g_bIsRunning  = true;
  1414.     m_sBTCT = bt_ct;
  1415.     ActiveMan->SetActiveFunction( ::REQ_Send_DisconnectACL_HCI_command );
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. void  CBTCT::REQ_Send_DisconnectACL_HCI_command( void )
  1419. {
  1420.     if(!g_bIsRunning)  return;
  1421.     m_bEventOk = false;
  1422.     BT_HCI_COMMAND hci_cmd;
  1423.     //m_uiEventCount = 0;
  1424.     Compose_DisconnectACL_HCI_Command( hci_cmd, m_sDisconnectACL );
  1425.     AddTxLogToStringList( hci_cmd );
  1426.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 10000, &hci_cmd, ::CNF_DisconnectACLHCIEvent, NULL, 0x05 );
  1427.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  1428.     {
  1429.         if( META_TIMEOUT == MetaResult )
  1430.         {
  1431.             Confirm( METAAPP_TIMEOUT );
  1432.             return;
  1433.         }
  1434.         else if( META_CANCEL == MetaResult )
  1435.         {
  1436.             Confirm( METAAPP_CANCEL );
  1437.             return;
  1438.         }
  1439.         else
  1440.         {
  1441.             Confirm( METAAPP_FAIL );
  1442.             return;
  1443.         }
  1444.     }
  1445.     Confirm(METAAPP_SUCCESS);
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. void __stdcall CBTCT::CNF_DisconnectACLHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1449. {
  1450.     if(!g_bIsRunning)  return;
  1451.     if( 0xFF == cnf->m_event )  return; // LMP
  1452.    // m_uiEventCount++;
  1453.     AddRxLogToStringList( *cnf );
  1454.     m_bEventOk = DeCompose_DisconnectACL_HCI_Event( *cnf, m_sDisconnectACL );
  1455. }
  1456. //--------------------------------------------------------------------------
  1457. void  CBTCT::Compose_DisconnectACL_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_DISCONNECT_ACL disconnect )
  1458. {
  1459.     // 33 8B 9E 30 0A
  1460.     hci_cmd.m_opcode = 0x0406;
  1461.     hci_cmd.m_len    = 3;
  1462.     Reset_HCI_Command( hci_cmd );
  1463.     vector<S_BT_CT_RES>:: iterator iter;
  1464.     for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1465.     {
  1466.         bool match = true;
  1467.         for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1468.         {
  1469.             if( iter->s_bd_addr.uc_BD_Addr[i] != disconnect.s_bd_addr.uc_BD_Addr[i] )
  1470.             {
  1471.                 match = false;
  1472.                 break;
  1473.             }
  1474.         }
  1475.         if( match )
  1476.         {
  1477.             break;
  1478.         }
  1479.     }
  1480.     hci_cmd.m_cmd[0] = iter->uc_h1_acl;
  1481.     hci_cmd.m_cmd[1] = iter->uc_h2_acl;
  1482.     hci_cmd.m_cmd[2] = 0x13;
  1483. }
  1484. //--------------------------------------------------------------------------
  1485. bool  CBTCT::DeCompose_DisconnectACL_HCI_Event( BT_HCI_EVENT hci_event, S_BT_DISCONNECT_ACL disconnect )
  1486. {
  1487.     if( (0x05 == hci_event.m_event) && (0x00 == hci_event.m_parms[0]) )
  1488.     {
  1489.         vector<S_BT_CT_RES>:: iterator iter;
  1490.         for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1491.         {
  1492.             bool match = true;
  1493.             for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1494.             {
  1495.                 if( iter->s_bd_addr.uc_BD_Addr[i] != disconnect.s_bd_addr.uc_BD_Addr[i] )
  1496.                 {
  1497.                     match = false;
  1498.                     break;
  1499.                 }
  1500.             }
  1501.             if( match )
  1502.             {
  1503.                 iter->e_acl_status = BT_ACL_STATUS_NONE;
  1504.                 break;
  1505.             }
  1506.         }
  1507.     }
  1508.     return true;
  1509. }
  1510. //===========================================================================
  1511. ///////////////////////////        Connect  SCO      ////////////////////////
  1512. //===========================================================================
  1513. void  CBTCT::REQ_ConnectSCO_Start( S_BT_CT bt_ct )
  1514. {
  1515.     gs_bt_ct_ptr  = this;
  1516.     g_bIsRunning  = true;
  1517.     m_sBTCT = bt_ct;
  1518.     ActiveMan->SetActiveFunction( ::REQ_Send_ConnectSCO_HCI_command );
  1519. }
  1520. //---------------------------------------------------------------------------
  1521. void  CBTCT::REQ_Send_ConnectSCO_HCI_command( void )
  1522. {
  1523.     if(!g_bIsRunning)  return;
  1524.     m_bEventOk = false;
  1525.     BT_HCI_COMMAND hci_cmd;
  1526.     //m_uiEventCount = 0;
  1527.     Compose_ConnectSCO_HCI_Command( hci_cmd, m_sConnectSCO );
  1528.     AddTxLogToStringList( hci_cmd );
  1529.     META_RESULT MetaResult = META_BT_SendHCICommand_r( m_META_HANDLE_Obj.Get_MainHandle(), 10000, &hci_cmd, ::CNF_ConnectSCOHCIEvent, NULL, 0x2C );
  1530.     if( (META_SUCCESS != MetaResult) || (!m_bEventOk) )
  1531.     {
  1532.         if( META_TIMEOUT == MetaResult )
  1533.         {
  1534.             Confirm( METAAPP_TIMEOUT );
  1535.             return;
  1536.         }
  1537.         else if( META_CANCEL == MetaResult )
  1538.         {
  1539.             Confirm( METAAPP_CANCEL );
  1540.             return;
  1541.         }
  1542.         else
  1543.         {
  1544.             Confirm( METAAPP_FAIL );
  1545.             return;
  1546.         }
  1547.     }
  1548.     Confirm(METAAPP_SUCCESS);
  1549. }
  1550. //---------------------------------------------------------------------------
  1551. void __stdcall CBTCT::CNF_ConnectSCOHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1552. {
  1553.     if(!g_bIsRunning)  return;
  1554.     if( 0xFF == cnf->m_event )  return; // LMP
  1555.    // m_uiEventCount++;
  1556.     AddRxLogToStringList( *cnf );
  1557.     m_bEventOk = DeCompose_ConnectSCO_HCI_Event( *cnf, m_sConnectSCO );
  1558. }
  1559. //--------------------------------------------------------------------------
  1560. void  CBTCT::Compose_ConnectSCO_HCI_Command(BT_HCI_COMMAND &hci_cmd, S_BT_CT_CONNECT_SCO connect)
  1561. {
  1562.     hci_cmd.m_opcode = 0x0428;
  1563.     hci_cmd.m_len    = 17;
  1564.     Reset_HCI_Command( hci_cmd );
  1565.     vector<S_BT_CT_RES>:: iterator iter;
  1566.     for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1567.     {
  1568.         bool match = true;
  1569.         for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1570.         {
  1571.             if( iter->s_bd_addr.uc_BD_Addr[i] != connect.s_bd_addr.uc_BD_Addr[i] )
  1572.             {
  1573.                 match = false;
  1574.                 break;
  1575.             }
  1576.         }
  1577.         if( match )
  1578.         {
  1579.             break;
  1580.         }
  1581.     }
  1582.     hci_cmd.m_cmd[0] = iter->uc_h1_acl;
  1583.     hci_cmd.m_cmd[1] = iter->uc_h2_acl;
  1584.     hci_cmd.m_cmd[2] = 0x40; // @4 Transmit_Bandwidth
  1585.     hci_cmd.m_cmd[3] = 0x1F;
  1586.     hci_cmd.m_cmd[4] = 0x00;
  1587.     hci_cmd.m_cmd[5] = 0x00;
  1588.     hci_cmd.m_cmd[6] = 0x40; // @4 Receive_Bandwidth
  1589.     hci_cmd.m_cmd[7] = 0x1F;
  1590.     hci_cmd.m_cmd[8] = 0x00;
  1591.     hci_cmd.m_cmd[9] = 0x00;
  1592.     hci_cmd.m_cmd[10]= 0x05; // @2 Max_Latency
  1593.     hci_cmd.m_cmd[11]= 0x00;
  1594.     hci_cmd.m_cmd[12]= 0xAC; // @2 Voice_Setting
  1595.     hci_cmd.m_cmd[13]= 0x00;
  1596.     hci_cmd.m_cmd[14]= 0x00; // @1 Retransmission_Effort
  1597.     hci_cmd.m_cmd[15]=  connect.e_pkt_type & 0x00FF; // @2 Packet_Type
  1598.     hci_cmd.m_cmd[16]= (connect.e_pkt_type & 0xFF00) >> 8;
  1599. }
  1600. //--------------------------------------------------------------------------
  1601. bool  CBTCT::DeCompose_ConnectSCO_HCI_Event(BT_HCI_EVENT hci_event, S_BT_CT_CONNECT_SCO connect)
  1602. {
  1603.     if( 0x2C == hci_event.m_event )
  1604.     {
  1605.         vector<S_BT_CT_RES>:: iterator iter;
  1606.         for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1607.         {
  1608.             bool match = true;
  1609.             for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1610.             {
  1611.                 if( iter->s_bd_addr.uc_BD_Addr[i] != connect.s_bd_addr.uc_BD_Addr[i] )
  1612.                 {
  1613.                     match = false;
  1614.                     break;
  1615.                 }
  1616.             }
  1617.             if( match )
  1618.             {
  1619.                 break;
  1620.             }
  1621.         }
  1622.         if( hci_event.m_parms[0] != 0   )
  1623.         {
  1624.             iter->e_sco_status = BT_CT_SCO_STATUS_FAIL;
  1625.             return false;
  1626.         }
  1627.         iter->e_sco_status = BT_CT_SCO_STATUS_SUCCESS;
  1628.         iter->uc_h1_sco    = hci_event.m_parms[1];
  1629.         iter->uc_h2_sco    = hci_event.m_parms[2];
  1630.     }
  1631.     return true;
  1632. }
  1633. //===========================================================================
  1634. ///////////////////////////      Disonnect  SCO      ////////////////////////
  1635. //===========================================================================
  1636. void  CBTCT::REQ_DisconnectSCO_Start(S_BT_CT bt_ct)
  1637. {
  1638.     gs_bt_ct_ptr  = this;
  1639.     g_bIsRunning  = true;
  1640.     m_sBTCT = bt_ct;
  1641.     ActiveMan->SetActiveFunction(::REQ_Send_DisconnectSCO_HCI_command);
  1642. }
  1643. //---------------------------------------------------------------------------
  1644. void  CBTCT::REQ_Send_DisconnectSCO_HCI_command(void)
  1645. {
  1646.     if(!g_bIsRunning)  return;
  1647.     m_bEventOk = false;
  1648.     BT_HCI_COMMAND hci_cmd;
  1649.     //m_uiEventCount = 0;
  1650.     Compose_DisconnectSCO_HCI_Command(hci_cmd, m_sDisconnectSCO);
  1651.     AddTxLogToStringList(hci_cmd);
  1652.     META_RESULT MetaResult = META_BT_SendHCICommand_r(m_META_HANDLE_Obj.Get_MainHandle(), 5000, &hci_cmd, ::CNF_DisconnectSCOHCIEvent, NULL, 0x05 );
  1653.     if ((META_SUCCESS != MetaResult) || (!m_bEventOk))
  1654.     {
  1655.         if (META_TIMEOUT == MetaResult)
  1656.         {
  1657.             Confirm(METAAPP_TIMEOUT);
  1658.         }
  1659.         else if (META_CANCEL == MetaResult)
  1660.         {
  1661.             Confirm(METAAPP_CANCEL);
  1662.         }
  1663.         else
  1664.         {
  1665.             Confirm(METAAPP_FAIL);
  1666.         }
  1667.         return;
  1668.     }
  1669.     Confirm(METAAPP_SUCCESS);
  1670. }
  1671. //---------------------------------------------------------------------------
  1672. void __stdcall CBTCT::CNF_DisconnectSCOHCIEvent(const BT_HCI_EVENT *cnf, const short token, void *usrData)
  1673. {
  1674.     if (!g_bIsRunning)
  1675.     {
  1676.         return;
  1677.     }
  1678.     if (0xFF == cnf->m_event) // LMP
  1679.     {
  1680.         return;
  1681.     }
  1682.    // m_uiEventCount++;
  1683.     AddRxLogToStringList( *cnf );
  1684.     m_bEventOk = DeCompose_DisconnectSCO_HCI_Event( *cnf, m_sDisconnectSCO );
  1685. }
  1686. //--------------------------------------------------------------------------
  1687. void  CBTCT::Compose_DisconnectSCO_HCI_Command( BT_HCI_COMMAND &hci_cmd, S_BT_CT_DISCONNECT_SCO disconnect )
  1688. {
  1689.     // 33 8B 9E 30 0A
  1690.     hci_cmd.m_opcode = 0x0406;
  1691.     hci_cmd.m_len    = 3;
  1692.     Reset_HCI_Command( hci_cmd );
  1693.     vector<S_BT_CT_RES>:: iterator iter;
  1694.     for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1695.     {
  1696.         bool match = true;
  1697.         for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1698.         {
  1699.             if( iter->s_bd_addr.uc_BD_Addr[i] != disconnect.s_bd_addr.uc_BD_Addr[i] )
  1700.             {
  1701.                 match = false;
  1702.                 break;
  1703.             }
  1704.         }
  1705.         if( match )
  1706.         {
  1707.             iter->e_sco_status = BT_CT_SCO_STATUS_NONE;
  1708.             break;
  1709.         }
  1710.     }
  1711.     hci_cmd.m_cmd[0] = iter->uc_h1_sco;
  1712.     hci_cmd.m_cmd[1] = iter->uc_h2_sco;
  1713.     hci_cmd.m_cmd[2] = 0x13;
  1714. }
  1715. //--------------------------------------------------------------------------
  1716. bool  CBTCT::DeCompose_DisconnectSCO_HCI_Event(BT_HCI_EVENT hci_event, S_BT_CT_DISCONNECT_SCO disconnect)
  1717. {
  1718.    // if( (0x05 == hci_event.m_event) && (0x00 == hci_event.m_parms[0]) )
  1719.     {
  1720.         vector<S_BT_CT_RES>:: iterator iter;
  1721.         for( iter = m_pvCTRes->begin(); iter != m_pvCTRes->end(); iter++ )
  1722.         {
  1723.             bool match = true;
  1724.             for( int i=0; i<BD_ADDRESS_LEN; i++ )
  1725.             {
  1726.                 if( iter->s_bd_addr.uc_BD_Addr[i] != disconnect.s_bd_addr.uc_BD_Addr[i] )
  1727.                 {
  1728.                     match = false;
  1729.                     break;
  1730.                 }
  1731.             }
  1732.             if (match)
  1733.             {
  1734.                 iter->e_sco_status = BT_CT_SCO_STATUS_NONE;
  1735.                 break;
  1736.             }
  1737.         }
  1738.     }
  1739.     return true;
  1740. }
  1741. //===========================================================================
  1742. ////////////////////////////   Log  information   ////////////////////////
  1743. //===========================================================================
  1744. void  CBTCT::AddTxLogToStringList(BT_HCI_COMMAND hci_cmd)
  1745. {
  1746.     if (NULL == m_sBTCT.log)
  1747.     {
  1748.         return;
  1749.     }
  1750.     char        str[1024];
  1751.     AnsiString as_log;
  1752.     sprintf( str, "%X,", hci_cmd.m_opcode & 0x00FF );
  1753.     AnsiString as_opcode_low(str);
  1754.     sprintf( str, "%X,", (hci_cmd.m_opcode & 0xFF00)>>8 );
  1755.     AnsiString as_opcode_high(str);
  1756.     Array_To_Hex_String_UnsignedChar( str, hci_cmd.m_cmd, hci_cmd.m_len, ',' );
  1757.     AnsiString as_cmd( (const char*)str );
  1758.     if( 0 == hci_cmd.m_len )
  1759.        as_cmd = "";
  1760.     sprintf( str, "%X,", hci_cmd.m_len );
  1761.     AnsiString as_length(str);
  1762.     as_log = "[" + TimeToStr(Time()) + "] TX: " + as_opcode_low + as_opcode_high + as_length  + as_cmd;
  1763.     m_sBTCT.log->Add( as_log );
  1764.     PostMessage(
  1765.                          m_sBTCT.hPostMsgDestHandle,
  1766.                          WM_ML_BT_PRINT_MSG,
  1767.                          PAGEIDX_BT_CT,
  1768.                          0
  1769.                         );
  1770. }
  1771. //--------------------------------------------------------------------------
  1772. void  CBTCT::AddRxLogToStringList(BT_HCI_EVENT hci_event)
  1773. {
  1774.     if (NULL == m_sBTCT.log)
  1775.     {
  1776.         return;
  1777.     }
  1778.     unsigned char        str[1024];
  1779.     AnsiString as_log;
  1780.     sprintf( str, "%X,", hci_event.m_event );
  1781.     AnsiString as_event( (const char*) str );
  1782.     sprintf( str, "%X,", hci_event.m_len );
  1783.     AnsiString as_length( (const char*) str );
  1784.     Array_To_Hex_String_UnsignedChar( str, hci_event.m_parms, hci_event.m_len, ',' );
  1785.     AnsiString as_param( (const char*) str );
  1786.     if(  0 == hci_event.m_len )
  1787.         as_param = "";
  1788.     as_log = "[" + TimeToStr(Time()) + "] RX: " + as_event + as_length + as_param;
  1789.     m_sBTCT.log->Add( as_log );
  1790.     PostMessage(
  1791.                          m_sBTCT.hPostMsgDestHandle,
  1792.                          WM_ML_BT_PRINT_MSG,
  1793.                          PAGEIDX_BT_CT,
  1794.                          0
  1795.                         );
  1796. }
  1797. //===========================================================================
  1798. ////////////////////////////   Global  information   ////////////////////////
  1799. //===========================================================================
  1800. E_METAAPP_RESULT_T CBTCT::Get_ConfirmState(void)
  1801. {
  1802.     return  m_eConfirmState;
  1803. }
  1804. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1805. S_BD_ADDRS CBTCT::Get_LocalBdAddr( void )
  1806. {   return m_sLocalBdAddr;
  1807. }
  1808. //---------------------------------------------------------------------------
  1809. void CBTCT::Set_LocalBdAddr( S_BD_ADDRS bd_addr )
  1810. {   m_sLocalBdAddr = bd_addr;
  1811. }
  1812. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1813. void CBTCT::Set_ScanParameter( S_BT_SCAN scan )
  1814. {   m_sScan = scan; 
  1815. }
  1816. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1817. void CBTCT::Set_InquiryTimeout( unsigned int timeout )
  1818. {   m_uiInquiryTimeout = timeout;
  1819. }
  1820. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1821. vector<S_BT_CT_RES>*  CBTCT::Get_CtResVector( void )
  1822. {
  1823.     return m_pvCTRes;
  1824. }
  1825. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1826. void CBTCT::Set_EnterPINParameter( S_BT_ENTER_PIN enter_pin )
  1827. {   m_sEnterPIN = enter_pin;
  1828. }
  1829. //--------------------------------------------------------------------------
  1830. void CBTCT::Set_ConnectACLParameter( S_BT_CONNECT_ACL connect )
  1831. {   m_sConnectACL = connect;
  1832. }
  1833. //---------------------------------------------------------------------------
  1834. void CBTCT::Set_DisconnectACLParameter( S_BT_DISCONNECT_ACL disconnect )
  1835. {   m_sDisconnectACL = disconnect;
  1836. }
  1837. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1838. void CBTCT::Set_ConnectSCOParameter( S_BT_CT_CONNECT_SCO connect )
  1839. {   m_sConnectSCO = connect;
  1840. }
  1841. //---------------------------------------------------------------------------
  1842. void CBTCT::Set_DisconnectSCOParameter( S_BT_CT_DISCONNECT_SCO disconnect )
  1843. {   m_sDisconnectSCO = disconnect;
  1844. }
  1845. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~