This commit is contained in:
@@ -201,6 +201,8 @@ void restd_server_free(restd_server_t *server)
|
|||||||
{
|
{
|
||||||
if (hook->method)
|
if (hook->method)
|
||||||
free(hook->method);
|
free(hook->method);
|
||||||
|
if (hook->path)
|
||||||
|
free(hook->path);
|
||||||
free(hook);
|
free(hook);
|
||||||
}
|
}
|
||||||
server->hooks->free(server->hooks);
|
server->hooks->free(server->hooks);
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ file(
|
|||||||
devices/outlet_dio.c
|
devices/outlet_dio.c
|
||||||
devices/shutter.c
|
devices/shutter.c
|
||||||
devices/sprinkler.c
|
devices/sprinkler.c
|
||||||
rest/rest_handler.c
|
rest/rest_devices_handlers.c
|
||||||
|
rest/rest_server.c
|
||||||
)
|
)
|
||||||
|
|
||||||
if(NOT DOMO_BUILD_TEST)
|
if(NOT DOMO_BUILD_TEST)
|
||||||
@@ -46,6 +47,7 @@ target_link_libraries (${PROJECT_NAME}
|
|||||||
json-c
|
json-c
|
||||||
event
|
event
|
||||||
event_pthreads
|
event_pthreads
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
install (TARGETS ${PROJECT_NAME} DESTINATION local/bin)
|
install (TARGETS ${PROJECT_NAME} DESTINATION local/bin)
|
||||||
@@ -74,4 +76,5 @@ target_link_libraries (test_device
|
|||||||
event_pthreads
|
event_pthreads
|
||||||
gcov
|
gcov
|
||||||
)
|
)
|
||||||
|
|
||||||
endif(DOMO_BUILD_TEST)
|
endif(DOMO_BUILD_TEST)
|
||||||
|
|||||||
@@ -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)
|
int devices_manager_create(devices_manager_t *dm, const char *capability, struct json_object *node)
|
||||||
{
|
{
|
||||||
// Sanity checks
|
// Sanity checks
|
||||||
|
|||||||
@@ -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 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_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_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);
|
extern int devices_manager_delete(devices_manager_t *dm, const char *capability, struct json_object *node);
|
||||||
|
|||||||
@@ -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)
|
outlet_dio_t *get_outlet_by_id(devices_manager_t *dm, uint16_t id)
|
||||||
{
|
{
|
||||||
qlist_obj_t obj;
|
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)
|
shutter_t *get_shutter_by_id(devices_manager_t *dm, uint16_t id)
|
||||||
{
|
{
|
||||||
qlist_obj_t obj;
|
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)
|
sprinkler_t *get_sprinkler_by_id(devices_manager_t *dm, uint16_t id)
|
||||||
{
|
{
|
||||||
qlist_obj_t obj;
|
qlist_obj_t obj;
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
restd_server_attach_event_loop(rest_server, ev_base);
|
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.");
|
fprintf(stderr, "Failed to setup rest Server\n.");
|
||||||
restd_server_free(rest_server);
|
restd_server_free(rest_server);
|
||||||
|
|||||||
63
src/rest/rest_devices_handlers.c
Normal file
63
src/rest/rest_devices_handlers.c
Normal file
@@ -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;
|
||||||
|
}
|
||||||
40
src/rest/rest_devices_handlers.h
Normal file
40
src/rest/rest_devices_handlers.h
Normal file
@@ -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 <restd.h>
|
||||||
|
|
||||||
|
// 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 */
|
||||||
@@ -28,7 +28,9 @@
|
|||||||
|
|
||||||
/*------------------------------- INCLUDES ----------------------------------*/
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
#include "rest_handler.h"
|
#include "rest_devices_handlers.h"
|
||||||
|
|
||||||
|
#include "rest_server.h"
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
@@ -50,7 +52,7 @@ 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.port", port);
|
||||||
restd_server_set_option(rest_server, "server.root_path", root_path);
|
restd_server_set_option(rest_server, "server.root_path", root_path);
|
||||||
@@ -60,5 +62,19 @@ int setup_rest_server(restd_server_t *rest_server, const char *port, const char
|
|||||||
|
|
||||||
restd_server_register_hook(rest_server, restd_http_handler, NULL);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -23,13 +23,13 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _REST_HANDLER_H
|
#ifndef _REST_SERVER_H
|
||||||
#define _REST_HANDLER_H
|
#define _REST_SERVER_H
|
||||||
|
|
||||||
/*------------------------------- INCLUDES ----------------------------------*/
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
#include <restd.h>
|
#include <restd.h>
|
||||||
|
|
||||||
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 */
|
||||||
@@ -29,6 +29,8 @@
|
|||||||
#include <qlibc/qlibc.h>
|
#include <qlibc/qlibc.h>
|
||||||
#include <json.h>
|
#include <json.h>
|
||||||
|
|
||||||
|
#include <restd.h>
|
||||||
|
|
||||||
#include "qunit.h"
|
#include "qunit.h"
|
||||||
|
|
||||||
#include "domo.h"
|
#include "domo.h"
|
||||||
@@ -51,6 +53,7 @@ TEST("devices_manager create and free\t")
|
|||||||
#include "test_sprinkler.c"
|
#include "test_sprinkler.c"
|
||||||
#include "test_shutter.c"
|
#include "test_shutter.c"
|
||||||
#include "test_outlet.c"
|
#include "test_outlet.c"
|
||||||
|
#include "test_rest.c"
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|
||||||
|
|||||||
70
src/tests/test_rest.c
Normal file
70
src/tests/test_rest.c
Normal file
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user