/**
 * Copyright (c) 2015 - 2021, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
 *
 * @defgroup ble_lbs LED Button Service Server
 * @{
 * @ingroup ble_sdk_srv
 *
 * @brief LED Button Service Server module.
 *
 * @details This module implements a custom LED Button Service with an LED and Button Characteristics.
 *          During initialization, the module adds the LED Button Service and Characteristics
 *          to the BLE stack database.
 *
 *          The application must supply an event handler for receiving LED Button Service
 *          events. Using this handler, the service notifies the application when the
 *          LED value changes.
 *
 *          The service also provides a function for letting the application notify
 *          the state of the Button Characteristic to connected peers.
 *
 * @note    The application must register this module as BLE event observer using the
 *          NRF_SDH_BLE_OBSERVER macro. Example:
 *          @code
 *              ble_hids_t instance;
 *              NRF_SDH_BLE_OBSERVER(anything, BLE_HIDS_BLE_OBSERVER_PRIO,
 *                                   ble_hids_on_ble_evt, &instance);
 *          @endcode
 */

#ifndef BLE_CS_H__
#define BLE_CS_H__

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

#ifdef __cplusplus
extern "C" {
#endif

/**@brief   Macro for defining a ble_lbs instance.
 *
 * @param   _name   Name of the instance.
 * @hideinitializer
 */
#define BLE_CS_DEF(_name)                                                                          \
static ble_cs_t _name;                                                                             \
NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                     BLE_CS_BLE_OBSERVER_PRIO,                                                     \
                     ble_cs_on_ble_evt, &_name)

#define CS_UUID_BASE              {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, \
                                    0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
#define CS_UUID_SERVICE             0xFF00
#define CS_UUID_MAC_ADDRESS_CHAR    0xFF1C
#define CS_UUID_ERROR_MESSAGES_CHAR    0xFF1D


/**@brief Customize Service event type. */
typedef enum
{
    BLE_CS_EVT_NOTIFICATION_ENABLED, 
    BLE_CS_EVT_NOTIFICATION_DISABLED
} ble_cs_evt_type_t;


/**@brief Customize Service event. */
typedef struct
{
    ble_cs_evt_type_t   evt_type;    /**< Type of event. */
    uint16_t            conn_handle; /**< Connection handle. */
} ble_cs_evt_t;


// Forward declaration of the ble_lbs_t type.
typedef struct ble_cs_s ble_cs_t;

typedef void (*ble_cs_handler_t) (ble_cs_t * p_cs, ble_cs_evt_t * p_evt);


typedef struct
{
     ble_cs_handler_t       evt_handler;                    /**< Event handler to be called when the LED Characteristic is written. */
     bool                   support_notification;
     ble_srv_report_ref_t * p_report_ref;
     uint8_t                mac_address;
     uint8_t                initial_error_msg;
     security_req_t         bl_rd_sec;                      /**< Security requirement for reading the BL characteristic value. */
     security_req_t         bl_cccd_wr_sec;                 /**< Security requirement for writing the BL characteristic CCCD. */
     security_req_t         bl_report_rd_sec;               /**< Security requirement for reading the BL characteristic descriptor. */
} ble_cs_init_t;



struct ble_cs_s
{
    ble_cs_handler_t            evt_handler;               /**< Event handler to be called when the LED Characteristic is written. */
    uint16_t                    service_handle;            /**< Handle of LED Button Service (as provided by the BLE stack). */
    ble_gatts_char_handles_t    mac_char_handles;          /**< Handles related to the MAC Address Characteristic. */
    ble_gatts_char_handles_t    error_char_handles;        /**< Handles related to the Error Messages Characteristic. */
    uint16_t                    report_ref_handle;         /**< Handle of the Report Reference descriptor. */
    uint8_t                     uuid_type;                 /**< UUID type for the LED Button Service. */
    bool                        is_notification_supported; /**< TRUE if notification of Battery Level is supported. */
};


uint32_t ble_cs_init(ble_cs_t * p_cs, const ble_cs_init_t * p_cs_init);


void ble_cs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);

ret_code_t ble_cs_error_check_update(ble_cs_t * p_cs, uint8_t   error_messages, uint16_t   conn_handle);


#ifdef __cplusplus
}
#endif

#endif // BLE_CS_H__

/** @} */
