static int discover_print(char *target_ip_str) { uint32_t target_ip = 0; if (target_ip_str) { target_ip = parse_ip_addr(target_ip_str); if (target_ip == 0) { fprintf(stderr, "invalid ip address: %s\n", target_ip_str); return -1; } } struct hdhomerun_discover_device_t result_list[64]; int count = hdhomerun_discover_find_devices_custom(target_ip, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, result_list, 64); if (count < 0) { fprintf(stderr, "error sending discover request\n"); return -1; } if (count == 0) { printf("no devices found\n"); return 0; } int index; for (index = 0; index < count; index++) { struct hdhomerun_discover_device_t *result = &result_list[index]; printf("hdhomerun device %08lX found at %u.%u.%u.%u\n", (unsigned long)result->device_id, (unsigned int)(result->ip_addr >> 24) & 0x0FF, (unsigned int)(result->ip_addr >> 16) & 0x0FF, (unsigned int)(result->ip_addr >> 8) & 0x0FF, (unsigned int)(result->ip_addr >> 0) & 0x0FF ); } return count; }
static void tvhdhomerun_device_discovery( void ) { struct hdhomerun_discover_device_t result_list[MAX_HDHOMERUN_DEVICES]; if (!tvheadend_running) return; int numDevices = hdhomerun_discover_find_devices_custom(0, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, result_list, MAX_HDHOMERUN_DEVICES); if (numDevices > 0) { while (numDevices > 0 ) { numDevices--; struct hdhomerun_discover_device_t* cDev = &result_list[numDevices]; if ( cDev->device_type == HDHOMERUN_DEVICE_TYPE_TUNER ) { if ( !tvhdhomerun_device_find(cDev->device_id) ) { tvhlog(LOG_INFO, "tvhdhomerun","Found HDHomerun device %08x with %d tuners", cDev->device_id, cDev->tuner_count); tvhdhomerun_device_create(cDev); } } } } }
PyObject *py_device_discover(PyObject *cls, PyObject *args, PyObject *kwds) { PyObject *result = NULL; PyObject *tuner = NULL; char *target_ip_str = NULL; uint32_t target_ip = 0; int count = 0, i; char *kwlist[] = {"target_ip", NULL}; struct hdhomerun_discover_device_t result_list[64]; if(!PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist, &target_ip_str)) return NULL; if(target_ip_str) { target_ip = parse_ip_addr(target_ip_str); if (target_ip == 0) { PyErr_SetString(hdhomerun_device_error, "invalid ip address"); return NULL; } } count = hdhomerun_discover_find_devices_custom(target_ip, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, result_list, 64); if(count < 0) { PyErr_SetString(hdhomerun_device_error, "error sending discover request"); return NULL; } result = PyList_New((Py_ssize_t)count); if(!result) { return NULL; } if(count > 0) { for(i=0; i<count; i++) { tuner = PyObject_CallFunction(cls, "II", result_list[i].ip_addr, result_list[i].device_id); if(tuner == NULL) { Py_DECREF(result); return NULL; } if(PyList_SetItem(result, i, tuner) != 0) { Py_DECREF(result); return NULL; } } } return result; }
static void * tvhdhomerun_device_discovery_thread( void *aux ) { struct hdhomerun_discover_device_t result_list[MAX_HDHOMERUN_DEVICES]; int numDevices, brk; while (tvheadend_is_running()) { numDevices = hdhomerun_discover_find_devices_custom(0, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, result_list, MAX_HDHOMERUN_DEVICES); if (numDevices > 0) { while (numDevices > 0 ) { numDevices--; struct hdhomerun_discover_device_t* cDev = &result_list[numDevices]; if ( cDev->device_type == HDHOMERUN_DEVICE_TYPE_TUNER ) { pthread_mutex_lock(&global_lock); tvhdhomerun_device_t *existing = tvhdhomerun_device_find(cDev->device_id); if ( tvheadend_is_running() ) { if ( !existing ) { tvhinfo(LS_TVHDHOMERUN,"Found HDHomerun device %08x with %d tuners", cDev->device_id, cDev->tuner_count); tvhdhomerun_device_create(cDev); } else if ( ((struct sockaddr_in *)&existing->hd_info.ip_address)->sin_addr.s_addr != htonl(cDev->ip_addr) ) { struct sockaddr_storage detected_dev_addr; memset(&detected_dev_addr, 0, sizeof(detected_dev_addr)); detected_dev_addr.ss_family = AF_INET; ((struct sockaddr_in *)&detected_dev_addr)->sin_addr.s_addr = htonl(cDev->ip_addr); char existing_ip[64]; tcp_get_str_from_ip(&existing->hd_info.ip_address, existing_ip, sizeof(existing_ip)); char detected_ip[64]; tcp_get_str_from_ip(&detected_dev_addr, detected_ip, sizeof(detected_ip)); tvhinfo(LS_TVHDHOMERUN,"HDHomerun device %08x switched IPs from %s to %s, updating", cDev->device_id, existing_ip, detected_ip); tvhdhomerun_device_destroy(existing); tvhdhomerun_device_create(cDev); } } pthread_mutex_unlock(&global_lock); } } } pthread_mutex_lock(&tvhdhomerun_discovery_lock); brk = 0; if (tvheadend_is_running()) { brk = tvh_cond_timedwait(&tvhdhomerun_discovery_cond, &tvhdhomerun_discovery_lock, mclk() + sec2mono(15)); brk = !ERRNO_AGAIN(brk) && brk != ETIMEDOUT; } pthread_mutex_unlock(&tvhdhomerun_discovery_lock); if (brk) break; } return NULL; }