REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
plugin.h
Go to the documentation of this file.
1#ifndef PLUGIN_LOADER_H
2#define PLUGIN_LOADER_H
3
4#include <ArduinoJson.h>
5#include <etl/optional.h>
6
7
8namespace loader {
9 using etl::optional;
10
18 struct Function {
19 enum class Returns { None, Bool, Int, String, JsonObject };
20 typedef void (None_f)(); typedef bool (Bool_f)(); typedef int (Int_f)();
21 typedef std::string (String_f)(); typedef JsonObject (JsonObject_f)();
22
25 };
26
27 void convertFromJson(JsonVariantConst src, Function& f);
28
47 struct Plugin {
48 uint8_t *load_addr=0;
51 optional<Function> exit;
52
53 bool can_load() const { return !load_addr || size; }
54 // bool is_loaded() const { return size; }
55 };
56
64 optional<Plugin> plugin;
65 uint8_t* load_addr=0;
67
68 bool can_load() const { return plugin ? plugin->can_load() : (load_addr!=0); }
69 //virtual bool load(uint8_t* code, size_t code_len); // convenience function, put code in place, returns true if success
70 //virtual bool load(size_t code_len);
71 int load(const Plugin& new_plugin);
72 void unload() { plugin.reset(); }
73
76 virtual int load_and_execute(JsonObjectConst msg_in, JsonObject &msg_out);
77 //virtual void show(JsonObjectConst msg_in, JsonObject &msg_out); ///< show what is currently there
78 virtual int unload(JsonObjectConst msg_in, JsonObject &msg_out);
80 };
81
99
100
102 //using PluginLoader = GlobalPluginLoader;
103
104 void convertToJson(const GlobalPluginLoader& src, JsonVariant dst);
105
106
107 /*
108
109 It is not so hard to come up with other loaders, such as ones reserving space on the
110 - Heap (can load multiple plugins, each of differing size)
111 Difficulties: Have to disable RAM2 cache for allowing data be executed there
112 - unreserved RAM1 just between stack and data segment (can run a heap there
113 or just use memory only when neccessary)
114 Difficulties: Have to be sure not to mess up with the regular stack
115 or other things. The above GlobalPluginLoader is just one example.
116
117 */
118} // end of namespace loader
119
120
121
122
123
124#endif /* PLUGIN_LOADER_H */
125
uint32_t
Definition flasher.cpp:195
uint32_t src
Definition flasher.cpp:63
virtual int load_and_execute(JsonObjectConst msg_in, JsonObject &msg_out)
load from protocol message, gives out reply msg, returns 0 on success
Definition plugin.cpp:150
void convertToJson(const GlobalPluginLoader &src, JsonVariant dst)
Definition plugin.cpp:99
GlobalPluginLoader PluginLoader
Definition plugin.cpp:234
void convertFromJson(JsonVariantConst src, Function &f)
Definition plugin.cpp:67
A jumpable function, ie something with a signature "ret_type foo();", located at relative or absolute...
Definition plugin.h:18
Returns ret_type
Definition plugin.h:24
void None_f()
Definition plugin.h:20
uint32_t addr
Definition plugin.h:23
bool Bool_f()
Definition plugin.h:20
std::string String_f()
Definition plugin.h:21
JsonObject JsonObject_f()
Definition plugin.h:21
Reserves storage in the data segment (address space that constains static variables,...
Definition plugin.h:96
A plugin (a synonym could also be "extension" or "module") is a small piece of user-defined code whic...
Definition plugin.h:47
bool can_load() const
Definition plugin.h:53
uint32_t size
Actual size of plugin memory image.
Definition plugin.h:49
Function entry
entry points ("constructor function"). entry.addr is relative to load_addr.
Definition plugin.h:50
uint8_t * load_addr
Absolute address in memory where the plugin is loaded to.
Definition plugin.h:48
optional< Function > exit
destructor function. entry.addr is relative to load_addr.
Definition plugin.h:51
The SinglePluginLoader can only load a single plugin at a given time.
Definition plugin.h:63
uint8_t * load_addr
Absolute address of the memory segment. It is always (plugin->load_addr = load_addr).
Definition plugin.h:65
bool can_load() const
Definition plugin.h:68
optional< Plugin > plugin
The slot for a single managed plugin (if loaded). It's load_addr defines the memory address.
Definition plugin.h:64
void unload()
frees memory, does not call unloader.
Definition plugin.h:72
uint32_t memsize
The maximum memory size managed/accessible/available by this loader. It is always (plugin->size <= me...
Definition plugin.h:66
int load(const Plugin &new_plugin)
returns 0 on success
Definition plugin.cpp:224