예제 #1
0
int main(int argc, char **argv)
{
	struct ut_cache **cache = NULL;
	int num_cache = 0;
	int c = 0;
	int action = UT_NO_TASK;
	int interactive = false;
	int game_type = 0;

	char *home_dir;
	home_dir = getenv("HOME");

	char cache_file[MAXLEN];
	memset( cache_file, 0, sizeof(cache_file));
	char tmp_cache[MAXLEN];
	memset( tmp_cache, 0, sizeof(tmp_cache));
	char tmp_config[MAXLEN];
	memset( tmp_config, 0, sizeof(tmp_config));
	char tmp_move[MAXLEN];
	memset( tmp_move, 0, sizeof(tmp_move));

	cache_dir = NULL;
	move_dir = NULL;
	config_dir = NULL;

	/* Handle args */
	while( (c = getopt_long(argc, argv, "adhivb:c:", long_options, NULL)) != -1 ) {
		switch (c) {
		case 'a':
			if(!action)
				action = UT_MOVE;
			else
				usage("can't move and delete all.");
			break;
		case 'b':
			move_dir = optarg;
			break;
		case 'c':
			config_dir = optarg;
			break;
		case 'd':
			if(!action)
				action = UT_DELETE;
			else
				usage("can't move and delete all.");
			break;
		case 'i':
			interactive = true;
			break;
		case 'v':
			//TODO: verbose echos move or delete action
			break;
		case UT99:
			if(!game_type)
				game_type = UT99;
			else
				usage("only one game type at a time");
			break;
		case UT2003:
			if(!game_type)
				game_type = UT2003;
			else
				usage("only one game type at a time");
			break;
		case UT2004:
			if(!game_type)
				game_type = UT2004;
			else
				usage("only one game type at a time");
			break;
		case 'h':
		default:
			usage(ut_cache_usage_string);
		}
	}

	if( config_dir == NULL ) {
		switch(game_type) {
		case UT99:
			memcpy( tmp_config, home_dir, strlen(home_dir) * sizeof(char) );
			strcat( tmp_config, "/.loki/ut");
			config_dir = tmp_config;
			break;
		case UT2003:
			memcpy( tmp_config, home_dir, strlen(home_dir) * sizeof(char) );
			strcat( tmp_config, "/.ut2003");
			config_dir = tmp_config;
			break;
		case UT2004:
			memcpy( tmp_config, home_dir, strlen(home_dir) * sizeof(char) );
			strcat( tmp_config, "/.ut2004");
			config_dir = tmp_config;
			break;
		default:
			usage(ut_cache_usage_string);
			break;
		}
	}
	memcpy( tmp_cache, config_dir, strlen(config_dir) * sizeof(char));
	strcat( tmp_cache, "/Cache" );
	cache_dir = tmp_cache;
	fprintf(stderr, "using \"%s\" for cache directory\n", cache_dir);

	if( move_dir == NULL ) {
		move_dir = config_dir;
		fprintf(stderr, "no move directory specified. using \"%s\"\n", move_dir);
	}

	memcpy( cache_file, cache_dir, strlen(cache_dir) * sizeof(char));
	strcat( cache_file, "/cache.ini" );
	cache = read_cache( cache_file, &num_cache);
	if( cache == NULL ) {
		return 1;
	}

	if( interactive ) {
		do_interactive( cache, num_cache );
	}else{
		do_all( cache, num_cache, action );
	}

	return 0;
}
int
wl_lib(char *input_str)
{
    struct ifreq ifr;
    char *ifname = NULL;
    int err = 0;
    int help = 0;
    int status = CMD_WL;
    void* serialHandle = NULL;
    char *tmp_argv[LINUX_NUM_ARGS];
    char **argv = tmp_argv;
    int argc;

    /* buf_to_args return 0 if no args or string too long
     * or return NDIS_NUM_ARGS if too many args
     */
    if (((argc = buf_to_args(input_str, argv)) == 0) || (argc == LINUX_NUM_ARGS)) {
        printf("wl:error: can't convert input string\n");
        return (-1);
    }
#else
/* Main client function */
int
main(int argc, char **argv)
{
    struct ifreq ifr;
    char *ifname = NULL;
    int err = 0;
    int help = 0;
    int status = CMD_WL;
#if defined(RWL_DONGLE) || RWL_SERIAL
    void* serialHandle = NULL;
#endif

#endif /* WLMSO */
    wlu_av0 = argv[0];

    wlu_init();
    memset(&ifr, 0, sizeof(ifr));
    (void)*argv++;

    if ((status = wl_option(&argv, &ifname, &help)) == CMD_OPT) {
        if (ifname)
        {
            if (strcmp(ifname, "eth1") == 0)
            {
                strncpy(ifr.ifr_name, "wl0", IFNAMSIZ);
            }
            else if (strcmp(ifname, "eth2") == 0)
            {
                strncpy(ifr.ifr_name, "wl1", IFNAMSIZ);
            }
            else
            {
                strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
            }
        }
        /* Bug fix: If -h is used as an option, the above function call
         * will notice it and raise the flag but it won't be processed
         * in this function so we undo the argv increment so that the -h
         * can be spotted by the next call of wl_option. This will ensure
         * that wl -h [cmd] will function as desired.
         */
        else if (help)
            (void)*argv--;
    }

    /* Linux client looking for a indongle reflector */
    if (*argv && strncmp (*argv, "--indongle", strlen(*argv)) == 0) {
        rwl_dut_autodetect = FALSE;
        (void)*argv++;
    }
    /* Linux client looking for a WinVista server */
    if (*argv && strncmp (*argv, "--vista", strlen(*argv)) == 0) {
        rwl_os_type = WINVISTA_OS;
        rwl_dut_autodetect = FALSE;
        (void)*argv++;
    }

    /* Provide option for disabling remote DUT autodetect */
    if (*argv && strncmp(*argv, "--nodetect", strlen(*argv)) == 0) {
        rwl_dut_autodetect = FALSE;
        argv++;
    }

    if (*argv && strncmp (*argv, "--debug", strlen(*argv)) == 0) {
        debug = TRUE;
        argv++;
    }

    /* RWL socket transport Usage: --socket ipaddr/hostname [port num] */
    if (*argv && strncmp (*argv, "--socket", strlen(*argv)) == 0) {
        (void)*argv++;

        remote_type = REMOTE_SOCKET;

        if (!(*argv)) {
            rwl_usage(remote_type);
            return err;
        }
        /* IP address validation is done in client_shared file */
        g_rwl_servIP = *argv;
        (void)*argv++;

        g_rwl_servport = DEFAULT_SERVER_PORT;
        if ((*argv) && isdigit(**argv)) {
            g_rwl_servport = atoi(*argv);
            (void)*argv++;
        }
    }

    /* RWL from system serial port on client to uart serial port on server */
    /* Usage: --serial /dev/ttyS0 */
    if (*argv && strncmp (*argv, "--serial", strlen(*argv)) == 0) {
        (void)*argv++;
        remote_type = REMOTE_SERIAL;
    }

    /* RWL from system serial port on client to uart dongle port on server */
    /* Usage: --dongle /dev/ttyS0 */
    if (*argv && strncmp (*argv, "--dongle", strlen(*argv)) == 0) {
        (void)*argv++;
        remote_type = REMOTE_DONGLE;
    }

#if defined(RWL_SERIAL) || defined(RWL_DONGLE)
    if (remote_type == REMOTE_SERIAL || remote_type == REMOTE_DONGLE) {
        if (!(*argv)) {
            rwl_usage(remote_type);
            return err;
        }
        g_rwl_device_name_serial = *argv;
        (void)*argv++;
        if ((serialHandle = rwl_open_pipe(remote_type, g_rwl_device_name_serial, 0, 0))
                == NULL) {
            DPRINT_ERR(ERR, "serial device open error\r\n");
            return -1;
        }
        ifr = (*(struct ifreq *)serialHandle);
    }
#endif /*  RWL_SERIAL */

    /* RWL over wifi.  Usage: --wifi mac_address */
    if (*argv && strncmp (*argv, "--wifi", strlen(*argv)) == 0) {
        (void)*argv++;
        /* use default interface */
        if (!*ifr.ifr_name)
            wl_find(&ifr);
        /* validate the interface */
        if (!*ifr.ifr_name) {
            errno = ENXIO;
            syserr("interface");
        }
        if ((err = wl_check((void *)&ifr)) < 0) {
            fprintf(stderr, "%s: wl driver adapter not found\n", wlu_av0);
            exit(1);
        }

        remote_type = REMOTE_WIFI;

        if (argc < 4) {
            rwl_usage(remote_type);
            return err;
        }
        /* copy server mac address to local buffer for later use by findserver cmd */
        if (!wl_ether_atoe(*argv, (struct ether_addr *)g_rwl_buf_mac)) {
            fprintf(stderr,
                    "could not parse as an ethternet MAC address\n");
            return FAIL;
        }
        (void)*argv++;
    }

    if ((*argv) && (strlen(*argv) > 2) &&
            (strncmp(*argv, "--interactive", strlen(*argv)) == 0)) {
        interactive_flag = 1;
    }

    /* Process for local wl */
    if (remote_type == NO_REMOTE) {
        if (interactive_flag == 1)
            (void)*argv--;
        err = process_args(&ifr, argv);
        return err;
    } else {
#ifndef OLYMPIC_RWL
        /* Autodetect remote DUT */
        if (rwl_dut_autodetect == TRUE)
            rwl_detect((void*)&ifr, debug, &rwl_os_type);
#endif /* OLYMPIC_RWL */
    }

    /* RWL client needs to initialize ioctl_version */
    if (wl_check((void *)&ifr) != 0) {
        fprintf(stderr, "%s: wl driver adapter not found\n", wlu_av0);
        exit(1);
    }

    if (interactive_flag == 1) {
        err = do_interactive(&ifr);
        return err;
    }

    if ((*argv) && (interactive_flag == 0)) {
        err = process_args(&ifr, argv);
        if ((err == SERIAL_PORT_ERR) && (remote_type == REMOTE_DONGLE)) {
            DPRINT_ERR(ERR, "\n Retry again\n");
            err = process_args((struct ifreq*)&ifr, argv);
        }
        return err;
    }
    rwl_usage(remote_type);
#if defined(RWL_DONGLE) || RWL_SERIAL
    if (remote_type == REMOTE_DONGLE || remote_type == REMOTE_SERIAL)
        rwl_close_pipe(remote_type, (void*)&ifr);
#endif /* RWL_DONGLE || RWL_SERIAL */
    return err;
}

/*
 * Function called for  'local' execution and for 'remote' non-interactive session
 * (shell cmd, wl cmd)
 */
int
process_args(struct ifreq* ifr, char **argv)
{
    char *ifname = NULL;
    int help = 0;
    int status = 0;
    int vista_cmd_index;
    int err = 0;
    cmd_t *cmd = NULL;
#ifdef RWL_WIFI
    int retry;
#endif

    while (*argv) {
        if ((strcmp (*argv, "sh") == 0) && (remote_type != NO_REMOTE)) {
            (void)*argv++; /* Get the shell command */
            if (*argv) {
                /* Register handler in case of shell command only */
                err = rwl_shell_cmd_proc((void*)ifr, argv, SHELL_CMD);
            } else {
                DPRINT_ERR(ERR, "Enter the shell "
                           "command, e.g. ls(Linux) or dir(Win CE)\n");
                err = -1;
            }
            return err;
        }

#ifdef RWLASD
        if ((strcmp (*argv, "asd") == 0) && (remote_type != NO_REMOTE)) {
            (void)*argv++; /* Get the asd command */
            if (*argv) {
                err = rwl_shell_cmd_proc((void*)ifr, argv, ASD_CMD);
            } else {
                DPRINT_ERR(ERR, "Enter the ASD command, e.g. ca_get_version\n");
                err = -1;
            }
            return err;
        }
#endif
        if (rwl_os_type == WINVISTA_OS) {
            for (vista_cmd_index = 0; remote_vista_cmds[vista_cmd_index] &&
                    strcmp(remote_vista_cmds[vista_cmd_index], *argv);
                    vista_cmd_index++);
            if (remote_vista_cmds[vista_cmd_index] != NULL) {
                err = rwl_shell_cmd_proc((void *)ifr, argv, VISTA_CMD);
                if ((remote_type == REMOTE_WIFI) && ((!strcmp(*argv, "join")))) {
#ifdef RWL_WIFI
                    DPRINT_INFO(OUTPUT,
                                "\nChannel will be synchronized by Findserver\n\n");
                    sleep(RWL_WIFI_JOIN_DELAY);
                    for (retry = 0; retry < RWL_WIFI_RETRY; retry++) {
                        if ((rwl_find_remote_wifi_server(ifr,
                                                         &g_rwl_buf_mac[0]) == 0)) {
                            break;
                        }
                    }
#endif /* RWL_WIFI */
                }
                return err;
            }
        }

        if ((status = wl_option(&argv, &ifname, &help)) == CMD_OPT) {
            if (help)
                break;
            if (ifname) {
                if (remote_type == NO_REMOTE) {
                    strncpy((*ifr).ifr_name, ifname, IFNAMSIZ);
                }
                else {
                    strncpy(g_rem_ifname, ifname, IFNAMSIZ);
                }
            }
            continue;
        }
        /* parse error */
        else if (status == CMD_ERR)
            break;

        if (remote_type == NO_REMOTE) {
            /* use default interface */
            if (!*(*ifr).ifr_name)
                wl_find(ifr);
            /* validate the interface */
            if (!*(*ifr).ifr_name) {
                errno = ENXIO;
                syserr("interface");
            }
            if ((err = wl_check((void *)ifr)) < 0) {
                fprintf(stderr, "%s: wl driver adapter not found\n", wlu_av0);
                exit(1);
            }

            if ((strcmp (*argv, "--interactive") == 0) || (interactive_flag == 1)) {
                err = do_interactive(ifr);
                return err;
            }
        }
        /* search for command */
        cmd = wl_find_cmd(*argv);
        /* if not found, use default set_var and get_var commands */
        if (!cmd) {
            cmd = &wl_varcmd;
        }
#ifdef RWL_WIFI
        if (!strcmp(cmd->name, "findserver")) {
            remote_wifi_ser_init_cmds((void *) ifr);
        }
#endif /* RWL_WIFI */

        /* RWL over Wifi supports 'lchannel' command which lets client
         * (ie *this* machine) change channels since normal 'channel' command
         * applies to the server (ie target machine)
         */
        if (remote_type == REMOTE_WIFI)	{
#ifdef RWL_WIFI
            if (!strcmp(argv[0], "lchannel")) {
                strcpy(argv[0], "channel");
                rwl_wifi_swap_remote_type(remote_type);
                err = (*cmd->func)((void *) ifr, cmd, argv);
                rwl_wifi_swap_remote_type(remote_type);
            } else {
                err = (*cmd->func)((void *) ifr, cmd, argv);
            }
            /* After join cmd's gets exeuted on the server side , client needs to know
            * the channel on which the server is associated with AP , after delay of
            * few seconds client will intiate the scan on diffrent channels by calling
            * rwl_find_remote_wifi_server fucntion
            */
            if ((!strcmp(cmd->name, "join") || ((!strcmp(cmd->name, "ssid") &&
                                                 (*(++argv) != NULL))))) {
                DPRINT_INFO(OUTPUT, "\n Findserver is called to synchronize the"
                            "channel\n\n");
                sleep(RWL_WIFI_JOIN_DELAY);
                for (retry = 0; retry < RWL_WIFI_RETRY; retry++) {
                    if ((rwl_find_remote_wifi_server(ifr,
                                                     &g_rwl_buf_mac[0]) == 0)) {
                        break;
                    }
                }
            }
#endif /* RWL_WIFI */
        } else {
            /* do command */
            err = (*cmd->func)((void *) ifr, cmd, argv);
        }
        break;
    } /* while loop end */

    /* provide for help on a particular command */
    if (help && *argv) {
        cmd = wl_find_cmd(*argv);
        if (cmd) {
            wl_cmd_usage(stdout, cmd);
        } else {
            DPRINT_ERR(ERR, "%s: Unrecognized command \"%s\", type -h for help\n",
                       wlu_av0, *argv);
        }
    } else if (!cmd)
        wl_usage(stdout, NULL);
    else if (err == USAGE_ERROR)
        wl_cmd_usage(stderr, cmd);
    else if (err == IOCTL_ERROR)
        wl_printlasterror((void *) ifr);
    else if (err == BCME_NODEVICE)
        DPRINT_ERR(ERR, "%s : wl driver adapter not found\n", g_rem_ifname);

    return err;
}
예제 #3
0
int main(int argc, char **argv) {
  float width = 8.5, height = 11.0;
  unsigned num_pages = 1;
  unsigned block_size = 1;
  int interactive_mode = 0;
  int duplex = 0;
  int list = 0;
  int quality = 90;
  int pixels_per_inch = 400;
  int flatbed = 0;
  int compression_type = 0x81;  // JPEG
  int output_to_stdout = 0;

  int first_page_number = 0;
  const char *device_name = 0;
  struct option longopts[] = {
    { "duplex", 0, &duplex, 1 },
    { "list", 0, &list, 1 },
    { "interactive", 0, &interactive_mode, 1 },
    { 0 } };

  int opt;
  while ((opt = getopt_long(argc, argv,
                            "d:n:p:q:b:w:h:c:sr:if",
                            longopts,
                            NULL)) != -1) {
    switch (opt) {
      case 0:  // it was a long option, already handled!
        break;
      case 'd':
        device_name = optarg;
        break;
      case 'n':
        num_pages = atoi(optarg);
        break;
      case 'p':
        first_page_number = atoi(optarg);
        break;
      case 'q':
        quality = atoi(optarg);
        break;
      case 'b':
        block_size = atoi(optarg);
        break;
      case 'w':
        width = strtof(optarg, NULL);
        break;
      case 'h':
        height = strtof(optarg, NULL);
        break;
      case 'c':
        compression_type = atoi(optarg);
        break;
      case 's':
        output_to_stdout = 1;
        break;
      case 'r':
        pixels_per_inch = atoi(optarg);
        break;
      case 'f':
        flatbed = 1;
        break;
      case 'i':
        interactive_mode++;
        break;
      default:
        fprintf(stderr, "Unknown option: %s\n", optarg);
        return usage(argv[0]);
    }
  }

  if (list) {
    char *mylist = list_3105_devices();
    fprintf(stdout, "%s", mylist);
    free(mylist);
    exit(0);
  }

  if (interactive_mode) {
    do_interactive();
    exit(0);
  }

  if (optind >= argc && !output_to_stdout)
    return usage(argv[0]);

  const char *const filebase = argv[optind];

  usb_handle uh = reset_and_attach(device_name);

  if (uh == NULL) {
    fprintf(stderr, "Cannot open scanner\n");
    return 2;
  }

  struct kvs3105_window window;
  kvs3105_window_init(&window);

  window.document_length = window.length = height * 1200;
  window.document_width = window.width = width * 1200;
  window.compression_argument = quality;
  window.compression_type = compression_type;

  // match the behavior of sheetfed_server
  window.emphasis = 0xf0;
  window.subsample = 0;
  window.xres = window.yres = pixels_per_inch;
  window.flatbed = flatbed;

  if (block_size > 254) {
    block_size = num_pages;
    if (num_pages > 254) {
      window.number_of_pages_to_scan = 0xff;
    } else {
      window.number_of_pages_to_scan = num_pages;
    }
  } else {
    window.number_of_pages_to_scan = block_size;
  }

  uint8_t requestsense[KVS3105_REQUEST_SENSE_SIZE];
  for (unsigned pageno = first_page_number;
       pageno < first_page_number + num_pages;) {
    if (kvs3105_reset_windows(uh, requestsense)) {
      report("Error resetting windows", requestsense);
      return 2;
    }
    if (kvs3105_set_windows(uh, &window, duplex, requestsense)) {
      report("Error setting windows", requestsense);
      return 2;
    }
    if (kvs3105_scan(uh, requestsense)) {
      report("Error starting scanning", requestsense);
      return 2;
    }

    // We scan in blocks of block_size pages
    int side = 0;
    for (unsigned page = 0; page < block_size;) {
      uint32_t width, height;
      if (kvs3105_picture_size(uh, page, side, &width, &height, requestsense)) {
        report("Error getting page size", requestsense);
        return 2;
      }

      int outfd;
      char *output_filename;
      if (output_to_stdout) {
        outfd = 1;
        output_filename = strdup("stdout");
      } else {
        if (asprintf(&output_filename, "%s-%03d-%s.jpeg", filebase,
                     pageno + page, side ? "B" : "A" ) == -1) {
          fprintf(stderr, "Memory allocation failed!\n");
          exit(1);
        }
        outfd = open(output_filename,
                     O_WRONLY | O_CREAT | O_TRUNC, 0644);
      }
      if (outfd < 0) {
        fprintf(stderr, "Failed to write to %s: %s\n", output_filename,
                strerror(errno));
        free(output_filename);
        return 2;
      }
      int waitstatus;
      if ((waitstatus = kvs3105_data_buffer_wait(uh, requestsense))) {
        report("Error waiting for image data", requestsense);
        // TODO(dgluss): 3 is a bad name for a condition.  Put in a name.
        if (side == 0 && waitstatus == 3)
          fprintf(stderr, "end of book.\n");
        close(outfd);
        unlink(output_filename);
        free(output_filename);
        return 2;
      }

      uint8_t buffer[KVS3105_BUFFER_SIZE];
      unsigned done = 0;
      unsigned written;
      char end_of_page;

      for (;;) {
        if (kvs3105_read_data(uh, page, side, buffer, sizeof(buffer),
                               &written, &end_of_page, requestsense)) {
          report("Error reading image", requestsense);
          free(output_filename);
          return 2;
        }

        written = write(outfd, buffer, written);
        done += written;
        if (end_of_page) break;
      }
      fprintf(stderr, "%s: %d bytes\n", output_filename, done);
      free(output_filename);

      close(outfd);
      if (duplex) {
        if (side) {
          page++;
          side = 0;
        } else {
          side = 1;
        }
      } else {
        page++;
      }
    }
    pageno += block_size;
  }
}