ef_factory h 代码设计

7 阅读3分钟

/*

  • This file is part of the EasyFlash Library.
  • Copyright (c) 2019, Armink, armink.ztl@gmail.com
  • Permission is hereby granted, free of charge, to any person obtaining
  • a copy of this software and associated documentation files (the
  • 'Software'), to deal in the Software without restriction, including
  • without limitation the rights to use, copy, modify, merge, publish,
  • distribute, sublicense, and/or sell copies of the Software, and to
  • permit persons to whom the Software is furnished to do so, subject to
  • the following conditions:
  • The above copyright notice and this permission notice shall be
  • included in all copies or substantial portions of the Software.
  • THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  • EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  • MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  • IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  • CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  • TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  • SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Function: Factory mode parameter management. Simple direct write without wear leveling.
  • Created on: 2026-02-10 */

#ifndef EF_FACTORY_H_ #define EF_FACTORY_H_

#include <stdint.h> #include <stddef.h> #include <stdbool.h> #include <easyflash.h>

#ifdef __cplusplus extern "C" { #endif

/**

  • Factory parameter header magic word ('F', 'A', 'C', 'P') */ #define FACTORY_PARAM_MAGIC 0x50434146

/**

  • Factory parameter version */ #define FACTORY_PARAM_VERSION 0x0001

/**

  • Factory parameter default address (front 4KB) */ #ifndef FACTORY_PARAM_ADDR #define FACTORY_PARAM_ADDR 0x08000000 #endif

/**

  • Factory parameter size (4KB) */ #ifndef FACTORY_PARAM_SIZE #define FACTORY_PARAM_SIZE (4 * 1024) #endif

/**

  • Maximum number of factory parameters */ #ifndef FACTORY_PARAM_MAX_COUNT #define FACTORY_PARAM_MAX_COUNT 32 #endif

/**

  • Maximum key length for factory parameter */ #ifndef FACTORY_PARAM_KEY_MAX_LEN #define FACTORY_PARAM_KEY_MAX_LEN 32 #endif

/**

  • Maximum value length for factory parameter (per item) */ #ifndef FACTORY_PARAM_VALUE_MAX_LEN #define FACTORY_PARAM_VALUE_MAX_LEN 128 #endif

/**

  • Factory parameter item structure */ typedef struct { char key[FACTORY_PARAM_KEY_MAX_LEN]; uint8_t value[FACTORY_PARAM_VALUE_MAX_LEN]; size_t value_len; bool is_valid; } factory_param_item_t;

/**

  • Factory parameter set (in-memory cache) */ typedef struct { factory_param_item_t items[FACTORY_PARAM_MAX_COUNT]; uint16_t count; uint16_t version; uint32_t crc32; bool is_init; } factory_param_set_t;

/**

  • Factory parameter header (stored in flash) */ typedef struct { uint32_t magic; /< Magic word ('F', 'A', 'C', 'P') */ uint16_t version; /< Parameter version */ uint16_t count; /< Number of parameters */ uint32_t crc32; /< CRC32 of all parameters */ } attribute((packed)) factory_param_hdr_t;

/* Factory parameter management API */

/**

  • Initialize factory parameter system
  • This function will load parameters from flash into RAM cache
  • @param default_params Default parameter set (can be NULL)
  • @param default_count Number of default parameters
  • @return EF_NO_ERR - success, other - error code */ EfErrCode ef_factory_init(const ef_env *default_params, size_t default_count);

/**

  • Read factory parameter by key name
  • @param key Parameter key name
  • @param value_buf Buffer to store value
  • @param buf_len Buffer length
  • @param actual_len Return the actual length of value (can be NULL)
  • @return Number of bytes read, 0 means not found */ size_t ef_factory_read(const char *key, void *value_buf, size_t buf_len, size_t *actual_len);

/**

  • Write factory parameter by key name
  • If key exists, update the value. If not, create new parameter.
  • @param key Parameter key name
  • @param value_buf Value buffer
  • @param value_len Value length
  • @return EF_NO_ERR - success, other - error code */ EfErrCode ef_factory_write(const char *key, const void *value_buf, size_t value_len);

/**

  • Delete factory parameter by key name
  • @param key Parameter key name
  • @return EF_NO_ERR - success, EF_PARAMS_INVALID - not found */ EfErrCode ef_factory_delete(const char *key);

/**

  • Save all factory parameters to flash
  • This will erase the factory area and write all parameters back
  • @return EF_NO_ERR - success, other - error code */ EfErrCode ef_factory_save(void);

/**

  • Dump all factory parameters to console */ void ef_factory_dump(void);

/**

  • Get the number of factory parameters
  • @return Number of parameters currently in RAM cache */ uint16_t ef_factory_get_count(void);

/**

  • Get factory parameter by index
  • @param index Parameter index (0-based)
  • @param item Output parameter item
  • @return true - success, false - invalid index */ bool ef_factory_get_by_index(uint16_t index, factory_param_item_t *item);

/**

  • Check if factory parameter area is valid
  • @return true - valid, false - invalid or empty */ bool ef_factory_is_valid(void);

/**

  • Erase all factory parameters (both flash and RAM cache)
  • @return EF_NO_ERR - success, other - error code */ EfErrCode ef_factory_erase_all(void);

/**

  • Reset factory parameters to defaults
  • @param default_params Default parameter set
  • @param default_count Number of default parameters
  • @return EF_NO_ERR - success, other - error code */ EfErrCode ef_factory_reset_to_defaults(const ef_env *default_params, size_t default_count);

#ifdef __cplusplus } #endif

#endif /* EF_FACTORY_H_ */