zephyr settings part 3: persistence of multiple key-values at once (handler)

LOAD MULTIPLE VARIABLES AT ONCE EXAMPLE:

direct_length_data: your structure variable that will be set:



struct direct_length_data {
    uint64_t length;
    uint16_t length_1;
    uint32_t length_2;
};

the handler: direct_loader

static int direct_loader(const char *name, size_t len, settings_read_cb read_cb,
              void *cb_arg, void *param)
{
    const char *next;
    size_t name_len;
    int rc;
    struct direct_length_data *dest = (struct direct_length_data *)param;

    printk("direct load: ");

    name_len = settings_name_next(name, &next);

    if (name_len == 0) {
        rc = read_cb(cb_arg, &(dest->length), sizeof(dest->length));
        printk("<alpha/length>\n");
        return 0;
    }

    name_len = settings_name_next(name, &next);
    if (next) {
        printk("nothing\n");
        return -ENOENT;
    }

    if (!strncmp(name, "1", name_len)) {
        rc = read_cb(cb_arg, &(dest->length_1), sizeof(dest->length_1));
        printk("<alpha/length/1>\n");
        return 0;
    }

    if (!strncmp(name, "2", name_len)) {
        rc = read_cb(cb_arg, &(dest->length_2), sizeof(dest->length_2));
        printk("<alpha/length/2>\n");
        return 0;
    }

    printk("nothing\n");
    return -ENOENT;
}

example using the handler direct_loader and structure variable struct direct_length_data:

static void example_direct_load_subtree(void)
{
    struct direct_length_data dld;
    int rc;

    /* load subtree directly using call-specific handler `direct_loader'
     * This handder loads subtree values to call-speciffic structure of type
     * 'direct_length_data`.
     */
    printk(SECTION_BEGIN_LINE);
    printk("loading subtree to destination provided by the caller\n\n");
    rc = settings_load_subtree_direct("alpha/length", direct_loader,
                      (void *)&dld);
    if (rc == 0) {
        printk("  direct.length = %" PRId64 "\n", dld.length);
        printk("  direct.length_1 = %d\n", dld.length_1);
        printk("  direct.length_2 = %d\n", dld.length_2);
    } else {
        printk("  direct load fails unexpectedly\n");
    }
}

You'll only receive email when they publish something new.

More from Loïc Devaux
All posts