From 9f58ec484fc39b6a194b147634f912d525d0bf15 Mon Sep 17 00:00:00 2001 From: Philipp Jungkamp Date: Tue, 18 Apr 2023 13:18:14 +0200 Subject: [PATCH] node-iec61850-8-1: fix ConfigError reporting Signed-off-by: Philipp Jungkamp --- include/villas/nodes/iec61850_goose.hpp | 18 ++-- lib/nodes/iec61850_goose.cpp | 123 ++++++++++-------------- 2 files changed, 59 insertions(+), 82 deletions(-) diff --git a/include/villas/nodes/iec61850_goose.hpp b/include/villas/nodes/iec61850_goose.hpp index 7c1765657..63afc69a1 100644 --- a/include/villas/nodes/iec61850_goose.hpp +++ b/include/villas/nodes/iec61850_goose.hpp @@ -202,17 +202,15 @@ protected: static void publish_values(GoosePublisher publisher, std::vector &values, bool changed, int burst = 1) noexcept; static void resend_thread(GooseNode::Output *output) noexcept; - int _parse(json_t *json, json_error_t *err); + void parseInput(json_t *json); + void parseSubscriber(json_t *json, SubscriberConfig &sc); + void parseSubscribers(json_t *json, std::map &ctx); + void parseInputSignals(json_t *json, std::vector &mappings); - int parseInput(json_t *json, json_error_t *err); - int parseSubscriber(json_t *json, json_error_t *err, SubscriberConfig &sc); - int parseSubscribers(json_t *json, json_error_t *err, std::map &ctx); - int parseInputSignals(json_t *json, json_error_t *err, std::vector &mappings); - - int parseOutput(json_t *json, json_error_t *err); - int parsePublisherData(json_t *json, json_error_t *err, std::vector &data); - int parsePublisher(json_t *json, json_error_t *err, PublisherConfig &pc); - int parsePublishers(json_t *json, json_error_t *err, std::vector &ctx); + void parseOutput(json_t *json); + void parsePublisherData(json_t *json, std::vector &data); + void parsePublisher(json_t *json, PublisherConfig &pc); + void parsePublishers(json_t *json, std::vector &ctx); virtual int _read(struct Sample *smps[], unsigned cnt) override; diff --git a/lib/nodes/iec61850_goose.cpp b/lib/nodes/iec61850_goose.cpp index 338ed9750..7b97c3f57 100644 --- a/lib/nodes/iec61850_goose.cpp +++ b/lib/nodes/iec61850_goose.cpp @@ -607,78 +607,63 @@ int GooseNode::parse(json_t *json, const uuid_t sn_uuid) if (ret) return ret; - ret = _parse(json, &err); - if (ret) - throw ConfigError(json, err, "node-config-node-iec61850-goose"); - - return 0; -} - -int GooseNode::_parse(json_t *json, json_error_t *err) -{ - int ret; - json_t *in_json = nullptr; json_t *out_json = nullptr; - ret = json_unpack_ex(json, err, 0, "{ s: o, s: o }", + ret = json_unpack_ex(json, &err, 0, "{ s: o, s: o }", "in", &in_json, "out", &out_json ); - if (ret) return ret; + if (ret) + throw ConfigError(json, err, "node-config-node-iec61850-8-1"); - ret = parseInput(in_json, err); - if (ret) return ret; - - ret = parseOutput(out_json, err); - if (ret) return ret; + parseInput(in_json); + parseOutput(out_json); return 0; } -int GooseNode::parseInput(json_t *json, json_error_t *err) +void GooseNode::parseInput(json_t *json) { int ret; + json_error_t err; json_t *subscribers_json = nullptr; json_t *signals_json = nullptr; char const *interface_id = input.interface_id.c_str(); int with_timestamp = true; - ret = json_unpack_ex(json, err, 0, "{ s: o, s: o, s?: s, s: b }", + ret = json_unpack_ex(json, &err, 0, "{ s: o, s: o, s?: s, s: b }", "subscribers", &subscribers_json, "signals", &signals_json, "interface", &interface_id, "with_timestamp", &with_timestamp ); - if (ret) return ret; + if (ret) + throw ConfigError(json, err, "node-config-node-iec61850-8-1"); - ret = parseSubscribers(subscribers_json, err, input.contexts); - if (ret) return ret; - - ret = parseInputSignals(signals_json, err, input.mappings); - if (ret) return ret; + parseSubscribers(subscribers_json, input.contexts); + parseInputSignals(signals_json, input.mappings); input.interface_id = interface_id; - input.with_timestamp = with_timestamp; - - return 0; } -int GooseNode::parseSubscriber(json_t *json, json_error_t *err, GooseNode::SubscriberConfig &sc) +void GooseNode::parseSubscriber(json_t *json, GooseNode::SubscriberConfig &sc) { int ret; + json_error_t err; char *go_cb_ref = nullptr; char *dst_address_str = nullptr; char *trigger = nullptr; int app_id = 0; - ret = json_unpack_ex(json, err, 0, "{ s: s, s?: s, s?: s, s?: i }", + ret = json_unpack_ex(json, &err, 0, "{ s: s, s?: s, s?: s, s?: i }", "go_cb_ref", &go_cb_ref, "trigger", &trigger, "dst_address", &dst_address_str, "app_id", &app_id ); - if (ret) return ret; + if (ret) + throw ConfigError(json, err, "node-config-node-iec61850-8-1"); sc.go_cb_ref = std::string { go_cb_ref }; @@ -698,31 +683,29 @@ int GooseNode::parseSubscriber(json_t *json, json_error_t *err, GooseNode::Subsc if (app_id != 0) sc.app_id = static_cast(app_id); - - return 0; } -int GooseNode::parseSubscribers(json_t *json, json_error_t *err, std::map &ctx) +void GooseNode::parseSubscribers(json_t *json, std::map &ctx) { - int ret; char const* key; json_t* subscriber_json; + if (!json_is_object(json)) + throw RuntimeError("subscribers is not an object"); + json_object_foreach(json, key, subscriber_json) { SubscriberConfig sc; - ret = parseSubscriber(subscriber_json, err, sc); - if (ret) return ret; + parseSubscriber(subscriber_json, sc); ctx[key] = InputEventContext { sc }; } - - return 0; } -int GooseNode::parseInputSignals(json_t *json, json_error_t *err, std::vector &mappings) +void GooseNode::parseInputSignals(json_t *json, std::vector &mappings) { int ret; + json_error_t err; int index; json_t *value; @@ -732,12 +715,13 @@ int GooseNode::parseInputSignals(json_t *json, json_error_t *err, std::vector &data) +void GooseNode::parsePublisherData(json_t *json, std::vector &data) { int ret; + json_error_t err; int index; json_t* signal_or_value_json; - json_array_foreach(json, index, signal_or_value_json) { + if (!json_is_array(json)) + throw RuntimeError("publisher data is not an array"); + json_array_foreach(json, index, signal_or_value_json) { char const *mms_type = nullptr; char const *signal_str = nullptr; json_t *value_json = nullptr; int bitstring_size = -1; - ret = json_unpack_ex(signal_or_value_json, err, 0, "{ s:s, s?:s, s?:o, s?:i }", + ret = json_unpack_ex(signal_or_value_json, &err, 0, "{ s:s, s?:s, s?:o, s?:i }", "mms_type", &mms_type, "signal", &signal_str, "value", &value_json, "mms_bitstring_size", &bitstring_size ); - if (ret) return ret; + if (ret) + throw ConfigError(json, err, "node-config-node-iec61850-8-1"); auto goose_type = GooseSignal::lookupMmsTypeName(mms_type).value(); std::optional meta = std::nullopt; @@ -804,7 +789,8 @@ int GooseNode::parsePublisherData(json_t *json, json_error_t *err, std::vectorsignal_type, value_json); - if (ret) return ret; + if (ret) + throw ConfigError(json, err, "node-config-node-iec61850-8-1"); } auto signal = std::optional {}; @@ -819,13 +805,12 @@ int GooseNode::parsePublisherData(json_t *json, json_error_t *err, std::vector &ctx) +void GooseNode::parsePublishers(json_t *json, std::vector &ctx) { - int ret; int index; json_t* publisher_json; json_array_foreach(json, index, publisher_json) { PublisherConfig pc; - ret = parsePublisher(publisher_json, err, pc); - if (ret) return ret; + parsePublisher(publisher_json, pc); ctx.push_back(OutputContext { pc }); } - - return 0; } std::vector GooseNode::getPollFDs()