MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_gpio_kv.h
Go to the documentation of this file.
1/**
2 * @file meshx_gpio_kv.h
3 * @brief MeshX GPIO KV Engine Serialization Format
4 *
5 * This file defines the binary serialization structures for GPIO configuration
6 * persistence using the MeshX KV Engine. The format is designed for:
7 * - Compact storage with versioning support
8 * - CRC16 checksums compatible with KV Engine's CRC algorithm
9 * - Product-specific key prefixes for OTA update compatibility
10 * - Forward/backward compatibility for configuration migration
11 *
12 * @author MeshX Team
13 * @date 2024
14 */
15
16#ifndef __MESHX_GPIO_KV_H
17#define __MESHX_GPIO_KV_H
18
19#include <stdint.h>
20#include <stdbool.h>
21#include <stddef.h>
22#include <stdio.h>
24#include "meshx_gpio_types.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/*============================================================================
31 * Version Definitions
32 *============================================================================*/
33
34/** @brief Current GPIO configuration serialization format version */
35#define MESHX_GPIO_CONFIG_VERSION_CURRENT 1
36
37/** @brief Minimum supported version (for backward compatibility) */
38#define MESHX_GPIO_CONFIG_VERSION_MIN 1
39
40/** @brief Maximum supported version (for forward compatibility) */
41#define MESHX_GPIO_CONFIG_VERSION_MAX 1
42
43/*============================================================================
44 * KV Engine Key Prefixes
45 *============================================================================*/
46
47/** @brief Maximum length for product name in key prefix */
48#define MESHX_GPIO_KV_PRODUCT_NAME_MAX_LEN 16
49
50/** @brief Maximum total key length (KV Engine limit is 32) */
51#define MESHX_GPIO_KV_KEY_MAX_LEN 32
52
53/** @brief Key suffix for main GPIO configuration */
54#define MESHX_GPIO_KV_KEY_CONFIG "config"
55
56/** @brief Key suffix for GPIO pin state */
57#define MESHX_GPIO_KV_KEY_STATE "state"
58
59/** @brief Key suffix for PWM configuration */
60#define MESHX_GPIO_KV_KEY_PWM "pwm"
61
62/** @brief Key suffix for interrupt configuration */
63#define MESHX_GPIO_KV_KEY_INTR "intr"
64
65/**
66 * @brief Construct KV Engine key for GPIO configuration
67 * @param product_name Product name (max 16 chars)
68 * @param buf Buffer to store the key
69 * @param buf_len Buffer length
70 * @return Key length, or 0 on error
71 */
72static inline uint8_t meshx_gpio_kv_make_config_key(const char *product_name,
73 char *buf, uint8_t buf_len)
74{
75 if (!product_name || !buf || buf_len < MESHX_GPIO_KV_KEY_MAX_LEN) {
76 return 0;
77 }
78 return (uint8_t)snprintf(buf, buf_len, "gpio_%.*s_%s",
80 product_name,
82}
83
84/**
85 * @brief Construct KV Engine key for GPIO pin state
86 * @param product_name Product name (max 16 chars)
87 * @param logical_pin Logical pin number
88 * @param buf Buffer to store the key
89 * @param buf_len Buffer length
90 * @return Key length, or 0 on error
91 */
92static inline uint8_t meshx_gpio_kv_make_state_key(const char *product_name,
93 uint8_t logical_pin,
94 char *buf, uint8_t buf_len)
95{
96 if (!product_name || !buf || buf_len < MESHX_GPIO_KV_KEY_MAX_LEN) {
97 return 0;
98 }
99 return (uint8_t)snprintf(buf, buf_len, "gpio_%.*s_%s_%u",
101 product_name,
103 logical_pin);
104}
105
106/**
107 * @brief Construct KV Engine key for PWM configuration
108 * @param product_name Product name (max 16 chars)
109 * @param logical_pin Logical pin number
110 * @param buf Buffer to store the key
111 * @param buf_len Buffer length
112 * @return Key length, or 0 on error
113 */
114static inline uint8_t meshx_gpio_kv_make_pwm_key(const char *product_name,
115 uint8_t logical_pin,
116 char *buf, uint8_t buf_len)
117{
118 if (!product_name || !buf || buf_len < MESHX_GPIO_KV_KEY_MAX_LEN) {
119 return 0;
120 }
121 return (uint8_t)snprintf(buf, buf_len, "gpio_%.*s_%s_%u",
123 product_name,
125 logical_pin);
126}
127
128/**
129 * @brief Construct KV Engine key for interrupt configuration
130 * @param product_name Product name (max 16 chars)
131 * @param logical_pin Logical pin number
132 * @param buf Buffer to store the key
133 * @param buf_len Buffer length
134 * @return Key length, or 0 on error
135 */
136static inline uint8_t meshx_gpio_kv_make_intr_key(const char *product_name,
137 uint8_t logical_pin,
138 char *buf, uint8_t buf_len)
139{
140 if (!product_name || !buf || buf_len < MESHX_GPIO_KV_KEY_MAX_LEN) {
141 return 0;
142 }
143 return (uint8_t)snprintf(buf, buf_len, "gpio_%.*s_%s_%u",
145 product_name,
147 logical_pin);
148}
149
150/*============================================================================
151 * Binary Serialization Structures
152 *============================================================================*/
153
154/**
155 * @struct meshx_gpio_config_header_kv_t
156 * @brief Header for serialized GPIO configuration stored in KV Engine.
157 *
158 * This header precedes all GPIO configuration data in KV Engine storage.
159 * It includes versioning for migration support and CRC16 for integrity.
160 */
161#pragma pack(push, 1)
162typedef struct {
163 uint8_t version; /**< Format version (MESHX_GPIO_CONFIG_VERSION_*) */
164 uint8_t pin_count; /**< Number of configured pins in this config */
165 uint16_t crc16; /**< CRC16 of all data following this header */
166 uint16_t total_size; /**< Total size of serialized data (header + pins) */
167 uint8_t flags; /**< Configuration flags (reserved for future use) */
168 uint8_t reserved[3]; /**< Reserved for future use, must be 0 */
170
171/**
172 * @struct meshx_gpio_pin_config_kv_t
173 * @brief Serialized GPIO pin configuration for KV Engine storage.
174 *
175 * Compact representation of a single GPIO pin configuration.
176 * Total size: 16 bytes (aligned for efficient storage).
177 */
178typedef struct {
179 uint8_t logical_pin; /**< Logical pin number (0-255) */
180 uint8_t physical_pin; /**< Physical pin number (BSP-specific) */
181 uint8_t mode; /**< Pin mode (meshx_gpio_mode_t) */
182 uint8_t pull; /**< Pull resistor setting (meshx_gpio_pull_t) */
183 uint8_t drive_strength; /**< Drive strength (meshx_gpio_drive_t) */
184 uint8_t initial_level; /**< Initial output level (0 or 1) */
185 uint8_t signal_inversion; /**< Signal inversion flag (0=false, 1=true) */
186 uint8_t config_type; /**< Extended config type: 0=none, 1=PWM, 2=interrupt */
188
189/**
190 * @struct meshx_gpio_pwm_config_kv_t
191 * @brief Serialized PWM configuration for KV Engine storage.
192 *
193 * Total size: 8 bytes.
194 * Follows meshx_gpio_pin_config_kv_t when config_type == 1.
195 */
196typedef struct {
197 uint32_t frequency; /**< PWM frequency in Hz */
198 uint8_t duty_cycle; /**< Duty cycle (0-100%) */
199 uint8_t resolution; /**< PWM resolution in bits */
200 uint8_t channel; /**< Hardware PWM channel */
201 uint8_t reserved; /**< Reserved, must be 0 */
203
204/**
205 * @struct meshx_gpio_intr_config_kv_t
206 * @brief Serialized interrupt configuration for KV Engine storage.
207 *
208 * Total size: 8 bytes.
209 * Follows meshx_gpio_pin_config_kv_t when config_type == 2.
210 */
211typedef struct {
212 uint8_t trigger_type; /**< Interrupt trigger type (meshx_gpio_intr_type_t) */
213 uint8_t task_priority; /**< Interrupt task priority */
214 uint16_t task_stack_size; /**< Interrupt task stack size in bytes */
215 uint8_t flags; /**< Interrupt flags (reserved) */
216 uint8_t reserved[3]; /**< Reserved, must be 0 */
218
219/**
220 * @struct meshx_gpio_pin_state_kv_t
221 * @brief Serialized GPIO pin state for KV Engine storage.
222 *
223 * Used for persisting runtime pin state across reboots.
224 * Total size: 8 bytes.
225 */
226typedef struct {
227 uint8_t logical_pin; /**< Logical pin number */
228 uint8_t current_level; /**< Current pin level (0 or 1) */
229 uint8_t pwm_started; /**< PWM running flag (0=false, 1=true) */
230 uint8_t current_duty; /**< Current PWM duty cycle (if applicable) */
231 uint32_t reserved; /**< Reserved for future use */
233#pragma pack(pop)
234
235/*============================================================================
236 * Size Constants
237 *============================================================================*/
238
239/** @brief Size of configuration header */
240#define MESHX_GPIO_CONFIG_HEADER_SIZE sizeof(meshx_gpio_config_header_kv_t)
241
242/** @brief Size of basic pin configuration */
243#define MESHX_GPIO_PIN_CONFIG_SIZE sizeof(meshx_gpio_pin_config_kv_t)
244
245/** @brief Size of PWM configuration extension */
246#define MESHX_GPIO_PWM_CONFIG_SIZE sizeof(meshx_gpio_pwm_config_kv_t)
247
248/** @brief Size of interrupt configuration extension */
249#define MESHX_GPIO_INTR_CONFIG_SIZE sizeof(meshx_gpio_intr_config_kv_t)
250
251/** @brief Size of pin state record */
252#define MESHX_GPIO_PIN_STATE_SIZE sizeof(meshx_gpio_pin_state_kv_t)
253
254/** @brief Maximum size per pin (including extensions) */
255#define MESHX_GPIO_PIN_MAX_SIZE (MESHX_GPIO_PIN_CONFIG_SIZE + \
256 MESHX_GPIO_PWM_CONFIG_SIZE)
257
258/** @brief Calculate total config size for given pin count */
259#define MESHX_GPIO_CONFIG_TOTAL_SIZE(pin_count) \
260 (MESHX_GPIO_CONFIG_HEADER_SIZE + ((pin_count) * MESHX_GPIO_PIN_CONFIG_SIZE))
261
262/*============================================================================
263 * Configuration Flags
264 *============================================================================*/
265
266/** @brief Flag: Configuration has PWM extensions */
267#define MESHX_GPIO_CONFIG_FLAG_HAS_PWM (1 << 0)
268
269/** @brief Flag: Configuration has interrupt extensions */
270#define MESHX_GPIO_CONFIG_FLAG_HAS_INTR (1 << 1)
271
272/** @brief Flag: Configuration is migrated from older version */
273#define MESHX_GPIO_CONFIG_FLAG_MIGRATED (1 << 2)
274
275/** @brief Flag: Configuration is in read-only mode */
276#define MESHX_GPIO_CONFIG_FLAG_READ_ONLY (1 << 3)
277
278/*============================================================================
279 * Config Type Values (for meshx_gpio_pin_config_kv_t.config_type)
280 *============================================================================*/
281
282/** @brief No extended configuration */
283#define MESHX_GPIO_CONFIG_TYPE_NONE 0
284
285/** @brief PWM configuration follows */
286#define MESHX_GPIO_CONFIG_TYPE_PWM 1
287
288/** @brief Interrupt configuration follows */
289#define MESHX_GPIO_CONFIG_TYPE_INTR 2
290
291/*============================================================================
292 * CRC16 Functions
293 *============================================================================*/
294
295/**
296 * @brief Calculate CRC16 compatible with KV Engine.
297 *
298 * Uses the same CRC-16 algorithm as meshx_kv_engine.c (Modbus CRC-16
299 * with polynomial 0xA001). This ensures the GPIO subsystem can
300 * independently verify data integrity.
301 *
302 * @param data Pointer to data buffer
303 * @param len Length of data in bytes
304 * @return CRC16 value
305 */
306static inline uint16_t meshx_gpio_kv_calc_crc16(const uint8_t *data, uint32_t len)
307{
308 uint16_t crc = 0xFFFF;
309 for (uint32_t i = 0; i < len; i++) {
310 crc ^= data[i];
311 for (int j = 0; j < 8; j++) {
312 if (crc & 1) {
313 crc = (crc >> 1) ^ 0xA001;
314 } else {
315 crc >>= 1;
316 }
317 }
318 }
319 return crc;
320}
321
322/**
323 * @brief Verify CRC16 of serialized GPIO configuration.
324 *
325 * @param data Pointer to serialized data (including header)
326 * @param len Total length of data
327 * @return true if CRC is valid, false otherwise
328 */
329static inline bool meshx_gpio_kv_verify_crc16(const uint8_t *data, uint32_t len)
330{
331 if (!data || len < MESHX_GPIO_CONFIG_HEADER_SIZE) {
332 return false;
333 }
334
335 const meshx_gpio_config_header_kv_t *header =
336 (const meshx_gpio_config_header_kv_t *)data;
337
338 /* CRC covers everything after the crc16 field in the header */
339 const uint8_t *crc_data = data + offsetof(meshx_gpio_config_header_kv_t, total_size);
340 uint32_t crc_len = len - offsetof(meshx_gpio_config_header_kv_t, total_size);
341
342 uint16_t calculated = meshx_gpio_kv_calc_crc16(crc_data, crc_len);
343 return (calculated == header->crc16);
344}
345
346/*============================================================================
347 * Serialization/Deserialization Functions
348 *============================================================================*/
349
350/**
351 * @brief Initialize a GPIO configuration header.
352 *
353 * @param header Pointer to header to initialize
354 * @param pin_count Number of pins in configuration
355 */
357 uint8_t pin_count)
358{
359 if (!header) return;
360
362 header->pin_count = pin_count;
363 header->crc16 = 0;
364 header->total_size = (uint16_t)MESHX_GPIO_CONFIG_TOTAL_SIZE(pin_count);
365 header->flags = 0;
366 header->reserved[0] = 0;
367 header->reserved[1] = 0;
368 header->reserved[2] = 0;
369}
370
371/**
372 * @brief Calculate and set CRC16 for serialized configuration.
373 *
374 * @param data Pointer to serialized data buffer (including header)
375 * @param len Total length of data
376 */
377static inline void meshx_gpio_kv_finalize_crc16(uint8_t *data, uint32_t len)
378{
379 if (!data || len < MESHX_GPIO_CONFIG_HEADER_SIZE) return;
380
382
383 /* CRC covers everything after the crc16 field */
384 const uint8_t *crc_data = data + offsetof(meshx_gpio_config_header_kv_t, total_size);
385 uint32_t crc_len = len - offsetof(meshx_gpio_config_header_kv_t, total_size);
386
387 header->crc16 = meshx_gpio_kv_calc_crc16(crc_data, crc_len);
388}
389
390/**
391 * @brief Convert runtime pin config to serialized format.
392 *
393 * @param src Source runtime configuration
394 * @param dst Destination serialized configuration
395 */
398{
399 if (!src || !dst) return;
400
401 dst->logical_pin = src->logical_pin;
402 dst->physical_pin = src->physical_pin;
403 dst->mode = src->mode;
404 dst->pull = src->pull;
405 dst->drive_strength = src->drive_strength;
406 dst->initial_level = src->initial_level;
407 dst->signal_inversion = src->signal_inversion ? 1 : 0;
408
409 /* Determine config type based on mode */
410 if (src->mode == 5) { /* MESHX_GPIO_MODE_PWM_OUTPUT */
412 } else if (src->mode == 0 && src->mode_config.interrupt.trigger != 0) {
413 /* Input mode with interrupt */
415 } else {
417 }
418}
419
420/**
421 * @brief Convert serialized pin config to runtime format.
422 *
423 * @param src Source serialized configuration
424 * @param dst Destination runtime configuration
425 */
428{
429 if (!src || !dst) return;
430
431 dst->logical_pin = src->logical_pin;
432 dst->physical_pin = src->physical_pin;
433 dst->mode = src->mode;
434 dst->pull = src->pull;
435 dst->drive_strength = src->drive_strength;
436 dst->initial_level = src->initial_level;
437 dst->signal_inversion = src->signal_inversion ? true : false;
438}
439
440/**
441 * @brief Serialize PWM configuration.
442 *
443 * @param src Source runtime PWM configuration
444 * @param dst Destination serialized configuration
445 */
448{
449 if (!src || !dst) return;
450
454 dst->channel = src->mode_config.pwm.channel;
455 dst->reserved = 0;
456}
457
458/**
459 * @brief Deserialize PWM configuration.
460 *
461 * @param src Source serialized configuration
462 * @param dst Destination runtime configuration
463 */
466{
467 if (!src || !dst) return;
468
472 dst->mode_config.pwm.channel = src->channel;
473}
474
475/**
476 * @brief Serialize interrupt configuration.
477 *
478 * @param src Source runtime interrupt configuration
479 * @param dst Destination serialized configuration
480 */
483{
484 if (!src || !dst) return;
485
489 dst->flags = 0;
490 dst->reserved[0] = 0;
491 dst->reserved[1] = 0;
492 dst->reserved[2] = 0;
493}
494
495/**
496 * @brief Deserialize interrupt configuration.
497 *
498 * @param src Source serialized configuration
499 * @param dst Destination runtime configuration
500 */
503{
504 if (!src || !dst) return;
505
509}
510
511/*============================================================================
512 * Version Compatibility Functions
513 *============================================================================*/
514
515/**
516 * @brief Check if a configuration version is supported.
517 *
518 * @param version Version number to check
519 * @return true if version is supported, false otherwise
520 */
521static inline bool meshx_gpio_kv_is_version_supported(uint8_t version)
522{
523 return (version >= MESHX_GPIO_CONFIG_VERSION_MIN &&
525}
526
527/**
528 * @brief Get migration requirement between versions.
529 *
530 * @param from_version Source version
531 * @param to_version Target version
532 * @return true if migration is needed, false if direct loading is possible
533 */
534static inline bool meshx_gpio_kv_needs_migration(uint8_t from_version,
535 uint8_t to_version)
536{
537 return (from_version != to_version);
538}
539
540/*============================================================================
541 * KV Engine Persistence API Functions
542 *============================================================================*/
543
544/**
545 * @brief Initialize GPIO KV Engine persistence.
546 *
547 * This function initializes the KV Engine for GPIO configuration storage.
548 * It must be called before any other GPIO KV functions.
549 *
550 * @param kv_partition Pointer to the flash partition to use for KV storage
551 * @param product_name Product name for key prefix (max 16 chars)
552 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
553 */
555 const char *product_name);
556
557/**
558 * @brief Save GPIO configuration to KV Engine.
559 *
560 * This function serializes and saves the GPIO configuration to KV Engine
561 * with versioning and CRC validation.
562 *
563 * @param configs Array of pin configurations to save
564 * @param pin_count Number of pins in configuration
565 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
566 */
568 uint8_t pin_count);
569
570/**
571 * @brief Load GPIO configuration from KV Engine.
572 *
573 * This function loads and deserializes GPIO configuration from KV Engine.
574 * If the configuration is not found or corrupted, returns MESHX_NOT_FOUND
575 * to allow fallback to compiled defaults.
576 *
577 * @param configs Array to store loaded pin configurations
578 * @param max_pins Maximum pins that can be stored
579 * @param[out] out_pin_count Actual number of pins loaded
580 * @return meshx_err_t MESHX_SUCCESS on success, MESHX_NOT_FOUND if not found,
581 * error code on other failures
582 */
584 uint8_t max_pins,
585 uint8_t *out_pin_count);
586
587/**
588 * @brief Check if GPIO configuration exists in KV Engine.
589 *
590 * @param[out] exists true if configuration exists, false otherwise
591 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
592 */
594
595/**
596 * @brief Save current pin state to KV Engine.
597 *
598 * Persists the runtime state of a GPIO pin across reboots.
599 *
600 * @param logical_pin Logical pin number
601 * @param state Pointer to pin state to save
602 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
603 */
606
607/**
608 * @brief Load pin state from KV Engine.
609 *
610 * Loads previously persisted runtime state of a GPIO pin.
611 *
612 * @param logical_pin Logical pin number
613 * @param[out] state Pointer to store loaded pin state
614 * @return meshx_err_t MESHX_SUCCESS on success, MESHX_NOT_FOUND if not found,
615 * error code on other failures
616 */
619
620/**
621 * @brief Export GPIO configuration to serialized format.
622 *
623 * Exports the configuration in binary format for transfer to another device.
624 *
625 * @param configs Array of pin configurations
626 * @param pin_count Number of pins
627 * @param[out] buf Output buffer
628 * @param buf_len Buffer length
629 * @param[out] out_size Actual exported size
630 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
631 */
633 uint8_t pin_count,
634 uint8_t *buf,
635 uint16_t buf_len,
636 uint16_t *out_size);
637
638/**
639 * @brief Import GPIO configuration from serialized format.
640 *
641 * Imports configuration from binary format received from another device.
642 *
643 * @param buf Input buffer
644 * @param buf_len Buffer length
645 * @param[out] configs Array to store imported configurations
646 * @param max_pins Maximum pins that can be stored
647 * @param[out] out_pin_count Actual number of pins imported
648 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
649 */
650meshx_err_t meshx_gpio_import_config(const uint8_t *buf,
651 uint16_t buf_len,
653 uint8_t max_pins,
654 uint8_t *out_pin_count);
655
656/**
657 * @brief Clear GPIO configuration from KV Engine.
658 *
659 * Removes the stored GPIO configuration, forcing fallback to defaults
660 * on next load.
661 *
662 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
663 */
665
666/**
667 * @brief Deinitialize GPIO KV Engine persistence.
668 *
669 * Cleans up KV persistence resources. Does not affect the underlying
670 * KV Engine partition.
671 *
672 * @return meshx_err_t MESHX_SUCCESS on success, error code on failure
673 */
675
676/**
677 * @brief Check if GPIO KV persistence is initialized.
678 *
679 * @return true if initialized, false otherwise
680 */
682
683#ifdef __cplusplus
684}
685#endif
686
687#endif /* __MESHX_GPIO_KV_H */
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
Flash Abstraction Layer (FAL) Interface for MeshX.
#define MESHX_GPIO_KV_KEY_STATE
Key suffix for GPIO pin state.
Definition meshx_gpio_kv.h:57
#define MESHX_GPIO_CONFIG_TOTAL_SIZE(pin_count)
Calculate total config size for given pin count.
Definition meshx_gpio_kv.h:259
static uint8_t meshx_gpio_kv_make_pwm_key(const char *product_name, uint8_t logical_pin, char *buf, uint8_t buf_len)
Construct KV Engine key for PWM configuration.
Definition meshx_gpio_kv.h:114
static void meshx_gpio_kv_deserialize_pin(const meshx_gpio_pin_config_kv_t *src, meshx_gpio_pin_config_t *dst)
Convert serialized pin config to runtime format.
Definition meshx_gpio_kv.h:426
static void meshx_gpio_kv_serialize_pin(const meshx_gpio_pin_config_t *src, meshx_gpio_pin_config_kv_t *dst)
Convert runtime pin config to serialized format.
Definition meshx_gpio_kv.h:396
static bool meshx_gpio_kv_verify_crc16(const uint8_t *data, uint32_t len)
Verify CRC16 of serialized GPIO configuration.
Definition meshx_gpio_kv.h:329
static uint8_t meshx_gpio_kv_make_state_key(const char *product_name, uint8_t logical_pin, char *buf, uint8_t buf_len)
Construct KV Engine key for GPIO pin state.
Definition meshx_gpio_kv.h:92
#define MESHX_GPIO_CONFIG_VERSION_MIN
Minimum supported version (for backward compatibility).
Definition meshx_gpio_kv.h:38
#define MESHX_GPIO_KV_KEY_CONFIG
Key suffix for main GPIO configuration.
Definition meshx_gpio_kv.h:54
static bool meshx_gpio_kv_is_version_supported(uint8_t version)
Check if a configuration version is supported.
Definition meshx_gpio_kv.h:521
#define MESHX_GPIO_CONFIG_TYPE_NONE
No extended configuration.
Definition meshx_gpio_kv.h:283
#define MESHX_GPIO_CONFIG_TYPE_PWM
PWM configuration follows.
Definition meshx_gpio_kv.h:286
meshx_err_t meshx_gpio_kv_init(const meshx_fal_partition_t *kv_partition, const char *product_name)
Initialize GPIO KV Engine persistence.
Definition meshx_gpio_kv.c:230
static void meshx_gpio_kv_serialize_pwm(const meshx_gpio_pin_config_t *src, meshx_gpio_pwm_config_kv_t *dst)
Serialize PWM configuration.
Definition meshx_gpio_kv.h:446
meshx_err_t meshx_gpio_save_pin_state_to_kv(uint8_t logical_pin, const meshx_gpio_pin_state_t *state)
Save current pin state to KV Engine.
Definition meshx_gpio_kv.c:420
#define MESHX_GPIO_KV_KEY_MAX_LEN
Maximum total key length (KV Engine limit is 32).
Definition meshx_gpio_kv.h:51
bool meshx_gpio_kv_is_initialized(void)
Check if GPIO KV persistence is initialized.
Definition meshx_gpio_kv.c:639
meshx_err_t meshx_gpio_load_config_from_kv(meshx_gpio_pin_config_t *configs, uint8_t max_pins, uint8_t *out_pin_count)
Load GPIO configuration from KV Engine.
Definition meshx_gpio_kv.c:331
#define MESHX_GPIO_CONFIG_HEADER_SIZE
Size of configuration header.
Definition meshx_gpio_kv.h:240
static uint8_t meshx_gpio_kv_make_config_key(const char *product_name, char *buf, uint8_t buf_len)
Construct KV Engine key for GPIO configuration.
Definition meshx_gpio_kv.h:72
#define MESHX_GPIO_KV_PRODUCT_NAME_MAX_LEN
Maximum length for product name in key prefix.
Definition meshx_gpio_kv.h:48
meshx_err_t meshx_gpio_kv_deinit(void)
Deinitialize GPIO KV Engine persistence.
Definition meshx_gpio_kv.c:620
static uint8_t meshx_gpio_kv_make_intr_key(const char *product_name, uint8_t logical_pin, char *buf, uint8_t buf_len)
Construct KV Engine key for interrupt configuration.
Definition meshx_gpio_kv.h:136
static void meshx_gpio_kv_finalize_crc16(uint8_t *data, uint32_t len)
Calculate and set CRC16 for serialized configuration.
Definition meshx_gpio_kv.h:377
static void meshx_gpio_kv_deserialize_intr(const meshx_gpio_intr_config_kv_t *src, meshx_gpio_pin_config_t *dst)
Deserialize interrupt configuration.
Definition meshx_gpio_kv.h:501
meshx_err_t meshx_gpio_export_config(const meshx_gpio_pin_config_t *configs, uint8_t pin_count, uint8_t *buf, uint16_t buf_len, uint16_t *out_size)
Export GPIO configuration to serialized format.
Definition meshx_gpio_kv.c:532
meshx_err_t meshx_gpio_load_pin_state_from_kv(uint8_t logical_pin, meshx_gpio_pin_state_t *state)
Load pin state from KV Engine.
Definition meshx_gpio_kv.c:477
static bool meshx_gpio_kv_needs_migration(uint8_t from_version, uint8_t to_version)
Get migration requirement between versions.
Definition meshx_gpio_kv.h:534
#define MESHX_GPIO_CONFIG_TYPE_INTR
Interrupt configuration follows.
Definition meshx_gpio_kv.h:289
meshx_err_t meshx_gpio_import_config(const uint8_t *buf, uint16_t buf_len, meshx_gpio_pin_config_t *configs, uint8_t max_pins, uint8_t *out_pin_count)
Import GPIO configuration from serialized format.
Definition meshx_gpio_kv.c:558
#define MESHX_GPIO_CONFIG_VERSION_CURRENT
Current GPIO configuration serialization format version.
Definition meshx_gpio_kv.h:35
static void meshx_gpio_kv_deserialize_pwm(const meshx_gpio_pwm_config_kv_t *src, meshx_gpio_pin_config_t *dst)
Deserialize PWM configuration.
Definition meshx_gpio_kv.h:464
#define MESHX_GPIO_KV_KEY_INTR
Key suffix for interrupt configuration.
Definition meshx_gpio_kv.h:63
static void meshx_gpio_kv_serialize_intr(const meshx_gpio_pin_config_t *src, meshx_gpio_intr_config_kv_t *dst)
Serialize interrupt configuration.
Definition meshx_gpio_kv.h:481
meshx_err_t meshx_gpio_clear_config_in_kv(void)
Clear GPIO configuration from KV Engine.
Definition meshx_gpio_kv.c:580
meshx_err_t meshx_gpio_save_config_to_kv(const meshx_gpio_pin_config_t *configs, uint8_t pin_count)
Save GPIO configuration to KV Engine.
Definition meshx_gpio_kv.c:269
meshx_err_t meshx_gpio_config_exists_in_kv(bool *exists)
Check if GPIO configuration exists in KV Engine.
Definition meshx_gpio_kv.c:384
static void meshx_gpio_kv_init_header(meshx_gpio_config_header_kv_t *header, uint8_t pin_count)
Initialize a GPIO configuration header.
Definition meshx_gpio_kv.h:356
#define MESHX_GPIO_CONFIG_VERSION_MAX
Maximum supported version (for forward compatibility).
Definition meshx_gpio_kv.h:41
static uint16_t meshx_gpio_kv_calc_crc16(const uint8_t *data, uint32_t len)
Calculate CRC16 compatible with KV Engine.
Definition meshx_gpio_kv.h:306
#define MESHX_GPIO_KV_KEY_PWM
Key suffix for PWM configuration.
Definition meshx_gpio_kv.h:60
MeshX GPIO Type Definitions.
uint8_t state
Definition meshx_serial.c:39
Definition meshx_fal_interface.h:27
Header for serialized GPIO configuration stored in KV Engine.
Definition meshx_gpio_kv.h:162
uint8_t flags
Definition meshx_gpio_kv.h:167
uint16_t total_size
Definition meshx_gpio_kv.h:166
uint16_t crc16
Definition meshx_gpio_kv.h:165
uint8_t reserved[3]
Definition meshx_gpio_kv.h:168
uint8_t pin_count
Definition meshx_gpio_kv.h:164
uint8_t version
Definition meshx_gpio_kv.h:163
Serialized interrupt configuration for KV Engine storage.
Definition meshx_gpio_kv.h:211
uint8_t trigger_type
Definition meshx_gpio_kv.h:212
uint8_t flags
Definition meshx_gpio_kv.h:215
uint8_t reserved[3]
Definition meshx_gpio_kv.h:216
uint16_t task_stack_size
Definition meshx_gpio_kv.h:214
uint8_t task_priority
Definition meshx_gpio_kv.h:213
Serialized GPIO pin configuration for KV Engine storage.
Definition meshx_gpio_kv.h:178
uint8_t logical_pin
Definition meshx_gpio_kv.h:179
uint8_t pull
Definition meshx_gpio_kv.h:182
uint8_t mode
Definition meshx_gpio_kv.h:181
uint8_t config_type
Definition meshx_gpio_kv.h:186
uint8_t physical_pin
Definition meshx_gpio_kv.h:180
uint8_t drive_strength
Definition meshx_gpio_kv.h:183
uint8_t signal_inversion
Definition meshx_gpio_kv.h:185
uint8_t initial_level
Definition meshx_gpio_kv.h:184
GPIO Pin Configuration Structure.
Definition meshx_gpio_types.h:51
uint8_t logical_pin
Definition meshx_gpio_types.h:52
uint8_t channel
Definition meshx_gpio_types.h:74
uint8_t resolution
Definition meshx_gpio_types.h:73
uint32_t frequency
Definition meshx_gpio_types.h:71
union meshx_gpio_pin_config_t::@052245231341204203052073036272225075106126036255 mode_config
Mode-specific configuration (union based on mode).
uint8_t task_priority
Definition meshx_gpio_types.h:65
uint8_t mode
Definition meshx_gpio_types.h:54
uint8_t drive_strength
Definition meshx_gpio_types.h:56
uint16_t task_stack_size
Definition meshx_gpio_types.h:66
uint8_t duty_cycle
Definition meshx_gpio_types.h:72
struct meshx_gpio_pin_config_t::@052245231341204203052073036272225075106126036255::@372143037256331057042353173011302234062346240366 interrupt
Interrupt configuration (for input pins with interrupts).
uint8_t initial_level
Definition meshx_gpio_types.h:57
uint8_t physical_pin
Definition meshx_gpio_types.h:53
uint8_t trigger
Definition meshx_gpio_types.h:64
struct meshx_gpio_pin_config_t::@052245231341204203052073036272225075106126036255::@134165302231261162316035331261052006324312255312 pwm
PWM configuration (for PWM output pins).
bool signal_inversion
Definition meshx_gpio_types.h:58
uint8_t pull
Definition meshx_gpio_types.h:55
Serialized GPIO pin state for KV Engine storage.
Definition meshx_gpio_kv.h:226
uint8_t current_duty
Definition meshx_gpio_kv.h:230
uint8_t current_level
Definition meshx_gpio_kv.h:228
uint8_t logical_pin
Definition meshx_gpio_kv.h:227
uint8_t pwm_started
Definition meshx_gpio_kv.h:229
uint32_t reserved
Definition meshx_gpio_kv.h:231
GPIO Pin State Structure.
Definition meshx_gpio_types.h:91
Serialized PWM configuration for KV Engine storage.
Definition meshx_gpio_kv.h:196
uint8_t duty_cycle
Definition meshx_gpio_kv.h:198
uint8_t channel
Definition meshx_gpio_kv.h:200
uint8_t reserved
Definition meshx_gpio_kv.h:201
uint8_t resolution
Definition meshx_gpio_kv.h:199
uint32_t frequency
Definition meshx_gpio_kv.h:197