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 = processGetChannelsList(data, packetLength);
205 result = processStartStreamingChannel(data, packetLength);
208 result = processGetBlock(data, packetLength);
211 result = processStopStreaming(data, packetLength);
214 result = processStartStreamingRecording(data, packetLength);
217 result = processGetChannelSchedule(data, packetLength);
220 result = processConfigSave(data, packetLength);
223 result = processConfigLoad(data, packetLength);
226 result = processReScanRecording(data, packetLength); // FIXME obselete
229 result = processGetTimers(data, packetLength);
232 result = processSetTimer(data, packetLength);
235 result = processPositionFromFrameNumber(data, packetLength);
238 result = processFrameNumberFromPosition(data, packetLength);
241 result = processMoveRecording(data, packetLength);
244 result = processGetIFrame(data, packetLength);
247 result = processGetRecInfo(data, packetLength);
250 result = processGetMarks(data, packetLength);
259 int MVPClient::processLogin(UCHAR* buffer, int length)
261 if (length != 6) return 0;
265 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
268 log->log("Client", Log::DEBUG, "No config dir!");
272 char configFileName[PATH_MAX];
273 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]);
274 config.init(configFileName);
276 // Send the login reply
278 time_t timeNow = time(NULL);
279 struct tm* timeStruct = localtime(&timeNow);
280 int timeOffset = timeStruct->tm_gmtoff;
282 UCHAR sendBuffer[12];
283 *(ULONG*)&sendBuffer[0] = htonl(8);
284 *(ULONG*)&sendBuffer[4] = htonl(timeNow);
285 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
287 tcp.sendPacket(sendBuffer, 12);
288 log->log("Client", Log::DEBUG, "written login reply");
294 int MVPClient::processGetRecordingsList(UCHAR* data, int length)
296 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
297 int count = 4; // leave space for the packet length
302 int Percent = VideoDiskSpace(&FreeMB);
303 int Total = (FreeMB / (100 - Percent)) * 100;
305 *(ULONG*)&sendBuffer[count] = htonl(Total);
306 count += sizeof(ULONG);
307 *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
308 count += sizeof(ULONG);
309 *(ULONG*)&sendBuffer[count] = htonl(Percent);
310 count += sizeof(ULONG);
313 cRecordings Recordings;
316 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
318 if (count > 49000) break; // just how big is that hard disk?!
319 *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
322 point = (char*)recording->Name();
323 strcpy((char*)&sendBuffer[count], point);
324 count += strlen(point) + 1;
326 point = (char*)recording->FileName();
327 strcpy((char*)&sendBuffer[count], point);
328 count += strlen(point) + 1;
331 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
333 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
335 tcp.sendPacket(sendBuffer, count);
337 log->log("Client", Log::DEBUG, "Written list");
342 int MVPClient::processDeleteRecording(UCHAR* data, int length)
344 // data is a pointer to the fileName string
346 cRecordings Recordings;
347 Recordings.Load(); // probably have to do this
349 cRecording* recording = Recordings.GetByName((char*)data);
351 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
355 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
357 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
360 if (recording->Delete())
362 // Copy svdrp's way of doing this, see if it works
363 #if VDRVERSNUM > 10300
364 ::Recordings.DelByName(recording->FileName());
386 int MVPClient::processMoveRecording(UCHAR* data, int length)
388 log->log("Client", Log::DEBUG, "Process move recording");
389 char* fileName = (char*)data;
390 char* newPath = NULL;
392 for (int k = 0; k < length; k++)
396 newPath = (char*)&data[k+1];
400 if (!newPath) return 0;
402 cRecordings Recordings;
403 Recordings.Load(); // probably have to do this
405 cRecording* recording = Recordings.GetByName((char*)fileName);
407 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
411 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
414 log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name());
415 log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName());
416 log->log("Client", Log::DEBUG, "to: %s", newPath);
418 const char* t = recording->FileName();
420 char* dateDirName = NULL; int k;
421 char* titleDirName = NULL; int j;
423 // Find the datedirname
424 for(k = strlen(t) - 1; k >= 0; k--)
428 log->log("Client", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
429 dateDirName = new char[strlen(&t[k+1]) + 1];
430 strcpy(dateDirName, &t[k+1]);
435 // Find the titledirname
437 for(j = k-1; j >= 0; j--)
441 log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
442 titleDirName = new char[(k - j - 1) + 1];
443 memcpy(titleDirName, &t[j+1], k - j - 1);
444 titleDirName[k - j - 1] = '\0';
449 log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName);
450 log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName);
452 log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory);
454 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1];
455 log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1);
456 sprintf(newContainer, "%s%s%s", VideoDirectory, newPath, titleDirName);
458 // FIXME Check whether this already exists before mkdiring it
460 log->log("Client", Log::DEBUG, "%s", newContainer);
464 int statret = stat(newContainer, &dstat);
465 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
467 log->log("Client", Log::DEBUG, "new dir does not exist");
468 int mkdirret = mkdir(newContainer, 0755);
471 delete[] dateDirName;
472 delete[] titleDirName;
473 delete[] newContainer;
478 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
480 delete[] dateDirName;
481 delete[] titleDirName;
482 delete[] newContainer;
487 // Ok, the directory container has been made, or it pre-existed.
489 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
490 sprintf(newDir, "%s/%s", newContainer, dateDirName);
492 log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
493 int renameret = rename(t, newDir);
496 // Success. Test for remove old dir containter
497 char* oldTitleDir = new char[k+1];
498 memcpy(oldTitleDir, t, k);
499 oldTitleDir[k] = '\0';
500 log->log("Client", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
501 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
502 delete[] oldTitleDir;
507 #if VDRVERSNUM > 10311
509 ::Recordings.Update();
511 // Success. Send a different packet from just a ulong
512 int totalLength = 4 + 4 + strlen(newDir) + 1;
513 UCHAR* sendBuffer = new UCHAR[totalLength];
514 *(ULONG*)&sendBuffer[0] = htonl(totalLength - 4);
515 *(ULONG*)&sendBuffer[4] = htonl(1); // success
516 strcpy((char*)&sendBuffer[8], newDir);
517 tcp.sendPacket(sendBuffer, totalLength);
525 delete[] dateDirName;
526 delete[] titleDirName;
527 delete[] newContainer;
543 int MVPClient::processGetChannelsList(UCHAR* data, int length)
545 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
546 int count = 4; // leave space for the packet length
550 char* chanConfig = config.getValueString("General", "Channels");
552 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
554 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
556 #if VDRVERSNUM < 10300
557 if (!channel->GroupSep() && (!channel->Ca() || allChans))
559 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
562 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
564 if (channel->Vpid()) type = 1;
565 #if VDRVERSNUM < 10300
568 else if (channel->Apid(0)) type = 2;
572 if (count > 49000) break;
573 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
576 *(ULONG*)&sendBuffer[count] = htonl(type);
579 point = (char*)channel->Name();
580 strcpy((char*)&sendBuffer[count], point);
581 count += strlen(point) + 1;
585 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
587 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
589 tcp.sendPacket(sendBuffer, count);
591 log->log("Client", Log::DEBUG, "Written channels list");
596 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
598 log->log("Client", Log::DEBUG, "length = %i", length);
599 ULONG channelNumber = ntohl(*(ULONG*)data);
601 cChannel* channel = channelFromNumber(channelNumber);
608 // get the priority we should use
610 int priority = config.getValueLong("General", "Live priority", &fail);
613 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
617 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
622 if (priority < 0) priority = 0;
623 if (priority > 99) priority = 99;
625 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
626 lp = MVPReceiver::create(channel, priority);
646 int MVPClient::processStopStreaming(UCHAR* data, int length)
648 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
659 delete recordingManager;
661 recordingManager = NULL;
668 int MVPClient::processGetBlock(UCHAR* data, int length)
672 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
676 ULLONG position = ntohll(*(ULLONG*)data);
677 data += sizeof(ULLONG);
678 ULONG amount = ntohl(*(ULONG*)data);
680 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
682 UCHAR sendBuffer[amount + 4];
683 ULONG amountReceived = 0; // compiler moan.
686 log->log("Client", Log::DEBUG, "getting from live");
687 amountReceived = lp->getBlock(&sendBuffer[4], amount);
691 // vdr has possibly disconnected the receiver
692 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
699 log->log("Client", Log::DEBUG, "getting from recording");
700 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
706 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
710 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
711 tcp.sendPacket(sendBuffer, amountReceived + 4);
712 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
718 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
720 // data is a pointer to the fileName string
722 recordingManager = new cRecordings;
723 recordingManager->Load();
725 cRecording* recording = recordingManager->GetByName((char*)data);
727 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
731 rp = new RecPlayer(recording);
733 UCHAR sendBuffer[16];
734 *(ULONG*)&sendBuffer[0] = htonl(12);
735 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
736 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
738 tcp.sendPacket(sendBuffer, 16);
739 log->log("Client", Log::DEBUG, "written totalLength");
743 delete recordingManager;
744 recordingManager = NULL;
749 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
753 ULONG frameNumber = ntohl(*(ULONG*)data);
758 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
762 retval = rp->positionFromFrameNumber(frameNumber);
765 UCHAR sendBuffer[12];
766 *(ULONG*)&sendBuffer[0] = htonl(8);
767 *(ULLONG*)&sendBuffer[4] = htonll(retval);
769 tcp.sendPacket(sendBuffer, 12);
770 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
774 int MVPClient::processFrameNumberFromPosition(UCHAR* data, int length)
778 ULLONG position = ntohll(*(ULLONG*)data);
783 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
787 retval = rp->frameNumberFromPosition(position);
791 *(ULONG*)&sendBuffer[0] = htonl(4);
792 *(ULONG*)&sendBuffer[4] = htonl(retval);
794 tcp.sendPacket(sendBuffer, 8);
795 log->log("Client", Log::DEBUG, "Wrote frameNumFromPos reply to client");
799 int MVPClient::processGetIFrame(UCHAR* data, int length)
801 bool success = false;
803 ULONG frameNumber = ntohl(*(ULONG*)data);
805 ULONG direction = ntohl(*(ULONG*)data);
808 ULLONG rfilePosition = 0;
809 ULONG rframeNumber = 0;
810 ULONG rframeLength = 0;
814 log->log("Client", Log::DEBUG, "GetIFrame recording called when no recording being played!");
818 success = rp->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
821 // returns file position, frame number, length
823 UCHAR sendBuffer[20];
829 *(ULONG*)&sendBuffer[0] = htonl(16);
830 *(ULLONG*)&sendBuffer[4] = htonll(rfilePosition);
831 *(ULONG*)&sendBuffer[12] = htonl(rframeNumber);
832 *(ULONG*)&sendBuffer[16] = htonl(rframeLength);
837 *(ULONG*)&sendBuffer[0] = htonl(4);
838 *(ULONG*)&sendBuffer[4] = 0;
841 log->log("Client", Log::DEBUG, "%llu %lu %lu", rfilePosition, rframeNumber, rframeLength);
843 tcp.sendPacket(sendBuffer, packetLength);
844 log->log("Client", Log::DEBUG, "Wrote GNIF reply to client");
848 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
850 ULONG channelNumber = ntohl(*(ULONG*)data);
852 ULONG startTime = ntohl(*(ULONG*)data);
854 ULONG duration = ntohl(*(ULONG*)data);
856 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
858 cChannel* channel = channelFromNumber(channelNumber);
862 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
866 log->log("Client", Log::DEBUG, "Got channel");
868 #if VDRVERSNUM < 10300
869 cMutexLock MutexLock;
870 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
872 cSchedulesLock MutexLock;
873 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
878 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
882 log->log("Client", Log::DEBUG, "Got schedule!s! object");
884 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
888 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
892 log->log("Client", Log::DEBUG, "Got schedule object");
894 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
895 ULONG sendBufferLength = 100000;
896 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
900 // assign all the event info to temp vars then we know exactly what size they are
903 ULONG thisEventDuration;
904 const char* thisEventTitle;
905 const char* thisEventSubTitle;
906 const char* thisEventDescription;
908 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
909 ULONG thisEventLength;
911 #if VDRVERSNUM < 10300
913 const cEventInfo *event;
914 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
916 event = Schedule->GetEventNumber(eventNumber);
918 thisEventID = event->GetEventID();
919 thisEventTime = event->GetTime();
920 thisEventDuration = event->GetDuration();
921 thisEventTitle = event->GetTitle();
922 thisEventSubTitle = event->GetSubtitle();
923 thisEventDescription = event->GetExtendedDescription();
927 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
929 thisEventID = event->EventID();
930 thisEventTime = event->StartTime();
931 thisEventDuration = event->Duration();
932 thisEventTitle = event->Title();
933 thisEventSubTitle = NULL;
934 thisEventDescription = event->Description();
938 log->log("Client", Log::DEBUG, "Got an event object %p", event);
941 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
944 if ((thisEventTime + thisEventDuration) <= startTime) continue;
947 if (thisEventTime >= (startTime + duration)) continue;
949 if (!thisEventTitle) thisEventTitle = empty;
950 if (!thisEventSubTitle) thisEventSubTitle = empty;
951 if (!thisEventDescription) thisEventDescription = empty;
953 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
955 log->log("Client", Log::DEBUG, "Done s1");
957 // now extend the buffer if necessary
958 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
960 log->log("Client", Log::DEBUG, "Extending buffer");
961 sendBufferLength += 100000;
962 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
966 UCHAR sendBuffer2[8];
967 *(ULONG*)&sendBuffer2[0] = htonl(4);
968 *(ULONG*)&sendBuffer2[4] = htonl(0);
969 tcp.sendPacket(sendBuffer2, 8);
970 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
976 log->log("Client", Log::DEBUG, "Done s2");
978 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
979 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
980 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
982 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
983 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
984 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
986 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
989 log->log("Client", Log::DEBUG, "Got all event data");
991 if (sendBufferUsed == sizeof(ULONG))
995 log->log("Client", Log::DEBUG, "Written 0 because no data");
999 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
1000 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
1001 tcp.sendPacket(sendBuffer, sendBufferUsed);
1002 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
1010 int MVPClient::processConfigSave(UCHAR* buffer, int length)
1012 char* section = (char*)buffer;
1016 for (int k = 0; k < length; k++)
1018 if (buffer[k] == '\0')
1022 key = (char*)&buffer[k+1];
1026 value = (char*)&buffer[k+1];
1032 // if the last string (value) doesnt have null terminator, give up
1033 if (buffer[length - 1] != '\0') return 0;
1035 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
1036 if (config.setValueString(section, key, value))
1048 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
1050 char* section = (char*)buffer;
1053 for (int k = 0; k < length; k++)
1055 if (buffer[k] == '\0')
1057 key = (char*)&buffer[k+1];
1062 char* value = config.getValueString(section, key);
1066 UCHAR sendBuffer[4 + strlen(value) + 1];
1067 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
1068 strcpy((char*)&sendBuffer[4], value);
1069 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
1071 log->log("Client", Log::DEBUG, "Written config load packet");
1076 UCHAR sendBuffer[8];
1077 *(ULONG*)&sendBuffer[0] = htonl(4);
1078 *(ULONG*)&sendBuffer[4] = htonl(0);
1079 tcp.sendPacket(sendBuffer, 8);
1081 log->log("Client", Log::DEBUG, "Written config load failed packet");
1087 void MVPClient::cleanConfig()
1089 log->log("Client", Log::DEBUG, "Clean config");
1091 cRecordings Recordings;
1096 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
1097 char* position = resumes;
1098 for(int k = 0; k < numReturns; k++)
1100 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
1102 cRecording* recording = Recordings.GetByName(position);
1105 // doesn't exist anymore
1106 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
1107 config.deleteValue("ResumeData", position);
1111 log->log("Client", Log::DEBUG, "This recording still exists");
1114 position += strlen(position) + 1;
1126 event = Schedule->GetPresentEvent();
1128 fprintf(f, "\n\nCurrent event\n\n");
1130 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1131 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1132 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1133 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1135 event = Schedule->GetFollowingEvent();
1137 fprintf(f, "\n\nFollowing event\n\n");
1139 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1140 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1141 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1142 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1148 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1149 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1150 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1151 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
1159 void MVPClient::test2()
1161 FILE* f = fopen("/tmp/s.txt", "w");
1163 #if VDRVERSNUM < 10300
1164 cMutexLock MutexLock;
1165 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1167 cSchedulesLock MutexLock;
1168 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1173 fprintf(f, "Schedules = NULL\n");
1178 fprintf(f, "Schedules dump:\n");
1182 const cSchedule *Schedule;
1183 int scheduleNumber = 0;
1186 cChannel *thisChannel;
1188 #if VDRVERSNUM < 10300
1189 const cEventInfo *event;
1190 int eventNumber = 0;
1192 const cEvent *event;
1195 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1196 // Schedule = Schedules->GetSchedule();
1197 Schedule = Schedules->First();
1200 fprintf(f, "First Schedule = NULL\n");
1207 fprintf(f, "Schedule #%i\n", scheduleNumber);
1208 fprintf(f, "-------------\n\n");
1210 #if VDRVERSNUM < 10300
1211 tchid = Schedule->GetChannelID();
1213 tchid = Schedule->ChannelID();
1216 #if VDRVERSNUM < 10300
1217 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1218 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1220 // put the count at the end.
1223 thisChannel = Channels.GetByChannelID(tchid, true);
1226 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1230 fprintf(f, "thisChannel = NULL for tchid\n");
1233 #if VDRVERSNUM < 10300
1234 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1236 event = Schedule->GetEventNumber(eventNumber);
1237 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1238 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1239 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1240 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1241 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1242 fprintf(f, "Event %u dump:\n", eventNumber);
1247 // This whole section needs rewriting to walk the list.
1248 event = Schedule->Events()->First();
1250 event = Schedule->Events()->Next(event);
1255 fprintf(f, "\nDump from object:\n");
1257 fprintf(f, "\nEND\n");
1267 fprintf(f, "End of current Schedule\n\n\n");
1269 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1283 const cEventInfo *GetPresentEvent(void) const;
1284 const cEventInfo *GetFollowingEvent(void) const;
1285 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1286 const cEventInfo *GetEventAround(time_t tTime) const;
1287 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1290 const unsigned char GetTableID(void) const;
1291 const char *GetTimeString(void) const;
1292 const char *GetEndTimeString(void) const;
1293 const char *GetDate(void) const;
1294 bool IsFollowing(void) const;
1295 bool IsPresent(void) const;
1296 const char *GetExtendedDescription(void) const;
1297 const char *GetSubtitle(void) const;
1298 const char *GetTitle(void) const;
1299 unsigned short GetEventID(void) const;
1300 long GetDuration(void) const;
1301 time_t GetTime(void) const;
1302 tChannelID GetChannelID(void) const;
1303 int GetChannelNumber(void) const { return nChannelNumber; }
1304 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1305 void Dump(FILE *f, const char *Prefix = "") const;
1311 void MVPClient::test(int channelNumber)
1313 FILE* f = fopen("/tmp/test.txt", "w");
1315 cMutexLock MutexLock;
1316 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1320 fprintf(f, "Schedules = NULL\n");
1325 fprintf(f, "Schedules dump:\n");
1326 // Schedules->Dump(f);
1328 const cSchedule *Schedule;
1329 cChannel *thisChannel;
1330 const cEventInfo *event;
1332 thisChannel = channelFromNumber(channelNumber);
1335 fprintf(f, "thisChannel = NULL\n");
1340 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1341 // Schedule = Schedules->GetSchedule();
1342 // Schedule = Schedules->First();
1345 fprintf(f, "First Schedule = NULL\n");
1350 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1352 // For some channels VDR seems to pick a random point in time to
1353 // start dishing out events, but they are in order
1354 // at some point in the list the time snaps to the current event
1359 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1361 event = Schedule->GetEventNumber(eventNumber);
1362 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1363 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1364 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1368 fprintf(f, "\nEND\n");
1382 Schedules = the collection of all the Schedule objects
1383 Schedule = One schedule, contants all the events for a channel
1384 Event = One programme
1393 Subtitle (used for "Programmes resume at ...")
1396 IsPresent ? easy to work out tho. Oh it doesn't always work
1401 void MVPClient::test2()
1403 log->log("-", Log::DEBUG, "Timers List");
1405 for (int i = 0; i < Timers.Count(); i++)
1407 cTimer *timer = Timers.Get(i);
1408 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1409 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1410 #if VDRVERSNUM < 10300
1411 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());
1413 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());
1415 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1416 log->log("-", Log::DEBUG, "");
1419 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1420 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1421 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1426 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1427 recording is a bool, 0 for not currently recording, 1 for currently recording
1428 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
1432 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1434 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1435 int count = 4; // leave space for the packet length
1437 const char* fileName;
1439 int numTimers = Timers.Count();
1441 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1443 for (int i = 0; i < numTimers; i++)
1445 if (count > 49000) break;
1447 timer = Timers.Get(i);
1449 #if VDRVERSNUM < 10300
1450 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1452 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1454 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1455 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1456 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1457 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1458 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1459 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1460 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1462 fileName = timer->File();
1463 strcpy((char*)&sendBuffer[count], fileName);
1464 count += strlen(fileName) + 1;
1467 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1469 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1471 tcp.sendPacket(sendBuffer, count);
1472 delete[] sendBuffer;
1473 log->log("Client", Log::DEBUG, "Written timers list");
1478 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1480 char* timerString = new char[strlen((char*)buffer) + 1];
1481 strcpy(timerString, (char*)buffer);
1483 #if VDRVERSNUM < 10300
1485 // If this is VDR 1.2 the date part of the timer string must be reduced
1486 // to just DD rather than YYYY-MM-DD
1488 int s = 0; // source
1489 int d = 0; // destination
1491 while(c != 2) // copy up to date section, including the second ':'
1493 timerString[d] = buffer[s];
1494 if (buffer[s] == ':') c++;
1498 // now it has copied up to the date section
1500 while(c != 2) // waste YYYY-MM-
1502 if (buffer[s] == '-') c++;
1505 // now source is at the DD
1506 memcpy(&timerString[d], &buffer[s], length - s);
1508 timerString[d] = '\0';
1510 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1511 log->log("Client", Log::DEBUG, "%s", timerString);
1515 cTimer *timer = new cTimer;
1516 if (timer->Parse((char*)timerString))
1518 cTimer *t = Timers.GetTimer(timer);
1522 #if VDRVERSNUM < 10300
1525 Timers.SetModified();
1543 int MVPClient::processGetRecInfo(UCHAR* data, int length)
1545 // data is a pointer to the fileName string
1547 cRecordings Recordings;
1548 Recordings.Load(); // probably have to do this
1550 cRecording *recording = Recordings.GetByName((char*)data);
1552 time_t timerStart = 0;
1553 time_t timerStop = 0;
1554 char* summary = NULL;
1555 ULONG resumePoint = 0;
1559 log->log("Client", Log::ERR, "GetRecInfo found no recording");
1564 ULONG sendBufferSize = 10000;
1565 UCHAR* sendBuffer = (UCHAR*)malloc(sendBufferSize);
1566 ULONG pos = 4; // leave first 4 bytes for size field
1570 4 bytes: start time for timer
1571 4 bytes: end time for timer
1572 4 bytes: resume point
1574 4 bytes: num components
1584 // Get current timer
1586 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
1589 timerStart = rc->Timer()->StartTime();
1590 timerStop = rc->Timer()->StopTime();
1591 log->log("Client", Log::DEBUG, "GRI: RC: %lu %lu", timerStart, timerStop);
1594 *(time_t*)&sendBuffer[pos] = htonl(timerStart); pos += 4;
1595 *(time_t*)&sendBuffer[pos] = htonl(timerStop); pos += 4;
1599 char* value = config.getValueString("ResumeData", (char*)data);
1602 resumePoint = strtoul(value, NULL, 10);
1605 log->log("Client", Log::DEBUG, "GRI: RP: %lu", resumePoint);
1607 *(ULONG*)&sendBuffer[pos] = htonl(resumePoint); pos += 4;
1612 #if VDRVERSNUM < 10300
1613 summary = (char*)recording->Summary();
1615 const cRecordingInfo *Info = recording->Info();
1616 summary = (char*)Info->ShortText();
1617 if (isempty(summary)) summary = (char*)Info->Description();
1619 log->log("Client", Log::DEBUG, "GRI: S: %s", summary);
1622 // memory insanity...
1623 if ((sendBufferSize - pos) < (strlen(summary) + 500)) // random
1625 UCHAR* newBuffer = (UCHAR*)realloc(sendBuffer, sendBufferSize + strlen(summary) + 10000);
1628 sendBuffer = newBuffer;
1629 sendBufferSize += strlen(summary) + 10000;
1639 strcpy((char*)&sendBuffer[pos], summary);
1640 pos += strlen(summary) + 1;
1644 strcpy((char*)&sendBuffer[pos], "");
1651 #if VDRVERSNUM < 10300
1653 // Send 0 for numchannels - this signals the client this info is not available
1654 *(ULONG*)&sendBuffer[pos] = 0; pos += 4;
1657 const cComponents* components = Info->Components();
1659 log->log("Client", Log::DEBUG, "GRI: D1: %p", components);
1663 *(ULONG*)&sendBuffer[pos] = htonl(0); pos += 4;
1667 *(ULONG*)&sendBuffer[pos] = htonl(components->NumComponents()); pos += 4;
1669 tComponent* component;
1670 for (int i = 0; i < components->NumComponents(); i++)
1672 component = components->Component(i);
1674 // memory insanity...
1675 ULONG extraNeeded = 2 + (component->language ? strlen(component->language) : 0)
1676 + (component->description ? strlen(component->description) : 0) + 2;
1678 if ((sendBufferSize - pos) < extraNeeded)
1680 UCHAR* newBuffer = (UCHAR*)realloc(sendBuffer, sendBufferSize + extraNeeded + 10000);
1683 sendBuffer = newBuffer;
1684 sendBufferSize += extraNeeded + 10000;
1694 log->log("Client", Log::DEBUG, "GRI: C: %i %u %u %s %s", i, component->stream, component->type, component->language, component->description);
1695 sendBuffer[pos] = component->stream; pos += 1;
1696 sendBuffer[pos] = component->type; pos += 1;
1697 if (component->language)
1699 strcpy((char*)&sendBuffer[pos], component->language);
1700 pos += strlen(component->language) + 1;
1704 strcpy((char*)&sendBuffer[pos], "");
1707 if (component->description)
1709 strcpy((char*)&sendBuffer[pos], component->description);
1710 pos += strlen(component->description) + 1;
1714 strcpy((char*)&sendBuffer[pos], "");
1725 *(ULONG*)&sendBuffer[0] = htonl(pos - 4); // -4 : take off the size field
1727 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1729 tcp.sendPacket(sendBuffer, pos);
1730 delete[] sendBuffer;
1731 log->log("Client", Log::DEBUG, "Written getrecinfo");
1741 int MVPClient::processReScanRecording(UCHAR* data, int length)
1745 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
1751 UCHAR sendBuffer[16];
1752 *(ULONG*)&sendBuffer[0] = htonl(12);
1753 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
1754 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
1756 tcp.sendPacket(sendBuffer, 16);
1757 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
1761 // FIXME without client calling rescan, getblock wont work even tho more data is avail
1764 int MVPClient::processGetMarks(UCHAR* data, int length)
1766 // data is a pointer to the fileName string
1768 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1769 int count = 4; // leave space for the packet length
1773 cRecordings Recordings;
1774 Recordings.Load(); // probably have to do this
1776 cRecording *recording = Recordings.GetByName((char*)data);
1778 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
1782 Marks.Load(recording->FileName());
1785 for (const cMark *m = Marks.First(); m; m = Marks.Next(m))
1787 log->log("Client", Log::DEBUG, "found Mark %i", m->position);
1789 if (count > 49000) break;
1790 *(ULONG*)&sendBuffer[count] = htonl(m->position);
1796 log->log("Client", Log::DEBUG, "no marks found, sending 0-mark");
1797 *(ULONG*)&sendBuffer[count] = htonl(0);
1802 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1804 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1806 tcp.sendPacket(sendBuffer, count);
1807 delete[] sendBuffer;
1808 log->log("Client", Log::DEBUG, "Written Marks list");