1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <sys/stat.h> |
---|
5 | #include <fcntl.h> |
---|
6 | #include <string.h> |
---|
7 | #include <getopt.h> |
---|
8 | #include "r5000/r5000.h" |
---|
9 | |
---|
10 | enum { |
---|
11 | TOGGLE_POWER, |
---|
12 | CHANGE_CHANNEL, |
---|
13 | READ_STREAM, |
---|
14 | OPEN_CLOSE, |
---|
15 | SCAN_DEVICES, |
---|
16 | MAX_CMD, |
---|
17 | }; |
---|
18 | char cmd_names[][20] = { |
---|
19 | {"TOGGLE_POWER"}, |
---|
20 | {"CHANGE_CHANNEL"}, |
---|
21 | {"READ_STREAM"}, |
---|
22 | {"OPEN_CLOSE"}, |
---|
23 | {"SCAN_DEVICES"}, |
---|
24 | }; |
---|
25 | |
---|
26 | char stb_names[][20] = { |
---|
27 | {"vip"}, |
---|
28 | {"directv"}, |
---|
29 | {"hdd"}, |
---|
30 | {"dsr"}, |
---|
31 | {0}, |
---|
32 | }; |
---|
33 | |
---|
34 | extern char strmfile[255]; |
---|
35 | void help(char *cmdline) { |
---|
36 | int i; |
---|
37 | printf("%s <-t type> <--m command> [--c channel] [--s serial] [-h]\n", cmdline); |
---|
38 | printf("\t-t/--type type : set STB type to type. 'type' can be either\n"); |
---|
39 | printf("\t a numerical value or string. Valid types are:\n"); |
---|
40 | for(i = 0; stb_names[i][0] != 0; i++) { |
---|
41 | printf("\t\t%d: %s\n", i, stb_names[i]); |
---|
42 | } |
---|
43 | printf("\t-m/--cmd command : set command to execute. 'command' can be either\n"); |
---|
44 | printf("\t a numerical value or string. Valid commands are:\n"); |
---|
45 | for(i = 0; i < MAX_CMD; i++) { |
---|
46 | printf("\t\t%d: %s\n", i, cmd_names[i]); |
---|
47 | } |
---|
48 | printf("\t-c/--channel : set channel to read (this does NOT tune\n"); |
---|
49 | printf("\t-s/--serial serial : set serial number\n"); |
---|
50 | printf("\t-h/--help : show this help message\n"); |
---|
51 | exit(0); |
---|
52 | } |
---|
53 | |
---|
54 | static unsigned char buffer[1000189]; |
---|
55 | static unsigned char *ptr = buffer; |
---|
56 | unsigned int r5000_device_tspacket_handler(unsigned char *tspacket, int len, void *callback_data) |
---|
57 | { |
---|
58 | int fd = *(int *) callback_data; |
---|
59 | if (len <= 0) |
---|
60 | return 0; |
---|
61 | if(memcpy(ptr, tspacket, len)); |
---|
62 | ptr+=len; |
---|
63 | if(ptr-buffer > 1000000) { |
---|
64 | write(fd, buffer, ptr-buffer); |
---|
65 | ptr = buffer; |
---|
66 | } |
---|
67 | return 1; |
---|
68 | } |
---|
69 | |
---|
70 | int main(int argc, char *argv[]) |
---|
71 | { |
---|
72 | r5kdev_t *usbdev = NULL; |
---|
73 | int glblfd, i, j; |
---|
74 | unsigned char buf[0x80]; |
---|
75 | int stb = -1; |
---|
76 | int cmd = -1; |
---|
77 | int channel = -1; |
---|
78 | const char *serial = NULL; |
---|
79 | struct option long_options[] = { |
---|
80 | {"type", required_argument, NULL, 't'}, |
---|
81 | {"cmd", required_argument, NULL, 'm'}, |
---|
82 | {"channel", required_argument, NULL, 'c'}, |
---|
83 | {"serial", required_argument, NULL, 's'}, |
---|
84 | {"dbgfile", required_argument, NULL, 'D'}, |
---|
85 | {"help", no_argument , NULL, 'h'}, |
---|
86 | {0, 0, 0, 0} |
---|
87 | }; |
---|
88 | while (1) { |
---|
89 | char c; |
---|
90 | c = getopt_long (argc, argv, |
---|
91 | "t:m:c:s:hD:", |
---|
92 | long_options, NULL); |
---|
93 | if(c == EOF) |
---|
94 | break; |
---|
95 | switch(c) { |
---|
96 | case 't': |
---|
97 | { |
---|
98 | char *ptr; |
---|
99 | int i; |
---|
100 | stb = strtol(optarg, &ptr, 10); |
---|
101 | if(ptr == optarg) { |
---|
102 | stb = -1; |
---|
103 | for(i = 0; stb_names[i][0] != 0; i++) { |
---|
104 | if(strncasecmp(optarg, stb_names[i], strlen(stb_names[i])) == 0) { |
---|
105 | stb = i; |
---|
106 | break; |
---|
107 | } |
---|
108 | } |
---|
109 | if(stb == -1) { |
---|
110 | fprintf(stderr, "Unknown STB type: %s\n", optarg); |
---|
111 | exit(-1); |
---|
112 | } |
---|
113 | } else if(stb < 0 || stb >= R5K_STB_MAX) { |
---|
114 | fprintf(stderr, "Illegal STB type: %d\n", stb); |
---|
115 | exit(-1); |
---|
116 | } |
---|
117 | break; |
---|
118 | } |
---|
119 | case 'm': |
---|
120 | { |
---|
121 | char *ptr; |
---|
122 | int i; |
---|
123 | cmd = strtol(optarg, &ptr, 10); |
---|
124 | if(ptr == optarg) { |
---|
125 | cmd = -1; |
---|
126 | for(i = 0; i < MAX_CMD; i++) { |
---|
127 | if(strncasecmp(optarg, cmd_names[i], strlen(cmd_names[i])) == 0) { |
---|
128 | cmd = i; |
---|
129 | break; |
---|
130 | } |
---|
131 | } |
---|
132 | if(cmd == -1) { |
---|
133 | fprintf(stderr, "Unknown command: %d\n", optarg); |
---|
134 | exit(-1); |
---|
135 | } |
---|
136 | } else if(cmd < 0 || cmd >= MAX_CMD) { |
---|
137 | fprintf(stderr, "Illegal command: %d\n", cmd); |
---|
138 | exit(-1); |
---|
139 | } |
---|
140 | break; |
---|
141 | } |
---|
142 | case 'c': |
---|
143 | { |
---|
144 | char *ptr; |
---|
145 | channel = strtol(optarg, &ptr, 0); |
---|
146 | if(optarg == ptr) { |
---|
147 | fprintf(stderr, "Couldn't parse channel '%s'\n", optarg); |
---|
148 | } |
---|
149 | break; |
---|
150 | } |
---|
151 | case 's': |
---|
152 | serial = optarg; |
---|
153 | break; |
---|
154 | case 'h': |
---|
155 | help(argv[0]); |
---|
156 | break; |
---|
157 | case 'D': |
---|
158 | #ifdef R5K_DEBUG |
---|
159 | strncpy(strmfile, optarg, 255); |
---|
160 | #else |
---|
161 | fprintf(stderr, "--dbgfile only supported in debug mode.\nMake sure you know what you are doing!\n"); |
---|
162 | exit(-1); |
---|
163 | #endif |
---|
164 | } |
---|
165 | } |
---|
166 | if(cmd == -1 || stb == -1) { |
---|
167 | fprintf(stderr, "Must specify --cmd and --stb\n"); |
---|
168 | exit(-1); |
---|
169 | } |
---|
170 | if(cmd == SCAN_DEVICES) { |
---|
171 | r5kenum_t devs; |
---|
172 | printf("Scanning for R5000 devices\n"); |
---|
173 | if(! r5000_find_stbs(&devs)) { |
---|
174 | printf("Failed to initialize r5000 devices\n"); |
---|
175 | } |
---|
176 | for(i=0; i < devs.count; i++) { |
---|
177 | printf("Found: %s\n", devs.serial[i]); |
---|
178 | } |
---|
179 | goto end; |
---|
180 | } |
---|
181 | usbdev = r5000_open(stb, r5000_device_tspacket_handler, &glblfd, serial); |
---|
182 | if(! usbdev) { |
---|
183 | fprintf(stderr, "R5000 device could not be found/opened\n"); |
---|
184 | exit(-1); |
---|
185 | } |
---|
186 | glblfd=open("raw.ts", O_WRONLY | O_TRUNC | O_CREAT, 0666); |
---|
187 | if(cmd == TOGGLE_POWER) { |
---|
188 | int new_state; |
---|
189 | printf("Toggling On/Off\n"); |
---|
190 | new_state = r5000_toggle_on_off(usbdev); |
---|
191 | printf("Turned power %s\n", new_state ? "On" : "Off"); |
---|
192 | } |
---|
193 | if(cmd == CHANGE_CHANNEL) { |
---|
194 | if(channel < 0) { |
---|
195 | printf("Must specify a channel when using '-m CHANGE_CHANNEL'\n"); |
---|
196 | exit(-1); |
---|
197 | } |
---|
198 | printf("Setting channel %d\n", channel); |
---|
199 | r5000_change_channel(usbdev, channel); |
---|
200 | } |
---|
201 | if(cmd == OPEN_CLOSE) { |
---|
202 | int on_off; |
---|
203 | printf("Doing open/close\n"); |
---|
204 | on_off = r5000_get_power_state(usbdev); |
---|
205 | printf("State1: %d\n", on_off); |
---|
206 | r5000_close(usbdev); |
---|
207 | printf("Closed\n"); |
---|
208 | sleep(2); |
---|
209 | usbdev = r5000_open(stb, r5000_device_tspacket_handler, &glblfd, serial); |
---|
210 | printf("ReOpened\n"); |
---|
211 | on_off = r5000_get_power_state(usbdev); |
---|
212 | printf("State2: %d\n", on_off); |
---|
213 | } |
---|
214 | if(cmd == READ_STREAM) { |
---|
215 | printf("Reading stream\n"); |
---|
216 | r5000_start_stream(usbdev); |
---|
217 | time_t t = time(NULL); |
---|
218 | while (time(NULL) - t < 10) |
---|
219 | { |
---|
220 | // This will timeout after 1ms regardless of data availability |
---|
221 | //usleep(10000); |
---|
222 | r5000_loop_iterate(usbdev, 10); |
---|
223 | } |
---|
224 | r5000_stop_stream(usbdev); |
---|
225 | } |
---|
226 | end: |
---|
227 | r5000_close(usbdev); |
---|
228 | if(ptr != buffer) |
---|
229 | write(glblfd, buffer, ptr-buffer); |
---|
230 | close(glblfd); |
---|
231 | } |
---|