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;
32 pthread_mutex_init(&mutex, NULL);
42 if (initted) shutdown();
45 VDR* VDR::getInstance()
50 int VDR::init(int tport)
52 if (initted) return 0;
55 logger = Log::getInstance();
61 if (!initted) return 0;
67 void VDR::findServers(vector<VDRServer>& servers)
70 char* message = "VOMP";
71 DatagramSocket ds(port);
73 int haveAtLeastOne = 0;
80 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
81 ds.send("255.255.255.255", 3024, message, strlen(message));
83 retval = ds.waitforMessage(waitType);
85 if (retval == 2) // we got a reply
87 if (!strcmp(ds.getData(), "VOMP")) // echo.....
94 newServer.ip = new char[16];
95 strcpy(newServer.ip, ds.getFromIPA());
97 if (ds.getDataLength() == 0)
99 newServer.name = new char[1];
100 newServer.name[0] = '\0';
104 newServer.name = new char[strlen(ds.getData())+1];
105 strcpy(newServer.name, ds.getData());
108 servers.push_back(newServer);
115 if (haveAtLeastOne) break;
119 sort(servers.begin(), servers.end(), ServerSorter());
122 void VDR::cancelFindingServer()
127 void VDR::setServerIP(char* newIP)
129 strcpy(serverIP, newIP);
136 if (tcp->connectTo(serverIP, 3024))
147 void VDR::disconnect()
152 Log::getInstance()->log("VDR", Log::DEBUG, "Disconnect");
155 void VDR::setReceiveWindow(size_t size)
157 tcp->setReceiveWindow(size);
160 ///////////////////////////////////////////////////////
164 packet = (UCHAR*)tcp->receivePacket();
170 packetLength = tcp->getDataLength();
174 void VDR::freePacket()
176 // Must be called if getPacket return 1, except in getBlock
183 int VDR::serverError()
185 if ((packetPos == 0) && (packetLength == 4) && !ntohl(*(ULONG*)packet)) return 1;
189 char* VDR::extractString()
191 if (serverError()) return NULL;
193 int length = strlen((char*)&packet[packetPos]);
194 if ((packetPos + length) > packetLength) return NULL;
195 char* str = new char[length + 1];
196 strcpy(str, (char*)&packet[packetPos]);
197 packetPos += length + 1;
201 ULONG VDR::extractULONG()
203 if ((packetPos + sizeof(ULONG)) > packetLength) return 0;
204 ULONG ul = ntohl(*(ULONG*)&packet[packetPos]);
205 packetPos += sizeof(ULONG);
209 ULLONG VDR::extractULLONG()
211 if ((packetPos + sizeof(ULLONG)) > packetLength) return 0;
212 ULLONG ull = ntohll(*(ULLONG*)&packet[packetPos]);
213 packetPos += sizeof(ULLONG);
217 long VDR::extractLONG()
219 if ((packetPos + sizeof(long)) > packetLength) return 0;
220 long l = ntohl(*(long*)&packet[packetPos]);
221 packetPos += sizeof(long);
225 /////////////////////////////////////////////////////////////////////////////
229 if (!connected) return 0;
233 *(unsigned long*)&buffer[0] = htonl(10);
234 *(unsigned long*)&buffer[4] = htonl(VDR_LOGIN);
236 tcp->getMAC((char*)&buffer[8]);
239 pthread_mutex_lock(&mutex);
240 int a = tcp->sendPacket(buffer, 14);
244 pthread_mutex_unlock(&mutex);
252 pthread_mutex_unlock(&mutex);
256 ULONG vdrTime = extractULONG();
257 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
258 long vdrTimeOffset = extractLONG();
259 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
262 pthread_mutex_unlock(&mutex);
265 struct timespec currentTime;
266 currentTime.tv_sec = vdrTime;
267 currentTime.tv_nsec = 0;
268 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
270 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
272 // now make a TZ variable and set it
276 if (vdrTimeOffset > 0) sign = '-';
279 vdrTimeOffset = abs(vdrTimeOffset);
281 hours = (int)vdrTimeOffset / 3600;
282 minutes = vdrTimeOffset % 3600;
284 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
286 minutes = (int)minutes / 60;
288 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
291 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
292 setenv("TZ", newTZ, 1);
294 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
299 Directory* VDR::getRecordingsList()
301 if (!connected) return 0;
305 *(unsigned long*)&buffer[0] = htonl(4);
306 *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST);
308 pthread_mutex_lock(&mutex);
309 int a = tcp->sendPacket(buffer, 8);
313 pthread_mutex_unlock(&mutex);
321 pthread_mutex_unlock(&mutex);
325 Directory* recDir = new Directory();
326 recDir->setName("/");
329 Directory::totalSpace = extractULONG();
330 Directory::freeSpace = extractULONG();
331 Directory::usedPercent = extractULONG();
335 while (packetPos < packetLength)
337 Recording* rec = new Recording();
339 rec->start = extractULONG();
341 string = extractString();
342 rec->setName(string);
345 rec->fileName = extractString();
349 char* dirName = rec->getDirName();
351 Directory* d = recDir->getDirByName(dirName);
356 // Log::getInstance()->log("VDR", Log::DEBUG, "Added a new directory = %s", d->name);
357 recDir->dirList.push_back(d);
360 d->recList.push_back(rec);
364 recDir->recList.push_back(rec);
367 // Log::getInstance()->log("VDR", Log::DEBUG, "%s", rec->fileName);
371 pthread_mutex_unlock(&mutex);
373 // Sort the directory order
374 sort(recDir->dirList.begin(), recDir->dirList.end(), DirectorySorter());
376 // Sort all the sub lists
379 DirectoryList::iterator i;
380 for (i = recDir->dirList.begin(); i != recDir->dirList.end(); i++)
383 sort(sortDir->recList.begin(), sortDir->recList.end(), RecordingSorter());
386 // Sort the root level list
387 sort(recDir->recList.begin(), recDir->recList.end(), RecordingSorter());
392 int VDR::deleteRecording(char* fileName)
394 if (!connected) return 0;
396 unsigned long totalLength = 8 + strlen(fileName) + 1;
397 UCHAR buffer[totalLength];
399 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
400 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
401 strcpy((char*)&buffer[8], fileName);
403 pthread_mutex_lock(&mutex);
404 unsigned int a = tcp->sendPacket(buffer, totalLength);
405 if (a != totalLength)
408 pthread_mutex_unlock(&mutex);
414 pthread_mutex_unlock(&mutex);
418 int toReturn = (int)extractULONG();
420 pthread_mutex_unlock(&mutex);
425 char* VDR::getRecordingSummary(char* fileName)
427 if (!connected) return 0;
429 unsigned long totalLength = 8 + strlen(fileName) + 1;
430 UCHAR buffer[totalLength];
432 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
433 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
434 strcpy((char*)&buffer[8], fileName);
436 pthread_mutex_lock(&mutex);
437 unsigned int a = tcp->sendPacket(buffer, totalLength);
438 if (a != totalLength)
441 pthread_mutex_unlock(&mutex);
447 pthread_mutex_unlock(&mutex);
450 char* toReturn = extractString();
452 pthread_mutex_unlock(&mutex);
457 ChannelList* VDR::getChannelsList(ULONG type)
459 if (!connected) return 0;
463 *(unsigned long*)&buffer[0] = htonl(4);
464 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
466 pthread_mutex_lock(&mutex);
467 int a = tcp->sendPacket(buffer, 8);
471 pthread_mutex_unlock(&mutex);
479 pthread_mutex_unlock(&mutex);
483 ChannelList* chanList = new ChannelList();
485 while (packetPos < packetLength)
487 Channel* chan = new Channel();
488 chan->number = extractULONG();
489 chan->type = extractULONG();
490 chan->name = extractString();
492 if (chan->type == type)
494 chanList->push_back(chan);
495 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
504 pthread_mutex_unlock(&mutex);
509 int VDR::streamChannel(ULONG number)
511 if (!connected) return 0;
515 *(unsigned long*)&buffer[0] = htonl(8);
516 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
517 *(unsigned long*)&buffer[8] = htonl(number);
519 pthread_mutex_lock(&mutex);
520 int a = tcp->sendPacket(buffer, 12);
525 pthread_mutex_unlock(&mutex);
531 pthread_mutex_unlock(&mutex);
535 int toReturn = (int)extractULONG();
537 pthread_mutex_unlock(&mutex);
542 int VDR::stopStreaming()
544 if (!connected) return 0;
548 *(unsigned long*)&buffer[0] = htonl(4);
549 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
551 pthread_mutex_lock(&mutex);
552 int a = tcp->sendPacket(buffer, 8);
557 pthread_mutex_unlock(&mutex);
563 pthread_mutex_unlock(&mutex);
567 int toReturn = (int)extractULONG();
569 pthread_mutex_unlock(&mutex);
574 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
576 if (!connected) return 0;
580 *(unsigned long*)&buffer[0] = htonl(16);
581 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
582 *(ULLONG*)&buffer[8] = htonll(position);
583 *(unsigned long*)&buffer[16] = htonl(maxAmount);
585 pthread_mutex_lock(&mutex);
586 int a = tcp->sendPacket(buffer, 20);
590 pthread_mutex_unlock(&mutex);
596 pthread_mutex_unlock(&mutex);
600 UCHAR* toReturn = packet;
601 *amountReceived = packetLength;
602 // Manually clean up instead of running freePacket to keep the block
606 pthread_mutex_unlock(&mutex);
611 ULLONG VDR::streamRecording(Recording* rec)
613 if (!connected) return 0;
615 unsigned long totalLength = 8 + strlen(rec->fileName) + 1;
616 UCHAR buffer[totalLength];
618 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
619 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
620 strcpy((char*)&buffer[8], rec->fileName);
622 pthread_mutex_lock(&mutex);
623 unsigned int a = tcp->sendPacket(buffer, totalLength);
624 if (a != totalLength)
627 pthread_mutex_unlock(&mutex);
633 pthread_mutex_unlock(&mutex);
637 ULLONG recordingLength = extractULLONG();
639 pthread_mutex_unlock(&mutex);
641 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
643 return recordingLength;
646 ULLONG VDR::rescanRecording()
648 if (!connected) return 0;
650 unsigned long totalLength = 8;
651 UCHAR buffer[totalLength];
653 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
654 *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
656 pthread_mutex_lock(&mutex);
657 unsigned int a = tcp->sendPacket(buffer, totalLength);
658 if (a != totalLength)
661 pthread_mutex_unlock(&mutex);
667 pthread_mutex_unlock(&mutex);
671 ULLONG recordingLength = extractULLONG();
673 pthread_mutex_unlock(&mutex);
675 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
677 return recordingLength;
680 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
682 if (!connected) return 0;
684 unsigned long totalLength = 12;
685 UCHAR buffer[totalLength];
687 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
688 *(unsigned long*)&buffer[4] = htonl(VDR_POSFROMFRAME);
689 *(unsigned long*)&buffer[8] = htonl(frameNumber);
691 pthread_mutex_lock(&mutex);
692 unsigned int a = tcp->sendPacket(buffer, totalLength);
693 if (a != totalLength)
696 pthread_mutex_unlock(&mutex);
702 pthread_mutex_unlock(&mutex);
706 ULLONG position = extractULLONG();
708 pthread_mutex_unlock(&mutex);
710 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
715 EventList* VDR::getChannelSchedule(ULONG number)
719 return getChannelSchedule(number, now, 24 * 60 * 60);
722 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
724 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
725 if (!connected) return 0;
729 *(unsigned long*)&buffer[0] = htonl(16);
730 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
731 *(unsigned long*)&buffer[8] = htonl(number);
732 *(unsigned long*)&buffer[12] = htonl(start);
733 *(unsigned long*)&buffer[16] = htonl(duration);
735 pthread_mutex_lock(&mutex);
736 int a = tcp->sendPacket(buffer, 20);
741 pthread_mutex_unlock(&mutex);
747 pthread_mutex_unlock(&mutex);
755 pthread_mutex_unlock(&mutex);
760 EventList* eventList = new EventList();
762 while (packetPos < packetLength)
764 Event* event = new Event();
765 event->id = extractULONG();
766 event->time = extractULONG();
767 event->duration = extractULONG();
768 event->title = extractString();
769 event->subtitle = extractString();
770 event->description = extractString();
771 eventList->push_back(event);
772 // eventList->next();
776 pthread_mutex_unlock(&mutex);
778 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
783 Log* l = Log::getInstance();
786 l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
789 for(eventList->reset(); !eventList->eol(); eventList->next())
791 currentEvent = (Event*)eventList->getCurrent();
792 l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
799 ULLONG VDR::getResumePoint(char* fileName)
801 if (!connected) return 0;
803 char* resumeString = configLoad("ResumeData", fileName);
804 if (!resumeString) return 0;
806 ULLONG toReturn = strtoull(resumeString, NULL, 10);
807 delete[] resumeString;
811 int VDR::configSave(char* section, char* key, const char* value)
813 if (!connected) return 0;
815 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
816 UCHAR buffer[totalLength];
818 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
819 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
822 strcpy((char*)&buffer[position], section);
823 position += strlen(section) + 1;
824 strcpy((char*)&buffer[position], key);
825 position += strlen(key) + 1;
826 strcpy((char*)&buffer[position], value);
828 pthread_mutex_lock(&mutex);
829 unsigned int a = tcp->sendPacket(buffer, totalLength);
830 if (a != totalLength)
833 pthread_mutex_unlock(&mutex);
839 pthread_mutex_unlock(&mutex);
843 int toReturn = (int)extractULONG();
845 pthread_mutex_unlock(&mutex);
850 char* VDR::configLoad(char* section, char* key)
852 if (!connected) return 0;
854 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
855 UCHAR buffer[totalLength];
857 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
858 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
861 strcpy((char*)&buffer[position], section);
862 position += strlen(section) + 1;
863 strcpy((char*)&buffer[position], key);
865 pthread_mutex_lock(&mutex);
866 unsigned int a = tcp->sendPacket(buffer, totalLength);
867 if (a != totalLength)
870 pthread_mutex_unlock(&mutex);
876 pthread_mutex_unlock(&mutex);
879 char* toReturn = extractString();
881 pthread_mutex_unlock(&mutex);
883 printf("%p %s\n", toReturn, toReturn);
888 RecTimerList* VDR::getRecTimersList()
890 if (!connected) return NULL;
894 *(unsigned long*)&buffer[0] = htonl(4);
895 *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS);
897 pthread_mutex_lock(&mutex);
898 int a = tcp->sendPacket(buffer, 8);
902 pthread_mutex_unlock(&mutex);
910 pthread_mutex_unlock(&mutex);
914 RecTimerList* recTimerList = new RecTimerList();
916 ULONG numTimers = extractULONG();
919 RecTimer* newRecTimer;
922 while (packetPos < packetLength)
924 newRecTimer = new RecTimer();
925 newRecTimer->active = extractULONG();
926 newRecTimer->recording = extractULONG();
927 newRecTimer->pending = extractULONG();
928 newRecTimer->priority = extractULONG();
929 newRecTimer->lifeTime = extractULONG();
930 newRecTimer->channelNumber = extractULONG();
931 newRecTimer->startTime = extractULONG();
932 newRecTimer->stopTime = extractULONG();
934 tempString = extractString();
935 newRecTimer->setFile(tempString);
938 recTimerList->push_back(newRecTimer);
939 Log::getInstance()->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
940 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
941 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
946 pthread_mutex_unlock(&mutex);
950 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
955 ULONG VDR::setEventTimer(char* timerString)
957 if (!connected) return false;
959 unsigned long totalLength = 8 + strlen(timerString) + 1;
960 UCHAR buffer[totalLength];
962 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
963 *(unsigned long*)&buffer[4] = htonl(VDR_SETTIMER);
964 strcpy((char*)&buffer[8], timerString);
966 pthread_mutex_lock(&mutex);
967 unsigned int a = tcp->sendPacket(buffer, totalLength);
968 if (a != totalLength)
971 pthread_mutex_unlock(&mutex);
977 pthread_mutex_unlock(&mutex);
981 ULONG toReturn = extractULONG();
983 pthread_mutex_unlock(&mutex);