/*==============================================================================
 * fstorage.h - FDS (Flash Data Storage) configuration module interface
 *
 * Stores and loads device configuration to/from the nRF52840 internal flash
 * via the Nordic FDS library. Replaces external EEPROM and coexists safely
 * with the SoftDevice.
 *
 * config_data_t (49 bytes, packed):
 *   magic_number  (4B) : format validation (0x20231226)
 *   hw_no         (12B): hardware version string
 *   serial_no     (12B): serial number (also used as BLE device name)
 *   static_passkey(6B) : BLE pairing passkey (6-digit numeric)
 *   bond_data_delete(1B): bond-delete flag
 *   reset_status  (1B) : reset cause code
 *   life_cycle    (4B) : device usage count
 *   piezo_*       (8B) : piezo measurement parameters
 *   factory_provisioned(1B): passkey provisioning lock flag
 *
 * API:
 *   fs_storage_init() : initialise FDS (once at boot)
 *   config_load()     : load config from FDS (creates defaults if absent)
 *   config_save()     : persist current config to FDS
 *============================================================================*/

#ifndef IHP_FSTORAGE_H_
#define IHP_FSTORAGE_H_

#include "sdk_config.h"

#include "nordic_common.h"
#include <stdint.h>

/*------------------------------------------------------------------------------
 * Default version identifiers (used for FDS defaults / empty field recovery)
 *
 * Hardware ID:
 *   VBTHW0100  = development / test  Ver 1.00
 *   VB0HW0100  = production          Ver 1.00
 *
 * Firmware ID:
 *   VBTFW0100  = development / test  Ver 1.00
 *   VB0FW0100  = production          Ver 1.00
 *
 * Serial number:
 *   VBT26030001 = dev/test, manufactured Mar 2026, unit #1
 *   VB026030001 = production,          Mar 2026, unit #1
 *----------------------------------------------------------------------------*/
#define HARDWARE_VERSION                "VBTHW0100"
#define SERIAL_NUMBER                   "VBT26030001"
#define DEFAULT_PASSKEY                 "123456"

#pragma pack(1)
typedef struct
{
    uint32_t    magic_number;       /* 4B  - format validation magic */
    char        hw_no[12];          /* 12B - HW version */
    char        serial_no[12];      /* 12B - serial number */
    uint8_t     static_passkey[6];  /* 6B  - BLE passkey */
    uint8_t     bond_data_delete;   /* 1B  - bond delete flag */
    int8_t      reset_status;       /* 1B  - reset status */
    uint32_t    life_cycle;         /* 4B  - device usage count */

    /* Piezo measurement parameters - 8B */
    uint8_t     piezo_freq_option;  /* 1B  - TX pulse frequency (0=1.8M, 1=2.1M, 2=2.0M, 3=1.7M) */
    uint8_t     piezo_cycles;       /* 1B  - burst pulse cycle count (3..7) */
    uint16_t    piezo_averaging;    /* 2B  - averages per channel (1..10) */
    uint16_t    piezo_delay_us;     /* 2B  - delay from TX pulse to ADC start (us) (0..30) */
    uint16_t    piezo_num_samples;  /* 2B  - ADC sample count (80..140) */

    /* Factory provisioning lock */
    uint8_t     factory_provisioned; /* 1B  - 0=passkey not set, 1=passkey set (locked) */
} config_data_t;  /* Total: 49 bytes */

extern config_data_t m_config;

void fds_default_value_set(void);
void config_load( void );
void config_save( void );

void fs_set_value(void);
void fs_storage_init(void);

#endif /* IHP_FSTORAGE_H_ */
