5 #include <jsoncpp/json/json.h>
8 #include <vdr/videodir.h>
9 #include <vdr/recording.h>
11 #include <vdr/timers.h>
15 int jsonserver_request_handler(struct mg_connection *conn)
17 Log* log = Log::getInstance();
18 const struct mg_request_info *request_info = mg_get_request_info(conn);
20 if (strcmp(request_info->uri, "/jsonserver")) return 0; // not for us
23 int wvrl = mg_get_var(request_info->query_string, strlen(request_info->query_string), "req", wvrequest, 20);
26 log->log("JSONServer", Log::ERR, "Could not decode req");
31 if (!strcmp(request_info->request_method, "OPTIONS"))
33 mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n");
34 mg_printf(conn, "%s", "Access-Control-Allow-Origin: *\r\n");
35 mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
43 if (!strcmp(request_info->request_method, "POST"))
45 const char* contentLength = mg_get_header(conn, "Content-Length");
46 int contentLengthI = atoi(contentLength);
47 log->log("JSONServer", Log::DEBUG, "POST data content length: %i", contentLengthI);
48 if (contentLengthI > 10000)
50 log->log("JSONServer", Log::DEBUG, "Length > 10000, rejecting");
54 if (contentLengthI > 0)
56 // FIXME - assume for now that all post data will be small enough to have arrived immediately
57 int bytesRead = mg_read(conn, postData, contentLengthI);
58 if (bytesRead != contentLengthI)
60 log->log("JSONServer", Log::DEBUG, "Could not read up to contentLength");
69 if (!strcmp(wvrequest, "reclist")) success = jsonserver_reclist(js);
70 else if (!strcmp(wvrequest, "recinfo")) success = jsonserver_recinfo(js, postData);
71 else if (!strcmp(wvrequest, "recdel")) success = jsonserver_recdel(js, postData);
72 else if (!strcmp(wvrequest, "recmove")) success = jsonserver_recmove(js, postData);
73 else if (!strcmp(wvrequest, "channellist")) success = jsonserver_channellist(js);
74 else if (!strcmp(wvrequest, "channelschedule")) success = jsonserver_channelschedule(js, postData);
75 else if (!strcmp(wvrequest, "timerlist")) success = jsonserver_timerlist(js);
76 else if (!strcmp(wvrequest, "timerdel")) success = jsonserver_timerdel(js, postData);
77 else if (!strcmp(wvrequest, "timerset")) success = jsonserver_timerset(js, postData);
79 if (!success) return 0; // the specific handler failed badly
81 // Now js will be filled
83 Json::StyledWriter sw;
84 std::string jsonout = sw.write(js);
85 mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n");
86 mg_printf(conn, "%s", "Content-Type: text/plain\r\n\r\n");
87 mg_write(conn, jsonout.c_str(), jsonout.length());
92 else if (event == MG_EVENT_LOG)
94 if (request_info->status_code == 400) // bad request
96 log->log("Mongoose", Log::DEBUG, "400 BAD REQUEST:");
97 log->log("Mongoose", Log::DEBUG, request_info->request_method);
98 log->log("Mongoose", Log::DEBUG, request_info->uri);
99 log->log("Mongoose", Log::DEBUG, request_info->http_version);
100 log->log("Mongoose", Log::DEBUG, request_info->query_string);
101 log->log("Mongoose", Log::DEBUG, request_info->log_message);
102 for (int i = 0; i < request_info->num_headers; i++)
104 log->log("Mongoose", Log::DEBUG, "%s: %s", request_info->http_headers[i].name, request_info->http_headers[i].value);
109 log->log("Mongoose", Log::DEBUG, request_info->log_message);
110 log->log("Mongoose", Log::DEBUG, request_info->uri);
115 // other events not handled:
116 // MG_HTTP_ERROR, MG_INIT_SSL
117 // Let mongoose do something with those
125 bool jsonserver_reclist(Json::Value& js)
127 Log* log = Log::getInstance();
128 log->log("JSONServer", Log::DEBUG, "reclist");
131 int Percent = VideoDiskSpace(&FreeMB);
132 int Total = (FreeMB / (100 - Percent)) * 100;
134 js["MBFree"] = FreeMB;
135 js["Percent"] = Percent;
138 Json::Value jsrecordings;
139 cRecordings Recordings;
141 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
144 oneRec["StartTime"] = (Json::UInt)recording->Start();
145 oneRec["Length"] = (Json::UInt)recording->LengthInSeconds();
146 oneRec["IsNew"] = recording->IsNew();
147 oneRec["Name"] = recording->Name();
148 oneRec["Filename"] = recording->FileName();
149 jsrecordings.append(oneRec);
151 js["Recordings"] = jsrecordings;
156 bool jsonserver_recinfo(Json::Value& js, const char* postData)
158 // For now this returns just the string summary of the recording
160 Log* log = Log::getInstance();
161 log->log("JSONServer", Log::DEBUG, "recinfo");
163 char reqfilename[1000];
164 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
167 log->log("JSONServer", Log::ERR, "Could not decode filename");
168 js["Result"] = false;
169 js["Error"] = "Could not decode filename";
173 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
175 cRecordings Recordings;
176 Recordings.Load(); // probably have to do this
177 cRecording *recording = Recordings.GetByName(reqfilename);
181 const cRecordingInfo *Info = recording->Info();
182 char* summary = (char*)Info->ShortText();
183 if (isempty(summary)) summary = (char*)Info->Description();
185 js["Summary"] = summary;
189 log->log("JSONServer", Log::ERR, "recinfo found no recording");
190 js["Result"] = false;
197 bool jsonserver_recdel(Json::Value& js, const char* postData)
199 Log* log = Log::getInstance();
200 log->log("JSONServer", Log::DEBUG, "recdel");
202 char reqfilename[1000];
203 int mgv1 = mg_get_var(postData, strlen(postData), "filename", reqfilename, 1000);
206 log->log("JSONServer", Log::ERR, "Could not decode filename");
207 js["Result"] = false;
208 js["Error"] = "Could not decode filename";
212 log->log("JSONServer", Log::DEBUG, "%s", reqfilename);
214 cRecordings Recordings;
215 Recordings.Load(); // probably have to do this
216 cRecording *recording = Recordings.GetByName(reqfilename);
220 js["Result"] = false;
221 js["Error"] = "Could not find recording to delete";
225 log->log("JSONServer", Log::DEBUG, "Deleting recording: %s", recording->Name());
226 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
229 js["Result"] = false;
230 js["Error"] = "This recording is still recording.. ho ho";
234 if (recording->Delete())
236 ::Recordings.DelByName(recording->FileName());
241 js["Result"] = false;
242 js["Error"] = "Failed to delete recording";
248 bool jsonserver_recmove(Json::Value& js, const char* postData)
250 Log* log = Log::getInstance();
251 log->log("JSONServer", Log::DEBUG, "recmove");
253 char sFileName[1000]; int mgv1 = mg_get_var(postData, strlen(postData), "filename", sFileName, 1000);
254 char sNewPath[1000]; int mgv2 = mg_get_var(postData, strlen(postData), "newpath", sNewPath, 1000);
256 if ( (mgv1 == -1) || (mgv2 == -1) || !strlen(sFileName) || !strlen(sNewPath) )
258 log->log("JSONServer", Log::ERR, "request mgvs: %i %i", mgv1, mgv2);
259 js["Result"] = false;
260 js["Error"] = "Bad request parameters";
264 cRecordings Recordings;
265 Recordings.Load(); // probably have to do this
266 cRecording* recording = Recordings.GetByName(sFileName);
270 log->log("JSONServer", Log::ERR, "Could not find recording to move");
271 js["Result"] = false;
272 js["Error"] = "Bad filename";
276 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
279 log->log("JSONServer", Log::ERR, "Could not move recording, it is still recording");
280 js["Result"] = false;
281 js["Error"] = "Cannot move recording in progress";
285 log->log("JSONServer", Log::DEBUG, "moving recording: %s", recording->Name());
286 log->log("JSONServer", Log::DEBUG, "moving recording: %s", recording->FileName());
287 log->log("JSONServer", Log::DEBUG, "to: %s", sNewPath);
289 const char* t = recording->FileName();
291 char* dateDirName = NULL; int k;
292 char* titleDirName = NULL; int j;
293 char* newContainer = NULL;
295 // Find the datedirname
296 for(k = strlen(t) - 1; k >= 0; k--)
300 log->log("JSONServer", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
301 dateDirName = new char[strlen(&t[k+1]) + 1];
302 strcpy(dateDirName, &t[k+1]);
307 // Find the titledirname
309 for(j = k-1; j >= 0; j--)
313 log->log("JSONServer", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
314 titleDirName = new char[(k - j - 1) + 1];
315 memcpy(titleDirName, &t[j+1], k - j - 1);
316 titleDirName[k - j - 1] = '\0';
321 log->log("JSONServer", Log::DEBUG, "datedirname: %s", dateDirName);
322 log->log("JSONServer", Log::DEBUG, "titledirname: %s", titleDirName);
323 log->log("JSONServer", Log::DEBUG, "viddir: %s", VideoDirectory);
325 ExchangeChars(sNewPath, true);
326 log->log("JSONServer", Log::DEBUG, "EC: %s", sNewPath);
328 newContainer = new char[strlen(VideoDirectory) + strlen(sNewPath) + strlen(titleDirName) + 1];
329 log->log("JSONServer", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(sNewPath) + strlen(titleDirName) + 1);
330 sprintf(newContainer, "%s%s%s", VideoDirectory, sNewPath, titleDirName);
331 log->log("JSONServer", Log::DEBUG, "%s", newContainer);
334 int statret = stat(newContainer, &dstat);
335 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
337 log->log("JSONServer", Log::DEBUG, "new dir does not exist");
338 int mkdirret = mkdir(newContainer, 0755);
341 delete[] dateDirName;
342 delete[] titleDirName;
343 delete[] newContainer;
345 log->log("JSONServer", Log::ERR, "Failed to make new dir");
346 js["Result"] = false;
347 js["Error"] = "Failed to create new directory";
351 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
353 delete[] dateDirName;
354 delete[] titleDirName;
355 delete[] newContainer;
357 log->log("JSONServer", Log::ERR, "Something already exists?");
358 js["Result"] = false;
359 js["Error"] = "Something already exists at the new path";
363 // Ok, the directory container has been made, or it pre-existed.
365 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
366 sprintf(newDir, "%s/%s", newContainer, dateDirName);
368 log->log("JSONServer", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
369 int renameret = rename(t, newDir);
372 // Success. Test for remove old dir containter
373 char* oldTitleDir = new char[k+1];
374 memcpy(oldTitleDir, t, k);
375 oldTitleDir[k] = '\0';
376 log->log("JSONServer", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
377 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
378 delete[] oldTitleDir;
380 ::Recordings.Update();
385 log->log("JSONServer", Log::ERR, "Rename failed");
386 js["Result"] = false;
387 js["Error"] = "Move failed";
390 delete[] dateDirName;
391 delete[] titleDirName;
392 delete[] newContainer;
398 bool jsonserver_channellist(Json::Value& js)
400 Log* log = Log::getInstance();
401 log->log("JSONServer", Log::DEBUG, "channellist");
403 Json::Value jschannels;
406 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
408 if (!channel->GroupSep())
410 log->log("JSONServer", Log::DEBUG, "name: '%s'", channel->Name());
412 if (channel->Vpid()) type = 1;
413 else if (channel->Apid(0)) type = 2;
416 Json::Value oneChannel;
417 oneChannel["Number"] = channel->Number();
418 oneChannel["Type"] = type;
419 oneChannel["Name"] = channel->Name();
420 #if VDRVERSNUM < 10703
421 oneChannel["VType"] = 2;
423 oneChannel["VType"] = channel->Vtype();
425 jschannels.append(oneChannel);
428 js["Channels"] = jschannels;
433 bool jsonserver_channelschedule(Json::Value& js, const char* postData)
435 Log* log = Log::getInstance();
436 log->log("JSONServer", Log::DEBUG, "channelschedule '%s'", postData);
438 char sChannelNumber[15]; int mgv1 = mg_get_var(postData, strlen(postData), "channelnumber", sChannelNumber, 15);
439 char sStartTime[15]; int mgv2 = mg_get_var(postData, strlen(postData), "starttime", sStartTime, 15);
440 char sDuration[15]; int mgv3 = mg_get_var(postData, strlen(postData), "duration", sDuration, 15);
442 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) )
444 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i", mgv1, mgv2, mgv3);
445 js["Result"] = false;
446 js["Error"] = "Bad request parameters";
450 int channelNumber = atoi(sChannelNumber);
451 int startTime = atoi(sStartTime);
452 int duration = atoi(sDuration);
454 cChannel* channel = NULL;
455 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
457 if (channel->GroupSep()) continue;
458 if (channel->Number() == channelNumber) break;
463 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
464 js["Result"] = false;
465 js["Error"] = "Could not find channel";
469 cSchedulesLock MutexLock;
470 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
473 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
474 js["Result"] = false;
475 js["Error"] = "Internal schedules error (1)";
478 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
481 log->log("JSONServer", Log::ERR, "Could not find requested channel: %i", channelNumber);
482 js["Result"] = false;
483 js["Error"] = "Internal schedules error (2)";
487 Json::Value jsevents;
489 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
492 if ((event->StartTime() + event->Duration()) < time(NULL)) continue;
494 if ((event->StartTime() + event->Duration()) <= startTime) continue;
496 if (event->StartTime() >= (startTime + duration)) continue;
498 Json::Value oneEvent;
499 oneEvent["ID"] = event->EventID();
500 oneEvent["Time"] = (Json::UInt)event->StartTime();
501 oneEvent["Duration"] = event->Duration();
502 oneEvent["Title"] = event->Title() ? event->Title() : "";
503 oneEvent["Description"] = event->Description() ? event->Description() : "";
504 jsevents.append(oneEvent);
507 js["Events"] = jsevents;
511 bool jsonserver_timerlist(Json::Value& js)
513 Log* log = Log::getInstance();
514 log->log("JSONServer", Log::DEBUG, "timerlist");
516 Json::Value jstimers;
519 int numTimers = Timers.Count();
521 for (int i = 0; i < numTimers; i++)
523 timer = Timers.Get(i);
524 Json::Value oneTimer;
525 oneTimer["Active"] = timer->HasFlags(tfActive);
526 oneTimer["Recording"] = timer->Recording();
527 oneTimer["Pending"] = timer->Pending();
528 oneTimer["Priority"] = timer->Priority();
529 oneTimer["Lifetime"] = timer->Lifetime();
530 oneTimer["ChannelNumber"] = timer->Channel()->Number();
531 oneTimer["StartTime"] = (int)timer->StartTime();
532 oneTimer["StopTime"] = (int)timer->StopTime();
533 oneTimer["Day"] = (int)timer->Day();
534 oneTimer["WeekDays"] = timer->WeekDays();
535 oneTimer["Name"] = timer->File();
536 jstimers.append(oneTimer);
539 js["Timers"] = jstimers;
543 bool jsonserver_timerdel(Json::Value& js, const char* postData)
545 Log* log = Log::getInstance();
546 log->log("JSONServer", Log::DEBUG, "timerdel");
548 char sdelChannel[15]; int mgv1 = mg_get_var(postData, strlen(postData), "delchannel", sdelChannel, 15);
549 char sdelWeekdays[15]; int mgv2 = mg_get_var(postData, strlen(postData), "delweekdays", sdelWeekdays, 15);
550 char sdelDay[15]; int mgv3 = mg_get_var(postData, strlen(postData), "delday", sdelDay, 15);
551 char sdelStart[15]; int mgv4 = mg_get_var(postData, strlen(postData), "delstart", sdelStart, 15);
552 char sdelStop[15]; int mgv5 = mg_get_var(postData, strlen(postData), "delstop", sdelStop, 15);
554 if ( (mgv1 == -1) || (mgv2 == -1) || (mgv3 == -1) || (mgv4 == -1) || (mgv5 == -1) )
556 log->log("JSONServer", Log::ERR, "request mgvs: %i %i %i %i %i", mgv1, mgv2, mgv3, mgv4, mgv5);
557 js["Result"] = false;
558 js["Error"] = "Bad request parameters";
562 int delChannel = atoi(sdelChannel);
563 int delWeekdays = atoi(sdelWeekdays);
564 int delDay = atoi(sdelDay);
565 int delStart = atoi(sdelStart);
566 int delStop = atoi(sdelStop);
569 for (ti = Timers.First(); ti; ti = Timers.Next(ti))
571 if ( (ti->Channel()->Number() == delChannel)
572 && ((ti->WeekDays() && (ti->WeekDays() == delWeekdays)) || (!ti->WeekDays() && (ti->Day() == delDay)))
573 && (ti->StartTime() == delStart)
574 && (ti->StopTime() == delStop) )
580 log->log("JSONServer", Log::ERR, "Could not find timer");
581 js["Result"] = false;
582 js["Error"] = "Could not find timer";
586 if (Timers.BeingEdited())
588 log->log("JSONServer", Log::ERR, "Unable to delete timer - timers being edited at VDR");
589 js["Result"] = false;
590 js["Error"] = "Timers being edited at VDR";
596 log->log("JSONServer", Log::ERR, "Unable to delete timer - timer is running");
597 js["Result"] = false;
598 js["Error"] = "Timer is running";
603 Timers.SetModified();
609 bool jsonserver_timerset(Json::Value& js, const char* postData)
611 Log* log = Log::getInstance();
612 log->log("JSONServer", Log::DEBUG, "timerset");
614 char sTimerString[1024]; int mgv1 = mg_get_var(postData, strlen(postData), "timerstring", sTimerString, 1024);
618 log->log("JSONServer", Log::ERR, "Could not get timerstring");
619 js["Result"] = false;
620 js["Error"] = "Bad request parameters";
624 log->log("JSONServer", Log::DEBUG, "'%s'", sTimerString);
625 cTimer *timer = new cTimer;
626 if (!timer->Parse(sTimerString))
629 js["Result"] = false;
630 js["Error"] = "Failed to parse timer request details";
634 cTimer *t = Timers.GetTimer(timer);
638 js["Result"] = false;
639 js["Error"] = "Timer already exists";
644 Timers.SetModified();