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 ///////////////////////////////////////////////////////
159 packet = (UCHAR*)tcp->receivePacket();
160 if (!packet) return 0;
161 packetLength = tcp->getDataLength();
165 void VDR::freePacket()
167 // Must be called if getPacket return 1, except in getBlock
174 int VDR::serverError()
176 if ((packetPos == 0) && (packetLength == 4) && !ntohl(*(ULONG*)packet)) return 1;
180 char* VDR::extractString()
182 if (serverError()) return NULL;
184 int length = strlen((char*)&packet[packetPos]);
185 if ((packetPos + length) > packetLength) return NULL;
186 char* str = new char[length + 1];
187 strcpy(str, (char*)&packet[packetPos]);
188 packetPos += length + 1;
192 ULONG VDR::extractULONG()
194 if ((packetPos + sizeof(ULONG)) > packetLength) return 0;
195 ULONG ul = ntohl(*(ULONG*)&packet[packetPos]);
196 packetPos += sizeof(ULONG);
200 ULLONG VDR::extractULLONG()
202 if ((packetPos + sizeof(ULLONG)) > packetLength) return 0;
203 ULLONG ull = ntohll(*(ULLONG*)&packet[packetPos]);
204 packetPos += sizeof(ULLONG);
208 long VDR::extractLONG()
210 if ((packetPos + sizeof(long)) > packetLength) return 0;
211 long l = ntohl(*(long*)&packet[packetPos]);
212 packetPos += sizeof(long);
216 /////////////////////////////////////////////////////////////////////////////
220 if (!connected) return 0;
224 *(unsigned long*)&buffer[0] = htonl(10);
225 *(unsigned long*)&buffer[4] = htonl(VDR_LOGIN);
227 tcp->getMAC((char*)&buffer[8]);
230 pthread_mutex_lock(&mutex);
231 int a = tcp->sendPacket(buffer, 14);
234 pthread_mutex_unlock(&mutex);
242 pthread_mutex_unlock(&mutex);
246 ULONG vdrTime = extractULONG();
247 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
248 long vdrTimeOffset = extractLONG();
249 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
252 pthread_mutex_unlock(&mutex);
255 struct timespec currentTime;
256 currentTime.tv_sec = vdrTime;
257 currentTime.tv_nsec = 0;
258 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
260 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
262 // now make a TZ variable and set it
266 if (vdrTimeOffset > 0) sign = '-';
269 vdrTimeOffset = abs(vdrTimeOffset);
271 hours = (int)vdrTimeOffset / 3600;
272 minutes = vdrTimeOffset % 3600;
274 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
276 minutes = (int)minutes / 60;
278 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
281 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
282 setenv("TZ", newTZ, 1);
284 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
289 Directory* VDR::getRecordingsList()
291 if (!connected) return 0;
295 *(unsigned long*)&buffer[0] = htonl(4);
296 *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST);
298 pthread_mutex_lock(&mutex);
299 int a = tcp->sendPacket(buffer, 8);
302 pthread_mutex_unlock(&mutex);
310 pthread_mutex_unlock(&mutex);
314 Directory* recDir = new Directory();
315 recDir->setName("/");
318 Directory::totalSpace = extractULONG();
319 Directory::freeSpace = extractULONG();
320 Directory::usedPercent = extractULONG();
324 while (packetPos < packetLength)
326 Recording* rec = new Recording();
328 rec->start = extractULONG();
330 string = extractString();
331 rec->setName(string);
334 rec->fileName = extractString();
338 char* dirName = rec->getDirName();
340 Directory* d = recDir->getDirByName(dirName);
345 Log::getInstance()->log("VDR", Log::DEBUG, "Added a new directory = %s", d->name);
346 recDir->dirList.push_back(d);
349 d->recList.push_back(rec);
353 recDir->recList.push_back(rec);
356 Log::getInstance()->log("VDR", Log::DEBUG, "%s", rec->fileName);
360 pthread_mutex_unlock(&mutex);
365 int VDR::deleteRecording(char* fileName)
367 if (!connected) return 0;
369 unsigned long totalLength = 8 + strlen(fileName) + 1;
370 UCHAR buffer[totalLength];
372 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
373 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
374 strcpy((char*)&buffer[8], fileName);
376 pthread_mutex_lock(&mutex);
377 unsigned int a = tcp->sendPacket(buffer, totalLength);
378 if (a != totalLength)
380 pthread_mutex_unlock(&mutex);
386 pthread_mutex_unlock(&mutex);
390 int toReturn = (int)extractULONG();
392 pthread_mutex_unlock(&mutex);
397 char* VDR::getRecordingSummary(char* fileName)
399 if (!connected) return 0;
401 unsigned long totalLength = 8 + strlen(fileName) + 1;
402 UCHAR buffer[totalLength];
404 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
405 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
406 strcpy((char*)&buffer[8], fileName);
408 pthread_mutex_lock(&mutex);
409 unsigned int a = tcp->sendPacket(buffer, totalLength);
410 if (a != totalLength)
412 pthread_mutex_unlock(&mutex);
418 pthread_mutex_unlock(&mutex);
421 char* toReturn = extractString();
423 pthread_mutex_unlock(&mutex);
428 ChannelList* VDR::getChannelsList(ULONG type)
430 if (!connected) return 0;
434 *(unsigned long*)&buffer[0] = htonl(4);
435 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
437 pthread_mutex_lock(&mutex);
438 int a = tcp->sendPacket(buffer, 8);
441 pthread_mutex_unlock(&mutex);
449 pthread_mutex_unlock(&mutex);
453 ChannelList* chanList = new ChannelList();
455 while (packetPos < packetLength)
457 Channel* chan = new Channel();
458 chan->number = extractULONG();
459 chan->type = extractULONG();
460 chan->name = extractString();
462 if (chan->type == type)
464 chanList->push_back(chan);
465 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
474 pthread_mutex_unlock(&mutex);
479 int VDR::streamChannel(ULONG number)
481 if (!connected) return 0;
485 *(unsigned long*)&buffer[0] = htonl(8);
486 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
487 *(unsigned long*)&buffer[8] = htonl(number);
489 pthread_mutex_lock(&mutex);
490 int a = tcp->sendPacket(buffer, 12);
494 pthread_mutex_unlock(&mutex);
500 pthread_mutex_unlock(&mutex);
504 int toReturn = (int)extractULONG();
506 pthread_mutex_unlock(&mutex);
511 int VDR::stopStreaming()
513 if (!connected) return 0;
517 *(unsigned long*)&buffer[0] = htonl(4);
518 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
520 pthread_mutex_lock(&mutex);
521 int a = tcp->sendPacket(buffer, 8);
525 pthread_mutex_unlock(&mutex);
529 printf("sent request\n");
533 pthread_mutex_unlock(&mutex);
536 printf("got reply\n");
539 int toReturn = (int)extractULONG();
541 pthread_mutex_unlock(&mutex);
546 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
548 if (!connected) return 0;
552 *(unsigned long*)&buffer[0] = htonl(16);
553 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
554 *(ULLONG*)&buffer[8] = htonll(position);
555 *(unsigned long*)&buffer[16] = htonl(maxAmount);
557 pthread_mutex_lock(&mutex);
558 int a = tcp->sendPacket(buffer, 20);
561 pthread_mutex_unlock(&mutex);
567 pthread_mutex_unlock(&mutex);
571 UCHAR* toReturn = packet;
572 *amountReceived = packetLength;
573 // Manually clean up instead of running freePacket to keep the block
577 pthread_mutex_unlock(&mutex);
582 ULLONG VDR::streamRecording(Recording* rec)
584 if (!connected) return 0;
586 unsigned long totalLength = 8 + strlen(rec->fileName) + 1;
587 UCHAR buffer[totalLength];
589 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
590 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
591 strcpy((char*)&buffer[8], rec->fileName);
593 pthread_mutex_lock(&mutex);
594 unsigned int a = tcp->sendPacket(buffer, totalLength);
595 if (a != totalLength)
597 pthread_mutex_unlock(&mutex);
603 pthread_mutex_unlock(&mutex);
607 ULLONG recordingLength = extractULLONG();
609 pthread_mutex_unlock(&mutex);
611 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
613 return recordingLength;
616 ULLONG VDR::rescanRecording()
618 if (!connected) return 0;
620 unsigned long totalLength = 8;
621 UCHAR buffer[totalLength];
623 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
624 *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
626 pthread_mutex_lock(&mutex);
627 unsigned int a = tcp->sendPacket(buffer, totalLength);
628 if (a != totalLength)
630 pthread_mutex_unlock(&mutex);
636 pthread_mutex_unlock(&mutex);
640 ULLONG recordingLength = extractULLONG();
642 pthread_mutex_unlock(&mutex);
644 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
646 return recordingLength;
649 EventList* VDR::getChannelSchedule(ULONG number)
653 return getChannelSchedule(number, now, 24 * 60 * 60);
656 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
658 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
659 if (!connected) return 0;
663 *(unsigned long*)&buffer[0] = htonl(16);
664 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
665 *(unsigned long*)&buffer[8] = htonl(number);
666 *(unsigned long*)&buffer[12] = htonl(start);
667 *(unsigned long*)&buffer[16] = htonl(duration);
669 pthread_mutex_lock(&mutex);
670 int a = tcp->sendPacket(buffer, 20);
674 pthread_mutex_unlock(&mutex);
680 pthread_mutex_unlock(&mutex);
687 pthread_mutex_unlock(&mutex);
692 EventList* eventList = new EventList();
694 while (packetPos < packetLength)
696 Event* event = new Event();
697 event->id = extractULONG();
698 event->time = extractULONG();
699 event->duration = extractULONG();
700 event->title = extractString();
701 event->subtitle = extractString();
702 event->description = extractString();
703 eventList->push_back(event);
704 // eventList->next();
708 pthread_mutex_unlock(&mutex);
710 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
715 Log* l = Log::getInstance();
718 l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
721 for(eventList->reset(); !eventList->eol(); eventList->next())
723 currentEvent = (Event*)eventList->getCurrent();
724 l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
731 ULLONG VDR::getResumePoint(char* fileName)
733 if (!connected) return 0;
735 char* resumeString = configLoad("ResumeData", fileName);
736 if (!resumeString) return 0;
738 ULLONG toReturn = strtoull(resumeString, NULL, 10);
739 delete[] resumeString;
743 int VDR::configSave(char* section, char* key, const char* value)
745 if (!connected) return 0;
747 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
748 UCHAR buffer[totalLength];
750 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
751 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
754 strcpy((char*)&buffer[position], section);
755 position += strlen(section) + 1;
756 strcpy((char*)&buffer[position], key);
757 position += strlen(key) + 1;
758 strcpy((char*)&buffer[position], value);
760 pthread_mutex_lock(&mutex);
761 unsigned int a = tcp->sendPacket(buffer, totalLength);
762 if (a != totalLength)
764 pthread_mutex_unlock(&mutex);
770 pthread_mutex_unlock(&mutex);
774 int toReturn = (int)extractULONG();
776 pthread_mutex_unlock(&mutex);
781 char* VDR::configLoad(char* section, char* key)
783 if (!connected) return 0;
785 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
786 UCHAR buffer[totalLength];
788 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
789 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
792 strcpy((char*)&buffer[position], section);
793 position += strlen(section) + 1;
794 strcpy((char*)&buffer[position], key);
796 pthread_mutex_lock(&mutex);
797 unsigned int a = tcp->sendPacket(buffer, totalLength);
798 if (a != totalLength)
800 pthread_mutex_unlock(&mutex);
806 pthread_mutex_unlock(&mutex);
809 char* toReturn = extractString();
811 pthread_mutex_unlock(&mutex);
816 RecTimerList* VDR::getRecTimersList()
818 if (!connected) return NULL;
822 *(unsigned long*)&buffer[0] = htonl(4);
823 *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS);
825 pthread_mutex_lock(&mutex);
826 int a = tcp->sendPacket(buffer, 8);
829 pthread_mutex_unlock(&mutex);
837 pthread_mutex_unlock(&mutex);
841 RecTimerList* recTimerList = new RecTimerList();
843 RecTimer* newRecTimer;
846 while (packetPos < packetLength)
848 newRecTimer = new RecTimer();
849 newRecTimer->active = extractULONG();
850 newRecTimer->recording = extractULONG();
851 newRecTimer->pending = extractULONG();
852 newRecTimer->priority = extractULONG();
853 newRecTimer->lifeTime = extractULONG();
854 newRecTimer->channelNumber = extractULONG();
855 newRecTimer->startTime = extractULONG();
856 newRecTimer->stopTime = extractULONG();
858 tempString = extractString();
859 newRecTimer->setFile(tempString);
862 newRecTimer->summary = extractString();
864 recTimerList->push_back(newRecTimer);
865 Log::getInstance()->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
866 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
867 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
871 pthread_mutex_unlock(&mutex);
875 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());