2025-01-14 14:42:39 +00:00
|
|
|
/* Lua expressions hook.
|
2021-02-19 06:50:25 +01:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2021-02-19 06:50:25 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-02-22 23:15:56 +01:00
|
|
|
#include <mutex>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <vector>
|
2021-02-22 23:15:56 +01:00
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
2021-02-19 06:50:25 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2025-01-14 14:42:39 +00:00
|
|
|
#include "lua.h"
|
2021-02-19 06:50:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Forward declarations
|
2021-02-19 06:50:25 +01:00
|
|
|
class LuaHook;
|
2021-08-10 10:12:48 -04:00
|
|
|
enum SignalType;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
|
|
|
class LuaSignalExpression {
|
|
|
|
|
|
|
|
protected:
|
2025-01-14 14:42:39 +00:00
|
|
|
int cookie;
|
|
|
|
lua_State *L;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
std::string expression;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_t *cfg;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
|
|
|
public:
|
2025-01-14 14:42:39 +00:00
|
|
|
LuaSignalExpression(lua_State *L, json_t *json_sig);
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void prepare();
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void parseExpression(const std::string &expr);
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void evaluate(union SignalData *data, enum SignalType type);
|
2021-02-19 06:50:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class LuaHook : public Hook {
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
friend LuaSignalExpression;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2021-02-22 23:15:56 +01:00
|
|
|
private:
|
2025-01-14 14:42:39 +00:00
|
|
|
static const int SELF_REFERENCE = 55;
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2021-02-19 06:50:25 +01:00
|
|
|
protected:
|
2025-01-14 14:42:39 +00:00
|
|
|
std::string script;
|
|
|
|
std::vector<LuaSignalExpression> expressions;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
SignalList::Ptr
|
|
|
|
signalsProcessed; // Signals as emited by Lua process() function
|
|
|
|
SignalList::Ptr signalsExpressions; // Signals as emited by Lua expressions
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
lua_State *L;
|
|
|
|
std::mutex mutex;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
bool useNames;
|
|
|
|
bool hasExpressions;
|
|
|
|
bool needsLocking;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Function indices
|
|
|
|
struct {
|
|
|
|
int start;
|
|
|
|
int stop;
|
|
|
|
int restart;
|
|
|
|
int process;
|
|
|
|
int periodic;
|
|
|
|
int prepare;
|
|
|
|
} functions;
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void parseExpressions(json_t *json_sigs);
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
void loadScript();
|
|
|
|
void lookupFunctions();
|
|
|
|
void setupEnvironment();
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Lua functions
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int luaInfo(lua_State *L);
|
|
|
|
int luaWarn(lua_State *L);
|
|
|
|
int luaError(lua_State *L);
|
|
|
|
int luaDebug(lua_State *L);
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int luaRegisterApiHandler(lua_State *L);
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
typedef int (LuaHook::*mem_func)(lua_State *L);
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// This template wraps a member function into a C-style "free" function compatible with lua.
|
|
|
|
template <mem_func func> static int dispatch(lua_State *L) {
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, SELF_REFERENCE);
|
|
|
|
void *vptr = lua_touserdata(L, -1);
|
|
|
|
lua_pop(L, 1);
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
LuaHook *ptr = static_cast<LuaHook *>(vptr);
|
|
|
|
return ((*ptr).*func)(L);
|
|
|
|
}
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2021-02-19 06:50:25 +01:00
|
|
|
public:
|
2025-01-14 14:42:39 +00:00
|
|
|
LuaHook(Path *p, Node *n, int fl, int prio, bool en = true);
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
virtual ~LuaHook();
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
virtual void parse(json_t *json);
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
virtual void prepare();
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Periodically called by the main thread.
|
|
|
|
virtual void periodic();
|
2021-02-22 23:15:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Called whenever a hook is started; before threads are created.
|
|
|
|
virtual void start();
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Called whenever a hook is stopped; after threads are destoyed.
|
|
|
|
virtual void stop();
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Called whenever a new simulation case is started. This is detected by a sequence no equal to zero.
|
|
|
|
virtual void restart();
|
2021-02-19 06:50:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Called whenever a sample is processed.
|
|
|
|
virtual Reason process(struct Sample *smp);
|
2021-02-19 06:50:25 +01:00
|
|
|
};
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|