Skip to content

Invoker

Invoker API

The Invoker API provides a command invocation mechanism using caller provided runtime and (command) hooks.

Component Diagram

invoker-api

Example

The following example demonstrates how to use the Invoker API.

// Copyright 2026 Robert Bosch GmbH

#include <stdio.h>
#include <stdlib.h>
#include <dse/clib/invoker/invoker.h>

enum { HOOK_INIT = 1, HOOK_RUN, HOOK_STOP };

typedef struct SysCmdCtx {
    int count;
} SysCmdCtx;

static int loader(InvokerDesc* inv)
{
    const InvokerHook hooks[] = {
        { .id = HOOK_INIT, .command = "echo '[init] starting'" },
        { .id = HOOK_RUN, .command = "echo '[run] working'" },
        { .id = HOOK_STOP, .command = "echo '[stop] done'" },
        { 0 },
    };
    return invoker_push(inv, hooks);
}

static int runtime(InvokerDesc* inv, const InvokerHook* hook)
{
    SysCmdCtx* ctx = inv->runtime_ctx;

    if (hook == NULL) {
        if (ctx != NULL) {
            printf("Total commands executed: %d\n", ctx->count);
            free(ctx);
            inv->runtime_ctx = NULL;
        }
        return 0;
    }
    if (ctx == NULL) {
        ctx = inv->runtime_ctx = calloc(1, sizeof(*ctx));
        if (ctx == NULL) return -ENOMEM;
    }

    ctx->count++;
    return system(hook->command);
}

int main(void)
{
    InvokerDesc inv = invoker_create(runtime, loader);
    if (inv.runtime_func == NULL) return 1;

    invoker_invoke(&inv, HOOK_INIT);
    invoker_invoke(&inv, HOOK_RUN);
    invoker_invoke(&inv, HOOK_STOP);

    invoker_destroy(&inv);
    return 0;
}

Typedefs

InvokerDesc

typedef struct InvokerDesc {
    InvokerRuntimeFunc runtime_func;
    void* runtime_ctx;
    int hooks;
}

InvokerHook

typedef struct InvokerHook {
    InvokerHookId id;
    const char* name;
    const char* command;
}

pthread_attr_t

typedef struct pthread_attr_t {
    char[56] __size;
    long __align;
}

Functions

invoker_create

Create an invoker with a runtime function and optionally load its hooks.

Parameters

runtime_func (InvokerRuntimeFunc)
Runtime function used to execute hooks. Must not be NULL.
loader_func (InvokerLoaderFunc)
Optional function used to populate the invoker's hooks. Pass NULL to skip loading.

Returns

InvokerDesc
Initialized invoker. If runtime_func is NULL, errno is set to EINVAL. If loader_func returns a nonzero value, the invoker is destroyed and errno is set to ECANCELED.

invoker_destroy

Destroy an invoker and release its registered hooks. If a runtime function is configured, it is called with a NULL hook before the hooks are released.

Parameters

inv (InvokerDesc*)
Pointer to the InvokerDesc, or NULL.

Returns

void
The invoker was destroyed, or the pointer was NULL.

invoker_invoke

Invoke the command for a hook ID using the configured runtime function. If no matching hook exists, or its command is NULL or empty, no runtime call is made.

Parameters

inv (InvokerDesc*)
Pointer to the InvokerDesc.
id (InvokerHookId)
ID of the hook to invoke.

Returns

N (int)
Return value from the runtime function, or zero if no command was invoked.
-EINVAL (-22)
inv is NULL or has no runtime function.

invoker_push

Copy hooks into an invoker and sort them by hook ID. The input array must end with an entry whose name and command are both NULL. Hook strings are copied and owned by the invoker.

Parameters

inv (InvokerDesc*)
Pointer to the InvokerDesc.
hooks (const InvokerHook*)
Sentinel-terminated array of hooks to copy into the invoker.

Returns

0 (int)
Hooks were copied successfully.
-EINVAL (-22)
inv or hooks is NULL.