Ticket #5905: fw_tester.2.patch

File fw_tester.2.patch, 4.7 KB (added by mythtv@…, 15 years ago)

firewire_tester patch

  • mythtv/contrib/development/firewire_tester/firewire_tester.c

     
    1919#define ACTION_TEST_P2P     1
    2020#define ACTION_FIX_BCAST    2
    2121#define ACTION_RESET_BUS    3
     22#define ACTION_OUTPUT_P2P   4
    2223
    2324#define SYNC_BYTE           0x47
    2425#define MIN_PACKETS         25
     
    2930int verbose = 0;
    3031int sync_failed = 0;
    3132int nodata = 0;
     33int run = 0;
    3234
    3335static int read_packet (unsigned char *tspacket, int len,
    3436                        unsigned int dropped, void *callback_data)
     
    5153    return 1;
    5254}
    5355
     56static int fwrite_packet (unsigned char *tspacket, int len,
     57                        unsigned int dropped, void *callback_data)
     58{
     59    int n;
     60    FILE *fp = (FILE *) callback_data;
     61
     62    if (dropped)
     63    {
     64        fprintf(stderr, "Dropped %d packet(s).\n", dropped);
     65        return 0;
     66    }
     67
     68    if (tspacket[0] != SYNC_BYTE)
     69    {
     70        sync_failed = 1;
     71        return 0;
     72    }
     73    nodata = 0;
     74    n = fwrite(tspacket, sizeof(char), 188, fp);
     75    //fprintf(stderr, "fwrite_packets: wrote %d of %d\n", n, len);
     76    return(n == len ? 0 : 1);
     77}
     78
    5479int test_connection(raw1394handle_t handle, int channel)
    5580{
    5681    int count = 0;
     
    210235    return 0;
    211236}
    212237
     238// create and test a p2p connection, copy packets to stdout
     239// returns 1 on success, 0 on failure
     240int output_p2p(raw1394handle_t handle, nodeid_t node) {
     241    int channel, count, success = 0;
     242    int retry = 0;
     243    FILE *fp = stdout;
     244    iec61883_mpeg2_t mpeg;
     245    struct timeval tv;
     246    fd_set rfds;
     247    channel = node;
     248    int fd = raw1394_get_fd(handle);
     249
     250    sync_failed = 0;
     251
     252    VERBOSE("P2P: Creating, node %d, channel %d\n", node, channel);
     253    fflush(stdout);
     254 
     255    // make connection
     256    if (iec61883_cmp_create_p2p_output(handle, node | 0xffc0, 0, channel,
     257                                       1 /* fix me, speed */ ) != 0)
     258    {
     259        fprintf(stderr, "iec61883_cmp_create_p2p_output failed\n");
     260        return 0;
     261    }
     262    mpeg = iec61883_mpeg2_recv_init(handle, fwrite_packet, (void*) fp);
     263    run = 1;
     264    iec61883_mpeg2_recv_start(mpeg, channel);
     265    while(run && retry < 2 && !sync_failed && nodata < MAX_NODATA)
     266    {
     267        FD_ZERO(&rfds);
     268        FD_SET(fd, &rfds);
     269        tv.tv_sec = 1;
     270        tv.tv_usec = 0;
     271
     272        if (select(fd + 1, &rfds, NULL, NULL, &tv) > 0)
     273        {
     274             nodata++;
     275             raw1394_loop_iterate(handle);
     276        }
     277        else
     278        {
     279            retry++;
     280        }
     281    }
     282    iec61883_mpeg2_recv_stop(mpeg);
     283    iec61883_mpeg2_close(mpeg);
     284
     285    if (sync_failed)
     286        return 0;
     287
     288    return count;
     289
     290    VERBOSE("P2P: Disconnecting.\n");
     291    iec61883_cmp_disconnect(handle, node | 0xffc0, 0,
     292                            raw1394_get_local_id (handle),
     293                            -1, channel, 0);
     294    return success;
     295}
     296
    213297void usage(void) {
    214298    printf("firewire_tester <action> -n <node> [-P <port>] [-r <n>] [-v]\n");
    215299    printf(" Actions: (one is required)\n");
     
    219303    printf("    -R          - reset the firewire bus\n");
    220304    printf(" Options\n");
    221305    printf("    -n <node>   - firewire node, required\n");
     306    printf("    -o          - write packets to stdout using p2p connection\n");
    222307    printf("    -P <port>   - firewire port, default 0\n");
    223308    printf("    -r <n>      - run action <n> times, default 1\n");
    224309    printf("    -v          - verbose\n");
     
    234319    int action = ACTION_NONE;
    235320
    236321    opterr = 0;
    237     while ((c = getopt(argc, argv, "Bbn:pP:r:Rv")) != -1)
     322    while ((c = getopt(argc, argv, "Bbn:opP:r:Rv")) != -1)
    238323    {
    239324        switch (c)
    240325        {
     
    269354                }
    270355                break;
    271356
     357            // output packets using p2p connection
     358            case 'o':
     359                if (action != ACTION_NONE)
     360                {
     361                    printf("Invalid command line\n");
     362                    usage();
     363                }
     364                action = ACTION_OUTPUT_P2P;
     365                break;
     366
    272367            // set the port, optional
    273368            case 'P':
    274369                port = atoi(optarg);
     
    368463                printf("Bus reset failed: %d\n", errno);
    369464            }
    370465            break;
     466        case ACTION_OUTPUT_P2P:
     467            runs = 1;
     468            fprintf(stderr,
     469                    "Action: Output P2P packets using node %d, channel %d\n",
     470                    node, node);
     471            success = output_p2p(handle, node);
     472            break;
    371473    }
    372474
    373475    VERBOSE("raw1394: Releasing handle.\n");