Skip to content

Commit

Permalink
Merge pull request #2009 from gc87/scan-tags-v2.9
Browse files Browse the repository at this point in the history
[v2.9]feat: add scan tags reqest & response
  • Loading branch information
fengzeroz committed May 24, 2024
2 parents 87acec1 + ba07160 commit 0512edb
Show file tree
Hide file tree
Showing 18 changed files with 431 additions and 9 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ set(NEURON_SOURCES
plugins/restful/group_config_handle.c
plugins/restful/plugin_handle.c
plugins/restful/version_handle.c
plugins/restful/scan_handle.c
plugins/restful/rest.c
plugins/restful/user.c)

Expand Down
3 changes: 3 additions & 0 deletions include/neuron/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ typedef struct adapter_callbacks {
void (*update_im)(neu_adapter_t *adapter, const char *group,
const char *tag, neu_dvalue_t value,
neu_tag_meta_t *metas, int n_meta);
void (*scan_tags_response)(neu_adapter_t *adapter, void *r,
int error, void *tags, neu_type_e type,
bool is_array);
} driver;
};
} adapter_callbacks_t;
Expand Down
1 change: 1 addition & 0 deletions include/neuron/errcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ typedef enum {
NEU_ERR_PLUGIN_NOT_SUPPORT_READ_SYNC = 3018,
NEU_ERR_PLUGIN_TYPE_NOT_SUPPORT = 3019,
NEU_ERR_PLUGIN_TAG_VALUE_OUT_OF_RANGE = 3020,
NEU_ERR_PLUGIN_NOT_SUPPORT_SCAN_TAGS = 3021,

NEU_ERR_MQTT_FAILURE = 4000,
NEU_ERR_MQTT_NO_CERTFILESET = 4001,
Expand Down
51 changes: 51 additions & 0 deletions include/neuron/json/neu_json_scan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.
**/

/*
* DO NOT EDIT THIS FILE MANUALLY!
* It was automatically generated by `json-autotype`.
*/

#ifndef _NEU_JSON_API_NEU_JSON_SCAN_H_
#define _NEU_JSON_API_NEU_JSON_SCAN_H_

#include "json/json.h"

#include "tag.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
char *node;
char *id;
} neu_json_scan_tags_req_t;

int neu_json_decode_scan_tags_req(char *buf, neu_json_scan_tags_req_t **result);

void neu_json_decode_scan_tags_req_free(neu_json_scan_tags_req_t *req);

int neu_json_encode_scan_tags_resp(void *json_object, void *param);

#ifdef __cplusplus
}
#endif

#endif
29 changes: 29 additions & 0 deletions include/neuron/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ typedef enum neu_reqresp_type {
NEU_REQ_PRGFILE_PROCESS,
NEU_RESP_PRGFILE_PROCESS,

NEU_REQ_SCAN_TAGS,
NEU_RESP_SCAN_TAGS,
} neu_reqresp_type_e;

static const char *neu_reqresp_type_string_t[] = {
Expand Down Expand Up @@ -189,6 +191,9 @@ static const char *neu_reqresp_type_string_t[] = {
[NEU_REQ_PRGFILE_UPLOAD] = "NEU_REQ_PRGFILE_UPLOAD",
[NEU_REQ_PRGFILE_PROCESS] = "NEU_REQ_PRGFILE_PROCESS",
[NEU_RESP_PRGFILE_PROCESS] = "NEU_RESP_PRGFILE_PROCESS",

[NEU_REQ_SCAN_TAGS] = "NEU_REQ_SCAN_TAGS",
[NEU_RESP_SCAN_TAGS] = "NEU_RESP_SCAN_TAGS",
};

inline static const char *neu_reqresp_type_string(neu_reqresp_type_e type)
Expand Down Expand Up @@ -923,6 +928,30 @@ typedef struct {
char reason[256];
} neu_resp_prgfile_process_t;

typedef struct neu_req_scan_tags {
char driver[NEU_NODE_NAME_LEN];
char id[NEU_TAG_ADDRESS_LEN];
} neu_req_scan_tags_t;

typedef struct {
char name[NEU_TAG_NAME_LEN];
char id[5 + 1 + NEU_TAG_ADDRESS_LEN]; // ns + ! + address
uint8_t type;
} neu_scan_tag_t;

typedef struct {
UT_array * scan_tags;
int32_t error;
neu_type_e type;
bool is_array;
} neu_resp_scan_tags_t;

typedef struct {
char name[NEU_TAG_NAME_LEN];
char id[5 + 1 + NEU_TAG_ADDRESS_LEN];
neu_type_e type;
} neu_scan_tag_attribute_t;

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions include/neuron/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ typedef struct neu_plugin_intf_funs {
neu_datatag_t *tags,
int n_tag); // create tags by API
int (*del_tags)(neu_plugin_t *plugin, int n_tag);
int (*scan_tags)(neu_plugin_t *plugin, void *req, char *id);
} driver;
};

Expand Down
10 changes: 10 additions & 0 deletions plugins/restful/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "normal_handle.h"
#include "plugin_handle.h"
#include "rw_handle.h"
#include "scan_handle.h"
#include "utils/http.h"
#include "version_handle.h"

Expand Down Expand Up @@ -117,6 +118,9 @@ static struct neu_http_handler cors_handler[] = {
{
.url = "/api/v2/metrics",
},
{
.url = "/api/v2/scan/tags",
},
};

static struct neu_http_handler rest_handlers[] = {
Expand Down Expand Up @@ -379,6 +383,12 @@ static struct neu_http_handler rest_handlers[] = {
.url = "/api/v2/metrics",
.value.handler = handle_get_metric,
},
{
.method = NEU_HTTP_METHOD_POST,
.type = NEU_HTTP_HANDLER_FUNCTION,
.url = "/api/v2/scan/tags",
.value.handler = handle_scan_tags,
},
};

void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size)
Expand Down
4 changes: 4 additions & 0 deletions plugins/restful/rest.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "plugin_handle.h"
#include "rest.h"
#include "rw_handle.h"
#include "scan_handle.h"
#include "utils/http.h"
#include "utils/log.h"
#include "utils/neu_jwt.h"
Expand Down Expand Up @@ -223,6 +224,9 @@ static int dashb_plugin_request(neu_plugin_t * plugin,
case NEU_RESP_READ_GROUP:
handle_read_resp(header->ctx, (neu_resp_read_group_t *) data);
break;
case NEU_RESP_SCAN_TAGS:
handle_scan_tags_resp(header->ctx, (neu_resp_scan_tags_t *) data);
break;
default:
nlog_fatal("recv unhandle msg: %s",
neu_reqresp_type_string(header->type));
Expand Down
68 changes: 68 additions & 0 deletions plugins/restful/scan_handle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdlib.h>

#include "plugin.h"
#include "utils/log.h"
#include "json/neu_json_fn.h"
#include "json/neu_json_scan.h"

#include "handle.h"
#include "utils/http.h"

#include "scan_handle.h"

void handle_scan_tags(nng_aio *aio)
{
neu_plugin_t *plugin = neu_rest_get_plugin();

NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
aio, neu_json_scan_tags_req_t, neu_json_decode_scan_tags_req, {
neu_reqresp_head_t header = { 0 };
neu_req_scan_tags_t cmd = { 0 };
int err_type;

nng_http_req *nng_req = nng_aio_get_input(aio, 0);
nlog_notice("<%p> req %s %s", aio, nng_http_req_get_method(nng_req),
nng_http_req_get_uri(nng_req));
header.ctx = aio;
header.type = NEU_REQ_SCAN_TAGS;

if (NULL != req->node && NEU_NODE_NAME_LEN <= strlen(req->node)) {
err_type = NEU_ERR_NODE_NAME_TOO_LONG;
goto error;
}

if (NULL != req->id && NEU_TAG_ADDRESS_LEN <= strlen(req->id)) {
err_type = NEU_ERR_NODE_NAME_TOO_LONG;
goto error;
}

if (NULL != req->node) {
strcpy(cmd.driver, req->node);
}

if (NULL != req->id) {
strcpy(cmd.id, req->id);
}

if (0 != neu_plugin_op(plugin, header, &cmd)) {
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
});
}
goto success;

error:
NEU_JSON_RESPONSE_ERROR(
err_type, { neu_http_response(aio, err_type, result_error); });

success:;
})
}

void handle_scan_tags_resp(nng_aio *aio, neu_resp_scan_tags_t *resp)
{
char *result = NULL;
neu_json_encode_by_fn(resp, neu_json_encode_scan_tags_resp, &result);
neu_http_ok(aio, result);
free(result);
}
29 changes: 29 additions & 0 deletions plugins/restful/scan_handle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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_SCAN_HANDLE_H_
#define _NEU_ACAN_HANDLE_H_

#include <nng/nng.h>

void handle_scan_tags(nng_aio *aio);
void handle_scan_tags_resp(nng_aio *aio, neu_resp_scan_tags_t *resp);
void handle_scan_tag(nng_aio *aio);

#endif
15 changes: 15 additions & 0 deletions src/adapter/adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ static int adapter_command(neu_adapter_t *adapter, neu_reqresp_head_t header,
strcpy(pheader->receiver, header.sender);
break;
}
case NEU_REQ_SCAN_TAGS: {
neu_req_scan_tags_t *cmd = (neu_req_scan_tags_t *) data;
strcpy(pheader->receiver, cmd->driver);
break;
}
default:
break;
}
Expand Down Expand Up @@ -1181,6 +1186,16 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
adapter->plugin, (neu_reqresp_head_t *) header, &header[1]);
break;
}
case NEU_REQ_SCAN_TAGS: {
neu_adapter_driver_scan_tags((neu_adapter_driver_t *) adapter, header);
break;
}
case NEU_RESP_SCAN_TAGS: {
adapter->module->intf_funs->request(
adapter->plugin, (neu_reqresp_head_t *) header, &header[1]);
neu_msg_free(msg);
break;
}
default:
nlog_warn("adapter: %s recv msg type error, type: %s", adapter->name,
neu_reqresp_type_string(header->type));
Expand Down
57 changes: 50 additions & 7 deletions src/adapter/driver/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,16 +322,33 @@ static void update(neu_adapter_t *adapter, const char *group, const char *tag,
update_with_meta(adapter, group, tag, value, NULL, 0);
}

static void scan_tags_response(neu_adapter_t *adapter, void *r, neu_error error,
void *tags, neu_type_e type, bool is_array)
{
neu_reqresp_head_t * req = (neu_reqresp_head_t *) r;
neu_resp_scan_tags_t value = {
.scan_tags = tags,
.error = error,
.type = type,
.is_array = is_array,
};
req->type = NEU_RESP_SCAN_TAGS;
nlog_notice("scan tags response <%p>", req->ctx);

adapter->cb_funs.response(adapter, req, &value);
}

neu_adapter_driver_t *neu_adapter_driver_create()
{
neu_adapter_driver_t *driver = calloc(1, sizeof(neu_adapter_driver_t));

driver->cache = neu_driver_cache_new();
driver->driver_events = neu_event_new();
driver->adapter.cb_funs.driver.update = update;
driver->adapter.cb_funs.driver.write_response = write_response;
driver->adapter.cb_funs.driver.update_im = update_im;
driver->adapter.cb_funs.driver.update_with_meta = update_with_meta;
driver->cache = neu_driver_cache_new();
driver->driver_events = neu_event_new();
driver->adapter.cb_funs.driver.update = update;
driver->adapter.cb_funs.driver.write_response = write_response;
driver->adapter.cb_funs.driver.update_im = update_im;
driver->adapter.cb_funs.driver.update_with_meta = update_with_meta;
driver->adapter.cb_funs.driver.scan_tags_response = scan_tags_response;

return driver;
}
Expand Down Expand Up @@ -2324,4 +2341,30 @@ void neu_adapter_driver_unsubscribe(neu_adapter_driver_t * driver,
}
}
pthread_mutex_unlock(&find->apps_mtx);
}
}

void neu_adapter_driver_scan_tags(neu_adapter_driver_t *driver,
neu_reqresp_head_t * req)
{
if (driver->adapter.state != NEU_NODE_RUNNING_STATE_RUNNING) {
driver->adapter.cb_funs.driver.scan_tags_response(
&driver->adapter, req, NEU_ERR_PLUGIN_NOT_RUNNING, NULL,
NEU_TYPE_ERROR, false);

nlog_warn("%s not running", driver->adapter.name);
return;
}

if (driver->adapter.module->intf_funs->driver.scan_tags == NULL) {
driver->adapter.cb_funs.driver.scan_tags_response(
&driver->adapter, req, NEU_ERR_PLUGIN_NOT_SUPPORT_SCAN_TAGS, NULL,
NEU_TYPE_ERROR, false);

nlog_warn("%s not support scan tags", driver->adapter.name);
return;
}

neu_req_scan_tags_t *cmd = (neu_req_scan_tags_t *) &req[1];
driver->adapter.module->intf_funs->driver.scan_tags(driver->adapter.plugin,
req, cmd->id);
}
3 changes: 3 additions & 0 deletions src/adapter/driver/driver_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@ void neu_adapter_driver_subscribe(neu_adapter_driver_t *driver,
neu_req_subscribe_t * req);
void neu_adapter_driver_unsubscribe(neu_adapter_driver_t * driver,
neu_req_unsubscribe_t *req);

void neu_adapter_driver_scan_tags(neu_adapter_driver_t *driver,
neu_reqresp_head_t * req);
#endif

0 comments on commit 0512edb

Please sign in to comment.