/*******************************************************************************
 * @file    ble_core.h
 * @brief   BLE Core Functions - Stack, GAP, GATT, Advertising
 * @author  Charles KWON <charleskwon@medithings.co.kr>
 * @date    2025-01-30
 * @copyright (c) 2025 Medithings Inc. All rights reserved.
 *
 * @details BLE stack initialization, GAP/GATT configuration, advertising.
 ******************************************************************************/

#ifndef BLE_CORE_H
#define BLE_CORE_H

#include <stdint.h>
#include <stdbool.h>
#include "ble.h"
#include "ble_advertising.h"
#include "nrf_ble_gatt.h"

/*==============================================================================
 * CONSTANTS
 *============================================================================*/

#define APP_BLE_CONN_CFG_TAG            1
#define NUS_SERVICE_UUID_TYPE           BLE_UUID_TYPE_VENDOR_BEGIN
#define APP_BLE_OBSERVER_PRIO           3
#define APP_ADV_INTERVAL                64      /* 40 ms in 0.625 ms units */

#if FEATURE_NO_SLEEP
#define APP_ADV_DURATION                0       /* Infinite advertising */
#else
#define APP_ADV_DURATION                18000   /* 180 seconds */
#endif

/* Connection Parameters */
#define MIN_CONN_INTERVAL               MSEC_TO_UNITS(20, UNIT_1_25_MS)
#define MAX_CONN_INTERVAL               MSEC_TO_UNITS(75, UNIT_1_25_MS)
#define SLAVE_LATENCY                   0
#define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)
#define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(5000)
#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(30000)
#define MAX_CONN_PARAMS_UPDATE_COUNT    3

/* GATT Constants */
#define OPCODE_LENGTH   1
#define HANDLE_LENGTH   2

/*==============================================================================
 * EXTERNAL VARIABLES
 *============================================================================*/

extern uint16_t m_conn_handle;
extern uint16_t m_ble_nus_max_data_len;

/*==============================================================================
 * FUNCTION PROTOTYPES
 *============================================================================*/

/**
 * @brief Initialize BLE SoftDevice stack
 */
void ble_stack_init(void);

/**
 * @brief Initialize GAP parameters
 */
void gap_params_init(void);

/**
 * @brief Initialize GATT module
 */
void gatt_init(void);

/**
 * @brief Initialize connection parameters module
 */
void conn_params_init(void);

/**
 * @brief Initialize BLE advertising
 */
void advertising_init(void);

/**
 * @brief Start BLE advertising
 * @param erase_bonds If true, delete bonds before advertising
 */
#if FEATURE_SECURE_CONNECTION
void advertising_start(bool erase_bonds);
#else
void advertising_start(void);
#endif

/**
 * @brief Disconnect a connection
 * @param conn_handle Connection handle to disconnect
 * @param p_context Unused context pointer
 */
void disconnect(uint16_t conn_handle, void *p_context);

/**
 * @brief Initialize BLE services (NUS, DFU)
 */
void services_init(void);

/**
 * @brief Wrapper for ble_nus_data_send (accesses static m_nus)
 */
uint32_t ble_nus_data_send_wrapper(uint8_t *p_data, uint16_t *p_length, uint16_t conn_handle);

/**
 * @brief Wrapper for ble_advertising_restart_without_whitelist (accesses static m_advertising)
 */
uint32_t ble_advertising_restart_without_whitelist_wrapper(void);

#if BLE_DFU_ENABLED
/**
 * @brief Initialize DFU async SVCI
 */
void dfu_init(void);
#endif

#endif /* BLE_CORE_H */
