bool FirewirePort::Init(void)
{
    // create firewire port handle
    handle = raw1394_new_handle();
    handle_bc = raw1394_new_handle();
    if (!handle || !handle_bc) {
        outStr << "FirewirePort: could not create handle" << std::endl;
        return false;
    }

    PortListType::const_iterator it;
    for (it = PortList.begin(); it != PortList.end(); it++) {
        if ((*it)->PortNum == PortNum)
            outStr << "WARNING: Firewire port " << PortNum
                   << " is already used (be careful about thread safety)" << std::endl;
        if ((*it)->handle == handle) // should never happen
            outStr << "WARNING: Firewire handle is already used" << std::endl;
    }
    PortList.push_back(this);

    memset(Node2Board, BoardIO::MAX_BOARDS, sizeof(Node2Board));

    // set the bus reset handler
    old_reset_handler = raw1394_set_bus_reset_handler(handle, reset_handler);
    old_reset_handler = raw1394_set_bus_reset_handler(handle_bc, reset_handler);

    // get number of ports
    int nports = raw1394_get_port_info(handle, NULL, 0);
    outStr << "FirewirePort: number of ports = " << nports << std::endl;
    if (nports < PortNum) {
        outStr << "FirewirePort: port " << PortNum << " does not exist" << std::endl;
        raw1394_destroy_handle(handle);
        handle = NULL;
        return false;
    }

    if (raw1394_set_port(handle, PortNum) || raw1394_set_port(handle_bc, PortNum)) {
        outStr << "FirewirePort: error setting port to " << PortNum << std::endl;
        raw1394_destroy_handle(handle);
        raw1394_destroy_handle(handle_bc);
        handle = NULL;
        handle_bc = NULL;
        return false;
    }
    outStr << "FirewirePort: successfully initialized port " << PortNum << std::endl;

    // stop cycle start packet
    StopCycleStartPacket();

    return ScanNodes();
}
Esempio n. 2
0
static int dc1394_setup( struct dc1394_input *conf )
{
	raw1394handle_t raw_handle;
	nodeid_t *camera_nodes = NULL;
	int numPorts;
	int actual_count = 0, c, i;
	struct raw1394_portinfo ports[16];

	raw_handle = raw1394_new_handle();
	if( ! raw_handle )
	{
		spook_log( SL_ERR,
			"dc1394: unable to acquire a raw1394 handle" );
		return -1;
	}

	numPorts = raw1394_get_port_info( raw_handle, ports, 16 );
	raw1394_destroy_handle( raw_handle );
	for( i = 0; i < numPorts; ++i )
	{
		int camCount = 0;

		raw_handle = raw1394_new_handle();
		raw1394_set_port( raw_handle, i );
		camera_nodes = dc1394_get_camera_nodes( raw_handle,
							&camCount, 1 );
		raw1394_destroy_handle( raw_handle );
		spook_log( SL_INFO, "dc1394: found %d cams on port %d",
				camCount, i );
		for( c = 0; c < camCount; ++c )
		{
			if( cam_setup( conf, actual_count, i,
						camera_nodes[c], c ) < 0 )
				return -1;
			if( ++actual_count == conf->cam_count ) return 0;
		}
		// this doesn't work, fix later (just a li'l memory leak...)
		//dc1394_free_camera_nodes( camera_nodes );
	}

	if( actual_count == 0 )
	{
		spook_log( SL_ERR, "dc1394: did not find any IIDC cameras" );
		return -1;
	}

	if( actual_count < conf->cam_count )
		spook_log( SL_WARN,
			"dc1394: only found %d cameras, some outputs will be inactive",
			actual_count );

	return 0;
}
static void
gst_hdv1394src_update_device_name (GstHDV1394Src * src)
{
    raw1394handle_t handle;
    gint portcount, port, nodecount, node;
    rom1394_directory directory;

    g_free (src->device_name);
    src->device_name = NULL;

    GST_LOG_OBJECT (src, "updating device name for current GUID");

    handle = raw1394_new_handle ();

    if (handle == NULL)
        goto gethandle_failed;

    portcount = raw1394_get_port_info (handle, NULL, 0);
    for (port = 0; port < portcount; port++) {
        if (raw1394_set_port (handle, port) >= 0) {
            nodecount = raw1394_get_nodecount (handle);
            for (node = 0; node < nodecount; node++) {
                if (src->guid == rom1394_get_guid (handle, node)) {
                    if (rom1394_get_directory (handle, node, &directory) >= 0) {
                        g_free (src->device_name);
                        src->device_name = g_strdup (directory.label);
                        rom1394_free_directory (&directory);
                        goto done;
                    } else {
                        GST_WARNING ("error reading rom directory for node %d", node);
                    }
                }
            }
        }
    }

    src->device_name = g_strdup ("Unknown");      /* FIXME: translate? */

done:

    raw1394_destroy_handle (handle);
    return;

    /* ERRORS */
gethandle_failed:
    {
        GST_WARNING ("failed to get raw1394 handle: %s", g_strerror (errno));
        src->device_name = g_strdup ("Unknown");    /* FIXME: translate? */
        return;
    }
}
Esempio n. 4
0
static void resetBus() {
	//cleanup primitive resources. required if the last user abruptly
	//terminated. this method isn't ideal since it just resets the first bus,
	//even though there might be more (there aren't on our robot, though).
	raw1394handle_t handle = raw1394_new_handle();
	if(!handle) {
		CV_Error(CV_StsError, "unable to open raw1394 handle (is there a bus available on this computer?)");
	}
	const int numPorts = raw1394_get_port_info(handle, NULL, 0);
	if(numPorts < 1) {
		raw1394_destroy_handle(handle);
		CV_Error(CV_StsError, "no raw1394 ports");
	}
	raw1394_set_port(handle, 0);
	raw1394_reset_bus(handle);
	raw1394_destroy_handle(handle);
}
Esempio n. 5
0
int main(int argc, char **argv)
{
        raw1394handle_t handle;

        parse_args(argc, argv);

        fprintf(stderr, "port: %ld\nloops: %ld\nfile: %s\n", which_port,
                loopcount, filename);

        handle = raw1394_new_handle();
        if (!handle) {
                if (!errno)
                        fprintf(stderr, "No working kernel driver found.\n");
                else
                        perror("raw1394_get_handle");
                exit(1);
        }

        do {
                if (raw1394_get_port_info(handle, NULL, 0) <= which_port) {
                        fprintf(stderr, "Port %ld does not exist.\n",
                                which_port);
                        exit(1);
                }

                raw1394_set_port(handle, which_port);
        } while (errno == ESTALE);

        if (errno) {
                perror("raw1394_set_port");
                exit(1);
        }

        if (filename)
                send_iso_file(handle);

        fprintf(stderr, "\n");
        raw1394_iso_shutdown(handle);
        raw1394_destroy_handle(handle);

        return 0;
}
Esempio n. 6
0
static GValueArray *
gst_1394_get_guid_array (void)
{
  GValueArray *result = NULL;
  raw1394handle_t handle = NULL;
  int num_ports = 0;
  int port = 0;
  int num_nodes = 0;
  int node = 0;
  rom1394_directory directory;
  GValue value = { 0, };

  handle = raw1394_new_handle ();

  if (handle == NULL)
    return NULL;

  num_ports = raw1394_get_port_info (handle, NULL, 0);
  for (port = 0; port < num_ports; port++) {
    if (raw1394_set_port (handle, port) >= 0) {
      num_nodes = raw1394_get_nodecount (handle);
      for (node = 0; node < num_nodes; node++) {
        rom1394_get_directory (handle, node, &directory);
        if (rom1394_get_node_type (&directory) == ROM1394_NODE_TYPE_AVC &&
            avc1394_check_subunit_type (handle, node,
                AVC1394_SUBUNIT_TYPE_VCR)) {
          if (result == NULL)
            result = g_value_array_new (3);     /* looks like a sensible default */
          g_value_init (&value, G_TYPE_UINT64);
          g_value_set_uint64 (&value, rom1394_get_guid (handle, node));
          g_value_array_append (result, &value);
          g_value_unset (&value);
        }
      }
    }
  }

  return result;
}
Esempio n. 7
0
/** Open the raw1394 device and get a handle.
 *  
 * \param port A 0-based number indicating which host adapter to use.
 * \return a raw1394 handle.
 */
raw1394handle_t raw1394_open( int port )
{
	int n_ports;
	struct raw1394_portinfo pinf[ 16 ];
	raw1394handle_t handle;

	/* get a raw1394 handle */
#ifdef RAW1394_V_0_8

	handle = raw1394_get_handle();
#else

	handle = raw1394_new_handle();
#endif

	if ( !handle )
	{
		fprintf( stderr, "raw1394 - failed to get handle: %s.\n", strerror( errno ) );
		exit( EXIT_FAILURE );
	}

	if ( ( n_ports = raw1394_get_port_info( handle, pinf, 16 ) ) < 0 )
	{
		fprintf( stderr, "raw1394 - failed to get port info: %s.\n", strerror( errno ) );
		raw1394_destroy_handle( handle );
		exit( EXIT_FAILURE );
	}

	/* tell raw1394 which host adapter to use */
	if ( raw1394_set_port( handle, port ) < 0 )
	{
		fprintf( stderr, "raw1394 - failed to set set port: %s.\n", strerror( errno ) );
		exit( EXIT_FAILURE );
	}

	raw1394_set_userdata( handle, ( void* ) port );
	return handle;
}
Esempio n. 8
0
void AVC1394Control::initialize()
{
	int i;

	current_command = COMMAND_NONE;
	device = -1;

	device_lock = new Mutex("AVC1394Control::device_lock");

#ifdef RAW1394_V_0_8
	handle = raw1394_get_handle();
#else
	handle = raw1394_new_handle();
#endif
//printf("AVC1394Control::initialize(): 1\n");
	if(!handle)
	{
//printf("AVC1394Control::initialize(): 2\n");
		if(!errno)
		{
//printf("AVC1394Control::initialize(): 3\n");
			fprintf(stderr, "AVC1394Control::initialize(): Not Compatable!\n");
		} 
		else 
		{
//printf("AVC1394Control::initialize(): 4\n");
			fprintf(stderr, "AVC1394Control::initialize(): couldn't get handle\n");
		}
		return;
	}

	if(raw1394_set_port(handle, 0) < 0) {
//printf("AVC1394Control::initialize(): 5\n");
		perror("AVC1394Control::initialize(): couldn't set port");
//		raw1394_destroy_handle(handle);
		return;
	}

	for(i = 0; i < raw1394_get_nodecount(handle); i++)
	{
		if(rom1394_get_directory(handle, i, &rom_dir) < 0)
		{
//printf("AVC1394Control::initialize(): 6\n");
			fprintf(stderr, "AVC1394Control::initialize(): node %d\n", i);
//			raw1394_destroy_handle(handle);
			return;
		}
		
		if((rom1394_get_node_type(&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
			avc1394_check_subunit_type(handle, i, AVC1394_SUBUNIT_TYPE_VCR))
		{
//printf("AVC1394Control::initialize(): 7\n");
			device = i;
			break;
		}
	}

	if(device == -1)
	{
//printf("AVC1394Control::initialize(): 8\n");
		fprintf(stderr, "AVC1394Control::initialize(): No AV/C Devices\n");
//		raw1394_destroy_handle(handle);
		return;
	}

}
Esempio n. 9
0
File: dv.c Progetto: 0xheart0/vlc
/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;

    struct raw1394_portinfo port_inf[ 16 ];

    msg_Dbg( p_access, "opening device" );

    /* Set up p_access */
    access_InitFields( p_access );
    ACCESS_SET_CALLBACKS( NULL, Block, Control, NULL );

    p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
    if( !p_sys )
        return VLC_EGENERIC;

    p_sys->i_cards = 0;
    p_sys->i_node = 0;
    p_sys->i_port = 0;
    p_sys->i_guid = 0;
    p_sys->i_channel = 63;
    p_sys->p_raw1394 = NULL;
    p_sys->p_avc1394 = NULL;
    p_sys->p_frame = NULL;
    p_sys->p_ev = NULL;

    vlc_mutex_init( &p_sys->lock );

    p_sys->i_node = DiscoverAVC( p_access, &p_sys->i_port, p_sys->i_guid );
    if( p_sys->i_node < 0 )
    {
        msg_Err( p_access, "failed to open a Firewire (IEEE1394) connection" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    p_sys->p_avc1394 = AVCOpen( p_access, p_sys->i_port );
    if( !p_sys->p_avc1394 )
    {
        msg_Err( p_access, "no Digital Video Control device found" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    p_sys->p_raw1394 = raw1394_new_handle();
    if( !p_sys->p_raw1394 )
    {
        msg_Err( p_access, "no Digital Video device found" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    p_sys->i_cards = raw1394_get_port_info( p_sys->p_raw1394, port_inf, 16 );
    if( p_sys->i_cards < 0 )
    {
        msg_Err( p_access, "failed to get port info" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    if( raw1394_set_port( p_sys->p_raw1394, p_sys->i_port ) < 0 )
    {
        msg_Err( p_access, "failed to set port info" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    if ( raw1394_iso_recv_init( p_sys->p_raw1394, Raw1394Handler,
                ISOCHRONOUS_QUEUE_LENGTH, ISOCHRONOUS_MAX_PACKET_SIZE,
                p_sys->i_channel, RAW1394_DMA_PACKET_PER_BUFFER, -1 ) < 0 )
    {
        msg_Err( p_access, "failed to init isochronous recv" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    raw1394_set_userdata( p_sys->p_raw1394, p_access );
    raw1394_iso_recv_start( p_sys->p_raw1394, -1, -1, 0 );

    p_sys->raw1394_poll.fd = raw1394_get_fd( p_sys->p_raw1394 );
    p_sys->raw1394_poll.events = POLLIN | POLLPRI;

    /* Now create our event thread catcher */
    p_sys->p_ev = calloc( 1, sizeof( *p_sys->p_ev ) );
    if( !p_sys->p_ev )
    {
        msg_Err( p_access, "failed to create event thread struct" );
        Close( p_this );
        return VLC_ENOMEM;
    }

    p_sys->p_ev->p_frame = NULL;
    p_sys->p_ev->pp_last = &p_sys->p_ev->p_frame;
    p_sys->p_ev->p_access = p_access;
    vlc_mutex_init( &p_sys->p_ev->lock );
    if( vlc_clone( &p_sys->p_ev->thread, Raw1394EventThread,
               p_sys->p_ev, VLC_THREAD_PRIORITY_OUTPUT ) )
    {
        msg_Err( p_access, "failed to clone event thread" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}
Esempio n. 10
0
/*
  bus reset handler checks if device is still on the bus. 
  if it is removed
    -> set device_present to 0 . bandwidth and channel are remotely freed on bus reset
  if device still present
    -> reallocate bw and channel
    -> channel is not guarranted to be the same after reallocation -> set new channel to camera if changed  
 */
int dcam_busreset_handler( raw1394handle_t raw1394handle, unsigned int generation )
{
	dcam_handle_t dcamhandle = ( dcam_handle_t ) raw1394_get_userdata( raw1394handle );
	int port;
	
	int channel;
	
	raw1394_update_generation( raw1394handle, generation );

	// check if the device is still there or if it has changed
	if( _dcam_find_device( &dcamhandle->unicap_device, 
			       &port, 
			       &dcamhandle->node, 
			       &dcamhandle->directory ) != STATUS_SUCCESS )
	{
		dcamhandle->device_present = 0;
		dcam_device_removed_event( dcamhandle );
		return 0;
	}
	
	// what happens if a PC-Card is removed ? does this change the port???
	if( port != dcamhandle->port )
	{
		if( raw1394_set_port( raw1394handle, port ) < 0 )
		{
			dcamhandle->device_present = 0;
			dcam_device_removed_event( dcamhandle );
			return 0;
		}
		dcamhandle->port = port;
	}
	
	// reallocate bandwidth 
	// ( reallocation should always succeed )
	if( dcamhandle->allocate_bandwidth )
	{
		if( !SUCCESS(_1394util_allocate_bandwidth( dcamhandle->raw1394handle, dcam_isoch_table[dcamhandle->current_iso_index].bytes_per_packet ) ) )
		{
			TRACE( "dcam.cpi: failed to reallocate bandwidth\n" );
			dcam_capture_stop( dcamhandle );
			return 0;
		}
	}
	
	// channel is freed on busreset
	// -> reallocate channel
	//    check if newly allocated channel matches previous channel
	//    -> conditionaly set new channel on the camera
	if( !SUCCESS(_1394util_allocate_channel(dcamhandle->raw1394handle, dcamhandle->channel_allocated ) ) )
	{
		
		if( ( channel = _1394util_find_free_channel( dcamhandle->raw1394handle ) ) < 0 )
		{
			TRACE( "dcam.cpi: failed to allocate channel\n");
			_1394util_free_bandwidth( dcamhandle->raw1394handle, dcam_isoch_table[dcamhandle->current_iso_index].bytes_per_packet );
			dcam_capture_stop( dcamhandle );
			return 0;
		}
		
		if( channel != dcamhandle->channel_allocated )
		{
			quadlet_t quad = 0;
			if( dcam_isoch_table[dcamhandle->current_iso_index].min_speed <= S400 )
			{
			   quad = ( channel << 28 ) | ( S400 << 24 );
			}
			else
			{
			   quad = ( channel << 28 ) | 
			      ( dcam_isoch_table[dcamhandle->current_iso_index].min_speed << 24 );
			}   
			
			if( _dcam_write_register( dcamhandle->raw1394handle, 
									  dcamhandle->node, 
									  dcamhandle->command_regs_base + 0x60c, 
									  quad ) < 0 )
			{
				_1394util_free_channel( dcamhandle->raw1394handle, 
										channel );
				_1394util_free_bandwidth( dcamhandle->raw1394handle, 
										  dcam_isoch_table[dcamhandle->current_iso_index].bytes_per_packet );
				return 0;
			}
		}
	}
	
	return 0;
}
Esempio n. 11
0
int main(int argc, char **argv)
{
	raw1394handle_t handle;
	int retval;
	quadlet_t rom[0x100];
	size_t rom_size;
	unsigned char rom_version;
	rom1394_directory dir;
	char *(leaf[2]);
	
	handle = raw1394_new_handle();
	
	if (!handle) {
		if (!errno) {
				printf(not_compatible);
		} else {
				perror("couldn't get handle");
				printf(not_loaded);
		}
		exit(EXIT_FAILURE);
	}
	
	if (raw1394_set_port(handle, 0) < 0) {
		perror("couldn't set port");
		exit(EXIT_FAILURE);
	}
	
	/* get the current rom image */
	retval=raw1394_get_config_rom(handle, rom, 0x100, &rom_size, &rom_version);
	rom_size = rom1394_get_size(rom);
	printf("get_config_rom returned %d, romsize %d, rom_version %d:",retval,rom_size,rom_version);
	
	/* get the local directory */
	rom1394_get_directory( handle, raw1394_get_local_id(handle) & 0x3f, &dir);
	
	/* free the allocated mem for the textual leaves */
	rom1394_free_directory( &dir);
	
	/* add an RFC 2734 unit directory */
	dir.unit_spec_id    = 0x0000005e;
	dir.unit_sw_version = 0x00000001;
	leaf[0] = "IANA";
    leaf[1] = "IPv4";
	dir.nr_textual_leafs = 2;
	dir.textual_leafs = leaf;
	
	/* manipulate the rom */
	retval = rom1394_add_unit( rom, &dir);
	
	/* get the computed size of the rom image */
	rom_size = rom1394_get_size(rom);
	
	printf("rom1394_add_unit_directory returned %d, romsize %d:",retval,rom_size);
	
	/* convert computed rom size from quadlets to bytes before update */
	rom_size *= sizeof(quadlet_t);
	retval = raw1394_update_config_rom(handle, rom, rom_size, rom_version);
	printf("update_config_rom returned %d\n",retval);
	printf("You need to reload your ieee1394 modules to reset the rom.\n");
    
    raw1394_reset_bus(handle);
	
	exit(EXIT_SUCCESS);
}
int main(int argc, char** argv)
{
    int rc; /**< return code */
    int port = 0;  /*!< fw handle port number */
    int nodeid = 0;   /*!< arm server node id */

    // parse command line (port number)
    opterr = 0;  // getopt no err output
    const char short_options[] = "hp:n:";
    int next_opt;
    do {
        next_opt = getopt(argc, argv, short_options);
        switch(next_opt)
        {
        case 'h':
            print_usage();
            return EXIT_SUCCESS;
            break;
        case 'p':
            port = atoi(optarg);
            break;
        case 'n':
            nodeid = atoi(optarg);
            break;
        case '?':
            std::cerr << "Invalid argument" << std::endl;
            break;
        default:
            break;
        }
    }
    while(next_opt != -1);


    // ----- Get handle and set port for the handle -------
    // create handle
    handle = raw1394_new_handle();
    if (handle == NULL) {
        std::cerr << "**** Error: could not create 1394 handle " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    // get port number & sanity check
    int numPorts = raw1394_get_port_info(handle, NULL, 0);
    if (port < 0 || port >= numPorts) {
        std::cerr << "Invalid port number" << std::endl;
        return EXIT_FAILURE;
    }

    // let user to choose which port to use
    rc = raw1394_set_port(handle, port);
    if (rc) {
        std::cerr << "**** Error: failed to set port " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }


    // -------- Set FireWire bus reset handler --------

    // set bus reset handler
    raw1394_set_bus_reset_handler(handle, my_bus_reset_handler);



    // ----------------------------------------------------------------------------
    // Start tutorial 6 isochronous send
    // ----------------------------------------------------------------------------

    /**
     * raw1394_iso_xmit_init - initialize isochronous transmission
     * @handle: libraw1394 handle
     * @handler: handler function for queueing packets
     * @buf_packets: number of isochronous packets to buffer
     * @max_packet_size: largest packet you need to handle, in bytes
     * (not including the isochronous header)
     * @channel: isochronous channel on which to transmit
     * @speed: speed at which to transmit
     * @irq_interval: maximum latency of wake-ups, in packets (-1 if you don't care)
     *
     * Allocates all user and kernel resources necessary for isochronous transmission.
     * Channel and bandwidth allocation at the IRM is not performed.
     *
     * Returns: 0 on success or -1 on failure (sets errno)
     **/
    size_t length;
    unsigned char channel, tag, sy;
    unsigned char buffer[BUF_SIZE + BUF_HEAD];
    channel = 5;
    tag = 6;
    sy = 7;

    // ----- transmitting end -------------
    rc = raw1394_iso_xmit_init(handle,      // 1394 handle
                               NULL,        // xmit handler
                               BUFFER,      // iso packets to buffer
                               MAX_PACKET,  // max packet size
                               channel,           // just pick 5 for fun
                               RAW1394_ISO_SPEED_400,
                               -1);         // irq_interval
    if (rc) {
        perror("raw1394_iso_xmit_init");
        exit(1);
    }
    rc = raw1394_iso_xmit_start(handle, -1, -1);
    if (rc) {
        perror("raw1394_iso_xmit_start");
        exit(1);
    }

    quadlet_t data = 0x0;
    while (true) {
        rc = raw1394_iso_xmit_write(handle,
                                    (unsigned char *)&data,
                                    4,
                                    tag,
                                    sy);
        if (rc) {
            perror("\nraw1394_iso_xmit_write");
            break;
        }
//        data++;
//        std::cout << "data = " << data << std::endl;
    }

    // stop, clean up & exit
    raw1394_iso_stop(handle);
    raw1394_iso_shutdown(handle);
    raw1394_destroy_handle(handle);

    return EXIT_SUCCESS;
}
int main(int argc, char** argv)
{
    int rc; /**< return code */
    int port = 0;  /*!< fw handle port number */

    // parse command line (port number)
    opterr = 0;  // getopt no err output
    const char short_options[] = "p:";
    int next_opt;
    do {
        next_opt = getopt(argc, argv, short_options);
        switch(next_opt)
        {
        case 'p':
            port = atoi(optarg);
            break;
        case '?':
            std::cerr << "Invalid argument" << std::endl;
            break;
        default:
            break;
        }
    }
    while(next_opt != -1);


    // Setup signal handler to exit on Ctrl-C
    signal(SIGINT, signal_handler);


    // ----- Get handle and set port for the handle -------
    // create handle
    handle = raw1394_new_handle();
    if (handle == NULL) {
        std::cerr << "**** Error: could not create 1394 handle " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    // get port number & sanity check
    int numPorts = raw1394_get_port_info(handle, NULL, 0);
    if (port < 0 || port >= numPorts) {
        std::cerr << "Invalid port number" << std::endl;
        return EXIT_FAILURE;
    }

    // let user to choose which port to use
    rc = raw1394_set_port(handle, 0);
    if (rc) {
        std::cerr << "**** Error: failed to set port " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }


    // -------- Set FireWire bus reset handler --------
    // set bus reset handler
    bus_reset_handler_t old_bus_reset_handler;
    old_bus_reset_handler = raw1394_set_bus_reset_handler(handle, my_bus_reset_handler);



    // ----------------------------------------------------------------------------
    // Start tutorial 2 arm server
    // ----------------------------------------------------------------------------


    // -------- Register arm register to handle arm request --------
    const nodeaddr_t arm_start_addr = 0xffffff000000;  // arm start address
    const size_t arm_length = 4;  // arm length to register

    // arm initial buffer
    byte_t arm_init_buffer[arm_length];
    memset(arm_init_buffer, 0x02, arm_length);  // set inital value to all 0x02

    // setup arm request handle
    raw1394_arm_reqhandle arm_reqhandle;
    char my_arm_callback_context[] = "my_arm_callback_context";
    arm_reqhandle.pcontext = my_arm_callback_context;
    arm_reqhandle.arm_callback = my_arm_req_callback;

    int access_mode = RAW1394_ARM_WRITE|RAW1394_ARM_READ;   // allow read and write transaction

    rc = raw1394_arm_register(handle,  // fw handle
                              arm_start_addr, // arm start address
			      arm_length * 4, // arm_length quadlet * 4 to bytes
			      arm_init_buffer,  // arm init buffer value
                              (octlet_t) &arm_reqhandle,  // arm request handler
                              access_mode,   // access permission
                              access_mode,   // client handler will be notified
                              0);            // client handler will need to handle these transactions

    if (rc) {
        std::cerr << "**** Error: failed to setup arm register, error " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    // -------- Read from this arm buffer ---------
    // NOTE: this is a simple way to test if
    const size_t arm_read_size = 4;
    byte_t arm_read_buffer[arm_read_size];

    rc = raw1394_arm_get_buf(handle, arm_start_addr, arm_read_size, arm_read_buffer);
    if (rc) {
        std::cerr << "**** Error: failed to read arm register, error " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    } else {
        // if success then print value
        std::cout << "ARM buffer read value: \n";
        for (size_t i = 0; i < arm_read_size; i++) {
            std::cout << std::hex << " " << (int)arm_read_buffer[i];
        }
        std::cout << std::endl;
    }

    // -------- Set value to this arm buffer ---------
    const size_t arm_write_size = 4;
    byte_t arm_write_buffer[arm_write_size] = {0x11, 0x22, 0x33, 0x44};
    rc = raw1394_arm_set_buf(handle, arm_start_addr, arm_write_size, arm_write_buffer);
    if (rc) {
        std::cerr << "**** Error: failed to set arm register, error " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }


    // --------- Now read the value back from buffer ------
    rc = raw1394_arm_get_buf(handle, arm_start_addr, arm_read_size, arm_read_buffer);
    if (rc) {
        std::cerr << "**** Error: failed to read arm register, error " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    } else {
        // if success then print value
        std::cout << "ARM buffer read value: \n";
        for (size_t i = 0; i < arm_read_size; i++) {
            std::cout << std::hex << " " << (int)arm_read_buffer[i];
        }
        std::cout << std::endl;
    }


    // --------- Infinite raw1394 event loop ----------

    std::cout << "--------- Now start arm server -----------" << std::endl;
    std::cout << " node id = " << raw1394_get_local_id(handle) << std::endl;

    while (true)
    {
        raw1394_loop_iterate(handle);
    }

    // clean up & exit
    raw1394_destroy_handle(handle);

    return EXIT_SUCCESS;
}
Esempio n. 14
0
bool firewire_source::try_open()
{
    auto_raw1394 handle(raw1394_new_handle());
    if (!handle.get())
	return false;
    raw1394_set_userdata(handle.get(), this);

    int n_ports = raw1394_get_port_info(handle.get(), NULL, 0);
    std::vector<raw1394_portinfo> ports(n_ports);
    if (n_ports > 0)
    {
	int n_ports_again =
	    raw1394_get_port_info(handle.get(), &ports[0], n_ports);
	if (n_ports > n_ports_again)
	{
	    n_ports = n_ports_again;
	    ports.resize(n_ports);
	}
    }
    if (n_ports == 0)
    {
	fprintf(stderr, "ERROR: No Firewire ports accessible\n");
	return false;
    }

    // Try converting name to an integer
    char * end;
    int i = strtoul(port_name_.c_str(), &end, 10);

    // If we didn't convert the whole string, assume it really is a name
    if (*end)
	for (i = 0; i != n_ports; ++i)
	    if (port_name_ == ports[i].name)
		break;

    if (i >= n_ports)
    {
	fprintf(stderr, "ERROR: %s: not found\n", port_name_.c_str());
	return false;
    }

    if (verbose)
	printf("INFO: Reading from Firewire port %s\n", ports[i].name);

    if (raw1394_set_port(handle.get(), i))
    {
	perror("raw1394_set_port");
	return false;
    }

    if (raw1394_iso_recv_init(handle.get(), raw_receive, /*buf_packets=*/ 600,
			      /*max_packet_size=*/
			      CIF_HEADER_SIZE + CIF_PACKET_SIZE + 8,
			      /*channel=*/ 63,
			      /*mode=*/ RAW1394_DMA_DEFAULT,
			      /*irq_interval=*/ 100))
    {
	perror("raw1394_iso_recv_init");
	return false;
    }

    if (raw1394_iso_recv_start(handle.get(), -1, -1, -1))
    {
	perror("raw1394_iso_recv_start");
	raw1394_iso_shutdown(handle.get()); // see comment on destructor
	return false;
    }

    handle_ = handle;
    return true;
}
static int
gst_hdv1394src_discover_avc_node (GstHDV1394Src * src)
{
    int node = -1;
    int i, j = 0;
    int m = src->num_ports;

    if (src->port >= 0) {
        /* search on explicit port */
        j = src->port;
        m = j + 1;
    }

    /* loop over all our ports */
    for (; j < m && node == -1; j++) {
        raw1394handle_t handle;
        struct raw1394_portinfo pinf[16];

        /* open the port */
        handle = raw1394_new_handle ();
        if (!handle) {
            GST_WARNING ("raw1394 - failed to get handle: %s.\n", strerror (errno));
            continue;
        }
        if (raw1394_get_port_info (handle, pinf, 16) < 0) {
            GST_WARNING ("raw1394 - failed to get port info: %s.\n",
                         strerror (errno));
            goto next;
        }

        /* tell raw1394 which host adapter to use */
        if (raw1394_set_port (handle, j) < 0) {
            GST_WARNING ("raw1394 - failed to set set port: %s.\n", strerror (errno));
            goto next;
        }

        /* now loop over all the nodes */
        for (i = 0; i < raw1394_get_nodecount (handle); i++) {
            /* are we looking for an explicit GUID ? */
            if (src->guid != 0) {
                if (src->guid == rom1394_get_guid (handle, i)) {
                    node = i;
                    src->port = j;
                    g_free (src->uri);
                    src->uri = g_strdup_printf ("dv://%d", src->port);
                    break;
                }
            } else {
                rom1394_directory rom_dir;

                /* select first AV/C Tape Recorder Player node */
                if (rom1394_get_directory (handle, i, &rom_dir) < 0) {
                    GST_WARNING ("error reading config rom directory for node %d\n", i);
                    continue;
                }
                if ((rom1394_get_node_type (&rom_dir) == ROM1394_NODE_TYPE_AVC) &&
                        avc1394_check_subunit_type (handle, i, AVC1394_SUBUNIT_TYPE_VCR)) {
                    node = i;
                    src->port = j;
                    src->guid = rom1394_get_guid (handle, i);
                    g_free (src->uri);
                    src->uri = g_strdup_printf ("dv://%d", src->port);
                    g_free (src->device_name);
                    src->device_name = g_strdup (rom_dir.label);
                    break;
                }
                rom1394_free_directory (&rom_dir);
            }
        }
next:
        raw1394_destroy_handle (handle);
    }
    return node;
}
int main(int argc, char** argv)
{
    int rc; /**< return code */
    int port = 0;  /*!< fw handle port number */
    int nodeid = 0;   /*!< arm server node id */

    // parse command line (port number)
    opterr = 0;  // getopt no err output
    const char short_options[] = "hp:n:";
    int next_opt;
    do {
        next_opt = getopt(argc, argv, short_options);
        switch(next_opt)
        {
        case 'h':
            print_usage();
            return EXIT_SUCCESS;
            break;
        case 'p':
            port = atoi(optarg);
            break;
        case 'n':
            nodeid = atoi(optarg);
            break;
        case '?':
            std::cerr << "Invalid argument" << std::endl;
            break;
        default:
            break;
        }
    }
    while(next_opt != -1);


    // ----- Get handle and set port for the handle -------
    // create handle
    handle = raw1394_new_handle();
    if (handle == NULL) {
        std::cerr << "**** Error: could not create 1394 handle " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }

    // get port number & sanity check
    int numPorts = raw1394_get_port_info(handle, NULL, 0);
    if (port < 0 || port >= numPorts) {
        std::cerr << "Invalid port number" << std::endl;
        return EXIT_FAILURE;
    }

    // let user to choose which port to use
    rc = raw1394_set_port(handle, port);
    if (rc) {
        std::cerr << "**** Error: failed to set port " << strerror(errno) << std::endl;
        return EXIT_FAILURE;
    }


    // -------- Set FireWire bus reset handler --------

    // set bus reset handler
    raw1394_set_bus_reset_handler(handle, my_bus_reset_handler);



    // ----------------------------------------------------------------------------
    // Start tutorial 4 async broadcast
    // ----------------------------------------------------------------------------

    /**
     *
     */
    // ----- receving end -------------
    unsigned char channel = 0x5;
    raw1394_iso_dma_recv_mode mode = RAW1394_DMA_DEFAULT;

    raw1394_iso_recv_init(handle,
                          my_iso_recv_handler,
                          BUFFER,      // buf_packets
                          PACKET_MAX,  // max_packet_size
                          channel,     // channel
                          mode,        // dma mode
                          -1);         // irq_interval

    // start receiving
    raw1394_iso_recv_start(handle, -1, -1, 0);
    while (true)
    {
        rc = raw1394_loop_iterate(handle);
        if (rc) break;
    }

    // stop, clean up & exit
    raw1394_iso_stop(handle);
    raw1394_iso_shutdown(handle);
    raw1394_destroy_handle(handle);

    return EXIT_SUCCESS;
}