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;
}
Esempio n. 2
0
void send_file_once(raw1394handle_t handle, int file)
{
        int count, i, ret;
        unsigned channel, tag, sy;
        size_t length;
        static unsigned char buffer[BUF_SIZE + BUF_HEAD];
        static unsigned int counter = 0;
        static int inited = 0;

        while (1) {
                count = read(file, buffer, BUF_HEAD);
                if (count < 0) {
                        perror("read");
                        exit(1);
                }
                if (count < BUF_HEAD)
                        return;
        
                i = 0;
                length = ((unsigned int *)buffer)[i];
                channel = buffer[i + 4];
                tag = buffer[i + 5];
                sy = buffer[i + 6];
        
                i += BUF_HEAD;
                while (count < length + BUF_HEAD) {
                        ret = read(file, buffer + count,
                                   length - count + BUF_HEAD);

                        if (ret < 0) {
                                perror("read");
                                exit(1);

                        }
                        if (ret == 0)
                                return;

                        count += ret;
                }
                
                if (inited == 0) {
                        /*
                        fprintf(stderr, "transmitting first packet with length "
                             "%d on channel %d with tag %d and sy %d\n",
                                length, channel, tag, sy);
                        */
                        ret = raw1394_iso_xmit_init(handle, NULL, BUFFER,
                                PACKET_MAX, channel, speed, -1);
                        if (ret < 0) {
                                perror("raw1394_iso_xmit_init");
                                exit(1);
                        }
                        raw1394_iso_xmit_start(handle, -1, -1);
                        inited = 1;
                }

                if (++counter % 1000 == 0)
                        fprintf(stderr, "\r%uK packets", counter/1000);

                ret = raw1394_iso_xmit_write(handle, &buffer[i],
                        length, tag, sy);
                if (ret < 0) {
                        perror("\nraw1394_iso_xmit_write");
                        exit(1);
                }
        }
}