2017-04-09 16:22:03 +02:00
|
|
|
/** API session.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-09 16:22:03 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/api/session.h>
|
|
|
|
#include <villas/web.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/memory.h>
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2017-08-27 17:04:25 +02:00
|
|
|
int api_session_init(struct api_session *s, enum api_mode m)
|
2017-04-09 16:22:03 +02:00
|
|
|
{
|
2017-08-27 17:04:25 +02:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-27 17:59:59 +02:00
|
|
|
s->runs = 0;
|
|
|
|
s->mode = m;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-27 17:04:25 +02:00
|
|
|
ret = buffer_init(&s->request.buffer, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-27 17:04:25 +02:00
|
|
|
ret = buffer_init(&s->response.buffer, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
ret = queue_init(&s->request.queue, 128, &memory_type_heap);
|
2017-08-27 17:04:25 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
ret = queue_init(&s->response.queue, 128, &memory_type_heap);
|
2017-08-27 17:04:25 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-27 17:59:59 +02:00
|
|
|
s->_name = NULL;
|
2017-04-09 16:22:03 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int api_session_destroy(struct api_session *s)
|
|
|
|
{
|
2017-08-27 17:04:25 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = buffer_destroy(&s->request.buffer);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-27 17:04:25 +02:00
|
|
|
ret = buffer_destroy(&s->response.buffer);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-27 17:04:25 +02:00
|
|
|
ret = queue_destroy(&s->request.queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-27 17:04:25 +02:00
|
|
|
ret = queue_destroy(&s->response.queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-27 17:59:59 +02:00
|
|
|
if (s->_name)
|
|
|
|
free(s->_name);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-09 16:22:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int api_session_run_command(struct api_session *s, json_t *json_in, json_t **json_out)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const char *action;
|
|
|
|
char *id;
|
|
|
|
struct plugin *p;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-10 13:31:34 +02:00
|
|
|
json_t *json_args = NULL;
|
|
|
|
json_t *json_resp = NULL;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-09 16:22:03 +02:00
|
|
|
ret = json_unpack(json_in, "{ s: s, s: s, s?: o }",
|
|
|
|
"action", &action,
|
|
|
|
"id", &id,
|
|
|
|
"request", &json_args);
|
|
|
|
if (ret) {
|
|
|
|
ret = -100;
|
|
|
|
*json_out = json_pack("{ s: s, s: i }",
|
|
|
|
"error", "invalid request",
|
|
|
|
"code", ret);
|
|
|
|
goto out;
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-09 16:22:03 +02:00
|
|
|
p = plugin_lookup(PLUGIN_TYPE_API, action);
|
|
|
|
if (!p) {
|
|
|
|
ret = -101;
|
|
|
|
*json_out = json_pack("{ s: s, s: s, s: i, s: s }",
|
|
|
|
"action", action,
|
|
|
|
"id", id,
|
|
|
|
"code", ret,
|
|
|
|
"error", "command not found");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:59:59 +02:00
|
|
|
debug(LOG_API, "Running API request: action=%s, id=%s", action, id);
|
2017-04-09 16:22:03 +02:00
|
|
|
|
|
|
|
ret = p->api.cb(&p->api, json_args, &json_resp, s);
|
|
|
|
if (ret)
|
|
|
|
*json_out = json_pack("{ s: s, s: s, s: i, s: s }",
|
|
|
|
"action", action,
|
|
|
|
"id", id,
|
|
|
|
"code", ret,
|
|
|
|
"error", "command failed");
|
|
|
|
else
|
2017-08-10 13:31:34 +02:00
|
|
|
*json_out = json_pack("{ s: s, s: s }",
|
2017-04-09 16:22:03 +02:00
|
|
|
"action", action,
|
2017-08-10 13:31:34 +02:00
|
|
|
"id", id);
|
|
|
|
|
|
|
|
if (json_resp)
|
2017-09-05 01:07:11 +02:00
|
|
|
json_object_set_new(*json_out, "response", json_resp);
|
2017-04-09 16:22:03 +02:00
|
|
|
|
2017-08-27 17:59:59 +02:00
|
|
|
out: debug(LOG_API, "Completed API request: action=%s, id=%s, code=%d", action, id, ret);
|
|
|
|
|
|
|
|
s->runs++;
|
2017-04-09 16:22:03 +02:00
|
|
|
|
|
|
|
return 0;
|
2017-07-24 19:33:35 +02:00
|
|
|
}
|
2017-08-27 17:59:59 +02:00
|
|
|
|
|
|
|
char * api_session_name(struct api_session *s)
|
|
|
|
{
|
|
|
|
if (!s->_name) {
|
|
|
|
char *mode;
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-27 17:59:59 +02:00
|
|
|
switch (s->mode) {
|
|
|
|
case API_MODE_WS: mode = "ws"; break;
|
|
|
|
case API_MODE_HTTP: mode = "http"; break;
|
|
|
|
default: mode = "unknown"; break;
|
|
|
|
}
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-30 23:29:18 +02:00
|
|
|
strcatf(&s->_name, "version=%d, mode=%s", s->version, mode);
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-30 23:29:18 +02:00
|
|
|
if (s->wsi) {
|
|
|
|
char name[128];
|
|
|
|
char ip[128];
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-30 23:29:18 +02:00
|
|
|
lws_get_peer_addresses(s->wsi, lws_get_socket_fd(s->wsi), name, sizeof(name), ip, sizeof(ip));
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-30 23:29:18 +02:00
|
|
|
strcatf(&s->_name, ", remote.name=%s, remote.ip=%s", name, ip);
|
|
|
|
}
|
2017-08-27 17:59:59 +02:00
|
|
|
}
|
2017-09-05 01:07:11 +02:00
|
|
|
|
2017-08-27 17:59:59 +02:00
|
|
|
return s->_name;
|
|
|
|
}
|