Ticket #7671: sa_control.2.cc

File sa_control.2.cc, 16.7 KB (added by mpurschke@…, 16 years ago)

new source file, replaces the old one.

Line 
1/*
2 * sa_control - control for Scientific Atlanta Cable Boxes, including
3 * the 4250 HDC which is unsupported in sa3250ch.c
4 * Based on sa3250ch.c, firewiredevice.{h.cpp}, and linuxavcinfo.{h,cpp}
5 *
6 * Martin Purschke ( mpurschke (at) gmail.com ) 11/28/2009
7 *
8 * allows to change the channel, query the power status, and turn
9 * the power on and off
10 *
11 * Usage: sa_control [-v] [-q] [ channel | on | off ]
12 * -h : this help
13 * -v : verbose, multiple -v for more verbosity
14 * -q : query power status, also sets exit status
15 * <channel> : set channel, implies power on
16 * -w <ms> : after a channel change, wait so many
17 * more milliseconds to allow the box time to tune
18 * on : switch on
19 * off : switch off
20 * Examples:
21 * sa_control -q
22 * sa_control -q -v
23 * sa_control -q -v -v
24 * sa_control 702
25 * sa_control -w 6000 702
26 * sa_control off
27 *
28 * Build it with
29 * g++ -o sa_control sa_control.cc -lrom1394 -lavc1394 -lraw1394
30 *
31 * This program is free software; you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation; either version 2 of the License, or
34 * (at your option) any later version.
35 *
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License
42 * along with this program; if not, write to the Free Software Foundation,
43 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 */
45
46#include <libavc1394/rom1394.h>
47#include <libavc1394/avc1394.h>
48#include <libraw1394/raw1394.h>
49#include <sys/types.h>
50#include <getopt.h>
51#include <errno.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include <iostream>
56#include <iomanip>
57#include <vector>
58
59using namespace std;
60
61#define COMMAND_CHANNEL 1
62#define COMMAND_POWER 2
63#define COMMAND_QUERY 3
64
65typedef enum
66 {
67 kAVCPowerOn,
68 kAVCPowerOff,
69 kAVCPowerUnknown,
70 kAVCPowerQueryFailed,
71 } PowerState;
72
73// AVC commands
74typedef enum
75 {
76 kAVCControlCommand = 0x00,
77 kAVCStatusInquiryCommand = 0x01,
78 kAVCSpecificInquiryCommand = 0x02,
79 kAVCNotifyCommand = 0x03,
80 kAVCGeneralInquiryCommand = 0x04,
81
82 kAVCNotImplementedStatus = 0x08,
83 kAVCAcceptedStatus = 0x09,
84 kAVCRejectedStatus = 0x0a,
85 kAVCInTransitionStatus = 0x0b,
86 kAVCImplementedStatus = 0x0c,
87 kAVCChangedStatus = 0x0d,
88
89 kAVCInterimStatus = 0x0f,
90 kAVCResponseImplemented = 0x0c,
91 } IEEE1394Command;
92
93// AVC unit addresses
94typedef enum
95 {
96 kAVCSubunitId0 = 0x00,
97 kAVCSubunitId1 = 0x01,
98 kAVCSubunitId2 = 0x02,
99 kAVCSubunitId3 = 0x03,
100 kAVCSubunitId4 = 0x04,
101 kAVCSubunitIdExtended = 0x05,
102 kAVCSubunitIdIgnore = 0x07,
103
104 kAVCSubunitTypeVideoMonitor = (0x00 << 3),
105 kAVCSubunitTypeAudio = (0x01 << 3),
106 kAVCSubunitTypePrinter = (0x02 << 3),
107 kAVCSubunitTypeDiscRecorder = (0x03 << 3),
108 kAVCSubunitTypeTapeRecorder = (0x04 << 3),
109 kAVCSubunitTypeTuner = (0x05 << 3),
110 kAVCSubunitTypeCA = (0x06 << 3),
111 kAVCSubunitTypeVideoCamera = (0x07 << 3),
112 kAVCSubunitTypePanel = (0x09 << 3),
113 kAVCSubunitTypeBulletinBoard = (0x0a << 3),
114 kAVCSubunitTypeCameraStorage = (0x0b << 3),
115 kAVCSubunitTypeMusic = (0x0c << 3),
116 kAVCSubunitTypeVendorUnique = (0x1c << 3),
117 kAVCSubunitTypeExtended = (0x1e << 3),
118 kAVCSubunitTypeUnit = (0x1f << 3),
119 } IEEE1394UnitAddress;
120
121// AVC opcode
122typedef enum
123 {
124 // Unit
125 kAVCUnitPlugInfoOpcode = 0x02,
126 kAVCUnitDigitalOutputOpcode = 0x10,
127 kAVCUnitDigitalInputOpcode = 0x11,
128 kAVCUnitChannelUsageOpcode = 0x12,
129 kAVCUnitOutputPlugSignalFormatOpcode = 0x18,
130 kAVCUnitInputPlugSignalFormatOpcode = 0x19,
131 kAVCUnitConnectAVOpcode = 0x20,
132 kAVCUnitDisconnectAVOpcode = 0x21,
133 kAVCUnitConnectionsOpcode = 0x22,
134 kAVCUnitConnectOpcode = 0x24,
135 kAVCUnitDisconnectOpcode = 0x25,
136 kAVCUnitUnitInfoOpcode = 0x30,
137 kAVCUnitSubunitInfoOpcode = 0x31,
138 kAVCUnitSignalSourceOpcode = 0x1a,
139 kAVCUnitPowerOpcode = 0xb2,
140
141 // Common Unit + Subunit
142 kAVCCommonOpenDescriptorOpcode = 0x08,
143 kAVCCommonReadDescriptorOpcode = 0x09,
144 kAVCCommonWriteDescriptorOpcode = 0x0A,
145 kAVCCommonSearchDescriptorOpcode = 0x0B,
146 kAVCCommonObjectNumberSelectOpcode = 0x0D,
147 kAVCCommonPowerOpcode = 0xB2,
148 kAVCCommonReserveOpcode = 0x01,
149 kAVCCommonPlugInfoOpcode = 0x02,
150 kAVCCommonVendorDependentOpcode = 0x00,
151
152 // Panel
153 kAVCPanelPassThrough = 0x7c,
154 } IEEE1394Opcode;
155
156// AVC param 0
157typedef enum
158 {
159 kAVCPowerStateOn = 0x70,
160 kAVCPowerStateOff = 0x60,
161 kAVCPowerStateQuery = 0x7f,
162 } IEEE1394UnitPowerParam0;
163
164typedef enum
165 {
166 kAVCPanelKeySelect = 0x00,
167 kAVCPanelKeyUp = 0x01,
168 kAVCPanelKeyDown = 0x02,
169 kAVCPanelKeyLeft = 0x03,
170 kAVCPanelKeyRight = 0x04,
171 kAVCPanelKeyRightUp = 0x05,
172 kAVCPanelKeyRightDown = 0x06,
173 kAVCPanelKeyLeftUp = 0x07,
174 kAVCPanelKeyLeftDown = 0x08,
175 kAVCPanelKeyRootMenu = 0x09,
176 kAVCPanelKeySetupMenu = 0x0A,
177 kAVCPanelKeyContentsMenu = 0x0B,
178 kAVCPanelKeyFavoriteMenu = 0x0C,
179 kAVCPanelKeyExit = 0x0D,
180
181 kAVCPanelKey0 = 0x20,
182 kAVCPanelKey1 = 0x21,
183 kAVCPanelKey2 = 0x22,
184 kAVCPanelKey3 = 0x23,
185 kAVCPanelKey4 = 0x24,
186 kAVCPanelKey5 = 0x25,
187 kAVCPanelKey6 = 0x26,
188 kAVCPanelKey7 = 0x27,
189 kAVCPanelKey8 = 0x28,
190 kAVCPanelKey9 = 0x29,
191 kAVCPanelKeyDot = 0x2A,
192 kAVCPanelKeyEnter = 0x2B,
193 kAVCPanelKeyClear = 0x2C,
194
195 kAVCPanelKeyChannelUp = 0x30,
196 kAVCPanelKeyChannelDown = 0x31,
197 kAVCPanelKeyPreviousChannel = 0x32,
198 kAVCPanelKeySoundSelect = 0x33,
199 kAVCPanelKeyInputSelect = 0x34,
200 kAVCPanelKeyDisplayInfo = 0x35,
201 kAVCPanelKeyHelp = 0x36,
202 kAVCPanelKeyPageUp = 0x37,
203 kAVCPanelKeyPageDown = 0x38,
204
205 kAVCPanelKeyPower = 0x40,
206 kAVCPanelKeyVolumeUp = 0x41,
207 kAVCPanelKeyVolumeDown = 0x42,
208 kAVCPanelKeyMute = 0x43,
209 kAVCPanelKeyPlay = 0x44,
210 kAVCPanelKeyStop = 0x45,
211 kAVCPanelKeyPause = 0x46,
212 kAVCPanelKeyRecord = 0x47,
213 kAVCPanelKeyRewind = 0x48,
214 kAVCPanelKeyFastForward = 0x49,
215 kAVCPanelKeyEject = 0x4a,
216 kAVCPanelKeyForward = 0x4b,
217 kAVCPanelKeyBackward = 0x4c,
218
219 kAVCPanelKeyAngle = 0x50,
220 kAVCPanelKeySubPicture = 0x51,
221
222 kAVCPanelKeyTuneFunction = 0x67,
223
224 kAVCPanelKeyPress = 0x00,
225 kAVCPanelKeyRelease = 0x80,
226
227 } IEEE1394PanelPassThroughParam0;
228
229
230
231
232
233/* SA3250HD IDs */
234/* WARNING: Please update firewiredevice.cpp when adding to this list. */
235
236#define SA_VENDOR_ID1 0x00000a73
237#define SA_VENDOR_ID2 0x00000f21
238#define SA_VENDOR_ID3 0x000011e6
239#define SA_VENDOR_ID4 0x000014f8
240#define SA_VENDOR_ID5 0x00001692
241#define SA_VENDOR_ID6 0x00001868
242#define SA_VENDOR_ID7 0x00001947
243#define SA_VENDOR_ID8 0x00001ac3
244#define SA_VENDOR_ID9 0x00001bd7
245#define SA_VENDOR_ID10 0x00001cea
246#define SA_VENDOR_ID11 0x00001e6b
247#define SA_VENDOR_ID12 0x000021be
248#define SA_VENDOR_ID13 0x0000223a
249#define SA_VENDOR_ID14 0x000022ce
250#define SA_VENDOR_ID15 0x000023be
251#define SA_VENDOR_ID16 0x0000252e
252
253#define SA3250HD_MODEL_ID1 0x00000be0
254#define SA4200HD_MODEL_ID1 0x00001072
255#define SA4250HDC_MODEL_ID1 0x000010cc
256#define SA8300HD_MODEL_ID1 0x000022ce
257
258
259#define STARTING_NODE 0
260
261int verbose = 0;
262
263void exitmsg()
264{
265 cout << "Usage: sa_control [-v] [-q] [-w <ms>] [ <channel> | on | off ]" << endl;
266 cout << " sa_control -h for help" << endl;
267 exit(1);
268}
269
270void exithelp()
271{
272 cout << "Usage: sa_control [-v] [-q] [-w <ms> [ <channel> | on | off ]" << endl;
273 cout << " -h : this help" << endl;
274 cout << " -v : verbose, multiple -v for more verbosity" << endl;
275 cout << " -q : query power status, also sets exit status" << endl;
276 cout << " <channel> : set channel, implies power on" << endl;
277 cout << " -w <ms> : after a channel change, wait so many " << endl;
278 cout << " more milliseconds to allow the box time to tune" << endl;
279 cout << " on : switch on" << endl;
280 cout << " off : switch off" << endl;
281 cout << "Examples:" << endl;
282 cout << " sa_control -q" << endl;
283 cout << " sa_control 702" << endl;
284 cout << " sa_control -w 6000 702" << endl;
285 cout << " sa_control off" << endl;
286 exit(0);
287}
288
289
290// adapted from linuxavcinfo.cpp
291
292int SendAVCCommand(const vector<uint8_t> &_cmd,
293 vector<uint8_t> &result,
294 int retry_cnt,
295 raw1394handle_t &fw_handle
296 )
297{
298 retry_cnt = (retry_cnt < 0) ? 2 : retry_cnt;
299
300 result.clear();
301
302 int node =1;
303
304 vector<uint8_t> cmd = _cmd;
305 while (cmd.size() & 0x3)
306 {
307 cmd.push_back(0x00);
308 }
309
310 if (cmd.size() > 4096)
311 return 1;
312
313 uint32_t cmdbuf[1024];
314 if (verbose >2)
315 {
316 for (uint i = 0; i < cmd.size(); i++)
317 {
318 std::cout << "avc command " << setw(3)<< i << " 0x" << hex << (unsigned int) cmd[i] << dec << std::endl;
319 }
320 }
321
322 for (uint i = 0; i < cmd.size(); i+=4)
323 {
324 cmdbuf[i>>2] = cmd[i]<<24 | cmd[i+1]<<16 | cmd[i+2]<<8 | cmd[i+3];
325 }
326 uint result_length = 0;
327
328
329 uint32_t *ret = avc1394_transaction_block(fw_handle, node, cmdbuf, cmd.size() >> 2, retry_cnt);
330 result_length = cmd.size() >> 2;
331
332 if (!ret)
333 return 1;
334
335 for (uint i = 0; i < result_length; i++)
336 {
337 result.push_back((ret[i]>>24) & 0xff);
338 result.push_back((ret[i]>>16) & 0xff);
339 result.push_back((ret[i]>>8) & 0xff);
340 result.push_back((ret[i]) & 0xff);
341 }
342
343 avc1394_transaction_block_close(fw_handle);
344
345 return 0;
346}
347
348
349
350
351int SetPower (const int onoff, raw1394handle_t &handle)
352{
353 vector<uint8_t> cmd;
354 vector<uint8_t> ret;
355
356 cmd.push_back(kAVCControlCommand);
357 cmd.push_back(kAVCSubunitTypeUnit | kAVCSubunitIdIgnore);
358 cmd.push_back(kAVCUnitPowerOpcode);
359 cmd.push_back((onoff) ? kAVCPowerStateOn : kAVCPowerStateOff);
360
361
362 if (SendAVCCommand(cmd, ret, -1, handle))
363 {
364 std::cout << "Power command failed (no response)" << std::endl;
365 return -1;
366 }
367
368 return 0;
369}
370
371int QueryPower(raw1394handle_t &handle)
372{
373
374 vector<uint8_t> cmd;
375 vector<uint8_t> ret;
376
377 cmd.push_back(kAVCStatusInquiryCommand);
378 cmd.push_back(kAVCSubunitTypeUnit | kAVCSubunitIdIgnore);
379 cmd.push_back(kAVCUnitPowerOpcode);
380 cmd.push_back(kAVCPowerStateQuery);
381
382 if (SendAVCCommand(cmd, ret, -1, handle))
383 {
384 return -1;
385 }
386
387 if (ret[0] != kAVCResponseImplemented)
388 {
389 return -2;
390 }
391
392 // check 1st operand..
393 if (ret[3] == kAVCPowerStateOn)
394 {
395 return 1;
396 }
397
398 if (ret[3] == kAVCPowerStateOff)
399 {
400 return 0;
401 }
402
403 return -3;
404
405}
406
407int ChangeChannel(const int channelnumber, raw1394handle_t &handle)
408{
409
410 // power on if off
411 if ( !QueryPower(handle) )
412 {
413 SetPower(1,handle);
414 sleep(1);
415 }
416
417 vector<uint8_t> cmd;
418 vector<uint8_t> ret;
419
420 cmd.push_back(kAVCControlCommand);
421 cmd.push_back(kAVCSubunitTypePanel);
422 cmd.push_back(kAVCPanelPassThrough);
423 cmd.push_back(kAVCPanelKeyTuneFunction | kAVCPanelKeyPress);
424 cmd.push_back(4); // operand length
425 cmd.push_back((channelnumber>>8) & 0x0f);
426 cmd.push_back(channelnumber & 0xff);
427 cmd.push_back(0x00);
428 cmd.push_back(0x00);
429
430 if (SendAVCCommand(cmd, ret, -1, handle))
431 {
432 std::cout << "Channel change command failed (no response)" << std::endl;
433 return -1;
434 }
435
436 return 0;
437}
438
439
440int main (int argc, char *argv[])
441{
442 rom1394_directory dir;
443 int device = -1;
444 int single = 0;
445 int i;
446 int channelnumber;
447 int onoff;
448 int sleeptime = 0;
449
450 if (argc < 2)
451 exitmsg();
452
453 extern char *optarg;
454 extern int optind;
455
456 int main_command = COMMAND_CHANNEL; // assume our job is to change a channel
457
458 char c;
459 while ((c = getopt(argc, argv, "vqhw:")) != EOF)
460 {
461 switch (c)
462 {
463
464 case 'h':
465 exithelp();
466 break;
467
468 case 'w':
469 if ( !sscanf(optarg, "%d", &sleeptime) ) exitmsg();
470 break;
471
472 case 'q': // query
473 main_command =COMMAND_QUERY; // job is to query the power state
474 break;
475
476 case 'v': // verbose
477 verbose++;
478 break;
479 }
480 }
481
482 if ( main_command != COMMAND_QUERY )
483 {
484 if ( optind >= argc) exitmsg();
485 if ( strcmp ( argv[optind], "on") ==0 )
486 {
487 onoff =1;
488 main_command =COMMAND_POWER; // job is to power on or off
489 }
490 else if (strcmp ( argv[optind], "off") ==0 )
491 {
492 onoff =0;
493 main_command =COMMAND_POWER; // job is to power on or off
494 }
495 else
496 {
497 if ( !sscanf(argv[optind], "%d", &channelnumber) ) exitmsg();
498 main_command =COMMAND_CHANNEL; // job is to switch channels
499 }
500 }
501
502 raw1394handle_t handle = raw1394_new_handle();
503
504 if (!handle)
505 {
506 if (!errno)
507 {
508 fprintf(stderr, "Not Compatible!\n");
509 }
510 else
511 {
512 perror("Couldn't get 1394 handle");
513 fprintf(stderr, "Is ieee1394, driver, and raw1394 loaded?\n");
514 }
515 exit(1);
516 }
517
518
519 if (raw1394_set_port(handle, 0) < 0)
520 {
521 perror("couldn't set port");
522 raw1394_destroy_handle(handle);
523 exit(1);
524 }
525
526 int nc = raw1394_get_nodecount(handle);
527 for (i=STARTING_NODE; i < nc; ++i)
528 {
529 if (rom1394_get_directory(handle, i, &dir) < 0)
530 {
531 fprintf(stderr,"error reading config rom directory for node %d\n", i);
532 raw1394_destroy_handle(handle);
533 exit(1);
534 }
535
536 /* WARNING: Please update firewiredevice.cpp when adding to this list. */
537 if (((dir.vendor_id == SA_VENDOR_ID1) ||
538 (dir.vendor_id == SA_VENDOR_ID2) ||
539 (dir.vendor_id == SA_VENDOR_ID3) ||
540 (dir.vendor_id == SA_VENDOR_ID4) ||
541 (dir.vendor_id == SA_VENDOR_ID5) ||
542 (dir.vendor_id == SA_VENDOR_ID6) ||
543 (dir.vendor_id == SA_VENDOR_ID7) ||
544 (dir.vendor_id == SA_VENDOR_ID8) ||
545 (dir.vendor_id == SA_VENDOR_ID9) ||
546 (dir.vendor_id == SA_VENDOR_ID10) ||
547 (dir.vendor_id == SA_VENDOR_ID11) ||
548 (dir.vendor_id == SA_VENDOR_ID12) ||
549 (dir.vendor_id == SA_VENDOR_ID13) ||
550 (dir.vendor_id == SA_VENDOR_ID14) ||
551 (dir.vendor_id == SA_VENDOR_ID15) ||
552 (dir.vendor_id == SA_VENDOR_ID16)) &&
553 ((dir.model_id == SA3250HD_MODEL_ID1) ||
554 (dir.model_id == SA4200HD_MODEL_ID1) ||
555 (dir.model_id == SA4250HDC_MODEL_ID1) ||
556 (dir.model_id == SA8300HD_MODEL_ID1)))
557 {
558 if (verbose > 1)
559 {
560 cout << "Vendor id = 0x" << hex << dir.vendor_id << " Model id = 0x" << dir.model_id << dec << endl;
561 }
562
563 device = i;
564 break;
565 }
566 }
567
568
569 if (device == -1)
570 {
571 cout << "Could not find a cable box on bus" << endl;
572 raw1394_destroy_handle(handle);
573 return 1;
574 }
575
576 int returnvalue = 0;
577
578 switch ( main_command)
579 {
580
581 case COMMAND_CHANNEL: // change channel
582 if (verbose)
583 {
584 cout << "Setting channel to " << channelnumber << endl;
585 }
586 returnvalue = ChangeChannel(channelnumber, handle);
587 // if so specified, sleep the inicated time
588 if (sleeptime > 0) usleep ( sleeptime * 1000);
589 break;
590
591 case COMMAND_POWER: // change channel
592 if (verbose)
593 {
594 cout << "Turning power " << ( (onoff) ? "on" : "off" ) << endl;
595 }
596 returnvalue = SetPower(onoff, handle);
597 break;
598
599 case COMMAND_QUERY: // change channel
600 int status = QueryPower(handle);
601 if ( status < 0)
602 {
603 cout << "Error executing query" << endl;
604 exit(1);
605 }
606
607 if (verbose)
608 {
609 cout << "Power is " << ((status) ? "on" : "off") << endl;
610 }
611 else
612 {
613 cout << ((status) ? "on" : "off") << endl;
614 }
615 returnvalue = (status)? 0 : 1;
616
617 }
618
619 raw1394_destroy_handle(handle);
620
621 return returnvalue;
622}
623
624
625