]> git.vomp.tv Git - vompclient.git/blob - vdr.cc
Windows port
[vompclient.git] / vdr.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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
19 */
20
21 #include "vdr.h"
22
23 VDR* VDR::instance = NULL;
24
25 #ifndef WIN32
26 #define MUTEX_LOCK(mutex) pthread_mutex_lock(mutex)
27 #define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex)
28 #else
29 #define MUTEX_LOCK(mutex) WaitForSingleObject(*(mutex), INFINITE )
30 #define MUTEX_UNLOCK(mutex) ReleaseMutex(*(mutex))
31 #endif
32
33
34 VDR::VDR()
35 {
36   if (instance) return;
37   instance = this;
38   initted = 0;
39   findingServer = 0;
40   tcp = NULL;
41 #ifndef WIN32
42   pthread_mutex_init(&mutex, NULL);
43 #else
44   mutex=CreateMutex(NULL,FALSE,NULL);
45 #endif
46   packetLength = 0;
47   packetPos = 0;
48   packet = NULL;
49   connected = false;
50 }
51
52 VDR::~VDR()
53 {
54 #ifdef WIN32
55   CloseHandle(mutex);
56 #endif
57   instance = NULL;
58   if (initted) shutdown();
59 }
60
61 VDR* VDR::getInstance()
62 {
63   return instance;
64 }
65
66 int VDR::init(int tport)
67 {
68   if (initted) return 0;
69   initted = 1;
70   port = tport;
71   logger = Log::getInstance();
72   return 1;
73 }
74
75 int VDR::shutdown()
76 {
77   if (!initted) return 0;
78   initted = 0;
79   disconnect();
80   return 1;
81 }
82
83 void VDR::findServers(vector<VDRServer>& servers)
84 {
85   findingServer = 1;
86   char* message = "VOMP";
87
88   DatagramSocket ds(port);
89   int haveAtLeastOne = 0;
90   int retval;
91   int waitType = 1;
92   while(findingServer)
93   {
94     if (waitType == 1)
95     {
96       ds.shutdown();
97       ds.init();
98       logger->log("VDR", Log::NOTICE, "Broadcasting for server");
99       ds.send("255.255.255.255", 3024, message, strlen(message));
100     }
101     retval = ds.waitforMessage(waitType);
102
103     if (retval == 2) // we got a reply
104     {
105       if (!strcmp(ds.getData(), "VOMP")) // echo.....
106       {
107         waitType = 2;
108       }
109       else
110       {
111         VDRServer newServer;
112         newServer.ip = new char[16];
113         strcpy(newServer.ip, ds.getFromIPA());
114
115         if (ds.getDataLength() == 0)
116         {
117           newServer.name = new char[1];
118           newServer.name[0] = '\0';
119         }
120         else
121         {
122           newServer.name = new char[strlen(ds.getData())+1];
123           strcpy(newServer.name, ds.getData());
124         }
125
126         servers.push_back(newServer);
127         waitType = 2;
128         haveAtLeastOne = 1;
129       }
130     }
131     else
132     {
133       if (haveAtLeastOne) break;
134       waitType = 1;
135     }
136   }
137   sort(servers.begin(), servers.end(), ServerSorter());
138 }
139
140 void VDR::cancelFindingServer()
141 {
142   findingServer = 0;
143 }
144
145 void VDR::setServerIP(char* newIP)
146 {
147   strcpy(serverIP, newIP);
148 }
149
150 int VDR::connect()
151 {
152   if (tcp) delete tcp;
153   tcp = new TCP();
154   if (tcp->connectTo(serverIP, 3024))
155   {
156     connected = true;
157     return 1;
158   }
159   else
160   {
161     return 0;
162   }
163 }
164
165 void VDR::disconnect()
166 {
167   if (tcp) delete tcp;
168   tcp = NULL;
169   connected = false;
170   Log::getInstance()->log("VDR", Log::DEBUG, "Disconnect");
171 }
172
173 void VDR::setReceiveWindow(size_t size)
174 {
175   if (connected) tcp->setReceiveWindow(size);
176 }
177
178 ///////////////////////////////////////////////////////
179
180 int VDR::getPacket()
181 {
182   packet = (UCHAR*)tcp->receivePacket();
183   if (!packet)
184   {
185     disconnect();
186     return 0;
187   }
188   packetLength = tcp->getDataLength();
189   return 1;
190 }
191
192 void VDR::freePacket()
193 {
194   // Must be called if getPacket return 1, except in getBlock
195   packetLength = 0;
196   packetPos = 0;
197   free(packet);
198   packet = NULL;
199 }
200
201 int VDR::serverError()
202 {
203   if ((packetPos == 0) && (packetLength == 4) && !ntohl(*(ULONG*)packet)) return 1;
204   else return 0;
205 }
206
207 char* VDR::extractString()
208 {
209   if (serverError()) return NULL;
210
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;
216   return str;
217 }
218
219 ULONG VDR::extractULONG()
220 {
221   if ((packetPos + sizeof(ULONG)) > packetLength) return 0;
222   ULONG ul = ntohl(*(ULONG*)&packet[packetPos]);
223   packetPos += sizeof(ULONG);
224   return ul;
225 }
226
227 ULLONG VDR::extractULLONG()
228 {
229   if ((packetPos + sizeof(ULLONG)) > packetLength) return 0;
230   ULLONG ull = ntohll(*(ULLONG*)&packet[packetPos]);
231   packetPos += sizeof(ULLONG);
232   return ull;
233 }
234
235 long VDR::extractLONG()
236 {
237   if ((packetPos + sizeof(long)) > packetLength) return 0;
238   long l = ntohl(*(long*)&packet[packetPos]);
239   packetPos += sizeof(long);
240   return l;
241 }
242
243 /////////////////////////////////////////////////////////////////////////////
244
245 int VDR::doLogin()
246 {
247   UCHAR buffer[14];
248
249   *(unsigned long*)&buffer[0] = htonl(10);
250   *(unsigned long*)&buffer[4] = htonl(VDR_LOGIN);
251
252   tcp->getMAC((char*)&buffer[8]);
253
254
255   MUTEX_LOCK(&mutex);
256   if (!connected)
257   {
258     MUTEX_UNLOCK(&mutex);
259     return 0;
260   }
261
262   int a = tcp->sendPacket(buffer, 14);
263   if (a != 14)
264   {
265     disconnect();
266     MUTEX_UNLOCK(&mutex);
267     return 0;
268   }
269
270   // reply
271
272   if (!getPacket())
273   {
274     MUTEX_UNLOCK(&mutex);
275     return 0;
276   }
277
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);
282
283   freePacket();
284   MUTEX_UNLOCK(&mutex);
285
286   // Set the time and zone on the MVP
287
288 #ifndef WIN32
289   struct timespec currentTime;
290   currentTime.tv_sec = vdrTime;
291   currentTime.tv_nsec = 0;
292   int b = clock_settime(CLOCK_REALTIME, &currentTime);
293
294   logger->log("VDR", Log::DEBUG, "set clock = %u", b);
295
296   // now make a TZ variable and set it
297   char sign;
298   int hours;
299   int minutes;
300   if (vdrTimeOffset > 0) sign = '-';
301   else sign = '+';
302
303   vdrTimeOffset = abs(vdrTimeOffset);
304
305   hours = (int)vdrTimeOffset / 3600;
306   minutes = vdrTimeOffset % 3600;
307
308   logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
309
310   minutes = (int)minutes / 60;
311
312   logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
313
314   char newTZ[30];
315   sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
316   setenv("TZ", newTZ, 1);
317
318   logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
319 #endif
320
321   return 1;
322 }
323
324 bool VDR::getRecordingsList(RecMan* recman)
325 {
326   UCHAR buffer[8];
327
328   *(unsigned long*)&buffer[0] = htonl(4);
329   *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST);
330
331   MUTEX_LOCK(&mutex);
332   if (!connected)
333   {
334     MUTEX_UNLOCK(&mutex);
335     return 0;
336   }
337
338   int a = tcp->sendPacket(buffer, 8);
339   if (a != 8)
340   {
341     disconnect();
342     MUTEX_UNLOCK(&mutex);
343     return false;
344   }
345
346   // reply
347
348   if (!getPacket())
349   {
350     MUTEX_UNLOCK(&mutex);
351     return false;
352   }
353
354   ULONG totalSpace = extractULONG();
355   ULONG freeSpace = extractULONG();
356   ULONG percent = extractULONG();
357   recman->setStats(totalSpace, freeSpace, percent);
358
359   ULONG start;
360   char* name;
361   char* fileName;
362
363   while (packetPos < packetLength)
364   {
365     start = extractULONG();
366     name = extractString();
367     fileName = extractString();
368
369     recman->addEntry(start, name, fileName);
370
371     delete[] name;
372     delete[] fileName;
373   }
374
375   freePacket();
376   MUTEX_UNLOCK(&mutex);
377
378   return true;
379 }
380
381 int VDR::deleteRecording(char* fileName)
382 {
383   unsigned long totalLength = 8 + strlen(fileName) + 1;
384   UCHAR* buffer = new UCHAR[totalLength];
385
386   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
387   *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
388   strcpy((char*)&buffer[8], fileName);
389
390   MUTEX_LOCK(&mutex);
391   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
392
393   unsigned int a = tcp->sendPacket(buffer, totalLength);
394   delete []buffer;
395
396   if (a != totalLength)
397   {
398     disconnect();
399     MUTEX_UNLOCK(&mutex);
400     return 0;
401   }
402
403   if (!getPacket())
404   {
405     MUTEX_UNLOCK(&mutex);
406     return 0;
407   }
408
409   int toReturn = (int)extractULONG();
410   freePacket();
411   MUTEX_UNLOCK(&mutex);
412
413   return toReturn;
414 }
415
416 int VDR::moveRecording(char* fileName, char* newPath)
417 {
418   unsigned long totalLength = 8 + strlen(fileName) + 1 + strlen(newPath) + 1;
419   UCHAR* buffer = new UCHAR[totalLength];
420
421   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
422   *(unsigned long*)&buffer[4] = htonl(VDR_MOVERECORDING);
423   strcpy((char*)&buffer[8], fileName);
424   strcpy((char*)&buffer[8 + strlen(fileName) + 1], newPath);
425
426   MUTEX_LOCK(&mutex);
427   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
428
429   unsigned int a = tcp->sendPacket(buffer, totalLength);
430   delete []buffer;
431
432   if (a != totalLength)
433   {
434     disconnect();
435     MUTEX_UNLOCK(&mutex);
436     return 0;
437   }
438
439   if (!getPacket())
440   {
441     MUTEX_UNLOCK(&mutex);
442     return 0;
443   }
444
445   int toReturn = (int)extractULONG();
446   freePacket();
447   MUTEX_UNLOCK(&mutex);
448
449   return toReturn;
450 }
451
452 char* VDR::getRecordingSummary(char* fileName)
453 {
454   unsigned long totalLength = 8 + strlen(fileName) + 1;
455   UCHAR* buffer = new UCHAR[totalLength];
456
457   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
458   *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
459   strcpy((char*)&buffer[8], fileName);
460
461   MUTEX_LOCK(&mutex);
462   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
463
464   unsigned int a = tcp->sendPacket(buffer, totalLength);
465   delete []buffer;
466
467   if (a != totalLength)
468   {
469     disconnect();
470     MUTEX_UNLOCK(&mutex);
471     return NULL;
472   }
473
474   if (!getPacket())
475   {
476     MUTEX_UNLOCK(&mutex);
477     return NULL;
478   }
479   char* toReturn = extractString();
480   freePacket();
481   MUTEX_UNLOCK(&mutex);
482
483   return toReturn;
484 }
485
486 ChannelList* VDR::getChannelsList(ULONG type)
487 {
488   UCHAR buffer[8];
489
490   *(unsigned long*)&buffer[0] = htonl(4);
491   *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
492
493   MUTEX_LOCK(&mutex);
494   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
495
496   int a = tcp->sendPacket(buffer, 8);
497   if (a != 8)
498   {
499     disconnect();
500     MUTEX_UNLOCK(&mutex);
501     return NULL;
502   }
503
504   // reply
505
506   if (!getPacket())
507   {
508     MUTEX_UNLOCK(&mutex);
509     return NULL;
510   }
511
512   ChannelList* chanList = new ChannelList();
513
514   while (packetPos < packetLength)
515   {
516     Channel* chan = new Channel();
517     chan->number = extractULONG();
518     chan->type = extractULONG();
519     chan->name = extractString();
520
521     if (chan->type == type)
522     {
523       chanList->push_back(chan);
524       Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
525     }
526     else
527     {
528       delete chan;
529     }
530   }
531
532   freePacket();
533   MUTEX_UNLOCK(&mutex);
534
535   return chanList;
536 }
537
538 int VDR::streamChannel(ULONG number)
539 {
540   UCHAR buffer[12];
541
542   *(unsigned long*)&buffer[0] = htonl(8);
543   *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
544   *(unsigned long*)&buffer[8] = htonl(number);
545
546   MUTEX_LOCK(&mutex);
547   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
548
549   int a = tcp->sendPacket(buffer, 12);
550
551   if (a != 12)
552   {
553     disconnect();
554     MUTEX_UNLOCK(&mutex);
555     return 0;
556   }
557
558   if (!getPacket())
559   {
560     MUTEX_UNLOCK(&mutex);
561     return 0;
562   }
563
564   int toReturn = (int)extractULONG();
565   freePacket();
566   MUTEX_UNLOCK(&mutex);
567
568   return toReturn;
569 }
570
571 int VDR::stopStreaming()
572 {
573   UCHAR buffer[8];
574
575   *(unsigned long*)&buffer[0] = htonl(4);
576   *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
577
578   MUTEX_LOCK(&mutex);
579   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
580
581   int a = tcp->sendPacket(buffer, 8);
582
583   if (a != 8)
584   {
585     disconnect();
586     MUTEX_UNLOCK(&mutex);
587     return 0;
588   }
589
590   if (!getPacket())
591   {
592     MUTEX_UNLOCK(&mutex);
593     return 0;
594   }
595
596   int toReturn = (int)extractULONG();
597   freePacket();
598   MUTEX_UNLOCK(&mutex);
599
600   return toReturn;
601 }
602
603 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
604 {
605   UCHAR buffer[20];
606
607   *(unsigned long*)&buffer[0] = htonl(16);
608   *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
609   *(ULLONG*)&buffer[8]        = htonll(position);
610   *(unsigned long*)&buffer[16] = htonl(maxAmount);
611
612   MUTEX_LOCK(&mutex);
613   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
614
615   int a = tcp->sendPacket(buffer, 20);
616   if (a != 20)
617   {
618     disconnect();
619     MUTEX_UNLOCK(&mutex);
620     return NULL;
621   }
622
623   if (!getPacket())
624   {
625     MUTEX_UNLOCK(&mutex);
626     return NULL;
627   }
628
629   if (serverError())
630   {
631     Log::getInstance()->log("VDR", Log::DEBUG, "Detected getblock 0");
632     freePacket();
633     MUTEX_UNLOCK(&mutex);
634     return NULL;
635   }
636
637   UCHAR* toReturn = packet;
638   *amountReceived = packetLength;
639   // Manually clean up instead of running freePacket to keep the block
640   packet = NULL;
641   packetLength = 0;
642   packetPos = 0;
643   MUTEX_UNLOCK(&mutex);
644
645
646   return toReturn;
647 }
648
649 ULLONG VDR::streamRecording(char* fileName)
650 {
651   unsigned long totalLength = 8 + strlen(fileName) + 1;
652   UCHAR* buffer = new UCHAR[totalLength];
653
654   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
655   *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
656   strcpy((char*)&buffer[8], fileName);
657
658   MUTEX_LOCK(&mutex);
659   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
660
661   unsigned int a = tcp->sendPacket(buffer, totalLength);
662   delete []buffer;
663
664   if (a != totalLength)
665   {
666     disconnect();
667     MUTEX_UNLOCK(&mutex);
668     return 0;
669   }
670
671   if (!getPacket())
672   {
673     MUTEX_UNLOCK(&mutex);
674     return 0;
675   }
676
677   ULLONG recordingLength = extractULLONG();
678   freePacket();
679   MUTEX_UNLOCK(&mutex);
680
681   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
682
683   return recordingLength;
684 }
685
686 ULLONG VDR::rescanRecording()
687 {
688   unsigned long totalLength = 8;
689   UCHAR* buffer = new UCHAR[totalLength];
690
691   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
692   *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
693
694   MUTEX_LOCK(&mutex);
695   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
696
697   unsigned int a = tcp->sendPacket(buffer, totalLength);
698   delete []buffer;
699
700   if (a != totalLength)
701   {
702     disconnect();
703     MUTEX_UNLOCK(&mutex);
704     return 0;
705   }
706
707   if (!getPacket())
708   {
709     MUTEX_UNLOCK(&mutex);
710     return 0;
711   }
712
713   ULLONG recordingLength = extractULLONG();
714   freePacket();
715   MUTEX_UNLOCK(&mutex);
716
717   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
718
719   return recordingLength;
720 }
721
722 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
723 {
724   unsigned long totalLength = 12;
725   UCHAR* buffer = new UCHAR[totalLength];
726
727   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
728   *(unsigned long*)&buffer[4] = htonl(VDR_POSFROMFRAME);
729   *(unsigned long*)&buffer[8] = htonl(frameNumber);
730
731   MUTEX_LOCK(&mutex);
732   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
733
734   unsigned int a = tcp->sendPacket(buffer, totalLength);
735   delete []buffer;
736
737   if (a != totalLength)
738   {
739     disconnect();
740     MUTEX_UNLOCK(&mutex);
741     return 0;
742   }
743
744   if (!getPacket())
745   {
746     MUTEX_UNLOCK(&mutex);
747     return 0;
748   }
749
750   ULLONG position = extractULLONG();
751   freePacket();
752   MUTEX_UNLOCK(&mutex);
753
754   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
755
756   return position;
757 }
758
759 ULONG VDR::frameNumberFromPosition(ULLONG position)
760 {
761   unsigned long totalLength = 16;
762   UCHAR* buffer = new UCHAR[totalLength];
763
764   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
765   *(unsigned long*)&buffer[4] = htonl(VDR_FRAMEFROMPOS);
766   *(ULLONG*)&buffer[8] = htonll(position);
767
768   MUTEX_LOCK(&mutex);
769   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
770
771   unsigned int a = tcp->sendPacket(buffer, totalLength);
772   delete []buffer;
773
774   if (a != totalLength)
775   {
776     disconnect();
777     MUTEX_UNLOCK(&mutex);
778     return 0;
779   }
780
781   if (!getPacket())
782   {
783     MUTEX_UNLOCK(&mutex);
784     return 0;
785   }
786
787   ULONG framenumber = extractULONG();
788   freePacket();
789   MUTEX_UNLOCK(&mutex);
790
791   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
792
793   return framenumber;
794 }
795
796 EventList* VDR::getChannelSchedule(ULONG number)
797 {
798   time_t now;
799   time(&now);
800   return getChannelSchedule(number, now, 24 * 60 * 60);
801 }
802
803 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
804 {
805 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
806   UCHAR buffer[20];
807
808   *(unsigned long*)&buffer[0] = htonl(16);
809   *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
810   *(unsigned long*)&buffer[8] = htonl(number);
811   *(unsigned long*)&buffer[12] = htonl(start);
812   *(unsigned long*)&buffer[16] = htonl(duration);
813
814   MUTEX_LOCK(&mutex);
815   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
816
817   int a = tcp->sendPacket(buffer, 20);
818
819   if (a != 20)
820   {
821     disconnect();
822     MUTEX_UNLOCK(&mutex);
823     return NULL;
824   }
825
826   if (!getPacket())
827   {
828     MUTEX_UNLOCK(&mutex);
829     return NULL;
830   }
831
832   // received a ulong(0) - schedules error in the plugin
833   if (serverError())
834   {
835     freePacket();
836     MUTEX_UNLOCK(&mutex);
837     return NULL;
838   }
839
840   EventList* eventList = new EventList();
841
842   while (packetPos < packetLength)
843   {
844     Event* event = new Event();
845     event->id = extractULONG();
846     event->time = extractULONG();
847     event->duration = extractULONG();
848     event->title = extractString();
849     event->subtitle = extractString();
850     event->description = extractString();
851     eventList->push_back(event);
852 //    eventList->next();
853   }
854
855   freePacket();
856   MUTEX_UNLOCK(&mutex);
857
858   Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
859
860
861   // debug
862 /*
863   Log* l = Log::getInstance();
864
865
866   l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
867
868   Event* currentEvent;
869   for(eventList->reset(); !eventList->eol(); eventList->next())
870   {
871     currentEvent = (Event*)eventList->getCurrent();
872     l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
873   }
874 */
875
876   return eventList;
877 }
878
879 ULLONG VDR::getResumePoint(char* fileName)
880 {
881   if (!connected) return 0;
882
883   char* resumeString = configLoad("ResumeData", fileName);
884   if (!resumeString) return 0;
885
886   ULLONG toReturn = STRTOULL(resumeString, NULL, 10);
887   delete[] resumeString;
888   return toReturn;
889 }
890
891 int VDR::configSave(char* section, char* key, const char* value)
892 {
893   ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
894   UCHAR* buffer = new UCHAR[totalLength];
895
896   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
897   *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
898
899   int position = 8;
900   strcpy((char*)&buffer[position], section);
901   position += strlen(section) + 1;
902   strcpy((char*)&buffer[position], key);
903   position += strlen(key) + 1;
904   strcpy((char*)&buffer[position], value);
905
906   MUTEX_LOCK(&mutex);
907   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
908
909   unsigned int a = tcp->sendPacket(buffer, totalLength);
910   delete[] buffer;
911
912   if (a != totalLength)
913   {
914     disconnect();
915     MUTEX_UNLOCK(&mutex);
916     return 0;
917   }
918
919   if (!getPacket())
920   {
921     MUTEX_UNLOCK(&mutex);
922     return 0;
923   }
924
925   int toReturn = (int)extractULONG();
926   freePacket();
927   MUTEX_UNLOCK(&mutex);
928
929   return toReturn;
930 }
931
932 char* VDR::configLoad(char* section, char* key)
933 {
934   ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
935   UCHAR* buffer = new UCHAR[totalLength];
936
937   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
938   *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
939
940   int position = 8;
941   strcpy((char*)&buffer[position], section);
942   position += strlen(section) + 1;
943   strcpy((char*)&buffer[position], key);
944
945   MUTEX_LOCK(&mutex);
946   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
947
948   unsigned int a = tcp->sendPacket(buffer, totalLength);
949   delete[] buffer;
950
951   if (a != totalLength)
952   {
953     disconnect();
954     MUTEX_UNLOCK(&mutex);
955     return NULL;
956   }
957
958   if (!getPacket())
959   {
960     MUTEX_UNLOCK(&mutex);
961     return NULL;
962   }
963   char* toReturn = extractString();
964   freePacket();
965   MUTEX_UNLOCK(&mutex);
966
967   return toReturn;
968 }
969
970 RecTimerList* VDR::getRecTimersList()
971 {
972   UCHAR buffer[8];
973
974   *(unsigned long*)&buffer[0] = htonl(4);
975   *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS);
976
977   MUTEX_LOCK(&mutex);
978   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
979
980   int a = tcp->sendPacket(buffer, 8);
981   if (a != 8)
982   {
983     disconnect();
984     MUTEX_UNLOCK(&mutex);
985     return NULL;
986   }
987
988   // reply
989
990   if (!getPacket())
991   {
992     MUTEX_UNLOCK(&mutex);
993     return NULL;
994   }
995
996   RecTimerList* recTimerList = new RecTimerList();
997
998   ULONG numTimers = extractULONG();
999   if (numTimers > 0)
1000   {
1001     RecTimer* newRecTimer;
1002     char* tempString;
1003
1004     while (packetPos < packetLength)
1005     {
1006       newRecTimer = new RecTimer();
1007       newRecTimer->active = extractULONG();
1008       newRecTimer->recording = extractULONG();
1009       newRecTimer->pending = extractULONG();
1010       newRecTimer->priority = extractULONG();
1011       newRecTimer->lifeTime = extractULONG();
1012       newRecTimer->channelNumber = extractULONG();
1013       newRecTimer->startTime = extractULONG();
1014       newRecTimer->stopTime = extractULONG();
1015
1016       tempString = extractString();
1017       newRecTimer->setFile(tempString);
1018       delete[] tempString;
1019
1020       recTimerList->push_back(newRecTimer);
1021       Log::getInstance()->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1022         newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1023         newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1024     }
1025   }
1026
1027   freePacket();
1028   MUTEX_UNLOCK(&mutex);
1029
1030   // Sort the list
1031
1032   sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1033
1034   return recTimerList;
1035 }
1036
1037 ULONG VDR::setEventTimer(char* timerString)
1038 {
1039   unsigned long totalLength = 8 + strlen(timerString) + 1;
1040   UCHAR* buffer = new UCHAR[totalLength];
1041
1042   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
1043   *(unsigned long*)&buffer[4] = htonl(VDR_SETTIMER);
1044   strcpy((char*)&buffer[8], timerString);
1045
1046   MUTEX_LOCK(&mutex);
1047   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
1048
1049   unsigned int a = tcp->sendPacket(buffer, totalLength);
1050   delete []buffer;
1051
1052   if (a != totalLength)
1053   {
1054     disconnect();
1055     MUTEX_UNLOCK(&mutex);
1056     return 0;
1057   }
1058
1059   if (!getPacket())
1060   {
1061     MUTEX_UNLOCK(&mutex);
1062     return 0;
1063   }
1064
1065   ULONG toReturn = extractULONG();
1066   freePacket();
1067   MUTEX_UNLOCK(&mutex);
1068
1069   return toReturn;
1070 }