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>
16 static const char *VERSION = "0.0.1";
17 static const char *DESCRIPTION = "JSON data server plugin for VDR";
18 static const char *MAINMENUENTRY = "Jsonserver";
20 class cPluginJsonserver : public cPlugin {
22 // Add any member variables or functions you may need here.
23 struct mg_context *mg;
31 cPluginJsonserver(void);
32 virtual ~cPluginJsonserver();
33 virtual const char *Version(void) { return VERSION; }
34 virtual const char *Description(void) { return DESCRIPTION; }
35 virtual const char *CommandLineHelp(void);
36 virtual bool ProcessArgs(int argc, char *argv[]);
37 virtual bool Initialize(void);
38 virtual bool Start(void);
39 virtual void Stop(void);
40 virtual void Housekeeping(void);
41 virtual void MainThreadHook(void);
42 virtual cString Active(void);
43 virtual time_t WakeupTime(void);
44 virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
45 virtual cOsdObject *MainMenuAction(void);
46 virtual cMenuSetupPage *SetupMenu(void);
47 virtual bool SetupParse(const char *Name, const char *Value);
48 virtual bool Service(const char *Id, void *Data = NULL);
49 virtual const char **SVDRPHelpPages(void);
50 virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
53 cPluginJsonserver::cPluginJsonserver(void)
55 // Initialize any member variables here.
56 // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
57 // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
63 cfgSSLFilename = NULL;
67 cPluginJsonserver::~cPluginJsonserver()
69 // Clean up after yourself!
73 if (config) delete config;
77 const char *cPluginJsonserver::CommandLineHelp(void)
79 // Return a string that describes all known command line options.
83 bool cPluginJsonserver::ProcessArgs(int argc, char *argv[])
85 // Implement command line argument processing here if applicable.
89 bool cPluginJsonserver::Initialize(void)
91 // Initialize any background activities the plugin shall perform.
95 bool cPluginJsonserver::Start(void)
97 // Start any background activities the plugin shall perform.
98 const char* configDir = cPlugin::ConfigDirectory("jsonserver");
101 dsyslog("jsonserver: Error: Could not get config dir from VDR");
105 if (asprintf(&configFile, "%s/jsonserver.conf", configDir) == -1)
107 dsyslog("jsonserver: Error: asprintf");
111 dsyslog("%s", configFile);
115 config = new Config();
116 if (config->init(configFile))
118 dsyslog("jsonserver: Config file found");
123 dsyslog("jsonserver: Error: Config file not found");
130 char* cfgLogFilename = config->getValueString("General", "Log file");
133 log->init(Log::DEBUG, cfgLogFilename);
134 delete[] cfgLogFilename;
135 log->log("Main", Log::INFO, "Logging started");
139 dsyslog("jsonserver: Logging disabled");
142 cfgDocRoot = config->getValueString("General", "JS App Dir");
145 log->log("Main", Log::CRIT, "Config General/JS App Dir not found");
146 dsyslog("jsonserver: Error: Could not load JS App Dir from plugin config file");
154 cfgPort = config->getValueString("General", "Port number");
157 cfgPort = new char[5];
158 strcpy(cfgPort, "8005");
161 char* cfgSSLFilename = config->getValueString("General", "SSL PEM File");
164 log->log("Main", Log::ALERT, "Config General / SSL PEM File not found - can't run!");
165 dsyslog("jsonserver: ERROR: Config: SSL PEM not found");
173 // Make Mongoose options
174 const char *options[] =
176 "document_root", cfgDocRoot,
177 // "listening_ports", cfgPort,
180 "listening_ports", "8005s",
181 "ssl_certificate", cfgSSLFilename,
183 // "auth_domain", "VDRWeb",
188 // Prepare callbacks structure. We have only one callback, the rest are NULL.
189 struct mg_callbacks callbacks;
190 memset(&callbacks, 0, sizeof(callbacks));
191 callbacks.begin_request = jsonserver_request_handler;
193 mg = mg_start(&callbacks, NULL, options);
194 log->log("JSONServer", Log::INFO, "Mongoose started");
199 void cPluginJsonserver::Stop(void)
201 // Stop any background activities the plugin is performing.
202 if (mgRunning) mg_stop(mg);
204 if (cfgDocRoot) delete[] cfgDocRoot; cfgDocRoot = NULL;
205 if (cfgPort) delete[] cfgPort; cfgPort = NULL;
206 if (cfgSSLFilename) delete[] cfgSSLFilename; cfgSSLFilename = NULL;
209 void cPluginJsonserver::Housekeeping(void)
211 // Perform any cleanup or other regular tasks.
214 void cPluginJsonserver::MainThreadHook(void)
216 // Perform actions in the context of the main program thread.
217 // WARNING: Use with great care - see PLUGINS.html!
220 cString cPluginJsonserver::Active(void)
222 // Return a message string if shutdown should be postponed
226 time_t cPluginJsonserver::WakeupTime(void)
228 // Return custom wakeup time for shutdown script
232 cOsdObject *cPluginJsonserver::MainMenuAction(void)
234 // Perform the action when selected from the main VDR menu.
238 cMenuSetupPage *cPluginJsonserver::SetupMenu(void)
240 // Return a setup menu in case the plugin supports one.
244 bool cPluginJsonserver::SetupParse(const char *Name, const char *Value)
246 // Parse your own setup parameters and store their values.
250 bool cPluginJsonserver::Service(const char *Id, void *Data)
252 // Handle custom service requests from other plugins
256 const char **cPluginJsonserver::SVDRPHelpPages(void)
258 // Return help text for SVDRP commands this plugin implements
262 cString cPluginJsonserver::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
264 // Process SVDRP commands this plugin implements
268 VDRPLUGINCREATOR(cPluginJsonserver); // Don't touch this!