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
|
/*
* hdhomerun_config.c
*
* Copyright © 2006-2008 Silicondust Engineering Ltd. <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 3 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, see <http://www.gnu.org/licenses/>.
*
* As a special exception to the GNU Lesser General Public License,
* you may link, statically or dynamically, an application with a
* publicly distributed version of the Library to produce an
* executable file containing portions of the Library, and
* distribute that executable file under terms of your choice,
* without any of the additional requirements listed in clause 4 of
* the GNU Lesser General Public License.
*
* By "a publicly distributed version of the Library", we mean
* either the unmodified Library as distributed by Silicondust, or a
* modified version of the Library that is distributed under the
* conditions defined in the GNU Lesser General Public License.
*/
#include "hdhomerun.h"
/*
* The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing.
* Attempting to restore on exit fails to restore if the program is terminated by the user.
* Solution - set the output format each printf.
*/
#if defined(__WINDOWS__)
#define printf console_printf
#define vprintf console_vprintf
#endif
static const char *appname;
struct hdhomerun_device_t *hd;
static int help(void)
{
printf("Usage:\n");
printf("\t%s discover\n", appname);
printf("\t%s <id> get help\n", appname);
printf("\t%s <id> get <item>\n", appname);
printf("\t%s <id> set <item> <value>\n", appname);
printf("\t%s <id> scan <tuner> [<filename>]\n", appname);
printf("\t%s <id> save <tuner> <filename>\n", appname);
printf("\t%s <id> upgrade <filename>\n", appname);
return -1;
}
static void extract_appname(const char *argv0)
{
const char *ptr = strrchr(argv0, '/');
if (ptr) {
argv0 = ptr + 1;
}
ptr = strrchr(argv0, '\\');
if (ptr) {
argv0 = ptr + 1;
}
appname = argv0;
}
static bool_t contains(const char *arg, const char *cmpstr)
{
if (strcmp(arg, cmpstr) == 0) {
return TRUE;
}
if (*arg++ != '-') {
return FALSE;
}
if (*arg++ != '-') {
return FALSE;
}
if (strcmp(arg, cmpstr) == 0) {
return TRUE;
}
return FALSE;
}
static uint32_t parse_ip_addr(const char *str)
{
unsigned long a[4];
if (sscanf(str, "%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2], &a[3]) != 4) {
return 0;
}
return (uint32_t)((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | (a[3] << 0));
}
static int discover_print(char *target_ip_str)
{
uint32_t target_ip = 0;
if (target_ip_str) {
target_ip = parse_ip_addr(target_ip_str);
if (target_ip == 0) {
fprintf(stderr, "invalid ip address: %s\n", target_ip_str);
return -1;
}
}
struct hdhomerun_discover_device_t result_list[64];
int count = hdhomerun_discover_find_devices_custom(target_ip, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, result_list, 64);
if (count < 0) {
fprintf(stderr, "error sending discover request\n");
return -1;
}
if (count == 0) {
printf("no devices found\n");
return 0;
}
int index;
for (index = 0; index < count; index++) {
struct hdhomerun_discover_device_t *result = &result_list[index];
printf("hdhomerun device %08lX found at %u.%u.%u.%u\n",
(unsigned long)result->device_id,
(unsigned int)(result->ip_addr >> 24) & 0x0FF, (unsigned int)(result->ip_addr >> 16) & 0x0FF,
(unsigned int)(result->ip_addr >> 8) & 0x0FF, (unsigned int)(result->ip_addr >> 0) & 0x0FF
);
}
return count;
}
static int cmd_get(const char *item)
{
char *ret_value;
char *ret_error;
if (hdhomerun_device_get_var(hd, item, &ret_value, &ret_error) < 0) {
fprintf(stderr, "communication error sending request to hdhomerun device\n");
return -1;
}
if (ret_error) {
printf("%s\n", ret_error);
return 0;
}
printf("%s\n", ret_value);
return 1;
}
static int cmd_set_internal(const char *item, const char *value)
{
char *ret_error;
if (hdhomerun_device_set_var(hd, item, value, NULL, &ret_error) < 0) {
fprintf(stderr, "communication error sending request to hdhomerun device\n");
return -1;
}
if (ret_error) {
printf("%s\n", ret_error);
return 0;
}
return 1;
}
static int cmd_set(const char *item, const char *value)
{
if (strcmp(value, "-") == 0) {
char *buffer = NULL;
size_t pos = 0;
while (1) {
buffer = (char *)realloc(buffer, pos + 1024);
if (!buffer) {
fprintf(stderr, "out of memory\n");
return -1;
}
size_t size = fread(buffer + pos, 1, 1024, stdin);
pos += size;
if (size < 1024) {
break;
}
}
buffer[pos] = 0;
int ret = cmd_set_internal(item, buffer);
free(buffer);
return ret;
}
return cmd_set_internal(item, value);
}
static void cmd_scan_printf(FILE *fp, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (fp) {
va_list apc;
va_copy(apc, ap);
vfprintf(fp, fmt, apc);
fflush(fp);
va_end(apc);
}
vprintf(fmt, ap);
fflush(stdout);
va_end(ap);
}
static int cmd_scan(const char *tuner_str, const char *filename)
{
if (hdhomerun_device_set_tuner_from_str(hd, tuner_str) <= 0) {
fprintf(stderr, "invalid tuner number\n");
return -1;
}
char *channelmap;
if (hdhomerun_device_get_tuner_channelmap(hd, &channelmap) <= 0) {
fprintf(stderr, "failed to query channelmap from device\n");
return -1;
}
const char *channelmap_scan_group = hdhomerun_channelmap_get_channelmap_scan_group(channelmap);
if (!channelmap_scan_group) {
fprintf(stderr, "unknown channelmap '%s'\n", channelmap);
return -1;
}
if (hdhomerun_device_channelscan_init(hd, channelmap_scan_group) <= 0) {
fprintf(stderr, "failed to initialize channel scan\n");
return -1;
}
FILE *fp = NULL;
if (filename) {
fp = fopen(filename, "w");
if (!fp) {
fprintf(stderr, "unable to create file: %s\n", filename);
return -1;
}
}
int ret;
while (1) {
struct hdhomerun_channelscan_result_t result;
ret = hdhomerun_device_channelscan_advance(hd, &result);
if (ret <= 0) {
break;
}
cmd_scan_printf(fp, "SCANNING: %lu (%s)\n",
result.frequency, result.channel_str
);
ret = hdhomerun_device_channelscan_detect(hd, &result);
if (ret <= 0) {
break;
}
cmd_scan_printf(fp, "LOCK: %s (ss=%u snq=%u seq=%u)\n",
result.status.lock_str, result.status.signal_strength,
result.status.signal_to_noise_quality, result.status.symbol_error_quality
);
int i;
for (i = 0; i < result.program_count; i++) {
struct hdhomerun_channelscan_program_t *program = &result.programs[i];
cmd_scan_printf(fp, "PROGRAM %s\n", program->program_str);
}
}
if (fp) {
fclose(fp);
}
if (ret < 0) {
fprintf(stderr, "communication error sending request to hdhomerun device\n");
}
return ret;
}
static bool_t cmd_saving = FALSE;
static void cmd_save_abort(int arg)
{
cmd_saving = FALSE;
}
static int cmd_save(const char *tuner_str, const char *filename)
{
if (hdhomerun_device_set_tuner_from_str(hd, tuner_str) <= 0) {
fprintf(stderr, "invalid tuner number\n");
return -1;
}
FILE *fp;
if (strcmp(filename, "null") == 0) {
fp = NULL;
} else if (strcmp(filename, "-") == 0) {
fp = stdout;
} else {
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "unable to create file %s\n", filename);
return -1;
}
}
int ret = hdhomerun_device_stream_start(hd);
if (ret <= 0) {
fprintf(stderr, "unable to start stream\n");
return ret;
}
signal(SIGINT, cmd_save_abort);
signal(SIGPIPE, cmd_save_abort);
struct hdhomerun_video_stats_t stats_old, stats_cur;
hdhomerun_device_get_video_stats(hd, &stats_old);
uint64_t next_progress = getcurrenttime() + 1000;
cmd_saving = TRUE;
while (cmd_saving) {
uint64_t loop_start_time = getcurrenttime();
size_t actual_size;
uint8_t *ptr = hdhomerun_device_stream_recv(hd, VIDEO_DATA_BUFFER_SIZE_1S, &actual_size);
if (!ptr) {
msleep(64);
continue;
}
if (fp) {
if (fwrite(ptr, 1, actual_size, fp) != actual_size) {
fprintf(stderr, "error writing output\n");
return -1;
}
}
if (loop_start_time >= next_progress) {
next_progress += 1000;
if (loop_start_time >= next_progress) {
next_progress = loop_start_time + 1000;
}
hdhomerun_device_get_video_stats(hd, &stats_cur);
if (stats_cur.overflow_error_count > stats_old.overflow_error_count) {
fprintf(stderr, "o");
} else if (stats_cur.network_error_count > stats_old.network_error_count) {
fprintf(stderr, "n");
} else if (stats_cur.transport_error_count > stats_old.transport_error_count) {
fprintf(stderr, "t");
} else if (stats_cur.sequence_error_count > stats_old.sequence_error_count) {
fprintf(stderr, "s");
} else {
fprintf(stderr, ".");
}
stats_old = stats_cur;
fflush(stderr);
}
int32_t delay = 64 - (int32_t)(getcurrenttime() - loop_start_time);
if (delay <= 0) {
continue;
}
msleep(delay);
}
if (fp) {
fclose(fp);
}
hdhomerun_device_stream_stop(hd);
hdhomerun_device_get_video_stats(hd, &stats_cur);
fprintf(stderr, "\n");
fprintf(stderr, "-- Video statistics --\n");
fprintf(stderr, "%u packets received, %u overflow errors, %u network errors, %u transport errors, %u sequence errors\n",
(unsigned int)stats_cur.packet_count,
(unsigned int)stats_cur.overflow_error_count,
(unsigned int)stats_cur.network_error_count,
(unsigned int)stats_cur.transport_error_count,
(unsigned int)stats_cur.sequence_error_count);
return 0;
}
static int cmd_upgrade(const char *filename)
{
FILE *fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "unable to open file %s\n", filename);
return -1;
}
printf("uploading firmware...\n");
if (hdhomerun_device_upgrade(hd, fp) <= 0) {
fprintf(stderr, "error sending upgrade file to hdhomerun device\n");
fclose(fp);
return -1;
}
sleep(2);
printf("upgrading firmware...\n");
sleep(8);
printf("rebooting...\n");
int count = 0;
char *version_str;
while (1) {
if (hdhomerun_device_get_version(hd, &version_str, NULL) >= 0) {
break;
}
count++;
if (count > 30) {
fprintf(stderr, "error finding device after firmware upgrade\n");
fclose(fp);
return -1;
}
sleep(1);
}
printf("upgrade complete - now running firmware %s\n", version_str);
return 0;
}
static int cmd_execute(void)
{
char *ret_value;
char *ret_error;
if (hdhomerun_device_get_var(hd, "/sys/boot", &ret_value, &ret_error) < 0) {
fprintf(stderr, "communication error sending request to hdhomerun device\n");
return -1;
}
if (ret_error) {
printf("%s\n", ret_error);
return 0;
}
char *end = ret_value + strlen(ret_value);
char *pos = ret_value;
while (1) {
if (pos >= end) {
break;
}
char *eol_r = strchr(pos, '\r');
if (!eol_r) {
eol_r = end;
}
char *eol_n = strchr(pos, '\n');
if (!eol_n) {
eol_n = end;
}
char *eol = min(eol_r, eol_n);
char *sep = strchr(pos, ' ');
if (!sep || sep > eol) {
pos = eol + 1;
continue;
}
*sep = 0;
*eol = 0;
char *item = pos;
char *value = sep + 1;
printf("set %s \"%s\"\n", item, value);
cmd_set_internal(item, value);
pos = eol + 1;
}
return 1;
}
static int main_cmd(int argc, char *argv[])
{
if (argc < 1) {
return help();
}
char *cmd = *argv++; argc--;
if (contains(cmd, "key")) {
if (argc < 2) {
return help();
}
uint32_t lockkey = strtoul(argv[0], NULL, 0);
hdhomerun_device_tuner_lockkey_use_value(hd, lockkey);
cmd = argv[1];
argv+=2; argc-=2;
}
if (contains(cmd, "get")) {
if (argc < 1) {
return help();
}
return cmd_get(argv[0]);
}
if (contains(cmd, "set")) {
if (argc < 2) {
return help();
}
return cmd_set(argv[0], argv[1]);
}
if (contains(cmd, "scan")) {
if (argc < 1) {
return help();
}
if (argc < 2) {
return cmd_scan(argv[0], NULL);
} else {
return cmd_scan(argv[0], argv[1]);
}
}
if (contains(cmd, "save")) {
if (argc < 2) {
return help();
}
return cmd_save(argv[0], argv[1]);
}
if (contains(cmd, "upgrade")) {
if (argc < 1) {
return help();
}
return cmd_upgrade(argv[0]);
}
if (contains(cmd, "execute")) {
return cmd_execute();
}
return help();
}
static int main_internal(int argc, char *argv[])
{
#if defined(__WINDOWS__)
/* Initialize network socket support. */
WORD wVersionRequested = MAKEWORD(2, 0);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
#endif
extract_appname(argv[0]);
argv++;
argc--;
if (argc == 0) {
return help();
}
char *id_str = *argv++; argc--;
if (contains(id_str, "help")) {
return help();
}
if (contains(id_str, "discover")) {
if (argc < 1) {
return discover_print(NULL);
} else {
return discover_print(argv[0]);
}
}
/* Device object. */
hd = hdhomerun_device_create_from_str(id_str, NULL);
if (!hd) {
fprintf(stderr, "invalid device id: %s\n", id_str);
return -1;
}
/* Device ID check. */
uint32_t device_id_requested = hdhomerun_device_get_device_id_requested(hd);
if (!hdhomerun_discover_validate_device_id(device_id_requested)) {
fprintf(stderr, "invalid device id: %08lX\n", (unsigned long)device_id_requested);
}
/* Connect to device and check model. */
const char *model = hdhomerun_device_get_model_str(hd);
if (!model) {
fprintf(stderr, "unable to connect to device\n");
hdhomerun_device_destroy(hd);
return -1;
}
/* Command. */
int ret = main_cmd(argc, argv);
/* Cleanup. */
hdhomerun_device_destroy(hd);
/* Complete. */
return ret;
}
int main(int argc, char *argv[])
{
int ret = main_internal(argc, argv);
if (ret <= 0) {
return 1;
}
return 0;
}
|