diff --git a/lib/src/restd_server.c b/lib/src/restd_server.c index 68bad0d..fa4c4be 100644 --- a/lib/src/restd_server.c +++ b/lib/src/restd_server.c @@ -201,6 +201,8 @@ void restd_server_free(restd_server_t *server) { if (hook->method) free(hook->method); + if (hook->path) + free(hook->path); free(hook); } server->hooks->free(server->hooks); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2353e9e..ce8f17c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,7 +31,8 @@ file( devices/outlet_dio.c devices/shutter.c devices/sprinkler.c - rest/rest_handler.c + rest/rest_devices_handlers.c + rest/rest_server.c ) if(NOT DOMO_BUILD_TEST) @@ -46,6 +47,7 @@ target_link_libraries (${PROJECT_NAME} json-c event event_pthreads + ) install (TARGETS ${PROJECT_NAME} DESTINATION local/bin) @@ -74,4 +76,5 @@ target_link_libraries (test_device event_pthreads gcov ) + endif(DOMO_BUILD_TEST) diff --git a/src/devices/devices_manager.c b/src/devices/devices_manager.c index 494744c..5fbd5ac 100644 --- a/src/devices/devices_manager.c +++ b/src/devices/devices_manager.c @@ -301,41 +301,6 @@ struct json_object *devices_manager_to_json_object(devices_manager_t *dm, const /*--------------------------------------------------------------------------*/ -int devices_manager_set(devices_manager_t *dm, const char *capability, struct json_object *node) -{ -#warning "TODO" - return 0; -} - -/*--------------------------------------------------------------------------*/ - -int devices_manager_set_state(devices_manager_t *dm, const char *capability, int id, bool state) -{ - int result = -1; - - if (capability == kOutletEntry) - { - result = set_outlet_state(dm, id, state); - } - else if (capability == kShutterEntry) - { - result = set_shutter_state(dm, id, state); - } - else if (capability == kSprinklerEntry) - { - result = set_sprinkler_state(dm, id, state); - } - - if (result == 0) - { - devices_manager_save(dm); - } - - return result; -} - -/*--------------------------------------------------------------------------*/ - int devices_manager_create(devices_manager_t *dm, const char *capability, struct json_object *node) { // Sanity checks diff --git a/src/devices/devices_manager.h b/src/devices/devices_manager.h index 41e4fe8..40a32fc 100644 --- a/src/devices/devices_manager.h +++ b/src/devices/devices_manager.h @@ -69,9 +69,6 @@ extern char *devices_manager_get_by_id(devices_manager_t *dm, const char *capabi extern struct json_object *devices_manager_to_json_object(devices_manager_t *dm, const char *capability); -extern int devices_manager_set(devices_manager_t *dm, const char *capability, struct json_object *node); -extern int devices_manager_set_state(devices_manager_t *dm, const char *capability, int id, bool state); - extern int devices_manager_create(devices_manager_t *dm, const char *capability, struct json_object *node); extern int devices_manager_update(devices_manager_t *dm, const char *capability, struct json_object *node); extern int devices_manager_delete(devices_manager_t *dm, const char *capability, struct json_object *node); diff --git a/src/devices/devices_manager_internal.c b/src/devices/devices_manager_internal.c index cf4298f..ebc4e09 100644 --- a/src/devices/devices_manager_internal.c +++ b/src/devices/devices_manager_internal.c @@ -192,14 +192,6 @@ int update_outlet(devices_manager_t *dm, uint16_t id, struct json_object *node) /*--------------------------------------------------------------------------*/ -int set_outlet_state(devices_manager_t *dm, uint16_t id, bool state) -{ -#warning "TODO" - return 0; -} - -/*--------------------------------------------------------------------------*/ - outlet_dio_t *get_outlet_by_id(devices_manager_t *dm, uint16_t id) { qlist_obj_t obj; @@ -394,14 +386,6 @@ int update_shutter(devices_manager_t *dm, uint16_t id, struct json_object *node) /*--------------------------------------------------------------------------*/ -int set_shutter_state(devices_manager_t *dm, uint16_t id, bool state) -{ -#warning "TODO" - return 0; -} - -/*--------------------------------------------------------------------------*/ - shutter_t *get_shutter_by_id(devices_manager_t *dm, uint16_t id) { qlist_obj_t obj; @@ -596,32 +580,6 @@ int update_sprinkler(devices_manager_t *dm, uint16_t id, struct json_object *nod /*--------------------------------------------------------------------------*/ -int set_sprinkler_state(devices_manager_t *dm, uint16_t id, bool state) -{ - qlist_obj_t obj; - sprinkler_t *sprinkler; - - // Sanity Checks - if (dm == NULL) - return -1; - - memset((void *)&obj, 0, sizeof(obj)); - - while (qlist_getnext(dm->sprinklers, &obj, true)) - { - sprinkler = obj.data; - if (sprinkler->device->id == id) - { - sprinkler->device->state = state; - return 0; - } - } - - return -1; -} - -/*--------------------------------------------------------------------------*/ - sprinkler_t *get_sprinkler_by_id(devices_manager_t *dm, uint16_t id) { qlist_obj_t obj; diff --git a/src/main.c b/src/main.c index ecef404..43ecaf0 100644 --- a/src/main.c +++ b/src/main.c @@ -163,7 +163,7 @@ int main(int argc, char **argv) restd_server_attach_event_loop(rest_server, ev_base); - if (setup_rest_server(rest_server, "8888", argv[1]) != 0) + if (setup_rest_server(rest_server, "8888", argv[1], device_manager) != 0) { fprintf(stderr, "Failed to setup rest Server\n."); restd_server_free(rest_server); diff --git a/src/rest/rest_devices_handlers.c b/src/rest/rest_devices_handlers.c new file mode 100644 index 0000000..f458e63 --- /dev/null +++ b/src/rest/rest_devices_handlers.c @@ -0,0 +1,63 @@ +/*! + * rest_devices_handlers.c + * + * Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 13/01/2020 + * + */ + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include "rest_devices_handlers.h" + +/*--------------------------------------------------------------------------*/ + +int outlet_create_handler(short event, restd_conn_t *conn, void *userdata) +{ + return 0; +} + +/*--------------------------------------------------------------------------*/ + +int outlet_list_handler(short event, restd_conn_t *conn, void *userdata) +{ + return 0; +} + +/*--------------------------------------------------------------------------*/ + +int outlet_get_handler(short event, restd_conn_t *conn, void *userdata) +{ + return 0; +} + +/*--------------------------------------------------------------------------*/ + +int outlet_update_handler(short event, restd_conn_t *conn, void *userdata) +{ + return 0; +} + +/*--------------------------------------------------------------------------*/ + +int outlet_remove_handler(short event, restd_conn_t *conn, void *userdata) +{ + return 0; +} diff --git a/src/rest/rest_devices_handlers.h b/src/rest/rest_devices_handlers.h new file mode 100644 index 0000000..fbaa3e9 --- /dev/null +++ b/src/rest/rest_devices_handlers.h @@ -0,0 +1,40 @@ +/*! + * rest_devices_handlers.h + * + * Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 13/01/2020 + * + */ + +#ifndef _REST_DEVICES_HANDLERS_H +#define _REST_DEVICES_HANDLERS_H + +/*------------------------------- INCLUDES ----------------------------------*/ + +#include + +// Outlets +extern int outlet_create_handler(short event, restd_conn_t *conn, void *userdata); +extern int outlet_list_handler(short event, restd_conn_t *conn, void *userdata); +extern int outlet_get_handler(short event, restd_conn_t *conn, void *userdata); +extern int outlet_update_handler(short event, restd_conn_t *conn, void *userdata); +extern int outlet_remove_handler(short event, restd_conn_t *conn, void *userdata); + +#endif /*_REST_DEVICES_HANDLERS_H */ diff --git a/src/rest/rest_handler.c b/src/rest/rest_server.c similarity index 70% rename from src/rest/rest_handler.c rename to src/rest/rest_server.c index 1234a87..2896304 100644 --- a/src/rest/rest_handler.c +++ b/src/rest/rest_server.c @@ -28,7 +28,9 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include "rest_handler.h" +#include "rest_devices_handlers.h" + +#include "rest_server.h" /*--------------------------------------------------------------------------*/ @@ -50,15 +52,29 @@ int my_error_handler(short event, restd_conn_t *conn, void *userdata) /*--------------------------------------------------------------------------*/ -int setup_rest_server(restd_server_t *rest_server, const char *port, const char *root_path) +int setup_rest_server(restd_server_t *rest_server, const char *port, const char *root_path, void *dm) { restd_server_set_option(rest_server, "server.port", port); - restd_server_set_option(rest_server, "server.root_path",root_path); + restd_server_set_option(rest_server, "server.root_path", root_path); restd_server_register_request_handler(rest_server, restd_rest_handler); restd_server_register_error_handler(rest_server, my_error_handler); restd_server_register_hook(rest_server, restd_http_handler, NULL); + // Capabilities. + restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/capabilities", NULL, dm); + + // Outlets + restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/outlets", outlet_create_handler, dm); + restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/outlets", outlet_list_handler, dm); + restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/outlets/:id", outlet_get_handler, dm); + restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/outlets/:id", outlet_update_handler, dm); + restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/outlets/:id", outlet_remove_handler, dm); + + // Outlets + + // Shutters + return 0; } diff --git a/src/rest/rest_handler.h b/src/rest/rest_server.h similarity index 90% rename from src/rest/rest_handler.h rename to src/rest/rest_server.h index 92b2828..f93c3c9 100644 --- a/src/rest/rest_handler.h +++ b/src/rest/rest_server.h @@ -23,13 +23,13 @@ * */ -#ifndef _REST_HANDLER_H -#define _REST_HANDLER_H +#ifndef _REST_SERVER_H +#define _REST_SERVER_H /*------------------------------- INCLUDES ----------------------------------*/ #include -extern int setup_rest_server(restd_server_t *rest_server, const char *port, const char *root_path); +extern int setup_rest_server(restd_server_t *rest_server, const char *port, const char *root_path, void *dm); -#endif /*_REST_HANDLER_H */ +#endif /*_REST_SERVER_H */ diff --git a/src/tests/test_main.c b/src/tests/test_main.c index 5b3b688..100ba44 100644 --- a/src/tests/test_main.c +++ b/src/tests/test_main.c @@ -29,6 +29,8 @@ #include #include +#include + #include "qunit.h" #include "domo.h" @@ -51,6 +53,7 @@ TEST("devices_manager create and free\t") #include "test_sprinkler.c" #include "test_shutter.c" #include "test_outlet.c" +#include "test_rest.c" #if 0 diff --git a/src/tests/test_rest.c b/src/tests/test_rest.c new file mode 100644 index 0000000..4de7a53 --- /dev/null +++ b/src/tests/test_rest.c @@ -0,0 +1,70 @@ +/*! + * test_rest.c + * + * Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 08/01/2020 + * + */ + +// This is an independent project of an individual developer. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com + +/*--------------------------------------------------------------------------*/ + +int my_test_handler(short event, restd_conn_t *conn, void *userdata) +{ + restd_http_response(conn, 200, "application/json", "{\"status\":\"error\"}", 18); + return RESTD_CLOSE; // Close connection. +} + + +TEST("Rest - create free\t") +{ + restd_server_t *rest_server; + + rest_server = restd_server_new(); + ASSERT_NOT_NULL(rest_server); + + restd_server_set_option(rest_server, "server.port", "7777"); + + restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_test_handler, NULL); + + + restd_server_free(rest_server); +} + +TEST("Rest - create free\t") +{ + restd_server_t *rest_server; + + rest_server = restd_server_new(); + ASSERT_NOT_NULL(rest_server); + + restd_server_set_option(rest_server, "server.port", "7777"); + + restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_test_handler, NULL); + restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_test_handler, NULL); + restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_test_handler, NULL); + restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/test/:id", my_test_handler, NULL); + restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/test/:id", my_test_handler, NULL); + + + restd_server_free(rest_server); +}