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);
253 result = processGetChannelPids(data, packetLength);
262 int MVPClient::processLogin(UCHAR* buffer, int length)
264 if (length != 6) return 0;
268 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
271 log->log("Client", Log::DEBUG, "No config dir!");
275 char configFileName[PATH_MAX];
276 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]);
277 config.init(configFileName);
279 // Send the login reply
281 time_t timeNow = time(NULL);
282 struct tm* timeStruct = localtime(&timeNow);
283 int timeOffset = timeStruct->tm_gmtoff;
285 UCHAR sendBuffer[12];
286 *(ULONG*)&sendBuffer[0] = htonl(8);
287 *(ULONG*)&sendBuffer[4] = htonl(timeNow);
288 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
290 tcp.sendPacket(sendBuffer, 12);
291 log->log("Client", Log::DEBUG, "written login reply");
297 int MVPClient::processGetRecordingsList(UCHAR* data, int length)
299 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
300 int count = 4; // leave space for the packet length
305 int Percent = VideoDiskSpace(&FreeMB);
306 int Total = (FreeMB / (100 - Percent)) * 100;
308 *(ULONG*)&sendBuffer[count] = htonl(Total);
309 count += sizeof(ULONG);
310 *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
311 count += sizeof(ULONG);
312 *(ULONG*)&sendBuffer[count] = htonl(Percent);
313 count += sizeof(ULONG);
316 cRecordings Recordings;
319 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
321 if (count > 49000) break; // just how big is that hard disk?!
322 *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
325 point = (char*)recording->Name();
326 strcpy((char*)&sendBuffer[count], point);
327 count += strlen(point) + 1;
329 point = (char*)recording->FileName();
330 strcpy((char*)&sendBuffer[count], point);
331 count += strlen(point) + 1;
334 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
336 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
338 tcp.sendPacket(sendBuffer, count);
340 log->log("Client", Log::DEBUG, "Written list");
345 int MVPClient::processDeleteRecording(UCHAR* data, int length)
347 // data is a pointer to the fileName string
349 cRecordings Recordings;
350 Recordings.Load(); // probably have to do this
352 cRecording* recording = Recordings.GetByName((char*)data);
354 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
358 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
360 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
363 if (recording->Delete())
365 // Copy svdrp's way of doing this, see if it works
366 #if VDRVERSNUM > 10300
367 ::Recordings.DelByName(recording->FileName());
389 int MVPClient::processMoveRecording(UCHAR* data, int length)
391 log->log("Client", Log::DEBUG, "Process move recording");
392 char* fileName = (char*)data;
393 char* newPath = NULL;
395 for (int k = 0; k < length; k++)
399 newPath = (char*)&data[k+1];
403 if (!newPath) return 0;
405 cRecordings Recordings;
406 Recordings.Load(); // probably have to do this
408 cRecording* recording = Recordings.GetByName((char*)fileName);
410 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
414 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
417 log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name());
418 log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName());
419 log->log("Client", Log::DEBUG, "to: %s", newPath);
421 const char* t = recording->FileName();
423 char* dateDirName = NULL; int k;
424 char* titleDirName = NULL; int j;
426 // Find the datedirname
427 for(k = strlen(t) - 1; k >= 0; k--)
431 log->log("Client", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
432 dateDirName = new char[strlen(&t[k+1]) + 1];
433 strcpy(dateDirName, &t[k+1]);
438 // Find the titledirname
440 for(j = k-1; j >= 0; j--)
444 log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
445 titleDirName = new char[(k - j - 1) + 1];
446 memcpy(titleDirName, &t[j+1], k - j - 1);
447 titleDirName[k - j - 1] = '\0';
452 log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName);
453 log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName);
455 log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory);
457 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1];
458 log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1);
459 sprintf(newContainer, "%s%s%s", VideoDirectory, newPath, titleDirName);
461 // FIXME Check whether this already exists before mkdiring it
463 log->log("Client", Log::DEBUG, "%s", newContainer);
467 int statret = stat(newContainer, &dstat);
468 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
470 log->log("Client", Log::DEBUG, "new dir does not exist");
471 int mkdirret = mkdir(newContainer, 0755);
474 delete[] dateDirName;
475 delete[] titleDirName;
476 delete[] newContainer;
481 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
483 delete[] dateDirName;
484 delete[] titleDirName;
485 delete[] newContainer;
490 // Ok, the directory container has been made, or it pre-existed.
492 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
493 sprintf(newDir, "%s/%s", newContainer, dateDirName);
495 log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
496 int renameret = rename(t, newDir);
499 // Success. Test for remove old dir containter
500 char* oldTitleDir = new char[k+1];
501 memcpy(oldTitleDir, t, k);
502 oldTitleDir[k] = '\0';
503 log->log("Client", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
504 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
505 delete[] oldTitleDir;
510 #if VDRVERSNUM > 10311
512 ::Recordings.Update();
514 // Success. Send a different packet from just a ulong
515 int totalLength = 4 + 4 + strlen(newDir) + 1;
516 UCHAR* sendBuffer = new UCHAR[totalLength];
517 *(ULONG*)&sendBuffer[0] = htonl(totalLength - 4);
518 *(ULONG*)&sendBuffer[4] = htonl(1); // success
519 strcpy((char*)&sendBuffer[8], newDir);
520 tcp.sendPacket(sendBuffer, totalLength);
528 delete[] dateDirName;
529 delete[] titleDirName;
530 delete[] newContainer;
546 int MVPClient::processGetChannelsList(UCHAR* data, int length)
548 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
549 int count = 4; // leave space for the packet length
553 char* chanConfig = config.getValueString("General", "Channels");
555 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
557 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
559 #if VDRVERSNUM < 10300
560 if (!channel->GroupSep() && (!channel->Ca() || allChans))
562 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
565 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
567 if (channel->Vpid()) type = 1;
568 #if VDRVERSNUM < 10300
571 else if (channel->Apid(0)) type = 2;
575 if (count > 49000) break;
576 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
579 *(ULONG*)&sendBuffer[count] = htonl(type);
582 point = (char*)channel->Name();
583 strcpy((char*)&sendBuffer[count], point);
584 count += strlen(point) + 1;
588 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
590 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
592 tcp.sendPacket(sendBuffer, count);
594 log->log("Client", Log::DEBUG, "Written channels list");
599 int MVPClient::processGetChannelPids(UCHAR* data, int length)
601 ULONG channelNumber = ntohl(*(ULONG*)data);
603 cChannel* channel = channelFromNumber(channelNumber);
611 ULONG spaceRequired = 12; // 4 for length field, 4 for vpid, 4 for number of apids
612 // Work out space required and number of Apids
613 for (const int *Apid = channel->Apids(); *Apid; Apid++)
615 spaceRequired += 4 + strlen(channel->Alang(numApids)) + 1; // 4 for pid, length of string + \0
619 // Format of response
627 UCHAR* sendBuffer = new UCHAR[spaceRequired];
629 *(ULONG*)&sendBuffer[point] = htonl(spaceRequired - 4); point += 4; // take off first 4 bytes
630 *(ULONG*)&sendBuffer[point] = htonl(channel->Vpid()); point += 4;
631 *(ULONG*)&sendBuffer[point] = htonl(numApids); point += 4;
633 for (ULONG i = 0; i < numApids; i++)
635 *(ULONG*)&sendBuffer[point] = htonl(channel->Apid(i)); point += 4;
636 strcpy((char*)&sendBuffer[point], channel->Alang(i)); point += strlen(channel->Alang(i)) + 1;
639 printf("About to send getchannelpids response. length = %u\n", spaceRequired);
640 tcp.dump(sendBuffer, spaceRequired);
642 tcp.sendPacket(sendBuffer, spaceRequired);
644 log->log("Client", Log::DEBUG, "Written channels pids");
649 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
651 log->log("Client", Log::DEBUG, "length = %i", length);
652 ULONG channelNumber = ntohl(*(ULONG*)data);
654 cChannel* channel = channelFromNumber(channelNumber);
661 // get the priority we should use
663 int priority = config.getValueLong("General", "Live priority", &fail);
666 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
670 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
675 if (priority < 0) priority = 0;
676 if (priority > 99) priority = 99;
678 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
679 lp = MVPReceiver::create(channel, priority);
699 int MVPClient::processStopStreaming(UCHAR* data, int length)
701 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
712 delete recordingManager;
714 recordingManager = NULL;
721 int MVPClient::processGetBlock(UCHAR* data, int length)
725 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
729 ULLONG position = ntohll(*(ULLONG*)data);
730 data += sizeof(ULLONG);
731 ULONG amount = ntohl(*(ULONG*)data);
733 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
735 UCHAR sendBuffer[amount + 4];
736 ULONG amountReceived = 0; // compiler moan.
739 log->log("Client", Log::DEBUG, "getting from live");
740 amountReceived = lp->getBlock(&sendBuffer[4], amount);
744 // vdr has possibly disconnected the receiver
745 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
752 log->log("Client", Log::DEBUG, "getting from recording");
753 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
759 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
763 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
764 tcp.sendPacket(sendBuffer, amountReceived + 4);
765 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
771 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
773 // data is a pointer to the fileName string
775 recordingManager = new cRecordings;
776 recordingManager->Load();
778 cRecording* recording = recordingManager->GetByName((char*)data);
780 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
784 rp = new RecPlayer(recording);
786 UCHAR sendBuffer[16];
787 *(ULONG*)&sendBuffer[0] = htonl(12);
788 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
789 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
791 tcp.sendPacket(sendBuffer, 16);
792 log->log("Client", Log::DEBUG, "written totalLength");
796 delete recordingManager;
797 recordingManager = NULL;
802 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
806 ULONG frameNumber = ntohl(*(ULONG*)data);
811 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
815 retval = rp->positionFromFrameNumber(frameNumber);
818 UCHAR sendBuffer[12];
819 *(ULONG*)&sendBuffer[0] = htonl(8);
820 *(ULLONG*)&sendBuffer[4] = htonll(retval);
822 tcp.sendPacket(sendBuffer, 12);
823 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
827 int MVPClient::processFrameNumberFromPosition(UCHAR* data, int length)
831 ULLONG position = ntohll(*(ULLONG*)data);
836 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
840 retval = rp->frameNumberFromPosition(position);
844 *(ULONG*)&sendBuffer[0] = htonl(4);
845 *(ULONG*)&sendBuffer[4] = htonl(retval);
847 tcp.sendPacket(sendBuffer, 8);
848 log->log("Client", Log::DEBUG, "Wrote frameNumFromPos reply to client");
852 int MVPClient::processGetIFrame(UCHAR* data, int length)
854 bool success = false;
856 ULONG frameNumber = ntohl(*(ULONG*)data);
858 ULONG direction = ntohl(*(ULONG*)data);
861 ULLONG rfilePosition = 0;
862 ULONG rframeNumber = 0;
863 ULONG rframeLength = 0;
867 log->log("Client", Log::DEBUG, "GetIFrame recording called when no recording being played!");
871 success = rp->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
874 // returns file position, frame number, length
876 UCHAR sendBuffer[20];
882 *(ULONG*)&sendBuffer[0] = htonl(16);
883 *(ULLONG*)&sendBuffer[4] = htonll(rfilePosition);
884 *(ULONG*)&sendBuffer[12] = htonl(rframeNumber);
885 *(ULONG*)&sendBuffer[16] = htonl(rframeLength);
890 *(ULONG*)&sendBuffer[0] = htonl(4);
891 *(ULONG*)&sendBuffer[4] = 0;
894 log->log("Client", Log::DEBUG, "%llu %lu %lu", rfilePosition, rframeNumber, rframeLength);
896 tcp.sendPacket(sendBuffer, packetLength);
897 log->log("Client", Log::DEBUG, "Wrote GNIF reply to client");
901 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
903 ULONG channelNumber = ntohl(*(ULONG*)data);
905 ULONG startTime = ntohl(*(ULONG*)data);
907 ULONG duration = ntohl(*(ULONG*)data);
909 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
911 cChannel* channel = channelFromNumber(channelNumber);
915 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
919 log->log("Client", Log::DEBUG, "Got channel");
921 #if VDRVERSNUM < 10300
922 cMutexLock MutexLock;
923 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
925 cSchedulesLock MutexLock;
926 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
931 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
935 log->log("Client", Log::DEBUG, "Got schedule!s! object");
937 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
941 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
945 log->log("Client", Log::DEBUG, "Got schedule object");
947 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
948 ULONG sendBufferLength = 100000;
949 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
953 // assign all the event info to temp vars then we know exactly what size they are
956 ULONG thisEventDuration;
957 const char* thisEventTitle;
958 const char* thisEventSubTitle;
959 const char* thisEventDescription;
961 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
962 ULONG thisEventLength;
964 #if VDRVERSNUM < 10300
966 const cEventInfo *event;
967 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
969 event = Schedule->GetEventNumber(eventNumber);
971 thisEventID = event->GetEventID();
972 thisEventTime = event->GetTime();
973 thisEventDuration = event->GetDuration();
974 thisEventTitle = event->GetTitle();
975 thisEventSubTitle = event->GetSubtitle();
976 thisEventDescription = event->GetExtendedDescription();
980 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
982 thisEventID = event->EventID();
983 thisEventTime = event->StartTime();
984 thisEventDuration = event->Duration();
985 thisEventTitle = event->Title();
986 thisEventSubTitle = NULL;
987 thisEventDescription = event->Description();
991 log->log("Client", Log::DEBUG, "Got an event object %p", event);
994 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
997 if ((thisEventTime + thisEventDuration) <= startTime) continue;
1000 if (thisEventTime >= (startTime + duration)) continue;
1002 if (!thisEventTitle) thisEventTitle = empty;
1003 if (!thisEventSubTitle) thisEventSubTitle = empty;
1004 if (!thisEventDescription) thisEventDescription = empty;
1006 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
1008 log->log("Client", Log::DEBUG, "Done s1");
1010 // now extend the buffer if necessary
1011 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
1013 log->log("Client", Log::DEBUG, "Extending buffer");
1014 sendBufferLength += 100000;
1015 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
1019 UCHAR sendBuffer2[8];
1020 *(ULONG*)&sendBuffer2[0] = htonl(4);
1021 *(ULONG*)&sendBuffer2[4] = htonl(0);
1022 tcp.sendPacket(sendBuffer2, 8);
1023 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
1029 log->log("Client", Log::DEBUG, "Done s2");
1031 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
1032 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
1033 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
1035 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
1036 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
1037 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
1039 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
1042 log->log("Client", Log::DEBUG, "Got all event data");
1044 if (sendBufferUsed == sizeof(ULONG))
1048 log->log("Client", Log::DEBUG, "Written 0 because no data");
1052 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
1053 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
1054 tcp.sendPacket(sendBuffer, sendBufferUsed);
1055 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
1063 int MVPClient::processConfigSave(UCHAR* buffer, int length)
1065 char* section = (char*)buffer;
1069 for (int k = 0; k < length; k++)
1071 if (buffer[k] == '\0')
1075 key = (char*)&buffer[k+1];
1079 value = (char*)&buffer[k+1];
1085 // if the last string (value) doesnt have null terminator, give up
1086 if (buffer[length - 1] != '\0') return 0;
1088 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
1089 if (config.setValueString(section, key, value))
1101 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
1103 char* section = (char*)buffer;
1106 for (int k = 0; k < length; k++)
1108 if (buffer[k] == '\0')
1110 key = (char*)&buffer[k+1];
1115 char* value = config.getValueString(section, key);
1119 UCHAR sendBuffer[4 + strlen(value) + 1];
1120 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
1121 strcpy((char*)&sendBuffer[4], value);
1122 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
1124 log->log("Client", Log::DEBUG, "Written config load packet");
1129 UCHAR sendBuffer[8];
1130 *(ULONG*)&sendBuffer[0] = htonl(4);
1131 *(ULONG*)&sendBuffer[4] = htonl(0);
1132 tcp.sendPacket(sendBuffer, 8);
1134 log->log("Client", Log::DEBUG, "Written config load failed packet");
1140 void MVPClient::cleanConfig()
1142 log->log("Client", Log::DEBUG, "Clean config");
1144 cRecordings Recordings;
1149 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
1150 char* position = resumes;
1151 for(int k = 0; k < numReturns; k++)
1153 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
1155 cRecording* recording = Recordings.GetByName(position);
1158 // doesn't exist anymore
1159 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
1160 config.deleteValue("ResumeData", position);
1164 log->log("Client", Log::DEBUG, "This recording still exists");
1167 position += strlen(position) + 1;
1179 event = Schedule->GetPresentEvent();
1181 fprintf(f, "\n\nCurrent event\n\n");
1183 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1184 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1185 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1186 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1188 event = Schedule->GetFollowingEvent();
1190 fprintf(f, "\n\nFollowing event\n\n");
1192 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1193 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1194 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1195 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1201 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1202 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1203 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1204 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
1212 void MVPClient::test2()
1214 FILE* f = fopen("/tmp/s.txt", "w");
1216 #if VDRVERSNUM < 10300
1217 cMutexLock MutexLock;
1218 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1220 cSchedulesLock MutexLock;
1221 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1226 fprintf(f, "Schedules = NULL\n");
1231 fprintf(f, "Schedules dump:\n");
1235 const cSchedule *Schedule;
1236 int scheduleNumber = 0;
1239 cChannel *thisChannel;
1241 #if VDRVERSNUM < 10300
1242 const cEventInfo *event;
1243 int eventNumber = 0;
1245 const cEvent *event;
1248 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1249 // Schedule = Schedules->GetSchedule();
1250 Schedule = Schedules->First();
1253 fprintf(f, "First Schedule = NULL\n");
1260 fprintf(f, "Schedule #%i\n", scheduleNumber);
1261 fprintf(f, "-------------\n\n");
1263 #if VDRVERSNUM < 10300
1264 tchid = Schedule->GetChannelID();
1266 tchid = Schedule->ChannelID();
1269 #if VDRVERSNUM < 10300
1270 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1271 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1273 // put the count at the end.
1276 thisChannel = Channels.GetByChannelID(tchid, true);
1279 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1283 fprintf(f, "thisChannel = NULL for tchid\n");
1286 #if VDRVERSNUM < 10300
1287 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1289 event = Schedule->GetEventNumber(eventNumber);
1290 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1291 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1292 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1293 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1294 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1295 fprintf(f, "Event %u dump:\n", eventNumber);
1300 // This whole section needs rewriting to walk the list.
1301 event = Schedule->Events()->First();
1303 event = Schedule->Events()->Next(event);
1308 fprintf(f, "\nDump from object:\n");
1310 fprintf(f, "\nEND\n");
1320 fprintf(f, "End of current Schedule\n\n\n");
1322 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1336 const cEventInfo *GetPresentEvent(void) const;
1337 const cEventInfo *GetFollowingEvent(void) const;
1338 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1339 const cEventInfo *GetEventAround(time_t tTime) const;
1340 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1343 const unsigned char GetTableID(void) const;
1344 const char *GetTimeString(void) const;
1345 const char *GetEndTimeString(void) const;
1346 const char *GetDate(void) const;
1347 bool IsFollowing(void) const;
1348 bool IsPresent(void) const;
1349 const char *GetExtendedDescription(void) const;
1350 const char *GetSubtitle(void) const;
1351 const char *GetTitle(void) const;
1352 unsigned short GetEventID(void) const;
1353 long GetDuration(void) const;
1354 time_t GetTime(void) const;
1355 tChannelID GetChannelID(void) const;
1356 int GetChannelNumber(void) const { return nChannelNumber; }
1357 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1358 void Dump(FILE *f, const char *Prefix = "") const;
1364 void MVPClient::test(int channelNumber)
1366 FILE* f = fopen("/tmp/test.txt", "w");
1368 cMutexLock MutexLock;
1369 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1373 fprintf(f, "Schedules = NULL\n");
1378 fprintf(f, "Schedules dump:\n");
1379 // Schedules->Dump(f);
1381 const cSchedule *Schedule;
1382 cChannel *thisChannel;
1383 const cEventInfo *event;
1385 thisChannel = channelFromNumber(channelNumber);
1388 fprintf(f, "thisChannel = NULL\n");
1393 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1394 // Schedule = Schedules->GetSchedule();
1395 // Schedule = Schedules->First();
1398 fprintf(f, "First Schedule = NULL\n");
1403 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1405 // For some channels VDR seems to pick a random point in time to
1406 // start dishing out events, but they are in order
1407 // at some point in the list the time snaps to the current event
1412 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1414 event = Schedule->GetEventNumber(eventNumber);
1415 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1416 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1417 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1421 fprintf(f, "\nEND\n");
1435 Schedules = the collection of all the Schedule objects
1436 Schedule = One schedule, contants all the events for a channel
1437 Event = One programme
1446 Subtitle (used for "Programmes resume at ...")
1449 IsPresent ? easy to work out tho. Oh it doesn't always work
1454 void MVPClient::test2()
1456 log->log("-", Log::DEBUG, "Timers List");
1458 for (int i = 0; i < Timers.Count(); i++)
1460 cTimer *timer = Timers.Get(i);
1461 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1462 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1463 #if VDRVERSNUM < 10300
1464 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());
1466 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());
1468 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1469 log->log("-", Log::DEBUG, "");
1472 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1473 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1474 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1479 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1480 recording is a bool, 0 for not currently recording, 1 for currently recording
1481 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
1485 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1487 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1488 int count = 4; // leave space for the packet length
1490 const char* fileName;
1492 int numTimers = Timers.Count();
1494 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1496 for (int i = 0; i < numTimers; i++)
1498 if (count > 49000) break;
1500 timer = Timers.Get(i);
1502 #if VDRVERSNUM < 10300
1503 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1505 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1507 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1508 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1509 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1510 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1511 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1512 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1513 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1515 fileName = timer->File();
1516 strcpy((char*)&sendBuffer[count], fileName);
1517 count += strlen(fileName) + 1;
1520 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1522 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1524 tcp.sendPacket(sendBuffer, count);
1525 delete[] sendBuffer;
1526 log->log("Client", Log::DEBUG, "Written timers list");
1531 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1533 char* timerString = new char[strlen((char*)buffer) + 1];
1534 strcpy(timerString, (char*)buffer);
1536 #if VDRVERSNUM < 10300
1538 // If this is VDR 1.2 the date part of the timer string must be reduced
1539 // to just DD rather than YYYY-MM-DD
1541 int s = 0; // source
1542 int d = 0; // destination
1544 while(c != 2) // copy up to date section, including the second ':'
1546 timerString[d] = buffer[s];
1547 if (buffer[s] == ':') c++;
1551 // now it has copied up to the date section
1553 while(c != 2) // waste YYYY-MM-
1555 if (buffer[s] == '-') c++;
1558 // now source is at the DD
1559 memcpy(&timerString[d], &buffer[s], length - s);
1561 timerString[d] = '\0';
1563 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1564 log->log("Client", Log::DEBUG, "%s", timerString);
1568 cTimer *timer = new cTimer;
1569 if (timer->Parse((char*)timerString))
1571 cTimer *t = Timers.GetTimer(timer);
1575 #if VDRVERSNUM < 10300
1578 Timers.SetModified();
1596 int MVPClient::processGetRecInfo(UCHAR* data, int length)
1598 // data is a pointer to the fileName string
1600 cRecordings Recordings;
1601 Recordings.Load(); // probably have to do this
1603 cRecording *recording = Recordings.GetByName((char*)data);
1605 time_t timerStart = 0;
1606 time_t timerStop = 0;
1607 char* summary = NULL;
1608 ULONG resumePoint = 0;
1612 log->log("Client", Log::ERR, "GetRecInfo found no recording");
1617 ULONG sendBufferSize = 10000;
1618 UCHAR* sendBuffer = (UCHAR*)malloc(sendBufferSize);
1619 ULONG pos = 4; // leave first 4 bytes for size field
1623 4 bytes: start time for timer
1624 4 bytes: end time for timer
1625 4 bytes: resume point
1627 4 bytes: num components
1637 // Get current timer
1639 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
1642 timerStart = rc->Timer()->StartTime();
1643 timerStop = rc->Timer()->StopTime();
1644 log->log("Client", Log::DEBUG, "GRI: RC: %lu %lu", timerStart, timerStop);
1647 *(time_t*)&sendBuffer[pos] = htonl(timerStart); pos += 4;
1648 *(time_t*)&sendBuffer[pos] = htonl(timerStop); pos += 4;
1652 char* value = config.getValueString("ResumeData", (char*)data);
1655 resumePoint = strtoul(value, NULL, 10);
1658 log->log("Client", Log::DEBUG, "GRI: RP: %lu", resumePoint);
1660 *(ULONG*)&sendBuffer[pos] = htonl(resumePoint); pos += 4;
1665 #if VDRVERSNUM < 10300
1666 summary = (char*)recording->Summary();
1668 const cRecordingInfo *Info = recording->Info();
1669 summary = (char*)Info->ShortText();
1670 if (isempty(summary)) summary = (char*)Info->Description();
1672 log->log("Client", Log::DEBUG, "GRI: S: %s", summary);
1675 // memory insanity...
1676 if ((sendBufferSize - pos) < (strlen(summary) + 500)) // random
1678 UCHAR* newBuffer = (UCHAR*)realloc(sendBuffer, sendBufferSize + strlen(summary) + 10000);
1681 sendBuffer = newBuffer;
1682 sendBufferSize += strlen(summary) + 10000;
1692 strcpy((char*)&sendBuffer[pos], summary);
1693 pos += strlen(summary) + 1;
1697 strcpy((char*)&sendBuffer[pos], "");
1704 #if VDRVERSNUM < 10300
1706 // Send 0 for numchannels - this signals the client this info is not available
1707 *(ULONG*)&sendBuffer[pos] = 0; pos += 4;
1710 const cComponents* components = Info->Components();
1712 log->log("Client", Log::DEBUG, "GRI: D1: %p", components);
1716 *(ULONG*)&sendBuffer[pos] = htonl(0); pos += 4;
1720 *(ULONG*)&sendBuffer[pos] = htonl(components->NumComponents()); pos += 4;
1722 tComponent* component;
1723 for (int i = 0; i < components->NumComponents(); i++)
1725 component = components->Component(i);
1727 // memory insanity...
1728 ULONG extraNeeded = 2 + (component->language ? strlen(component->language) : 0)
1729 + (component->description ? strlen(component->description) : 0) + 2;
1731 if ((sendBufferSize - pos) < extraNeeded)
1733 UCHAR* newBuffer = (UCHAR*)realloc(sendBuffer, sendBufferSize + extraNeeded + 10000);
1736 sendBuffer = newBuffer;
1737 sendBufferSize += extraNeeded + 10000;
1747 log->log("Client", Log::DEBUG, "GRI: C: %i %u %u %s %s", i, component->stream, component->type, component->language, component->description);
1748 sendBuffer[pos] = component->stream; pos += 1;
1749 sendBuffer[pos] = component->type; pos += 1;
1750 if (component->language)
1752 strcpy((char*)&sendBuffer[pos], component->language);
1753 pos += strlen(component->language) + 1;
1757 strcpy((char*)&sendBuffer[pos], "");
1760 if (component->description)
1762 strcpy((char*)&sendBuffer[pos], component->description);
1763 pos += strlen(component->description) + 1;
1767 strcpy((char*)&sendBuffer[pos], "");
1778 *(ULONG*)&sendBuffer[0] = htonl(pos - 4); // -4 : take off the size field
1780 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1782 tcp.sendPacket(sendBuffer, pos);
1783 delete[] sendBuffer;
1784 log->log("Client", Log::DEBUG, "Written getrecinfo");
1794 int MVPClient::processReScanRecording(UCHAR* data, int length)
1798 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
1804 UCHAR sendBuffer[16];
1805 *(ULONG*)&sendBuffer[0] = htonl(12);
1806 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
1807 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
1809 tcp.sendPacket(sendBuffer, 16);
1810 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
1814 // FIXME without client calling rescan, getblock wont work even tho more data is avail
1817 int MVPClient::processGetMarks(UCHAR* data, int length)
1819 // data is a pointer to the fileName string
1821 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1822 int count = 4; // leave space for the packet length
1826 cRecordings Recordings;
1827 Recordings.Load(); // probably have to do this
1829 cRecording *recording = Recordings.GetByName((char*)data);
1831 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
1835 Marks.Load(recording->FileName());
1838 for (const cMark *m = Marks.First(); m; m = Marks.Next(m))
1840 log->log("Client", Log::DEBUG, "found Mark %i", m->position);
1842 if (count > 49000) break;
1843 *(ULONG*)&sendBuffer[count] = htonl(m->position);
1849 log->log("Client", Log::DEBUG, "no marks found, sending 0-mark");
1850 *(ULONG*)&sendBuffer[count] = htonl(0);
1855 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1857 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1859 tcp.sendPacket(sendBuffer, count);
1860 delete[] sendBuffer;
1861 log->log("Client", Log::DEBUG, "Written Marks list");