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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include "vdrrequestpacket.h"
32 #include "vdrresponsepacket.h"
35 VDR* VDR::instance = NULL;
46 channelNumberWidth = 1;
47 TEMP_SINGLE_VDR_PR = NULL;
53 if (initted) shutdown();
56 VDR* VDR::getInstance()
61 int VDR::init(int tport)
63 if (initted) return 0;
66 logger = Log::getInstance();
72 if (!initted) return 0;
78 void VDR::findServers(vector<VDRServer>& servers)
80 Wol* wol = Wol::getInstance();
82 char* message = "VOMP";
84 DatagramSocket ds(port);
85 int haveAtLeastOne = 0;
88 bool firstloop = true;
95 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
96 ds.send("255.255.255.255", 3024, message, strlen(message));
97 if(!firstloop) wol->doWakeUp();
99 retval = ds.waitforMessage(waitType);
101 if (retval == 2) // we got a reply
103 if (!strcmp(ds.getData(), "VOMP")) // echo.....
110 newServer.ip = new char[16];
111 strcpy(newServer.ip, ds.getFromIPA());
113 if (ds.getDataLength() == 0)
115 newServer.name = new char[1];
116 newServer.name[0] = '\0';
120 newServer.name = new char[strlen(ds.getData())+1];
121 strcpy(newServer.name, ds.getData());
124 servers.push_back(newServer);
131 if (haveAtLeastOne) break;
136 sort(servers.begin(), servers.end(), ServerSorter());
139 void VDR::cancelFindingServer()
144 void VDR::setServerIP(char* newIP)
146 strcpy(serverIP, newIP);
151 maxChannelNumber = 0;
152 channelNumberWidth = 1;
156 if (tcp->connectTo(serverIP, 3024))
168 void VDR::disconnect()
174 logger->log("VDR", Log::DEBUG, "Disconnect");
177 void VDR::setReceiveWindow(size_t size)
179 if (connected) tcp->setReceiveWindow(size);
182 ///////////////////////////////////////////////////////
184 void VDR::threadMethod()
186 logger->log("VDR", Log::DEBUG, "VDR RUN");
188 threadSetKillable(); // FIXME - change this to deal with the EDRs
193 ULONG userDataLength;
199 VDR_ResponsePacket* vresp;
202 ULONG lastKAsent = 0;
203 ULONG lastKArecv = time(NULL);
208 timeNow = time(NULL);
210 readSuccess = tcp->readData((UCHAR*)&channelID, sizeof(ULONG)); // 2s timeout atm
214 logger->log("VDR", Log::DEBUG, "Net read timeout");
215 if (!tcp->isConnected()) { connectionDied(); return; } // return to stop this thread
220 /* This is the simple version of timeout system until I work out how to make it better
221 e.g. different timeout lengths for different requests, a decent keepalive system
222 running in parallel etc etc.
224 Ignore the stream channel
225 Start with lastPacketReceived = 0
226 When a packet comes in, set lastPacketReceived = time(NULL)
229 if last packet is over 20s ago
231 if lastKAsent is less than 20s ago, continue
233 if lastKAsent = 0, send a KA, continue
235 if lastKAsent is over 20s ago, connection is dead, kill it
240 if (!lastKAsent) // have not sent a KA
242 if (lastKArecv < (timeNow - 5))
244 logger->log("VDR", Log::DEBUG, "Sending KA packet");
245 if (!sendKA(timeNow))
247 logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
251 lastKAsent = timeNow;
256 if (lastKAsent <= (timeNow - 10))
258 logger->log("VDR", Log::DEBUG, "lastKA over 10s ago, calling connectionDied");
264 if (!readSuccess) continue; // no data was read but the connection is ok.
268 channelID = ntohl(channelID);
270 if (channelID == CHANNEL_REQUEST_RESPONSE)
272 if (!tcp->readData((UCHAR*)&requestID, sizeof(ULONG))) break;
273 requestID = ntohl(requestID);
274 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
275 userDataLength = ntohl(userDataLength);
276 if (userDataLength > 5000000) break; // how big can these packets get?
278 if (userDataLength > 0)
280 userData = (UCHAR*)malloc(userDataLength);
281 if (!userData) break;
282 if (!tcp->readData(userData, userDataLength)) break;
285 vresp = new VDR_ResponsePacket();
286 vresp->setResponse(requestID, userData, userDataLength);
287 logger->log("VDR", Log::DEBUG, "Rxd a response packet, requestID=%lu, len=%lu", requestID, userDataLength);
289 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
291 // If edFindAndCall returns true, edr was called and vresp was handed off.
292 // else, delete vresp here.
296 else if (channelID == CHANNEL_STREAM)
298 if (!tcp->readData((UCHAR*)&streamID, sizeof(ULONG))) break;
299 streamID = ntohl(streamID);
301 if (!tcp->readData((UCHAR*)&flag, sizeof(ULONG))) break;
304 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
305 userDataLength = ntohl(userDataLength);
307 if (userDataLength > 0)
309 userData = (UCHAR*)malloc(userDataLength);
310 if (!userData) break;
311 if (!tcp->readData(userData, userDataLength)) break;
314 vresp = new VDR_ResponsePacket();
315 vresp->setStream(streamID, flag, userData, userDataLength);
316 // logger->log("VDR", Log::DEBUG, "Rxd a stream packet, streamID=%lu, flag=%lu, len=%lu", streamID, flag, userDataLength);
318 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
320 // If edFindAndCall returns true, edr was called and vresp was handed off.
321 // else, delete vresp here.
325 else if (channelID == CHANNEL_KEEPALIVE)
328 if (!tcp->readData((UCHAR*)&KAreply, sizeof(ULONG))) break;
329 KAreply = (ULONG)ntohl(KAreply);
330 if (KAreply == lastKAsent) // successful KA response
333 lastKArecv = KAreply;
334 logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
339 logger->log("VDR", Log::ERR, "Rxd a response packet on channel %lu !!", channelID);
343 // Who deletes vresp?
344 // If RR, the individual protocol functions must delete vresp.
345 // If stream, the data and length is taken out in ed_cb_call and vresp is deleted there.
351 void VDR::connectionDied()
353 // Called from within threadMethod to do cleanup if it decides the connection has died
355 connected = false; // though actually it could still be connected until someone calls vdr->disconnect
357 // Need to wake up any waiting channel 1 request-response threads
358 // Normally this is done by a packet coming in with channelid and requestid
359 // Instead, go through the list and for each channel 1 edr, make an empty vresp
360 // An empty vresp will have userData == NULL, which means vresp->noResponse() == true
362 // If it's a stream receiver, generate a stream packet with flag == connection_lost
365 VDR_PacketReceiver* vdrpr;
366 VDR_ResponsePacket* vresp;
367 while(receivers.size())
369 vdrpr = (VDR_PacketReceiver*) *(receivers.begin());
370 if (vdrpr->receiverChannel == CHANNEL_REQUEST_RESPONSE)
372 vresp = new VDR_ResponsePacket();
373 vresp->setResponse(vdrpr->requestSerialNumber, NULL, 0);
374 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for request serial %lu", vdrpr->requestSerialNumber);
376 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
378 // If edFindAndCall returns true, edr was called and vresp was handed off.
379 // else, delete vresp here.
380 logger->log("VDR", Log::ERR, "Timeouts: no waiting thread found for request serial %lu !!!", vdrpr->requestSerialNumber);
385 else if (vdrpr->receiverChannel == CHANNEL_STREAM)
387 vresp = new VDR_ResponsePacket();
388 vresp->setStream(vdrpr->streamID, 2 /* connection-lost flag */ , NULL, 0);
389 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for streamid %lu", vdrpr->streamID);
391 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
393 // If edFindAndCall returns true, edr was called and vresp was handed off.
394 // else, delete vresp here.
395 logger->log("VDR", Log::ERR, "Timeouts: no waiting stream receiver found for streamid %lu !!!", vdrpr->streamID);
400 for(EDRL::iterator i = receivers.begin(); i != receivers.end(); i++)
401 if ((VDR_PacketReceiver*)*i == vdrpr) { receivers.erase(i); break; }
405 // Ok, all event receviers should be dealt with. just in case there weren't any, inform command
406 logger->log("VDR", Log::DEBUG, "edUnlock at end of connectionDied");
408 Command::getInstance()->connectionLost();
411 bool VDR::ed_cb_find(EDReceiver* edr, void* userTag)
413 // edr is a VDR_PacketReceiver object made in VDR::RequestResponse
414 // userTag is a VDR_ResponsePacket made in threadMethod
416 VDR_PacketReceiver* vdrpr = (VDR_PacketReceiver*)edr;
417 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
419 // Is vresp for vdrpr ?
421 ULONG packetChannel = vresp->getChannelID();
422 if (vdrpr->receiverChannel != packetChannel) return false;
424 if (packetChannel == CHANNEL_REQUEST_RESPONSE)
426 if (vdrpr->requestSerialNumber == vresp->getRequestID()) return true;
428 else if (packetChannel == CHANNEL_STREAM)
430 if (vdrpr->streamID == vresp->getStreamID()) return true;
436 VDR_ResponsePacket* VDR::RequestResponse(VDR_RequestPacket* vrp)
438 logger->log("VDR", Log::DEBUG, "RR %lu", vrp->getOpcode());
442 logger->log("VDR", Log::DEBUG, "RR when !connected");
443 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
444 return vresp; // "no-response" return
447 // ED make new VDR and register
448 // make a VDR_PacketReceiver
449 // - init with serial number of request packet
451 VDR_PacketReceiver vdrpr;
452 // vdrpr.requestTime = time(NULL);
453 vdrpr.receiverChannel = VDR::CHANNEL_REQUEST_RESPONSE;
454 vdrpr.requestSerialNumber = vrp->getSerial();
460 if ((ULONG)tcp->sendData(vrp->getPtr(), vrp->getLen()) != vrp->getLen())
463 edUnregister(&vdrpr);
464 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
465 return vresp; // "no-response" return
468 // Sleep and block this thread. The sleep unlocks the mutex
469 logger->log("VDR", Log::DEBUG, "RR sleep");
470 edSleepThisReceiver(&vdrpr);
471 logger->log("VDR", Log::DEBUG, "RR unsleep");
473 // Woken because a response packet has arrived, mutex will be locked
476 return vdrpr.save_vresp;
479 bool VDR::sendKA(ULONG timeStamp)
482 *(ULONG*)&buffer[0] = htonl(CHANNEL_KEEPALIVE);
483 *(ULONG*)&buffer[4] = htonl(timeStamp);
484 if ((ULONG)tcp->sendData(buffer, 8) != 8) return false;
488 /////////////////////////////////////////////////////////////////////////////
490 // Here VDR takes a break for the VDR_PacketReceiver helper class
492 bool VDR_PacketReceiver::call(void* userTag)
494 if (receiverChannel == VDR::CHANNEL_REQUEST_RESPONSE)
496 // It's a RR. Save vresp and, signal the waiting thread and return.
497 // VDR::RequestResponse will be blocking waiting for this to happen.
498 // That function has a pointer to this object and can read save_vresp.
499 save_vresp = (VDR_ResponsePacket*)userTag;
500 return true; // Signals ED to remove edr from receivers and wake up edr thread
503 if (receiverChannel == VDR::CHANNEL_STREAM)
505 // It's a stream packet.
506 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
507 streamReceiver->streamReceive(vresp->getFlag(), vresp->getUserData(), vresp->getUserDataLength());
512 abort(); // unknown receiverChannel, should not happen
515 /////////////////////////////////////////////////////////////////////////////
519 VDR_RequestPacket vrp;
520 if (!vrp.init(VDR_LOGIN, true, 6)) return 0;
523 tcp->getMAC((char*)mactemp);
524 if (!vrp.copyin((UCHAR*)mactemp, 6)) return 0;
526 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
527 if (vresp->noResponse()) { delete vresp; return 0; }
529 ULONG vdrTime = vresp->extractULONG();
530 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
531 long vdrTimeOffset = vresp->extractLONG();
532 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
536 // Set the time and zone on the MVP
539 struct timespec currentTime;
540 currentTime.tv_sec = vdrTime;
541 currentTime.tv_nsec = 0;
542 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
544 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
546 // now make a TZ variable and set it
550 if (vdrTimeOffset > 0) sign = '-';
553 vdrTimeOffset = abs(vdrTimeOffset);
555 hours = (int)vdrTimeOffset / 3600;
556 minutes = vdrTimeOffset % 3600;
558 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
560 minutes = (int)minutes / 60;
562 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
565 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
566 setenv("TZ", newTZ, 1);
568 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
574 bool VDR::getRecordingsList(RecMan* recman)
576 VDR_RequestPacket vrp;
577 if (!vrp.init(VDR_GETRECORDINGLIST, true, 0)) return false;
579 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
580 if (vresp->noResponse()) { delete vresp; return false; }
582 ULONG totalSpace = vresp->extractULONG();
583 ULONG freeSpace = vresp->extractULONG();
584 ULONG percent = vresp->extractULONG();
585 recman->setStats(totalSpace, freeSpace, percent);
591 while (!vresp->end())
593 start = vresp->extractULONG();
594 name = vresp->extractString();
595 fileName = vresp->extractString();
597 recman->addEntry(start, name, fileName);
608 int VDR::deleteRecording(char* fileName)
610 VDR_RequestPacket vrp;
611 if (!vrp.init(VDR_DELETERECORDING, true, strlen(fileName) + 1)) return 0;
612 if (!vrp.addString(fileName)) return 0;
614 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
615 if (vresp->noResponse()) { delete vresp; return 0; }
617 int toReturn = (int)vresp->extractULONG();
623 char* VDR::moveRecording(char* fileName, char* newPath)
625 VDR_RequestPacket vrp;
626 if (!vrp.init(VDR_MOVERECORDING, true, strlen(fileName) + 1 + strlen(newPath) + 1)) return NULL;
627 if (!vrp.addString(fileName)) return NULL;
628 if (!vrp.addString(newPath)) return NULL;
630 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
631 if (vresp->noResponse()) { delete vresp; return NULL; }
633 char* toReturn = NULL;
634 int success = (int)vresp->extractULONG();
637 toReturn = vresp->extractString();
645 ChannelList* VDR::getChannelsList(ULONG type)
647 VDR_RequestPacket vrp;
648 if (!vrp.init(VDR_GETCHANNELLIST, true, 0)) return NULL;
650 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
651 if (vresp->noResponse()) { delete vresp; return NULL; }
653 ChannelList* chanList = new ChannelList();
655 while (!vresp->end())
657 Channel* chan = new Channel();
658 chan->number = vresp->extractULONG();
659 chan->type = vresp->extractULONG();
660 chan->name = vresp->extractString();
662 if (chan->type == type)
664 chanList->push_back(chan);
665 logger->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
666 if (chan->number > maxChannelNumber) maxChannelNumber = chan->number;
676 if (maxChannelNumber > 99999)
677 channelNumberWidth = 6;
678 else if (maxChannelNumber > 9999)
679 channelNumberWidth = 5;
680 else if (maxChannelNumber > 999)
681 channelNumberWidth = 4;
682 else if (maxChannelNumber > 99)
683 channelNumberWidth = 3;
684 else if (maxChannelNumber > 9)
685 channelNumberWidth = 2;
687 channelNumberWidth = 1;
692 int VDR::streamChannel(ULONG number, StreamReceiver* tstreamReceiver)
694 VDR_RequestPacket vrp;
695 if (!vrp.init(VDR_STREAMCHANNEL, true, sizeof(ULONG))) return 0;
696 if (!vrp.addULONG(number)) return 0;
699 VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
700 vdrpr->receiverChannel = VDR::CHANNEL_STREAM;
701 vdrpr->streamID = vrp.getSerial();
702 vdrpr->streamReceiver = tstreamReceiver;
704 TEMP_SINGLE_VDR_PR = vdrpr;
706 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
707 if (vresp->noResponse())
715 int toReturn = (int)vresp->extractULONG();
716 logger->log("VDR", Log::DEBUG, "VDR said %lu to start streaming request", toReturn);
722 int VDR::stopStreaming()
724 VDR_RequestPacket vrp;
725 if (!vrp.init(VDR_STOPSTREAMING, true, 0)) return 0;
727 if (TEMP_SINGLE_VDR_PR) // this block only needs to be done if it was a live stream
728 // TEMP_SINGLE_VDR_PR will not be set unless we are streaming a channel
730 edUnregister(TEMP_SINGLE_VDR_PR);
731 delete TEMP_SINGLE_VDR_PR;
732 TEMP_SINGLE_VDR_PR = NULL;
735 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
736 if (vresp->noResponse()) { delete vresp; return 0; }
738 int toReturn = (int)vresp->extractULONG();
744 UCHAR* VDR::getImageBlock(ULONG position, UINT maxAmount, UINT* amountReceived)
746 return getBlock(position, maxAmount, amountReceived, VDR_GETIMAGEBLOCK);
749 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
751 return getBlock(position, maxAmount, amountReceived, VDR_GETBLOCK);
754 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived, ULONG cmd)
756 VDR_RequestPacket vrp;
757 if (!vrp.init(cmd, true, sizeof(ULLONG) + sizeof(ULONG))) return NULL;
758 if (!vrp.addULLONG(position)) return NULL;
759 if (!vrp.addULONG(maxAmount)) return NULL;
761 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
762 if (vresp->noResponse()) { delete vresp; return NULL; }
764 if (vresp->serverError())
766 logger->log("VDR", Log::DEBUG, "Detected getblock 0");
771 // Special handling for getblock
772 UCHAR* toReturn = vresp->getUserData();
773 *amountReceived = vresp->getUserDataLength();
780 ULLONG VDR::streamRecording(char* fileName, ULONG* totalFrames)
782 VDR_RequestPacket vrp;
783 if (!vrp.init(VDR_STREAMRECORDING, true, strlen(fileName) + 1)) return 0;
784 if (!vrp.addString(fileName)) return 0;
786 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
787 if (vresp->noResponse()) { delete vresp; return 0; }
789 ULLONG lengthBytes = vresp->extractULLONG();
790 ULONG lengthFrames = vresp->extractULONG();
793 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
795 *totalFrames = lengthFrames;
799 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
801 VDR_RequestPacket vrp;
802 if (!vrp.init(VDR_POSFROMFRAME, true, sizeof(ULONG))) return 0;
803 if (!vrp.addULONG(frameNumber)) return 0;
805 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
806 if (vresp->noResponse()) { delete vresp; return 0; }
808 ULLONG position = vresp->extractULLONG();
811 logger->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
816 ULONG VDR::frameNumberFromPosition(ULLONG position)
818 VDR_RequestPacket vrp;
819 if (!vrp.init(VDR_FRAMEFROMPOS, true, sizeof(ULLONG))) return 0;
820 if (!vrp.addULLONG(position)) return 0;
822 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
823 if (vresp->noResponse()) { delete vresp; return 0; }
825 ULONG framenumber = vresp->extractULONG();
828 logger->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
833 bool VDR::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
835 VDR_RequestPacket vrp;
836 if (!vrp.init(VDR_GETNEXTIFRAME, true, sizeof(ULONG)*2)) return false;
837 if (!vrp.addULONG(frameNumber)) return false;
838 if (!vrp.addULONG(direction)) return false;
840 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
841 if (vresp->noResponse()) { delete vresp; return false; }
843 if (vresp->serverError())
845 logger->log("VDR", Log::DEBUG, "Detected getNextIFrame error");
850 *rfilePosition = vresp->extractULLONG();
851 *rframeNumber = vresp->extractULONG();
852 *rframeLength = vresp->extractULONG();
856 // logger->log("VDR", Log::DEBUG, "VDR GNIF said %llu %lu %lu", *rfilePosition, *rframeNumber, *rframeLength);
861 EventList* VDR::getChannelSchedule(ULONG number)
865 return getChannelSchedule(number, now, 24 * 60 * 60);
868 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
870 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
872 VDR_RequestPacket vrp;
873 if (!vrp.init(VDR_GETCHANNELSCHEDULE, true, sizeof(ULONG)*3)) return NULL;
874 if (!vrp.addULONG(number)) return NULL;
875 if (!vrp.addULONG(start)) return NULL;
876 if (!vrp.addULONG(duration)) return NULL;
878 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
879 if (vresp->noResponse()) { delete vresp; return NULL; }
881 // received a ulong(0) - schedules error in the plugin
882 if (vresp->serverError())
888 EventList* eventList = new EventList();
890 while (!vresp->end())
892 Event* event = new Event();
893 event->id = vresp->extractULONG();
894 event->time = vresp->extractULONG();
895 event->duration = vresp->extractULONG();
896 event->title = vresp->extractString();
897 event->subtitle = vresp->extractString();
898 event->description = vresp->extractString();
899 eventList->push_back(event);
904 logger->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
908 int VDR::configSave(const char* section, const char* key, const char* value)
910 VDR_RequestPacket vrp;
911 if (!vrp.init(VDR_CONFIGSAVE, false, 0)) return 0;
912 if (!vrp.addString(section)) return 0;
913 if (!vrp.addString(key)) return 0;
914 if (!vrp.addString(value)) return 0;
916 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
917 if (vresp->noResponse()) { delete vresp; return 0; }
919 int toReturn = (int)vresp->extractULONG();
925 char* VDR::configLoad(const char* section, const char* key)
927 VDR_RequestPacket vrp;
928 if (!vrp.init(VDR_CONFIGLOAD, false, 0)) return NULL;
929 if (!vrp.addString(section)) return NULL;
930 if (!vrp.addString(key)) return NULL;
932 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
933 if (vresp->noResponse()) { delete vresp; return NULL; }
935 char* toReturn = vresp->extractString();
941 RecTimerList* VDR::getRecTimersList()
943 VDR_RequestPacket vrp;
944 if (!vrp.init(VDR_GETTIMERS, true, 0)) return NULL;
946 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
947 if (vresp->noResponse()) { delete vresp; return NULL; }
949 RecTimerList* recTimerList = new RecTimerList();
951 ULONG numTimers = vresp->extractULONG();
954 RecTimer* newRecTimer;
957 while (!vresp->end())
959 newRecTimer = new RecTimer();
960 newRecTimer->active = vresp->extractULONG();
961 newRecTimer->recording = vresp->extractULONG();
962 newRecTimer->pending = vresp->extractULONG();
963 newRecTimer->priority = vresp->extractULONG();
964 newRecTimer->lifeTime = vresp->extractULONG();
965 newRecTimer->channelNumber = vresp->extractULONG();
966 newRecTimer->startTime = vresp->extractULONG();
967 newRecTimer->stopTime = vresp->extractULONG();
968 newRecTimer->day = vresp->extractULONG();
969 newRecTimer->weekDays = vresp->extractULONG();
971 tempString = vresp->extractString();
972 newRecTimer->setFile(tempString);
975 recTimerList->push_back(newRecTimer);
976 logger->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
977 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
978 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
984 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
989 ULONG VDR::setEventTimer(char* timerString)
991 VDR_RequestPacket vrp;
992 if (!vrp.init(VDR_SETTIMER, true, strlen(timerString) + 1)) return 0;
993 if (!vrp.addString(timerString)) return 0;
995 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
996 if (vresp->noResponse()) { delete vresp; return 0; }
998 ULONG toReturn = vresp->extractULONG();
1004 RecInfo* VDR::getRecInfo(char* fileName)
1006 VDR_RequestPacket vrp;
1007 if (!vrp.init(VDR_GETRECINFO, true, strlen(fileName) + 1)) return NULL;
1008 if (!vrp.addString(fileName)) return NULL;
1010 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1011 if (vresp->noResponse()) { delete vresp; return NULL; }
1013 if (vresp->serverError())
1015 logger->log("VDR", Log::DEBUG, "Could not get rec info");
1020 RecInfo* recInfo = new RecInfo();
1022 recInfo->timerStart = vresp->extractULONG();
1023 recInfo->timerEnd = vresp->extractULONG();
1024 recInfo->resumePoint = vresp->extractULONG();
1025 recInfo->summary = vresp->extractString();
1027 ULONG numComponents = vresp->extractULONG();
1030 recInfo->setNumComponents(numComponents);
1031 for (ULONG i = 0; i < numComponents; i++)
1033 recInfo->streams[i] = vresp->extractUCHAR();
1034 recInfo->types[i] = vresp->extractUCHAR();
1035 recInfo->languages[i] = vresp->extractString();
1036 recInfo->descriptions[i] = vresp->extractString();
1047 ULLONG VDR::rescanRecording(ULONG* totalFrames)
1049 VDR_RequestPacket vrp;
1050 if (!vrp.init(VDR_RESCANRECORDING, true, 0)) return 0;
1052 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1053 if (vresp->noResponse()) { delete vresp; return 0; }
1055 ULLONG lengthBytes = vresp->extractULLONG();
1056 ULONG lengthFrames = vresp->extractULONG();
1059 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
1061 *totalFrames = lengthFrames;
1065 MarkList* VDR::getMarks(char* fileName)
1067 VDR_RequestPacket vrp;
1068 if (!vrp.init(VDR_GETMARKS, true, strlen(fileName) + 1)) return NULL;
1069 if (!vrp.addString(fileName)) return NULL;
1071 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1072 if (vresp->noResponse()) { delete vresp; return NULL; }
1074 if (vresp->serverError())
1080 MarkList* markList = new MarkList();
1082 while (!vresp->end())
1084 Mark* mark = new Mark();
1085 mark->pos = vresp->extractULONG();
1087 markList->push_back(mark);
1088 logger->log("VDR", Log::DEBUG, "Have added a mark to list. %lu", mark->pos);
1096 void VDR::getChannelPids(Channel* channel)
1098 VDR_RequestPacket vrp;
1099 if (!vrp.init(VDR_GETCHANNELPIDS, true, sizeof(ULONG))) return ;
1100 if (!vrp.addULONG(channel->number)) return ;
1102 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1103 if (vresp->noResponse()) { delete vresp; return ; }
1105 // Format of response
1113 channel->vpid = vresp->extractULONG();
1114 channel->numAPids = vresp->extractULONG();
1116 for (ULONG i = 0; i < channel->numAPids; i++)
1119 newapid.pid = vresp->extractULONG();
1120 newapid.name = vresp->extractString();
1121 channel->apids.push_back(newapid);
1124 channel->numDPids = vresp->extractULONG();
1126 for (ULONG i = 0; i < channel->numDPids; i++)
1129 newdpid.pid = vresp->extractULONG();
1130 newdpid.name = vresp->extractString();
1131 channel->dpids.push_back(newdpid);
1134 channel->numSPids = vresp->extractULONG();
1136 for (ULONG i = 0; i < channel->numSPids; i++)
1139 newspid.pid = vresp->extractULONG();
1140 newspid.name = vresp->extractString();
1141 channel->spids.push_back(newspid);
1143 channel->tpid = vresp->extractULONG();
1151 * media List Request:
1152 * 4 flags (currently unused)
1155 * Media List response:
1163 * 4 strlen (incl. 0 Byte)
1167 MediaList* VDR::getMediaList(const char* parent,int mediaType)
1169 logger->log("VDR", Log::DEBUG, "getMediaList %s,type=%d", (parent?parent:"NULL"), mediaType);
1171 VDR_RequestPacket vrp;
1172 if (!vrp.init(VDR_GETMEDIALIST, false, 0)) return NULL;
1173 if (!vrp.addULONG(0)) return NULL; // unused flags
1177 if (!vrp.addString(parent)) return NULL;
1180 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1181 if (vresp->noResponse()) { delete vresp; return NULL; }
1183 if (vresp->serverError())
1189 if (vresp->getUserDataLength() < 12)
1191 logger->log("VDR", Log::ERR, "receiveMediaList packet too short, expected 12, got %d", vresp->getUserDataLength());
1196 MediaList* mediaList = new MediaList();
1198 code = vresp->extractULONG();
1199 ULONG numEntries = vresp->extractULONG();
1200 Log::getInstance()->log("VDR", Log::DEBUG, "receiveMediaList with %d entries",numEntries);
1201 while (!vresp->end() && numEntries >0)
1203 Media* m = new Media();
1204 ULONG mtype = vresp->extractULONG();
1205 ULONG mtime=vresp->extractULONG();
1207 flags=vresp->extractULONG();
1208 ULONG stsize=vresp->extractULONG();
1209 char * name=vresp->extractString();
1210 if (! name || stsize != (strlen(name)+1)) {
1211 Log::getInstance()->log("VDR", Log::ERR, "receiveMediaList invalid packet entry, read size %d, strlen %d", stsize, strlen(name)+1);
1217 //ignore . and .. entries
1218 if (strcmp(name,".") == 0 || strcmp(name,"..")==0) {
1222 m->setFileName(name);
1224 m->setMediaType(mtype);
1225 mediaList->push_back(m);
1226 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a media to list. %s, type=%d, time=%d", name,mtype,mtime);
1235 * get image Request:
1236 * 4 flags (currently unused)
1241 * get image response:
1246 ULONG VDR::loadImage(const char* fileName, ULONG x, ULONG y)
1248 VDR_RequestPacket vrp;
1249 if (!vrp.init(VDR_GETIMAGE, false, 0)) return 0;
1250 if (!vrp.addULONG(0)) return 0; // unused flags
1251 if (!vrp.addULONG(x)) return 0;
1252 if (!vrp.addULONG(y)) return 0;
1253 if (!vrp.addString(fileName)) return 0;
1255 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1256 if (vresp->noResponse()) { delete vresp; return 0; }
1258 ULONG cmd = vresp->extractULONG();
1259 ULONG lengthBytes = vresp->extractULONG();
1262 Log::getInstance()->log("VDR", Log::DEBUG, "getImage %s: cmd=%lu len=%lu", fileName, cmd, lengthBytes);
1266 int VDR::deleteTimer(RecTimer* delTimer)
1268 Log::getInstance()->log("VDR", Log::DEBUG, "Delete timer called");
1270 VDR_RequestPacket vrp;
1271 if (!vrp.init(VDR_DELETETIMER, false, 0)) return 0;
1272 if (!vrp.addULONG(delTimer->channelNumber)) return 0;
1273 if (!vrp.addULONG(delTimer->weekDays)) return 0;
1274 if (!vrp.addULONG(delTimer->day)) return 0;
1275 if (!vrp.addULONG(delTimer->startTime)) return 0;
1276 if (!vrp.addULONG(delTimer->stopTime)) return 0;
1278 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1279 if (vresp->noResponse()) { delete vresp; return 0; }
1281 int toReturn = (int)vresp->extractULONG();
1287 I18n::lang_code_list VDR::getLanguageList()
1289 I18n::lang_code_list CodeList;
1290 CodeList["en"] = "English"; // Default entry
1291 VDR_RequestPacket vrp;
1292 if (!vrp.init(VDR_GETLANGUAGELIST, false, 0)) return CodeList;
1293 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1294 if (vresp->noResponse() || vresp->end())
1300 while (!vresp->end())
1302 char* c_code = vresp->extractString();
1303 char* c_name = vresp->extractString();
1304 string code = c_code;
1305 string name = c_name;
1306 CodeList[code] = name;
1314 int VDR::getLanguageContent(const std::string code, I18n::trans_table& texts)
1316 VDR_RequestPacket vrp;
1317 if (!vrp.init(VDR_GETLANGUAGECONTENT, false, 0)) return 0;
1318 if (!vrp.addString(code.c_str())) return 0;
1319 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1320 if (vresp->noResponse()) { delete vresp; return 0; }
1322 while (!vresp->end())
1324 char* c_key = vresp->extractString();
1325 char* c_text = vresp->extractString();
1327 string text = c_text;