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.setValueLongLong("ResumeData", (char*)rp->getCurrentRecording()->FileName(), rp->getLastPosition());
120 void MVPClient::sendULONG(ULONG ul)
123 *(ULONG*)&sendBuffer[0] = htonl(4);
124 *(ULONG*)&sendBuffer[4] = htonl(ul);
126 tcp.sendPacket(sendBuffer, 8);
127 log->log("Client", Log::DEBUG, "written ULONG %lu", ul);
130 void MVPClientStartThread(void* arg)
132 MVPClient* m = (MVPClient*)arg;
134 // Nothing external to this class has a reference to it
135 // This is the end of the thread.. so delete m
142 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
143 log->log("Client", Log::DEBUG, "MVPClient run success");
147 void MVPClient::run2()
152 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
153 pthread_detach(runThread); // Detach
155 tcp.disableReadTimeout();
157 tcp.setSoKeepTime(3);
158 tcp.setNonBlocking();
168 log->log("Client", Log::DEBUG, "Waiting");
169 buffer = (UCHAR*)tcp.receivePacket();
170 log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength());
173 log->log("Client", Log::DEBUG, "Detected connection closed");
177 packetLength = tcp.getDataLength() - 4;
178 opcode = ntohl(*(ULONG*)buffer);
181 if (!loggedIn && (opcode != 1))
187 log->log("Client", Log::DEBUG, "SwitchOp");
191 result = processLogin(data, packetLength);
194 result = processGetRecordingsList(data, packetLength);
197 result = processDeleteRecording(data, packetLength);
200 result = processGetSummary(data, packetLength);
203 result = processGetChannelsList(data, packetLength);
206 result = processStartStreamingChannel(data, packetLength);
209 result = processGetBlock(data, packetLength);
212 result = processStopStreaming(data, packetLength);
215 result = processStartStreamingRecording(data, packetLength);
218 result = processGetChannelSchedule(data, packetLength);
221 result = processConfigSave(data, packetLength);
224 result = processConfigLoad(data, packetLength);
227 result = processReScanRecording(data, packetLength);
230 result = processGetTimers(data, packetLength);
233 result = processSetTimer(data, packetLength);
236 result = processPositionFromFrameNumber(data, packetLength);
239 result = processFrameNumberFromPosition(data, packetLength);
242 result = processMoveRecording(data, packetLength);
251 int MVPClient::processLogin(UCHAR* buffer, int length)
253 if (length != 6) return 0;
257 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
260 log->log("Client", Log::DEBUG, "No config dir!");
264 char configFileName[PATH_MAX];
265 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]);
266 config.init(configFileName);
268 // Send the login reply
270 time_t timeNow = time(NULL);
271 struct tm* timeStruct = localtime(&timeNow);
272 int timeOffset = timeStruct->tm_gmtoff;
274 UCHAR sendBuffer[12];
275 *(ULONG*)&sendBuffer[0] = htonl(8);
276 *(ULONG*)&sendBuffer[4] = htonl(timeNow);
277 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
279 tcp.sendPacket(sendBuffer, 12);
280 log->log("Client", Log::DEBUG, "written login reply");
286 int MVPClient::processGetRecordingsList(UCHAR* data, int length)
288 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
289 int count = 4; // leave space for the packet length
294 int Percent = VideoDiskSpace(&FreeMB);
295 int Total = (FreeMB / (100 - Percent)) * 100;
297 *(ULONG*)&sendBuffer[count] = htonl(Total);
298 count += sizeof(ULONG);
299 *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
300 count += sizeof(ULONG);
301 *(ULONG*)&sendBuffer[count] = htonl(Percent);
302 count += sizeof(ULONG);
305 cRecordings Recordings;
308 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
310 if (count > 49000) break; // just how big is that hard disk?!
311 *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
314 point = (char*)recording->Name();
315 strcpy((char*)&sendBuffer[count], point);
316 count += strlen(point) + 1;
318 point = (char*)recording->FileName();
319 strcpy((char*)&sendBuffer[count], point);
320 count += strlen(point) + 1;
323 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
325 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
327 tcp.sendPacket(sendBuffer, count);
329 log->log("Client", Log::DEBUG, "Written list");
334 int MVPClient::processDeleteRecording(UCHAR* data, int length)
336 // data is a pointer to the fileName string
338 cRecordings Recordings;
339 Recordings.Load(); // probably have to do this
341 cRecording* recording = Recordings.GetByName((char*)data);
343 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
347 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
349 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
352 if (recording->Delete())
354 // Copy svdrp's way of doing this, see if it works
355 #if VDRVERSNUM > 10300
356 ::Recordings.DelByName(recording->FileName());
378 int MVPClient::processMoveRecording(UCHAR* data, int length)
380 log->log("Client", Log::DEBUG, "Process move recording");
381 char* fileName = (char*)data;
382 char* newPath = NULL;
384 for (int k = 0; k < length; k++)
388 newPath = (char*)&data[k+1];
392 if (!newPath) return 0;
394 cRecordings Recordings;
395 Recordings.Load(); // probably have to do this
397 cRecording* recording = Recordings.GetByName((char*)fileName);
399 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
403 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
407 log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name());
408 log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName());
409 log->log("Client", Log::DEBUG, "to: %s", newPath);
411 const char* t = recording->FileName();
413 char* dateDirName = NULL; int k;
414 char* titleDirName = NULL; int j;
416 // Find the datedirname
417 for(k = strlen(t) - 1; k >= 0; k--)
421 log->log("Client", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
422 dateDirName = new char[strlen(&t[k+1]) + 1];
423 strcpy(dateDirName, &t[k+1]);
428 // Find the titledirname
430 for(j = k-1; j >= 0; j--)
434 log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
435 titleDirName = new char[(k - j - 1) + 1];
436 memcpy(titleDirName, &t[j+1], k - j - 1);
437 titleDirName[k - j - 1] = '\0';
442 log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName);
443 log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName);
445 log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory);
447 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + 1 + strlen(titleDirName) + 1];
448 log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + 1 + strlen(titleDirName) + 1);
449 sprintf(newContainer, "%s%s/%s", VideoDirectory, newPath, titleDirName);
451 // FIXME Check whether this already exists before mkdiring it
453 log->log("Client", Log::DEBUG, "%s", newContainer);
457 int statret = stat(newContainer, &dstat);
458 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
460 log->log("Client", Log::DEBUG, "new dir does not exist");
461 int mkdirret = mkdir(newContainer, 0755);
464 delete[] dateDirName;
465 delete[] titleDirName;
466 delete[] newContainer;
471 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
473 delete[] dateDirName;
474 delete[] titleDirName;
475 delete[] newContainer;
480 // Ok, the directory container has been made, or it pre-existed.
482 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
483 sprintf(newDir, "%s/%s", newContainer, dateDirName);
485 log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
486 int renameret = rename(t, newDir);
489 // Success. Test for remove old dir containter
490 char* oldTitleDir = new char[k+1];
491 memcpy(oldTitleDir, t, k);
492 oldTitleDir[k] = '\0';
493 log->log("Client", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
494 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
495 delete[] oldTitleDir;
498 delete[] dateDirName;
499 delete[] titleDirName;
500 delete[] newContainer;
503 if (renameret == 0) sendULONG(1);
524 int MVPClient::processGetSummary(UCHAR* data, int length)
526 // data is a pointer to the fileName string
528 cRecordings Recordings;
529 Recordings.Load(); // probably have to do this
531 cRecording *recording = Recordings.GetByName((char*)data);
533 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
537 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
538 int count = 4; // leave space for the packet length
541 #if VDRVERSNUM < 10300
542 point = (char*)recording->Summary();
544 const cRecordingInfo *Info = recording->Info();
545 point = (char*)Info->ShortText();
546 log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point);
549 point = (char*)Info->Description();
550 log->log("Client", Log::DEBUG, "description pointer %p", point);
556 strcpy((char*)&sendBuffer[count], point);
557 count += strlen(point) + 1;
561 strcpy((char*)&sendBuffer[count], "");
565 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
567 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
569 tcp.sendPacket(sendBuffer, count);
571 log->log("Client", Log::DEBUG, "Written summary");
583 int MVPClient::processGetChannelsList(UCHAR* data, int length)
585 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
586 int count = 4; // leave space for the packet length
590 char* chanConfig = config.getValueString("General", "Channels");
592 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
594 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
596 #if VDRVERSNUM < 10300
597 if (!channel->GroupSep() && (!channel->Ca() || allChans))
599 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
602 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
604 if (channel->Vpid()) type = 1;
605 #if VDRVERSNUM < 10300
608 else if (channel->Apid(0)) type = 2;
612 if (count > 49000) break;
613 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
616 *(ULONG*)&sendBuffer[count] = htonl(type);
619 point = (char*)channel->Name();
620 strcpy((char*)&sendBuffer[count], point);
621 count += strlen(point) + 1;
625 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
627 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
629 tcp.sendPacket(sendBuffer, count);
631 log->log("Client", Log::DEBUG, "Written channels list");
636 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
638 log->log("Client", Log::DEBUG, "length = %i", length);
639 ULONG channelNumber = ntohl(*(ULONG*)data);
641 cChannel* channel = channelFromNumber(channelNumber);
648 // get the priority we should use
650 int priority = config.getValueLong("General", "Live priority", &fail);
653 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
657 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
662 if (priority < 0) priority = 0;
663 if (priority > 99) priority = 99;
665 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
666 lp = MVPReceiver::create(channel, priority);
686 int MVPClient::processStopStreaming(UCHAR* data, int length)
688 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
699 delete recordingManager;
701 recordingManager = NULL;
708 int MVPClient::processGetBlock(UCHAR* data, int length)
712 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
716 ULLONG position = ntohll(*(ULLONG*)data);
717 data += sizeof(ULLONG);
718 ULONG amount = ntohl(*(ULONG*)data);
720 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
722 UCHAR sendBuffer[amount + 4];
723 ULONG amountReceived = 0; // compiler moan.
726 log->log("Client", Log::DEBUG, "getting from live");
727 amountReceived = lp->getBlock(&sendBuffer[4], amount);
731 // vdr has possibly disconnected the receiver
732 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
739 log->log("Client", Log::DEBUG, "getting from recording");
740 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
746 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
750 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
751 tcp.sendPacket(sendBuffer, amountReceived + 4);
752 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
758 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
760 // data is a pointer to the fileName string
762 recordingManager = new cRecordings;
763 recordingManager->Load();
765 cRecording* recording = recordingManager->GetByName((char*)data);
767 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
771 rp = new RecPlayer(recording);
773 UCHAR sendBuffer[12];
774 *(ULONG*)&sendBuffer[0] = htonl(8);
775 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
777 tcp.sendPacket(sendBuffer, 12);
778 log->log("Client", Log::DEBUG, "written totalLength");
782 delete recordingManager;
783 recordingManager = NULL;
788 int MVPClient::processReScanRecording(UCHAR* data, int length)
794 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
799 retval = rp->getTotalLength();
802 UCHAR sendBuffer[12];
803 *(ULONG*)&sendBuffer[0] = htonl(8);
804 *(ULLONG*)&sendBuffer[4] = htonll(retval);
806 tcp.sendPacket(sendBuffer, 12);
807 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
811 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
815 ULONG frameNumber = ntohl(*(ULONG*)data);
820 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
824 retval = rp->positionFromFrameNumber(frameNumber);
827 UCHAR sendBuffer[12];
828 *(ULONG*)&sendBuffer[0] = htonl(8);
829 *(ULLONG*)&sendBuffer[4] = htonll(retval);
831 tcp.sendPacket(sendBuffer, 12);
832 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
836 int MVPClient::processFrameNumberFromPosition(UCHAR* data, int length)
840 ULLONG position = ntohll(*(ULLONG*)data);
845 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
849 retval = rp->frameNumberFromPosition(position);
853 *(ULONG*)&sendBuffer[0] = htonl(4);
854 *(ULONG*)&sendBuffer[4] = htonl(retval);
856 tcp.sendPacket(sendBuffer, 8);
857 log->log("Client", Log::DEBUG, "Wrote frameNumFromPos reply to client");
861 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
863 ULONG channelNumber = ntohl(*(ULONG*)data);
865 ULONG startTime = ntohl(*(ULONG*)data);
867 ULONG duration = ntohl(*(ULONG*)data);
869 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
871 cChannel* channel = channelFromNumber(channelNumber);
875 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
879 log->log("Client", Log::DEBUG, "Got channel");
881 #if VDRVERSNUM < 10300
882 cMutexLock MutexLock;
883 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
885 cSchedulesLock MutexLock;
886 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
891 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
895 log->log("Client", Log::DEBUG, "Got schedule!s! object");
897 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
901 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
905 log->log("Client", Log::DEBUG, "Got schedule object");
907 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
908 ULONG sendBufferLength = 100000;
909 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
913 // assign all the event info to temp vars then we know exactly what size they are
916 ULONG thisEventDuration;
917 const char* thisEventTitle;
918 const char* thisEventSubTitle;
919 const char* thisEventDescription;
921 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
922 ULONG thisEventLength;
924 #if VDRVERSNUM < 10300
926 const cEventInfo *event;
927 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
929 event = Schedule->GetEventNumber(eventNumber);
931 thisEventID = event->GetEventID();
932 thisEventTime = event->GetTime();
933 thisEventDuration = event->GetDuration();
934 thisEventTitle = event->GetTitle();
935 thisEventSubTitle = event->GetSubtitle();
936 thisEventDescription = event->GetExtendedDescription();
940 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
942 thisEventID = event->EventID();
943 thisEventTime = event->StartTime();
944 thisEventDuration = event->Duration();
945 thisEventTitle = event->Title();
946 thisEventSubTitle = NULL;
947 thisEventDescription = event->Description();
951 log->log("Client", Log::DEBUG, "Got an event object %p", event);
954 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
957 if ((thisEventTime + thisEventDuration) <= startTime) continue;
960 if (thisEventTime >= (startTime + duration)) continue;
962 if (!thisEventTitle) thisEventTitle = empty;
963 if (!thisEventSubTitle) thisEventSubTitle = empty;
964 if (!thisEventDescription) thisEventDescription = empty;
966 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
968 log->log("Client", Log::DEBUG, "Done s1");
970 // now extend the buffer if necessary
971 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
973 log->log("Client", Log::DEBUG, "Extending buffer");
974 sendBufferLength += 100000;
975 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
979 UCHAR sendBuffer2[8];
980 *(ULONG*)&sendBuffer2[0] = htonl(4);
981 *(ULONG*)&sendBuffer2[4] = htonl(0);
982 tcp.sendPacket(sendBuffer2, 8);
983 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
989 log->log("Client", Log::DEBUG, "Done s2");
991 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
992 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
993 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
995 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
996 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
997 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
999 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
1002 log->log("Client", Log::DEBUG, "Got all event data");
1004 if (sendBufferUsed == sizeof(ULONG))
1008 log->log("Client", Log::DEBUG, "Written 0 because no data");
1012 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
1013 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
1014 tcp.sendPacket(sendBuffer, sendBufferUsed);
1015 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
1023 int MVPClient::processConfigSave(UCHAR* buffer, int length)
1025 char* section = (char*)buffer;
1029 for (int k = 0; k < length; k++)
1031 if (buffer[k] == '\0')
1035 key = (char*)&buffer[k+1];
1039 value = (char*)&buffer[k+1];
1045 // if the last string (value) doesnt have null terminator, give up
1046 if (buffer[length - 1] != '\0') return 0;
1048 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
1049 if (config.setValueString(section, key, value))
1061 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
1063 char* section = (char*)buffer;
1066 for (int k = 0; k < length; k++)
1068 if (buffer[k] == '\0')
1070 key = (char*)&buffer[k+1];
1075 char* value = config.getValueString(section, key);
1079 UCHAR sendBuffer[4 + strlen(value) + 1];
1080 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
1081 strcpy((char*)&sendBuffer[4], value);
1082 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
1084 log->log("Client", Log::DEBUG, "Written config load packet");
1089 UCHAR sendBuffer[8];
1090 *(ULONG*)&sendBuffer[0] = htonl(4);
1091 *(ULONG*)&sendBuffer[4] = htonl(0);
1092 tcp.sendPacket(sendBuffer, 8);
1094 log->log("Client", Log::DEBUG, "Written config load failed packet");
1100 void MVPClient::cleanConfig()
1102 log->log("Client", Log::DEBUG, "Clean config");
1104 cRecordings Recordings;
1109 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
1110 char* position = resumes;
1111 for(int k = 0; k < numReturns; k++)
1113 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
1115 cRecording* recording = Recordings.GetByName(position);
1118 // doesn't exist anymore
1119 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
1120 config.deleteValue("ResumeData", position);
1124 log->log("Client", Log::DEBUG, "This recording still exists");
1127 position += strlen(position) + 1;
1139 event = Schedule->GetPresentEvent();
1141 fprintf(f, "\n\nCurrent event\n\n");
1143 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1144 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1145 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1146 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1148 event = Schedule->GetFollowingEvent();
1150 fprintf(f, "\n\nFollowing event\n\n");
1152 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1153 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1154 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1155 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1161 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1162 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1163 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1164 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
1172 void MVPClient::test2()
1174 FILE* f = fopen("/tmp/s.txt", "w");
1176 #if VDRVERSNUM < 10300
1177 cMutexLock MutexLock;
1178 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1180 cSchedulesLock MutexLock;
1181 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1186 fprintf(f, "Schedules = NULL\n");
1191 fprintf(f, "Schedules dump:\n");
1195 const cSchedule *Schedule;
1196 int scheduleNumber = 0;
1199 cChannel *thisChannel;
1201 #if VDRVERSNUM < 10300
1202 const cEventInfo *event;
1203 int eventNumber = 0;
1205 const cEvent *event;
1208 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1209 // Schedule = Schedules->GetSchedule();
1210 Schedule = Schedules->First();
1213 fprintf(f, "First Schedule = NULL\n");
1220 fprintf(f, "Schedule #%i\n", scheduleNumber);
1221 fprintf(f, "-------------\n\n");
1223 #if VDRVERSNUM < 10300
1224 tchid = Schedule->GetChannelID();
1226 tchid = Schedule->ChannelID();
1229 #if VDRVERSNUM < 10300
1230 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1231 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1233 // put the count at the end.
1236 thisChannel = Channels.GetByChannelID(tchid, true);
1239 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1243 fprintf(f, "thisChannel = NULL for tchid\n");
1246 #if VDRVERSNUM < 10300
1247 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1249 event = Schedule->GetEventNumber(eventNumber);
1250 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1251 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1252 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1253 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1254 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1255 fprintf(f, "Event %u dump:\n", eventNumber);
1260 // This whole section needs rewriting to walk the list.
1261 event = Schedule->Events()->First();
1263 event = Schedule->Events()->Next(event);
1268 fprintf(f, "\nDump from object:\n");
1270 fprintf(f, "\nEND\n");
1280 fprintf(f, "End of current Schedule\n\n\n");
1282 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1296 const cEventInfo *GetPresentEvent(void) const;
1297 const cEventInfo *GetFollowingEvent(void) const;
1298 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1299 const cEventInfo *GetEventAround(time_t tTime) const;
1300 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1303 const unsigned char GetTableID(void) const;
1304 const char *GetTimeString(void) const;
1305 const char *GetEndTimeString(void) const;
1306 const char *GetDate(void) const;
1307 bool IsFollowing(void) const;
1308 bool IsPresent(void) const;
1309 const char *GetExtendedDescription(void) const;
1310 const char *GetSubtitle(void) const;
1311 const char *GetTitle(void) const;
1312 unsigned short GetEventID(void) const;
1313 long GetDuration(void) const;
1314 time_t GetTime(void) const;
1315 tChannelID GetChannelID(void) const;
1316 int GetChannelNumber(void) const { return nChannelNumber; }
1317 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1318 void Dump(FILE *f, const char *Prefix = "") const;
1324 void MVPClient::test(int channelNumber)
1326 FILE* f = fopen("/tmp/test.txt", "w");
1328 cMutexLock MutexLock;
1329 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1333 fprintf(f, "Schedules = NULL\n");
1338 fprintf(f, "Schedules dump:\n");
1339 // Schedules->Dump(f);
1341 const cSchedule *Schedule;
1342 cChannel *thisChannel;
1343 const cEventInfo *event;
1345 thisChannel = channelFromNumber(channelNumber);
1348 fprintf(f, "thisChannel = NULL\n");
1353 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1354 // Schedule = Schedules->GetSchedule();
1355 // Schedule = Schedules->First();
1358 fprintf(f, "First Schedule = NULL\n");
1363 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1365 // For some channels VDR seems to pick a random point in time to
1366 // start dishing out events, but they are in order
1367 // at some point in the list the time snaps to the current event
1372 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1374 event = Schedule->GetEventNumber(eventNumber);
1375 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1376 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1377 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1381 fprintf(f, "\nEND\n");
1395 Schedules = the collection of all the Schedule objects
1396 Schedule = One schedule, contants all the events for a channel
1397 Event = One programme
1406 Subtitle (used for "Programmes resume at ...")
1409 IsPresent ? easy to work out tho. Oh it doesn't always work
1414 void MVPClient::test2()
1416 log->log("-", Log::DEBUG, "Timers List");
1418 for (int i = 0; i < Timers.Count(); i++)
1420 cTimer *timer = Timers.Get(i);
1421 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1422 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1423 #if VDRVERSNUM < 10300
1424 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());
1426 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());
1428 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1429 log->log("-", Log::DEBUG, "");
1432 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1433 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1434 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1439 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1440 recording is a bool, 0 for not currently recording, 1 for currently recording
1441 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
1445 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1447 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1448 int count = 4; // leave space for the packet length
1450 const char* fileName;
1452 int numTimers = Timers.Count();
1454 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1456 for (int i = 0; i < numTimers; i++)
1458 if (count > 49000) break;
1460 timer = Timers.Get(i);
1462 #if VDRVERSNUM < 10300
1463 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1465 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1467 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1468 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1469 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1470 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1471 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1472 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1473 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1475 fileName = timer->File();
1476 strcpy((char*)&sendBuffer[count], fileName);
1477 count += strlen(fileName) + 1;
1480 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1482 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1484 tcp.sendPacket(sendBuffer, count);
1485 delete[] sendBuffer;
1486 log->log("Client", Log::DEBUG, "Written timers list");
1491 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1493 char* timerString = new char[strlen((char*)buffer) + 1];
1494 strcpy(timerString, (char*)buffer);
1496 #if VDRVERSNUM < 10300
1498 // If this is VDR 1.2 the date part of the timer string must be reduced
1499 // to just DD rather than YYYY-MM-DD
1501 int s = 0; // source
1502 int d = 0; // destination
1504 while(c != 2) // copy up to date section, including the second ':'
1506 timerString[d] = buffer[s];
1507 if (buffer[s] == ':') c++;
1511 // now it has copied up to the date section
1513 while(c != 2) // waste YYYY-MM-
1515 if (buffer[s] == '-') c++;
1518 // now source is at the DD
1519 memcpy(&timerString[d], &buffer[s], length - s);
1521 timerString[d] = '\0';
1523 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1524 log->log("Client", Log::DEBUG, "%s", timerString);
1528 cTimer *timer = new cTimer;
1529 if (timer->Parse((char*)timerString))
1531 cTimer *t = Timers.GetTimer(timer);
1535 #if VDRVERSNUM < 10300
1538 Timers.SetModified();