diff --git a/CMakeLists.txt b/CMakeLists.txt index f9d3d0f3..42382d79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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/) diff --git a/Install-dependencies.md b/Install-dependencies.md index 62a22ca1..49d76117 100644 --- a/Install-dependencies.md +++ b/Install-dependencies.md @@ -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 diff --git a/include/neuron/tag_class.h b/include/neuron/tag_sort.h similarity index 53% rename from include/neuron/tag_class.h rename to include/neuron/tag_sort.h index 9a788025..8794ac75 100644 --- a/include/neuron/tag_class.h +++ b/include/neuron/tag_sort.h @@ -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 #include @@ -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 \ No newline at end of file diff --git a/install.sh b/install.sh index e8634024..be96ea24 100755 --- a/install.sh +++ b/install.sh @@ -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 diff --git a/neuron.yaml b/neuron.yaml deleted file mode 100644 index e69de29b..00000000 diff --git a/package.sh b/package.sh index 49383d3f..b4d3a563 100755 --- a/package.sh +++ b/package.sh @@ -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/ diff --git a/package/create_neuron_deb.py b/package/create_neuron_deb.py index f30d36df..38208eff 100755 --- a/package/create_neuron_deb.py +++ b/package/create_neuron_deb.py @@ -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")) diff --git a/package/create_neuron_sdk_deb.py b/package/create_neuron_sdk_deb.py index 25c5f0d1..3d6c8472 100755 --- a/package/create_neuron_sdk_deb.py +++ b/package/create_neuron_sdk_deb.py @@ -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/")) diff --git a/plugins/dummy/dummy.c b/plugins/dummy/dummy.c index edf8ae13..5896c94d 100644 --- a/plugins/dummy/dummy.c +++ b/plugins/dummy/dummy.c @@ -24,7 +24,6 @@ #include #include -#include "config.h" #include "dummy.h" #include "log.h" #include "plugin.h" diff --git a/plugins/mqtt/util.c b/plugins/mqtt/util.c index 18663223..4ff19857 100644 --- a/plugins/mqtt/util.c +++ b/plugins/mqtt/util.c @@ -24,7 +24,6 @@ #include #include "util.h" -#include #include #include #include diff --git a/plugins/restful/log_handle.c b/plugins/restful/log_handle.c index 4681a0b5..34337204 100644 --- a/plugins/restful/log_handle.c +++ b/plugins/restful/log_handle.c @@ -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" diff --git a/plugins/restful/normal_handle.c b/plugins/restful/normal_handle.c index 3b02028f..0680051b 100644 --- a/plugins/restful/normal_handle.c +++ b/plugins/restful/normal_handle.c @@ -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" diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 480b5449..d99c9c88 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -24,7 +24,6 @@ #include #include -#include "config.h" #include "handle.h" #include "http.h" #include "log.h" diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index e8712593..b35efd08 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -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" diff --git a/src/base/tag_class.c b/src/base/tag_class.c deleted file mode 100644 index 8b69e2f2..00000000 --- a/src/base/tag_class.c +++ /dev/null @@ -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 - -#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; - } -} \ No newline at end of file diff --git a/src/base/tag_sort.c b/src/base/tag_sort.c new file mode 100644 index 00000000..8072f961 --- /dev/null +++ b/src/base/tag_sort.c @@ -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 + +#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; + } +} \ No newline at end of file diff --git a/src/config.c b/src/config.c deleted file mode 100644 index 37e722ed..00000000 --- a/src/config.c +++ /dev/null @@ -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 -#include -#include -#include - -#include - -#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; -} \ No newline at end of file diff --git a/src/config.h b/src/config.h deleted file mode 100644 index ee116ad1..00000000 --- a/src/config.h +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/core/plugin_manager.c b/src/core/plugin_manager.c index 42c24a1e..c69ff4b8 100644 --- a/src/core/plugin_manager.c +++ b/src/core/plugin_manager.c @@ -29,7 +29,6 @@ #include #include "adapter/adapter_internal.h" -#include "config.h" #include "dummy/dummy.h" #include "neu_vector.h" #include "panic.h" diff --git a/src/main.c b/src/main.c index 53b7a9c8..ef64d37c 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,6 @@ #include #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); diff --git a/src/utils/neu_jwt.c b/src/utils/neu_jwt.c index 8fdf8f00..1f970441 100644 --- a/src/utils/neu_jwt.c +++ b/src/utils/neu_jwt.c @@ -25,7 +25,6 @@ #include #include -#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; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0124ba76..18ef9987 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/config_test.cc b/tests/config_test.cc deleted file mode 100644 index fc11613f..00000000 --- a/tests/config_test.cc +++ /dev/null @@ -1,24 +0,0 @@ -#include - -#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); -} diff --git a/tests/jwt_test.cc b/tests/jwt_test.cc index ead69844..8e02e38c 100644 --- a/tests/jwt_test.cc +++ b/tests/jwt_test.cc @@ -2,7 +2,6 @@ #include -#include "config.h" #include "jwt.h" #include "log.h" #include "neu_jwt.h" diff --git a/tests/tag_sort_test.cc b/tests/tag_sort_test.cc new file mode 100644 index 00000000..2c90b200 --- /dev/null +++ b/tests/tag_sort_test.cc @@ -0,0 +1,218 @@ +#include +#include + +#include + +#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); +}