summaryrefslogtreecommitdiffstats
path: root/utils/hwstub/tools/hwstub_test.cpp
blob: c93e601c3668133420f034b69aaa87ccdd3c1dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2015 by Amaury Pouly
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <cstdio>
#include <thread>
#include <chrono>
#include <cstring>
#include <iostream>
#include "hwstub.hpp"
#include "hwstub_usb.hpp"
#include "hwstub_uri.hpp"
#include <signal.h>
#include <getopt.h>

/* capture CTRL+C */
volatile sig_atomic_t g_exit_loop = 0;

void do_signal(int sig)
{
    g_exit_loop = 1;
}

std::shared_ptr<hwstub::context> g_ctx;

void print_error(hwstub::error err, bool nl = true)
{
    switch(err)
    {
        case hwstub::error::SUCCESS: printf("success"); break;
        case hwstub::error::ERROR: printf("error"); break;
        default: printf("unknown(%d)", (int)err); break;
    }
    if(nl)
        printf("\n");
}

const char *target_string(uint32_t id)
{
    switch(id)
    {
        case HWSTUB_TARGET_UNK: return "unknown";
        case HWSTUB_TARGET_STMP: return "stmp";
        case HWSTUB_TARGET_RK27: return "rk27";
        case HWSTUB_TARGET_PP: return "pp";
        case HWSTUB_TARGET_ATJ: return "atj";
        case HWSTUB_TARGET_JZ: return "jz";
        default: return "unknown";
    }
}

void print_dev_details(std::shared_ptr<hwstub::device> dev)
{
    std::shared_ptr<hwstub::handle> h;
    hwstub::error err = dev->open(h);
    if(err != hwstub::error::SUCCESS)
    {
        printf("  [cannot open dev: %s]\n", error_string(err).c_str());
        return;
    }
    /* version */
    struct hwstub_version_desc_t ver_desc;
    err = h->get_version_desc(ver_desc);
    if(err != hwstub::error::SUCCESS)
    {
        printf("  [cannot get version descriptor: %s]\n", error_string(err).c_str());
        return;
    }
    printf("  [version %d.%d.%d]\n", ver_desc.bMajor, ver_desc.bMinor, ver_desc.bRevision);
    /* target */
    struct hwstub_target_desc_t target_desc;
    err = h->get_target_desc(target_desc);
    if(err != hwstub::error::SUCCESS)
    {
        printf("  [cannot get target descriptor: %s]\n", error_string(err).c_str());
        return;
    }
    std::string name(target_desc.bName, sizeof(target_desc.bName));
    printf("  [target %s: %s]\n", target_string(target_desc.dID), name.c_str());
    /* layout */
    struct hwstub_layout_desc_t layout_desc;
    err = h->get_layout_desc(layout_desc);
    if(err != hwstub::error::SUCCESS)
    {
        printf("  [cannot get layout descriptor: %s]\n", error_string(err).c_str());
        return;
    }
    printf("  [code layout %#x bytes @ %#x]\n", layout_desc.dCodeSize, layout_desc.dCodeStart);
    printf("  [stack layout %#x bytes @ %#x]\n", layout_desc.dStackSize, layout_desc.dStackStart);
    printf("  [buffer layout %#x bytes @ %#x]\n", layout_desc.dBufferSize, layout_desc.dBufferStart);
}

void print_device(std::shared_ptr<hwstub::device> dev, bool arrived = true)
{
    hwstub::usb::device *udev = dynamic_cast< hwstub::usb::device* >(dev.get());
    if(arrived)
        printf("--> ");
    else
        printf("<-- ");
    if(udev)
    {
        libusb_device *uudev = udev->native_device();
        struct libusb_device_descriptor dev_desc;
        libusb_get_device_descriptor(uudev, &dev_desc);
        printf("USB device @ %d.%u: ID %04x:%04x\n", udev->get_bus_number(),
               udev->get_address(), dev_desc.idVendor, dev_desc.idProduct);
    }
    else
        printf("Unknown device\n");
    if(arrived)
        print_dev_details(dev);
}

void dev_changed(std::shared_ptr<hwstub::context> ctx, bool arrived, std::shared_ptr<hwstub::device> dev)
{
    print_device(dev, arrived);
}

void print_list()
{
    std::vector<std::shared_ptr<hwstub::device>> list;
    hwstub::error ret = g_ctx->get_device_list(list);
    if(ret != hwstub::error::SUCCESS)
    {
        printf("Cannot get device list: %s\n", error_string(ret).c_str());
        return;
    }
    for(auto d : list)
        print_device(d);
}

int usage()
{
    printf("usage: hwstub_test [options]\n");
    printf("  --help/-h           Display this help\n");
    printf("  --verbose/-v        Verbose output\n");
    printf("  --context/-c <uri>  Context URI (see below)\n");
    printf("\n");
    hwstub::uri::print_usage(stdout, true, false);
    return 1;
}

int main(int argc, char **argv)
{
    hwstub::uri::uri uri = hwstub::uri::default_uri();
    bool verbose = false;

    while(1)
    {
        static struct option long_options[] =
        {
            {"help", no_argument, 0, 'h'},
            {"verbose", no_argument, 0, 'v'},
            {"context", required_argument, 0, 'c'},
            {0, 0, 0, 0}
        };

        int c = getopt_long(argc, argv, "hvc:", long_options, NULL);
        if(c == -1)
            break;
        switch(c)
        {
            case -1:
                break;
            case 'v':
                verbose = true;
                break;
            case 'h':
                return usage();
            case 'c':
                uri = hwstub::uri::uri(optarg);
                break;
            default:
                abort();
        }
    }

    if(optind != argc)
        return usage();

    /* intercept CTRL+C */
    signal(SIGINT, do_signal);

    std::string error;
    g_ctx = hwstub::uri::create_context(uri, &error);
    if(!g_ctx)
    {
        printf("Cannot create context: %s\n", error.c_str());
        return 1;
    }
    if(verbose)
        g_ctx->set_debug(std::cout);
    print_list();
    g_ctx->register_callback(dev_changed);
    g_ctx->start_polling();
    while(!g_exit_loop)
        std::this_thread::sleep_for(std::chrono::seconds(1));
    printf("Shutting down...\n");
    g_ctx.reset(); /* will cleanup */

    return 0;
}