2 * jsonserver.c: A plugin for the Video Disk Recorder
4 * See the README file for copyright information and how to reach the author.
8 #include <vdr/plugin.h>
11 // Config docs: http://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C_002b_002b-API
12 #include <libconfig.h++>
18 static const char *VERSION = "0.0.1";
19 static const char *DESCRIPTION = "JSON data server plugin for VDR";
20 static const char *MAINMENUENTRY = "Jsonserver";
22 class cPluginJsonserver : public cPlugin {
24 // Add any member variables or functions you may need here.
25 struct mg_context *mg;
27 libconfig::Config config;
30 cPluginJsonserver(void);
31 virtual ~cPluginJsonserver();
32 virtual const char *Version(void) { return VERSION; }
33 virtual const char *Description(void) { return DESCRIPTION; }
34 virtual const char *CommandLineHelp(void);
35 virtual bool ProcessArgs(int argc, char *argv[]);
36 virtual bool Initialize(void);
37 virtual bool Start(void);
38 virtual void Stop(void);
39 virtual void Housekeeping(void);
40 virtual void MainThreadHook(void);
41 virtual cString Active(void);
42 virtual time_t WakeupTime(void);
43 virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
44 virtual cOsdObject *MainMenuAction(void);
45 virtual cMenuSetupPage *SetupMenu(void);
46 virtual bool SetupParse(const char *Name, const char *Value);
47 virtual bool Service(const char *Id, void *Data = NULL);
48 virtual const char **SVDRPHelpPages(void);
49 virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
52 cPluginJsonserver::cPluginJsonserver(void)
54 // Initialize any member variables here.
55 // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
56 // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
62 cPluginJsonserver::~cPluginJsonserver()
64 // Clean up after yourself!
69 const char *cPluginJsonserver::CommandLineHelp(void)
71 // Return a string that describes all known command line options.
75 bool cPluginJsonserver::ProcessArgs(int argc, char *argv[])
77 // Implement command line argument processing here if applicable.
81 bool cPluginJsonserver::Initialize(void)
83 // Initialize any background activities the plugin shall perform.
87 bool cPluginJsonserver::Start(void)
89 // Start any background activities the plugin shall perform.
90 const char* configDir = cPlugin::ConfigDirectory("jsonserver");
93 dsyslog("jsonserver: Error: Could not get config dir from VDR");
97 std::string configFile(std::string(configDir) + std::string("/server.conf"));
98 dsyslog("%s", configFile.c_str());
101 config.readFile(configFile.c_str());
103 catch (const libconfig::FileIOException &fioex)
105 dsyslog("jsonserver: Failed to read config file");
108 catch(const libconfig::ParseException &pex)
110 dsyslog("jsonserver: Config parse error at %s: %i - %s", pex.getFile(), pex.getLine(), pex.getError());
114 std::string cfgLogFilename;
115 if (config.lookupValue("log-file", cfgLogFilename))
118 log->init(Log::DEBUG, cfgLogFilename.c_str());
119 log->log("Main", Log::INFO, "Logging started");
123 dsyslog("jsonserver: Logging disabled");
126 std::string cfgDocRoot;
127 if (!config.lookupValue("doc-root", cfgDocRoot))
129 log->log("Main", Log::CRIT, "Failed to load doc-root from config");
130 dsyslog("jsonserver: Could not load JS App Dir from plugin config file");
137 if (!config.lookupValue("http-port", cfgPort))
139 log->log("Main", Log::CRIT, "Failed to load http-port from config");
140 dsyslog("jsonserver: Could not load http-port from plugin config file");
146 std::string cfgSSLFilename;
147 if (!config.lookupValue("ssl-pem-file", cfgSSLFilename))
149 log->log("Main", Log::CRIT, "Failed to load ssl-pem-file from config");
150 dsyslog("jsonserver: Could not load ssl-pem-file from plugin config file");
156 // Make Mongoose options
157 const char *options[] =
159 "document_root", cfgDocRoot.c_str(),
160 "listening_ports", cfgPort.c_str(),
162 "ssl_certificate", cfgSSLFilename.c_str(),
166 // Prepare callbacks structure. We have only one callback, the rest are NULL.
167 struct mg_callbacks callbacks;
168 memset(&callbacks, 0, sizeof(callbacks));
169 callbacks.begin_request = jsonserver_request_handler;
171 mg = mg_start(&callbacks, NULL, options);
172 log->log("JSONServer", Log::INFO, "Mongoose started");
177 void cPluginJsonserver::Stop(void)
179 // Stop any background activities the plugin is performing.
180 if (mgRunning) mg_stop(mg);
184 void cPluginJsonserver::Housekeeping(void)
186 // Perform any cleanup or other regular tasks.
189 void cPluginJsonserver::MainThreadHook(void)
191 // Perform actions in the context of the main program thread.
192 // WARNING: Use with great care - see PLUGINS.html!
195 cString cPluginJsonserver::Active(void)
197 // Return a message string if shutdown should be postponed
201 time_t cPluginJsonserver::WakeupTime(void)
203 // Return custom wakeup time for shutdown script
207 cOsdObject *cPluginJsonserver::MainMenuAction(void)
209 // Perform the action when selected from the main VDR menu.
213 cMenuSetupPage *cPluginJsonserver::SetupMenu(void)
215 // Return a setup menu in case the plugin supports one.
219 bool cPluginJsonserver::SetupParse(const char *Name, const char *Value)
221 // Parse your own setup parameters and store their values.
225 bool cPluginJsonserver::Service(const char *Id, void *Data)
227 // Handle custom service requests from other plugins
231 const char **cPluginJsonserver::SVDRPHelpPages(void)
233 // Return help text for SVDRP commands this plugin implements
237 cString cPluginJsonserver::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
239 // Process SVDRP commands this plugin implements
243 VDRPLUGINCREATOR(cPluginJsonserver); // Don't touch this!