79 lines
2.3 KiB
C
79 lines
2.3 KiB
C
|
#ifndef _INCLUDE_EXTERNAL_INTERFACE_EVENT_
|
||
|
#define _INCLUDE_EXTERNAL_INTERFACE_EVENT_
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <stdint.h>
|
||
|
#include <string.h>
|
||
|
#include "external_interface.h"
|
||
|
|
||
|
#define ET_TYPE_EVENT 32
|
||
|
#define ET_EVENT_TRIGGER_BUFFER_SIZE 32
|
||
|
|
||
|
#ifdef __cplusplus
|
||
|
extern "C" {
|
||
|
#endif
|
||
|
|
||
|
typedef void (*event_callback)(const char*, size_t, void*, void*);
|
||
|
|
||
|
typedef struct _EventRegister {
|
||
|
const char* name;
|
||
|
event_callback callback;
|
||
|
void* user_data;
|
||
|
} EventRegister, EventUnregister;
|
||
|
|
||
|
typedef struct _EventTrigger {
|
||
|
const char* name;
|
||
|
size_t args_size;
|
||
|
uint8_t args[ET_EVENT_TRIGGER_BUFFER_SIZE];
|
||
|
} EventTrigger;
|
||
|
|
||
|
#define EXINT_EVENT_PUBLIC static __inline
|
||
|
#ifndef EXINT_EVENT
|
||
|
#define EXINT_EVENT_PUBLIC_FUNC_NAME(name) exint_ ## name
|
||
|
#else
|
||
|
#define EXINT_EVENT_PUBLIC_FUNC_NAME(name) exint_public_ ## name
|
||
|
#endif
|
||
|
#define EXINT_EVENT_PUBLIC_FUNC(name) EXINT_EVENT_PUBLIC void EXINT_EVENT_PUBLIC_FUNC_NAME(name)
|
||
|
|
||
|
EXINT_EVENT_PUBLIC_FUNC(event)
|
||
|
(const char* name, const size_t args_size, void* args) {
|
||
|
if (args_size <= ET_EVENT_TRIGGER_BUFFER_SIZE) {
|
||
|
EventTrigger trigger = {name, args_size, {0}};
|
||
|
memcpy(trigger.args, args, args_size);
|
||
|
exint_send(
|
||
|
ET_TYPE_EVENT,
|
||
|
sizeof(EventTrigger) + args_size - ET_EVENT_TRIGGER_BUFFER_SIZE,
|
||
|
&trigger
|
||
|
);
|
||
|
} else {
|
||
|
EventTrigger* trigger = (EventTrigger*)malloc(sizeof(EventTrigger) + args_size - ET_EVENT_TRIGGER_BUFFER_SIZE);
|
||
|
if (trigger == NULL) return;
|
||
|
trigger->name = name;
|
||
|
trigger->args_size = args_size;
|
||
|
memcpy(trigger->args, args, args_size);
|
||
|
exint_send(
|
||
|
ET_TYPE_EVENT,
|
||
|
sizeof(EventTrigger) + args_size - ET_EVENT_TRIGGER_BUFFER_SIZE,
|
||
|
trigger
|
||
|
);
|
||
|
free(trigger);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
EXINT_EVENT_PUBLIC_FUNC(event_register)
|
||
|
(const char *event_name, event_callback callback, void* user_data) {
|
||
|
EventRegister register_ = {event_name, callback, user_data};
|
||
|
EXINT_EVENT_PUBLIC_FUNC_NAME(event)("event_register", sizeof(EventRegister), ®ister_);
|
||
|
}
|
||
|
|
||
|
EXINT_EVENT_PUBLIC_FUNC(event_unregister)
|
||
|
(const char *event_name, event_callback callback, void* user_data) {
|
||
|
EventUnregister unregister = {event_name, callback, user_data};
|
||
|
EXINT_EVENT_PUBLIC_FUNC_NAME(event)("event_unregister", sizeof(EventUnregister), &unregister);
|
||
|
}
|
||
|
|
||
|
#ifdef __cplusplus
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
#endif
|