#ifndef DATA_PACKET_H
#define DATA_PACKET_H

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

// 帧同步魔数（高3字节固定），低字节为类型标志
#define PACKET_TYPE_STANDARD    0xFFFFFFFFUL  // 标准包（无光泵）
#define PACKET_TYPE_WITH_OPTIC  0xFFFFFFFEUL  // 扩展包（含光泵，末尾追加 optical_mag）
#define PACKET_SYNC_MASK        0xFFFFFF00UL  // 同步检测掩码

// 兼容旧代码
#define PACKET_START_BYTE       PACKET_TYPE_STANDARD

// GPS位置字段由 app_config.h 中 CFG_ENABLE_GPS_POSITION 控制
#if CFG_ENABLE_GPS_POSITION
#define ENABLE_GPS_POSITION
#endif

// ─── 原始ADC数据包 ────────────────────────────────────────────────────────────

typedef struct __attribute__((packed)) {
    uint32_t start_byte;    // PACKET_TYPE_STANDARD
    int32_t  adc_data1;
    int32_t  adc_data2;
    int32_t  adc_data3;
    uint32_t gps_time;      // HHMMSS格式，始终存在
#ifdef ENABLE_GPS_POSITION
    float    gps_latitude;
    float    gps_longitude;
    float    gps_altitude;
#endif
} DataPacket_t;

typedef struct __attribute__((packed)) {
    uint32_t start_byte;    // PACKET_TYPE_WITH_OPTIC
    int32_t  adc_data1;
    int32_t  adc_data2;
    int32_t  adc_data3;
    uint32_t gps_time;
#ifdef ENABLE_GPS_POSITION
    float    gps_latitude;
    float    gps_longitude;
    float    gps_altitude;
#endif
    uint32_t optical_mag;   // 单位 0.001 nT，例如 123456789 = 123456.789 nT
} DataPacketWithOptic_t;

// ─── 校正数据包 ───────────────────────────────────────────────────────────────

typedef struct __attribute__((packed)) {
    uint32_t start_byte;    // PACKET_TYPE_STANDARD
    float    corrected_x;
    float    corrected_y;
    float    corrected_z;
    uint32_t gps_time;
#ifdef ENABLE_GPS_POSITION
    float    gps_latitude;
    float    gps_longitude;
    float    gps_altitude;
#endif
} CorrectedDataPacket_t;

// 与 CorrectedDataPacket_t 相同，保留别名兼容旧代码
typedef CorrectedDataPacket_t CorrectedDataPacketWithGPS_t;

typedef struct __attribute__((packed)) {
    uint32_t start_byte;    // PACKET_TYPE_WITH_OPTIC
    float    corrected_x;
    float    corrected_y;
    float    corrected_z;
    uint32_t gps_time;
#ifdef ENABLE_GPS_POSITION
    float    gps_latitude;
    float    gps_longitude;
    float    gps_altitude;
#endif
    uint32_t optical_mag;   // 单位 0.001 nT
} CorrectedDataPacketWithOptic_t;

// ─── 函数声明 ─────────────────────────────────────────────────────────────────

uint16_t Calculate_CRC16(const uint8_t *data, uint16_t len);

void PackData(DataPacket_t *packet,
              int32_t adc1, int32_t adc2, int32_t adc3,
              uint32_t gps_time, float latitude, float longitude, float altitude);

void PackDataWithOptic(DataPacketWithOptic_t *packet,
                       int32_t adc1, int32_t adc2, int32_t adc3,
                       uint32_t gps_time, float latitude, float longitude, float altitude,
                       uint32_t optical_mag);

void PackCorrectedData(CorrectedDataPacket_t *packet,
                       float x, float y, float z,
                       uint32_t gps_time, float latitude, float longitude, float altitude);

void PackCorrectedDataWithGPS(CorrectedDataPacketWithGPS_t *packet,
                              float x, float y, float z,
                              uint32_t gps_time, float latitude, float longitude, float altitude);

void PackCorrectedDataWithOptic(CorrectedDataPacketWithOptic_t *packet,
                                float x, float y, float z,
                                uint32_t gps_time, float latitude, float longitude, float altitude,
                                uint32_t optical_mag);

uint8_t ValidatePacket(const DataPacket_t *packet);
uint8_t ValidateCorrectedPacket(const CorrectedDataPacket_t *packet);

#endif // DATA_PACKET_H
