/*******************************************************************************
 * @file    cmd_parse.h
 * @brief   Command Parser Interface (Legacy Compatibility Layer)
 * @author  Charles KWON <charleskwon@medithings.co.kr>
 * @date    2025-02-04
 * @copyright (c) 2025 Medithings Inc. All rights reserved.
 *
 * @details This header provides backward compatibility with existing code.
 *          Actual parsing is handled by mt_parser.
 ******************************************************************************/

#ifndef _CMD_PARSE_H_
#define _CMD_PARSE_H_

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

/*==============================================================================
 * @section API Command Processing Entry Point
 *============================================================================*/

/**
 * @brief Process received command from UART or BLE
 * @param data_array  Raw command data
 * @param cmd_t       Command source (CMD_UART or CMD_BLE)
 * @param length      Data length
 */
void received_command_process(uint8_t const *data_array, which_cmd_t cmd_t, uint8_t length);

/*==============================================================================
 * @section UTIL Error Handling Functions
 *============================================================================*/

/**
 * @brief Send parameter error response
 * @param cmd  Command tag that caused the error
 */
void param_error(const char *cmd);

/**
 * @brief Send activation error response
 * @param cmd            Command tag
 * @param device_status  Current device status
 * @return               true if error occurred
 */
bool activate_error(const char *cmd, bool device_status);

/**
 * @brief Send length error response
 * @param cmd           Command tag
 * @param target_length Expected length
 * @param length        Actual length
 * @return              true if error occurred
 */
bool length_error(const char *cmd, uint8_t target_length, uint8_t length);

/**
 * @brief Send quest error response
 * @param cmd  Command tag
 */
void quest_error(const char *cmd);

/*==============================================================================
 * @section VALID Validation Functions
 *============================================================================*/

/**
 * @brief Validate serial number format
 * @param serial  Serial number string
 * @return        true if valid
 */
bool is_valid_serial_no(const char *serial);

/**
 * @brief Validate passkey format
 * @param passkey  Passkey string
 * @return         true if valid
 */
bool is_valid_passkey(const char *passkey);

/**
 * @brief Hash serial number to passkey
 * @param input  Serial number string
 * @return       Hashed passkey value
 */
uint32_t serial_to_passkey_hash(const char *input);

/*==============================================================================
 * @section CRC CRC Functions
 *============================================================================*/

/**
 * @brief Check CRC16 against expected value
 * @param p_data       Data buffer
 * @param data_len     Data length
 * @param expected_crc Expected CRC value
 * @return             true if match
 */
bool crc16_check(uint8_t const *p_data, uint32_t data_len, uint16_t expected_crc);

/**
 * @brief Check CRC16 in packet (last 2 bytes)
 * @param packet       Complete packet
 * @param packet_len   Packet length
 * @return             true if valid
 */
bool crc16_check_packet(uint8_t const *packet, uint32_t packet_len);

/*==============================================================================
 * @section EEPROM EEPROM Functions
 *============================================================================*/

/**
 * @brief Read initial values from EEPROM
 * @return NRF_SUCCESS on success
 */
ret_code_t eeprom_init_values_read(void);

#endif /* _CMD_PARSE_H_ */
