Sa4250 ch guid

From MythTV Official Wiki
Revision as of 18:28, 21 August 2011 by Microe (talk | contribs) (Added an argument to sa4250_ch, -k. If this argument is used a key release will be sent to the STB after the channel change.)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Important.png Note: The correct title of this article is sa4250_ch_guid. It appears incorrectly here due to technical restrictions.


Author Bradley S. Corsello
Description Channel-change script for use with Scientific Atlanta SA4250HD cable box using GUID.
Supports


My Myth setup includes two SA4250HD STBs, which I control using Firewire. I've been using MajorIdiot's sa4250_ch channel changing program, but found it didn't handle multiple STBs well, so I modified it a bit.

My new setup has a shell wrapper, sa4250_ch_guid, and a modified version of Major's channel changer, sa4250_ch.

Invoke sa4250_ch_guid with the GUID of the box you want to control and the channel as parameters. You don't need the whole GUID, just enough of it to be a unique search key (there is no checking of this so make sure you have it right).

sa4250_ch_guid determines the node number of the STB with the given GUID and passes that as a parameter to sa4250_ch. This is necessary because node numbers often seem to change at unpredictable times.


Script.png sa4250_ch_guid

#!/bin/sh
#
# sa4250_ch_guid  Copyright 2008 by Bradley S. Corsello
# Licensed under GPL
#
# Because the node number assigned to Firewire STBs can change, this~
# script changes channel on connnected SA4250 STB identified by GUID.
# You don't need to use the whole GUID, just enough to make it unique
#

GUID="$1"
CHANNEL="$2"

/usr/local/bin/sa4250_ch -n $(plugreport 2>/dev/null | awk '/'$GUID'/ {print $2}' - ) $CHANNEL


Script.png sa4250_ch.c

/*
 * Samo's SA4250HD firewire channel changer by majoridiot
 * with node selection added by Bradley S. Corsello
 *
 * requires: libavc1394-dev libraw1394-dev
 *
 * compile with: gcc -o sa4250_ch sa4250_ch.c -lrom1394 -lavc1394 -lraw1394
 *
 * based on mythtv source code and
 *
 * sa3250ch - an external channel changer for SA3250HD Tuner
 * Based off 6200ch.c by Stacey D. Son
 *
 * Copyright 2004,2005 by Stacey D. Son ( mythdev a son d org )
 * Copyright 2005 Matt Porter ( mporter a kernel d crashing d org )
 * Portions Copyright 2006 Chris Ingrassia ( chris a spicecoffee org ) (SA4200
 * and Single-digit command mode)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <libavc1394/rom1394.h>
#include <libavc1394/avc1394.h>
#include <libraw1394/raw1394.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

/* SA42xxHD IDs */
#define SA4200HD_VENDOR_ID1    0x000014f8
#define SA4200HD_MODEL_ID1     0x00001072
#define SA4250HD_VENDOR_ID1    0x00001cea  /* samo's stb */
#define SA4250HD_MODEL_ID1     0x000010cc  /* samo's stb */

/* add additional vendor and model id's here- addition needed to if statement
 * starting @ line 134 below
 */

#define AVC1394_SA3250_COMMAND_CHANNEL 0x000007c00
#define AVC1394_SA3250_OPERAND_KEY_PRESS 0xe7
#define AVC1394_SA3250_OPERAND_KEY_RELEASE 0x67

#define CTL_CMD0 AVC1394_CTYPE_CONTROL | AVC1394_SUBUNIT_TYPE_PANEL | \
        AVC1394_SUBUNIT_ID_0 | AVC1394_SA3250_COMMAND_CHANNEL
#define CTL_CMD1 (0x04 << 24)
#define CTL_CMD2 0xff000000

#define STARTING_NODE 0

void usage()
{
    fprintf(stderr, "Usage: sa4250_ch_new [-v] [-n <node>] -[k] <channel_num>\n");
    fprintf(stderr, "  -v : Verbose Mode\n");
    fprintf(stderr, "  -n : Node to change - default 0\n");
    fprintf(stderr, "  -k : Perform key release after channel change\n");
    exit(1);
}

int main (int argc, char *argv[])
{
    rom1394_directory dir;
    int device = -1;
    int single = 0;
    int i;
    int verbose = 0;
    int keyrelease = 0;
    int dig[3];
    int chn = 708;
    int unode = 0;

    if (argc < 2)
        usage();

    for(i = 1; i < argc; ++i) {
        if ((argv[i][0] == '-') && (strlen(argv[i]) > 1)) {
            switch(argv[i][1]) {
                case 'v':
                    verbose = 1;
                    break;
                case 's':
                    single = 1;
                    break;
                case 'n':
                    unode = atoi(argv[++i]);
                    break;
                case 'k':
                    keyrelease = 1;
                    break;
                default:
                    fprintf(stderr, "WARNING: Unknown option \'%c\', ignoring",
                            argv[i][1]);
            }
        }
        else {
            chn = atoi(argv[i]);
        }
    }

#ifdef RAW1394_V_0_8
    raw1394handle_t handle = raw1394_get_handle();
#else
    raw1394handle_t handle = raw1394_new_handle();
#endif

    if (!handle) {
        if (!errno) {
            fprintf(stderr, "Not Compatible!\n");
        } else {
            perror("Couldn't get 1394 handle");
            fprintf(stderr, "Is ieee1394, driver, and raw1394 loaded?  "
                    "Are /dev/raw1394 permissions set correctly?\n");
        }
        exit(1);
    }

    if (raw1394_set_port(handle, 0) < 0) {
        perror("ERROR-- could not set port");
        raw1394_destroy_handle(handle);
        exit(1);
    }

    int nc = raw1394_get_nodecount(handle);
    if (rom1394_get_directory(handle, unode, &dir) < 0) {
        fprintf(stderr,"ERROR reading config rom directory for node %d\n", unode);
        raw1394_destroy_handle(handle);
        exit(1);
    }

    if (verbose)
        printf("node %d: vendor_id = 0x%08x model_id = 0x%08x\n",
               unode, dir.vendor_id, dir.model_id);

    /* add new vendor and model ids to if stanza below */
    if ( ((dir.vendor_id == SA4250HD_VENDOR_ID1) &&
          (dir.model_id == SA4250HD_MODEL_ID1))  ||
         ((dir.vendor_id == SA4200HD_VENDOR_ID1) &&
          (dir.model_id == SA4200HD_MODEL_ID1)))
    {
        device = unode;
    }

    if (device == -1) {
        fprintf(stderr, "Could not find SA42XXHD on the 1394 bus!\n");
        fprintf(stderr, "Try running again with -v and check source code for "
                "matching Vendor ID and Model ID-\n");
        fprintf(stderr, "Add if necessary and recompile.\n");
        raw1394_destroy_handle(handle);
        exit(1);
    }

    if (verbose)
        printf("Device acquired on node %d\n", device);

    printf("Changing channel %d\n", chn);

    quadlet_t cmd[3] =
    {
        CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_PRESS,
        CTL_CMD1 | (chn << 8),
        CTL_CMD2,
    };

    if (verbose)
        printf("AV/C Command: cmd0=0x%08x cmd1=0x%08x cmd2=0x%08x\n",
               cmd[0], cmd[1], cmd[2]);

    avc1394_transaction_block(handle, device, cmd, 3, 1);

    if (keyrelease) {
        if (verbose)
            printf("Performing key release\n");

        quadlet_t cmd2[3] =
        {
            CTL_CMD0 | AVC1394_SA3250_OPERAND_KEY_RELEASE,
            CTL_CMD1 | (chn << 8),
            CTL_CMD2,
        };

        if (verbose)
            printf("AV/C Command: cmd0=0x%08x cmd1=0x%08x cmd2=0x%08x\n",
                   cmd2[0], cmd2[1], cmd2[2]);

        avc1394_transaction_block(handle, device, cmd2, 3, 1);
    }

    raw1394_destroy_handle(handle);

    return 0;
}