Debian Codegraph @

src/ClientInstance.cpp

raw
1 /*
2 * Copyright (C) 2010-2021 Team Kodi (https://kodi.tv)
3 * Copyright (C) 2010 Alwin Esch (Team Kodi)
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSE.md for more information.
7 */
8
9 #include "ClientInstance.h"
10
11 #include "GUIWindowAdmin.h"
12 #include "GUIWindowChannelScan.h"
13 #include "InputstreamDemux.h"
14 #include "InputstreamRecording.h"
15 #include "RequestPacket.h"
16 #include "ResponsePacket.h"
17 #include "Settings.h"
18 #include "vnsicommand.h"
19
20 #include <algorithm>
21 #include <kodi/General.h>
22 #include <kodi/Network.h>
23 #include <kodi/Filesystem.h>
24 #include <string.h>
25 #include <time.h>
26
27 // helper functions (taken from VDR)
28
29 namespace
30 {
31
32 time_t IncDay(time_t t, int days)
33 {
34 struct tm tm = *localtime(&t);
35 tm.tm_mday += days;
36 int h = tm.tm_hour;
37 tm.tm_isdst = -1;
38 t = mktime(&tm);
39 tm.tm_hour = h;
40 return mktime(&tm);
41 }
42
43 int GetWDay(time_t t)
44 {
45 int weekday = localtime(&t)->tm_wday;
46 return weekday == 0 ? 6 : weekday - 1; // we start with Monday==0!
47 }
48
49 bool DayMatches(time_t t, unsigned int weekdays)
50 {
51 return (weekdays & (1 << GetWDay(t))) != 0;
52 }
53
54 time_t SetTime(time_t t, int secondsFromMidnight)
55 {
56 struct tm tm = *localtime(&t);
57 tm.tm_hour = secondsFromMidnight / 3600;
58 tm.tm_min = (secondsFromMidnight % 3600) / 60;
59 tm.tm_sec = secondsFromMidnight % 60;
60 tm.tm_isdst = -1; // makes sure mktime() will determine the correct DST setting
61 return mktime(&tm);
62 }
63
64 } /* namespace */
65
66 CVNSIClientInstance::SMessage& CVNSIClientInstance::Queue::Enqueue(uint32_t serial)
67 {
68 std::lock_guard<std::recursive_mutex> lock(m_mutex);
69 return m_queue[serial];
70 }
71
72 std::unique_ptr<cResponsePacket> CVNSIClientInstance::Queue::Dequeue(uint32_t serial,
73 SMessage& message)
74 {
75 std::lock_guard<std::recursive_mutex> lock(m_mutex);
76 auto vresp = std::move(message.pkt);
77 m_queue.erase(serial);
78 return vresp;
79 }
80
81 void CVNSIClientInstance::Queue::Set(std::unique_ptr<cResponsePacket>&& vresp)
82 {
83 std::lock_guard<std::recursive_mutex> lock(m_mutex);
84 SMessages::iterator it = m_queue.find(vresp->getRequestID());
85 if (it != m_queue.end())
86 {
87 it->second.pkt = std::move(vresp);
88 it->second.m_condition.notify_one();
89 }
90 }
91
92 CVNSIClientInstance::CVNSIClientInstance(const CPVRAddon& base,
93 const kodi::addon::IInstanceInfo& instance)
94 : kodi::addon::CInstancePVRClient(instance),
95 cVNSISession(*dynamic_cast<kodi::addon::CInstancePVRClient*>(this)),
96 m_base(base)
97 {
98 }
99
100 CVNSIClientInstance::~CVNSIClientInstance()
101 {
102 m_abort = true;
103 m_running = false;
104 if (m_thread.joinable())
105 m_thread.join();
106 if (m_startInformThread.joinable())
107 m_startInformThread.join();
108 Close();
109 }
110
111 void CVNSIClientInstance::OnDisconnect()
112 {
113 kodi::addon::CInstancePVRClient::ConnectionStateChange("vnsi connection lost",
114 PVR_CONNECTION_STATE_DISCONNECTED,
115 kodi::addon::GetLocalizedString(30044));
116 }
117
118 void CVNSIClientInstance::OnReconnect()
119 {
120 EnableStatusInterface(true, false);
121
122 if (m_startInformThread.joinable())
123 m_startInformThread.join();
124
125 m_startInformThread = std::thread([&]() {
126 kodi::addon::CInstancePVRClient::ConnectionStateChange("vnsi connection established",
127 PVR_CONNECTION_STATE_CONNECTED,
128 kodi::addon::GetLocalizedString(30045));
129
130 kodi::addon::CInstancePVRClient::TriggerChannelUpdate();
131 kodi::addon::CInstancePVRClient::TriggerTimerUpdate();
132 kodi::addon::CInstancePVRClient::TriggerRecordingUpdate();
133 });
134 }
135
136 bool CVNSIClientInstance::Start(const std::string& hostname,
137 int port,
138 const char* name,
139 const std::string& mac)
140 {
141 m_hostname = hostname;
142 m_port = port;
143 m_wolMac = mac;
144
145 if (name != nullptr)
146 m_name = name;
147
148 kodi::addon::CInstancePVRClient::ConnectionStateChange(
149 "VNSI started", PVR_CONNECTION_STATE_CONNECTING, "VNSI started");
150
151 m_abort = false;
152 m_connectionLost = true;
153 m_running = true;
154 m_thread = std::thread([&] { Process(); });
155
156 kodi::addon::PVRMenuhook hook;
157 hook.SetHookId(1);
158 hook.SetCategory(PVR_MENUHOOK_SETTING);
159 hook.SetLocalizedStringId(30107);
160 kodi::addon::CInstancePVRClient::AddMenuHook(hook);
161
162 return true;
163 }
164
165 bool CVNSIClientInstance::SupportChannelScan()
166 {
167 cRequestPacket vrp;
168 vrp.init(VNSI_SCAN_SUPPORTED);
169
170 auto vresp = ReadResult(&vrp);
171 if (!vresp)
172 {
173 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
174 return false;
175 }
176
177 uint32_t ret = vresp->extract_U32();
178 return ret == VNSI_RET_OK ? true : false;
179 }
180
181 bool CVNSIClientInstance::SupportRecordingsUndelete()
182 {
183 if (GetProtocol() > 7)
184 {
185 cRequestPacket vrp;
186 vrp.init(VNSI_RECORDINGS_DELETED_ACCESS_SUPPORTED);
187
188 auto vresp = ReadResult(&vrp);
189 if (!vresp)
190 {
191 kodi::Log(ADDON_LOG_INFO, "%s - Can't get response packed", __func__);
192 return false;
193 }
194
195 uint32_t ret = vresp->extract_U32();
196 return ret == VNSI_RET_OK ? true : false;
197 }
198
199 kodi::Log(ADDON_LOG_INFO, "%s - Undelete not supported on backend (min. Ver. 1.3.0; Protocol 7)",
200 __func__);
201 return false;
202 }
203
204 PVR_ERROR CVNSIClientInstance::GetCapabilities(kodi::addon::PVRCapabilities& capabilities)
205 {
206 capabilities.SetSupportsEPG(true);
207 capabilities.SetSupportsRecordings(true);
208 capabilities.SetSupportsRecordingsDelete(true);
209 capabilities.SetSupportsRecordingEdl(true);
210 capabilities.SetSupportsTimers(true);
211 capabilities.SetSupportsTV(true);
212 capabilities.SetSupportsRadio(true);
213 capabilities.SetSupportsChannelGroups(true);
214 capabilities.SetHandlesInputStream(true);
215 capabilities.SetHandlesDemuxing(true);
216 if (SupportChannelScan())
217 capabilities.SetSupportsChannelScan(true);
218 if (SupportRecordingsUndelete())
219 capabilities.SetSupportsRecordingsUndelete(true);
220 capabilities.SetSupportsRecordingsRename(true);
221 capabilities.SetSupportsRecordingsLifetimeChange(false);
222 capabilities.SetSupportsDescrambleInfo(false);
223
224 return PVR_ERROR_NO_ERROR;
225 }
226
227 PVR_ERROR CVNSIClientInstance::GetBackendName(std::string& name)
228 {
229 name = GetServerName();
230 return PVR_ERROR_NO_ERROR;
231 }
232
233 PVR_ERROR CVNSIClientInstance::GetBackendVersion(std::string& version)
234 {
235 version = GetVersion() + "(Protocol: " + std::to_string(GetProtocol()) + ")";
236 return PVR_ERROR_NO_ERROR;
237 }
238
239 PVR_ERROR CVNSIClientInstance::GetBackendHostname(std::string& hostname)
240 {
241 hostname = m_hostname;
242 return PVR_ERROR_NO_ERROR;
243 }
244
245 PVR_ERROR CVNSIClientInstance::GetConnectionString(std::string& connection)
246 {
247 connection = m_hostname + ":" + std::to_string(m_port);
248 return PVR_ERROR_NO_ERROR;
249 }
250
251 PVR_ERROR CVNSIClientInstance::GetDriveSpace(uint64_t& total, uint64_t& used)
252 {
253 cRequestPacket vrp;
254 vrp.init(VNSI_RECORDINGS_DISKSIZE);
255
256 auto vresp = ReadResult(&vrp);
257 if (!vresp)
258 {
259 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
260 return PVR_ERROR_SERVER_ERROR;
261 }
262
263 uint32_t totalspace = vresp->extract_U32();
264 uint32_t freespace = vresp->extract_U32();
265 /* vresp->extract_U32()); percent not used */
266
267 total = totalspace;
268 used = (totalspace - freespace);
269
270 /* Convert from kBytes to Bytes */
271 total *= 1024;
272 used *= 1024;
273
274 return PVR_ERROR_NO_ERROR;
275 }
276
277 PVR_ERROR CVNSIClientInstance::CallSettingsMenuHook(const kodi::addon::PVRMenuhook& menuhook)
278 {
279 try
280 {
281 if (menuhook.GetHookId() == 1)
282 {
283 cVNSIAdmin osd(*this);
284 osd.Open(m_hostname, m_port, m_wolMac);
285 }
286 return PVR_ERROR_NO_ERROR;
287 }
288 catch (std::exception e)
289 {
290 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
291 return PVR_ERROR_SERVER_ERROR;
292 }
293 }
294
295 PVR_ERROR CVNSIClientInstance::OpenDialogChannelScan()
296 {
297 try
298 {
299 cVNSIChannelScan scanner(*this);
300 scanner.Open(m_hostname, m_port, m_wolMac);
301 return PVR_ERROR_NO_ERROR;
302 }
303 catch (std::exception e)
304 {
305 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
306 return PVR_ERROR_SERVER_ERROR;
307 }
308 }
309
310 PVR_ERROR CVNSIClientInstance::OnSystemSleep()
311 {
312 return PVR_ERROR_NO_ERROR;
313 }
314
315 PVR_ERROR CVNSIClientInstance::OnSystemWake()
316 {
317 if (!m_wolMac.empty())
318 {
319 kodi::network::WakeOnLan(m_wolMac);
320 }
321
322 return PVR_ERROR_NO_ERROR;
323 }
324
325 PVR_ERROR CVNSIClientInstance::OnPowerSavingActivated()
326 {
327 return PVR_ERROR_NO_ERROR;
328 }
329
330 PVR_ERROR CVNSIClientInstance::OnPowerSavingDeactivated()
331 {
332 return PVR_ERROR_NO_ERROR;
333 }
334
335 /*******************************************/
336 /** PVR Channel Functions **/
337
338 PVR_ERROR CVNSIClientInstance::GetChannelsAmount(int& amount)
339 {
340 try
341 {
342 cRequestPacket vrp;
343 vrp.init(VNSI_CHANNELS_GETCOUNT);
344
345 auto vresp = ReadResult(&vrp);
346 if (!vresp)
347 {
348 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
349 return PVR_ERROR_SERVER_ERROR;
350 }
351
352 amount = vresp->extract_U32();
353 return PVR_ERROR_NO_ERROR;
354 }
355 catch (std::exception e)
356 {
357 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
358 return PVR_ERROR_SERVER_ERROR;
359 }
360 }
361
362 PVR_ERROR CVNSIClientInstance::GetChannels(bool radio, kodi::addon::PVRChannelsResultSet& results)
363 {
364 try
365 {
366 cRequestPacket vrp;
367 vrp.init(VNSI_CHANNELS_GETCHANNELS);
368 vrp.add_U32(radio);
369 vrp.add_U8(1); // apply filter
370
371 auto vresp = ReadResult(&vrp);
372 if (!vresp)
373 {
374 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
375 return PVR_ERROR_SERVER_ERROR;
376 }
377
378 while (vresp->getRemainingLength() >= 3 * 4 + 3)
379 {
380 kodi::addon::PVRChannel tag;
381
382 tag.SetChannelNumber(vresp->extract_U32());
383 tag.SetChannelName(vresp->extract_String());
384 char* strProviderName = vresp->extract_String();
385 tag.SetUniqueId(vresp->extract_U32());
386 tag.SetEncryptionSystem(vresp->extract_U32());
387 char* strCaids = vresp->extract_String();
388 if (m_protocol >= 6)
389 {
390 std::string path = CVNSISettings::Get().GetIconPath();
391 std::string ref = vresp->extract_String(); // Picon ref
392 if (!path.empty())
393 {
394 if (path[path.length() - 1] != '/')
395 path += '/';
396
397 ref += ".png";
398 std::string image_path = path + ref;
399 if (!kodi::vfs::FileExists(image_path)) // Use channel name
400 {
401 std::string channelNameImage = tag.GetChannelName();
402 std::transform(channelNameImage.begin(), channelNameImage.end(), channelNameImage.begin(), ::tolower);
403 channelNameImage += ".png";
404 image_path = path + channelNameImage;
405 }
406 tag.SetIconPath(image_path);
407 }
408 }
409 tag.SetIsRadio(radio);
410
411 results.Add(tag);
412 }
413
414 return PVR_ERROR_NO_ERROR;
415 }
416 catch (std::exception e)
417 {
418 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
419 return PVR_ERROR_SERVER_ERROR;
420 }
421 }
422
423 PVR_ERROR CVNSIClientInstance::GetSignalStatus(int channelUid,
424 kodi::addon::PVRSignalStatus& signalStatus)
425 {
426 if (!m_demuxer)
427 return PVR_ERROR_SERVER_ERROR;
428
429 try
430 {
431 return (m_demuxer->GetSignalStatus(signalStatus) ? PVR_ERROR_NO_ERROR : PVR_ERROR_SERVER_ERROR);
432 }
433 catch (std::exception e)
434 {
435 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
436 return PVR_ERROR_SERVER_ERROR;
437 }
438 }
439
440 /*******************************************/
441 /** PVR Channelgroups Functions **/
442
443 PVR_ERROR CVNSIClientInstance::GetChannelGroupsAmount(int& amount)
444 {
445 try
446 {
447 cRequestPacket vrp;
448 vrp.init(VNSI_CHANNELGROUP_GETCOUNT);
449 vrp.add_U32(CVNSISettings::Get().GetAutoChannelGroups());
450
451 auto vresp = ReadResult(&vrp);
452 if (vresp == nullptr || vresp->noResponse())
453 {
454 return PVR_ERROR_SERVER_ERROR;
455 }
456
457 amount = vresp->extract_U32();
458
459 return PVR_ERROR_NO_ERROR;
460 }
461 catch (std::exception e)
462 {
463 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
464 return PVR_ERROR_SERVER_ERROR;
465 }
466 }
467
468 PVR_ERROR CVNSIClientInstance::GetChannelGroups(bool radio,
469 kodi::addon::PVRChannelGroupsResultSet& results)
470 {
471 try
472 {
473 int amount = 0;
474 GetChannelGroupsAmount(amount);
475
476 if (amount > 0)
477 {
478 cRequestPacket vrp;
479 vrp.init(VNSI_CHANNELGROUP_LIST);
480 vrp.add_U8(radio);
481
482 auto vresp = ReadResult(&vrp);
483 if (vresp == nullptr || vresp->noResponse())
484 {
485 return PVR_ERROR_SERVER_ERROR;
486 }
487
488 while (vresp->getRemainingLength() >= 1 + 1)
489 {
490 kodi::addon::PVRChannelGroup tag;
491
492 tag.SetGroupName(vresp->extract_String());
493 tag.SetIsRadio(vresp->extract_U8() != 0 ? true : false);
494 tag.SetPosition(0);
495
496 results.Add(tag);
497 }
498 }
499
500 return PVR_ERROR_NO_ERROR;
501 }
502 catch (std::exception e)
503 {
504 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
505 return PVR_ERROR_SERVER_ERROR;
506 }
507 }
508
509 PVR_ERROR CVNSIClientInstance::GetChannelGroupMembers(
510 const kodi::addon::PVRChannelGroup& group,
511 kodi::addon::PVRChannelGroupMembersResultSet& results)
512 {
513 try
514 {
515 cRequestPacket vrp;
516 vrp.init(VNSI_CHANNELGROUP_MEMBERS);
517 vrp.add_String(group.GetGroupName().c_str());
518 vrp.add_U8(group.GetIsRadio());
519 vrp.add_U8(1); // filter channels
520
521 auto vresp = ReadResult(&vrp);
522 if (vresp == nullptr || vresp->noResponse())
523 {
524 return PVR_ERROR_SERVER_ERROR;
525 }
526
527 while (vresp->getRemainingLength() >= 2 * 4)
528 {
529 kodi::addon::PVRChannelGroupMember tag;
530
531 tag.SetGroupName(group.GetGroupName());
532 tag.SetChannelUniqueId(vresp->extract_U32());
533 tag.SetChannelNumber(vresp->extract_U32());
534
535 results.Add(tag);
536 }
537
538 return PVR_ERROR_NO_ERROR;
539 }
540 catch (std::exception e)
541 {
542 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
543 return PVR_ERROR_SERVER_ERROR;
544 }
545 }
546
547 /*******************************************/
548 /** PVR EPG Functions **/
549
550 PVR_ERROR CVNSIClientInstance::GetEPGForChannel(int channelUid,
551 time_t start,
552 time_t end,
553 kodi::addon::PVREPGTagsResultSet& results)
554 {
555 try
556 {
557 cRequestPacket vrp;
558 vrp.init(VNSI_EPG_GETFORCHANNEL);
559 vrp.add_U32(channelUid);
560 vrp.add_U32(start);
561 vrp.add_U32(end - start);
562
563 auto vresp = ReadResult(&vrp);
564 if (!vresp)
565 {
566 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
567 return PVR_ERROR_SERVER_ERROR;
568 }
569
570 while (vresp->getRemainingLength() >= 5 * 4 + 3)
571 {
572 kodi::addon::PVREPGTag tag;
573
574 tag.SetUniqueChannelId(channelUid);
575 tag.SetUniqueBroadcastId(vresp->extract_U32());
576 tag.SetStartTime(vresp->extract_U32());
577 tag.SetEndTime(tag.GetStartTime() + vresp->extract_U32());
578 uint32_t content(vresp->extract_U32());
579 tag.SetGenreType(content & 0xF0);
580 tag.SetGenreSubType(content & 0x0F);
581 tag.SetGenreDescription("");
582 tag.SetParentalRating(vresp->extract_U32());
583 tag.SetTitle(vresp->extract_String());
584 tag.SetPlotOutline(vresp->extract_String());
585 tag.SetPlot(vresp->extract_String());
586 tag.SetOriginalTitle("");
587 tag.SetCast("");
588 tag.SetDirector("");
589 tag.SetWriter("");
590 tag.SetYear(0);
591 tag.SetIMDBNumber("");
592 if (!tag.GetPlotOutline().empty())
593 tag.SetEpisodeName(tag.GetPlotOutline());
594 tag.SetFlags(EPG_TAG_FLAG_UNDEFINED);
595 tag.SetSeriesNumber(EPG_TAG_INVALID_SERIES_EPISODE);
596 tag.SetEpisodeNumber(EPG_TAG_INVALID_SERIES_EPISODE);
597 tag.SetEpisodePartNumber(EPG_TAG_INVALID_SERIES_EPISODE);
598
599 results.Add(tag);
600 }
601
602 return PVR_ERROR_NO_ERROR;
603 }
604 catch (std::exception e)
605 {
606 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
607 return PVR_ERROR_SERVER_ERROR;
608 }
609 }
610
611 /*******************************************/
612 /** PVR Recording Functions **/
613
614 PVR_ERROR CVNSIClientInstance::GetRecordingsAmount(bool deleted, int& amount)
615 {
616 try
617 {
618 cRequestPacket vrp;
619 vrp.init(!deleted ? VNSI_RECORDINGS_GETCOUNT : VNSI_RECORDINGS_DELETED_GETCOUNT);
620
621 auto vresp = ReadResult(&vrp);
622 if (!vresp)
623 {
624 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
625 return PVR_ERROR_SERVER_ERROR;
626 }
627
628 amount = vresp->extract_U32();
629 return PVR_ERROR_NO_ERROR;
630 }
631 catch (std::exception e)
632 {
633 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
634 return PVR_ERROR_SERVER_ERROR;
635 }
636 }
637
638 PVR_ERROR CVNSIClientInstance::GetRecordings(bool deleted,
639 kodi::addon::PVRRecordingsResultSet& results)
640 {
641 try
642 {
643 if (!deleted)
644 return GetAvailableRecordings(results);
645 else
646 return GetDeletedRecordings(results);
647
648 return PVR_ERROR_NO_ERROR;
649 }
650 catch (std::exception e)
651 {
652 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
653 return PVR_ERROR_SERVER_ERROR;
654 }
655 }
656
657 PVR_ERROR CVNSIClientInstance::GetAvailableRecordings(kodi::addon::PVRRecordingsResultSet& results)
658 {
659 cRequestPacket vrp;
660 vrp.init(VNSI_RECORDINGS_GETLIST);
661
662 auto vresp = ReadResult(&vrp);
663 if (!vresp)
664 {
665 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
666 return PVR_ERROR_UNKNOWN;
667 }
668
669 std::string strRecordingId;
670 while (vresp->getRemainingLength() >= 5 * 4 + 5)
671 {
672 kodi::addon::PVRRecording tag;
673
674 tag.SetRecordingTime(vresp->extract_U32());
675 tag.SetDuration(vresp->extract_U32());
676 tag.SetPriority(vresp->extract_U32());
677 tag.SetLifetime(vresp->extract_U32());
678 tag.SetIsDeleted(false);
679 tag.SetSeriesNumber(PVR_RECORDING_INVALID_SERIES_EPISODE);
680 tag.SetEpisodeNumber(PVR_RECORDING_INVALID_SERIES_EPISODE);
681 tag.SetChannelName(vresp->extract_String());
682 if (GetProtocol() >= 9)
683 {
684 tag.SetChannelUid(-1);
685 uint32_t uuid = vresp->extract_U32();
686 if (uuid > 0)
687 tag.SetChannelUid(uuid);
688 uint8_t type = vresp->extract_U8();
689 if (type == 1)
690 tag.SetChannelType(PVR_RECORDING_CHANNEL_TYPE_RADIO);
691 else if (type == 2)
692 tag.SetChannelType(PVR_RECORDING_CHANNEL_TYPE_TV);
693 else
694 tag.SetChannelType(PVR_RECORDING_CHANNEL_TYPE_UNKNOWN);
695 }
696 else
697 {
698 tag.SetChannelUid(PVR_CHANNEL_INVALID_UID);
699 tag.SetChannelType(PVR_RECORDING_CHANNEL_TYPE_UNKNOWN);
700 }
701
702 tag.SetTitle(vresp->extract_String());
703 tag.SetEpisodeName(vresp->extract_String());
704 tag.SetPlotOutline(tag.GetEpisodeName());
705 tag.SetPlot(vresp->extract_String());
706 tag.SetDirectory(vresp->extract_String());
707 tag.SetRecordingId(std::to_string(vresp->extract_U32()));
708
709 results.Add(tag);
710 }
711
712 return PVR_ERROR_NO_ERROR;
713 }
714
715 PVR_ERROR CVNSIClientInstance::GetDeletedRecordings(kodi::addon::PVRRecordingsResultSet& results)
716 {
717 cRequestPacket vrp;
718 vrp.init(VNSI_RECORDINGS_DELETED_GETLIST);
719
720 auto vresp = ReadResult(&vrp);
721 if (!vresp)
722 {
723 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
724 return PVR_ERROR_UNKNOWN;
725 }
726
727 std::string strRecordingId;
728 while (vresp->getRemainingLength() >= 5 * 4 + 5)
729 {
730 kodi::addon::PVRRecording tag;
731
732 tag.SetRecordingTime(vresp->extract_U32());
733 tag.SetDuration(vresp->extract_U32());
734 tag.SetPriority(vresp->extract_U32());
735 tag.SetLifetime(vresp->extract_U32());
736 tag.SetIsDeleted(true);
737 tag.SetChannelName(vresp->extract_String());
738 if (GetProtocol() >= 9)
739 {
740 tag.SetChannelUid(vresp->extract_S32());
741 }
742 else
743 tag.SetChannelUid(PVR_CHANNEL_INVALID_UID);
744
745 tag.SetTitle(vresp->extract_String());
746 tag.SetEpisodeName(vresp->extract_String());
747 tag.SetPlotOutline(tag.GetEpisodeName());
748 tag.SetPlot(vresp->extract_String());
749 tag.SetDirectory(vresp->extract_String());
750 tag.SetRecordingId(std::to_string(vresp->extract_U32()));
751
752 results.Add(tag);
753 }
754
755 return PVR_ERROR_NO_ERROR;
756 }
757
758 PVR_ERROR CVNSIClientInstance::RenameRecording(const kodi::addon::PVRRecording& recording)
759 {
760 try
761 {
762 cRequestPacket vrp;
763 vrp.init(VNSI_RECORDINGS_RENAME);
764
765 // add uid
766 kodi::Log(ADDON_LOG_DEBUG, "%s - uid: %s", __func__, recording.GetRecordingId().c_str());
767 vrp.add_U32(std::stoi(recording.GetRecordingId()));
768
769 // add new title
770 vrp.add_String(recording.GetTitle().c_str());
771
772 auto vresp = ReadResult(&vrp);
773 if (vresp == nullptr || vresp->noResponse())
774 {
775 return PVR_ERROR_SERVER_ERROR;
776 }
777
778 uint32_t returnCode = vresp->extract_U32();
779
780 if (returnCode != 0)
781 return PVR_ERROR_FAILED;
782
783 return PVR_ERROR_NO_ERROR;
784 }
785 catch (std::exception e)
786 {
787 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
788 return PVR_ERROR_SERVER_ERROR;
789 }
790 }
791
792 PVR_ERROR CVNSIClientInstance::DeleteRecording(const kodi::addon::PVRRecording& recording)
793 {
794 try
795 {
796 cRequestPacket vrp;
797 vrp.init(recording.GetIsDeleted() ? VNSI_RECORDINGS_DELETED_DELETE : VNSI_RECORDINGS_DELETE);
798 vrp.add_U32(std::stoi(recording.GetRecordingId()));
799
800 auto vresp = ReadResult(&vrp);
801 if (vresp == nullptr || vresp->noResponse())
802 {
803 return PVR_ERROR_UNKNOWN;
804 }
805
806 uint32_t returnCode = vresp->extract_U32();
807
808 switch (returnCode)
809 {
810 case VNSI_RET_DATALOCKED:
811 return PVR_ERROR_FAILED;
812
813 case VNSI_RET_RECRUNNING:
814 return PVR_ERROR_RECORDING_RUNNING;
815
816 case VNSI_RET_DATAINVALID:
817 return PVR_ERROR_INVALID_PARAMETERS;
818
819 case VNSI_RET_ERROR:
820 return PVR_ERROR_SERVER_ERROR;
821 }
822
823 return PVR_ERROR_NO_ERROR;
824 }
825 catch (std::exception e)
826 {
827 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
828 return PVR_ERROR_SERVER_ERROR;
829 }
830 }
831
832 PVR_ERROR CVNSIClientInstance::UndeleteRecording(const kodi::addon::PVRRecording& recording)
833 {
834 try
835 {
836 cRequestPacket vrp;
837 vrp.init(VNSI_RECORDINGS_DELETED_UNDELETE);
838 vrp.add_U32(std::stoi(recording.GetRecordingId()));
839
840 auto vresp = ReadResult(&vrp);
841 if (vresp == nullptr || vresp->noResponse())
842 {
843 return PVR_ERROR_UNKNOWN;
844 }
845
846 uint32_t returnCode = vresp->extract_U32();
847 switch (returnCode)
848 {
849 case VNSI_RET_DATALOCKED:
850 return PVR_ERROR_FAILED;
851
852 case VNSI_RET_RECRUNNING:
853 return PVR_ERROR_RECORDING_RUNNING;
854
855 case VNSI_RET_DATAINVALID:
856 return PVR_ERROR_INVALID_PARAMETERS;
857
858 case VNSI_RET_ERROR:
859 return PVR_ERROR_SERVER_ERROR;
860 }
861
862 return PVR_ERROR_NO_ERROR;
863 }
864 catch (std::exception e)
865 {
866 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
867 return PVR_ERROR_SERVER_ERROR;
868 }
869 }
870
871 PVR_ERROR CVNSIClientInstance::DeleteAllRecordingsFromTrash()
872 {
873 try
874 {
875 cRequestPacket vrp;
876 vrp.init(VNSI_RECORDINGS_DELETED_DELETE_ALL);
877
878 auto vresp = ReadResult(&vrp);
879 if (vresp == nullptr || vresp->noResponse())
880 {
881 return PVR_ERROR_UNKNOWN;
882 }
883
884 uint32_t returnCode = vresp->extract_U32();
885 switch (returnCode)
886 {
887 case VNSI_RET_DATALOCKED:
888 return PVR_ERROR_FAILED;
889
890 case VNSI_RET_RECRUNNING:
891 return PVR_ERROR_RECORDING_RUNNING;
892
893 case VNSI_RET_DATAINVALID:
894 return PVR_ERROR_INVALID_PARAMETERS;
895
896 case VNSI_RET_ERROR:
897 return PVR_ERROR_SERVER_ERROR;
898 }
899
900 return PVR_ERROR_NO_ERROR;
901 }
902 catch (std::exception e)
903 {
904 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
905 return PVR_ERROR_SERVER_ERROR;
906 }
907 }
908
909 PVR_ERROR CVNSIClientInstance::GetRecordingEdl(const kodi::addon::PVRRecording& recording,
910 std::vector<kodi::addon::PVREDLEntry>& edl)
911 {
912 try
913 {
914 cRequestPacket vrp;
915 vrp.init(VNSI_RECORDINGS_GETEDL);
916 vrp.add_U32(std::stoi(recording.GetRecordingId()));
917
918 int size = 0;
919 auto vresp = ReadResult(&vrp);
920
921 if (vresp == nullptr)
922 {
923 return PVR_ERROR_UNKNOWN;
924 }
925 else if (vresp->noResponse())
926 {
927 return PVR_ERROR_NO_ERROR;
928 }
929
930 while (vresp->getRemainingLength() >= 2 * 8 + 4 && size++ < PVR_ADDON_EDL_LENGTH)
931 {
932 kodi::addon::PVREDLEntry entry;
933
934 entry.SetStart(vresp->extract_S64());
935 entry.SetEnd(vresp->extract_S64());
936 entry.SetType(static_cast<PVR_EDL_TYPE>(vresp->extract_S32()));
937
938 edl.push_back(std::move(entry));
939 }
940
941 return PVR_ERROR_NO_ERROR;
942 }
943 catch (std::exception e)
944 {
945 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
946 return PVR_ERROR_SERVER_ERROR;
947 }
948 }
949
950
951 /*******************************************/
952 /** PVR Timer Functions **/
953
954 PVR_ERROR CVNSIClientInstance::GetTimerTypes(std::vector<kodi::addon::PVRTimerType>& types)
955 {
956 try
957 {
958 {
959 // One-shot manual
960 kodi::addon::PVRTimerType type;
961
962 type.SetId(VNSI_TIMER_TYPE_MAN);
963 type.SetDescription(kodi::addon::GetLocalizedString(30200));
964 type.SetAttributes(PVR_TIMER_TYPE_IS_MANUAL | PVR_TIMER_TYPE_SUPPORTS_ENABLE_DISABLE |
965 PVR_TIMER_TYPE_SUPPORTS_CHANNELS | PVR_TIMER_TYPE_SUPPORTS_START_TIME |
966 PVR_TIMER_TYPE_SUPPORTS_END_TIME | PVR_TIMER_TYPE_SUPPORTS_PRIORITY |
967 PVR_TIMER_TYPE_SUPPORTS_LIFETIME |
968 PVR_TIMER_TYPE_SUPPORTS_RECORDING_FOLDERS);
969
970 types.push_back(std::move(type));
971 }
972
973 {
974 // Repeating manual
975 kodi::addon::PVRTimerType type;
976
977 type.SetId(VNSI_TIMER_TYPE_MAN_REPEAT);
978 type.SetDescription(kodi::addon::GetLocalizedString(30201));
979 type.SetAttributes(PVR_TIMER_TYPE_IS_MANUAL | PVR_TIMER_TYPE_IS_REPEATING |
980 PVR_TIMER_TYPE_SUPPORTS_ENABLE_DISABLE | PVR_TIMER_TYPE_SUPPORTS_CHANNELS |
981 PVR_TIMER_TYPE_SUPPORTS_START_TIME | PVR_TIMER_TYPE_SUPPORTS_END_TIME |
982 PVR_TIMER_TYPE_SUPPORTS_PRIORITY | PVR_TIMER_TYPE_SUPPORTS_LIFETIME |
983 PVR_TIMER_TYPE_SUPPORTS_FIRST_DAY | PVR_TIMER_TYPE_SUPPORTS_WEEKDAYS |
984 PVR_TIMER_TYPE_SUPPORTS_RECORDING_FOLDERS);
985
986 types.push_back(std::move(type));
987 }
988
989 {
990 // Repeating manual
991 kodi::addon::PVRTimerType type;
992
993 type.SetId(VNSI_TIMER_TYPE_MAN_REPEAT_CHILD);
994 type.SetDescription(kodi::addon::GetLocalizedString(30205));
995 type.SetAttributes(PVR_TIMER_TYPE_IS_MANUAL | PVR_TIMER_TYPE_IS_READONLY |
996 PVR_TIMER_TYPE_SUPPORTS_CHANNELS | PVR_TIMER_TYPE_SUPPORTS_START_TIME |
997 PVR_TIMER_TYPE_SUPPORTS_END_TIME | PVR_TIMER_TYPE_SUPPORTS_PRIORITY |
998 PVR_TIMER_TYPE_SUPPORTS_LIFETIME |
999 PVR_TIMER_TYPE_SUPPORTS_RECORDING_FOLDERS);
1000
1001 types.push_back(std::move(type));
1002 }
1003
1004 {
1005 // One-shot epg-based
1006 kodi::addon::PVRTimerType type;
1007
1008 type.SetId(VNSI_TIMER_TYPE_EPG);
1009 type.SetDescription(kodi::addon::GetLocalizedString(30202));
1010 type.SetAttributes(
1011 PVR_TIMER_TYPE_SUPPORTS_ENABLE_DISABLE | PVR_TIMER_TYPE_REQUIRES_EPG_TAG_ON_CREATE |
1012 PVR_TIMER_TYPE_SUPPORTS_CHANNELS | PVR_TIMER_TYPE_SUPPORTS_START_TIME |
1013 PVR_TIMER_TYPE_SUPPORTS_END_TIME | PVR_TIMER_TYPE_SUPPORTS_PRIORITY |
1014 PVR_TIMER_TYPE_SUPPORTS_LIFETIME | PVR_TIMER_TYPE_SUPPORTS_RECORDING_FOLDERS);
1015
1016 types.push_back(std::move(type));
1017 }
1018
1019 // addition timer supported by backend
1020 if (GetProtocol() >= 9)
1021 {
1022 cRequestPacket vrp;
1023 vrp.init(VNSI_TIMER_GETTYPES);
1024 auto vresp = ReadResult(&vrp);
1025 if (!vresp)
1026 {
1027 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
1028 return PVR_ERROR_NO_ERROR;
1029 }
1030 uint32_t vnsitimers = vresp->extract_U32();
1031
1032 if (vnsitimers & VNSI_TIMER_TYPE_EPG_SEARCH)
1033 {
1034 // EPG search timer
1035 kodi::addon::PVRTimerType type;
1036
1037 type.SetId(VNSI_TIMER_TYPE_EPG_SEARCH);
1038 type.SetDescription(kodi::addon::GetLocalizedString(30204));
1039 type.SetAttributes(PVR_TIMER_TYPE_IS_REPEATING | PVR_TIMER_TYPE_SUPPORTS_ENABLE_DISABLE |
1040 PVR_TIMER_TYPE_SUPPORTS_CHANNELS |
1041 PVR_TIMER_TYPE_SUPPORTS_TITLE_EPG_MATCH |
1042 PVR_TIMER_TYPE_SUPPORTS_PRIORITY | PVR_TIMER_TYPE_SUPPORTS_LIFETIME);
1043 types.push_back(std::move(type));
1044 }
1045
1046 // VPS Timer
1047 kodi::addon::PVRTimerType type;
1048
1049 type.SetId(VNSI_TIMER_TYPE_VPS);
1050 type.SetDescription(kodi::addon::GetLocalizedString(30203));
1051 type.SetAttributes(PVR_TIMER_TYPE_IS_MANUAL | PVR_TIMER_TYPE_SUPPORTS_ENABLE_DISABLE |
1052 PVR_TIMER_TYPE_SUPPORTS_CHANNELS | PVR_TIMER_TYPE_SUPPORTS_START_TIME |
1053 PVR_TIMER_TYPE_SUPPORTS_END_TIME | PVR_TIMER_TYPE_SUPPORTS_PRIORITY |
1054 PVR_TIMER_TYPE_SUPPORTS_LIFETIME |
1055 PVR_TIMER_TYPE_SUPPORTS_RECORDING_FOLDERS);
1056 types.push_back(std::move(type));
1057 }
1058
1059 return PVR_ERROR_NO_ERROR;
1060 }
1061 catch (std::exception e)
1062 {
1063 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1064 return PVR_ERROR_SERVER_ERROR;
1065 }
1066 }
1067
1068 PVR_ERROR CVNSIClientInstance::GetTimersAmount(int& amount)
1069 {
1070 try
1071 {
1072 cRequestPacket vrp;
1073 vrp.init(VNSI_TIMER_GETCOUNT);
1074
1075 auto vresp = ReadResult(&vrp);
1076 if (!vresp)
1077 {
1078 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
1079 return PVR_ERROR_SERVER_ERROR;
1080 }
1081
1082 amount = vresp->extract_U32();
1083 return PVR_ERROR_NO_ERROR;
1084 }
1085 catch (std::exception e)
1086 {
1087 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1088 return PVR_ERROR_SERVER_ERROR;
1089 }
1090 }
1091
1092 PVR_ERROR CVNSIClientInstance::GetTimers(kodi::addon::PVRTimersResultSet& results)
1093 {
1094 try
1095 {
1096 cRequestPacket vrp;
1097 vrp.init(VNSI_TIMER_GETLIST);
1098
1099 auto vresp = ReadResult(&vrp);
1100 if (!vresp)
1101 {
1102 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
1103 return PVR_ERROR_SERVER_ERROR;
1104 }
1105
1106 uint32_t numTimers = vresp->extract_U32();
1107 if (numTimers > 0)
1108 {
1109 while (vresp->getRemainingLength() >= 12 * 4 + 1)
1110 {
1111 kodi::addon::PVRTimer tag;
1112
1113 if (GetProtocol() >= 9)
1114 {
1115 tag.SetTimerType(vresp->extract_U32());
1116 }
1117
1118 tag.SetClientIndex(vresp->extract_U32());
1119 int iActive(vresp->extract_U32());
1120 int iRecording(vresp->extract_U32());
1121 int iPending(vresp->extract_U32());
1122 if (iRecording)
1123 tag.SetState(PVR_TIMER_STATE_RECORDING);
1124 else if (iPending || iActive)
1125 tag.SetState(PVR_TIMER_STATE_SCHEDULED);
1126 else
1127 tag.SetState(PVR_TIMER_STATE_DISABLED);
1128 tag.SetPriority(vresp->extract_U32());
1129 tag.SetLifetime(vresp->extract_U32());
1130 vresp->extract_U32(); // channel number - unused
1131 tag.SetClientChannelUid(vresp->extract_U32());
1132 tag.SetStartTime(vresp->extract_U32());
1133 tag.SetEndTime(vresp->extract_U32());
1134 tag.SetFirstDay(vresp->extract_U32());
1135 tag.SetWeekdays(vresp->extract_U32());
1136 tag.SetTitle(vresp->extract_String());
1137 tag.SetMarginStart(0);
1138 tag.SetMarginEnd(0);
1139
1140 if (GetProtocol() >= 9)
1141 {
1142 tag.SetEPGSearchString(vresp->extract_String());
1143
1144 if (tag.GetTimerType() == VNSI_TIMER_TYPE_MAN && tag.GetWeekdays())
1145 tag.SetTimerType(VNSI_TIMER_TYPE_MAN_REPEAT);
1146 }
1147
1148 if (GetProtocol() >= 10)
1149 {
1150 tag.SetParentClientIndex(vresp->extract_U32());
1151 }
1152
1153 if (tag.GetStartTime() == 0)
1154 tag.SetStartAnyTime(true);
1155 if (tag.GetEndTime() == 0)
1156 tag.SetEndAnyTime(true);
1157
1158 results.Add(tag);
1159
1160 if (tag.GetTimerType() == VNSI_TIMER_TYPE_MAN_REPEAT &&
1161 tag.GetState() != PVR_TIMER_STATE_DISABLED)
1162 {
1163 GenTimerChildren(tag, results);
1164 }
1165 }
1166 }
1167 return PVR_ERROR_NO_ERROR;
1168 }
1169 catch (std::exception e)
1170 {
1171 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1172 return PVR_ERROR_SERVER_ERROR;
1173 }
1174 }
1175
1176 PVR_ERROR CVNSIClientInstance::AddTimer(const kodi::addon::PVRTimer& timer)
1177 {
1178 try
1179 {
1180 cRequestPacket vrp;
1181 vrp.init(VNSI_TIMER_ADD);
1182
1183 // add directory in front of the title
1184 std::string path = GenTimerFolder(timer.GetDirectory(), timer.GetTitle());
1185 if (path.empty())
1186 {
1187 kodi::Log(ADDON_LOG_ERROR, "%s - Empty filename !", __func__);
1188 return PVR_ERROR_UNKNOWN;
1189 }
1190
1191 // use timer margin to calculate start/end times
1192 uint32_t starttime = timer.GetStartTime() - timer.GetMarginStart() * 60;
1193 uint32_t endtime = timer.GetEndTime() + timer.GetMarginEnd() * 60;
1194
1195 if (GetProtocol() >= 9)
1196 {
1197 vrp.add_U32(timer.GetTimerType());
1198 }
1199
1200 vrp.add_U32(timer.GetState() == PVR_TIMER_STATE_SCHEDULED);
1201 vrp.add_U32(timer.GetPriority());
1202 vrp.add_U32(timer.GetLifetime());
1203 vrp.add_U32(timer.GetClientChannelUid());
1204 vrp.add_U32(starttime);
1205 vrp.add_U32(endtime);
1206 vrp.add_U32(timer.GetWeekdays() != PVR_WEEKDAY_NONE ? timer.GetFirstDay() : 0);
1207 vrp.add_U32(timer.GetWeekdays());
1208 vrp.add_String(path.c_str());
1209 vrp.add_String(timer.GetTitle().c_str());
1210
1211 if (GetProtocol() >= 9)
1212 {
1213 vrp.add_String(timer.GetEPGSearchString().c_str());
1214 }
1215
1216 if (GetProtocol() >= 10)
1217 {
1218 vrp.add_U32(timer.GetMarginStart() * 60);
1219 vrp.add_U32(timer.GetMarginEnd() * 60);
1220 }
1221
1222 auto vresp = ReadResult(&vrp);
1223 if (vresp == nullptr || vresp->noResponse())
1224 {
1225 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
1226 return PVR_ERROR_UNKNOWN;
1227 }
1228 uint32_t returnCode = vresp->extract_U32();
1229 if (returnCode == VNSI_RET_DATALOCKED)
1230 return PVR_ERROR_ALREADY_PRESENT;
1231 else if (returnCode == VNSI_RET_DATAINVALID)
1232 return PVR_ERROR_INVALID_PARAMETERS;
1233 else if (returnCode == VNSI_RET_ERROR)
1234 return PVR_ERROR_SERVER_ERROR;
1235
1236 return PVR_ERROR_NO_ERROR;
1237 }
1238 catch (std::exception e)
1239 {
1240 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1241 return PVR_ERROR_SERVER_ERROR;
1242 }
1243 }
1244
1245 PVR_ERROR CVNSIClientInstance::DeleteTimer(const kodi::addon::PVRTimer& timer, bool forceDelete)
1246 {
1247 try
1248 {
1249 cRequestPacket vrp;
1250 vrp.init(VNSI_TIMER_DELETE);
1251 vrp.add_U32(timer.GetClientIndex());
1252 vrp.add_U32(forceDelete);
1253
1254 auto vresp = ReadResult(&vrp);
1255 if (vresp == nullptr || vresp->noResponse())
1256 {
1257 return PVR_ERROR_UNKNOWN;
1258 }
1259 uint32_t returnCode = vresp->extract_U32();
1260
1261 if (returnCode == VNSI_RET_DATALOCKED)
1262 return PVR_ERROR_FAILED;
1263 else if (returnCode == VNSI_RET_RECRUNNING)
1264 return PVR_ERROR_RECORDING_RUNNING;
1265 else if (returnCode == VNSI_RET_DATAINVALID)
1266 return PVR_ERROR_INVALID_PARAMETERS;
1267 else if (returnCode == VNSI_RET_ERROR)
1268 return PVR_ERROR_SERVER_ERROR;
1269 return PVR_ERROR_NO_ERROR;
1270 }
1271 catch (std::exception e)
1272 {
1273 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1274 return PVR_ERROR_SERVER_ERROR;
1275 }
1276 }
1277
1278 PVR_ERROR CVNSIClientInstance::UpdateTimer(const kodi::addon::PVRTimer& timer)
1279 {
1280 try
1281 {
1282 // use timer margin to calculate start/end times
1283 uint32_t starttime = timer.GetStartTime() - timer.GetMarginStart() * 60;
1284 uint32_t endtime = timer.GetEndTime() + timer.GetMarginEnd() * 60;
1285
1286 // add directory in front of the title
1287 std::string path = GenTimerFolder(timer.GetDirectory(), timer.GetTitle());
1288 if (path.empty())
1289 {
1290 kodi::Log(ADDON_LOG_ERROR, "%s - Empty filename !", __func__);
1291 return PVR_ERROR_UNKNOWN;
1292 }
1293
1294 cRequestPacket vrp;
1295 vrp.init(VNSI_TIMER_UPDATE);
1296
1297 vrp.add_U32(timer.GetClientIndex());
1298 if (GetProtocol() >= 9)
1299 {
1300 vrp.add_U32(timer.GetTimerType());
1301 }
1302 vrp.add_U32(timer.GetState() == PVR_TIMER_STATE_SCHEDULED);
1303 vrp.add_U32(timer.GetPriority());
1304 vrp.add_U32(timer.GetLifetime());
1305 vrp.add_U32(timer.GetClientChannelUid());
1306 vrp.add_U32(starttime);
1307 vrp.add_U32(endtime);
1308 vrp.add_U32(timer.GetWeekdays() != PVR_WEEKDAY_NONE ? timer.GetFirstDay() : 0);
1309 vrp.add_U32(timer.GetWeekdays());
1310 vrp.add_String(path.c_str());
1311 vrp.add_String(timer.GetTitle().c_str());
1312
1313 if (GetProtocol() >= 9)
1314 {
1315 vrp.add_String(timer.GetEPGSearchString().c_str());
1316 }
1317
1318 auto vresp = ReadResult(&vrp);
1319 if (vresp == nullptr || vresp->noResponse())
1320 {
1321 return PVR_ERROR_UNKNOWN;
1322 }
1323 uint32_t returnCode = vresp->extract_U32();
1324 if (returnCode == VNSI_RET_DATAUNKNOWN)
1325 return PVR_ERROR_FAILED;
1326 else if (returnCode == VNSI_RET_DATAINVALID)
1327 return PVR_ERROR_INVALID_PARAMETERS;
1328 else if (returnCode == VNSI_RET_ERROR)
1329 return PVR_ERROR_SERVER_ERROR;
1330
1331 return PVR_ERROR_NO_ERROR;
1332 }
1333 catch (std::exception e)
1334 {
1335 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1336 return PVR_ERROR_SERVER_ERROR;
1337 }
1338 }
1339
1340 bool CVNSIClientInstance::GenTimerChildren(const kodi::addon::PVRTimer& timer,
1341 kodi::addon::PVRTimersResultSet& results)
1342 {
1343 time_t now = time(nullptr);
1344 time_t firstDay = timer.GetFirstDay();
1345 time_t startTime = timer.GetStartTime();
1346 time_t endTime = timer.GetEndTime();
1347
1348 struct tm* loctime = localtime(&startTime);
1349 int startSec = loctime->tm_hour * 3600 + loctime->tm_min * 60;
1350 loctime = localtime(&endTime);
1351 int stopSec = loctime->tm_hour * 3600 + loctime->tm_min * 60;
1352 int length = stopSec - startSec;
1353 if (length < 0)
1354 length += 3600 * 24;
1355
1356 for (int n = 0; n < 2; ++n)
1357 {
1358 for (int i = -1; i <= 7; i++)
1359 {
1360 time_t t0 = IncDay(firstDay ? std::max(firstDay, now) : now, i);
1361 if (DayMatches(t0, timer.GetWeekdays()))
1362 {
1363 time_t start = SetTime(t0, startSec);
1364 time_t stop = start + length;
1365 if ((!firstDay || start >= firstDay) && now < stop)
1366 {
1367 kodi::addon::PVRTimer child = timer;
1368
1369 child.SetClientIndex(timer.GetClientIndex() + n | 0xF000);
1370 child.SetParentClientIndex(timer.GetClientIndex());
1371 child.SetTimerType(VNSI_TIMER_TYPE_MAN_REPEAT_CHILD);
1372 child.SetStartTime(start);
1373 child.SetEndTime(stop);
1374 child.SetWeekdays(0);
1375
1376 results.Add(child);
1377
1378 firstDay = start + length + 300;
1379 break;
1380 }
1381 }
1382 }
1383 }
1384 return true;
1385 }
1386
1387 std::string CVNSIClientInstance::GenTimerFolder(std::string directory, std::string title)
1388 {
1389 // add directory in front of the title
1390 std::string path;
1391 if (strlen(directory.c_str()) > 0)
1392 {
1393 path += directory;
1394 if (path == "/")
1395 {
1396 path.clear();
1397 }
1398 else if (path.size() > 1)
1399 {
1400 if (path[0] == '/')
1401 {
1402 path = path.substr(1);
1403 }
1404 }
1405
1406 if (path.size() > 0 && path[path.size() - 1] != '/')
1407 {
1408 path += "/";
1409 }
1410 }
1411
1412 // replace directory separators
1413 for (std::size_t i = 0; i < path.size(); i++)
1414 {
1415 if (path[i] == '/' || path[i] == '\\')
1416 {
1417 path[i] = '~';
1418 }
1419 }
1420
1421 if (strlen(title.c_str()) > 0)
1422 {
1423 path += title;
1424 }
1425
1426 // replace colons
1427 for (std::size_t i = 0; i < path.size(); i++)
1428 {
1429 if (path[i] == ':')
1430 {
1431 path[i] = '|';
1432 }
1433 }
1434
1435 return path;
1436 }
1437
1438 PVR_ERROR CVNSIClientInstance::GetTimerInfo(unsigned int timernumber, kodi::addon::PVRTimer& tag)
1439 {
1440 cRequestPacket vrp;
1441
1442 vrp.init(VNSI_TIMER_GET);
1443 vrp.add_U32(timernumber);
1444
1445 auto vresp = ReadResult(&vrp);
1446 if (!vresp)
1447 {
1448 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
1449 return PVR_ERROR_UNKNOWN;
1450 }
1451
1452 uint32_t returnCode = vresp->extract_U32();
1453 if (returnCode != VNSI_RET_OK)
1454 {
1455 if (returnCode == VNSI_RET_DATAUNKNOWN)
1456 return PVR_ERROR_FAILED;
1457 else if (returnCode == VNSI_RET_ERROR)
1458 return PVR_ERROR_SERVER_ERROR;
1459 }
1460
1461 if (GetProtocol() >= 9)
1462 {
1463 tag.SetTimerType(vresp->extract_U32());
1464 }
1465
1466 tag.SetClientIndex(vresp->extract_U32());
1467 int iActive(vresp->extract_U32());
1468 int iRecording(vresp->extract_U32());
1469 int iPending(vresp->extract_U32());
1470 if (iRecording)
1471 tag.SetState(PVR_TIMER_STATE_RECORDING);
1472 else if (iPending || iActive)
1473 tag.SetState(PVR_TIMER_STATE_SCHEDULED);
1474 else
1475 tag.SetState(PVR_TIMER_STATE_DISABLED);
1476 tag.SetPriority(vresp->extract_U32());
1477 tag.SetLifetime(vresp->extract_U32());
1478 vresp->extract_U32(); // channel number - unused
1479 tag.SetClientChannelUid(vresp->extract_U32());
1480 tag.SetStartTime(vresp->extract_U32());
1481 tag.SetEndTime(vresp->extract_U32());
1482 tag.SetFirstDay(vresp->extract_U32());
1483 tag.SetWeekdays(vresp->extract_U32());
1484 tag.SetTitle(vresp->extract_String());
1485
1486 if (GetProtocol() >= 9)
1487 {
1488 tag.SetEPGSearchString(vresp->extract_String());
1489
1490 if (tag.GetTimerType() == VNSI_TIMER_TYPE_MAN && tag.GetWeekdays())
1491 tag.SetTimerType(VNSI_TIMER_TYPE_MAN_REPEAT);
1492 }
1493
1494 if (GetProtocol() >= 10)
1495 {
1496 tag.SetParentClientIndex(vresp->extract_U32());
1497 }
1498
1499 return PVR_ERROR_NO_ERROR;
1500 }
1501
1502 PVR_ERROR CVNSIClientInstance::RenameTimer(const kodi::addon::PVRTimer& timerinfo,
1503 const std::string& newname)
1504 {
1505 kodi::addon::PVRTimer timerinfo1;
1506
1507 PVR_ERROR ret = GetTimerInfo(timerinfo.GetClientIndex(), timerinfo1);
1508 if (ret != PVR_ERROR_NO_ERROR)
1509 return ret;
1510
1511 timerinfo1.SetTitle(newname);
1512 return UpdateTimer(timerinfo1);
1513 }
1514
1515
1516 /*******************************************/
1517 /** PVR Live Stream Functions **/
1518
1519 bool CVNSIClientInstance::OpenLiveStream(const kodi::addon::PVRChannel& channel)
1520 {
1521 CloseLiveStream();
1522
1523 try
1524 {
1525 m_demuxer = new cVNSIDemux(*this);
1526 m_isRealtime = true;
1527 if (!m_demuxer->OpenChannel(channel))
1528 {
1529 delete m_demuxer;
1530 m_demuxer = nullptr;
1531 return false;
1532 }
1533
1534 return true;
1535 }
1536 catch (std::exception e)
1537 {
1538 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1539 delete m_demuxer;
1540 m_demuxer = nullptr;
1541 return false;
1542 }
1543 return false;
1544 }
1545
1546 void CVNSIClientInstance::CloseLiveStream()
1547 {
1548 delete m_demuxer;
1549 m_demuxer = nullptr;
1550 }
1551
1552
1553 PVR_ERROR CVNSIClientInstance::GetStreamProperties(
1554 std::vector<kodi::addon::PVRStreamProperties>& properties)
1555 {
1556 if (!m_demuxer)
1557 return PVR_ERROR_SERVER_ERROR;
1558
1559 return (m_demuxer->GetStreamProperties(properties) ? PVR_ERROR_NO_ERROR : PVR_ERROR_SERVER_ERROR);
1560 }
1561
1562 DEMUX_PACKET* CVNSIClientInstance::DemuxRead()
1563 {
1564 if (!m_demuxer)
1565 return nullptr;
1566
1567 DEMUX_PACKET* pkt;
1568 try
1569 {
1570 pkt = m_demuxer->Read();
1571 }
1572 catch (std::exception e)
1573 {
1574 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1575 return nullptr;
1576 }
1577
1578 if (pkt)
1579 {
1580 std::lock_guard<std::recursive_mutex> lock(m_timeshiftMutex);
1581 m_isTimeshift = m_demuxer->IsTimeshift();
1582 if ((m_ptsBufferEnd - pkt->dts) / STREAM_TIME_BASE > 10)
1583 m_isTimeshift = false;
1584 else
1585 m_isTimeshift = true;
1586 }
1587 return pkt;
1588 }
1589
1590 void CVNSIClientInstance::DemuxAbort()
1591 {
1592 if (m_demuxer)
1593 m_demuxer->Abort();
1594 }
1595
1596 bool CVNSIClientInstance::SeekTime(double time, bool backwards, double& startpts)
1597 {
1598 bool ret = false;
1599 try
1600 {
1601 if (m_demuxer)
1602 ret = m_demuxer->SeekTime(time, backwards, startpts);
1603 }
1604 catch (std::exception e)
1605 {
1606 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1607 }
1608 return ret;
1609 }
1610
1611 bool CVNSIClientInstance::CanPauseStream()
1612 {
1613 bool ret = false;
1614 if (m_demuxer)
1615 ret = m_demuxer->IsTimeshift();
1616 return ret;
1617 }
1618
1619 bool CVNSIClientInstance::CanSeekStream()
1620 {
1621 bool ret = false;
1622 if (m_demuxer)
1623 ret = m_demuxer->IsTimeshift();
1624 return ret;
1625 }
1626
1627 bool CVNSIClientInstance::IsRealTimeStream()
1628 {
1629 if (m_demuxer)
1630 {
1631 std::lock_guard<std::recursive_mutex> lock(m_timeshiftMutex);
1632 if (!m_isTimeshift)
1633 return true;
1634 if (m_isRealtime)
1635 return true;
1636 }
1637 return false;
1638 }
1639
1640 PVR_ERROR CVNSIClientInstance::GetStreamTimes(kodi::addon::PVRStreamTimes& times)
1641 {
1642 if (m_demuxer && m_demuxer->GetStreamTimes(times))
1643 {
1644 m_ptsBufferEnd = times.GetPTSEnd();
1645 return PVR_ERROR_NO_ERROR;
1646 }
1647 else if (m_recording && m_recording->GetStreamTimes(times))
1648 {
1649 m_ptsBufferEnd = times.GetPTSEnd();
1650 return PVR_ERROR_NO_ERROR;
1651 }
1652 else
1653 return PVR_ERROR_SERVER_ERROR;
1654 }
1655
1656 /*******************************************/
1657 /** PVR Recording Stream Functions **/
1658
1659 bool CVNSIClientInstance::OpenRecordedStream(const kodi::addon::PVRRecording& recording)
1660 {
1661 CloseRecordedStream();
1662
1663 m_recording = new cVNSIRecording(*this);
1664 try
1665 {
1666 if (!m_recording->OpenRecording(recording))
1667 {
1668 delete m_recording;
1669 m_recording = nullptr;
1670 return false;
1671 }
1672
1673 return true;
1674 }
1675 catch (std::exception e)
1676 {
1677 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1678 delete m_recording;
1679 m_recording = nullptr;
1680 return false;
1681 }
1682 }
1683
1684 void CVNSIClientInstance::CloseRecordedStream()
1685 {
1686 delete m_recording;
1687 m_recording = nullptr;
1688 }
1689
1690 int CVNSIClientInstance::ReadRecordedStream(unsigned char* buffer, unsigned int size)
1691 {
1692 if (!m_recording)
1693 return -1;
1694
1695 try
1696 {
1697 return m_recording->Read(buffer, size);
1698 }
1699 catch (std::exception e)
1700 {
1701 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1702 return -1;
1703 }
1704 }
1705
1706 int64_t CVNSIClientInstance::SeekRecordedStream(int64_t position, int whence)
1707 {
1708 try
1709 {
1710 if (m_recording)
1711 return m_recording->Seek(position, whence);
1712 }
1713 catch (std::exception e)
1714 {
1715 kodi::Log(ADDON_LOG_ERROR, "%s - %s", __func__, e.what());
1716 }
1717
1718 return -1;
1719 }
1720
1721 int64_t CVNSIClientInstance::LengthRecordedStream()
1722 {
1723 if (m_recording)
1724 return m_recording->Length();
1725 return 0;
1726 }
1727
1728 PVR_ERROR CVNSIClientInstance::GetStreamReadChunkSize(int& chunksize)
1729 {
1730 chunksize = CVNSISettings::Get().GetChunkSize();
1731 return PVR_ERROR_NO_ERROR;
1732 }
1733
1734 //--==----==----==----==----==----==----==----==----==----==----==----==----==
1735
1736 bool CVNSIClientInstance::EnableStatusInterface(bool onOff, bool wait)
1737 {
1738 cRequestPacket vrp;
1739 vrp.init(VNSI_ENABLESTATUSINTERFACE);
1740 vrp.add_U8(onOff);
1741
1742 if (!wait)
1743 {
1744 cVNSISession::TransmitMessage(&vrp);
1745 return true;
1746 }
1747
1748 auto vresp = ReadResult(&vrp);
1749 if (!vresp)
1750 {
1751 kodi::Log(ADDON_LOG_ERROR, "%s - Can't get response packed", __func__);
1752 return false;
1753 }
1754
1755 uint32_t ret = vresp->extract_U32();
1756 return ret == VNSI_RET_OK ? true : false;
1757 }
1758
1759 bool CVNSIClientInstance::OnResponsePacket(cResponsePacket* pkt)
1760 {
1761 return false;
1762 }
1763
1764 std::unique_ptr<cResponsePacket> CVNSIClientInstance::ReadResult(cRequestPacket* vrp)
1765 {
1766 SMessage& message = m_queue.Enqueue(vrp->getSerial());
1767
1768 std::unique_lock<std::recursive_mutex> lock(m_queue.m_mutex);
1769 if (cVNSISession::TransmitMessage(vrp) &&
1770 message.m_condition.wait_for(
1771 lock, std::chrono::seconds(CVNSISettings::Get().GetConnectTimeout())) ==
1772 std::cv_status::timeout)
1773 {
1774 kodi::Log(ADDON_LOG_ERROR, "%s - request timed out after %d seconds", __func__,
1775 CVNSISettings::Get().GetConnectTimeout());
1776 }
1777
1778 return m_queue.Dequeue(vrp->getSerial(), message);
1779 }
1780
1781 void CVNSIClientInstance::Process()
1782 {
1783 std::unique_ptr<cResponsePacket> vresp;
1784
1785 while (m_running)
1786 {
1787 // try to reconnect
1788 if (m_connectionLost)
1789 {
1790 // First wake up the VDR server in case a MAC-Address is specified
1791 if (!m_wolMac.empty())
1792 {
1793 if (!kodi::network::WakeOnLan(m_wolMac))
1794 {
1795 kodi::Log(ADDON_LOG_ERROR, "Error waking up VNSI Server at MAC-Address %s",
1796 m_wolMac.c_str());
1797 }
1798 }
1799
1800 cVNSISession::eCONNECTIONSTATE state = TryReconnect();
1801 if (state != cVNSISession::CONN_ESABLISHED)
1802 {
1803 if (state == cVNSISession::CONN_HOST_NOT_REACHABLE)
1804 {
1805 kodi::addon::CInstancePVRClient::ConnectionStateChange(
1806 "vnsi server not reacheable", PVR_CONNECTION_STATE_SERVER_UNREACHABLE, "");
1807 }
1808
1809 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1810 continue;
1811 }
1812 }
1813
1814 // if there's anything in the buffer, read it
1815 if ((vresp = cVNSISession::ReadMessage(5, 10000)) == nullptr)
1816 {
1817 std::this_thread::sleep_for(std::chrono::milliseconds(5));
1818 continue;
1819 }
1820
1821 // CHANNEL_REQUEST_RESPONSE
1822 if (vresp->getChannelID() == VNSI_CHANNEL_REQUEST_RESPONSE)
1823 {
1824 m_queue.Set(std::move(vresp));
1825 }
1826
1827 // CHANNEL_STATUS
1828
1829 else if (vresp->getChannelID() == VNSI_CHANNEL_STATUS)
1830 {
1831 if (vresp->getRequestID() == VNSI_STATUS_MESSAGE)
1832 {
1833 uint32_t type = vresp->extract_U32();
1834 char* msgstr = vresp->extract_String();
1835
1836 std::string strMessageTranslated;
1837 if (CVNSISettings::Get().GetCharsetConv())
1838 kodi::UnknownToUTF8(msgstr, strMessageTranslated);
1839 else
1840 strMessageTranslated = msgstr;
1841
1842 if (type == 2)
1843 kodi::QueueNotification(QUEUE_ERROR, "", strMessageTranslated);
1844 if (type == 1)
1845 kodi::QueueNotification(QUEUE_WARNING, "", strMessageTranslated);
1846 else
1847 kodi::QueueNotification(QUEUE_INFO, "", strMessageTranslated);
1848 }
1849 else if (vresp->getRequestID() == VNSI_STATUS_RECORDING)
1850 {
1851 vresp->extract_U32(); // device currently unused
1852 vresp->extract_U32(); // on (not used)
1853 char* str1 = vresp->extract_String();
1854 char* str2 = vresp->extract_String();
1855
1856 // kodi::addon::CInstancePVRClient::Recording(str1, str2, on!=0?true:false);
1857 kodi::addon::CInstancePVRClient::TriggerTimerUpdate();
1858 }
1859 else if (vresp->getRequestID() == VNSI_STATUS_TIMERCHANGE)
1860 {
1861 kodi::Log(ADDON_LOG_DEBUG, "Server requested timer update");
1862 kodi::addon::CInstancePVRClient::TriggerTimerUpdate();
1863 }
1864 else if (vresp->getRequestID() == VNSI_STATUS_CHANNELCHANGE)
1865 {
1866 kodi::Log(ADDON_LOG_DEBUG, "Server requested channel update");
1867 kodi::addon::CInstancePVRClient::TriggerChannelUpdate();
1868 }
1869 else if (vresp->getRequestID() == VNSI_STATUS_RECORDINGSCHANGE)
1870 {
1871 kodi::Log(ADDON_LOG_DEBUG, "Server requested recordings update");
1872 kodi::addon::CInstancePVRClient::TriggerRecordingUpdate();
1873 }
1874 else if (vresp->getRequestID() == VNSI_STATUS_EPGCHANGE)
1875 {
1876 uint32_t channel = vresp->extract_U32();
1877 kodi::Log(ADDON_LOG_DEBUG, "Server requested Epg update for channel: %d", channel);
1878 kodi::addon::CInstancePVRClient::TriggerEpgUpdate(channel);
1879 }
1880 }
1881
1882 // UNKOWN CHANNELID
1883
1884 else if (!OnResponsePacket(vresp.get()))
1885 {
1886 kodi::Log(ADDON_LOG_ERROR, "%s - Rxd a response packet on channel %lu !!", __func__,
1887 vresp->getChannelID());
1888 }
1889 }
1890 }