/*
 * Copyright (c) 1999-2003 WIDE Project
 * All rights reserved.
 *
 * Author : Kohei OGURA (koh39@sfc.wide.ad.jp)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code MUST retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form MUST reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    MUST display the following acknowledgement:
 *      This product includes software developed by Akimichi OGAWA.
 * 4. The name of the author MAY NOT be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <libraw1394/ieee1394.h>
#include <libraw1394/raw1394.h>
#include <libraw1394/csr.h>
#include <libavc1394/avc1394.h>
#include <libavc1394/avc1394_vcr.h>
#include <libavc1394/rom1394.h>

#define MAX_PORT 16
#define RETRY 10

struct avc_frame {
	quadlet_t quadlets[4];
};

main(int argc, char *argv[])
{
	if(argc !=4){
		printf("Usage: [port] [nodeid] [channel]\n");
		exit(1);
	}
	
	struct avc_frame gv1394tv;
	struct raw1394_portinfo port_info[MAX_PORT];
	static raw1394handle_t handle;
	nodeid_t node_num = atoi(argv[2]);
	int port_num = atoi(argv[1]);
	int channel = atoi(argv[3])-1;
	int port_found;
	int node_found;

// Prepare AV/C packet
	gv1394tv.quadlets[0] = AVC1394_CTYPE_CONTROL |
			       AVC1394_SUBUNIT_TYPE_UNIT |
			       AVC1394_COMMAND_VENDOR_DEPENDENT |
			       0x00;
	gv1394tv.quadlets[1] = 0xa0b01100;
	gv1394tv.quadlets[2] = 0x02 << 24 | channel << 16 |
			       0x80 << 8 | 0x0a;	
	gv1394tv.quadlets[3] = 0x00000000;;

// Prepare IEEE1394 interface
	handle = raw1394_new_handle();
	if(handle == NULL){
		perror("raw1394_new_handle()");
		exit(1);
	}

	port_found = raw1394_get_port_info(handle, port_info, MAX_PORT);
	if(port_found < 0){
		perror("port not found\n");
		exit(1);
	}
	else if(port_found < port_num){
		perror("port does not exist\n");
	}

	if(raw1394_set_port(handle, atoi(argv[1])) < 0){
		perror("raw1394_set_port()");
		exit(1);
	}

	node_found = raw1394_get_nodecount(handle);
	if(node_found < node_num){
		perror("node does not exist\n");
	}

// Send AV/C packet
	if((avc1394_transaction_block(handle, node_num, (quadlet_t *)&gv1394tv, 4, RETRY)) == NULL){
		perror("avc1394_transaction_block()");
	}
}
