Skip to content

Commit

Permalink
Merge pull request #88 from MausTec/mt-actions
Browse files Browse the repository at this point in the history
mt actions
  • Loading branch information
MauAbata authored Apr 2, 2024
2 parents 0d9f171 + 2f6dc08 commit d6f1635
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "lib/tlibmp"]
path = lib/tlibmp
url = https://github.com/MauAbata/tlibmp
[submodule "lib/mt-actions"]
path = lib/mt-actions
url = https://github.com/MausTec/mt-actions
32 changes: 25 additions & 7 deletions examples/drivercfg/mb-gp8.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,54 @@
"gpio": {
"0": {
"mode": "OUTPUT",
"level": "LOW"
"level": 0
}
}
},
"functions": {
"@high": {
"setPin": {
"gpio.setPin": {
"pin": 0,
"level": "HIGH"
"level": 1
}
},
"@low": {
"setPin": {
"gpio.setPin": {
"pin": 0,
"level": "LOW"
"level": 0
}
},
"@pulse": [
{
"if": {
"all": {
"eq": [
"$ms",
0
]
},
"then": [
"return"
],
"else": []
}
},
"@high",
{
"delay": {
"ms": "1000"
"ms": "$ms"
}
},
"@low"
]
},
"events": {
"orgasmDenial": [
"@pulse"
{
"@pulse": {
"ms": 1000
}
}
]
}
}
18 changes: 18 additions & 0 deletions include/actions/index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __actions__index_h
#define __actions__index_h

#ifdef __cplusplus
extern "C" {
#endif

void actions_register_system(void);

static inline void actions_register_all(void) {
actions_register_system();
}

#ifdef __cplusplus
}
#endif

#endif
16 changes: 16 additions & 0 deletions include/actions/system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __actions__system_h
#define __actions__system_h

#ifdef __cplusplus
extern "C" {
#endif

#include "mt_actions.h"

int action_system_delay(mta_plugin_t* plugin, cJSON* args);

#ifdef __cplusplus
}
#endif

#endif
4 changes: 2 additions & 2 deletions include/system/action_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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_load_plugin(const char* filename);
void action_manager_load_all_plugins(void);
void action_manager_dispatch_event(const char* event, int val);

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion lib/mt-accessory-common
1 change: 1 addition & 0 deletions lib/mt-actions
Submodule mt-actions added at 7a1a29
11 changes: 7 additions & 4 deletions sdkconfig.esp32dev
Original file line number Diff line number Diff line change
Expand Up @@ -821,12 +821,15 @@ CONFIG_HAL_DEFAULT_ASSERTION_LEVEL=2
#
# Heap memory debugging
#
CONFIG_HEAP_POISONING_DISABLED=y
# CONFIG_HEAP_POISONING_LIGHT is not set
# CONFIG_HEAP_POISONING_DISABLED is not set
CONFIG_HEAP_POISONING_LIGHT=y
# CONFIG_HEAP_POISONING_COMPREHENSIVE is not set
CONFIG_HEAP_TRACING_OFF=y
# CONFIG_HEAP_TRACING_STANDALONE is not set
# CONFIG_HEAP_TRACING_OFF is not set
CONFIG_HEAP_TRACING_STANDALONE=y
# CONFIG_HEAP_TRACING_TOHOST is not set
CONFIG_HEAP_TRACING=y
CONFIG_HEAP_TRACING_STACK_DEPTH=2
# CONFIG_HEAP_TASK_TRACKING is not set
# CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS is not set
# end of Heap memory debugging

Expand Down
9 changes: 5 additions & 4 deletions sdkconfig.esp32dev.old
Original file line number Diff line number Diff line change
Expand Up @@ -848,13 +848,14 @@ CONFIG_HEAP_TRACING_OFF=y
# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set
# CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set
# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set
# CONFIG_LOG_DEFAULT_LEVEL_INFO is not set
CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y
CONFIG_LOG_DEFAULT_LEVEL_INFO=y
# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set
# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_LOG_DEFAULT_LEVEL=3
CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y
# CONFIG_LOG_MAXIMUM_LEVEL_DEBUG is not set
# CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set
CONFIG_LOG_MAXIMUM_LEVEL=4
CONFIG_LOG_MAXIMUM_LEVEL=3
CONFIG_LOG_COLORS=y
# CONFIG_LOG_TIMESTAMP_SOURCE_RTOS is not set
CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM=y
Expand Down
26 changes: 26 additions & 0 deletions src/actions/system.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "actions/system.h"
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

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

int action_system_delay(struct mta_plugin* plugin, cJSON* args) {
char* arg = cJSON_Print(args);
ESP_LOGI(TAG, "fn args: %s", arg);
free(arg);

cJSON* ms_json = cJSON_GetObjectItem(args, "ms");
if (ms_json == NULL) return -1;
int ms = ms_json->valueint;

ESP_LOGI(TAG, "delay(%d) start", ms);
vTaskDelay(ms / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "delay done");

return 0;
}

void actions_register_system(void) {
mta_register_system_function("delay", action_system_delay);
}
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void app_main() {
}

// Initialize Action Manager
action_manager_load_all_drivercfg();
action_manager_load_all_plugins();

// Final delay on encoder colors.
vTaskDelayUntil(&boot_tick, 1000UL / portTICK_PERIOD_MS);
Expand Down
39 changes: 21 additions & 18 deletions src/system/action_manager.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "system/action_manager.h"
#include "actions/index.h"
#include "cJSON.h"
#include "eom-hal.h"
#include "maus_bus_drivercfg.h"
#include "mt_actions.h"
#include "system/event_manager.h"
#include "util/list.h"
#include "util/strcase.h"
Expand All @@ -12,15 +13,16 @@

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

static list_t _drivercfgs = LIST_DEFAULT();
static list_t _plugins = LIST_DEFAULT();

#define PLUGIN_DIR "plugins"
#define DRIVERCFG_DIR "drivercfg"

void action_manager_load_all_drivercfg(void) {
void action_manager_load_all_plugins(void) {
DIR* d;
struct dirent* dir;
char* path = NULL;
asiprintf(&path, "%s/%s", eom_hal_get_sd_mount_point(), DRIVERCFG_DIR);
asiprintf(&path, "%s/%s", eom_hal_get_sd_mount_point(), PLUGIN_DIR);
if (path == NULL) return;

d = opendir(path);
Expand All @@ -33,7 +35,7 @@ void action_manager_load_all_drivercfg(void) {
asiprintf(&file, "%s/%s", path, dir->d_name);
if (file == NULL) break;

action_manager_load_drivercfg(file);
action_manager_load_plugin(file);
free(file);
}
}
Expand All @@ -44,12 +46,12 @@ void action_manager_load_all_drivercfg(void) {
free(path);
}

void action_manager_load_drivercfg(const char* path) {
ESP_LOGI(TAG, "Loading drivercfg: %s", path);
void action_manager_load_plugin(const char* path) {
ESP_LOGI(TAG, "Loading plugin: %s", path);
long fsize;
size_t result;
char* buffer = NULL;
cJSON* drivercfg_json = NULL;
cJSON* plugin_json = NULL;
FILE* f = fopen(path, "r");

if (!f) {
Expand All @@ -72,29 +74,29 @@ void action_manager_load_drivercfg(const char* path) {
goto cleanup;
}

drivercfg_json = cJSON_ParseWithLength(buffer, fsize);
plugin_json = cJSON_ParseWithLength(buffer, fsize);

if (drivercfg_json == NULL) {
if (plugin_json == NULL) {
goto cleanup;
}

maus_bus_drivercfg_t* drivercfg = NULL;
maus_bus_driver_load(&drivercfg, drivercfg_json);
mta_plugin_t* plugin = NULL;
mta_load(&plugin, plugin_json);

if (drivercfg == NULL) {
if (plugin == NULL) {
goto cleanup;
}

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

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

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

const char* evt_strip = strstr("EVT_", event);
if (strncmp("EVT_", event, 4)) return;
Expand All @@ -107,8 +109,8 @@ void action_manager_dispatch_event(const char* event, int arg) {

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);
list_foreach(_plugins, plugin) {
mta_event_invoke(plugin, evt_name, arg);
}

free(evt_name);
Expand All @@ -124,5 +126,6 @@ void action_manager_event_handler(
}

void action_manager_init(void) {
actions_register_all();
event_manager_register_handler(EVT_ALL, action_manager_event_handler, NULL);
}
27 changes: 15 additions & 12 deletions src/system/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
static const char* TAG = "http_server";
static httpd_handle_t _server = NULL;

static const httpd_uri_t routes[] = {{
static const httpd_uri_t routes[] = { {
.uri = "/",
.method = HTTP_GET,
.handler = &websocket_handler,
.user_ctx = NULL,
.is_websocket = true,
.handle_ws_control_frames = true,
}};
} };

static void init_routes(httpd_handle_t server) {
for (size_t i = 0; i < sizeof(routes) / sizeof(routes[0]); i++) {
Expand Down Expand Up @@ -50,9 +50,9 @@ static httpd_handle_t start_webserver(void) {
}

if (Config.use_ssl) {
// this will be enabled soon, though right now the server start handles ~only~ the HTTP server
// and expects that as a return, so there's no easy way to also configure SSL. Also, the certs
// should be loaded off SD card, not ASM.
// this will be enabled soon, though right now the server start handles ~only~ the HTTP
// server and expects that as a return, so there's no easy way to also configure SSL. Also,
// the certs should be loaded off SD card, not ASM.

// httpd_ssl_config_t ssl_config = HTTPD_SSL_CONFIG_DEFAULT();
// ssl_config.httpd = config;
Expand Down Expand Up @@ -80,19 +80,21 @@ static httpd_handle_t start_webserver(void) {
return server;
}

static void stop_webserver(httpd_handle_t server) { httpd_stop(server); }
static void stop_webserver(httpd_handle_t server) {
httpd_stop(server);
}

static void connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id,
void* event_data) {
static void
connect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
httpd_handle_t* server = (httpd_handle_t*)arg;
if (*server == NULL) {
ESP_LOGI(TAG, "Starting webserver...");
*server = start_webserver();
}
}

static void disconnect_handler(void* arg, esp_event_base_t event_base, int32_t event_id,
void* event_data) {
static void
disconnect_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
httpd_handle_t* server = (httpd_handle_t*)arg;
if (*server) {
ESP_LOGI(TAG, "Stopping webserver...");
Expand All @@ -103,8 +105,9 @@ static void disconnect_handler(void* arg, esp_event_base_t event_base, int32_t e

esp_err_t http_server_init(void) {
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &_server);
esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler,
&_server);
esp_event_handler_register(
WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &_server
);

api_register_all();
return ESP_OK;
Expand Down

0 comments on commit d6f1635

Please sign in to comment.