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
24 //#include <vdr/menu.h>
26 pthread_mutex_t threadClientMutex;
27 int MVPClient::nr_clients = 0;
30 MVPClient::MVPClient(Config* cfgBase, char* tconfigDirExtra, int tsocket)
36 recordingManager = NULL;
37 log = Log::getInstance();
39 configDirExtra = tconfigDirExtra;
44 MVPClient::~MVPClient()
46 log->log("Client", Log::DEBUG, "MVP client destructor");
57 delete recordingManager;
59 recordingManager = NULL;
62 if (loggedIn) cleanConfig();
66 ULLONG MVPClient::ntohll(ULLONG a)
71 ULLONG MVPClient::htonll(ULLONG a)
73 #if BYTE_ORDER == BIG_ENDIAN
78 b = ((a << 56) & 0xFF00000000000000ULL)
79 | ((a << 40) & 0x00FF000000000000ULL)
80 | ((a << 24) & 0x0000FF0000000000ULL)
81 | ((a << 8) & 0x000000FF00000000ULL)
82 | ((a >> 8) & 0x00000000FF000000ULL)
83 | ((a >> 24) & 0x0000000000FF0000ULL)
84 | ((a >> 40) & 0x000000000000FF00ULL)
85 | ((a >> 56) & 0x00000000000000FFULL) ;
91 cChannel* MVPClient::channelFromNumber(ULONG channelNumber)
93 cChannel* channel = NULL;
95 for (channel = Channels.First(); channel; channel = Channels.Next(channel))
97 if (!channel->GroupSep())
99 log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name());
101 if (channel->Number() == (int)channelNumber)
103 int vpid = channel->Vpid();
104 #if VDRVERSNUM < 10300
105 int apid1 = channel->Apid1();
107 int apid1 = channel->Apid(0);
109 log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1);
117 log->log("Client", Log::DEBUG, "Channel not found");
123 void MVPClient::writeResumeData()
125 config.setValueLong("ResumeData",
126 (char*)rp->getCurrentRecording()->FileName(),
127 rp->frameNumberFromPosition(rp->getLastPosition()) );
130 void MVPClient::sendULONG(ULONG ul)
133 *(ULONG*)&sendBuffer[0] = htonl(4);
134 *(ULONG*)&sendBuffer[4] = htonl(ul);
136 tcp.sendPacket(sendBuffer, 8);
137 log->log("Client", Log::DEBUG, "written ULONG %lu", ul);
140 void MVPClientStartThread(void* arg)
142 MVPClient* m = (MVPClient*)arg;
144 // Nothing external to this class has a reference to it
145 // This is the end of the thread.. so delete m
152 if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0;
153 log->log("Client", Log::DEBUG, "MVPClient run success");
157 void MVPClient::run2()
162 pthread_sigmask(SIG_BLOCK, &sigset, NULL);
163 pthread_detach(runThread); // Detach
165 tcp.disableReadTimeout();
167 tcp.setSoKeepTime(3);
168 tcp.setNonBlocking();
178 log->log("Client", Log::DEBUG, "Waiting");
179 buffer = (UCHAR*)tcp.receivePacket();
180 log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength());
183 log->log("Client", Log::DEBUG, "Detected connection closed");
187 packetLength = tcp.getDataLength() - 4;
188 opcode = ntohl(*(ULONG*)buffer);
191 if (!loggedIn && (opcode != 1))
197 log->log("Client", Log::DEBUG, "SwitchOp");
201 result = processLogin(data, packetLength);
204 result = processGetRecordingsList(data, packetLength);
207 result = processDeleteRecording(data, packetLength);
210 result = processGetChannelsList(data, packetLength);
213 result = processStartStreamingChannel(data, packetLength);
216 result = processGetBlock(data, packetLength);
219 result = processStopStreaming(data, packetLength);
222 result = processStartStreamingRecording(data, packetLength);
225 result = processGetChannelSchedule(data, packetLength);
228 result = processConfigSave(data, packetLength);
231 result = processConfigLoad(data, packetLength);
234 result = processReScanRecording(data, packetLength); // FIXME obselete
237 result = processGetTimers(data, packetLength);
240 result = processSetTimer(data, packetLength);
243 result = processPositionFromFrameNumber(data, packetLength);
246 result = processFrameNumberFromPosition(data, packetLength);
249 result = processMoveRecording(data, packetLength);
252 result = processGetIFrame(data, packetLength);
255 result = processGetRecInfo(data, packetLength);
258 result = processGetMarks(data, packetLength);
261 result = processGetChannelPids(data, packetLength);
264 result = processGetMediaList(data, packetLength);
267 result = processGetPicture(data, packetLength);
270 result = processGetImageBlock(data, packetLength);
279 int MVPClient::processLogin(UCHAR* buffer, int length)
281 if (length != 6) return 0;
285 const char* configDir = cPlugin::ConfigDirectory(configDirExtra);
288 log->log("Client", Log::DEBUG, "No config dir!");
292 char configFileName[PATH_MAX];
293 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]);
294 config.init(configFileName);
296 // Send the login reply
298 time_t timeNow = time(NULL);
299 struct tm* timeStruct = localtime(&timeNow);
300 int timeOffset = timeStruct->tm_gmtoff;
302 UCHAR sendBuffer[12];
303 *(ULONG*)&sendBuffer[0] = htonl(8);
304 *(ULONG*)&sendBuffer[4] = htonl(timeNow);
305 *(signed int*)&sendBuffer[8] = htonl(timeOffset);
307 tcp.sendPacket(sendBuffer, 12);
308 log->log("Client", Log::DEBUG, "written login reply");
314 int MVPClient::processGetRecordingsList(UCHAR* data, int length)
316 UCHAR* sendBuffer = new UCHAR[50000]; // hope this is enough
317 int count = 4; // leave space for the packet length
322 int Percent = VideoDiskSpace(&FreeMB);
323 int Total = (FreeMB / (100 - Percent)) * 100;
325 *(ULONG*)&sendBuffer[count] = htonl(Total);
326 count += sizeof(ULONG);
327 *(ULONG*)&sendBuffer[count] = htonl(FreeMB);
328 count += sizeof(ULONG);
329 *(ULONG*)&sendBuffer[count] = htonl(Percent);
330 count += sizeof(ULONG);
333 cRecordings Recordings;
336 for (cRecording *recording = Recordings.First(); recording; recording = Recordings.Next(recording))
338 if (count > 49000) break; // just how big is that hard disk?!
339 *(ULONG*)&sendBuffer[count] = htonl(recording->start);// + timeOffset);
342 point = (char*)recording->Name();
343 strcpy((char*)&sendBuffer[count], point);
344 count += strlen(point) + 1;
346 point = (char*)recording->FileName();
347 strcpy((char*)&sendBuffer[count], point);
348 count += strlen(point) + 1;
351 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
353 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
355 tcp.sendPacket(sendBuffer, count);
357 log->log("Client", Log::DEBUG, "Written list");
362 int MVPClient::processDeleteRecording(UCHAR* data, int length)
364 // data is a pointer to the fileName string
366 cRecordings Recordings;
367 Recordings.Load(); // probably have to do this
369 cRecording* recording = Recordings.GetByName((char*)data);
371 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
375 log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name());
377 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
380 if (recording->Delete())
382 // Copy svdrp's way of doing this, see if it works
383 #if VDRVERSNUM > 10300
384 ::Recordings.DelByName(recording->FileName());
406 int MVPClient::processMoveRecording(UCHAR* data, int length)
408 log->log("Client", Log::DEBUG, "Process move recording");
409 char* fileName = (char*)data;
410 char* newPath = NULL;
412 for (int k = 0; k < length; k++)
416 newPath = (char*)&data[k+1];
420 if (!newPath) return 0;
422 cRecordings Recordings;
423 Recordings.Load(); // probably have to do this
425 cRecording* recording = Recordings.GetByName((char*)fileName);
427 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
431 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
434 log->log("Client", Log::DEBUG, "moving recording: %s", recording->Name());
435 log->log("Client", Log::DEBUG, "moving recording: %s", recording->FileName());
436 log->log("Client", Log::DEBUG, "to: %s", newPath);
438 const char* t = recording->FileName();
440 char* dateDirName = NULL; int k;
441 char* titleDirName = NULL; int j;
443 // Find the datedirname
444 for(k = strlen(t) - 1; k >= 0; k--)
448 log->log("Client", Log::DEBUG, "l1: %i", strlen(&t[k+1]) + 1);
449 dateDirName = new char[strlen(&t[k+1]) + 1];
450 strcpy(dateDirName, &t[k+1]);
455 // Find the titledirname
457 for(j = k-1; j >= 0; j--)
461 log->log("Client", Log::DEBUG, "l2: %i", (k - j - 1) + 1);
462 titleDirName = new char[(k - j - 1) + 1];
463 memcpy(titleDirName, &t[j+1], k - j - 1);
464 titleDirName[k - j - 1] = '\0';
469 log->log("Client", Log::DEBUG, "datedirname: %s", dateDirName);
470 log->log("Client", Log::DEBUG, "titledirname: %s", titleDirName);
472 log->log("Client", Log::DEBUG, "viddir: %s", VideoDirectory);
474 char* newContainer = new char[strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1];
475 log->log("Client", Log::DEBUG, "l10: %i", strlen(VideoDirectory) + strlen(newPath) + strlen(titleDirName) + 1);
476 sprintf(newContainer, "%s%s%s", VideoDirectory, newPath, titleDirName);
478 // FIXME Check whether this already exists before mkdiring it
480 log->log("Client", Log::DEBUG, "%s", newContainer);
484 int statret = stat(newContainer, &dstat);
485 if ((statret == -1) && (errno == ENOENT)) // Dir does not exist
487 log->log("Client", Log::DEBUG, "new dir does not exist");
488 int mkdirret = mkdir(newContainer, 0755);
491 delete[] dateDirName;
492 delete[] titleDirName;
493 delete[] newContainer;
498 else if ((statret == 0) && (! (dstat.st_mode && S_IFDIR))) // Something exists but it's not a dir
500 delete[] dateDirName;
501 delete[] titleDirName;
502 delete[] newContainer;
507 // Ok, the directory container has been made, or it pre-existed.
509 char* newDir = new char[strlen(newContainer) + 1 + strlen(dateDirName) + 1];
510 sprintf(newDir, "%s/%s", newContainer, dateDirName);
512 log->log("Client", Log::DEBUG, "doing rename '%s' '%s'", t, newDir);
513 int renameret = rename(t, newDir);
516 // Success. Test for remove old dir containter
517 char* oldTitleDir = new char[k+1];
518 memcpy(oldTitleDir, t, k);
519 oldTitleDir[k] = '\0';
520 log->log("Client", Log::DEBUG, "len: %i, cp: %i, strlen: %i, oldtitledir: %s", k+1, k, strlen(oldTitleDir), oldTitleDir);
521 rmdir(oldTitleDir); // can't do anything about a fail result at this point.
522 delete[] oldTitleDir;
527 #if VDRVERSNUM > 10311
529 ::Recordings.Update();
531 // Success. Send a different packet from just a ulong
532 int totalLength = 4 + 4 + strlen(newDir) + 1;
533 UCHAR* sendBuffer = new UCHAR[totalLength];
534 *(ULONG*)&sendBuffer[0] = htonl(totalLength - 4);
535 *(ULONG*)&sendBuffer[4] = htonl(1); // success
536 strcpy((char*)&sendBuffer[8], newDir);
537 tcp.sendPacket(sendBuffer, totalLength);
545 delete[] dateDirName;
546 delete[] titleDirName;
547 delete[] newContainer;
563 int MVPClient::processGetChannelsList(UCHAR* data, int length)
565 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
566 int count = 4; // leave space for the packet length
570 char* chanConfig = config.getValueString("General", "Channels");
572 if (chanConfig) allChans = strcasecmp(chanConfig, "FTA only");
574 for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
576 #if VDRVERSNUM < 10300
577 if (!channel->GroupSep() && (!channel->Ca() || allChans))
579 if (!channel->GroupSep() && (!channel->Ca(0) || allChans))
582 log->log("Client", Log::DEBUG, "name: '%s'", channel->Name());
584 if (channel->Vpid()) type = 1;
585 #if VDRVERSNUM < 10300
588 else if (channel->Apid(0)) type = 2;
592 if (count > 49000) break;
593 *(ULONG*)&sendBuffer[count] = htonl(channel->Number());
596 *(ULONG*)&sendBuffer[count] = htonl(type);
599 point = (char*)channel->Name();
600 strcpy((char*)&sendBuffer[count], point);
601 count += strlen(point) + 1;
605 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
607 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
609 tcp.sendPacket(sendBuffer, count);
611 log->log("Client", Log::DEBUG, "Written channels list");
616 int MVPClient::processGetChannelPids(UCHAR* data, int length)
618 ULONG channelNumber = ntohl(*(ULONG*)data);
620 cChannel* channel = channelFromNumber(channelNumber);
628 ULONG spaceRequired = 12; // 4 for length field, 4 for vpid, 4 for number of apids
629 // Work out space required and number of Apids
631 #if VDRVERSNUM < 10300
633 log->log("Client", Log::DEBUG, "Apid1: %i", channel->Apid1());
634 log->log("Client", Log::DEBUG, "Apid2: %i", channel->Apid2());
636 if (channel->Apid2())
639 spaceRequired += 10; // 8 + 2 nulls
641 else if (channel->Apid1())
644 spaceRequired += 5; // 4 + 1 null
653 for (const int *Apid = channel->Apids(); *Apid; Apid++)
655 spaceRequired += 4 + strlen(channel->Alang(numApids)) + 1; // 4 for pid, length of string + \0
661 // Format of response
669 UCHAR* sendBuffer = new UCHAR[spaceRequired];
671 *(ULONG*)&sendBuffer[point] = htonl(spaceRequired - 4); point += 4; // take off first 4 bytes
672 *(ULONG*)&sendBuffer[point] = htonl(channel->Vpid()); point += 4;
673 *(ULONG*)&sendBuffer[point] = htonl(numApids); point += 4;
675 #if VDRVERSNUM < 10300
678 *(ULONG*)&sendBuffer[point] = htonl(channel->Apid1()); point += 4;
679 sendBuffer[point] = '\0'; point += 1;
683 *(ULONG*)&sendBuffer[point] = htonl(channel->Apid2()); point += 4;
684 sendBuffer[point] = '\0'; point += 1;
687 for (ULONG i = 0; i < numApids; i++)
689 *(ULONG*)&sendBuffer[point] = htonl(channel->Apid(i)); point += 4;
690 strcpy((char*)&sendBuffer[point], channel->Alang(i)); point += strlen(channel->Alang(i)) + 1;
694 // printf("About to send getchannelpids response. length = %u\n", spaceRequired);
695 tcp.dump(sendBuffer, spaceRequired);
697 tcp.sendPacket(sendBuffer, spaceRequired);
699 log->log("Client", Log::DEBUG, "Written channels pids");
704 int MVPClient::processStartStreamingChannel(UCHAR* data, int length)
706 log->log("Client", Log::DEBUG, "length = %i", length);
707 ULONG channelNumber = ntohl(*(ULONG*)data);
709 cChannel* channel = channelFromNumber(channelNumber);
716 // get the priority we should use
718 int priority = config.getValueLong("General", "Live priority", &fail);
721 log->log("Client", Log::DEBUG, "Config: Live TV priority: %i", priority);
725 log->log("Client", Log::DEBUG, "Config: Live TV priority config fail");
730 if (priority < 0) priority = 0;
731 if (priority > 99) priority = 99;
733 log->log("Client", Log::DEBUG, "Using live TV priority %i", priority);
734 lp = MVPReceiver::create(channel, priority);
754 int MVPClient::processStopStreaming(UCHAR* data, int length)
756 log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED");
767 delete recordingManager;
769 recordingManager = NULL;
776 int MVPClient::processGetBlock(UCHAR* data, int length)
780 log->log("Client", Log::DEBUG, "Get block called when no streaming happening!");
784 ULLONG position = ntohll(*(ULLONG*)data);
785 data += sizeof(ULLONG);
786 ULONG amount = ntohl(*(ULONG*)data);
788 log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount);
790 UCHAR sendBuffer[amount + 4];
791 ULONG amountReceived = 0; // compiler moan.
794 log->log("Client", Log::DEBUG, "getting from live");
795 amountReceived = lp->getBlock(&sendBuffer[4], amount);
799 // vdr has possibly disconnected the receiver
800 log->log("Client", Log::DEBUG, "VDR has disconnected the live receiver");
807 log->log("Client", Log::DEBUG, "getting from recording");
808 amountReceived = rp->getBlock(&sendBuffer[4], position, amount);
814 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
818 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
819 tcp.sendPacket(sendBuffer, amountReceived + 4);
820 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);
826 int MVPClient::processStartStreamingRecording(UCHAR* data, int length)
828 // data is a pointer to the fileName string
830 recordingManager = new cRecordings;
831 recordingManager->Load();
833 cRecording* recording = recordingManager->GetByName((char*)data);
835 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
839 rp = new RecPlayer(recording);
841 UCHAR sendBuffer[16];
842 *(ULONG*)&sendBuffer[0] = htonl(12);
843 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
844 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
846 tcp.sendPacket(sendBuffer, 16);
847 log->log("Client", Log::DEBUG, "written totalLength");
851 delete recordingManager;
852 recordingManager = NULL;
857 int MVPClient::processPositionFromFrameNumber(UCHAR* data, int length)
861 ULONG frameNumber = ntohl(*(ULONG*)data);
866 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
870 retval = rp->positionFromFrameNumber(frameNumber);
873 UCHAR sendBuffer[12];
874 *(ULONG*)&sendBuffer[0] = htonl(8);
875 *(ULLONG*)&sendBuffer[4] = htonll(retval);
877 tcp.sendPacket(sendBuffer, 12);
878 log->log("Client", Log::DEBUG, "Wrote posFromFrameNum reply to client");
882 int MVPClient::processFrameNumberFromPosition(UCHAR* data, int length)
886 ULLONG position = ntohll(*(ULLONG*)data);
891 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
895 retval = rp->frameNumberFromPosition(position);
899 *(ULONG*)&sendBuffer[0] = htonl(4);
900 *(ULONG*)&sendBuffer[4] = htonl(retval);
902 tcp.sendPacket(sendBuffer, 8);
903 log->log("Client", Log::DEBUG, "Wrote frameNumFromPos reply to client");
907 int MVPClient::processGetIFrame(UCHAR* data, int length)
909 bool success = false;
911 ULONG frameNumber = ntohl(*(ULONG*)data);
913 ULONG direction = ntohl(*(ULONG*)data);
916 ULLONG rfilePosition = 0;
917 ULONG rframeNumber = 0;
918 ULONG rframeLength = 0;
922 log->log("Client", Log::DEBUG, "GetIFrame recording called when no recording being played!");
926 success = rp->getNextIFrame(frameNumber, direction, &rfilePosition, &rframeNumber, &rframeLength);
929 // returns file position, frame number, length
931 UCHAR sendBuffer[20];
937 *(ULONG*)&sendBuffer[0] = htonl(16);
938 *(ULLONG*)&sendBuffer[4] = htonll(rfilePosition);
939 *(ULONG*)&sendBuffer[12] = htonl(rframeNumber);
940 *(ULONG*)&sendBuffer[16] = htonl(rframeLength);
945 *(ULONG*)&sendBuffer[0] = htonl(4);
946 *(ULONG*)&sendBuffer[4] = 0;
949 log->log("Client", Log::DEBUG, "%llu %lu %lu", rfilePosition, rframeNumber, rframeLength);
951 tcp.sendPacket(sendBuffer, packetLength);
952 log->log("Client", Log::DEBUG, "Wrote GNIF reply to client");
956 int MVPClient::processGetChannelSchedule(UCHAR* data, int length)
958 ULONG channelNumber = ntohl(*(ULONG*)data);
960 ULONG startTime = ntohl(*(ULONG*)data);
962 ULONG duration = ntohl(*(ULONG*)data);
964 log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber);
966 cChannel* channel = channelFromNumber(channelNumber);
970 log->log("Client", Log::DEBUG, "written 0 because channel = NULL");
974 log->log("Client", Log::DEBUG, "Got channel");
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 log->log("Client", Log::DEBUG, "written 0 because Schedule!s! = NULL");
990 log->log("Client", Log::DEBUG, "Got schedule!s! object");
992 const cSchedule *Schedule = Schedules->GetSchedule(channel->GetChannelID());
996 log->log("Client", Log::DEBUG, "written 0 because Schedule = NULL");
1000 log->log("Client", Log::DEBUG, "Got schedule object");
1002 UCHAR* sendBuffer = (UCHAR*)malloc(100000);
1003 ULONG sendBufferLength = 100000;
1004 ULONG sendBufferUsed = sizeof(ULONG); // leave a hole for the entire packet length
1008 // assign all the event info to temp vars then we know exactly what size they are
1010 ULONG thisEventTime;
1011 ULONG thisEventDuration;
1012 const char* thisEventTitle;
1013 const char* thisEventSubTitle;
1014 const char* thisEventDescription;
1016 ULONG constEventLength = sizeof(thisEventID) + sizeof(thisEventTime) + sizeof(thisEventDuration);
1017 ULONG thisEventLength;
1019 #if VDRVERSNUM < 10300
1021 const cEventInfo *event;
1022 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1024 event = Schedule->GetEventNumber(eventNumber);
1026 thisEventID = event->GetEventID();
1027 thisEventTime = event->GetTime();
1028 thisEventDuration = event->GetDuration();
1029 thisEventTitle = event->GetTitle();
1030 thisEventSubTitle = event->GetSubtitle();
1031 thisEventDescription = event->GetExtendedDescription();
1035 for (const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event))
1037 thisEventID = event->EventID();
1038 thisEventTime = event->StartTime();
1039 thisEventDuration = event->Duration();
1040 thisEventTitle = event->Title();
1041 thisEventSubTitle = NULL;
1042 thisEventDescription = event->Description();
1046 log->log("Client", Log::DEBUG, "Got an event object %p", event);
1048 //in the past filter
1049 if ((thisEventTime + thisEventDuration) < (ULONG)time(NULL)) continue;
1052 if ((thisEventTime + thisEventDuration) <= startTime) continue;
1055 if (thisEventTime >= (startTime + duration)) continue;
1057 if (!thisEventTitle) thisEventTitle = empty;
1058 if (!thisEventSubTitle) thisEventSubTitle = empty;
1059 if (!thisEventDescription) thisEventDescription = empty;
1061 thisEventLength = constEventLength + strlen(thisEventTitle) + 1 + strlen(thisEventSubTitle) + 1 + strlen(thisEventDescription) + 1;
1063 log->log("Client", Log::DEBUG, "Done s1");
1065 // now extend the buffer if necessary
1066 if ((sendBufferUsed + thisEventLength) > sendBufferLength)
1068 log->log("Client", Log::DEBUG, "Extending buffer");
1069 sendBufferLength += 100000;
1070 UCHAR* temp = (UCHAR*)realloc(sendBuffer, sendBufferLength);
1074 UCHAR sendBuffer2[8];
1075 *(ULONG*)&sendBuffer2[0] = htonl(4);
1076 *(ULONG*)&sendBuffer2[4] = htonl(0);
1077 tcp.sendPacket(sendBuffer2, 8);
1078 log->log("Client", Log::DEBUG, "written 0 because failed to realloc packet");
1084 log->log("Client", Log::DEBUG, "Done s2");
1086 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventID); sendBufferUsed += sizeof(ULONG);
1087 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventTime); sendBufferUsed += sizeof(ULONG);
1088 *(ULONG*)&sendBuffer[sendBufferUsed] = htonl(thisEventDuration); sendBufferUsed += sizeof(ULONG);
1090 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventTitle); sendBufferUsed += strlen(thisEventTitle) + 1;
1091 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventSubTitle); sendBufferUsed += strlen(thisEventSubTitle) + 1;
1092 strcpy((char*)&sendBuffer[sendBufferUsed], thisEventDescription); sendBufferUsed += strlen(thisEventDescription) + 1;
1094 log->log("Client", Log::DEBUG, "Done s3 %lu", sendBufferUsed);
1097 log->log("Client", Log::DEBUG, "Got all event data");
1099 if (sendBufferUsed == sizeof(ULONG))
1103 log->log("Client", Log::DEBUG, "Written 0 because no data");
1107 // Write the length into the first 4 bytes. It's sendBufferUsed - 4 because of the hole!
1108 *(ULONG*)&sendBuffer[0] = htonl(sendBufferUsed - sizeof(ULONG));
1109 tcp.sendPacket(sendBuffer, sendBufferUsed);
1110 log->log("Client", Log::DEBUG, "written %lu schedules packet", sendBufferUsed);
1118 int MVPClient::processConfigSave(UCHAR* buffer, int length)
1120 char* section = (char*)buffer;
1124 for (int k = 0; k < length; k++)
1126 if (buffer[k] == '\0')
1130 key = (char*)&buffer[k+1];
1134 value = (char*)&buffer[k+1];
1140 // if the last string (value) doesnt have null terminator, give up
1141 if (buffer[length - 1] != '\0') return 0;
1143 log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value);
1144 if (config.setValueString(section, key, value))
1156 int MVPClient::processConfigLoad(UCHAR* buffer, int length)
1158 char* section = (char*)buffer;
1161 for (int k = 0; k < length; k++)
1163 if (buffer[k] == '\0')
1165 key = (char*)&buffer[k+1];
1170 char* value = config.getValueString(section, key);
1174 UCHAR sendBuffer[4 + strlen(value) + 1];
1175 *(ULONG*)&sendBuffer[0] = htonl(strlen(value) + 1);
1176 strcpy((char*)&sendBuffer[4], value);
1177 tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1);
1179 log->log("Client", Log::DEBUG, "Written config load packet");
1184 UCHAR sendBuffer[8];
1185 *(ULONG*)&sendBuffer[0] = htonl(4);
1186 *(ULONG*)&sendBuffer[4] = htonl(0);
1187 tcp.sendPacket(sendBuffer, 8);
1189 log->log("Client", Log::DEBUG, "Written config load failed packet");
1195 void MVPClient::cleanConfig()
1197 log->log("Client", Log::DEBUG, "Clean config");
1199 cRecordings Recordings;
1204 char* resumes = config.getSectionKeyNames("ResumeData", numReturns, length);
1205 char* position = resumes;
1206 for(int k = 0; k < numReturns; k++)
1208 log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position);
1210 cRecording* recording = Recordings.GetByName(position);
1213 // doesn't exist anymore
1214 log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore");
1215 config.deleteValue("ResumeData", position);
1219 log->log("Client", Log::DEBUG, "This recording still exists");
1222 position += strlen(position) + 1;
1234 event = Schedule->GetPresentEvent();
1236 fprintf(f, "\n\nCurrent event\n\n");
1238 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1239 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1240 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1241 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1243 event = Schedule->GetFollowingEvent();
1245 fprintf(f, "\n\nFollowing event\n\n");
1247 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", 0, event->GetEventID(), event->GetTime(), event->GetDuration());
1248 fprintf(f, "Event %i title = %s subtitle = %s\n", 0, event->GetTitle(), event->GetSubtitle());
1249 fprintf(f, "Event %i extendeddescription = %s\n", 0, event->GetExtendedDescription());
1250 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", 0, event->IsFollowing(), event->IsPresent());
1256 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1257 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1258 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1259 fprintf(f, "Event %i isFollowing = %i, isPresent = %i\n", eventNumber, event->IsFollowing(), event->IsPresent());
1267 void MVPClient::test2()
1269 FILE* f = fopen("/tmp/s.txt", "w");
1271 #if VDRVERSNUM < 10300
1272 cMutexLock MutexLock;
1273 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1275 cSchedulesLock MutexLock;
1276 const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
1281 fprintf(f, "Schedules = NULL\n");
1286 fprintf(f, "Schedules dump:\n");
1290 const cSchedule *Schedule;
1291 int scheduleNumber = 0;
1294 cChannel *thisChannel;
1296 #if VDRVERSNUM < 10300
1297 const cEventInfo *event;
1298 int eventNumber = 0;
1300 const cEvent *event;
1303 // Schedule = Schedules->GetSchedule(channel->GetChannelID());
1304 // Schedule = Schedules->GetSchedule();
1305 Schedule = Schedules->First();
1308 fprintf(f, "First Schedule = NULL\n");
1315 fprintf(f, "Schedule #%i\n", scheduleNumber);
1316 fprintf(f, "-------------\n\n");
1318 #if VDRVERSNUM < 10300
1319 tchid = Schedule->GetChannelID();
1321 tchid = Schedule->ChannelID();
1324 #if VDRVERSNUM < 10300
1325 fprintf(f, "ChannelID.ToString() = %s\n", tchid.ToString());
1326 fprintf(f, "NumEvents() = %i\n", Schedule->NumEvents());
1328 // put the count at the end.
1331 thisChannel = Channels.GetByChannelID(tchid, true);
1334 fprintf(f, "Channel Number: %p %i\n", thisChannel, thisChannel->Number());
1338 fprintf(f, "thisChannel = NULL for tchid\n");
1341 #if VDRVERSNUM < 10300
1342 for (eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1344 event = Schedule->GetEventNumber(eventNumber);
1345 fprintf(f, "Event %i tableid = %i timestring = %s endtimestring = %s\n", eventNumber, event->GetTableID(), event->GetTimeString(), event->GetEndTimeString());
1346 fprintf(f, "Event %i date = %s isfollowing = %i ispresent = %i\n", eventNumber, event->GetDate(), event->IsFollowing(), event->IsPresent());
1347 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1348 fprintf(f, "Event %i subtitle = %s title = %s\n", eventNumber, event->GetSubtitle(), event->GetTitle());
1349 fprintf(f, "Event %i eventid = %u duration = %li time = %lu channelnumber = %i\n", eventNumber, event->GetEventID(), event->GetDuration(), event->GetTime(), event->GetChannelNumber());
1350 fprintf(f, "Event %u dump:\n", eventNumber);
1355 // This whole section needs rewriting to walk the list.
1356 event = Schedule->Events()->First();
1358 event = Schedule->Events()->Next(event);
1363 fprintf(f, "\nDump from object:\n");
1365 fprintf(f, "\nEND\n");
1375 fprintf(f, "End of current Schedule\n\n\n");
1377 Schedule = (const cSchedule *)Schedules->Next(Schedule);
1391 const cEventInfo *GetPresentEvent(void) const;
1392 const cEventInfo *GetFollowingEvent(void) const;
1393 const cEventInfo *GetEvent(unsigned short uEventID, time_t tTime = 0) const;
1394 const cEventInfo *GetEventAround(time_t tTime) const;
1395 const cEventInfo *GetEventNumber(int n) const { return Events.Get(n); }
1398 const unsigned char GetTableID(void) const;
1399 const char *GetTimeString(void) const;
1400 const char *GetEndTimeString(void) const;
1401 const char *GetDate(void) const;
1402 bool IsFollowing(void) const;
1403 bool IsPresent(void) const;
1404 const char *GetExtendedDescription(void) const;
1405 const char *GetSubtitle(void) const;
1406 const char *GetTitle(void) const;
1407 unsigned short GetEventID(void) const;
1408 long GetDuration(void) const;
1409 time_t GetTime(void) const;
1410 tChannelID GetChannelID(void) const;
1411 int GetChannelNumber(void) const { return nChannelNumber; }
1412 void SetChannelNumber(int ChannelNumber) const { ((cEventInfo *)this)->nChannelNumber = ChannelNumber; } // doesn't modify the EIT data, so it's ok to make it 'const'
1413 void Dump(FILE *f, const char *Prefix = "") const;
1419 void MVPClient::test(int channelNumber)
1421 FILE* f = fopen("/tmp/test.txt", "w");
1423 cMutexLock MutexLock;
1424 const cSchedules *Schedules = cSIProcessor::Schedules(MutexLock);
1428 fprintf(f, "Schedules = NULL\n");
1433 fprintf(f, "Schedules dump:\n");
1434 // Schedules->Dump(f);
1436 const cSchedule *Schedule;
1437 cChannel *thisChannel;
1438 const cEventInfo *event;
1440 thisChannel = channelFromNumber(channelNumber);
1443 fprintf(f, "thisChannel = NULL\n");
1448 Schedule = Schedules->GetSchedule(thisChannel->GetChannelID());
1449 // Schedule = Schedules->GetSchedule();
1450 // Schedule = Schedules->First();
1453 fprintf(f, "First Schedule = NULL\n");
1458 fprintf(f, "NumEvents() = %i\n\n", Schedule->NumEvents());
1460 // For some channels VDR seems to pick a random point in time to
1461 // start dishing out events, but they are in order
1462 // at some point in the list the time snaps to the current event
1467 for (int eventNumber = 0; eventNumber < Schedule->NumEvents(); eventNumber++)
1469 event = Schedule->GetEventNumber(eventNumber);
1470 fprintf(f, "Event %i eventid = %u time = %lu duration = %li\n", eventNumber, event->GetEventID(), event->GetTime(), event->GetDuration());
1471 fprintf(f, "Event %i title = %s subtitle = %s\n", eventNumber, event->GetTitle(), event->GetSubtitle());
1472 fprintf(f, "Event %i extendeddescription = %s\n", eventNumber, event->GetExtendedDescription());
1476 fprintf(f, "\nEND\n");
1490 Schedules = the collection of all the Schedule objects
1491 Schedule = One schedule, contants all the events for a channel
1492 Event = One programme
1501 Subtitle (used for "Programmes resume at ...")
1504 IsPresent ? easy to work out tho. Oh it doesn't always work
1509 void MVPClient::test2()
1511 log->log("-", Log::DEBUG, "Timers List");
1513 for (int i = 0; i < Timers.Count(); i++)
1515 cTimer *timer = Timers.Get(i);
1516 //Reply(i < Timers.Count() - 1 ? -250 : 250, "%d %s", timer->Index() + 1, timer->ToText());
1517 log->log("-", Log::DEBUG, "i=%i count=%i index=%d", i, Timers.Count(), timer->Index() + 1);
1518 #if VDRVERSNUM < 10300
1519 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());
1521 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());
1523 log->log("-", Log::DEBUG, "channel=%i file=%s summary=%s", timer->Channel()->Number(), timer->File(), timer->Summary());
1524 log->log("-", Log::DEBUG, "");
1527 // asprintf(&buffer, "%d:%s:%s :%04d:%04d:%d:%d:%s:%s\n",
1528 // active, (UseChannelID ? Channel()->GetChannelID().ToString() : itoa(Channel()->Number())),
1529 // PrintDay(day, firstday), start, stop, priority, lifetime, file, summary ? summary : "");
1534 Active seems to be a bool - whether the timer should be done or not. If set to inactive it stays around after its time
1535 recording is a bool, 0 for not currently recording, 1 for currently recording
1536 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
1540 int MVPClient::processGetTimers(UCHAR* buffer, int length)
1542 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1543 int count = 4; // leave space for the packet length
1545 const char* fileName;
1547 int numTimers = Timers.Count();
1549 *(ULONG*)&sendBuffer[count] = htonl(numTimers); count += 4;
1551 for (int i = 0; i < numTimers; i++)
1553 if (count > 49000) break;
1555 timer = Timers.Get(i);
1557 #if VDRVERSNUM < 10300
1558 *(ULONG*)&sendBuffer[count] = htonl(timer->Active()); count += 4;
1560 *(ULONG*)&sendBuffer[count] = htonl(timer->HasFlags(tfActive)); count += 4;
1562 *(ULONG*)&sendBuffer[count] = htonl(timer->Recording()); count += 4;
1563 *(ULONG*)&sendBuffer[count] = htonl(timer->Pending()); count += 4;
1564 *(ULONG*)&sendBuffer[count] = htonl(timer->Priority()); count += 4;
1565 *(ULONG*)&sendBuffer[count] = htonl(timer->Lifetime()); count += 4;
1566 *(ULONG*)&sendBuffer[count] = htonl(timer->Channel()->Number()); count += 4;
1567 *(ULONG*)&sendBuffer[count] = htonl(timer->StartTime()); count += 4;
1568 *(ULONG*)&sendBuffer[count] = htonl(timer->StopTime()); count += 4;
1570 fileName = timer->File();
1571 strcpy((char*)&sendBuffer[count], fileName);
1572 count += strlen(fileName) + 1;
1575 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1577 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1579 tcp.sendPacket(sendBuffer, count);
1580 delete[] sendBuffer;
1581 log->log("Client", Log::DEBUG, "Written timers list");
1586 int MVPClient::processSetTimer(UCHAR* buffer, int length)
1588 char* timerString = new char[strlen((char*)buffer) + 1];
1589 strcpy(timerString, (char*)buffer);
1591 #if VDRVERSNUM < 10300
1593 // If this is VDR 1.2 the date part of the timer string must be reduced
1594 // to just DD rather than YYYY-MM-DD
1596 int s = 0; // source
1597 int d = 0; // destination
1599 while(c != 2) // copy up to date section, including the second ':'
1601 timerString[d] = buffer[s];
1602 if (buffer[s] == ':') c++;
1606 // now it has copied up to the date section
1608 while(c != 2) // waste YYYY-MM-
1610 if (buffer[s] == '-') c++;
1613 // now source is at the DD
1614 memcpy(&timerString[d], &buffer[s], length - s);
1616 timerString[d] = '\0';
1618 log->log("Client", Log::DEBUG, "Timer string after 1.2 conversion:");
1619 log->log("Client", Log::DEBUG, "%s", timerString);
1623 cTimer *timer = new cTimer;
1624 if (timer->Parse((char*)timerString))
1626 cTimer *t = Timers.GetTimer(timer);
1630 #if VDRVERSNUM < 10300
1633 Timers.SetModified();
1651 void MVPClient::incClients()
1653 pthread_mutex_lock(&threadClientMutex);
1654 MVPClient::nr_clients++;
1655 pthread_mutex_unlock(&threadClientMutex);
1658 void MVPClient::decClients()
1660 pthread_mutex_lock(&threadClientMutex);
1661 MVPClient::nr_clients--;
1662 pthread_mutex_unlock(&threadClientMutex);
1665 int MVPClient::getNrClients()
1668 pthread_mutex_lock(&threadClientMutex);
1669 nrClients = MVPClient::nr_clients;
1670 pthread_mutex_unlock(&threadClientMutex);
1674 int MVPClient::processGetRecInfo(UCHAR* data, int length)
1676 // data is a pointer to the fileName string
1678 cRecordings Recordings;
1679 Recordings.Load(); // probably have to do this
1681 cRecording *recording = Recordings.GetByName((char*)data);
1683 time_t timerStart = 0;
1684 time_t timerStop = 0;
1685 char* summary = NULL;
1686 ULONG resumePoint = 0;
1690 log->log("Client", Log::ERR, "GetRecInfo found no recording");
1695 ULONG sendBufferSize = 10000;
1696 UCHAR* sendBuffer = (UCHAR*)malloc(sendBufferSize);
1697 ULONG pos = 4; // leave first 4 bytes for size field
1701 4 bytes: start time for timer
1702 4 bytes: end time for timer
1703 4 bytes: resume point
1705 4 bytes: num components
1715 // Get current timer
1717 cRecordControl *rc = cRecordControls::GetRecordControl(recording->FileName());
1720 timerStart = rc->Timer()->StartTime();
1721 timerStop = rc->Timer()->StopTime();
1722 log->log("Client", Log::DEBUG, "GRI: RC: %lu %lu", timerStart, timerStop);
1725 *(time_t*)&sendBuffer[pos] = htonl(timerStart); pos += 4;
1726 *(time_t*)&sendBuffer[pos] = htonl(timerStop); pos += 4;
1730 char* value = config.getValueString("ResumeData", (char*)data);
1733 resumePoint = strtoul(value, NULL, 10);
1736 log->log("Client", Log::DEBUG, "GRI: RP: %lu", resumePoint);
1738 *(ULONG*)&sendBuffer[pos] = htonl(resumePoint); pos += 4;
1743 #if VDRVERSNUM < 10300
1744 summary = (char*)recording->Summary();
1746 const cRecordingInfo *Info = recording->Info();
1747 summary = (char*)Info->ShortText();
1748 if (isempty(summary)) summary = (char*)Info->Description();
1750 log->log("Client", Log::DEBUG, "GRI: S: %s", summary);
1753 // memory insanity...
1754 if ((sendBufferSize - pos) < (strlen(summary) + 500)) // random
1756 UCHAR* newBuffer = (UCHAR*)realloc(sendBuffer, sendBufferSize + strlen(summary) + 10000);
1759 sendBuffer = newBuffer;
1760 sendBufferSize += strlen(summary) + 10000;
1770 strcpy((char*)&sendBuffer[pos], summary);
1771 pos += strlen(summary) + 1;
1775 strcpy((char*)&sendBuffer[pos], "");
1782 #if VDRVERSNUM < 10300
1784 // Send 0 for numchannels - this signals the client this info is not available
1785 *(ULONG*)&sendBuffer[pos] = 0; pos += 4;
1788 const cComponents* components = Info->Components();
1790 log->log("Client", Log::DEBUG, "GRI: D1: %p", components);
1794 *(ULONG*)&sendBuffer[pos] = htonl(0); pos += 4;
1798 *(ULONG*)&sendBuffer[pos] = htonl(components->NumComponents()); pos += 4;
1800 tComponent* component;
1801 for (int i = 0; i < components->NumComponents(); i++)
1803 component = components->Component(i);
1805 // memory insanity...
1806 ULONG extraNeeded = 2 + (component->language ? strlen(component->language) : 0)
1807 + (component->description ? strlen(component->description) : 0) + 2;
1809 if ((sendBufferSize - pos) < extraNeeded)
1811 UCHAR* newBuffer = (UCHAR*)realloc(sendBuffer, sendBufferSize + extraNeeded + 10000);
1814 sendBuffer = newBuffer;
1815 sendBufferSize += extraNeeded + 10000;
1825 log->log("Client", Log::DEBUG, "GRI: C: %i %u %u %s %s", i, component->stream, component->type, component->language, component->description);
1826 sendBuffer[pos] = component->stream; pos += 1;
1827 sendBuffer[pos] = component->type; pos += 1;
1828 if (component->language)
1830 strcpy((char*)&sendBuffer[pos], component->language);
1831 pos += strlen(component->language) + 1;
1835 strcpy((char*)&sendBuffer[pos], "");
1838 if (component->description)
1840 strcpy((char*)&sendBuffer[pos], component->description);
1841 pos += strlen(component->description) + 1;
1845 strcpy((char*)&sendBuffer[pos], "");
1856 *(ULONG*)&sendBuffer[0] = htonl(pos - 4); // -4 : take off the size field
1858 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1860 tcp.sendPacket(sendBuffer, pos);
1861 delete[] sendBuffer;
1862 log->log("Client", Log::DEBUG, "Written getrecinfo");
1872 int MVPClient::processReScanRecording(UCHAR* data, int length)
1876 log->log("Client", Log::DEBUG, "Rescan recording called when no recording being played!");
1882 UCHAR sendBuffer[16];
1883 *(ULONG*)&sendBuffer[0] = htonl(12);
1884 *(ULLONG*)&sendBuffer[4] = htonll(rp->getLengthBytes());
1885 *(ULONG*)&sendBuffer[12] = htonl(rp->getLengthFrames());
1887 tcp.sendPacket(sendBuffer, 16);
1888 log->log("Client", Log::DEBUG, "Rescan recording, wrote new length to client");
1892 // FIXME without client calling rescan, getblock wont work even tho more data is avail
1895 int MVPClient::processGetMarks(UCHAR* data, int length)
1897 // data is a pointer to the fileName string
1899 UCHAR* sendBuffer = new UCHAR[50000]; // FIXME hope this is enough
1900 int count = 4; // leave space for the packet length
1904 cRecordings Recordings;
1905 Recordings.Load(); // probably have to do this
1907 cRecording *recording = Recordings.GetByName((char*)data);
1909 log->log("Client", Log::DEBUG, "recording pointer %p", recording);
1913 Marks.Load(recording->FileName());
1916 for (const cMark *m = Marks.First(); m; m = Marks.Next(m))
1918 log->log("Client", Log::DEBUG, "found Mark %i", m->position);
1920 if (count > 49000) break;
1921 *(ULONG*)&sendBuffer[count] = htonl(m->position);
1927 log->log("Client", Log::DEBUG, "no marks found, sending 0-mark");
1928 *(ULONG*)&sendBuffer[count] = htonl(0);
1933 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
1935 log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(ULONG*)&sendBuffer[0]));
1937 tcp.sendPacket(sendBuffer, count);
1938 delete[] sendBuffer;
1939 log->log("Client", Log::DEBUG, "Written Marks list");
1946 * media List Request:
1948 * 4 VDR_GETMEDIALIST
1949 * 4 flags (currently unused)
1952 * Media List response:
1960 * 4 strlen (incl. 0 Byte)
1964 #define MLISTBUF 500000
1965 int MVPClient::processGetMediaList(UCHAR* data, int length)
1968 log->log("Client", Log::ERR, "getMediaList packet too short %d", length);
1971 char * dirname=NULL;
1973 //we have a dirname provided
1974 dirname=(char *)&data[4];
1975 log->log("Client", Log::DEBUG, "getMediaList for %s", dirname);
1979 UCHAR* sendBuffer = new UCHAR[MLISTBUF]; // FIXME hope this is enough
1980 int count = 4; // leave space for the header
1982 MediaList * ml=MediaList::readList(baseConfig,dirname);
1984 log->log("Client", Log::ERR, "getMediaList returned NULL");
1987 //response code (not yet set)
1988 *(ULONG*)&sendBuffer[count] = htonl(0);
1991 *(ULONG*)&sendBuffer[count] = htonl(ml->Count());
1993 for (int nm=0;nm<ml->Count() && count < (MLISTBUF-1000);nm++) {
1994 Media *m=ml->Get(nm);
1995 log->log("Client", Log::DEBUG, "found media entry %s, type=%d",m->getFilename(),m->getType());
1996 *(ULONG*)&sendBuffer[count] = htonl(m->getType());
1999 *(ULONG*)&sendBuffer[count] = htonl(m->getTime());
2002 *(ULONG*)&sendBuffer[count] = htonl(0);
2004 int len=strlen(m->getFilename());
2006 *(ULONG*)&sendBuffer[count] = htonl(len+1);
2008 //should have a check for strlen > 1000...
2009 strcpy((char *)&sendBuffer[count],m->getFilename());
2014 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
2016 log->log("Client", Log::DEBUG, "getMediaList size %u", ntohl(*(ULONG*)&sendBuffer[0]));
2018 tcp.sendPacket(sendBuffer, count);
2019 delete[] sendBuffer;
2020 log->log("Client", Log::DEBUG, "Written Media list");
2026 * get image Request:
2029 * 4 flags (currently unused)
2034 * get image response:
2039 #define MLISTBUF 500000
2040 int MVPClient::processGetPicture(UCHAR* data, int length)
2043 log->log("Client", Log::ERR, "getPicture packet too short %d", length);
2050 char * filename=NULL;
2052 //we have a dirname provided
2053 filename=(char *)&data[4];
2054 log->log("Client", Log::DEBUG, "getPicture %s", filename);
2057 log->log("Client", Log::ERR, "getPicture empty filename");
2060 imageFile=fopen(filename,"r");
2061 if (!imageFile) log->log("Client", Log::ERR, "getPicture unable to open %s",filename);
2066 if ( fstat(fileno(imageFile),&st) == 0) size=st.st_size;
2068 UCHAR* sendBuffer = new UCHAR[12];
2069 int count = 4; // leave space for the header
2070 //response code (not yet set)
2071 *(ULONG*)&sendBuffer[count] = htonl(31);
2074 *(ULONG*)&sendBuffer[count] = htonl(size);
2076 *(ULONG*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field
2077 log->log("Client", Log::DEBUG, "getPicture size %u", size);
2079 tcp.sendPacket(sendBuffer, count);
2080 delete[] sendBuffer;
2081 log->log("Client", Log::DEBUG, "Written Media list");
2087 int MVPClient::processGetImageBlock(UCHAR* data, int length)
2091 log->log("Client", Log::DEBUG, "Get image block called when no image active");
2095 ULLONG position = ntohll(*(ULLONG*)data);
2096 data += sizeof(ULLONG);
2097 ULONG amount = ntohl(*(ULONG*)data);
2099 log->log("Client", Log::DEBUG, "getImageblock pos = %llu length = %lu", position, amount);
2101 UCHAR sendBuffer[amount + 4];
2102 ULONG amountReceived = 0; // compiler moan.
2103 ULLONG cpos=ftell(imageFile);
2104 if (position != cpos) {
2105 fseek(imageFile,position-cpos,SEEK_CUR);
2107 if (position != (ULLONG)ftell(imageFile)) {
2108 log->log("Client", Log::DEBUG, "getImageblock pos = %llu not available", position);
2111 amountReceived=fread(&sendBuffer[4],1,amount,imageFile);
2114 if (!amountReceived)
2117 log->log("Client", Log::DEBUG, "written 4(0) as getblock got 0");
2121 *(ULONG*)&sendBuffer[0] = htonl(amountReceived);
2122 tcp.sendPacket(sendBuffer, amountReceived + 4);
2123 log->log("Client", Log::DEBUG, "written ok %lu", amountReceived);