2 Copyright 2004-2008 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 if (!lastKAsent) // have not sent a KA
222 if (lastKArecv < (timeNow - 5))
224 logger->log("VDR", Log::DEBUG, "Sending KA packet");
225 if (!sendKA(timeNow))
227 logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
231 lastKAsent = timeNow;
236 if (lastKAsent <= (timeNow - 10))
238 logger->log("VDR", Log::DEBUG, "lastKA over 10s ago, calling connectionDied");
244 if (!readSuccess) continue; // no data was read but the connection is ok.
248 channelID = ntohl(channelID);
250 if (channelID == CHANNEL_REQUEST_RESPONSE)
252 if (!tcp->readData((UCHAR*)&requestID, sizeof(ULONG))) break;
253 requestID = ntohl(requestID);
254 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
255 userDataLength = ntohl(userDataLength);
256 if (userDataLength > 5000000) break; // how big can these packets get?
258 if (userDataLength > 0)
260 userData = (UCHAR*)malloc(userDataLength);
261 if (!userData) break;
262 if (!tcp->readData(userData, userDataLength)) break;
265 vresp = new VDR_ResponsePacket();
266 vresp->setResponse(requestID, userData, userDataLength);
267 logger->log("VDR", Log::DEBUG, "Rxd a response packet, requestID=%lu, len=%lu", requestID, userDataLength);
269 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
271 // If edFindAndCall returns true, edr was called and vresp was handed off.
272 // else, delete vresp here.
276 else if (channelID == CHANNEL_STREAM)
278 if (!tcp->readData((UCHAR*)&streamID, sizeof(ULONG))) break;
279 streamID = ntohl(streamID);
281 if (!tcp->readData((UCHAR*)&flag, sizeof(ULONG))) break;
284 if (!tcp->readData((UCHAR*)&userDataLength, sizeof(ULONG))) break;
285 userDataLength = ntohl(userDataLength);
287 if (userDataLength > 0)
289 userData = (UCHAR*)malloc(userDataLength);
290 if (!userData) break;
291 if (!tcp->readData(userData, userDataLength)) break;
294 vresp = new VDR_ResponsePacket();
295 vresp->setStream(streamID, flag, userData, userDataLength);
296 // logger->log("VDR", Log::DEBUG, "Rxd a stream packet, streamID=%lu, flag=%lu, len=%lu", streamID, flag, userDataLength);
298 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
300 // If edFindAndCall returns true, edr was called and vresp was handed off.
301 // else, delete vresp here.
305 else if (channelID == CHANNEL_KEEPALIVE)
308 if (!tcp->readData((UCHAR*)&KAreply, sizeof(ULONG))) break;
309 KAreply = (ULONG)ntohl(KAreply);
310 if (KAreply == lastKAsent) // successful KA response
313 lastKArecv = KAreply;
314 logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
319 logger->log("VDR", Log::ERR, "Rxd a response packet on channel %lu !!", channelID);
323 // Who deletes vresp?
324 // If RR, the individual protocol functions must delete vresp.
325 // If stream, the data and length is taken out in ed_cb_call and vresp is deleted there.
331 void VDR::connectionDied()
333 // Called from within threadMethod to do cleanup if it decides the connection has died
335 connected = false; // though actually it could still be connected until someone calls vdr->disconnect
337 // Need to wake up any waiting channel 1 request-response threads
338 // Normally this is done by a packet coming in with channelid and requestid
339 // Instead, go through the list and for each channel 1 edr, make an empty vresp
340 // An empty vresp will have userData == NULL, which means vresp->noResponse() == true
342 // If it's a stream receiver, generate a stream packet with flag == connection_lost
345 VDR_PacketReceiver* vdrpr;
346 VDR_ResponsePacket* vresp;
347 while(receivers.size())
349 vdrpr = (VDR_PacketReceiver*) *(receivers.begin());
350 if (vdrpr->receiverChannel == CHANNEL_REQUEST_RESPONSE)
352 vresp = new VDR_ResponsePacket();
353 vresp->setResponse(vdrpr->requestSerialNumber, NULL, 0);
354 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for request serial %lu", vdrpr->requestSerialNumber);
356 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
358 // If edFindAndCall returns true, edr was called and vresp was handed off.
359 // else, delete vresp here.
360 logger->log("VDR", Log::ERR, "Timeouts: no waiting thread found for request serial %lu !!!", vdrpr->requestSerialNumber);
365 else if (vdrpr->receiverChannel == CHANNEL_STREAM)
367 vresp = new VDR_ResponsePacket();
368 vresp->setStream(vdrpr->streamID, 2 /* connection-lost flag */ , NULL, 0);
369 logger->log("VDR", Log::DEBUG, "Timeouts: created blank response packet for streamid %lu", vdrpr->streamID);
371 if (!edFindAndCall(vresp)) // makes ED lock, find receiver for vresp (using ed_cb_find() ) and then call (using ed_cb_call() )
373 // If edFindAndCall returns true, edr was called and vresp was handed off.
374 // else, delete vresp here.
375 logger->log("VDR", Log::ERR, "Timeouts: no waiting stream receiver found for streamid %lu !!!", vdrpr->streamID);
380 for(EDRL::iterator i = receivers.begin(); i != receivers.end(); i++)
381 if ((VDR_PacketReceiver*)*i == vdrpr) { receivers.erase(i); break; }
385 // Ok, all event receviers should be dealt with. just in case there weren't any, inform command
386 logger->log("VDR", Log::DEBUG, "edUnlock at end of connectionDied");
388 Command::getInstance()->connectionLost();
391 bool VDR::ed_cb_find(EDReceiver* edr, void* userTag)
393 // edr is a VDR_PacketReceiver object made in VDR::RequestResponse
394 // userTag is a VDR_ResponsePacket made in threadMethod
396 VDR_PacketReceiver* vdrpr = (VDR_PacketReceiver*)edr;
397 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
399 // Is vresp for vdrpr ?
401 ULONG packetChannel = vresp->getChannelID();
402 if (vdrpr->receiverChannel != packetChannel) return false;
404 if (packetChannel == CHANNEL_REQUEST_RESPONSE)
406 if (vdrpr->requestSerialNumber == vresp->getRequestID()) return true;
408 else if (packetChannel == CHANNEL_STREAM)
410 if (vdrpr->streamID == vresp->getStreamID()) return true;
416 VDR_ResponsePacket* VDR::RequestResponse(VDR_RequestPacket* vrp)
418 //logger->log("VDR", Log::DEBUG, "RR %lu", vrp->getOpcode());
422 logger->log("VDR", Log::DEBUG, "RR when !connected");
423 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
424 return vresp; // "no-response" return
427 // ED make new VDR and register
428 // make a VDR_PacketReceiver
429 // - init with serial number of request packet
431 VDR_PacketReceiver vdrpr;
432 // vdrpr.requestTime = time(NULL);
433 vdrpr.receiverChannel = VDR::CHANNEL_REQUEST_RESPONSE;
434 vdrpr.requestSerialNumber = vrp->getSerial();
440 if ((ULONG)tcp->sendData(vrp->getPtr(), vrp->getLen()) != vrp->getLen())
443 edUnregister(&vdrpr);
444 VDR_ResponsePacket* vresp = new VDR_ResponsePacket();
445 return vresp; // "no-response" return
448 // Sleep and block this thread. The sleep unlocks the mutex
449 logger->log("VDR", Log::DEBUG, "RR sleep - opcode %lu", vrp->getOpcode());
450 edSleepThisReceiver(&vdrpr);
451 logger->log("VDR", Log::DEBUG, "RR unsleep");
453 // Woken because a response packet has arrived, mutex will be locked
456 return vdrpr.save_vresp;
459 bool VDR::sendKA(ULONG timeStamp)
462 *(ULONG*)&buffer[0] = htonl(CHANNEL_KEEPALIVE);
463 *(ULONG*)&buffer[4] = htonl(timeStamp);
464 if ((ULONG)tcp->sendData(buffer, 8) != 8) return false;
468 /////////////////////////////////////////////////////////////////////////////
470 // Here VDR takes a break for the VDR_PacketReceiver helper class
472 bool VDR_PacketReceiver::call(void* userTag)
474 if (receiverChannel == VDR::CHANNEL_REQUEST_RESPONSE)
476 // It's a RR. Save vresp and, signal the waiting thread and return.
477 // VDR::RequestResponse will be blocking waiting for this to happen.
478 // That function has a pointer to this object and can read save_vresp.
479 save_vresp = (VDR_ResponsePacket*)userTag;
480 return true; // Signals ED to remove edr from receivers and wake up edr thread
483 if (receiverChannel == VDR::CHANNEL_STREAM)
485 // It's a stream packet.
486 VDR_ResponsePacket* vresp = (VDR_ResponsePacket*)userTag;
487 streamReceiver->streamReceive(vresp->getFlag(), vresp->getUserData(), vresp->getUserDataLength());
492 abort(); // unknown receiverChannel, should not happen
495 /////////////////////////////////////////////////////////////////////////////
499 VDR_RequestPacket vrp;
500 if (!vrp.init(VDR_LOGIN, true, 6)) return 0;
503 tcp->getMAC((char*)mactemp);
504 if (!vrp.copyin((UCHAR*)mactemp, 6)) return 0;
506 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
507 if (vresp->noResponse()) { delete vresp; return 0; }
509 ULONG vdrTime = vresp->extractULONG();
510 logger->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
511 long vdrTimeOffset = vresp->extractLONG();
512 logger->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
516 // Set the time and zone on the MVP
519 struct timespec currentTime;
520 currentTime.tv_sec = vdrTime;
521 currentTime.tv_nsec = 0;
522 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
524 logger->log("VDR", Log::DEBUG, "set clock = %u", b);
526 // now make a TZ variable and set it
530 if (vdrTimeOffset > 0) sign = '-';
533 vdrTimeOffset = abs(vdrTimeOffset);
535 hours = (int)vdrTimeOffset / 3600;
536 minutes = vdrTimeOffset % 3600;
538 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
540 minutes = (int)minutes / 60;
542 logger->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
545 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
546 setenv("TZ", newTZ, 1);
548 logger->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
554 bool VDR::getRecordingsList(RecMan* recman)
556 VDR_RequestPacket vrp;
557 if (!vrp.init(VDR_GETRECORDINGLIST, true, 0)) return false;
559 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
560 if (vresp->noResponse()) { delete vresp; return false; }
562 ULONG totalSpace = vresp->extractULONG();
563 ULONG freeSpace = vresp->extractULONG();
564 ULONG percent = vresp->extractULONG();
565 recman->setStats(totalSpace, freeSpace, percent);
571 while (!vresp->end())
573 start = vresp->extractULONG();
574 name = vresp->extractString();
575 fileName = vresp->extractString();
577 recman->addEntry(start, name, fileName);
588 int VDR::deleteRecording(char* fileName)
590 VDR_RequestPacket vrp;
591 if (!vrp.init(VDR_DELETERECORDING, true, strlen(fileName) + 1)) return 0;
592 if (!vrp.addString(fileName)) return 0;
594 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
595 if (vresp->noResponse()) { delete vresp; return 0; }
597 int toReturn = (int)vresp->extractULONG();
603 char* VDR::moveRecording(char* fileName, char* newPath)
605 VDR_RequestPacket vrp;
606 if (!vrp.init(VDR_MOVERECORDING, true, strlen(fileName) + 1 + strlen(newPath) + 1)) return NULL;
607 if (!vrp.addString(fileName)) return NULL;
608 if (!vrp.addString(newPath)) return NULL;
610 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
611 if (vresp->noResponse()) { delete vresp; return NULL; }
613 char* toReturn = NULL;
614 int success = (int)vresp->extractULONG();
617 toReturn = vresp->extractString();
625 ChannelList* VDR::getChannelsList(ULONG type)
627 VDR_RequestPacket vrp;
628 if (!vrp.init(VDR_GETCHANNELLIST, true, 0)) return NULL;
630 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
631 if (vresp->noResponse()) { delete vresp; return NULL; }
633 ChannelList* chanList = new ChannelList();
635 while (!vresp->end())
637 Channel* chan = new Channel();
638 chan->number = vresp->extractULONG();
639 chan->type = vresp->extractULONG();
640 chan->name = vresp->extractString();
642 if (chan->type == type)
644 chanList->push_back(chan);
645 logger->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
646 if (chan->number > maxChannelNumber) maxChannelNumber = chan->number;
656 if (maxChannelNumber > 99999)
657 channelNumberWidth = 6;
658 else if (maxChannelNumber > 9999)
659 channelNumberWidth = 5;
660 else if (maxChannelNumber > 999)
661 channelNumberWidth = 4;
662 else if (maxChannelNumber > 99)
663 channelNumberWidth = 3;
664 else if (maxChannelNumber > 9)
665 channelNumberWidth = 2;
667 channelNumberWidth = 1;
672 int VDR::streamChannel(ULONG number, StreamReceiver* tstreamReceiver)
674 VDR_RequestPacket vrp;
675 if (!vrp.init(VDR_STREAMCHANNEL, true, sizeof(ULONG))) return 0;
676 if (!vrp.addULONG(number)) return 0;
679 VDR_PacketReceiver* vdrpr = new VDR_PacketReceiver();
680 vdrpr->receiverChannel = VDR::CHANNEL_STREAM;
681 vdrpr->streamID = vrp.getSerial();
682 vdrpr->streamReceiver = tstreamReceiver;
684 TEMP_SINGLE_VDR_PR = vdrpr;
686 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
687 if (vresp->noResponse())
695 int toReturn = (int)vresp->extractULONG();
696 logger->log("VDR", Log::DEBUG, "VDR said %lu to start streaming request", toReturn);
702 int VDR::stopStreaming()
704 VDR_RequestPacket vrp;
705 if (!vrp.init(VDR_STOPSTREAMING, true, 0)) return 0;
707 if (TEMP_SINGLE_VDR_PR) // this block only needs to be done if it was a live stream
708 // TEMP_SINGLE_VDR_PR will not be set unless we are streaming a channel
710 edUnregister(TEMP_SINGLE_VDR_PR);
711 delete TEMP_SINGLE_VDR_PR;
712 TEMP_SINGLE_VDR_PR = NULL;
715 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
716 if (vresp->noResponse()) { delete vresp; return 0; }
718 int toReturn = (int)vresp->extractULONG();
724 UCHAR* VDR::getImageBlock(ULONG position, UINT maxAmount, UINT* amountReceived)
726 return getBlock(position, maxAmount, amountReceived, VDR_GETIMAGEBLOCK);
729 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
731 return getBlock(position, maxAmount, amountReceived, VDR_GETBLOCK);
734 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived, ULONG cmd)
736 VDR_RequestPacket vrp;
737 if (!vrp.init(cmd, true, sizeof(ULLONG) + sizeof(ULONG))) return NULL;
738 if (!vrp.addULLONG(position)) return NULL;
739 if (!vrp.addULONG(maxAmount)) return NULL;
741 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
742 if (vresp->noResponse()) { delete vresp; return NULL; }
744 if (vresp->serverError())
746 logger->log("VDR", Log::DEBUG, "Detected getblock 0");
751 // Special handling for getblock
752 UCHAR* toReturn = vresp->getUserData();
753 *amountReceived = vresp->getUserDataLength();
760 ULLONG VDR::streamRecording(char* fileName, ULONG* totalFrames)
762 VDR_RequestPacket vrp;
763 if (!vrp.init(VDR_STREAMRECORDING, true, strlen(fileName) + 1)) return 0;
764 if (!vrp.addString(fileName)) return 0;
766 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
767 if (vresp->noResponse()) { delete vresp; return 0; }
769 ULLONG lengthBytes = vresp->extractULLONG();
770 ULONG lengthFrames = vresp->extractULONG();
773 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
775 *totalFrames = lengthFrames;
779 ULLONG VDR::positionFromFrameNumber(ULONG frameNumber)
781 VDR_RequestPacket vrp;
782 if (!vrp.init(VDR_POSFROMFRAME, true, sizeof(ULONG))) return 0;
783 if (!vrp.addULONG(frameNumber)) return 0;
785 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
786 if (vresp->noResponse()) { delete vresp; return 0; }
788 ULLONG position = vresp->extractULLONG();
791 logger->log("VDR", Log::DEBUG, "VDR said new position is: %llu", position);
796 ULONG VDR::frameNumberFromPosition(ULLONG position)
798 VDR_RequestPacket vrp;
799 if (!vrp.init(VDR_FRAMEFROMPOS, true, sizeof(ULLONG))) return 0;
800 if (!vrp.addULLONG(position)) return 0;
802 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
803 if (vresp->noResponse()) { delete vresp; return 0; }
805 ULONG framenumber = vresp->extractULONG();
808 logger->log("VDR", Log::DEBUG, "VDR said new framenumber is: %u", framenumber);
813 bool VDR::getNextIFrame(ULONG frameNumber, ULONG direction, ULLONG* rfilePosition, ULONG* rframeNumber, ULONG* rframeLength)
815 VDR_RequestPacket vrp;
816 if (!vrp.init(VDR_GETNEXTIFRAME, true, sizeof(ULONG)*2)) return false;
817 if (!vrp.addULONG(frameNumber)) return false;
818 if (!vrp.addULONG(direction)) return false;
820 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
821 if (vresp->noResponse()) { delete vresp; return false; }
823 if (vresp->serverError())
825 logger->log("VDR", Log::DEBUG, "Detected getNextIFrame error");
830 *rfilePosition = vresp->extractULLONG();
831 *rframeNumber = vresp->extractULONG();
832 *rframeLength = vresp->extractULONG();
836 // logger->log("VDR", Log::DEBUG, "VDR GNIF said %llu %lu %lu", *rfilePosition, *rframeNumber, *rframeLength);
841 EventList* VDR::getChannelSchedule(ULONG number)
845 return getChannelSchedule(number, now, 24 * 60 * 60);
848 EventList* VDR::getChannelSchedule(ULONG number, time_t start, ULONG duration)
850 // retrieve event list (vector of events) from vdr within filter window. duration is in seconds
852 VDR_RequestPacket vrp;
853 if (!vrp.init(VDR_GETCHANNELSCHEDULE, true, sizeof(ULONG)*3)) return NULL;
854 if (!vrp.addULONG(number)) return NULL;
855 if (!vrp.addULONG(start)) return NULL;
856 if (!vrp.addULONG(duration)) return NULL;
858 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
859 if (vresp->noResponse()) { delete vresp; return NULL; }
861 // received a ulong(0) - schedules error in the plugin
862 if (vresp->serverError())
868 EventList* eventList = new EventList();
870 while (!vresp->end())
872 Event* event = new Event();
873 event->id = vresp->extractULONG();
874 event->time = vresp->extractULONG();
875 event->duration = vresp->extractULONG();
876 event->title = vresp->extractString();
877 event->subtitle = vresp->extractString();
878 event->description = vresp->extractString();
879 eventList->push_back(event);
884 logger->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule");
888 int VDR::configSave(const char* section, const char* key, const char* value)
890 VDR_RequestPacket vrp;
891 if (!vrp.init(VDR_CONFIGSAVE, false, 0)) return 0;
892 if (!vrp.addString(section)) return 0;
893 if (!vrp.addString(key)) return 0;
894 if (!vrp.addString(value)) return 0;
896 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
897 if (vresp->noResponse()) { delete vresp; return 0; }
899 int toReturn = (int)vresp->extractULONG();
905 char* VDR::configLoad(const char* section, const char* key)
907 VDR_RequestPacket vrp;
908 if (!vrp.init(VDR_CONFIGLOAD, false, 0)) return NULL;
909 if (!vrp.addString(section)) return NULL;
910 if (!vrp.addString(key)) return NULL;
912 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
913 if (vresp->noResponse()) { delete vresp; return NULL; }
915 char* toReturn = vresp->extractString();
921 RecTimerList* VDR::getRecTimersList()
923 VDR_RequestPacket vrp;
924 if (!vrp.init(VDR_GETTIMERS, true, 0)) return NULL;
926 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
927 if (vresp->noResponse()) { delete vresp; return NULL; }
929 RecTimerList* recTimerList = new RecTimerList();
931 ULONG numTimers = vresp->extractULONG();
934 RecTimer* newRecTimer;
937 while (!vresp->end())
939 newRecTimer = new RecTimer();
940 newRecTimer->active = vresp->extractULONG();
941 newRecTimer->recording = vresp->extractULONG();
942 newRecTimer->pending = vresp->extractULONG();
943 newRecTimer->priority = vresp->extractULONG();
944 newRecTimer->lifeTime = vresp->extractULONG();
945 newRecTimer->channelNumber = vresp->extractULONG();
946 newRecTimer->startTime = vresp->extractULONG();
947 newRecTimer->stopTime = vresp->extractULONG();
948 newRecTimer->day = vresp->extractULONG();
949 newRecTimer->weekDays = vresp->extractULONG();
951 tempString = vresp->extractString();
952 newRecTimer->setFile(tempString);
955 recTimerList->push_back(newRecTimer);
956 logger->log("VDR", Log::DEBUG, "TL: %lu %lu %lu %lu %lu %lu %lu %lu %s",
957 newRecTimer->active, newRecTimer->recording, newRecTimer->pending, newRecTimer->priority, newRecTimer->lifeTime,
958 newRecTimer->channelNumber, newRecTimer->startTime, newRecTimer->stopTime, newRecTimer->getFile());
964 sort(recTimerList->begin(), recTimerList->end(), RecTimerSorter());
969 ULONG VDR::setEventTimer(char* timerString)
971 VDR_RequestPacket vrp;
972 if (!vrp.init(VDR_SETTIMER, true, strlen(timerString) + 1)) return 0;
973 if (!vrp.addString(timerString)) return 0;
975 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
976 if (vresp->noResponse()) { delete vresp; return 0; }
978 ULONG toReturn = vresp->extractULONG();
984 RecInfo* VDR::getRecInfo(char* fileName)
986 VDR_RequestPacket vrp;
987 if (!vrp.init(VDR_GETRECINFO, true, strlen(fileName) + 1)) return NULL;
988 if (!vrp.addString(fileName)) return NULL;
990 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
991 if (vresp->noResponse()) { delete vresp; return NULL; }
993 if (vresp->serverError())
995 logger->log("VDR", Log::DEBUG, "Could not get rec info");
1000 RecInfo* recInfo = new RecInfo();
1002 recInfo->timerStart = vresp->extractULONG();
1003 recInfo->timerEnd = vresp->extractULONG();
1004 recInfo->resumePoint = vresp->extractULONG();
1005 recInfo->summary = vresp->extractString();
1007 ULONG numComponents = vresp->extractULONG();
1010 recInfo->setNumComponents(numComponents);
1011 for (ULONG i = 0; i < numComponents; i++)
1013 recInfo->streams[i] = vresp->extractUCHAR();
1014 recInfo->types[i] = vresp->extractUCHAR();
1015 recInfo->languages[i] = vresp->extractString();
1016 recInfo->descriptions[i] = vresp->extractString();
1027 ULLONG VDR::rescanRecording(ULONG* totalFrames)
1029 VDR_RequestPacket vrp;
1030 if (!vrp.init(VDR_RESCANRECORDING, true, 0)) return 0;
1032 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1033 if (vresp->noResponse()) { delete vresp; return 0; }
1035 ULLONG lengthBytes = vresp->extractULLONG();
1036 ULONG lengthFrames = vresp->extractULONG();
1039 logger->log("VDR", Log::DEBUG, "VDR said length is: %llu %lu", lengthBytes, lengthFrames);
1041 *totalFrames = lengthFrames;
1045 MarkList* VDR::getMarks(char* fileName)
1047 VDR_RequestPacket vrp;
1048 if (!vrp.init(VDR_GETMARKS, true, strlen(fileName) + 1)) return NULL;
1049 if (!vrp.addString(fileName)) return NULL;
1051 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1052 if (vresp->noResponse()) { delete vresp; return NULL; }
1054 if (vresp->serverError())
1060 MarkList* markList = new MarkList();
1062 while (!vresp->end())
1064 Mark* mark = new Mark();
1065 mark->pos = vresp->extractULONG();
1067 markList->push_back(mark);
1068 logger->log("VDR", Log::DEBUG, "Have added a mark to list. %lu", mark->pos);
1076 void VDR::getChannelPids(Channel* channel)
1078 VDR_RequestPacket vrp;
1079 if (!vrp.init(VDR_GETCHANNELPIDS, true, sizeof(ULONG))) return ;
1080 if (!vrp.addULONG(channel->number)) return ;
1082 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1083 if (vresp->noResponse()) { delete vresp; return ; }
1085 // Format of response
1093 channel->vpid = vresp->extractULONG();
1094 channel->numAPids = vresp->extractULONG();
1096 for (ULONG i = 0; i < channel->numAPids; i++)
1099 newapid.pid = vresp->extractULONG();
1100 newapid.name = vresp->extractString();
1101 channel->apids.push_back(newapid);
1104 channel->numDPids = vresp->extractULONG();
1106 for (ULONG i = 0; i < channel->numDPids; i++)
1109 newdpid.pid = vresp->extractULONG();
1110 newdpid.name = vresp->extractString();
1111 channel->dpids.push_back(newdpid);
1114 channel->numSPids = vresp->extractULONG();
1116 for (ULONG i = 0; i < channel->numSPids; i++)
1119 newspid.pid = vresp->extractULONG();
1120 newspid.name = vresp->extractString();
1121 channel->spids.push_back(newspid);
1123 channel->tpid = vresp->extractULONG();
1131 * media List Request:
1132 * 4 flags (currently unused)
1135 * Media List response:
1143 * 4 strlen (incl. 0 Byte)
1147 MediaList* VDR::getMediaList(const char* parent,int mediaType)
1149 logger->log("VDR", Log::DEBUG, "getMediaList %s,type=%d", (parent?parent:"NULL"), mediaType);
1151 VDR_RequestPacket vrp;
1152 if (!vrp.init(VDR_GETMEDIALIST, false, 0)) return NULL;
1153 if (!vrp.addULONG(0)) return NULL; // unused flags
1157 if (!vrp.addString(parent)) return NULL;
1160 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1161 if (vresp->noResponse()) { delete vresp; return NULL; }
1163 if (vresp->serverError())
1169 if (vresp->getUserDataLength() < 12)
1171 logger->log("VDR", Log::ERR, "receiveMediaList packet too short, expected 12, got %d", vresp->getUserDataLength());
1176 MediaList* mediaList = new MediaList();
1178 code = vresp->extractULONG();
1179 ULONG numEntries = vresp->extractULONG();
1180 Log::getInstance()->log("VDR", Log::DEBUG, "receiveMediaList with %d entries",numEntries);
1181 while (!vresp->end() && numEntries >0)
1183 Media* m = new Media();
1184 ULONG mtype = vresp->extractULONG();
1185 ULONG mtime=vresp->extractULONG();
1187 flags=vresp->extractULONG();
1188 ULONG stsize=vresp->extractULONG();
1189 char * name=vresp->extractString();
1190 if (! name || stsize != (strlen(name)+1)) {
1191 Log::getInstance()->log("VDR", Log::ERR, "receiveMediaList invalid packet entry, read size %d, strlen %d", stsize, strlen(name)+1);
1197 //ignore . and .. entries
1198 if (strcmp(name,".") == 0 || strcmp(name,"..")==0) {
1202 m->setFileName(name);
1204 m->setMediaType(mtype);
1205 mediaList->push_back(m);
1206 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a media to list. %s, type=%d, time=%d", name,mtype,mtime);
1215 * get image Request:
1216 * 4 flags (currently unused)
1221 * get image response:
1226 ULONG VDR::loadImage(const char* fileName, ULONG x, ULONG y)
1228 VDR_RequestPacket vrp;
1229 if (!vrp.init(VDR_GETIMAGE, false, 0)) return 0;
1230 if (!vrp.addULONG(0)) return 0; // unused flags
1231 if (!vrp.addULONG(x)) return 0;
1232 if (!vrp.addULONG(y)) return 0;
1233 if (!vrp.addString(fileName)) return 0;
1235 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1236 if (vresp->noResponse()) { delete vresp; return 0; }
1238 ULONG cmd = vresp->extractULONG();
1239 ULONG lengthBytes = vresp->extractULONG();
1242 Log::getInstance()->log("VDR", Log::DEBUG, "getImage %s: cmd=%lu len=%lu", fileName, cmd, lengthBytes);
1246 int VDR::deleteTimer(RecTimer* delTimer)
1248 Log::getInstance()->log("VDR", Log::DEBUG, "Delete timer called");
1250 VDR_RequestPacket vrp;
1251 if (!vrp.init(VDR_DELETETIMER, false, 0)) return 0;
1252 if (!vrp.addULONG(delTimer->channelNumber)) return 0;
1253 if (!vrp.addULONG(delTimer->weekDays)) return 0;
1254 if (!vrp.addULONG(delTimer->day)) return 0;
1255 if (!vrp.addULONG(delTimer->startTime)) return 0;
1256 if (!vrp.addULONG(delTimer->stopTime)) return 0;
1258 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1259 if (vresp->noResponse()) { delete vresp; return 0; }
1261 int toReturn = (int)vresp->extractULONG();
1267 I18n::lang_code_list VDR::getLanguageList()
1269 I18n::lang_code_list CodeList;
1270 CodeList["en"] = "English"; // Default entry
1271 VDR_RequestPacket vrp;
1272 if (!vrp.init(VDR_GETLANGUAGELIST, false, 0)) return CodeList;
1273 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1274 if (vresp->noResponse() || vresp->end())
1280 while (!vresp->end())
1282 char* c_code = vresp->extractString();
1283 char* c_name = vresp->extractString();
1284 string code = c_code;
1285 string name = c_name;
1286 CodeList[code] = name;
1294 int VDR::getLanguageContent(const std::string code, I18n::trans_table& texts)
1296 VDR_RequestPacket vrp;
1297 if (!vrp.init(VDR_GETLANGUAGECONTENT, false, 0)) return 0;
1298 if (!vrp.addString(code.c_str())) return 0;
1299 VDR_ResponsePacket* vresp = RequestResponse(&vrp);
1300 if (vresp->noResponse()) { delete vresp; return 0; }
1302 while (!vresp->end())
1304 char* c_key = vresp->extractString();
1305 char* c_text = vresp->extractString();
1307 string text = c_text;