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<char*>& serverIPs)
70 char* message = "VOMP CLIENT";
71 DatagramSocket ds(port);
73 int haveAtLeastOne = 0;
81 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
82 ds.send("255.255.255.255", 3024, message, strlen(message));
84 retval = ds.waitforMessage(waitType);
86 if (retval == 2) // we got a reply
88 if (strcmp(ds.getData(), "VOMP SERVER")) // echo.....
95 strcpy(newIP, ds.getFromIPA());
96 serverIPs.push_back(newIP);
103 if (haveAtLeastOne) break;
109 void VDR::cancelFindingServer()
114 void VDR::setServerIP(char* newIP)
116 strcpy(serverIP, newIP);
123 if (tcp->connectTo(serverIP, 3024))
134 void VDR::disconnect()
139 Log::getInstance()->log("VDR", Log::DEBUG, "Disconnect");
142 ///////////////////////////////////////////////////////
146 packet = (UCHAR*)tcp->receivePacket();
147 if (!packet) return 0;
148 packetLength = tcp->getDataLength();
152 void VDR::freePacket()
154 // Must be called if getPacket return 1, except in getBlock
161 int VDR::serverError()
163 if ((packetPos == 0) && (packetLength == 4) && !ntohl(*(ULONG*)packet)) return 1;
167 char* VDR::extractString()
169 if (serverError()) return NULL;
171 int length = strlen((char*)&packet[packetPos]);
172 if ((packetPos + length) > packetLength) return NULL;
173 char* str = new char[length + 1];
174 strcpy(str, (char*)&packet[packetPos]);
175 packetPos += length + 1;
179 ULONG VDR::extractULONG()
181 if ((packetPos + sizeof(ULONG)) > packetLength) return 0;
182 ULONG ul = ntohl(*(ULONG*)&packet[packetPos]);
183 packetPos += sizeof(ULONG);
187 ULLONG VDR::extractULLONG()
189 if ((packetPos + sizeof(ULLONG)) > packetLength) return 0;
190 ULLONG ull = ntohll(*(ULLONG*)&packet[packetPos]);
191 packetPos += sizeof(ULLONG);
195 long VDR::extractLONG()
197 if ((packetPos + sizeof(long)) > packetLength) return 0;
198 long l = ntohl(*(long*)&packet[packetPos]);
199 packetPos += sizeof(long);
203 /////////////////////////////////////////////////////////////////////////////
207 if (!connected) return 0;
211 *(unsigned long*)&buffer[0] = htonl(4);
212 *(unsigned long*)&buffer[4] = htonl(VDR_LOGIN);
214 pthread_mutex_lock(&mutex);
215 int a = tcp->sendPacket(buffer, 8);
218 pthread_mutex_unlock(&mutex);
226 pthread_mutex_unlock(&mutex);
230 ULONG vdrTime = extractULONG();
231 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
232 long vdrTimeOffset = extractLONG();
233 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
236 pthread_mutex_unlock(&mutex);
239 struct timespec currentTime;
240 currentTime.tv_sec = vdrTime;
241 currentTime.tv_nsec = 0;
242 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
244 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
246 // now make a TZ variable and set it
250 if (vdrTimeOffset > 0) sign = '-';
253 vdrTimeOffset = abs(vdrTimeOffset);
255 hours = (int)vdrTimeOffset / 3600;
256 minutes = vdrTimeOffset % 3600;
258 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
260 minutes = (int)minutes / 60;
262 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
265 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
266 setenv("TZ", newTZ, 1);
268 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
273 Directory* VDR::getRecordingsList()
275 if (!connected) return 0;
279 *(unsigned long*)&buffer[0] = htonl(4);
280 *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST);
282 pthread_mutex_lock(&mutex);
283 int a = tcp->sendPacket(buffer, 8);
286 pthread_mutex_unlock(&mutex);
294 pthread_mutex_unlock(&mutex);
298 Directory* recDir = new Directory();
299 recDir->setName("/");
302 Directory::totalSpace = extractULONG();
303 Directory::freeSpace = extractULONG();
304 Directory::usedPercent = extractULONG();
308 while (packetPos < packetLength)
310 Recording* rec = new Recording();
312 rec->start = extractULONG();
314 string = extractString();
315 rec->setName(string);
318 rec->fileName = extractString();
322 char* dirName = rec->getDirName();
324 Directory* d = recDir->getDirByName(dirName);
329 Log::getInstance()->log("VDR", Log::DEBUG, "Added a new directory = %s", d->name);
330 recDir->dirList.push_back(d);
333 d->recList.push_back(rec);
337 recDir->recList.push_back(rec);
340 Log::getInstance()->log("VDR", Log::DEBUG, "%s", rec->fileName);
344 pthread_mutex_unlock(&mutex);
349 int VDR::deleteRecording(char* fileName)
351 if (!connected) return 0;
353 unsigned long totalLength = 8 + strlen(fileName) + 1;
354 UCHAR buffer[totalLength];
356 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
357 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
358 strcpy((char*)&buffer[8], fileName);
360 pthread_mutex_lock(&mutex);
361 unsigned int a = tcp->sendPacket(buffer, totalLength);
362 if (a != totalLength)
364 pthread_mutex_unlock(&mutex);
370 pthread_mutex_unlock(&mutex);
374 int toReturn = (int)extractULONG();
376 pthread_mutex_unlock(&mutex);
381 char* VDR::getRecordingSummary(char* fileName)
383 if (!connected) return 0;
385 unsigned long totalLength = 8 + strlen(fileName) + 1;
386 UCHAR buffer[totalLength];
388 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
389 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
390 strcpy((char*)&buffer[8], fileName);
392 pthread_mutex_lock(&mutex);
393 unsigned int a = tcp->sendPacket(buffer, totalLength);
394 if (a != totalLength)
396 pthread_mutex_unlock(&mutex);
402 pthread_mutex_unlock(&mutex);
405 char* toReturn = extractString();
407 pthread_mutex_unlock(&mutex);
412 ChannelList* VDR::getChannelsList(ULONG type)
414 if (!connected) return 0;
418 *(unsigned long*)&buffer[0] = htonl(4);
419 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
421 pthread_mutex_lock(&mutex);
422 int a = tcp->sendPacket(buffer, 8);
425 pthread_mutex_unlock(&mutex);
433 pthread_mutex_unlock(&mutex);
437 ChannelList* chanList = new ChannelList();
439 while (packetPos < packetLength)
441 Channel* chan = new Channel();
442 chan->number = extractULONG();
443 chan->type = extractULONG();
444 chan->name = extractString();
446 if (chan->type == type)
448 chanList->push_back(chan);
449 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
458 pthread_mutex_unlock(&mutex);
463 int VDR::streamChannel(ULONG number)
465 if (!connected) return 0;
469 *(unsigned long*)&buffer[0] = htonl(8);
470 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
471 *(unsigned long*)&buffer[8] = htonl(number);
473 pthread_mutex_lock(&mutex);
474 int a = tcp->sendPacket(buffer, 12);
478 pthread_mutex_unlock(&mutex);
484 pthread_mutex_unlock(&mutex);
488 int toReturn = (int)extractULONG();
490 pthread_mutex_unlock(&mutex);
495 int VDR::stopStreaming()
497 if (!connected) return 0;
501 *(unsigned long*)&buffer[0] = htonl(4);
502 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
504 pthread_mutex_lock(&mutex);
505 int a = tcp->sendPacket(buffer, 8);
509 pthread_mutex_unlock(&mutex);
515 pthread_mutex_unlock(&mutex);
519 int toReturn = (int)extractULONG();
521 pthread_mutex_unlock(&mutex);
526 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
528 if (!connected) return 0;
532 *(unsigned long*)&buffer[0] = htonl(16);
533 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
534 *(ULLONG*)&buffer[8] = htonll(position);
535 *(unsigned long*)&buffer[16] = htonl(maxAmount);
537 pthread_mutex_lock(&mutex);
538 int a = tcp->sendPacket(buffer, 20);
541 pthread_mutex_unlock(&mutex);
547 pthread_mutex_unlock(&mutex);
551 UCHAR* toReturn = packet;
552 *amountReceived = packetLength;
553 // Manually clean up instead of running freePacket to keep the block
557 pthread_mutex_unlock(&mutex);
562 ULLONG VDR::streamRecording(Recording* rec)
564 if (!connected) return 0;
566 unsigned long totalLength = 8 + strlen(rec->fileName) + 1;
567 UCHAR buffer[totalLength];
569 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
570 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
571 strcpy((char*)&buffer[8], rec->fileName);
573 pthread_mutex_lock(&mutex);
574 unsigned int a = tcp->sendPacket(buffer, totalLength);
575 if (a != totalLength)
577 pthread_mutex_unlock(&mutex);
583 pthread_mutex_unlock(&mutex);
587 ULLONG recordingLength = extractULLONG();
589 pthread_mutex_unlock(&mutex);
591 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
593 return recordingLength;
596 ULLONG VDR::rescanRecording()
598 if (!connected) return 0;
600 unsigned long totalLength = 8;
601 UCHAR buffer[totalLength];
603 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
604 *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
606 pthread_mutex_lock(&mutex);
607 unsigned int a = tcp->sendPacket(buffer, totalLength);
608 if (a != totalLength)
610 pthread_mutex_unlock(&mutex);
616 pthread_mutex_unlock(&mutex);
620 ULLONG recordingLength = extractULLONG();
622 pthread_mutex_unlock(&mutex);
624 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
626 return recordingLength;
629 EventList* VDR::getChannelSchedule(ULONG number)
631 if (!connected) return 0;
635 *(unsigned long*)&buffer[0] = htonl(8);
636 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
637 *(unsigned long*)&buffer[8] = htonl(number);
639 pthread_mutex_lock(&mutex);
640 int a = tcp->sendPacket(buffer, 12);
644 pthread_mutex_unlock(&mutex);
650 pthread_mutex_unlock(&mutex);
657 pthread_mutex_unlock(&mutex);
662 EventList* eventList = new EventList();
664 while (packetPos < packetLength)
666 Event* event = new Event();
667 event->id = extractULONG();
668 event->time = extractULONG();
669 event->duration = extractULONG();
670 event->title = extractString();
671 event->subtitle = extractString();
672 event->description = extractString();
673 eventList->push_back(event);
674 // eventList->next();
678 pthread_mutex_unlock(&mutex);
680 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
685 Log* l = Log::getInstance();
688 l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
691 for(eventList->reset(); !eventList->eol(); eventList->next())
693 currentEvent = (Event*)eventList->getCurrent();
694 l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
701 ULLONG VDR::getResumePoint(char* fileName)
703 if (!connected) return 0;
705 char* resumeString = configLoad("ResumeData", fileName);
706 if (!resumeString) return 0;
708 ULLONG toReturn = strtoull(resumeString, NULL, 10);
709 delete[] resumeString;
713 int VDR::configSave(char* section, char* key, char* value)
715 if (!connected) return 0;
717 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
718 UCHAR buffer[totalLength];
720 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
721 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
724 strcpy((char*)&buffer[position], section);
725 position += strlen(section) + 1;
726 strcpy((char*)&buffer[position], key);
727 position += strlen(key) + 1;
728 strcpy((char*)&buffer[position], value);
730 pthread_mutex_lock(&mutex);
731 unsigned int a = tcp->sendPacket(buffer, totalLength);
732 if (a != totalLength)
734 pthread_mutex_unlock(&mutex);
740 pthread_mutex_unlock(&mutex);
744 int toReturn = (int)extractULONG();
746 pthread_mutex_unlock(&mutex);
751 char* VDR::configLoad(char* section, char* key)
753 if (!connected) return 0;
755 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
756 UCHAR buffer[totalLength];
758 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
759 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
762 strcpy((char*)&buffer[position], section);
763 position += strlen(section) + 1;
764 strcpy((char*)&buffer[position], key);
766 pthread_mutex_lock(&mutex);
767 unsigned int a = tcp->sendPacket(buffer, totalLength);
768 if (a != totalLength)
770 pthread_mutex_unlock(&mutex);
776 pthread_mutex_unlock(&mutex);
779 char* toReturn = extractString();
781 pthread_mutex_unlock(&mutex);