]> git.vomp.tv Git - jsonserver.git/blob - jsonserver.c
New Makefile, min VDR now 1.7.35
[jsonserver.git] / jsonserver.c
1 /*
2  * jsonserver.c: A plugin for the Video Disk Recorder
3  *
4  * See the README file for copyright information and how to reach the author.
5  *
6  */
7
8 #include <vdr/plugin.h>
9 #include <stdio.h>
10
11 #include "mongoose.h"
12 #include "handler.h"
13 #include "log.h"
14 #include "config.h"
15
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";
19
20 class cPluginJsonserver : public cPlugin {
21 private:
22   // Add any member variables or functions you may need here.
23   struct mg_context *mg;
24   Log* log;
25   Config* config;
26   char* cfgDocRoot;
27   char* cfgPort;
28 public:
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);
49   };
50
51 cPluginJsonserver::cPluginJsonserver(void)
52 {
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!
56
57   config = NULL;
58   log = NULL;
59   cfgDocRoot = NULL;
60   cfgPort = NULL;
61 }
62
63 cPluginJsonserver::~cPluginJsonserver()
64 {
65   // Clean up after yourself!
66   if (log) delete log;
67   log = NULL;
68
69   if (config) delete config;
70   config = NULL;
71 }
72
73 const char *cPluginJsonserver::CommandLineHelp(void)
74 {
75   // Return a string that describes all known command line options.
76   return NULL;
77 }
78
79 bool cPluginJsonserver::ProcessArgs(int argc, char *argv[])
80 {
81   // Implement command line argument processing here if applicable.
82   return true;
83 }
84
85 bool cPluginJsonserver::Initialize(void)
86 {
87   // Initialize any background activities the plugin shall perform.
88   return true;
89 }
90
91 bool cPluginJsonserver::Start(void)
92 {
93   // Start any background activities the plugin shall perform.
94   const char* configDir = cPlugin::ConfigDirectory("jsonserver");
95   if (!configDir)
96   {
97     dsyslog("jsonserver: Error: Could not get config dir from VDR");
98     return false;
99   }
100   char* configFile;
101   if (asprintf(&configFile, "%s/jsonserver.conf", configDir) == -1)
102   {
103     dsyslog("jsonserver: Error: asprintf");
104     return false;
105   }
106   
107   dsyslog("%s", configFile);
108   
109   log = new Log();
110   
111   config = new Config();
112   if (config->init(configFile))
113   {
114     dsyslog("jsonserver: Config file found");
115     free(configFile);
116   }
117   else
118   {
119     dsyslog("jsonserver: Error: Config file not found");
120     free(configFile);
121     delete config;
122     config = NULL;
123     return false;
124   }
125
126   char* cfgLogFilename = config->getValueString("General", "Log file");
127   if (cfgLogFilename)
128   {
129     log->init(Log::DEBUG, cfgLogFilename);
130     delete[] cfgLogFilename;
131     log->log("Main", Log::INFO, "Logging started");
132   }
133   else
134   {
135     dsyslog("jsonserver: Logging disabled");
136   }
137   
138   cfgDocRoot = config->getValueString("General", "JS App Dir");
139   if (!cfgDocRoot)
140   {
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");
143     delete log;
144     delete config;
145     log = NULL;
146     config = NULL;
147     return false;
148   }
149
150   cfgPort = config->getValueString("General", "Port number");
151   if (!cfgPort)
152   {
153     cfgPort = new char[5];
154     strcpy(cfgPort, "8005");
155   }
156   
157   // Make Mongoose options
158   const char *options[] =
159   {
160     "document_root", cfgDocRoot,
161 //    "listening_ports", cfgPort,
162     "num_threads", "5",
163
164     "listening_ports", "8005s",
165     "ssl_certificate", "/opt/jsonserver/sslcert.pem",
166
167 //    "auth_domain", "VDRWeb",
168
169     NULL
170   };
171
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;
176   
177   mg = mg_start(&callbacks, NULL, options);
178   log->log("JSONServer", Log::INFO, "Mongoose started");
179   return true;
180 }
181
182 void cPluginJsonserver::Stop(void)
183 {
184   // Stop any background activities the plugin is performing.
185   mg_stop(mg);
186   if (cfgDocRoot) delete[] cfgDocRoot; cfgDocRoot = NULL;
187   if (cfgPort) delete[] cfgPort; cfgPort = NULL;
188 }
189
190 void cPluginJsonserver::Housekeeping(void)
191 {
192   // Perform any cleanup or other regular tasks.
193 }
194
195 void cPluginJsonserver::MainThreadHook(void)
196 {
197   // Perform actions in the context of the main program thread.
198   // WARNING: Use with great care - see PLUGINS.html!
199 }
200
201 cString cPluginJsonserver::Active(void)
202 {
203   // Return a message string if shutdown should be postponed
204   return NULL;
205 }
206
207 time_t cPluginJsonserver::WakeupTime(void)
208 {
209   // Return custom wakeup time for shutdown script
210   return 0;
211 }
212
213 cOsdObject *cPluginJsonserver::MainMenuAction(void)
214 {
215   // Perform the action when selected from the main VDR menu.
216   return NULL;
217 }
218
219 cMenuSetupPage *cPluginJsonserver::SetupMenu(void)
220 {
221   // Return a setup menu in case the plugin supports one.
222   return NULL;
223 }
224
225 bool cPluginJsonserver::SetupParse(const char *Name, const char *Value)
226 {
227   // Parse your own setup parameters and store their values.
228   return false;
229 }
230
231 bool cPluginJsonserver::Service(const char *Id, void *Data)
232 {
233   // Handle custom service requests from other plugins
234   return false;
235 }
236
237 const char **cPluginJsonserver::SVDRPHelpPages(void)
238 {
239   // Return help text for SVDRP commands this plugin implements
240   return NULL;
241 }
242
243 cString cPluginJsonserver::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
244 {
245   // Process SVDRP commands this plugin implements
246   return NULL;
247 }
248
249 VDRPLUGINCREATOR(cPluginJsonserver); // Don't touch this!