/*************************************************************************
  File name     : hal_env.h
  Module        : hal_env
  Author        : 
  Copyright     : 
  Version       : 0.1
  Created on    : 2022-02-10
  Creator       : amir.liang
  Description   :
      get/set/save enviroment paramters.
  Modify History:
  1. Date:        Author:         Modification:
     2022-04-20   amir             need add a task or threadpool to save in some time.
***************************************************************************/

#ifndef __HAL_ENV_H__
#define __HAL_ENV_H__
#include <stdint.h>

//*****************************************************************************
//
//! ***************************Flash layout***************************************
//! HEADER: 64K(env_back) +  64K(env1) + 64K(env2)

//
//*****************************************************************************
#define ENV_CFG_0        "env0"
#define ENV_CFG_1        "env1"
#define ENV_CFG_BACKUP   "env_backup"//not modify after factory
#define ENV_SIZE         ((uint16_t)(64*1024))


//each env field is an enum, supports up to 255
typedef enum{
	ENV_CFG_START = 0,
   	ENV_CFG_DEV,
   	ENV_CFG_TEST,
   	ENV_CFG_TEST2,
    ENV_CFG_MAX
}ENV_CFG;

//boot和rtos公用的头结构
typedef struct
{
    uint32_t crc;            //the crc value calculated from age to the end of value, last 2 zero is counted
    uint32_t age;            //the times of upgrading the firmware
    uint32_t version;
} Env_Header;

//grep -rl newString . | xargs sed -i 's/newString/newString/g'
/**
* hal_env init, must be called before use any functions of env
* @return !=0 err
*/
int32_t hal_env_init();
    
/**
 * hal env deinit
 * @return 0 ok
 */
int32_t hal_env_deinit();

//*****************************************************************************
//! if want to change the env info, please follows the steps:
//! 1. env_mutex_lock
//! 2. change the values of env by call env_set_string/env_set_int
//! 3. call env_save to save to spi flash
//! 4. env_mutex_unlock
//*****************************************************************************


int32_t hal_env_mutex_lock();
int32_t hal_env_mutex_unlock();
int32_t hal_env_save();

/**
* get the integer of e
* @param [in] e ENV_CFG to get
* @param [in] def_val return def_val if e has not been set
* @return 0 ok
*/

//下面的get／set函数都需要调用hal_env_mutex_lock／hal_env_mutex_unlock 保护一下。
int32_t  hal_env_get_int(ENV_CFG e, int32_t def_val);

/**
 * return true if the value is changed, false is the value is same as the old one
 */
int32_t hal_env_set_int(ENV_CFG e, int32_t value);

//留意返回的这个字符串自己要备份起来。否则返回的这个指针可能会被内部hv_free掉。
// never return "" except def_val is "", 
const char * hal_env_get_string(ENV_CFG e, const char * def_val);

//return true if the value is changed, false is the value is same as the old one
int32_t hal_env_set_string(ENV_CFG e, const char * value);

int32_t hal_env_is_same_value(ENV_CFG e, const char * value);

void hal_env_clear();


#endif /* __HAL_ENV_H__ */
