diff --git a/plugins/mqtt/message_handle.c b/plugins/mqtt/message_handle.c index bdb13bc8..4b21e59f 100644 --- a/plugins/mqtt/message_handle.c +++ b/plugins/mqtt/message_handle.c @@ -392,49 +392,65 @@ void message_handle_add_tag(neu_plugin_t * plugin, free(result); } -void message_handle_read_tag_group_list( - neu_plugin_t *plugin, struct neu_paser_read_tag_group_list_req *req) +static void message_handle_get_group_config_by_name( + neu_plugin_t *plugin, const uint32_t node_id, const char *config_name, + struct neu_paser_get_group_config_res *res) { - UNUSED(req); - vector_t nodes = neu_system_get_nodes(plugin, NEU_NODE_TYPE_DRIVER); - - char * json_str = NULL; - struct neu_paser_read_tag_group_list_res res = { - .function = NEU_PARSE_OP_READ_TAG_GROUP_LIST, - .uuid = (char *) "1d892fe8-f37e-11eb-9a34-932aa06a28f3", - .error = 0, - .n_group = 3, - .names = NULL - }; - + vector_t nodes = neu_system_get_nodes(plugin, NEU_NODE_TYPE_DRIVER); neu_node_info_t *node_info; int count = 0; VECTOR_FOR_EACH(&nodes, iter) { node_info = (neu_node_info_t *) iterator_get(&iter); + if (0 != node_id && node_id != node_info->node_id) { + continue; + } + vector_t configs = neu_system_get_group_configs(plugin, node_info->node_id); neu_taggrp_config_t *config; VECTOR_FOR_EACH(&configs, iter) { config = *(neu_taggrp_config_t **) iterator_get(&iter); - if (NULL == res.names) { - res.names = - (struct neu_paser_read_tag_group_list_name *) malloc( - sizeof(struct neu_paser_read_tag_group_list_name)); + if (NULL == config) { + continue; } - if (NULL != res.names) { - res.names = realloc( - res.names, - (count + 1) * - sizeof(struct neu_paser_read_tag_group_list_name)); + if (0 != strcmp(config_name, neu_taggrp_cfg_get_name(config))) { + continue; } - res.names[count].name = + if (NULL == res->rows) { + res->rows = + (struct neu_paser_get_group_config_res_row *) malloc( + sizeof(struct neu_paser_get_group_config_res_row)); + } + if (NULL != res->rows) { + res->rows = realloc( + res->rows, + (count + 1) * + sizeof(struct neu_paser_get_group_config_res_row)); + } + + res->rows[count].name = strdup((char *) neu_taggrp_cfg_get_name(config)); + res->rows[count].group = strdup(""); + res->rows[count].node_id = node_info->node_id; + res->rows[count].read_interval = + neu_taggrp_cfg_get_interval(config); + + vector_t *pipes = neu_taggrp_cfg_get_subpipes(config); + if (NULL != pipes) { + res->rows[count].pipe_count = pipes->size; + } + + vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); + if (NULL != ids) { + res->rows[count].tag_count = ids->size; + } + count++; } - res.n_group = count; + res->n_config = count; VECTOR_FOR_EACH(&configs, iter) { @@ -444,6 +460,99 @@ void message_handle_read_tag_group_list( } vector_uninit(&configs); } + vector_uninit(&nodes); +} + +static void +message_handle_get_group_config_all(neu_plugin_t * plugin, + const uint32_t node_id, + struct neu_paser_get_group_config_res *res) +{ + vector_t nodes = neu_system_get_nodes(plugin, NEU_NODE_TYPE_DRIVER); + neu_node_info_t *node_info; + int count = 0; + VECTOR_FOR_EACH(&nodes, iter) + { + node_info = (neu_node_info_t *) iterator_get(&iter); + if (0 != node_id && node_id != node_info->node_id) { + continue; + } + + vector_t configs = + neu_system_get_group_configs(plugin, node_info->node_id); + neu_taggrp_config_t *config; + VECTOR_FOR_EACH(&configs, iter) + { + config = *(neu_taggrp_config_t **) iterator_get(&iter); + if (NULL == config) { + continue; + } + + if (NULL == res->rows) { + res->rows = + (struct neu_paser_get_group_config_res_row *) malloc( + sizeof(struct neu_paser_get_group_config_res_row)); + } + if (NULL != res->rows) { + res->rows = realloc( + res->rows, + (count + 1) * + sizeof(struct neu_paser_get_group_config_res_row)); + } + + res->rows[count].name = + strdup((char *) neu_taggrp_cfg_get_name(config)); + res->rows[count].group = strdup(""); + res->rows[count].node_id = node_info->node_id; + res->rows[count].read_interval = + neu_taggrp_cfg_get_interval(config); + + vector_t *pipes = neu_taggrp_cfg_get_subpipes(config); + if (NULL != pipes) { + res->rows[count].pipe_count = pipes->size; + } + + vector_t *ids = neu_taggrp_cfg_get_datatag_ids(config); + if (NULL != ids) { + res->rows[count].tag_count = ids->size; + } + + count++; + } + res->n_config = count; + + VECTOR_FOR_EACH(&configs, iter) + { + neu_taggrp_config_t *cur_grp_config; + cur_grp_config = *(neu_taggrp_config_t **) iterator_get(&iter); + neu_taggrp_cfg_free(cur_grp_config); + } + vector_uninit(&configs); + } + vector_uninit(&nodes); +} + +void message_handle_get_group_config(neu_plugin_t *plugin, + struct neu_paser_get_group_config_req *req) +{ + log_debug("uuid:%s, node id:%d, config:%s", req->uuid, req->node_id, + req->config); + + char * json_str = NULL; + struct neu_paser_get_group_config_res res = { + .function = NEU_PARSE_OP_GET_GROUP_CONFIG, + .uuid = (char *) req->uuid, + .error = 0, + .n_config = 0, + .rows = NULL + }; + + if (NULL != req->config && 0 != strlen(req->config)) { + message_handle_get_group_config_by_name(plugin, req->node_id, + req->config, &res); + } else { + message_handle_get_group_config_all(plugin, req->node_id, &res); + } int rc = neu_parse_encode(&res, &json_str); if (0 == rc) { @@ -451,10 +560,272 @@ void message_handle_read_tag_group_list( } int i; - for (i = 0; i < count; i++) { - free(res.names[i].name); + for (i = 0; i < res.n_config; i++) { + free(res.rows[i].name); + free(res.rows[i].group); } - free(res.names); + free(res.rows); free(json_str); - vector_uninit(&nodes); } + +static int message_handle_node_id_exist(vector_t *v, const uint32_t node_id) +{ + neu_node_info_t *node_info; + VECTOR_FOR_EACH(v, iter) + { + node_info = (neu_node_info_t *) iterator_get(&iter); + if (node_id == node_info->node_id) { + return 0; + } + } + return -1; +} + +static int message_handle_config_exist(vector_t *v, const char *config_name) +{ + neu_taggrp_config_t *config; + + VECTOR_FOR_EACH(v, iter) + { + config = *(neu_taggrp_config_t **) iterator_get(&iter); + if (NULL == config) { + continue; + } + + if (0 == strcmp(config_name, neu_taggrp_cfg_get_name(config))) { + return 0; + } + } + return -1; +} + +static int message_handle_inner_add_config(neu_plugin_t * plugin, + const char * group_name, + const char * config_name, + const uint32_t src_node_id, + const uint32_t dst_node_id, + const int read_interval) +{ + UNUSED(group_name); + + if (0 == src_node_id || 0 == dst_node_id) { + return -1; + } + + vector_t nodes = neu_system_get_nodes(plugin, NEU_NODE_TYPE_DRIVER); + int rc = message_handle_node_id_exist(&nodes, src_node_id); + if (0 != rc) { + vector_uninit(&nodes); + return -2; + } + rc = message_handle_node_id_exist(&nodes, dst_node_id); + if (0 != rc) { + vector_uninit(&nodes); + return -3; + } + vector_uninit(&nodes); + + vector_t configs = neu_system_get_group_configs(plugin, src_node_id); + rc = message_handle_config_exist(&configs, config_name); + VECTOR_FOR_EACH(&configs, iter) + { + neu_taggrp_config_t *cur_grp_config; + cur_grp_config = *(neu_taggrp_config_t **) iterator_get(&iter); + neu_taggrp_cfg_free(cur_grp_config); + } + vector_uninit(&configs); + + if (0 == rc) { + return -4; + } + + neu_taggrp_config_t *grp_config = neu_taggrp_cfg_new((char *) config_name); + if (NULL == grp_config) { + return -5; + } + neu_taggrp_cfg_set_interval(grp_config, read_interval); + intptr_t error = neu_system_add_group_config(plugin, src_node_id, + dst_node_id, grp_config); + if (0 != error) { + return -6; + } + return 0; +} + +void message_handle_add_group_config(neu_plugin_t *plugin, + struct neu_paser_add_group_config_req *req) +{ + log_debug("uuid:%s, group:%s, config:%s, src node id:%d, dst node id:%d, " + "read interval:%d", + req->uuid, req->group, req->config, req->src_node_id, + req->dst_node_id, req->read_interval); + + int rc = message_handle_inner_add_config(plugin, req->group, req->config, + req->src_node_id, req->dst_node_id, + req->read_interval); + + char * json_str = NULL; + struct neu_paser_group_config_res res = { + .function = NEU_PARSE_OP_ADD_GROUP_CONFIG, + .uuid = (char *) req->uuid, + .error = rc, + }; + + rc = neu_parse_encode(&res, &json_str); + if (0 == rc) { + message_handle_send(json_str, strlen(json_str)); + } + + free(json_str); +} + +static int message_handle_inner_update_config(neu_plugin_t * plugin, + const char * group_name, + const char * config_name, + const uint32_t src_node_id, + const uint32_t dst_node_id, + const int read_interval) +{ + UNUSED(group_name); + + if (0 == src_node_id || 0 == dst_node_id) { + return -1; + } + + vector_t nodes = neu_system_get_nodes(plugin, NEU_NODE_TYPE_DRIVER); + int rc = message_handle_node_id_exist(&nodes, src_node_id); + if (0 != rc) { + vector_uninit(&nodes); + return -2; + } + rc = message_handle_node_id_exist(&nodes, dst_node_id); + if (0 != rc) { + vector_uninit(&nodes); + return -3; + } + vector_uninit(&nodes); + + vector_t configs = neu_system_get_group_configs(plugin, src_node_id); + rc = message_handle_config_exist(&configs, config_name); + VECTOR_FOR_EACH(&configs, iter) + { + neu_taggrp_config_t *cur_grp_config; + cur_grp_config = *(neu_taggrp_config_t **) iterator_get(&iter); + neu_taggrp_cfg_free(cur_grp_config); + } + vector_uninit(&configs); + + if (0 != rc) { + return -4; + } + + neu_taggrp_config_t *grp_config = neu_taggrp_cfg_new((char *) config_name); + if (NULL == grp_config) { + return -5; + } + neu_taggrp_cfg_set_interval(grp_config, read_interval); + + intptr_t error = neu_system_update_group_config(plugin, src_node_id, + dst_node_id, grp_config); + if (0 != error) { + return -6; + } + return 0; +} + +void message_handle_update_group_config( + neu_plugin_t *plugin, struct neu_paser_update_group_config_req *req) +{ + log_debug("uuid:%s, group:%s, config:%s, src node id:%d, dst node id:%d, " + "read interval:%d", + req->uuid, req->group, req->config, req->src_node_id, + req->dst_node_id, req->read_interval); + + int rc = message_handle_inner_update_config( + plugin, req->group, req->config, req->src_node_id, req->dst_node_id, + req->read_interval); + + char * json_str = NULL; + struct neu_paser_group_config_res res = { + .function = NEU_PARSE_OP_UPDATE_GROUP_CONFIG, + .uuid = (char *) req->uuid, + .error = rc, + }; + + rc = neu_parse_encode(&res, &json_str); + if (0 == rc) { + message_handle_send(json_str, strlen(json_str)); + } + + free(json_str); +} + +static int message_handle_inner_delete_config(neu_plugin_t * plugin, + const char * group_name, + const char * config_name, + const uint32_t node_id) +{ + UNUSED(group_name); + + if (0 == node_id) { + return -1; + } + + vector_t nodes = neu_system_get_nodes(plugin, NEU_NODE_TYPE_DRIVER); + int rc = message_handle_node_id_exist(&nodes, node_id); + if (0 != rc) { + vector_uninit(&nodes); + return -2; + } + rc = message_handle_node_id_exist(&nodes, node_id); + if (0 != rc) { + vector_uninit(&nodes); + return -3; + } + vector_uninit(&nodes); + + vector_t configs = neu_system_get_group_configs(plugin, node_id); + rc = message_handle_config_exist(&configs, config_name); + VECTOR_FOR_EACH(&configs, iter) + { + neu_taggrp_config_t *cur_grp_config; + cur_grp_config = *(neu_taggrp_config_t **) iterator_get(&iter); + neu_taggrp_cfg_free(cur_grp_config); + } + vector_uninit(&configs); + + if (0 != rc) { + return -4; + } + + intptr_t error = + neu_system_del_group_config(plugin, node_id, (char *) config_name); + if (0 != error) { + return -5; + } + return 0; +} + +void message_handle_delete_group_config( + neu_plugin_t *plugin, struct neu_paser_delete_group_config_req *req) +{ + log_debug("uuid:%s, group:%s, config:%s, src node id:%d", req->uuid, + req->group, req->config, req->node_id); + + int rc = message_handle_inner_delete_config(plugin, req->group, req->config, + req->node_id); + + char * json_str = NULL; + struct neu_paser_group_config_res res = { + .function = NEU_PARSE_OP_DELETE_GROUP_CONFIG, + .uuid = (char *) req->uuid, + .error = rc, + }; + + rc = neu_parse_encode(&res, &json_str); + if (0 == rc) { + message_handle_send(json_str, strlen(json_str)); + } + + free(json_str); +} \ No newline at end of file diff --git a/plugins/mqtt/message_handle.h b/plugins/mqtt/message_handle.h index 5fe07421..759beaab 100644 --- a/plugins/mqtt/message_handle.h +++ b/plugins/mqtt/message_handle.h @@ -30,8 +30,8 @@ extern "C" { #include "paho_client.h" #include "parser/neu_json_add_tag.h" #include "parser/neu_json_get_tag.h" +#include "parser/neu_json_group_config.h" #include "parser/neu_json_read.h" -#include "parser/neu_json_tag_group.h" #include "parser/neu_json_write.h" void message_handle_set_paho_client(paho_client_t *paho); @@ -46,8 +46,14 @@ void message_handle_get_tag_list(neu_plugin_t * plugin, struct neu_parse_get_tags_req *req); void message_handle_add_tag(neu_plugin_t * plugin, struct neu_parse_add_tags_req *req); -void message_handle_read_tag_group_list( - neu_plugin_t *plugin, struct neu_paser_read_tag_group_list_req *req); +void message_handle_get_group_config( + neu_plugin_t *plugin, struct neu_paser_get_group_config_req *req); +void message_handle_add_group_config( + neu_plugin_t *plugin, struct neu_paser_add_group_config_req *req); +void message_handle_update_group_config( + neu_plugin_t *plugin, struct neu_paser_update_group_config_req *req); +void message_handle_delete_group_config( + neu_plugin_t *plugin, struct neu_paser_delete_group_config_req *req); #ifdef __cplusplus } diff --git a/plugins/mqtt/plugin_mqtt.c b/plugins/mqtt/plugin_mqtt.c index 577182ce..93fba1bd 100644 --- a/plugins/mqtt/plugin_mqtt.c +++ b/plugins/mqtt/plugin_mqtt.c @@ -80,11 +80,22 @@ static void plugin_response_handle(const char *topic_name, size_t topic_len, } switch (req->function) { - case NEU_PARSE_OP_READ_TAG_GROUP_LIST: - message_handle_read_tag_group_list( - plugin, (struct neu_paser_read_tag_group_list_req *) result); + case NEU_PARSE_OP_GET_GROUP_CONFIG: + message_handle_get_group_config( + plugin, (struct neu_paser_get_group_config_req *) result); + break; + case NEU_PARSE_OP_ADD_GROUP_CONFIG: + message_handle_add_group_config( + plugin, (struct neu_paser_add_group_config_req *) result); + break; + case NEU_PARSE_OP_UPDATE_GROUP_CONFIG: + message_handle_update_group_config( + plugin, (struct neu_paser_update_group_config_req *) result); + break; + case NEU_PARSE_OP_DELETE_GROUP_CONFIG: + message_handle_delete_group_config( + plugin, (struct neu_paser_delete_group_config_req *) result); break; - case NEU_PARSE_OP_READ: message_handle_read(plugin, (struct neu_parse_read_req *) result); break; diff --git a/src/core/neu_datatag_manager.c b/src/core/neu_datatag_manager.c index 7be89b48..aeb873c0 100644 --- a/src/core/neu_datatag_manager.c +++ b/src/core/neu_datatag_manager.c @@ -175,8 +175,7 @@ int neu_datatag_mng_del_grp_config(neu_datatag_manager_t *datatag_manager, pthread_mutex_unlock(&datatag_manager->mtx); if (rv == 0) { - log_info("Delete group config: %s", - neu_taggrp_cfg_get_name(grp_config)); + log_info("Delete group config: %s", config_name); } return rv; } @@ -201,14 +200,14 @@ int neu_datatag_mng_update_grp_config(neu_datatag_manager_t *datatag_manager, // neu_taggrp_cfg_unanchor(grp_config); neu_taggrp_cfg_free(grp_config); // neu_taggrp_cfg_anchor(new_grp_config); - vector_assign(&datatag_manager->p_configs, index, new_grp_config); + vector_assign(&datatag_manager->p_configs, index, &new_grp_config); rv = 0; } pthread_mutex_unlock(&datatag_manager->mtx); if (rv == 0) { log_info("Update group config: %s", - neu_taggrp_cfg_get_name(grp_config)); + neu_taggrp_cfg_get_name(new_grp_config)); } return rv; } diff --git a/src/parser/neu_json_group_config.c b/src/parser/neu_json_group_config.c new file mode 100644 index 00000000..bd026d69 --- /dev/null +++ b/src/parser/neu_json_group_config.c @@ -0,0 +1,336 @@ +/** + * 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 "utils/json.h" + +#include "neu_json_group_config.h" + +#define GROUP "group" +#define NAMES "names" +#define NAME "name" +#define TYPE "type" +#define TIMESTAMP "timestamp" +#define VALUE "value" +#define TAGS "tags" + +int neu_paser_decode_get_group_config_req( + char *buf, struct neu_paser_get_group_config_req **result) +{ + struct neu_paser_get_group_config_req *req = + malloc(sizeof(struct neu_paser_get_group_config_req)); + memset(req, 0, sizeof(struct neu_paser_get_group_config_req)); + + neu_json_elem_t elem[] = { { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + }, + { + .name = "nodeId", + .t = NEU_JSON_INT, + }, + { + .name = "config", + .t = NEU_JSON_STR, + } + + }; + + int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); + if (ret != 0) { + free(req); + return -1; + } + req->function = NEU_PARSE_OP_GET_GROUP_CONFIG; + req->uuid = elem[0].v.val_str; + req->node_id = elem[1].v.val_int; + req->config = elem[2].v.val_str; + + *result = req; + return 0; +} + +int neu_parse_encode_get_group_config_res( + struct neu_paser_get_group_config_res *res, char **buf) +{ + void *array = NULL; + for (int i = 0; i < res->n_config; i++) { + neu_json_elem_t group[] = { { + .name = NAME, + .t = NEU_JSON_STR, + .v.val_str = res->rows[i].name, + }, + { + .name = "group", + .t = NEU_JSON_STR, + .v.val_str = res->rows[i].group, + }, + { + .name = "nodeID", + .t = NEU_JSON_INT, + .v.val_int = res->rows[i].node_id, + }, + { + .name = "readInterval", + .t = NEU_JSON_INT, + .v.val_int = res->rows[i].read_interval, + }, + { + .name = "pipeCount", + .t = NEU_JSON_INT, + .v.val_int = res->rows[i].pipe_count, + }, + { + .name = "tagCount", + .t = NEU_JSON_INT, + .v.val_int = res->rows[i].tag_count, + } }; + array = neu_json_encode_array(array, group, 4); + } + neu_json_elem_t elems[] = { { + .name = NEU_PARSE_FUNCTION, + .t = NEU_JSON_INT, + .v.val_int = NEU_PARSE_OP_GET_GROUP_CONFIG, + }, + { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + .v.val_str = res->uuid, + }, + { + .name = NEU_PARSE_ERROR, + .t = NEU_JSON_INT, + .v.val_int = res->error, + }, + { + .name = "groups", + .t = NEU_JSON_OBJECT, + .v.object = array, + } }; + return neu_json_encode(elems, 4, buf); +} + +int neu_paser_decode_add_group_config_req( + char *buf, struct neu_paser_add_group_config_req **result) +{ + struct neu_paser_add_group_config_req *req = + malloc(sizeof(struct neu_paser_add_group_config_req)); + memset(req, 0, sizeof(struct neu_paser_add_group_config_req)); + + neu_json_elem_t elem[] = { { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + }, + { + .name = "group", + .t = NEU_JSON_STR, + }, + { + .name = "config", + .t = NEU_JSON_STR, + }, + { + .name = "srcNodeId", + .t = NEU_JSON_INT, + }, + { + .name = "dstNodeId", + .t = NEU_JSON_INT, + }, + { + .name = "readInterval", + .t = NEU_JSON_INT, + } }; + + int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); + if (ret != 0) { + free(req); + return -1; + } + req->function = NEU_PARSE_OP_ADD_GROUP_CONFIG; + req->uuid = elem[0].v.val_str; + req->group = elem[1].v.val_str; + req->config = elem[2].v.val_str; + req->src_node_id = elem[3].v.val_int; + req->dst_node_id = elem[4].v.val_int; + req->read_interval = elem[5].v.val_int; + + *result = req; + return 0; +} + +int neu_parse_encode_add_group_config_res( + struct neu_paser_group_config_res *res, char **buf) +{ + neu_json_elem_t elems[] = { { + .name = NEU_PARSE_FUNCTION, + .t = NEU_JSON_INT, + .v.val_int = NEU_PARSE_OP_ADD_GROUP_CONFIG, + }, + { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + .v.val_str = res->uuid, + }, + { + .name = NEU_PARSE_ERROR, + .t = NEU_JSON_INT, + .v.val_int = res->error, + } }; + + return neu_json_encode(elems, 3, buf); +} + +int neu_paser_decode_update_group_config_req( + char *buf, struct neu_paser_update_group_config_req **result) +{ + struct neu_paser_update_group_config_req *req = + malloc(sizeof(struct neu_paser_update_group_config_req)); + memset(req, 0, sizeof(struct neu_paser_update_group_config_req)); + + neu_json_elem_t elem[] = { { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + }, + { + .name = "group", + .t = NEU_JSON_STR, + }, + { + .name = "config", + .t = NEU_JSON_STR, + }, + { + .name = "srcNodeId", + .t = NEU_JSON_INT, + }, + { + .name = "dstNodeId", + .t = NEU_JSON_INT, + }, + { + .name = "readInterval", + .t = NEU_JSON_INT, + } }; + + int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); + if (ret != 0) { + free(req); + return -1; + } + req->function = NEU_PARSE_OP_UPDATE_GROUP_CONFIG; + req->uuid = elem[0].v.val_str; + req->group = elem[1].v.val_str; + req->config = elem[2].v.val_str; + req->src_node_id = elem[3].v.val_int; + req->dst_node_id = elem[4].v.val_int; + req->read_interval = elem[5].v.val_int; + + *result = req; + return 0; +} + +int neu_parse_encode_update_group_config_res( + struct neu_paser_group_config_res *res, char **buf) +{ + neu_json_elem_t elems[] = { { + .name = NEU_PARSE_FUNCTION, + .t = NEU_JSON_INT, + .v.val_int = + NEU_PARSE_OP_UPDATE_GROUP_CONFIG, + }, + { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + .v.val_str = res->uuid, + }, + { + .name = NEU_PARSE_ERROR, + .t = NEU_JSON_INT, + .v.val_int = res->error, + } }; + + return neu_json_encode(elems, 3, buf); +} + +int neu_paser_decode_delete_group_config_req( + char *buf, struct neu_paser_delete_group_config_req **result) +{ + struct neu_paser_delete_group_config_req *req = + malloc(sizeof(struct neu_paser_delete_group_config_req)); + memset(req, 0, sizeof(struct neu_paser_delete_group_config_req)); + + neu_json_elem_t elem[] = { { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + }, + { + .name = "group", + .t = NEU_JSON_STR, + }, + { + .name = "config", + .t = NEU_JSON_STR, + }, + { + .name = "nodeId", + .t = NEU_JSON_INT, + } + + }; + + int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); + if (ret != 0) { + free(req); + return -1; + } + req->function = NEU_PARSE_OP_DELETE_GROUP_CONFIG; + req->uuid = elem[0].v.val_str; + req->group = elem[1].v.val_str; + req->config = elem[2].v.val_str; + req->node_id = elem[3].v.val_int; + + *result = req; + return 0; +} + +int neu_parse_encode_delete_group_config_res( + struct neu_paser_group_config_res *res, char **buf) +{ + neu_json_elem_t elems[] = { { + .name = NEU_PARSE_FUNCTION, + .t = NEU_JSON_INT, + .v.val_int = + NEU_PARSE_OP_DELETE_GROUP_CONFIG, + }, + { + .name = NEU_PARSE_UUID, + .t = NEU_JSON_STR, + .v.val_str = res->uuid, + }, + { + .name = NEU_PARSE_ERROR, + .t = NEU_JSON_INT, + .v.val_int = res->error, + } }; + + return neu_json_encode(elems, 3, buf); +} \ No newline at end of file diff --git a/src/parser/neu_json_group_config.h b/src/parser/neu_json_group_config.h new file mode 100644 index 00000000..bb65edda --- /dev/null +++ b/src/parser/neu_json_group_config.h @@ -0,0 +1,111 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2021 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ +#ifndef _NEU_JSON_API_GROUP_CONFIG_H_ +#define _NEU_JSON_API_GROUP_CONFIG_H_ + +#include + +#include "neu_json_parser.h" + +struct neu_paser_get_group_config_req { + enum neu_parse_function function; + char * uuid; + char * config; + int node_id; +}; + +struct neu_paser_get_group_config_res_row { + char *name; + char *group; + int node_id; + int read_interval; + int pipe_count; + int tag_count; +}; + +struct neu_paser_get_group_config_res { + enum neu_parse_function function; + char * uuid; + int error; + int n_config; + struct neu_paser_get_group_config_res_row *rows; +}; + +struct neu_paser_group_config_row { + char *name; + int type; + int decimal; + char *address; + int flag; +}; + +struct neu_paser_add_group_config_req { + enum neu_parse_function function; + char * uuid; + char * group; + char * config; + uint32_t src_node_id; + uint32_t dst_node_id; + int read_interval; +}; + +struct neu_paser_update_group_config_req { + enum neu_parse_function function; + char * uuid; + char * group; + char * config; + uint32_t src_node_id; + uint32_t dst_node_id; + int read_interval; +}; + +struct neu_paser_delete_group_config_req { + enum neu_parse_function function; + char * uuid; + char * group; + char * config; + uint32_t node_id; +}; + +struct neu_paser_group_config_res { + enum neu_parse_function function; + char * uuid; + int error; +}; + +int neu_paser_decode_get_group_config_req( + char *buf, struct neu_paser_get_group_config_req **result); +int neu_parse_encode_get_group_config_res( + struct neu_paser_get_group_config_res *res, char **buf); + +int neu_paser_decode_add_group_config_req( + char *buf, struct neu_paser_add_group_config_req **result); +int neu_paser_decode_update_group_config_req( + char *buf, struct neu_paser_update_group_config_req **result); +int neu_paser_decode_delete_group_config_req( + char *buf, struct neu_paser_delete_group_config_req **result); + +int neu_parse_encode_add_group_config_res( + struct neu_paser_group_config_res *res, char **buf); +int neu_parse_encode_update_group_config_res( + struct neu_paser_group_config_res *res, char **buf); +int neu_parse_encode_delete_group_config_res( + struct neu_paser_group_config_res *res, char **buf); + +#endif \ No newline at end of file diff --git a/src/parser/neu_json_parser.c b/src/parser/neu_json_parser.c index 32a3e3a9..c0f15448 100644 --- a/src/parser/neu_json_parser.c +++ b/src/parser/neu_json_parser.c @@ -26,10 +26,10 @@ #include "neu_json_add_tag.h" #include "neu_json_delete_tag.h" #include "neu_json_get_tag.h" +#include "neu_json_group_config.h" #include "neu_json_login.h" #include "neu_json_parser.h" #include "neu_json_read.h" -#include "neu_json_tag_group.h" #include "neu_json_updata_tag.h" #include "neu_json_write.h" @@ -45,9 +45,24 @@ int neu_parse_decode(char *buf, void **result) } switch (func_code.v.val_int) { - case NEU_PARSE_OP_READ_TAG_GROUP_LIST: { - ret = neu_paser_decode_read_tag_group_list_req( - buf, (struct neu_paser_read_tag_group_list_req **) result); + case NEU_PARSE_OP_GET_GROUP_CONFIG: { + ret = neu_paser_decode_get_group_config_req( + buf, (struct neu_paser_get_group_config_req **) result); + break; + } + case NEU_PARSE_OP_ADD_GROUP_CONFIG: { + ret = neu_paser_decode_add_group_config_req( + buf, (struct neu_paser_add_group_config_req **) result); + break; + } + case NEU_PARSE_OP_UPDATE_GROUP_CONFIG: { + ret = neu_paser_decode_update_group_config_req( + buf, (struct neu_paser_update_group_config_req **) result); + break; + } + case NEU_PARSE_OP_DELETE_GROUP_CONFIG: { + ret = neu_paser_decode_delete_group_config_req( + buf, (struct neu_paser_delete_group_config_req **) result); break; } case NEU_PARSE_OP_READ: @@ -90,9 +105,24 @@ int neu_parse_encode(void *result, char **buf) enum neu_parse_function *function = result; switch (*function) { - case NEU_PARSE_OP_READ_TAG_GROUP_LIST: { - neu_parse_encode_read_tag_group_list_res( - (struct neu_paser_read_tag_group_list_res *) result, buf); + case NEU_PARSE_OP_GET_GROUP_CONFIG: { + neu_parse_encode_get_group_config_res( + (struct neu_paser_get_group_config_res *) result, buf); + break; + } + case NEU_PARSE_OP_ADD_GROUP_CONFIG: { + neu_parse_encode_add_group_config_res( + (struct neu_paser_group_config_res *) result, buf); + break; + } + case NEU_PARSE_OP_UPDATE_GROUP_CONFIG: { + neu_parse_encode_update_group_config_res( + (struct neu_paser_group_config_res *) result, buf); + break; + } + case NEU_PARSE_OP_DELETE_GROUP_CONFIG: { + neu_parse_encode_delete_group_config_res( + (struct neu_paser_group_config_res *) result, buf); break; } case NEU_PARSE_OP_READ: @@ -132,12 +162,34 @@ void neu_parse_decode_free(void *result) enum neu_parse_function *function = result; switch (*function) { - case NEU_PARSE_OP_READ_TAG_GROUP_LIST: { - struct neu_paser_read_tag_group_list_req *req = result; + case NEU_PARSE_OP_GET_GROUP_CONFIG: { + struct neu_paser_get_group_config_req *req = result; free(req->uuid); free(req->config); break; } + case NEU_PARSE_OP_ADD_GROUP_CONFIG: { + struct neu_paser_add_group_config_req *req = result; + free(req->uuid); + free(req->group); + free(req->config); + break; + } + case NEU_PARSE_OP_UPDATE_GROUP_CONFIG: { + struct neu_paser_update_group_config_req *req = result; + free(req->uuid); + free(req->group); + free(req->config); + break; + } + case NEU_PARSE_OP_DELETE_GROUP_CONFIG: { + struct neu_paser_delete_group_config_req *req = result; + free(req->uuid); + free(req->group); + free(req->config); + break; + break; + } case NEU_PARSE_OP_READ: { struct neu_parse_read_req *req = result; diff --git a/src/parser/neu_json_parser.h b/src/parser/neu_json_parser.h index ee974ceb..d0f1e03b 100644 --- a/src/parser/neu_json_parser.h +++ b/src/parser/neu_json_parser.h @@ -31,7 +31,10 @@ extern "C" { #define NEU_PARSE_ERROR "error" enum neu_parse_function { - NEU_PARSE_OP_READ_TAG_GROUP_LIST = 29, + NEU_PARSE_OP_GET_GROUP_CONFIG = 29, + NEU_PARSE_OP_ADD_GROUP_CONFIG = 90, + NEU_PARSE_OP_UPDATE_GROUP_CONFIG = 91, + NEU_PARSE_OP_DELETE_GROUP_CONFIG = 92, NEU_PARSE_OP_READ = 50, NEU_PARSE_OP_WRITE = 51, NEU_PARSE_OP_LOGIN = 10, diff --git a/src/parser/neu_json_tag_group.c b/src/parser/neu_json_tag_group.c deleted file mode 100644 index 668b4b26..00000000 --- a/src/parser/neu_json_tag_group.c +++ /dev/null @@ -1,100 +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 "utils/json.h" - -#include "neu_json_tag_group.h" - -#define GROUP "group" -#define NAMES "names" -#define NAME "name" -#define TYPE "type" -#define TIMESTAMP "timestamp" -#define VALUE "value" -#define TAGS "tags" - -int neu_paser_decode_read_tag_group_list_req( - char *buf, struct neu_paser_read_tag_group_list_req **result) -{ - struct neu_paser_read_tag_group_list_req *req = - malloc(sizeof(struct neu_paser_read_tag_group_list_req)); - memset(req, 0, sizeof(struct neu_paser_read_tag_group_list_req)); - - neu_json_elem_t elem[] = { { - .name = NEU_PARSE_UUID, - .t = NEU_JSON_STR, - }, - { - .name = "config", - .t = NEU_JSON_STR, - } - - }; - - int ret = neu_json_decode(buf, NEU_JSON_ELEM_SIZE(elem), elem); - if (ret != 0) { - free(req); - return -1; - } - req->function = NEU_PARSE_OP_READ_TAG_GROUP_LIST; - req->uuid = elem[0].v.val_str; - req->config = elem[1].v.val_str; - - *result = req; - return 0; -} - -int neu_parse_encode_read_tag_group_list_res( - struct neu_paser_read_tag_group_list_res *res, char **buf) -{ - void *array = NULL; - for (int i = 0; i < res->n_group; i++) { - neu_json_elem_t group[] = { { - .name = NAME, - .t = NEU_JSON_STR, - .v.val_str = res->names[i].name, - } }; - array = neu_json_encode_array(array, group, 1); - } - neu_json_elem_t elems[] = { { - .name = NEU_PARSE_FUNCTION, - .t = NEU_JSON_INT, - .v.val_int = - NEU_PARSE_OP_READ_TAG_GROUP_LIST, - }, - { - .name = NEU_PARSE_UUID, - .t = NEU_JSON_STR, - .v.val_str = res->uuid, - }, - { - .name = NEU_PARSE_ERROR, - .t = NEU_JSON_INT, - .v.val_int = res->error, - }, - { - .name = "groups", - .t = NEU_JSON_OBJECT, - .v.object = array, - } }; - return neu_json_encode(elems, 4, buf); -} \ No newline at end of file diff --git a/src/parser/neu_json_tag_group.h b/src/parser/neu_json_tag_group.h deleted file mode 100644 index 597565e2..00000000 --- a/src/parser/neu_json_tag_group.h +++ /dev/null @@ -1,49 +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. - **/ -#ifndef _NEU_JSON_API_TAG_GROUP_H_ -#define _NEU_JSON_API_TAG_GROUP_H_ - -#include - -#include "neu_json_parser.h" - -struct neu_paser_read_tag_group_list_req { - enum neu_parse_function function; - char * uuid; - char * config; -}; - -struct neu_paser_read_tag_group_list_name { - char *name; -}; - -struct neu_paser_read_tag_group_list_res { - enum neu_parse_function function; - char * uuid; - int error; - int n_group; - struct neu_paser_read_tag_group_list_name *names; -}; - -int neu_paser_decode_read_tag_group_list_req( - char *buf, struct neu_paser_read_tag_group_list_req **result); -int neu_parse_encode_read_tag_group_list_res( - struct neu_paser_read_tag_group_list_res *res, char **buf); - -#endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index feaa8a72..53dc3d24 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -29,12 +29,12 @@ target_include_directories(modbus_point_test PRIVATE ) target_link_libraries(modbus_point_test neuron-base gtest_main gtest pthread) -add_executable(tag_group_config_test tag_group_config_test.cc ${SRC_PARSER}) -target_include_directories(tag_group_config_test PRIVATE +add_executable(json_group_config_test json_group_config_test.cc ${SRC_PARSER}) +target_include_directories(json_group_config_test PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include ) -target_link_libraries(tag_group_config_test neuron-base gtest_main gtest) +target_link_libraries(json_group_config_test neuron-base gtest_main gtest) add_executable(datatag_table_test datatag_table_test.cc ${CMAKE_SOURCE_DIR}/src/base/neu_datatag_table.c) target_include_directories(datatag_table_test PRIVATE @@ -55,6 +55,6 @@ gtest_discover_tests(json_test) gtest_discover_tests(api_json_test) gtest_discover_tests(variable_test) gtest_discover_tests(modbus_point_test) -gtest_discover_tests(tag_group_config_test) +gtest_discover_tests(json_group_config_test) gtest_discover_tests(datatag_table_test) gtest_discover_tests(json_datatag_table_test) diff --git a/tests/json_group_config_test.cc b/tests/json_group_config_test.cc new file mode 100644 index 00000000..384c7e68 --- /dev/null +++ b/tests/json_group_config_test.cc @@ -0,0 +1,189 @@ +#include +#include + +#include "parser/neu_json_group_config.h" +#include "parser/neu_json_parser.h" + +TEST(TagGroupConfigTest, neu_paser_decode_get_group_config_req) +{ + char *json = (char *) "{ \"function\": 29, \"uuid\": " + "\"1d892fe8-f37e-11eb-9a34-932aa06a28f3\", " + "\"nodeId\": 123456, " + "\"config\": \"Config0\" }"; + + void * result = NULL; + struct neu_paser_get_group_config_req *req = NULL; + + EXPECT_EQ(0, neu_parse_decode(json, &result)); + + req = (struct neu_paser_get_group_config_req *) result; + EXPECT_EQ(NEU_PARSE_OP_GET_GROUP_CONFIG, req->function); + EXPECT_STREQ("1d892fe8-f37e-11eb-9a34-932aa06a28f3", req->uuid); + EXPECT_EQ(123456, req->node_id); + EXPECT_STREQ("Config0", req->config); + neu_parse_decode_free(result); +} + +TEST(TagGroupConfigTest, neu_paser_decode_get_group_config_res) +{ + char *buf = (char *) "{\"function\": 29, \"uuid\": " + "\"1d892fe8-f37e-11eb-9a34-932aa06a28f3\", \"error\": " + "0, \"groups\": [{\"name\": \"config0\", \"nodeID\": " + "0, \"readInterval\": 0}, {\"name\": \"config1\", " + "\"nodeID\": 0, \"readInterval\": 0}, {\"name\": " + "\"config2\", \"nodeID\": 0, \"readInterval\": 0}]}"; + char * result = NULL; + struct neu_paser_get_group_config_res res = { + .function = NEU_PARSE_OP_GET_GROUP_CONFIG, + .uuid = (char *) "1d892fe8-f37e-11eb-9a34-932aa06a28f3", + .error = 0, + .n_config = 3, + }; + + res.rows = (struct neu_paser_get_group_config_res_row *) calloc( + 3, sizeof(struct neu_paser_get_group_config_res_row)); + + res.rows[0].name = strdup((char *) "config0"); + res.rows[1].name = strdup((char *) "config1"); + res.rows[2].name = strdup((char *) "config2"); + + EXPECT_EQ(0, neu_parse_encode(&res, &result)); + EXPECT_STREQ(buf, result); + + free(res.rows[0].name); + free(res.rows[1].name); + free(res.rows[2].name); + free(res.rows); + free(result); +} + +TEST(TagGroupConfigTest, neu_paser_decode_add_group_config_req) +{ + char *buf = (char *) "{\"function\":90, " + "\"uuid\":\"554f5fd8-f437-11eb-975c-7704b9e17821\", " + "\"group\":\"Group0\", " + "\"config\":\"Config0\", " + "\"srcNodeId\":123456, " + "\"dstNodeId\":654321, " + "\"readInterval\": 2000 " + " }"; + + void * result = NULL; + struct neu_paser_add_group_config_req *req = NULL; + + EXPECT_EQ(0, neu_parse_decode(buf, &result)); + + req = (struct neu_paser_add_group_config_req *) result; + + EXPECT_EQ(NEU_PARSE_OP_ADD_GROUP_CONFIG, req->function); + EXPECT_STREQ("554f5fd8-f437-11eb-975c-7704b9e17821", req->uuid); + EXPECT_EQ(123456, req->src_node_id); + EXPECT_EQ(654321, req->dst_node_id); + EXPECT_EQ(2000, req->read_interval); + + neu_parse_decode_free(result); +} + +TEST(TagGroupConfigTest, neu_parse_encode_add_group_config_res) +{ + char *buf = (char *) "{\"function\": 90, " + "\"uuid\": \"554f5fd8-f437-11eb-975c-7704b9e17821\", " + "\"error\": 0}"; + char * result = NULL; + struct neu_paser_group_config_res res = { + .function = NEU_PARSE_OP_ADD_GROUP_CONFIG, + .uuid = (char *) "554f5fd8-f437-11eb-975c-7704b9e17821", + .error = 0, + }; + + EXPECT_EQ(0, neu_parse_encode(&res, &result)); + EXPECT_STREQ(buf, result); + + free(result); +} + +TEST(TagGroupConfigTest, neu_parse_encode_update_group_config_req) +{ + char *buf = (char *) "{\"function\":91, " + "\"uuid\":\"554f5fd8-f437-11eb-975c-7704b9e17821\", " + "\"group\":\"Group0\", " + "\"config\":\"Config0\", " + "\"srcNodeId\":123456, " + "\"dstNodeId\":654321, " + "\"readInterval\": 2000 " + " }"; + + void * result = NULL; + struct neu_paser_update_group_config_req *req = NULL; + + EXPECT_EQ(0, neu_parse_decode(buf, &result)); + + req = (struct neu_paser_update_group_config_req *) result; + + EXPECT_EQ(NEU_PARSE_OP_UPDATE_GROUP_CONFIG, req->function); + EXPECT_STREQ("554f5fd8-f437-11eb-975c-7704b9e17821", req->uuid); + EXPECT_EQ(123456, req->src_node_id); + EXPECT_EQ(654321, req->dst_node_id); + EXPECT_EQ(2000, req->read_interval); + + neu_parse_decode_free(result); +} + +TEST(TagGroupConfigTest, neu_parse_encode_update_group_config_res) +{ + char *buf = (char *) "{\"function\": 91, " + "\"uuid\": \"554f5fd8-f437-11eb-975c-7704b9e17821\", " + "\"error\": 0}"; + char * result = NULL; + struct neu_paser_group_config_res res = { + .function = NEU_PARSE_OP_UPDATE_GROUP_CONFIG, + .uuid = (char *) "554f5fd8-f437-11eb-975c-7704b9e17821", + .error = 0, + }; + + EXPECT_EQ(0, neu_parse_encode(&res, &result)); + EXPECT_STREQ(buf, result); + + free(result); +} + +TEST(TagGroupConfigTest, neu_parse_encode_delete_group_config_req) +{ + char *buf = (char *) "{\"function\":92, " + "\"uuid\":\"554f5fd8-f437-11eb-975c-7704b9e17821\", " + "\"group\":\"Group0\", " + "\"config\":\"Config0\", " + "\"nodeId\":123456 " + "}"; + + void * result = NULL; + struct neu_paser_delete_group_config_req *req = NULL; + + EXPECT_EQ(0, neu_parse_decode(buf, &result)); + + req = (struct neu_paser_delete_group_config_req *) result; + + EXPECT_EQ(NEU_PARSE_OP_DELETE_GROUP_CONFIG, req->function); + EXPECT_STREQ("554f5fd8-f437-11eb-975c-7704b9e17821", req->uuid); + EXPECT_EQ(123456, req->node_id); + + neu_parse_decode_free(result); +} + +TEST(TagGroupConfigTest, neu_parse_encode_delete_group_config_res) +{ + char *buf = (char *) "{\"function\": 92, " + "\"uuid\": \"554f5fd8-f437-11eb-975c-7704b9e17821\", " + "\"error\": 0}"; + char * result = NULL; + struct neu_paser_group_config_res res = { + .function = NEU_PARSE_OP_DELETE_GROUP_CONFIG, + .uuid = (char *) "554f5fd8-f437-11eb-975c-7704b9e17821", + .error = 0, + }; + + EXPECT_EQ(0, neu_parse_encode(&res, &result)); + EXPECT_STREQ(buf, result); + + free(result); +} \ No newline at end of file diff --git a/tests/tag_group_config_test.cc b/tests/tag_group_config_test.cc deleted file mode 100644 index 00f3c52f..00000000 --- a/tests/tag_group_config_test.cc +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include - -#include "parser/neu_json_parser.h" -#include "parser/neu_json_tag_group.h" - -TEST(TagGroupConfigTest, neu_paser_decode_read_tag_group_list_req) -{ - char *json = (char *) "{ \"function\": 29, \"uuid\": " - "\"1d892fe8-f37e-11eb-9a34-932aa06a28f3\", " - "\"config\": \"Config0\" }"; - - void * result = NULL; - struct neu_paser_read_tag_group_list_req *req = NULL; - - EXPECT_EQ(0, neu_parse_decode(json, &result)); - - req = (struct neu_paser_read_tag_group_list_req *) result; - EXPECT_EQ(NEU_PARSE_OP_READ_TAG_GROUP_LIST, req->function); - EXPECT_STREQ("1d892fe8-f37e-11eb-9a34-932aa06a28f3", req->uuid); - EXPECT_STREQ("Config0", req->config); - neu_parse_decode_free(result); -} - -TEST(TagGroupConfigTest, neu_paser_decode_read_tag_group_list_res) -{ - char *buf = (char *) "{\"function\": 29, \"uuid\": " - "\"1d892fe8-f37e-11eb-9a34-932aa06a28f3\", \"error\": " - "0, \"groups\": [{\"name\": \"Group0\"}, {\"name\": " - "\"Group1\"}, {\"name\": \"Group2\"}]}"; - char * result = NULL; - struct neu_paser_read_tag_group_list_res res = { - .function = NEU_PARSE_OP_READ_TAG_GROUP_LIST, - .uuid = (char *) "1d892fe8-f37e-11eb-9a34-932aa06a28f3", - .error = 0, - .n_group = 3, - }; - - res.names = (struct neu_paser_read_tag_group_list_name *) calloc( - 3, sizeof(struct neu_paser_read_tag_group_list_name)); - - res.names[0].name = strdup((char *) "Group0"); - res.names[1].name = strdup((char *) "Group1"); - res.names[2].name = strdup((char *) "Group2"); - - EXPECT_EQ(0, neu_parse_encode(&res, &result)); - EXPECT_STREQ(buf, result); - - free(res.names[0].name); - free(res.names[1].name); - free(res.names[2].name); - free(res.names); - free(result); -} \ No newline at end of file