mirror of
https://github.com/emqx/neuron.git
synced 2026-10-23 02:00:57 +08:00
Merge pull request #496 from fengzeroz/main
refactor(tag_sort): categorize tags
This commit is contained in:
+5
-8
@@ -98,7 +98,6 @@ set(PERSIST_SOURCES
|
||||
set(NEURON_SOURCES
|
||||
src/main.c
|
||||
src/argparse.c
|
||||
src/config.c
|
||||
src/daemon.c
|
||||
src/core/message.c
|
||||
src/core/databuf.c
|
||||
@@ -133,9 +132,9 @@ target_sources(neuron PRIVATE ${NEURON_SOURCES})
|
||||
target_include_directories(neuron PRIVATE ${NEURON_INCLUDE_DIRECTORIES})
|
||||
target_include_directories(neuron PUBLIC ${CMAKE_STAGING_PREFIX})
|
||||
if(APPLE)
|
||||
target_link_libraries(neuron nng::nng jansson yaml neuron-base)
|
||||
target_link_libraries(neuron nng::nng jansson neuron-base)
|
||||
else()
|
||||
target_link_libraries(neuron dl nng jansson yaml neuron-base)
|
||||
target_link_libraries(neuron dl nng jansson neuron-base)
|
||||
endif()
|
||||
target_link_libraries(neuron ssl crypto ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
@@ -148,7 +147,7 @@ set(NEURON_BASE_SOURCES
|
||||
src/base/neu_tag_group_config.c
|
||||
src/base/neu_plugin_common.c
|
||||
src/base/neu_data_expr.c
|
||||
src/base/tag_class.c
|
||||
src/base/tag_sort.c
|
||||
src/connection/tcp_client.c
|
||||
src/connection/tcp_server.c
|
||||
src/connection/connection.c
|
||||
@@ -163,8 +162,7 @@ set(NEURON_BASE_SOURCES
|
||||
src/utils/json.c
|
||||
src/utils/neu_jwt.c
|
||||
src/utils/file.c
|
||||
src/utils/base64.c
|
||||
src/config.c)
|
||||
src/utils/base64.c)
|
||||
|
||||
if(USE_NNG_MQTT)
|
||||
message("-- USE_NNG_MQTT is defined")
|
||||
@@ -191,13 +189,12 @@ if(USE_NNG_MQTT)
|
||||
target_link_libraries(neuron-base mbedtls mbedx509 mbedcrypto nng)
|
||||
endif()
|
||||
|
||||
target_link_libraries(neuron-base libzlog.so vector-static csptr jansson crypto ssl mqttc yaml jwt
|
||||
target_link_libraries(neuron-base libzlog.so vector-static csptr jansson crypto ssl mqttc jwt
|
||||
${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
#copy file for run
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/zlog.conf DESTINATION ${CMAKE_BINARY_DIR}/config)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/.gitkeep DESTINATION ${CMAKE_BINARY_DIR}/logs)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/neuron.yaml DESTINATION ${CMAKE_BINARY_DIR}/config)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugins/ekuiper/ekuiper.json DESTINATION ${CMAKE_BINARY_DIR}/plugins/schema/)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugins/modbus/modbus-tcp.json DESTINATION ${CMAKE_BINARY_DIR}/plugins/schema/)
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/plugins/mqtt/mqtt.json DESTINATION ${CMAKE_BINARY_DIR}/plugins/schema/)
|
||||
|
||||
@@ -12,14 +12,6 @@ $ cd zlog
|
||||
$ make && sudo make install
|
||||
```
|
||||
|
||||
[yaml](https://github.com/yaml/libyaml.git)
|
||||
```shell
|
||||
$ git clone -b 0.2.5 https://github.com/yaml/libyaml.git
|
||||
$ cd libyaml && mkdir build && cd build
|
||||
$ cmake DBUILD_SHARED_LIBS=OFF .. && make
|
||||
$ sudo make install
|
||||
```
|
||||
|
||||
[jansson](https://github.com/akheron/jansson)
|
||||
```shell
|
||||
$ git clone -b v2.14 https://github.com/akheron/jansson.git
|
||||
|
||||
@@ -17,8 +17,12 @@
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
config_ **/
|
||||
|
||||
#ifndef _NEU_TAG_CLASS_H_
|
||||
#define _NEU_TAG_CLASS_H_
|
||||
#ifndef _NEU_TAG_SORT_H_
|
||||
#define _NEU_TAG_SORT_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@@ -26,35 +30,38 @@ config_ **/
|
||||
#include "utils/utarray.h"
|
||||
#include "utils/utlist.h"
|
||||
|
||||
typedef struct neu_tag_class_el {
|
||||
void * tag;
|
||||
struct neu_tag_class_el *next, *prev;
|
||||
} neu_tag_class_el;
|
||||
typedef struct neu_tag_sort_elem {
|
||||
void * tag;
|
||||
struct neu_tag_sort_elem *next, *prev;
|
||||
} neu_tag_sort_elem_t;
|
||||
|
||||
typedef int (*neu_tag_el_sort_fn)(neu_tag_class_el *tag1,
|
||||
neu_tag_class_el *tag2);
|
||||
|
||||
typedef struct neu_tag_class_info {
|
||||
uint16_t size;
|
||||
void * context;
|
||||
} neu_tag_class_info_t;
|
||||
typedef int (*neu_tag_sort_cmp)(neu_tag_sort_elem_t *tag1,
|
||||
neu_tag_sort_elem_t *tag2);
|
||||
|
||||
typedef struct {
|
||||
neu_tag_class_info_t info;
|
||||
UT_array * tags;
|
||||
} neu_tag_class_t;
|
||||
struct {
|
||||
uint16_t size;
|
||||
void * context;
|
||||
} info;
|
||||
|
||||
UT_array *tags;
|
||||
} neu_tag_sort_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t n_class;
|
||||
neu_tag_class_t *class;
|
||||
} neu_tag_class_result_t;
|
||||
uint16_t n_sort;
|
||||
neu_tag_sort_t *sorts;
|
||||
} neu_tag_sort_result_t;
|
||||
|
||||
typedef bool (*neu_tag_class_fn)(neu_tag_class_info_t *info, void *tag,
|
||||
void *tag_to_be_classified);
|
||||
typedef bool (*neu_tag_sort_fn)(neu_tag_sort_t *sort, void *tag,
|
||||
void *tag_to_be_sorted);
|
||||
|
||||
neu_tag_class_result_t *neu_tag_class(UT_array *tags, neu_tag_class_fn class_fn,
|
||||
neu_tag_el_sort_fn sort_fn);
|
||||
neu_tag_sort_result_t *neu_tag_sort(UT_array *tags, neu_tag_sort_fn sort,
|
||||
neu_tag_sort_cmp cmp);
|
||||
|
||||
void neu_tag_class_free(neu_tag_class_result_t *result);
|
||||
void neu_tag_sort_free(neu_tag_sort_result_t *result);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -81,7 +81,6 @@ ssl_lib_flag="-DOPENSSL_ROOT_DIR=${install_dir}/openssl \
|
||||
-DOPENSSL_LIBRARIES='${install_dir}/openssl/lib'"
|
||||
|
||||
build_zlog
|
||||
compile_source yaml/libyaml.git 0.2.5 "-DBUILD_SHARED_LIBS=OFF"
|
||||
compile_source madler/zlib.git v1.2.11
|
||||
compile_source akheron/jansson.git v2.14 "-DJANSSON_BUILD_DOCS=OFF -DJANSSON_EXAMPLES=OFF"
|
||||
build_openssl
|
||||
|
||||
+1
-2
@@ -50,8 +50,7 @@ cp build/libneuron-base.so \
|
||||
build/neuron \
|
||||
${package_name}
|
||||
|
||||
cp build/config/neuron.yaml \
|
||||
neuron.key \
|
||||
cp neuron.key \
|
||||
neuron.pem \
|
||||
${package_name}/config/
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ rules.append(mkdeb.FileMap("../.gitkeep", "/opt/neuron/core/"))
|
||||
|
||||
rules.append(mkdeb.FileMap("../neuron.key", "/opt/neuron/config/"))
|
||||
rules.append(mkdeb.FileMap("../neuron.pem", "/opt/neuron/config/"))
|
||||
rules.append(mkdeb.FileMap("../neuron.yaml", "/opt/neuron/config/"))
|
||||
|
||||
rules.append(mkdeb.FileMap("../default_plugins.json",
|
||||
"/opt/neuron/persistence/", "r", "plugins.json"))
|
||||
|
||||
@@ -101,7 +101,7 @@ rules.append(mkdeb.FileMap("../include/neuron/plugin.h",
|
||||
|
||||
rules.append(mkdeb.FileMap("../include/neuron/neu_tag.h",
|
||||
"/usr/local/include/neuron/"))
|
||||
rules.append(mkdeb.FileMap("../include/neuron/tag_class.h",
|
||||
rules.append(mkdeb.FileMap("../include/neuron/tag_sort.h",
|
||||
"/usr/local/include/neuron/"))
|
||||
rules.append(mkdeb.FileMap("../include/neuron/neu_vector.h",
|
||||
"/usr/local/include/neuron/"))
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <nng/nng.h>
|
||||
#include <nng/supplemental/http/http.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "dummy.h"
|
||||
#include "log.h"
|
||||
#include "plugin.h"
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
#include <config.h>
|
||||
#include <json/json.h>
|
||||
#include <json/neu_json_param.h>
|
||||
#include <neuron.h>
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
#include "json/neu_json_fn.h"
|
||||
#include "json/neu_json_log.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "handle.h"
|
||||
#include "http.h"
|
||||
#include "neu_vector.h"
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
#include "json/neu_json_login.h"
|
||||
#include "json/neu_json_tty.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "file.h"
|
||||
#include "handle.h"
|
||||
#include "http.h"
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <nng/nng.h>
|
||||
#include <nng/supplemental/http/http.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "handle.h"
|
||||
#include "http.h"
|
||||
#include "log.h"
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
|
||||
#include "adapter.h"
|
||||
#include "adapter_internal.h"
|
||||
#include "config.h"
|
||||
#include "core/message.h"
|
||||
#include "core/neu_manager.h"
|
||||
#include "core/neu_trans_buf.h"
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
/**
|
||||
* 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 <stdlib.h>
|
||||
|
||||
#include "tag_class.h"
|
||||
|
||||
static neu_tag_class_el *array_to_list(UT_array *tags);
|
||||
static void tag_class(neu_tag_class_result_t *result, void *tag,
|
||||
neu_tag_class_fn fn, UT_icd icd);
|
||||
|
||||
neu_tag_class_result_t *neu_tag_class(UT_array *tags, neu_tag_class_fn class_fn,
|
||||
neu_tag_el_sort_fn sort_fn)
|
||||
{
|
||||
neu_tag_class_result_t *result = calloc(1, sizeof(neu_tag_class_result_t));
|
||||
neu_tag_class_el * elt = NULL, *tmp = NULL;
|
||||
neu_tag_class_el * head = array_to_list(tags);
|
||||
|
||||
DL_SORT(head, sort_fn);
|
||||
DL_FOREACH_SAFE(head, elt, tmp)
|
||||
{
|
||||
tag_class(result, elt->tag, class_fn, tags->icd);
|
||||
DL_DELETE(head, elt);
|
||||
free(elt);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void neu_tag_class_free(neu_tag_class_result_t *result)
|
||||
{
|
||||
for (uint16_t i = 0; i < result->n_class; i++) {
|
||||
utarray_free(result->class[i].tags);
|
||||
}
|
||||
|
||||
free(result->class);
|
||||
free(result);
|
||||
}
|
||||
|
||||
static neu_tag_class_el *array_to_list(UT_array *tags)
|
||||
{
|
||||
neu_tag_class_el *head = NULL, *tmp = NULL;
|
||||
|
||||
for (void **tag = utarray_front(tags); tag != NULL;
|
||||
tag = utarray_next(tags, tag)) {
|
||||
tmp = calloc(1, sizeof(neu_tag_class_el));
|
||||
tmp->tag = *tag;
|
||||
DL_APPEND(head, tmp);
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
static void tag_class(neu_tag_class_result_t *result, void *tag,
|
||||
neu_tag_class_fn fn, UT_icd icd)
|
||||
{
|
||||
bool classified = false;
|
||||
|
||||
for (uint16_t i = 0; i < result->n_class; i++) {
|
||||
if (fn(&result->class[i].info,
|
||||
*(void **) utarray_back(result->class[i].tags), tag)) {
|
||||
utarray_push_back(result->class[i].tags, &tag);
|
||||
result->class[i].info.size += 1;
|
||||
classified = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!classified) {
|
||||
result->n_class += 1;
|
||||
result->class =
|
||||
realloc(result->class, sizeof(neu_tag_class_t) * result->n_class);
|
||||
|
||||
memset(&result->class[result->n_class - 1], 0, sizeof(neu_tag_class_t));
|
||||
utarray_new(result->class[result->n_class - 1].tags, &icd);
|
||||
utarray_push_back(result->class[result->n_class - 1].tags, &tag);
|
||||
result->class[result->n_class - 1].info.size = 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* 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 <stdlib.h>
|
||||
|
||||
#include "tag_sort.h"
|
||||
|
||||
static neu_tag_sort_elem_t *array_to_list(UT_array *tags);
|
||||
static void tag_sort(neu_tag_sort_result_t *result, void *tag,
|
||||
neu_tag_sort_fn fn, UT_icd icd);
|
||||
|
||||
neu_tag_sort_result_t *neu_tag_sort(UT_array *tags, neu_tag_sort_fn sort,
|
||||
neu_tag_sort_cmp cmp)
|
||||
{
|
||||
neu_tag_sort_result_t *result = calloc(1, sizeof(neu_tag_sort_result_t));
|
||||
neu_tag_sort_elem_t * elt = NULL, *tmp = NULL;
|
||||
neu_tag_sort_elem_t * head = array_to_list(tags);
|
||||
|
||||
DL_SORT(head, cmp);
|
||||
DL_FOREACH_SAFE(head, elt, tmp)
|
||||
{
|
||||
tag_sort(result, elt->tag, sort, tags->icd);
|
||||
DL_DELETE(head, elt);
|
||||
free(elt);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void neu_tag_sort_free(neu_tag_sort_result_t *result)
|
||||
{
|
||||
for (uint16_t i = 0; i < result->n_sort; i++) {
|
||||
utarray_free(result->sorts[i].tags);
|
||||
}
|
||||
|
||||
free(result->sorts);
|
||||
free(result);
|
||||
}
|
||||
|
||||
static neu_tag_sort_elem_t *array_to_list(UT_array *tags)
|
||||
{
|
||||
neu_tag_sort_elem_t *head = NULL, *tmp = NULL;
|
||||
|
||||
for (void **tag = utarray_front(tags); tag != NULL;
|
||||
tag = utarray_next(tags, tag)) {
|
||||
tmp = calloc(1, sizeof(neu_tag_sort_elem_t));
|
||||
tmp->tag = *tag;
|
||||
DL_APPEND(head, tmp);
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
static void tag_sort(neu_tag_sort_result_t *result, void *tag,
|
||||
neu_tag_sort_fn fn, UT_icd icd)
|
||||
{
|
||||
bool sorted = false;
|
||||
|
||||
for (uint16_t i = 0; i < result->n_sort; i++) {
|
||||
if (fn(&result->sorts[i],
|
||||
*(void **) utarray_back(result->sorts[i].tags), tag)) {
|
||||
utarray_push_back(result->sorts[i].tags, &tag);
|
||||
result->sorts[i].info.size += 1;
|
||||
sorted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!sorted) {
|
||||
result->n_sort += 1;
|
||||
result->sorts =
|
||||
realloc(result->sorts, sizeof(neu_tag_sort_t) * result->n_sort);
|
||||
|
||||
memset(&result->sorts[result->n_sort - 1], 0, sizeof(neu_tag_sort_t));
|
||||
utarray_new(result->sorts[result->n_sort - 1].tags, &icd);
|
||||
utarray_push_back(result->sorts[result->n_sort - 1].tags, &tag);
|
||||
result->sorts[result->n_sort - 1].info.size = 1;
|
||||
}
|
||||
}
|
||||
-134
@@ -1,134 +0,0 @@
|
||||
/**
|
||||
* 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 <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <yaml.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
static char *config = NULL;
|
||||
|
||||
int neu_config_init(const char *config_path)
|
||||
{
|
||||
FILE *file = fopen(config_path, "r");
|
||||
|
||||
if (file == NULL) {
|
||||
log_error("open config %s fail, error: %d", config_path, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
config = strdup(config_path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void neu_config_uninit()
|
||||
{
|
||||
free(config);
|
||||
}
|
||||
|
||||
static int find_first_key(yaml_parser_t *parser, char *key)
|
||||
{
|
||||
yaml_token_t token = { 0 };
|
||||
|
||||
do {
|
||||
yaml_parser_scan(parser, &token);
|
||||
|
||||
if (token.type == YAML_KEY_TOKEN) {
|
||||
yaml_token_delete(&token);
|
||||
yaml_parser_scan(parser, &token);
|
||||
|
||||
if (token.type == YAML_SCALAR_TOKEN &&
|
||||
strncmp((char *) token.data.scalar.value, key, strlen(key)) ==
|
||||
0) {
|
||||
yaml_token_delete(&token);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (token.type == YAML_STREAM_END_TOKEN) {
|
||||
yaml_token_delete(&token);
|
||||
return -1;
|
||||
}
|
||||
|
||||
yaml_token_delete(&token);
|
||||
} while (1);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int find_next_key(yaml_parser_t *parser, char *key)
|
||||
{
|
||||
return find_first_key(parser, key);
|
||||
}
|
||||
|
||||
char *neu_config_get_value(int n_key, char *key, ...)
|
||||
{
|
||||
yaml_token_t token = { 0 };
|
||||
yaml_token_t token_v = { 0 };
|
||||
yaml_parser_t parser = { 0 };
|
||||
char * result = NULL;
|
||||
FILE * file = fopen(config, "r");
|
||||
|
||||
if (file == NULL) {
|
||||
log_error("open config %s fail, error: %d", config, errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
yaml_parser_initialize(&parser);
|
||||
yaml_parser_set_input_file(&parser, file);
|
||||
|
||||
va_list kv;
|
||||
va_start(kv, key);
|
||||
|
||||
if (find_first_key(&parser, key) != 0) {
|
||||
yaml_parser_delete(&parser);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 1; i < n_key; i++) {
|
||||
char *tk = va_arg(kv, char *);
|
||||
if (find_next_key(&parser, tk) != 0) {
|
||||
yaml_parser_delete(&parser);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
va_end(kv);
|
||||
|
||||
yaml_parser_scan(&parser, &token);
|
||||
yaml_parser_scan(&parser, &token_v);
|
||||
if (token.type == YAML_VALUE_TOKEN && token_v.type == YAML_SCALAR_TOKEN) {
|
||||
result = strdup((char *) token_v.data.scalar.value);
|
||||
}
|
||||
|
||||
yaml_token_delete(&token);
|
||||
yaml_token_delete(&token_v);
|
||||
yaml_parser_delete(&parser);
|
||||
|
||||
fclose(file);
|
||||
return result;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int neu_config_init(const char *config_file);
|
||||
void neu_config_uninit();
|
||||
// need free value
|
||||
char *neu_config_get_value(int n_key, char *key, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -29,7 +29,6 @@
|
||||
#include <nng/supplemental/util/platform.h>
|
||||
|
||||
#include "adapter/adapter_internal.h"
|
||||
#include "config.h"
|
||||
#include "dummy/dummy.h"
|
||||
#include "neu_vector.h"
|
||||
#include "panic.h"
|
||||
|
||||
-13
@@ -34,7 +34,6 @@
|
||||
#include <nng/supplemental/util/platform.h>
|
||||
|
||||
#include "argparse.h"
|
||||
#include "config.h"
|
||||
#include "core/message.h"
|
||||
#include "core/neu_manager.h"
|
||||
#include "daemon.h"
|
||||
@@ -82,11 +81,6 @@ static void uninit()
|
||||
fclose(g_log_file);
|
||||
}
|
||||
|
||||
static int read_neuron_config(const char *config)
|
||||
{
|
||||
return neu_config_init(config);
|
||||
}
|
||||
|
||||
static void sig_handler(int sig)
|
||||
{
|
||||
// Receive abort signal, exit the neuron
|
||||
@@ -214,13 +208,6 @@ int main(int argc, char *argv[])
|
||||
goto main_end;
|
||||
}
|
||||
|
||||
rv = read_neuron_config(args.conf_file);
|
||||
if (rv < 0) {
|
||||
log_error("failed to get neuron configuration.");
|
||||
rv = -1;
|
||||
goto main_end;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < args.restart; ++i) {
|
||||
// flush log
|
||||
fflush(g_log_file);
|
||||
|
||||
+1
-3
@@ -25,7 +25,6 @@
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "errcodes.h"
|
||||
#include "log.h"
|
||||
#include "neu_jwt.h"
|
||||
@@ -106,8 +105,7 @@ int neu_jwt_init(char *dir_path)
|
||||
char *content = NULL;
|
||||
|
||||
if (!strcmp((char *) ptr->d_name, ".") ||
|
||||
!strcmp((char *) ptr->d_name, "..") ||
|
||||
!strcmp((char *) ptr->d_name, "neuron.yaml")) {
|
||||
!strcmp((char *) ptr->d_name, "..")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
+15
-17
@@ -74,15 +74,6 @@ target_include_directories(datatag_table_test PRIVATE
|
||||
)
|
||||
target_link_libraries(datatag_table_test neuron-base gtest_main gtest pthread jansson)
|
||||
|
||||
add_executable(config_test config_test.cc ${CMAKE_SOURCE_DIR}/src/config.c)
|
||||
target_include_directories(config_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
target_link_libraries(config_test neuron-base gtest_main gtest pthread yaml)
|
||||
#copy yaml for test
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/neuron.yaml DESTINATION ${CMAKE_BINARY_DIR}/tests)
|
||||
|
||||
add_executable(data_val_test data_val_test.cc ${SRC_PARSER})
|
||||
target_include_directories(data_val_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
@@ -94,14 +85,13 @@ aux_source_directory(${CMAKE_SOURCE_DIR}/plugins/restful SRC_RESTFUL)
|
||||
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)
|
||||
${SRC_RESTFUL} ${SRC_DUMMY} ${SRC_PARSER})
|
||||
target_include_directories(plugin_manager_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/plugins
|
||||
)
|
||||
target_link_libraries(plugin_manager_test neuron-base gtest_main gtest pthread jansson dl nng yaml)
|
||||
target_link_libraries(plugin_manager_test neuron-base gtest_main gtest pthread jansson dl nng)
|
||||
|
||||
add_executable(http_test http_test.cc
|
||||
${CMAKE_SOURCE_DIR}/plugins/restful/http.c)
|
||||
@@ -110,18 +100,17 @@ target_include_directories(http_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/plugins/restful)
|
||||
target_link_libraries(http_test neuron-base gtest_main gtest yaml jansson nng)
|
||||
target_link_libraries(http_test neuron-base gtest_main gtest jansson nng)
|
||||
|
||||
#file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema.json DESTINATION ${CMAKE_BINARY_DIR}/tests)
|
||||
add_executable(mqtt_test mqtt_test.cc
|
||||
${CMAKE_SOURCE_DIR}/src/config.c
|
||||
${CMAKE_SOURCE_DIR}/plugins/mqtt/util.c)
|
||||
target_include_directories(mqtt_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/src/schema
|
||||
${CMAKE_SOURCE_DIR}/plugins/mqtt)
|
||||
target_link_libraries(mqtt_test neuron-base gtest_main gtest yaml jansson ssl crypto)
|
||||
target_link_libraries(mqtt_test neuron-base gtest_main gtest jansson ssl crypto)
|
||||
|
||||
aux_source_directory(${CMAKE_SOURCE_DIR}/src/persist/json SRC_PERSIST_JSON)
|
||||
add_executable(json_persistence_test json_persistence_test.cc ${SRC_PERSIST_JSON})
|
||||
@@ -146,7 +135,7 @@ target_include_directories(jwt_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/src/utils)
|
||||
target_link_libraries(jwt_test neuron-base gtest_main gtest yaml jansson ssl crypto jwt)
|
||||
target_link_libraries(jwt_test neuron-base gtest_main gtest jansson ssl crypto jwt)
|
||||
|
||||
add_executable(base64_test base64_test.cc ${CMAKE_SOURCE_DIR}/src/utils/base64.c)
|
||||
target_include_directories(base64_test PRIVATE
|
||||
@@ -155,10 +144,18 @@ target_include_directories(base64_test PRIVATE
|
||||
)
|
||||
target_link_libraries(base64_test neuron-base gtest_main gtest crypto)
|
||||
|
||||
aux_source_directory(${CMAKE_SOURCE_DIR}/src/base/tag_sort.c SRC_SORT)
|
||||
add_executable(tag_sort_test tag_sort_test.cc ${SRC_SORT})
|
||||
target_include_directories(tag_sort_test PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/src
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
)
|
||||
target_link_libraries(tag_sort_test neuron-base gtest_main gtest)
|
||||
#target_link_directories(modbus_point_test PRIVATE /usr/local/lib)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(data_val_test)
|
||||
gtest_discover_tests(modbus_point_test)
|
||||
#gtest_discover_tests(config_test)
|
||||
gtest_discover_tests(plugin_manager_test)
|
||||
gtest_discover_tests(datatag_table_test)
|
||||
gtest_discover_tests(json_test)
|
||||
@@ -174,3 +171,4 @@ gtest_discover_tests(jwt_test)
|
||||
gtest_discover_tests(json_persistence_test)
|
||||
gtest_discover_tests(persistence_test)
|
||||
gtest_discover_tests(base64_test)
|
||||
gtest_discover_tests(tag_sort_test)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
TEST(ConfigTest, ReadConfig)
|
||||
{
|
||||
neu_config_init((char *) "./neuron.yaml");
|
||||
|
||||
char *result = neu_config_get_value(2, (char *) "web", (char *) "host");
|
||||
EXPECT_STREQ("0.0.0.0", result);
|
||||
free(result);
|
||||
|
||||
result = neu_config_get_value(2, (char *) "web", (char *) "port");
|
||||
EXPECT_STREQ("7000", result);
|
||||
free(result);
|
||||
|
||||
result = neu_config_get_value(2, (char *) "api", (char *) "host");
|
||||
EXPECT_STREQ("0.0.0.0", result);
|
||||
free(result);
|
||||
|
||||
result = neu_config_get_value(2, (char *) "api", (char *) "port");
|
||||
EXPECT_STREQ("7001", result);
|
||||
free(result);
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "jwt.h"
|
||||
#include "log.h"
|
||||
#include "neu_jwt.h"
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "tag_sort.h"
|
||||
|
||||
struct tag {
|
||||
uint8_t station;
|
||||
uint8_t area;
|
||||
uint16_t address;
|
||||
uint8_t type;
|
||||
};
|
||||
|
||||
static int tag_cmp(neu_tag_sort_elem_t *tag1, neu_tag_sort_elem_t *tag2)
|
||||
{
|
||||
int ret = memcmp(tag1->tag, tag2->tag, sizeof(struct tag));
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
static bool tag_sort_fn(neu_tag_sort_t *sort, void *tag, void *tag_to_be_sorted)
|
||||
{
|
||||
struct tag *p_tag = (struct tag *) tag;
|
||||
struct tag *p_tag1 = (struct tag *) tag_to_be_sorted;
|
||||
|
||||
if (p_tag->station != p_tag1->station) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p_tag->area != p_tag1->area) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p_tag->address + 1 < p_tag1->address) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST(TagSortTest, SortSimple)
|
||||
{
|
||||
UT_array * tags = NULL;
|
||||
neu_tag_sort_result_t *result = NULL;
|
||||
struct tag * tag1 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag2 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag3 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
|
||||
tag1->type = 1;
|
||||
tag1->station = 1;
|
||||
tag1->area = 1;
|
||||
tag1->address = 1;
|
||||
|
||||
tag2->type = 1;
|
||||
tag2->station = 1;
|
||||
tag2->area = 1;
|
||||
tag2->address = 2;
|
||||
|
||||
tag3->type = 1;
|
||||
tag3->station = 1;
|
||||
tag3->area = 1;
|
||||
tag3->address = 3;
|
||||
|
||||
utarray_new(tags, &ut_ptr_icd);
|
||||
utarray_push_back(tags, &tag2);
|
||||
utarray_push_back(tags, &tag1);
|
||||
utarray_push_back(tags, &tag3);
|
||||
|
||||
result = neu_tag_sort(tags, tag_sort_fn, tag_cmp);
|
||||
|
||||
EXPECT_EQ(1, result->n_sort);
|
||||
EXPECT_EQ(result->sorts->info.size, 3);
|
||||
|
||||
struct tag **p_tag =
|
||||
(struct tag **) utarray_eltptr(result->sorts[0].tags, 0);
|
||||
EXPECT_EQ((*p_tag)->address, 1);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[0].tags, 1);
|
||||
EXPECT_EQ((*p_tag)->address, 2);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[0].tags, 2);
|
||||
EXPECT_EQ((*p_tag)->address, 3);
|
||||
|
||||
neu_tag_sort_free(result);
|
||||
utarray_free(tags);
|
||||
|
||||
free(tag1);
|
||||
free(tag2);
|
||||
free(tag3);
|
||||
}
|
||||
|
||||
TEST(TagSortTest, SortDup)
|
||||
{
|
||||
UT_array * tags = NULL;
|
||||
neu_tag_sort_result_t *result = NULL;
|
||||
struct tag * tag1 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag2 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag3 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag4 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
|
||||
tag1->type = 1;
|
||||
tag1->station = 1;
|
||||
tag1->area = 1;
|
||||
tag1->address = 1;
|
||||
|
||||
tag2->type = 1;
|
||||
tag2->station = 1;
|
||||
tag2->area = 1;
|
||||
tag2->address = 2;
|
||||
|
||||
tag3->type = 1;
|
||||
tag3->station = 1;
|
||||
tag3->area = 1;
|
||||
tag3->address = 3;
|
||||
|
||||
tag4->type = 1;
|
||||
tag4->station = 1;
|
||||
tag4->area = 1;
|
||||
tag4->address = 3;
|
||||
|
||||
utarray_new(tags, &ut_ptr_icd);
|
||||
utarray_push_back(tags, &tag4);
|
||||
utarray_push_back(tags, &tag2);
|
||||
utarray_push_back(tags, &tag1);
|
||||
utarray_push_back(tags, &tag3);
|
||||
|
||||
result = neu_tag_sort(tags, tag_sort_fn, tag_cmp);
|
||||
|
||||
EXPECT_EQ(1, result->n_sort);
|
||||
EXPECT_EQ(result->sorts->info.size, 4);
|
||||
|
||||
struct tag **p_tag =
|
||||
(struct tag **) utarray_eltptr(result->sorts[0].tags, 0);
|
||||
EXPECT_EQ((*p_tag)->address, 1);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[0].tags, 1);
|
||||
EXPECT_EQ((*p_tag)->address, 2);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[0].tags, 2);
|
||||
EXPECT_EQ((*p_tag)->address, 3);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[0].tags, 3);
|
||||
EXPECT_EQ((*p_tag)->address, 3);
|
||||
|
||||
neu_tag_sort_free(result);
|
||||
utarray_free(tags);
|
||||
|
||||
free(tag1);
|
||||
free(tag2);
|
||||
free(tag3);
|
||||
free(tag4);
|
||||
}
|
||||
|
||||
TEST(TagSortTest, SortDiff)
|
||||
{
|
||||
UT_array * tags = NULL;
|
||||
neu_tag_sort_result_t *result = NULL;
|
||||
struct tag * tag1 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag2 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag3 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
struct tag * tag4 = (struct tag *) calloc(1, sizeof(struct tag));
|
||||
|
||||
tag1->type = 1;
|
||||
tag1->station = 1;
|
||||
tag1->area = 1;
|
||||
tag1->address = 1;
|
||||
|
||||
tag2->type = 1;
|
||||
tag2->station = 1;
|
||||
tag2->area = 1;
|
||||
tag2->address = 2;
|
||||
|
||||
tag3->type = 1;
|
||||
tag3->station = 1;
|
||||
tag3->area = 1;
|
||||
tag3->address = 3;
|
||||
|
||||
tag4->type = 1;
|
||||
tag4->station = 2;
|
||||
tag4->area = 1;
|
||||
tag4->address = 3;
|
||||
|
||||
utarray_new(tags, &ut_ptr_icd);
|
||||
utarray_push_back(tags, &tag4);
|
||||
utarray_push_back(tags, &tag2);
|
||||
utarray_push_back(tags, &tag1);
|
||||
utarray_push_back(tags, &tag3);
|
||||
|
||||
result = neu_tag_sort(tags, tag_sort_fn, tag_cmp);
|
||||
|
||||
EXPECT_EQ(2, result->n_sort);
|
||||
EXPECT_EQ(result->sorts[0].info.size, 3);
|
||||
EXPECT_EQ(result->sorts[1].info.size, 1);
|
||||
|
||||
struct tag **p_tag =
|
||||
(struct tag **) utarray_eltptr(result->sorts[0].tags, 0);
|
||||
EXPECT_EQ((*p_tag)->address, 1);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[0].tags, 1);
|
||||
EXPECT_EQ((*p_tag)->address, 2);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[0].tags, 2);
|
||||
EXPECT_EQ((*p_tag)->address, 3);
|
||||
EXPECT_EQ((*p_tag)->station, 1);
|
||||
|
||||
p_tag = (struct tag **) utarray_eltptr(result->sorts[1].tags, 0);
|
||||
EXPECT_EQ((*p_tag)->address, 3);
|
||||
EXPECT_EQ((*p_tag)->station, 2);
|
||||
|
||||
neu_tag_sort_free(result);
|
||||
utarray_free(tags);
|
||||
|
||||
free(tag1);
|
||||
free(tag2);
|
||||
free(tag3);
|
||||
free(tag4);
|
||||
}
|
||||
Reference in New Issue
Block a user