/********************************** (C) COPYRIGHT *******************************
* File Name          : gattprofile.h
* Author             : WCH
* Version            : V1.0
* Date               : 2018/12/11
* Description        : 
            
*******************************************************************************/

#ifndef GATTPROFILE_H
#define GATTPROFILE_H
#include "CONFIG.h"
#ifdef __cplusplus
extern "C"
{
#endif

/*********************************************************************
 * INCLUDES
 */

/*********************************************************************
 * CONSTANTS
 */

// Profile Parameters
#define SIMPLEPROFILE_CHAR1                   0  // RW uint8 - Profile Characteristic 1 value 
#define SIMPLEPROFILE_CHAR2                   1  // RW uint8 - Profile Characteristic 2 value
#define SIMPLEPROFILE_CHAR3                   2  // RW uint8 - Profile Characteristic 3 value
#define SIMPLEPROFILE_CHAR4                   3  // RW uint8 - Profile Characteristic 4 value
#define SIMPLEPROFILE_CHAR5                   4  // RW uint8 - Profile Characteristic 4 value
  
// Simple Profile Service UUID
#define SIMPLEPROFILE_SERV_UUID               0xFFE0
    
// Key Pressed UUID
#define SIMPLEPROFILE_CHAR1_UUID              0xFFE1
#define SIMPLEPROFILE_CHAR2_UUID              0xFFE2
#define SIMPLEPROFILE_CHAR3_UUID              0xFFE3
#define SIMPLEPROFILE_CHAR4_UUID              0xFFE4
#define SIMPLEPROFILE_CHAR5_UUID              0xFFE5
  
// Simple Keys Profile Services bit fields
#define SIMPLEPROFILE_SERVICE                 0x00000001

// Length of characteristic in bytes ( Default MTU is 23 )
#define SIMPLEPROFILE_CHAR1_LEN               BLE_BUFF_MAX_LEN-4  
#define SIMPLEPROFILE_CHAR2_LEN               4  
//#define SIMPLEPROFILE_CHAR3_LEN             1  
#define SIMPLEPROFILE_CHAR4_LEN               BLE_BUFF_MAX_LEN-4  
#define SIMPLEPROFILE_CHAR5_LEN               1  

#define BLE_DEVICE_NAME_LEN   (GAP_DEVICE_NAME_LEN - 1)        //蓝牙设备名称长度
#define BLE_INFO_NUM                       5               //蓝牙接收缓存个数

#define SRAM_BLE_RECEIVE_START   0x031000      																			//BLE接收缓存起始地址
#define SRAM_BLE_RECEIVE_END     (0x031000 + BLE_BUFF_MAX_LEN*BLE_INFO_NUM -1)      //BLE接收缓存结束地址
#define SRAM_BLE_SEND_START      (SRAM_BLE_RECEIVE_END+1)     												//BLE发送缓存起始地址
#define SRAM_BLE_SEND_END        (SRAM_BLE_SEND_START+BLE_BUFF_MAX_LEN-1)     			//BLE发送缓存起始地址
#define SRAM_BLE_SAVE_START      (SRAM_BLE_SEND_END+ 1)     											//BLE保存参数起始地址
#define SRAM_BLE_END     					0x0313FF      																	//BLE缓存结束地址

typedef struct DEVICE_CONFIG*  DEVICE_CONFIG_Ptr;   //配置信息指针

typedef struct DEVICE_CONFIG{
	
	uint8    device_type;                             //设备类型
	uint8    device_addr;                             //设备地址
	uint8    ble_type:1;                             //蓝牙类型，0:主机/1:从机
	uint8    connect_sta:1;                          //连接状态， 1：连接  0：未连接
	uint8    uuid_type:1;                            //UUID类型，1:128bit， 0:16bit
	uint8    connecting:1;                          //正在连接标记， 1：正在连接  0：未发起连接
	uint8    addr_type:3;                            //蓝牙mac地址类型
	uint8    mac[6];                                 //对应的mac地址
	uint16   server_uuid;                          //起始服务uuid
	uint16   char_uuid;                            //特征uuid
	uint8    uuid_128bit[16];                      //128bit UUID,在uuid类型为128bit时使用
	DEVICE_CONFIG_Ptr  next;                      //下一个节点
	
}__attribute__ ((__packed__))BLE_DEVICE_CONFIG;                              //蓝牙设备配置信息

#define BLE_TYPE_CENTRAL            0             //蓝牙主机
#define BLE_TYPE_PERIPHERAL         1             //蓝牙从机

#define UUID_TYPE_16BIT             0             //16bit UUID
#define UUID_TYPE_128BIT            1             //128bit UUID

#define BLE_DATA_TYPE_DATA          0             //数据类型
#define BLE_DATA_TYPE_CMD           1             //命令类型
//typedef struct{
//	
//	uint8    ble_type:1;                           //蓝牙类型，主机/从机
//	uint8    
//	uint8    addr:5;                               //设备地址、编号
//	uint16   server_uuid;                          //服务uuid
//	uint16   char_uuid;                            //特征uuid
//	
//}BLE_DEVICE_INFO;
typedef enum{
	BLE_ERR_SUCCESS = 0,                                 //操作成功
	BLE_ERR_WAITING_CONNECT = 1,                         //等待连接
	BLE_ERR_WAITING_SEND = 2,                         		//等待发送
	BLE_ERR_CONNECT_FAIL = 3,                            //发起连接失败
	BLE_ERR_DEVICE_Not_EXIST = 4,                        //设备不存在
	BLE_ERR_FAIL = 5,                                    //操作失败
	BLE_ERR_INVALID = 6,                                 //操作无效
	
}BLE_STA;

#pragma pack(1)
typedef struct{
	
	uint8    ble_type:1;                           //目标蓝牙类型，0:主机/1:从机
	uint8    send_flg:1;                           //发送标志
	uint8   wait_connected:1;                      //等待连接标志 1:等待连接
	uint8   wait_send:1;                           //等待发送标志 1:等待发送
	uint8   data_type:1;                           //数据类型,0:数据; 1:命令（打开关闭从机 NOTIFY）
	uint8   send_state:3;                         //发送状态
	uint16   wait_count;                      		//等待连接计数 最大只能到
	uint8   device_type;                             //设备类型
	uint8   device_addr;                             //设备地址
//	uint8    sendDATA[BLE_BUFF_MAX_LEN];           //蓝牙发送缓存
//	uint8    sendLEN;                              //发送数据长度
	uint16   server_handle;                          //服务句柄
	uint16   char_handle;                            //特征句柄
	uint8   notify_id:4;                             //通知句柄id(编号)
	uint8   write_id:4;                             //写句柄id(编号)
	uint32 BLE_SendAddr;                            //发送缓存地址
	uint32 BLE_WriteAddr;                           //接收缓存写地址
	uint32 BLE_ReadAddr;                            //接收缓存读地址
}BLE_SEND_INFO;


#pragma pack()

extern BLE_SEND_INFO         ble_send_info;
extern BLE_DEVICE_CONFIG*   Ble_Device_Info_Ptr;
/*********************************************************************
 * TYPEDEFS
 */

  
/*********************************************************************
 * MACROS
 */

/*********************************************************************
 * Profile Callbacks
 */

// Callback when a characteristic value has changed
typedef void (*simpleProfileChange_t)( uint8 paramID );

typedef struct
{
  simpleProfileChange_t        pfnSimpleProfileChange;  // Called when characteristic value changes
} simpleProfileCBs_t;

extern unsigned char attDeviceName[BLE_DEVICE_NAME_LEN];

/*********************************************************************
 * API FUNCTIONS 
 */


/*
 * SimpleProfile_AddService- Initializes the Simple GATT Profile service by registering
 *          GATT attributes with the GATT server.
 *
 * @param   services - services to add. This is a bit map and can
 *                     contain more than one service.
 */

extern bStatus_t SimpleProfile_AddService( uint32 services );

/*
 * SimpleProfile_RegisterAppCBs - Registers the application callback function.
 *                    Only call this function once.
 *
 *    appCallbacks - pointer to application callbacks.
 */
extern bStatus_t SimpleProfile_RegisterAppCBs( simpleProfileCBs_t *appCallbacks );

/*
 * SimpleProfile_SetParameter - Set a Simple GATT Profile parameter.
 *
 *    param - Profile parameter ID
 *    len - length of data to right
 *    value - pointer to data to write.  This is dependent on
 *          the parameter ID and WILL be cast to the appropriate 
 *          data type (example: data type of uint16 will be cast to 
 *          uint16 pointer).
 */
extern bStatus_t SimpleProfile_SetParameter( uint8 param, uint8 len, void *value );
  
/*
 * SimpleProfile_GetParameter - Get a Simple GATT Profile parameter.
 *
 *    param - Profile parameter ID
 *    value - pointer to data to write.  This is dependent on
 *          the parameter ID and WILL be cast to the appropriate 
 *          data type (example: data type of uint16 will be cast to 
 *          uint16 pointer).
 */
extern bStatus_t SimpleProfile_GetParameter( uint8 param, void *value );

/*
 * simpleProfile_Notify - Send notification.
 *
 *    connHandle - connect handle
 *    pNoti - pointer to structure to notify.  
 */
extern bStatus_t simpleProfile_Notify( uint16 connHandle, attHandleValueNoti_t *pNoti );

/*********************************************************************
*********************************************************************/

void Ble_Parameter_Init(void);
void Ble_Recive_Task(void);


#ifdef __cplusplus
}
#endif

#endif
