Skip to content

Commit 8ed09c2

Browse files
committed
Changed event system to work without signals
Fixed some more memory leaks Better function arguments passing
1 parent 9849926 commit 8ed09c2

17 files changed

+189
-94
lines changed

BuiltinServices/Expression.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ variant_t* process_template_impl(struct service_method_t* method, va_list args)
4242

4343
if(NULL != service_stack)
4444
{
45-
variant_t* token_table_var = stack_pop_front(service_stack->stack);
46-
hash_table_t* token_table = (hash_table_t*)variant_get_ptr(token_table_var);
45+
//variant_t* token_table_var = stack_pop_front(service_stack->stack);
46+
hash_table_t* token_table = (hash_table_t*)service_stack->data_storage;//variant_get_ptr(token_table_var);
4747

4848
// Replace all tokens starting with "$" with matching values from token table
4949
char* template = (char*)variant_get_string(template_var);
@@ -111,7 +111,7 @@ variant_t* process_template_impl(struct service_method_t* method, va_list args)
111111
++tokens;
112112
}
113113

114-
variant_free(token_table_var);
114+
//variant_free(token_table_var);
115115
}
116116

117117
// Remove last space...

Makefile

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ COMMON_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
3030
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
3131
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
3232
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
33-
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
34-
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
35-
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
36-
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
33+
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
34+
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
35+
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
36+
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
3737
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
3838
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
3939
$(OUTDIR)/zwave-automation-engine.o
@@ -45,10 +45,10 @@ ALL_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
4545
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
4646
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
4747
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
48-
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
49-
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
50-
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
51-
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
48+
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
49+
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
50+
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
51+
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
5252
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
5353
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
5454
$(OUTDIR)/zwave-automation-engine.o -l:libreadline.a -l:libncurses.a \
@@ -107,10 +107,10 @@ COMMON_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
107107
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
108108
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
109109
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
110-
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
111-
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
112-
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
113-
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
110+
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
111+
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
112+
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
113+
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
114114
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
115115
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
116116
$(OUTDIR)/zwave-automation-engine.o
@@ -122,10 +122,10 @@ ALL_OBJ=$(OUTDIR)/Conditional.o $(OUTDIR)/Expression.o \
122122
$(OUTDIR)/cli_sensor.o $(OUTDIR)/cli_service.o \
123123
$(OUTDIR)/command_class.o $(OUTDIR)/command_parser.o \
124124
$(OUTDIR)/config.o $(OUTDIR)/data_callbacks.o \
125-
$(OUTDIR)/device_callbacks.o $(OUTDIR)/event_manager.o \
126-
$(OUTDIR)/http_server.o $(OUTDIR)/logging_modules.o \
127-
$(OUTDIR)/operator.o $(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o \
128-
$(OUTDIR)/scene.o $(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
125+
$(OUTDIR)/device_callbacks.o $(OUTDIR)/http_server.o \
126+
$(OUTDIR)/logging_modules.o $(OUTDIR)/operator.o \
127+
$(OUTDIR)/parser_dfa.o $(OUTDIR)/resolver.o $(OUTDIR)/scene.o \
128+
$(OUTDIR)/scene_action.o $(OUTDIR)/scene_manager.o \
129129
$(OUTDIR)/script_action_handler.o $(OUTDIR)/service_manager.o \
130130
$(OUTDIR)/user_manager.o $(OUTDIR)/vty_io.o \
131131
$(OUTDIR)/zwave-automation-engine.o -l:libreadline.a -l:libncurses.a \

builtin_service.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
DECLARE_LOGGER(BuiltinService)
66

7+
static hash_table_t* service_stack_table;
8+
79
builtin_service_t* builtin_service_create(hash_table_t* service_table, const char* name, const char* description)
810
{
911
builtin_service_t* new_service = calloc(1, sizeof(builtin_service_t));
@@ -34,33 +36,40 @@ void service_stack_delete(void* arg)
3436
service_stack_t* s = (service_stack_t*)arg;
3537
free(s->service_name);
3638
stack_free(s->stack);
39+
variant_hash_free(s->data_storage);
3740
free(s);
3841
}
3942

4043
void builtin_service_stack_create(const char* service_name)
4144
{
45+
uint32_t key = crc32(0, service_name, strlen(service_name));
46+
4247
if(NULL == service_stack_table)
4348
{
4449
service_stack_table = variant_hash_init();
4550
}
51+
52+
if(NULL != variant_hash_get(service_stack_table, key))
53+
{
54+
return;
55+
}
4656

4757
service_stack_t* s = malloc(sizeof(service_stack_t));
4858
s->service_name = strdup(service_name);
4959
s->stack = stack_create();
50-
51-
uint32_t key = crc32(0, service_name, strlen(service_name));
60+
s->data_storage = variant_hash_init();
5261
variant_hash_insert(service_stack_table, key, variant_create_ptr(DT_PTR, s, service_stack_delete));
5362
}
5463

55-
void builtin_service_stack_add(const char* service_name, variant_t* data)
64+
void builtin_service_stack_add(const char* service_name, uint32_t value_key, variant_t* value)
5665
{
5766
uint32_t key = crc32(0, service_name, strlen(service_name));
5867
variant_t* stack_var = variant_hash_get(service_stack_table, key);
5968

6069
if(NULL != stack_var)
6170
{
6271
service_stack_t* s = (service_stack_t*)variant_get_ptr(stack_var);
63-
stack_push_front(s->stack, data);
72+
variant_hash_insert(s->data_storage, value_key, value);
6473
}
6574
}
6675

@@ -77,9 +86,12 @@ service_stack_t* builtin_service_stack_get(const char* service_name)
7786
return NULL;
7887
}
7988

80-
void builtin_service_stack_clear(const char* service_name)
89+
void builtin_service_stack_clear()
8190
{
82-
uint32_t key = crc32(0, service_name, strlen(service_name));
83-
variant_hash_remove(service_stack_table, key);
91+
if(NULL != service_stack_table)
92+
{
93+
variant_hash_free(service_stack_table);
94+
service_stack_table = NULL;
95+
}
8496
}
8597

builtin_service.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ typedef struct service_stack_t
1919
{
2020
char* service_name;
2121
variant_stack_t* stack;
22+
hash_table_t* data_storage;
2223
} service_stack_t;
2324

24-
static hash_table_t* service_stack_table;
25-
2625
builtin_service_t* builtin_service_create(hash_table_t* service_table, const char* name, const char* description);
2726
void builtin_service_add_method(builtin_service_t* service, const char* name, const char* help, int args, variant_t* (*eval_callback)(struct service_method_t*, va_list));
2827
void builtin_service_stack_create(const char* service_name);
29-
void builtin_service_stack_add(const char* service_name, variant_t* data);
28+
void builtin_service_stack_add(const char* service_name, uint32_t value_key, variant_t* value);
3029
service_stack_t* builtin_service_stack_get(const char* service_name);
31-
void builtin_service_stack_clear(const char* service_name);
30+
void builtin_service_stack_clear();
3231

cli_scene.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool cmd_scene_disable(vty_t* vty, variant_stack_t* params);
4949
void show_scene_helper(scene_t* scene, void* arg);
5050
void show_scene_action_helper(action_t* action, void* arg);
5151
void show_scene_action_env_helper(env_t* env, void* arg);
52-
void show_scene_action_token_helper(env_t* env, void* arg);
52+
void show_scene_action_token_helper(method_stack_item_t* env, void* arg);
5353

5454
USING_LOGGER(Scene);
5555

@@ -91,8 +91,8 @@ cli_command_t scene_action_command_list[] = {
9191
};
9292

9393
cli_command_t scene_template_command_list[] = {
94-
{"template-token WORD value LINE", cmd_add_action_token, "Add token value to command action"},
95-
{"no template-token WORD", cmd_del_action_token, "Remove token value from command action"},
94+
{"argument class WORD name WORD value LINE", cmd_add_action_token, "Add argument to command action"},
95+
{"no argument class WORD name WORD", cmd_del_action_token, "Remove argument from command action"},
9696
//{"end", cmd_exit_node, "Exit environment configuration"},
9797
{NULL, NULL, NULL}
9898
};
@@ -230,11 +230,11 @@ bool cmd_add_action_environment(vty_t* vty, variant_stack_t* params)
230230
bool cmd_add_action_token(vty_t* vty, variant_stack_t* params)
231231
{
232232
char command[1024] = {0};
233-
cli_assemble_line(params, 3, command);
233+
cli_assemble_line(params, 6, command);
234234
scene_t* scene = scene_manager_get_scene(scene_node->context);
235235
action_t* action = scene_get_action(scene, scene_template_token_node->context);
236-
scene_action_add_environment(action,
237-
variant_get_string(stack_peek_at(params, 1)),
236+
scene_action_add_method_stack_item(action, variant_get_string(stack_peek_at(params, 2)),
237+
variant_get_string(stack_peek_at(params, 4)),
238238
command);
239239
}
240240

@@ -243,7 +243,7 @@ bool cmd_del_action_token(vty_t* vty, variant_stack_t* params)
243243
scene_t* scene = scene_manager_get_scene(scene_node->context);
244244
action_t* action = scene_get_action(scene, scene_template_token_node->context);
245245

246-
scene_action_del_environment(action, variant_get_string(stack_peek_at(params, 2)));
246+
scene_action_del_method_stack_item(action, variant_get_string(stack_peek_at(params, 3)), variant_get_string(stack_peek_at(params, 5)));
247247
}
248248

249249
bool cmd_delete_scene_action_script(vty_t* vty, variant_stack_t* params)
@@ -409,7 +409,7 @@ void show_scene_action_helper(action_t* action, void* arg)
409409
break;
410410
case A_COMMAND:
411411
vty_write(vty, " action command %s\n", action->path);
412-
scene_action_for_each_environment(action, show_scene_action_token_helper, vty);
412+
scene_action_for_each_method_stack_item(action, show_scene_action_token_helper, vty);
413413
vty_write(vty, " !\n");
414414
break;
415415
case A_SCENE:
@@ -435,8 +435,8 @@ void show_scene_action_env_helper(env_t* env, void* arg)
435435
vty_write(vty, " environment %s value %s\n", env->name, env->value);
436436
}
437437

438-
void show_scene_action_token_helper(env_t* env, void* arg)
438+
void show_scene_action_token_helper(method_stack_item_t* env, void* arg)
439439
{
440440
vty_t* vty = (vty_t*)arg;
441-
vty_write(vty, " template-token %s value %s\n", env->name, env->value);
441+
vty_write(vty, " argument class %s name %s value %s\n", env->stack_name, env->name, env->value);
442442
}

event_manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void event_received(int sig);
2020

2121
//pthread_cond_t event_received_condition;
2222
//pthread_mutex_t event_received_cond_mutex;
23-
sem_t event_semaphore;
23+
extern sem_t event_semaphore;
2424

2525
DECLARE_LOGGER(Event)
2626

libZaeUtil/event.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,38 @@
22
#include <stdlib.h>
33
#include <signal.h>
44
#include <pthread.h>
5+
#include <semaphore.h>
6+
#include "logger.h"
57

68
variant_stack_t* event_list;
9+
variant_stack_t* registered_handlers;
10+
711
pthread_mutex_t event_list_mutex;
12+
sem_t event_semaphore;
813

914
void event_destroy(void* arg);
1015

16+
DECLARE_LOGGER(Event)
17+
18+
void* event_handle_event(void* arg);
19+
20+
void event_manager_init()
21+
{
22+
LOG_ADVANCED(Event, "Initializing event manager");
23+
event_list = stack_create();
24+
registered_handlers = stack_create();
25+
26+
// Init the semaphore
27+
sem_init(&event_semaphore, 0, 0);
28+
29+
// Now create event thread
30+
pthread_t event_thread;
31+
pthread_create(&event_thread, NULL, event_handle_event, NULL);
32+
pthread_detach(event_thread);
33+
34+
LOG_ADVANCED(Event, "Event manager initialized");
35+
}
36+
1137
event_t* event_create(int source_id, variant_t* data)
1238
{
1339
event_t* new_event = (event_t*)malloc(sizeof(event_t));
@@ -28,7 +54,8 @@ void event_post(event_t* event)
2854
pthread_mutex_lock(&event_list_mutex);
2955
stack_push_back(event_list, variant_create_ptr(DT_PTR, event, NULL));
3056
pthread_mutex_unlock(&event_list_mutex);
31-
raise(SIGUSR1);
57+
sem_post(&event_semaphore);
58+
//raise(SIGUSR1);
3259
}
3360

3461
event_t* event_receive()
@@ -40,3 +67,28 @@ event_t* event_receive()
4067
free(event_variant);
4168
return event;
4269
}
70+
71+
void event_register_handler(void (*event_handler)(event_t*))
72+
{
73+
stack_push_back(registered_handlers, variant_create_ptr(DT_PTR, event_handler, NULL));
74+
}
75+
76+
void* event_handle_event(void* arg)
77+
{
78+
while(true)
79+
{
80+
sem_wait(&event_semaphore);
81+
LOG_DEBUG(Event, "Event received");
82+
event_t* event = event_receive();
83+
LOG_ADVANCED(Event, "Received event id %d", event->source_id);
84+
85+
stack_for_each(registered_handlers, handler_var)
86+
{
87+
void (*event_handler)(event_t*);
88+
89+
event_handler = (void(*)(event_t*))variant_get_ptr(handler_var);
90+
event_handler(event);
91+
}
92+
event_delete(event);
93+
}
94+
}

libZaeUtil/event.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ typedef struct service_event_data_t
2727
char* data;
2828
} service_event_data_t;
2929

30+
void event_manager_init();
31+
void event_register_handler(void (*event_handler)(event_t*));
3032
event_t* event_create(int source_id, variant_t* data);
3133
void event_delete(event_t* event);
3234
void event_post(event_t* event);

libZaeUtil/hash.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,9 @@ variant_t* variant_hash_iterator_value(hash_iterator_t* it)
419419
{
420420
return it->hash_table->node_array[it->index]->data;
421421
}
422+
423+
uint32_t variant_hash_iterator_key(hash_iterator_t* it)
424+
{
425+
return it->hash_table->node_array[it->index]->key;
426+
}
427+

libZaeUtil/hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ hash_iterator_t* variant_hash_end(hash_table_t* hash_table);
5757
bool variant_hash_iterator_is_end(hash_iterator_t* it);
5858
hash_iterator_t* variant_hash_iterator_next(hash_iterator_t* it);
5959
variant_t* variant_hash_iterator_value(hash_iterator_t* it);
60+
uint32_t variant_hash_iterator_key(hash_iterator_t* it);
61+

0 commit comments

Comments
 (0)