/*==============================================================================
 * cmd_common.h - Shared header for command handlers
 *
 * Bundles every include / extern declaration / macro used in common across
 * the cmd_*.c modules. A handler module only needs this header plus its own
 * area-specific driver headers.
 *============================================================================*/
#ifndef CMD_COMMON_H
#define CMD_COMMON_H

#include "parser.h"
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "debug_print.h"
#include "dr_util.h"
#include "main.h"
#include "app_timer.h"
#include "fstorage.h"   /* config_data_t */

/*------------------------------------------------------------------------------
 * BLE transmission / formatting (defined in main.c)
 *----------------------------------------------------------------------------*/
extern uint8_t ble_bin_buffer[];
extern void single_format_data(uint8_t *buffer, const char *tag, uint16_t value);
extern void ascii_format_data(uint8_t *buffer, const char *tag, const char *ascii, size_t length);
extern void format_data(uint8_t *buffer, const char *tag, const uint16_t *data_array, size_t length);
extern void dr_binary_tx_safe(const uint8_t *buffer, uint16_t length);  /* length: word count */
extern void dr_sd_delay_ms(uint32_t ms);
extern volatile bool data_tx_in_progress;

/*------------------------------------------------------------------------------
 * Device state / flags
 *----------------------------------------------------------------------------*/
extern bool device_status;
extern bool con_single;
extern bool lock_check;
extern uint8_t resetCount;

/*------------------------------------------------------------------------------
 * Measurement functions (defined elsewhere)
 *----------------------------------------------------------------------------*/
extern void battery_level_meas(void);
extern void pressure_all_level_meas(void);
extern void tmp235_voltage_level_meas(void);
extern int  imu_read_direct(void);
extern void battery_timer_stop(void);
extern void main_timer_start(void);
extern void hw_i2c_init_once(void);

/*------------------------------------------------------------------------------
 * info4 mode sensor cache (used to assemble the rbb: response in mbb?)
 *----------------------------------------------------------------------------*/
extern bool info4;
extern bool ble_got_new_data;
extern bool go_batt;
extern bool motion_data_once;
extern bool motion_raw_data_enabled;
extern volatile uint16_t info_batt;
extern volatile uint16_t info_temp;
extern volatile uint16_t info_imu[6];
extern volatile bool tmp235_saadc_done;
extern volatile bool battery_saadc_done;

/*------------------------------------------------------------------------------
 * Device identifiers / passkey (FDS-backed)
 *----------------------------------------------------------------------------*/
extern char SERIAL_NO[12];
extern char HW_NO[12];
extern char m_static_passkey[6];
extern uint32_t m_life_cycle;

/*------------------------------------------------------------------------------
 * Power / reset / bonding control
 *----------------------------------------------------------------------------*/
extern bool go_device_power_off;
extern bool go_NVIC_SystemReset;
extern bool bond_data_delete;
extern uint8_t m_reset_status;
extern void config_save(void);
extern config_data_t m_config;

/*------------------------------------------------------------------------------
 * AGC gain switch (P0.20)
 *----------------------------------------------------------------------------*/
#define GAIN_SW_PIN  NRF_GPIO_PIN_MAP(0, 20)
#define AGC_GAIN_SW(x) do { if(x) nrf_gpio_pin_set(GAIN_SW_PIN); else nrf_gpio_pin_clear(GAIN_SW_PIN); } while(0)

#endif /* CMD_COMMON_H */
