11/**
22 * @file eeprom_directory.h
3- * @brief EEPROM Directory Management
3+ * @author Sogo Nishihara (sogonishi@gmail.com)
4+ * @brief EEPROM Directory Management API
45 *
5- * This file provides functions to initialize and manage an EEPROM directory with partitions.
6- * Functions return `eeprom_status_t` error codes, which are defined in `eeprom_status.h` .
6+ * This file defines the public API for a simple key-value directory
7+ * stored on EEPROM device .
78 *
8- */
9+ * The directory provides block-based persistent storage, managing
10+ * allocation, lookup, insertion, and deletion of values associated
11+ * with fixed-size (4-byte) keys.
12+ *
13+ * All functions return eeprom_status_t error codes defined in
14+ * eeprom_status.h. */
915
1016#ifndef EEPROM_DIRECTORY_H
1117#define EEPROM_DIRECTORY_H
1218
13- #include "eeprom_status.h"
14- #include <stddef.h>
15- #include <stdint.h>
16-
17- struct partition_cfg {
18- const char * id ; /* The ID of the partition */
19- uint16_t size ; /* The size of the partition in bytes */
20- uint16_t address ;
21- uint16_t head_address ;
22- };
19+ #include <stdlib.h>
20+ #include <stdio.h>
21+ #include <string.h>
2322
24- typedef struct {
25- struct partition_cfg * partitions ;
26- size_t num_partitions ;
27- } eeprom_directory_t ;
23+ #include "m24c32.h"
24+ #include "eeprom_status.h"
25+ #include "eeprom_directory_struct.h"
26+ #include "eeprom_alloc.h"
27+ #include "eeprom_storage.h"
2828
2929/**
30- * @brief Initializes an EEPROM directory with partition IDs and sizes .
30+ * @brief Initialize the EEPROM directory structure .
3131 *
32- * This function sets up partition configurations within the EEPROM memory.
33- * The total allocated address space is calculated based on the provided partitions.
34- *
35- * @param directory Pointer to the EEPROM directory structure.
36- * @param partitions Array of partition configurations, including IDs and sizes.
37- * @param num_partitions Number of partitions to create.
38- * @return eeprom_status_t Returns EEPROM_OK on success or an error code on failure.
32+ * This function initializes the directory structure with the provided device interface,
33+ * loads the allocation table and key map from EEPROM memory.
34+ *
35+ * @param device Pointer to the M24C32 device interface structure.
36+ * @param directory Pointer to the directory structure to initialize. Must be allocated by caller.
37+ *
38+ * @return eeprom_status_t Returns EEPROM_OK on success, or an error code on failure.
39+ *
40+ * @retval EEPROM_ERROR_NULL_POINTER If device or directory is NULL.
41+ * @retval EEPROM_ERROR If initialization of alloc table or storage fails.
3942 */
40- eeprom_status_t directory_init (eeprom_directory_t * directory ,
41- const struct partition_cfg partitions [],
42- size_t num_partitions );
43+ eeprom_status_t directory_init (
44+ m24c32_t * device ,
45+ eeprom_directory_t * directory
46+ );
4347
4448/**
45- * @brief Get the address of the beginning of a partition.
46- *
47- * This function searches for a partition by its name and returns its starting address.
49+ * @brief Retrieve a value from the directory by key.
50+ *
51+ * This function looks up a key in the directory and retrieves the associated data.
52+ * The output buffer is allocated by this function and must be freed by the caller.
4853 *
49- * @param directory Pointer to the EEPROM directory structure.
50- * @param key Name of the partition.
51- * @param address Pointer to store the retrieved base address.
52- * @return eeprom_status_t Returns EEPROM_OK on success or an error code if the partition is not found.
54+ * @param directory Pointer to the initialized directory structure.
55+ * @param key Pointer to a 4-byte key (not null-terminated).
56+ * @param out Pointer to a pointer that will receive the allocated output buffer.
57+ * The caller is responsible for freeing this buffer.
58+ * @param out_size Pointer to store the size of the output data in bytes.
59+ *
60+ * @return eeprom_status_t Returns EEPROM_OK on success, or an error code on failure.
61+ *
62+ * @retval EEPROM_ERROR_NULL_POINTER If directory, key, out, or out_size is NULL.
63+ * @retval EEPROM_ERROR_NOT_FOUND If the key does not exist in the directory.
64+ * @retval EEPROM_ERROR_ALLOCATION If memory allocation fails.
5365 */
54- eeprom_status_t eeprom_get_base_address (const eeprom_directory_t * directory ,
55- const char * key , uint16_t * address );
66+ eeprom_status_t get_directory_value (
67+ eeprom_directory_t * directory ,
68+ const uint8_t * key ,
69+ uint8_t * * out ,
70+ uint16_t * out_size
71+ );
5672
5773/**
58- * @brief Get the current read/write head of a partition.
59- *
60- * This function provides the address of the next location available for writing within the partition.
74+ * @brief Store a value in the directory with the specified key.
75+ *
76+ * This function stores data in the directory. If the key already exists, the old value
77+ * is deleted first. The data is stored in blocks of BLOCK_SIZE bytes, with remaining
78+ * bytes padded with zeros.
79+ *
80+ * @param directory Pointer to the initialized directory structure.
81+ * @param key Pointer to a 4-byte key (not null-terminated).
82+ * @param value Pointer to the data to store.
83+ * @param value_size Size of the data in bytes (maximum 16 bytes, i.e., 4 blocks).
6184 *
62- * @param directory Pointer to EEPROM directory struct.
63- * @param key Name of the partition.
64- * @param address Pointer to store the retrieved head address.
65- * @return eeprom_status_t Returns EEPROM_OK on success or an error code if the partition is not found.
85+ * @return eeprom_status_t Returns EEPROM_OK on success, or an error code on failure.
86+ *
87+ * @retval EEPROM_ERROR_NULL_POINTER If directory or value is NULL.
88+ * @retval EEPROM_ERROR If value_size is 0.
89+ * @retval EEPROM_ERROR_TOO_BIG If value_size exceeds 16 bytes (4 blocks).
90+ * @retval EEPROM_ERROR_ALLOCATION If block allocation fails.
6691 */
67- eeprom_status_t eeprom_get_head_address (const eeprom_directory_t * directory ,
68- const char * key , uint16_t * address );
92+ eeprom_status_t set_directory_value (
93+ eeprom_directory_t * directory ,
94+ const uint8_t * key ,
95+ uint8_t * value ,
96+ const uint16_t value_size
97+ );
6998
7099/**
71- * @brief Get the size of a partition
72- *
73- * This function returns the allocated size of a specific partition in bytes.
100+ * @brief Delete a value from the directory by key.
101+ *
102+ * This function removes a key-value pair from the directory and frees the associated
103+ * blocks. The blocks are marked as available for reuse.
104+ *
105+ * @param directory Pointer to the initialized directory structure.
106+ * @param key Pointer to a 4-byte key (not null-terminated).
107+ *
108+ * @return eeprom_status_t Returns EEPROM_OK on success, or an error code on failure.
74109 *
75- * @param directory Pointer to EEPROM directory struct.
76- * @param key Name of a partition.
77- * @param size Pointer to store the partition size.
78- * @return eeprom_status_t Returns EEPROM_OK on success or an error code if the partition is not found.
110+ * @retval EEPROM_ERROR_NULL_POINTER If directory or key is NULL.
111+ * @retval EEPROM_ERROR_NOT_FOUND If the key does not exist in the directory.
79112 */
80- eeprom_status_t eeprom_get_size (const eeprom_directory_t * directory ,
81- const char * key , uint16_t * size );
113+ eeprom_status_t delete_directory_value (
114+ eeprom_directory_t * directory ,
115+ const uint8_t * key
116+ );
82117
83118#endif // EEPROM_DIRECTORY_H
0 commit comments