From ef9ddd30ea58279c825c2a4d501ae6a5b9817cbc Mon Sep 17 00:00:00 2001 From: eeff <33566201+eeff@users.noreply.github.com> Date: Wed, 31 May 2023 16:18:51 +0800 Subject: [PATCH] feat: add json parser for north driver map requests --- src/core/manager.c | 27 ++++++++ src/parser/neu_json_ndriver.c | 118 ++++++++++++++++++++++++++++++++++ src/parser/neu_json_ndriver.h | 43 +++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 src/parser/neu_json_ndriver.c create mode 100644 src/parser/neu_json_ndriver.h diff --git a/src/core/manager.c b/src/core/manager.c index c78cccd4..900aaf13 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -917,6 +917,33 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data) case NEU_RESP_READ_GROUP: forward_msg(manager, msg, header->receiver); break; + + case NEU_REQ_ADD_NDRIVER_MAP: { + neu_req_ndriver_map_t *cmd = (neu_req_ndriver_map_t *) &header[1]; + neu_resp_error_t error = { 0 }; + + // TODO + nlog_info("add map ndriver:`%s` driver:`%s` group:`%s`", cmd->ndriver, + cmd->driver, cmd->group); + + header->type = NEU_RESP_ERROR; + strcpy(header->receiver, header->sender); + reply(manager, header, &error); + break; + } + case NEU_REQ_DEL_NDRIVER_MAP: { + neu_req_ndriver_map_t *cmd = (neu_req_ndriver_map_t *) &header[1]; + neu_resp_error_t error = { 0 }; + + // TODO + nlog_info("del map ndriver:`%s` driver:`%s` group:`%s`", cmd->ndriver, + cmd->driver, cmd->group); + + header->type = NEU_RESP_ERROR; + strcpy(header->receiver, header->sender); + reply(manager, header, &error); + break; + } case NEU_REQ_UPDATE_LOG_LEVEL: if (neu_node_manager_find(manager->node_manager, header->receiver) == NULL) { diff --git a/src/parser/neu_json_ndriver.c b/src/parser/neu_json_ndriver.c new file mode 100644 index 00000000..910e3be2 --- /dev/null +++ b/src/parser/neu_json_ndriver.c @@ -0,0 +1,118 @@ +/** + * 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_ndriver.h" + +int neu_json_encode_ndriver_map(void *json_object, void *param) +{ + int ret = 0; + neu_json_ndriver_map_t *req = param; + + neu_json_elem_t req_elems[] = { + { + .name = "ndriver", + .t = NEU_JSON_STR, + .v.val_str = req->ndriver, + }, + { + .name = "driver", + .t = NEU_JSON_STR, + .v.val_str = req->driver, + }, + { + .name = "group", + .t = NEU_JSON_STR, + .v.val_str = req->group, + }, + }; + + ret = neu_json_encode_field(json_object, req_elems, + NEU_JSON_ELEM_SIZE(req_elems)); + + return ret; +} + +void neu_json_decode_ndriver_map_free(neu_json_ndriver_map_t *req); + +int neu_json_decode_ndriver_map(char *buf, neu_json_ndriver_map_t **result) +{ + int ret = 0; + void * json_obj = NULL; + neu_json_ndriver_map_t *req = NULL; + + json_obj = neu_json_decode_new(buf); + if (NULL == json_obj) { + return -1; + } + + req = calloc(1, sizeof(*req)); + if (req == NULL) { + goto error; + } + + neu_json_elem_t req_elems[] = { + { + .name = "ndriver", + .t = NEU_JSON_STR, + }, + { + .name = "driver", + .t = NEU_JSON_STR, + }, + { + .name = "group", + .t = NEU_JSON_STR, + }, + }; + ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(req_elems), + req_elems); + req->ndriver = req_elems[0].v.val_str; + req->driver = req_elems[1].v.val_str; + req->group = req_elems[2].v.val_str; + + if (ret != 0) { + goto error; + } + + *result = req; + neu_json_decode_free(json_obj); + return 0; + +error: + neu_json_decode_ndriver_map_free(req); + neu_json_decode_free(json_obj); + return -1; +} + +void neu_json_decode_ndriver_map_free(neu_json_ndriver_map_t *req) +{ + if (req) { + free(req->ndriver); + free(req->driver); + free(req->group); + free(req); + } +} diff --git a/src/parser/neu_json_ndriver.h b/src/parser/neu_json_ndriver.h new file mode 100644 index 00000000..b50e8d7d --- /dev/null +++ b/src/parser/neu_json_ndriver.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_NDRIVER_H_ +#define _NEU_JSON_API_NEU_JSON_NDRIVER_H_ + +#include "json/json.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + char *ndriver; + char *driver; + char *group; +} neu_json_ndriver_map_t; + +int neu_json_encode_ndriver_map(void *json_object, void *param); +int neu_json_decode_ndriver_map(char *buf, neu_json_ndriver_map_t **result); +void neu_json_decode_ndriver_map_free(neu_json_ndriver_map_t *req); + +#ifdef __cplusplus +} +#endif + +#endif