diff options
Diffstat (limited to 'v4l2-apps')
-rw-r--r-- | v4l2-apps/test/Makefile | 1 | ||||
-rw-r--r-- | v4l2-apps/test/parse_sniffusb2.pl | 84 | ||||
-rw-r--r-- | v4l2-apps/test/stress-buffer.c | 140 | ||||
-rw-r--r-- | v4l2-apps/util/Makefile | 10 | ||||
-rw-r--r-- | v4l2-apps/util/v4l2_sysfs_path.c | 191 |
5 files changed, 425 insertions, 1 deletions
diff --git a/v4l2-apps/test/Makefile b/v4l2-apps/test/Makefile index faafed14d..a996c0baf 100644 --- a/v4l2-apps/test/Makefile +++ b/v4l2-apps/test/Makefile @@ -9,6 +9,7 @@ binaries = ioctl-test \ v4lgrab \ driver-test \ pixfmt-test \ + stress-buffer \ capture_example .PHONY: all clean install diff --git a/v4l2-apps/test/parse_sniffusb2.pl b/v4l2-apps/test/parse_sniffusb2.pl new file mode 100644 index 000000000..34406b0ea --- /dev/null +++ b/v4l2-apps/test/parse_sniffusb2.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl +# +# Author: Franklin Meng <fmeng2002@yahoo.com> +# Parser for USB snoops captured from SniffUSB 2.0. +# + +use strict; +use warnings; +use Data::Dumper; + +foreach my $curfile (@ARGV) { + parsefile($curfile); + #we can only process 1 file + exit; +} + +sub parsefile { + my $curfile = shift; + my $SetupPacket =''; + my $preS = ''; + my $TransferBuffer =''; + my $preT = ''; + my $Direction =''; + my $preD = ''; + my @tmpsplit; + my $t=0; + my $s=0; + + open(FD, $curfile) || die("Error: $!\n"); + + while(<FD>) { + chomp; + if($t==1 && /^\s+\d{8}/) { +# print $_ . "\n"; + @tmpsplit = split(/\:\s/,$_); + $TransferBuffer = $TransferBuffer . $tmpsplit[1] . " "; + } elsif($s==1 && /^\s+\d{8}/) { +# print $_ . "\n"; + @tmpsplit = split(/\:\s/,$_); + $SetupPacket = $SetupPacket . $tmpsplit[1] ; + } else { + $t=0; + $s=0; + } + if(/[<>]{3}/){ + #print out last packet if valid + if($SetupPacket) { + if($preT) { + print "$SetupPacket $preD $preT\n"; + + } else { + print "$SetupPacket $Direction $TransferBuffer\n"; + } + } +# print "$SetupPacket $Direction $TransferBuffer\n"; + #clear variables + $preT = $TransferBuffer; + $TransferBuffer = ''; + $preS = $SetupPacket; + $SetupPacket = ''; + $preD = $Direction; + $t = 0; + $s = 0; + # get direction + @tmpsplit = split(/\s+/, $_); + $Direction = $tmpsplit[2]; +# print $_ . "\n"; + } elsif(/TransferBufferMDL/) { + $t = 1 + } elsif(/SetupPacket/) { + $s = 1; + } + } + #print last packet +# print "$SetupPacket $Direction $TransferBuffer\n"; + if($SetupPacket) { + if($preT) { + print "$SetupPacket $preD $preT\n"; + } else { + print "$SetupPacket $Direction $TransferBuffer\n"; + } + } +} + diff --git a/v4l2-apps/test/stress-buffer.c b/v4l2-apps/test/stress-buffer.c new file mode 100644 index 000000000..60410c70c --- /dev/null +++ b/v4l2-apps/test/stress-buffer.c @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2009 Douglas Schilling Landgraf <dougsland@redhat.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * stress-buffer tool makes infinite calls using read() for + * any device specified by the command argument. + * + * The size of buffer shall be a random number from 0 up to 1000. + * + * Also is automatically created a file called: stats-M-D-Y-h-m-s.txt + * in current directory with data executed. + * + * The stress test is performed by several read() calls, + * and it helped to identify real issues like: + * + * - memory leaks + * - specific crashs that are rare and hard to reproduce + * + * To compile: + * gcc -o stress-buffer stress-buffer.c -Wall + * + * To execute: + * ./stress-buffer /dev/device_for_test + * + * Example: + * ./stress-buffer /dev/video0 + */ + +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdio.h> +#include <sys/ioctl.h> +#include <stdlib.h> +#include <time.h> +#include <string.h> +#include <errno.h> + +int main(int argc, char **argv) +{ + char buffer[1000]; + char fname[30]; + int fd, ret, magic_buffer_size, cnt = 0; + time_t t1, t2; + double dif; + time_t current; + struct tm *timep; + + FILE *fd_file; + + if (argc != 2) { + printf("Usage: %s /dev/device_to_test\n", argv[0]); + return -1; + } + + current = time(NULL); + timep = localtime(¤t); + + memset(fname, 0, sizeof(fname)); + + snprintf(fname, sizeof(fname), "stats-%.2d-%.2d-%.2d-%.2d-%.2d-%.2d.txt", + timep->tm_mon+1, timep->tm_mday, timep->tm_year + 1900, + timep->tm_hour, timep->tm_min, timep->tm_sec); + + fd_file = fopen(fname, "a+"); + if (!fd_file) { + perror("error opening file"); + return -1; + } + + srand(time(NULL)); + + while (1) { + + if (time(&t1) < 0) { + perror("time_t t1"); + fclose(fd_file); + return -1; + } + + fd = open(argv[1], O_RDONLY); + if (fd < 0) { + perror("error opening device"); + fclose(fd_file); + return -1; + } + + /* Random number 0 - 1000 */ + magic_buffer_size = rand() % sizeof(buffer); + + memset(buffer, 0, sizeof(buffer)); + + ret = access(fname, W_OK); + if (ret < 0) { + close(fd); + perror("Error"); + return -1; + } + + ret = read(fd, buffer, magic_buffer_size); + if (ret < 0) { + fprintf(fd_file, "[%s] error reading buffer - [%s]\n", + argv[1], strerror(errno)); + fflush(fd_file); + perror("error reading buffer from device"); + return -1; + } + + if (time(&t2) < 0) { + perror("time_t t2"); + fclose(fd_file); + return -1; + } + + dif = difftime(t2, t1); + + printf("Seconds: [%d] - Test Number: [%d] - Read [%d] bytes\n", + (int)dif, cnt, ret); + + fprintf(fd_file, "Seconds: [%.2f] - Test number: [%d] - Read [%d] bytes\n", + dif, cnt, ret); + + fflush(fd_file); + + cnt++; + + close(fd); + + } + return 0; +} diff --git a/v4l2-apps/util/Makefile b/v4l2-apps/util/Makefile index b80de190b..f2c4c0a38 100644 --- a/v4l2-apps/util/Makefile +++ b/v4l2-apps/util/Makefile @@ -7,7 +7,7 @@ endif CPPFLAGS += -I../include -D_GNU_SOURCE LDFLAGS += -lm -binaries = v4l2-ctl v4l2-dbg v4l2-compliance ivtv-ctl cx18-ctl +binaries = v4l2-ctl v4l2-dbg v4l2-compliance ivtv-ctl cx18-ctl v4l2_sysfs_path ifeq ($(prefix),) prefix = /usr @@ -34,6 +34,14 @@ v4l2-dbg: v4l2-dbg.o v4l2-chipids.o v4l2-dbg.o: v4l2-dbg.h v4l2-dbg-bttv.h v4l2-dbg-em28xx.h v4l2-dbg-saa7134.h +v4l2_sysfs_path.o: v4l2_sysfs_path.c ../lib/v4l2_driver.h + +v4l2_sysfs_path: v4l2_sysfs_path.o ../lib/libv4l2.a + $(CC) $^ -o $@ -lsysfs + +../lib/libv4l2.a: + $(MAKE) -C ../lib libv4l2.a + install: mkdir -p $(prefix)/bin cp $(binaries) $(prefix)/bin diff --git a/v4l2-apps/util/v4l2_sysfs_path.c b/v4l2-apps/util/v4l2_sysfs_path.c new file mode 100644 index 000000000..2fe89b6c7 --- /dev/null +++ b/v4l2-apps/util/v4l2_sysfs_path.c @@ -0,0 +1,191 @@ +/* + Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@redhat.com> + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation version 2 of the License. + + The sysfs logic were adapted from a C++/Boost snippet code sent by + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "../lib/v4l2_driver.h" +#include <sysfs/libsysfs.h> +#include <errno.h> +#include <malloc.h> +#include <stdio.h> +#include <string.h> +#include <dirent.h> + +#define USB_ID "usb-" +#define PCI_ID "PCI:" +#define PCIe_ID "PCIe:" + +char *obtain_bus_sysfs_path(char *bus_info) +{ + struct sysfs_device *pcictl = NULL; + struct sysfs_bus *bus = NULL; + struct sysfs_device *busdev = NULL; + struct dlist *busdevs = NULL; + struct sysfs_device *pdev = NULL; + char *tmp = NULL, *busname, *buspath; + int pci; + + if (!strncmp(bus_info, USB_ID, strlen(USB_ID))) { + bus_info += strlen(USB_ID); + pci = 0; + } else if (!strncmp(bus_info, PCI_ID, strlen(PCI_ID))) { + bus_info += strlen(PCI_ID); + pci = 1; + } else if (!strncmp(bus_info, PCIe_ID, strlen(PCIe_ID))) { + bus_info += strlen(PCIe_ID); + pci = 1; + } else + return NULL; + + busname = strtok(bus_info, "-"); + if (!busname) + return NULL; + + buspath = strtok(NULL, "-"); + if (!buspath && !pci) + return NULL; + + /* open bus host controller */ + pcictl = sysfs_open_device("pci", busname); + if (!pcictl) + goto err; + + /* We have all we need for PCI devices */ + if (pci) { + char *name; + + asprintf(&name, "%s", pcictl->path); + return name; + } + + /* find matching usb bus */ + bus = sysfs_open_bus("usb"); + if (!bus) + goto err; + + busdevs = sysfs_get_bus_devices(bus); + if (!busdevs) + goto err; + + dlist_for_each_data(busdevs, busdev, struct sysfs_device) { + /* compare pathes of bus host controller and + parent of enumerated bus devices */ + + pdev = sysfs_get_device_parent(busdev); + if (!pdev) + continue; + + if (!strcmp(pcictl->path, pdev->path)) + break; + } + + if (!pdev) + goto err; + + sysfs_close_device(pcictl); + pcictl = NULL; + + /* assemble bus device path */ + if (busdev) { + struct sysfs_attribute *busnumattr; + unsigned int busnum; + char *name; + + busnumattr = sysfs_get_device_attr(busdev, "busnum"); + if (!busnumattr) + goto err; + + tmp = malloc(busnumattr->len + 1); + strncpy(tmp, busnumattr->value, busnumattr->len); + tmp[busnumattr->len] = '\0'; + + if (sscanf(tmp, "%u", &busnum) != 1) + goto err; + + asprintf(&name, "%s/%d-%s", busdev->path, + busnum, buspath); + + free(tmp); + sysfs_close_bus(bus); + + return name; + } + +err: + if (tmp) + free(tmp); + if (bus) + sysfs_close_bus(bus); + if (pcictl) + sysfs_close_device(pcictl); + + return NULL; +} + +void get_sysfs(char *fname) +{ + struct v4l2_driver drv; + char *path; + if (v4l2_open(fname, 0, &drv) < 0) { + perror(fname); + return; + } + + printf("device = %s\n", fname); + printf("bus info = %s\n", drv.cap.bus_info); + path = obtain_bus_sysfs_path((char *)drv.cap.bus_info); + if (path) { + printf("sysfs path = %s\n\n", path); + free(path); + } + + v4l2_close(&drv); +} + +void read_dir(char *dirname) +{ + DIR *dir; + struct dirent *entry; + const char *vid = "video"; + const char *rad = "radio"; + char *p, name[512]; + + dir = opendir(dirname); + if (!dir) + return; + + strcpy(name, dirname); + strcat(name, "/"); + p = name + strlen(name); + + entry = readdir(dir); + while (entry) { + if (!strncmp(entry->d_name, vid, strlen(vid)) || + !strncmp(entry->d_name, rad, strlen(rad))) { + strcpy(p, entry->d_name); + + get_sysfs(name); + } + entry = readdir(dir); + } + closedir(dir); +} + +int main(void) +{ + read_dir("/dev"); + return 0; +} |