Changeset 73b0773 in mainline


Ignore:
Timestamp:
2017-12-15T23:12:44Z (6 years ago)
Author:
Petr Mánek <petr.manek@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2bd04b2
Parents:
64d138b
git-author:
Petr Mánek <petr.manek@…> (2017-12-15 23:11:44)
git-committer:
Petr Mánek <petr.manek@…> (2017-12-15 23:12:44)
Message:

usbdiag: simple tmon command matching framework

Location:
uspace/app/tmon
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tmon/Makefile

    r64d138b r73b0773  
    3333
    3434SOURCES = \
    35         main.c
     35        main.c\
     36        list.c
    3637
    3738include $(USPACE_PREFIX)/Makefile.common
  • uspace/app/tmon/main.c

    r64d138b r73b0773  
    3636
    3737#include <stdio.h>
    38 #include <loc.h>
    39 #include <usb/diag/diag.h>
    40 #include <usb/diag/iface.h>
    41 #include <devman.h>
    42 #include <errno.h>
     38#include "commands.h"
    4339
    4440#define NAME "tmon"
     
    4945}
    5046
    51 static void print_list_item(service_id_t svc)
    52 {
    53         int rc;
    54         devman_handle_t diag_handle = 0;
     47typedef struct {
     48        const char *name;
     49        int (*action)(int, char **);
     50} usb_diag_cmd_t;
    5551
    56         if ((rc = devman_fun_sid_to_handle(svc, &diag_handle))) {
    57                 printf(NAME ": Error resolving handle of device with SID %ld, skipping.\n", svc);
    58                 return;
     52static usb_diag_cmd_t commands[] = {
     53        {
     54                .name = "list",
     55                .action = tmon_list,
     56        },
     57        {
     58                .name = NULL
    5959        }
    60 }
    61 
    62 static int print_list()
    63 {
    64         category_id_t diag_cat;
    65         service_id_t *svcs;
    66         size_t count;
    67         int rc;
    68 
    69         if ((rc = loc_category_get_id(USB_DIAG_CATEGORY, &diag_cat, 0))) {
    70                 printf(NAME ": Error resolving category '%s'", USB_DIAG_CATEGORY);
    71                 return 1;
    72         }
    73 
    74         if ((rc = loc_category_get_svcs(diag_cat, &svcs, &count))) {
    75                 printf(NAME ": Error getting list of host controllers.\n");
    76                 return 1;
    77         }
    78 
    79         for (unsigned i = 0; i < count; ++i) {
    80                 print_list_item(svcs[i]);
    81         }
    82 
    83         free(svcs);
    84         return 0;
    85 }
     60};
    8661
    8762int main(int argc, char *argv[])
    8863{
    89         if (argc <= 1) {
     64        // Find a command to execute.
     65        usb_diag_cmd_t *cmd = NULL;
     66        for (int i = 0; argc > 1 && commands[i].name; ++i) {
     67                if (str_cmp(argv[1], commands[i].name) == 0) {
     68                        cmd = commands + i;
     69                        break;
     70                }
     71        }
     72
     73        if (!cmd) {
    9074                print_usage(argv[0]);
    9175                return -1;
    9276        }
    9377
    94         return print_list();
     78        return cmd->action(argc - 2, argv + 2);
    9579}
    9680
Note: See TracChangeset for help on using the changeset viewer.