700 changes: 350 additions & 350 deletions mythtv/libs/libmythhdhomerun/hdhomerun_channelscan.c

Large diffs are not rendered by default.

1,401 changes: 702 additions & 699 deletions mythtv/libs/libmythhdhomerun/hdhomerun_config.c

Large diffs are not rendered by default.

2,490 changes: 1,245 additions & 1,245 deletions mythtv/libs/libmythhdhomerun/hdhomerun_device.c

Large diffs are not rendered by default.

939 changes: 439 additions & 500 deletions mythtv/libs/libmythhdhomerun/hdhomerun_discover.c

Large diffs are not rendered by default.

173 changes: 87 additions & 86 deletions mythtv/libs/libmythhdhomerun/hdhomerun_discover.h
Original file line number Diff line number Diff line change
@@ -1,86 +1,87 @@
/*
* hdhomerun_discover.h
*
* Copyright © 2006-2007 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception to the GNU Lesser General Public License,
* you may link, statically or dynamically, an application with a
* publicly distributed version of the Library to produce an
* executable file containing portions of the Library, and
* distribute that executable file under terms of your choice,
* without any of the additional requirements listed in clause 4 of
* the GNU Lesser General Public License.
*
* By "a publicly distributed version of the Library", we mean
* either the unmodified Library as distributed by Silicondust, or a
* modified version of the Library that is distributed under the
* conditions defined in the GNU Lesser General Public License.
*/
#ifdef __cplusplus
extern "C" {
#endif

struct hdhomerun_discover_device_t {
uint32_t ip_addr;
uint32_t device_type;
uint32_t device_id;
};

/*
* Find devices.
*
* The device information is stored in caller-supplied array of hdhomerun_discover_device_t vars.
* Multiple attempts are made to find devices.
* Execution time is typically 400ms if max_count is not reached.
*
* Set target_ip to zero to auto-detect the IP address.
* Set device_type to HDHOMERUN_DEVICE_TYPE_TUNER to detect HDHomeRun tuner devices.
* Set device_id to HDHOMERUN_DEVICE_ID_WILDCARD to detect all device ids.
*
* 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);

/*
* Optional: persistent discover instance available for discover polling use.
*/
extern LIBTYPE struct hdhomerun_discover_t *hdhomerun_discover_create(void);
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);

/*
* 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.
*/
extern LIBTYPE bool_t 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.
*/
extern LIBTYPE bool_t hdhomerun_discover_is_ip_multicast(uint32_t ip_addr);

#ifdef __cplusplus
}
#endif
/*
* hdhomerun_discover.h
*
* Copyright © 2006-2007 Silicondust USA Inc. <www.silicondust.com>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception to the GNU Lesser General Public License,
* you may link, statically or dynamically, an application with a
* publicly distributed version of the Library to produce an
* executable file containing portions of the Library, and
* distribute that executable file under terms of your choice,
* without any of the additional requirements listed in clause 4 of
* the GNU Lesser General Public License.
*
* By "a publicly distributed version of the Library", we mean
* either the unmodified Library as distributed by Silicondust, or a
* modified version of the Library that is distributed under the
* conditions defined in the GNU Lesser General Public License.
*/
#ifdef __cplusplus
extern "C" {
#endif

struct hdhomerun_discover_device_t {
uint32_t ip_addr;
uint32_t device_type;
uint32_t device_id;
uint8_t tuner_count;
};

/*
* Find devices.
*
* The device information is stored in caller-supplied array of hdhomerun_discover_device_t vars.
* Multiple attempts are made to find devices.
* Execution time is typically 400ms if max_count is not reached.
*
* Set target_ip to zero to auto-detect the IP address.
* Set device_type to HDHOMERUN_DEVICE_TYPE_TUNER to detect HDHomeRun tuner devices.
* Set device_id to HDHOMERUN_DEVICE_ID_WILDCARD to detect all device ids.
*
* 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);

/*
* Optional: persistent discover instance available for discover polling use.
*/
extern LIBTYPE struct hdhomerun_discover_t *hdhomerun_discover_create(void);
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);

/*
* 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.
*/
extern LIBTYPE bool_t 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.
*/
extern LIBTYPE bool_t hdhomerun_discover_is_ip_multicast(uint32_t ip_addr);

#ifdef __cplusplus
}
#endif
16 changes: 16 additions & 0 deletions mythtv/libs/libmythhdhomerun/hdhomerun_os_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@

#include "hdhomerun_os.h"

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

uint32_t Result;
if (fread(&Result, 4, 1, fp) != 1) {
Result = (uint32_t)rand();
}

fclose(fp);
return Result;
}

uint64_t getcurrenttime(void)
{
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythhdhomerun/hdhomerun_os_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef void (*sig_t)(int);
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);
Expand Down
16 changes: 14 additions & 2 deletions mythtv/libs/libmythhdhomerun/hdhomerun_os_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@

#include "hdhomerun_os.h"

uint32_t random_get32(void)
{
HCRYPTPROV hProv;
if (!CryptAcquireContext(&hProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
return (uint32_t)rand();
}

uint32_t Result;
CryptGenRandom(hProv, sizeof(Result), (BYTE*)&Result);

CryptReleaseContext(hProv, 0);
return Result;
}

uint64_t getcurrenttime(void)
{
static pthread_mutex_t lock = INVALID_HANDLE_VALUE;
Expand Down Expand Up @@ -76,7 +90,6 @@ void msleep_minimum(uint64_t ms)
}
}

#if !defined(PTHREAD_H)
int pthread_create(pthread_t *tid, void *attr, LPTHREAD_START_ROUTINE start, void *arg)
{
*tid = CreateThread(NULL, 0, start, arg, 0, NULL);
Expand Down Expand Up @@ -113,7 +126,6 @@ void pthread_mutex_unlock(pthread_mutex_t *mutex)
{
ReleaseMutex(*mutex);
}
#endif

/*
* The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing.
Expand Down
14 changes: 1 addition & 13 deletions mythtv/libs/libmythhdhomerun/hdhomerun_os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@
*/

#define _WINSOCKAPI_
#if defined(USING_MINGW)
/* MinGW lacks wspiapi.h; set minimum WINVER to WinXP to remove dependency */
#define WINVER 0x0501
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#if !defined(USING_MINGW)
#include <wspiapi.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
Expand All @@ -59,10 +53,6 @@
#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;
Expand All @@ -74,7 +64,6 @@ typedef unsigned __int64 uint64_t;
typedef void (*sig_t)(int);
typedef HANDLE pthread_t;
typedef HANDLE pthread_mutex_t;
#endif

#define va_copy(x, y) x = y
#define atoll _atoi64
Expand All @@ -89,17 +78,16 @@ typedef HANDLE pthread_mutex_t;
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);

#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

/*
* The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing.
Expand Down
355 changes: 178 additions & 177 deletions mythtv/libs/libmythhdhomerun/hdhomerun_pkt.h

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion mythtv/libs/libmythhdhomerun/hdhomerun_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
extern "C" {
#endif

struct hdhomerun_local_ip_info_t {
uint32_t ip_addr;
uint32_t subnet_mask;
};

extern LIBTYPE int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count);

#define HDHOMERUN_SOCK_INVALID -1

typedef int hdhomerun_sock_t;
Expand All @@ -56,7 +63,6 @@ extern LIBTYPE bool_t hdhomerun_sock_sendto(hdhomerun_sock_t sock, uint32_t remo
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);


#ifdef __cplusplus
}
#endif
66 changes: 66 additions & 0 deletions mythtv/libs/libmythhdhomerun/hdhomerun_sock_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,72 @@

#include "hdhomerun.h"

#include <net/if.h>
#include <sys/ioctl.h>
#ifndef SIOCGIFCONF
#include <sys/sockio.h>
#endif
#ifndef _SIZEOF_ADDR_IFREQ
#define _SIZEOF_ADDR_IFREQ(x) sizeof(x)
#endif

int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count)
{
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == HDHOMERUN_SOCK_INVALID) {
return -1;
}

struct ifconf ifc;
uint8_t buf[8192];
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (char *)buf;

memset(buf, 0, sizeof(buf));

if (ioctl(sock, SIOCGIFCONF, &ifc) != 0) {
close(sock);
return -1;
}

uint8_t *ptr = (uint8_t *)ifc.ifc_req;
uint8_t *end = (uint8_t *)&ifc.ifc_buf[ifc.ifc_len];

int count = 0;
while (ptr <= end) {
struct ifreq *ifr = (struct ifreq *)ptr;
ptr += _SIZEOF_ADDR_IFREQ(*ifr);

if (ioctl(sock, SIOCGIFADDR, ifr) != 0) {
continue;
}

struct sockaddr_in *ip_addr_in = (struct sockaddr_in *)&(ifr->ifr_addr);
uint32_t ip_addr = ntohl(ip_addr_in->sin_addr.s_addr);
if (ip_addr == 0) {
continue;
}

if (ioctl(sock, SIOCGIFNETMASK, ifr) != 0) {
continue;
}

struct sockaddr_in *subnet_mask_in = (struct sockaddr_in *)&(ifr->ifr_addr);
uint32_t subnet_mask = ntohl(subnet_mask_in->sin_addr.s_addr);

struct hdhomerun_local_ip_info_t *ip_info = &ip_info_list[count++];
ip_info->ip_addr = ip_addr;
ip_info->subnet_mask = subnet_mask;

if (count >= max_count) {
break;
}
}

close(sock);
return count;
}

hdhomerun_sock_t hdhomerun_sock_create_udp(void)
{
/* Create socket. */
Expand Down
56 changes: 56 additions & 0 deletions mythtv/libs/libmythhdhomerun/hdhomerun_sock_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,62 @@
*/

#include "hdhomerun.h"
#include <windows.h>
#include <iphlpapi.h>

int hdhomerun_local_ip_info(struct hdhomerun_local_ip_info_t ip_info_list[], int max_count)
{
PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);

DWORD Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
if (Ret != NO_ERROR) {
free(pAdapterInfo);
if (Ret != ERROR_BUFFER_OVERFLOW) {
return -1;
}
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
if (Ret != NO_ERROR) {
free(pAdapterInfo);
return -1;
}
}

int count = 0;
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter) {
IP_ADDR_STRING *pIPAddr = &pAdapter->IpAddressList;
while (pIPAddr) {
uint32_t ip_addr = ntohl(inet_addr(pIPAddr->IpAddress.String));
uint32_t subnet_mask = ntohl(inet_addr(pIPAddr->IpMask.String));

if (ip_addr == 0) {
pIPAddr = pIPAddr->Next;
continue;
}

struct hdhomerun_local_ip_info_t *ip_info = &ip_info_list[count++];
ip_info->ip_addr = ip_addr;
ip_info->subnet_mask = subnet_mask;

if (count >= max_count) {
break;
}

pIPAddr = pIPAddr->Next;
}

if (count >= max_count) {
break;
}

pAdapter = pAdapter->Next;
}

free(pAdapterInfo);
return count;
}

hdhomerun_sock_t hdhomerun_sock_create_udp(void)
{
Expand Down