#include #include #include #include #include #include #include #include #include void api_v1_controllers_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; (void)nc; uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { LOG_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); return; } controller_t* controller = controller_get_by_uid(target_uid); if(!controller) { LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); return; } cJSON *json = controller_to_json(controller); endpoint_response_json(response, 200, json); cJSON_Delete(json); controller_free(controller); } void api_v1_controllers_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; (void)nc; uuid_t target_uid; if(uuid_parse(args[0].value.v_str, target_uid)) { LOG_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); return; } controller_t* controller = controller_get_by_uid(target_uid); if(!controller) { LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); return; } cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len); if(json == NULL) { static const char content[] = "no valid json was supplied"; endpoint_response_text(response, 400, content, STRLEN(content)); controller_free(controller); return; } cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name"); if(json_name) { if(cJSON_IsString(json_name) && json_name->valuestring) { strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH); controller->name[MAX_NAME_LENGTH] = '\0'; } else { static const char content[] = "the given name is no valid string"; endpoint_response_text(response, 400, content, STRLEN(content)); cJSON_Delete(json); controller_free(controller); return; } } cJSON *json_ip = cJSON_GetObjectItemCaseSensitive(json, "ip"); if(json_ip) { if(cJSON_IsString(json_ip) && json_ip->valuestring) { unsigned char buf[sizeof(struct in_addr)]; if(inet_pton(AF_INET, json_ip->valuestring, buf)) { strncpy(controller->ip, json_ip->valuestring, IP_LENGTH); controller->ip[IP_LENGTH] = '\0'; } else { static const char content[] = "the given ip address is no valid IPv4 address"; endpoint_response_text(response, 400, content, STRLEN(content)); cJSON_Delete(json); controller_free(controller); return; } } else { static const char content[] = "the given ip address is no valid string"; endpoint_response_text(response, 400, content, STRLEN(content)); cJSON_Delete(json); controller_free(controller); return; } } if(controller_save(controller)) { LOG_ERROR("failed to save controller\n"); controller_free(controller); cJSON_Delete(json); static const char content[] = "failed to save controller to database"; endpoint_response_text(response, 500, content, STRLEN(content)); return; } cJSON_Delete(json); json = controller_to_json(controller); command_set_controller_name(controller); endpoint_response_json(response, 200, json); cJSON_Delete(json); controller_free(controller); } void api_v1_controllers_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { (void)hm; (void)nc; const char *target_uid_str = args[0].value.v_str; uuid_t target_uid; if(uuid_parse(target_uid_str, target_uid)) { LOG_DEBUG("failed to unparse uid\n"); static const char content[] = "given id was invalid"; endpoint_response_text(response, 400, content, STRLEN(content)); return; } controller_t* controller = controller_get_by_uid(target_uid); if(!controller) { LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str); static const char content[] = "no controller for id found"; endpoint_response_text(response, 404, content, STRLEN(content)); return; } if(controller_remove(controller)) { LOG_ERROR("failed to remove controller from database\n"); static const char content[] = "failed to remove controller from database"; endpoint_response_text(response, 500, content, STRLEN(content)); } else { endpoint_response_text(response, 200, "", 0); } controller_free(controller); return; }