Index: BT_doublebyte/sdk/Librarier/sdk/yc_bt2.h
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_bt2.h	(nonexistent)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_bt2.h	(working copy)
@@ -0,0 +1,658 @@
+/**
+  ******************************************************************************
+  * @file    yc_bt.h
+  * @author  Yichip
+  * @version V1.1
+  * @date    28-May-2020
+  * @brief   Yichip bluetooth encapsulation.The HCI protocol refers to YC Dual mode module spec-v4.15
+*
+  ******************************************************************************
+  */
+
+
+#ifndef __YC_BT2_H__
+#define __YC_BT2_H__
+
+#include "yc_uart.h"
+#include "yc_ipc2.h"
+
+#define HCI_BT_NUM 10
+#define HCT_DATA_LEN 800
+#define HCT_STATE_LEN 100
+
+#define HCI_CMD                           0x01
+#define HCI_EVENT                         0x02
+#define MAX_BLUETOOTH_DATA_LEN            255
+#define MAX_TCP_DATA_LEN                  235
+#define MAX_BIG_DATA_LEN                  1452
+
+
+
+/*BIT judge*/
+#define BIT_SET_BT_ADDR                   0
+#define BIT_SET_BLE_ADDR                  1
+#define BIT_SET_VISIBILITY                2
+#define BIT_SET_BT_NAME                   3
+#define BIT_SET_BLE_NAME                  4
+#define BIT_SEND_SPP_DATA                 5
+#define BIT_SEND_BLE_DATA                 6
+#define BIT_STATUS_REQUEST                7
+#define BIT_SET_PAIRING_MODE              8
+#define BIT_SET_PINCODE                   9
+#define BIT_GET_VERSION                   10
+#define BIT_BT_DISCONNECT                 11
+#define BIT_BLE_DISCONNECT                12
+#define BIT_SET_NVRAM                     13
+#define BIT_SET_TXPOWER                   14
+#define BIT_ENTER_SLEEP_MODE              15
+#define BIT_CONFIRM_GKEY                  16
+#define BIT_POWER_SET                     17
+#define BIT_GET_CREADIT_GIVEN             18
+#define BIT_PASSKEY_ENTRY                 19
+#define BIT_LE_SET_PARING                 20
+#define BIT_LE_SET_ADV_DATA               21
+#define BIT_LE_SET_SCAN_DATA              22
+#define BIT_LE_SEND_CONN_UPDATE           23
+#define BIT_LE_SET_ADV_PARM               24
+#define BIT_REJECT_JUSTWORK               25
+#define BIT_LE_SET_FIXED_PASSKEY          26
+#define BIT_SET_PSCAN_INTERVAL            27
+#define BIT_MODULE_READY                  28
+#define	BIT_SET_COD                       29
+#define BIT_SET_CREDIT                    30
+#define BIT_DELETE_SERVICE                31
+#define BIT_ADD_UUID                      32
+#define BIT_DNS_REQ                       33
+#define BIT_CONNECT_BNEP                  34
+#define BIT_CONNECT_TCP                   35
+#define BIT_BNEP_SEND_TCP_DATA            36
+#define BIT_DISCONNECT_TCP                37
+#define BIT_BNEP_SEND_UDP_DATA            38
+#define BIT_BNEP_SEND_TCP_BIG_DATA        39
+#define BIT_BNEP_SEND_TCP_BIG_DATA_FINISH 40
+#define BIT_BNEP_SEND_TCP_BIG_DATA_STATUS 41
+#define BIT_SET_LPM_MODE                  42
+/*CMD*/
+#define HCI_CMD_SET_BT_ADDR               0x00  //设置 BT3.0 地址
+#define HCI_CMD_SET_BLE_ADDR              0x01  //设置 BLE 地址
+#define HCI_CMD_SET_VISIBILITY            0x02  //设置可发现和广播
+#define HCI_CMD_SET_BT_NAME               0x03  //设置 BT3.0 名称
+#define HCI_CMD_SET_BLE_NAME              0x04  //设置 BLE 名称
+#define HCI_CMD_SEND_SPP_DATA             0x05  //发�?? BT3.0（SPP）数�?
+#define HCI_CMD_SEND_BLE_DATA             0x09  //发�?? BLE 数据
+#define HCI_CMD_STATUS_REQUEST            0x0B  //请求蓝牙状�??
+#define HCI_CMD_SET_PAIRING_MODE          0x0C  //设置配对模式
+#define HCI_CMD_SET_PINCODE               0x0D  //设置配对�?
+#define HCI_CMD_VERSION_REQUEST           0x10  //查询模块固件版本
+#define HCI_CMD_BT_DISCONNECT             0x11  //断开 BT3.0 连接
+#define HCI_CMD_BLE_DISCONNECT            0x12  //断开 BLE 连接
+#define HCI_CMD_SET_NVRAM                 0x26  //下发 NV 数据
+#define HCI_CMD_ENTER_SLEEP_MODE          0x27  //进入睡眠模式
+#define HCI_CMD_CONFIRM_GKEY              0x28  //Numeric Comparison 配对方式中对密钥的比较（见HCI_EVENT_GKEY注释�?
+#define HCI_CMD_GET_CREADIT_GIVEN         0x29
+#define HCI_CMD_PASSKEY_ENTRY             0x30  //passkey配对方式中对密钥的比�?
+#define HCI_CMD_LE_SET_PAIRING            0x33
+#define HCI_CMD_LE_SET_ADV_DATA           0x34
+#define HCI_CMD_LE_SET_SCAN_DATA          0x35
+#define HCI_CMD_LE_SEND_CONN_UPDATE_REQ   0x36
+#define HCI_CMD_LE_SET_ADV_PARM           0x37
+#define HCI_CMD_SET_TX_POWER              0x42
+#define HCI_CMD_REJECT_JUSTWORK           0x49
+#define HCI_CMD_LE_SET_FIXED_PASSKEY      0x61
+#define	HCI_CMD_DELETE_CUSTOMIZE_SERVICE  0x76
+#define	HCI_CMD_ADD_SERVICE_UUID          0x77
+#define	HCI_CMD_ADD_CHARACTERISTIC_UUID   0x78
+#define	HCI_CMD_SET_COD                   0x15  //设置 COD
+#define HCI_CMD_CONNECT_BNEP              0x7e //建立BNEP连接
+#define HCI_CMD_CONNECT_TCP               0x7f //建立TCP连接
+#define HCI_CMD_DISCONNECT_TCP            0x80 //断开TCP连接
+#define HCI_CMD_BNEP_SEND_TCP_DATA        0x81 //发�?�TCP数据
+#define HCI_CMD_DNS_REQ                   0x82 //域名解析指令
+#define HCI_CMD_SEND_UDP_DATA             0x84 //发�?�UDP数据
+#define HCI_CMD_SEND_BIG_DATA             0x85 //发�?�TCP大包数据
+#define HCI_CMD_SET_LPM_MODE              0xff //开关低功耗
+
+
+/*EVENT*/
+#define HCI_EVENT_BT_CONNECTED            0x00  /**BT3.0 连接建立*/
+#define HCI_EVENT_BLE_CONNECTED           0x02  /**BLE 连接建立*/
+#define HCI_EVENT_BT_DISCONNECTED         0x03  /**BT3.0 连接断开*/
+#define HCI_EVENT_BLE_DISCONNECTED        0x05  /**BLE 连接断开*/
+#define HCI_EVENT_CMD_COMPLETE            0x06  /**命令已完�?*/
+#define HCI_EVENT_SPP_DATA_RECEIVED       0x07  /**接收�? BT3.0 数据（SPP�?*/
+#define HCI_EVENT_BLE_DATA_RECEIVED       0x08  /**接收�? BLE 数据*/
+#define HCI_EVENT_I_AM_READY              0x09  /**模块准备�?*/
+#define HCI_EVENT_STAUS_RESPONSE          0x0A  /**状�?�回�?*/
+#define HCI_EVENT_NVRAM_CHANGED           0x0D  /**上传 NVRAM 数据*/
+#define HCI_EVENT_GKEY                    0x0E  /**HCI 包格式错�?*/
+#define HCI_EVENT_UART_EXCEPTION          0x0F  /*cmd format error*/
+#define HCI_EVENT_GET_PASSKEY             0x10  /**PASSKEY 配对方式中�?�知MCU 返回密钥,MCU收到此事件后�?发�?�HCI_CMD_PASSKEY_ENTRY指令回复蓝牙模块*/
+#define HCI_EVENT_PAIRING_COMPLETED       0x11  /**配对完成的事件，通知 MCU配对完成*/
+#define HCI_EVENT_LE_PAIRING_STATE        0x14  /**上报配对状�??*/
+#define	HCI_EVENT_UUID_HANDLE             0x29
+#define HCI_EVENT_BNEP_CONNECT            0x2c  /**通知MCU BNEP连接成功*/
+#define HCI_EVENT_BNEP_DISCONNECT         0x2d  /**通知MCU BNEP断开连接*/
+#define HCI_EVENT_BNEP_CONNECT_FAIL       0x2e  /**通知MCU BNEP连接失败原因*/
+#define HCI_EVENT_TCP_CONNECT             0x2f /**通知MCU TCP连接成功*/
+#define HCI_EVENT_TCP_DISCONNECT          0x30 /**通知MCU TCP断开连接*/
+#define HCI_EVENT_BNEP_RECEIVE_TCP_DATA   0x31  /*接收到TCP数据*/
+#define HCI_EVNET_TCP_FAIL                0x32  /*TCP连接或�?�断�?失败*/
+#define HCI_EVENT_TCP_CONNECT_LOG         0x33  /*TCP连接LOG*/
+#define HCI_EVENT_BNEP_CHIP_ERROR         0x34  /*芯片不支持BNEP*/
+#define HCI_EVENT_DNS_RSP                 0x35  /*DNS返回的ip地址*/
+#define HCI_EVENT_BB_DISCONNECT           0x36  /*经典蓝牙baseband断开后返回的事件*/
+#define HCI_EVENT_BT_NAME                 0x37  /*BT master name*/
+#define HCI_EVENT_UDP_INFO                0x38  /*UDP信息�?,包含远端IP、本地IP、远端端口�?�本地端�?,后面会紧随一包UDP数据*/
+#define HCI_EVENT_BNEP_RECEIVE_UDP_DATA   0x39  /*接收到UDP数据*/
+#define HCI_EVENT_BIG_DATA_FINISH         0x3a  /*大数据分包发送完成事�?*/
+#define HCI_EVENT_BIG_DATA_STATUS         0x3b  /*大数据分包发送状�?*/
+
+
+/**
+  * @brief  event code enum definition
+  */
+typedef enum
+{
+	BT_CONNECTED        		=HCI_EVENT_BT_CONNECTED,
+	BLE_CONNECTED       		=HCI_EVENT_BLE_CONNECTED,
+	BT_DISCONNECTED     		=HCI_EVENT_BT_DISCONNECTED,
+	BLE_DISCONNECTED    		=HCI_EVENT_BLE_DISCONNECTED,
+	BT_DATA             		=HCI_EVENT_SPP_DATA_RECEIVED,
+	BLE_DATA            		=HCI_EVENT_BLE_DATA_RECEIVED,
+	NVRAM_DATA          		=HCI_EVENT_NVRAM_CHANGED,
+	CONFIRM_GKEY        		=HCI_EVENT_GKEY,
+	PASSKEY             		=HCI_EVENT_GET_PASSKEY,
+	PAIRING_COMPLETED   		=HCI_EVENT_PAIRING_COMPLETED,
+	PAIRING_STATE        		=HCI_EVENT_LE_PAIRING_STATE,
+	BNEP_CONNECT        		=HCI_EVENT_BNEP_CONNECT,
+	TCP_CONNECT         		=HCI_EVENT_TCP_CONNECT,
+	BNEP_RECEIVE_TCP_DATA		=HCI_EVENT_BNEP_RECEIVE_TCP_DATA,
+	TCP_DISCONNECT       		=HCI_EVENT_TCP_DISCONNECT,
+	BNEP_DISCONNECT      		=HCI_EVENT_BNEP_DISCONNECT,
+	BNEP_CONNECT_FAIL    		=HCI_EVENT_BNEP_CONNECT_FAIL,
+	TCP_FAIL             		=HCI_EVNET_TCP_FAIL,
+	TCP_CONNECT_LOG      		=HCI_EVENT_TCP_CONNECT_LOG,
+	BNEP_CHIP_ERROR      		=HCI_EVENT_BNEP_CHIP_ERROR,
+	DNS_RSP              		=HCI_EVENT_DNS_RSP,
+	BB_DISCONNECT        		=HCI_EVENT_BB_DISCONNECT,
+	BT_NAME              		=HCI_EVENT_BT_NAME,
+	UDP_INFO             		=HCI_EVENT_UDP_INFO,
+	BNEP_RECEIVE_UDP_DATA		=HCI_EVENT_BNEP_RECEIVE_UDP_DATA
+
+}BT_EventCodeEnum;
+
+
+
+typedef struct
+{
+	uint8_t* pBuf;
+    	int BufSize;
+    	int count;		 /*!< count of valid data */
+    	int ReadIndex;
+    	int WriteIndex;
+} BT_BufTypeDef;
+
+/**
+  * @brief   Set the address of the bluetooth 3.0 device
+  * @param  bt_addr: pointer to  address(6 bytes)
+  * @retval TRUE:set addr success.
+  *         FALSE:set addr failed
+  */
+Boolean BT_SetBtAddr(uint8_t * bt_addr);
+
+
+/**
+  * @brief  Set the address of the bluetooth 4.0 device
+  * @param  bt_addr: pointer to  address(6 bytes)
+  * @retval TRUE:set addr success.
+  *         FALSE:set addr failed
+  */
+Boolean BT_SetBleAddr(uint8_t * bt_addr);
+
+
+/**
+  * @brief   Set the name of the bluetooth 3.0 device
+  * @param  bt_addr: pointer to  address information.
+  *         name_len: name length,not include '\0'
+  * @retval TRUE:set name success.
+  *         FALSE:set name failed
+  */
+Boolean BT_SetBtName(uint8_t * bt_name, uint16_t name_len);
+
+
+/**
+  * @brief   Set the name of the bluetooth 4.0 device
+  * @param  ble_name: pointer to  name information.
+  *         name_len: name length,not include '\0'
+  * @retval TRUE:set name success.
+  *         FALSE:set name failed
+  */
+Boolean BT_SetBleName(uint8_t* ble_name, uint16_t name_len);
+
+
+/**
+  * @brief  Set visibility mode.
+  * @param  bt_discoverable: 0--bt_discoverable OFF;1--bt_discoverable ON.
+  *         bt_connectability:0--bt_connectability OFF;1--bt_connectability ON.
+  *         ble_discoverable:0--ble_discoverable OFF;1--ble_discoverable ON.
+  * @retval TRUE:set visibility success.
+  *         FALSE:set visibility failed
+  */
+Boolean BT_SetVisibility(Boolean bt_discoverable, Boolean bt_connectability, Boolean ble_discoverable);
+
+
+/**
+  * @brief  Bt send spp(bt3.0) data
+  * @param  spp_data: pointer to  spp data
+  *         len: spp data length(max len is 255)
+  * @retval TRUE:send spp data success.
+  *         FALSE:send spp data failed
+  */
+Boolean Bt_CmdSenddata(uint8_t * spp_data, uint16_t len);
+
+/**
+  * @brief  Bt set LPM mode
+  * @param  mode:0x00-close LPM mode; 0x01-open LPM mode
+  * @retval TRUE or FALSE
+  *
+  */
+Boolean BT_SetLpmMode(uint8_t mode);
+/**
+  * @brief  Send ble data
+  * @param  ble_data: pointer to  ble data ,includes two bytes handle(byte0-byte1 is ble handle).
+  *         len: data length(max len is 255)
+  * @retval TRUE:send ble data success.
+  *         FALSE:send ble data failed
+  */
+  Boolean BT_SendBleData(uint8_t * ble_data, uint16_t len);
+
+
+/**
+  * @brief  get bluetooth connect status
+  * @param  None
+  * @retval bit0:BT 3.0 Can be discover
+  *         bit1:BT 3.0 Can be connected
+  *         bit2:BT 4.0 Can be discover and connected
+  *         bit4:BT 3.0 connected
+  *         bit5:BT 4.0 connected
+  *         bit7:get status timer out
+  */
+uint8_t Bt_GetBtStatus(void);
+
+
+
+/**
+  * @brief  set classic bluetooth paring mode
+  * @param  mode:the paramater can be one of the following value
+                0x00:pincode
+                0x01:just work
+                0x02:passkey
+                0x03:confirm
+  * @retval	   ture or FALSE
+  */
+  Boolean BT_SetParingMode(uint8_t mode);
+
+
+
+/**
+  * @brief  set classic bluetooth paring PIN code
+  * @param  Pincode:the pointer ponint to pincode data
+            len: normally the value can be 0x01~0x10,
+  * @retval	   ture or FALSE
+  */
+Boolean BT_SetPincode(uint8_t* Pincode,uint8_t len);
+
+
+
+/**
+  * @brief  get the bluetooth module firmware version number
+  * @param  none
+  * @retval firmware version number(the value is 1~65535),0 is fail
+  *
+  */
+uint32_t BT_GetVersion(void);
+
+
+/**
+  * @brief  bt disconnect
+  * @param  none
+  * @retval TRUE:bt disconnected success.
+  *         FALSE:bt disconnected failed
+  */
+Boolean BT_BtDisconnect(void);
+
+/**
+  * @brief  Set NVRAM
+  * @param  NvData:the pointer point to NVRAMData
+	* 				len:bnep is 34(one group),other is 170(5*34=170 five group).
+  * @retval TRUE:bt disconnected success.
+  *         FALSE:bt disconnected failed
+  */
+Boolean BT_SetNVRAM(uint8_t * NvData,int len);
+
+
+/**
+  * @brief  Bt Enter Sleep mode
+  * @param  none
+  * @retval TRUE:enter sleep mode success
+  *
+  */
+Boolean BT_EnterSleepMode(void);
+
+
+
+/**
+  * @brief  Numeric Comparison key matching in paring mode
+  * @param  mismatching:the paramater can be the follwing value
+                        0x00: key match
+                        0x01: key mismatch
+  * @retval TRUE:set ConfirmGkey success
+  *         FALSE:set ConfirmGkey failed
+ */
+Boolean BT_ConfirmGkey(uint8_t isMatching);
+
+
+/**
+  * @brief  set spp flow control
+  * @param  packetNum:the flow control packet number
+  * @retval TRUE:success
+  *         FALSE:failed
+ */
+Boolean BT_SetSppFlowcontrol(uint8_t packetNum);
+
+
+/**
+  * @brief  Comparison of keys in  PASSKEY paring
+  * @param  key_data: point to key data,length is 4 bytes,(data in hexadecimal form,
+  *                   starting with high bytes;for example ,218372(10) = 0x35504 ->key_data[]={0x00,0x03,0x55,0x04})
+  * @retval TRUE:set passkey success
+  *         FALSE:set passkey  failed
+  */
+Boolean BT_PasskeyEntry(uint8_t *key_data);
+
+/**
+  * @brief  Set ble paring mode
+  * @param  mode: the paramater can be the follwing value
+  *               0x00:none
+  *               0x01:just work
+  *               0x02:pass key
+  *               0x81:secure connect just work
+  *               0x82:secure connect numeric
+  *               0x83:secure connect pass key
+  * @retval TRUE:set ble paring success
+  *         FALSE:set ble paring  failed
+  */
+Boolean BT_SetLEParing(uint8_t mode);
+
+/**
+  * @brief    Send ble adv data
+  * @param    adv_data: pointer to adv data.
+	*  					DataLen: data length(must be 0x1f)
+  * @retval	  TRUE:set ble adv  data success.
+  *           FALSE:set ble adv data failed
+  */
+Boolean BT_SetLEAdvData(uint8_t*  adv_data, int DataLen);
+
+
+/**
+  * @brief  set ble scan data
+  * @param  scan_data: pointer to  scan data.
+  *         DataLen: data length(length < 0x20)
+  * @retval TRUE:set ble scan  data success.
+  *         FALSE:set ble scan data failed
+  */
+Boolean BT_SetLEScanData(uint8_t* scan_data, int DataLen);
+
+/**
+  * @brief  ble send update ble connect parameter data
+  * @param  data:
+  *             byte0-byte1:min connect interval
+  *             byte2-byte3:max connect interval
+  *             byte4-byte5:Slave  latency
+  *             byte6-byte7:Connection Supervision Timeout
+  *         len:len must be 8
+  * @retval TRUE:success.
+  *         FALSE:failed
+  */
+Boolean BT_SetLESendConnUpdate(uint8_t *data,int len);
+
+
+/**
+  * @brief  Set ble adv parm
+  * @param  data: pointer to  adv parmdata.
+  *         DataLen: data length(len must be 2)
+  * @retval TRUE:set ble adv parm success.
+  *         FALSE:set ble adv parm failed
+  */
+Boolean BT_SetLEAdvParm(uint8_t*data,int DataLen);
+
+/**
+  * @brief  Set reject just work
+  * @param  justwork:
+  *                 0:turn off reject just work
+  *                 1:turn on reject just work
+  * @retval TRUE:set ble scan  data success.
+  *         FALSE:set ble scan data failed
+  */
+Boolean BT_RejectJustWork(uint8_t justwork);
+
+
+/**
+  * @brief  Set fixed passkey
+  * @param  key: pointer to key data ,key length is 5 bytes
+  * @retval TRUE:set fixed passkey  success.
+  *         FALSE:set ble fixed passkey failed
+  */
+Boolean BT_Set_FixedPasskey(uint8_t* key);
+
+
+/**
+  * @brief  ble disconnect
+  * @param  none
+  * @retval TRUE:ble disconnected success.
+  *         FALSE:ble disconnected failed
+  */
+Boolean BT_BleDisconnect(void);
+
+
+/**
+  * @brief  Set spp(bt3.0) class of device
+  * @param  bt_cod:cod buf(must be 3 byte)
+  * @retval TRUE:success.
+  *         FALSE:failed
+  */
+Boolean BT_SetCOD(uint8_t* bt_cod);
+
+
+
+
+/**
+  * @brief  Set RF tx power
+  * @param  power: 0:0db
+  *                1:3db
+  *                2:5db
+  *                3:-3db
+  *                4:-5db
+  * @retval TRUE:set SetTxPower  success.
+  *         FALSE:set SetTxPower failed
+  */
+Boolean BT_SetTxPower(uint8_t power);
+
+
+/**
+  * @brief  Delete ble custom Service
+  * @param  none
+  * @retval TRUE:success.
+  *         FALSE:failed
+  */
+Boolean BT_DeleteService(void);
+
+
+/**
+  * @brief  add ble custom Service
+  * @param  ble_service_uuid:
+	*														byte0: uuid length(2 or 16)
+	*														byte1-2(16): uuid
+	*					service_uuid_len: sizeof(ble_service_uuid)
+  * @retval 0:failed
+  *         other:Service handle
+  */
+uint16_t BT_AddBleService(uint8_t* ble_service_uuid, uint16_t service_uuid_len);
+
+
+/**
+  * @brief  add ble custom Characteristic
+  * @param  ble_Characteristic_uuid:
+	*														byte0: characterisitic attribute
+	*																	bit0		Broadcast
+	*																	bit1		Read
+	*																	bit2		Write without Response
+	*																	bit3		Write
+	*																	bit4		Notify
+	*																	bit5		Indicate
+	*																	bit6		Authentication Signed Write
+	*																	bit7		Extended Properties
+	*														byte1:			characterisitic uuid length(2 or 16)
+	*														byte2-3(17):characterisitic uuid
+	*														byte4(18):	write/read payload length(1--20;default:1)
+	*														byte5(19)-x:write/read payload(default:00)
+	*					service_Characteristic_payload_len: sizeof(ble_Characteristic_uuid)
+  * @retval 0:failed
+  *         other:Characteristic handle
+  */
+uint16_t BT_AddBleCharacteristic(uint8_t* ble_Characteristic_uuid, uint16_t service_Characteristic_payload_len);
+
+/**
+  * @brief   parse bluetooth module data packet according to yichip HCI protocol from ipc buf,you shoud call it at BT_UART IRQHandler function
+  * @param  Unone
+  *
+  * @retval none
+  */
+void BT_ParseBTData(void);
+
+
+
+
+/**
+  * @brief   Handling user events about bluetooth module,This is not an interrupt function,A circular call is required to detect bluetooth data
+  *
+  * @param  none
+  *
+  * @retval none
+  */
+void BT_Progress(void);
+
+
+/**
+  * @brief  BT Init
+  * @param  none
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+//Boolean BT_Init(uint8_t * rxbuf, uint16_t rxlen);
+
+Boolean BT_Init(void);
+
+
+/**
+  * @brief  DNS(domain name resolution)
+  * @param  dns_data: pointer to  domain name(www.baidu.com).
+            DataLen: data length(max 62 bytes)
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_DnsReq( uint8_t *dns_data,int len);
+/**
+  * @brief  Connect Bnep
+  * @param   *phone_mac_addr: pointer to phone_mac_addr.
+	           Byte0~Byte5   The MAC address you want to connect to your phone
+						 Byte6~Byte21  Link key
+						 (phone MAC and Link key from NVRAM event,all zero is connect last pair phone)
+             len: data length(must be 22 byte)
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_ConnectBnep( uint8_t *phone_mac_addr,int len);
+
+/**
+  * @brief  disconnect Bnep
+  * @param   none
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_disconnectBnep(void);
+
+
+
+/**
+  * @brief  Connect Tcp
+  * @param  tcp_ip_addr: pointer to  tcp_ip_addr.
+	          Byte0  TCP connect handle(0x00 or 0x01)
+						Byte1~Byte4  IP address(192.168.1.1 is c0 a8 01 01)
+						Byte5~Byte6  port number(8888 is 22 b8)
+            len: data length(must be 7 byte)
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_ConnectTcp( uint8_t *tcp_ip_addr,int len);
+
+
+
+/**
+  * @brief  Send  TcpData
+  * @param  tcpdata: pointer to  tcpdata.
+	          Byte0  TCP handle
+						Byte1~ByteN  The TCP data need to send(max 235 bytes)
+            len: data length(max value 1+235)
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_BnepSendTcpData( uint8_t *tcpdata,int len);
+
+
+/**
+  * @brief  Send  Tcp big packet Data
+  * @param  tcpdata: pointer to  tcpdata.
+	          Byte0  TCP handle
+						Byte1~ByteN  The TCP data need to send(max 1460 bytes)
+            len: data length(max value 1+1460)
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_BnepSendTcpBigData( uint8_t *tcpdata,int len);
+
+
+/**
+  * @brief  Send  UDP Data
+  * @param  tcpdata: pointer to  udpdata.
+	          Byte0~Byte3  UDP remote IP(192.168.1.1 is c0 a8 01 01)
+						Byte4~Byte5  UDP local port(8888 is 22 b8)
+						Byte6~Byte7  UDP remote port(12345 is 30 39)
+						Byte8~ByteN  The UDP data need to send(max 247 bytes)
+            len: data length(max value 255)
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_BnepSendUdpData( uint8_t *udpdata,int len);
+
+
+
+/**
+  * @brief  Disconnect  Tcp
+  * @param  tcp_handle: the want to disconnect TCP handle(0x00 or 0x01)
+  * @retval TRUE:success
+  *         FALSE:fail
+  */
+Boolean BT_DisconnectTcp(uint8_t tcp_handle);
+
+
+
+
+extern uint8_t prt_get_bt_type(void);
+
+//extern uint32_t prt_get_bt_data(void * buf, int len);
+extern uint32_t prt_get_bt_data(void);
+extern uint8_t prt_get_bt_connection_status(void);
+
+#endif
+
Index: BT_doublebyte/sdk/Librarier/sdk/yc_crypt/yc_mf_crypt.7z
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream

Property changes on: BT_doublebyte/sdk/Librarier/sdk/yc_crypt/yc_mf_crypt.7z
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: BT_doublebyte/sdk/Librarier/sdk/yc_bt2.c
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_bt2.c	(nonexistent)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_bt2.c	(working copy)
@@ -0,0 +1,1499 @@
+#include "yc_bt2.h"
+#include "yc_systick.h"
+#include "yc_timer.h"
+#include "yc_ipc2.h"
+
+#define BT_LOG	0
+#define IO_DEBUG 0
+
+static tick StartTick;
+static uint32_t HCI_CMD_BIT_FLAG[2]={0};
+#define BTTIMEOUT 500
+
+static uint8_t CmdRepData[5];
+
+static uint8_t bt_con_type = 0;
+static uint8_t bt_con_sta  = 0;
+
+
+extern uint8_t prt_get_bt_connection_status(void)
+{
+    return bt_con_sta;
+}
+extern uint8_t prt_get_bt_type(void)
+{
+    return bt_con_type;
+}
+
+
+extern IPC_TypeDef *IpcTx; 
+extern IPC_TypeDef *IpcRx;
+
+
+HCI_TypeDef hci_recappdata[HCI_BT_NUM];
+
+
+uint8_t hci_btble_data[HCI_BT_NUM][HCT_DATA_LEN];
+uint8_t hci_buf_write_index = 0;
+uint8_t hci_buf_read_index = 0;
+
+HCI_TypeDef hci_rec_state_event;
+uint8_t hci_rec_state[HCT_STATE_LEN];
+
+extern uint8_t bt2m0buf_full_flag;
+
+void BT_BIT_SET(uint32_t* a,int b)
+{
+	if(b<32)
+	{
+		a[0] |= (1<<b);
+	}
+	else
+	{
+		a[1] |= (1<<(b-32));
+	}
+}
+
+void BT_BIT_CLEAR(uint32_t* a,int b)
+{
+	if(b<32)
+	{
+		a[0] &= ~(1<<b);
+	}
+	else
+	{
+		a[1] &= ~(1<<(b-32));
+	}
+}
+
+Boolean BT_BIT_GET(uint32_t* a,int b)
+{
+	if(b<32)
+	{
+		return (Boolean)((a[0]&(1<<b))>>b);
+	}
+	else
+	{
+		return (Boolean)((a[1]&(1<<(b-32)))>>(b-32));
+	}
+}
+
+//static void SkipData(int Num)
+//{
+//	BluetoothBuf.ReadIndex+=Num;
+//	if(BluetoothBuf.ReadIndex>=BluetoothBuf.BufSize)	BluetoothBuf.ReadIndex-=BluetoothBuf.BufSize;
+//	NVIC_DisableIRQ(BT_IRQn);
+//	BluetoothBuf.count-=Num;
+//	NVIC_EnableIRQ(BT_IRQn);
+//}
+
+//static void SaveData(uint8_t* data,int len)
+//{
+//	int i;
+//	if(BluetoothBuf.count+len>BluetoothBuf.BufSize)
+//	{
+//		#ifdef BT_ERROR_LOG
+//		MyPrintf("BluetoothBuf full\r\n");
+//		#endif
+//		return;
+//	}
+//	for(i=0;i<len;i++)
+//	{
+//		BluetoothBuf.pBuf[BluetoothBuf.WriteIndex]=data[i];
+//		BluetoothBuf.count++;
+//		BluetoothBuf.WriteIndex++;
+//		if(BluetoothBuf.WriteIndex>=BluetoothBuf.BufSize)	BluetoothBuf.WriteIndex=0;
+//	}
+//}
+
+//#define PrintLog
+#define DOMAIN_MAX_LEN	62
+Boolean BNEP_DomainConvert(unsigned char * domainString, unsigned char domainStringLen, unsigned char *newDomainString,int* newDomainStringIndex)
+{
+	#ifdef PrintLog
+	MyPrintf("input domain:%s \n", domainString);
+	#endif
+	unsigned char domainCount = 0;
+	*newDomainStringIndex = 1;
+	int newDomainDelimiterIndex = 0;
+	#ifdef PrintLog
+	MyPrintf("domain hex:\n");
+	for (int i = 0; i < domainStringLen; i++)
+	{
+		MyPrintf("%02X ", domainString[i]);
+	}
+	MyPrintf("\n");
+	#endif
+	while(domainStringLen--)
+	{
+		domainCount++;
+		if (*domainString == '.')
+		{
+			newDomainString[newDomainDelimiterIndex] = domainCount-1;
+			newDomainDelimiterIndex = *newDomainStringIndex;
+			(*newDomainStringIndex)++;
+			if((*newDomainStringIndex)>=DOMAIN_MAX_LEN)	return FALSE;
+			domainCount = 0;
+		}
+		else
+		{
+			newDomainString[(*newDomainStringIndex)++] = *domainString;
+			if((*newDomainStringIndex)>=DOMAIN_MAX_LEN)	return FALSE;
+		}
+		domainString++;
+	}
+	newDomainString[newDomainDelimiterIndex] = domainCount;
+	newDomainString[(*newDomainStringIndex)++] = 00;
+	if((*newDomainStringIndex)>=DOMAIN_MAX_LEN)	return FALSE;
+	#ifdef PrintLog
+	MyPrintf("format Domain:\n");
+	for (int  i = 0; i < *newDomainStringIndex; i++)
+	{
+	MyPrintf("%02X ", newDomainString[i]);
+	}
+	MyPrintf("\n");
+	#endif
+	return TRUE;
+}
+
+
+Boolean BT_Init(void)
+{
+	HCI_CMD_BIT_FLAG[0]=0;//全局变量问题优化
+	HCI_CMD_BIT_FLAG[1]=0;
+	StartTick=0;
+	int i;
+	for(i=0;i<5;i++)	CmdRepData[i]=0;
+    
+    //save app data buf
+	for (i=0;i<HCI_BT_NUM;i++)
+    {
+        hci_recappdata[i].p_data =  &hci_btble_data[i][0]; 
+    }
+    
+    //save bt report state
+    hci_rec_state_event.p_data = hci_rec_state;
+	IpcInit();
+	return TRUE;
+}
+
+static Boolean  SendCMD(HCI_TypeDef* msg)
+{
+	if((msg->DataLen+3) > IPC_get_available_size())
+	{
+		#if BT_LOG
+		MyPrintf("IPC BT Tx FULL\n");
+		#endif
+		return FALSE;
+	}
+	msg->type=HCI_CMD;
+#if BT_LOG	
+	MyPrintf("Print Send Data Pack:\n");
+	PrintHCIPack(msg, "Send");//打印发�?�包
+#endif
+	IPC_TxData(msg);
+return TRUE;
+}
+Boolean BT_SetBtAddr(uint8_t *bt_addr)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_BT_ADDR;
+	msg.DataLen = 6;
+	msg.p_data = bt_addr;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_BT_ADDR);
+  SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_BT_ADDR))
+		{
+			return TRUE;
+		}
+  }while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms
+	return FALSE;
+}
+
+
+Boolean BT_SetBleAddr(uint8_t * bt_addr)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_BLE_ADDR;
+	msg.DataLen = 6;
+	msg.p_data = bt_addr;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_BLE_ADDR);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_BLE_ADDR))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms    
+	return FALSE;
+}
+
+Boolean BT_SetVisibility(Boolean bt_discoverable, Boolean bt_connectability, Boolean ble_discoverable)
+{
+	HCI_TypeDef msg;
+	uint8_t visibility_data = 0;
+	if (bt_discoverable == 1)
+		visibility_data = visibility_data | 0x01;
+	if (bt_connectability == 1)
+		visibility_data = visibility_data | 0x02;
+	if (ble_discoverable == 1)
+		visibility_data = visibility_data | 0x04;
+
+	msg.opcode = HCI_CMD_SET_VISIBILITY;
+	msg.DataLen = 0x01;
+	msg.p_data = &visibility_data;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_VISIBILITY);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_VISIBILITY))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));   
+	return FALSE;
+}
+
+Boolean BT_SetBtName(uint8_t * bt_name, uint16_t name_len)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_BT_NAME;
+	msg.DataLen = name_len;
+	msg.p_data  = bt_name;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_BT_NAME);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_BT_NAME))
+		{
+    		return TRUE;
+		}
+  }  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT)); 
+	return FALSE;
+}
+
+
+Boolean BT_SetBleName(uint8_t* ble_name, uint16_t name_len)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_BLE_NAME;
+	msg.DataLen = name_len;
+	msg.p_data  = ble_name;        
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_BLE_NAME);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_BLE_NAME))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;
+}
+
+
+Boolean Bt_CmdSenddata(uint8_t * spp_data, uint16_t DataLen)
+{
+			Boolean ret;
+		int i=0;		
+		uint16_t len,offset=0;
+	
+		HCI_TypeDef msg; 
+
+		//MyPrintf("sedn %d bytes bt data:\n",DataLen);
+		//for(;i<DataLen;i++)	MyPrintf("%02x ",spp_data[i]);
+	
+		if(DataLen==0)	return FALSE;
+		for(len = DataLen; len>0; )
+		{	
+			msg.opcode = HCI_CMD_SEND_SPP_DATA;
+			msg.DataLen = len>MAX_BLUETOOTH_DATA_LEN?MAX_BLUETOOTH_DATA_LEN:len;
+			len -= msg.DataLen;
+			msg.p_data = spp_data+offset;
+			MyPrintf("sedn %d bytes bt data:\n",msg.DataLen);
+			offset += msg.DataLen;
+			BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SEND_SPP_DATA);
+			SendCMD(&msg);
+			StartTick=SysTick_GetTick();
+			ret =  FALSE;
+			do
+			{
+					if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SEND_SPP_DATA))
+					{
+	//						return TRUE;
+							ret = TRUE;
+							break;
+					}
+			}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+			if (ret== FALSE) 
+				return FALSE;
+		}
+		
+		return TRUE;
+	
+	
+//	if(DataLen==0||DataLen>MAX_BLUETOOTH_DATA_LEN)	return FALSE;
+//	HCI_TypeDef msg;
+//	msg.opcode = HCI_CMD_SEND_SPP_DATA;
+//	msg.DataLen = DataLen;
+//	msg.p_data = spp_data;	
+//	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SEND_SPP_DATA);
+//	SendCMD(&msg);
+//	StartTick=SysTick_GetTick();
+//	do
+//	{
+//		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SEND_SPP_DATA))
+//		{
+//			return TRUE;
+//		}
+//	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+//	return FALSE;
+}
+
+Boolean BT_SendBleData(uint8_t * ble_data, uint16_t DataLen)
+{
+	if(DataLen<2||DataLen>MAX_BLUETOOTH_DATA_LEN)	return FALSE;
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SEND_BLE_DATA;
+	msg.DataLen = DataLen;
+
+	msg.p_data = ble_data;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SEND_BLE_DATA);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SEND_BLE_DATA))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return FALSE;
+}
+
+
+uint8_t Bt_GetBtStatus(void)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_STATUS_REQUEST;
+	msg.DataLen = 0;
+	msg.p_data=NULL;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_STATUS_REQUEST);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_STATUS_REQUEST))
+		{
+			return CmdRepData[0];
+		}
+	} while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));//ms
+  return 0x80;
+}
+
+Boolean BT_SetParingMode(uint8_t mode)
+{
+	uint8_t tmode = mode ;
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_PAIRING_MODE;
+	msg.DataLen = 1;
+	msg.p_data = &tmode;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_PAIRING_MODE);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_PAIRING_MODE))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));	
+	return FALSE;
+}
+
+Boolean BT_SetPincode(uint8_t* Pincode,uint8_t DataLen)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_PINCODE;
+	msg.DataLen = DataLen;
+	msg.p_data = Pincode;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_PINCODE);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_PINCODE))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;
+}
+
+
+
+Boolean BT_BtDisconnect(void)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_BT_DISCONNECT;
+	msg.DataLen = 0;
+	msg.p_data = NULL;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_BT_DISCONNECT);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_BT_DISCONNECT))
+		{
+			return TRUE;
+		}
+	}while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return FALSE;
+}
+
+uint32_t BT_GetVersion(void)
+{
+	HCI_TypeDef msg;
+	uint32_t TempVersion=0;
+	msg.opcode = HCI_CMD_VERSION_REQUEST;
+	msg.DataLen = 0;
+	msg.p_data = NULL;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_GET_VERSION);
+	SendCMD(&msg);	
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_GET_VERSION))
+		{
+			TempVersion=(CmdRepData[0]<<8) |(CmdRepData[1]);
+			return TempVersion;
+		}
+	}while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return 0;
+}
+
+
+Boolean BT_BleDisconnect(void)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_BLE_DISCONNECT;
+	msg.DataLen = 0;
+	msg.p_data = NULL;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_BLE_DISCONNECT);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_BLE_DISCONNECT))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return FALSE;
+}
+
+
+Boolean BT_SetNVRAM(uint8_t * NvData,int len)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_NVRAM;
+	msg.DataLen = len;
+	msg.p_data = NvData;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_NVRAM);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_NVRAM))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return FALSE;
+}
+
+
+Boolean BT_EnterSleepMode(void)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_ENTER_SLEEP_MODE;
+	msg.DataLen = 0;
+	msg.p_data = NULL;
+	SendCMD(&msg);
+	return TRUE;
+}
+extern uint8_t bt_lpm_mode;
+
+Boolean BT_SetLpmMode(uint8_t mode)
+{
+	HCI_TypeDef msg;
+	uint8_t temp_mode=mode;
+	msg.opcode = HCI_CMD_SET_LPM_MODE;
+	msg.DataLen =1;
+	msg.p_data = &temp_mode;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_LPM_MODE);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_LPM_MODE))
+		{
+			bt_lpm_mode = mode;
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return FALSE;
+}
+
+
+Boolean BT_ConfirmGkey(uint8_t isMatching)
+{
+	HCI_TypeDef msg;
+	uint8_t tmismatching = isMatching;
+	msg.opcode = HCI_CMD_CONFIRM_GKEY;
+	msg.DataLen =1;
+	msg.p_data = &tmismatching;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_CONFIRM_GKEY);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_CONFIRM_GKEY))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return FALSE;
+}
+
+Boolean BT_SetSppFlowcontrol(uint8_t packetNum)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_GET_CREADIT_GIVEN;
+	msg.DataLen = 1;
+	uint8_t temp_packetNum=packetNum;
+	msg.p_data = &temp_packetNum;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_GET_CREADIT_GIVEN);
+	SendCMD(&msg);
+	return TRUE;//no event responce
+}
+
+
+Boolean BT_PasskeyEntry(uint8_t *key_data)
+{
+#define PKLEN 0x04
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_PASSKEY_ENTRY;
+	msg.DataLen = PKLEN;
+	msg.p_data = key_data;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_PASSKEY_ENTRY);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_PASSKEY_ENTRY))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));	
+	return FALSE;
+}
+
+Boolean BT_SetLEParing(uint8_t mode)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_LE_SET_PAIRING;
+	msg.DataLen = 1;
+	uint8_t temp_mode=mode;
+	msg.p_data = &temp_mode;		
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_LE_SET_PARING);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_LE_SET_PARING))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));	
+	return FALSE;
+}
+
+Boolean BT_SetLEAdvData(uint8_t* adv_data, int DataLen)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_LE_SET_ADV_DATA;
+	msg.DataLen = DataLen;
+	msg.p_data = adv_data;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_LE_SET_ADV_DATA);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_LE_SET_ADV_DATA))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;     
+}
+
+Boolean BT_SetLEScanData(uint8_t* scan_data, int DataLen)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_LE_SET_SCAN_DATA;
+	msg.DataLen = DataLen;
+	msg.p_data = scan_data;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_LE_SET_SCAN_DATA);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_LE_SET_SCAN_DATA))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;     
+}
+
+Boolean BT_SetLESendConnUpdate(uint8_t *data,int len)
+{
+	if(0x08 != len) 
+		return FALSE;
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_LE_SEND_CONN_UPDATE_REQ;
+	msg.DataLen = len;	
+	msg.p_data = data;	
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_LE_SEND_CONN_UPDATE);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_LE_SEND_CONN_UPDATE))
+		{
+			return TRUE;
+		}
+	} while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;
+}
+
+Boolean BT_SetLEAdvParm(uint8_t*nvdata,int DataLen)//bt code did not exist
+{
+	if(0 == DataLen) 
+		return FALSE;
+    
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_LE_SET_ADV_PARM;
+	msg.DataLen = DataLen;	
+	msg.p_data = nvdata;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_LE_SET_ADV_PARM);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_LE_SET_ADV_PARM))
+		{
+			return TRUE;
+		}
+	} while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;
+}
+
+
+
+Boolean BT_RejectJustWork(uint8_t justwork)
+{
+	uint8_t td = justwork;
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_REJECT_JUSTWORK;
+	msg.DataLen = 0x01;  
+	msg.p_data = &td;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_REJECT_JUSTWORK);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_REJECT_JUSTWORK))
+		{
+			return TRUE;
+		}
+	} while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;
+}
+
+Boolean BT_SetTxPower(uint8_t power)
+{
+	uint8_t tpower = power;	
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_TX_POWER;
+	msg.DataLen = 0x01;	
+	msg.p_data = &tpower;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_TXPOWER);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_TXPOWER))
+		{
+			return TRUE;
+		}
+	} while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;
+}
+
+Boolean BT_Set_FixedPasskey(uint8_t* key)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_LE_SET_FIXED_PASSKEY;
+	msg.DataLen = 0x05;	
+	msg.p_data = key;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_LE_SET_FIXED_PASSKEY);
+	SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_LE_SET_FIXED_PASSKEY))
+		{
+			return TRUE;
+		}
+	} while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return FALSE;
+}
+
+Boolean BT_SetCOD(uint8_t * bt_cod)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SET_COD;
+	msg.DataLen = 3;
+	msg.p_data = bt_cod;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_SET_COD);
+	SendCMD(&msg);	
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_SET_COD))
+		{
+			return TRUE;
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms    
+	return FALSE;
+}
+
+
+Boolean BT_DeleteService(void)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_DELETE_CUSTOMIZE_SERVICE;
+	msg.DataLen = 0;
+	msg.p_data = NULL;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_DELETE_SERVICE);
+	SendCMD(&msg);	
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_DELETE_SERVICE))
+		{			
+			return TRUE;
+		}
+	}while(!SysTick_IsTimeOut(StartTick, BTTIMEOUT));
+	return FALSE;
+}
+
+uint16_t BT_AddBleService(uint8_t* ble_service_uuid, uint16_t service_uuid_len)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_ADD_SERVICE_UUID;
+	msg.DataLen = service_uuid_len;
+	msg.p_data  = ble_service_uuid;        
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_ADD_UUID);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_ADD_UUID))
+		{
+			return (uint16_t)(CmdRepData[0]|(CmdRepData[1]<<8));
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return 0x00;
+}
+
+
+	
+	
+uint16_t BT_AddBleCharacteristic(uint8_t* ble_Characteristic_uuid, uint16_t service_Characteristic_payload_len)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_ADD_CHARACTERISTIC_UUID;
+	msg.DataLen = service_Characteristic_payload_len;
+	msg.p_data  = ble_Characteristic_uuid;        
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_ADD_UUID);
+	SendCMD(&msg);
+	StartTick=SysTick_GetTick();
+	do
+	{
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_ADD_UUID))
+		{
+			return (uint16_t)(CmdRepData[0]|(CmdRepData[1]<<8));
+		}
+	}  while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));
+	return 0x00;
+}
+
+Boolean BT_DnsReq( uint8_t *dns_data,int len)
+{
+	uint8_t format_dns[DOMAIN_MAX_LEN];
+	int format_dns_len;
+	if(TRUE != BNEP_DomainConvert(dns_data,len,format_dns,&format_dns_len))
+	{
+		return FALSE;
+	}
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_DNS_REQ;
+	msg.DataLen = format_dns_len;
+	msg.p_data = format_dns;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_DNS_REQ);
+  SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_DNS_REQ))
+		{
+			return TRUE;
+		}
+  }while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms
+	return FALSE;
+}
+
+Boolean BT_ConnectBnep( uint8_t *phone_mac_addr,int len)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_CONNECT_BNEP;
+	msg.DataLen = len;
+	msg.p_data = phone_mac_addr;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_CONNECT_BNEP);
+  SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_CONNECT_BNEP))
+		{
+			return TRUE;
+		}
+  }while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms
+	return FALSE;
+}
+
+Boolean BT_disconnectBnep()
+{
+	return BT_BtDisconnect();
+}
+
+Boolean BT_ConnectTcp(uint8_t *tcp_ip_addr,int len)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_CONNECT_TCP;
+	msg.DataLen = len;
+	msg.p_data = tcp_ip_addr;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_CONNECT_TCP);
+  SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_CONNECT_TCP))
+		{
+			return TRUE;
+		}
+  }while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms
+	return FALSE;
+}
+
+
+
+Boolean BT_BnepSendTcpData( uint8_t *tcpdata,int len)
+{
+	if((len-1)>MAX_TCP_DATA_LEN)	return FALSE;
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_BNEP_SEND_TCP_DATA;
+	msg.DataLen = len;
+	msg.p_data = tcpdata;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_DATA);
+  SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_DATA))
+		{
+			return TRUE;
+		}
+  }while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms
+	return FALSE;
+}
+
+#define BIG_DATA_HEAD_LEN	5
+Boolean BT_BnepSendTcpBigData( uint8_t *tcpdata,int len)
+{
+	if((len-1)>MAX_BIG_DATA_LEN)	return FALSE;
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SEND_BIG_DATA;
+	int sendedLen=0;
+	int packetLen;
+	uint8_t firstPacketData[MAX_BLUETOOTH_DATA_LEN];
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA_FINISH);
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA_STATUS);
+	while(sendedLen<(len-1))
+	{
+		if(sendedLen==0)
+		{
+			packetLen=(len-1)>(MAX_BLUETOOTH_DATA_LEN-BIG_DATA_HEAD_LEN)?(MAX_BLUETOOTH_DATA_LEN-BIG_DATA_HEAD_LEN):(len-1);
+			msg.DataLen=packetLen+BIG_DATA_HEAD_LEN;
+			firstPacketData[0]=tcpdata[0];//handle
+			
+			firstPacketData[1]=((len-1)&0xff);
+			firstPacketData[2]=(((len-1)>>8)&0xff);//big packet len
+			
+			firstPacketData[3]=0;
+			firstPacketData[4]=0;//reserved
+			memcpy((uint8_t*)&firstPacketData[5],(uint8_t*)&tcpdata[1],packetLen);
+			msg.p_data = firstPacketData;
+		}
+		else
+		{
+			packetLen=((len-1)-sendedLen)>MAX_BLUETOOTH_DATA_LEN?MAX_BLUETOOTH_DATA_LEN:((len-1)-sendedLen);
+			msg.DataLen=packetLen;
+			msg.p_data =(uint8_t*)&tcpdata[sendedLen+1];
+		}
+
+		BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA);
+		SendCMD(&msg);
+		StartTick=SysTick_GetTick();
+		while(!BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA))
+		{
+			if(SysTick_IsTimeOut(StartTick,BTTIMEOUT))
+			{
+				return FALSE;
+			}
+		}
+		sendedLen +=packetLen;
+	}
+	//wait big data finish
+	StartTick=SysTick_GetTick();
+	while(!BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA_FINISH))
+	{
+		if(SysTick_IsTimeOut(StartTick,BTTIMEOUT))
+		{
+			return FALSE;
+		}
+	}
+	//wait big data status
+	StartTick=SysTick_GetTick();
+	while(!BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA_STATUS))
+	{
+		if(SysTick_IsTimeOut(StartTick,BTTIMEOUT))
+		{
+			return FALSE;
+		}
+	}
+	return TRUE;
+}
+
+
+Boolean BT_BnepSendUdpData( uint8_t *udpdata,int len)
+{
+	if(len<8||len>MAX_BLUETOOTH_DATA_LEN)	return FALSE;
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_SEND_UDP_DATA;
+	msg.DataLen = len;
+	msg.p_data = udpdata;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_UDP_DATA);
+  SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_UDP_DATA))
+		{
+			return TRUE;
+		}
+  }while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms
+	return FALSE;
+}
+
+
+Boolean BT_DisconnectTcp(uint8_t tcp_handle)
+{
+	HCI_TypeDef msg;
+	msg.opcode = HCI_CMD_DISCONNECT_TCP;
+	msg.DataLen = 1;
+    uint8_t temp_tcp_handle=tcp_handle;
+	msg.p_data = &temp_tcp_handle;
+	BT_BIT_CLEAR(HCI_CMD_BIT_FLAG,BIT_DISCONNECT_TCP);
+  SendCMD(&msg);
+  StartTick=SysTick_GetTick();
+  do
+  {
+		if(BT_BIT_GET(HCI_CMD_BIT_FLAG,BIT_DISCONNECT_TCP))
+		{
+			return TRUE;
+		}
+  }while(!SysTick_IsTimeOut(StartTick,BTTIMEOUT));//ms
+	return FALSE;
+}
+
+
+extern void prt_yc3121_bt_post_event(uint16_t event);
+//extern Boolean prt_is_ble_data(uint16_t handler);
+//extern void prt_ble_first_set(Boolean need);
+//extern Boolean prt_ble_first_get(void);
+
+void BT_ParseBTData()
+{
+    uint8_t opcode_val = 0;
+    opcode_val = IPC_ReadIPC_Opcode();
+    if ((opcode_val == HCI_EVENT_SPP_DATA_RECEIVED) || (opcode_val == HCI_EVENT_BLE_DATA_RECEIVED))
+    {
+        #if BT_LOG
+        MyPrintf("data1 in\n");
+        #endif
+        #if IO_DEBUG
+        GPIO_CONFIG(5) = 0x3e;
+        #endif
+        IPC_ReadBtData_DMA(&hci_recappdata[hci_buf_write_index]);
+        #if IO_DEBUG
+        GPIO_CONFIG(5) = 0x3f;
+        #endif
+    }
+    else
+    {
+        #if BT_LOG
+        MyPrintf("event\n");
+        #endif
+        if (hci_buf_write_index == 0)
+        {
+            hci_buf_write_index = HCI_BT_NUM-1;
+        }
+        else
+        {
+            hci_buf_write_index--;
+        }
+        IPC_ReadBtData(&hci_rec_state_event);
+        switch (opcode_val)
+        {
+            case HCI_EVENT_BT_CONNECTED:
+                prt_yc3121_bt_post_event(HCI_EVENT_BT_CONNECTED);
+                bt_con_sta = HCI_EVENT_BT_CONNECTED;
+                break;
+            case HCI_EVENT_BLE_CONNECTED:
+                prt_yc3121_bt_post_event(HCI_EVENT_BLE_CONNECTED);
+                bt_con_sta = HCI_EVENT_BLE_CONNECTED;
+                break;
+
+            case HCI_EVENT_BT_DISCONNECTED:
+                prt_yc3121_bt_post_event(HCI_EVENT_BT_DISCONNECTED);
+                bt_con_sta = HCI_EVENT_BT_DISCONNECTED;
+                break;
+            case HCI_EVENT_BLE_DISCONNECTED:                    
+                prt_yc3121_bt_post_event(HCI_EVENT_BLE_DISCONNECTED);
+                //prt_ble_first_set(FALSE);
+                bt_con_sta = HCI_EVENT_BLE_DISCONNECTED;
+                break;
+            case HCI_EVENT_GKEY:
+            case HCI_EVENT_NVRAM_CHANGED:
+            case HCI_EVENT_GET_PASSKEY:
+            case HCI_EVENT_PAIRING_COMPLETED:
+            case HCI_EVENT_LE_PAIRING_STATE:
+            case HCI_EVENT_BNEP_CONNECT:
+            case HCI_EVENT_BNEP_DISCONNECT:
+            case HCI_EVENT_BNEP_CONNECT_FAIL:
+            case HCI_EVENT_TCP_CONNECT:
+            case HCI_EVENT_TCP_DISCONNECT:
+            case HCI_EVNET_TCP_FAIL:
+            case HCI_EVENT_TCP_CONNECT_LOG:
+            case HCI_EVENT_BNEP_CHIP_ERROR:
+            case HCI_EVENT_DNS_RSP:
+            case HCI_EVENT_BB_DISCONNECT:
+            case HCI_EVENT_BT_NAME:
+            case HCI_EVENT_UDP_INFO:
+                //save hci_rec_state_event package is ok
+                break;
+
+            case HCI_EVENT_CMD_COMPLETE:
+
+                switch(hci_rec_state_event.p_data[0])
+                {
+                    case HCI_CMD_SET_BT_ADDR:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_BT_ADDR);
+                        }
+                        break;
+
+                    case HCI_CMD_SET_BLE_ADDR:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_BLE_ADDR);
+                        }
+                        break;
+
+                    case HCI_CMD_SET_VISIBILITY:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_VISIBILITY);
+                        }
+                        break;
+
+                    case HCI_CMD_SET_BT_NAME:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_BT_NAME);
+                        }
+                        break;
+                    case HCI_CMD_SET_BLE_NAME:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_BLE_NAME);
+                        }
+                        break;
+
+                    case HCI_CMD_SEND_SPP_DATA:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SEND_SPP_DATA);
+                        }
+                        break;
+
+                    case HCI_CMD_SEND_BLE_DATA:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SEND_BLE_DATA);
+                        }
+                        break;
+
+                    case HCI_CMD_SET_PAIRING_MODE:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_PAIRING_MODE);
+                        }
+                        break;
+
+                    case HCI_CMD_SET_PINCODE:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_PINCODE);
+                        }
+                        break;
+                        
+                    case HCI_CMD_VERSION_REQUEST:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_GET_VERSION);
+                            CmdRepData[0] = hci_rec_state_event.p_data[2];
+                            CmdRepData[1] = hci_rec_state_event.p_data[3];
+                        }
+                        break;
+
+                    case HCI_CMD_LE_SET_PAIRING:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_LE_SET_PARING);
+                        }
+                        break;
+
+                    case HCI_CMD_BT_DISCONNECT:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_BT_DISCONNECT);
+                        }
+                        break;
+
+                    case HCI_CMD_BLE_DISCONNECT:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_BLE_DISCONNECT);
+                        }
+                        break;
+                    case HCI_CMD_SET_NVRAM:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_NVRAM);
+                        }
+                        break;
+
+                    case HCI_CMD_CONFIRM_GKEY:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_CONFIRM_GKEY);
+                        }
+                        break;
+
+                    case HCI_CMD_SET_LPM_MODE:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_LPM_MODE);
+                        }
+                        break;
+
+                    case HCI_CMD_PASSKEY_ENTRY:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_PASSKEY_ENTRY);
+                        }
+                        break;
+
+                    case HCI_CMD_LE_SET_ADV_DATA:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_LE_SET_ADV_DATA);
+                        }
+                    break;
+
+                    case HCI_CMD_LE_SET_SCAN_DATA:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_LE_SET_SCAN_DATA);
+                        }
+                    break;
+                    
+                    case HCI_CMD_LE_SEND_CONN_UPDATE_REQ:
+                        if(!hci_rec_state_event.p_data[1])
+                        {
+                            BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_LE_SEND_CONN_UPDATE);
+                        }
+                    break;
+                    case HCI_CMD_LE_SET_ADV_PARM:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_LE_SET_ADV_PARM);
+                    }							
+                    break;
+                    
+                    case HCI_CMD_SET_TX_POWER:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_TXPOWER);
+                    }							
+                    break;
+                    
+                    case HCI_CMD_REJECT_JUSTWORK:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_REJECT_JUSTWORK);
+                    }								
+                    break;
+                    
+                    case HCI_CMD_SET_COD:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                        BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_SET_COD);
+                    }
+                    break;
+                                            
+                    case HCI_CMD_DELETE_CUSTOMIZE_SERVICE:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                        BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_DELETE_SERVICE);
+                    }
+                    break;					
+            
+                    case HCI_CMD_LE_SET_FIXED_PASSKEY:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_LE_SET_FIXED_PASSKEY);
+                    }
+                    break;
+                    
+                    case HCI_CMD_CONNECT_BNEP:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_CONNECT_BNEP);
+                    }
+                    break;
+                    
+                    case HCI_CMD_CONNECT_TCP:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_CONNECT_TCP);
+                    }
+                    break;
+                    
+                    case HCI_CMD_DISCONNECT_TCP:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_DISCONNECT_TCP);
+                    }
+                    break;
+                    
+                    case HCI_CMD_BNEP_SEND_TCP_DATA:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_DATA);
+                    }
+                    break;
+                    
+                    case HCI_CMD_SEND_UDP_DATA:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_UDP_DATA);
+                    }
+                    break;
+                    
+                    case HCI_CMD_SEND_BIG_DATA:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA);
+                    }
+                    break;
+                    
+                    case HCI_CMD_DNS_REQ:
+                    if(!hci_rec_state_event.p_data[1])
+                    {
+                         BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_DNS_REQ);
+                    }
+                    break;
+                    
+                    default:
+                        break;
+                }
+                break;
+
+				case HCI_EVENT_I_AM_READY:
+					BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_MODULE_READY);
+					break;
+
+				case HCI_EVENT_STAUS_RESPONSE:
+					BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_STATUS_REQUEST);
+					CmdRepData[0] = hci_rec_state_event.p_data[0];
+					break;
+				
+				case HCI_EVENT_UUID_HANDLE:
+					BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_ADD_UUID);
+					CmdRepData[0] = hci_rec_state_event.p_data[0];
+					CmdRepData[1] = hci_rec_state_event.p_data[1];
+					break;
+				
+				case HCI_EVENT_BIG_DATA_FINISH:
+					if(hci_rec_state_event.p_data[0]==0x00)
+					{
+						BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA_FINISH);
+					}
+					break;
+				
+				case HCI_EVENT_BIG_DATA_STATUS:
+					if(hci_rec_state_event.p_data[0]==0x03||hci_rec_state_event.p_data[0]==0x04)
+					{
+						BT_BIT_SET(HCI_CMD_BIT_FLAG,BIT_BNEP_SEND_TCP_BIG_DATA_STATUS);
+					}
+					break;
+
+				default:
+                    #if BT_LOG
+                    MyPrintf("bt event err");
+                    #endif
+					break;
+			}
+    }
+    IPC_HOLD_BT = 0;
+    WAKEUP_BT &= ~(1<<WAKEUP_BT_FLAG);
+}
+
+//extern Boolean prt_bt_ble_flow_proc(uint8_t package_cnt);
+//extern void prt_bt_ble_set_mtu(void);
+
+uint8_t credit_num = 0;
+extern uint32_t prt_get_bt_data(void)
+
+{
+	int eventCmd;
+    int BtDataLen =0;
+    uint16_t handler = 0;
+    uint16_t ret = 0;
+    const uint16_t subscribe = 0x0014;
+    
+    if (hci_buf_write_index == hci_buf_read_index)
+    {
+        if (bt2m0buf_full_flag == 1)
+        {
+			enable_intr(INTR_BT);
+            BT_CONFIG |= ((1<<BT_INIT_FLAG));
+        }
+        return -1;
+    }
+    else
+    {
+        hci_buf_read_index++;
+        if (hci_buf_read_index >= HCI_BT_NUM)
+        {
+            hci_buf_read_index = 0;
+        }
+        
+        eventCmd = hci_recappdata[hci_buf_read_index].opcode;
+    }
+	switch(eventCmd)
+	{	
+	case BT_DATA:
+        #if BT_LOG
+        GPIO_CONFIG(10) = 0x3e;
+        #endif
+        credit_num++;
+        if (credit_num >= 2)
+        {
+            #if (MEM_SPP_FLOWCTRL_FLAG_VALUE==0x01)
+            if(BT_SetSppFlowcontrol(2) != TRUE)
+            {
+                MyPrintf("set spp flow failed\n");
+            }
+            #endif
+            #if BT_LOG
+            MyPrintf("BT_DATA\n");
+            #endif
+            
+            credit_num = 0;
+        }
+        #if BT_LOG
+        GPIO_CONFIG(10) = 0x3f;
+        #endif
+        UART_SendBuf(UART0, hci_recappdata[hci_buf_read_index].p_data, hci_recappdata[hci_buf_read_index].DataLen);
+        break;
+
+	case BLE_DATA:
+//        ret = hci_recappdata[hci_buf_read_index].p_data[0];
+//        handler = hci_recappdata[hci_buf_read_index].p_data[1];
+//        handler <<= 8;
+//        handler += ret;
+//        #if BT_LOG
+//        MyPrintf("BLE_DATA\n");
+//        #endif
+//        UART_SendBuf(UART0, hci_recappdata[hci_buf_read_index].p_data, hci_recappdata[hci_buf_read_index].DataLen);
+
+//        if (prt_is_ble_data(handler) == TRUE){
+//            prt_bt_ble_flow_proc(1);
+//        } else {          
+//            if (prt_ble_first_get() == FALSE) {
+//                MyPrintf("handler:%x,%d\n", handler, (handler == subscribe));
+//                if (handler == subscribe) {
+//                    prt_ble_first_set(TRUE);
+//                    prt_bt_ble_flow_proc(12);
+//                    prt_bt_ble_set_mtu();
+//                }
+//            }
+//            return 0;
+//        }
+
+        break;
+    case -1:
+        return 0;
+    default:
+        return 0;
+    }
+    return BtDataLen;
+}
+
+
Index: BT_doublebyte/sdk/Librarier/sdk/yc_ipc2.h
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_ipc2.h	(nonexistent)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_ipc2.h	(working copy)
@@ -0,0 +1,116 @@
+#ifndef __YC_IPC2_H__
+#define __YC_IPC2_H__
+
+#include "yc3121.h"
+#include "yc_timer.h"
+#include "bt_code.h"
+#include "misc.h"
+#include "yc_lpm.h"
+
+#define IPCREVSIZE                 256
+#define HW_REG_8BIT(reg, value)    (*((volatile uint8_t *)((uint32_t)reg)) = value)
+#define HR_REG_8BIT(reg)           (*(volatile uint8_t *)((uint32_t)reg))
+#define HR_REG_16BIT(reg)          ((uint16_t)(HR_REG_8BIT(reg) |((HR_REG_8BIT(reg+1)<<8)&0xff00)))
+#define HW_REG_16BIT(reg,value)    do{HW_REG_8BIT(reg,value&0x00ff);HW_REG_8BIT(reg + 1,value>>8);}while(0)
+#define HR_IPC_REG_8BIT(reg)       HR_REG_8BIT(reg|M0_BASE_ADDR)
+#define HW_IPC_REG_8BIT(reg,value) HW_REG_8BIT(reg|M0_BASE_ADDR,value)
+
+#define RB_UPDATE_PTR(p,s,e)       ((p) == (e))?((p)=(s)):((p)++)
+#define M0_BASE_ADDR               0xC0000
+
+#define MEM_SPP_FLOWCTRL_FLAG_ADDR    (0xc453d)
+#define MEM_SPP_FLOWCTRL_FLAG_VALUE   (1)	//0x00:disable flowctrl  0x01:enable flowctrl
+
+
+#define MEM_RFCOMM_LMP_DIS_FLAG_ADDR	(0xc4acf)
+#define MEM_RFCOMM_LMP_DIS_FLAG_VALUE	(0x00)	//0x00:?? rfcomm disconnected ???lmp ???  0x01:??rfcomm disconnected??lmp
+
+
+
+#pragma pack(1)
+typedef struct
+{
+    uint16_t IpcStartAddr;
+    uint16_t IpcEndAddr;
+    uint16_t ReadPtr;
+    uint16_t WrtiePtr;
+} IPC_TypeDef;
+#pragma pack()
+
+/**
+  * @brief  yichip HCI CMD Structure definition
+  */
+#pragma pack(1)
+typedef struct
+{
+    uint8_t type;
+    uint8_t opcode;
+    uint16_t DataLen;
+    uint8_t *p_data;
+} HCI_TypeDef;
+#pragma pack()
+
+/**
+  * @brief   Ipc send data
+	*
+  * @param   IpcData: HCI_TypeDef
+  *
+  * @retval	 SUCCESS or ERROR
+  */
+void IPC_TxData(HCI_TypeDef *IpcData);
+
+///**
+//  * @brief   Ipc Put Bt Data
+//	*
+//  * @param   *buf: Conform to format HCI_TypeDefs
+//  *
+//	* @param   size: *buf data len(bte),max value is 0xff+3=258
+//  *
+//  * @retval	 TRUE or FALSE
+//  */
+//Boolean IPC_PutBtData(const void *buf, uint32_t size);
+
+/**
+  * @brief   IpcReadBtData(get A complete package)
+	*
+  * @param   IpcData: HCI_TypeDef
+  *
+  * @retval	 TRUE or FALSE
+  */
+Boolean IPC_ReadBtData(HCI_TypeDef *IpcData);
+Boolean IPC_ReadBtData_DMA(HCI_TypeDef *IpcData);
+
+/**
+  * @brief   get opcode val
+	*
+  * @param   none
+  *
+  * @retval	 val*/
+uint8_t IPC_ReadIPC_Opcode(void);
+/**
+  * @brief   get ipc buf available size
+	*
+  * @param   none
+  *
+  * @retval	 available size
+  */
+uint16_t IPC_get_available_size(void);
+
+/**
+  * @brief   get ipc buf available size
+	*
+  * @param   none
+  *
+  * @retval	 TRUE:have data  FALSE:not data
+  */
+Boolean IPC_have_data(void);
+
+void IpcInit(void);
+
+void PrintHCIPack(HCI_TypeDef *msg, const char *str);
+    
+
+
+#endif
+
+
Index: BT_doublebyte/sdk/Librarier/sdk/yc_ipc2.c
===================================================================
--- /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_ipc2.c	(nonexistent)
+++ /YC3121_demo/ModuleDemo/BlueTooth/BT_doublebyte/sdk/Librarier/sdk/yc_ipc2.c	(working copy)
@@ -0,0 +1,371 @@
+#include "yc_ipc2.h"
+
+//#define	IPC_DEBUG
+
+IPC_TypeDef *IpcRx = (IPC_TypeDef *)(IPC_RX_START_ADDR);
+IPC_TypeDef *IpcTx = (IPC_TypeDef *)(IPC_TX_START_ADDR);
+
+uint8_t bt_lpm_mode=0;
+
+static void noinline _dma_start(int ch, const void *src, int srclen, void *dest, int dstlen)
+{
+    DMA_SRC_ADDR(ch)  = (int)src;
+    DMA_DEST_ADDR(ch) = (int)dest;
+    DMA_LEN(ch)       = srclen << 16 | dstlen;
+    DMA_START(ch)     = 0x80;
+}
+
+void noinline _dma_wait(int ch)
+{
+    while (!(DMA_STATUS(ch) & 1));
+}
+
+void _dmacopy(void *dest, void *src, int len)
+{
+    _dma_start(DMACH_MEMCP, src, len, dest, len);
+    _dma_wait(DMACH_MEMCP);
+}
+
+Boolean IPC_have_data()
+{
+    return HR_REG_16BIT(&IpcRx->ReadPtr) != HR_REG_16BIT(&IpcRx->WrtiePtr) ? TRUE : FALSE;
+}
+
+uint16_t IPC_get_available_size()
+{
+    uint16_t readtx = HR_REG_16BIT(&IpcTx->ReadPtr);
+    uint16_t wrtptr = HR_REG_16BIT(&IpcTx->WrtiePtr);
+    uint16_t ipcendaddr = HR_REG_16BIT(&IpcTx->IpcEndAddr);
+    uint16_t ipcstartaddr = HR_REG_16BIT(&IpcTx->IpcStartAddr);
+
+    if (readtx > wrtptr)
+    {
+        return readtx - wrtptr;
+    }
+    else
+    {
+        return ((ipcendaddr - ipcstartaddr + 1) - (readtx - wrtptr));
+    }
+}
+
+void IPC_TxData(HCI_TypeDef *IpcData)
+{
+	#ifdef IPC_DEBUG
+	PrintHCIPack(IpcData,"tx");
+	#endif
+    WAKEUP_BT |= (1 << WAKEUP_BT_FLAG);
+    IPC_HOLD_BT  = 1;
+    //delay_ms(10);
+    uint16_t Wptr  = HR_REG_16BIT(&IpcTx->WrtiePtr);
+    uint16_t Len   = IpcData->DataLen + 3;
+    uint8_t *Rptr = (uint8_t *)IpcData;
+    while (Len--)
+    {
+        HW_IPC_REG_8BIT(Wptr, *Rptr++);
+        RB_UPDATE_PTR(Wptr, HR_REG_16BIT(&IpcTx->IpcStartAddr),  HR_REG_16BIT(&IpcTx->IpcEndAddr));
+        if (Len == IpcData->DataLen)
+            Rptr = IpcData->p_data;
+    }
+
+    HW_REG_16BIT(&IpcTx->WrtiePtr, (uint32_t)Wptr);
+    IPC_HOLD_BT = 0;
+    WAKEUP_BT &= ~(1 << WAKEUP_BT_FLAG);
+}
+
+//Boolean IPC_PutBtData(const void *buf, uint32_t size)
+//{
+//    if (size == 0 || size > 258)
+//    {
+//        return FALSE;
+//    }
+//    WAKEUP_BT |= (1 << WAKEUP_BT_FLAG);
+//    IPC_HOLD_BT  = 1;
+//    delay_ms(10);
+//    uint16_t Wptr  = HR_REG_16BIT(&IpcTx->WrtiePtr);
+//    uint8_t *Rptr = (uint8_t *)buf;
+//    while (size--)
+//    {
+//        HW_IPC_REG_8BIT(Wptr, *Rptr++);
+//        RB_UPDATE_PTR(Wptr, HR_REG_16BIT(&IpcTx->IpcStartAddr),  HR_REG_16BIT(&IpcTx->IpcEndAddr));
+//    }
+
+//    HW_REG_16BIT(&IpcTx->WrtiePtr, (uint32_t)Wptr);
+//    IPC_HOLD_BT = 0;
+//    WAKEUP_BT &= ~(1 << WAKEUP_BT_FLAG);
+//    return TRUE;
+//}
+
+Boolean IPC_ReadBtData(HCI_TypeDef *IpcData)
+{
+	uint16_t DataLenTemp;
+    if (HR_REG_16BIT(&IpcRx->ReadPtr) != HR_REG_16BIT(&IpcRx->WrtiePtr))
+    {
+        uint16_t Rptr = HR_REG_16BIT(&IpcRx->ReadPtr);
+        IpcData->type = HR_IPC_REG_8BIT(Rptr);
+        RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+        IpcData->opcode = HR_IPC_REG_8BIT(Rptr);
+        RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+        DataLenTemp = HR_IPC_REG_8BIT(Rptr);
+        RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+        IpcData->DataLen = HR_IPC_REG_8BIT(Rptr);
+        RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+		IpcData->DataLen <<=8;
+		IpcData->DataLen += DataLenTemp;
+
+        for (uint16_t i = 0; i < IpcData->DataLen; i++)
+        {
+            IpcData->p_data[i] = HR_IPC_REG_8BIT(Rptr);
+            RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+        }
+        HW_REG_16BIT(&IpcRx->ReadPtr, (uint32_t)Rptr);
+		#ifdef IPC_DEBUG
+		PrintHCIPack(IpcData,"rx");
+		#endif
+        return TRUE;
+    }
+    else
+    {
+        return FALSE;
+    }
+}
+
+Boolean IPC_ReadBtData_DMA(HCI_TypeDef *IpcData)
+{
+	uint16_t DataLenTemp,ret,index=0;
+    uint16_t start_addr = 0, end_addr = 0;
+    int addr;
+    
+    if (HR_REG_16BIT(&IpcRx->ReadPtr) != HR_REG_16BIT(&IpcRx->WrtiePtr))
+    {
+        uint32_t Rptr = HR_REG_16BIT(&IpcRx->ReadPtr);
+        if (Rptr <= 0x1ffb)
+        {
+            IpcData->type = HR_IPC_REG_8BIT(Rptr++);
+            IpcData->opcode = HR_IPC_REG_8BIT(Rptr++);
+            DataLenTemp = HR_IPC_REG_8BIT(Rptr++);
+            IpcData->DataLen = HR_IPC_REG_8BIT(Rptr++);
+            HW_REG_16BIT(&IpcRx->ReadPtr, (uint32_t)(Rptr));
+        }
+        else
+        {
+            IpcData->type = HR_IPC_REG_8BIT(Rptr);
+            RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+            IpcData->opcode = HR_IPC_REG_8BIT(Rptr);
+            RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+            DataLenTemp = HR_IPC_REG_8BIT(Rptr);
+            RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+            IpcData->DataLen = HR_IPC_REG_8BIT(Rptr);
+            RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+        }
+		IpcData->DataLen <<=8;
+		IpcData->DataLen += DataLenTemp;
+        
+        DataLenTemp = IpcData->DataLen;
+        
+        start_addr = HR_REG_16BIT(&IpcRx->IpcStartAddr);
+        end_addr = HR_REG_16BIT(&IpcRx->IpcEndAddr);
+
+        ret = DataLenTemp + Rptr - 1;
+
+        if (ret >= end_addr)
+        {
+            DataLenTemp = end_addr - Rptr + 1;
+            addr = Rptr | 0xc0000;
+            
+            //copy first
+            _dmacopy((byte *)&IpcData->p_data[0], (byte *)addr, DataLenTemp);
+            HW_REG_16BIT(&IpcRx->ReadPtr, (uint32_t)start_addr);
+            
+            Rptr = start_addr;
+            
+            index = DataLenTemp;
+            
+            DataLenTemp = IpcData->DataLen - DataLenTemp;
+        }
+        if (DataLenTemp > 0)
+        {
+            addr = Rptr | 0xc0000;
+            _dmacopy((byte *)&IpcData->p_data[index], (byte *)addr, DataLenTemp);
+            HW_REG_16BIT(&IpcRx->ReadPtr, (uint32_t)(Rptr+DataLenTemp));
+        }
+
+		#ifdef IPC_DEBUG
+		PrintHCIPack(IpcData,"rx");
+		#endif
+        return TRUE;
+    }
+    else
+    {
+        return FALSE;
+    }
+}
+
+uint8_t IPC_ReadIPC_Opcode(void)
+{
+    uint8_t val = 0;
+    
+    uint16_t Rptr = HR_REG_16BIT(&IpcRx->ReadPtr);
+    if (Rptr != 0x1fff)
+    {
+        val = HR_IPC_REG_8BIT(Rptr++);    //type
+        val = HR_IPC_REG_8BIT(Rptr);    //opcode
+    }
+    else
+    {
+        val = HR_IPC_REG_8BIT(Rptr);    //type
+        RB_UPDATE_PTR(Rptr, HR_REG_16BIT(&IpcRx->IpcStartAddr),  HR_REG_16BIT(&IpcRx->IpcEndAddr));
+        val = HR_IPC_REG_8BIT(Rptr);    //opcode
+    }
+    return val;
+}
+
+void _download_btcode(const byte *btcode)
+{
+    int addr, len;
+    btcode += 2;
+    if (GETWORD(btcode) != 0x55aa)
+        return;
+    btcode += 2;
+    enable_clock(CLKCLS_BT);
+    len = GETWORD(btcode);
+    btcode += 2;
+    BT_UCODE_LO   = 0;
+    BT_UCODE_HI   = 0;
+    BT_UCODE_CTRL = 0x80;
+    DMA_CONFIG(DMACH_MEMCP) = 8;
+    _dmacopy((byte *)&BT_UCODE_DATA, (byte *)btcode, len);
+    BT_UCODE_CTRL = 0;
+    btcode       += len;
+    DMA_CONFIG(DMACH_MEMCP) = 0;
+
+    while (GETWORD(btcode) == 0x55aa)
+    {
+        btcode += 2;
+        len  = GETWORD(btcode);
+        btcode += 2;
+        addr = GETWORD(btcode) | 0xc0000;
+        btcode += 2;
+        if(addr==MEM_SPP_FLOWCTRL_FLAG_ADDR && len==0x0004)
+        {
+            byte mem_spp_flowctrl_buf[4];
+            mem_spp_flowctrl_buf[0]=MEM_SPP_FLOWCTRL_FLAG_VALUE;
+            memcpy((byte*)&mem_spp_flowctrl_buf[1],btcode+1,3);
+            _dmacopy((byte *)addr, mem_spp_flowctrl_buf, len);
+        }
+        else if(addr==MEM_RFCOMM_LMP_DIS_FLAG_ADDR && len==0x0001)
+        {
+            byte mem_rfcomm_lmp_dis_flag;
+            mem_rfcomm_lmp_dis_flag=MEM_RFCOMM_LMP_DIS_FLAG_VALUE;
+            _dmacopy((byte *)addr, (byte *)&mem_rfcomm_lmp_dis_flag, len);
+        }
+        else
+        {
+          _dmacopy((byte *)addr, (byte *)btcode, len);
+        }
+        btcode += len;
+    }
+}
+
+//extern uint8_t BT_Wake;
+extern const unsigned char bt_code[];
+
+void erase_memory(void)
+{
+    for (uint32_t i = 0; i < 0x1000; i++)
+    {
+        *(volatile uint8_t *)(0xc0000 + i) = 0;
+        *(volatile uint8_t *)(0xc4000 + i) = 0;
+    }
+}
+#if (VERSIONS == NO_BT)
+static uint16_t get_dpll_status(void)
+{
+    uint16_t addr = 0;
+    addr  = (*(uint8_t *)(0xc800e));
+    addr += (*(uint8_t *)(0xc800f)) << 8;
+    return addr;
+}
+
+static Boolean is_dpll_opend(void)
+{
+    uint16_t addr =  get_dpll_status();
+
+    Boolean ret = FALSE;
+
+    if ((0x4c77 == addr) || (0x4c78 == addr) || (0x4c79 == addr))
+    {
+        ret = TRUE;
+    }
+    else
+    {
+        ret = FALSE;
+    }
+    MyPrintf("is_dpll_opend=%x-%x", addr, ret);
+    return ret;
+}
+#endif
+
+Boolean ipc_inited = FALSE;
+void IpcInit(void)
+{
+#if (VERSIONS == EXIST_BT)
+    if (ipc_inited == FALSE)
+    {
+        disable_intr(INTR_BT);
+        ipc_inited = TRUE;
+        SYSCTRL_ROM_SWITCH = 0x9c;
+ //       delay(10000);   //wait rom switch ok
+				delay_ms(1);
+        BT_RESET = 1;
+ //       delay(10000);   //wait for reset ok
+				delay_ms(1);
+        while (!(BT_CONFIG & (1 << BT_INIT_FLAG)));
+        BT_CONFIG &= (~(1 << BT_INIT_FLAG));
+        erase_memory();
+        _download_btcode(bt_code);
+        WAKEUP_BT = 0x9c;
+        delay_ms(100);
+    }
+#elif (VERSIONS == NO_BT)
+    if (ipc_inited == FALSE || is_dpll_opend() == FALSE)
+    {
+        disable_intr(INTR_BT);
+        ipc_inited = TRUE;
+
+        lpm_bt_write(1, 0xd00000);
+        delay_us(1000);
+
+        SYSCTRL_ROM_SWITCH = 0x94;
+        delay(10000);   //wait rom switch ok
+        BT_RESET = 1;
+        delay(10000);   //wait for reset ok
+        while (!(BT_CONFIG & (1 << BT_INIT_FLAG)));
+        BT_CONFIG &= (~(1 << BT_INIT_FLAG));
+        *(volatile uint8_t *)0xC4176 = 0x0f;
+        *(volatile uint8_t *)0xC4177 = 0x84;
+        *(volatile uint8_t *)0xC4178 = 0x30;
+        *(volatile uint8_t *)0xC4179 = 0x0a;
+        *(volatile uint8_t *)0xC40c3 = 0x00;
+        *(volatile uint8_t *)0xC40c4 = 0x4f;
+        *(volatile uint8_t *)0xC4f00 = 0xff;
+        delay_ms(60);
+    }
+#endif
+}
+
+//鎵撳嵃HCI鏁版嵁鍖?
+void PrintHCIPack(HCI_TypeDef *msg, const char *str)
+{
+    MyPrintf("M0 %s:\n", str);
+    MyPrintf("%s Type:%02x\n", str, msg->type);
+    MyPrintf("%s OPCode: %02x\n", str, msg->opcode);
+    MyPrintf("%s Length: %02x\n", str, msg->DataLen);
+    MyPrintf("%s Data:\n", str);
+    for (uint8_t cnt = 0; cnt < msg->DataLen; cnt++)
+    {
+        MyPrintf("%02x ", msg->p_data[cnt]);
+        if ((cnt % 16) == 0&&cnt!=0)
+            MyPrintf("\n");
+    }
+    MyPrintf("\n");
+}
