mirror of
https://github.com/emqx/neuron.git
synced 2026-10-17 04:40:42 +08:00
refactor plugin interface config
This commit is contained in:
@@ -93,8 +93,6 @@ set(NEURON_SOURCES
|
||||
src/adapter/adapter.c
|
||||
src/adapter/driver/cache.c
|
||||
src/adapter/driver/driver.c
|
||||
src/adapter/config/config.c
|
||||
src/adapter/control/control.c
|
||||
plugins/restful/handle.c
|
||||
plugins/restful/license.c
|
||||
plugins/restful/license_handle.c
|
||||
|
||||
@@ -38,21 +38,9 @@ typedef enum {
|
||||
|
||||
#include "adapter_msg.h"
|
||||
|
||||
typedef struct neu_adapter neu_adapter_t;
|
||||
typedef struct neu_adapter_driver neu_adapter_driver_t;
|
||||
typedef struct neu_adapter_config neu_adapter_config_t;
|
||||
typedef struct neu_adapter_control neu_adapter_control_t;
|
||||
|
||||
typedef enum neu_config_type {
|
||||
NEU_CONFIG_UNKNOW,
|
||||
NEU_CONFIG_SETTING,
|
||||
} neu_config_type_e;
|
||||
|
||||
typedef struct neu_config {
|
||||
neu_config_type_e type;
|
||||
uint32_t buf_len;
|
||||
void * buf;
|
||||
} neu_config_t;
|
||||
typedef struct neu_adapter neu_adapter_t;
|
||||
typedef struct neu_adapter_driver neu_adapter_driver_t;
|
||||
typedef struct neu_adapter_app neu_adapter_app_t;
|
||||
|
||||
typedef enum {
|
||||
NEU_NODE_LINK_STATUS_DISCONNECTED = 0,
|
||||
@@ -78,9 +66,6 @@ typedef struct adapter_callbacks {
|
||||
int (*response)(neu_adapter_t *adapter, neu_reqresp_head_t *head,
|
||||
void *data);
|
||||
|
||||
void (*link_status)(neu_adapter_t * adapter,
|
||||
neu_node_link_status_e link_status);
|
||||
|
||||
union {
|
||||
struct {
|
||||
void (*update)(neu_adapter_t *adapter, const char *group,
|
||||
|
||||
@@ -65,7 +65,7 @@ typedef struct neu_plugin_intf_funs {
|
||||
int (*uninit)(neu_plugin_t *plugin);
|
||||
int (*start)(neu_plugin_t *plugin);
|
||||
int (*stop)(neu_plugin_t *plugin);
|
||||
int (*config)(neu_plugin_t *plugin, neu_config_t *configs);
|
||||
int (*setting)(neu_plugin_t *plugin, const char *setting);
|
||||
|
||||
int (*request)(neu_plugin_t *plugin, neu_reqresp_head_t *head, void *data);
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ static int dummy_plugin_uninit(neu_plugin_t *plugin)
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int dummy_plugin_config(neu_plugin_t *plugin, neu_config_t *configs)
|
||||
static int dummy_plugin_config(neu_plugin_t *plugin, const char *configs)
|
||||
{
|
||||
int rv = 0;
|
||||
|
||||
@@ -125,7 +125,7 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
.uninit = dummy_plugin_uninit,
|
||||
.start = dummy_plugin_start,
|
||||
.stop = dummy_plugin_stop,
|
||||
.config = dummy_plugin_config,
|
||||
.setting = dummy_plugin_config,
|
||||
.request = dummy_plugin_request,
|
||||
};
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@ static int ekuiper_plugin_stop(neu_plugin_t *plugin)
|
||||
return NEU_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
static int ekuiper_plugin_config(neu_plugin_t *plugin, neu_config_t *configs)
|
||||
static int ekuiper_plugin_config(neu_plugin_t *plugin, const char *configs)
|
||||
{
|
||||
int rv = 0;
|
||||
|
||||
@@ -208,7 +208,7 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
.uninit = ekuiper_plugin_uninit,
|
||||
.start = ekuiper_plugin_start,
|
||||
.stop = ekuiper_plugin_stop,
|
||||
.config = ekuiper_plugin_config,
|
||||
.setting = ekuiper_plugin_config,
|
||||
.request = ekuiper_plugin_request,
|
||||
};
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ static int driver_init(neu_plugin_t *plugin);
|
||||
static int driver_uninit(neu_plugin_t *plugin);
|
||||
static int driver_start(neu_plugin_t *plugin);
|
||||
static int driver_stop(neu_plugin_t *plugin);
|
||||
static int driver_config(neu_plugin_t *plugin, neu_config_t *config);
|
||||
static int driver_config(neu_plugin_t *plugin, const char *config);
|
||||
static int driver_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
|
||||
void *data);
|
||||
|
||||
@@ -48,7 +48,7 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
.uninit = driver_uninit,
|
||||
.start = driver_start,
|
||||
.stop = driver_stop,
|
||||
.config = driver_config,
|
||||
.setting = driver_config,
|
||||
.request = driver_request,
|
||||
|
||||
.driver.validate_tag = driver_validate_tag,
|
||||
@@ -125,7 +125,7 @@ static int driver_stop(neu_plugin_t *plugin)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int driver_config(neu_plugin_t *plugin, neu_config_t *config)
|
||||
static int driver_config(neu_plugin_t *plugin, const char *config)
|
||||
{
|
||||
int ret = 0;
|
||||
char * err_param = NULL;
|
||||
@@ -134,10 +134,11 @@ static int driver_config(neu_plugin_t *plugin, neu_config_t *config)
|
||||
neu_json_elem_t host = { .name = "host", .t = NEU_JSON_STR };
|
||||
neu_conn_param_t param = { 0 };
|
||||
|
||||
ret = neu_parse_param(config->buf, &err_param, 3, &port, &host, &timeout);
|
||||
ret =
|
||||
neu_parse_param((char *) config, &err_param, 3, &port, &host, &timeout);
|
||||
|
||||
if (ret != 0) {
|
||||
plog_warn(plugin, "config: %s, decode error: %s", (char *) config->buf,
|
||||
plog_warn(plugin, "config: %s, decode error: %s", (char *) config,
|
||||
err_param);
|
||||
free(err_param);
|
||||
free(host.v.val_str);
|
||||
|
||||
+4
-4
@@ -408,7 +408,7 @@ static void *mqtt_routine_loop(void *argument)
|
||||
}
|
||||
|
||||
static mqtt_routine_t *mqtt_routine_start(neu_plugin_t *plugin,
|
||||
neu_config_t *config)
|
||||
const char * config)
|
||||
{
|
||||
assert(NULL != plugin);
|
||||
assert(NULL != config);
|
||||
@@ -419,7 +419,7 @@ static mqtt_routine_t *mqtt_routine_start(neu_plugin_t *plugin,
|
||||
}
|
||||
|
||||
// MQTT client start
|
||||
int rc = mqtt_option_init(config, &routine->option);
|
||||
int rc = mqtt_option_init((char *) config, &routine->option);
|
||||
if (0 != rc) {
|
||||
mqtt_option_uninit(&routine->option);
|
||||
free(routine);
|
||||
@@ -572,7 +572,7 @@ static int mqtt_plugin_uninit(neu_plugin_t *plugin)
|
||||
return NEU_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
static int mqtt_plugin_config(neu_plugin_t *plugin, neu_config_t *config)
|
||||
static int mqtt_plugin_config(neu_plugin_t *plugin, const char *config)
|
||||
{
|
||||
assert(NULL != plugin);
|
||||
assert(NULL != config);
|
||||
@@ -761,7 +761,7 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
.uninit = mqtt_plugin_uninit,
|
||||
.start = mqtt_plugin_start,
|
||||
.stop = mqtt_plugin_stop,
|
||||
.config = mqtt_plugin_config,
|
||||
.setting = mqtt_plugin_config,
|
||||
.request = mqtt_plugin_request,
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -86,7 +86,7 @@ void mqtt_option_uninit(neu_mqtt_option_t *option)
|
||||
}
|
||||
}
|
||||
|
||||
int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option)
|
||||
int mqtt_option_init(char *config, neu_mqtt_option_t *option)
|
||||
{
|
||||
if (NULL == config || NULL == option) {
|
||||
return -1;
|
||||
@@ -107,7 +107,7 @@ int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option)
|
||||
neu_json_elem_t key = { .name = "key", .t = NEU_JSON_STR };
|
||||
neu_json_elem_t keypass = { .name = "keypass", .t = NEU_JSON_STR };
|
||||
|
||||
ret = neu_parse_param(config->buf, &error, 12, &id, &upload, &format, &ssl,
|
||||
ret = neu_parse_param(config, &error, 12, &id, &upload, &format, &ssl,
|
||||
&host, &port, &username, &password, &ca, &cert, &key,
|
||||
&keypass);
|
||||
if (0 != ret) {
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ extern "C" {
|
||||
#include "connection/mqtt_client_intf.h"
|
||||
#include <neuron.h>
|
||||
|
||||
int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option);
|
||||
int mqtt_option_init(char *config, neu_mqtt_option_t *option);
|
||||
void mqtt_option_uninit(neu_mqtt_option_t *option);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -241,7 +241,7 @@ static int dashb_plugin_uninit(neu_plugin_t *plugin)
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int dashb_plugin_config(neu_plugin_t *plugin, neu_config_t *configs)
|
||||
static int dashb_plugin_config(neu_plugin_t *plugin, const char *configs)
|
||||
{
|
||||
int rv = 0;
|
||||
|
||||
@@ -324,7 +324,7 @@ static const neu_plugin_intf_funs_t plugin_intf_funs = {
|
||||
.uninit = dashb_plugin_uninit,
|
||||
.start = dashb_plugin_start,
|
||||
.stop = dashb_plugin_stop,
|
||||
.config = dashb_plugin_config,
|
||||
.setting = dashb_plugin_config,
|
||||
.request = dashb_plugin_request,
|
||||
};
|
||||
|
||||
|
||||
+10
-11
@@ -542,8 +542,8 @@ void neu_adapter_destroy(neu_adapter_t *adapter)
|
||||
if (adapter->name != NULL) {
|
||||
free(adapter->name);
|
||||
}
|
||||
if (NULL != adapter->setting.buf) {
|
||||
free(adapter->setting.buf);
|
||||
if (NULL != adapter->setting) {
|
||||
free(adapter->setting);
|
||||
}
|
||||
|
||||
nng_mtx_free(adapter->nng.mtx);
|
||||
@@ -832,21 +832,20 @@ neu_adapter_type_e neu_adapter_get_type(neu_adapter_t *adapter)
|
||||
return (neu_adapter_type_e) adapter->plugin_info.module->type;
|
||||
}
|
||||
|
||||
int neu_adapter_set_setting(neu_adapter_t *adapter, neu_config_t *config)
|
||||
int neu_adapter_set_setting(neu_adapter_t *adapter, const char *setting)
|
||||
{
|
||||
int rv = -1;
|
||||
|
||||
const neu_plugin_intf_funs_t *intf_funs;
|
||||
|
||||
intf_funs = adapter->plugin_info.module->intf_funs;
|
||||
rv = intf_funs->config(adapter->plugin_info.plugin, config);
|
||||
rv = intf_funs->setting(adapter->plugin_info.plugin, setting);
|
||||
if (rv == 0) {
|
||||
adapter->setting.buf_len = config->buf_len;
|
||||
adapter->setting.type = config->type;
|
||||
if (adapter->setting != NULL) {
|
||||
free(adapter->setting);
|
||||
}
|
||||
adapter->setting = strdup(setting);
|
||||
|
||||
if (adapter->setting.buf != NULL)
|
||||
free(adapter->setting.buf);
|
||||
adapter->setting.buf = strdup(config->buf);
|
||||
if (adapter->state == ADAPTER_STATE_INIT) {
|
||||
adapter->state = ADAPTER_STATE_READY;
|
||||
}
|
||||
@@ -864,8 +863,8 @@ int neu_adapter_get_setting(neu_adapter_t *adapter, char **config)
|
||||
return NEU_ERR_NODE_NOT_EXIST;
|
||||
}
|
||||
|
||||
if (adapter->setting.buf != NULL) {
|
||||
*config = strdup(adapter->setting.buf);
|
||||
if (adapter->setting != NULL) {
|
||||
*config = strdup(adapter->setting);
|
||||
return NEU_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -48,8 +48,7 @@ struct neu_adapter {
|
||||
uint32_t req_id;
|
||||
|
||||
adapter_callbacks_t cb_funs;
|
||||
neu_config_t setting;
|
||||
void * x;
|
||||
char * setting;
|
||||
|
||||
struct {
|
||||
void * handle; // handle of dynamic lib
|
||||
@@ -84,7 +83,7 @@ void neu_adapter_del_timer(neu_adapter_t *adapter, neu_event_timer_t *timer);
|
||||
const char * neu_adapter_get_name(neu_adapter_t *adapter);
|
||||
neu_manager_t * neu_adapter_get_manager(neu_adapter_t *adapter);
|
||||
neu_adapter_type_e neu_adapter_get_type(neu_adapter_t *adapter);
|
||||
int neu_adapter_set_setting(neu_adapter_t *adapter, neu_config_t *config);
|
||||
int neu_adapter_set_setting(neu_adapter_t *adapter, const char *config);
|
||||
int neu_adapter_get_setting(neu_adapter_t *adapter, char **config);
|
||||
neu_plugin_state_t neu_adapter_get_state(neu_adapter_t *adapter);
|
||||
neu_plugin_running_state_e
|
||||
|
||||
@@ -15,5 +15,4 @@
|
||||
* 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 "config_internal.h"
|
||||
**/
|
||||
@@ -15,5 +15,4 @@
|
||||
* 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 "control_internal.h"
|
||||
**/
|
||||
@@ -1,23 +0,0 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2022 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_ADAPTER_CONFIG_INTERNAL_H_
|
||||
#define _NEU_ADAPTER_CONFIG_INTERNAL_H_
|
||||
|
||||
#endif
|
||||
@@ -1,23 +0,0 @@
|
||||
/**
|
||||
* NEURON IIoT System for Industry 4.0
|
||||
* Copyright (C) 2020-2022 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_ADAPTER_CONTROL_INTERNAL_H_
|
||||
#define _NEU_ADAPTER_CONTROL_INTERNAL_H_
|
||||
|
||||
#endif
|
||||
@@ -345,17 +345,12 @@ int neu_manager_node_ctl(neu_manager_t *manager, const char *node,
|
||||
int neu_manager_node_setting(neu_manager_t *manager, const char *node,
|
||||
const char *setting)
|
||||
{
|
||||
neu_config_t config = { 0 };
|
||||
neu_adapter_t *adapter = neu_node_manager_find(manager->node_manager, node);
|
||||
if (adapter == NULL) {
|
||||
return NEU_ERR_NODE_NOT_EXIST;
|
||||
}
|
||||
|
||||
config.type = NEU_CONFIG_SETTING;
|
||||
config.buf = (char *) setting;
|
||||
config.buf_len = strlen(setting);
|
||||
|
||||
return neu_adapter_set_setting(adapter, &config);
|
||||
return neu_adapter_set_setting(adapter, setting);
|
||||
}
|
||||
|
||||
int neu_manager_node_get_setting(neu_manager_t *manager, const char *node,
|
||||
|
||||
Reference in New Issue
Block a user