/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 */
 
/**
 * @file ble_uwbrange.h
 * @brief Declarations of uwbrange service (ble)
 * @version 6.0
 * @date 27/06/2017
 */

#ifndef BLE_UWBRANGE_H__
#define BLE_UWBRANGE_H__

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

// BLE UUIDs
#define LBS_UUID_BASE {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF} /**< Base UUID. */

#define BLE_UUID_UWBRANGE_SERVICE						0x1100			/**< UWB Range Service UUID. */
#define BLE_UUID_UWBRANGE_INST_MEASUREMENT_CHAR			0x1101			/**< UWB Range instant range characteristic UUID. */
#define BLE_UUID_UWBRANGE_AVG_MEASUREMENT_CHAR			0x1102			/**< UWB Range average range characteristic UUID. */
#define BLE_UUID_UWBRANGE_PERIOD_CONFIGURATION_CHAR		0x1103			/**< UWB Range period configuration characteristic UUID. */
#define BLE_UUID_UWBRANGE_UWB_CONFIGURATION_CHAR		0x1104			/**< UWB Range signal configuration characteristic UUID. */
#define BLE_UUID_UWBRANGE_ROLE_CONFIGURATION_CHAR		0x1105			/**< UWB Range role configuration characteristic UUID. */
#define BLE_UUID_UWBRANGE_AVG_CONFIGURATION_CHAR		0x1106			/**< UWB Range average configuration characteristic UUID. */
#define BLE_UUID_UWBRANGE_THRESHOLD_CONFIGURATION_CHAR	0x1107			/**< UWB Range threshold configuration characteristic UUID. */
#define BLE_UUID_UWBRANGE_STATUS_CHAR					0x1108			/**< UWB Range status characteristic UUID. */
#define BLE_UUID_UWBRANGE_CONTROLS_CHAR					0x1109			/**< UWB Range controls characteristic UUID. */


#define BLE_DIST_STATUS_ENABLE		 	0
#define BLE_DIST_STATUS_STHRESHOLD 		1
#define BLE_DIST_STATUS_LTHRESHOLD 		2
#define BLE_DIST_STATUS_DISCMODE 		3
#define BLE_DIST_STATUS_RANGINGMODE 	4


/**@brief UWB Range Service event type. */
typedef enum
{
    BLE_DIST_EVT_NOTIFICATION_ENABLED,			/**< Distance value notification enabled event. */
    BLE_DIST_EVT_NOTIFICATION_DISABLED,       	/**< Distance value notification disabled event. */
	BLE_DIST_EVT_PERIOD_CHANGED,				/**< Period changed event. */
	BLE_DIST_EVT_UWB_CHANGED,					/**< Channel changed event. */
	BLE_DIST_EVT_ROLE_CHANGED,					/**< Mode changed event. */
	BLE_DIST_EVT_AVG_CHANGED,					/**< Average changed event. */	
	BLE_DIST_EVT_THRESHOLD_CHANGED,				/**< Thresholds changed event. */
	BLE_DIST_EVT_CONTROLS_CHANGED				/**< Controls changed event. */
} ble_uwbrange_evt_type_t;

/**@brief UWB Range Service event. */
typedef struct
{
    ble_uwbrange_evt_type_t evt_type;  			/**< Type of event. */
	uint32_t value;     						/**< Value of event. */
} ble_uwbrange_evt_t;

// Forward declaration of the ble_uwbrange_t type. 
typedef struct ble_uwbrange_s ble_uwbrange_t;

/**@brief UWB Range Service event handler type. */
typedef void (*ble_uwbrange_evt_handler_t) (ble_uwbrange_t * p_bas, ble_uwbrange_evt_t * p_evt);

/**@brief UWB Range Service init structure. This contains all options and data needed for
 *        initialization of the service.*/
typedef struct
{
	ble_srv_report_ref_t *        p_report_ref;
    ble_uwbrange_evt_handler_t    evt_handler;                    				/**< Event handler to be called for handling events in the Accelerometer Service. */
    bool                          support_notification;           				/**< TRUE if notification of Accelerometer Level measurement is supported. */
	
	ble_srv_cccd_security_mode_t  uwbrange_inst_measurement_char_attr_md;     	/**< Initial security level for uwbrange instant measurement characteristics attribute */
    ble_gap_conn_sec_mode_t       uwbrange_inst_measurement_report_read_perm; 	/**< Initial security level for uwbrange instant measurement report read attribute */
	double                        uwbrange_inst_measurement_initial_value;    	/**< Initial value */
	
	ble_srv_cccd_security_mode_t  uwbrange_avg_measurement_char_attr_md;     	/**< Initial security level for uwbrange avg measurement characteristics attribute */
    ble_gap_conn_sec_mode_t       uwbrange_avg_measurement_report_read_perm; 	/**< Initial security level for uwbrange avg measurement report read attribute */
	double                        uwbrange_avg_measurement_initial_value;     	/**< Initial value */
	
	ble_srv_cccd_security_mode_t  period_configuration_char_attr_md;     		/**< Initial security level for period configuration characteristics attribute */
    ble_gap_conn_sec_mode_t       period_configuration_report_read_perm; 		/**< Initial security level for period configuration  report read attribute */
	uint16_t                      period_configuration_initial_value;     		/**< Initial value */
	
	ble_srv_cccd_security_mode_t  channel_configuration_char_attr_md;     		/**< Initial security level for channel configuration characteristics attribute */
    ble_gap_conn_sec_mode_t       channel_configuration_report_read_perm; 		/**< Initial security level for channel configuration  report read attribute */
	uint16_t                       channel_configuration_initial_value;     	/**< Initial value */
	
	ble_srv_cccd_security_mode_t  mode_configuration_char_attr_md;     			/**< Initial security level for mode configuration characteristics attribute */
    ble_gap_conn_sec_mode_t       mode_configuration_report_read_perm; 			/**< Initial security level for mode configuration  report read attribute */
	uint8_t                       mode_configuration_initial_value;     		/**< Initial value */
	
	ble_srv_cccd_security_mode_t  avg_configuration_char_attr_md;     			/**< Initial security level for avg configuration characteristics attribute */
    ble_gap_conn_sec_mode_t       avg_configuration_report_read_perm; 			/**< Initial security level for avg configuration  report read attribute */
	uint8_t                       avg_configuration_initial_value;     			/**< Initial value */
	
	ble_srv_cccd_security_mode_t  threshold_configuration_char_attr_md;     	/**< Initial security level for threshold configuration characteristics attribute */
    ble_gap_conn_sec_mode_t       threshold_configuration_report_read_perm; 	/**< Initial security level for threshold configuration report read attribute */
	uint32_t                      threshold_configuration_initial_value;     	/**< Initial value */
	
	ble_srv_cccd_security_mode_t  status_char_attr_md;     						/**< Initial security level for events characteristics attribute */
    ble_gap_conn_sec_mode_t       status_report_read_perm; 						/**< Initial security level for events report read attribute */
	uint8_t                       status_initial_value;     					/**< Initial value */
	
	ble_srv_cccd_security_mode_t  controls_char_attr_md;     					/**< Initial security level for controls characteristics attribute */
    ble_gap_conn_sec_mode_t       controls_report_read_perm; 					/**< Initial security level for controls report read attribute */
	uint32_t                      controls_initial_value;     					/**< Initial value */
	
} ble_uwbrange_init_t;

/**@brief UWB Range Service structure. This contains various status information for the service. */
typedef struct ble_uwbrange_s
{
    ble_uwbrange_evt_handler_t    evt_handler;                    				/**< Event handler to be called for handling events in the Accelerometer Service. */
    uint16_t                      service_handle;                 				/**< Handle of uwbrange Service (as provided by the BLE stack). */
    ble_gatts_char_handles_t      uwbrange_inst_measurement_handles;        	/**< Handles related to the instant uwbrange Level characteristic. */
	ble_gatts_char_handles_t      uwbrange_avg_measurement_handles;         	/**< Handles related to the average uwbrange Level characteristic. */
	ble_gatts_char_handles_t      period_configuration_handles;          		/**< Handles related to the period_configuration characteristic. */
	ble_gatts_char_handles_t      uwb_configuration_handles;          			/**< Handles related to the uwb_configuration characteristic. */
	ble_gatts_char_handles_t      role_configuration_handles;          			/**< Handles related to the role_configuration characteristic. */
	ble_gatts_char_handles_t      avg_configuration_handles;          			/**< Handles related to the avg_configuration characteristic. */
	ble_gatts_char_handles_t      threshold_configuration_handles;          	/**< Handles related to the threshold_configuration characteristic. */
	ble_gatts_char_handles_t      status_handles;          						/**< Handles related to the status characteristic. */
	ble_gatts_char_handles_t      controls_handles;          					/**< Handles related to the controls characteristic. */
    uint16_t                      report_ref_handle;              				/**< Handle of the Report Reference descriptor. */
    uint16_t                      conn_handle;                    				/**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
    bool                          is_notification_supported;      				/**< TRUE if notification of uwbrange Level is supported. */
	uint8_t						  uuid_type;
} ble_uwbrange_t;

void on_uwbrange_connect (ble_uwbrange_t *, ble_evt_t *);
void on_uwbrange_disconnect (ble_uwbrange_t *, ble_evt_t *);
void on_uwbrange_write (ble_uwbrange_t *, ble_evt_t *);

uint32_t ble_uwbrange_init(ble_uwbrange_t * p_uwbrange, const ble_uwbrange_init_t * p_uwbrange_init);
uint32_t ble_uwbrange_instant_measurement_update(ble_uwbrange_t * p_uwbrange, double distance);
uint32_t ble_uwbrange_avg_measurement_update(ble_uwbrange_t * p_uwbrange, double distance);
uint32_t ble_uwbrange_status_update(ble_uwbrange_t * p_uwbrange, uint8_t events);
void ble_uwbrange_on_ble_evt(ble_uwbrange_t * p_uwbrange, ble_evt_t * p_ble_evt);

#endif
