2014-12-05 12:30:48 +01:00
|
|
|
/** Node type: OPAL (AsyncApi)
|
|
|
|
*
|
|
|
|
* This file implements the opal subtype for nodes.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2015-06-02 21:53:04 +02:00
|
|
|
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
|
2015-08-07 01:11:43 +02:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-12-05 12:30:48 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2014-12-05 12:30:48 +01:00
|
|
|
#include "opal.h"
|
2015-03-17 22:46:46 +01:00
|
|
|
#include "utils.h"
|
|
|
|
|
2015-03-31 13:48:41 +02:00
|
|
|
static struct opal_global *og = NULL;
|
2015-06-02 22:04:03 +02:00
|
|
|
|
|
|
|
/** List of all opal nodes */
|
2015-05-06 11:48:30 +02:00
|
|
|
static struct list opals;
|
2015-03-17 22:46:46 +01:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
int opal_init(int argc, char *argv[], struct settings *set)
|
|
|
|
{ INDENT
|
2015-03-17 22:46:46 +01:00
|
|
|
int err;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
if (argc != 4)
|
|
|
|
return -1;
|
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
og = alloc(sizeof(struct opal_global));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
pthread_mutex_init(&og->lock, NULL);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
og->async_shmem_name = argv[1];
|
|
|
|
og->async_shmem_size = atoi(argv[2]);
|
|
|
|
og->print_shmem_name = argv[3];
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
/* Enable the OpalPrint function. This prints to the OpalDisplay. */
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalSystemCtrl_Register(og->print_shmem_name);
|
|
|
|
if (err != EOK)
|
2015-03-17 22:46:46 +01:00
|
|
|
error("OpalPrint() access not available (%d)", err);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
/* Open Share Memory created by the model. */
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalOpenAsyncMem(og->async_shmem_size, og->async_shmem_name);
|
|
|
|
if (err != EOK)
|
2015-03-17 22:46:46 +01:00
|
|
|
error("Model shared memory not available (%d)", err);
|
|
|
|
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalGetAsyncCtrlParameters(&og->params, sizeof(Opal_GenAsyncParam_Ctrl));
|
|
|
|
if (err != EOK)
|
2015-03-17 22:46:46 +01:00
|
|
|
error("Could not get OPAL controller parameters (%d)", err);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
/* Get list of Send and RecvIDs */
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalGetNbAsyncSendIcon(&og->send_icons);
|
|
|
|
if (err != EOK)
|
2015-03-17 22:46:46 +01:00
|
|
|
error("Failed to get number of send blocks (%d)", err);
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalGetNbAsyncRecvIcon(&og->recv_icons);
|
|
|
|
if (err != EOK)
|
2015-03-17 22:46:46 +01:00
|
|
|
error("Failed to get number of recv blocks (%d)", err);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
og->send_ids = alloc(og->send_icons * sizeof(int));
|
|
|
|
og->recv_ids = alloc(og->recv_icons * sizeof(int));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalGetAsyncSendIDList(og->send_ids, og->send_icons * sizeof(int));
|
|
|
|
if (err != EOK)
|
2015-03-17 22:46:46 +01:00
|
|
|
error("Failed to get list of send ids (%d)", err);
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalGetAsyncRecvIDList(og->recv_ids, og->recv_icons * sizeof(int));
|
|
|
|
if (err != EOK)
|
2015-03-17 22:46:46 +01:00
|
|
|
error("Failed to get list of recv ids (%d)", err);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
info("Started as OPAL Asynchronous process");
|
2015-05-07 13:57:43 +02:00
|
|
|
info("This is Simulator2Simulator Server (S2SS) %s (built on %s, %s)",
|
|
|
|
VERSION, __DATE__, __TIME__);
|
2015-09-22 13:20:20 +02:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
opal_print_global(og);
|
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
int opal_deinit()
|
2015-05-06 11:48:30 +02:00
|
|
|
{ INDENT
|
2015-03-17 22:59:43 +01:00
|
|
|
int err;
|
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
if (!og)
|
|
|
|
return 0;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalCloseAsyncMem(og->async_shmem_size, og->async_shmem_name);
|
|
|
|
if (err != EOK)
|
2015-03-20 18:55:14 +01:00
|
|
|
error("Failed to close shared memory area (%d)", err);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
debug(4, "Closing OPAL shared memory mapping");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-19 15:28:28 +02:00
|
|
|
err = OpalSystemCtrl_UnRegister(og->print_shmem_name);
|
|
|
|
if (err != EOK)
|
2015-03-20 18:55:14 +01:00
|
|
|
error("Failed to close shared memory for system control (%d)", err);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
pthread_mutex_destroy(&og->lock);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
free(og->send_ids);
|
|
|
|
free(og->recv_ids);
|
|
|
|
free(og);
|
2015-03-17 22:46:46 +01:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
og = NULL;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
int opal_print_global(struct opal_global *g)
|
|
|
|
{ INDENT
|
2015-09-22 12:58:37 +02:00
|
|
|
debug(2, "Controller ID: %u", og->params.controllerID);
|
|
|
|
|
|
|
|
char *sbuf = alloc(og->send_icons * 5);
|
|
|
|
char *rbuf = alloc(og->recv_icons * 5);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
for (int i = 0; i < og->send_icons; i++)
|
2015-09-22 12:58:37 +02:00
|
|
|
strcatf(&sbuf, "%u ", og->send_ids[i]);
|
2015-05-07 13:57:43 +02:00
|
|
|
for (int i = 0; i < og->recv_icons; i++)
|
2015-09-22 12:58:37 +02:00
|
|
|
strcatf(&rbuf, "%u ", og->recv_ids[i]);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
debug(2, "Send Blocks: %s", sbuf);
|
|
|
|
debug(2, "Receive Blocks: %s", rbuf);
|
2015-09-22 12:58:37 +02:00
|
|
|
|
|
|
|
free(sbuf);
|
|
|
|
free(rbuf);
|
2015-03-17 22:46:46 +01:00
|
|
|
|
2015-03-20 18:55:14 +01:00
|
|
|
debug(2, "Control Block Parameters:");
|
2015-10-06 09:10:08 +02:00
|
|
|
for (int i = 0; i < GENASYNC_NB_FLOAT_PARAM; i++)
|
2015-05-07 13:57:43 +02:00
|
|
|
debug(2, "FloatParam[]%u] = %f", i, og->params.FloatParam[i]);
|
2015-10-06 09:10:08 +02:00
|
|
|
for (int i = 0; i < GENASYNC_NB_STRING_PARAM; i++)
|
2015-05-07 13:57:43 +02:00
|
|
|
debug(2, "StringParam[%u] = %s", i, og->params.StringParam[i]);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
return 0;
|
2015-03-12 22:56:58 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 13:48:41 +02:00
|
|
|
int opal_parse(config_setting_t *cfg, struct node *n)
|
2015-05-06 11:48:30 +02:00
|
|
|
{
|
2015-03-31 13:48:41 +02:00
|
|
|
struct opal *o = alloc(sizeof(struct opal));
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 12:59:57 +02:00
|
|
|
/* Checks */
|
|
|
|
if (n->combine != 1) {
|
|
|
|
config_setting_t *cfg_combine = config_setting_get_member(cfg, "combine");
|
|
|
|
cerror(cfg_combine, "The OPAL-RT node type does not support combining!");
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-31 13:48:41 +02:00
|
|
|
config_setting_lookup_int(cfg, "send_id", &o->send_id);
|
|
|
|
config_setting_lookup_int(cfg, "recv_id", &o->recv_id);
|
|
|
|
config_setting_lookup_bool(cfg, "reply", &o->reply);
|
|
|
|
|
|
|
|
n->opal = o;
|
|
|
|
n->cfg = cfg;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
list_push(&opals, o);
|
2015-03-31 13:48:41 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
char * opal_print(struct node *n)
|
2015-03-12 22:56:58 +01:00
|
|
|
{
|
2015-03-17 22:46:46 +01:00
|
|
|
struct opal *o = n->opal;
|
2015-09-22 12:58:37 +02:00
|
|
|
char *buf = NULL;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
/** @todo: Print send_params, recv_params */
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-09-22 12:58:37 +02:00
|
|
|
return strcatf(&buf, "send_id=%u, recv_id=%u, reply=%u",
|
2015-03-17 22:46:46 +01:00
|
|
|
o->send_id, o->recv_id, o->reply);
|
|
|
|
}
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
int opal_open(struct node *n)
|
|
|
|
{
|
|
|
|
struct opal *o = n->opal;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
if (!og)
|
|
|
|
error("The server was not started as an OPAL asynchronous process!");
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
/* Search for valid send and recv ids */
|
|
|
|
int sfound = 0, rfound = 0;
|
2015-05-07 13:57:43 +02:00
|
|
|
for (int i = 0; i < og->send_icons; i++)
|
2015-05-06 11:48:30 +02:00
|
|
|
sfound += og->send_ids[i] == o->send_id;
|
2015-10-06 09:10:08 +02:00
|
|
|
for (int i = 0; i < og->send_icons; i++)
|
2015-05-06 11:48:30 +02:00
|
|
|
rfound += og->send_ids[i] == o->send_id;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
if (!sfound)
|
|
|
|
error("Invalid send_id '%u' for node '%s'", o->send_id, n->name);
|
|
|
|
if (!rfound)
|
|
|
|
error("Invalid recv_id '%u' for node '%s'", o->recv_id, n->name);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-06 11:48:30 +02:00
|
|
|
/* Get some more informations and paramters from OPAL-RT */
|
2015-03-17 22:46:46 +01:00
|
|
|
OpalGetAsyncSendIconMode(&o->mode, o->send_id);
|
|
|
|
OpalGetAsyncSendParameters(&o->send_params, sizeof(Opal_SendAsyncParam), o->send_id);
|
|
|
|
OpalGetAsyncRecvParameters(&o->recv_params, sizeof(Opal_RecvAsyncParam), o->recv_id);
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
return 0;
|
2015-03-12 22:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int opal_close(struct node *n)
|
|
|
|
{
|
2015-03-17 22:46:46 +01:00
|
|
|
return 0;
|
2015-03-12 22:56:58 +01:00
|
|
|
}
|
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
int opal_read(struct node *n, struct msg *pool, int poolsize, int first, int cnt)
|
2015-03-12 22:56:58 +01:00
|
|
|
{
|
2015-03-17 22:46:46 +01:00
|
|
|
struct opal *o = n->opal;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
int state, len, ret;
|
|
|
|
unsigned id;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
struct msg *m = &pool[first % poolsize];
|
2015-08-07 01:11:43 +02:00
|
|
|
|
|
|
|
double data[MSG_VALUES];
|
|
|
|
|
2015-03-12 22:56:58 +01:00
|
|
|
/* This call unblocks when the 'Data Ready' line of a send icon is asserted. */
|
2015-03-17 22:46:46 +01:00
|
|
|
do {
|
2015-09-19 15:28:28 +02:00
|
|
|
ret = OpalWaitForAsyncSendRequest(&id);
|
|
|
|
if (ret != EOK) {
|
2015-03-17 22:46:46 +01:00
|
|
|
state = OpalGetAsyncModelState();
|
2015-05-07 13:57:43 +02:00
|
|
|
if ((state == STATE_RESET) || (state == STATE_STOP))
|
|
|
|
error("OpalGetAsyncModelState(): Model stopped or resetted!");
|
2015-03-17 22:46:46 +01:00
|
|
|
|
|
|
|
return -1; // FIXME: correct return value
|
2015-03-12 22:56:58 +01:00
|
|
|
}
|
2015-03-17 22:46:46 +01:00
|
|
|
} while (id != o->send_id);
|
2015-03-12 22:56:58 +01:00
|
|
|
|
|
|
|
/* No errors encountered yet */
|
2015-03-17 22:46:46 +01:00
|
|
|
OpalSetAsyncSendIconError(0, o->send_id);
|
2015-03-12 22:56:58 +01:00
|
|
|
|
|
|
|
/* Get the size of the data being sent by the unblocking SendID */
|
2015-03-17 22:46:46 +01:00
|
|
|
OpalGetAsyncSendIconDataLength(&len, o->send_id);
|
|
|
|
if (len > sizeof(data)) {
|
|
|
|
warn("Ignoring the last %u of %u values for OPAL node '%s' (send_id=%u).",
|
|
|
|
len / sizeof(double) - MSG_VALUES, len / sizeof(double), n->name, o->send_id);
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
len = sizeof(data);
|
2015-03-12 22:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read data from the model */
|
2015-03-17 22:46:46 +01:00
|
|
|
OpalGetAsyncSendIconData(data, len, o->send_id);
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
m->sequence = htons(o->seq_no++);
|
|
|
|
m->length = len / sizeof(double);
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
for (int i = 0; i < m->length; i++)
|
2015-05-07 13:57:43 +02:00
|
|
|
m->data[i].f = (float) data[i]; /* OPAL provides double precission */
|
2015-03-12 22:56:58 +01:00
|
|
|
|
|
|
|
/* This next call allows the execution of the "asynchronous" process
|
|
|
|
* to actually be synchronous with the model. To achieve this, you
|
|
|
|
* should set the "Sending Mode" in the Async_Send block to
|
|
|
|
* NEED_REPLY_BEFORE_NEXT_SEND or NEED_REPLY_NOW. This will force
|
|
|
|
* the model to wait for this process to call this
|
|
|
|
* OpalAsyncSendRequestDone function before continuing. */
|
2015-03-17 22:46:46 +01:00
|
|
|
if (o->reply)
|
|
|
|
OpalAsyncSendRequestDone(o->send_id);
|
2015-03-12 22:56:58 +01:00
|
|
|
|
|
|
|
/* Before continuing, we make sure that the real-time model
|
|
|
|
* has not been stopped. If it has, we quit. */
|
2015-03-17 22:46:46 +01:00
|
|
|
state = OpalGetAsyncModelState();
|
2015-05-07 13:57:43 +02:00
|
|
|
if ((state == STATE_RESET) || (state == STATE_STOP))
|
|
|
|
error("OpalGetAsyncModelState(): Model stopped or resetted!");
|
2015-03-12 22:56:58 +01:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
return 1;
|
2015-03-12 22:56:58 +01:00
|
|
|
}
|
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
int opal_write(struct node *n, struct msg *pool, int poolsize, int first, int cnt)
|
2015-03-12 22:56:58 +01:00
|
|
|
{
|
2015-03-17 22:46:46 +01:00
|
|
|
struct opal *o = n->opal;
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
struct msg *m = &pool[first % poolsize];
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
int state;
|
|
|
|
int len;
|
2015-05-07 13:57:43 +02:00
|
|
|
double data[m->length];
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
state = OpalGetAsyncModelState();
|
2015-05-07 13:57:43 +02:00
|
|
|
if ((state == STATE_RESET) || (state == STATE_STOP))
|
|
|
|
error("OpalGetAsyncModelState(): Model stopped or resetted!");
|
2015-03-17 22:46:46 +01:00
|
|
|
|
|
|
|
OpalSetAsyncRecvIconStatus(m->sequence, o->recv_id); /* Set the Status to the message ID */
|
|
|
|
OpalSetAsyncRecvIconError(0, o->recv_id); /* Set the Error to 0 */
|
|
|
|
|
|
|
|
/* Get the number of signals to send back to the model */
|
|
|
|
OpalGetAsyncRecvIconDataLength(&len, o->recv_id);
|
|
|
|
if (len > sizeof(data))
|
2015-05-07 13:57:43 +02:00
|
|
|
warn("OPAL node '%s' is expecting more signals (%u) than values in message (%u)",
|
|
|
|
n->name, len / sizeof(double), m->length);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-03-17 22:46:46 +01:00
|
|
|
for (int i = 0; i < m->length; i++)
|
2015-05-07 13:57:43 +02:00
|
|
|
data[i] = (double) m->data[i].f; /* OPAL expects double precission */
|
2015-03-17 22:46:46 +01:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
OpalSetAsyncRecvIconData(data, m->length * sizeof(double), o->recv_id);
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2015-05-07 13:57:43 +02:00
|
|
|
return 1;
|
2015-03-12 22:56:58 +01:00
|
|
|
}
|
2015-09-19 15:26:30 +02:00
|
|
|
|
|
|
|
REGISTER_NODE_TYPE(OPAL_ASYNC, "opal", opal)
|