add: endpoint responses
This commit is contained in:
		
							parent
							
								
									5a19e99627
								
							
						
					
					
						commit
						5e64f5963c
					
				
					 18 changed files with 587 additions and 235 deletions
				
			
		| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_controllers.h>
 | 
					#include <endpoints/api_v1_controllers.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
| 
						 | 
					@ -7,7 +8,7 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)args;
 | 
					    (void)args;
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
| 
						 | 
					@ -26,14 +27,21 @@ api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct ht
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print controllers json\n");
 | 
					        LOG_ERROR("failed to print controllers json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for controllers";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    controller_free_list(all_controllers);
 | 
					    controller_free_list(all_controllers);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <command.h>
 | 
					#include <command.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_controllers.h>
 | 
					#include <endpoints/api_v1_controllers.h>
 | 
				
			||||||
| 
						 | 
					@ -7,16 +8,21 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
					    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,42 +30,60 @@ api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!controller)
 | 
					    if(!controller)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no controller for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cJSON *json =  controller_to_json(controller);
 | 
					    cJSON *json = controller_to_json(controller);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    char *json_str = cJSON_Print(json);
 | 
					    char *json_str = cJSON_Print(json);
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print controllers json\n");
 | 
					        LOG_ERROR("failed to print controller json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for controller";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    controller_free(controller);
 | 
					    controller_free(controller);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
					    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -67,9 +91,14 @@ api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!controller)
 | 
					    if(!controller)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no controller for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -80,11 +109,16 @@ api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struc
 | 
				
			||||||
        const char *error_ptr = cJSON_GetErrorPtr();
 | 
					        const char *error_ptr = cJSON_GetErrorPtr();
 | 
				
			||||||
        if (error_ptr != NULL)
 | 
					        if (error_ptr != NULL)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            LOG_ERROR("error before: %s\n", error_ptr);
 | 
					            LOG_DEBUG("error before: %s\n", error_ptr);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no valid json was supplied";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -105,43 +139,49 @@ api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struc
 | 
				
			||||||
    if(controller_save(controller))
 | 
					    if(controller_save(controller))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to save controller\n");
 | 
					        LOG_ERROR("failed to save controller\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					 | 
				
			||||||
        free(controller);
 | 
					        free(controller);
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        static const char content[] = "failed to save controller to database";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    json = controller_to_json(controller);
 | 
					    json = controller_to_json(controller);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int result = command_set_controller_name(controller);
 | 
					    command_set_controller_name(controller);
 | 
				
			||||||
    int status_code = 200;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if(result)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        status_code = 504;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    char *json_str = cJSON_Print(json);
 | 
					    char *json_str = cJSON_Print(json);
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print controller json\n");
 | 
					        LOG_ERROR("failed to print controller json\n");
 | 
				
			||||||
        mg_send_head(c, status_code, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "failed to print json for controller";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, status_code, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    controller_free(controller);
 | 
					    controller_free(controller);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -150,9 +190,14 @@ api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, st
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(uuid_parse(target_uid_str, target_uid))
 | 
					    if(uuid_parse(target_uid_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -160,21 +205,35 @@ api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, st
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!controller)
 | 
					    if(!controller)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a controller for uid '%s'\n", target_uid_str);
 | 
					        LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no controller for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(controller_remove(controller))
 | 
					    if(controller_remove(controller))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        LOG_ERROR("failed to remove controller from database\n");
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					
 | 
				
			||||||
 | 
					        static const char content[] = "failed to remove controller from database";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
 | 
					        response->content_length = 0;
 | 
				
			||||||
 | 
					        response->content = "";
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    controller_free(controller);
 | 
					    controller_free(controller);
 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_controllers.h>
 | 
					#include <endpoints/api_v1_controllers.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
| 
						 | 
					@ -6,16 +7,21 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
					    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,9 +29,14 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!controller)
 | 
					    if(!controller)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no controller for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
| 
						 | 
					@ -44,14 +55,21 @@ api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print relays json\n");
 | 
					        LOG_ERROR("failed to print relays json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for relays";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    relay_free_list(all_relays);
 | 
					    relay_free_list(all_relays);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <command.h>
 | 
					#include <command.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_controllers.h>
 | 
					#include <endpoints/api_v1_controllers.h>
 | 
				
			||||||
| 
						 | 
					@ -9,16 +10,21 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
					    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,9 +32,14 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!controller)
 | 
					    if(!controller)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no controller for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,9 +47,14 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!relay)
 | 
					    if(!relay)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a relay with num %d for controller '%s'\n", args[1].value.v_int, args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no relay for this controller found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -47,15 +63,22 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
    char *json_str = cJSON_Print(json);
 | 
					    char *json_str = cJSON_Print(json);
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print relays json\n");
 | 
					        LOG_ERROR("failed to print relay json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for relay";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    relay_free(relay);
 | 
					    relay_free(relay);
 | 
				
			||||||
| 
						 | 
					@ -63,16 +86,21 @@ api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
					    if(uuid_parse(args[0].value.v_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -80,9 +108,14 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!controller)
 | 
					    if(!controller)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no controller for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -120,8 +153,13 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
            LOG_ERROR("error before: %s\n", error_ptr);
 | 
					            LOG_ERROR("error before: %s\n", error_ptr);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no valid json was supplied";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -146,16 +184,27 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                LOG_DEBUG("schedules[%d] is missing uid\n", schedule_position);
 | 
					                LOG_DEBUG("schedules[%d] is missing uid\n", schedule_position);
 | 
				
			||||||
                cJSON_Delete(json);
 | 
					                cJSON_Delete(json);
 | 
				
			||||||
                mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
                mg_printf(c, "{}");
 | 
					                static const char content[] = "at least one schedule is missing an id";
 | 
				
			||||||
 | 
					                response->status_code = 400;
 | 
				
			||||||
 | 
					                response->content_type = "text/plain";
 | 
				
			||||||
 | 
					                response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					                response->content = content;
 | 
				
			||||||
 | 
					                response->alloced_content = false;
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            uuid_t target_uid;
 | 
					            uuid_t target_uid;
 | 
				
			||||||
            if(schedule_uid_parse(json_schedule_uid->valuestring, target_uid))
 | 
					            if(schedule_uid_parse(json_schedule_uid->valuestring, target_uid))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                LOG_ERROR("failed to unparse uid\n");
 | 
					                LOG_DEBUG("schedules[%d] has bad uid\n", schedule_position);
 | 
				
			||||||
                mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					                cJSON_Delete(json);
 | 
				
			||||||
                mg_printf(c, "{}");
 | 
					
 | 
				
			||||||
 | 
					                static const char content[] = "at least one schedule has a bad id";
 | 
				
			||||||
 | 
					                response->status_code = 400;
 | 
				
			||||||
 | 
					                response->content_type = "text/plain";
 | 
				
			||||||
 | 
					                response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					                response->content = content;
 | 
				
			||||||
 | 
					                response->alloced_content = false;
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -181,9 +230,15 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
            uuid_t target_uid;
 | 
					            uuid_t target_uid;
 | 
				
			||||||
            if(schedule_uid_parse(json_active_schedule_uid->valuestring, target_uid))
 | 
					            if(schedule_uid_parse(json_active_schedule_uid->valuestring, target_uid))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                LOG_ERROR("failed to unparse uid\n");
 | 
					                LOG_DEBUG("active_schedule has bad uid\n");
 | 
				
			||||||
                mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					                cJSON_Delete(json);
 | 
				
			||||||
                mg_printf(c, "{}");
 | 
					
 | 
				
			||||||
 | 
					                static const char content[] = "active_schedule has a bad id";
 | 
				
			||||||
 | 
					                response->status_code = 400;
 | 
				
			||||||
 | 
					                response->content_type = "text/plain";
 | 
				
			||||||
 | 
					                response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					                response->content = content;
 | 
				
			||||||
 | 
					                response->alloced_content = false;
 | 
				
			||||||
                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);
 | 
				
			||||||
| 
						 | 
					@ -193,10 +248,16 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
    if(relay_save(relay))
 | 
					    if(relay_save(relay))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to save relay\n");
 | 
					        LOG_ERROR("failed to save relay\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        free(controller);
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					 | 
				
			||||||
        free(relay);
 | 
					 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        static const char content[] = "failed to save relay to database";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -232,14 +293,21 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print relay json\n");
 | 
					        LOG_ERROR("failed to print relay json\n");
 | 
				
			||||||
        mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "failed to print json for relay";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    relay_free(relay);
 | 
					    relay_free(relay);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_controllers.h>
 | 
					#include <endpoints/api_v1_controllers.h>
 | 
				
			||||||
#include <models/controller.h>
 | 
					#include <models/controller.h>
 | 
				
			||||||
| 
						 | 
					@ -104,15 +105,20 @@ send_udp_broadcast(const char *addr, uint16_t port, void *message, size_t length
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int discover_server_socket = bind_tcp_server("0.0.0.0", "0", 20);
 | 
					    int discover_server_socket = bind_tcp_server("0.0.0.0", "0", 20);
 | 
				
			||||||
    int discover_server_port = get_server_port(discover_server_socket);
 | 
					    int discover_server_port = get_server_port(discover_server_socket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(discover_server_port == -1)
 | 
					    if(discover_server_port == -1)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        LOG_ERROR("failed to get server port for discovery\n");
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -121,8 +127,13 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        LOG_ERROR("failed to send UDP broadcast\n");
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -292,5 +303,5 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    controller_free_list(known_controllers);
 | 
					    controller_free_list(known_controllers);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    api_v1_controllers_GET(c, args, hm);
 | 
					    api_v1_controllers_GET(hm, args, response);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_relays.h>
 | 
					#include <endpoints/api_v1_relays.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
| 
						 | 
					@ -7,7 +8,7 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)args;
 | 
					    (void)args;
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
| 
						 | 
					@ -26,14 +27,21 @@ api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_me
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print relays json\n");
 | 
					        LOG_ERROR("failed to print relays json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for relays";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    relay_free_list(all_relays);
 | 
					    relay_free_list(all_relays);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_relays.h>
 | 
					#include <endpoints/api_v1_relays.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
| 
						 | 
					@ -7,7 +8,7 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,9 +16,14 @@ api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
    int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id);
 | 
					    int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id);
 | 
				
			||||||
    if(relays_ids == NULL)
 | 
					    if(relays_ids == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print relays json\n");
 | 
					        LOG_ERROR("failed to load relays for tag from database\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to load relays for tag from database";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -42,14 +48,21 @@ api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print relays json\n");
 | 
					        LOG_ERROR("failed to print relays json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for relays";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_schedules.h>
 | 
					#include <endpoints/api_v1_schedules.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
| 
						 | 
					@ -7,7 +8,7 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)args;
 | 
					    (void)args;
 | 
				
			||||||
    cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
 | 
					    cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
 | 
				
			||||||
| 
						 | 
					@ -20,16 +21,28 @@ api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct htt
 | 
				
			||||||
            LOG_ERROR("error before: %s\n", error_ptr);
 | 
					            LOG_ERROR("error before: %s\n", error_ptr);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        static const char content[] = "no valid json was supplied";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
 | 
					    cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
 | 
				
			||||||
    if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
 | 
					    if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_DEBUG("no name for schedule provided\n");
 | 
					        LOG_DEBUG("no name for schedule provided\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        static const char content[] = "no name for schedule provided";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON *json_period;
 | 
					    cJSON *json_period;
 | 
				
			||||||
| 
						 | 
					@ -113,21 +126,28 @@ api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct htt
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print schedule json\n");
 | 
					        LOG_ERROR("failed to print schedule json\n");
 | 
				
			||||||
        mg_send_head(c, 201, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "failed to print json for schedule";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 201, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 201;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    schedule_free(new_schedule);
 | 
					    schedule_free(new_schedule);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_schedules_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)args;
 | 
					    (void)args;
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
| 
						 | 
					@ -146,14 +166,21 @@ api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print schedules json\n");
 | 
					        LOG_ERROR("failed to print schedules json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for schedules";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    schedule_free_list(all_schedules);
 | 
					    schedule_free_list(all_schedules);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_schedules.h>
 | 
					#include <endpoints/api_v1_schedules.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
| 
						 | 
					@ -10,16 +11,21 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(schedule_uid_parse(args[0].value.v_str, target_uid))
 | 
					    if(schedule_uid_parse(args[0].value.v_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,9 +33,14 @@ api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!schedule)
 | 
					    if(!schedule)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a schedule for uid '%s'\n", args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no schedule for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,30 +50,42 @@ api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print schedules json\n");
 | 
					        LOG_ERROR("failed to print schedules json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for schedules";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    schedule_free(schedule);
 | 
					    schedule_free(schedule);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(schedule_uid_parse(args[0].value.v_str, target_uid))
 | 
					    if(schedule_uid_parse(args[0].value.v_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,9 +93,14 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!schedule)
 | 
					    if(!schedule)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a schedule for uid '%s'\n", args[0].value.v_str);
 | 
					        LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no schedule for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -83,11 +111,16 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
        const char *error_ptr = cJSON_GetErrorPtr();
 | 
					        const char *error_ptr = cJSON_GetErrorPtr();
 | 
				
			||||||
        if (error_ptr != NULL)
 | 
					        if (error_ptr != NULL)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            LOG_ERROR("error before: %s\n", error_ptr);
 | 
					            LOG_DEBUG("error before: %s\n", error_ptr);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "no valid json was supplied";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -149,10 +182,15 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
    if(schedule_save(schedule))
 | 
					    if(schedule_save(schedule))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to save schedule\n");
 | 
					        LOG_ERROR("failed to save schedule\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					 | 
				
			||||||
        free(schedule);
 | 
					        free(schedule);
 | 
				
			||||||
        cJSON_Delete(json);
 | 
					        cJSON_Delete(json);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        static const char content[] = "failed to save schedule to database";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -192,21 +230,28 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print schedule json\n");
 | 
					        LOG_ERROR("failed to print schedule json\n");
 | 
				
			||||||
        mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "failed to print json for schedule";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
    schedule_free(schedule);
 | 
					    schedule_free(schedule);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -215,36 +260,62 @@ api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, stru
 | 
				
			||||||
    uuid_t target_uid;
 | 
					    uuid_t target_uid;
 | 
				
			||||||
    if(schedule_uid_parse(target_uid_str, target_uid))
 | 
					    if(schedule_uid_parse(target_uid_str, target_uid))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to unparse uid\n");
 | 
					        LOG_DEBUG("failed to unparse uid\n");
 | 
				
			||||||
        mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        static const char content[] = "given id was invalid";
 | 
				
			||||||
 | 
					        response->status_code = 400;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    schedule_t* schedule = schedule_get_by_uid(target_uid);
 | 
					    schedule_t* schedule = schedule_get_by_uid(target_uid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(schedule_is_protected(schedule))
 | 
					    if(!schedule)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 403, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					
 | 
				
			||||||
 | 
					        static const char content[] = "no schedule for id found";
 | 
				
			||||||
 | 
					        response->status_code = 404;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(!schedule)
 | 
					    if(schedule_is_protected(schedule))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("could not find a schedule for uid '%s'\n", target_uid_str);
 | 
					        static const char content[] = "target schedule is protected";
 | 
				
			||||||
        mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 403;
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(schedule_remove(schedule))
 | 
					    if(schedule_remove(schedule))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        LOG_ERROR("failed to remove schedule from database\n");
 | 
				
			||||||
        mg_printf(c, "{}");
 | 
					
 | 
				
			||||||
        return;
 | 
					        static const char content[] = "failed to remove schedule from database";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					    else
 | 
				
			||||||
    mg_printf(c, "{}");
 | 
					    {
 | 
				
			||||||
 | 
					        response->status_code = 200;
 | 
				
			||||||
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
 | 
					        response->content_length = 0;
 | 
				
			||||||
 | 
					        response->content = "";
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    schedule_free(schedule);
 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
#include <cJSON.h>
 | 
					#include <cJSON.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <endpoints/api_v1_schedules.h>
 | 
					#include <endpoints/api_v1_schedules.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
| 
						 | 
					@ -7,7 +8,7 @@
 | 
				
			||||||
#include <models/tag.h>
 | 
					#include <models/tag.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,9 +16,14 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, str
 | 
				
			||||||
    int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id);
 | 
					    int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id);
 | 
				
			||||||
    if(schedules_ids == NULL)
 | 
					    if(schedules_ids == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print schedules json\n");
 | 
					        LOG_ERROR("failed to load schedules for tag from database\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to load schedules for tag from database";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -42,14 +48,21 @@ api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, str
 | 
				
			||||||
    if (json_str == NULL)
 | 
					    if (json_str == NULL)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        LOG_ERROR("failed to print schedules json\n");
 | 
					        LOG_ERROR("failed to print schedules json\n");
 | 
				
			||||||
        mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					
 | 
				
			||||||
        mg_printf(c, "[]");
 | 
					        static const char content[] = "failed to print json for schedules";
 | 
				
			||||||
 | 
					        response->status_code = 500;
 | 
				
			||||||
 | 
					        response->content_type = "text/plain";
 | 
				
			||||||
 | 
					        response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					        response->content = content;
 | 
				
			||||||
 | 
					        response->alloced_content = false;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
 | 
					        response->status_code = 200;
 | 
				
			||||||
        mg_printf(c, "%s", json_str);
 | 
					        response->content_type = "application/json";
 | 
				
			||||||
        free(json_str);
 | 
					        response->content_length = strlen(json_str);
 | 
				
			||||||
 | 
					        response->content = json_str;
 | 
				
			||||||
 | 
					        response->alloced_content = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cJSON_Delete(json);
 | 
					    cJSON_Delete(json);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,9 +1,20 @@
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <constants.h>
 | 
					#include <constants.h>
 | 
				
			||||||
#include <mongoose.h>
 | 
					#include <mongoose.h>
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
#include <router.h>
 | 
					#include <router.h>
 | 
				
			||||||
#include <handlers.h>
 | 
					#include <handlers.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define STD_HEADERS "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: *\r\nAccess-Control-Allow-Methods: *\r\n"
 | 
				
			||||||
 | 
					#define HEADERS_FMT STD_HEADERS "Content-Type: %s"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// -2 for "%s" -1 for \0
 | 
				
			||||||
 | 
					#define HEADERS_FMT_LEN (sizeof(HEADERS_FMT) - 3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define STD_RESPONSE_CONTENT "the server did not create a response"
 | 
				
			||||||
 | 
					#define STD_RESPONSE_CONTENT_LEN (sizeof(STD_RESPONSE_CONTENT) - 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
handler_connection(struct mg_connection *c, int ev, void *p)
 | 
					handler_connection(struct mg_connection *c, int ev, void *p)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -18,7 +29,27 @@ handler_connection(struct mg_connection *c, int ev, void *p)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if(endpoint->func)
 | 
					            if(endpoint->func)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                endpoint->func(c, endpoint->args, p);
 | 
					                endpoint_response_t response;
 | 
				
			||||||
 | 
					                response.status_code = 500;
 | 
				
			||||||
 | 
					                response.content_type = "text/plain";
 | 
				
			||||||
 | 
					                response.content_length = STD_RESPONSE_CONTENT_LEN;
 | 
				
			||||||
 | 
					                response.content = STD_RESPONSE_CONTENT;
 | 
				
			||||||
 | 
					                response.alloced_content = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                endpoint->func(p, endpoint->args, &response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                char *response_headers = malloc(sizeof(char) * (HEADERS_FMT_LEN + strlen(response.content_type) + 1));
 | 
				
			||||||
 | 
					                sprintf(response_headers, HEADERS_FMT, response.content_type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                mg_send_head(c, response.status_code, response.content_length, response_headers);
 | 
				
			||||||
 | 
					                mg_printf(c, "%s", response.content);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                free(response_headers);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if(response.alloced_content)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    free((char*)response.content);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                for(int i = 0; i < endpoint->args_count; ++i)
 | 
					                for(int i = 0; i < endpoint->args_count; ++i)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
| 
						 | 
					@ -33,11 +64,7 @@ handler_connection(struct mg_connection *c, int ev, void *p)
 | 
				
			||||||
                if(endpoint->method == HTTP_METHOD_OPTIONS)
 | 
					                if(endpoint->method == HTTP_METHOD_OPTIONS)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    char options_header[256]; // TODO make more generic
 | 
					                    char options_header[256]; // TODO make more generic
 | 
				
			||||||
                    sprintf(options_header, "Allow: OPTIONS%s%s%s%s\r\nAccess-Control-Allow-Methods: OPTIONS%s%s%s%s\r\n" STANDARD_HEADERS,
 | 
					                    sprintf(options_header, STD_HEADERS "Allow: OPTIONS%s%s%s%s",
 | 
				
			||||||
                            endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
 | 
					 | 
				
			||||||
                            endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
 | 
					 | 
				
			||||||
                            endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
 | 
					 | 
				
			||||||
                            endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : "",
 | 
					 | 
				
			||||||
                            endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
 | 
					                            endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
 | 
				
			||||||
                            endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
 | 
					                            endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
 | 
				
			||||||
                            endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
 | 
					                            endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,6 +28,4 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define PIFACE_GPIO_BASE 200
 | 
					#define PIFACE_GPIO_BASE 200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define STANDARD_HEADERS "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: *"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#endif /* CORE_CONTANTS_H */
 | 
					#endif /* CORE_CONTANTS_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,27 +4,27 @@
 | 
				
			||||||
#include <router.h>
 | 
					#include <router.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_discover_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_STR_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_relays_INT_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_STR_relays_INT_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_controllers_STR_relays_INT_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* CORE_ENDPOINTS_API_V1_CONTROLLERS_H */
 | 
					#endif /* CORE_ENDPOINTS_API_V1_CONTROLLERS_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,9 +4,9 @@
 | 
				
			||||||
#include <router.h>
 | 
					#include <router.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_relays_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_relays_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_relays_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* CORE_ENDPOINTS_API_V1_RELAYS_H */
 | 
					#endif /* CORE_ENDPOINTS_API_V1_RELAYS_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,21 +4,21 @@
 | 
				
			||||||
#include <router.h>
 | 
					#include <router.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_schedules_POST(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_schedules_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_schedules_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_schedules_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_schedules_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					api_v1_schedules_tag_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* CORE_ENDPOINTS_API_V1_SCHEDULES_H */
 | 
					#endif /* CORE_ENDPOINTS_API_V1_SCHEDULES_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										1
									
								
								include/macros.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								include/macros.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					#define STRLEN(s) ((sizeof(s)/sizeof(s[0])) - sizeof(s[0]))
 | 
				
			||||||
| 
						 | 
					@ -30,7 +30,16 @@ typedef struct
 | 
				
			||||||
    } value;
 | 
					    } value;
 | 
				
			||||||
} endpoint_args_t;
 | 
					} endpoint_args_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef void (*endpoint_func_f)(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
 | 
					typedef struct
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    int status_code;
 | 
				
			||||||
 | 
					    const char *content_type;
 | 
				
			||||||
 | 
					    size_t content_length;
 | 
				
			||||||
 | 
					    const char *content;
 | 
				
			||||||
 | 
					    int alloced_content;
 | 
				
			||||||
 | 
					} endpoint_response_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef void (*endpoint_func_f)(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct
 | 
					typedef struct
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										45
									
								
								router.c
									
										
									
									
									
								
							
							
						
						
									
										45
									
								
								router.c
									
										
									
									
									
								
							| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <logger.h>
 | 
					#include <logger.h>
 | 
				
			||||||
#include <router.h>
 | 
					#include <router.h>
 | 
				
			||||||
 | 
					#include <macros.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <endpoints/api_v1_schedules.h>
 | 
					#include <endpoints/api_v1_schedules.h>
 | 
				
			||||||
#include <endpoints/api_v1_controllers.h>
 | 
					#include <endpoints/api_v1_controllers.h>
 | 
				
			||||||
| 
						 | 
					@ -14,21 +15,31 @@ static int endpoints_registered = 0;
 | 
				
			||||||
static const char delimiter[2] = "/";
 | 
					static const char delimiter[2] = "/";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
endpoint_index_func(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					endpoint_index_func(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    (void)args;
 | 
					 | 
				
			||||||
    mg_send_head(c, 200, hm->body.len, "Content-Type: text/plain");
 | 
					 | 
				
			||||||
    //mg_printf(c, "%.*s", (int)hm->message.len, hm->message.p);
 | 
					 | 
				
			||||||
    mg_printf(c, "%.*s", (int)hm->body.len, hm->body.p);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
static void
 | 
					 | 
				
			||||||
endpoint_not_found_func(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    (void)args;
 | 
					    (void)args;
 | 
				
			||||||
    (void)hm;
 | 
					    (void)hm;
 | 
				
			||||||
    mg_send_head(c, 404, 9, "Content-Type: text/plain");
 | 
					
 | 
				
			||||||
    mg_printf(c, "not found");
 | 
					    static const char content[] = "Emgauwa";
 | 
				
			||||||
 | 
					    response->status_code = 200;
 | 
				
			||||||
 | 
					    response->content_type = "text/plain";
 | 
				
			||||||
 | 
					    response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					    response->content = content;
 | 
				
			||||||
 | 
					    response->alloced_content = false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void
 | 
				
			||||||
 | 
					endpoint_not_found_func(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    (void)args;
 | 
				
			||||||
 | 
					    (void)hm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    static const char content[] = "404 - NOT FOUND";
 | 
				
			||||||
 | 
					    response->status_code = 404;
 | 
				
			||||||
 | 
					    response->content_type = "text/plain";
 | 
				
			||||||
 | 
					    response->content_length = STRLEN(content);;
 | 
				
			||||||
 | 
					    response->content = content;
 | 
				
			||||||
 | 
					    response->alloced_content = false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct mg_str
 | 
					static struct mg_str
 | 
				
			||||||
| 
						 | 
					@ -107,6 +118,16 @@ router_register_endpoint(const char *route, int method, endpoint_func_f func)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            options_endpoint = router_register_endpoint(route, HTTP_METHOD_OPTIONS, NULL);
 | 
					            options_endpoint = router_register_endpoint(route, HTTP_METHOD_OPTIONS, NULL);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            for(int i = 0; i < options_endpoint->args_count; ++i)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                if(options_endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    free((char*)options_endpoint->args[i].value.v_str);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(endpoints_registered >= ENDPOINTS_MAX_COUNT)
 | 
					    if(endpoints_registered >= ENDPOINTS_MAX_COUNT)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue