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);
568 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
569 tcp.sendPacket(sendBuffer, amountReceived + 4);
570 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
575 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
577 // data is a pointer to the fileName string
579 recordingManager = new cRecordings;
580 recordingManager->Load();
582 cRecording* recording = recordingManager->GetByName((char*)data);
584 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
588 rp = new RecPlayer(recording);
590 UCHAR sendBuffer[12];
591 *(ULONG*)&sendBuffer[0] = htonl(8);
592 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
594 tcp.sendPacket(sendBuffer, 12);
595 log->log("Client", Log::DEBUG, "written totalLength");
599 delete recordingManager;
600 recordingManager = NULL;
605 int MVPClient::processReScanRecording(UCHAR* data, int length)
611 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
616 retval = rp->getTotalLength();
619 UCHAR sendBuffer[12];
620 *(ULONG*)&sendBuffer[0] = htonl(8);
621 *(ULLONG*)&sendBuffer[4] = htonll(retval);
623 tcp.sendPacket(sendBuffer, 12);
624 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
628 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
632 ULONG frameNumber = ntohl(*(ULONG*)data);
637 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
641 retval = rp->positionFromFrameNumber(frameNumber);
644 UCHAR sendBuffer[12];
645 *(ULONG*)&sendBuffer[0] = htonl(8);
646 *(ULLONG*)&sendBuffer[4] = htonll(retval);
648 tcp.sendPacket(sendBuffer, 12);
649 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
653 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
655 ULONG channelNumber = ntohl(*(ULONG*)data);
657 ULONG startTime = ntohl(*(ULONG*)data);
659 ULONG duration = ntohl(*(ULONG*)data);
661 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
663 cChannel* channel = channelFromNumber(channelNumber);
667 *(ULONG*)&sendBuffer[0] = htonl(4);
668 *(ULONG*)&sendBuffer[4] = htonl(0);
669 tcp.sendPacket(sendBuffer, 8);
670 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
674 log->log("Client", Log::DEBUG, "Got channel");
676 #if VDRVERSNUM < 10300
677 cMutexLock MutexLock;
678 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
680 cSchedulesLock MutexLock;
681 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
686 *(ULONG*)&sendBuffer[0] = htonl(4);
687 *(ULONG*)&sendBuffer[4] = htonl(0);
688 tcp.sendPacket(sendBuffer, 8);
689 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
693 log->log("Client", Log::DEBUG, "Got schedule!s! object");
695 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
699 *(ULONG*)&sendBuffer[0] = htonl(4);
700 *(ULONG*)&sendBuffer[4] = htonl(0);
701 tcp.sendPacket(sendBuffer, 8);
702 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
706 log->log("Client", Log::DEBUG, "Got schedule object");
708 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
709 ULONG sendBufferLength = 100000;
710 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
714 // assign all the event info to temp vars then we know exactly what size they are
717 ULONG thisEventDuration;
718 const char* thisEventTitle;
719 const char* thisEventSubTitle;
720 const char* thisEventDescription;
722 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
723 ULONG thisEventLength;
725 #if VDRVERSNUM < 10300
727 const cEventInfo *event;
728 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
730 event = Schedule->GetEventNumber(eventNumber);
732 thisEventID = event->GetEventID();
733 thisEventTime = event->GetTime();
734 thisEventDuration = event->GetDuration();
735 thisEventTitle = event->GetTitle();
736 thisEventSubTitle = event->GetSubtitle();
737 thisEventDescription = event->GetExtendedDescription();
741 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
743 thisEventID = event->EventID();
744 thisEventTime = event->StartTime();
745 thisEventDuration = event->Duration();
746 thisEventTitle = event->Title();
747 thisEventSubTitle = NULL;
748 thisEventDescription = event->Description();
752 log->log("Client", Log::DEBUG, "Got an event object %p", event);
755 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
758 if ((thisEventTime + thisEventDuration) <= startTime) continue;
761 if (thisEventTime >= (startTime + duration)) continue;
763 if (!thisEventTitle) thisEventTitle = empty;
764 if (!thisEventSubTitle) thisEventSubTitle = empty;
765 if (!thisEventDescription) thisEventDescription = empty;
767 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
769 log->log("Client", Log::DEBUG, "Done s1");
771 // now extend the buffer if necessary
772 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
774 log->log("Client", Log::DEBUG, "Extending buffer");
775 sendBufferLength += 100000;
776 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
780 UCHAR sendBuffer2[8];
781 *(ULONG*)&sendBuffer2[0] = htonl(4);
782 *(ULONG*)&sendBuffer2[4] = htonl(0);
783 tcp.sendPacket(sendBuffer2, 8);
784 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
790 log->log("Client", Log::DEBUG, "Done s2");
792 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
793 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
794 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
796 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
797 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
798 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
800 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
803 log->log("Client", Log::DEBUG, "Got all event data");
805 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
806 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
808 tcp.sendPacket(sendBuffer, sendBufferUsed);
809 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
816 int MVPClient::processConfigSave(UCHAR* buffer, int length)
818 char* section = (char*)buffer;
822 for (int k = 0; k < length; k++)
824 if (buffer[k] == '\0')
828 key = (char*)&buffer[k+1];
832 value = (char*)&buffer[k+1];
838 // if the last string (value) doesnt have null terminator, give up
839 if (buffer[length - 1] != '\0') return 0;
841 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
842 if (config.setValueString(section, key, value))
854 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
856 char* section = (char*)buffer;
859 for (int k = 0; k < length; k++)
861 if (buffer[k] == '\0')
863 key = (char*)&buffer[k+1];
868 char* value = config.getValueString(section, key);
872 UCHAR sendBuffer[4 + strlen(value) + 1];
873 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
874 strcpy((char*)&sendBuffer[4], value);
875 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
877 log->log("Client", Log::DEBUG, "Written config load packet");
883 *(ULONG*)&sendBuffer[0] = htonl(4);
884 *(ULONG*)&sendBuffer[4] = htonl(0);
885 tcp.sendPacket(sendBuffer, 8);
887 log->log("Client", Log::DEBUG, "Written config load failed packet");
893 void MVPClient::cleanConfig()
895 log->log("Client", Log::DEBUG, "Clean config");
897 cRecordings Recordings;
902 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
903 char* position = resumes;
904 for(int k = 0; k < numReturns; k++)
906 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
908 cRecording* recording = Recordings.GetByName(position);
911 // doesn't exist anymore
912 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
913 config.deleteValue("ResumeData", position);
917 log->log("Client", Log::DEBUG, "This recording still exists");
920 position += strlen(position) + 1;
932 event = Schedule->GetPresentEvent();
934 fprintf(f, "\n\nCurrent event\n\n");
936 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
937 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
938 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
939 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
941 event = Schedule->GetFollowingEvent();
943 fprintf(f, "\n\nFollowing event\n\n");
945 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
946 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
947 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
948 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
954 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
955 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
956 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
957 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
965 void MVPClient::test2()
967 FILE* f = fopen("/tmp/s.txt", "w");
969 #if VDRVERSNUM < 10300
970 cMutexLock MutexLock;
971 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
973 cSchedulesLock MutexLock;
974 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
979 fprintf(f, "Schedules = NULL\n");
984 fprintf(f, "Schedules dump:\n");
988 const cSchedule *Schedule;
989 int scheduleNumber = 0;
992 cChannel *thisChannel;
994 #if VDRVERSNUM < 10300
995 const cEventInfo *event;
1001 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1002 // Schedule = Schedules->GetSchedule();
1003 Schedule = Schedules->First();
1006 fprintf(f, "First Schedule = NULL\n");
1013 fprintf(f, "Schedule #%i\n", scheduleNumber);
1014 fprintf(f, "-------------\n\n");
1016 #if VDRVERSNUM < 10300
1017 tchid = Schedule->GetChannelID();
1019 tchid = Schedule->ChannelID();
1022 #if VDRVERSNUM < 10300
1023 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1024 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1026 // put the count at the end.
1029 thisChannel = Channels.GetByChannelID(tchid, true);
1032 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1036 fprintf(f, "thisChannel = NULL for tchid\n");
1039 #if VDRVERSNUM < 10300
1040 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1042 event = Schedule->GetEventNumber(eventNumber);
1043 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1044 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1045 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1046 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1047 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1048 fprintf(f, "Event %u dump:\n", eventNumber);
1053 // This whole section needs rewriting to walk the list.
1054 event = Schedule->Events()->First();
1056 event = Schedule->Events()->Next(event);
1061 fprintf(f, "\nDump from object:\n");
1063 fprintf(f, "\nEND\n");
1073 fprintf(f, "End of current Schedule\n\n\n");
1075 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1089 const cEventInfo *GetPresentEvent(void) const;
1090 const cEventInfo *GetFollowingEvent(void) const;
1091 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1092 const cEventInfo *GetEventAround(time_t tTime) const;
1093 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1096 const unsigned char GetTableID(void) const;
1097 const char *GetTimeString(void) const;
1098 const char *GetEndTimeString(void) const;
1099 const char *GetDate(void) const;
1100 bool IsFollowing(void) const;
1101 bool IsPresent(void) const;
1102 const char *GetExtendedDescription(void) const;
1103 const char *GetSubtitle(void) const;
1104 const char *GetTitle(void) const;
1105 unsigned short GetEventID(void) const;
1106 long GetDuration(void) const;
1107 time_t GetTime(void) const;
1108 tChannelID GetChannelID(void) const;
1109 int GetChannelNumber(void) const { return nChannelNumber; }
1110 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1111 void Dump(FILE *f, const char *Prefix = "") const;
1117 void MVPClient::test(int channelNumber)
1119 FILE* f = fopen("/tmp/test.txt", "w");
1121 cMutexLock MutexLock;
1122 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1126 fprintf(f, "Schedules = NULL\n");
1131 fprintf(f, "Schedules dump:\n");
1132 // Schedules->Dump(f);
1134 const cSchedule *Schedule;
1135 cChannel *thisChannel;
1136 const cEventInfo *event;
1138 thisChannel = channelFromNumber(channelNumber);
1141 fprintf(f, "thisChannel = NULL\n");
1146 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1147 // Schedule = Schedules->GetSchedule();
1148 // Schedule = Schedules->First();
1151 fprintf(f, "First Schedule = NULL\n");
1156 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1158 // For some channels VDR seems to pick a random point in time to
1159 // start dishing out events, but they are in order
1160 // at some point in the list the time snaps to the current event
1165 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1167 event = Schedule->GetEventNumber(eventNumber);
1168 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1169 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1170 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1174 fprintf(f, "\nEND\n");
1188 Schedules = the collection of all the Schedule objects
1189 Schedule = One schedule, contants all the events for a channel
1190 Event = One programme
1199 Subtitle (used for "Programmes resume at ...")
1202 IsPresent ? easy to work out tho. Oh it doesn't always work
1207 void MVPClient::test2()
1209 log->log("-", Log::DEBUG, "Timers List");
1211 for (int i = 0; i < Timers.Count(); i++)
1213 cTimer *timer = Timers.Get(i);
1214 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1215 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1216 #if VDRVERSNUM < 10300
1217 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());
1219 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());
1221 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1222 log->log("-", Log::DEBUG, "");
1225 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1226 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1227 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1232 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1233 recording is a bool, 0 for not currently recording, 1 for currently recording
1234 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
1238 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1240 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1241 int count = 4; // leave space for the packet length
1243 const char* fileName;
1245 int numTimers = Timers.Count();
1247 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1249 for (int i = 0; i < numTimers; i++)
1251 if (count > 49000) break;
1253 timer = Timers.Get(i);
1255 #if VDRVERSNUM < 10300
1256 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1258 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1260 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1261 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1262 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1263 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1264 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1265 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1266 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1268 fileName = timer->File();
1269 strcpy((char*)&sendBuffer[count], fileName);
1270 count += strlen(fileName) + 1;
1273 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1275 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1277 tcp.sendPacket(sendBuffer, count);
1278 delete[] sendBuffer;
1279 log->log("Client", Log::DEBUG, "Written timers list");
1284 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1286 char* timerString = new char[strlen((char*)buffer) + 1];
1287 strcpy(timerString, (char*)buffer);
1289 #if VDRVERSNUM < 10300
1291 // If this is VDR 1.2 the date part of the timer string must be reduced
1292 // to just DD rather than YYYY-MM-DD
1294 int s = 0; // source
1295 int d = 0; // destination
1297 while(c != 2) // copy up to date section, including the second ':'
1299 timerString[d] = buffer[s];
1300 if (buffer[s] == ':') c++;
1304 // now it has copied up to the date section
1306 while(c != 2) // waste YYYY-MM-
1308 if (buffer[s] == '-') c++;
1311 // now source is at the DD
1312 memcpy(&timerString[d], &buffer[s], length - s);
1314 timerString[d] = '\0';
1316 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1317 log->log("Client", Log::DEBUG, "%s", timerString);
1321 cTimer *timer = new cTimer;
1322 if (timer->Parse((char*)timerString))
1324 cTimer *t = Timers.GetTimer(timer);
1328 #if VDRVERSNUM < 10300
1331 Timers.SetModified();