]> git.vomp.tv Git - vompclient.git/blob - vdr.cc
Completion of move recording code. Fixes for dir counts, other bugs.
[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 char* 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   char* toReturn = NULL;
446   int success = (int)extractULONG();
447   if (success == 1)
448   {
449     toReturn = extractString();
450   }
451
452   freePacket();
453   MUTEX_UNLOCK(&mutex);
454
455   return toReturn;
456 }
457
458 char* VDR::getRecordingSummary(char* fileName)
459 {
460   unsigned long totalLength = 8 + strlen(fileName) + 1;
461   UCHAR* buffer = new UCHAR[totalLength];
462
463   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
464   *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
465   strcpy((char*)&buffer[8], fileName);
466
467   MUTEX_LOCK(&mutex);
468   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
469
470   unsigned int a = tcp->sendPacket(buffer, totalLength);
471   delete []buffer;
472
473   if (a != totalLength)
474   {
475     disconnect();
476     MUTEX_UNLOCK(&mutex);
477     return NULL;
478   }
479
480   if (!getPacket())
481   {
482     MUTEX_UNLOCK(&mutex);
483     return NULL;
484   }
485   char* toReturn = extractString();
486   freePacket();
487   MUTEX_UNLOCK(&mutex);
488
489   return toReturn;
490 }
491
492 ChannelList* VDR::getChannelsList(ULONG type)
493 {
494   UCHAR buffer[8];
495
496   *(unsigned long*)&buffer[0] = htonl(4);
497   *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
498
499   MUTEX_LOCK(&mutex);
500   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
501
502   int a = tcp->sendPacket(buffer, 8);
503   if (a != 8)
504   {
505     disconnect();
506     MUTEX_UNLOCK(&mutex);
507     return NULL;
508   }
509
510   // reply
511
512   if (!getPacket())
513   {
514     MUTEX_UNLOCK(&mutex);
515     return NULL;
516   }
517
518   ChannelList* chanList = new ChannelList();
519
520   while (packetPos < packetLength)
521   {
522     Channel* chan = new Channel();
523     chan->number = extractULONG();
524     chan->type = extractULONG();
525     chan->name = extractString();
526
527     if (chan->type == type)
528     {
529       chanList->push_back(chan);
530       Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
531     }
532     else
533     {
534       delete chan;
535     }
536   }
537
538   freePacket();
539   MUTEX_UNLOCK(&mutex);
540
541   return chanList;
542 }
543
544 int VDR::streamChannel(ULONG number)
545 {
546   UCHAR buffer[12];
547
548   *(unsigned long*)&buffer[0] = htonl(8);
549   *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
550   *(unsigned long*)&buffer[8] = htonl(number);
551
552   MUTEX_LOCK(&mutex);
553   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
554
555   int a = tcp->sendPacket(buffer, 12);
556
557   if (a != 12)
558   {
559     disconnect();
560     MUTEX_UNLOCK(&mutex);
561     return 0;
562   }
563
564   if (!getPacket())
565   {
566     MUTEX_UNLOCK(&mutex);
567     return 0;
568   }
569
570   int toReturn = (int)extractULONG();
571   freePacket();
572   MUTEX_UNLOCK(&mutex);
573
574   return toReturn;
575 }
576
577 int VDR::stopStreaming()
578 {
579   UCHAR buffer[8];
580
581   *(unsigned long*)&buffer[0] = htonl(4);
582   *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
583
584   MUTEX_LOCK(&mutex);
585   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
586
587   int a = tcp->sendPacket(buffer, 8);
588
589   if (a != 8)
590   {
591     disconnect();
592     MUTEX_UNLOCK(&mutex);
593     return 0;
594   }
595
596   if (!getPacket())
597   {
598     MUTEX_UNLOCK(&mutex);
599     return 0;
600   }
601
602   int toReturn = (int)extractULONG();
603   freePacket();
604   MUTEX_UNLOCK(&mutex);
605
606   return toReturn;
607 }
608
609 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
610 {
611   UCHAR buffer[20];
612
613   *(unsigned long*)&buffer[0] = htonl(16);
614   *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
615   *(ULLONG*)&buffer[8]        = htonll(position);
616   *(unsigned long*)&buffer[16] = htonl(maxAmount);
617
618   MUTEX_LOCK(&mutex);
619   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
620
621   int a = tcp->sendPacket(buffer, 20);
622   if (a != 20)
623   {
624     disconnect();
625     MUTEX_UNLOCK(&mutex);
626     return NULL;
627   }
628
629   if (!getPacket())
630   {
631     MUTEX_UNLOCK(&mutex);
632     return NULL;
633   }
634
635   if (serverError())
636   {
637     Log::getInstance()->log("VDR", Log::DEBUG, "Detected getblock 0");
638     freePacket();
639     MUTEX_UNLOCK(&mutex);
640     return NULL;
641   }
642
643   UCHAR* toReturn = packet;
644   *amountReceived = packetLength;
645   // Manually clean up instead of running freePacket to keep the block
646   packet = NULL;
647   packetLength = 0;
648   packetPos = 0;
649   MUTEX_UNLOCK(&mutex);
650
651
652   return toReturn;
653 }
654
655 ULLONG VDR::streamRecording(char* fileName)
656 {
657   unsigned long totalLength = 8 + strlen(fileName) + 1;
658   UCHAR* buffer = new UCHAR[totalLength];
659
660   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
661   *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
662   strcpy((char*)&buffer[8], fileName);
663
664   MUTEX_LOCK(&mutex);
665   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
666
667   unsigned int a = tcp->sendPacket(buffer, totalLength);
668   delete []buffer;
669
670   if (a != totalLength)
671   {
672     disconnect();
673     MUTEX_UNLOCK(&mutex);
674     return 0;
675   }
676
677   if (!getPacket())
678   {
679     MUTEX_UNLOCK(&mutex);
680     return 0;
681   }
682
683   ULLONG recordingLength = extractULLONG();
684   freePacket();
685   MUTEX_UNLOCK(&mutex);
686
687   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
688
689   return recordingLength;
690 }
691
692 ULLONG VDR::rescanRecording()
693 {
694   unsigned long totalLength = 8;
695   UCHAR* buffer = new UCHAR[totalLength];
696
697   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
698   *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
699
700   MUTEX_LOCK(&mutex);
701   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
702
703   unsigned int a = tcp->sendPacket(buffer, totalLength);
704   delete []buffer;
705
706   if (a != totalLength)
707   {
708     disconnect();
709     MUTEX_UNLOCK(&mutex);
710     return 0;
711   }
712
713   if (!getPacket())
714   {
715     MUTEX_UNLOCK(&mutex);
716     return 0;
717   }
718
719   ULLONG recordingLength = extractULLONG();
720   freePacket();
721   MUTEX_UNLOCK(&mutex);
722
723   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
724
725   return recordingLength;
726 }
727
728 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
729 {
730   unsigned long totalLength = 12;
731   UCHAR* buffer = new UCHAR[totalLength];
732
733   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
734   *(unsigned long*)&buffer[4] = htonl(VDR_POSFROMFRAME);
735   *(unsigned long*)&buffer[8] = htonl(frameNumber);
736
737   MUTEX_LOCK(&mutex);
738   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
739
740   unsigned int a = tcp->sendPacket(buffer, totalLength);
741   delete []buffer;
742
743   if (a != totalLength)
744   {
745     disconnect();
746     MUTEX_UNLOCK(&mutex);
747     return 0;
748   }
749
750   if (!getPacket())
751   {
752     MUTEX_UNLOCK(&mutex);
753     return 0;
754   }
755
756   ULLONG position = extractULLONG();
757   freePacket();
758   MUTEX_UNLOCK(&mutex);
759
760   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
761
762   return position;
763 }
764
765 ULONG VDR::frameNumberFromPosition(ULLONG position)
766 {
767   unsigned long totalLength = 16;
768   UCHAR* buffer = new UCHAR[totalLength];
769
770   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
771   *(unsigned long*)&buffer[4] = htonl(VDR_FRAMEFROMPOS);
772   *(ULLONG*)&buffer[8] = htonll(position);
773
774   MUTEX_LOCK(&mutex);
775   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
776
777   unsigned int a = tcp->sendPacket(buffer, totalLength);
778   delete []buffer;
779
780   if (a != totalLength)
781   {
782     disconnect();
783     MUTEX_UNLOCK(&mutex);
784     return 0;
785   }
786
787   if (!getPacket())
788   {
789     MUTEX_UNLOCK(&mutex);
790     return 0;
791   }
792
793   ULONG framenumber = extractULONG();
794   freePacket();
795   MUTEX_UNLOCK(&mutex);
796
797   Log::getInstance()->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
798
799   return framenumber;
800 }
801
802 EventList* VDR::getChannelSchedule(ULONG number)
803 {
804   time_t now;
805   time(&now);
806   return getChannelSchedule(number, now, 24 * 60 * 60);
807 }
808
809 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
810 {
811 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
812   UCHAR buffer[20];
813
814   *(unsigned long*)&buffer[0] = htonl(16);
815   *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
816   *(unsigned long*)&buffer[8] = htonl(number);
817   *(unsigned long*)&buffer[12] = htonl(start);
818   *(unsigned long*)&buffer[16] = htonl(duration);
819
820   MUTEX_LOCK(&mutex);
821   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
822
823   int a = tcp->sendPacket(buffer, 20);
824
825   if (a != 20)
826   {
827     disconnect();
828     MUTEX_UNLOCK(&mutex);
829     return NULL;
830   }
831
832   if (!getPacket())
833   {
834     MUTEX_UNLOCK(&mutex);
835     return NULL;
836   }
837
838   // received a ulong(0) - schedules error in the plugin
839   if (serverError())
840   {
841     freePacket();
842     MUTEX_UNLOCK(&mutex);
843     return NULL;
844   }
845
846   EventList* eventList = new EventList();
847
848   while (packetPos < packetLength)
849   {
850     Event* event = new Event();
851     event->id = extractULONG();
852     event->time = extractULONG();
853     event->duration = extractULONG();
854     event->title = extractString();
855     event->subtitle = extractString();
856     event->description = extractString();
857     eventList->push_back(event);
858 //    eventList->next();
859   }
860
861   freePacket();
862   MUTEX_UNLOCK(&mutex);
863
864   Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
865
866
867   // debug
868 /*
869   Log* l = Log::getInstance();
870
871
872   l->log("VDR", Log::DEBUG, "datalength = %i count = %i", dataLength, count);
873
874   Event* currentEvent;
875   for(eventList->reset(); !eventList->eol(); eventList->next())
876   {
877     currentEvent = (Event*)eventList->getCurrent();
878     l->log("VDR", Log::DEBUG, "%lu %lu %lu %s %s %s", currentEvent->id, currentEvent->time, currentEvent->duration, currentEvent->title, currentEvent->subtitle, currentEvent->description);
879   }
880 */
881
882   return eventList;
883 }
884
885 ULLONG VDR::getResumePoint(char* fileName)
886 {
887   if (!connected) return 0;
888
889   char* resumeString = configLoad("ResumeData", fileName);
890   if (!resumeString) return 0;
891
892   ULLONG toReturn = STRTOULL(resumeString, NULL, 10);
893   delete[] resumeString;
894   return toReturn;
895 }
896
897 int VDR::configSave(char* section, char* key, const char* value)
898 {
899   ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
900   UCHAR* buffer = new UCHAR[totalLength];
901
902   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
903   *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
904
905   int position = 8;
906   strcpy((char*)&buffer[position], section);
907   position += strlen(section) + 1;
908   strcpy((char*)&buffer[position], key);
909   position += strlen(key) + 1;
910   strcpy((char*)&buffer[position], value);
911
912   MUTEX_LOCK(&mutex);
913   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
914
915   unsigned int a = tcp->sendPacket(buffer, totalLength);
916   delete[] buffer;
917
918   if (a != totalLength)
919   {
920     disconnect();
921     MUTEX_UNLOCK(&mutex);
922     return 0;
923   }
924
925   if (!getPacket())
926   {
927     MUTEX_UNLOCK(&mutex);
928     return 0;
929   }
930
931   int toReturn = (int)extractULONG();
932   freePacket();
933   MUTEX_UNLOCK(&mutex);
934
935   return toReturn;
936 }
937
938 char* VDR::configLoad(char* section, char* key)
939 {
940   ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
941   UCHAR* buffer = new UCHAR[totalLength];
942
943   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
944   *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
945
946   int position = 8;
947   strcpy((char*)&buffer[position], section);
948   position += strlen(section) + 1;
949   strcpy((char*)&buffer[position], key);
950
951   MUTEX_LOCK(&mutex);
952   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
953
954   unsigned int a = tcp->sendPacket(buffer, totalLength);
955   delete[] buffer;
956
957   if (a != totalLength)
958   {
959     disconnect();
960     MUTEX_UNLOCK(&mutex);
961     return NULL;
962   }
963
964   if (!getPacket())
965   {
966     MUTEX_UNLOCK(&mutex);
967     return NULL;
968   }
969   char* toReturn = extractString();
970   freePacket();
971   MUTEX_UNLOCK(&mutex);
972
973   return toReturn;
974 }
975
976 RecTimerList* VDR::getRecTimersList()
977 {
978   UCHAR buffer[8];
979
980   *(unsigned long*)&buffer[0] = htonl(4);
981   *(unsigned long*)&buffer[4] = htonl(VDR_GETTIMERS);
982
983   MUTEX_LOCK(&mutex);
984   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
985
986   int a = tcp->sendPacket(buffer, 8);
987   if (a != 8)
988   {
989     disconnect();
990     MUTEX_UNLOCK(&mutex);
991     return NULL;
992   }
993
994   // reply
995
996   if (!getPacket())
997   {
998     MUTEX_UNLOCK(&mutex);
999     return NULL;
1000   }
1001
1002   RecTimerList* recTimerList = new RecTimerList();
1003
1004   ULONG numTimers = extractULONG();
1005   if (numTimers > 0)
1006   {
1007     RecTimer* newRecTimer;
1008     char* tempString;
1009
1010     while (packetPos < packetLength)
1011     {
1012       newRecTimer = new RecTimer();
1013       newRecTimer->active = extractULONG();
1014       newRecTimer->recording = extractULONG();
1015       newRecTimer->pending = extractULONG();
1016       newRecTimer->priority = extractULONG();
1017       newRecTimer->lifeTime = extractULONG();
1018       newRecTimer->channelNumber = extractULONG();
1019       newRecTimer->startTime = extractULONG();
1020       newRecTimer->stopTime = extractULONG();
1021
1022       tempString = extractString();
1023       newRecTimer->setFile(tempString);
1024       delete[] tempString;
1025
1026       recTimerList->push_back(newRecTimer);
1027       Log::getInstance()->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
1028         newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
1029         newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
1030     }
1031   }
1032
1033   freePacket();
1034   MUTEX_UNLOCK(&mutex);
1035
1036   // Sort the list
1037
1038   sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
1039
1040   return recTimerList;
1041 }
1042
1043 ULONG VDR::setEventTimer(char* timerString)
1044 {
1045   unsigned long totalLength = 8 + strlen(timerString) + 1;
1046   UCHAR* buffer = new UCHAR[totalLength];
1047
1048   *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
1049   *(unsigned long*)&buffer[4] = htonl(VDR_SETTIMER);
1050   strcpy((char*)&buffer[8], timerString);
1051
1052   MUTEX_LOCK(&mutex);
1053   if (!connected) { MUTEX_UNLOCK(&mutex); return 0; }
1054
1055   unsigned int a = tcp->sendPacket(buffer, totalLength);
1056   delete []buffer;
1057
1058   if (a != totalLength)
1059   {
1060     disconnect();
1061     MUTEX_UNLOCK(&mutex);
1062     return 0;
1063   }
1064
1065   if (!getPacket())
1066   {
1067     MUTEX_UNLOCK(&mutex);
1068     return 0;
1069   }
1070
1071   ULONG toReturn = extractULONG();
1072   freePacket();
1073   MUTEX_UNLOCK(&mutex);
1074
1075   return toReturn;
1076 }