Changeset 56a07e0 in mainline


Ignore:
Timestamp:
2012-12-22T20:34:09Z (11 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b591cab
Parents:
38b4a25
Message:

vuhid: Add help and few useful options.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/vuhid/main.c

    r38b4a25 r56a07e0  
    4040#include <errno.h>
    4141#include <str_error.h>
     42#include <getopt.h>
    4243
    4344#include <usb/usb.h>
     
    5152#include "ifaces.h"
    5253#include "stdreq.h"
     54
     55#define DEFAULT_CONTROLLER   "/virt/usbhc/ctl"
    5356
    5457static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
     
    151154
    152155
     156static struct option long_options[] = {
     157        {"help", optional_argument, NULL, 'h'},
     158        {"controller", required_argument, NULL, 'c' },
     159        {"list", no_argument, NULL, 'l' },
     160        {0, 0, NULL, 0}
     161};
     162static const char *short_options = "hc:l";
     163
     164static void print_help(const char* name, const char* module)
     165{
     166        if (module == NULL) {
     167                /* Default help */
     168                printf("Usage: %s [options] device.\n", name);
     169                printf("\t-h, --help [device]\n");
     170                printf("\t\to With no argument print this help and exit.\n");
     171                printf("\t\to With argument print device specific help and exit.\n");
     172                printf("\t-l, --list \n\t\tPrint list of available devices.\n");
     173                printf("\t-c, --controller \n\t\t"
     174                    "Use provided virtual hc instead of default (%s)\n",
     175                    DEFAULT_CONTROLLER);
     176                return;
     177        }
     178        printf("HELP for module %s\n", module);
     179}
     180
     181static void print_list(void)
     182{
     183        printf("Available devices:\n");
     184        for (vuhid_interface_t **i = available_hid_interfaces; *i != NULL; ++i)
     185        {
     186                printf("\t`%s'\t%s\n", (*i)->id, (*i)->name);
     187        }
     188
     189}
     190
     191static const char *controller = DEFAULT_CONTROLLER;
     192
    153193int main(int argc, char * argv[])
    154194{
    155         int rc;
     195
     196        if (argc == 1) {
     197                print_help(*argv, NULL);
     198                return 0;
     199        }
     200
     201        int opt = 0;
     202        while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) > 0) {
     203                switch (opt)
     204                {
     205                case 'h':
     206                        print_help(*argv, optarg);
     207                        return 0;
     208                case 'c':
     209                        controller = optarg;
     210                        break;
     211                case 'l':
     212                        print_list();
     213                        return 0;
     214                case -1:
     215                default:
     216                        break;
     217                }
     218        }
     219
    156220
    157221        log_init("vuhid");
     
    161225
    162226        /* Determine which interfaces to initialize. */
    163         int i;
    164         for (i = 1; i < argc; i++) {
    165                 rc = add_interface_by_id(available_hid_interfaces, argv[i],
     227        for (int i = optind; i < argc; i++) {
     228                int rc = add_interface_by_id(available_hid_interfaces, argv[i],
    166229                    &hid_dev);
    167230                if (rc != EOK) {
     
    173236        }
    174237
    175         for (i = 0; i < (int) hid_dev.descriptors->configuration->extra_count; i++) {
     238        for (int i = 0; i < (int) hid_dev.descriptors->configuration->extra_count; i++) {
    176239                usb_log_debug("Found extra descriptor: %s.\n",
    177240                    usb_debug_str_buffer(
     
    181244        }
    182245
    183         rc = usbvirt_device_plug(&hid_dev, "/virt/usbhc/ctl");
     246        const int rc = usbvirt_device_plug(&hid_dev, controller);
    184247        if (rc != EOK) {
    185                 printf("Unable to start communication with VHCD: %s.\n",
    186                     str_error(rc));
     248                printf("Unable to start communication with VHCD `%s': %s.\n",
     249                    controller, str_error(rc));
    187250                return rc;
    188251        }
    189252       
    190         printf("Connected to VHCD...\n");
     253        printf("Connected to VHCD `%s'...\n", controller);
    191254
    192255        wait_for_interfaces_death(&hid_dev);
Note: See TracChangeset for help on using the changeset viewer.