#include "ble_mpu.h"
#include "ble_srv_common.h"
#include "sdk_common.h"


/**@brief Function for handling the Connect event.
 *
 * @param[in] p_lbs      LED Button Service structure.
 * @param[in] p_ble_evt  Event received from the BLE stack.
 */
static void on_connect(ble_mpus_t * p_lbs, ble_evt_t * p_ble_evt)
{
    p_lbs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
}


/**@brief Function for handling the Disconnect event.
 *
 * @param[in] p_lbs      LED Button Service structure.
 * @param[in] p_ble_evt  Event received from the BLE stack.
 */
static void on_disconnect(ble_mpus_t * p_lbs, ble_evt_t * p_ble_evt)
{
    UNUSED_PARAMETER(p_ble_evt);
    p_lbs->conn_handle = BLE_CONN_HANDLE_INVALID;
}

void ble_mpus_on_ble_evt(ble_mpus_t * p_lbs, ble_evt_t * p_ble_evt)
{
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_lbs, p_ble_evt);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            on_disconnect(p_lbs, p_ble_evt);
            break;
            
        case BLE_GATTS_EVT_WRITE:
            //on_write(p_lbs, p_ble_evt);
            break;

        default:
            // No implementation needed.
            break;
    }
}
/**********************************************************************************************
 * 描  述 : 添加THI特性(thi是温度、湿度、人体红外检测的缩写)
 * 参  数 : p_lbs[in]:指向thi服务结构体
 *          p_lbs_init[in]:thi服务初始化结构体
 * 返回值 : 成功返回NRF_SUCCESS，否则返回错误代码
 ***********************************************************************************************/ 
static uint32_t mpu_char_add(ble_mpus_t * p_lbs, const ble_mpus_init_t * p_lbs_init)
{
    ble_gatts_char_md_t char_md;
    ble_gatts_attr_md_t cccd_md;
    ble_gatts_attr_t    attr_char_value;
    ble_uuid_t          ble_uuid;
    ble_gatts_attr_md_t attr_md;

    memset(&cccd_md, 0, sizeof(cccd_md));

	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
	cccd_md.vloc = BLE_GATTS_VLOC_STACK;

	memset(&char_md, 0, sizeof(char_md));

	char_md.char_props.read = 1;
	char_md.char_props.notify = 1;
	char_md.p_char_user_desc  = NULL;
	char_md.p_char_pf         = NULL;
	char_md.p_user_desc_md    = NULL;
	char_md.p_cccd_md         = NULL;
	char_md.p_cccd_md         = &cccd_md;

	ble_uuid.type = p_lbs->uuid_type;
	ble_uuid.uuid = MPUS_UUID_MPU_CHAR;

	memset(&attr_md, 0, sizeof(attr_md));

	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
	BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
	attr_md.vloc       = BLE_GATTS_VLOC_STACK;
	attr_md.rd_auth    = 0;
	attr_md.wr_auth    = 0;
	attr_md.vlen       = 0;

	memset(&attr_char_value, 0, sizeof(attr_char_value));

	attr_char_value.p_uuid       = &ble_uuid;
	attr_char_value.p_attr_md    = &attr_md;

	attr_char_value.init_len     = 12;
	attr_char_value.init_offs    = 0;
	attr_char_value.max_len      = 24;
	attr_char_value.p_value      = NULL;

	return sd_ble_gatts_characteristic_add(p_lbs->service_handle,
                                               &char_md,
                                               &attr_char_value,
                                               &p_lbs->mpu_char_handles);
}
uint32_t ble_mpus_init(ble_mpus_t * p_mpus, const ble_mpus_init_t * p_mpus_init)
{
    uint32_t   err_code;
    ble_uuid_t ble_uuid;

    // Initialize service structure.
    p_mpus->conn_handle       = BLE_CONN_HANDLE_INVALID;
    //p_mpus->led_write_handler = p_mpus_init->led_write_handler;

    // Add service.
    ble_uuid128_t base_uuid = {LBS_UUID_BASE};
    err_code = sd_ble_uuid_vs_add(&base_uuid, &p_mpus->uuid_type);
    VERIFY_SUCCESS(err_code);

    ble_uuid.type = p_mpus->uuid_type;
    ble_uuid.uuid = MPUS_UUID_SERVICE;

    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_mpus->service_handle);
    VERIFY_SUCCESS(err_code);

    // Add characteristics.
    err_code = mpu_char_add(p_mpus, p_mpus_init);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    return NRF_SUCCESS;
}

/**********************************************************************************************
 * 描  述 : 使用notification将三轴加速度、三轴陀螺仪、三轴磁力传感、温度数据发送给主机
 * 参  数 : ble_mpus_t * p_mpus, 
			int16_t *pAVal, 三轴加速度
			int16_t *pGVal, 三轴陀螺仪
			int16_t *pMVal, 三轴磁力传感
			int16_t *pTVal, 温度
 * 返回值 : 成功返回NRF_SUCCESS，否则返回错误代码
 ***********************************************************************************************/ 
uint16_t ble_mpu9250_on_data_read(ble_mpus_t * p_mpus, int16_t *pAVal, int16_t *pGVal, int16_t *pMVal, int16_t *pTVal)
{
	ble_gatts_hvx_params_t params;
	const uint16_t len = 24;
	uint32_t err_code;
	uint8_t  i=0,mpu9250_val[len]={0x0};

	for(i=0;i<3;i++)
	{ 
		mpu9250_val[i*2] = (uint8_t)(*(pAVal+i)>>8);
		mpu9250_val[i*2+1] = (uint8_t)(*(pAVal+i));
		mpu9250_val[i*2+6] = (uint8_t)(*(pGVal+i)>>8);
		mpu9250_val[i*2+7] = (uint8_t)(*(pGVal+i));
		mpu9250_val[i*2+12] = (uint8_t)(*(pMVal+i)>>8);
		mpu9250_val[i*2+13] = (uint8_t)(*(pMVal+i));
	}
	mpu9250_val[18] = (uint8_t)(*(pTVal)>>8);
	mpu9250_val[19] = (uint8_t)(*(pTVal));
	mpu9250_val[20] = '0';
	mpu9250_val[21] = '0';
	mpu9250_val[22] = '0';
	mpu9250_val[23] = '0';
	
	//for(i=0;i<12;i++)app_uart_put(mpu9250_val[i]);
	if (p_mpus->conn_handle != BLE_CONN_HANDLE_INVALID)
	{
		memset(&params, 0, sizeof(params));
		params.type = BLE_GATT_HVX_NOTIFICATION;
		params.handle = p_mpus->mpu_char_handles.value_handle;
		params.p_data = mpu9250_val;
		params.p_len = (uint16_t *)&len;

		return sd_ble_gatts_hvx(p_mpus->conn_handle, &params);
	}
	else
	{
		err_code = NRF_ERROR_INVALID_STATE;
	}
	return err_code;
    
}

/********************************************END FILE*******************************************/

