Skip to content

Commit

Permalink
Action manager registration and dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAbata committed Mar 29, 2024
1 parent c611a28 commit 327528e
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/system/action_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
extern "C" {
#endif

void action_manager_init(void);
void action_manager_load_drivercfg(const char* filename);
void action_manager_load_all_drivercfg(void);
void action_manager_dispatch_event(const char* event, int val);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions include/system/event_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {

#define EVENT_HANDLER_ARG_TYPE void*

const char* EVT_ALL;
EVENTS(EVENT_DECL);

/**
Expand Down
16 changes: 16 additions & 0 deletions include/util/strcase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __util__strcase_h
#define __util__strcase_h

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

size_t str_to_camel_case(char* out, size_t out_len, const char* in);

#ifdef __cplusplus
}
#endif

#endif
2 changes: 1 addition & 1 deletion lib/mt-accessory-common
10 changes: 10 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "util/i18n.h"
#include "version.h"
#include "wifi_manager.h"
#include <esp_system.h>
#include <time.h>

static const char* TAG = "main";
Expand Down Expand Up @@ -82,6 +83,14 @@ static void loop_task(void* args) {
}

if (millis() - lastTick > 1000 / 15) {
ESP_LOGD(
TAG,
"%%heap=%d, min=%d, internal=%d",
esp_get_free_heap_size(),
esp_get_minimum_free_heap_size(),
esp_get_free_internal_heap_size()
);

lastTick = millis();
api_broadcast_readings();
}
Expand Down Expand Up @@ -127,6 +136,7 @@ void app_main() {
bluetooth_driver_init();
orgasm_control_init();
i18n_init();
action_manager_init();

// Red = preboot
eom_hal_set_sensor_sensitivity(Config.sensor_sensitivity);
Expand Down
41 changes: 41 additions & 0 deletions src/system/action_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
#include "cJSON.h"
#include "eom-hal.h"
#include "maus_bus_drivercfg.h"
#include "system/event_manager.h"
#include "util/list.h"
#include "util/strcase.h"
#include <dirent.h>
#include <esp_log.h>
#include <stdio.h>
#include <string.h>

static const char* TAG = "system:action_manager";

static list_t _drivercfgs = LIST_DEFAULT();

#define DRIVERCFG_DIR "drivercfg"

void action_manager_load_all_drivercfg(void) {
Expand Down Expand Up @@ -80,8 +85,44 @@ void action_manager_load_drivercfg(const char* path) {
goto cleanup;
}

list_add(&_drivercfgs, (void*)drivercfg);

cleanup:
cJSON_Delete(drivercfg_json);
free(buffer);
fclose(f);
}

void action_manager_dispatch_event(const char* event, int arg) {
maus_bus_drivercfg_t* drivercfg = NULL;

const char* evt_strip = strstr("EVT_", event);
if (strncmp("EVT_", event, 4)) return;

size_t evt_name_len = str_to_camel_case(NULL, 0, event + 4);
if (evt_name_len == -1) return;

char* evt_name = (char*)malloc(evt_name_len + 1);
if (evt_name == NULL) return;

str_to_camel_case(evt_name, evt_name_len + 1, event + 4);

list_foreach(_drivercfgs, drivercfg) {
maus_bus_driver_event_invoke(drivercfg, evt_name, arg);
}

free(evt_name);
}

void action_manager_event_handler(
const char* event,
EVENT_HANDLER_ARG_TYPE event_arg_ptr,
int event_arg_int,
EVENT_HANDLER_ARG_TYPE handler_arg
) {
action_manager_dispatch_event(event, event_arg_int);
}

void action_manager_init(void) {
event_manager_register_handler(EVT_ALL, action_manager_event_handler, NULL);
}
31 changes: 27 additions & 4 deletions src/system/event_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

static const char* TAG = "system:event_manager";

const char* EVT_ALL = "";
EVENTS(EVENT_DEFINE);

static event_node_t* _handlers = NULL;
Expand Down Expand Up @@ -40,11 +41,22 @@ static event_node_t* _get_handlers(const char* event) {
return node;
}

void event_manager_dispatch(
const char* event, EVENT_HANDLER_ARG_TYPE event_arg_ptr, int event_arg_int
void event_manager_dispatch_synthetic(
const char* match_event,
const char* event,
EVENT_HANDLER_ARG_TYPE event_arg_ptr,
int event_arg_int
) {
ESP_LOGD(TAG, "event_manager_dispatch(\"%s\", %p, %d);", event, event_arg_ptr, event_arg_int);
event_node_t* event_node = _get_handlers(event);
ESP_LOGD(
TAG,
"event_manager_dispatch(\"%s\", \"%s\", %p, %d);",
match_event,
event,
event_arg_ptr,
event_arg_int
);

event_node_t* event_node = _get_handlers(match_event);
if (event_node == NULL) return;

event_handler_node_t* ptr = event_node->handlers;
Expand All @@ -54,6 +66,17 @@ void event_manager_dispatch(
ptr->handler(event, event_arg_ptr, event_arg_int, ptr->handler_arg);
ptr = ptr->next;
}

// Also, dispatch for "All" handlers:
if (match_event != EVT_ALL) {
event_manager_dispatch_synthetic(EVT_ALL, event, event_arg_ptr, event_arg_int);
}
}

void event_manager_dispatch(
const char* event, EVENT_HANDLER_ARG_TYPE event_arg_ptr, int event_arg_int
) {
event_manager_dispatch_synthetic(event, event, event_arg_ptr, event_arg_int);
}

event_handler_node_t* event_manager_register_handler(
Expand Down
32 changes: 32 additions & 0 deletions src/util/strcase.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "util/strcase.h"
#include <ctype.h>
#include <string.h>

size_t str_to_camel_case(char* out, size_t out_len, const char* in) {
if (in == NULL) return -1;

size_t len = strlen(in);
size_t final = 0;
int uc_next = 0;

for (int i = 0; i < len; i++) {
if (out != NULL && i >= out_len) break;
if (in[i] == '_' || in[i] == '-') {
uc_next = 1;
continue;
}

if (out != NULL) {
out[final] = uc_next ? toupper(in[i]) : tolower(in[i]);
}

uc_next = 0;
final++;
}

if (out != NULL) {
out[final] = '\0';
}

return final;
}

0 comments on commit 327528e

Please sign in to comment.