summaryrefslogtreecommitdiffstats
path: root/utils/hwstub/lib/hwstub.cpp
blob: 7c81146c77e56aeaa3b5c4a2d41309c3299a8ffc (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/***************************************************************************
 *             __________               __   ___.
 *   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 "hwstub.hpp"
#include <algorithm>
#include <cstring>

namespace hwstub {

std::ostream cnull(0);
std::wostream wcnull(0);

std::string error_string(error err)
{
    switch(err)
    {
        case error::SUCCESS: return "success";
        case error::ERROR: return "unspecified error";
        case error::DISCONNECTED: return "device was disconnected";
        case error::PROBE_FAILURE: return "probing failed";
        case error::NO_CONTEXT: return "context was destroyed";
        case error::USB_ERROR: return "unspecified USB error";
        case error::DUMMY: return "operation on dummy device";
        case error::NO_SERVER: return "server could not be reached";
        case error::SERVER_DISCONNECTED: return "server disconnected";
        case error::SERVER_MISMATCH: return "incompatible server";
        case error::NET_ERROR: return "network error";
        case error::PROTOCOL_ERROR: return "network protocol error";
        case error::TIMEOUT: return "timeout";
        case error::OVERFLW: return "overflow";
        default: return "unknown error";
    }
}

/**
 * Context
 */

context::context()
    :m_next_cb_ref(0)
{
    clear_debug();
}

context::~context()
{
}

void context::set_debug(std::ostream& os)
{
    m_debug = &os;
}

std::ostream& context::debug()
{
    return *m_debug;
}

error context::get_device_list(std::vector<std::shared_ptr<device>>& list)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    error err = update_list();
    if(err != error::SUCCESS)
        return err;
    list.resize(m_devlist.size());
    for(size_t i = 0; i < m_devlist.size(); i++)
        list[i] = m_devlist[i];
    return error::SUCCESS;
}

error context::get_dummy_device(std::shared_ptr<device>& dev)
{
    // NOTE: can't use make_shared() because of the protected ctor */
    dev.reset(new dummy_device(shared_from_this()));
    return error::SUCCESS;
}

void context::notify_device(bool arrived, std::shared_ptr<device> dev)
{
    for(auto cb : m_callbacks)
        cb.callback(shared_from_this(), arrived, dev);
}

void context::change_device(bool arrived, std::shared_ptr<device> dev)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    if(arrived)
    {
        /* add to the list (assumed it's not already there) */
        m_devlist.push_back(dev);
        /* notify */
        notify_device(arrived, dev);
    }
    else
    {
        dev->disconnect();
        /* notify first */
        notify_device(arrived, dev);
        auto it = std::find(m_devlist.begin(), m_devlist.end(), dev);
        if(it != m_devlist.end())
            m_devlist.erase(it);
    }
}

error context::update_list()
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* fetch new list */
    std::vector<ctx_dev_t> new_list;
    void* ptr;
    error err = fetch_device_list(new_list, ptr);
    if(err != error::SUCCESS)
        return err;
    /* determine devices that have left */
    std::vector<std::shared_ptr<device>> to_del;
    for(auto dev : m_devlist)
    {
        bool still_there = false;
        for(auto new_dev : new_list)
            if(match_device(new_dev, dev))
                still_there = true;
        if(!still_there)
            to_del.push_back(dev);
    }
    for(auto dev : to_del)
        change_device(false, dev);
    /* determine new devices */
    std::vector<ctx_dev_t> to_add;
    for(auto new_dev : new_list)
    {
        bool exists = false;
        for(auto dev : m_devlist)
            if(match_device(new_dev, dev))
                exists = true;
        if(!exists)
            to_add.push_back(new_dev);
    }
    /* create new devices */
    for(auto dev : to_add)
    {
        std::shared_ptr<device> new_dev;
        err = create_device(dev, new_dev);
        if(err == error::SUCCESS)
            change_device(true, new_dev);
    }
    /* destroy list */
    destroy_device_list(ptr);
    return error::SUCCESS;
}

context::callback_ref_t context::register_callback(const notification_callback_t& fn)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    struct callback_t cb;
    cb.callback = fn;
    cb.ref = m_next_cb_ref++;
    m_callbacks.push_back(cb);
    return cb.ref;
}

void context::unregister_callback(callback_ref_t ref)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    for(auto it = m_callbacks.begin(); it != m_callbacks.end(); ++it)
    {
        if((*it).ref == ref)
        {
            m_callbacks.erase(it);
            return;
        }
    }
}

void context::start_polling(std::chrono::milliseconds interval)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* create poller on demand */
    if(!m_poller)
        m_poller.reset(new context_poller(shared_from_this(), interval));
    else
        m_poller->set_interval(interval);
    m_poller->start();
}

void context::stop_polling()
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    m_poller->stop();
}

/**
 * Context Poller
 */

context_poller::context_poller(std::weak_ptr<context> ctx, std::chrono::milliseconds interval)
    :m_ctx(ctx), m_running(false), m_exit(false), m_interval(interval)
{
    m_thread = std::thread(context_poller::thread, this);
}

context_poller::~context_poller()
{
    /* set exit flag, wakeup thread, wait for exit */
    m_mutex.lock();
    m_exit = true;
    m_mutex.unlock();
    m_cond.notify_one();
    m_thread.join();
}

void context_poller::set_interval(std::chrono::milliseconds interval)
{
    std::unique_lock<std::mutex> lock(m_mutex);
    /* change interval, wakeup thread to take new interval into account */
    m_interval = interval;
    m_cond.notify_one();
}

void context_poller::start()
{
    std::unique_lock<std::mutex> lock(m_mutex);
    /* change running flag, wakeup thread to start polling */
    m_running = true;
    m_cond.notify_one();
}

void context_poller::stop()
{
    std::unique_lock<std::mutex> lock(m_mutex);
    /* change running flag, wakeup thread to stop polling */
    m_running = false;
    m_cond.notify_one();
}

void context_poller::poll()
{
    std::unique_lock<std::mutex> lock(m_mutex);
    while(true)
    {
        /* if asked, exit */
        if(m_exit)
            break;
        /* if running, poll and then sleep for some time */
        if(m_running)
        {
            std::shared_ptr<context> ctx = m_ctx.lock();
            if(ctx)
                ctx->update_list();
            ctx.reset();
            m_cond.wait_for(lock, m_interval);
        }
        /* if not, sleep until awaken */
        else
            m_cond.wait(lock);
    }
}

void context_poller::thread(context_poller *poller)
{
    poller->poll();
}

/**
 * Device
 */
device::device(std::shared_ptr<context> ctx)
    :m_ctx(ctx), m_connected(true)
{
}

device::~device()
{
}

error device::open(std::shared_ptr<handle>& handle)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* get a pointer so that it's not destroyed during the runtime of the function,
     * the pointer will be released at the end of the function */
    std::shared_ptr<context> ctx = get_context();
    if(!ctx)
        return error::NO_CONTEXT;
    /* do not even try if device is disconnected */
    if(!connected())
        return error::DISCONNECTED;
    /* NOTE at the moment handle is state-less which means that we can
     * safely give the same handle each time open() is called without callers
     * interfering with each other. If handle eventually get a state,
     * one will need to create a proxy class to encapsulate the state */
    handle = m_handle.lock();
    if(has_multiple_open() || !handle)
    {
        error err = open_dev(handle);
        m_handle = handle;
        return err;
    }
    else
        return error::SUCCESS;
}

void device::disconnect()
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    m_connected = false;
}

bool device::connected()
{
    return m_connected;
}

std::shared_ptr<context> device::get_context()
{
    return m_ctx.lock();
}

/**
 * Handle
 */

handle::handle(std::shared_ptr<device > dev)
    :m_dev(dev)
{
}

handle::~handle()
{
}

std::shared_ptr<device> handle::get_device()
{
    return m_dev;
}

error handle::get_desc(uint16_t desc, void *buf, size_t& buf_sz)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* get a pointer so that it's not destroyed during the runtime of the function,
     * the pointer will be released at the end of the function */
    std::shared_ptr<context> ctx = m_dev->get_context();
    if(!ctx)
        return error::NO_CONTEXT;
    /* ensure valid status */
    error err = status();
    if(err != error::SUCCESS)
        return err;
    return get_dev_desc(desc, buf, buf_sz);
}

error handle::get_log(void *buf, size_t& buf_sz)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* get a pointer so that it's not destroyed during the runtime of the function,
     * the pointer will be released at the end of the function */
    std::shared_ptr<context> ctx = m_dev->get_context();
    if(!ctx)
        return error::NO_CONTEXT;
    /* ensure valid status */
    error err = status();
    if(err != error::SUCCESS)
        return err;
    return get_dev_log(buf, buf_sz);
}

error handle::exec(uint32_t addr, uint16_t flags)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* get a pointer so that it's not destroyed during the runtime of the function,
     * the pointer will be released at the end of the function */
    std::shared_ptr<context> ctx = m_dev->get_context();
    if(!ctx)
        return error::NO_CONTEXT;
    /* ensure valid status */
    error err = status();
    if(err != error::SUCCESS)
        return err;
    return exec_dev(addr, flags);
}

error handle::read(uint32_t addr, void *buf, size_t& sz, bool atomic)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* get a pointer so that it's not destroyed during the runtime of the function,
     * the pointer will be released at the end of the function */
    std::shared_ptr<context> ctx = m_dev->get_context();
    if(!ctx)
        return error::NO_CONTEXT;
    /* ensure valid status */
    error err = status();
    if(err != error::SUCCESS)
        return err;
    /* split transfer as needed */
    size_t cnt = 0;
    uint8_t *bufp = (uint8_t *)buf;
    while(sz > 0)
    {
        size_t xfer = std::min(sz, get_buffer_size());
        err = read_dev(addr, buf, xfer, atomic);
        if(err != error::SUCCESS)
            return err;
        sz -= xfer;
        bufp += xfer;
        addr += xfer;
        cnt += xfer;
    }
    sz = cnt;
    return error::SUCCESS;
}

error handle::write(uint32_t addr, const void *buf, size_t& sz, bool atomic)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    /* get a pointer so that it's not destroyed during the runtime of the function,
     * the pointer will be released at the end of the function */
    std::shared_ptr<context> ctx = m_dev->get_context();
    if(!ctx)
        return error::NO_CONTEXT;
    /* ensure valid status */
    error err = status();
    if(err != error::SUCCESS)
        return err;
    /* split transfer as needed */
    size_t cnt = 0;
    const uint8_t *bufp = (uint8_t *)buf;
    while(sz > 0)
    {
        size_t xfer = std::min(sz, get_buffer_size());
        err = write_dev(addr, buf, xfer, atomic);
        if(err != error::SUCCESS)
            return err;
        sz -= xfer;
        bufp += xfer;
        addr += xfer;
        cnt += xfer;
    }
    sz = cnt;
    return error::SUCCESS;
}

error handle::status() const
{
    /* check context */
    if(!m_dev->get_context())
        return error::NO_CONTEXT;
    if(!m_dev->connected())
        return error::DISCONNECTED;
    else
        return error::SUCCESS;
}

namespace
{
    template<typename T>
    error helper_get_desc(handle *h, uint8_t type, T& desc)
    {
        size_t sz = sizeof(desc);
        error ret = h->get_desc(type, &desc, sz);
        if(ret != error::SUCCESS)
            return ret;
        if(sz != sizeof(desc) || desc.bDescriptorType != type ||
                desc.bLength != sizeof(desc))
            return error::ERROR;
        else
            return error::SUCCESS;
    }
}

error handle::get_version_desc(hwstub_version_desc_t& desc)
{
    return helper_get_desc(this, HWSTUB_DT_VERSION, desc);
}

error handle::get_layout_desc(hwstub_layout_desc_t& desc)
{
    return helper_get_desc(this, HWSTUB_DT_LAYOUT, desc);
}

error handle::get_stmp_desc(hwstub_stmp_desc_t& desc)
{
    return helper_get_desc(this, HWSTUB_DT_STMP, desc);
}

error handle::get_pp_desc(hwstub_pp_desc_t& desc)
{
    return helper_get_desc(this, HWSTUB_DT_PP, desc);
}

error handle::get_jz_desc(hwstub_jz_desc_t& desc)
{
    return helper_get_desc(this, HWSTUB_DT_JZ, desc);
}

error handle::get_target_desc(hwstub_target_desc_t& desc)
{
    return helper_get_desc(this, HWSTUB_DT_TARGET, desc);
}

/** Dummy device */
dummy_device::dummy_device(std::shared_ptr<context> ctx)
    :device(ctx)
{
}

dummy_device::~dummy_device()
{
}

error dummy_device::open_dev(std::shared_ptr<handle>& handle)
{
    handle.reset(new dummy_handle(shared_from_this()));
    return error::SUCCESS;
}

bool dummy_device::has_multiple_open() const
{
    return true;
}

/** Dummy handle */
dummy_handle::dummy_handle(std::shared_ptr<device> dev)
    :handle(dev)
{
    m_desc_version.bLength = sizeof(m_desc_version);
    m_desc_version.bDescriptorType = HWSTUB_DT_VERSION;
    m_desc_version.bMajor = HWSTUB_VERSION_MAJOR;
    m_desc_version.bMinor = HWSTUB_VERSION_MINOR;
    m_desc_version.bRevision = 0;

    m_desc_layout.bLength = sizeof(m_desc_layout);
    m_desc_layout.bDescriptorType = HWSTUB_DT_LAYOUT;
    m_desc_layout.dCodeStart = 0;
    m_desc_layout.dCodeSize = 0;
    m_desc_layout.dStackStart = 0;
    m_desc_layout.dStackSize = 0;
    m_desc_layout.dBufferStart = 0;
    m_desc_layout.dBufferSize = 1;

    m_desc_target.bLength = sizeof(m_desc_target);
    m_desc_target.bDescriptorType = HWSTUB_DT_TARGET;
    m_desc_target.dID = HWSTUB_TARGET_UNK;
    strcpy(m_desc_target.bName, "Dummy target");
}

dummy_handle::~dummy_handle()
{
}

error dummy_handle::read_dev(uint32_t addr, void *buf, size_t& sz, bool atomic)
{
    (void) addr;
    (void) buf;
    (void) sz;
    (void) atomic;
    return error::DUMMY;
}

error dummy_handle::write_dev(uint32_t addr, const void *buf, size_t& sz, bool atomic)
{
    (void) addr;
    (void) buf;
    (void) sz;
    (void) atomic;
    return error::DUMMY;
}

error dummy_handle::get_dev_desc(uint16_t desc, void *buf, size_t& buf_sz)
{
    void *p = nullptr;
    switch(desc)
    {
        case HWSTUB_DT_VERSION: p = &m_desc_version; break;
        case HWSTUB_DT_LAYOUT: p = &m_desc_layout; break;
        case HWSTUB_DT_TARGET: p = &m_desc_target; break;
        default: break;
    }
    if(p == nullptr)
        return error::ERROR;
    /* size is in the bLength field of the descriptor */
    size_t desc_sz = *(uint8_t *)p;
    buf_sz = std::min(buf_sz, desc_sz);
    memcpy(buf, p, buf_sz);
    return error::SUCCESS;
}

error dummy_handle::get_dev_log(void *buf, size_t& buf_sz)
{
    (void) buf;
    (void) buf_sz;
    return error::DUMMY;
}

error dummy_handle::exec_dev(uint32_t addr, uint16_t flags)
{
    (void) addr;
    (void) flags;
    return error::DUMMY;
}

error dummy_handle::status() const
{
    error err = handle::status();
    return err == error::SUCCESS ? error::DUMMY : err;
}

size_t dummy_handle::get_buffer_size()
{
    return 1;
}

} // namespace hwstub