add: more debugging

add: more options for testing
This commit is contained in:
Tobias Reisinger 2020-08-14 23:18:22 +02:00
parent 0efbd2a22f
commit e84d54f562
23 changed files with 130 additions and 63 deletions

2
.gitignore vendored
View file

@ -1,7 +1,7 @@
build/ build/
docs/ docs/
tests/testing_tmp/ tests/testing_latest/
tests/testing_bak/ tests/testing_bak/
include/sql/*.h include/sql/*.h

View file

@ -48,18 +48,28 @@ add_custom_target(debug
DEPENDS core DEPENDS core
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
) )
add_custom_target(debug-full add_custom_target(debug-leak
COMMAND valgrind --leak-check=full --show-leak-kinds=all ./core start COMMAND valgrind --leak-check=full --show-leak-kinds=all ./core start
DEPENDS core DEPENDS core
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
) )
add_custom_target(debug-callgrind
COMMAND valgrind --tool=callgrind ./core start
DEPENDS core
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_custom_target(docs add_custom_target(docs
COMMAND doxygen COMMAND doxygen
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
) )
add_custom_target(test add_custom_target(test
COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core "--leak-check=full --show-leak-kinds=all --track-origins=yes"
DEPENDS core
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
)
add_custom_target(test-callgrind
COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core "--tool=callgrind"
DEPENDS core DEPENDS core
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
) )

View file

@ -4,6 +4,8 @@
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <cJSON.h>
#include <constants.h> #include <constants.h>
#include <helpers.h> #include <helpers.h>
#include <models/relay.h> #include <models/relay.h>
@ -31,7 +33,7 @@ controller_save(controller_t* contoller);
int int
controller_remove(controller_t* contoller); controller_remove(controller_t* contoller);
char* cJSON*
controller_to_json(controller_t* contoller); controller_to_json(controller_t* contoller);
controller_t* controller_t*

View file

@ -4,6 +4,8 @@
#include <string.h> #include <string.h>
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <cJSON.h>
#include <constants.h> #include <constants.h>
#include <helpers.h> #include <helpers.h>
#include <database.h> #include <database.h>
@ -27,10 +29,10 @@ relay_save();
void void
relay_reload_active_schedule(relay_t *relay); relay_reload_active_schedule(relay_t *relay);
char* cJSON*
relay_to_json(relay_t *relay); relay_to_json(relay_t *relay);
char* cJSON*
relay_list_to_json(relay_t **relays); relay_list_to_json(relay_t **relays);
void void

View file

@ -3,6 +3,8 @@
#include <uuid/uuid.h> #include <uuid/uuid.h>
#include <cJSON.h>
#include <constants.h> #include <constants.h>
#include <models/period.h> #include <models/period.h>
@ -30,7 +32,7 @@ schedule_free(schedule_t *schedule);
void void
schedule_free_list(schedule_t **schedule); schedule_free_list(schedule_t **schedule);
char* cJSON*
schedule_to_json(schedule_t *schedule); schedule_to_json(schedule_t *schedule);
void void

View file

@ -17,9 +17,10 @@ api_v1_controllers_GET(struct mg_connection *nc, struct http_message *hm, endpoi
cJSON *json = cJSON_CreateArray(); cJSON *json = cJSON_CreateArray();
LOGGER_DEBUG("filling json array\n");
for(int i = 0; all_controllers[i] != NULL; ++i) for(int i = 0; all_controllers[i] != NULL; ++i)
{ {
cJSON *json_controller = cJSON_CreateRaw(controller_to_json(all_controllers[i])); cJSON *json_controller = controller_to_json(all_controllers[i]);
cJSON_AddItemToArray(json, json_controller); cJSON_AddItemToArray(json, json_controller);
} }

View file

@ -35,8 +35,9 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en
endpoint_response_text(response, 404, content, STRLEN(content)); endpoint_response_text(response, 404, content, STRLEN(content));
return; return;
} }
LOGGER_DEBUG("returning controller for uid '%s'\n", args[0].value.v_str);
cJSON *json = cJSON_CreateRaw(controller_to_json(controller)); cJSON *json = controller_to_json(controller);
endpoint_response_json(response, 200, json); endpoint_response_json(response, 200, json);
cJSON_Delete(json); cJSON_Delete(json);
@ -69,6 +70,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
endpoint_response_text(response, 404, content, STRLEN(content)); endpoint_response_text(response, 404, content, STRLEN(content));
return; return;
} }
LOGGER_DEBUG("starting overwrite for controller %s\n", args[0].value.v_str);
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len); cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
@ -87,6 +89,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
{ {
strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH); strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
controller->name[MAX_NAME_LENGTH] = '\0'; controller->name[MAX_NAME_LENGTH] = '\0';
LOGGER_DEBUG("new controller name: %s\n", controller->name);
} }
else else
{ {
@ -108,6 +111,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
{ {
strncpy(controller->ip, json_ip->valuestring, IP_LENGTH); strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
controller->ip[IP_LENGTH] = '\0'; controller->ip[IP_LENGTH] = '\0';
LOGGER_DEBUG("new controller ip: %s\n", controller->ip);
} }
else else
{ {
@ -138,9 +142,10 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en
endpoint_response_text(response, 500, content, STRLEN(content)); endpoint_response_text(response, 500, content, STRLEN(content));
return; return;
} }
LOGGER_DEBUG("saved controller %s\n", args[0].value.v_str);
cJSON_Delete(json); cJSON_Delete(json);
json = cJSON_CreateRaw(controller_to_json(controller)); json = controller_to_json(controller);
command_set_controller_name(controller); command_set_controller_name(controller);
@ -187,6 +192,7 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm,
} }
else else
{ {
LOGGER_DEBUG("deleted controller %s\n", args[0].value.v_str);
endpoint_response_text(response, 200, "", 0); endpoint_response_text(response, 200, "", 0);
} }
controller_free(controller); controller_free(controller);

View file

@ -37,9 +37,10 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *nc, struct http_message
cJSON *json = cJSON_CreateArray(); cJSON *json = cJSON_CreateArray();
LOGGER_DEBUG("returning all relays for controller %s\n", args[0].value.v_str);
for(int i = 0; all_relays[i] != NULL; ++i) for(int i = 0; all_relays[i] != NULL; ++i)
{ {
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(all_relays[i])); cJSON *json_relay = relay_to_json(all_relays[i]);
cJSON_AddItemToArray(json, json_relay); cJSON_AddItemToArray(json, json_relay);
} }

View file

@ -47,7 +47,8 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess
return; return;
} }
cJSON *json = cJSON_CreateRaw(relay_to_json(relay)); LOGGER_DEBUG("returning relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
cJSON *json = relay_to_json(relay);
endpoint_response_json(response, 200, json); endpoint_response_json(response, 200, json);
cJSON_Delete(json); cJSON_Delete(json);
@ -82,10 +83,20 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
return; return;
} }
if(args[1].value.v_int > controller->relay_count)
{
LOGGER_DEBUG("relay num %d is too high for %s\n", args[1].value.v_int, args[0].value.v_str);
static const char content[] = "relay number is too high for this controller";
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int); relay_t* relay = relay_get_for_controller(controller->id, args[1].value.v_int);
if(!relay) if(!relay)
{ {
LOGGER_DEBUG("relay num %d not found for %s - creating default relay\n", args[1].value.v_int, args[0].value.v_str);
relay = malloc(sizeof(relay_t)); relay = malloc(sizeof(relay_t));
relay->id = 0; relay->id = 0;
relay->number = args[1].value.v_int; relay->number = args[1].value.v_int;
@ -106,6 +117,8 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)]; relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
} }
LOGGER_DEBUG("overwriting relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len); cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
if(json == NULL) if(json == NULL)
@ -120,6 +133,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
{ {
strncpy(relay->name, json_name->valuestring, MAX_NAME_LENGTH); strncpy(relay->name, json_name->valuestring, MAX_NAME_LENGTH);
relay->name[MAX_NAME_LENGTH] = '\0'; relay->name[MAX_NAME_LENGTH] = '\0';
LOGGER_DEBUG("new name: %s\n", relay->name);
} }
@ -154,6 +168,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
schedule_free(relay->schedules[schedule_position]); schedule_free(relay->schedules[schedule_position]);
relay->schedules[schedule_position] = schedule_get_by_uid_or_off(target_uid); relay->schedules[schedule_position] = schedule_get_by_uid_or_off(target_uid);
LOGGER_DEBUG("new schedule[%d]: %s\n", schedule_position, relay->schedules[schedule_position]);
++schedule_position; ++schedule_position;
} }
@ -182,6 +197,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
return; return;
} }
relay->schedules[day_of_week] = schedule_get_by_uid_or_off(target_uid); relay->schedules[day_of_week] = schedule_get_by_uid_or_off(target_uid);
LOGGER_DEBUG("new active schedule: %s\n", relay->schedules[day_of_week]);
} }
} }
@ -200,6 +216,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags"); cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
if(cJSON_IsArray(json_tags)) if(cJSON_IsArray(json_tags))
{ {
LOGGER_DEBUG("cleaning tags");
junction_tag_remove_for_relay(relay->id); junction_tag_remove_for_relay(relay->id);
} }
cJSON_ArrayForEach(json_tag, json_tags) cJSON_ArrayForEach(json_tag, json_tags)
@ -220,8 +237,9 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
} }
cJSON_Delete(json); cJSON_Delete(json);
json = cJSON_CreateRaw(relay_to_json(relay)); json = relay_to_json(relay);
LOGGER_DEBUG("commanding schedules");
command_set_relay_schedule(relay); command_set_relay_schedule(relay);
endpoint_response_json(response, 200, json); endpoint_response_json(response, 200, json);

View file

@ -57,9 +57,11 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht
{ {
duration = json_duration->valueint & 0xFF; duration = json_duration->valueint & 0xFF;
} }
LOGGER_DEBUG("pulsing with custom duration %d\n", duration);
cJSON_Delete(json); cJSON_Delete(json);
} }
LOGGER_DEBUG("commanding pulse to relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
command_pulse(relay, duration); command_pulse(relay, duration);
endpoint_response_text(response, 200, "", 0); endpoint_response_text(response, 200, "", 0);

View file

@ -127,6 +127,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
int16_t payload[1]; int16_t payload[1];
payload[0] = discover_server_port; payload[0] = discover_server_port;
LOGGER_DEBUG("sending udp broadcast\n");
if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0) if(send_udp_broadcast("255.255.255.255", global_config.discovery_port, payload, sizeof(payload)) < 0)
{ {
LOGGER_ERR("failed to send UDP broadcast\n"); LOGGER_ERR("failed to send UDP broadcast\n");
@ -149,7 +150,7 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
controller_t **known_controllers = controller_get_all(); controller_t **known_controllers = controller_get_all();
while(true) for(;;)
{ {
addr_size = sizeof(their_addr); addr_size = sizeof(their_addr);
@ -198,6 +199,8 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
continue; continue;
} }
LOGGER_DEBUG("received info for discovered controller\n");
uuid_t discovered_id; uuid_t discovered_id;
mpack_tree_t tree; mpack_tree_t tree;
@ -225,6 +228,8 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
{ {
if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0) if(uuid_compare(known_controllers[i]->uid, discovered_id) == 0)
{ {
LOGGER_DEBUG("rediscovered a known controller\n");
known_controllers[i]->active = 1; known_controllers[i]->active = 1;
strncpy(known_controllers[i]->name, discovered_name, discovered_name_len); strncpy(known_controllers[i]->name, discovered_name, discovered_name_len);
known_controllers[i]->name[discovered_name_len] = '\0'; known_controllers[i]->name[discovered_name_len] = '\0';
@ -246,6 +251,8 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
if(!found_discovered_in_list) if(!found_discovered_in_list)
{ {
LOGGER_DEBUG("discovered a new controller\n");
controller_t *discovered_controller = malloc(sizeof(controller_t)); controller_t *discovered_controller = malloc(sizeof(controller_t));
discovered_controller->id = 0; discovered_controller->id = 0;
strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr)); strcpy(discovered_controller->ip, inet_ntoa(addr.sin_addr));
@ -299,9 +306,9 @@ api_v1_controllers_discover_POST(struct mg_connection *nc, struct http_message *
} }
for(int i = 0; known_controllers[i] != NULL; i++) for(int i = 0; known_controllers[i] != NULL; i++)
{ {
LOGGER_DEBUG("lost controller %s at %s\n", known_controllers[i]->name, known_controllers[i]->ip);
known_controllers[i]->active = false; known_controllers[i]->active = false;
controller_save(known_controllers[i]); controller_save(known_controllers[i]);
LOGGER_DEBUG("lost: %s\n", known_controllers[i]->name);
} }
controller_free_list(known_controllers); controller_free_list(known_controllers);

View file

@ -20,7 +20,7 @@ api_v1_relays_GET(struct mg_connection *nc, struct http_message *hm, endpoint_ar
for(int i = 0; all_relays[i] != NULL; ++i) for(int i = 0; all_relays[i] != NULL; ++i)
{ {
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(all_relays[i])); cJSON *json_relay = relay_to_json(all_relays[i]);
cJSON_AddItemToArray(json, json_relay); cJSON_AddItemToArray(json, json_relay);
} }

View file

@ -38,9 +38,10 @@ api_v1_relays_tag_STR_GET(struct mg_connection *nc, struct http_message *hm, end
if(!relay) if(!relay)
{ {
LOGGER_DEBUG("failed to get relay %d for tag %s\n", relays_ids[i], args[0].value.v_str);
continue; continue;
} }
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relay)); cJSON *json_relay = relay_to_json(relay);
cJSON_AddItemToArray(json, json_relay); cJSON_AddItemToArray(json, json_relay);

View file

@ -145,7 +145,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
} }
cJSON_Delete(json); cJSON_Delete(json);
json = cJSON_CreateRaw(schedule_to_json(new_schedule)); json = schedule_to_json(new_schedule);
endpoint_response_json(response, 201, json); endpoint_response_json(response, 201, json);
cJSON_Delete(json); cJSON_Delete(json);
@ -165,7 +165,7 @@ api_v1_schedules_GET(struct mg_connection *nc, struct http_message *hm, endpoint
for(int i = 0; all_schedules[i] != NULL; ++i) for(int i = 0; all_schedules[i] != NULL; ++i)
{ {
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(all_schedules[i])); cJSON *json_schedule = schedule_to_json(all_schedules[i]);
cJSON_AddItemToArray(json, json_schedule); cJSON_AddItemToArray(json, json_schedule);
} }

View file

@ -37,7 +37,7 @@ api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endp
return; return;
} }
cJSON *json = cJSON_CreateRaw(schedule_to_json(schedule)); cJSON *json = schedule_to_json(schedule);
endpoint_response_json(response, 200, json); endpoint_response_json(response, 200, json);
cJSON_Delete(json); cJSON_Delete(json);
@ -176,7 +176,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp
} }
cJSON_Delete(json); cJSON_Delete(json);
json = cJSON_CreateRaw(schedule_to_json(schedule)); json = schedule_to_json(schedule);
endpoint_response_json(response, 200, json); endpoint_response_json(response, 200, json);
cJSON_Delete(json); cJSON_Delete(json);

View file

@ -40,7 +40,7 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *nc, struct http_message *hm,
{ {
continue; continue;
} }
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(schedule)); cJSON *json_schedule = schedule_to_json(schedule);
cJSON_AddItemToArray(json, json_schedule); cJSON_AddItemToArray(json, json_schedule);

View file

@ -52,7 +52,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_
{ {
continue; continue;
} }
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relay)); cJSON *json_relay = relay_to_json(relay);
cJSON_AddItemToArray(json_relays, json_relay); cJSON_AddItemToArray(json_relays, json_relay);
@ -66,7 +66,7 @@ api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_
{ {
continue; continue;
} }
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(schedule)); cJSON *json_schedule = schedule_to_json(schedule);
cJSON_AddItemToArray(json_schedules, json_schedule); cJSON_AddItemToArray(json_schedules, json_schedule);

View file

@ -93,7 +93,7 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
LOGGER_DEBUG("failed to normalize uri %.*s\n", hm->uri.len, hm->uri.p); LOGGER_DEBUG("failed to normalize uri %.*s\n", hm->uri.len, hm->uri.p);
return; return;
} }
LOGGER_DEBUG("requested file: %.*s\n", hm->uri.len, hm->uri.p); LOGGER_DEBUG("no endpoint found - serving file\n");
char *request_file_org = malloc(sizeof(char) * hm->uri.len); char *request_file_org = malloc(sizeof(char) * hm->uri.len);
strncpy(request_file_org, hm->uri.p + 1, hm->uri.len); strncpy(request_file_org, hm->uri.p + 1, hm->uri.len);
@ -174,8 +174,9 @@ handler_http(struct mg_connection *nc, int ev, void *p)
{ {
char addr[32]; char addr[32];
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP); mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
LOGGER_DEBUG("new http request from %s\n", addr);
struct http_message *hm = (struct http_message*)p; struct http_message *hm = (struct http_message*)p;
LOGGER_DEBUG("======================================\n");
LOGGER_DEBUG("new http %.*s request from %s for %.*s\n", hm->method.len, hm->method.p, addr, hm->uri.len, hm->uri.p);
handle_http_request(nc, hm); handle_http_request(nc, hm);
} }
} }

View file

@ -187,13 +187,17 @@ controller_free_list(controller_t **controllers)
free(controllers); free(controllers);
} }
char* cJSON*
controller_to_json(controller_t *controller) controller_to_json(controller_t *controller)
{ {
cJSON *json;
char *cached = cache_get_json_controller(controller->id); char *cached = cache_get_json_controller(controller->id);
if(cached) if(cached)
{ {
return cached; json = cJSON_CreateRaw(cached);
free(cached);
return json;
} }
char uuid_str[UUID_STR_LEN]; char uuid_str[UUID_STR_LEN];
@ -201,7 +205,7 @@ controller_to_json(controller_t *controller)
LOGGER_DEBUG("JSONifying controller %s\n", uuid_str); LOGGER_DEBUG("JSONifying controller %s\n", uuid_str);
cJSON *json = cJSON_CreateObject(); json = cJSON_CreateObject();
cJSON *json_name = cJSON_CreateString(controller->name); cJSON *json_name = cJSON_CreateString(controller->name);
if(json_name == NULL) if(json_name == NULL)
@ -255,17 +259,19 @@ controller_to_json(controller_t *controller)
cJSON *json_relays = cJSON_CreateArray(); cJSON *json_relays = cJSON_CreateArray();
for(int i = 0; relays[i] != NULL; ++i) for(int i = 0; relays[i] != NULL; ++i)
{ {
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relays[i])); cJSON *json_relay = relay_to_json(relays[i]);
cJSON_AddItemToArray(json_relays, json_relay); cJSON_AddItemToArray(json_relays, json_relay);
} }
cJSON_AddItemToObject(json, "relays", json_relays); cJSON_AddItemToObject(json, "relays", json_relays);
relay_free_list(relays); relay_free_list(relays);
char *result = cJSON_Print(json); char *json_str = cJSON_Print(json);
cache_put_json_controller(controller->id, json_str);
cJSON_Delete(json); cJSON_Delete(json);
cache_put_json_controller(controller->id, result); json = cJSON_CreateRaw(json_str);
return result; free(json_str);
return json;
} }
controller_t* controller_t*

View file

@ -196,13 +196,17 @@ relay_free_list(relay_t **relays)
free(relays); free(relays);
} }
char* cJSON*
relay_to_json(relay_t *relay) relay_to_json(relay_t *relay)
{ {
cJSON *json;
char *cached = cache_get_json_relay(relay->id); char *cached = cache_get_json_relay(relay->id);
if(cached) if(cached)
{ {
return cached; json = cJSON_CreateRaw(cached);
free(cached);
return json;
} }
controller_t *controller = controller_get_by_id(relay->controller_id); controller_t *controller = controller_get_by_id(relay->controller_id);
@ -218,7 +222,7 @@ relay_to_json(relay_t *relay)
relay_reload_active_schedule(relay); relay_reload_active_schedule(relay);
cJSON *json = cJSON_CreateObject(); json = cJSON_CreateObject();
cJSON *json_number = cJSON_CreateNumber(relay->number); cJSON *json_number = cJSON_CreateNumber(relay->number);
if(json_number == NULL) if(json_number == NULL)
@ -264,13 +268,13 @@ relay_to_json(relay_t *relay)
} }
cJSON_AddItemToObject(json, "is_on", json_is_on); cJSON_AddItemToObject(json, "is_on", json_is_on);
cJSON *json_active_schedule = cJSON_CreateRaw(schedule_to_json(relay->active_schedule)); cJSON *json_active_schedule = schedule_to_json(relay->active_schedule);
cJSON_AddItemToObject(json, "active_schedule", json_active_schedule); cJSON_AddItemToObject(json, "active_schedule", json_active_schedule);
cJSON *json_schedules = cJSON_CreateArray(); cJSON *json_schedules = cJSON_CreateArray();
for(int i = 0; i < 7; ++i) for(int i = 0; i < 7; ++i)
{ {
cJSON *json_schedule = cJSON_CreateRaw(schedule_to_json(relay->schedules[i])); cJSON *json_schedule = schedule_to_json(relay->schedules[i]);
cJSON_AddItemToArray(json_schedules, json_schedule); cJSON_AddItemToArray(json_schedules, json_schedule);
} }
cJSON_AddItemToObject(json, "schedules", json_schedules); cJSON_AddItemToObject(json, "schedules", json_schedules);
@ -301,27 +305,27 @@ relay_to_json(relay_t *relay)
} }
cJSON_AddItemToObject(json, "tags", json_tags); cJSON_AddItemToObject(json, "tags", json_tags);
char *result = cJSON_Print(json); char *json_str = cJSON_Print(json);
cache_put_json_relay(relay->id, json_str);
cJSON_Delete(json); cJSON_Delete(json);
cache_put_json_relay(relay->id, result); json = cJSON_CreateRaw(json_str);
return result; free(json_str);
return json;
} }
char* cJSON*
relay_list_to_json(relay_t **relays) relay_list_to_json(relay_t **relays)
{ {
cJSON *json = cJSON_CreateArray(); cJSON *json = cJSON_CreateArray();
for(int i = 0; relays[i] != NULL; ++i) for(int i = 0; relays[i] != NULL; ++i)
{ {
cJSON *json_relay = cJSON_CreateRaw(relay_to_json(relays[i])); cJSON *json_relay = relay_to_json(relays[i]);
cJSON_AddItemToArray(json, json_relay); cJSON_AddItemToArray(json, json_relay);
} }
char *result = cJSON_Print(json); return json;
cJSON_Delete(json);
return result;
} }
relay_t* relay_t*

View file

@ -219,13 +219,17 @@ schedule_periods_to_blob(schedule_t *schedule)
return blob; return blob;
} }
char* cJSON*
schedule_to_json(schedule_t *schedule) schedule_to_json(schedule_t *schedule)
{ {
cJSON *json;
char *cached = cache_get_json_schedule(schedule->id); char *cached = cache_get_json_schedule(schedule->id);
if(cached) if(cached)
{ {
return cached; json = cJSON_CreateRaw(cached);
free(cached);
return json;
} }
char uuid_str[UUID_STR_LEN]; char uuid_str[UUID_STR_LEN];
@ -233,7 +237,7 @@ schedule_to_json(schedule_t *schedule)
LOGGER_DEBUG("JSONifying schedule %s\n", uuid_str); LOGGER_DEBUG("JSONifying schedule %s\n", uuid_str);
cJSON *json = cJSON_CreateObject(); json = cJSON_CreateObject();
cJSON *json_name = cJSON_CreateString(schedule->name); cJSON *json_name = cJSON_CreateString(schedule->name);
if(json_name == NULL) if(json_name == NULL)
@ -319,11 +323,13 @@ schedule_to_json(schedule_t *schedule)
} }
cJSON_AddItemToObject(json, "tags", json_tags); cJSON_AddItemToObject(json, "tags", json_tags);
char *result = cJSON_Print(json); char *json_str = cJSON_Print(json);
cache_put_json_schedule(schedule->id, json_str);
cJSON_Delete(json); cJSON_Delete(json);
cache_put_json_schedule(schedule->id, result); json = cJSON_CreateRaw(json_str);
return result; free(json_str);
return json;
} }
schedule_t* schedule_t*

View file

@ -1,5 +1,6 @@
#include <cJSON.h> #include <cJSON.h>
#include <cache.h>
#include <status.h> #include <status.h>
#include <logger.h> #include <logger.h>
@ -34,6 +35,7 @@ status_reload_entry(int relay_id)
relays[i]->is_on = is_on_backup; relays[i]->is_on = is_on_backup;
relay_status_list_json_str = NULL; relay_status_list_json_str = NULL;
relay_status_list_json_str_len = 0; relay_status_list_json_str_len = 0;
cache_invalidate_relay(relay_id);
} }
} }
@ -67,7 +69,7 @@ validate_json_str_cache()
{ {
relay_t **relays = global_relay_status_list; relay_t **relays = global_relay_status_list;
cJSON *json = cJSON_CreateRaw(relay_list_to_json(relays)); cJSON *json = relay_list_to_json(relays);
char *json_str = cJSON_Print(json); char *json_str = cJSON_Print(json);
size_t json_str_len = strlen(json_str); size_t json_str_len = strlen(json_str);

View file

@ -1,15 +1,15 @@
#!/usr/bin/env sh #!/usr/bin/env sh
source_dir=$PWD source_dir=$PWD
working_dir=$PWD/testing_tmp working_dir=$PWD/testing_latest
working_bak=$PWD/testing_bak working_bak=$PWD/testing_bak
alias valgrind_emgauwa="valgrind --leak-check=full \ alias valgrind_emgauwa="valgrind $2 --log-file=$working_dir/valgrind.log"
--show-leak-kinds=all \
--track-origins=yes \
--log-file=$working_dir/valgrind.log" rm -rf $working_bak
mv $working_dir $working_bak
rm -rf $working_dir
mkdir -p $working_dir mkdir -p $working_dir
cd $working_dir cd $working_dir
@ -62,8 +62,4 @@ test_result=$?
kill $core_id kill $core_id
kill $controller_id kill $controller_id
rm -rf $working_bak
mv $working_dir $working_bak
rm -rf $working_dir
exit $test_result exit $test_result