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);
362 sort(recDir->recList.begin(), recDir->recList.end(), RecordingSorter());
367 int VDR::deleteRecording(char* fileName)
369 if (!connected) return 0;
371 unsigned long totalLength = 8 + strlen(fileName) + 1;
372 UCHAR buffer[totalLength];
374 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
375 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
376 strcpy((char*)&buffer[8], fileName);
378 pthread_mutex_lock(&mutex);
379 unsigned int a = tcp->sendPacket(buffer, totalLength);
380 if (a != totalLength)
382 pthread_mutex_unlock(&mutex);
388 pthread_mutex_unlock(&mutex);
392 int toReturn = (int)extractULONG();
394 pthread_mutex_unlock(&mutex);
399 char* VDR::getRecordingSummary(char* fileName)
401 if (!connected) return 0;
403 unsigned long totalLength = 8 + strlen(fileName) + 1;
404 UCHAR buffer[totalLength];
406 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
407 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
408 strcpy((char*)&buffer[8], fileName);
410 pthread_mutex_lock(&mutex);
411 unsigned int a = tcp->sendPacket(buffer, totalLength);
412 if (a != totalLength)
414 pthread_mutex_unlock(&mutex);
420 pthread_mutex_unlock(&mutex);
423 char* toReturn = extractString();
425 pthread_mutex_unlock(&mutex);
430 ChannelList* VDR::getChannelsList(ULONG type)
432 if (!connected) return 0;
436 *(unsigned long*)&buffer[0] = htonl(4);
437 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
439 pthread_mutex_lock(&mutex);
440 int a = tcp->sendPacket(buffer, 8);
443 pthread_mutex_unlock(&mutex);
451 pthread_mutex_unlock(&mutex);
455 ChannelList* chanList = new ChannelList();
457 while (packetPos < packetLength)
459 Channel* chan = new Channel();
460 chan->number = extractULONG();
461 chan->type = extractULONG();
462 chan->name = extractString();
464 if (chan->type == type)
466 chanList->push_back(chan);
467 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
476 pthread_mutex_unlock(&mutex);
481 int VDR::streamChannel(ULONG number)
483 if (!connected) return 0;
487 *(unsigned long*)&buffer[0] = htonl(8);
488 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
489 *(unsigned long*)&buffer[8] = htonl(number);
491 pthread_mutex_lock(&mutex);
492 int a = tcp->sendPacket(buffer, 12);
496 pthread_mutex_unlock(&mutex);
502 pthread_mutex_unlock(&mutex);
506 int toReturn = (int)extractULONG();
508 pthread_mutex_unlock(&mutex);
513 int VDR::stopStreaming()
515 if (!connected) return 0;
519 *(unsigned long*)&buffer[0] = htonl(4);
520 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
522 pthread_mutex_lock(&mutex);
523 int a = tcp->sendPacket(buffer, 8);
527 pthread_mutex_unlock(&mutex);
531 printf("sent request\n");
535 pthread_mutex_unlock(&mutex);
538 printf("got reply\n");
541 int toReturn = (int)extractULONG();
543 pthread_mutex_unlock(&mutex);
548 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
550 if (!connected) return 0;
554 *(unsigned long*)&buffer[0] = htonl(16);
555 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
556 *(ULLONG*)&buffer[8] = htonll(position);
557 *(unsigned long*)&buffer[16] = htonl(maxAmount);
559 pthread_mutex_lock(&mutex);
560 int a = tcp->sendPacket(buffer, 20);
563 pthread_mutex_unlock(&mutex);
569 pthread_mutex_unlock(&mutex);
573 UCHAR* toReturn = packet;
574 *amountReceived = packetLength;
575 // Manually clean up instead of running freePacket to keep the block
579 pthread_mutex_unlock(&mutex);
584 ULLONG VDR::streamRecording(Recording* rec)
586 if (!connected) return 0;
588 unsigned long totalLength = 8 + strlen(rec->fileName) + 1;
589 UCHAR buffer[totalLength];
591 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
592 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
593 strcpy((char*)&buffer[8], rec->fileName);
595 pthread_mutex_lock(&mutex);
596 unsigned int a = tcp->sendPacket(buffer, totalLength);
597 if (a != totalLength)
599 pthread_mutex_unlock(&mutex);
605 pthread_mutex_unlock(&mutex);
609 ULLONG recordingLength = extractULLONG();
611 pthread_mutex_unlock(&mutex);
613 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
615 return recordingLength;
618 ULLONG VDR::rescanRecording()
620 if (!connected) return 0;
622 unsigned long totalLength = 8;
623 UCHAR buffer[totalLength];
625 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
626 *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
628 pthread_mutex_lock(&mutex);
629 unsigned int a = tcp->sendPacket(buffer, totalLength);
630 if (a != totalLength)
632 pthread_mutex_unlock(&mutex);
638 pthread_mutex_unlock(&mutex);
642 ULLONG recordingLength = extractULLONG();
644 pthread_mutex_unlock(&mutex);
646 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
648 return recordingLength;
651 EventList* VDR::getChannelSchedule(ULONG number)
655 return getChannelSchedule(number, now, 24 * 60 * 60);
658 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
660 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
661 if (!connected) return 0;
665 *(unsigned long*)&buffer[0] = htonl(16);
666 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
667 *(unsigned long*)&buffer[8] = htonl(number);
668 *(unsigned long*)&buffer[12] = htonl(start);
669 *(unsigned long*)&buffer[16] = htonl(duration);
671 pthread_mutex_lock(&mutex);
672 int a = tcp->sendPacket(buffer, 20);
676 pthread_mutex_unlock(&mutex);
682 pthread_mutex_unlock(&mutex);
689 pthread_mutex_unlock(&mutex);
694 EventList* eventList = new EventList();
696 while (packetPos < packetLength)
698 Event* event = new Event();
699 event->id = extractULONG();
700 event->time = extractULONG();
701 event->duration = extractULONG();
702 event->title = extractString();
703 event->subtitle = extractString();
704 event->description = extractString();
705 eventList->push_back(event);
706 // eventList->next();
710 pthread_mutex_unlock(&mutex);
712 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
717 Log* l = Log::getInstance();
720 l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
723 for(eventList->reset(); !eventList->eol(); eventList->next())
725 currentEvent = (Event*)eventList->getCurrent();
726 l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
733 ULLONG VDR::getResumePoint(char* fileName)
735 if (!connected) return 0;
737 char* resumeString = configLoad("ResumeData", fileName);
738 if (!resumeString) return 0;
740 ULLONG toReturn = strtoull(resumeString, NULL, 10);
741 delete[] resumeString;
745 int VDR::configSave(char* section, char* key, const char* value)
747 if (!connected) return 0;
749 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
750 UCHAR buffer[totalLength];
752 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
753 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
756 strcpy((char*)&buffer[position], section);
757 position += strlen(section) + 1;
758 strcpy((char*)&buffer[position], key);
759 position += strlen(key) + 1;
760 strcpy((char*)&buffer[position], value);
762 pthread_mutex_lock(&mutex);
763 unsigned int a = tcp->sendPacket(buffer, totalLength);
764 if (a != totalLength)
766 pthread_mutex_unlock(&mutex);
772 pthread_mutex_unlock(&mutex);
776 int toReturn = (int)extractULONG();
778 pthread_mutex_unlock(&mutex);
783 char* VDR::configLoad(char* section, char* key)
785 if (!connected) return 0;
787 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
788 UCHAR buffer[totalLength];
790 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
791 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
794 strcpy((char*)&buffer[position], section);
795 position += strlen(section) + 1;
796 strcpy((char*)&buffer[position], key);
798 pthread_mutex_lock(&mutex);
799 unsigned int a = tcp->sendPacket(buffer, totalLength);
800 if (a != totalLength)
802 pthread_mutex_unlock(&mutex);
808 pthread_mutex_unlock(&mutex);
811 char* toReturn = extractString();
813 pthread_mutex_unlock(&mutex);
818 RecTimerList* VDR::getRecTimersList()
820 if (!connected) return NULL;
824 *(unsigned long*)&buffer[0] = htonl(4);
825 *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS);
827 pthread_mutex_lock(&mutex);
828 int a = tcp->sendPacket(buffer, 8);
831 pthread_mutex_unlock(&mutex);
839 pthread_mutex_unlock(&mutex);
843 RecTimerList* recTimerList = new RecTimerList();
845 RecTimer* newRecTimer;
848 while (packetPos < packetLength)
850 newRecTimer = new RecTimer();
851 newRecTimer->active = extractULONG();
852 newRecTimer->recording = extractULONG();
853 newRecTimer->pending = extractULONG();
854 newRecTimer->priority = extractULONG();
855 newRecTimer->lifeTime = extractULONG();
856 newRecTimer->channelNumber = extractULONG();
857 newRecTimer->startTime = extractULONG();
858 newRecTimer->stopTime = extractULONG();
860 tempString = extractString();
861 newRecTimer->setFile(tempString);
864 newRecTimer->summary = extractString();
866 recTimerList->push_back(newRecTimer);
867 Log::getInstance()->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
868 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
869 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
873 pthread_mutex_unlock(&mutex);
877 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());