
#ifndef _DEVICENAME_H__
#define _DEVICENAME_H__
// <<< Use Configuration Wizard in Context Menu >>>\n

#include <stdint.h>
#include "ble.h"
#include "nrf_sdh_ble.h"
#include "ble_srv_common.h"
	

#ifdef __cplusplus
extern "C" {
#endif
 
// <h> nRF_BLE 
// <q> BLE_DNS_ENABLED  - ble_dns - Device Name Service
 

#ifndef BLE_DNS_ENABLED
#define BLE_DNS_ENABLED 1
#endif

// <o> BLE_DNS_BLE_OBSERVER_PRIO  - Observer prioritry
 

#ifndef BLE_DNS_BLE_OBSERVER_PRIO
#define BLE_DNS_BLE_OBSERVER_PRIO 2
#endif

// <o> BLE_DNS_DEV_NAME_LEN - Device Name maximum lenght
	
#ifndef BLE_DNS_DEV_NAME_MAX_LEN
#define BLE_DNS_DEV_NAME_MAX_LEN 6
#endif

// <o> BLE_DNS_DEV_NAME_LEN - Group Name maximum lenght
	
#ifndef BLE_DNS_GRP_NAME_MAX_LEN
#define BLE_DNS_GRP_NAME_MAX_LEN 6
#endif

// <o> BLE_DNS_DEV_NAME_LEN - Subgroup Name maximum lenght
	
#ifndef BLE_DNS_SUBGRP_NAME_MAX_LEN
#define BLE_DNS_SUBGRP_NAME_MAX_LEN 6
#endif


/**@brief   Macro for defining a ble_dns instance.
 *
 * @param   _name   Name of the instance.
 * @hideinitializer
 */
#define BLE_DNS_DEF( _name )						\
static ble_dns_t _name;								\
NRF_SDH_BLE_OBSERVER( _name ## _obs,				\
					  BLE_DNS_BLE_OBSERVER_PRIO,	\
					  ble_dns_on_ble_evt, &_name )
	
// Device Name Service UUID: 902dc2ea-29a0-45d9-8a75-cf09e476bb39
// Device Name Data I/O Characteristic UUID: 902dc2ea-29a0-45d9-8a75-cf09e476bb40
// The bytes need to be in reverse order to match the UUID from the spec
// Note: They are stored in little endian, meaning that the Least Significant Byte
// is stored first.
#define BLE_UUID_DNS_BASE_UUID					{ 0x90, 0x2d, 0xc2, 0xea, 0x29, 0xa0, 0x45, 0xd9, \
												  0x8a, 0x75, 0xcf, 0x09, 0x00, 0x00, 0x00, 0x00 } // 128-bit base UUID
//												  0x8a, 0x75, 0xcf, 0x09, 0xe4, 0x76, 0x00, 0x00 } // 128-bit base UUID

#define DNS_UUID_SERVICE						0xBB39	/**< Device Name Service UUID */
#define DNS_UUID_GROUP_ID_CHAR					0xBB40	/**< Group id charcteristic UUID */
#define DNS_UUID_DEVICE_NAME_CHAR				0xBB41	/**< Device name characteristic UUID */
#define DNS_UUID_SUBGROUP_ID_CHAR				0xBB42	/**< Group id charcteristic UUID */
#define DNS_UUID_GROUP_NAME_STRING_CHAR			0xBB43	/**< Group name string characteristic UUID */
#define DNS_UUID_SUBGROUP_NUMBER_STRING_CHAR	0xBB44	/**< Subgroup string characteristic UUID */

typedef struct ble_dns_s 		ble_dns_t;
typedef struct ble_dns_init_s	ble_dns_init_t;

typedef void ( *ble_dns_group_id_handler )	( ble_dns_t *p_dns, uint8_t id );
typedef void ( *ble_dns_name_handler_t )	( ble_dns_t *p_dns, const uint8_t *name, uint16_t length );
/**@brief Device Name Service structure. This contains various status information for the service.
 *
 * @details 
 */
struct ble_dns_init_s
{
    uint8_t			             	group_id;               	/**< Group ID. */
    uint8_t			             	subgroup_id;            	/**< Subgroup ID. */
	ble_srv_utf8_str_t 				group_name_str;				/**< Group Name String */
	ble_srv_utf8_str_t 				device_name_str;			/**< Device Name String */
    ble_srv_utf8_str_t             	subgroup_name_str;			/**< Serial Number String. */
	ble_srv_utf8_str_t				user_desctcriptor_str;		/**< Characteristic user descriptor */
    ble_srv_security_mode_t			dns_attr_md;				/**< Initial Security Setting for Device Information Characteristics. */
	ble_dns_name_handler_t 			data_io_handler;			/**< Handle of data io */
};
//

struct ble_dns_s
{
	uint8_t 						uuid_type;					/**< UUID type */
	
	uint16_t 						conn_handle;				/**< Connection handle */
	uint16_t						service_handle;				/**< Service handle */
	
	ble_gatts_char_handles_t		group_id_char_handle;		/**< Group id characteristic handle */
	ble_gatts_char_handles_t		group_name_char_handle;		/**< Group name characteristic handle */
	ble_gatts_char_handles_t		device_name_char_handle;	/**< Device name characteristic handle */
	ble_gatts_char_handles_t		subgroup_id_char_handle;	/**< Subgroup id characteristic handle */
	ble_gatts_char_handles_t		subgroup_name_char_handle;	/**< Subgroup name characteristic handle */
	
	ble_dns_group_id_handler		group_id_handler;			/**< Handler for group id */
	ble_dns_name_handler_t			group_name_handler;			/**< Handler for group name */
	ble_dns_name_handler_t			device_name_handler;		/**< Handler for device name */
	ble_dns_group_id_handler		subgroup_id_handler;		/**< Handler for subgroup id */
	ble_dns_name_handler_t			subgroup_name_handler;		/**< Handler for subgroup name */
};
//

/**@brief Function for handling BLE Stack events related to our service and characteristic.
 *
 * @details Handles all events from the BLE stack of interest to Our Service.
 *
 * @param[in]   p_dns       Device Name Service structure.
 * @param[in]   p_ble_evt  Event received from the BLE stack.
 */
void ble_dns_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context );

/**@brief Function for initializing our new service.
 *
 * @param[in]   p_dns       Pointer to Device Name Service structure.
 */
uint32_t ble_dns_init( ble_dns_t * p_dns, ble_dns_init_t const * p_dns_init );

 
#ifdef __cplusplus
}
#endif
 
#endif  /* _ OUR_SERVICE_H__ */
