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
23 VDR* VDR::instance = NULL;
26 #define MUTEX_LOCK(mutex) pthread_mutex_lock(mutex)
27 #define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex)
29 #define MUTEX_LOCK(mutex) WaitForSingleObject(*(mutex), INFINITE )
30 #define MUTEX_UNLOCK(mutex) ReleaseMutex(*(mutex))
42 pthread_mutex_init(&mutex, NULL);
44 mutex=CreateMutex(NULL,FALSE,NULL);
58 if (initted) shutdown();
61 VDR* VDR::getInstance()
66 int VDR::init(int tport)
68 if (initted) return 0;
71 logger = Log::getInstance();
77 if (!initted) return 0;
83 void VDR::findServers(vector<VDRServer>& servers)
86 char* message = "VOMP";
88 DatagramSocket ds(port);
89 int haveAtLeastOne = 0;
98 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
99 ds.send("255.255.255.255", 3024, message, strlen(message));
101 retval = ds.waitforMessage(waitType);
103 if (retval == 2) // we got a reply
105 if (!strcmp(ds.getData(), "VOMP")) // echo.....
112 newServer.ip = new char[16];
113 strcpy(newServer.ip, ds.getFromIPA());
115 if (ds.getDataLength() == 0)
117 newServer.name = new char[1];
118 newServer.name[0] = '\0';
122 newServer.name = new char[strlen(ds.getData())+1];
123 strcpy(newServer.name, ds.getData());
126 servers.push_back(newServer);
133 if (haveAtLeastOne) break;
137 sort(servers.begin(), servers.end(), ServerSorter());
140 void VDR::cancelFindingServer()
145 void VDR::setServerIP(char* newIP)
147 strcpy(serverIP, newIP);
154 if (tcp->connectTo(serverIP, 3024))
165 void VDR::disconnect()
170 Log::getInstance()->log("VDR", Log::DEBUG, "Disconnect");
173 void VDR::setReceiveWindow(size_t size)
175 if (connected) tcp->setReceiveWindow(size);
178 ///////////////////////////////////////////////////////
182 packet = (UCHAR*)tcp->receivePacket();
188 packetLength = tcp->getDataLength();
192 void VDR::freePacket()
194 // Must be called if getPacket return 1, except in getBlock
201 int VDR::serverError()
203 if ((packetPos == 0) && (packetLength == 4) && !ntohl(*(ULONG*)packet)) return 1;
207 char* VDR::extractString()
209 if (serverError()) return NULL;
211 int length = strlen((char*)&packet[packetPos]);
212 if ((packetPos + length) > packetLength) return NULL;
213 char* str = new char[length + 1];
214 strcpy(str, (char*)&packet[packetPos]);
215 packetPos += length + 1;
219 ULONG VDR::extractULONG()
221 if ((packetPos + sizeof(ULONG)) > packetLength) return 0;
222 ULONG ul = ntohl(*(ULONG*)&packet[packetPos]);
223 packetPos += sizeof(ULONG);
227 ULLONG VDR::extractULLONG()
229 if ((packetPos + sizeof(ULLONG)) > packetLength) return 0;
230 ULLONG ull = ntohll(*(ULLONG*)&packet[packetPos]);
231 packetPos += sizeof(ULLONG);
235 long VDR::extractLONG()
237 if ((packetPos + sizeof(long)) > packetLength) return 0;
238 long l = ntohl(*(long*)&packet[packetPos]);
239 packetPos += sizeof(long);
243 /////////////////////////////////////////////////////////////////////////////
249 *(unsigned long*)&buffer[0] = htonl(10);
250 *(unsigned long*)&buffer[4] = htonl(VDR_LOGIN);
252 tcp->getMAC((char*)&buffer[8]);
258 MUTEX_UNLOCK(&mutex);
262 int a = tcp->sendPacket(buffer, 14);
266 MUTEX_UNLOCK(&mutex);
274 MUTEX_UNLOCK(&mutex);
278 ULONG vdrTime = extractULONG();
279 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
280 long vdrTimeOffset = extractLONG();
281 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
284 MUTEX_UNLOCK(&mutex);
286 // Set the time and zone on the MVP
289 struct timespec currentTime;
290 currentTime.tv_sec = vdrTime;
291 currentTime.tv_nsec = 0;
292 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
294 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
296 // now make a TZ variable and set it
300 if (vdrTimeOffset > 0) sign = '-';
303 vdrTimeOffset = abs(vdrTimeOffset);
305 hours = (int)vdrTimeOffset / 3600;
306 minutes = vdrTimeOffset % 3600;
308 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
310 minutes = (int)minutes / 60;
312 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
315 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
316 setenv("TZ", newTZ, 1);
318 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
324 Directory* VDR::getRecordingsList()
328 *(unsigned long*)&buffer[0] = htonl(4);
329 *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST);
334 MUTEX_UNLOCK(&mutex);
338 int a = tcp->sendPacket(buffer, 8);
342 MUTEX_UNLOCK(&mutex);
350 MUTEX_UNLOCK(&mutex);
354 Directory* recDir = new Directory();
355 recDir->setName("/");
358 Directory::totalSpace = extractULONG();
359 Directory::freeSpace = extractULONG();
360 Directory::usedPercent = extractULONG();
364 while (packetPos < packetLength)
366 Recording* rec = new Recording();
368 rec->start = extractULONG();
370 string = extractString();
371 rec->setName(string);
374 rec->fileName = extractString();
378 char* dirName = rec->getDirName();
380 Directory* d = recDir->getDirByName(dirName);
385 // Log::getInstance()->log("VDR", Log::DEBUG, "Added a new directory = %s", d->name);
386 recDir->dirList.push_back(d);
389 d->recList.push_back(rec);
393 recDir->recList.push_back(rec);
396 // Log::getInstance()->log("VDR", Log::DEBUG, "%s", rec->fileName);
400 MUTEX_UNLOCK(&mutex);
401 // Sort the directory order
402 sort(recDir->dirList.begin(), recDir->dirList.end(), DirectorySorter());
404 // Sort all the sub lists
407 DirectoryList::iterator i;
408 for (i = recDir->dirList.begin(); i != recDir->dirList.end(); i++)
411 sort(sortDir->recList.begin(), sortDir->recList.end(), RecordingSorter());
414 // Sort the root level list
415 sort(recDir->recList.begin(), recDir->recList.end(), RecordingSorter());
420 int VDR::deleteRecording(char* fileName)
422 unsigned long totalLength = 8 + strlen(fileName) + 1;
423 UCHAR* buffer = new UCHAR[totalLength];
425 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
426 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
427 strcpy((char*)&buffer[8], fileName);
430 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
432 unsigned int a = tcp->sendPacket(buffer, totalLength);
435 if (a != totalLength)
438 MUTEX_UNLOCK(&mutex);
444 MUTEX_UNLOCK(&mutex);
448 int toReturn = (int)extractULONG();
450 MUTEX_UNLOCK(&mutex);
455 char* VDR::getRecordingSummary(char* fileName)
457 unsigned long totalLength = 8 + strlen(fileName) + 1;
458 UCHAR* buffer = new UCHAR[totalLength];
460 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
461 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
462 strcpy((char*)&buffer[8], fileName);
465 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
467 unsigned int a = tcp->sendPacket(buffer, totalLength);
470 if (a != totalLength)
473 MUTEX_UNLOCK(&mutex);
479 MUTEX_UNLOCK(&mutex);
482 char* toReturn = extractString();
484 MUTEX_UNLOCK(&mutex);
489 ChannelList* VDR::getChannelsList(ULONG type)
493 *(unsigned long*)&buffer[0] = htonl(4);
494 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
497 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
499 int a = tcp->sendPacket(buffer, 8);
503 MUTEX_UNLOCK(&mutex);
511 MUTEX_UNLOCK(&mutex);
515 ChannelList* chanList = new ChannelList();
517 while (packetPos < packetLength)
519 Channel* chan = new Channel();
520 chan->number = extractULONG();
521 chan->type = extractULONG();
522 chan->name = extractString();
524 if (chan->type == type)
526 chanList->push_back(chan);
527 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
536 MUTEX_UNLOCK(&mutex);
541 int VDR::streamChannel(ULONG number)
545 *(unsigned long*)&buffer[0] = htonl(8);
546 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
547 *(unsigned long*)&buffer[8] = htonl(number);
550 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
552 int a = tcp->sendPacket(buffer, 12);
557 MUTEX_UNLOCK(&mutex);
563 MUTEX_UNLOCK(&mutex);
567 int toReturn = (int)extractULONG();
569 MUTEX_UNLOCK(&mutex);
574 int VDR::stopStreaming()
578 *(unsigned long*)&buffer[0] = htonl(4);
579 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
582 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
584 int a = tcp->sendPacket(buffer, 8);
589 MUTEX_UNLOCK(&mutex);
595 MUTEX_UNLOCK(&mutex);
599 int toReturn = (int)extractULONG();
601 MUTEX_UNLOCK(&mutex);
606 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
610 *(unsigned long*)&buffer[0] = htonl(16);
611 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
612 *(ULLONG*)&buffer[8] = htonll(position);
613 *(unsigned long*)&buffer[16] = htonl(maxAmount);
616 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
618 int a = tcp->sendPacket(buffer, 20);
622 MUTEX_UNLOCK(&mutex);
628 MUTEX_UNLOCK(&mutex);
634 Log::getInstance()->log("VDR", Log::DEBUG, "Detected getblock 0");
636 MUTEX_UNLOCK(&mutex);
640 UCHAR* toReturn = packet;
641 *amountReceived = packetLength;
642 // Manually clean up instead of running freePacket to keep the block
646 MUTEX_UNLOCK(&mutex);
652 ULLONG VDR::streamRecording(Recording* rec)
654 unsigned long totalLength = 8 + strlen(rec->fileName) + 1;
655 UCHAR* buffer = new UCHAR[totalLength];
657 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
658 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
659 strcpy((char*)&buffer[8], rec->fileName);
662 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
664 unsigned int a = tcp->sendPacket(buffer, totalLength);
667 if (a != totalLength)
670 MUTEX_UNLOCK(&mutex);
676 MUTEX_UNLOCK(&mutex);
680 ULLONG recordingLength = extractULLONG();
682 MUTEX_UNLOCK(&mutex);
684 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
686 return recordingLength;
689 ULLONG VDR::rescanRecording()
691 unsigned long totalLength = 8;
692 UCHAR* buffer = new UCHAR[totalLength];
694 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
695 *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
698 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
700 unsigned int a = tcp->sendPacket(buffer, totalLength);
703 if (a != totalLength)
706 MUTEX_UNLOCK(&mutex);
712 MUTEX_UNLOCK(&mutex);
716 ULLONG recordingLength = extractULLONG();
718 MUTEX_UNLOCK(&mutex);
720 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
722 return recordingLength;
725 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
727 unsigned long totalLength = 12;
728 UCHAR* buffer = new UCHAR[totalLength];
730 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
731 *(unsigned long*)&buffer[4] = htonl(VDR_POSFROMFRAME);
732 *(unsigned long*)&buffer[8] = htonl(frameNumber);
735 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
737 unsigned int a = tcp->sendPacket(buffer, totalLength);
740 if (a != totalLength)
743 MUTEX_UNLOCK(&mutex);
749 MUTEX_UNLOCK(&mutex);
753 ULLONG position = extractULLONG();
755 MUTEX_UNLOCK(&mutex);
757 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
762 ULONG VDR::frameNumberFromPosition(ULLONG position)
764 unsigned long totalLength = 16;
765 UCHAR* buffer = new UCHAR[totalLength];
767 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
768 *(unsigned long*)&buffer[4] = htonl(VDR_FRAMEFROMPOS);
769 *(ULLONG*)&buffer[8] = htonll(position);
772 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
774 unsigned int a = tcp->sendPacket(buffer, totalLength);
777 if (a != totalLength)
780 MUTEX_UNLOCK(&mutex);
786 MUTEX_UNLOCK(&mutex);
790 ULONG framenumber = extractULONG();
792 MUTEX_UNLOCK(&mutex);
794 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
799 EventList* VDR::getChannelSchedule(ULONG number)
803 return getChannelSchedule(number, now, 24 * 60 * 60);
806 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
808 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
811 *(unsigned long*)&buffer[0] = htonl(16);
812 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
813 *(unsigned long*)&buffer[8] = htonl(number);
814 *(unsigned long*)&buffer[12] = htonl(start);
815 *(unsigned long*)&buffer[16] = htonl(duration);
818 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
820 int a = tcp->sendPacket(buffer, 20);
825 MUTEX_UNLOCK(&mutex);
831 MUTEX_UNLOCK(&mutex);
835 // received a ulong(0) - schedules error in the plugin
839 MUTEX_UNLOCK(&mutex);
843 EventList* eventList = new EventList();
845 while (packetPos < packetLength)
847 Event* event = new Event();
848 event->id = extractULONG();
849 event->time = extractULONG();
850 event->duration = extractULONG();
851 event->title = extractString();
852 event->subtitle = extractString();
853 event->description = extractString();
854 eventList->push_back(event);
855 // eventList->next();
859 MUTEX_UNLOCK(&mutex);
861 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
866 Log* l = Log::getInstance();
869 l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
872 for(eventList->reset(); !eventList->eol(); eventList->next())
874 currentEvent = (Event*)eventList->getCurrent();
875 l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
882 ULLONG VDR::getResumePoint(char* fileName)
884 if (!connected) return 0;
886 char* resumeString = configLoad("ResumeData", fileName);
887 if (!resumeString) return 0;
889 ULLONG toReturn = STRTOULL(resumeString, NULL, 10);
890 delete[] resumeString;
894 int VDR::configSave(char* section, char* key, const char* value)
896 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
897 UCHAR* buffer = new UCHAR[totalLength];
899 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
900 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
903 strcpy((char*)&buffer[position], section);
904 position += strlen(section) + 1;
905 strcpy((char*)&buffer[position], key);
906 position += strlen(key) + 1;
907 strcpy((char*)&buffer[position], value);
910 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
912 unsigned int a = tcp->sendPacket(buffer, totalLength);
915 if (a != totalLength)
918 MUTEX_UNLOCK(&mutex);
924 MUTEX_UNLOCK(&mutex);
928 int toReturn = (int)extractULONG();
930 MUTEX_UNLOCK(&mutex);
935 char* VDR::configLoad(char* section, char* key)
937 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
938 UCHAR* buffer = new UCHAR[totalLength];
940 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
941 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
944 strcpy((char*)&buffer[position], section);
945 position += strlen(section) + 1;
946 strcpy((char*)&buffer[position], key);
949 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
951 unsigned int a = tcp->sendPacket(buffer, totalLength);
954 if (a != totalLength)
957 MUTEX_UNLOCK(&mutex);
963 MUTEX_UNLOCK(&mutex);
966 char* toReturn = extractString();
968 MUTEX_UNLOCK(&mutex);
973 RecTimerList* VDR::getRecTimersList()
977 *(unsigned long*)&buffer[0] = htonl(4);
978 *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS);
981 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
983 int a = tcp->sendPacket(buffer, 8);
987 MUTEX_UNLOCK(&mutex);
995 MUTEX_UNLOCK(&mutex);
999 RecTimerList* recTimerList = new RecTimerList();
1001 ULONG numTimers = extractULONG();
1004 RecTimer* newRecTimer;
1007 while (packetPos < packetLength)
1009 newRecTimer = new RecTimer();
1010 newRecTimer->active = extractULONG();
1011 newRecTimer->recording = extractULONG();
1012 newRecTimer->pending = extractULONG();
1013 newRecTimer->priority = extractULONG();
1014 newRecTimer->lifeTime = extractULONG();
1015 newRecTimer->channelNumber = extractULONG();
1016 newRecTimer->startTime = extractULONG();
1017 newRecTimer->stopTime = extractULONG();
1019 tempString = extractString();
1020 newRecTimer->setFile(tempString);
1021 delete[] tempString;
1023 recTimerList->push_back(newRecTimer);
1024 Log::getInstance()->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1025 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1026 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1031 MUTEX_UNLOCK(&mutex);
1035 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1037 return recTimerList;
1040 ULONG VDR::setEventTimer(char* timerString)
1042 unsigned long totalLength = 8 + strlen(timerString) + 1;
1043 UCHAR* buffer = new UCHAR[totalLength];
1045 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
1046 *(unsigned long*)&buffer[4] = htonl(VDR_SETTIMER);
1047 strcpy((char*)&buffer[8], timerString);
1050 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
1052 unsigned int a = tcp->sendPacket(buffer, totalLength);
1055 if (a != totalLength)
1058 MUTEX_UNLOCK(&mutex);
1064 MUTEX_UNLOCK(&mutex);
1068 ULONG toReturn = extractULONG();
1070 MUTEX_UNLOCK(&mutex);