/*
 * The MIT License (MIT)
 * Copyright (c) 2018 Novel Bits
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */
 
#ifndef MODE_SERVICE_H
#define MODE_SERVICE_H
 
#include <stdint.h>
#include "boards.h"
#include "ble.h"
#include "ble_srv_common.h"
#include "nrf_sdh_ble.h"
#include "mode_select.h"

//   Mode service:          B67E0001-3C89-4FC8-A96D-8E1C06943426
//   Mode characteristic:   B67E0002-3C89-4FC8-A96D-8E1C06943426 
//   Mode Base UUID:        B67E0000-3C89-4FC8-A96D-8E1C06943426

#define BLE_UUID_MODE_SERVICE_BASE_UUID  {0x26, 0x34, 0x94, 0x06, 0x1C, 0x8E, 0x6D, 0xA9, 0xC8, 0x4F, 0x89, 0x3C, 0x00, 0x00, 0x7E, 0xB6}
#define BLE_UUID_MODE_SERVICE_UUID       0x0001
#define BLE_UUID_MODE_CHAR_UUID          0x0002

#define BLE_MODE_SERVICE_BLE_OBSERVER_PRIO 2

/**@brief   Macro for defining a ble_mode_service instance.
 *
 * @param   _name   Name of the instance.
 * @hideinitializer
 */
#define BLE_MODE_SERVICE_DEF(_name)                                                                          \
static ble_mode_service_t _name;                                                                             \
NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                          \
                     BLE_MODE_SERVICE_BLE_OBSERVER_PRIO,                                                     \
                     ble_mode_service_on_ble_evt, &_name)
 

// Forward declaration of the custom_service_t type.
typedef struct ble_mode_service_s ble_mode_service_t;
 
typedef void (*ble_mode_service_mode_change_handler_t) (uint16_t conn_handle, ble_mode_service_t * p_mode_service, uint8_t new_state);


/** @brief WBAN Mode Service init structure. This structure contains all options and data needed for
 *        initialization of the service.*/
typedef struct
{
    ble_mode_service_mode_change_handler_t mode_change_handler; /**< Event handler to be called when the WBAN Mode Characteristic is written. */
} ble_mode_service_init_t;
 

/**@brief WBAN Mode Service structure.
 *        This contains various status information
 *        for the service.
 */
typedef struct ble_mode_service_s
{
    uint16_t                            conn_handle;
    uint16_t                            service_handle;
    uint8_t                             uuid_type;
    ble_gatts_char_handles_t            mode_char_handles;
    ble_mode_service_mode_change_handler_t mode_change_handler;
 
} ble_mode_service_t;
 

// Function Declarations
 
/**@brief Function for initializing the WBAN Mode Service.
 *
 * @param[out]  p_mode_service    WBAN Mode Service structure. This structure will have to be supplied by
 *                                the application. It will be initialized by this function, and will later
 *                                be used to identify this particular service instance.
 *
 * @return      NRF_SUCCESS on successful initialization of service, otherwise an error code.
 */
uint32_t mode_service_init(ble_mode_service_t * p_mode_service, const ble_mode_service_init_t * p_mode_service_init);

 
/**@brief Function for handling the application's BLE stack events.
 *
 * @details This function handles all events from the BLE stack that are of interest to the WBAN Mode Service.
 *
 * @param[in] p_ble_evt  Event received from the BLE stack.
 * @param[in] p_context  WBAN Mode Service structure.
 */
void ble_mode_service_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
 
#endif // MODE_SERVICE_H 