/*******************************************************************************
 * @file    ble_data_tx.h
 * @brief   BLE Data Transmission Functions
 * @author  Charles KWON <charleskwon@medithings.co.kr>
 * @date    2025-01-30
 * @copyright (c) 2025 Medithings Inc. All rights reserved.
 *
 * @details BLE NUS data transmission with CRC16 checksum.
 *          - ASCII data transmission
 *          - Binary data transmission
 *          - Data formatting functions
 ******************************************************************************/

#ifndef BLE_DATA_TX_H
#define BLE_DATA_TX_H

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "main.h"

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

extern volatile bool ble_connection_st;
extern volatile bool data_tx_in_progress;
extern uint8_t ble_bin_buffer[];

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

/**
 * @brief Send ASCII data over BLE NUS with CRC
 * @param p_data_to_send Null-terminated string ending with '\r'
 */
void data_tx_handler(char const *p_data_to_send);

/**
 * @brief Send binary data over BLE NUS with CRC
 * @param ble_bin_buff Binary data buffer
 * @param length Number of 16-bit words to send
 */
void binary_tx_handler(uint8_t const *ble_bin_buff, uint16_t length);

/**
 * @brief Format single 16-bit value with 4-character tag
 * @param buffer Output buffer (minimum 6 bytes)
 * @param tag 4-character tag string
 * @param value 16-bit value to format
 */
void single_format_data(uint8_t *buffer, const char *tag, const uint16_t value);

/**
 * @brief Format 16-bit array with 4-character tag
 * @param buffer Output buffer
 * @param tag 4-character tag string
 * @param data_array Array of 16-bit values
 * @param length Number of elements in array
 */
void format_data(uint8_t *buffer, const char *tag, const uint16_t *data_array, size_t length);

/**
 * @brief Format 8-bit array with 4-character tag
 * @param buffer Output buffer
 * @param tag 4-character tag string
 * @param data_array Array of 8-bit values
 * @param length Number of bytes in array
 */
void format_data_byte(uint8_t *buffer, const char *tag, const uint8_t *data_array, size_t length);

/**
 * @brief Format ASCII string with 4-character tag
 * @param buffer Output buffer
 * @param tag 4-character tag string
 * @param data_ascii ASCII string data
 * @param length String length
 */
void ascii_format_data(uint8_t *buffer, const char *tag, const char *data_ascii, size_t length);

#endif /* BLE_DATA_TX_H */
