-
Notifications
You must be signed in to change notification settings - Fork 45
/
hdhomerun_device.c
1597 lines (1304 loc) · 42.3 KB
/
hdhomerun_device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* hdhomerun_device.c
*
* Copyright © 2006-2022 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "hdhomerun.h"
struct hdhomerun_device_t {
struct hdhomerun_control_sock_t *cs;
struct hdhomerun_video_sock_t *vs;
struct hdhomerun_debug_t *dbg;
struct hdhomerun_channelscan_t *scan;
struct sockaddr_storage multicast_addr;
uint32_t device_id;
unsigned int tuner;
uint32_t lockkey;
char name[32];
char model[32];
};
int hdhomerun_device_set_device(struct hdhomerun_device_t *hd, uint32_t device_id, uint32_t device_ip)
{
struct sockaddr_in device_addr;
memset(&device_addr, 0, sizeof(device_addr));
device_addr.sin_family = AF_INET;
device_addr.sin_addr.s_addr = htonl(device_ip);
return hdhomerun_device_set_device_ex(hd, device_id, (const struct sockaddr *)&device_addr);
}
int hdhomerun_device_set_device_ex(struct hdhomerun_device_t *hd, uint32_t device_id, const struct sockaddr *device_addr)
{
if ((device_id == 0) && !hdhomerun_sock_sockaddr_is_addr(device_addr)) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_set_device: device not specified\n");
return -1;
}
if (hdhomerun_sock_sockaddr_is_multicast(device_addr)) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_set_device: invalid address\n");
return -1;
}
if (!hd->cs) {
hd->cs = hdhomerun_control_create(0, 0, hd->dbg);
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_set_device: failed to create control object\n");
return -1;
}
}
hdhomerun_control_set_device_ex(hd->cs, device_id, device_addr);
if ((device_id == 0) || (device_id == HDHOMERUN_DEVICE_ID_WILDCARD)) {
device_id = hdhomerun_control_get_device_id(hd->cs);
}
memset(&hd->multicast_addr, 0, sizeof(hd->multicast_addr));
hd->device_id = device_id;
hd->tuner = 0;
hd->lockkey = 0;
hdhomerun_sprintf(hd->name, hd->name + sizeof(hd->name), "%08X-%u", (unsigned int)hd->device_id, hd->tuner);
hdhomerun_sprintf(hd->model, hd->model + sizeof(hd->model), ""); /* clear cached model string */
return 1;
}
int hdhomerun_device_set_multicast(struct hdhomerun_device_t *hd, uint32_t multicast_ip, uint16_t multicast_port)
{
struct sockaddr_in multicast_addr;
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = htonl(multicast_ip);
multicast_addr.sin_port = htons(multicast_port);
return hdhomerun_device_set_multicast_ex(hd, (const struct sockaddr *)&multicast_addr);
}
int hdhomerun_device_set_multicast_ex(struct hdhomerun_device_t *hd, const struct sockaddr *multicast_addr)
{
if (!hdhomerun_sock_sockaddr_is_multicast(multicast_addr)) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_set_device_multicast: invalid address\n");
return -1;
}
uint16_t multicast_port = hdhomerun_sock_sockaddr_get_port(multicast_addr);
if (multicast_port == 0) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_set_device_multicast: invalid port %u\n", (unsigned int)multicast_port);
return -1;
}
if (hd->cs) {
hdhomerun_control_destroy(hd->cs);
hd->cs = NULL;
}
hdhomerun_sock_sockaddr_copy(&hd->multicast_addr, multicast_addr);
hd->device_id = 0;
hd->tuner = 0;
hd->lockkey = 0;
hdhomerun_sprintf(hd->name, hd->name + sizeof(hd->name), "multicast:%u", (unsigned int)multicast_port);
hdhomerun_sprintf(hd->model, hd->model + sizeof(hd->model), "multicast");
return 1;
}
int hdhomerun_device_set_tuner(struct hdhomerun_device_t *hd, unsigned int tuner)
{
if (hdhomerun_sock_sockaddr_is_addr((struct sockaddr *)&hd->multicast_addr)) {
if (tuner != 0) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_set_tuner: tuner cannot be specified in multicast mode\n");
return -1;
}
return 1;
}
hd->tuner = tuner;
hdhomerun_sprintf(hd->name, hd->name + sizeof(hd->name), "%08X-%u", (unsigned int)hd->device_id, hd->tuner);
return 1;
}
int hdhomerun_device_set_tuner_from_str(struct hdhomerun_device_t *hd, const char *tuner_str)
{
unsigned int tuner;
if (sscanf(tuner_str, "%u", &tuner) == 1) {
hdhomerun_device_set_tuner(hd, tuner);
return 1;
}
if (sscanf(tuner_str, "/tuner%u", &tuner) == 1) {
hdhomerun_device_set_tuner(hd, tuner);
return 1;
}
return -1;
}
static struct hdhomerun_device_t *hdhomerun_device_create_internal(struct hdhomerun_debug_t *dbg)
{
struct hdhomerun_device_t *hd = (struct hdhomerun_device_t *)calloc(1, sizeof(struct hdhomerun_device_t));
if (!hd) {
hdhomerun_debug_printf(dbg, "hdhomerun_device_create: failed to allocate device object\n");
return NULL;
}
hd->dbg = dbg;
return hd;
}
struct hdhomerun_device_t *hdhomerun_device_create(uint32_t device_id, uint32_t device_ip, unsigned int tuner, struct hdhomerun_debug_t *dbg)
{
struct sockaddr_in device_addr;
memset(&device_addr, 0, sizeof(device_addr));
device_addr.sin_family = AF_INET;
device_addr.sin_addr.s_addr = htonl(device_ip);
return hdhomerun_device_create_ex(device_id, (const struct sockaddr *)&device_addr, tuner, dbg);
}
struct hdhomerun_device_t *hdhomerun_device_create_ex(uint32_t device_id, const struct sockaddr *device_addr, unsigned int tuner, struct hdhomerun_debug_t *dbg)
{
if ((device_id != 0) && !hdhomerun_discover_validate_device_id(device_id)) {
return NULL;
}
struct hdhomerun_device_t *hd = hdhomerun_device_create_internal(dbg);
if (!hd) {
return NULL;
}
if ((device_id == 0) && !hdhomerun_sock_sockaddr_is_addr(device_addr) && (tuner == 0)) {
return hd;
}
if (hdhomerun_device_set_device_ex(hd, device_id, device_addr) <= 0) {
free(hd);
return NULL;
}
if (hdhomerun_device_set_tuner(hd, tuner) <= 0) {
free(hd);
return NULL;
}
return hd;
}
struct hdhomerun_device_t *hdhomerun_device_create_multicast(uint32_t multicast_ip, uint16_t multicast_port, struct hdhomerun_debug_t *dbg)
{
struct sockaddr_in multicast_addr;
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = htonl(multicast_ip);
multicast_addr.sin_port = htons(multicast_port);
return hdhomerun_device_create_multicast_ex((const struct sockaddr *)&multicast_addr, dbg);
}
struct hdhomerun_device_t *hdhomerun_device_create_multicast_ex(const struct sockaddr *multicast_addr, struct hdhomerun_debug_t *dbg)
{
struct hdhomerun_device_t *hd = hdhomerun_device_create_internal(dbg);
if (!hd) {
return NULL;
}
if (hdhomerun_device_set_multicast_ex(hd, multicast_addr) <= 0) {
free(hd);
return NULL;
}
return hd;
}
void hdhomerun_device_destroy(struct hdhomerun_device_t *hd)
{
if (hd->scan) {
channelscan_destroy(hd->scan);
}
if (hd->vs) {
hdhomerun_video_destroy(hd->vs);
}
if (hd->cs) {
hdhomerun_control_destroy(hd->cs);
}
free(hd);
}
static bool hdhomerun_device_create_from_str_parse_device_id(const char *name, uint32_t *pdevice_id)
{
char *end;
uint32_t device_id = (uint32_t)strtoul(name, &end, 16);
if (end != name + 8) {
return false;
}
if (*end != 0) {
return false;
}
*pdevice_id = device_id;
return true;
}
static bool hdhomerun_device_create_from_str_parse_dns(const char *name, struct sockaddr_storage *device_addr)
{
const char *ptr = name;
if (*ptr == 0) {
return false;
}
while (1) {
char c = *ptr++;
if (c == 0) {
break;
}
if ((c >= '0') && (c <= '9')) {
continue;
}
if ((c >= 'a') && (c <= 'z')) {
continue;
}
if ((c >= 'A') && (c <= 'Z')) {
continue;
}
if ((c == '.') || (c == '-')) {
continue;
}
return false;
}
return hdhomerun_sock_getaddrinfo_addr_ex(AF_INET, name, device_addr);
}
static struct hdhomerun_device_t *hdhomerun_device_create_from_str_tail(const char *tail, uint32_t device_id, struct sockaddr_storage *device_addr, struct hdhomerun_debug_t *dbg)
{
const char *ptr = tail;
if (*ptr == 0) {
return hdhomerun_device_create_ex(device_id, (struct sockaddr *)device_addr, 0, dbg);
}
if (*ptr == ':') {
ptr++;
char *end;
unsigned long port = strtoul(ptr + 1, &end, 10);
if (*end != 0) {
return NULL;
}
if ((port < 1024) || (port > 65535)) {
return NULL;
}
if (device_addr->ss_family == AF_INET) {
struct sockaddr_in *device_addr_in = (struct sockaddr_in *)device_addr;
device_addr_in->sin_port = htons((uint16_t)port);
return hdhomerun_device_create_multicast_ex((struct sockaddr *)device_addr, dbg);
}
if (device_addr->ss_family == AF_INET6) {
struct sockaddr_in6 *device_addr_in = (struct sockaddr_in6 *)device_addr;
device_addr_in->sin6_port = htons((uint16_t)port);
return hdhomerun_device_create_multicast_ex((struct sockaddr *)device_addr, dbg);
}
return NULL;
}
if (*ptr == '-') {
ptr++;
char *end;
unsigned int tuner_index = (unsigned int)strtoul(ptr, &end, 10);
if (*end != 0) {
return NULL;
}
return hdhomerun_device_create_ex(device_id, (struct sockaddr *)device_addr, tuner_index, dbg);
}
return NULL;
}
struct hdhomerun_device_t *hdhomerun_device_create_from_str(const char *device_str, struct hdhomerun_debug_t *dbg)
{
char str[64];
if (!hdhomerun_sprintf(str, str + sizeof(str), "%s", device_str)) {
return NULL;
}
uint32_t device_id = HDHOMERUN_DEVICE_ID_WILDCARD;
struct sockaddr_storage device_addr;
device_addr.ss_family = 0;
char *ptr = str;
bool framed = (*ptr == '[');
if (framed) {
ptr++;
char *end = strchr(ptr, ']');
if (!end) {
return NULL;
}
*end++ = 0;
if (hdhomerun_sock_ip_str_to_sockaddr(ptr, &device_addr)) {
return hdhomerun_device_create_from_str_tail(end, device_id, &device_addr, dbg);
}
return NULL;
}
char *dash = strchr(ptr, '-');
if (dash) {
*dash = 0;
if (hdhomerun_device_create_from_str_parse_device_id(ptr, &device_id)) {
*dash = '-';
return hdhomerun_device_create_from_str_tail(dash, device_id, &device_addr, dbg);
}
if (hdhomerun_sock_ip_str_to_sockaddr(ptr, &device_addr)) {
*dash = '-';
return hdhomerun_device_create_from_str_tail(dash, device_id, &device_addr, dbg);
}
*dash = '-';
if (hdhomerun_device_create_from_str_parse_dns(ptr, &device_addr)) {
return hdhomerun_device_create_ex(device_id, (struct sockaddr *)&device_addr, 0, dbg);
}
return NULL;
}
char *colon = strchr(ptr, ':');
if (colon) {
char *second_colon = strchr(colon, ':');
if (second_colon) {
if (hdhomerun_sock_ip_str_to_sockaddr(ptr, &device_addr)) {
return hdhomerun_device_create_ex(device_id, (struct sockaddr *)&device_addr, 0, dbg);
}
return NULL;
}
*colon = 0;
if (hdhomerun_sock_ip_str_to_sockaddr(ptr, &device_addr)) {
*colon = ':';
return hdhomerun_device_create_from_str_tail(colon, device_id, &device_addr, dbg);
}
return NULL;
}
if (hdhomerun_device_create_from_str_parse_device_id(ptr, &device_id)) {
return hdhomerun_device_create_ex(device_id, (struct sockaddr *)&device_addr, 0, dbg);
}
if (hdhomerun_sock_ip_str_to_sockaddr(ptr, &device_addr)) {
return hdhomerun_device_create_ex(device_id, (struct sockaddr *)&device_addr, 0, dbg);
}
if (hdhomerun_device_create_from_str_parse_dns(ptr, &device_addr)) {
return hdhomerun_device_create_ex(device_id, (struct sockaddr *)&device_addr, 0, dbg);
}
return NULL;
}
const char *hdhomerun_device_get_name(struct hdhomerun_device_t *hd)
{
return hd->name;
}
uint32_t hdhomerun_device_get_device_id(struct hdhomerun_device_t *hd)
{
return hd->device_id;
}
uint32_t hdhomerun_device_get_device_ip(struct hdhomerun_device_t *hd)
{
struct sockaddr_storage device_addr;
if (!hdhomerun_device_get_device_addr(hd, &device_addr)) {
return 0;
}
if (device_addr.ss_family != AF_INET) {
return 0;
}
struct sockaddr_in *device_addr_in = (struct sockaddr_in *)&device_addr;
return ntohl(device_addr_in->sin_addr.s_addr);
}
bool hdhomerun_device_get_device_addr(struct hdhomerun_device_t *hd, struct sockaddr_storage *result)
{
if (hdhomerun_sock_sockaddr_is_addr((struct sockaddr *)&hd->multicast_addr)) {
*result = hd->multicast_addr;
return true;
}
if (!hd->cs) {
memset(result, 0, sizeof(struct sockaddr_storage));
return false;
}
return hdhomerun_control_get_device_addr(hd->cs, result);
}
uint32_t hdhomerun_device_get_device_id_requested(struct hdhomerun_device_t *hd)
{
if (hdhomerun_sock_sockaddr_is_addr((struct sockaddr *)&hd->multicast_addr)) {
return 0;
}
if (!hd->cs) {
return 0;
}
return hdhomerun_control_get_device_id_requested(hd->cs);
}
uint32_t hdhomerun_device_get_device_ip_requested(struct hdhomerun_device_t *hd)
{
struct sockaddr_storage device_addr;
if (!hdhomerun_device_get_device_addr_requested(hd, &device_addr)) {
return 0;
}
if (device_addr.ss_family != AF_INET) {
return 0;
}
struct sockaddr_in *device_addr_in = (struct sockaddr_in *)&device_addr;
return ntohl(device_addr_in->sin_addr.s_addr);
}
bool hdhomerun_device_get_device_addr_requested(struct hdhomerun_device_t *hd, struct sockaddr_storage *result)
{
if (hdhomerun_sock_sockaddr_is_addr((struct sockaddr *)&hd->multicast_addr)) {
*result = hd->multicast_addr;
return true;
}
if (!hd->cs) {
memset(result, 0, sizeof(struct sockaddr_storage));
return false;
}
return hdhomerun_control_get_device_addr_requested(hd->cs, result);
}
unsigned int hdhomerun_device_get_tuner(struct hdhomerun_device_t *hd)
{
return hd->tuner;
}
struct hdhomerun_control_sock_t *hdhomerun_device_get_control_sock(struct hdhomerun_device_t *hd)
{
return hd->cs;
}
struct hdhomerun_video_sock_t *hdhomerun_device_get_video_sock(struct hdhomerun_device_t *hd)
{
if (hd->vs) {
return hd->vs;
}
bool allow_port_reuse = false;
struct sockaddr_storage listen_addr;
memset(&listen_addr, 0, sizeof(listen_addr));
if (hdhomerun_sock_sockaddr_is_addr((struct sockaddr *)&hd->multicast_addr)) {
listen_addr.ss_family = hd->multicast_addr.ss_family;
hdhomerun_sock_sockaddr_set_port((struct sockaddr *)&listen_addr, hdhomerun_sock_sockaddr_get_port((struct sockaddr *)&hd->multicast_addr));
allow_port_reuse = true;
}
struct sockaddr_storage device_addr;
if (!hdhomerun_control_get_device_addr(hd->cs, &device_addr)) {
return NULL;
}
listen_addr.ss_family = device_addr.ss_family;
hd->vs = hdhomerun_video_create_ex((struct sockaddr *)&listen_addr, allow_port_reuse, VIDEO_DATA_BUFFER_SIZE_1S * 2, hd->dbg);
if (!hd->vs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_video_sock: failed to create video object\n");
return NULL;
}
return hd->vs;
}
uint32_t hdhomerun_device_get_local_machine_addr(struct hdhomerun_device_t *hd)
{
struct sockaddr_storage local_addr;
if (!hdhomerun_device_get_local_machine_addr_ex(hd, &local_addr)) {
return 0;
}
if (local_addr.ss_family != AF_INET) {
return 0;
}
struct sockaddr_in *local_addr_in = (struct sockaddr_in *)&local_addr;
return ntohl(local_addr_in->sin_addr.s_addr);
}
bool hdhomerun_device_get_local_machine_addr_ex(struct hdhomerun_device_t *hd, struct sockaddr_storage *result)
{
if (!hd->cs) {
memset(result, 0, sizeof(struct sockaddr_storage));
return false;
}
return hdhomerun_control_get_local_addr_ex(hd->cs, result);
}
static uint32_t hdhomerun_device_get_status_parse(const char *status_str, const char *tag)
{
const char *ptr = strstr(status_str, tag);
if (!ptr) {
return 0;
}
unsigned int value = 0;
(void)sscanf(ptr + strlen(tag), "%u", &value);
return (uint32_t)value;
}
static bool hdhomerun_device_get_tuner_status_lock_is_bcast(struct hdhomerun_tuner_status_t *status)
{
if (strcmp(status->lock_str, "8vsb") == 0) {
return true;
}
if (strcmp(status->lock_str, "atsc3") == 0) {
return true;
}
if (strncmp(status->lock_str, "t8", 2) == 0) {
return true;
}
if (strncmp(status->lock_str, "t7", 2) == 0) {
return true;
}
if (strncmp(status->lock_str, "t6", 2) == 0) {
return true;
}
return false;
}
uint32_t hdhomerun_device_get_tuner_status_ss_color(struct hdhomerun_tuner_status_t *status)
{
unsigned int ss_yellow_min;
unsigned int ss_green_min;
if (!status->lock_supported) {
return HDHOMERUN_STATUS_COLOR_NEUTRAL;
}
if (hdhomerun_device_get_tuner_status_lock_is_bcast(status)) {
ss_yellow_min = 50; /* -30dBmV */
ss_green_min = 75; /* -15dBmV */
} else {
ss_yellow_min = 80; /* -12dBmV */
ss_green_min = 90; /* -6dBmV */
}
if (status->signal_strength >= ss_green_min) {
return HDHOMERUN_STATUS_COLOR_GREEN;
}
if (status->signal_strength >= ss_yellow_min) {
return HDHOMERUN_STATUS_COLOR_YELLOW;
}
return HDHOMERUN_STATUS_COLOR_RED;
}
uint32_t hdhomerun_device_get_tuner_status_snq_color(struct hdhomerun_tuner_status_t *status)
{
if (status->signal_to_noise_quality >= 70) {
return HDHOMERUN_STATUS_COLOR_GREEN;
}
if (status->signal_to_noise_quality >= 50) {
return HDHOMERUN_STATUS_COLOR_YELLOW;
}
return HDHOMERUN_STATUS_COLOR_RED;
}
uint32_t hdhomerun_device_get_tuner_status_seq_color(struct hdhomerun_tuner_status_t *status)
{
if (status->symbol_error_quality >= 100) {
return HDHOMERUN_STATUS_COLOR_GREEN;
}
return HDHOMERUN_STATUS_COLOR_RED;
}
int hdhomerun_device_get_tuner_status(struct hdhomerun_device_t *hd, char **pstatus_str, struct hdhomerun_tuner_status_t *status)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_status: device not set\n");
return -1;
}
memset(status, 0, sizeof(struct hdhomerun_tuner_status_t));
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/status", hd->tuner);
char *status_str;
int ret = hdhomerun_control_get(hd->cs, name, &status_str, NULL);
if (ret <= 0) {
return ret;
}
if (pstatus_str) {
*pstatus_str = status_str;
}
if (status) {
char *channel = strstr(status_str, "ch=");
if (channel) {
(void)sscanf(channel + 3, "%31s", status->channel);
}
char *lock = strstr(status_str, "lock=");
if (lock) {
(void)sscanf(lock + 5, "%31s", status->lock_str);
}
status->signal_strength = (unsigned int)hdhomerun_device_get_status_parse(status_str, "ss=");
status->signal_to_noise_quality = (unsigned int)hdhomerun_device_get_status_parse(status_str, "snq=");
status->symbol_error_quality = (unsigned int)hdhomerun_device_get_status_parse(status_str, "seq=");
status->raw_bits_per_second = hdhomerun_device_get_status_parse(status_str, "bps=");
status->packets_per_second = hdhomerun_device_get_status_parse(status_str, "pps=");
status->signal_present = status->signal_strength >= 35;
if (strcmp(status->lock_str, "none") != 0) {
if (status->lock_str[0] == '(') {
status->lock_unsupported = true;
} else {
status->lock_supported = true;
}
}
}
return 1;
}
int hdhomerun_device_get_oob_status(struct hdhomerun_device_t *hd, char **pstatus_str, struct hdhomerun_tuner_status_t *status)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_oob_status: device not set\n");
return -1;
}
memset(status, 0, sizeof(struct hdhomerun_tuner_status_t));
char *status_str;
int ret = hdhomerun_control_get(hd->cs, "/oob/status", &status_str, NULL);
if (ret <= 0) {
return ret;
}
if (pstatus_str) {
*pstatus_str = status_str;
}
if (status) {
char *channel = strstr(status_str, "ch=");
if (channel) {
(void)sscanf(channel + 3, "%31s", status->channel);
}
char *lock = strstr(status_str, "lock=");
if (lock) {
(void)sscanf(lock + 5, "%31s", status->lock_str);
}
status->signal_strength = (unsigned int)hdhomerun_device_get_status_parse(status_str, "ss=");
status->signal_to_noise_quality = (unsigned int)hdhomerun_device_get_status_parse(status_str, "snq=");
status->signal_present = status->signal_strength >= 35;
status->lock_supported = (strcmp(status->lock_str, "none") != 0);
}
return 1;
}
int hdhomerun_device_get_tuner_vstatus(struct hdhomerun_device_t *hd, char **pvstatus_str, struct hdhomerun_tuner_vstatus_t *vstatus)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_vstatus: device not set\n");
return -1;
}
memset(vstatus, 0, sizeof(struct hdhomerun_tuner_vstatus_t));
char var_name[32];
hdhomerun_sprintf(var_name, var_name + sizeof(var_name), "/tuner%u/vstatus", hd->tuner);
char *vstatus_str;
int ret = hdhomerun_control_get(hd->cs, var_name, &vstatus_str, NULL);
if (ret <= 0) {
return ret;
}
if (pvstatus_str) {
*pvstatus_str = vstatus_str;
}
if (vstatus) {
char *vch = strstr(vstatus_str, "vch=");
if (vch) {
(void)sscanf(vch + 4, "%31s", vstatus->vchannel);
}
char *name = strstr(vstatus_str, "name=");
if (name) {
(void)sscanf(name + 5, "%31s", vstatus->name);
}
char *auth = strstr(vstatus_str, "auth=");
if (auth) {
(void)sscanf(auth + 5, "%31s", vstatus->auth);
}
char *cci = strstr(vstatus_str, "cci=");
if (cci) {
(void)sscanf(cci + 4, "%31s", vstatus->cci);
}
char *cgms = strstr(vstatus_str, "cgms=");
if (cgms) {
(void)sscanf(cgms + 5, "%31s", vstatus->cgms);
}
if (strncmp(vstatus->auth, "not-subscribed", 14) == 0) {
vstatus->not_subscribed = true;
}
if (strncmp(vstatus->auth, "error", 5) == 0) {
vstatus->not_available = true;
}
if (strncmp(vstatus->auth, "dialog", 6) == 0) {
vstatus->not_available = true;
}
if (strncmp(vstatus->cci, "protected", 9) == 0) {
vstatus->copy_protected = true;
}
if (strncmp(vstatus->cgms, "protected", 9) == 0) {
vstatus->copy_protected = true;
}
}
return 1;
}
int hdhomerun_device_get_tuner_plpinfo(struct hdhomerun_device_t *hd, char **pplpinfo)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_plpinfo: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/plpinfo", hd->tuner);
return hdhomerun_control_get(hd->cs, name, pplpinfo, NULL);
}
int hdhomerun_device_get_tuner_streaminfo(struct hdhomerun_device_t *hd, char **pstreaminfo)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_streaminfo: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/streaminfo", hd->tuner);
return hdhomerun_control_get(hd->cs, name, pstreaminfo, NULL);
}
int hdhomerun_device_get_tuner_channel(struct hdhomerun_device_t *hd, char **pchannel)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_channel: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/channel", hd->tuner);
return hdhomerun_control_get(hd->cs, name, pchannel, NULL);
}
int hdhomerun_device_get_tuner_vchannel(struct hdhomerun_device_t *hd, char **pvchannel)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_vchannel: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/vchannel", hd->tuner);
return hdhomerun_control_get(hd->cs, name, pvchannel, NULL);
}
int hdhomerun_device_get_tuner_channelmap(struct hdhomerun_device_t *hd, char **pchannelmap)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_channelmap: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/channelmap", hd->tuner);
return hdhomerun_control_get(hd->cs, name, pchannelmap, NULL);
}
int hdhomerun_device_get_tuner_filter(struct hdhomerun_device_t *hd, char **pfilter)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_filter: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/filter", hd->tuner);
return hdhomerun_control_get(hd->cs, name, pfilter, NULL);
}
int hdhomerun_device_get_tuner_program(struct hdhomerun_device_t *hd, char **pprogram)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_program: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/program", hd->tuner);
return hdhomerun_control_get(hd->cs, name, pprogram, NULL);
}
int hdhomerun_device_get_tuner_target(struct hdhomerun_device_t *hd, char **ptarget)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_target: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/target", hd->tuner);
return hdhomerun_control_get(hd->cs, name, ptarget, NULL);
}
static int hdhomerun_device_get_tuner_plotsample_internal(struct hdhomerun_device_t *hd, const char *name, struct hdhomerun_plotsample_t **psamples, size_t *pcount)
{
char *result;
int ret = hdhomerun_control_get(hd->cs, name, &result, NULL);
if (ret <= 0) {
return ret;
}
struct hdhomerun_plotsample_t *samples = (struct hdhomerun_plotsample_t *)result;
*psamples = samples;
size_t count = 0;
while (1) {
char *ptr = strchr(result, ' ');
if (!ptr) {
break;
}
*ptr++ = 0;
unsigned int raw;
if (sscanf(result, "%x", &raw) != 1) {
break;
}
uint16_t real = (raw >> 12) & 0x0FFF;
if (real & 0x0800) {
real |= 0xF000;
}
uint16_t imag = (raw >> 0) & 0x0FFF;
if (imag & 0x0800) {
imag |= 0xF000;
}
samples->real = (int16_t)real;
samples->imag = (int16_t)imag;
samples++;
count++;
result = ptr;
}
*pcount = count;
return 1;
}
int hdhomerun_device_get_tuner_plotsample(struct hdhomerun_device_t *hd, struct hdhomerun_plotsample_t **psamples, size_t *pcount)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_plotsample: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/plotsample", hd->tuner);
return hdhomerun_device_get_tuner_plotsample_internal(hd, name, psamples, pcount);
}
int hdhomerun_device_get_oob_plotsample(struct hdhomerun_device_t *hd, struct hdhomerun_plotsample_t **psamples, size_t *pcount)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_oob_plotsample: device not set\n");
return -1;
}
return hdhomerun_device_get_tuner_plotsample_internal(hd, "/oob/plotsample", psamples, pcount);
}
int hdhomerun_device_get_tuner_lockkey_owner(struct hdhomerun_device_t *hd, char **powner)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_tuner_lockkey_owner: device not set\n");
return -1;
}
char name[32];
hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/lockkey", hd->tuner);
return hdhomerun_control_get(hd->cs, name, powner, NULL);
}
int hdhomerun_device_get_ir_target(struct hdhomerun_device_t *hd, char **ptarget)
{
if (!hd->cs) {
hdhomerun_debug_printf(hd->dbg, "hdhomerun_device_get_ir_target: device not set\n");
return -1;