2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mvpclient.h"
23 // This is here else it causes compile errors with something in libdvbmpeg
26 MVPClient::MVPClient(char* tconfigDirExtra, int tsocket)
31 recordingManager = NULL;
32 log = Log::getInstance();
34 configDirExtra = tconfigDirExtra;
37 MVPClient::~MVPClient()
39 log->log("Client", Log::DEBUG, "MVP client destructor");
50 delete recordingManager;
52 recordingManager = NULL;
55 if (loggedIn) cleanConfig();
58 ULLONG MVPClient::ntohll(ULLONG a)
63 ULLONG MVPClient::htonll(ULLONG a)
65 #if BYTE_ORDER == BIG_ENDIAN
70 b = ((a << 56) & 0xFF00000000000000ULL)
71 | ((a << 40) & 0x00FF000000000000ULL)
72 | ((a << 24) & 0x0000FF0000000000ULL)
73 | ((a << 8) & 0x000000FF00000000ULL)
74 | ((a >> 8) & 0x00000000FF000000ULL)
75 | ((a >> 24) & 0x0000000000FF0000ULL)
76 | ((a >> 40) & 0x000000000000FF00ULL)
77 | ((a >> 56) & 0x00000000000000FFULL) ;
83 cChannel* MVPClient::channelFromNumber(ULONG channelNumber)
85 cChannel* channel = NULL;
87 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
89 if (!channel->GroupSep())
91 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
93 if (channel->Number() == (int)channelNumber)
95 int vpid = channel->Vpid();
96 #if VDRVERSNUM < 10300
97 int apid1 = channel->Apid1();
99 int apid1 = channel->Apid(0);
101 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
109 log->log("Client", Log::DEBUG, "Channel not found");
115 void MVPClient::writeResumeData()
117 config.setValueLong("ResumeData",
118 (char*)rp->getCurrentRecording()->FileName(),
119 rp->frameNumberFromPosition(rp->getLastPosition()) );
122 void MVPClient::sendULONG(ULONG ul)
125 *(ULONG*)&sendBuffer[0] = htonl(4);
126 *(ULONG*)&sendBuffer[4] = htonl(ul);
128 tcp.sendPacket(sendBuffer, 8);
129 log->log("Client", Log::DEBUG, "written ULONG %lu", ul);
132 void MVPClientStartThread(void* arg)
134 MVPClient* m = (MVPClient*)arg;
136 // Nothing external to this class has a reference to it
137 // This is the end of the thread.. so delete m
144 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
145 log->log("Client", Log::DEBUG, "MVPClient run success");
149 void MVPClient::run2()
154 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
155 pthread_detach(runThread); // Detach
157 tcp.disableReadTimeout();
159 tcp.setSoKeepTime(3);
160 tcp.setNonBlocking();
170 log->log("Client", Log::DEBUG, "Waiting");
171 buffer = (UCHAR*)tcp.receivePacket();
172 log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength());
175 log->log("Client", Log::DEBUG, "Detected connection closed");
179 packetLength = tcp.getDataLength() - 4;
180 opcode = ntohl(*(ULONG*)buffer);
183 if (!loggedIn && (opcode != 1))
189 log->log("Client", Log::DEBUG, "SwitchOp");
193 result = processLogin(data, packetLength);
196 result = processGetRecordingsList(data, packetLength);
199 result = processDeleteRecording(data, packetLength);
202 result = processGetSummary(data, packetLength);
205 result = processGetChannelsList(data, packetLength);
208 result = processStartStreamingChannel(data, packetLength);
211 result = processGetBlock(data, packetLength);
214 result = processStopStreaming(data, packetLength);
217 result = processStartStreamingRecording(data, packetLength);
220 result = processGetChannelSchedule(data, packetLength);
223 result = processConfigSave(data, packetLength);
226 result = processConfigLoad(data, packetLength);
229 result = processReScanRecording(data, packetLength);
232 result = processGetTimers(data, packetLength);
235 result = processSetTimer(data, packetLength);
238 result = processPositionFromFrameNumber(data, packetLength);
241 result = processFrameNumberFromPosition(data, packetLength);
244 result = processMoveRecording(data, packetLength);
247 result = processGetIFrame(data, packetLength);
256 int MVPClient::processLogin(UCHAR* buffer, int length)
258 if (length != 6) return 0;
262 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
265 log->log("Client", Log::DEBUG, "No config dir!");
269 char configFileName[PATH_MAX];
270 snprintf(configFileName, PATH_MAX, "%s/vomp-%02X-%02X-%02X-%02X-%02X-%02X.conf", configDir, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
271 config.init(configFileName);
273 // Send the login reply
275 time_t timeNow = time(NULL);
276 struct tm* timeStruct = localtime(&timeNow);
277 int timeOffset = timeStruct->tm_gmtoff;
279 UCHAR sendBuffer[12];
280 *(ULONG*)&sendBuffer[0] = htonl(8);
281 *(ULONG*)&sendBuffer[4] = htonl(timeNow);
282 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
284 tcp.sendPacket(sendBuffer, 12);
285 log->log("Client", Log::DEBUG, "written login reply");
291 int MVPClient::processGetRecordingsList(UCHAR* data, int length)
293 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
294 int count = 4; // leave space for the packet length
299 int Percent = VideoDiskSpace(&FreeMB);
300 int Total = (FreeMB / (100 - Percent)) * 100;
302 *(ULONG*)&sendBuffer[count] = htonl(Total);
303 count += sizeof(ULONG);
304 *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
305 count += sizeof(ULONG);
306 *(ULONG*)&sendBuffer[count] = htonl(Percent);
307 count += sizeof(ULONG);
310 cRecordings Recordings;
313 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
315 if (count > 49000) break; // just how big is that hard disk?!
316 *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
319 point = (char*)recording->Name();
320 strcpy((char*)&sendBuffer[count], point);
321 count += strlen(point) + 1;
323 point = (char*)recording->FileName();
324 strcpy((char*)&sendBuffer[count], point);
325 count += strlen(point) + 1;
328 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
330 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
332 tcp.sendPacket(sendBuffer, count);
334 log->log("Client", Log::DEBUG, "Written list");
339 int MVPClient::processDeleteRecording(UCHAR* data, int length)
341 // data is a pointer to the fileName string
343 cRecordings Recordings;
344 Recordings.Load(); // probably have to do this
346 cRecording* recording = Recordings.GetByName((char*)data);
348 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
352 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
354 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
357 if (recording->Delete())
359 // Copy svdrp's way of doing this, see if it works
360 #if VDRVERSNUM > 10300
361 ::Recordings.DelByName(recording->FileName());
383 int MVPClient::processMoveRecording(UCHAR* data, int length)
385 log->log("Client", Log::DEBUG, "Process move recording");
386 char* fileName = (char*)data;
387 char* newPath = NULL;
389 for (int k = 0; k < length; k++)
393 newPath = (char*)&data[k+1];
397 if (!newPath) return 0;
399 cRecordings Recordings;
400 Recordings.Load(); // probably have to do this
402 cRecording* recording = Recordings.GetByName((char*)fileName);
404 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
408 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
411 log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name());
412 log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName());
413 log->log("Client", Log::DEBUG, "to: %s", newPath);
415 const char* t = recording->FileName();
417 char* dateDirName = NULL; int k;
418 char* titleDirName = NULL; int j;
420 // Find the datedirname
421 for(k = strlen(t) - 1; k >= 0; k--)
425 log->log("Client", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
426 dateDirName = new char[strlen(&t[k+1]) + 1];
427 strcpy(dateDirName, &t[k+1]);
432 // Find the titledirname
434 for(j = k-1; j >= 0; j--)
438 log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
439 titleDirName = new char[(k - j - 1) + 1];
440 memcpy(titleDirName, &t[j+1], k - j - 1);
441 titleDirName[k - j - 1] = '\0';
446 log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName);
447 log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName);
449 log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory);
451 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1];
452 log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1);
453 sprintf(newContainer, "%s%s%s", VideoDirectory, newPath, titleDirName);
455 // FIXME Check whether this already exists before mkdiring it
457 log->log("Client", Log::DEBUG, "%s", newContainer);
461 int statret = stat(newContainer, &dstat);
462 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
464 log->log("Client", Log::DEBUG, "new dir does not exist");
465 int mkdirret = mkdir(newContainer, 0755);
468 delete[] dateDirName;
469 delete[] titleDirName;
470 delete[] newContainer;
475 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
477 delete[] dateDirName;
478 delete[] titleDirName;
479 delete[] newContainer;
484 // Ok, the directory container has been made, or it pre-existed.
486 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
487 sprintf(newDir, "%s/%s", newContainer, dateDirName);
489 log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
490 int renameret = rename(t, newDir);
493 // Success. Test for remove old dir containter
494 char* oldTitleDir = new char[k+1];
495 memcpy(oldTitleDir, t, k);
496 oldTitleDir[k] = '\0';
497 log->log("Client", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
498 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
499 delete[] oldTitleDir;
504 #if VDRVERSNUM > 10311
506 ::Recordings.Update();
508 // Success. Send a different packet from just a ulong
509 int totalLength = 4 + 4 + strlen(newDir) + 1;
510 UCHAR* sendBuffer = new UCHAR[totalLength];
511 *(ULONG*)&sendBuffer[0] = htonl(totalLength - 4);
512 *(ULONG*)&sendBuffer[4] = htonl(1); // success
513 strcpy((char*)&sendBuffer[8], newDir);
514 tcp.sendPacket(sendBuffer, totalLength);
522 delete[] dateDirName;
523 delete[] titleDirName;
524 delete[] newContainer;
540 int MVPClient::processGetSummary(UCHAR* data, int length)
542 // data is a pointer to the fileName string
544 cRecordings Recordings;
545 Recordings.Load(); // probably have to do this
547 cRecording *recording = Recordings.GetByName((char*)data);
549 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
553 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
554 int count = 4; // leave space for the packet length
557 #if VDRVERSNUM < 10300
558 point = (char*)recording->Summary();
560 const cRecordingInfo *Info = recording->Info();
561 point = (char*)Info->ShortText();
562 log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point);
565 point = (char*)Info->Description();
566 log->log("Client", Log::DEBUG, "description pointer %p", point);
572 strcpy((char*)&sendBuffer[count], point);
573 count += strlen(point) + 1;
577 strcpy((char*)&sendBuffer[count], "");
581 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
583 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
585 tcp.sendPacket(sendBuffer, count);
587 log->log("Client", Log::DEBUG, "Written summary");
599 int MVPClient::processGetChannelsList(UCHAR* data, int length)
601 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
602 int count = 4; // leave space for the packet length
606 char* chanConfig = config.getValueString("General", "Channels");
608 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
610 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
612 #if VDRVERSNUM < 10300
613 if (!channel->GroupSep() && (!channel->Ca() || allChans))
615 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
618 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
620 if (channel->Vpid()) type = 1;
621 #if VDRVERSNUM < 10300
624 else if (channel->Apid(0)) type = 2;
628 if (count > 49000) break;
629 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
632 *(ULONG*)&sendBuffer[count] = htonl(type);
635 point = (char*)channel->Name();
636 strcpy((char*)&sendBuffer[count], point);
637 count += strlen(point) + 1;
641 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
643 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
645 tcp.sendPacket(sendBuffer, count);
647 log->log("Client", Log::DEBUG, "Written channels list");
652 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
654 log->log("Client", Log::DEBUG, "length = %i", length);
655 ULONG channelNumber = ntohl(*(ULONG*)data);
657 cChannel* channel = channelFromNumber(channelNumber);
664 // get the priority we should use
666 int priority = config.getValueLong("General", "Live priority", &fail);
669 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
673 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
678 if (priority < 0) priority = 0;
679 if (priority > 99) priority = 99;
681 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
682 lp = MVPReceiver::create(channel, priority);
702 int MVPClient::processStopStreaming(UCHAR* data, int length)
704 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
715 delete recordingManager;
717 recordingManager = NULL;
724 int MVPClient::processGetBlock(UCHAR* data, int length)
728 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
732 ULLONG position = ntohll(*(ULLONG*)data);
733 data += sizeof(ULLONG);
734 ULONG amount = ntohl(*(ULONG*)data);
736 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
738 UCHAR sendBuffer[amount + 4];
739 ULONG amountReceived = 0; // compiler moan.
742 log->log("Client", Log::DEBUG, "getting from live");
743 amountReceived = lp->getBlock(&sendBuffer[4], amount);
747 // vdr has possibly disconnected the receiver
748 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
755 log->log("Client", Log::DEBUG, "getting from recording");
756 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
762 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
766 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
767 tcp.sendPacket(sendBuffer, amountReceived + 4);
768 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
774 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
776 // data is a pointer to the fileName string
778 recordingManager = new cRecordings;
779 recordingManager->Load();
781 cRecording* recording = recordingManager->GetByName((char*)data);
783 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
787 rp = new RecPlayer(recording);
789 UCHAR sendBuffer[16];
790 *(ULONG*)&sendBuffer[0] = htonl(12);
791 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
792 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
794 tcp.sendPacket(sendBuffer, 16);
795 log->log("Client", Log::DEBUG, "written totalLength");
799 delete recordingManager;
800 recordingManager = NULL;
805 int MVPClient::processReScanRecording(UCHAR* data, int length)
809 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
815 UCHAR sendBuffer[16];
816 *(ULONG*)&sendBuffer[0] = htonl(12);
817 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
818 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
820 tcp.sendPacket(sendBuffer, 16);
821 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
825 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
829 ULONG frameNumber = ntohl(*(ULONG*)data);
834 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
838 retval = rp->positionFromFrameNumber(frameNumber);
841 UCHAR sendBuffer[12];
842 *(ULONG*)&sendBuffer[0] = htonl(8);
843 *(ULLONG*)&sendBuffer[4] = htonll(retval);
845 tcp.sendPacket(sendBuffer, 12);
846 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
850 int MVPClient::processFrameNumberFromPosition(UCHAR* data, int length)
854 ULLONG position = ntohll(*(ULLONG*)data);
859 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
863 retval = rp->frameNumberFromPosition(position);
867 *(ULONG*)&sendBuffer[0] = htonl(4);
868 *(ULONG*)&sendBuffer[4] = htonl(retval);
870 tcp.sendPacket(sendBuffer, 8);
871 log->log("Client", Log::DEBUG, "Wrote frameNumFromPos reply to client");
875 int MVPClient::processGetIFrame(UCHAR* data, int length)
877 bool success = false;
879 ULONG frameNumber = ntohl(*(ULONG*)data);
881 ULONG direction = ntohl(*(ULONG*)data);
884 ULLONG rfilePosition = 0;
885 ULONG rframeNumber = 0;
886 ULONG rframeLength = 0;
890 log->log("Client", Log::DEBUG, "GetIFrame recording called when no recording being played!");
894 success = rp->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
897 // returns file position, frame number, length
899 UCHAR sendBuffer[20];
905 *(ULONG*)&sendBuffer[0] = htonl(16);
906 *(ULLONG*)&sendBuffer[4] = htonll(rfilePosition);
907 *(ULONG*)&sendBuffer[12] = htonl(rframeNumber);
908 *(ULONG*)&sendBuffer[16] = htonl(rframeLength);
913 *(ULONG*)&sendBuffer[0] = htonl(4);
914 *(ULONG*)&sendBuffer[4] = 0;
917 log->log("Client", Log::DEBUG, "%llu %lu %lu", rfilePosition, rframeNumber, rframeLength);
919 tcp.sendPacket(sendBuffer, packetLength);
920 log->log("Client", Log::DEBUG, "Wrote GNIF reply to client");
924 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
926 ULONG channelNumber = ntohl(*(ULONG*)data);
928 ULONG startTime = ntohl(*(ULONG*)data);
930 ULONG duration = ntohl(*(ULONG*)data);
932 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
934 cChannel* channel = channelFromNumber(channelNumber);
938 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
942 log->log("Client", Log::DEBUG, "Got channel");
944 #if VDRVERSNUM < 10300
945 cMutexLock MutexLock;
946 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
948 cSchedulesLock MutexLock;
949 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
954 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
958 log->log("Client", Log::DEBUG, "Got schedule!s! object");
960 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
964 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
968 log->log("Client", Log::DEBUG, "Got schedule object");
970 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
971 ULONG sendBufferLength = 100000;
972 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
976 // assign all the event info to temp vars then we know exactly what size they are
979 ULONG thisEventDuration;
980 const char* thisEventTitle;
981 const char* thisEventSubTitle;
982 const char* thisEventDescription;
984 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
985 ULONG thisEventLength;
987 #if VDRVERSNUM < 10300
989 const cEventInfo *event;
990 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
992 event = Schedule->GetEventNumber(eventNumber);
994 thisEventID = event->GetEventID();
995 thisEventTime = event->GetTime();
996 thisEventDuration = event->GetDuration();
997 thisEventTitle = event->GetTitle();
998 thisEventSubTitle = event->GetSubtitle();
999 thisEventDescription = event->GetExtendedDescription();
1003 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
1005 thisEventID = event->EventID();
1006 thisEventTime = event->StartTime();
1007 thisEventDuration = event->Duration();
1008 thisEventTitle = event->Title();
1009 thisEventSubTitle = NULL;
1010 thisEventDescription = event->Description();
1014 log->log("Client", Log::DEBUG, "Got an event object %p", event);
1016 //in the past filter
1017 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
1020 if ((thisEventTime + thisEventDuration) <= startTime) continue;
1023 if (thisEventTime >= (startTime + duration)) continue;
1025 if (!thisEventTitle) thisEventTitle = empty;
1026 if (!thisEventSubTitle) thisEventSubTitle = empty;
1027 if (!thisEventDescription) thisEventDescription = empty;
1029 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
1031 log->log("Client", Log::DEBUG, "Done s1");
1033 // now extend the buffer if necessary
1034 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
1036 log->log("Client", Log::DEBUG, "Extending buffer");
1037 sendBufferLength += 100000;
1038 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
1042 UCHAR sendBuffer2[8];
1043 *(ULONG*)&sendBuffer2[0] = htonl(4);
1044 *(ULONG*)&sendBuffer2[4] = htonl(0);
1045 tcp.sendPacket(sendBuffer2, 8);
1046 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
1052 log->log("Client", Log::DEBUG, "Done s2");
1054 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
1055 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
1056 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
1058 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
1059 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
1060 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
1062 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
1065 log->log("Client", Log::DEBUG, "Got all event data");
1067 if (sendBufferUsed == sizeof(ULONG))
1071 log->log("Client", Log::DEBUG, "Written 0 because no data");
1075 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
1076 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
1077 tcp.sendPacket(sendBuffer, sendBufferUsed);
1078 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
1086 int MVPClient::processConfigSave(UCHAR* buffer, int length)
1088 char* section = (char*)buffer;
1092 for (int k = 0; k < length; k++)
1094 if (buffer[k] == '\0')
1098 key = (char*)&buffer[k+1];
1102 value = (char*)&buffer[k+1];
1108 // if the last string (value) doesnt have null terminator, give up
1109 if (buffer[length - 1] != '\0') return 0;
1111 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
1112 if (config.setValueString(section, key, value))
1124 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
1126 char* section = (char*)buffer;
1129 for (int k = 0; k < length; k++)
1131 if (buffer[k] == '\0')
1133 key = (char*)&buffer[k+1];
1138 char* value = config.getValueString(section, key);
1142 UCHAR sendBuffer[4 + strlen(value) + 1];
1143 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
1144 strcpy((char*)&sendBuffer[4], value);
1145 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
1147 log->log("Client", Log::DEBUG, "Written config load packet");
1152 UCHAR sendBuffer[8];
1153 *(ULONG*)&sendBuffer[0] = htonl(4);
1154 *(ULONG*)&sendBuffer[4] = htonl(0);
1155 tcp.sendPacket(sendBuffer, 8);
1157 log->log("Client", Log::DEBUG, "Written config load failed packet");
1163 void MVPClient::cleanConfig()
1165 log->log("Client", Log::DEBUG, "Clean config");
1167 cRecordings Recordings;
1172 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
1173 char* position = resumes;
1174 for(int k = 0; k < numReturns; k++)
1176 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
1178 cRecording* recording = Recordings.GetByName(position);
1181 // doesn't exist anymore
1182 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
1183 config.deleteValue("ResumeData", position);
1187 log->log("Client", Log::DEBUG, "This recording still exists");
1190 position += strlen(position) + 1;
1202 event = Schedule->GetPresentEvent();
1204 fprintf(f, "\n\nCurrent event\n\n");
1206 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1207 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1208 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1209 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1211 event = Schedule->GetFollowingEvent();
1213 fprintf(f, "\n\nFollowing event\n\n");
1215 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1216 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1217 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1218 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1224 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1225 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1226 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1227 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
1235 void MVPClient::test2()
1237 FILE* f = fopen("/tmp/s.txt", "w");
1239 #if VDRVERSNUM < 10300
1240 cMutexLock MutexLock;
1241 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1243 cSchedulesLock MutexLock;
1244 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1249 fprintf(f, "Schedules = NULL\n");
1254 fprintf(f, "Schedules dump:\n");
1258 const cSchedule *Schedule;
1259 int scheduleNumber = 0;
1262 cChannel *thisChannel;
1264 #if VDRVERSNUM < 10300
1265 const cEventInfo *event;
1266 int eventNumber = 0;
1268 const cEvent *event;
1271 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1272 // Schedule = Schedules->GetSchedule();
1273 Schedule = Schedules->First();
1276 fprintf(f, "First Schedule = NULL\n");
1283 fprintf(f, "Schedule #%i\n", scheduleNumber);
1284 fprintf(f, "-------------\n\n");
1286 #if VDRVERSNUM < 10300
1287 tchid = Schedule->GetChannelID();
1289 tchid = Schedule->ChannelID();
1292 #if VDRVERSNUM < 10300
1293 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1294 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1296 // put the count at the end.
1299 thisChannel = Channels.GetByChannelID(tchid, true);
1302 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1306 fprintf(f, "thisChannel = NULL for tchid\n");
1309 #if VDRVERSNUM < 10300
1310 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1312 event = Schedule->GetEventNumber(eventNumber);
1313 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1314 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1315 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1316 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1317 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1318 fprintf(f, "Event %u dump:\n", eventNumber);
1323 // This whole section needs rewriting to walk the list.
1324 event = Schedule->Events()->First();
1326 event = Schedule->Events()->Next(event);
1331 fprintf(f, "\nDump from object:\n");
1333 fprintf(f, "\nEND\n");
1343 fprintf(f, "End of current Schedule\n\n\n");
1345 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1359 const cEventInfo *GetPresentEvent(void) const;
1360 const cEventInfo *GetFollowingEvent(void) const;
1361 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1362 const cEventInfo *GetEventAround(time_t tTime) const;
1363 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1366 const unsigned char GetTableID(void) const;
1367 const char *GetTimeString(void) const;
1368 const char *GetEndTimeString(void) const;
1369 const char *GetDate(void) const;
1370 bool IsFollowing(void) const;
1371 bool IsPresent(void) const;
1372 const char *GetExtendedDescription(void) const;
1373 const char *GetSubtitle(void) const;
1374 const char *GetTitle(void) const;
1375 unsigned short GetEventID(void) const;
1376 long GetDuration(void) const;
1377 time_t GetTime(void) const;
1378 tChannelID GetChannelID(void) const;
1379 int GetChannelNumber(void) const { return nChannelNumber; }
1380 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1381 void Dump(FILE *f, const char *Prefix = "") const;
1387 void MVPClient::test(int channelNumber)
1389 FILE* f = fopen("/tmp/test.txt", "w");
1391 cMutexLock MutexLock;
1392 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1396 fprintf(f, "Schedules = NULL\n");
1401 fprintf(f, "Schedules dump:\n");
1402 // Schedules->Dump(f);
1404 const cSchedule *Schedule;
1405 cChannel *thisChannel;
1406 const cEventInfo *event;
1408 thisChannel = channelFromNumber(channelNumber);
1411 fprintf(f, "thisChannel = NULL\n");
1416 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1417 // Schedule = Schedules->GetSchedule();
1418 // Schedule = Schedules->First();
1421 fprintf(f, "First Schedule = NULL\n");
1426 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1428 // For some channels VDR seems to pick a random point in time to
1429 // start dishing out events, but they are in order
1430 // at some point in the list the time snaps to the current event
1435 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1437 event = Schedule->GetEventNumber(eventNumber);
1438 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1439 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1440 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1444 fprintf(f, "\nEND\n");
1458 Schedules = the collection of all the Schedule objects
1459 Schedule = One schedule, contants all the events for a channel
1460 Event = One programme
1469 Subtitle (used for "Programmes resume at ...")
1472 IsPresent ? easy to work out tho. Oh it doesn't always work
1477 void MVPClient::test2()
1479 log->log("-", Log::DEBUG, "Timers List");
1481 for (int i = 0; i < Timers.Count(); i++)
1483 cTimer *timer = Timers.Get(i);
1484 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1485 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1486 #if VDRVERSNUM < 10300
1487 log->log("-", Log::DEBUG, "active=%i recording=%i pending=%i start=%li stop=%li priority=%i lifetime=%i", timer->Active(), timer->Recording(), timer->Pending(), timer->StartTime(), timer->StopTime(), timer->Priority(), timer->Lifetime());
1489 log->log("-", Log::DEBUG, "active=%i recording=%i pending=%i start=%li stop=%li priority=%i lifetime=%i", timer->HasFlags(tfActive), timer->Recording(), timer->Pending(), timer->StartTime(), timer->StopTime(), timer->Priority(), timer->Lifetime());
1491 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1492 log->log("-", Log::DEBUG, "");
1495 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1496 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1497 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1502 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1503 recording is a bool, 0 for not currently recording, 1 for currently recording
1504 pending is a bool, 0 for would not be trying to record this right now, 1 for would/is trying to record this right now
1508 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1510 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1511 int count = 4; // leave space for the packet length
1513 const char* fileName;
1515 int numTimers = Timers.Count();
1517 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1519 for (int i = 0; i < numTimers; i++)
1521 if (count > 49000) break;
1523 timer = Timers.Get(i);
1525 #if VDRVERSNUM < 10300
1526 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1528 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1530 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1531 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1532 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1533 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1534 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1535 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1536 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1538 fileName = timer->File();
1539 strcpy((char*)&sendBuffer[count], fileName);
1540 count += strlen(fileName) + 1;
1543 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1545 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1547 tcp.sendPacket(sendBuffer, count);
1548 delete[] sendBuffer;
1549 log->log("Client", Log::DEBUG, "Written timers list");
1554 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1556 char* timerString = new char[strlen((char*)buffer) + 1];
1557 strcpy(timerString, (char*)buffer);
1559 #if VDRVERSNUM < 10300
1561 // If this is VDR 1.2 the date part of the timer string must be reduced
1562 // to just DD rather than YYYY-MM-DD
1564 int s = 0; // source
1565 int d = 0; // destination
1567 while(c != 2) // copy up to date section, including the second ':'
1569 timerString[d] = buffer[s];
1570 if (buffer[s] == ':') c++;
1574 // now it has copied up to the date section
1576 while(c != 2) // waste YYYY-MM-
1578 if (buffer[s] == '-') c++;
1581 // now source is at the DD
1582 memcpy(&timerString[d], &buffer[s], length - s);
1584 timerString[d] = '\0';
1586 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1587 log->log("Client", Log::DEBUG, "%s", timerString);
1591 cTimer *timer = new cTimer;
1592 if (timer->Parse((char*)timerString))
1594 cTimer *t = Timers.GetTimer(timer);
1598 #if VDRVERSNUM < 10300
1601 Timers.SetModified();