summaryrefslogtreecommitdiffstats
path: root/utils/hwstub/lib/hwstub_virtual.cpp
blob: fada56fb83b69b2080e04961d66c1f102de1274d (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 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_virtual.hpp"
#include <algorithm>
#include <cstring>

namespace hwstub {
namespace virt {

/**
 * Context
 */
context::context()
{
}

context::~context()
{
}

std::shared_ptr<context> context::create()
{
    // NOTE: can't use make_shared() because of the protected ctor */
    return std::shared_ptr<context>(new context());
}

std::shared_ptr<context> context::create_spec(const std::string& spec, std::string *error)
{
    std::shared_ptr<context> ctx = create();
    /* parse spec */
    std::string str = spec;
    while(!str.empty())
    {
        /* find next separator, if any */
        std::string dev_spec;
        size_t dev_sep = str.find(';');
        if(dev_sep == std::string::npos)
        {
            dev_spec = str;
            str.clear();
        }
        else
        {
            dev_spec = str.substr(0, dev_sep);
            str = str.substr(dev_sep + 1);
        }
        /* handle dev spec: find ( and )*/
        size_t lparen = dev_spec.find('(');
        if(lparen == std::string::npos)
        {
            if(error)
                *error = "invalid device spec '" + dev_spec + "': missing (";
            return std::shared_ptr<context>();
        }
        if(dev_spec.back() != ')')
        {
            if(error)
                *error = "invalid device spec '" + dev_spec + "': missing )";
            return std::shared_ptr<context>();
        }
        std::string args = dev_spec.substr(lparen + 1, dev_spec.size() - lparen - 2);
        std::string type = dev_spec.substr(0, lparen);
        if(type == "dummy")
        {
            ctx->connect(std::make_shared<dummy_hardware>(args));
        }
        else
        {
            if(error)
                *error = "invalid device spec '" + dev_spec + "': unknown device type";
            return std::shared_ptr<context>();
        }
    }
    return ctx;
}

bool context::connect(std::shared_ptr<hardware> hw)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    if(std::find(m_hwlist.begin(), m_hwlist.end(), hw) != m_hwlist.end())
        return false;
    m_hwlist.push_back(hw);
    return true;
}

bool context::disconnect(std::shared_ptr<hardware> hw)
{
    std::unique_lock<std::recursive_mutex> lock(m_mutex);
    auto it = std::find(m_hwlist.begin(), m_hwlist.end(), hw);
    if(it == m_hwlist.end())
        return false;
    m_hwlist.erase(it);
    return true;
}

std::shared_ptr<hardware> context::from_ctx_dev(ctx_dev_t dev)
{
    return ((hardware *)dev)->shared_from_this();
}

hwstub::context::ctx_dev_t context::to_ctx_dev(std::shared_ptr<hardware>& dev)
{
   return (ctx_dev_t)dev.get();
}

error context::fetch_device_list(std::vector<ctx_dev_t>& list, void*& ptr)
{
    (void) ptr;
    list.resize(m_hwlist.size());
    for(size_t i = 0; i < m_hwlist.size(); i++)
        list[i] = to_ctx_dev(m_hwlist[i]);
    return error::SUCCESS;
}

void context::destroy_device_list(void *ptr)
{
    (void) ptr;
}

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

bool context::match_device(ctx_dev_t dev, std::shared_ptr<hwstub::device> hwdev)
{
    device *udev = dynamic_cast<device*>(hwdev.get());
    return udev != nullptr && udev->native_device().get() == from_ctx_dev(dev).get();
}

/**
 * Device
 */
device::device(std::shared_ptr<hwstub::context> ctx, std::shared_ptr<hardware> dev)
    :hwstub::device(ctx), m_hwdev(dev)
{
}

device::~device()
{
}

std::shared_ptr<hardware> device::native_device()
{
    return m_hwdev.lock();
}

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

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

/**
 * Handle
 */
handle::handle(std::shared_ptr<hwstub::device> dev)
    :hwstub::handle(dev)
{
    m_hwdev = dynamic_cast<device*>(dev.get())->native_device();
}

handle::~handle()
{
}


error handle::read_dev(uint32_t addr, void *buf, size_t& sz, bool atomic)
{
    auto p = m_hwdev.lock();
    return p ? p->read_dev(addr, buf, sz, atomic) : error::DISCONNECTED;
}

error handle::write_dev(uint32_t addr, const void *buf, size_t& sz, bool atomic)
{
    auto p = m_hwdev.lock();
    return p ? p->write_dev(addr, buf, sz, atomic) : error::DISCONNECTED;
}

error handle::get_dev_desc(uint16_t desc, void *buf, size_t& buf_sz)
{
    auto p = m_hwdev.lock();
    return p ? p->get_dev_desc(desc, buf, buf_sz) : error::DISCONNECTED;
}

error handle::get_dev_log(void *buf, size_t& buf_sz)
{
    auto p = m_hwdev.lock();
    return p ? p->get_dev_log(buf, buf_sz) : error::DISCONNECTED;
}

error handle::exec_dev(uint32_t addr, uint16_t flags)
{
    auto p = m_hwdev.lock();
    return p ? p->exec_dev(addr, flags) : error::DISCONNECTED;
}

error handle::status() const
{
    return hwstub::handle::status();
}

size_t handle::get_buffer_size()
{
    auto p = m_hwdev.lock();
    return p ? p->get_buffer_size() : 1;
}

/**
 * Hardware
 */
hardware::hardware()
{
}

hardware::~hardware()
{
}

/**
 * Dummy hardware
 */
dummy_hardware::dummy_hardware(const std::string& name)
{
    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;
    strncpy(m_desc_target.bName, name.c_str(), sizeof(m_desc_target.bName));
    m_desc_target.bName[sizeof(m_desc_target.bName) - 1] = 0;
}

dummy_hardware::~dummy_hardware()
{
}

error dummy_hardware::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_hardware::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_hardware::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_hardware::get_dev_log(void *buf, size_t& buf_sz)
{
    (void) buf;
    (void) buf_sz;
    return error::DUMMY;
}

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

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

} // namespace virt
} // namespace net