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 MVPClient::MVPClient(int tsocket)
28 recordingManager = NULL;
29 log = Log::getInstance();
33 MVPClient::~MVPClient()
35 log->log("Client", Log::DEBUG, "MVP client destructor");
46 delete recordingManager;
48 recordingManager = NULL;
51 if (loggedIn) cleanConfig();
54 ULLONG MVPClient::ntohll(ULLONG a)
59 ULLONG MVPClient::htonll(ULLONG a)
61 #if BYTE_ORDER == BIG_ENDIAN
66 b = ((a << 56) & 0xFF00000000000000ULL)
67 | ((a << 40) & 0x00FF000000000000ULL)
68 | ((a << 24) & 0x0000FF0000000000ULL)
69 | ((a << 8) & 0x000000FF00000000ULL)
70 | ((a >> 8) & 0x00000000FF000000ULL)
71 | ((a >> 24) & 0x0000000000FF0000ULL)
72 | ((a >> 40) & 0x000000000000FF00ULL)
73 | ((a >> 56) & 0x00000000000000FFULL) ;
79 cChannel* MVPClient::channelFromNumber(ULONG channelNumber)
81 cChannel* channel = NULL;
83 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
85 if (!channel->GroupSep())
87 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
89 if (channel->Number() == (int)channelNumber)
91 int vpid = channel->Vpid();
92 #if VDRVERSNUM < 10300
93 int apid1 = channel->Apid1();
95 int apid1 = channel->Apid(0);
97 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
105 log->log("Client", Log::DEBUG, "Channel not found");
111 void MVPClient::writeResumeData()
113 config.setValueLongLong("ResumeData", (char*)rp->getCurrentRecording()->FileName(), rp->getLastPosition());
116 void MVPClient::sendULONG(ULONG ul)
119 *(ULONG*)&sendBuffer[0] = htonl(4);
120 *(ULONG*)&sendBuffer[4] = htonl(ul);
122 tcp.sendPacket(sendBuffer, 8);
123 log->log("Client", Log::DEBUG, "written ULONG %lu", ul);
126 void MVPClientStartThread(void* arg)
128 MVPClient* m = (MVPClient*)arg;
130 // Nothing external to this class has a reference to it
131 // This is the end of the thread.. so delete m
138 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
139 log->log("Client", Log::DEBUG, "MVPClient run success");
143 void MVPClient::run2()
148 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
149 pthread_detach(runThread); // Detach
151 tcp.disableReadTimeout();
153 tcp.setSoKeepTime(3);
154 tcp.setNonBlocking();
164 log->log("Client", Log::DEBUG, "Waiting");
165 buffer = (UCHAR*)tcp.receivePacket();
166 log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength());
169 log->log("Client", Log::DEBUG, "Detected connection closed");
173 packetLength = tcp.getDataLength() - 4;
174 opcode = ntohl(*(ULONG*)buffer);
177 if (!loggedIn && (opcode != 1))
186 result = processLogin(data, packetLength);
189 result = processGetRecordingsList(data, packetLength);
192 result = processDeleteRecording(data, packetLength);
195 result = processGetSummary(data, packetLength);
198 result = processGetChannelsList(data, packetLength);
201 result = processStartStreamingChannel(data, packetLength);
204 result = processGetBlock(data, packetLength);
207 result = processStopStreaming(data, packetLength);
210 result = processStartStreamingRecording(data, packetLength);
213 result = processGetChannelSchedule(data, packetLength);
216 result = processConfigSave(data, packetLength);
219 result = processConfigLoad(data, packetLength);
222 result = processReScanRecording(data, packetLength);
225 result = processGetTimers(data, packetLength);
228 result = processSetTimer(data, packetLength);
231 result = processPositionFromFrameNumber(data, packetLength);
240 int MVPClient::processLogin(UCHAR* buffer, int length)
242 if (length != 6) return 0;
246 const char* configDir = cPlugin::ConfigDirectory();
249 log->log("Client", Log::DEBUG, "No config dir!");
253 char configFileName[PATH_MAX];
254 snprintf(configFileName, PATH_MAX - strlen(configDir) - 17 - 20, "%s/vomp-%02X-%02X-%02X-%02X-%02X-%02X.conf", configDir, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
255 //( ^^^^^^^^^^^^^eh?^^^^^^^^^^^^^)
256 config.init(configFileName);
258 // Send the login reply
260 time_t timeNow = time(NULL);
261 struct tm* timeStruct = localtime(&timeNow);
262 int timeOffset = timeStruct->tm_gmtoff;
264 UCHAR sendBuffer[12];
265 *(ULONG*)&sendBuffer[0] = htonl(8);
266 *(ULONG*)&sendBuffer[4] = htonl(timeNow);
267 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
269 tcp.sendPacket(sendBuffer, 12);
270 log->log("Client", Log::DEBUG, "written login reply");
276 int MVPClient::processGetRecordingsList(UCHAR* data, int length)
278 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
279 int count = 4; // leave space for the packet length
284 int Percent = VideoDiskSpace(&FreeMB);
285 int Total = (FreeMB / (100 - Percent)) * 100;
287 *(ULONG*)&sendBuffer[count] = htonl(Total);
288 count += sizeof(ULONG);
289 *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
290 count += sizeof(ULONG);
291 *(ULONG*)&sendBuffer[count] = htonl(Percent);
292 count += sizeof(ULONG);
295 cRecordings Recordings;
298 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
300 if (count > 49000) break; // just how big is that hard disk?!
301 *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
304 point = (char*)recording->Name();
305 strcpy((char*)&sendBuffer[count], point);
306 count += strlen(point) + 1;
308 point = (char*)recording->FileName();
309 strcpy((char*)&sendBuffer[count], point);
310 count += strlen(point) + 1;
313 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
315 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
317 tcp.sendPacket(sendBuffer, count);
319 log->log("Client", Log::DEBUG, "Written list");
324 int MVPClient::processDeleteRecording(UCHAR* data, int length)
326 // data is a pointer to the fileName string
328 cRecordings Recordings;
329 Recordings.Load(); // probably have to do this
331 cRecording* recording = Recordings.GetByName((char*)data);
333 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
337 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
349 int MVPClient::processGetSummary(UCHAR* data, int length)
351 // data is a pointer to the fileName string
353 cRecordings Recordings;
354 Recordings.Load(); // probably have to do this
356 cRecording *recording = Recordings.GetByName((char*)data);
358 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
362 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
363 int count = 4; // leave space for the packet length
366 #if VDRVERSNUM < 10300
367 point = (char*)recording->Summary();
369 const cRecordingInfo *Info = recording->Info();
370 point = (char*)Info->ShortText();
371 log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point);
374 point = (char*)Info->Description();
375 log->log("Client", Log::DEBUG, "description pointer %p", point);
381 strcpy((char*)&sendBuffer[count], point);
382 count += strlen(point) + 1;
386 strcpy((char*)&sendBuffer[count], "");
390 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
392 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
394 tcp.sendPacket(sendBuffer, count);
396 log->log("Client", Log::DEBUG, "Written summary");
408 int MVPClient::processGetChannelsList(UCHAR* data, int length)
410 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
411 int count = 4; // leave space for the packet length
415 char* chanConfig = config.getValueString("General", "Channels");
417 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
419 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
421 #if VDRVERSNUM < 10300
422 if (!channel->GroupSep() && (!channel->Ca() || allChans))
424 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
427 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
429 if (channel->Vpid()) type = 1;
430 #if VDRVERSNUM < 10300
433 else if (channel->Apid(0)) type = 2;
437 if (count > 49000) break;
438 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
441 *(ULONG*)&sendBuffer[count] = htonl(type);
444 point = (char*)channel->Name();
445 strcpy((char*)&sendBuffer[count], point);
446 count += strlen(point) + 1;
450 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
452 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
454 tcp.sendPacket(sendBuffer, count);
456 log->log("Client", Log::DEBUG, "Written channels list");
461 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
463 log->log("Client", Log::DEBUG, "length = %i", length);
464 ULONG channelNumber = ntohl(*(ULONG*)data);
466 cChannel* channel = channelFromNumber(channelNumber);
473 // get the priority we should use
475 int priority = config.getValueLong("General", "Live priority", &fail);
478 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
482 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
487 if (priority < 0) priority = 0;
488 if (priority > 99) priority = 99;
490 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
491 lp = MVPReceiver::create(channel, priority);
511 int MVPClient::processStopStreaming(UCHAR* data, int length)
513 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
524 delete recordingManager;
526 recordingManager = NULL;
533 int MVPClient::processGetBlock(UCHAR* data, int length)
537 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
541 ULLONG position = ntohll(*(ULLONG*)data);
542 data += sizeof(ULLONG);
543 ULONG amount = ntohl(*(ULONG*)data);
545 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
547 UCHAR sendBuffer[amount + 4];
548 ULONG amountReceived = 0; // compiler moan.
551 log->log("Client", Log::DEBUG, "getting from live");
552 amountReceived = lp->getBlock(&sendBuffer[4], amount);
556 // vdr has possibly disconnected the receiver
557 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
564 log->log("Client", Log::DEBUG, "getting from recording");
565 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
571 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
575 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
576 tcp.sendPacket(sendBuffer, amountReceived + 4);
577 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
583 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
585 // data is a pointer to the fileName string
587 recordingManager = new cRecordings;
588 recordingManager->Load();
590 cRecording* recording = recordingManager->GetByName((char*)data);
592 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
596 rp = new RecPlayer(recording);
598 UCHAR sendBuffer[12];
599 *(ULONG*)&sendBuffer[0] = htonl(8);
600 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
602 tcp.sendPacket(sendBuffer, 12);
603 log->log("Client", Log::DEBUG, "written totalLength");
607 delete recordingManager;
608 recordingManager = NULL;
613 int MVPClient::processReScanRecording(UCHAR* data, int length)
619 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
624 retval = rp->getTotalLength();
627 UCHAR sendBuffer[12];
628 *(ULONG*)&sendBuffer[0] = htonl(8);
629 *(ULLONG*)&sendBuffer[4] = htonll(retval);
631 tcp.sendPacket(sendBuffer, 12);
632 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
636 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
640 ULONG frameNumber = ntohl(*(ULONG*)data);
645 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
649 retval = rp->positionFromFrameNumber(frameNumber);
652 UCHAR sendBuffer[12];
653 *(ULONG*)&sendBuffer[0] = htonl(8);
654 *(ULLONG*)&sendBuffer[4] = htonll(retval);
656 tcp.sendPacket(sendBuffer, 12);
657 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
661 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
663 ULONG channelNumber = ntohl(*(ULONG*)data);
665 ULONG startTime = ntohl(*(ULONG*)data);
667 ULONG duration = ntohl(*(ULONG*)data);
669 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
671 cChannel* channel = channelFromNumber(channelNumber);
675 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
679 log->log("Client", Log::DEBUG, "Got channel");
681 #if VDRVERSNUM < 10300
682 cMutexLock MutexLock;
683 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
685 cSchedulesLock MutexLock;
686 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
691 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
695 log->log("Client", Log::DEBUG, "Got schedule!s! object");
697 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
701 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
705 log->log("Client", Log::DEBUG, "Got schedule object");
707 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
708 ULONG sendBufferLength = 100000;
709 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
713 // assign all the event info to temp vars then we know exactly what size they are
716 ULONG thisEventDuration;
717 const char* thisEventTitle;
718 const char* thisEventSubTitle;
719 const char* thisEventDescription;
721 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
722 ULONG thisEventLength;
724 #if VDRVERSNUM < 10300
726 const cEventInfo *event;
727 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
729 event = Schedule->GetEventNumber(eventNumber);
731 thisEventID = event->GetEventID();
732 thisEventTime = event->GetTime();
733 thisEventDuration = event->GetDuration();
734 thisEventTitle = event->GetTitle();
735 thisEventSubTitle = event->GetSubtitle();
736 thisEventDescription = event->GetExtendedDescription();
740 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
742 thisEventID = event->EventID();
743 thisEventTime = event->StartTime();
744 thisEventDuration = event->Duration();
745 thisEventTitle = event->Title();
746 thisEventSubTitle = NULL;
747 thisEventDescription = event->Description();
751 log->log("Client", Log::DEBUG, "Got an event object %p", event);
754 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
757 if ((thisEventTime + thisEventDuration) <= startTime) continue;
760 if (thisEventTime >= (startTime + duration)) continue;
762 if (!thisEventTitle) thisEventTitle = empty;
763 if (!thisEventSubTitle) thisEventSubTitle = empty;
764 if (!thisEventDescription) thisEventDescription = empty;
766 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
768 log->log("Client", Log::DEBUG, "Done s1");
770 // now extend the buffer if necessary
771 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
773 log->log("Client", Log::DEBUG, "Extending buffer");
774 sendBufferLength += 100000;
775 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
779 UCHAR sendBuffer2[8];
780 *(ULONG*)&sendBuffer2[0] = htonl(4);
781 *(ULONG*)&sendBuffer2[4] = htonl(0);
782 tcp.sendPacket(sendBuffer2, 8);
783 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
789 log->log("Client", Log::DEBUG, "Done s2");
791 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
792 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
793 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
795 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
796 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
797 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
799 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
802 log->log("Client", Log::DEBUG, "Got all event data");
804 if (sendBufferUsed == sizeof(ULONG))
808 log->log("Client", Log::DEBUG, "Written 0 because no data");
812 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
813 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
814 tcp.sendPacket(sendBuffer, sendBufferUsed);
815 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
823 int MVPClient::processConfigSave(UCHAR* buffer, int length)
825 char* section = (char*)buffer;
829 for (int k = 0; k < length; k++)
831 if (buffer[k] == '\0')
835 key = (char*)&buffer[k+1];
839 value = (char*)&buffer[k+1];
845 // if the last string (value) doesnt have null terminator, give up
846 if (buffer[length - 1] != '\0') return 0;
848 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
849 if (config.setValueString(section, key, value))
861 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
863 char* section = (char*)buffer;
866 for (int k = 0; k < length; k++)
868 if (buffer[k] == '\0')
870 key = (char*)&buffer[k+1];
875 char* value = config.getValueString(section, key);
879 UCHAR sendBuffer[4 + strlen(value) + 1];
880 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
881 strcpy((char*)&sendBuffer[4], value);
882 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
884 log->log("Client", Log::DEBUG, "Written config load packet");
890 *(ULONG*)&sendBuffer[0] = htonl(4);
891 *(ULONG*)&sendBuffer[4] = htonl(0);
892 tcp.sendPacket(sendBuffer, 8);
894 log->log("Client", Log::DEBUG, "Written config load failed packet");
900 void MVPClient::cleanConfig()
902 log->log("Client", Log::DEBUG, "Clean config");
904 cRecordings Recordings;
909 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
910 char* position = resumes;
911 for(int k = 0; k < numReturns; k++)
913 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
915 cRecording* recording = Recordings.GetByName(position);
918 // doesn't exist anymore
919 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
920 config.deleteValue("ResumeData", position);
924 log->log("Client", Log::DEBUG, "This recording still exists");
927 position += strlen(position) + 1;
939 event = Schedule->GetPresentEvent();
941 fprintf(f, "\n\nCurrent event\n\n");
943 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
944 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
945 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
946 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
948 event = Schedule->GetFollowingEvent();
950 fprintf(f, "\n\nFollowing event\n\n");
952 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
953 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
954 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
955 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
961 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
962 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
963 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
964 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
972 void MVPClient::test2()
974 FILE* f = fopen("/tmp/s.txt", "w");
976 #if VDRVERSNUM < 10300
977 cMutexLock MutexLock;
978 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
980 cSchedulesLock MutexLock;
981 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
986 fprintf(f, "Schedules = NULL\n");
991 fprintf(f, "Schedules dump:\n");
995 const cSchedule *Schedule;
996 int scheduleNumber = 0;
999 cChannel *thisChannel;
1001 #if VDRVERSNUM < 10300
1002 const cEventInfo *event;
1003 int eventNumber = 0;
1005 const cEvent *event;
1008 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1009 // Schedule = Schedules->GetSchedule();
1010 Schedule = Schedules->First();
1013 fprintf(f, "First Schedule = NULL\n");
1020 fprintf(f, "Schedule #%i\n", scheduleNumber);
1021 fprintf(f, "-------------\n\n");
1023 #if VDRVERSNUM < 10300
1024 tchid = Schedule->GetChannelID();
1026 tchid = Schedule->ChannelID();
1029 #if VDRVERSNUM < 10300
1030 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1031 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1033 // put the count at the end.
1036 thisChannel = Channels.GetByChannelID(tchid, true);
1039 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1043 fprintf(f, "thisChannel = NULL for tchid\n");
1046 #if VDRVERSNUM < 10300
1047 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1049 event = Schedule->GetEventNumber(eventNumber);
1050 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1051 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1052 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1053 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1054 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1055 fprintf(f, "Event %u dump:\n", eventNumber);
1060 // This whole section needs rewriting to walk the list.
1061 event = Schedule->Events()->First();
1063 event = Schedule->Events()->Next(event);
1068 fprintf(f, "\nDump from object:\n");
1070 fprintf(f, "\nEND\n");
1080 fprintf(f, "End of current Schedule\n\n\n");
1082 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1096 const cEventInfo *GetPresentEvent(void) const;
1097 const cEventInfo *GetFollowingEvent(void) const;
1098 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1099 const cEventInfo *GetEventAround(time_t tTime) const;
1100 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1103 const unsigned char GetTableID(void) const;
1104 const char *GetTimeString(void) const;
1105 const char *GetEndTimeString(void) const;
1106 const char *GetDate(void) const;
1107 bool IsFollowing(void) const;
1108 bool IsPresent(void) const;
1109 const char *GetExtendedDescription(void) const;
1110 const char *GetSubtitle(void) const;
1111 const char *GetTitle(void) const;
1112 unsigned short GetEventID(void) const;
1113 long GetDuration(void) const;
1114 time_t GetTime(void) const;
1115 tChannelID GetChannelID(void) const;
1116 int GetChannelNumber(void) const { return nChannelNumber; }
1117 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1118 void Dump(FILE *f, const char *Prefix = "") const;
1124 void MVPClient::test(int channelNumber)
1126 FILE* f = fopen("/tmp/test.txt", "w");
1128 cMutexLock MutexLock;
1129 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1133 fprintf(f, "Schedules = NULL\n");
1138 fprintf(f, "Schedules dump:\n");
1139 // Schedules->Dump(f);
1141 const cSchedule *Schedule;
1142 cChannel *thisChannel;
1143 const cEventInfo *event;
1145 thisChannel = channelFromNumber(channelNumber);
1148 fprintf(f, "thisChannel = NULL\n");
1153 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1154 // Schedule = Schedules->GetSchedule();
1155 // Schedule = Schedules->First();
1158 fprintf(f, "First Schedule = NULL\n");
1163 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1165 // For some channels VDR seems to pick a random point in time to
1166 // start dishing out events, but they are in order
1167 // at some point in the list the time snaps to the current event
1172 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1174 event = Schedule->GetEventNumber(eventNumber);
1175 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1176 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1177 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1181 fprintf(f, "\nEND\n");
1195 Schedules = the collection of all the Schedule objects
1196 Schedule = One schedule, contants all the events for a channel
1197 Event = One programme
1206 Subtitle (used for "Programmes resume at ...")
1209 IsPresent ? easy to work out tho. Oh it doesn't always work
1214 void MVPClient::test2()
1216 log->log("-", Log::DEBUG, "Timers List");
1218 for (int i = 0; i < Timers.Count(); i++)
1220 cTimer *timer = Timers.Get(i);
1221 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1222 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1223 #if VDRVERSNUM < 10300
1224 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());
1226 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());
1228 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1229 log->log("-", Log::DEBUG, "");
1232 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1233 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1234 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1239 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1240 recording is a bool, 0 for not currently recording, 1 for currently recording
1241 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
1245 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1247 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1248 int count = 4; // leave space for the packet length
1250 const char* fileName;
1252 int numTimers = Timers.Count();
1254 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1256 for (int i = 0; i < numTimers; i++)
1258 if (count > 49000) break;
1260 timer = Timers.Get(i);
1262 #if VDRVERSNUM < 10300
1263 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1265 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1267 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1268 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1269 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1270 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1271 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1272 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1273 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1275 fileName = timer->File();
1276 strcpy((char*)&sendBuffer[count], fileName);
1277 count += strlen(fileName) + 1;
1280 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1282 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1284 tcp.sendPacket(sendBuffer, count);
1285 delete[] sendBuffer;
1286 log->log("Client", Log::DEBUG, "Written timers list");
1291 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1293 char* timerString = new char[strlen((char*)buffer) + 1];
1294 strcpy(timerString, (char*)buffer);
1296 #if VDRVERSNUM < 10300
1298 // If this is VDR 1.2 the date part of the timer string must be reduced
1299 // to just DD rather than YYYY-MM-DD
1301 int s = 0; // source
1302 int d = 0; // destination
1304 while(c != 2) // copy up to date section, including the second ':'
1306 timerString[d] = buffer[s];
1307 if (buffer[s] == ':') c++;
1311 // now it has copied up to the date section
1313 while(c != 2) // waste YYYY-MM-
1315 if (buffer[s] == '-') c++;
1318 // now source is at the DD
1319 memcpy(&timerString[d], &buffer[s], length - s);
1321 timerString[d] = '\0';
1323 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1324 log->log("Client", Log::DEBUG, "%s", timerString);
1328 cTimer *timer = new cTimer;
1329 if (timer->Parse((char*)timerString))
1331 cTimer *t = Timers.GetTimer(timer);
1335 #if VDRVERSNUM < 10300
1338 Timers.SetModified();