2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 VDR* VDR::instance = NULL;
32 pthread_mutex_init(&mutex, NULL);
38 if (initted) shutdown();
41 VDR* VDR::getInstance()
46 int VDR::init(int tport)
48 if (initted) return 0;
51 logger = Log::getInstance();
57 if (!initted) return 0;
63 void VDR::findServers(std::vector<char*>& serverIPs)
66 char* message = "VOMP CLIENT";
67 DatagramSocket ds(port);
69 int haveAtLeastOne = 0;
77 logger->log("VDR", Log::NOTICE, "Broadcasting for server");
78 ds.send("255.255.255.255", 3024, message, strlen(message));
80 retval = ds.waitforMessage(waitType);
82 if (retval == 2) // we got a reply
84 if (strcmp(ds.getData(), "VOMP SERVER")) // echo.....
91 strcpy(newIP, ds.getFromIPA());
92 serverIPs.push_back(newIP);
99 if (haveAtLeastOne) break;
105 void VDR::cancelFindingServer()
110 void VDR::setServerIP(char* newIP)
112 strcpy(serverIP, newIP);
119 return tcp->connectTo(serverIP, 3024);
122 void VDR::disconnect()
126 Log::getInstance()->log("VDR", Log::DEBUG, "Disconnect");
129 long VDR::getSimpleReply()
131 unsigned char* p = (unsigned char*)tcp->receivePacket();
134 Log::getInstance()->log("VDR", Log::DEBUG, "tcp data length = %i", tcp->getDataLength());
136 if (tcp->getDataLength() != 4)
142 ULONG reply = ntohl(*(ULONG*)p);
144 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said %li", reply);
151 char* VDR::getStringReply()
153 unsigned char* p = (unsigned char*)tcp->receivePacket();
156 int dataLength = tcp->getDataLength();
158 // Log::getInstance()->log("VDR", Log::DEBUG, "GSR Data length %u", dataLength);
160 if ((dataLength == 4) && (p[0] == '\0'))
162 Log::getInstance()->log("VDR", Log::DEBUG, "GSR returning null no string returned");
164 return NULL; // no string returned
170 tLength = strlen((char*)p);
171 returnText = new char[tLength + 1];
172 strcpy(returnText, (char*)p);
179 /////////////////////////////////////////////////////////////////////////////
185 *(unsigned long*)&buffer[0] = htonl(4);
186 *(unsigned long*)&buffer[4] = htonl(VDR_LOGIN);
188 pthread_mutex_lock(&mutex);
189 int a = tcp->sendPacket(buffer, 8);
192 pthread_mutex_unlock(&mutex);
198 UCHAR* p = tcp->receivePacket();
199 pthread_mutex_unlock(&mutex);
204 unsigned long vdrTime = ntohl(*(unsigned long*)&p[count]);
205 count += sizeof(unsigned long);
206 Log::getInstance()->log("VDR", Log::DEBUG, "vdrtime = %lu", vdrTime);
208 struct timespec currentTime;
209 currentTime.tv_sec = vdrTime;
210 currentTime.tv_nsec = 0;
211 int b = clock_settime(CLOCK_REALTIME, ¤tTime);
213 Log::getInstance()->log("VDR", Log::DEBUG, "set clock = %u", b);
215 // now make a TZ variable and set it
216 signed int vdrTimeOffset = ntohl(*(signed int*)&p[count]);
217 Log::getInstance()->log("VDR", Log::DEBUG, "offset = %i", vdrTimeOffset);
222 if (vdrTimeOffset > 0) sign = '-';
225 vdrTimeOffset = abs(vdrTimeOffset);
227 hours = (int)vdrTimeOffset / 3600;
228 minutes = vdrTimeOffset % 3600;
230 Log::getInstance()->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
232 minutes = (int)minutes / 60;
234 Log::getInstance()->log("VDR", Log::DEBUG, "%c %i %i", sign, hours, minutes);
237 sprintf(newTZ, "MVP%c%i:%i", sign, hours, minutes);
238 setenv("TZ", newTZ, 1);
240 Log::getInstance()->log("VDR", Log::DEBUG, "Timezone data: %s", newTZ);
246 Directory* VDR::getRecordingsList()
250 *(unsigned long*)&buffer[0] = htonl(4);
251 *(unsigned long*)&buffer[4] = htonl(VDR_GETRECORDINGLIST);
253 pthread_mutex_lock(&mutex);
254 int a = tcp->sendPacket(buffer, 8);
257 pthread_mutex_unlock(&mutex);
263 unsigned char* p = (unsigned char*)tcp->receivePacket();
264 pthread_mutex_unlock(&mutex);
267 Directory* recDir = new Directory();
268 recDir->setName("/");
271 int dataLength = tcp->getDataLength();
273 Log::getInstance()->log("VDR", Log::DEBUG, "Data length %u", dataLength);
277 Directory::totalSpace = (*(ULONG*)&p[count]);
278 count += sizeof(ULONG);
279 Directory::freeSpace = (*(ULONG*)&p[count]);
280 count += sizeof(ULONG);
281 Directory::usedPercent = (*(ULONG*)&p[count]);
282 count += sizeof(ULONG);
286 while (count < dataLength)
288 Recording* rec = new Recording();
289 rec->start = ntohl(*(unsigned long*)&p[count]);
292 tLength = strlen((char*)&p[count]);
293 rec->setName((char*)&p[count]);
295 // rec->name = new char[tLength + 1];
296 // strcpy(rec->name, (char*)&p[count]);
297 count += tLength + 1;
299 tLength = strlen((char*)&p[count]);
300 rec->fileName = new char[tLength + 1];
301 strcpy(rec->fileName, (char*)&p[count]);
302 count += tLength + 1;
306 char* dirName = rec->getDirName();
308 Directory* d = recDir->getDirByName(dirName);
313 Log::getInstance()->log("VDR", Log::DEBUG, "Added a new directory = %s", d->name);
314 recDir->dirList->add(d);
317 d->recList->add(rec);
322 recDir->recList->add(rec);
323 recDir->recList->next();
326 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a recording to list. %lu %s", rec->start, rec->getProgName());
334 int VDR::deleteRecording(char* fileName)
336 unsigned long totalLength = 8 + strlen(fileName) + 1;
337 UCHAR buffer[totalLength];
339 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
340 *(unsigned long*)&buffer[4] = htonl(VDR_DELETERECORDING);
341 strcpy((char*)&buffer[8], fileName);
343 pthread_mutex_lock(&mutex);
344 unsigned int a = tcp->sendPacket(buffer, totalLength);
345 if (a != totalLength)
347 pthread_mutex_unlock(&mutex);
351 int toReturn = getSimpleReply();
352 pthread_mutex_unlock(&mutex);
356 char* VDR::getRecordingSummary(char* fileName)
358 unsigned long totalLength = 8 + strlen(fileName) + 1;
359 UCHAR buffer[totalLength];
361 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
362 *(unsigned long*)&buffer[4] = htonl(VDR_GETSUMMARY);
363 strcpy((char*)&buffer[8], fileName);
365 pthread_mutex_lock(&mutex);
366 unsigned int a = tcp->sendPacket(buffer, totalLength);
367 if (a != totalLength)
369 pthread_mutex_unlock(&mutex);
373 char* toReturn = getStringReply();
374 pthread_mutex_unlock(&mutex);
378 List* VDR::getChannelsList(ULONG type)
382 *(unsigned long*)&buffer[0] = htonl(4);
383 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELLIST);
385 pthread_mutex_lock(&mutex);
386 int a = tcp->sendPacket(buffer, 8);
389 pthread_mutex_unlock(&mutex);
395 unsigned char* p = (unsigned char*)tcp->receivePacket();
396 pthread_mutex_unlock(&mutex);
399 List* chanList = new List();
401 int dataLength = tcp->getDataLength();
403 Log::getInstance()->log("VDR", Log::DEBUG, "Data length %u", dataLength);
409 while (count < dataLength)
411 Channel* chan = new Channel();
412 chan->number = ntohl(*(unsigned long*)&p[count]);
414 chan->type = ntohl(*(unsigned long*)&p[count]);
417 tLength = strlen((char*)&p[count]);
418 chan->name = new char[tLength + 1];
419 strcpy(chan->name, (char*)&p[count]);
420 count += tLength + 1;
422 if (chan->type == type)
426 Log::getInstance()->log("VDR", Log::DEBUG, "Have added a channel to list. %lu %lu %s", chan->number, chan->type, chan->name);
439 int VDR::streamChannel(ULONG number)
443 *(unsigned long*)&buffer[0] = htonl(8);
444 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMCHANNEL);
445 *(unsigned long*)&buffer[8] = htonl(number);
447 pthread_mutex_lock(&mutex);
448 int a = tcp->sendPacket(buffer, 12);
452 pthread_mutex_unlock(&mutex);
456 int toReturn = getSimpleReply();
457 pthread_mutex_unlock(&mutex);
461 int VDR::stopStreaming()
465 *(unsigned long*)&buffer[0] = htonl(4);
466 *(unsigned long*)&buffer[4] = htonl(VDR_STOPSTREAMING);
468 pthread_mutex_lock(&mutex);
469 int a = tcp->sendPacket(buffer, 8);
473 pthread_mutex_unlock(&mutex);
477 int toReturn = getSimpleReply();
478 pthread_mutex_unlock(&mutex);
482 UCHAR* VDR::getBlock(ULLONG position, UINT maxAmount, UINT* amountReceived)
486 *(unsigned long*)&buffer[0] = htonl(16);
487 *(unsigned long*)&buffer[4] = htonl(VDR_GETBLOCK);
488 *(ULLONG*)&buffer[8] = htonll(position);
489 *(unsigned long*)&buffer[16] = htonl(maxAmount);
491 pthread_mutex_lock(&mutex);
492 int a = tcp->sendPacket(buffer, 20);
495 pthread_mutex_unlock(&mutex);
499 unsigned char* p = (unsigned char*)tcp->receivePacket();
500 pthread_mutex_unlock(&mutex);
502 *amountReceived = tcp->getDataLength();
506 ULLONG VDR::streamRecording(Recording* rec)
508 unsigned long totalLength = 8 + strlen(rec->fileName) + 1;
509 UCHAR buffer[totalLength];
511 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
512 *(unsigned long*)&buffer[4] = htonl(VDR_STREAMRECORDING);
513 strcpy((char*)&buffer[8], rec->fileName);
515 pthread_mutex_lock(&mutex);
516 unsigned int a = tcp->sendPacket(buffer, totalLength);
517 if (a != totalLength)
519 pthread_mutex_unlock(&mutex);
523 unsigned char* p = (unsigned char*)tcp->receivePacket();
524 pthread_mutex_unlock(&mutex);
527 if (tcp->getDataLength() != 8)
533 ULLONG recordingLength = ntohll(*(ULLONG*)p);
535 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
538 return recordingLength;
541 ULLONG VDR::rescanRecording()
543 unsigned long totalLength = 8;
544 UCHAR buffer[totalLength];
546 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
547 *(unsigned long*)&buffer[4] = htonl(VDR_RESCANRECORDING);
549 pthread_mutex_lock(&mutex);
550 unsigned int a = tcp->sendPacket(buffer, totalLength);
551 if (a != totalLength)
553 pthread_mutex_unlock(&mutex);
557 unsigned char* p = (unsigned char*)tcp->receivePacket();
558 pthread_mutex_unlock(&mutex);
561 if (tcp->getDataLength() != 8)
567 ULLONG recordingLength = ntohll(*(ULLONG*)p);
569 Log::getInstance()->log("VDR", Log::DEBUG, "VDR said length is: %llu", recordingLength);
572 return recordingLength;
575 int VDR::getChannelSchedule(ULONG number)
579 *(unsigned long*)&buffer[0] = htonl(8);
580 *(unsigned long*)&buffer[4] = htonl(VDR_GETCHANNELSCHEDULE);
581 *(unsigned long*)&buffer[8] = htonl(number);
583 pthread_mutex_lock(&mutex);
584 int a = tcp->sendPacket(buffer, 12);
588 pthread_mutex_unlock(&mutex);
592 unsigned char* p = (unsigned char*)tcp->receivePacket();
593 pthread_mutex_unlock(&mutex);
596 int dataLength = tcp->getDataLength();
604 ULONG data = ntohl(*(ULONG*)p);
607 Log::getInstance()->log("VDR", Log::DEBUG, "Success got to end of getChannelSchedule %lu", data);
612 ULLONG VDR::getResumePoint(char* fileName)
614 char* resumeString = configLoad("ResumeData", fileName);
615 if (!resumeString) return 0;
617 ULLONG toReturn = strtoull(resumeString, NULL, 10);
618 delete[] resumeString;
622 int VDR::configSave(char* section, char* key, char* value)
624 ULONG totalLength = 8 + strlen(section) + strlen(key) + strlen(value) + 3; // 8 for headers, 3 for nulls
625 UCHAR buffer[totalLength];
627 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
628 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGSAVE);
631 strcpy((char*)&buffer[position], section);
632 position += strlen(section) + 1;
633 strcpy((char*)&buffer[position], key);
634 position += strlen(key) + 1;
635 strcpy((char*)&buffer[position], value);
637 pthread_mutex_lock(&mutex);
638 unsigned int a = tcp->sendPacket(buffer, totalLength);
639 if (a != totalLength)
641 pthread_mutex_unlock(&mutex);
645 int toReturn = getSimpleReply();
646 pthread_mutex_unlock(&mutex);
650 char* VDR::configLoad(char* section, char* key)
652 ULONG totalLength = 8 + strlen(section) + strlen(key) + 2; // 8 for headers, 2 for nulls
653 UCHAR buffer[totalLength];
655 *(unsigned long*)&buffer[0] = htonl(totalLength - 4);
656 *(unsigned long*)&buffer[4] = htonl(VDR_CONFIGLOAD);
659 strcpy((char*)&buffer[position], section);
660 position += strlen(section) + 1;
661 strcpy((char*)&buffer[position], key);
663 pthread_mutex_lock(&mutex);
664 unsigned int a = tcp->sendPacket(buffer, totalLength);
665 if (a != totalLength)
667 pthread_mutex_unlock(&mutex);
671 char* toReturn = getStringReply();
672 pthread_mutex_unlock(&mutex);