diff options
| author | 2001-06-08 00:31:12 +0000 | |
|---|---|---|
| committer | 2001-06-08 00:31:12 +0000 | |
| commit | 135babee19a43abe7fb48cddf334473de0322643 (patch) | |
| tree | 91d9da667ace98f196a175912a9d68766800c2e2 /tools | |
| parent | debian: Remove a now useless substvar. (diff) | |
Moved testlibraw.c from src to tools directory.
Added sendiso and dumpiso programs in tools directory.
Added man pages for sendiso and dumpiso in doc directory.
git-svn-id: svn://svn.linux1394.org/libraw1394/trunk@66 53a565d1-3bb7-0310-b661-cf11e63c67ab
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/Makefile.am | 4 | ||||
| -rw-r--r-- | tools/dumpiso.c | 236 | ||||
| -rw-r--r-- | tools/sendiso.c | 294 | ||||
| -rw-r--r-- | tools/testlibraw.c | 171 |
4 files changed, 705 insertions, 0 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am new file mode 100644 index 0000000..eaaa7ca --- /dev/null +++ b/tools/Makefile.am @@ -0,0 +1,4 @@ + +# testlibraw +bin_PROGRAMS = testlibraw sendiso dumpiso +LDADD = ../src/libraw1394.la diff --git a/tools/dumpiso.c b/tools/dumpiso.c new file mode 100644 index 0000000..2fcef78 --- /dev/null +++ b/tools/dumpiso.c @@ -0,0 +1,236 @@ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <getopt.h> +#include <errno.h> +#include <fcntl.h> + +#include "../src/raw1394.h" + + +u_int64_t listen_channels; +unsigned long which_port; +char *filename; +int file; + +int done; + +void usage_exit(int exitcode) +{ + fprintf(stderr, +"Usage: dumpiso [opts] [FILE]\n" +"Dump IEEE 1394 isochronous channels to FILE or standard output.\n" +"\n" +"-c --channels CHANNELS Listen on these channels; CHANNELS is either a\n" +" number X or a range X-Y.\n" +"-p --port PORT Choose 1394 chip PORT. (default: 0)\n" +"-h --help Show this help.\n" +); + + exit(exitcode); +} + +void parse_args(int argc, char **argv) +{ + int i; + char *tail; + unsigned long chan1, chan2; + + int c; + int index; + static struct option opts[] = { + { "channels", required_argument, NULL, 'c' }, + { "port", required_argument, NULL, 'p' }, + { "help", no_argument, NULL, 'h' }, + { 0 } + }; + + while (1) { + c = getopt_long(argc, argv, "hc:p:", opts, &index); + if (c == -1) break; + + switch (c) { + case 'c': + chan1 = strtoul(optarg, &tail, 10); + chan2 = chan1; + + if (*tail) { + if (tail[0] != '-' || !tail[1]) { + fprintf(stderr, + "invalid argument to channels: %s\n", + optarg); + usage_exit(1); + } + + tail++; + chan2 = strtoul(tail, &tail, 10); + if (*tail) { + fprintf(stderr, + "invalid argument to channels: %s\n", + optarg); + usage_exit(1); + } + } + + if (chan2 < chan1) { + unsigned long x = chan1; + chan1 = chan2; + chan2 = x; + } + + if (chan2 > 63) { + fprintf(stderr, + "invalid channel numbers: %s\n", + optarg); + exit(1); + } + + for (i = chan1; i <= chan2; i++) + listen_channels |= 1ULL << i; + + break; + case 'p': + which_port = strtoul(optarg, &tail, 10); + if (*tail) { + fprintf(stderr, + "invalid argument to port: %s\n", + optarg); + usage_exit(1); + } + break; + case 'h': + usage_exit(0); + case '?': + usage_exit(1); + default: + abort(); + } + } + + argv += optind; + argc -= optind; + + if (argc > 1) { + fprintf(stderr, "Too many arguments.\n"); + usage_exit(1); + } + + if (argc) filename = *argv; + + if (!listen_channels) listen_channels = ~0ULL; +} + +void write_header() +{ + static char header[32] = "1394 isodump v1"; + int i; + + for (i = 0; i < 8; i++) + header[i+16] = (listen_channels >> (56 - 8*i)) & 0xff; + + i = 0; + while (i < 32) { + int ret; + ret = write(file, header + i, 32 - i); + + if (ret < 0) { + perror("header write"); + exit(1); + } + + i += ret; + } +} + +void open_dumpfile() +{ + if (!filename || !filename[0] || (filename[0] == '-' && !filename[1])) { + file = fileno(stdout); + write_header(); + return; + } + + file = open(filename, O_CREAT | O_WRONLY, 0666); + if (file < 0) { + perror("dumpfile open"); + exit(1); + } + + ftruncate(file, 0); + write_header(); +} + +int iso_packet_handler(raw1394handle_t handle, int channel, size_t length, + quadlet_t *data) +{ + int ret; + static unsigned int count; + + count++; + fprintf(stderr, "\r%u", count); + fflush(stderr); + + while (length) { + ret = write(file, data, length); + if (ret < 0) { + perror("data write"); + exit(1); + } + + length -= ret; + data = (quadlet_t *)(((char *)data) + ret); + } + + return 0; +} + +int main(int argc, char **argv) +{ + raw1394handle_t handle; + int i; + + parse_args(argc, argv); + + fprintf(stderr, "port: %ld\nchannels: %#016llx\nfile: %s\n", which_port, + listen_channels, 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); + } + + open_dumpfile(); + + for (i = 0; i < 64; i++) { + if (!(listen_channels & 1ULL << i)) continue; + + raw1394_set_iso_handler(handle, i, iso_packet_handler); + raw1394_start_iso_rcv(handle, i); + } + + /* This should actually do something with the done variable, and set up + signal handlers. */ + while (!done) raw1394_loop_iterate(handle); + + return 0; +} diff --git a/tools/sendiso.c b/tools/sendiso.c new file mode 100644 index 0000000..743c83e --- /dev/null +++ b/tools/sendiso.c @@ -0,0 +1,294 @@ +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <getopt.h> +#include <errno.h> +#include <fcntl.h> + +#include "../src/raw1394.h" + + +unsigned long which_port; +char *filename; + +unsigned long loopcount = 1; +unsigned int speed; +unsigned long packetcount; +volatile unsigned int pend_req; + +void usage_exit(int exitcode) +{ + fprintf(stderr, +"Usage: sendiso [opts] FILE\n" +"Send IEEE 1394 isochronous packets from dump file FILE.\n" +"\n" +"-l --loop COUNT Repeat sending data COUNT times.\n" +"-i --infinite Repeat sending data infinitely.\n" +"-s --speed SPEED Send data at SPEED (valid values are 100, 200, 400 or\n" +" alternatively 1, 2, 4). (default: 100)\n" +"\n" +"-p --port PORT Choose 1394 chip PORT. (default: 0)\n" +"-h --help Show this help.\n" +); + + exit(exitcode); +} + +void parse_args(int argc, char **argv) +{ + char *tail; + + int c; + int index; + static struct option opts[] = { + { "file", required_argument, NULL, 'f' }, + { "loop", required_argument, NULL, 'l' }, + { "infinite", no_argument, NULL, 'i' }, + { "speed", required_argument, NULL, 's' }, + { "port", required_argument, NULL, 'p' }, + { "help", no_argument, NULL, 'h' }, + { 0 } + }; + + while (1) { + c = getopt_long(argc, argv, "f:l:is:p:h", opts, &index); + if (c == -1) break; + + switch (c) { + case 'f': + filename = optarg; + break; + case 'l': + loopcount = strtoul(optarg, &tail, 10); + if (*tail) { + fprintf(stderr, + "invalid argument to loop: %s\n", + optarg); + usage_exit(1); + } + break; + case 'i': + loopcount = 0; + break; + case 's': + speed = strtoul(optarg, &tail, 10); + if (*tail) speed = -1; + + switch (speed) { + case 1: + case 100: + speed = 0; + break; + case 2: + case 200: + speed = 1; + break; + case 4: + case 400: + speed = 2; + break; + default: + fprintf(stderr, + "invalid argument to speed: %s\n", + optarg); + usage_exit(1); + } + break; + case 'p': + which_port = strtoul(optarg, &tail, 10); + if (*tail) { + fprintf(stderr, + "invalid argument to port: %s\n", + optarg); + usage_exit(1); + } + break; + case 'h': + usage_exit(0); + case '?': + usage_exit(1); + case 0: + break; + default: + abort(); + } + } + + argv += optind; + argc -= optind; + + if (argc > 1) { + fprintf(stderr, "Too many arguments.\n"); + usage_exit(1); + } + + if (argc) filename = *argv; +} + + +int dec_int_callback(raw1394handle_t unused, int *counter, int unused_errcode) +{ + (*counter)--; + packetcount++; + return 0; +} + +#define BUF_SIZE 65536 +#define BUF_OVER BUF_SIZE +void send_file_once(raw1394handle_t handle, int file) +{ + int count, i, ret; + unsigned channel, tag, sy; + size_t length; + static char buffer[BUF_SIZE + BUF_OVER]; + + static struct raw1394_reqhandle rh = { + (req_callback_t)dec_int_callback, + &pend_req + }; + + while (1) { + while (pend_req > 30) raw1394_loop_iterate(handle); + + count = read(file, buffer, BUF_SIZE); + if (count < 0) { + perror("read"); + exit(1); + } + if (count < 4) return; + + i = 0; + while (i < count) { + length = (buffer[i] << 8) | buffer[i + 1]; + channel = buffer[i + 2] & 0x3f; + tag = buffer[i + 2] >> 6; + sy = buffer[i + 3] & 0xf; + + i += 4; + while (i + length > count) { + ret = read(file, buffer + BUF_SIZE, + i + length - BUF_SIZE); + + if (ret < 0) { + perror("read"); + exit(1); + } + + if (ret == 0) return; + + count += ret; + } + + raw1394_start_iso_write(handle, channel, tag, sy, + speed, length, + (quadlet_t *)(buffer + i), + (unsigned long)&rh); + i += length; + pend_req++; + } + } +} + + +void send_iso_file(raw1394handle_t handle) +{ + int file; + int count, ret; + char buffer[32]; + + if (filename[0] == '-' && filename[1] == '\0') { + file = fileno(stdin); + } else { + file = open(filename, O_RDONLY, 0); + + if (file < 0) { + perror("open"); + exit(1); + } + } + + count = 32; + while (count) { + ret = read(file, buffer, count); + + if (!ret) goto bad_format; + + if (ret < 0) { + perror("read"); + exit(1); + } + + count -= ret; + } + + if (memcmp("1394 isodump v", buffer, 14)) goto bad_format; + if (buffer[14] != '1') goto wrong_version; + + while (1) { + send_file_once(handle, file); + + if (!loopcount) { + if (lseek(file, 32, SEEK_SET) < 0) { + perror("lseek"); + exit(1); + } + continue; + } + if (!(--loopcount)) break; + + if (lseek(file, 32, SEEK_SET) < 0) { + perror("lseek"); + exit(1); + } + } + + return; + +bad_format: + fprintf(stderr, "Input file format not recognized.\n"); + exit(1); + +wrong_version: + fprintf(stderr, "Format version of input file not supported.\n"); + exit(1); +} + + +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); + + return 0; +} diff --git a/tools/testlibraw.c b/tools/testlibraw.c new file mode 100644 index 0000000..21c2de2 --- /dev/null +++ b/tools/testlibraw.c @@ -0,0 +1,171 @@ +/* + * libraw1394 - library for raw access to the 1394 bus with the Linux subsystem. + * + * Copyright (C) 1999,2000 Andreas Bombe + * + * This library is licensed under the GNU Lesser General Public License (LGPL), + * version 2.1 or later. See the file COPYING.LIB in the distribution for + * details. + */ + +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <sys/poll.h> + +#include "../src/raw1394.h" +#include "../src/csr.h" + + +#define TESTADDR (CSR_REGISTER_BASE + CSR_CYCLE_TIME) + +const char not_compatible[] = "\ +This libraw1394 does not work with your version of Linux. You need a different +version that matches your kernel (see kernel help text for the raw1394 option to +find out which is the correct version).\n"; + +const char not_loaded[] = "\ +This probably means that you don't have raw1394 support in the kernel or that +you haven't loaded the raw1394 module.\n"; + +quadlet_t buffer; + +int my_tag_handler(raw1394handle_t handle, unsigned long tag, + raw1394_errcode_t errcode) +{ + int err = raw1394_errcode_to_errno(errcode); + + if (err) { + printf("failed with error: %s\n", strerror(err)); + } else { + printf("completed with value 0x%08x\n", buffer); + } + + return 0; +} + +int my_fcp_handler(raw1394handle_t handle, nodeid_t nodeid, int response, + size_t length, unsigned char *data) +{ + printf("got fcp %s from node %d of %d bytes:", + (response ? "response" : "command"), nodeid & 0x3f, length); + + while (length) { + printf(" %02x", *data); + data++; + length--; + } + + printf("\n"); + + return 0; +} + + +int main(int argc, char **argv) +{ + raw1394handle_t handle; + int i, numcards; + struct raw1394_portinfo pinf[16]; + + tag_handler_t std_handler; + int retval; + + struct pollfd pfd; + unsigned char fcp_test[] = { 0x1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; + + handle = raw1394_new_handle(); + + if (!handle) { + if (!errno) { + printf(not_compatible); + } else { + perror("couldn't get handle"); + printf(not_loaded); + } + exit(1); + } + + printf("successfully got handle\n"); + printf("current generation number: %d\n", raw1394_get_generation(handle)); + + numcards = raw1394_get_port_info(handle, pinf, 16); + if (numcards < 0) { + perror("couldn't get card info"); + exit(1); + } else { + printf("%d card(s) found\n", numcards); + } + + if (!numcards) { + exit(0); + } + + for (i = 0; i < numcards; i++) { + printf(" nodes on bus: %2d, card name: %s\n", pinf[i].nodes, + pinf[i].name); + } + + if (raw1394_set_port(handle, 0) < 0) { + perror("couldn't set port"); + exit(1); + } + + printf("using first card found: %d nodes on bus, local ID is %d, IRM is %d\n", + raw1394_get_nodecount(handle), + raw1394_get_local_id(handle) & 0x3f, + raw1394_get_irm_id(handle) & 0x3f); + + printf("\ndoing transactions with custom tag handler\n"); + std_handler = raw1394_set_tag_handler(handle, my_tag_handler); + for (i = 0; i < pinf[0].nodes; i++) { + printf("trying to send read request to node %d... ", i); + fflush(stdout); + buffer = 0; + + if (raw1394_start_read(handle, 0xffc0 | i, TESTADDR, 4, + &buffer, 0) < 0) { + perror("failed"); + continue; + } + raw1394_loop_iterate(handle); + } + + printf("\nusing standard tag handler and synchronous calls\n"); + raw1394_set_tag_handler(handle, std_handler); + for (i = 0; i < pinf[0].nodes; i++) { + printf("trying to read from node %d... ", i); + fflush(stdout); + buffer = 0; + + retval = raw1394_read(handle, 0xffc0 | i, TESTADDR, 4, &buffer); + if (retval < 0) { + perror("failed with error"); + } else { + printf("completed with value 0x%08x\n", buffer); + } + } + + printf("\ntesting FCP monitoring on local node\n"); + raw1394_set_fcp_handler(handle, my_fcp_handler); + raw1394_start_fcp_listen(handle); + raw1394_write(handle, raw1394_get_local_id(handle), + CSR_REGISTER_BASE + CSR_FCP_COMMAND, sizeof(fcp_test), + (quadlet_t *)fcp_test); + raw1394_write(handle, raw1394_get_local_id(handle), + CSR_REGISTER_BASE + CSR_FCP_RESPONSE, sizeof(fcp_test), + (quadlet_t *)fcp_test); + + printf("\npolling for leftover messages\n"); + pfd.fd = raw1394_get_fd(handle); + pfd.events = POLLIN; + pfd.revents = 0; + while (1) { + retval = poll(&pfd, 1, 10); + if (retval < 1) break; + raw1394_loop_iterate(handle); + } + + if (retval < 0) perror("poll failed"); + exit(0); +} |
