#ifndef BLE_DMX_SERVICE_C_H__
#define BLE_DMX_SERVICE_C_H__

#include <stdint.h>
#include "ble.h"
#include "ble_db_discovery.h"


#define DMX_UUID_BASE				{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xF0, 0x0F, 0xEE, \
									0xDD, 0xCC, 0xBB, 0xAA, 0x00, 0x00, 0x00, 0x00}
#define DMX_UUID_SERVICE			0x1502
#define DMX_UUID_CHANNEL_CHAR		0x1701
#define DMX_UUID_CHANNEL_GROUP_CHAR	0x1702

/**@brief DMX Client event type. */
typedef enum
{
	BLE_DMX_C_EVT_DISCOVERY_COMPLETE = 1,  /**< Event indicating that the DMX Service has been discovered at the peer. */
} ble_DMX_c_evt_type_t;

/**@brief Structure containing the handles related to the DMX Service found on the peer. */
typedef struct
{
	uint16_t channel_handle[16];       /**< Handle of the DMX characteristic as provided by the SoftDevice. */
	uint16_t channel_group_handle[2];  /**< Handle of the DMX characteristic as provided by the SoftDevice. */
} serv_c_DMX_handles_db;


/**@brief DMX Event structure. */
typedef struct
{
	ble_DMX_c_evt_type_t 	evt_type;		/**< Type of the event. */
	uint16_t				conn_handle;	/**< Connection handle on which the event occured.*/
	serv_c_DMX_handles_db	peer_db;		/**< LED Button Service related handles found on the peer device. This will be filled if the evt_type is @ref BLE_LBS_C_EVT_DISCOVERY_COMPLETE.*/
} ble_DMX_c_evt_t;

/* Connection handles for each receiver number */
extern uint8_t receiver_on_handle[8];
// Forward declaration of the ble_serv_c_DMX.
struct ble_serv_c_DMX;

/**@brief   Event handler type.
 *
 * @details This is the type of the event handler that should be provided by the application
 *          of this module in order to receive events.
 */
typedef void (* ble_serv_c_DMX_evt_han_t) (struct ble_serv_c_DMX * p_ble_lbs_c, ble_DMX_c_evt_t * p_evt);


/**@brief DMX Client structure. */
struct ble_serv_c_DMX
{
	uint16_t					conn_handle;  /**< Connection handle as provided by the SoftDevice. */
	uint8_t 					ble_address;  /* Bluetooth address of peer */
	int8_t						rssi;         /* RSSI of connection */
	serv_c_DMX_handles_db		peer_DMX_handles_db;  /**< Handles related to DMX on the peer */
	ble_serv_c_DMX_evt_han_t	evt_handler;  /**< Application event handler to be called when there is an event related to the DMX service. */
	uint8_t						uuid_type;    /**< UUID type. */
};

/**@brief DMX Client initialization structure. */
typedef struct
{
	ble_serv_c_DMX_evt_han_t evt_handler;  /**< Event handler to be called by the DMX Client module whenever there is an event related to the LED Button Service. */
} ble_serv_c_DMX_init_t;


/**@brief Function for initializing the DMX client module.
 *
 * @details This function will register with the DB Discovery module. There it registers for the
 *          LED Button Service. Doing so will make the DB Discovery module look for the presence
 *          of a LED Button Service instance at the peer when a discovery is started.
 *
 * @param[in] p_ble_lbs_c      Pointer to the LED Button client structure.
 * @param[in] p_ble_lbs_c_init Pointer to the LED Button initialization structure containing the
 *                             initialization information.
 *
 * @retval    NRF_SUCCESS On successful initialization. Otherwise an error code. This function
 *                        propagates the error code returned by the Database Discovery module API
 *                        @ref ble_db_discovery_evt_register.
 */
uint32_t ble_service_c_DMX_init(struct ble_serv_c_DMX * p_ble_serv_c_DMX, ble_serv_c_DMX_init_t * p_ble_DMX_c_init);

/**@brief Function for handling BLE events from the SoftDevice.
 *
 * @details This function will handle the BLE events received from the SoftDevice. If a BLE event
 *          is relevant to the LED Button Client module, then it uses it to update interval
 *          variables and, if necessary, send events to the application.
 *
 * @param[in] p_ble_lbs_c Pointer to the LED button client structure.
 * @param[in] p_ble_evt   Pointer to the BLE event.
 */
void ble_service_c_DMX_on_ble_evt(struct ble_serv_c_DMX * p_ble_serv_c_DMX, const ble_evt_t * p_ble_evt);


/**@brief Function for requesting the peer to start sending notification of the Button
 *        Characteristic.
 *
 * @details This function will enable to notification of the Button at the peer
 *          by writing to the CCCD of the Button Characteristic.
 *
 * @param[in] p_ble_lbs_c Pointer to the LED Button Client structure.
 *
 * @retval  NRF_SUCCESS If the SoftDevice has been requested to write to the CCCD of the peer.
 *                      Otherwise, an error code. This function propagates the error code returned
 *                      by the SoftDevice API @ref sd_ble_gattc_write.
 *          NRF_ERROR_INVALID_STATE if no connection handle has been assigned (@ref ble_lbs_c_handles_assign)
 *          NRF_ERROR_NULL if the given parameter is NULL
 */
uint32_t ble_DMX_c_button_notif_enable(struct ble_serv_c_DMX * p_ble_serv_c_DMX);


/**@brief Function for handling events from the database discovery module.
 *
 * @details Call this function when getting a callback event from the DB discovery module. This
 *          function will handle an event from the database discovery module, and determine if it
 *          relates to the discovery of LED Button service at the peer. If so, it will call the
 *          application's event handler indicating that the LED Button service has been discovered
 *          at the peer. It also populates the event with the service related information before
 *          providing it to the application.
 *
 * @param[in] p_ble_lbs_c Pointer to the LED Button client structure.
 * @param[in] p_evt Pointer to the event received from the database discovery module.
 */
void ble_service_c_DMX_disc_evt(struct ble_serv_c_DMX * p_ble_serv_c_DMX, const ble_db_discovery_evt_t * p_evt);


/**@brief     Function for assigning a Handles to this instance of lbs_c.
 *
 * @details Call this function when a link has been established with a peer to associate this link
 *          to this instance of the module. This makes it  possible to handle several links and
 *          associate each link to a particular instance of this module.
 *
 * @param[in] p_ble_lbs_c    Pointer to the LED Button client structure instance to associate.
 * @param[in] conn_handle    Connection handle to associate with the given LED Button Client Instance.
 * @param[in] p_peer_handles LED Button Service handles found on the peer (from @ref BLE_LBS_C_EVT_DISCOVERY_COMPLETE event).
 *
 */
uint32_t ble_DMX_c_handles_assign(struct ble_serv_c_DMX *    p_ble_serv_c_DMX,
								  uint16_t conn_handle,
								  const serv_c_DMX_handles_db * p_peer_handles);


/**@brief Function for writing the LED status to the connected server.
 *
 * @param[in] p_ble_lbs_c Pointer to the LED Button client structure.
 * @param[in] status      LED status to send.
 *
 * @retval NRF_SUCCESS If the staus was sent successfully. Otherwise, an error code is returned.
 */
uint32_t ble_DMX_channel_send(struct ble_serv_c_DMX * p_ble_serv_c_DMX, uint16_t channel, uint16_t value);
uint32_t ble_DMX_channel_group_send(struct ble_serv_c_DMX * p_ble_serv_c_DMX, uint8_t group, uint16_t * value);
#endif // BLE_DMX_SERVICE_C_H__
