/*****************************************************************************
 * file    Inc/flash_log.h 
 * author  YuLiang
 * version 1.0.0
 * date    26-Dec-2022
 * brief   This file provides all the headers of log function.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2022 LandPower</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of WTOE nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __FLASH_LOG_H__
#define __FLASH_LOG_H__

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include "main.h"
    
/* Define --------------------------------------------------------------------*/    
/* log前8个字节为id,后面跟time+type+data. */
#define FLASH_LOG_ID_LEN 8
#define FLASH_LOG_DATA_LEN 248
#define FLASH_LOG_BUF_LEN 256
#define FLASH_LOG_NUM 8192

#define FD_ID_LEN 8
#define FD_DATA_LEN 248
#define FD_BUF_LEN 256
#define FD_NUM 8192

#define POSITION_ID_LEN 8
#define POSITION_DATA_LEN 248
#define POSITION_BUF_LEN 256
#define POSITION_NUM 8192

#define ST_ID_LEN 8
#define ST_DATA_LEN 248
#define ST_BUF_LEN 256
#define ST_NUM 8192
    
#define FLASH_LOG_INVALID_ID 0xFFFFFFFFFFFFFFFF
#define FD_INVALID_ID 0xFFFFFFFFFFFFFFFF
#define POSITION_INVALID_ID 0xFFFFFFFFFFFFFFFF
#define ST_INVALID_ID 0xFFFFFFFFFFFFFFFF
    
#define FLASH_LOG_RETURN_MUTEX(_f_) \
    do { \
        HAL_StatusTypeDef _rv_ = HAL_ERROR; \
        if ((_rv_ = _f_) != HAL_OK) \
        { \
            ERROR_PRINT(_rv_); \
            _flash_log_mutex_unlock(); \
            return _rv_; \
        } \
    } while(0)
    
/* Exported types ------------------------------------------------------------*/
typedef enum
{
    FLASH_LOG_TYPE_ERROR = 0,
    FLASH_LOG_TYPE_WARNING,
    FLASH_LOG_TYPE_NOTIFY,
    FLASH_LOG_TYPE_INFO,
    FLASH_LOG_TYPE_CNT
} flash_log_type_e;

/* Note: 必须保证这个结构体的大小是可以被flash扇区大小整除的,比如128,256... */
typedef struct 
{
    uint64_t id;
    uint8_t data[FLASH_LOG_DATA_LEN];
} falsh_log_t;

/* Note: 必须保证这个结构体的大小是可以被flash扇区大小整除的,比如128,256... */
typedef struct 
{
    uint64_t id;
    uint32_t run_time;                      // 时间, 单位 s.
    int16_t temperature;                    // 设备温度, 单位: 0.1℃.
    uint16_t vin;                           // 工作电压, 单位: mv.
    uint16_t vbat;                          // 电池电压, 单位: mv.
    uint16_t vsc;                           // 超级电容电压, 单位: mv.
    uint32_t elec[CFG_ADC_CH_CNT];          // 通道电流有效值 mA.
    uint16_t sen_temp[SENSOR_SUM];          // 传感器温度: 单位: 0.1℃.
    uint16_t sen_x[SENSOR_SUM];             // 传感器震动 x 轴.
    uint16_t sen_y[SENSOR_SUM];             // 传感器震动 y 轴.
    uint16_t sen_z[SENSOR_SUM];             // 传感器震动 z 轴.
    uint8_t energy_mode;
    uint8_t sen_short;                      // 传感器是否短路, 0 – 没有短路, 1 – 短路.
    uint8_t is_not_updata;                  // 数据是否上传.
    uint8_t reserve[1];
    uint8_t sen_valid[SENSOR_SUM];          // 传感器数据是否有效, 0 - 无效, 1 - 有效.
} fd_date_t;

typedef struct
{
    float v[POSITION_SUM];          //电流幅值
    uint32_t s[POSITION_SUM];       //秒时刻
    uint32_t ns[POSITION_SUM];      //纳秒时刻
    uint32_t file_s;                //录波文件秒
    uint32_t file_ns;               //录波文件纳秒
    uint32_t file_feq;              //录波文件频率
} position_wave_info_t;

/* Note: 必须保证这个结构体的大小是可以被flash扇区大小整除的,比如128,256... */
typedef struct
{
    uint64_t id;
    uint32_t run_time;              //时间, 单位 s.
    uint16_t vol;                   //工作电压, 单位: v.
    uint16_t temperature;           //设备温度, 单位: ℃.
    position_wave_info_t wave_info;
} position_data_t;

/* Note: 必须保证这个结构体的大小是可以被flash扇区大小整除的,比如128,256... */
typedef struct 
{
    uint64_t id;
    uint64_t position_up_id;
    uint8_t energy_mode;
    uint8_t is_bat_charge;
    uint8_t force_sleep_up;
    uint8_t wave_force;
    uint32_t wave_time;
    uint8_t wave_up_start;
    uint8_t wave_start;
    uint16_t wave_index;
    int16_t wave_min_save;
    int16_t wave_max_save;
    int16_t wave_max[WAVE_SUM];
} st_date_t;

/* Extern global variables ---------------------------------------------------*/
extern uint64_t flash_log_id;
extern uint64_t fd_id;
extern uint64_t position_id;
extern uint64_t st_id;

/* Extern macro --------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
extern HAL_StatusTypeDef flash_log_write(uint8_t type, char *format, ...);
extern HAL_StatusTypeDef flash_log_read(uint64_t log_id, falsh_log_t *log_data);
extern void flash_log_show(uint32_t num);
extern HAL_StatusTypeDef flash_log_clear(void);
extern void flash_log_init(void);
extern HAL_StatusTypeDef fd_write(fd_date_t *data);
extern HAL_StatusTypeDef fd_modify(uint64_t id, fd_date_t *data);
extern HAL_StatusTypeDef fd_read(uint64_t id, fd_date_t *data);
extern HAL_StatusTypeDef fd_clear(void);
extern void fd_show(uint32_t num);
extern HAL_StatusTypeDef position_write(position_data_t *data);
extern HAL_StatusTypeDef position_read(uint64_t id, position_data_t *data);
extern HAL_StatusTypeDef position_clear(void);
extern void position_show(uint32_t num);
extern HAL_StatusTypeDef st_write(st_date_t *data);
extern HAL_StatusTypeDef st_read(uint64_t id, st_date_t *data);
extern HAL_StatusTypeDef st_clear(void);
extern void st_show(uint32_t num);
extern void st_init(void);
#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/
