diff --git a/.clang-tidy b/.clang-tidy index 19745c8..154372e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,2 +1,2 @@ --- -Checks: 'clang-diagnostics-*,clang-analyzer-*,linuxkernel-*,modernize-*' +Checks: 'clang-diagnostics-*,clang-analyzer-*,linuxkernel-*,modernize-*,readability-*' diff --git a/include/endpoint.h b/include/endpoint.h index 6447487..ea4ba66 100644 --- a/include/endpoint.h +++ b/include/endpoint.h @@ -53,6 +53,9 @@ endpoint_func_index(struct mg_connection *nc, struct http_message *hm, endpoint_ void endpoint_func_not_found(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response); +void +endpoint_response_msg(endpoint_response_t *response, int status_code, const char *content, int content_length); + void endpoint_response_text(endpoint_response_t *response, int status_code, const char *content, int content_length); diff --git a/include/macros.h b/include/macros.h index 062081a..1781aad 100644 --- a/include/macros.h +++ b/include/macros.h @@ -5,20 +5,26 @@ #define M_STRLEN(s) ((sizeof(s)/sizeof(s[0])) - sizeof(s[0])) -#define M_RESPONSE_TEXT_STATIC(logger_func, response, code, content) do { logger_func("%s\n", content); endpoint_response_text(response, code, content, M_STRLEN(content)); } while(0) -#define M_RESPONSE_400_NO_VALID_JSON(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request is not valid json") -#define M_RESPONSE_400_NO_VALID_ID(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contain valid id (uuid)") -#define M_RESPONSE_400_NO_NAME(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contain a name") +#define M_RESPONSE_MSG(logger_func, response, code, content) do { logger_func("%s\n", content); endpoint_response_msg(response, code, content, M_STRLEN(content)); } while(0) +#define M_RESPONSE_TEXT(logger_func, response, code, content) do { logger_func("%s\n", content); endpoint_response_text(response, code, content, M_STRLEN(content)); } while(0) -#define M_RESPONSE_403_PROTECTED_SCHEDULE(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 403, "the target schedule is protected") +#define M_RESPONSE_400_NO_VALID_JSON(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request is not valid json") +#define M_RESPONSE_400_NO_VALID_ID(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain a valid id (uuid)") +#define M_RESPONSE_400_NO_NAME(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain a name") +#define M_RESPONSE_400_NO_VALID_NAME(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain a valid name") +#define M_RESPONSE_400_NO_IP(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain an ip address") +#define M_RESPONSE_400_NO_VALID_IP(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain a valid ip address") -#define M_RESPONSE_404_NO_CONTROLLER_FOUND_FOR_ID(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no controller was found for the requested id") -#define M_RESPONSE_404_NO_SCHEDULE_FOUND_FOR_ID(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no schedule was found for the requested id") -#define M_RESPONSE_404_NO_MACRO_FOUND_FOR_ID(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no schedule was found for the requested id") -#define M_RESPONSE_404_NO_TAG_FOUND(response) M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "the requested tag was not found") +#define M_RESPONSE_403_PROTECTED_SCHEDULE(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 403, "the target schedule is protected") -#define M_RESPONSE_500_FAILED_TO_SAVE_TO_DATABASE(response) M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "the server failed to save to the database") -#define M_RESPONSE_500_FAILED_TO_READ_FROM_DATABASE(response) M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "the server failed to read from the database") -#define M_RESPONSE_500_FAILED_TO_DELETE_FROM_DATABASE(response) M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "the server failed to delete from the database") +#define M_RESPONSE_404_NO_CONTROLLER_FOUND_FOR_ID(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "no controller was found for the requested id") +#define M_RESPONSE_404_NO_RELAY_FOUND_FOR_ID_AND_NUMBER(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "no relay was found for the requested controller id and relay number") +#define M_RESPONSE_404_NO_SCHEDULE_FOUND_FOR_ID(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "no schedule was found for the requested id") +#define M_RESPONSE_404_NO_MACRO_FOUND_FOR_ID(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "no schedule was found for the requested id") +#define M_RESPONSE_404_NO_TAG_FOUND(response) M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "the requested tag was not found") + +#define M_RESPONSE_500_FAILED_TO_SAVE_TO_DATABASE(response) M_RESPONSE_MSG(LOGGER_ERR, response, 500, "the server failed to save to the database") +#define M_RESPONSE_500_FAILED_TO_READ_FROM_DATABASE(response) M_RESPONSE_MSG(LOGGER_ERR, response, 500, "the server failed to read from the database") +#define M_RESPONSE_500_FAILED_TO_DELETE_FROM_DATABASE(response) M_RESPONSE_MSG(LOGGER_ERR, response, 500, "the server failed to delete from the database") #endif /* CORE_MACROS_H */ diff --git a/src/command.c b/src/command.c index 6856afa..8fb0ac4 100644 --- a/src/command.c +++ b/src/command.c @@ -211,8 +211,6 @@ command_controller_name_set(controller_t *controller) int command_send(controller_t *controller, char *payload, uint32_t payload_size) { - int bytes_transferred; - int fd_controller = helper_connect_tcp_server(controller->ip, controller->port); if(fd_controller == -1) diff --git a/src/endpoint.c b/src/endpoint.c index 3902e33..4d0ad01 100644 --- a/src/endpoint.c +++ b/src/endpoint.c @@ -42,6 +42,32 @@ endpoint_func_not_found(struct mg_connection *nc, struct http_message *hm, endpo } +void +endpoint_response_msg(endpoint_response_t *response, int status_code, const char *content, int content_length) +{ + cJSON *json; + + json = cJSON_CreateObject(); + cJSON *json_msg; + + if(content_length) + { + json_msg = cJSON_CreateStringReference(content); + } + else + { + json_msg = cJSON_CreateString(content); + } + if(json_msg == NULL) + { + endpoint_response_text(response, status_code, content, content_length); + return; + } + cJSON_AddItemToObject(json, "msg", json_msg); + + endpoint_response_json(response, status_code, json); +} + void endpoint_response_text(endpoint_response_t *response, int status_code, const char *content, int content_length) { @@ -84,5 +110,5 @@ endpoint_response_json(endpoint_response_t *response, int status_code, const cJS } } - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "failed to print json"); + M_RESPONSE_MSG(LOGGER_ERR, response, 500, "failed to print json"); } diff --git a/src/endpoints/api_v1_controllers_STR.c b/src/endpoints/api_v1_controllers_STR.c index 0a6c0ba..25d375f 100644 --- a/src/endpoints/api_v1_controllers_STR.c +++ b/src/endpoints/api_v1_controllers_STR.c @@ -26,7 +26,7 @@ api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, en if(!controller) { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no controller found for id"); + M_RESPONSE_404_NO_CONTROLLER_FOUND_FOR_ID(response); return; } LOGGER_DEBUG("returning controller for uid '%s'\n", args[0].value.v_str); @@ -78,7 +78,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en cJSON_Delete(json); controller_free(controller); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the given name is invalid"); + M_RESPONSE_400_NO_VALID_NAME(response); return; } @@ -95,7 +95,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en cJSON_Delete(json); controller_free(controller); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "no ip address given"); + M_RESPONSE_400_NO_IP(response); return; } @@ -111,7 +111,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en cJSON_Delete(json); controller_free(controller); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the given ip address is not valid"); + M_RESPONSE_400_NO_VALID_IP(response); return; } } @@ -121,7 +121,7 @@ api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, en controller_free(controller); cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "failed to save controller to database"); + M_RESPONSE_500_FAILED_TO_SAVE_TO_DATABASE(response); return; } LOGGER_DEBUG("saved controller %s\n", args[0].value.v_str); @@ -161,11 +161,11 @@ api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, if(controller_remove(controller)) { - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "failed to remove controller from database"); + M_RESPONSE_500_FAILED_TO_DELETE_FROM_DATABASE(response); } else { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "deleted controller"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "deleted controller"); } controller_free(controller); return; diff --git a/src/endpoints/api_v1_controllers_STR_relays_INT.c b/src/endpoints/api_v1_controllers_STR_relays_INT.c index d6ec00d..d470ef2 100644 --- a/src/endpoints/api_v1_controllers_STR_relays_INT.c +++ b/src/endpoints/api_v1_controllers_STR_relays_INT.c @@ -37,7 +37,7 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *nc, struct http_mess if(!relay) { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no relay for this controller found"); + M_RESPONSE_404_NO_RELAY_FOUND_FOR_ID_AND_NUMBER(response); return; } @@ -76,7 +76,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess if(args[1].value.v_int > controller_relay_count) { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "relay number is too high for this controller"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "relay number is too high for this controller"); return; } @@ -137,7 +137,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one schedule is missing an id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one schedule is missing an id"); return; } uuid_t target_uid; @@ -145,7 +145,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "at least one schedule has a bad id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "at least one schedule has a bad id"); return; } @@ -174,7 +174,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "active schedule has a bad uid"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "active schedule has a bad uid"); return; } relay->schedules[day_of_week] = schedule_get_by_uid_or_off(target_uid); @@ -190,7 +190,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess database_transaction_rollback(&lock); cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "failed to save relay to database"); + M_RESPONSE_500_FAILED_TO_SAVE_TO_DATABASE(response); return; } @@ -214,7 +214,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess cJSON_Delete(json); free(tag_ids); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "invalid tag in tags"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "invalid tag in tags"); return; } const char *tag = json_tag->valuestring; diff --git a/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c b/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c index f72812a..9398b64 100644 --- a/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c +++ b/src/endpoints/api_v1_controllers_STR_relays_INT_pulse.c @@ -37,7 +37,7 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht if(!relay) { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no relay found"); + M_RESPONSE_404_NO_RELAY_FOUND_FOR_ID_AND_NUMBER(response); return; } @@ -58,6 +58,6 @@ api_v1_controllers_STR_relays_INT_pulse_POST(struct mg_connection *nc, struct ht LOGGER_DEBUG("commanding pulse to relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str); command_relay_pulse(relay, duration); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "sent pulse"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "sent pulse"); relay_free(relay); } diff --git a/src/endpoints/api_v1_controllers_discover.c b/src/endpoints/api_v1_controllers_discover.c index d4c5fb1..37bae48 100644 --- a/src/endpoints/api_v1_controllers_discover.c +++ b/src/endpoints/api_v1_controllers_discover.c @@ -116,7 +116,7 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h if(discover_server_port == -1) { - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "the server failed to prepare discovery"); + M_RESPONSE_MSG(LOGGER_ERR, response, 500, "the server failed to prepare discovery"); return; } @@ -126,7 +126,7 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h LOGGER_DEBUG("sending udp broadcast\n"); if(send_udp_broadcast("255.255.255.255", global_config->discovery_port, payload, sizeof(payload)) < 0) { - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "the server failed to send discovery broadcast"); + M_RESPONSE_MSG(LOGGER_ERR, response, 500, "the server failed to send discovery broadcast"); return; } @@ -172,8 +172,6 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h } char *answer_payload = (char*)malloc((payload_length)); - ssize_t bytes_transferred; - if(recv(client_fd, answer_payload, payload_length, 0) <= 0) { LOGGER_ERR("error receiving payload from client\n"); diff --git a/src/endpoints/api_v1_macros.c b/src/endpoints/api_v1_macros.c index e78b14f..e9cc5ac 100644 --- a/src/endpoints/api_v1_macros.c +++ b/src/endpoints/api_v1_macros.c @@ -58,7 +58,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contains actions for the macro"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contains actions for the macro"); return; } @@ -93,7 +93,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without weekday"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action without weekday"); return; } @@ -104,7 +104,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without schedule"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action without schedule"); return; } cJSON *json_action_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_action_schedule, "id"); @@ -114,7 +114,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without schedule id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action without schedule id"); return; } uuid_t action_schedule_uid; @@ -124,7 +124,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action with an invalid schedule id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action with an invalid schedule id"); return; } @@ -135,7 +135,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "the schedule for at least one action was not found"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "the schedule for at least one action was not found"); return; } @@ -150,7 +150,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without relay"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action without relay"); return; } cJSON *json_action_relay_number = cJSON_GetObjectItemCaseSensitive(json_action_relay, "number"); @@ -160,7 +160,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without relay number"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action without relay number"); return; } cJSON *json_action_relay_controller_uid = cJSON_GetObjectItemCaseSensitive(json_action_relay, "controller_id"); @@ -170,7 +170,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without relay controller id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action without relay controller id"); return; } uuid_t action_controller_uid; @@ -180,7 +180,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action with an invalid relay controller id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an action with an invalid relay controller id"); return; } @@ -191,7 +191,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "the controller for at least one action relay was not found"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "the controller for at least one action relay was not found"); return; } @@ -207,7 +207,7 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a cJSON_Delete(json); macro_free(new_macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "the relay for at least one action was not found"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 404, "the relay for at least one action was not found"); return; } diff --git a/src/endpoints/api_v1_macros_STR.c b/src/endpoints/api_v1_macros_STR.c index 44a5e18..fd03267 100644 --- a/src/endpoints/api_v1_macros_STR.c +++ b/src/endpoints/api_v1_macros_STR.c @@ -81,7 +81,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "macro could not be saved"); + M_RESPONSE_500_FAILED_TO_SAVE_TO_DATABASE(response); return; } } @@ -101,7 +101,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action is missing a weekday"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action is missing a weekday"); return; } @@ -112,7 +112,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action is missing a schedule"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action is missing a schedule"); return; } cJSON *json_action_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_action_schedule, "id"); @@ -122,7 +122,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action is missing a schedule id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action is missing a schedule id"); return; } uuid_t action_schedule_uid; @@ -132,7 +132,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action has a bad schedule id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action has a bad schedule id"); return; } @@ -143,7 +143,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action schedule was not found"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action schedule was not found"); return; } @@ -158,7 +158,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action is missing a relay"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action is missing a relay"); return; } cJSON *json_action_relay_number = cJSON_GetObjectItemCaseSensitive(json_action_relay, "number"); @@ -168,7 +168,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action is missing a relay number"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action is missing a relay number"); return; } cJSON *json_action_relay_controller_uid = cJSON_GetObjectItemCaseSensitive(json_action_relay, "controller_id"); @@ -178,7 +178,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action is missing a relay controller id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action is missing a relay controller id"); return; } uuid_t action_controller_uid; @@ -188,7 +188,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action has a bad relay controller id"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action has a bad relay controller id"); return; } @@ -199,7 +199,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "at least one action relay controller was not found"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "at least one action relay controller was not found"); return; } @@ -215,7 +215,7 @@ api_v1_macros_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "action relay was not found"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "action relay was not found"); return; } @@ -264,17 +264,17 @@ api_v1_macros_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endp if(!macro) { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "no macro with id found"); + M_RESPONSE_404_NO_MACRO_FOUND_FOR_ID(response); return; } if(macro_remove(macro)) { - M_RESPONSE_TEXT_STATIC(LOGGER_ERR, response, 500, "failed to remove macro from database"); + M_RESPONSE_500_FAILED_TO_DELETE_FROM_DATABASE(response); } else { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "deleted macro"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "deleted macro"); } macro_free(macro); return; diff --git a/src/endpoints/api_v1_macros_STR_execute.c b/src/endpoints/api_v1_macros_STR_execute.c index 33f3952..411277b 100644 --- a/src/endpoints/api_v1_macros_STR_execute.c +++ b/src/endpoints/api_v1_macros_STR_execute.c @@ -60,5 +60,5 @@ api_v1_macros_STR_execute_PUT(struct mg_connection *nc, struct http_message *hm, macro_action_free_list(macro_actions); macro_free(macro); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "macro got executed"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "macro got executed"); } diff --git a/src/endpoints/api_v1_schedules.c b/src/endpoints/api_v1_schedules.c index d72b34b..0b1599d 100644 --- a/src/endpoints/api_v1_schedules.c +++ b/src/endpoints/api_v1_schedules.c @@ -34,7 +34,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contain periods"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain periods"); return; } @@ -46,7 +46,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one invalid tag"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one invalid tag"); return; } } @@ -75,7 +75,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period without a start"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period without a start"); return; } if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL)) @@ -83,7 +83,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period without an end"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period without an end"); return; } @@ -94,7 +94,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid start"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid start"); return; } if(period_helper_parse_hhmm(json_period_end->valuestring, &end)) @@ -102,7 +102,7 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin cJSON_Delete(json); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid end"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid end"); return; } diff --git a/src/endpoints/api_v1_schedules_STR.c b/src/endpoints/api_v1_schedules_STR.c index 3abc0c8..9862b43 100644 --- a/src/endpoints/api_v1_schedules_STR.c +++ b/src/endpoints/api_v1_schedules_STR.c @@ -201,7 +201,7 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e } else { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "the target schedule got deleted"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "the target schedule got deleted"); } schedule_free(schedule); return; diff --git a/src/endpoints/api_v1_schedules_list.c b/src/endpoints/api_v1_schedules_list.c index 46d2b8a..d9be32a 100644 --- a/src/endpoints/api_v1_schedules_list.c +++ b/src/endpoints/api_v1_schedules_list.c @@ -27,7 +27,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name"); if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL)) { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contain a name for at least one schedule"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain a name for at least one schedule"); return; } @@ -36,7 +36,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en { cJSON_Delete(json_list); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contain periods for at least one schedule"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain periods for at least one schedule"); return; } @@ -48,7 +48,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en { cJSON_Delete(json_list); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one invalid tag"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one invalid tag"); return; } } @@ -77,7 +77,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en cJSON_Delete(json_list); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period without a start"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period without a start"); return; } if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL)) @@ -85,7 +85,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en cJSON_Delete(json_list); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period without an end"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period without an end"); return; } @@ -96,7 +96,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en cJSON_Delete(json_list); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid start"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid start"); return; } if(period_helper_parse_hhmm(json_period_end->valuestring, &end)) @@ -104,7 +104,7 @@ api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, en cJSON_Delete(json_list); schedule_free(new_schedule); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid end"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid end"); return; } diff --git a/src/endpoints/api_v1_tags.c b/src/endpoints/api_v1_tags.c index 20a0591..85b403e 100644 --- a/src/endpoints/api_v1_tags.c +++ b/src/endpoints/api_v1_tags.c @@ -55,7 +55,7 @@ api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_arg { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contain a tag"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request does not contain a tag"); return; } @@ -63,7 +63,7 @@ api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_arg { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an empty tag"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the request contains an empty tag"); return; } @@ -71,7 +71,7 @@ api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_arg { cJSON_Delete(json); - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the tag does already exist"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the tag does already exist"); return; } diff --git a/src/endpoints/api_v1_tags_STR.c b/src/endpoints/api_v1_tags_STR.c index e0e7c55..6bf0d9c 100644 --- a/src/endpoints/api_v1_tags_STR.c +++ b/src/endpoints/api_v1_tags_STR.c @@ -94,6 +94,6 @@ api_v1_tags_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoi } else { - M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "the tag got deleted"); + M_RESPONSE_MSG(LOGGER_DEBUG, response, 200, "the tag got deleted"); } } diff --git a/src/handlers/http.c b/src/handlers/http.c index 6821c64..1dd2a7a 100644 --- a/src/handlers/http.c +++ b/src/handlers/http.c @@ -84,7 +84,7 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm) endpoint_response_t response; - M_RESPONSE_TEXT_STATIC(LOGGER_NONE, &response, 500, "server did not create a response"); + M_RESPONSE_MSG(LOGGER_NONE, &response, 500, "server did not create a response"); if(!endpoint) {