home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / quicktime / libraw1394.2217 / testlibraw.c < prev   
C/C++ Source or Header  |  2000-11-29  |  5KB  |  168 lines

  1. /*
  2.  * libraw1394 - library for raw access to the 1394 bus with the Linux subsystem.
  3.  *
  4.  * Copyright (C) 1999,2000 Andreas Bombe
  5.  *
  6.  * This library is licensed under the GNU Lesser General Public License (LGPL),
  7.  * version 2.1 or later. See the file COPYING.LIB in the distribution for
  8.  * details.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <sys/poll.h>
  14.  
  15. #include "raw1394.h"
  16. #include "csr.h"
  17.  
  18.  
  19. #define TESTADDR (CSR_REGISTER_BASE + CSR_CYCLE_TIME)
  20.  
  21. const char not_compatible[] = "\
  22. This libraw1394 does not work with your version of Linux. You need a different
  23. version that matches your kernel (see kernel help text for the raw1394 option to
  24. find out which is the correct version).\n";
  25.  
  26. const char not_loaded[] = "\
  27. This probably means that you don't have raw1394 support in the kernel or that
  28. you haven't loaded the raw1394 module.\n";
  29.  
  30. quadlet_t buffer;
  31.  
  32. int my_tag_handler(raw1394handle_t handle, unsigned long tag, int error)
  33. {
  34.         if (error < 0) {
  35.                 printf("failed with error %d\n", error);
  36.         } else {
  37.                 printf("completed with 0x%08x, value 0x%08x\n", error, buffer);
  38.         }
  39.  
  40.         return 0;
  41. }
  42.  
  43. int my_fcp_handler(raw1394handle_t handle, nodeid_t nodeid, int response,
  44.                    size_t length, unsigned char *data)
  45. {
  46.         printf("got fcp %s from node %d of %d bytes:",
  47.                (response ? "response" : "command"), nodeid & 0x3f, length);
  48.  
  49.         while (length) {
  50.                 printf(" %02x", *data);
  51.                 data++;
  52.                 length--;
  53.         }
  54.  
  55.         printf("\n");
  56. }
  57.  
  58.  
  59. int main(int argc, char **argv)
  60. {
  61.         raw1394handle_t handle;
  62.         int i, numcards;
  63.         struct raw1394_portinfo pinf[16];
  64.  
  65.         tag_handler_t std_handler;
  66.         int retval;
  67.         
  68.         struct pollfd pfd;
  69.         unsigned char fcp_test[] = { 0x1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
  70.  
  71.         handle = raw1394_get_handle();
  72.  
  73.         if (!handle) {
  74.                 if (!errno) {
  75.                         printf(not_compatible);
  76.                 } else {
  77.                         perror("couldn't get handle");
  78.                         printf(not_loaded);
  79.                 }
  80.                 exit(1);
  81.         }
  82.  
  83.         printf("successfully got handle\n");
  84.         printf("current generation number: %d\n", raw1394_get_generation(handle));
  85.  
  86.         numcards = raw1394_get_port_info(handle, pinf, 16);
  87.         if (numcards < 0) {
  88.                 perror("couldn't get card info");
  89.                 exit(1);
  90.         } else {
  91.                 printf("%d card(s) found\n", numcards);
  92.         }
  93.  
  94.         if (!numcards) {
  95.                 exit(0);
  96.         }
  97.  
  98.         for (i = 0; i < numcards; i++) {
  99.                 printf("  nodes on bus: %2d, card name: %s\n", pinf[i].nodes,
  100.                        pinf[i].name);
  101.         }
  102.         
  103.         if (raw1394_set_port(handle, 0) < 0) {
  104.                 perror("couldn't set port");
  105.                 exit(1);
  106.         }
  107.  
  108.         printf("using first card found: %d nodes on bus, local ID is %d\n",
  109.                raw1394_get_nodecount(handle),
  110.                raw1394_get_local_id(handle) & 0x3f);
  111.  
  112.         printf("\ndoing transactions with custom tag handler\n");
  113.         std_handler = raw1394_set_tag_handler(handle, my_tag_handler);
  114.         for (i = 0; i < pinf[0].nodes; i++) {
  115.                 printf("trying to send read request to node %d... ", i);
  116.                 fflush(stdout);
  117.                 buffer = 0;
  118.  
  119.                 if (raw1394_start_read(handle, 0xffc0 | i, TESTADDR, 4,
  120.                                        &buffer, 0) < 0) {
  121.                         perror("failed");
  122.                         continue;
  123.                 }
  124.                 raw1394_loop_iterate(handle);
  125.         }
  126.  
  127.         printf("\nusing standard tag handler and synchronous calls\n");
  128.         raw1394_set_tag_handler(handle, std_handler);
  129.         for (i = 0; i < pinf[0].nodes; i++) {
  130.                 printf("trying to read from node %d... ", i);
  131.                 fflush(stdout);
  132.                 buffer = 0;
  133.  
  134.                 retval = raw1394_read(handle, 0xffc0 | i, TESTADDR, 4, &buffer);
  135.                 if (retval < 0) {
  136.                         printf("failed with error %d\n", retval);
  137.                 } else {
  138.                         printf("completed with 0x%08x, value 0x%08x\n", retval,
  139.                                buffer);
  140.                 }
  141.         }
  142.  
  143.         printf("\ntesting FCP monitoring on local node\n");
  144.         raw1394_set_fcp_handler(handle, my_fcp_handler);
  145.         raw1394_start_fcp_listen(handle);
  146.         retval = raw1394_write(handle, raw1394_get_local_id(handle),
  147.                                CSR_REGISTER_BASE + CSR_FCP_COMMAND, sizeof(fcp_test),
  148.                                (quadlet_t *)fcp_test);
  149.         retval = raw1394_write(handle, raw1394_get_local_id(handle),
  150.                       CSR_REGISTER_BASE + CSR_FCP_RESPONSE, sizeof(fcp_test),
  151.                       (quadlet_t *)fcp_test);
  152.  
  153.         printf("\npolling for leftover messages\n");
  154.         pfd.fd = raw1394_get_fd(handle);
  155.         pfd.events = POLLIN;
  156.         pfd.revents = 0;
  157.         while (1) {
  158.                 if (poll(&pfd, 1, 10) < 1) break;
  159.                 raw1394_loop_iterate(handle);
  160.         }
  161.  
  162.         if (retval < 0) {
  163.                 perror("poll failed");
  164.         }
  165.  
  166.         exit(0);
  167. }
  168.