Ticket #2656: mythtv_hdhomerun_20061102.patch
File mythtv_hdhomerun_20061102.patch, 13.1 KB (added by , 17 years ago) |
---|
-
libs/libmythtv/hdhomerun/hdhomerun_config.c
18 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 19 */ 20 20 21 #include <stdio.h>22 21 #include "hdhomerun_os.h" 23 22 #include "hdhomerun_pkt.h" 24 23 #include "hdhomerun_discover.h" … … 33 32 printf("\t%s <id|ip> get help\n", appname); 34 33 printf("\t%s <id|ip> get <item>\n", appname); 35 34 printf("\t%s <id|ip> set <item> <value>\n", appname); 35 printf("\t%s <id|ip> scan <tuner> <starting channel>\n", appname); 36 36 printf("\t%s <id|ip> upgrade <filename>\n", appname); 37 37 return 1; 38 38 } … … 234 234 return 0; 235 235 } 236 236 237 int cmd_scan(struct hdhomerun_control_sock_t *control_sock, const char *tuner_str, const char *start_value) 238 { 239 int tuner = atoi(tuner_str); 240 241 /* Test starting channel. */ 242 char item[64]; 243 sprintf(item, "/tuner%d/channel", tuner); 244 int ret = cmd_set(control_sock, item, start_value); 245 if (ret != 0) { 246 return ret; 247 } 248 249 char channel_value[64]; 250 strncpy(channel_value, start_value, sizeof(channel_value)); 251 channel_value[sizeof(channel_value) - 8] = 0; 252 253 char *ptr = strrchr(channel_value, ':'); 254 if (!ptr) { 255 ptr = channel_value; 256 } else { 257 ptr++; 258 } 259 260 int channel = atol(ptr); 261 if (channel == 0) { 262 fprintf(stderr, "invalid starting channel\n"); 263 return 1; 264 } 265 266 while (1) { 267 /* Update channel value */ 268 sprintf(ptr, "%d", channel); 269 270 /* Set channel. */ 271 sprintf(item, "/tuner%d/channel", tuner); 272 if (hdhomerun_control_send_set_request(control_sock, item, channel_value) < 0) { 273 fprintf(stderr, "communication error sending request to hdhomerun device\n"); 274 return 1; 275 } 276 277 /* Verify set succeeded. */ 278 struct hdhomerun_control_data_t result; 279 if (hdhomerun_control_recv(control_sock, &result, 1000) <= 0) { 280 fprintf(stderr, "communication error receiving response from hdhomerun device\n"); 281 return 1; 282 } 283 if (result.type != HDHOMERUN_TYPE_GETSET_RPY) { 284 fprintf(stderr, "unexpected reply type from hdhomerun device\n"); 285 return 1; 286 } 287 while (result.ptr < result.end) { 288 unsigned char tag; 289 int length; 290 unsigned char *value; 291 if (hdhomerun_read_tlv(&result.ptr, result.end, &tag, &length, &value) < 0) { 292 break; 293 } 294 if (tag == HDHOMERUN_TAG_ERROR_MESSAGE) { 295 return 0; 296 } 297 } 298 299 /* Wait for 1s. */ 300 sleep(1); 301 302 /* Get status. */ 303 sprintf(item, "/tuner%d/status", tuner); 304 if (hdhomerun_control_send_get_request(control_sock, item) < 0) { 305 fprintf(stderr, "communication error sending request to hdhomerun device\n"); 306 return 1; 307 } 308 309 /* Status result. */ 310 if (hdhomerun_control_recv(control_sock, &result, 1000) <= 0) { 311 fprintf(stderr, "communication error receiving response from hdhomerun device\n"); 312 return 1; 313 } 314 if (result.type != HDHOMERUN_TYPE_GETSET_RPY) { 315 fprintf(stderr, "unexpected reply type from hdhomerun device\n"); 316 return 1; 317 } 318 char *status = NULL; 319 while (result.ptr < result.end) { 320 unsigned char tag; 321 int length; 322 unsigned char *value; 323 if (hdhomerun_read_tlv(&result.ptr, result.end, &tag, &length, &value) < 0) { 324 break; 325 } 326 if (tag == HDHOMERUN_TAG_ERROR_MESSAGE) { 327 return 0; 328 } 329 if (tag == HDHOMERUN_TAG_GETSET_VALUE) { 330 status = (char *)value; 331 } 332 } 333 if (!status) { 334 fprintf(stderr, "unexpected reply type from hdhomerun device\n"); 335 return 1; 336 } 337 338 /* If no signal then advance to next channel. */ 339 char *ss_str = strstr(status, "ss="); 340 if (!ss_str) { 341 printf("%s\n", status); 342 channel++; 343 continue; 344 } 345 int ss = atoi(ss_str + strlen("ss=")); 346 if (ss == 0) { 347 printf("%s\n", status); 348 channel++; 349 continue; 350 } 351 352 /* Wait for 2s. */ 353 sleep(2); 354 355 /* Display channel status. */ 356 cmd_get(control_sock, item); 357 358 /* Advance to next channel. */ 359 channel++; 360 } 361 } 362 237 363 int cmd_upgrade(struct hdhomerun_control_sock_t *control_sock, const char *filename) 238 364 { 239 365 FILE *fp = fopen(filename, "rb"); … … 272 398 return 1; 273 399 } 274 400 401 printf("upgrade complete\n"); 275 402 return 0; 276 403 } 277 404 … … 297 424 return cmd_set(control_sock, argv[0], argv[1]); 298 425 } 299 426 427 if (contains(cmd, "scan")) { 428 if (argc < 2) { 429 return help(); 430 } 431 return cmd_scan(control_sock, argv[0], argv[1]); 432 } 433 300 434 if (contains(cmd, "upgrade")) { 301 435 if (argc < 1) { 302 436 return help(); -
libs/libmythtv/hdhomerun/hdhomerun_os.h
19 19 */ 20 20 21 21 #include <stdlib.h> 22 #include <stdio.h> 22 23 #include <string.h> 23 24 #include <unistd.h> 24 25 #include <errno.h> … … 29 30 #include <netdb.h> 30 31 #include <sys/time.h> 31 32 #include <fcntl.h> 33 34 #if !defined(TRUE) 35 #define TRUE 1 36 #endif 37 #if !defined(FALSE) 38 #define FALSE 0 39 #endif -
libs/libmythtv/hdhomerun/hdhomerun_video.c
129 129 return 1; 130 130 } 131 131 132 int hdhomerun_video_get_sock(struct hdhomerun_video_sock_t *vs) 133 { 134 return vs->sock; 135 } 136 132 137 static void *hdhomerun_video_thread(void *arg) 133 138 { 134 139 struct hdhomerun_video_sock_t *vs = (struct hdhomerun_video_sock_t *)arg; … … 183 188 vs->tail = tail; 184 189 } 185 190 191 unsigned long hdhomerun_video_available_length(struct hdhomerun_video_sock_t *vs) 192 { 193 unsigned long head = vs->head; 194 unsigned long tail = vs->tail; 195 196 if (head >= tail) { 197 return head - tail - vs->advance; 198 } else { 199 return head + vs->buffer_size - tail - vs->advance; 200 } 201 } 202 186 203 unsigned long hdhomerun_video_recv_memcpy(struct hdhomerun_video_sock_t *vs, unsigned char *buffer, unsigned long size) 187 204 { 188 205 unsigned long head = vs->head; -
libs/libmythtv/hdhomerun/hdhomerun_video.h
30 30 extern void hdhomerun_video_destroy(struct hdhomerun_video_sock_t *vs); 31 31 extern unsigned short hdhomerun_video_get_local_port(struct hdhomerun_video_sock_t *vs); 32 32 extern int hdhomerun_video_get_state(struct hdhomerun_video_sock_t *vs); 33 extern int hdhomerun_video_get_sock(struct hdhomerun_video_sock_t *vs); 34 extern unsigned long hdhomerun_video_available_length(struct hdhomerun_video_sock_t *vs); 33 35 extern unsigned long hdhomerun_video_recv_memcpy(struct hdhomerun_video_sock_t *vs, unsigned char *buffer, unsigned long size); 34 36 extern unsigned char *hdhomerun_video_recv_inplace(struct hdhomerun_video_sock_t *vs, unsigned long max_size, unsigned long *pactual_size); 35 37 extern void hdhomerun_video_flush(struct hdhomerun_video_sock_t *vs); -
libs/libmythtv/hdhomerun/hdhomerun_discover.c
22 22 #include "hdhomerun_pkt.h" 23 23 #include "hdhomerun_discover.h" 24 24 25 #if defined(__CYGWIN__) 26 #include <windows.h> 27 #include <iptypes.h> 28 #include <iphlpapi.h> 29 #endif 30 25 31 struct hdhomerun_discover_sock_t { 26 32 int sock; 27 33 }; … … 71 77 free(ds); 72 78 } 73 79 74 int hdhomerun_discover_send(struct hdhomerun_discover_sock_t *ds, unsigned long device_type, unsigned long device_id)80 static int hdhomerun_discover_send_packet(struct hdhomerun_discover_sock_t *ds, unsigned long ip_addr, unsigned long device_type, unsigned long device_id) 75 81 { 76 82 unsigned char buffer[1024]; 77 83 unsigned char *ptr = buffer; … … 80 86 struct sockaddr_in sock_addr; 81 87 memset(&sock_addr, 0, sizeof(sock_addr)); 82 88 sock_addr.sin_family = AF_INET; 83 sock_addr.sin_addr.s_addr = htonl( INADDR_BROADCAST);89 sock_addr.sin_addr.s_addr = htonl(ip_addr); 84 90 sock_addr.sin_port = htons(HDHOMERUN_DISCOVER_UDP_PORT); 85 91 86 92 int length = ptr - buffer; … … 91 97 return 0; 92 98 } 93 99 100 #if defined(__CYGWIN__) 101 static int hdhomerun_discover_send_internal(struct hdhomerun_discover_sock_t *ds, unsigned long device_type, unsigned long device_id) 102 { 103 PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); 104 unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO); 105 106 DWORD Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); 107 if (Ret != NO_ERROR) { 108 free(pAdapterInfo); 109 if (Ret != ERROR_BUFFER_OVERFLOW) { 110 return -1; 111 } 112 pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen); 113 Ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); 114 if (Ret != NO_ERROR) { 115 free(pAdapterInfo); 116 return -1; 117 } 118 } 119 120 int send_count = 0; 121 PIP_ADAPTER_INFO pAdapter = pAdapterInfo; 122 while (pAdapter) { 123 IP_ADDR_STRING *pIPAddr = &pAdapter->IpAddressList; 124 while (pIPAddr) { 125 unsigned long addr = ntohl(inet_addr(pIPAddr->IpAddress.String)); 126 unsigned long mask = ntohl(inet_addr(pIPAddr->IpMask.String)); 127 128 unsigned long broadcast = addr | ~mask; 129 if ((broadcast == 0x00000000) || (broadcast == 0xFFFFFFFF)) { 130 pIPAddr = pIPAddr->Next; 131 continue; 132 } 133 134 hdhomerun_discover_send_packet(ds, broadcast, device_type, device_id); 135 send_count++; 136 137 pIPAddr = pIPAddr->Next; 138 } 139 140 pAdapter = pAdapter->Next; 141 } 142 143 free(pAdapterInfo); 144 145 if (send_count == 0) { 146 return -1; 147 } 148 return 0; 149 } 150 #endif 151 152 #if defined(__linux__) 153 static int hdhomerun_discover_send_internal(struct hdhomerun_discover_sock_t *ds, unsigned long device_type, unsigned long device_id) 154 { 155 FILE *fp = fopen("/proc/net/route", "r"); 156 if (!fp) { 157 return -1; 158 } 159 160 int send_count = 0; 161 while (1) { 162 char line[256]; 163 if (!fgets(line, sizeof(line), fp)) { 164 break; 165 } 166 line[255] = 0; 167 168 unsigned long dest; 169 unsigned long mask; 170 if (sscanf(line, "%*s %lx %*x %*x %*d %*d %*d %lx", &dest, &mask) != 2) { 171 continue; 172 } 173 dest = ntohl(dest); 174 mask = ntohl(mask); 175 176 unsigned long broadcast = dest | ~mask; 177 178 if ((broadcast == 0x00000000) || (broadcast == 0xFFFFFFFF)) { 179 continue; 180 } 181 182 hdhomerun_discover_send_packet(ds, broadcast, device_type, device_id); 183 send_count++; 184 } 185 186 fclose(fp); 187 if (send_count == 0) { 188 return -1; 189 } 190 return 0; 191 } 192 #endif 193 194 #if !defined(__CYGWIN__) && !defined(__linux__) 195 static int hdhomerun_discover_send_internal(struct hdhomerun_discover_sock_t *ds, unsigned long device_type, unsigned long device_id) 196 { 197 return -1; 198 } 199 #endif 200 201 int hdhomerun_discover_send(struct hdhomerun_discover_sock_t *ds, unsigned long device_type, unsigned long device_id) 202 { 203 if (hdhomerun_discover_send_internal(ds, device_type, device_id) < 0) { 204 return hdhomerun_discover_send_packet(ds, 0xFFFFFFFF, device_type, device_id); 205 } 206 return 0; 207 } 208 94 209 int hdhomerun_discover_recv(struct hdhomerun_discover_sock_t *ds, struct hdhomerun_discover_device_t *result, unsigned long timeout) 95 210 { 96 211 struct timeval t; -
libs/libmythtv/hdhomerun/hdhomerun_pkt.c
182 182 hdhomerun_write_u16(&ptr, length); 183 183 } 184 184 185 staticvoid hdhomerun_write_crc(unsigned char **pptr, unsigned char *start)185 void hdhomerun_write_crc(unsigned char **pptr, unsigned char *start) 186 186 { 187 187 unsigned char *ptr = *pptr; 188 188 unsigned long crc = hdhomerun_calc_crc(start, ptr); -
libs/libmythtv/hdhomerun/hdhomerun_pkt.h
49 49 extern void hdhomerun_write_u8(unsigned char **pptr, unsigned char v); 50 50 extern void hdhomerun_write_u16(unsigned char **pptr, unsigned short v); 51 51 extern void hdhomerun_write_u32(unsigned char **pptr, unsigned long v); 52 extern void hdhomerun_write_crc(unsigned char **pptr, unsigned char *start); 52 53 53 54 extern int hdhomerun_peek_packet_length(unsigned char *ptr); 54 55 extern int hdhomerun_process_packet(unsigned char **pptr, unsigned char **pend); -
libs/libmythtv/hdhomerun/Makefile
1 1 2 HDHOMERUN_CONFIG_SRCS += hdhomerun_pkt.c3 HDHOMERUN_CONFIG_SRCS += hdhomerun_discover.c4 HDHOMERUN_CONFIG_SRCS += hdhomerun_control.c5 HDHOMERUN_CONFIG_SRCS += hdhomerun_config.c2 SRCS += hdhomerun_pkt.c 3 SRCS += hdhomerun_discover.c 4 SRCS += hdhomerun_control.c 5 SRCS += hdhomerun_config.c 6 6 7 7 CFLAGS += -Wall -O2 8 8 9 hdhomerun_config : $( HDHOMERUN_CONFIG_SRCS)10 gcc $(CFLAGS) $( HDHOMERUN_CONFIG_SRCS) -o hdhomerun_config11 strip hdhomerun_config9 hdhomerun_config : $(SRCS) 10 gcc $(CFLAGS) $(SRCS) -o $@ 11 strip $@ 12 12 13 hdhomerun_config.exe : $(SRCS) 14 gcc $(CFLAGS) $(SRCS) -liphlpapi -o $@ 15 strip $@ 16 13 17 clean : 14 18 rm -f hdhomerun_config 19 rm -f hdhomerun_config.exe