2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mvpclient.h"
23 // This is here else it causes compile errors with something in libdvbmpeg
26 MVPClient::MVPClient(char* tconfigDirExtra, int tsocket)
31 recordingManager = NULL;
32 log = Log::getInstance();
34 configDirExtra = tconfigDirExtra;
37 MVPClient::~MVPClient()
39 log->log("Client", Log::DEBUG, "MVP client destructor");
50 delete recordingManager;
52 recordingManager = NULL;
55 if (loggedIn) cleanConfig();
58 ULLONG MVPClient::ntohll(ULLONG a)
63 ULLONG MVPClient::htonll(ULLONG a)
65 #if BYTE_ORDER == BIG_ENDIAN
70 b = ((a << 56) & 0xFF00000000000000ULL)
71 | ((a << 40) & 0x00FF000000000000ULL)
72 | ((a << 24) & 0x0000FF0000000000ULL)
73 | ((a << 8) & 0x000000FF00000000ULL)
74 | ((a >> 8) & 0x00000000FF000000ULL)
75 | ((a >> 24) & 0x0000000000FF0000ULL)
76 | ((a >> 40) & 0x000000000000FF00ULL)
77 | ((a >> 56) & 0x00000000000000FFULL) ;
83 cChannel* MVPClient::channelFromNumber(ULONG channelNumber)
85 cChannel* channel = NULL;
87 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
89 if (!channel->GroupSep())
91 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
93 if (channel->Number() == (int)channelNumber)
95 int vpid = channel->Vpid();
96 #if VDRVERSNUM < 10300
97 int apid1 = channel->Apid1();
99 int apid1 = channel->Apid(0);
101 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
109 log->log("Client", Log::DEBUG, "Channel not found");
115 void MVPClient::writeResumeData()
117 config.setValueLong("ResumeData",
118 (char*)rp->getCurrentRecording()->FileName(),
119 rp->frameNumberFromPosition(rp->getLastPosition()) );
122 void MVPClient::sendULONG(ULONG ul)
125 *(ULONG*)&sendBuffer[0] = htonl(4);
126 *(ULONG*)&sendBuffer[4] = htonl(ul);
128 tcp.sendPacket(sendBuffer, 8);
129 log->log("Client", Log::DEBUG, "written ULONG %lu", ul);
132 void MVPClientStartThread(void* arg)
134 MVPClient* m = (MVPClient*)arg;
136 // Nothing external to this class has a reference to it
137 // This is the end of the thread.. so delete m
144 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
145 log->log("Client", Log::DEBUG, "MVPClient run success");
149 void MVPClient::run2()
154 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
155 pthread_detach(runThread); // Detach
157 tcp.disableReadTimeout();
159 tcp.setSoKeepTime(3);
160 tcp.setNonBlocking();
170 log->log("Client", Log::DEBUG, "Waiting");
171 buffer = (UCHAR*)tcp.receivePacket();
172 log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength());
175 log->log("Client", Log::DEBUG, "Detected connection closed");
179 packetLength = tcp.getDataLength() - 4;
180 opcode = ntohl(*(ULONG*)buffer);
183 if (!loggedIn && (opcode != 1))
189 log->log("Client", Log::DEBUG, "SwitchOp");
193 result = processLogin(data, packetLength);
196 result = processGetRecordingsList(data, packetLength);
199 result = processDeleteRecording(data, packetLength);
202 result = processGetSummary(data, packetLength);
205 result = processGetChannelsList(data, packetLength);
208 result = processStartStreamingChannel(data, packetLength);
211 result = processGetBlock(data, packetLength);
214 result = processStopStreaming(data, packetLength);
217 result = processStartStreamingRecording(data, packetLength);
220 result = processGetChannelSchedule(data, packetLength);
223 result = processConfigSave(data, packetLength);
226 result = processConfigLoad(data, packetLength);
229 result = processReScanRecording(data, packetLength);
232 result = processGetTimers(data, packetLength);
235 result = processSetTimer(data, packetLength);
238 result = processPositionFromFrameNumber(data, packetLength);
241 result = processFrameNumberFromPosition(data, packetLength);
244 result = processMoveRecording(data, packetLength);
253 int MVPClient::processLogin(UCHAR* buffer, int length)
255 if (length != 6) return 0;
259 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
262 log->log("Client", Log::DEBUG, "No config dir!");
266 char configFileName[PATH_MAX];
267 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]);
268 config.init(configFileName);
270 // Send the login reply
272 time_t timeNow = time(NULL);
273 struct tm* timeStruct = localtime(&timeNow);
274 int timeOffset = timeStruct->tm_gmtoff;
276 UCHAR sendBuffer[12];
277 *(ULONG*)&sendBuffer[0] = htonl(8);
278 *(ULONG*)&sendBuffer[4] = htonl(timeNow);
279 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
281 tcp.sendPacket(sendBuffer, 12);
282 log->log("Client", Log::DEBUG, "written login reply");
288 int MVPClient::processGetRecordingsList(UCHAR* data, int length)
290 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
291 int count = 4; // leave space for the packet length
296 int Percent = VideoDiskSpace(&FreeMB);
297 int Total = (FreeMB / (100 - Percent)) * 100;
299 *(ULONG*)&sendBuffer[count] = htonl(Total);
300 count += sizeof(ULONG);
301 *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
302 count += sizeof(ULONG);
303 *(ULONG*)&sendBuffer[count] = htonl(Percent);
304 count += sizeof(ULONG);
307 cRecordings Recordings;
310 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
312 if (count > 49000) break; // just how big is that hard disk?!
313 *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
316 point = (char*)recording->Name();
317 strcpy((char*)&sendBuffer[count], point);
318 count += strlen(point) + 1;
320 point = (char*)recording->FileName();
321 strcpy((char*)&sendBuffer[count], point);
322 count += strlen(point) + 1;
325 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
327 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
329 tcp.sendPacket(sendBuffer, count);
331 log->log("Client", Log::DEBUG, "Written list");
336 int MVPClient::processDeleteRecording(UCHAR* data, int length)
338 // data is a pointer to the fileName string
340 cRecordings Recordings;
341 Recordings.Load(); // probably have to do this
343 cRecording* recording = Recordings.GetByName((char*)data);
345 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
349 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
351 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
354 if (recording->Delete())
356 // Copy svdrp's way of doing this, see if it works
357 #if VDRVERSNUM > 10300
358 ::Recordings.DelByName(recording->FileName());
380 int MVPClient::processMoveRecording(UCHAR* data, int length)
382 log->log("Client", Log::DEBUG, "Process move recording");
383 char* fileName = (char*)data;
384 char* newPath = NULL;
386 for (int k = 0; k < length; k++)
390 newPath = (char*)&data[k+1];
394 if (!newPath) return 0;
396 cRecordings Recordings;
397 Recordings.Load(); // probably have to do this
399 cRecording* recording = Recordings.GetByName((char*)fileName);
401 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
405 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
408 log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name());
409 log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName());
410 log->log("Client", Log::DEBUG, "to: %s", newPath);
412 const char* t = recording->FileName();
414 char* dateDirName = NULL; int k;
415 char* titleDirName = NULL; int j;
417 // Find the datedirname
418 for(k = strlen(t) - 1; k >= 0; k--)
422 log->log("Client", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
423 dateDirName = new char[strlen(&t[k+1]) + 1];
424 strcpy(dateDirName, &t[k+1]);
429 // Find the titledirname
431 for(j = k-1; j >= 0; j--)
435 log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
436 titleDirName = new char[(k - j - 1) + 1];
437 memcpy(titleDirName, &t[j+1], k - j - 1);
438 titleDirName[k - j - 1] = '\0';
443 log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName);
444 log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName);
446 log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory);
448 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1];
449 log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1);
450 sprintf(newContainer, "%s%s%s", VideoDirectory, newPath, titleDirName);
452 // FIXME Check whether this already exists before mkdiring it
454 log->log("Client", Log::DEBUG, "%s", newContainer);
458 int statret = stat(newContainer, &dstat);
459 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
461 log->log("Client", Log::DEBUG, "new dir does not exist");
462 int mkdirret = mkdir(newContainer, 0755);
465 delete[] dateDirName;
466 delete[] titleDirName;
467 delete[] newContainer;
472 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
474 delete[] dateDirName;
475 delete[] titleDirName;
476 delete[] newContainer;
481 // Ok, the directory container has been made, or it pre-existed.
483 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
484 sprintf(newDir, "%s/%s", newContainer, dateDirName);
486 log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
487 int renameret = rename(t, newDir);
490 // Success. Test for remove old dir containter
491 char* oldTitleDir = new char[k+1];
492 memcpy(oldTitleDir, t, k);
493 oldTitleDir[k] = '\0';
494 log->log("Client", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
495 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
496 delete[] oldTitleDir;
502 ::Recordings.Update();
504 // Success. Send a different packet from just a ulong
505 int totalLength = 4 + 4 + strlen(newDir) + 1;
506 UCHAR* sendBuffer = new UCHAR[totalLength];
507 *(ULONG*)&sendBuffer[0] = htonl(totalLength - 4);
508 *(ULONG*)&sendBuffer[4] = htonl(1); // success
509 strcpy((char*)&sendBuffer[8], newDir);
510 tcp.sendPacket(sendBuffer, totalLength);
518 delete[] dateDirName;
519 delete[] titleDirName;
520 delete[] newContainer;
536 int MVPClient::processGetSummary(UCHAR* data, int length)
538 // data is a pointer to the fileName string
540 cRecordings Recordings;
541 Recordings.Load(); // probably have to do this
543 cRecording *recording = Recordings.GetByName((char*)data);
545 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
549 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
550 int count = 4; // leave space for the packet length
553 #if VDRVERSNUM < 10300
554 point = (char*)recording->Summary();
556 const cRecordingInfo *Info = recording->Info();
557 point = (char*)Info->ShortText();
558 log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point);
561 point = (char*)Info->Description();
562 log->log("Client", Log::DEBUG, "description pointer %p", point);
568 strcpy((char*)&sendBuffer[count], point);
569 count += strlen(point) + 1;
573 strcpy((char*)&sendBuffer[count], "");
577 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
579 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
581 tcp.sendPacket(sendBuffer, count);
583 log->log("Client", Log::DEBUG, "Written summary");
595 int MVPClient::processGetChannelsList(UCHAR* data, int length)
597 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
598 int count = 4; // leave space for the packet length
602 char* chanConfig = config.getValueString("General", "Channels");
604 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
606 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
608 #if VDRVERSNUM < 10300
609 if (!channel->GroupSep() && (!channel->Ca() || allChans))
611 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
614 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
616 if (channel->Vpid()) type = 1;
617 #if VDRVERSNUM < 10300
620 else if (channel->Apid(0)) type = 2;
624 if (count > 49000) break;
625 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
628 *(ULONG*)&sendBuffer[count] = htonl(type);
631 point = (char*)channel->Name();
632 strcpy((char*)&sendBuffer[count], point);
633 count += strlen(point) + 1;
637 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
639 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
641 tcp.sendPacket(sendBuffer, count);
643 log->log("Client", Log::DEBUG, "Written channels list");
648 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
650 log->log("Client", Log::DEBUG, "length = %i", length);
651 ULONG channelNumber = ntohl(*(ULONG*)data);
653 cChannel* channel = channelFromNumber(channelNumber);
660 // get the priority we should use
662 int priority = config.getValueLong("General", "Live priority", &fail);
665 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
669 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
674 if (priority < 0) priority = 0;
675 if (priority > 99) priority = 99;
677 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
678 lp = MVPReceiver::create(channel, priority);
698 int MVPClient::processStopStreaming(UCHAR* data, int length)
700 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
711 delete recordingManager;
713 recordingManager = NULL;
720 int MVPClient::processGetBlock(UCHAR* data, int length)
724 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
728 ULLONG position = ntohll(*(ULLONG*)data);
729 data += sizeof(ULLONG);
730 ULONG amount = ntohl(*(ULONG*)data);
732 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
734 UCHAR sendBuffer[amount + 4];
735 ULONG amountReceived = 0; // compiler moan.
738 log->log("Client", Log::DEBUG, "getting from live");
739 amountReceived = lp->getBlock(&sendBuffer[4], amount);
743 // vdr has possibly disconnected the receiver
744 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
751 log->log("Client", Log::DEBUG, "getting from recording");
752 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
758 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
762 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
763 tcp.sendPacket(sendBuffer, amountReceived + 4);
764 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
770 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
772 // data is a pointer to the fileName string
774 recordingManager = new cRecordings;
775 recordingManager->Load();
777 cRecording* recording = recordingManager->GetByName((char*)data);
779 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
783 rp = new RecPlayer(recording);
785 UCHAR sendBuffer[16];
786 *(ULONG*)&sendBuffer[0] = htonl(12);
787 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
788 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
790 tcp.sendPacket(sendBuffer, 16);
791 log->log("Client", Log::DEBUG, "written totalLength");
795 delete recordingManager;
796 recordingManager = NULL;
801 int MVPClient::processReScanRecording(UCHAR* data, int length)
805 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
811 UCHAR sendBuffer[16];
812 *(ULONG*)&sendBuffer[0] = htonl(12);
813 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
814 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
816 tcp.sendPacket(sendBuffer, 16);
817 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
821 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
825 ULONG frameNumber = ntohl(*(ULONG*)data);
830 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
834 retval = rp->positionFromFrameNumber(frameNumber);
837 UCHAR sendBuffer[12];
838 *(ULONG*)&sendBuffer[0] = htonl(8);
839 *(ULLONG*)&sendBuffer[4] = htonll(retval);
841 tcp.sendPacket(sendBuffer, 12);
842 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
846 int MVPClient::processFrameNumberFromPosition(UCHAR* data, int length)
850 ULLONG position = ntohll(*(ULLONG*)data);
855 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
859 retval = rp->frameNumberFromPosition(position);
863 *(ULONG*)&sendBuffer[0] = htonl(4);
864 *(ULONG*)&sendBuffer[4] = htonl(retval);
866 tcp.sendPacket(sendBuffer, 8);
867 log->log("Client", Log::DEBUG, "Wrote frameNumFromPos reply to client");
871 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
873 ULONG channelNumber = ntohl(*(ULONG*)data);
875 ULONG startTime = ntohl(*(ULONG*)data);
877 ULONG duration = ntohl(*(ULONG*)data);
879 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
881 cChannel* channel = channelFromNumber(channelNumber);
885 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
889 log->log("Client", Log::DEBUG, "Got channel");
891 #if VDRVERSNUM < 10300
892 cMutexLock MutexLock;
893 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
895 cSchedulesLock MutexLock;
896 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
901 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
905 log->log("Client", Log::DEBUG, "Got schedule!s! object");
907 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
911 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
915 log->log("Client", Log::DEBUG, "Got schedule object");
917 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
918 ULONG sendBufferLength = 100000;
919 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
923 // assign all the event info to temp vars then we know exactly what size they are
926 ULONG thisEventDuration;
927 const char* thisEventTitle;
928 const char* thisEventSubTitle;
929 const char* thisEventDescription;
931 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
932 ULONG thisEventLength;
934 #if VDRVERSNUM < 10300
936 const cEventInfo *event;
937 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
939 event = Schedule->GetEventNumber(eventNumber);
941 thisEventID = event->GetEventID();
942 thisEventTime = event->GetTime();
943 thisEventDuration = event->GetDuration();
944 thisEventTitle = event->GetTitle();
945 thisEventSubTitle = event->GetSubtitle();
946 thisEventDescription = event->GetExtendedDescription();
950 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
952 thisEventID = event->EventID();
953 thisEventTime = event->StartTime();
954 thisEventDuration = event->Duration();
955 thisEventTitle = event->Title();
956 thisEventSubTitle = NULL;
957 thisEventDescription = event->Description();
961 log->log("Client", Log::DEBUG, "Got an event object %p", event);
964 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
967 if ((thisEventTime + thisEventDuration) <= startTime) continue;
970 if (thisEventTime >= (startTime + duration)) continue;
972 if (!thisEventTitle) thisEventTitle = empty;
973 if (!thisEventSubTitle) thisEventSubTitle = empty;
974 if (!thisEventDescription) thisEventDescription = empty;
976 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
978 log->log("Client", Log::DEBUG, "Done s1");
980 // now extend the buffer if necessary
981 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
983 log->log("Client", Log::DEBUG, "Extending buffer");
984 sendBufferLength += 100000;
985 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
989 UCHAR sendBuffer2[8];
990 *(ULONG*)&sendBuffer2[0] = htonl(4);
991 *(ULONG*)&sendBuffer2[4] = htonl(0);
992 tcp.sendPacket(sendBuffer2, 8);
993 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
999 log->log("Client", Log::DEBUG, "Done s2");
1001 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
1002 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
1003 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
1005 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
1006 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
1007 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
1009 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
1012 log->log("Client", Log::DEBUG, "Got all event data");
1014 if (sendBufferUsed == sizeof(ULONG))
1018 log->log("Client", Log::DEBUG, "Written 0 because no data");
1022 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
1023 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
1024 tcp.sendPacket(sendBuffer, sendBufferUsed);
1025 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
1033 int MVPClient::processConfigSave(UCHAR* buffer, int length)
1035 char* section = (char*)buffer;
1039 for (int k = 0; k < length; k++)
1041 if (buffer[k] == '\0')
1045 key = (char*)&buffer[k+1];
1049 value = (char*)&buffer[k+1];
1055 // if the last string (value) doesnt have null terminator, give up
1056 if (buffer[length - 1] != '\0') return 0;
1058 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
1059 if (config.setValueString(section, key, value))
1071 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
1073 char* section = (char*)buffer;
1076 for (int k = 0; k < length; k++)
1078 if (buffer[k] == '\0')
1080 key = (char*)&buffer[k+1];
1085 char* value = config.getValueString(section, key);
1089 UCHAR sendBuffer[4 + strlen(value) + 1];
1090 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
1091 strcpy((char*)&sendBuffer[4], value);
1092 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
1094 log->log("Client", Log::DEBUG, "Written config load packet");
1099 UCHAR sendBuffer[8];
1100 *(ULONG*)&sendBuffer[0] = htonl(4);
1101 *(ULONG*)&sendBuffer[4] = htonl(0);
1102 tcp.sendPacket(sendBuffer, 8);
1104 log->log("Client", Log::DEBUG, "Written config load failed packet");
1110 void MVPClient::cleanConfig()
1112 log->log("Client", Log::DEBUG, "Clean config");
1114 cRecordings Recordings;
1119 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
1120 char* position = resumes;
1121 for(int k = 0; k < numReturns; k++)
1123 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
1125 cRecording* recording = Recordings.GetByName(position);
1128 // doesn't exist anymore
1129 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
1130 config.deleteValue("ResumeData", position);
1134 log->log("Client", Log::DEBUG, "This recording still exists");
1137 position += strlen(position) + 1;
1149 event = Schedule->GetPresentEvent();
1151 fprintf(f, "\n\nCurrent event\n\n");
1153 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1154 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1155 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1156 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1158 event = Schedule->GetFollowingEvent();
1160 fprintf(f, "\n\nFollowing event\n\n");
1162 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1163 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1164 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1165 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1171 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1172 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1173 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1174 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
1182 void MVPClient::test2()
1184 FILE* f = fopen("/tmp/s.txt", "w");
1186 #if VDRVERSNUM < 10300
1187 cMutexLock MutexLock;
1188 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1190 cSchedulesLock MutexLock;
1191 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1196 fprintf(f, "Schedules = NULL\n");
1201 fprintf(f, "Schedules dump:\n");
1205 const cSchedule *Schedule;
1206 int scheduleNumber = 0;
1209 cChannel *thisChannel;
1211 #if VDRVERSNUM < 10300
1212 const cEventInfo *event;
1213 int eventNumber = 0;
1215 const cEvent *event;
1218 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1219 // Schedule = Schedules->GetSchedule();
1220 Schedule = Schedules->First();
1223 fprintf(f, "First Schedule = NULL\n");
1230 fprintf(f, "Schedule #%i\n", scheduleNumber);
1231 fprintf(f, "-------------\n\n");
1233 #if VDRVERSNUM < 10300
1234 tchid = Schedule->GetChannelID();
1236 tchid = Schedule->ChannelID();
1239 #if VDRVERSNUM < 10300
1240 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1241 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1243 // put the count at the end.
1246 thisChannel = Channels.GetByChannelID(tchid, true);
1249 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1253 fprintf(f, "thisChannel = NULL for tchid\n");
1256 #if VDRVERSNUM < 10300
1257 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1259 event = Schedule->GetEventNumber(eventNumber);
1260 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1261 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1262 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1263 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1264 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1265 fprintf(f, "Event %u dump:\n", eventNumber);
1270 // This whole section needs rewriting to walk the list.
1271 event = Schedule->Events()->First();
1273 event = Schedule->Events()->Next(event);
1278 fprintf(f, "\nDump from object:\n");
1280 fprintf(f, "\nEND\n");
1290 fprintf(f, "End of current Schedule\n\n\n");
1292 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1306 const cEventInfo *GetPresentEvent(void) const;
1307 const cEventInfo *GetFollowingEvent(void) const;
1308 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1309 const cEventInfo *GetEventAround(time_t tTime) const;
1310 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1313 const unsigned char GetTableID(void) const;
1314 const char *GetTimeString(void) const;
1315 const char *GetEndTimeString(void) const;
1316 const char *GetDate(void) const;
1317 bool IsFollowing(void) const;
1318 bool IsPresent(void) const;
1319 const char *GetExtendedDescription(void) const;
1320 const char *GetSubtitle(void) const;
1321 const char *GetTitle(void) const;
1322 unsigned short GetEventID(void) const;
1323 long GetDuration(void) const;
1324 time_t GetTime(void) const;
1325 tChannelID GetChannelID(void) const;
1326 int GetChannelNumber(void) const { return nChannelNumber; }
1327 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1328 void Dump(FILE *f, const char *Prefix = "") const;
1334 void MVPClient::test(int channelNumber)
1336 FILE* f = fopen("/tmp/test.txt", "w");
1338 cMutexLock MutexLock;
1339 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1343 fprintf(f, "Schedules = NULL\n");
1348 fprintf(f, "Schedules dump:\n");
1349 // Schedules->Dump(f);
1351 const cSchedule *Schedule;
1352 cChannel *thisChannel;
1353 const cEventInfo *event;
1355 thisChannel = channelFromNumber(channelNumber);
1358 fprintf(f, "thisChannel = NULL\n");
1363 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1364 // Schedule = Schedules->GetSchedule();
1365 // Schedule = Schedules->First();
1368 fprintf(f, "First Schedule = NULL\n");
1373 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1375 // For some channels VDR seems to pick a random point in time to
1376 // start dishing out events, but they are in order
1377 // at some point in the list the time snaps to the current event
1382 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1384 event = Schedule->GetEventNumber(eventNumber);
1385 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1386 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1387 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1391 fprintf(f, "\nEND\n");
1405 Schedules = the collection of all the Schedule objects
1406 Schedule = One schedule, contants all the events for a channel
1407 Event = One programme
1416 Subtitle (used for "Programmes resume at ...")
1419 IsPresent ? easy to work out tho. Oh it doesn't always work
1424 void MVPClient::test2()
1426 log->log("-", Log::DEBUG, "Timers List");
1428 for (int i = 0; i < Timers.Count(); i++)
1430 cTimer *timer = Timers.Get(i);
1431 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1432 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1433 #if VDRVERSNUM < 10300
1434 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());
1436 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());
1438 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1439 log->log("-", Log::DEBUG, "");
1442 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1443 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1444 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1449 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1450 recording is a bool, 0 for not currently recording, 1 for currently recording
1451 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
1455 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1457 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1458 int count = 4; // leave space for the packet length
1460 const char* fileName;
1462 int numTimers = Timers.Count();
1464 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1466 for (int i = 0; i < numTimers; i++)
1468 if (count > 49000) break;
1470 timer = Timers.Get(i);
1472 #if VDRVERSNUM < 10300
1473 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1475 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1477 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1478 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1479 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1480 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1481 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1482 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1483 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1485 fileName = timer->File();
1486 strcpy((char*)&sendBuffer[count], fileName);
1487 count += strlen(fileName) + 1;
1490 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1492 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1494 tcp.sendPacket(sendBuffer, count);
1495 delete[] sendBuffer;
1496 log->log("Client", Log::DEBUG, "Written timers list");
1501 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1503 char* timerString = new char[strlen((char*)buffer) + 1];
1504 strcpy(timerString, (char*)buffer);
1506 #if VDRVERSNUM < 10300
1508 // If this is VDR 1.2 the date part of the timer string must be reduced
1509 // to just DD rather than YYYY-MM-DD
1511 int s = 0; // source
1512 int d = 0; // destination
1514 while(c != 2) // copy up to date section, including the second ':'
1516 timerString[d] = buffer[s];
1517 if (buffer[s] == ':') c++;
1521 // now it has copied up to the date section
1523 while(c != 2) // waste YYYY-MM-
1525 if (buffer[s] == '-') c++;
1528 // now source is at the DD
1529 memcpy(&timerString[d], &buffer[s], length - s);
1531 timerString[d] = '\0';
1533 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1534 log->log("Client", Log::DEBUG, "%s", timerString);
1538 cTimer *timer = new cTimer;
1539 if (timer->Parse((char*)timerString))
1541 cTimer *t = Timers.GetTimer(timer);
1545 #if VDRVERSNUM < 10300
1548 Timers.SetModified();