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 bool VDR::getRecordingsList(RecMan* recman)
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 recman->setStats(extractULONG(), extractULONG(), extractULONG());
360 while (packetPos < packetLength)
362 start = extractULONG();
363 name = extractString();
364 fileName = extractString();
366 recman->addEntry(start, name, fileName);
373 MUTEX_UNLOCK(&mutex);
378 int VDR::deleteRecording(char* fileName)
380 unsigned long totalLength = 8 + strlen(fileName) + 1;
381 UCHAR* buffer = new UCHAR[totalLength];
383 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
384 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
385 strcpy((char*)&buffer[8], fileName);
388 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
390 unsigned int a = tcp->sendPacket(buffer, totalLength);
393 if (a != totalLength)
396 MUTEX_UNLOCK(&mutex);
402 MUTEX_UNLOCK(&mutex);
406 int toReturn = (int)extractULONG();
408 MUTEX_UNLOCK(&mutex);
413 int VDR::moveRecording(char* fileName, char* newPath)
415 unsigned long totalLength = 8 + strlen(fileName) + 1 + strlen(newPath) + 1;
416 UCHAR* buffer = new UCHAR[totalLength];
418 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
419 *(unsigned long*)&buffer[4] = htonl(VDR_MOVERECORDING);
420 strcpy((char*)&buffer[8], fileName);
421 strcpy((char*)&buffer[8 + strlen(fileName) + 1], newPath);
424 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
426 unsigned int a = tcp->sendPacket(buffer, totalLength);
429 if (a != totalLength)
432 MUTEX_UNLOCK(&mutex);
438 MUTEX_UNLOCK(&mutex);
442 int toReturn = (int)extractULONG();
444 MUTEX_UNLOCK(&mutex);
449 char* VDR::getRecordingSummary(char* fileName)
451 unsigned long totalLength = 8 + strlen(fileName) + 1;
452 UCHAR* buffer = new UCHAR[totalLength];
454 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
455 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
456 strcpy((char*)&buffer[8], fileName);
459 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
461 unsigned int a = tcp->sendPacket(buffer, totalLength);
464 if (a != totalLength)
467 MUTEX_UNLOCK(&mutex);
473 MUTEX_UNLOCK(&mutex);
476 char* toReturn = extractString();
478 MUTEX_UNLOCK(&mutex);
483 ChannelList* VDR::getChannelsList(ULONG type)
487 *(unsigned long*)&buffer[0] = htonl(4);
488 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
491 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
493 int a = tcp->sendPacket(buffer, 8);
497 MUTEX_UNLOCK(&mutex);
505 MUTEX_UNLOCK(&mutex);
509 ChannelList* chanList = new ChannelList();
511 while (packetPos < packetLength)
513 Channel* chan = new Channel();
514 chan->number = extractULONG();
515 chan->type = extractULONG();
516 chan->name = extractString();
518 if (chan->type == type)
520 chanList->push_back(chan);
521 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
530 MUTEX_UNLOCK(&mutex);
535 int VDR::streamChannel(ULONG number)
539 *(unsigned long*)&buffer[0] = htonl(8);
540 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
541 *(unsigned long*)&buffer[8] = htonl(number);
544 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
546 int a = tcp->sendPacket(buffer, 12);
551 MUTEX_UNLOCK(&mutex);
557 MUTEX_UNLOCK(&mutex);
561 int toReturn = (int)extractULONG();
563 MUTEX_UNLOCK(&mutex);
568 int VDR::stopStreaming()
572 *(unsigned long*)&buffer[0] = htonl(4);
573 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
576 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
578 int a = tcp->sendPacket(buffer, 8);
583 MUTEX_UNLOCK(&mutex);
589 MUTEX_UNLOCK(&mutex);
593 int toReturn = (int)extractULONG();
595 MUTEX_UNLOCK(&mutex);
600 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
604 *(unsigned long*)&buffer[0] = htonl(16);
605 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
606 *(ULLONG*)&buffer[8] = htonll(position);
607 *(unsigned long*)&buffer[16] = htonl(maxAmount);
610 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
612 int a = tcp->sendPacket(buffer, 20);
616 MUTEX_UNLOCK(&mutex);
622 MUTEX_UNLOCK(&mutex);
628 Log::getInstance()->log("VDR", Log::DEBUG, "Detected getblock 0");
630 MUTEX_UNLOCK(&mutex);
634 UCHAR* toReturn = packet;
635 *amountReceived = packetLength;
636 // Manually clean up instead of running freePacket to keep the block
640 MUTEX_UNLOCK(&mutex);
646 ULLONG VDR::streamRecording(char* fileName)
648 unsigned long totalLength = 8 + strlen(fileName) + 1;
649 UCHAR* buffer = new UCHAR[totalLength];
651 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
652 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
653 strcpy((char*)&buffer[8], fileName);
656 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
658 unsigned int a = tcp->sendPacket(buffer, totalLength);
661 if (a != totalLength)
664 MUTEX_UNLOCK(&mutex);
670 MUTEX_UNLOCK(&mutex);
674 ULLONG recordingLength = extractULLONG();
676 MUTEX_UNLOCK(&mutex);
678 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
680 return recordingLength;
683 ULLONG VDR::rescanRecording()
685 unsigned long totalLength = 8;
686 UCHAR* buffer = new UCHAR[totalLength];
688 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
689 *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
692 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
694 unsigned int a = tcp->sendPacket(buffer, totalLength);
697 if (a != totalLength)
700 MUTEX_UNLOCK(&mutex);
706 MUTEX_UNLOCK(&mutex);
710 ULLONG recordingLength = extractULLONG();
712 MUTEX_UNLOCK(&mutex);
714 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
716 return recordingLength;
719 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
721 unsigned long totalLength = 12;
722 UCHAR* buffer = new UCHAR[totalLength];
724 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
725 *(unsigned long*)&buffer[4] = htonl(VDR_POSFROMFRAME);
726 *(unsigned long*)&buffer[8] = htonl(frameNumber);
729 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
731 unsigned int a = tcp->sendPacket(buffer, totalLength);
734 if (a != totalLength)
737 MUTEX_UNLOCK(&mutex);
743 MUTEX_UNLOCK(&mutex);
747 ULLONG position = extractULLONG();
749 MUTEX_UNLOCK(&mutex);
751 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
756 ULONG VDR::frameNumberFromPosition(ULLONG position)
758 unsigned long totalLength = 16;
759 UCHAR* buffer = new UCHAR[totalLength];
761 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
762 *(unsigned long*)&buffer[4] = htonl(VDR_FRAMEFROMPOS);
763 *(ULLONG*)&buffer[8] = htonll(position);
766 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
768 unsigned int a = tcp->sendPacket(buffer, totalLength);
771 if (a != totalLength)
774 MUTEX_UNLOCK(&mutex);
780 MUTEX_UNLOCK(&mutex);
784 ULONG framenumber = extractULONG();
786 MUTEX_UNLOCK(&mutex);
788 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
793 EventList* VDR::getChannelSchedule(ULONG number)
797 return getChannelSchedule(number, now, 24 * 60 * 60);
800 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
802 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
805 *(unsigned long*)&buffer[0] = htonl(16);
806 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
807 *(unsigned long*)&buffer[8] = htonl(number);
808 *(unsigned long*)&buffer[12] = htonl(start);
809 *(unsigned long*)&buffer[16] = htonl(duration);
812 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
814 int a = tcp->sendPacket(buffer, 20);
819 MUTEX_UNLOCK(&mutex);
825 MUTEX_UNLOCK(&mutex);
829 // received a ulong(0) - schedules error in the plugin
833 MUTEX_UNLOCK(&mutex);
837 EventList* eventList = new EventList();
839 while (packetPos < packetLength)
841 Event* event = new Event();
842 event->id = extractULONG();
843 event->time = extractULONG();
844 event->duration = extractULONG();
845 event->title = extractString();
846 event->subtitle = extractString();
847 event->description = extractString();
848 eventList->push_back(event);
849 // eventList->next();
853 MUTEX_UNLOCK(&mutex);
855 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
860 Log* l = Log::getInstance();
863 l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
866 for(eventList->reset(); !eventList->eol(); eventList->next())
868 currentEvent = (Event*)eventList->getCurrent();
869 l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
876 ULLONG VDR::getResumePoint(char* fileName)
878 if (!connected) return 0;
880 char* resumeString = configLoad("ResumeData", fileName);
881 if (!resumeString) return 0;
883 ULLONG toReturn = STRTOULL(resumeString, NULL, 10);
884 delete[] resumeString;
888 int VDR::configSave(char* section, char* key, const char* value)
890 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
891 UCHAR* buffer = new UCHAR[totalLength];
893 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
894 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
897 strcpy((char*)&buffer[position], section);
898 position += strlen(section) + 1;
899 strcpy((char*)&buffer[position], key);
900 position += strlen(key) + 1;
901 strcpy((char*)&buffer[position], value);
904 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
906 unsigned int a = tcp->sendPacket(buffer, totalLength);
909 if (a != totalLength)
912 MUTEX_UNLOCK(&mutex);
918 MUTEX_UNLOCK(&mutex);
922 int toReturn = (int)extractULONG();
924 MUTEX_UNLOCK(&mutex);
929 char* VDR::configLoad(char* section, char* key)
931 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
932 UCHAR* buffer = new UCHAR[totalLength];
934 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
935 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
938 strcpy((char*)&buffer[position], section);
939 position += strlen(section) + 1;
940 strcpy((char*)&buffer[position], key);
943 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
945 unsigned int a = tcp->sendPacket(buffer, totalLength);
948 if (a != totalLength)
951 MUTEX_UNLOCK(&mutex);
957 MUTEX_UNLOCK(&mutex);
960 char* toReturn = extractString();
962 MUTEX_UNLOCK(&mutex);
967 RecTimerList* VDR::getRecTimersList()
971 *(unsigned long*)&buffer[0] = htonl(4);
972 *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS);
975 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
977 int a = tcp->sendPacket(buffer, 8);
981 MUTEX_UNLOCK(&mutex);
989 MUTEX_UNLOCK(&mutex);
993 RecTimerList* recTimerList = new RecTimerList();
995 ULONG numTimers = extractULONG();
998 RecTimer* newRecTimer;
1001 while (packetPos < packetLength)
1003 newRecTimer = new RecTimer();
1004 newRecTimer->active = extractULONG();
1005 newRecTimer->recording = extractULONG();
1006 newRecTimer->pending = extractULONG();
1007 newRecTimer->priority = extractULONG();
1008 newRecTimer->lifeTime = extractULONG();
1009 newRecTimer->channelNumber = extractULONG();
1010 newRecTimer->startTime = extractULONG();
1011 newRecTimer->stopTime = extractULONG();
1013 tempString = extractString();
1014 newRecTimer->setFile(tempString);
1015 delete[] tempString;
1017 recTimerList->push_back(newRecTimer);
1018 Log::getInstance()->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1019 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1020 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1025 MUTEX_UNLOCK(&mutex);
1029 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1031 return recTimerList;
1034 ULONG VDR::setEventTimer(char* timerString)
1036 unsigned long totalLength = 8 + strlen(timerString) + 1;
1037 UCHAR* buffer = new UCHAR[totalLength];
1039 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
1040 *(unsigned long*)&buffer[4] = htonl(VDR_SETTIMER);
1041 strcpy((char*)&buffer[8], timerString);
1044 if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
1046 unsigned int a = tcp->sendPacket(buffer, totalLength);
1049 if (a != totalLength)
1052 MUTEX_UNLOCK(&mutex);
1058 MUTEX_UNLOCK(&mutex);
1062 ULONG toReturn = extractULONG();
1064 MUTEX_UNLOCK(&mutex);