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 asprintf(&configFile, "%s/jsonserver.conf", configDir);
107 config = new Config();
108 if (config->init(configFile))
110 dsyslog("jsonserver: Config file found");
115 dsyslog("jsonserver: Error: Config file not found");
122 char* cfgLogFilename = config->getValueString("General", "Log file");
125 log->init(Log::DEBUG, cfgLogFilename);
126 delete[] cfgLogFilename;
127 log->log("Main", Log::INFO, "Logging started");
131 dsyslog("jsonserver: Logging disabled");
134 cfgDocRoot = config->getValueString("General", "JS App Dir");
137 log->log("Main", Log::CRIT, "Config General/JS App Dir not found");
138 dsyslog("jsonserver: Error: Could not load JS App Dir from plugin config file");
146 cfgPort = config->getValueString("General", "Port number");
149 cfgPort = new char[5];
150 strcpy(cfgPort, "8005");
153 // Make Mongoose options
154 const char *options[] =
156 "document_root", cfgDocRoot,
157 "listening_ports", cfgPort,
162 // Prepare callbacks structure. We have only one callback, the rest are NULL.
163 struct mg_callbacks callbacks;
164 memset(&callbacks, 0, sizeof(callbacks));
165 callbacks.begin_request = jsonserver_request_handler;
167 mg = mg_start(&callbacks, NULL, options);
168 log->log("JSONServer", Log::INFO, "Mongoose started");
172 void cPluginJsonserver::Stop(void)
174 // Stop any background activities the plugin is performing.
176 if (cfgDocRoot) delete[] cfgDocRoot; cfgDocRoot = NULL;
177 if (cfgPort) delete[] cfgPort; cfgPort = NULL;
180 void cPluginJsonserver::Housekeeping(void)
182 // Perform any cleanup or other regular tasks.
185 void cPluginJsonserver::MainThreadHook(void)
187 // Perform actions in the context of the main program thread.
188 // WARNING: Use with great care - see PLUGINS.html!
191 cString cPluginJsonserver::Active(void)
193 // Return a message string if shutdown should be postponed
197 time_t cPluginJsonserver::WakeupTime(void)
199 // Return custom wakeup time for shutdown script
203 cOsdObject *cPluginJsonserver::MainMenuAction(void)
205 // Perform the action when selected from the main VDR menu.
209 cMenuSetupPage *cPluginJsonserver::SetupMenu(void)
211 // Return a setup menu in case the plugin supports one.
215 bool cPluginJsonserver::SetupParse(const char *Name, const char *Value)
217 // Parse your own setup parameters and store their values.
221 bool cPluginJsonserver::Service(const char *Id, void *Data)
223 // Handle custom service requests from other plugins
227 const char **cPluginJsonserver::SVDRPHelpPages(void)
229 // Return help text for SVDRP commands this plugin implements
233 cString cPluginJsonserver::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
235 // Process SVDRP commands this plugin implements
239 VDRPLUGINCREATOR(cPluginJsonserver); // Don't touch this!