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

/* This code belongs in ble_cus.h*/
//#define CUSTOM_SERVICE_UUID_BASE         {0xBC, 0x8A, 0xBF, 0x45, 0xCA, 0x05, 0x50, 0xBA, \
//                                          0x40, 0x42, 0xB0, 0x00, 0xC9, 0xAD, 0x64, 0xF3}

#define CUSTOM_SERVICE_UUID_BASE         {0xF0, 0xFF}


#define CUSTOM_SERVICE_UUID               0x1400
#define CUSTOM_VALUE_CHAR_UUID_1          0x1401
#define CUSTOM_VALUE_CHAR_UUID_2          0x1402
#define CUSTOM_VALUE_CHAR_UUID_3          0x1403
#define CUSTOM_VALUE_CHAR_UUID_4          0x1404
#define CUSTOM_VALUE_CHAR_UUID_5          0x1405
#define CUSTOM_VALUE_CHAR_UUID_6          0x1406
#define CUSTOM_VALUE_CHAR_UUID_7          0x1407
#define CUSTOM_VALUE_CHAR_UUID_8          0x1408
#define CUSTOM_VALUE_CHAR_UUID_9          0x1409
#define CUSTOM_VALUE_CHAR_UUID_10         0x140A

/* This code belongs in ble_cus.h*/

/**@brief   Macro for defining a ble_cus instance.
 *
 * @param   _name   Name of the instance.
 * @hideinitializer
 */
#define BLE_CUS_DEF(_name)                                                                          \
static ble_cus_t _name;                                                                             \
NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                     BLE_HRS_BLE_OBSERVER_PRIO,                                                     \
                     ble_cus_on_ble_evt, &_name)


/**@brief Custom Service init structure. This contains all options and data needed for
 *        initialization of the service.*/
typedef struct
{
    uint8_t                       initial_custom_value;           /**< Initial custom value */
    ble_srv_cccd_security_mode_t  custom_value_char_attr_md;     /**< Initial security level for Custom characteristics attribute */
} ble_cus_init_t;

/**@brief Custom Service structure. This contains various status information for the service. */
struct ble_cus_s
{
    uint16_t                      service_handle;                 /**< Handle of Custom Service (as provided by the BLE stack). */
    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). */
    uint8_t                       uuid_type; 
    ble_gatts_char_handles_t      custom_value_handles_1;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_2;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_3;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_4;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_5;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_6;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_7;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_8;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_9;           /**< Handles related to the Custom Value characteristic. */
    ble_gatts_char_handles_t      custom_value_handles_10;          /**< Handles related to the Custom Value characteristic. */
};    

// Forward declaration of the ble_cus_t type.
typedef struct ble_cus_s ble_cus_t;

/**@brief Function for handling the Application's BLE Stack events.
 *
 * @details Handles all events from the BLE stack of interest to the Battery Service.
 *
 * @note 
 *
 * @param[in]   p_ble_evt  Event received from the BLE stack.
 * @param[in]   p_context  Custom Service structure.
 */
void ble_cus_on_ble_evt( ble_evt_t const * p_ble_evt, void * p_context);

 /* This code belongs in ble_cus.h*/

/**@brief Function for updating the custom value.
 *
 * @details The application calls this function when the cutom value should be updated. If
 *          notification has been enabled, the custom value characteristic is sent to the client.
 *
 * @note 
 *       
 * @param[in]   p_cus          Custom Service structure.
 * @param[in]   Custom value 
 *
 * @return      NRF_SUCCESS on success, otherwise an error code.
 */

uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value,uint8_t char_no);