diff --git a/src/parser/neu_json_global_config.c b/src/parser/neu_json_global_config.c new file mode 100644 index 00000000..0e87521a --- /dev/null +++ b/src/parser/neu_json_global_config.c @@ -0,0 +1,61 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2023 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 "json/json.h" + +#include "neu_json_global_config.h" + +int neu_json_decode_global_config_req(char * buf, + neu_json_global_config_req_t **result) +{ + neu_json_global_config_req_t *req = calloc(1, sizeof(*req)); + if (NULL == req) { + return -1; + } + + void *json_obj = neu_json_decode_new(buf); + if (NULL == json_obj) { + free(req); + return -1; + } + + int ret = 0; + if (0 == neu_json_decode_get_nodes_resp_json(json_obj, &req->nodes)) { + *result = req; + } else { + ret = -1; + neu_json_decode_global_config_req_free(req); + } + + neu_json_decode_free(json_obj); + return ret; +} + +void neu_json_decode_global_config_req_free(neu_json_global_config_req_t *req) +{ + if (req) { + neu_json_decode_get_nodes_resp_free(req->nodes); + free(req); + } +} diff --git a/src/parser/neu_json_global_config.h b/src/parser/neu_json_global_config.h new file mode 100644 index 00000000..f8055178 --- /dev/null +++ b/src/parser/neu_json_global_config.h @@ -0,0 +1,43 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2023 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_NEU_JSON_GLOBAL_CONFIG_H_ +#define _NEU_JSON_API_NEU_JSON_GLOBAL_CONFIG_H_ + +#include "json/json.h" + +#include "parser/neu_json_node.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + neu_json_get_nodes_resp_t *nodes; +} neu_json_global_config_req_t; + +int neu_json_decode_global_config_req(char * buf, + neu_json_global_config_req_t **result); +void neu_json_decode_global_config_req_free(neu_json_global_config_req_t *req); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/parser/neu_json_node.c b/src/parser/neu_json_node.c index db1e960f..e11509fc 100644 --- a/src/parser/neu_json_node.c +++ b/src/parser/neu_json_node.c @@ -281,6 +281,94 @@ int neu_json_encode_get_nodes_resp(void *json_object, void *param) return ret; } +int neu_json_decode_get_nodes_resp(char * buf, + neu_json_get_nodes_resp_t **result) +{ + json_t *json_obj = neu_json_decode_new(buf); + if (NULL == json_obj) { + return -1; + } + + int ret = neu_json_decode_get_nodes_resp_json(json_obj, result); + neu_json_decode_free(json_obj); + return ret; +} + +int neu_json_decode_get_nodes_resp_json(void * json_obj, + neu_json_get_nodes_resp_t **result) +{ + int ret = 0; + json_t * node_array = NULL; + neu_json_get_nodes_resp_t *resp = NULL; + int i = 0; + + node_array = json_object_get(json_obj, "nodes"); + if (!json_is_array(node_array)) { + goto decode_fail; + } + + resp = calloc(1, sizeof(*resp)); + if (resp == NULL) { + goto decode_fail; + } + + resp->n_node = json_array_size(node_array); + if (resp->n_node <= 0) { + goto decode_fail; + } + + resp->nodes = calloc(resp->n_node, sizeof(neu_json_get_nodes_resp_node_t)); + if (NULL == resp->nodes) { + goto decode_fail; + } + + for (i = 0; i < resp->n_node; i++) { + neu_json_elem_t tag_elems[] = { + { + .name = "name", + .t = NEU_JSON_STR, + }, + { + .name = "plugin", + .t = NEU_JSON_STR, + }, + }; + + ret = neu_json_decode_by_json(json_array_get(node_array, i), + NEU_JSON_ELEM_SIZE(tag_elems), tag_elems); + + // set the fields before check for easy clean up on error + resp->nodes[i].name = tag_elems[0].v.val_str; + resp->nodes[i].plugin = tag_elems[1].v.val_str; + + if (ret != 0) { + goto decode_fail; + } + } + + *result = resp; + goto decode_exit; + +decode_fail: + neu_json_decode_get_nodes_resp_free(resp); + ret = -1; + +decode_exit: + return ret; +} + +void neu_json_decode_get_nodes_resp_free(neu_json_get_nodes_resp_t *resp) +{ + if (resp) { + for (int i = 0; i < resp->n_node; ++i) { + free(resp->nodes[i].name); + free(resp->nodes[i].plugin); + } + free(resp->nodes); + free(resp); + } +} + int neu_json_decode_update_node_req(char * buf, neu_json_update_node_req_t **result) { diff --git a/src/parser/neu_json_node.h b/src/parser/neu_json_node.h index 717f6f56..164265cb 100644 --- a/src/parser/neu_json_node.h +++ b/src/parser/neu_json_node.h @@ -80,7 +80,12 @@ typedef struct { neu_json_get_nodes_resp_node_t *nodes; } neu_json_get_nodes_resp_t; -int neu_json_encode_get_nodes_resp(void *json_object, void *param); +int neu_json_encode_get_nodes_resp(void *json_object, void *param); +int neu_json_decode_get_nodes_resp(char * buf, + neu_json_get_nodes_resp_t **result); +int neu_json_decode_get_nodes_resp_json(void * json_obj, + neu_json_get_nodes_resp_t **result); +void neu_json_decode_get_nodes_resp_free(neu_json_get_nodes_resp_t *result); typedef struct { char * name;