ppd/include/mod.h
2023-03-13 08:32:55 +01:00

70 lines
1.8 KiB
C

#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include "logger.h"
typedef int (*ppd_global_init_cb)(int argc, char** argk, char** argv, void **ctx);
typedef void (*ppd_global_free_cb)(void *ctx);
typedef int (*ppd_thread_init_cb)(int core, void * global_ctx, void **ctx);
typedef void (*ppd_thread_free_cb)(int core, void *global_ctx, void *ctx);
typedef int (*ppd_conn_init_cb)(void * global_ctx, void * thread_ctx, void **ctx);
typedef void (*ppd_conn_free_cb)(void * global_ctx, void * thread_ctx, void *conn_ctx);
typedef int (*ppd_conn_recv_cb)(const char * data, size_t sz, void * global_ctx, void * thread_ctx, void * conn_ctx);
typedef int (*ppd_conn_send_cb)(const char * out, size_t sz, size_t * out_sz, void * global_ctx, void * thread_ctx, void * conn_ctx);
struct ppd_mod_info {
const char * name;
ppd_global_init_cb global_init_cb;
ppd_global_free_cb global_free_cb;
ppd_thread_init_cb thread_init_cb;
ppd_thread_free_cb thread_free_cb;
ppd_conn_init_cb conn_init_cb;
ppd_conn_free_cb conn_free_cb;
ppd_conn_send_cb conn_send_cb;
ppd_conn_recv_cb conn_recv_cb;
};
typedef struct ppd_mod_info * (*ppd_get_mod_info_fn)(void);
#define PPD_GET_MOD_INFO_ID ppd_getmod_info
#define STR(x) #x
#define STRX(x) STR(x)
#define inline inline __attribute__((unused))
static inline struct ppd_mod_info *
ppd_load_module(const char *path)
{
void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (handle == NULL) {
E("Failed to load module %s: %s.\n", path, dlerror());
}
ppd_get_mod_info_fn fn = (ppd_get_mod_info_fn)dlfunc(handle, STRX(PPD_GET_MOD_INFO_ID));
if (fn == NULL) {
E("Failed to find symbol %s: %s\n", STRX(PPD_GET_MOD_INFO_ID), dlerror());
}
return fn();
}
#undef inline
#ifdef __cplusplus
}
#endif