smallin/my-storage

1.1.0

Latest
uploaded 1 day ago
Storage component(NVS struct存储支持)

readme

# MyStorage组件
一个一级结构化数据存储组件(暂不支持数组),目前支持常规数据、string;


## 使用要求
1. 提供默认构造函数;
2. 适当给予较大的NVS分区;
3. 使用非标准布局时,需在在相应CMakeLists.txt中设置```set_source_files_properties(my_time.cc PROPERTIES COMPILE_FLAGS "-Wno-invalid-offsetof")```编译选项


## 存储特点
```cpp
using FieldSize = int16_t;      // 结构体中字段字节数
using FieldCount = uint16_t;    // 用户结构体字段数量
enum FieldType : uint8_t {
    kNormal_1,      // 1字节常规数据,如char、int8_t
    kNormal_2,
    kNormal_4,
    kNormal_8,
    kArgStruct,     // 函数参数
    kString,        // std::string字符串
    kArray_Char,    // char[]字符串
    kArray_Blob,    // 字节数组,如int8_t[]
    kUnsupport
};

// 目前仅支持32位
struct ArgStruct {
    uint32_t    len{0};         // 参数长度
    void*       data{nullptr};  // 参数指针,only for 32bits
    _Static_assert(sizeof(void*) == 4, "ArgStruct only support 32 bits.");
};


struct FieldMeta {
    FieldType   type;       // 字段类型
    FieldSize   size;       // 字段所占字节数
    size_t      offset;     // 字段相对头部偏移
    constexpr FieldMeta(FieldType t, FieldSize s, size_t o)
        : type(t), size(s), offset(o) {}
};
struct StructMeta {
    FieldCount              count{0};       // 字段数量
    FieldSize               total_size{0};  // 结构总字节数
    std::vector<FieldMeta>  fields{};       // 字段数据
    constexpr StructMeta(){}
    constexpr StructMeta(std::initializer_list<FieldMeta> list, FieldSize size)
        : count(static_cast<FieldCount>(list.size()))
        , total_size(size)
        , fields(list) {}
};
```
1. NVS使用名字空间为"storage";
2. 字段元信息:由用户定义与组件定义2部分组合完成,受NVS键字段串最长15个字符影响(不包括结束字符\0),用户定义部(prefix)分限制为8个字符(结构体长度小于100个元素时)。
    1. 存储位图:prefix+"bitmap",后续可能会扩展为+"bitmap#"
    2. 字段数据:prefix+"-mlen"
    3. 字段总字节数:prefix+"-byte"
    4. 第i字段字节数、偏移:prefix+"-mfby#"、prefix+"-mfof#"
3. 具体数据存储:prefix + "-" + bitmap_index + "-" + field_index

## plan to do
1. 是否有必要增加修改指定字段API

### 数组支持:当前仅支持char[]数组和其他
1. 如何增加数组的支持?----数组,使用blob进行存储,还要存储其长度
2. 指针支持请直接使用ArgStruct结构(需提供数据的长度信息)

## 使用示例
请参考my-time组件

Links

Supports all targets

License: Apache 2.0

To add this component to your project, run:

idf.py add-dependency "smallin/my-storage^1.1.0"

download archive

Stats

  • Archive size
    Archive size ~ 11.32 KB
  • Downloaded in total
    Downloaded in total 0 times
  • Downloaded this version
    This version: 0 times

Badge

smallin/my-storage version: 1.1.0
|