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);
378 strcpy((char*)&sendBuffer[count], point);
379 count += strlen(point) + 1;
380 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
382 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
384 tcp.sendPacket(sendBuffer, count);
386 log->log("Client", Log::DEBUG, "Written summary");
398 int MVPClient::processGetChannelsList(UCHAR* data, int length)
400 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
401 int count = 4; // leave space for the packet length
405 char* chanConfig = config.getValueString("General", "Channels");
407 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
409 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
411 #if VDRVERSNUM < 10300
412 if (!channel->GroupSep() && (!channel->Ca() || allChans))
414 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
417 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
419 if (channel->Vpid()) type = 1;
420 #if VDRVERSNUM < 10300
423 else if (channel->Apid(0)) type = 2;
427 if (count > 49000) break;
428 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
431 *(ULONG*)&sendBuffer[count] = htonl(type);
434 point = (char*)channel->Name();
435 strcpy((char*)&sendBuffer[count], point);
436 count += strlen(point) + 1;
440 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
442 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
444 tcp.sendPacket(sendBuffer, count);
446 log->log("Client", Log::DEBUG, "Written channels list");
451 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
453 log->log("Client", Log::DEBUG, "length = %i", length);
454 ULONG channelNumber = ntohl(*(ULONG*)data);
456 cChannel* channel = channelFromNumber(channelNumber);
463 // get the priority we should use
465 int priority = config.getValueLong("General", "Live priority", &fail);
468 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
472 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
477 if (priority < 0) priority = 0;
478 if (priority > 99) priority = 99;
480 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
481 lp = MVPReceiver::create(channel, priority);
501 int MVPClient::processStopStreaming(UCHAR* data, int length)
503 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
514 delete recordingManager;
516 recordingManager = NULL;
523 int MVPClient::processGetBlock(UCHAR* data, int length)
527 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
531 ULLONG position = ntohll(*(ULLONG*)data);
532 data += sizeof(ULLONG);
533 ULONG amount = ntohl(*(ULONG*)data);
535 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
537 UCHAR sendBuffer[amount + 4];
538 ULONG amountReceived = 0; // compiler moan.
541 log->log("Client", Log::DEBUG, "getting from live");
542 amountReceived = lp->getBlock(&sendBuffer[4], amount);
546 // vdr has possibly disconnected the receiver
547 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
554 log->log("Client", Log::DEBUG, "getting from recording");
555 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
558 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
559 tcp.sendPacket(sendBuffer, amountReceived + 4);
560 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
565 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
567 // data is a pointer to the fileName string
569 recordingManager = new cRecordings;
570 recordingManager->Load();
572 cRecording* recording = recordingManager->GetByName((char*)data);
574 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
578 rp = new RecPlayer(recording);
580 UCHAR sendBuffer[12];
581 *(ULONG*)&sendBuffer[0] = htonl(8);
582 *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength());
584 tcp.sendPacket(sendBuffer, 12);
585 log->log("Client", Log::DEBUG, "written totalLength");
589 delete recordingManager;
590 recordingManager = NULL;
595 int MVPClient::processReScanRecording(UCHAR* data, int length)
601 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
606 retval = rp->getTotalLength();
609 UCHAR sendBuffer[12];
610 *(ULONG*)&sendBuffer[0] = htonl(8);
611 *(ULLONG*)&sendBuffer[4] = htonll(retval);
613 tcp.sendPacket(sendBuffer, 12);
614 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
618 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
622 ULONG frameNumber = ntohl(*(ULONG*)data);
627 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
631 retval = rp->positionFromFrameNumber(frameNumber);
634 UCHAR sendBuffer[12];
635 *(ULONG*)&sendBuffer[0] = htonl(8);
636 *(ULLONG*)&sendBuffer[4] = htonll(retval);
638 tcp.sendPacket(sendBuffer, 12);
639 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
643 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
645 ULONG channelNumber = ntohl(*(ULONG*)data);
647 ULONG startTime = ntohl(*(ULONG*)data);
649 ULONG duration = ntohl(*(ULONG*)data);
651 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
653 cChannel* channel = channelFromNumber(channelNumber);
657 *(ULONG*)&sendBuffer[0] = htonl(4);
658 *(ULONG*)&sendBuffer[4] = htonl(0);
659 tcp.sendPacket(sendBuffer, 8);
660 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
664 log->log("Client", Log::DEBUG, "Got channel");
666 #if VDRVERSNUM < 10300
667 cMutexLock MutexLock;
668 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
670 cSchedulesLock MutexLock;
671 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
676 *(ULONG*)&sendBuffer[0] = htonl(4);
677 *(ULONG*)&sendBuffer[4] = htonl(0);
678 tcp.sendPacket(sendBuffer, 8);
679 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
683 log->log("Client", Log::DEBUG, "Got schedule!s! object");
685 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
689 *(ULONG*)&sendBuffer[0] = htonl(4);
690 *(ULONG*)&sendBuffer[4] = htonl(0);
691 tcp.sendPacket(sendBuffer, 8);
692 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
696 log->log("Client", Log::DEBUG, "Got schedule object");
698 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
699 ULONG sendBufferLength = 100000;
700 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
704 // assign all the event info to temp vars then we know exactly what size they are
707 ULONG thisEventDuration;
708 const char* thisEventTitle;
709 const char* thisEventSubTitle;
710 const char* thisEventDescription;
712 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
713 ULONG thisEventLength;
715 #if VDRVERSNUM < 10300
717 const cEventInfo *event;
718 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
720 event = Schedule->GetEventNumber(eventNumber);
722 thisEventID = event->GetEventID();
723 thisEventTime = event->GetTime();
724 thisEventDuration = event->GetDuration();
725 thisEventTitle = event->GetTitle();
726 thisEventSubTitle = event->GetSubtitle();
727 thisEventDescription = event->GetExtendedDescription();
731 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
733 thisEventID = event->EventID();
734 thisEventTime = event->StartTime();
735 thisEventDuration = event->Duration();
736 thisEventTitle = event->Title();
737 thisEventSubTitle = NULL;
738 thisEventDescription = event->Description();
742 log->log("Client", Log::DEBUG, "Got an event object %p", event);
745 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
748 if ((thisEventTime + thisEventDuration) <= startTime) continue;
751 if (thisEventTime >= (startTime + duration)) continue;
753 if (!thisEventTitle) thisEventTitle = empty;
754 if (!thisEventSubTitle) thisEventSubTitle = empty;
755 if (!thisEventDescription) thisEventDescription = empty;
757 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
759 log->log("Client", Log::DEBUG, "Done s1");
761 // now extend the buffer if necessary
762 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
764 log->log("Client", Log::DEBUG, "Extending buffer");
765 sendBufferLength += 100000;
766 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
770 UCHAR sendBuffer2[8];
771 *(ULONG*)&sendBuffer2[0] = htonl(4);
772 *(ULONG*)&sendBuffer2[4] = htonl(0);
773 tcp.sendPacket(sendBuffer2, 8);
774 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
780 log->log("Client", Log::DEBUG, "Done s2");
782 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
783 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
784 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
786 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
787 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
788 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
790 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
793 log->log("Client", Log::DEBUG, "Got all event data");
795 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
796 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
798 tcp.sendPacket(sendBuffer, sendBufferUsed);
799 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
806 int MVPClient::processConfigSave(UCHAR* buffer, int length)
808 char* section = (char*)buffer;
812 for (int k = 0; k < length; k++)
814 if (buffer[k] == '\0')
818 key = (char*)&buffer[k+1];
822 value = (char*)&buffer[k+1];
828 // if the last string (value) doesnt have null terminator, give up
829 if (buffer[length - 1] != '\0') return 0;
831 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
832 if (config.setValueString(section, key, value))
844 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
846 char* section = (char*)buffer;
849 for (int k = 0; k < length; k++)
851 if (buffer[k] == '\0')
853 key = (char*)&buffer[k+1];
858 char* value = config.getValueString(section, key);
862 UCHAR sendBuffer[4 + strlen(value) + 1];
863 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
864 strcpy((char*)&sendBuffer[4], value);
865 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
867 log->log("Client", Log::DEBUG, "Written config load packet");
873 *(ULONG*)&sendBuffer[0] = htonl(4);
874 *(ULONG*)&sendBuffer[4] = htonl(0);
875 tcp.sendPacket(sendBuffer, 8);
877 log->log("Client", Log::DEBUG, "Written config load failed packet");
883 void MVPClient::cleanConfig()
885 log->log("Client", Log::DEBUG, "Clean config");
887 cRecordings Recordings;
892 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
893 char* position = resumes;
894 for(int k = 0; k < numReturns; k++)
896 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
898 cRecording* recording = Recordings.GetByName(position);
901 // doesn't exist anymore
902 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
903 config.deleteValue("ResumeData", position);
907 log->log("Client", Log::DEBUG, "This recording still exists");
910 position += strlen(position) + 1;
922 event = Schedule->GetPresentEvent();
924 fprintf(f, "\n\nCurrent event\n\n");
926 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
927 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
928 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
929 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
931 event = Schedule->GetFollowingEvent();
933 fprintf(f, "\n\nFollowing event\n\n");
935 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
936 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
937 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
938 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
944 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
945 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
946 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
947 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
955 void MVPClient::test2()
957 FILE* f = fopen("/tmp/s.txt", "w");
959 #if VDRVERSNUM < 10300
960 cMutexLock MutexLock;
961 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
963 cSchedulesLock MutexLock;
964 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
969 fprintf(f, "Schedules = NULL\n");
974 fprintf(f, "Schedules dump:\n");
978 const cSchedule *Schedule;
979 int scheduleNumber = 0;
982 cChannel *thisChannel;
984 #if VDRVERSNUM < 10300
985 const cEventInfo *event;
991 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
992 // Schedule = Schedules->GetSchedule();
993 Schedule = Schedules->First();
996 fprintf(f, "First Schedule = NULL\n");
1003 fprintf(f, "Schedule #%i\n", scheduleNumber);
1004 fprintf(f, "-------------\n\n");
1006 #if VDRVERSNUM < 10300
1007 tchid = Schedule->GetChannelID();
1009 tchid = Schedule->ChannelID();
1012 #if VDRVERSNUM < 10300
1013 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1014 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1016 // put the count at the end.
1019 thisChannel = Channels.GetByChannelID(tchid, true);
1022 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1026 fprintf(f, "thisChannel = NULL for tchid\n");
1029 #if VDRVERSNUM < 10300
1030 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1032 event = Schedule->GetEventNumber(eventNumber);
1033 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1034 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1035 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1036 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1037 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1038 fprintf(f, "Event %u dump:\n", eventNumber);
1043 // This whole section needs rewriting to walk the list.
1044 event = Schedule->Events()->First();
1046 event = Schedule->Events()->Next(event);
1051 fprintf(f, "\nDump from object:\n");
1053 fprintf(f, "\nEND\n");
1063 fprintf(f, "End of current Schedule\n\n\n");
1065 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1079 const cEventInfo *GetPresentEvent(void) const;
1080 const cEventInfo *GetFollowingEvent(void) const;
1081 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1082 const cEventInfo *GetEventAround(time_t tTime) const;
1083 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1086 const unsigned char GetTableID(void) const;
1087 const char *GetTimeString(void) const;
1088 const char *GetEndTimeString(void) const;
1089 const char *GetDate(void) const;
1090 bool IsFollowing(void) const;
1091 bool IsPresent(void) const;
1092 const char *GetExtendedDescription(void) const;
1093 const char *GetSubtitle(void) const;
1094 const char *GetTitle(void) const;
1095 unsigned short GetEventID(void) const;
1096 long GetDuration(void) const;
1097 time_t GetTime(void) const;
1098 tChannelID GetChannelID(void) const;
1099 int GetChannelNumber(void) const { return nChannelNumber; }
1100 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1101 void Dump(FILE *f, const char *Prefix = "") const;
1107 void MVPClient::test(int channelNumber)
1109 FILE* f = fopen("/tmp/test.txt", "w");
1111 cMutexLock MutexLock;
1112 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1116 fprintf(f, "Schedules = NULL\n");
1121 fprintf(f, "Schedules dump:\n");
1122 // Schedules->Dump(f);
1124 const cSchedule *Schedule;
1125 cChannel *thisChannel;
1126 const cEventInfo *event;
1128 thisChannel = channelFromNumber(channelNumber);
1131 fprintf(f, "thisChannel = NULL\n");
1136 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1137 // Schedule = Schedules->GetSchedule();
1138 // Schedule = Schedules->First();
1141 fprintf(f, "First Schedule = NULL\n");
1146 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1148 // For some channels VDR seems to pick a random point in time to
1149 // start dishing out events, but they are in order
1150 // at some point in the list the time snaps to the current event
1155 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1157 event = Schedule->GetEventNumber(eventNumber);
1158 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1159 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1160 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1164 fprintf(f, "\nEND\n");
1178 Schedules = the collection of all the Schedule objects
1179 Schedule = One schedule, contants all the events for a channel
1180 Event = One programme
1189 Subtitle (used for "Programmes resume at ...")
1192 IsPresent ? easy to work out tho. Oh it doesn't always work
1197 void MVPClient::test2()
1199 log->log("-", Log::DEBUG, "Timers List");
1201 for (int i = 0; i < Timers.Count(); i++)
1203 cTimer *timer = Timers.Get(i);
1204 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1205 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1206 #if VDRVERSNUM < 10300
1207 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());
1209 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());
1211 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1212 log->log("-", Log::DEBUG, "");
1215 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1216 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1217 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1222 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1223 recording is a bool, 0 for not currently recording, 1 for currently recording
1224 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
1228 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1230 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1231 int count = 4; // leave space for the packet length
1233 const char* fileName;
1235 int numTimers = Timers.Count();
1237 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1239 for (int i = 0; i < numTimers; i++)
1241 if (count > 49000) break;
1243 timer = Timers.Get(i);
1245 #if VDRVERSNUM < 10300
1246 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1248 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1250 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1251 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1252 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1253 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1254 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1255 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1256 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1258 fileName = timer->File();
1259 strcpy((char*)&sendBuffer[count], fileName);
1260 count += strlen(fileName) + 1;
1263 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1265 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1267 tcp.sendPacket(sendBuffer, count);
1268 delete[] sendBuffer;
1269 log->log("Client", Log::DEBUG, "Written timers list");
1274 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1276 char* timerString = new char[strlen((char*)buffer) + 1];
1277 strcpy(timerString, (char*)buffer);
1279 #if VDRVERSNUM < 10300
1281 // If this is VDR 1.2 the date part of the timer string must be reduced
1282 // to just DD rather than YYYY-MM-DD
1284 int s = 0; // source
1285 int d = 0; // destination
1287 while(c != 2) // copy up to date section, including the second ':'
1289 timerString[d] = buffer[s];
1290 if (buffer[s] == ':') c++;
1294 // now it has copied up to the date section
1296 while(c != 2) // waste YYYY-MM-
1298 if (buffer[s] == '-') c++;
1301 // now source is at the DD
1302 memcpy(&timerString[d], &buffer[s], length - s);
1304 timerString[d] = '\0';
1306 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1307 log->log("Client", Log::DEBUG, "%s", timerString);
1311 cTimer *timer = new cTimer;
1312 if (timer->Parse((char*)timerString))
1314 cTimer *t = Timers.GetTimer(timer);
1318 #if VDRVERSNUM < 10300
1321 Timers.SetModified();