diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 1bd92d5d..305bc925 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -79,5 +79,5 @@ } } ], - "version": 3 + "version": 4 } \ No newline at end of file diff --git a/bsp/board/domo/ovl/usr/local/configs/restd/v1-lights.json b/bsp/board/domo/ovl/usr/local/configs/restd/v1-outlets.json similarity index 60% rename from bsp/board/domo/ovl/usr/local/configs/restd/v1-lights.json rename to bsp/board/domo/ovl/usr/local/configs/restd/v1-outlets.json index d428a4ac..c8e13059 100644 --- a/bsp/board/domo/ovl/usr/local/configs/restd/v1-lights.json +++ b/bsp/board/domo/ovl/usr/local/configs/restd/v1-outlets.json @@ -1,43 +1,43 @@ -// Lights +// Outlets { "resources": [ { "model": "post", - "endpoint": "/v1/lights", + "endpoint": "/v1/outlets", "ubus": { - "path": "domo.lights", + "path": "domo.outlets", "method": "create" } }, { "model": "get", - "endpoint": "/v1/lights", + "endpoint": "/v1/outlets", "ubus": { - "path": "domo.lights", + "path": "domo.outlets", "method": "list" } }, { "model": "get", - "endpoint": "/v1/lights/:id", + "endpoint": "/v1/outlets/:id", "ubus": { - "path": "domo.lights", + "path": "domo.outlets", "method": "read" } }, { "model": "put", - "endpoint": "/v1/lights/:id", + "endpoint": "/v1/outlets/:id", "ubus": { - "path": "domo.lights", + "path": "domo.outlets", "method": "update" } }, { "model": "delete", - "endpoint": "/v1/lights/:id", + "endpoint": "/v1/outlets/:id", "ubus": { - "path": "domo.lights", + "path": "domo.outlets", "method": "delete" } } diff --git a/bsp/board/domo/ovl/usr/local/configs/restd/v1-shutters.json b/bsp/board/domo/ovl/usr/local/configs/restd/v1-shutters.json index e3f79822..5d5c6e65 100644 --- a/bsp/board/domo/ovl/usr/local/configs/restd/v1-shutters.json +++ b/bsp/board/domo/ovl/usr/local/configs/restd/v1-shutters.json @@ -40,7 +40,7 @@ "path": "domo.shutters", "method": "delete" } - } + }, { "model": "put", "endpoint": "/v1/shutters/:id/up", diff --git a/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.cpp b/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.cpp index 1418339b..a14dc190 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.cpp @@ -100,7 +100,7 @@ WebConnection *WebController::new_connection(struct uhttpd_ops *an_ops, struct c * * @brief return true if the controller match with the url. */ -bool WebController::find(const std::string &an_url) +bool WebController::find(uint8_t a_method, const std::string &an_url) { return true; } diff --git a/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.h b/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.h index bf6cc48b..d951a2f0 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.h +++ b/src/plugins/uhttpd/uhttpd-rest-api/core/web-controller.h @@ -44,7 +44,7 @@ class WebController WebController(const std::string &a_path); virtual ~WebController(void); - virtual bool find(const std::string &an_url); + virtual bool find(uint8_t a_method, const std::string &an_url); void set_name(const std::string &a_name); const std::string &get_name(void); diff --git a/src/plugins/uhttpd/uhttpd-rest-api/plugin.c b/src/plugins/uhttpd/uhttpd-rest-api/plugin.c index 29ae4ca0..cf9cb3d3 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/plugin.c +++ b/src/plugins/uhttpd/uhttpd-rest-api/plugin.c @@ -49,11 +49,11 @@ static struct dispatch_handler g_ubus_dispatch = { }; /*! ---------------------------------------------------------------------------- - * @fn uh_awox_api_plugin_init + * @fn uh_rest_api_plugin_init * * @brief */ -static int uh_awox_api_plugin_init(const struct uhttpd_ops *an_ops, struct config *a_conf) +static int uh_rest_api_plugin_init(const struct uhttpd_ops *an_ops, struct config *a_conf) { struct ubus_context *the_ctx; @@ -72,11 +72,11 @@ static int uh_awox_api_plugin_init(const struct uhttpd_ops *an_ops, struct confi } /*! ---------------------------------------------------------------------------- - * @fn uh_awox_api_plugin_post_init + * @fn uh_rest_api_plugin_post_init * * @brief */ -static void uh_awox_api_plugin_post_init(void) +static void uh_rest_api_plugin_post_init(void) { ubus_add_uloop(get_uhttp_server_ctx()); } @@ -85,6 +85,6 @@ static void uh_awox_api_plugin_post_init(void) struct uhttpd_plugin uhttpd_plugin = { - .init = uh_awox_api_plugin_init, - .post_init = uh_awox_api_plugin_post_init, + .init = uh_rest_api_plugin_init, + .post_init = uh_rest_api_plugin_post_init, }; diff --git a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp index 00e47c1f..2b8979df 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.cpp @@ -22,6 +22,8 @@ /*------------------------------- INCLUDES ----------------------------------*/ +#include + #include #include "rest/rest-connection.h" @@ -108,17 +110,7 @@ bool RestController::from_json(struct json_object *a_node) // endpoint if (json_object_object_get_ex(a_node, kControllerEndpointKey, &the_value_node)) { - int the_len, i; - struct json_object *the_ep_node; - the_len = json_object_array_length(the_value_node); - - for (i = 0; i < the_len; i++) - { - std::string the_endpoint; - the_ep_node = json_object_array_get_idx(the_value_node, i); - the_endpoint = json_object_get_string(the_ep_node); - m_endpoint.push_back(the_endpoint); - } + m_endpoint = json_object_get_string(the_value_node); } // ubus @@ -144,21 +136,28 @@ bool RestController::from_json(struct json_object *a_node) * * @brief return true if the controller match with the url. */ -bool RestController::find(const std::string &an_url) +bool RestController::find(uint8_t a_method, const std::string &an_url) { std::vector::iterator the_it; + std::size_t the_pos; - printf("find : %s\n", an_url.c_str()); + printf("find : url: %s, endpoint:%s \n", an_url.c_str(), m_endpoint.c_str()); - for (the_it = m_endpoint.begin(); the_it != m_endpoint.end(); ++the_it) + if ((m_endpoint == an_url) && (m_method == a_method)) { - int the_error; - the_error = (*the_it).compare(an_url); - printf("compare <%s> et <%s> %d\n", (*the_it).c_str(), an_url.c_str(), the_error); - if (the_error == 0) - return true; + return true; } + the_pos = m_endpoint.find_first_of(":"); + if (the_pos == std::string::npos) + { + return false; + } + + printf("ya un param.\n"); + std::string the_endpoint = m_endpoint.substr(the_pos); + printf("endpoint: %s\n", the_endpoint.c_str()); + return false; } @@ -172,6 +171,16 @@ const Ubus &RestController::get_ubus(void) const return m_ubus; } +/*! ---------------------------------------------------------------------------- + * @fn get_endpoint + * + * @brief return the endpoint for this controller. + */ + +const std::string &RestController::get_endpoint(void) const +{ + return m_endpoint; +} /*! ---------------------------------------------------------------------------- * @fn is_raw_response diff --git a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h index eeb0e567..16992ff1 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h +++ b/src/plugins/uhttpd/uhttpd-rest-api/rest/rest-controller.h @@ -27,7 +27,6 @@ #include #include -#include #include #include @@ -56,9 +55,10 @@ class RestController : public WebController bool from_json(struct json_object *a_node); - bool find(const std::string &an_url); + bool find(uint8_t a_method, const std::string &an_url); const Ubus &get_ubus(void) const; + const std::string &get_endpoint(void) const; bool is_raw_response(void); @@ -71,7 +71,7 @@ class RestController : public WebController */ protected: uint8_t m_method; - std::vector m_endpoint; + std::string m_endpoint; Ubus m_ubus; bool mf_raw_response; diff --git a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp index 16181640..27ca6f60 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp +++ b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.cpp @@ -59,7 +59,7 @@ extern "C" { /*------------------------------- GLOBALS ----------------------------------*/ -#define kControllerKey "controller" +#define kResourcesKey "resources" #define kEtagControllerKey "etag_controller" #define kNotificationKey "notification" @@ -242,12 +242,6 @@ UhttpServer::~UhttpServer(void) { ConnectionIterator the_cnx_it; - for (std::vector< WebController *>::iterator the_it = m_controllers.begin(); - the_it != m_controllers.end(); ++the_it) - { - delete *the_it; - } - m_controllers.clear(); for (the_cnx_it = m_connections.begin(); the_cnx_it != m_connections.end(); ++the_cnx_it) @@ -288,12 +282,12 @@ struct ubus_context *UhttpServer::get_context(void) */ bool UhttpServer::check_url(const std::string &an_url) { - std::vector::iterator the_it; + ControllerIterator the_it; printf("UhttpServer::check_url: %s\n", an_url.c_str()); for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it) { - if ( (*the_it)->find(an_url)) + if ( the_it->second.get()->find(0, an_url)) { return true; } @@ -328,7 +322,7 @@ void UhttpServer::handle_request(struct client *a_cl, const std::string &an_url, the_url = an_url; } - the_controller = get_controller(the_url); + the_controller = get_controller(a_cl->request.method, the_url); if (the_controller == NULL) { printf ("not found :( \n"); @@ -474,7 +468,7 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node) int the_len; int i; - if (json_object_object_get_ex(a_root_node, kControllerKey, &the_ctr_array_node)) + if (json_object_object_get_ex(a_root_node, kResourcesKey, &the_ctr_array_node)) { RestController *the_rest_controller; the_len = json_object_array_length(the_ctr_array_node); @@ -487,7 +481,7 @@ int UhttpServer::add_controller_from_json(struct json_object *a_root_node) if (the_rest_controller->from_json(the_ctrl_node)) { //new RestController(the_model_name, the_get_method, the_set_method, kDefaultTimeout, the_raw_response) - add_controller("", the_rest_controller); + add_controller(the_rest_controller->get_endpoint(), the_rest_controller); } } } @@ -660,18 +654,12 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont { std::string the_path; std::size_t the_pos; + if (a_controller == NULL) return -1; - if (an_uri.empty()) - { - m_controllers.push_back(a_controller); - } - else { - - a_controller->set_name(an_uri); - // TODO - } + a_controller->set_name(an_uri); + m_controllers[an_uri] = std::unique_ptr(a_controller); //m_controller_array[an_uri] = a_controller; @@ -698,17 +686,17 @@ int UhttpServer::add_controller(const std::string &an_uri, WebController *a_cont * * @brief Send an error message to the client. */ -WebController *UhttpServer::get_controller(const std::string &an_url) +WebController *UhttpServer::get_controller(uint8_t a_method, const std::string &an_url) { - std::vector::iterator the_it; + ControllerIterator the_it; WebController *the_controller = NULL; for (the_it = m_controllers.begin(); the_it != m_controllers.end(); ++the_it) { - if ((*the_it)->find(an_url)) + if (the_it->second.get()->find(a_method, an_url)) { printf ("return the controller.\n"); - return (*the_it); + return the_it->second.get(); } } diff --git a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h index 9cccdf2b..18e8e653 100644 --- a/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h +++ b/src/plugins/uhttpd/uhttpd-rest-api/uhttp-server.h @@ -26,6 +26,7 @@ /*------------------------------- INCLUDES ----------------------------------*/ #include +#include #include #include @@ -45,7 +46,7 @@ class WebConnection; class UhttpServer { public: - typedef std::unordered_map ControllerContainer; + typedef std::unordered_map> ControllerContainer; typedef ControllerContainer::iterator ControllerIterator; typedef std::unordered_map ConnectionContainer; typedef ConnectionContainer::iterator ConnectionIterator; @@ -75,13 +76,13 @@ class UhttpServer std::string &a_get_method, std::string &a_set_method, bool &a_raw_response); struct json_object *load(const std::string &a_file); int add_controller(const std::string &an_uri, WebController *a_controller); - WebController *get_controller(const std::string &an_url); + WebController *get_controller(uint8_t a_method, const std::string &an_url); void send_error(struct client *a_cl, int a_code, const char *a_summary, const std::string &a_msg); private: struct ubus_context *m_ctx; struct uhttpd_ops *m_ops; - std::vector< WebController *> m_controllers; + ControllerContainer m_controllers; ConnectionContainer m_connections; std::list m_path_list; };