// Copyright (c) 2023 Ziga Miklosic
// All Rights Reserved
////////////////////////////////////////////////////////////////////////////////
/**
*@file      ble_c.h
*@brief     Bluetooth Centrol drivers
*@author    Ziga Miklosic
*@date      10.01.2023
*@version   V1.0.0
*
*@note      This file shall be in following directory:
*           
*               /drivers/peripheral/ble
*/
////////////////////////////////////////////////////////////////////////////////
/**
*@addtogroup BLE_C_API
* @{ <!-- BEGIN GROUP -->
*
*/
////////////////////////////////////////////////////////////////////////////////

#ifndef __BLE_C_H
#define __BLE_C_H

////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>

////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////

/**
 *  BLE Central status
 */
typedef enum
{
    eBLE_C_OK       = 0x00,     /**<Normal operation */
    eBLE_C_ERROR    = 0x01,     /**<General error code */
} ble_c_status_t;

/**
 *  BLE Central event types
 */
typedef enum
{
    eBLE_C_EVT_CONNECT  = 0,    /**<Peer connected event */
    eBLE_C_EVT_DISCONNECT,      /**<Peer dis-connected event */
    eBLE_C_EVT_RX_DATA,         /**<Client writes to Rx characteristics */
    eBLE_C_EVT_SCAN_START,      /**<Scan started event */
    eBLE_C_EVT_SCAN_END,        /**<Scan ended event */
    eBLE_C_EVT_ADV_RX,          /**<Advertisment packet received */
} ble_c_evt_type_t;

 /**
  *     BLE Connection State
  */
 typedef enum
 {
    eBLE_C_CONN_UNKNOWN     = 0,        /**<Connection state is unknown. Not being connected nor disconnected jet */
    eBLE_C_CONN_CONNECTED,              /**<Active connection */
    eBLE_C_CONN_DISCONNECTED,           /**<Peer device disconnected */
 } ble_c_conn_state_t;

/**
 *  BLE Central event data
 */
typedef struct
{
    ble_c_evt_type_t    type;           /**<Event type */
    uint16_t            conn_handle;    /**<Connection handle */
    uint8_t             peer_mac[6];    /**<Peer MAC */
    uint8_t             conn_num;       /**<Connection number (index) assigned at connection state at free connection slot */  
    bool                scan_timeout;   /**<Scanning timeouted flag */
} ble_c_evt_t;

/**
 *  Connection parameters 
 */
typedef struct
{
    uint32_t    conn_inteval;           /**<Connection interval in ms */
    uint32_t    supervision_timeout;    /**<Supervision timeout in ms */
    uint8_t     slave_latency;          /**<Slave latency */
} ble_c_conn_par_t;

/**
 *  Scan parameters
 */
typedef struct
{
    uint16_t    interval;   /**<Scanning interval in ms */
    uint16_t    window;     /**<Scanning window in ms */
    uint16_t    duration;   /**<Scanning duration in ms */
} ble_c_scan_par_t;

/**
 *  Observer data
 */
typedef struct
{
    uint8_t mac[6];     /**<Peer MAC */
    uint8_t data[32];   /**<Advertisement - Manufacturer specific data */
    uint8_t size;       /**<Size of data */
    int8_t  rssi;       /**<RSSI of received advertisement packet */
} ble_c_obs_data_t;


////////////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////////////
ble_c_status_t ble_c_init               (const ble_c_conn_par_t * const p_conn_params, const ble_c_scan_par_t * const p_scan_params, const bool scan_start);
ble_c_status_t ble_c_is_init            (bool * const p_is_init);

ble_c_status_t ble_c_scan_start         (void);
ble_c_status_t ble_c_scan_stop          (void);
ble_c_status_t ble_c_is_scan            (bool * const p_is_scan);
ble_c_status_t ble_c_change_conn_param  (const uint16_t conn_num, const ble_c_conn_par_t * const p_conn_params);

ble_c_status_t ble_c_is_connected       (const uint16_t conn_num, bool * const p_is_conn);
ble_c_status_t ble_c_transmit           (const uint16_t conn_num, const uint8_t * const p_data, const uint16_t len);
ble_c_status_t ble_c_receive            (const uint16_t conn_num, uint8_t * const p_data);
ble_c_status_t ble_c_clear_buffers      (const uint16_t conn_num);

ble_c_status_t ble_c_obs_receive        (ble_c_obs_data_t * const p_data);
ble_c_status_t ble_c_obs_clear_buffer   (void);

ble_c_status_t ble_c_set_tx_power       (const uint16_t conn_num, const int8_t tx_power);
ble_c_status_t ble_c_rssi_start         (const uint16_t conn_num);
ble_c_status_t ble_c_rssi_stop          (const uint16_t conn_num);
ble_c_status_t ble_c_rssi_get           (const uint16_t conn_num, int8_t * const p_rssi);

ble_c_status_t ble_c_get_active_conn    (uint8_t * const p_active_conn);
ble_c_status_t ble_c_get_max_conn       (uint8_t * const p_max_conn);
ble_c_status_t ble_c_get_conn_info      (const uint16_t conn_num, ble_c_conn_state_t * const p_conn_state, uint16_t * const p_conn_handle, uint8_t * const p_mac);

ble_c_status_t ble_c_get_mac            (uint8_t * const p_mac);

void ble_c_evt_cb(const ble_c_evt_t event);

#endif // __BLE_C_H

////////////////////////////////////////////////////////////////////////////////
/**
* @} <!-- END GROUP -->
*/
////////////////////////////////////////////////////////////////////////////////
