1 | /* Copyright 2007 Alan Nisota <alannisota@gmail.com> |
---|
2 | * |
---|
3 | * This program is free software; you can redistribute it and/or |
---|
4 | * modify it under the terms of the GNU General Public License as |
---|
5 | * published by the Free Software Foundation; either version 2 of |
---|
6 | * the License, or (at your option) any later version. |
---|
7 | * |
---|
8 | * This program is distributed in the hope that it will be useful, |
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | * GNU General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <stdlib.h> |
---|
18 | #include <stdio.h> |
---|
19 | #include <sys/types.h> |
---|
20 | #include <sys/stat.h> |
---|
21 | #include <fcntl.h> |
---|
22 | #include <string.h> |
---|
23 | #include <getopt.h> |
---|
24 | #include "r5000/r5000.h" |
---|
25 | |
---|
26 | enum { |
---|
27 | TOGGLE_POWER, |
---|
28 | CHANGE_CHANNEL, |
---|
29 | READ_STREAM, |
---|
30 | MULTI_READ, |
---|
31 | MULTI_OPEN, |
---|
32 | OPEN_CLOSE, |
---|
33 | SCAN_DEVICES, |
---|
34 | MAX_CMD, |
---|
35 | }; |
---|
36 | char cmd_names[][20] = { |
---|
37 | {"TOGGLE_POWER"}, |
---|
38 | {"CHANGE_CHANNEL"}, |
---|
39 | {"READ_STREAM"}, |
---|
40 | {"MULTI_READ"}, |
---|
41 | {"MULTI_OPEN"}, |
---|
42 | {"OPEN_CLOSE"}, |
---|
43 | {"SCAN_DEVICES"}, |
---|
44 | }; |
---|
45 | |
---|
46 | char stb_names[][20] = { |
---|
47 | {"vip"}, |
---|
48 | {"directv"}, |
---|
49 | {"hdd"}, |
---|
50 | {"dsr"}, |
---|
51 | {0}, |
---|
52 | }; |
---|
53 | |
---|
54 | extern char strmfile[255]; |
---|
55 | void help(char *cmdline) { |
---|
56 | int i; |
---|
57 | printf("%s <-t type> <--m command> [--c channel] [--s serial] [-h]\n", cmdline); |
---|
58 | printf("\t-t/--type type : set STB type to type. 'type' can be either\n"); |
---|
59 | printf("\t a numerical value or string. Valid types are:\n"); |
---|
60 | for(i = 0; stb_names[i][0] != 0; i++) { |
---|
61 | printf("\t\t%d: %s\n", i, stb_names[i]); |
---|
62 | } |
---|
63 | printf("\t-m/--cmd command : set command to execute. 'command' can be either\n"); |
---|
64 | printf("\t a numerical value or string. Valid commands are:\n"); |
---|
65 | for(i = 0; i < MAX_CMD; i++) { |
---|
66 | printf("\t\t%d: %s\n", i, cmd_names[i]); |
---|
67 | } |
---|
68 | printf("\t-c/--channel : set channel to read (this does NOT tune\n"); |
---|
69 | printf("\t-s/--serial serial : set serial number\n"); |
---|
70 | printf("\t-h/--help : show this help message\n"); |
---|
71 | exit(0); |
---|
72 | } |
---|
73 | |
---|
74 | void printstr(char *str) |
---|
75 | { |
---|
76 | fprintf(stderr, "%s", str); |
---|
77 | } |
---|
78 | |
---|
79 | static unsigned char buffer[1000189]; |
---|
80 | static unsigned char *ptr = buffer; |
---|
81 | unsigned int r5000_device_tspacket_handler(unsigned char *tspacket, int len, void *callback_data) |
---|
82 | { |
---|
83 | int fd = *(int *) callback_data; |
---|
84 | if (len <= 0) |
---|
85 | return 0; |
---|
86 | if(memcpy(ptr, tspacket, len)); |
---|
87 | ptr+=len; |
---|
88 | if(ptr-buffer > 1000000) { |
---|
89 | write(fd, buffer, ptr-buffer); |
---|
90 | ptr = buffer; |
---|
91 | } |
---|
92 | return 1; |
---|
93 | } |
---|
94 | |
---|
95 | int main(int argc, char *argv[]) |
---|
96 | { |
---|
97 | r5kdev_t *usbdev = NULL; |
---|
98 | int glblfd, i, j; |
---|
99 | unsigned char buf[0x80]; |
---|
100 | int stb = -1; |
---|
101 | int cmd = -1; |
---|
102 | int channel = -1; |
---|
103 | const char *serial = NULL; |
---|
104 | int runtime = 10; |
---|
105 | struct option long_options[] = { |
---|
106 | {"type", required_argument, NULL, 't'}, |
---|
107 | {"cmd", required_argument, NULL, 'm'}, |
---|
108 | {"channel", required_argument, NULL, 'c'}, |
---|
109 | {"serial", required_argument, NULL, 's'}, |
---|
110 | {"time", required_argument, NULL, 'i'}, |
---|
111 | {"dbgfile", required_argument, NULL, 'D'}, |
---|
112 | {"help", no_argument , NULL, 'h'}, |
---|
113 | {0, 0, 0, 0} |
---|
114 | }; |
---|
115 | while (1) { |
---|
116 | char c; |
---|
117 | c = getopt_long (argc, argv, |
---|
118 | "t:m:c:s:hD:i:", |
---|
119 | long_options, NULL); |
---|
120 | if(c == EOF) |
---|
121 | break; |
---|
122 | switch(c) { |
---|
123 | case 't': |
---|
124 | { |
---|
125 | char *ptr; |
---|
126 | int i; |
---|
127 | stb = strtol(optarg, &ptr, 10); |
---|
128 | if(ptr == optarg) { |
---|
129 | stb = -1; |
---|
130 | for(i = 0; stb_names[i][0] != 0; i++) { |
---|
131 | if(strncasecmp(optarg, stb_names[i], strlen(stb_names[i])) == 0) { |
---|
132 | stb = i; |
---|
133 | break; |
---|
134 | } |
---|
135 | } |
---|
136 | if(stb == -1) { |
---|
137 | fprintf(stderr, "Unknown STB type: %s\n", optarg); |
---|
138 | exit(-1); |
---|
139 | } |
---|
140 | } else if(stb < 0 || stb >= R5K_STB_MAX) { |
---|
141 | fprintf(stderr, "Illegal STB type: %d\n", stb); |
---|
142 | exit(-1); |
---|
143 | } |
---|
144 | break; |
---|
145 | } |
---|
146 | case 'm': |
---|
147 | { |
---|
148 | char *ptr; |
---|
149 | int i; |
---|
150 | cmd = strtol(optarg, &ptr, 10); |
---|
151 | if(ptr == optarg) { |
---|
152 | cmd = -1; |
---|
153 | for(i = 0; i < MAX_CMD; i++) { |
---|
154 | if(strncasecmp(optarg, cmd_names[i], strlen(cmd_names[i])) == 0) { |
---|
155 | cmd = i; |
---|
156 | break; |
---|
157 | } |
---|
158 | } |
---|
159 | if(cmd == -1) { |
---|
160 | fprintf(stderr, "Unknown command: %d\n", optarg); |
---|
161 | exit(-1); |
---|
162 | } |
---|
163 | } else if(cmd < 0 || cmd >= MAX_CMD) { |
---|
164 | fprintf(stderr, "Illegal command: %d\n", cmd); |
---|
165 | exit(-1); |
---|
166 | } |
---|
167 | break; |
---|
168 | } |
---|
169 | case 'c': |
---|
170 | { |
---|
171 | char *ptr; |
---|
172 | channel = strtol(optarg, &ptr, 0); |
---|
173 | if(optarg == ptr) { |
---|
174 | fprintf(stderr, "Couldn't parse channel '%s'\n", optarg); |
---|
175 | } |
---|
176 | break; |
---|
177 | } |
---|
178 | case 'i': |
---|
179 | { |
---|
180 | char *ptr; |
---|
181 | runtime = strtol(optarg, &ptr, 0); |
---|
182 | if(optarg == ptr) { |
---|
183 | fprintf(stderr, "Couldn't parse time '%s'\n", optarg); |
---|
184 | } |
---|
185 | break; |
---|
186 | } |
---|
187 | case 's': |
---|
188 | serial = optarg; |
---|
189 | break; |
---|
190 | case 'h': |
---|
191 | help(argv[0]); |
---|
192 | break; |
---|
193 | case 'D': |
---|
194 | #ifdef R5K_DEBUG |
---|
195 | strncpy(strmfile, optarg, 255); |
---|
196 | #else |
---|
197 | fprintf(stderr, "--dbgfile only supported in debug mode.\nMake sure you know what you are doing!\n"); |
---|
198 | exit(-1); |
---|
199 | #endif |
---|
200 | } |
---|
201 | } |
---|
202 | if(cmd == -1 || stb == -1) { |
---|
203 | fprintf(stderr, "Must specify --cmd and --stb\n"); |
---|
204 | exit(-1); |
---|
205 | } |
---|
206 | r5000_init(printstr); |
---|
207 | if(cmd == SCAN_DEVICES) { |
---|
208 | r5kenum_t devs; |
---|
209 | printf("Scanning for R5000 devices\n"); |
---|
210 | if(! r5000_find_stbs(&devs)) { |
---|
211 | printf("Failed to initialize r5000 devices\n"); |
---|
212 | } |
---|
213 | for(i=0; i < devs.count; i++) { |
---|
214 | printf("Found: %s\n", devs.serial[i]); |
---|
215 | } |
---|
216 | goto end; |
---|
217 | } |
---|
218 | usbdev = r5000_open(stb, r5000_device_tspacket_handler, &glblfd, serial); |
---|
219 | if(! usbdev) { |
---|
220 | fprintf(stderr, "R5000 device could not be found/opened\n"); |
---|
221 | exit(-1); |
---|
222 | } |
---|
223 | glblfd=open("raw.ts", O_WRONLY | O_TRUNC | O_CREAT, 0666); |
---|
224 | if(cmd == TOGGLE_POWER) { |
---|
225 | int new_state; |
---|
226 | printf("Toggling On/Off\n"); |
---|
227 | new_state = r5000_toggle_on_off(usbdev); |
---|
228 | printf("Turned power %s\n", new_state ? "On" : "Off"); |
---|
229 | } |
---|
230 | if(cmd == CHANGE_CHANNEL) { |
---|
231 | if(channel < 0) { |
---|
232 | printf("Must specify a channel when using '-m CHANGE_CHANNEL'\n"); |
---|
233 | exit(-1); |
---|
234 | } |
---|
235 | printf("Setting channel %d\n", channel); |
---|
236 | r5000_change_channel(usbdev, channel, 0); |
---|
237 | } |
---|
238 | if(cmd == OPEN_CLOSE) { |
---|
239 | int on_off; |
---|
240 | printf("Doing open/close\n"); |
---|
241 | on_off = r5000_get_power_state(usbdev); |
---|
242 | printf("State1: %d\n", on_off); |
---|
243 | r5000_close(usbdev); |
---|
244 | printf("Closed\n"); |
---|
245 | sleep(1); |
---|
246 | usbdev = r5000_open(stb, r5000_device_tspacket_handler, &glblfd, serial); |
---|
247 | printf("ReOpened\n"); |
---|
248 | on_off = r5000_get_power_state(usbdev); |
---|
249 | printf("State2: %d\n", on_off); |
---|
250 | } |
---|
251 | if(cmd == READ_STREAM || cmd == MULTI_READ) { |
---|
252 | if(channel > 0) { |
---|
253 | printf("Setting channel %d\n"); |
---|
254 | r5000_change_channel(usbdev, 0, channel); |
---|
255 | } |
---|
256 | printf("Reading stream\n"); |
---|
257 | r5000_start_stream(usbdev); |
---|
258 | time_t t = time(NULL); |
---|
259 | while (time(NULL) - t < runtime) |
---|
260 | { |
---|
261 | // This will timeout after 1ms regardless of data availability |
---|
262 | //usleep(10000); |
---|
263 | r5000_loop_iterate(usbdev, 10); |
---|
264 | } |
---|
265 | r5000_stop_stream(usbdev); |
---|
266 | } |
---|
267 | if(cmd == MULTI_READ) { |
---|
268 | printf("Reading stream again\n"); |
---|
269 | close(glblfd); |
---|
270 | glblfd=open("raw_1.ts", O_WRONLY | O_TRUNC | O_CREAT, 0666); |
---|
271 | sleep(1); |
---|
272 | r5000_start_stream(usbdev); |
---|
273 | time_t t = time(NULL); |
---|
274 | while (time(NULL) - t < 10) |
---|
275 | { |
---|
276 | // This will timeout after 1ms regardless of data availability |
---|
277 | //usleep(10000); |
---|
278 | r5000_loop_iterate(usbdev, 10); |
---|
279 | } |
---|
280 | r5000_stop_stream(usbdev); |
---|
281 | } |
---|
282 | if(cmd == MULTI_OPEN) { |
---|
283 | printf("Doing multi-open\n"); |
---|
284 | while(usbdev) { |
---|
285 | time_t t; |
---|
286 | r5000_start_stream(usbdev); |
---|
287 | t = time(NULL); |
---|
288 | while (time(NULL) - t < 2) |
---|
289 | { |
---|
290 | // This will timeout after 1ms regardless of data availability |
---|
291 | //usleep(10000); |
---|
292 | r5000_loop_iterate(usbdev, 10); |
---|
293 | } |
---|
294 | r5000_stop_stream(usbdev); |
---|
295 | r5000_close(usbdev); |
---|
296 | printf("Closed\n"); |
---|
297 | sleep(1); |
---|
298 | usbdev = r5000_open(stb, r5000_device_tspacket_handler, &glblfd, serial); |
---|
299 | printf("ReOpened %s\n", serial); |
---|
300 | } |
---|
301 | r5000_start_stream(usbdev); |
---|
302 | time_t t = time(NULL); |
---|
303 | while (time(NULL) - t < 10) |
---|
304 | { |
---|
305 | // This will timeout after 1ms regardless of data availability |
---|
306 | //usleep(10000); |
---|
307 | r5000_loop_iterate(usbdev, 10); |
---|
308 | } |
---|
309 | r5000_stop_stream(usbdev); |
---|
310 | } |
---|
311 | end: |
---|
312 | r5000_close(usbdev); |
---|
313 | if(ptr != buffer) |
---|
314 | write(glblfd, buffer, ptr-buffer); |
---|
315 | close(glblfd); |
---|
316 | } |
---|