diff --git a/include/villas/nodes/gtwif.h b/include/villas/nodes/gtwif.h index 19676b4a7..fe37b9a6f 100644 --- a/include/villas/nodes/gtwif.h +++ b/include/villas/nodes/gtwif.h @@ -33,7 +33,7 @@ #include "node.h" #include "list.h" -#include "rscad.h" +#include "rtds/rscad.h" struct gtwif { struct sockaddr_in remote; /**< The IP / port address of the RTDS racks. */ diff --git a/include/villas/rtds/gtwif.h b/include/villas/rtds/gtwif.h new file mode 100644 index 000000000..06a33bdd5 --- /dev/null +++ b/include/villas/rtds/gtwif.h @@ -0,0 +1,50 @@ +/** Parsers for RSCAD file formats + * + * @file + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLASnode + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *********************************************************************************/ + +#pragma once + +#include +#include + +#define GTWIF_CMD_MODIFY 0x004D +#define GTWIF_CMD_EXAMINE 0x0058 +#define GTWIF_CMD_READLIST2 0x0143 +#define GTWIF_CMD_PING 0x0136 + +/** Send command CMD_READLIST2 to a GTWIF card. + * + * This command reads a list of signals from the GTWIF card. + */ +int gtwif_cmd_readlist2(int sd, uint32_t cnt, uint32_t addrs[], uint32_t vals[]); + +/** Send command CMD_EXAMINE to a GTWIF card. + * + * This command modifies a memory location in the GTWIF memory. + */ +int gtwif_cmd_examine(int sd, uint32_t addr, uint32_t *val); + +int gtwif_cmd_modify(int sd, uint32_t addr, uint32_t val); + +int gtwif_cmd_modify_many(int sd, uint32_t cnt, uint32_t addrs[], uint32_t vals[], uint32_t old_vals[]); + +int gtwif_cmd_ping(int sd, char *name, size_t namelen, char *casename, size_t casenamelen); diff --git a/include/villas/rscad.h b/include/villas/rtds/rscad.h similarity index 100% rename from include/villas/rscad.h rename to include/villas/rtds/rscad.h diff --git a/lib/nodes/gtwif.c b/lib/nodes/gtwif.c index e174f3cba..8104606f0 100644 --- a/lib/nodes/gtwif.c +++ b/lib/nodes/gtwif.c @@ -27,105 +27,10 @@ #include "plugin.h" #include "timing.h" #include "nodes/gtwif.h" - -#define GTWIF_CMD_MODIFY 0x004D -#define GTWIF_CMD_READLIST2 0x0143 +#include "rtds/gtwif.h" #define GTWIF_MAX_RATE 10.0 -static int gtwif_cmd_readlist2(int sd, uint32_t cnt, uint32_t addrs[], uint32_t vals[]) -{ - ssize_t sent, recvd; - - uint32_t buf[cnt + 4]; - - int retries = 3; - - buf[0] = htons(GTWIF_CMD_READLIST2); - buf[1] = 0; - buf[2] = 0; - buf[3] = cnt; - - for (int i = 0; i < cnt; i++) - buf[i+3] = addrs[i]; - -retry: sent = send(sd, buf, sizeof(buf), 0); - if (sent < 0) - return -1; - - recvd = recv(sd, buf, sizeof(buf), 0); - if (recvd < 0) { - if (errno == ETIMEDOUT && retries > 0) { - retries--; - goto retry; - } - else - return -1; - } - - if (buf[0] != htons(GTWIF_CMD_READLIST2)) - return -1; - - /* We update the number of values with the returned number of values */ - cnt = buf[1]; - - for (int i = 0; i < cnt; i++) - vals[i] = buf[i+3]; - - return cnt; -} - -static int gtwif_cmd_modify(int sd, uint32_t addr, uint32_t val) -{ - ssize_t sent, recvd; - uint32_t buf[4]; - int retries = 3; - - buf[0] = htons(GTWIF_CMD_MODIFY); - buf[1] = htonl(addr); - buf[2] = htonl(1); - buf[3] = htonl(val); - -retry: sent = send(sd, buf, sizeof(buf), 0); - if (sent < 0) - return -1; - - recvd = recv(sd, buf, sizeof(buf), 0); - if (recvd < 0) { - if (errno == ETIMEDOUT && retries > 0) { - retries--; - goto retry; - } - else - return -1; - } - - return 0; -} - -/** Sends multiple CMD_MODIFY commands to the GTWIF card. - * - * We we only modify values which have been changed. - * This is checked by comparing \p vals[] with \p old_vals[] - */ -static int gtwif_cmd_modify_many(int sd, uint32_t cnt, uint32_t addrs[], uint32_t vals[], uint32_t old_vals[]) -{ - int ret, mod = 0; - - for (int i = 0; i < cnt; i++) { - if (old_vals && vals[i] == old_vals[i]) - continue; - - ret = gtwif_cmd_modify(sd, addrs[i], vals[i]); - if (ret) - continue; - - mod++; - } - - return mod; -} - static int gtwif_parse_direction(struct gtwif_direction *d, config_setting_t *cfg) { if (config_setting_type(cfg) != CONFIG_TYPE_ARRAY) diff --git a/lib/rtds/gtwif.c b/lib/rtds/gtwif.c new file mode 100644 index 000000000..b65ed53ec --- /dev/null +++ b/lib/rtds/gtwif.c @@ -0,0 +1,133 @@ +/** Node type: GTWIF - RSCAD protocol for RTDS + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + * @license GNU General Public License (version 3) + * + * VILLASnode + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *********************************************************************************/ + +#include +#include + +#include "rtds/gtwif.h" + +int gtwif_cmd_readlist2(int sd, uint32_t cnt, uint32_t addrs[], uint32_t vals[]) +{ + ssize_t sent, recvd; + + uint32_t buf[cnt + 4]; + + int retries = 3; + + buf[0] = htons(GTWIF_CMD_READLIST2); + buf[1] = 0; + buf[2] = 0; + buf[3] = cnt; + + for (int i = 0; i < cnt; i++) + buf[i+3] = addrs[i]; + +retry: sent = send(sd, buf, sizeof(buf), 0); + if (sent < 0) + return -1; + + recvd = recv(sd, buf, sizeof(buf), 0); + if (recvd < 0) { + if (errno == ETIMEDOUT && retries > 0) { + retries--; + goto retry; + } + else + return -1; + } + + if (buf[0] != htons(GTWIF_CMD_READLIST2)) + return -1; + + /* We update the number of values with the returned number of values */ + cnt = buf[1]; + + for (int i = 0; i < cnt; i++) + vals[i] = buf[i+3]; + + return cnt; +} + +int gtwif_cmd_modify(int sd, uint32_t addr, uint32_t val) +{ + ssize_t sent, recvd; + uint32_t buf[4]; + int retries = 3; + + buf[0] = htons(GTWIF_CMD_MODIFY); + buf[1] = htonl(addr); + buf[2] = htonl(1); + buf[3] = htonl(val); + +retry: sent = send(sd, buf, sizeof(buf), 0); + if (sent < 0) + return -1; + + recvd = recv(sd, buf, sizeof(buf), 0); + if (recvd < 0) { + if (errno == ETIMEDOUT && retries > 0) { + retries--; + goto retry; + } + else + return -1; + } + + return 0; +} + +/** Sends multiple CMD_MODIFY commands to the GTWIF card. + * + * We we only modify values which have been changed. + * This is checked by comparing \p vals[] with \p old_vals[] + */ +int gtwif_cmd_modify_many(int sd, uint32_t cnt, uint32_t addrs[], uint32_t vals[], uint32_t old_vals[]) +{ + int ret, mod = 0; + + for (int i = 0; i < cnt; i++) { + if (old_vals && vals[i] == old_vals[i]) + continue; + + ret = gtwif_cmd_modify(sd, addrs[i], vals[i]); + if (ret) + continue; + + mod++; + } + + return mod; +} + +int gtwif_cmd_ping(int sd, char *name, size_t namelen, char *casename, size_t casenamelen) +{ + /**< @todo Implement */ + + return -1; +} + +int gtwif_cmd_examine(int sd, uint32_t addr, uint32_t *val) +{ + /**< @todo Implement */ + + return -1; +} diff --git a/lib/rscad.c b/lib/rtds/rscad.c similarity index 99% rename from lib/rscad.c rename to lib/rtds/rscad.c index 264844d3d..75e89087c 100644 --- a/lib/rscad.c +++ b/lib/rtds/rscad.c @@ -24,7 +24,7 @@ #include #include "log.h" -#include "rscad.h" +#include "rtds/rscad.h" static int rscad_inf_destroy_attribute(struct rscad_inf_attribute *a) { diff --git a/tests/unit/rscad.c b/tests/unit/rtds.c similarity index 94% rename from tests/unit/rscad.c rename to tests/unit/rtds.c index 493d593fe..8d6af1b61 100644 --- a/tests/unit/rscad.c +++ b/tests/unit/rtds.c @@ -22,11 +22,17 @@ #include -#include "rscad.h" +#include "rtds/rscad.h" +#include "rtds/gtwif.h" #define PATH_INF "tests/data/rscad/vdiv.inf" -Test(rscad, inf) +Test(rtds, gtwif) +{ + +} + +Test(rtds, rscad_inf) { int ret; struct rscad_inf i;