From 75a3ab32f2d438c4ffe6fab929886bb324d8b827 Mon Sep 17 00:00:00 2001 From: eeff Date: Tue, 4 Jan 2022 10:52:05 +0800 Subject: [PATCH] Add dummy plugin --- CMakeLists.txt | 1 + plugins/dummy/dummy.c | 160 ++++++++++++++++++++++++++++++++++++++ plugins/dummy/dummy.h | 27 +++++++ src/core/neu_manager.c | 6 ++ src/core/plugin_manager.c | 13 ++++ src/core/plugin_manager.h | 2 + tests/CMakeLists.txt | 6 +- 7 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 plugins/dummy/dummy.c create mode 100644 plugins/dummy/dummy.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f04ec5c0..622a2d66 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,6 +108,7 @@ set(NEURON_SOURCES plugins/restful/plugin_handle.c plugins/restful/rest.c plugins/restful/http.c + plugins/dummy/dummy.c ${PERSIST_SOURCES}) set(NEURON_INCLUDE_DIRECTORIES include src plugins) diff --git a/plugins/dummy/dummy.c b/plugins/dummy/dummy.c new file mode 100644 index 00000000..954cffc3 --- /dev/null +++ b/plugins/dummy/dummy.c @@ -0,0 +1,160 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. + * + * This program 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 3 of the License, or (at your option) any later version. + * + * This program 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 program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#include +#include +#include + +#include +#include + +#include "config.h" +#include "dummy.h" +#include "neu_log.h" +#include "neu_plugin.h" + +#define neu_plugin_module default_dummy_plugin_module + +struct neu_plugin { + neu_plugin_common_t common; +}; + +static neu_plugin_t *dummy_plugin_open(neu_adapter_t * adapter, + const adapter_callbacks_t *callbacks) +{ + neu_plugin_t *plugin; + + if (adapter == NULL || callbacks == NULL) { + log_error("Open plugin with NULL adapter or callbacks"); + return NULL; + } + + plugin = (neu_plugin_t *) malloc(sizeof(neu_plugin_t)); + if (plugin == NULL) { + log_error("Failed to allocate plugin %s", + neu_plugin_module.module_name); + return NULL; + } + + neu_plugin_common_init(&plugin->common); + plugin->common.adapter = adapter; + plugin->common.adapter_callbacks = callbacks; + + log_info("Success to create plugin: %s", neu_plugin_module.module_name); + return plugin; +} + +static int dummy_plugin_close(neu_plugin_t *plugin) +{ + free(plugin); + log_info("Success to free plugin: %s", neu_plugin_module.module_name); + return 0; +} + +static int dummy_plugin_init(neu_plugin_t *plugin) +{ + int rv = 0; + + (void) plugin; + + log_info("Initialize plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dummy_plugin_uninit(neu_plugin_t *plugin) +{ + int rv = 0; + + (void) plugin; + + log_info("Uninitialize plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dummy_plugin_config(neu_plugin_t *plugin, neu_config_t *configs) +{ + int rv = 0; + + (void) plugin; + (void) configs; + + log_info("config plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dummy_plugin_request(neu_plugin_t *plugin, neu_request_t *req) +{ + int rv = 0; + + if (plugin == NULL || req == NULL) { + log_warn("The plugin pointer or request is NULL"); + return (-1); + } + + log_info("send request to plugin: %s", neu_plugin_module.module_name); + log_info("%s received request %d\n", neu_plugin_module.module_name, + req->req_type); + + return rv; +} + +static int dummy_plugin_event_reply(neu_plugin_t * plugin, + neu_event_reply_t *reply) +{ + int rv = 0; + + (void) plugin; + (void) reply; + + log_info("reply event to plugin: %s", neu_plugin_module.module_name); + return rv; +} + +static int dummy_plugin_start(neu_plugin_t *plugin) +{ + (void) plugin; + return 0; +} + +static int dummy_plugin_stop(neu_plugin_t *plugin) +{ + (void) plugin; + return 0; +} + +static const neu_plugin_intf_funs_t plugin_intf_funs = { + .open = dummy_plugin_open, + .close = dummy_plugin_close, + .init = dummy_plugin_init, + .uninit = dummy_plugin_uninit, + .start = dummy_plugin_start, + .stop = dummy_plugin_stop, + .config = dummy_plugin_config, + .request = dummy_plugin_request, + .event_reply = dummy_plugin_event_reply +}; + +#define DEFAULT_DUMMY_PLUGIN_DESCR "A plugin that does nothing" + +const neu_plugin_module_t default_dummy_plugin_module = { + .version = NEURON_PLUGIN_VER_1_0, + .module_name = "neuron-default-dummy", + .module_descr = DEFAULT_DUMMY_PLUGIN_DESCR, + .intf_funs = &plugin_intf_funs +}; diff --git a/plugins/dummy/dummy.h b/plugins/dummy/dummy.h new file mode 100644 index 00000000..e9cc4932 --- /dev/null +++ b/plugins/dummy/dummy.h @@ -0,0 +1,27 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. + * + * This program 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 3 of the License, or (at your option) any later version. + * + * This program 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 program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#ifndef _NEU_PLUGIN_DUMMY_H_ +#define _NEU_PLUGIN_DUMMY_H_ + +#include "neu_plugin.h" + +extern const neu_plugin_module_t default_dummy_plugin_module; + +#endif diff --git a/src/core/neu_manager.c b/src/core/neu_manager.c index 0a128d20..0d1092dd 100644 --- a/src/core/neu_manager.c +++ b/src/core/neu_manager.c @@ -232,6 +232,12 @@ static const plugin_reg_param_t default_plugin_infos[] = { .plugin_name = DEFAULT_DASHBOARD_PLUGIN_NAME, .plugin_lib_name = DEFAULT_DASHBOARD_PLUGIN_LIB_NAME }, + { + .plugin_kind = PLUGIN_KIND_STATIC, + .adapter_type = ADAPTER_TYPE_APP, + .plugin_name = DEFAULT_DUMMY_PLUGIN_NAME, + .plugin_lib_name = DEFAULT_DUMMY_PLUGIN_LIB_NAME + }, /* { .plugin_kind = PLUGIN_KIND_SYSTEM, diff --git a/src/core/plugin_manager.c b/src/core/plugin_manager.c index bbe7dd3c..a0981d60 100644 --- a/src/core/plugin_manager.c +++ b/src/core/plugin_manager.c @@ -29,6 +29,7 @@ #include #include "adapter/adapter_internal.h" +#include "dummy/dummy.h" #include "neu_panic.h" #include "neu_vector.h" #include "plugin_manager.h" @@ -62,6 +63,13 @@ static const plugin_reg_entity_t builtin_static_plugins[] = { .plugin_name = DEFAULT_DASHBOARD_PLUGIN_NAME, .plugin_lib_name = DEFAULT_DASHBOARD_PLUGIN_LIB_NAME, }, + { + .plugin_id = { 2 }, + .plugin_kind = PLUGIN_KIND_STATIC, + .adapter_type = ADAPTER_TYPE_APP, + .plugin_name = DEFAULT_DUMMY_PLUGIN_NAME, + .plugin_lib_name = DEFAULT_DUMMY_PLUGIN_LIB_NAME, + }, }; #define BUILTIN_STATIC_PLUGIN_COUNT \ (sizeof(builtin_static_plugins) / sizeof(builtin_static_plugins[0])) @@ -620,6 +628,11 @@ static const plugin_module_map_t plugin_module_maps[] = { { DEFAULT_DASHBOARD_PLUGIN_LIB_NAME, &default_dashboard_plugin_module, + + }, + { + DEFAULT_DUMMY_PLUGIN_LIB_NAME, + &default_dummy_plugin_module, }, }; #define PLUGIN_MODULE_MAPS_COUNT \ diff --git a/src/core/plugin_manager.h b/src/core/plugin_manager.h index c939406e..53d06eb0 100644 --- a/src/core/plugin_manager.h +++ b/src/core/plugin_manager.h @@ -32,6 +32,8 @@ extern "C" { #define DEFAULT_DASHBOARD_PLUGIN_NAME "default-dashboard-plugin" #define DEFAULT_DASHBOARD_PLUGIN_LIB_NAME "libplugin_default_dashboard" +#define DEFAULT_DUMMY_PLUGIN_NAME "default-dummy-plugin" +#define DEFAULT_DUMMY_PLUGIN_LIB_NAME "libplugin_default_dummy" typedef struct plugin_reg_param { plugin_kind_e plugin_kind; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9a91b9db..8c961a53 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -84,7 +84,11 @@ target_include_directories(data_val_test PRIVATE target_link_libraries(data_val_test neuron-base gtest_main gtest pthread jansson) aux_source_directory(${CMAKE_SOURCE_DIR}/plugins/restful SRC_RESTFUL) -add_executable(plugin_manager_test plugin_manager_test.cc ${CMAKE_SOURCE_DIR}/src/core/plugin_manager.c ${SRC_RESTFUL} ${SRC_PARSER} ${CMAKE_SOURCE_DIR}/src/config.c) +aux_source_directory(${CMAKE_SOURCE_DIR}/plugins/dummy SRC_DUMMY) +add_executable(plugin_manager_test plugin_manager_test.cc + ${CMAKE_SOURCE_DIR}/src/core/plugin_manager.c + ${SRC_RESTFUL} ${SRC_DUMMY} ${SRC_PARSER} + ${CMAKE_SOURCE_DIR}/src/config.c) target_include_directories(plugin_manager_test PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include