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;
29 cPluginJsonserver(void);
30 virtual ~cPluginJsonserver();
31 virtual const char *Version(void) { return VERSION; }
32 virtual const char *Description(void) { return DESCRIPTION; }
33 virtual const char *CommandLineHelp(void);
34 virtual bool ProcessArgs(int argc, char *argv[]);
35 virtual bool Initialize(void);
36 virtual bool Start(void);
37 virtual void Stop(void);
38 virtual void Housekeeping(void);
39 virtual void MainThreadHook(void);
40 virtual cString Active(void);
41 virtual time_t WakeupTime(void);
42 virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
43 virtual cOsdObject *MainMenuAction(void);
44 virtual cMenuSetupPage *SetupMenu(void);
45 virtual bool SetupParse(const char *Name, const char *Value);
46 virtual bool Service(const char *Id, void *Data = NULL);
47 virtual const char **SVDRPHelpPages(void);
48 virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
51 cPluginJsonserver::cPluginJsonserver(void)
53 // Initialize any member variables here.
54 // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
55 // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
63 cPluginJsonserver::~cPluginJsonserver()
65 // Clean up after yourself!
69 if (config) delete config;
73 const char *cPluginJsonserver::CommandLineHelp(void)
75 // Return a string that describes all known command line options.
79 bool cPluginJsonserver::ProcessArgs(int argc, char *argv[])
81 // Implement command line argument processing here if applicable.
85 bool cPluginJsonserver::Initialize(void)
87 // Initialize any background activities the plugin shall perform.
91 bool cPluginJsonserver::Start(void)
93 // Start any background activities the plugin shall perform.
94 const char* configDir = cPlugin::ConfigDirectory("jsonserver");
97 dsyslog("jsonserver: Error: Could not get config dir from VDR");
101 if (asprintf(&configFile, "%s/jsonserver.conf", configDir) == -1)
103 dsyslog("jsonserver: Error: asprintf");
107 dsyslog("%s", configFile);
111 config = new Config();
112 if (config->init(configFile))
114 dsyslog("jsonserver: Config file found");
119 dsyslog("jsonserver: Error: Config file not found");
126 char* cfgLogFilename = config->getValueString("General", "Log file");
129 log->init(Log::DEBUG, cfgLogFilename);
130 delete[] cfgLogFilename;
131 log->log("Main", Log::INFO, "Logging started");
135 dsyslog("jsonserver: Logging disabled");
138 cfgDocRoot = config->getValueString("General", "JS App Dir");
141 log->log("Main", Log::CRIT, "Config General/JS App Dir not found");
142 dsyslog("jsonserver: Error: Could not load JS App Dir from plugin config file");
150 cfgPort = config->getValueString("General", "Port number");
153 cfgPort = new char[5];
154 strcpy(cfgPort, "8005");
157 // Make Mongoose options
158 const char *options[] =
160 "document_root", cfgDocRoot,
161 // "listening_ports", cfgPort,
164 "listening_ports", "8005s",
165 "ssl_certificate", "/opt/jsonserver/sslcert.pem",
167 // "auth_domain", "VDRWeb",
172 // Prepare callbacks structure. We have only one callback, the rest are NULL.
173 struct mg_callbacks callbacks;
174 memset(&callbacks, 0, sizeof(callbacks));
175 callbacks.begin_request = jsonserver_request_handler;
177 mg = mg_start(&callbacks, NULL, options);
178 log->log("JSONServer", Log::INFO, "Mongoose started");
182 void cPluginJsonserver::Stop(void)
184 // Stop any background activities the plugin is performing.
186 if (cfgDocRoot) delete[] cfgDocRoot; cfgDocRoot = NULL;
187 if (cfgPort) delete[] cfgPort; cfgPort = NULL;
190 void cPluginJsonserver::Housekeeping(void)
192 // Perform any cleanup or other regular tasks.
195 void cPluginJsonserver::MainThreadHook(void)
197 // Perform actions in the context of the main program thread.
198 // WARNING: Use with great care - see PLUGINS.html!
201 cString cPluginJsonserver::Active(void)
203 // Return a message string if shutdown should be postponed
207 time_t cPluginJsonserver::WakeupTime(void)
209 // Return custom wakeup time for shutdown script
213 cOsdObject *cPluginJsonserver::MainMenuAction(void)
215 // Perform the action when selected from the main VDR menu.
219 cMenuSetupPage *cPluginJsonserver::SetupMenu(void)
221 // Return a setup menu in case the plugin supports one.
225 bool cPluginJsonserver::SetupParse(const char *Name, const char *Value)
227 // Parse your own setup parameters and store their values.
231 bool cPluginJsonserver::Service(const char *Id, void *Data)
233 // Handle custom service requests from other plugins
237 const char **cPluginJsonserver::SVDRPHelpPages(void)
239 // Return help text for SVDRP commands this plugin implements
243 cString cPluginJsonserver::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
245 // Process SVDRP commands this plugin implements
249 VDRPLUGINCREATOR(cPluginJsonserver); // Don't touch this!