/* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the license.txt file.
 */
#include "sdk_common.h"
#include "ble_battery_service.h"
#include "ble_srv_common.h"
#include "app_error.h"
#include "SEGGER_RTT.h"

#include "adc.h"
void update_battery_values(struct ble_battery_service_handle_t *p_battery_service)
{
	uint32_t err_code;
	ble_gatts_value_t gatts_value;
	if (p_battery_service->conn_handle != BLE_CONN_HANDLE_INVALID)
	{
		for(int i = 0; i < 3; i++)
		{
			if(voltages_device.cell[i] != p_battery_service->voltage_cell_last[i])
			{
				memset(&gatts_value, 0, sizeof(gatts_value));
				gatts_value.len = 4;
				gatts_value.offset = 0;
				gatts_value.p_value = (uint8_t *) &voltages_device.cell[i];
				
				p_battery_service->voltage_cell_last[i] = voltages_device.cell[i];
				
				err_code = sd_ble_gatts_value_set(p_battery_service->conn_handle, p_battery_service->cell_char_handles[i].value_handle, &gatts_value);
				APP_ERROR_CHECK(err_code);
			}
		}
		
		uint16_t               len = 4;
		ble_gatts_hvx_params_t hvx_params;
		memset(&hvx_params, 0, sizeof(hvx_params));

		hvx_params.handle = p_battery_service->pack_char_handles.value_handle;
		hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
		hvx_params.offset = 0;
		hvx_params.p_len  = &len;
		hvx_params.p_data = (uint8_t*)&voltages_device.input;

		sd_ble_gatts_hvx(p_battery_service->conn_handle, &hvx_params);
	}
}

static uint32_t pack_char_add(struct ble_battery_service_handle_t * p_battery_service)
{
	uint32_t            err_code;
	ble_uuid_t          char_uuid;
	ble_uuid128_t       base_uuid = BATTERY_UUID_BASE;

	char_uuid.uuid      = BATTERY_UUID_PACK_CHAR;

	err_code = sd_ble_uuid_vs_add(&base_uuid, &char_uuid.type);
	APP_ERROR_CHECK(err_code);

	// Configuring Client Characteristic Configuration Descriptor metadata and add to char_md structure
	ble_gatts_attr_md_t cccd_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;    

	// OUR_JOB: Step 2.F Add read/write properties to our characteristic
	ble_gatts_char_md_t char_md;
	memset(&char_md, 0, sizeof(char_md));
	char_md.char_props.read   = 1;
	char_md.char_props.write  = 0;
	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         = &cccd_md;
	char_md.p_sccd_md         = NULL;

	// OUR_JOB: Step 2.B, Configure the attribute metadata
	ble_gatts_attr_md_t attr_md;
	memset(&attr_md, 0, sizeof(attr_md));  
	attr_md.vloc = BLE_GATTS_VLOC_STACK;
	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
	BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);

	// OUR_JOB: Step 2.C, Configure the characteristic value attribute
	ble_gatts_attr_t    attr_char_value;
	memset(&attr_char_value, 0, sizeof(attr_char_value));
	attr_char_value.p_uuid      = &char_uuid;
	attr_char_value.p_attr_md   = &attr_md;
	attr_char_value.max_len     = 4;
	attr_char_value.init_len    = 4;
	attr_char_value.p_value     = (uint8_t *) &voltages_device.input;

	// OUR_JOB: Step 2.E, Add our new characteristic to the service
	err_code = sd_ble_gatts_characteristic_add(p_battery_service->service_handle,
												&char_md,
												&attr_char_value,
												&p_battery_service->pack_char_handles);
	APP_ERROR_CHECK(err_code);

	return NRF_SUCCESS;
}
static uint32_t cell_chars_add(struct ble_battery_service_handle_t * p_battery_service)
{
	uint32_t            err_code;
	ble_uuid_t          char_uuid;
	ble_uuid128_t       base_uuid = BATTERY_UUID_BASE;

	char_uuid.uuid      = BATTERY_UUID_CELL_CHAR;

	err_code = sd_ble_uuid_vs_add(&base_uuid, &char_uuid.type);
	APP_ERROR_CHECK(err_code);

	ble_gatts_char_md_t char_md;
	memset(&char_md, 0, sizeof(char_md));
	char_md.char_props.read   = 1;
	char_md.char_props.write  = 0;
	char_md.char_props.notify = 0;
	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_sccd_md         = NULL;

	ble_gatts_attr_md_t attr_md;
	memset(&attr_md, 0, sizeof(attr_md));  
	attr_md.vloc = BLE_GATTS_VLOC_STACK;

	BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
	BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);

	ble_gatts_attr_t    attr_char_value;
	memset(&attr_char_value, 0, sizeof(attr_char_value));
	attr_char_value.p_uuid      = &char_uuid;
	attr_char_value.p_attr_md   = &attr_md;
	attr_char_value.max_len     = 4;
	attr_char_value.init_len    = 4;
	attr_char_value.p_value     = (uint8_t *) &voltages_device.cell[0];

	err_code = sd_ble_gatts_characteristic_add(p_battery_service->service_handle,
												&char_md,
												&attr_char_value,
												&p_battery_service->cell_char_handles[0]);
	APP_ERROR_CHECK(err_code);

	attr_char_value.p_value     = (uint8_t *) &voltages_device.cell[1];

	err_code = sd_ble_gatts_characteristic_add(p_battery_service->service_handle,
												&char_md,
												&attr_char_value,
												&p_battery_service->cell_char_handles[1]);
	APP_ERROR_CHECK(err_code);

	attr_char_value.p_value     = (uint8_t *) &voltages_device.cell[2];

	err_code = sd_ble_gatts_characteristic_add(p_battery_service->service_handle,
												&char_md,
												&attr_char_value,
												&p_battery_service->cell_char_handles[2]);
	APP_ERROR_CHECK(err_code);

	return NRF_SUCCESS;
}

void ble_battery_service_init(struct ble_battery_service_handle_t * p_battery_service)
{
	uint32_t err_code;
	ble_uuid_t service_uuid;
	ble_uuid128_t base_uuid = {BATTERY_UUID_BASE};

	service_uuid.uuid = BATTERY_UUID_SERVICE;

	err_code = sd_ble_uuid_vs_add(&base_uuid, &service_uuid.type);
	APP_ERROR_CHECK(err_code);

	p_battery_service->conn_handle = BLE_CONN_HANDLE_INVALID;

	err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
										&service_uuid,
										&p_battery_service->service_handle);
	APP_ERROR_CHECK(err_code);
	
	SEGGER_RTT_WriteString(0, "Exectuing our_service_init().\n");
//	SEGGER_RTT_printf(0, "Service UUID: 0x%#04x\n", service_uuid.uuid);
//	SEGGER_RTT_printf(0, "Service UUID type: 0x%#02x\n", service_uuid.type);
//	SEGGER_RTT_printf(0, "Service handle: 0x%#04x\n", p_test_service->service_handle);
	pack_char_add(p_battery_service);
	cell_chars_add(p_battery_service);
}

void ble_battery_on_ble_evt(struct ble_battery_service_handle_t * p_battery_service, ble_evt_t * p_ble_evt)
{
	// OUR_JOB: Step 3.D Implement switch case handling BLE events related to our service.
	switch (p_ble_evt->header.evt_id)
	{
		case BLE_GAP_EVT_CONNECTED:
			p_battery_service->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
			break;
		case BLE_GAP_EVT_DISCONNECTED:
			p_battery_service->conn_handle = BLE_CONN_HANDLE_INVALID;
			break;
		default:
			// No implementation needed.
			break;
	}
}

