59 changes: 27 additions & 32 deletions mythtv/external/libhdhomerun/hdhomerun_channelscan.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_channelscan.c
*
* Copyright © 2007-2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2007-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* 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"
Expand Down Expand Up @@ -139,10 +127,10 @@ static void channelscan_extract_name(struct hdhomerun_channelscan_program_t *pro
program->name[length] = 0;
}

static int channelscan_detect_programs(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result, bool_t *pchanged, bool_t *pincomplete)
static int channelscan_detect_programs(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result, bool *pchanged, bool *pincomplete)
{
*pchanged = FALSE;
*pincomplete = FALSE;
*pchanged = false;
*pincomplete = false;

char *streaminfo;
int ret = hdhomerun_device_get_tuner_streaminfo(scan->hd, &streaminfo);
Expand All @@ -164,8 +152,15 @@ static int channelscan_detect_programs(struct hdhomerun_channelscan_t *scan, str

unsigned int transport_stream_id;
if (sscanf(line, "tsid=0x%x", &transport_stream_id) == 1) {
result->transport_stream_id = transport_stream_id;
result->transport_stream_id_detected = TRUE;
result->transport_stream_id = (uint16_t)transport_stream_id;
result->transport_stream_id_detected = true;
continue;
}

unsigned int original_network_id;
if (sscanf(line, "onid=0x%x", &original_network_id) == 1) {
result->original_network_id = (uint16_t)original_network_id;
result->original_network_id_detected = true;
continue;
}

Expand All @@ -187,9 +182,9 @@ static int channelscan_detect_programs(struct hdhomerun_channelscan_t *scan, str
virtual_minor = 0;
}

program.program_number = program_number;
program.virtual_major = virtual_major;
program.virtual_minor = virtual_minor;
program.program_number = (uint16_t)program_number;
program.virtual_major = (uint16_t)virtual_major;
program.virtual_minor = (uint16_t)virtual_minor;

channelscan_extract_name(&program, line);

Expand All @@ -199,28 +194,28 @@ static int channelscan_detect_programs(struct hdhomerun_channelscan_t *scan, str
program.type = HDHOMERUN_CHANNELSCAN_PROGRAM_ENCRYPTED;
} else if (strstr(line, "(no data)")) {
program.type = HDHOMERUN_CHANNELSCAN_PROGRAM_NODATA;
*pincomplete = TRUE;
*pincomplete = true;
} else {
program.type = HDHOMERUN_CHANNELSCAN_PROGRAM_NORMAL;
if ((program.virtual_major == 0) || (program.name[0] == 0)) {
*pincomplete = TRUE;
*pincomplete = true;
}
}

if (memcmp(&result->programs[program_count], &program, sizeof(program)) != 0) {
memcpy(&result->programs[program_count], &program, sizeof(program));
*pchanged = TRUE;
*pchanged = true;
}

program_count++;
}

if (program_count == 0) {
*pincomplete = TRUE;
*pincomplete = true;
}
if (result->program_count != program_count) {
result->program_count = program_count;
*pchanged = TRUE;
*pchanged = true;
}

return 1;
Expand Down Expand Up @@ -287,7 +282,7 @@ int channelscan_detect(struct hdhomerun_channelscan_t *scan, struct hdhomerun_ch
uint64_t complete_time = getcurrenttime() + 1000;

while (1) {
bool_t changed, incomplete;
bool changed, incomplete;
ret = channelscan_detect_programs(scan, result, &changed, &incomplete);
if (ret <= 0) {
return ret;
Expand Down Expand Up @@ -348,5 +343,5 @@ uint8_t channelscan_get_progress(struct hdhomerun_channelscan_t *scan)
}
}

return scan->scanned_channels * 100 / (scan->scanned_channels + channels_remaining);
return (uint8_t) (scan->scanned_channels * 100 / (scan->scanned_channels + channels_remaining));
}
32 changes: 10 additions & 22 deletions mythtv/external/libhdhomerun/hdhomerun_channelscan.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_channelscan.h
*
* Copyright © 2007-2008 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2007-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef __cplusplus
Expand All @@ -41,12 +29,12 @@ extern "C" {

struct hdhomerun_channelscan_t;

extern LIBTYPE struct hdhomerun_channelscan_t *channelscan_create(struct hdhomerun_device_t *hd, const char *channelmap);
extern LIBTYPE void channelscan_destroy(struct hdhomerun_channelscan_t *scan);
extern LIBHDHOMERUN_API struct hdhomerun_channelscan_t *channelscan_create(struct hdhomerun_device_t *hd, const char *channelmap);
extern LIBHDHOMERUN_API void channelscan_destroy(struct hdhomerun_channelscan_t *scan);

extern LIBTYPE int channelscan_advance(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result);
extern LIBTYPE int channelscan_detect(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result);
extern LIBTYPE uint8_t channelscan_get_progress(struct hdhomerun_channelscan_t *scan);
extern LIBHDHOMERUN_API int channelscan_advance(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result);
extern LIBHDHOMERUN_API int channelscan_detect(struct hdhomerun_channelscan_t *scan, struct hdhomerun_channelscan_result_t *result);
extern LIBHDHOMERUN_API uint8_t channelscan_get_progress(struct hdhomerun_channelscan_t *scan);

#ifdef __cplusplus
}
Expand Down
67 changes: 25 additions & 42 deletions mythtv/external/libhdhomerun/hdhomerun_config.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
/*
* hdhomerun_config.c
*
* Copyright © 2006-2008 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2017 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* 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"

/*
* 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;
Expand Down Expand Up @@ -72,23 +50,23 @@ static void extract_appname(const char *argv0)
appname = argv0;
}

static bool_t contains(const char *arg, const char *cmpstr)
static bool contains(const char *arg, const char *cmpstr)
{
if (strcmp(arg, cmpstr) == 0) {
return TRUE;
return true;
}

if (*arg++ != '-') {
return FALSE;
return false;
}
if (*arg++ != '-') {
return FALSE;
return false;
}
if (strcmp(arg, cmpstr) == 0) {
return TRUE;
return true;
}

return FALSE;
return false;
}

static uint32_t parse_ip_addr(const char *str)
Expand All @@ -113,7 +91,7 @@ static int discover_print(char *target_ip_str)
}

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);
int count = hdhomerun_discover_find_devices_custom_v2(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;
Expand Down Expand Up @@ -202,17 +180,17 @@ static int cmd_set(const char *item, const char *value)
return cmd_set_internal(item, value);
}

static volatile sig_atomic_t sigabort_flag = FALSE;
static volatile sig_atomic_t siginfo_flag = FALSE;
static volatile sig_atomic_t sigabort_flag = false;
static volatile sig_atomic_t siginfo_flag = false;

static void sigabort_handler(int arg)
{
sigabort_flag = TRUE;
sigabort_flag = true;
}

static void siginfo_handler(int arg)
{
siginfo_flag = TRUE;
siginfo_flag = true;
}

static void register_signal_handlers(sig_t sigpipe_handler, sig_t sigint_handler, sig_t siginfo_handler)
Expand Down Expand Up @@ -323,6 +301,9 @@ static int cmd_scan(const char *tuner_str, const char *filename)
if (result.transport_stream_id_detected) {
cmd_scan_printf(fp, "TSID: 0x%04X\n", result.transport_stream_id);
}
if (result.original_network_id_detected) {
cmd_scan_printf(fp, "ONID: 0x%04X\n", result.original_network_id);
}

int i;
for (i = 0; i < result.program_count; i++) {
Expand Down Expand Up @@ -398,7 +379,7 @@ static int cmd_save(const char *tuner_str, const char *filename)
if (siginfo_flag) {
fprintf(stderr, "\n");
cmd_save_print_stats();
siginfo_flag = FALSE;
siginfo_flag = false;
}

size_t actual_size;
Expand All @@ -422,7 +403,7 @@ static int cmd_save(const char *tuner_str, const char *filename)
}

/* Windows - indicate activity to suppress auto sleep mode. */
#if defined(__WINDOWS__)
#if defined(_WIN32)
SetThreadExecutionState(ES_SYSTEM_REQUIRED);
#endif

Expand Down Expand Up @@ -579,7 +560,7 @@ static int main_cmd(int argc, char *argv[])
if (argc < 2) {
return help();
}
uint32_t lockkey = strtoul(argv[0], NULL, 0);
uint32_t lockkey = (uint32_t)strtoul(argv[0], NULL, 0);
hdhomerun_device_tuner_lockkey_use_value(hd, lockkey);

cmd = argv[1];
Expand Down Expand Up @@ -634,7 +615,9 @@ static int main_cmd(int argc, char *argv[])

static int main_internal(int argc, char *argv[])
{
#if defined(__WINDOWS__)
#if defined(_WIN32)
/* Configure console for UTF-8. */
SetConsoleOutputCP(CP_UTF8);
/* Initialize network socket support. */
WORD wVersionRequested = MAKEWORD(2, 0);
WSADATA wsaData;
Expand Down
84 changes: 37 additions & 47 deletions mythtv/external/libhdhomerun/hdhomerun_control.c
Original file line number Diff line number Diff line change
@@ -1,61 +1,49 @@
/*
* hdhomerun_control.c
*
* Copyright © 2006-2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2016 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* 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"

#define HDHOMERUN_CONTROL_CONNECT_TIMEOUT 2500
#define HDHOMERUN_CONTROL_SEND_TIMEOUT 2500
#define HDHOMERUN_CONTROL_RECV_TIMEOUT 2500
#define HDHOMERUN_CONTROL_UPGRADE_TIMEOUT 20000
#define HDHOMERUN_CONTROL_UPGRADE_TIMEOUT 30000

struct hdhomerun_control_sock_t {
uint32_t desired_device_id;
uint32_t desired_device_ip;
uint32_t actual_device_id;
uint32_t actual_device_ip;
hdhomerun_sock_t sock;
struct hdhomerun_sock_t *sock;
struct hdhomerun_debug_t *dbg;
struct hdhomerun_pkt_t tx_pkt;
struct hdhomerun_pkt_t rx_pkt;
};

static void hdhomerun_control_close_sock(struct hdhomerun_control_sock_t *cs)
{
if (cs->sock == HDHOMERUN_SOCK_INVALID) {
if (!cs->sock) {
return;
}

hdhomerun_sock_destroy(cs->sock);
cs->sock = HDHOMERUN_SOCK_INVALID;
cs->sock = NULL;
}

void hdhomerun_control_set_device(struct hdhomerun_control_sock_t *cs, uint32_t device_id, uint32_t device_ip)
Expand All @@ -77,7 +65,6 @@ struct hdhomerun_control_sock_t *hdhomerun_control_create(uint32_t device_id, ui
}

cs->dbg = dbg;
cs->sock = HDHOMERUN_SOCK_INVALID;
hdhomerun_control_set_device(cs, device_id, device_ip);

return cs;
Expand All @@ -89,46 +76,46 @@ void hdhomerun_control_destroy(struct hdhomerun_control_sock_t *cs)
free(cs);
}

static bool_t hdhomerun_control_connect_sock(struct hdhomerun_control_sock_t *cs)
static bool hdhomerun_control_connect_sock(struct hdhomerun_control_sock_t *cs)
{
if (cs->sock != HDHOMERUN_SOCK_INVALID) {
return TRUE;
if (cs->sock) {
return true;
}

if ((cs->desired_device_id == 0) && (cs->desired_device_ip == 0)) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_connect_sock: no device specified\n");
return FALSE;
return false;
}
if (hdhomerun_discover_is_ip_multicast(cs->desired_device_ip)) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_connect_sock: cannot use multicast ip address for device operations\n");
return FALSE;
return false;
}

/* Find device. */
struct hdhomerun_discover_device_t result;
if (hdhomerun_discover_find_devices_custom(cs->desired_device_ip, HDHOMERUN_DEVICE_TYPE_WILDCARD, cs->desired_device_id, &result, 1) <= 0) {
if (hdhomerun_discover_find_devices_custom_v2(cs->desired_device_ip, HDHOMERUN_DEVICE_TYPE_WILDCARD, cs->desired_device_id, &result, 1) <= 0) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_connect_sock: device not found\n");
return FALSE;
return false;
}
cs->actual_device_ip = result.ip_addr;
cs->actual_device_id = result.device_id;

/* Create socket. */
cs->sock = hdhomerun_sock_create_tcp();
if (cs->sock == HDHOMERUN_SOCK_INVALID) {
if (!cs->sock) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_connect_sock: failed to create socket (%d)\n", hdhomerun_sock_getlasterror());
return FALSE;
return false;
}

/* Initiate connection. */
if (!hdhomerun_sock_connect(cs->sock, cs->actual_device_ip, HDHOMERUN_CONTROL_TCP_PORT, HDHOMERUN_CONTROL_CONNECT_TIMEOUT)) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_connect_sock: failed to connect (%d)\n", hdhomerun_sock_getlasterror());
hdhomerun_control_close_sock(cs);
return FALSE;
return false;
}

/* Success. */
return TRUE;
return true;
}

uint32_t hdhomerun_control_get_device_id(struct hdhomerun_control_sock_t *cs)
Expand Down Expand Up @@ -177,18 +164,18 @@ uint32_t hdhomerun_control_get_local_addr(struct hdhomerun_control_sock_t *cs)
return addr;
}

static bool_t hdhomerun_control_send_sock(struct hdhomerun_control_sock_t *cs, struct hdhomerun_pkt_t *tx_pkt)
static bool hdhomerun_control_send_sock(struct hdhomerun_control_sock_t *cs, struct hdhomerun_pkt_t *tx_pkt)
{
if (!hdhomerun_sock_send(cs->sock, tx_pkt->start, tx_pkt->end - tx_pkt->start, HDHOMERUN_CONTROL_SEND_TIMEOUT)) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_send_sock: send failed (%d)\n", hdhomerun_sock_getlasterror());
hdhomerun_control_close_sock(cs);
return FALSE;
return false;
}

return TRUE;
return true;
}

static bool_t hdhomerun_control_recv_sock(struct hdhomerun_control_sock_t *cs, struct hdhomerun_pkt_t *rx_pkt, uint16_t *ptype, uint64_t recv_timeout)
static bool hdhomerun_control_recv_sock(struct hdhomerun_control_sock_t *cs, struct hdhomerun_pkt_t *rx_pkt, uint16_t *ptype, uint64_t recv_timeout)
{
uint64_t stop_time = getcurrenttime() + recv_timeout;
hdhomerun_pkt_reset(rx_pkt);
Expand All @@ -198,14 +185,14 @@ static bool_t hdhomerun_control_recv_sock(struct hdhomerun_control_sock_t *cs, s
if (current_time >= stop_time) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_recv_sock: timeout\n");
hdhomerun_control_close_sock(cs);
return FALSE;
return false;
}

size_t length = rx_pkt->limit - rx_pkt->end;
if (!hdhomerun_sock_recv(cs->sock, rx_pkt->end, &length, stop_time - current_time)) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_recv_sock: recv failed (%d)\n", hdhomerun_sock_getlasterror());
hdhomerun_control_close_sock(cs);
return FALSE;
return false;
}

rx_pkt->end += length;
Expand All @@ -214,10 +201,10 @@ static bool_t hdhomerun_control_recv_sock(struct hdhomerun_control_sock_t *cs, s
if (ret < 0) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_recv_sock: frame error\n");
hdhomerun_control_close_sock(cs);
return FALSE;
return false;
}
if (ret > 0) {
return TRUE;
return true;
}
}
}
Expand All @@ -228,7 +215,7 @@ static int hdhomerun_control_send_recv_internal(struct hdhomerun_control_sock_t

int i;
for (i = 0; i < 2; i++) {
if (cs->sock == HDHOMERUN_SOCK_INVALID) {
if (!cs->sock) {
if (!hdhomerun_control_connect_sock(cs)) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_send_recv: connect failed\n");
return -1;
Expand Down Expand Up @@ -272,7 +259,7 @@ static int hdhomerun_control_get_set(struct hdhomerun_control_sock_t *cs, const
/* Request. */
hdhomerun_pkt_reset(tx_pkt);

int name_len = (int)strlen(name) + 1;
size_t name_len = strlen(name) + 1;
if (tx_pkt->end + 3 + name_len > tx_pkt->limit) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_get_set: request too long\n");
return -1;
Expand All @@ -282,7 +269,7 @@ static int hdhomerun_control_get_set(struct hdhomerun_control_sock_t *cs, const
hdhomerun_pkt_write_mem(tx_pkt, (const void *)name, name_len);

if (value) {
int value_len = (int)strlen(value) + 1;
size_t value_len = strlen(value) + 1;
if (tx_pkt->end + 3 + value_len > tx_pkt->limit) {
hdhomerun_debug_printf(cs->dbg, "hdhomerun_control_get_set: request too long\n");
return -1;
Expand Down Expand Up @@ -340,6 +327,9 @@ static int hdhomerun_control_get_set(struct hdhomerun_control_sock_t *cs, const
}

return 0;

default:
break;
}

rx_pkt->pos = next;
Expand Down Expand Up @@ -368,7 +358,7 @@ int hdhomerun_control_upgrade(struct hdhomerun_control_sock_t *cs, FILE *upgrade
{
struct hdhomerun_pkt_t *tx_pkt = &cs->tx_pkt;
struct hdhomerun_pkt_t *rx_pkt = &cs->rx_pkt;
bool_t upload_delay = FALSE;
bool upload_delay = false;
uint32_t sequence = 0;

/* Special case detection. */
Expand All @@ -380,8 +370,8 @@ int hdhomerun_control_upgrade(struct hdhomerun_control_sock_t *cs, FILE *upgrade

/* Upload. */
while (1) {
uint8_t data[256];
size_t length = fread(data, 1, 256, upgrade_file);
uint8_t data[1024];
size_t length = fread(data, 1, 1024, upgrade_file);
if (length == 0) {
break;
}
Expand Down
48 changes: 18 additions & 30 deletions mythtv/external/libhdhomerun/hdhomerun_control.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_control.h
*
* Copyright © 2006 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef __cplusplus
extern "C" {
Expand All @@ -49,20 +37,20 @@ struct hdhomerun_control_sock_t;
*
* When no longer needed, the socket should be destroyed by calling hdhomerun_control_destroy.
*/
extern LIBTYPE struct hdhomerun_control_sock_t *hdhomerun_control_create(uint32_t device_id, uint32_t device_ip, struct hdhomerun_debug_t *dbg);
extern LIBTYPE void hdhomerun_control_destroy(struct hdhomerun_control_sock_t *cs);
extern LIBHDHOMERUN_API struct hdhomerun_control_sock_t *hdhomerun_control_create(uint32_t device_id, uint32_t device_ip, struct hdhomerun_debug_t *dbg);
extern LIBHDHOMERUN_API void hdhomerun_control_destroy(struct hdhomerun_control_sock_t *cs);

/*
* Get the actual device id or ip of the device.
*
* Returns 0 if the device id cannot be determined.
*/
extern LIBTYPE uint32_t hdhomerun_control_get_device_id(struct hdhomerun_control_sock_t *cs);
extern LIBTYPE uint32_t hdhomerun_control_get_device_ip(struct hdhomerun_control_sock_t *cs);
extern LIBTYPE uint32_t hdhomerun_control_get_device_id_requested(struct hdhomerun_control_sock_t *cs);
extern LIBTYPE uint32_t hdhomerun_control_get_device_ip_requested(struct hdhomerun_control_sock_t *cs);
extern LIBHDHOMERUN_API uint32_t hdhomerun_control_get_device_id(struct hdhomerun_control_sock_t *cs);
extern LIBHDHOMERUN_API uint32_t hdhomerun_control_get_device_ip(struct hdhomerun_control_sock_t *cs);
extern LIBHDHOMERUN_API uint32_t hdhomerun_control_get_device_id_requested(struct hdhomerun_control_sock_t *cs);
extern LIBHDHOMERUN_API uint32_t hdhomerun_control_get_device_ip_requested(struct hdhomerun_control_sock_t *cs);

extern LIBTYPE void hdhomerun_control_set_device(struct hdhomerun_control_sock_t *cs, uint32_t device_id, uint32_t device_ip);
extern LIBHDHOMERUN_API void hdhomerun_control_set_device(struct hdhomerun_control_sock_t *cs, uint32_t device_id, uint32_t device_ip);

/*
* Get the local machine IP address used when communicating with the device.
Expand All @@ -71,12 +59,12 @@ extern LIBTYPE void hdhomerun_control_set_device(struct hdhomerun_control_sock_t
*
* Returns 32-bit IP address with native endianness, or 0 on error.
*/
extern LIBTYPE uint32_t hdhomerun_control_get_local_addr(struct hdhomerun_control_sock_t *cs);
extern LIBHDHOMERUN_API uint32_t hdhomerun_control_get_local_addr(struct hdhomerun_control_sock_t *cs);

/*
* Low-level communication.
*/
extern LIBTYPE int hdhomerun_control_send_recv(struct hdhomerun_control_sock_t *cs, struct hdhomerun_pkt_t *tx_pkt, struct hdhomerun_pkt_t *rx_pkt, uint16_t type);
extern LIBHDHOMERUN_API int hdhomerun_control_send_recv(struct hdhomerun_control_sock_t *cs, struct hdhomerun_pkt_t *tx_pkt, struct hdhomerun_pkt_t *rx_pkt, uint16_t type);

/*
* Get/set a control variable on the device.
Expand All @@ -95,9 +83,9 @@ extern LIBTYPE int hdhomerun_control_send_recv(struct hdhomerun_control_sock_t *
* Returns 0 if the operation was rejected (pvalue NULL, perror set).
* Returns -1 if a communication error occurs.
*/
extern LIBTYPE int hdhomerun_control_get(struct hdhomerun_control_sock_t *cs, const char *name, char **pvalue, char **perror);
extern LIBTYPE int hdhomerun_control_set(struct hdhomerun_control_sock_t *cs, const char *name, const char *value, char **pvalue, char **perror);
extern LIBTYPE int hdhomerun_control_set_with_lockkey(struct hdhomerun_control_sock_t *cs, const char *name, const char *value, uint32_t lockkey, char **pvalue, char **perror);
extern LIBHDHOMERUN_API int hdhomerun_control_get(struct hdhomerun_control_sock_t *cs, const char *name, char **pvalue, char **perror);
extern LIBHDHOMERUN_API int hdhomerun_control_set(struct hdhomerun_control_sock_t *cs, const char *name, const char *value, char **pvalue, char **perror);
extern LIBHDHOMERUN_API int hdhomerun_control_set_with_lockkey(struct hdhomerun_control_sock_t *cs, const char *name, const char *value, uint32_t lockkey, char **pvalue, char **perror);

/*
* Upload new firmware to the device.
Expand All @@ -108,7 +96,7 @@ extern LIBTYPE int hdhomerun_control_set_with_lockkey(struct hdhomerun_control_s
* Returns 0 if the upload was rejected.
* Returns -1 if an error occurs.
*/
extern LIBTYPE int hdhomerun_control_upgrade(struct hdhomerun_control_sock_t *cs, FILE *upgrade_file);
extern LIBHDHOMERUN_API int hdhomerun_control_upgrade(struct hdhomerun_control_sock_t *cs, FILE *upgrade_file);

#ifdef __cplusplus
}
Expand Down
199 changes: 99 additions & 100 deletions mythtv/external/libhdhomerun/hdhomerun_debug.c

Large diffs are not rendered by default.

44 changes: 16 additions & 28 deletions mythtv/external/libhdhomerun/hdhomerun_debug.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_debug.h
*
* Copyright © 2006 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2007-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
Expand All @@ -44,20 +32,20 @@ extern "C" {

struct hdhomerun_debug_t;

extern LIBTYPE struct hdhomerun_debug_t *hdhomerun_debug_create(void);
extern LIBTYPE void hdhomerun_debug_destroy(struct hdhomerun_debug_t *dbg);
extern LIBHDHOMERUN_API struct hdhomerun_debug_t *hdhomerun_debug_create(void);
extern LIBHDHOMERUN_API void hdhomerun_debug_destroy(struct hdhomerun_debug_t *dbg);

extern LIBTYPE void hdhomerun_debug_set_prefix(struct hdhomerun_debug_t *dbg, const char *prefix);
extern LIBTYPE void hdhomerun_debug_set_filename(struct hdhomerun_debug_t *dbg, const char *filename);
extern LIBTYPE void hdhomerun_debug_enable(struct hdhomerun_debug_t *dbg);
extern LIBTYPE void hdhomerun_debug_disable(struct hdhomerun_debug_t *dbg);
extern LIBTYPE bool_t hdhomerun_debug_enabled(struct hdhomerun_debug_t *dbg);
extern LIBHDHOMERUN_API void hdhomerun_debug_set_prefix(struct hdhomerun_debug_t *dbg, const char *prefix);
extern LIBHDHOMERUN_API void hdhomerun_debug_set_filename(struct hdhomerun_debug_t *dbg, const char *filename);
extern LIBHDHOMERUN_API void hdhomerun_debug_enable(struct hdhomerun_debug_t *dbg);
extern LIBHDHOMERUN_API void hdhomerun_debug_disable(struct hdhomerun_debug_t *dbg);
extern LIBHDHOMERUN_API bool hdhomerun_debug_enabled(struct hdhomerun_debug_t *dbg);

extern LIBTYPE void hdhomerun_debug_flush(struct hdhomerun_debug_t *dbg, uint64_t timeout);
extern LIBTYPE void hdhomerun_debug_close(struct hdhomerun_debug_t *dbg, uint64_t timeout);
extern LIBHDHOMERUN_API void hdhomerun_debug_flush(struct hdhomerun_debug_t *dbg, uint64_t timeout);
extern LIBHDHOMERUN_API void hdhomerun_debug_close(struct hdhomerun_debug_t *dbg, uint64_t timeout);

extern LIBTYPE void hdhomerun_debug_printf(struct hdhomerun_debug_t *dbg, const char *fmt, ...);
extern LIBTYPE void hdhomerun_debug_vprintf(struct hdhomerun_debug_t *dbg, const char *fmt, va_list args);
extern LIBHDHOMERUN_API void hdhomerun_debug_printf(struct hdhomerun_debug_t *dbg, const char *fmt, ...);
extern LIBHDHOMERUN_API void hdhomerun_debug_vprintf(struct hdhomerun_debug_t *dbg, const char *fmt, va_list args);

#ifdef __cplusplus
}
Expand Down
337 changes: 153 additions & 184 deletions mythtv/external/libhdhomerun/hdhomerun_device.c

Large diffs are not rendered by default.

151 changes: 70 additions & 81 deletions mythtv/external/libhdhomerun/hdhomerun_device.h

Large diffs are not rendered by default.

243 changes: 170 additions & 73 deletions mythtv/external/libhdhomerun/hdhomerun_device_selector.c

Large diffs are not rendered by default.

44 changes: 17 additions & 27 deletions mythtv/external/libhdhomerun/hdhomerun_device_selector.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_device_selector.h
*
* Copyright © 2009 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2009-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef __cplusplus
Expand All @@ -39,33 +27,35 @@ extern "C" {
* All tuners registered with a specific device selector instance must have the same signal source.
* The dbg parameter may be null.
*/
extern LIBTYPE struct hdhomerun_device_selector_t *hdhomerun_device_selector_create(struct hdhomerun_debug_t *dbg);
extern LIBTYPE void hdhomerun_device_selector_destroy(struct hdhomerun_device_selector_t *hds, bool_t destroy_devices);
extern LIBHDHOMERUN_API struct hdhomerun_device_selector_t *hdhomerun_device_selector_create(struct hdhomerun_debug_t *dbg);
extern LIBHDHOMERUN_API void hdhomerun_device_selector_destroy(struct hdhomerun_device_selector_t *hds, bool destroy_devices);

/*
* Get the number of devices in the list.
*/
extern LIBTYPE int hdhomerun_device_selector_get_device_count(struct hdhomerun_device_selector_t *hds);
extern LIBHDHOMERUN_API int hdhomerun_device_selector_get_device_count(struct hdhomerun_device_selector_t *hds);

/*
* Populate device selector with devices from given source.
* Returns the number of devices populated.
*/
extern LIBTYPE int hdhomerun_device_selector_load_from_file(struct hdhomerun_device_selector_t *hds, char *filename);
#if defined(__WINDOWS__)
extern LIBTYPE int hdhomerun_device_selector_load_from_windows_registry(struct hdhomerun_device_selector_t *hds, wchar_t *wsource);
extern LIBHDHOMERUN_API int hdhomerun_device_selector_load_from_str(struct hdhomerun_device_selector_t *hds, char *device_str);
extern LIBHDHOMERUN_API int hdhomerun_device_selector_load_from_file(struct hdhomerun_device_selector_t *hds, char *filename);

#if defined(_WIN32) && !defined(_WINRT)
extern LIBHDHOMERUN_API int hdhomerun_device_selector_load_from_windows_registry(struct hdhomerun_device_selector_t *hds, wchar_t *wsource);
#endif

/*
* Add/remove a device from the selector list.
*/
extern LIBTYPE void hdhomerun_device_selector_add_device(struct hdhomerun_device_selector_t *hds, struct hdhomerun_device_t *hd);
extern LIBTYPE void hdhomerun_device_selector_remove_device(struct hdhomerun_device_selector_t *hds, struct hdhomerun_device_t *hd);
extern LIBHDHOMERUN_API void hdhomerun_device_selector_add_device(struct hdhomerun_device_selector_t *hds, struct hdhomerun_device_t *hd);
extern LIBHDHOMERUN_API void hdhomerun_device_selector_remove_device(struct hdhomerun_device_selector_t *hds, struct hdhomerun_device_t *hd);

/*
* Find a device in the selector list.
*/
extern LIBTYPE struct hdhomerun_device_t *hdhomerun_device_selector_find_device(struct hdhomerun_device_selector_t *hds, uint32_t device_id, unsigned int tuner_index);
extern LIBHDHOMERUN_API struct hdhomerun_device_t *hdhomerun_device_selector_find_device(struct hdhomerun_device_selector_t *hds, uint32_t device_id, unsigned int tuner_index);

/*
* Select and lock an available device.
Expand All @@ -90,7 +80,7 @@ extern LIBTYPE struct hdhomerun_device_t *hdhomerun_device_selector_find_device(
* to refresh the lock. If this function succeeds then the same device can be used. If this fucntion fails
* then call hdhomerun_device_selector_choose_and_lock() to choose and lock an available tuner.
*/
extern LIBTYPE struct hdhomerun_device_t *hdhomerun_device_selector_choose_and_lock(struct hdhomerun_device_selector_t *hds, struct hdhomerun_device_t *prefered);
extern LIBHDHOMERUN_API struct hdhomerun_device_t *hdhomerun_device_selector_choose_and_lock(struct hdhomerun_device_selector_t *hds, struct hdhomerun_device_t *prefered);

#ifdef __cplusplus
}
Expand Down
180 changes: 116 additions & 64 deletions mythtv/external/libhdhomerun/hdhomerun_discover.c
Original file line number Diff line number Diff line change
@@ -1,91 +1,80 @@
/*
* hdhomerun_discover.c
*
* Copyright © 2006-2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2017 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* 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"

#define HDHOMERUN_DISOCVER_MAX_SOCK_COUNT 16
#define HDHOMERUN_DISCOVER_MAX_SOCK_COUNT 16

struct hdhomerun_discover_sock_t {
hdhomerun_sock_t sock;
bool_t detected;
struct hdhomerun_sock_t *sock;
bool detected;
uint32_t local_ip;
uint32_t subnet_mask;
};

struct hdhomerun_discover_t {
struct hdhomerun_discover_sock_t socks[HDHOMERUN_DISOCVER_MAX_SOCK_COUNT];
struct hdhomerun_discover_sock_t socks[HDHOMERUN_DISCOVER_MAX_SOCK_COUNT];
unsigned int sock_count;
struct hdhomerun_pkt_t tx_pkt;
struct hdhomerun_pkt_t rx_pkt;
struct hdhomerun_debug_t *dbg;
};

static bool_t hdhomerun_discover_sock_add(struct hdhomerun_discover_t *ds, uint32_t local_ip, uint32_t subnet_mask)
static bool hdhomerun_discover_sock_add(struct hdhomerun_discover_t *ds, uint32_t local_ip, uint32_t subnet_mask)
{
unsigned int i;
for (i = 1; i < ds->sock_count; i++) {
struct hdhomerun_discover_sock_t *dss = &ds->socks[i];

if ((dss->local_ip == local_ip) && (dss->subnet_mask == subnet_mask)) {
dss->detected = TRUE;
return TRUE;
dss->detected = true;
return true;
}
}

if (ds->sock_count >= HDHOMERUN_DISOCVER_MAX_SOCK_COUNT) {
return FALSE;
if (ds->sock_count >= HDHOMERUN_DISCOVER_MAX_SOCK_COUNT) {
return false;
}

/* Create socket. */
hdhomerun_sock_t sock = hdhomerun_sock_create_udp();
if (sock == HDHOMERUN_SOCK_INVALID) {
return FALSE;
struct hdhomerun_sock_t *sock = hdhomerun_sock_create_udp();
if (!sock) {
hdhomerun_debug_printf(ds->dbg, "discover: failed to allocate socket (%d)\n", hdhomerun_sock_getlasterror());
return false;
}

/* Bind socket. */
if (!hdhomerun_sock_bind(sock, local_ip, 0, FALSE)) {
if (!hdhomerun_sock_bind(sock, local_ip, 0, false)) {
hdhomerun_debug_printf(ds->dbg, "discover: failed to bind to %u.%u.%u.%u:0\n", (unsigned int)(local_ip >> 24) & 0xFF, (unsigned int)(local_ip >> 16) & 0xFF, (unsigned int)(local_ip >> 8) & 0xFF, (unsigned int)(local_ip >> 0) & 0xFF);
hdhomerun_sock_destroy(sock);
return FALSE;
return false;
}

/* Write sock entry. */
struct hdhomerun_discover_sock_t *dss = &ds->socks[ds->sock_count++];
dss->sock = sock;
dss->detected = TRUE;
dss->detected = true;
dss->local_ip = local_ip;
dss->subnet_mask = subnet_mask;

return TRUE;
return true;
}

struct hdhomerun_discover_t *hdhomerun_discover_create(struct hdhomerun_debug_t *dbg)
Expand Down Expand Up @@ -123,18 +112,18 @@ static void hdhomerun_discover_sock_detect(struct hdhomerun_discover_t *ds)
unsigned int i;
for (i = 1; i < ds->sock_count; i++) {
struct hdhomerun_discover_sock_t *dss = &ds->socks[i];
dss->detected = FALSE;
dss->detected = false;
}

struct hdhomerun_local_ip_info_t ip_info_list[HDHOMERUN_DISOCVER_MAX_SOCK_COUNT];
int count = hdhomerun_local_ip_info(ip_info_list, HDHOMERUN_DISOCVER_MAX_SOCK_COUNT);
struct hdhomerun_local_ip_info_t ip_info_list[HDHOMERUN_DISCOVER_MAX_SOCK_COUNT];
int count = hdhomerun_local_ip_info(ip_info_list, HDHOMERUN_DISCOVER_MAX_SOCK_COUNT);
if (count < 0) {
hdhomerun_debug_printf(ds->dbg, "discover: hdhomerun_local_ip_info returned error\n");
count = 0;
}
if (count > HDHOMERUN_DISOCVER_MAX_SOCK_COUNT) {
if (count > HDHOMERUN_DISCOVER_MAX_SOCK_COUNT) {
hdhomerun_debug_printf(ds->dbg, "discover: too many local IP addresses\n");
count = HDHOMERUN_DISOCVER_MAX_SOCK_COUNT;
count = HDHOMERUN_DISCOVER_MAX_SOCK_COUNT;
}

int index;
Expand All @@ -160,10 +149,10 @@ static void hdhomerun_discover_sock_detect(struct hdhomerun_discover_t *ds)
count++;
}

ds->sock_count = count;
ds->sock_count = (unsigned int)count;
}

static bool_t hdhomerun_discover_send_internal(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_sock_t *dss, uint32_t target_ip, uint32_t device_type, uint32_t device_id)
static bool hdhomerun_discover_send_internal(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_sock_t *dss, uint32_t target_ip, uint32_t device_type, uint32_t device_id)
{
struct hdhomerun_pkt_t *tx_pkt = &ds->tx_pkt;
hdhomerun_pkt_reset(tx_pkt);
Expand All @@ -179,9 +168,9 @@ static bool_t hdhomerun_discover_send_internal(struct hdhomerun_discover_t *ds,
return hdhomerun_sock_sendto(dss->sock, target_ip, HDHOMERUN_DISCOVER_UDP_PORT, tx_pkt->start, tx_pkt->end - tx_pkt->start, 0);
}

static bool_t hdhomerun_discover_send_wildcard_ip(struct hdhomerun_discover_t *ds, uint32_t device_type, uint32_t device_id)
static bool hdhomerun_discover_send_wildcard_ip(struct hdhomerun_discover_t *ds, uint32_t device_type, uint32_t device_id)
{
bool_t result = FALSE;
bool result = false;

/*
* Send subnet broadcast using each local ip socket.
Expand All @@ -205,9 +194,9 @@ static bool_t hdhomerun_discover_send_wildcard_ip(struct hdhomerun_discover_t *d
return result;
}

static bool_t hdhomerun_discover_send_target_ip(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id)
static bool hdhomerun_discover_send_target_ip(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id)
{
bool_t result = FALSE;
bool result = false;

/*
* Send targeted packet from any local ip that is in the same subnet.
Expand All @@ -216,6 +205,9 @@ static bool_t hdhomerun_discover_send_target_ip(struct hdhomerun_discover_t *ds,
unsigned int i;
for (i = 1; i < ds->sock_count; i++) {
struct hdhomerun_discover_sock_t *dss = &ds->socks[i];
if (dss->subnet_mask == 0) {
continue;
}
if ((target_ip & dss->subnet_mask) != (dss->local_ip & dss->subnet_mask)) {
continue;
}
Expand All @@ -234,7 +226,7 @@ static bool_t hdhomerun_discover_send_target_ip(struct hdhomerun_discover_t *ds,
return result;
}

static bool_t hdhomerun_discover_send(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id)
static bool hdhomerun_discover_send(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id)
{
if (target_ip == 0) {
return hdhomerun_discover_send_wildcard_ip(ds, device_type, device_id);
Expand All @@ -243,32 +235,57 @@ static bool_t hdhomerun_discover_send(struct hdhomerun_discover_t *ds, uint32_t
}
}

static bool_t hdhomerun_discover_recv_internal(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_sock_t *dss, struct hdhomerun_discover_device_t *result)
static bool hdhomerun_discover_is_legacy(uint32_t device_id)
{
switch (device_id >> 20) {
case 0x100: /* TECH-US/TECH3-US */
return (device_id < 0x10040000);

case 0x120: /* TECH3-EU */
return (device_id < 0x12030000);

case 0x101: /* HDHR-US */
case 0x102: /* HDHR-T1-US */
case 0x103: /* HDHR3-US */
case 0x111: /* HDHR3-DT */
case 0x121: /* HDHR-EU */
case 0x122: /* HDHR3-EU */
return true;

default:
return false;
}
}

static bool hdhomerun_discover_recv_internal(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_sock_t *dss, struct hdhomerun_discover_device_t *result)
{
static char hdhomerun_discover_recv_base64_encode_table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
struct hdhomerun_pkt_t *rx_pkt = &ds->rx_pkt;
hdhomerun_pkt_reset(rx_pkt);

uint32_t remote_addr;
uint16_t remote_port;
size_t length = rx_pkt->limit - rx_pkt->end;
if (!hdhomerun_sock_recvfrom(dss->sock, &remote_addr, &remote_port, rx_pkt->end, &length, 0)) {
return FALSE;
return false;
}

rx_pkt->end += length;

uint16_t type;
if (hdhomerun_pkt_open_frame(rx_pkt, &type) <= 0) {
return FALSE;
return false;
}
if (type != HDHOMERUN_TYPE_DISCOVER_RPY) {
return FALSE;
return false;
}

memset(result, 0, sizeof(struct hdhomerun_discover_device_t));
result->ip_addr = remote_addr;
result->device_type = 0;
result->device_id = 0;
result->tuner_count = 0;

hdhomerun_sprintf(result->base_url, result->base_url + sizeof(result->base_url), "http://%u.%u.%u.%u:80",
(remote_addr >> 24) & 0xFF, (remote_addr >> 16) & 0xFF, (remote_addr >> 8) & 0xFF, (remote_addr >> 0) & 0xFF
);

while (1) {
uint8_t tag;
Expand All @@ -278,6 +295,7 @@ static bool_t hdhomerun_discover_recv_internal(struct hdhomerun_discover_t *ds,
break;
}

int i;
switch (tag) {
case HDHOMERUN_TAG_DEVICE_TYPE:
if (len != 4) {
Expand All @@ -291,6 +309,7 @@ static bool_t hdhomerun_discover_recv_internal(struct hdhomerun_discover_t *ds,
break;
}
result->device_id = hdhomerun_pkt_read_u32(rx_pkt);
result->is_legacy = hdhomerun_discover_is_legacy(result->device_id);
break;

case HDHOMERUN_TAG_TUNER_COUNT:
Expand All @@ -300,6 +319,39 @@ static bool_t hdhomerun_discover_recv_internal(struct hdhomerun_discover_t *ds,
result->tuner_count = hdhomerun_pkt_read_u8(rx_pkt);
break;

case HDHOMERUN_TAG_DEVICE_AUTH_STR:
if (len >= sizeof(result->device_auth)) {
break;
}
hdhomerun_pkt_read_mem(rx_pkt, result->device_auth, len);
result->device_auth[len] = 0;
break;

case HDHOMERUN_TAG_DEVICE_AUTH_BIN:
if (len != 18) {
break;
}
for (i = 0; i < 24; i += 4) {
uint32_t raw24;
raw24 = (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 16;
raw24 |= (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 8;
raw24 |= (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 0;
result->device_auth[i + 0] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 18) & 0x3F];
result->device_auth[i + 1] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 12) & 0x3F];
result->device_auth[i + 2] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 6) & 0x3F];
result->device_auth[i + 3] = hdhomerun_discover_recv_base64_encode_table[(raw24 >> 0) & 0x3F];
}
result->device_auth[24] = 0;
break;

case HDHOMERUN_TAG_BASE_URL:
if (len >= sizeof(result->base_url)) {
break;
}
hdhomerun_pkt_read_mem(rx_pkt, result->base_url, len);
result->base_url[len] = 0;
break;

default:
break;
}
Expand All @@ -325,21 +377,21 @@ static bool_t hdhomerun_discover_recv_internal(struct hdhomerun_discover_t *ds,
}
}

return TRUE;
return true;
}

static bool_t hdhomerun_discover_recv(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_device_t *result)
static bool hdhomerun_discover_recv(struct hdhomerun_discover_t *ds, struct hdhomerun_discover_device_t *result)
{
unsigned int i;
for (i = 0; i < ds->sock_count; i++) {
struct hdhomerun_discover_sock_t *dss = &ds->socks[i];

if (hdhomerun_discover_recv_internal(ds, dss, result)) {
return TRUE;
return true;
}
}

return FALSE;
return false;
}

static struct hdhomerun_discover_device_t *hdhomerun_discover_find_in_list(struct hdhomerun_discover_device_t result_list[], int count, struct hdhomerun_discover_device_t *lookup)
Expand All @@ -355,7 +407,7 @@ static struct hdhomerun_discover_device_t *hdhomerun_discover_find_in_list(struc
return NULL;
}

int hdhomerun_discover_find_devices(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count)
int hdhomerun_discover_find_devices_v2(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count)
{
hdhomerun_discover_sock_detect(ds);

Expand All @@ -375,7 +427,7 @@ int hdhomerun_discover_find_devices(struct hdhomerun_discover_t *ds, uint32_t ta
if (getcurrenttime() >= timeout) {
break;
}
msleep_approx(10);
msleep_approx(16);
continue;
}

Expand Down Expand Up @@ -407,7 +459,7 @@ int hdhomerun_discover_find_devices(struct hdhomerun_discover_t *ds, uint32_t ta
return count;
}

int hdhomerun_discover_find_devices_custom(uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count)
int hdhomerun_discover_find_devices_custom_v2(uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count)
{
if (hdhomerun_discover_is_ip_multicast(target_ip)) {
return 0;
Expand All @@ -418,17 +470,17 @@ int hdhomerun_discover_find_devices_custom(uint32_t target_ip, uint32_t device_t
return -1;
}

int ret = hdhomerun_discover_find_devices(ds, target_ip, device_type, device_id, result_list, max_count);
int ret = hdhomerun_discover_find_devices_v2(ds, target_ip, device_type, device_id, result_list, max_count);

hdhomerun_discover_destroy(ds);
return ret;
}

bool_t hdhomerun_discover_validate_device_id(uint32_t device_id)
bool hdhomerun_discover_validate_device_id(uint32_t device_id)
{
static uint32_t lookup_table[16] = {0xA, 0x5, 0xF, 0x6, 0x7, 0xC, 0x1, 0xB, 0x9, 0x2, 0x8, 0xD, 0x4, 0x3, 0xE, 0x0};
static uint8_t lookup_table[16] = {0xA, 0x5, 0xF, 0x6, 0x7, 0xC, 0x1, 0xB, 0x9, 0x2, 0x8, 0xD, 0x4, 0x3, 0xE, 0x0};

uint32_t checksum = 0;
uint8_t checksum = 0;

checksum ^= lookup_table[(device_id >> 28) & 0x0F];
checksum ^= (device_id >> 24) & 0x0F;
Expand All @@ -442,7 +494,7 @@ bool_t hdhomerun_discover_validate_device_id(uint32_t device_id)
return (checksum == 0);
}

bool_t hdhomerun_discover_is_ip_multicast(uint32_t ip_addr)
bool hdhomerun_discover_is_ip_multicast(uint32_t ip_addr)
{
return (ip_addr >= 0xE0000000) && (ip_addr < 0xF0000000);
}
45 changes: 18 additions & 27 deletions mythtv/external/libhdhomerun/hdhomerun_discover.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_discover.h
*
* Copyright © 2006-2007 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef __cplusplus
extern "C" {
Expand All @@ -38,6 +26,9 @@ struct hdhomerun_discover_device_t {
uint32_t device_type;
uint32_t device_id;
uint8_t tuner_count;
bool is_legacy;
char device_auth[25];
char base_url[29];
};

/*
Expand All @@ -54,33 +45,33 @@ struct hdhomerun_discover_device_t {
* Returns the number of devices found.
* Retruns -1 on error.
*/
extern LIBTYPE int hdhomerun_discover_find_devices_custom(uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count);
extern LIBHDHOMERUN_API int hdhomerun_discover_find_devices_custom_v2(uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count);

/*
* Optional: persistent discover instance available for discover polling use.
*/
extern LIBTYPE struct hdhomerun_discover_t *hdhomerun_discover_create(struct hdhomerun_debug_t *dbg);
extern LIBTYPE void hdhomerun_discover_destroy(struct hdhomerun_discover_t *ds);
extern LIBTYPE int hdhomerun_discover_find_devices(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count);
extern LIBHDHOMERUN_API struct hdhomerun_discover_t *hdhomerun_discover_create(struct hdhomerun_debug_t *dbg);
extern LIBHDHOMERUN_API void hdhomerun_discover_destroy(struct hdhomerun_discover_t *ds);
extern LIBHDHOMERUN_API int hdhomerun_discover_find_devices_v2(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count);

/*
* Verify that the device ID given is valid.
*
* The device ID contains a self-check sequence that detects common user input errors including
* single-digit errors and two digit transposition errors.
*
* Returns TRUE if valid.
* Returns FALSE if not valid.
* Returns true if valid.
* Returns false if not valid.
*/
extern LIBTYPE bool_t hdhomerun_discover_validate_device_id(uint32_t device_id);
extern LIBHDHOMERUN_API bool hdhomerun_discover_validate_device_id(uint32_t device_id);

/*
* Detect if an IP address is multicast.
*
* Returns TRUE if multicast.
* Returns FALSE if zero, unicast, expermental, or broadcast.
* Returns true if multicast.
* Returns false if zero, unicast, expermental, or broadcast.
*/
extern LIBTYPE bool_t hdhomerun_discover_is_ip_multicast(uint32_t ip_addr);
extern LIBHDHOMERUN_API bool hdhomerun_discover_is_ip_multicast(uint32_t ip_addr);

#ifdef __cplusplus
}
Expand Down
36 changes: 6 additions & 30 deletions mythtv/external/libhdhomerun/hdhomerun_os.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
/*
* hdhomerun_os.h
*
* Copyright © 2006-2008 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#if defined(_WIN32) || defined(_WIN64)
#define __WINDOWS__
#endif

#if defined(__WINDOWS__)
#if defined(_WIN32)
#include "hdhomerun_os_windows.h"
#else
#include "hdhomerun_os_posix.h"
#endif

#if !defined(TRUE)
#define TRUE 1
#endif

#if !defined(FALSE)
#define FALSE 0
#endif
241 changes: 184 additions & 57 deletions mythtv/external/libhdhomerun/hdhomerun_os_posix.c
Original file line number Diff line number Diff line change
@@ -1,92 +1,112 @@
/*
* hdhomerun_os_posix.c
*
* Copyright © 2006-2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2017 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* 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_os.h"

uint32_t random_get32(void)
{
FILE *fp = fopen("/dev/urandom", "rb");
if (!fp) {
return (uint32_t)getcurrenttime();
}
#if defined(__APPLE__)

uint32_t Result;
if (fread(&Result, 4, 1, fp) != 1) {
Result = (uint32_t)getcurrenttime();
}
#include <mach/clock.h>
#include <mach/mach.h>

fclose(fp);
return Result;
static pthread_once_t clock_monotonic_once = PTHREAD_ONCE_INIT;
static clock_serv_t clock_monotonic_clock_serv;

static void clock_monotonic_init(void)
{
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &clock_monotonic_clock_serv);
}

uint64_t getcurrenttime(void)
static inline void clock_monotonic_timespec(struct timespec *ts)
{
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static uint64_t result = 0;
static uint64_t previous_time = 0;
pthread_once(&clock_monotonic_once, clock_monotonic_init);

pthread_mutex_lock(&lock);
struct mach_timespec mt;
clock_get_time(clock_monotonic_clock_serv, &mt);
ts->tv_nsec = mt.tv_nsec;
ts->tv_sec = mt.tv_sec;
}

static inline void clock_realtime_timespec(struct timespec *ts)
{
struct timeval tv;
gettimeofday(&tv, NULL);
ts->tv_nsec = tv.tv_usec * 1000;
ts->tv_sec = tv.tv_sec;
}

#if defined(CLOCK_MONOTONIC)
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
uint64_t current_time = ((uint64_t)tp.tv_sec * 1000) + (tp.tv_nsec / 1000000);
#else
struct timeval t;
gettimeofday(&t, NULL);
uint64_t current_time = ((uint64_t)t.tv_sec * 1000) + (t.tv_usec / 1000);

static inline void clock_monotonic_timespec(struct timespec *ts)
{
clock_gettime(CLOCK_MONOTONIC, ts);
}

static inline void clock_realtime_timespec(struct timespec *ts)
{
clock_gettime(CLOCK_REALTIME, ts);
}

#endif

if (current_time > previous_time) {
result += current_time - previous_time;
static pthread_once_t random_get32_once = PTHREAD_ONCE_INIT;
static FILE *random_get32_fp = NULL;

static void random_get32_init(void)
{
random_get32_fp = fopen("/dev/urandom", "rb");
}

uint32_t random_get32(void)
{
pthread_once(&random_get32_once, random_get32_init);

if (!random_get32_fp) {
return (uint32_t)getcurrenttime();
}

previous_time = current_time;
uint32_t Result;
if (fread(&Result, 4, 1, random_get32_fp) != 1) {
return (uint32_t)getcurrenttime();
}

pthread_mutex_unlock(&lock);
return result;
return Result;
}

uint64_t getcurrenttime(void)
{
struct timespec ts;
clock_monotonic_timespec(&ts);
return ((uint64_t)ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
}

void msleep_approx(uint64_t ms)
{
unsigned int delay_s = ms / 1000;
uint64_t delay_s = ms / 1000;
if (delay_s > 0) {
sleep(delay_s);
sleep((unsigned int)delay_s);
ms -= delay_s * 1000;
}

unsigned int delay_us = ms * 1000;
uint64_t delay_us = ms * 1000;
if (delay_us > 0) {
usleep(delay_us);
usleep((useconds_t)delay_us);
}
}

Expand All @@ -104,32 +124,139 @@ void msleep_minimum(uint64_t ms)
}
}

bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap)
struct thread_task_execute_args_t {
thread_task_func_t func;
void *arg;
};

static void *thread_task_execute(void *arg)
{
struct thread_task_execute_args_t *execute_args = (struct thread_task_execute_args_t *)arg;
execute_args->func(execute_args->arg);
free(execute_args);
return NULL;
}

bool thread_task_create(thread_task_t *tid, thread_task_func_t func, void *arg)
{
struct thread_task_execute_args_t *execute_args = (struct thread_task_execute_args_t *)malloc(sizeof(struct thread_task_execute_args_t));
if (!execute_args) {
return false;
}

execute_args->func = func;
execute_args->arg = arg;

if (pthread_create(tid, NULL, thread_task_execute, execute_args) != 0) {
free(execute_args);
return false;
}

return true;
}

void thread_task_join(thread_task_t tid)
{
pthread_join(tid, NULL);
}

void thread_mutex_init(thread_mutex_t *mutex)
{
pthread_mutex_init(mutex, NULL);
}

void thread_mutex_dispose(pthread_mutex_t *mutex)
{
}

void thread_mutex_lock(thread_mutex_t *mutex)
{
pthread_mutex_lock(mutex);
}

void thread_mutex_unlock(thread_mutex_t *mutex)
{
pthread_mutex_unlock(mutex);
}

void thread_cond_init(thread_cond_t *cond)
{
cond->signaled = false;
pthread_mutex_init(&cond->lock, NULL);
pthread_cond_init(&cond->cond, NULL);
}

void thread_cond_dispose(thread_cond_t *cond)
{
}

void thread_cond_signal(thread_cond_t *cond)
{
pthread_mutex_lock(&cond->lock);

cond->signaled = true;
pthread_cond_signal(&cond->cond);

pthread_mutex_unlock(&cond->lock);
}

void thread_cond_wait(thread_cond_t *cond)
{
pthread_mutex_lock(&cond->lock);

if (!cond->signaled) {
pthread_cond_wait(&cond->cond, &cond->lock);
}

cond->signaled = false;
pthread_mutex_unlock(&cond->lock);
}

void thread_cond_wait_with_timeout(thread_cond_t *cond, uint64_t max_wait_time)
{
pthread_mutex_lock(&cond->lock);

if (!cond->signaled) {
struct timespec ts;
clock_realtime_timespec(&ts);

uint64_t tv_nsec = (uint64_t)ts.tv_nsec + (max_wait_time * 1000000);
ts.tv_nsec = (long)(tv_nsec % 1000000000);
ts.tv_sec += (time_t)(tv_nsec / 1000000000);

pthread_cond_timedwait(&cond->cond, &cond->lock, &ts);
}

cond->signaled = false;
pthread_mutex_unlock(&cond->lock);
}

bool hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap)
{
if (buffer >= end) {
return FALSE;
return false;
}

int length = vsnprintf(buffer, end - buffer - 1, fmt, ap);
if (length < 0) {
*buffer = 0;
return FALSE;
return false;
}

if (buffer + length + 1 > end) {
*(end - 1) = 0;
return FALSE;
return false;

}

return TRUE;
return true;
}

bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...)
bool hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
bool_t result = hdhomerun_vsprintf(buffer, end, fmt, ap);
bool result = hdhomerun_vsprintf(buffer, end, fmt, ap);
va_end(ap);
return result;
}
70 changes: 39 additions & 31 deletions mythtv/external/libhdhomerun/hdhomerun_os_posix.h
Original file line number Diff line number Diff line change
@@ -1,75 +1,83 @@
/*
* hdhomerun_os_posix.h
*
* Copyright © 2006-2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2017 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define _FILE_OFFSET_BITS 64
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
#include <netdb.h>
#include <pthread.h>

typedef int bool_t;

#if !defined(_DARWIN_C_SOURCE)
typedef void (*sig_t)(int);
#endif
typedef void (*thread_task_func_t)(void *arg);
typedef pthread_t thread_task_t;
typedef pthread_mutex_t thread_mutex_t;

typedef struct {
volatile bool signaled;
pthread_mutex_t lock;
pthread_cond_t cond;
} thread_cond_t;

#define LIBTYPE
#define console_vprintf vprintf
#define console_printf printf
#define THREAD_FUNC_PREFIX void *
#define LIBHDHOMERUN_API

#ifdef __cplusplus
extern "C" {
#endif

extern LIBTYPE uint32_t random_get32(void);
extern LIBTYPE uint64_t getcurrenttime(void);
extern LIBTYPE void msleep_approx(uint64_t ms);
extern LIBTYPE void msleep_minimum(uint64_t ms);
extern LIBHDHOMERUN_API uint32_t random_get32(void);
extern LIBHDHOMERUN_API uint64_t getcurrenttime(void);
extern LIBHDHOMERUN_API void msleep_approx(uint64_t ms);
extern LIBHDHOMERUN_API void msleep_minimum(uint64_t ms);

extern LIBHDHOMERUN_API bool thread_task_create(thread_task_t *tid, thread_task_func_t func, void *arg);
extern LIBHDHOMERUN_API void thread_task_join(thread_task_t tid);

extern LIBHDHOMERUN_API void thread_mutex_init(thread_mutex_t *mutex);
extern LIBHDHOMERUN_API void thread_mutex_dispose(thread_mutex_t *mutex);
extern LIBHDHOMERUN_API void thread_mutex_lock(thread_mutex_t *mutex);
extern LIBHDHOMERUN_API void thread_mutex_unlock(thread_mutex_t *mutex);

extern LIBHDHOMERUN_API void thread_cond_init(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_dispose(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_signal(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_wait(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_wait_with_timeout(thread_cond_t *cond, uint64_t max_wait_time);

extern LIBTYPE bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap);
extern LIBTYPE bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...);
extern LIBHDHOMERUN_API bool hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap);
extern LIBHDHOMERUN_API bool hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...);

#ifdef __cplusplus
}
Expand Down
183 changes: 98 additions & 85 deletions mythtv/external/libhdhomerun/hdhomerun_os_windows.c
Original file line number Diff line number Diff line change
@@ -1,74 +1,57 @@
/*
* hdhomerun_os_windows.c
*
* Copyright © 2006-2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2017 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* 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_os.h"
#include "hdhomerun.h"

#if defined(_WINRT)
uint32_t random_get32(void)
{
HCRYPTPROV hProv;
if (!CryptAcquireContext(&hProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
return (uint32_t)getcurrenttime();
return (uint32_t)getcurrenttime();
}
#else
uint32_t random_get32(void)
{
static DWORD random_get32_context_tls = 0xFFFFFFFF;
if (random_get32_context_tls == 0xFFFFFFFF) {
random_get32_context_tls = TlsAlloc();
}

HCRYPTPROV *phProv = (HCRYPTPROV *)TlsGetValue(random_get32_context_tls);
if (!phProv) {
phProv = (HCRYPTPROV *)calloc(1, sizeof(HCRYPTPROV));
CryptAcquireContext(phProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
TlsSetValue(random_get32_context_tls, phProv);
}

uint32_t Result;
CryptGenRandom(hProv, sizeof(Result), (BYTE*)&Result);
if (!CryptGenRandom(*phProv, sizeof(Result), (BYTE *)&Result)) {
return (uint32_t)getcurrenttime();
}

CryptReleaseContext(hProv, 0);
return Result;
}
#endif

uint64_t getcurrenttime(void)
{
static pthread_mutex_t lock = INVALID_HANDLE_VALUE;
static uint64_t result = 0;
static uint32_t previous_time = 0;

/* Initialization is not thread safe. */
if (lock == INVALID_HANDLE_VALUE) {
pthread_mutex_init(&lock, NULL);
}

pthread_mutex_lock(&lock);

uint32_t current_time = GetTickCount();

if (current_time > previous_time) {
result += current_time - previous_time;
}

previous_time = current_time;

pthread_mutex_unlock(&lock);
return result;
return GetTickCount64();
}

void msleep_approx(uint64_t ms)
Expand All @@ -90,92 +73,122 @@ void msleep_minimum(uint64_t ms)
}
}

#if !defined(PTHREAD_H)
int pthread_create(pthread_t *tid, void *attr, LPTHREAD_START_ROUTINE start, void *arg)
struct thread_task_execute_args_t {
thread_task_func_t func;
void *arg;
};

static DWORD WINAPI thread_task_execute(void *arg)
{
struct thread_task_execute_args_t *execute_args = (struct thread_task_execute_args_t *)arg;
execute_args->func(execute_args->arg);
free(execute_args);
return 0;
}

bool thread_task_create(thread_task_t *tid, thread_task_func_t func, void *arg)
{
*tid = CreateThread(NULL, 0, start, arg, 0, NULL);
struct thread_task_execute_args_t *execute_args = (struct thread_task_execute_args_t *)malloc(sizeof(struct thread_task_execute_args_t));
if (!execute_args) {
return false;
}

execute_args->func = func;
execute_args->arg = arg;

*tid = CreateThread(NULL, 0, thread_task_execute, execute_args, 0, NULL);
if (!*tid) {
return (int)GetLastError();
free(execute_args);
return false;
}
return 0;

return true;
}

int pthread_join(pthread_t tid, void **value_ptr)
void thread_task_join(thread_task_t tid)
{
while (1) {
DWORD ExitCode = 0;
if (!GetExitCodeThread(tid, &ExitCode)) {
return (int)GetLastError();
return;
}
if (ExitCode != STILL_ACTIVE) {
return 0;
return;
}
}
}

void pthread_mutex_init(pthread_mutex_t *mutex, void *attr)
void thread_mutex_init(thread_mutex_t *mutex)
{
*mutex = CreateMutex(NULL, FALSE, NULL);
*mutex = CreateMutex(NULL, false, NULL);
}

void pthread_mutex_lock(pthread_mutex_t *mutex)
void thread_mutex_dispose(thread_mutex_t *mutex)
{
CloseHandle(*mutex);
}

void thread_mutex_lock(thread_mutex_t *mutex)
{
WaitForSingleObject(*mutex, INFINITE);
}

void pthread_mutex_unlock(pthread_mutex_t *mutex)
void thread_mutex_unlock(thread_mutex_t *mutex)
{
ReleaseMutex(*mutex);
}
#endif

bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap)
void thread_cond_init(thread_cond_t *cond)
{
*cond = CreateEvent(NULL, false, false, NULL);
}

void thread_cond_dispose(thread_cond_t *cond)
{
CloseHandle(*cond);
}

void thread_cond_signal(thread_cond_t *cond)
{
SetEvent(*cond);
}

void thread_cond_wait(thread_cond_t *cond)
{
WaitForSingleObject(*cond, INFINITE);
}

void thread_cond_wait_with_timeout(thread_cond_t *cond, uint64_t max_wait_time)
{
WaitForSingleObject(*cond, (DWORD)max_wait_time);
}

bool hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap)
{
if (buffer >= end) {
return FALSE;
return false;
}

int length = _vsnprintf(buffer, end - buffer - 1, fmt, ap);
if (length < 0) {
*buffer = 0;
return FALSE;
return false;
}

if (buffer + length + 1 > end) {
*(end - 1) = 0;
return FALSE;
return false;

}

return TRUE;
return true;
}

bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...)
bool hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
bool_t result = hdhomerun_vsprintf(buffer, end, fmt, ap);
bool result = hdhomerun_vsprintf(buffer, end, fmt, ap);
va_end(ap);
return result;
}

/*
* 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.
*/
void console_vprintf(const char *fmt, va_list ap)
{
UINT cp = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
vprintf(fmt, ap);
SetConsoleOutputCP(cp);
}

void console_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
console_vprintf(fmt, ap);
va_end(ap);
}
123 changes: 55 additions & 68 deletions mythtv/external/libhdhomerun/hdhomerun_os_windows.h
Original file line number Diff line number Diff line change
@@ -1,116 +1,103 @@
/*
* hdhomerun_os_windows.h
*
* Copyright © 2006-2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2017 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#define _WINSOCKAPI_
#if defined(USING_MINGW)
/* MinGW lacks wspiapi.h; set minimum WINVER to WinXP to remove dependency */
#define WINVER 0x0501
#ifdef _WINRT
#include <SDKDDKVer.h>
#endif
#include <windows.h>

#ifndef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_VISTA
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif

#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <wincrypt.h>
#if !defined(USING_MINGW)
#include <wspiapi.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>

#if defined(DLL_IMPORT)
#define LIBTYPE __declspec( dllimport )
#elif defined(DLL_EXPORT)
#define LIBTYPE __declspec( dllexport )
#else
#define LIBTYPE
#ifdef LIBHDHOMERUN_DLLEXPORT
#define LIBHDHOMERUN_API __declspec(dllexport)
#endif
#ifdef LIBHDHOMERUN_DLLIMPORT
#define LIBHDHOMERUN_API __declspec(dllimport)
#endif
#ifndef LIBHDHOMERUN_API
#define LIBHDHOMERUN_API
#endif

typedef int bool_t;
#if defined(USING_MINGW)
#include <stdint.h>
#include <pthread.h>
#else
typedef signed __int8 int8_t;
typedef signed __int16 int16_t;
typedef signed __int32 int32_t;
typedef signed __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
typedef void (*sig_t)(int);
typedef HANDLE pthread_t;
typedef HANDLE pthread_mutex_t;
#endif
typedef void (*thread_task_func_t)(void *arg);
typedef HANDLE thread_task_t;
typedef HANDLE thread_mutex_t;
typedef HANDLE thread_cond_t;

#if !defined(va_copy)
#define va_copy(x, y) x = y
#endif

#define atoll _atoi64
#define strdup _strdup
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define fseeko _fseeki64
#define ftello _ftelli64
#define THREAD_FUNC_PREFIX DWORD WINAPI

#ifdef __cplusplus
extern "C" {
#endif

extern LIBTYPE uint32_t random_get32(void);
extern LIBTYPE uint64_t getcurrenttime(void);
extern LIBTYPE void msleep_approx(uint64_t ms);
extern LIBTYPE void msleep_minimum(uint64_t ms);
extern LIBHDHOMERUN_API uint32_t random_get32(void);
extern LIBHDHOMERUN_API uint64_t getcurrenttime(void);
extern LIBHDHOMERUN_API void msleep_approx(uint64_t ms);
extern LIBHDHOMERUN_API void msleep_minimum(uint64_t ms);

#if !defined(PTHREAD_H)
extern LIBTYPE int pthread_create(pthread_t *tid, void *attr, LPTHREAD_START_ROUTINE start, void *arg);
extern LIBTYPE int pthread_join(pthread_t tid, void **value_ptr);
extern LIBTYPE void pthread_mutex_init(pthread_mutex_t *mutex, void *attr);
extern LIBTYPE void pthread_mutex_lock(pthread_mutex_t *mutex);
extern LIBTYPE void pthread_mutex_unlock(pthread_mutex_t *mutex);
#endif
extern LIBHDHOMERUN_API bool thread_task_create(thread_task_t *tid, thread_task_func_t func, void *arg);
extern LIBHDHOMERUN_API void thread_task_join(thread_task_t tid);

extern LIBTYPE bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap);
extern LIBTYPE bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...);
extern LIBHDHOMERUN_API void thread_mutex_init(thread_mutex_t *mutex);
extern LIBHDHOMERUN_API void thread_mutex_dispose(thread_mutex_t *mutex);
extern LIBHDHOMERUN_API void thread_mutex_lock(thread_mutex_t *mutex);
extern LIBHDHOMERUN_API void thread_mutex_unlock(thread_mutex_t *mutex);

/*
* 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.
*/
extern LIBTYPE void console_vprintf(const char *fmt, va_list ap);
extern LIBTYPE void console_printf(const char *fmt, ...);
extern LIBHDHOMERUN_API void thread_cond_init(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_dispose(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_signal(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_wait(thread_cond_t *cond);
extern LIBHDHOMERUN_API void thread_cond_wait_with_timeout(thread_cond_t *cond, uint64_t max_wait_time);

extern LIBHDHOMERUN_API bool hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap);
extern LIBHDHOMERUN_API bool hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...);

#ifdef __cplusplus
}
Expand Down
28 changes: 11 additions & 17 deletions mythtv/external/libhdhomerun/hdhomerun_pkt.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_pkt.c
*
* Copyright © 2005-2006 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2014 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* 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"
Expand Down Expand Up @@ -137,6 +125,12 @@ uint8_t *hdhomerun_pkt_read_tlv(struct hdhomerun_pkt_t *pkt, uint8_t *ptag, size
return pkt->pos + *plength;
}

void hdhomerun_pkt_read_mem(struct hdhomerun_pkt_t *pkt, void *mem, size_t length)
{
memcpy(mem, pkt->pos, length);
pkt->pos += length;
}

void hdhomerun_pkt_write_u8(struct hdhomerun_pkt_t *pkt, uint8_t v)
{
*pkt->pos++ = v;
Expand Down
57 changes: 25 additions & 32 deletions mythtv/external/libhdhomerun/hdhomerun_pkt.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_pkt.h
*
* Copyright © 2005-2006 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2006-2015 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -139,9 +127,13 @@ extern "C" {
#define HDHOMERUN_TAG_GETSET_LOCKKEY 0x15
#define HDHOMERUN_TAG_ERROR_MESSAGE 0x05
#define HDHOMERUN_TAG_TUNER_COUNT 0x10
#define HDHOMERUN_TAG_DEVICE_AUTH_BIN 0x29
#define HDHOMERUN_TAG_BASE_URL 0x2A
#define HDHOMERUN_TAG_DEVICE_AUTH_STR 0x2B

#define HDHOMERUN_DEVICE_TYPE_WILDCARD 0xFFFFFFFF
#define HDHOMERUN_DEVICE_TYPE_TUNER 0x00000001
#define HDHOMERUN_DEVICE_TYPE_STORAGE 0x00000005
#define HDHOMERUN_DEVICE_ID_WILDCARD 0xFFFFFFFF

#define HDHOMERUN_MIN_PEEK_LENGTH 4
Expand All @@ -154,24 +146,25 @@ struct hdhomerun_pkt_t {
uint8_t buffer[3074];
};

extern LIBTYPE struct hdhomerun_pkt_t *hdhomerun_pkt_create(void);
extern LIBTYPE void hdhomerun_pkt_destroy(struct hdhomerun_pkt_t *pkt);
extern LIBTYPE void hdhomerun_pkt_reset(struct hdhomerun_pkt_t *pkt);
extern LIBHDHOMERUN_API struct hdhomerun_pkt_t *hdhomerun_pkt_create(void);
extern LIBHDHOMERUN_API void hdhomerun_pkt_destroy(struct hdhomerun_pkt_t *pkt);
extern LIBHDHOMERUN_API void hdhomerun_pkt_reset(struct hdhomerun_pkt_t *pkt);

extern LIBTYPE uint8_t hdhomerun_pkt_read_u8(struct hdhomerun_pkt_t *pkt);
extern LIBTYPE uint16_t hdhomerun_pkt_read_u16(struct hdhomerun_pkt_t *pkt);
extern LIBTYPE uint32_t hdhomerun_pkt_read_u32(struct hdhomerun_pkt_t *pkt);
extern LIBTYPE size_t hdhomerun_pkt_read_var_length(struct hdhomerun_pkt_t *pkt);
extern LIBTYPE uint8_t *hdhomerun_pkt_read_tlv(struct hdhomerun_pkt_t *pkt, uint8_t *ptag, size_t *plength);
extern LIBHDHOMERUN_API uint8_t hdhomerun_pkt_read_u8(struct hdhomerun_pkt_t *pkt);
extern LIBHDHOMERUN_API uint16_t hdhomerun_pkt_read_u16(struct hdhomerun_pkt_t *pkt);
extern LIBHDHOMERUN_API uint32_t hdhomerun_pkt_read_u32(struct hdhomerun_pkt_t *pkt);
extern LIBHDHOMERUN_API size_t hdhomerun_pkt_read_var_length(struct hdhomerun_pkt_t *pkt);
extern LIBHDHOMERUN_API uint8_t *hdhomerun_pkt_read_tlv(struct hdhomerun_pkt_t *pkt, uint8_t *ptag, size_t *plength);
extern LIBHDHOMERUN_API void hdhomerun_pkt_read_mem(struct hdhomerun_pkt_t *pkt, void *mem, size_t length);

extern LIBTYPE void hdhomerun_pkt_write_u8(struct hdhomerun_pkt_t *pkt, uint8_t v);
extern LIBTYPE void hdhomerun_pkt_write_u16(struct hdhomerun_pkt_t *pkt, uint16_t v);
extern LIBTYPE void hdhomerun_pkt_write_u32(struct hdhomerun_pkt_t *pkt, uint32_t v);
extern LIBTYPE void hdhomerun_pkt_write_var_length(struct hdhomerun_pkt_t *pkt, size_t v);
extern LIBTYPE void hdhomerun_pkt_write_mem(struct hdhomerun_pkt_t *pkt, const void *mem, size_t length);
extern LIBHDHOMERUN_API void hdhomerun_pkt_write_u8(struct hdhomerun_pkt_t *pkt, uint8_t v);
extern LIBHDHOMERUN_API void hdhomerun_pkt_write_u16(struct hdhomerun_pkt_t *pkt, uint16_t v);
extern LIBHDHOMERUN_API void hdhomerun_pkt_write_u32(struct hdhomerun_pkt_t *pkt, uint32_t v);
extern LIBHDHOMERUN_API void hdhomerun_pkt_write_var_length(struct hdhomerun_pkt_t *pkt, size_t v);
extern LIBHDHOMERUN_API void hdhomerun_pkt_write_mem(struct hdhomerun_pkt_t *pkt, const void *mem, size_t length);

extern LIBTYPE bool_t hdhomerun_pkt_open_frame(struct hdhomerun_pkt_t *pkt, uint16_t *ptype);
extern LIBTYPE void hdhomerun_pkt_seal_frame(struct hdhomerun_pkt_t *pkt, uint16_t frame_type);
extern LIBHDHOMERUN_API int hdhomerun_pkt_open_frame(struct hdhomerun_pkt_t *pkt, uint16_t *ptype);
extern LIBHDHOMERUN_API void hdhomerun_pkt_seal_frame(struct hdhomerun_pkt_t *pkt, uint16_t frame_type);

#ifdef __cplusplus
}
Expand Down
64 changes: 28 additions & 36 deletions mythtv/external/libhdhomerun/hdhomerun_sock.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
/*
* hdhomerun_sock.h
*
* Copyright © 2010 Silicondust USA Inc. <www.silicondust.com>.
* Copyright © 2010-2016 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* 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.
* 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, 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.
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef __cplusplus
extern "C" {
Expand All @@ -38,33 +26,37 @@ struct hdhomerun_local_ip_info_t {
uint32_t subnet_mask;
};

extern LIBTYPE int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count);
extern LIBHDHOMERUN_API int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count);
extern LIBHDHOMERUN_API void hdhomerun_local_ip_info_set_str(const char *ip_info_str); /* WinRT only */

#define HDHOMERUN_SOCK_INVALID -1
struct hdhomerun_sock_t;

typedef int hdhomerun_sock_t;
extern LIBHDHOMERUN_API struct hdhomerun_sock_t *hdhomerun_sock_create_udp(void);
extern LIBHDHOMERUN_API struct hdhomerun_sock_t *hdhomerun_sock_create_tcp(void);
extern LIBHDHOMERUN_API void hdhomerun_sock_stop(struct hdhomerun_sock_t *sock);
extern LIBHDHOMERUN_API void hdhomerun_sock_destroy(struct hdhomerun_sock_t *sock);

extern LIBTYPE hdhomerun_sock_t hdhomerun_sock_create_udp(void);
extern LIBTYPE hdhomerun_sock_t hdhomerun_sock_create_tcp(void);
extern LIBTYPE void hdhomerun_sock_destroy(hdhomerun_sock_t sock);
extern LIBHDHOMERUN_API void hdhomerun_sock_set_send_buffer_size(struct hdhomerun_sock_t *sock, size_t size);
extern LIBHDHOMERUN_API void hdhomerun_sock_set_recv_buffer_size(struct hdhomerun_sock_t *sock, size_t size);
extern LIBHDHOMERUN_API void hdhomerun_sock_set_allow_reuse(struct hdhomerun_sock_t *sock);

extern LIBTYPE int hdhomerun_sock_getlasterror(void);
extern LIBTYPE uint32_t hdhomerun_sock_getsockname_addr(hdhomerun_sock_t sock);
extern LIBTYPE uint16_t hdhomerun_sock_getsockname_port(hdhomerun_sock_t sock);
extern LIBTYPE uint32_t hdhomerun_sock_getpeername_addr(hdhomerun_sock_t sock);
extern LIBTYPE uint32_t hdhomerun_sock_getaddrinfo_addr(hdhomerun_sock_t sock, const char *name);
extern LIBHDHOMERUN_API int hdhomerun_sock_getlasterror(void);
extern LIBHDHOMERUN_API uint32_t hdhomerun_sock_getsockname_addr(struct hdhomerun_sock_t *sock);
extern LIBHDHOMERUN_API uint16_t hdhomerun_sock_getsockname_port(struct hdhomerun_sock_t *sock);
extern LIBHDHOMERUN_API uint32_t hdhomerun_sock_getpeername_addr(struct hdhomerun_sock_t *sock);
extern LIBHDHOMERUN_API uint32_t hdhomerun_sock_getaddrinfo_addr(struct hdhomerun_sock_t *sock, const char *name);

extern LIBTYPE bool_t hdhomerun_sock_join_multicast_group(hdhomerun_sock_t sock, uint32_t multicast_ip, uint32_t local_ip);
extern LIBTYPE bool_t hdhomerun_sock_leave_multicast_group(hdhomerun_sock_t sock, uint32_t multicast_ip, uint32_t local_ip);
extern LIBHDHOMERUN_API bool hdhomerun_sock_join_multicast_group(struct hdhomerun_sock_t *sock, uint32_t multicast_ip, uint32_t local_ip);
extern LIBHDHOMERUN_API bool hdhomerun_sock_leave_multicast_group(struct hdhomerun_sock_t *sock, uint32_t multicast_ip, uint32_t local_ip);

extern LIBTYPE bool_t hdhomerun_sock_bind(hdhomerun_sock_t sock, uint32_t local_addr, uint16_t local_port, bool_t allow_reuse);
extern LIBTYPE bool_t hdhomerun_sock_connect(hdhomerun_sock_t sock, uint32_t remote_addr, uint16_t remote_port, uint64_t timeout);
extern LIBHDHOMERUN_API bool hdhomerun_sock_bind(struct hdhomerun_sock_t *sock, uint32_t local_addr, uint16_t local_port, bool allow_reuse);
extern LIBHDHOMERUN_API bool hdhomerun_sock_connect(struct hdhomerun_sock_t *sock, uint32_t remote_addr, uint16_t remote_port, uint64_t timeout);

extern LIBTYPE bool_t hdhomerun_sock_send(hdhomerun_sock_t sock, const void *data, size_t length, uint64_t timeout);
extern LIBTYPE bool_t hdhomerun_sock_sendto(hdhomerun_sock_t sock, uint32_t remote_addr, uint16_t remote_port, const void *data, size_t length, uint64_t timeout);
extern LIBHDHOMERUN_API bool hdhomerun_sock_send(struct hdhomerun_sock_t *sock, const void *data, size_t length, uint64_t timeout);
extern LIBHDHOMERUN_API bool hdhomerun_sock_sendto(struct hdhomerun_sock_t *sock, uint32_t remote_addr, uint16_t remote_port, const void *data, size_t length, uint64_t timeout);

extern LIBTYPE bool_t hdhomerun_sock_recv(hdhomerun_sock_t sock, void *data, size_t *length, uint64_t timeout);
extern LIBTYPE bool_t hdhomerun_sock_recvfrom(hdhomerun_sock_t sock, uint32_t *remote_addr, uint16_t *remote_port, void *data, size_t *length, uint64_t timeout);
extern LIBHDHOMERUN_API bool hdhomerun_sock_recv(struct hdhomerun_sock_t *sock, void *data, size_t *length, uint64_t timeout);
extern LIBHDHOMERUN_API bool hdhomerun_sock_recvfrom(struct hdhomerun_sock_t *sock, uint32_t *remote_addr, uint16_t *remote_port, void *data, size_t *length, uint64_t timeout);

#ifdef __cplusplus
}
Expand Down
Loading