summaryrefslogtreecommitdiff
path: root/utils/hwstub/include/hwstub_uri.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/hwstub/include/hwstub_uri.hpp')
-rw-r--r--utils/hwstub/include/hwstub_uri.hpp131
1 files changed, 131 insertions, 0 deletions
diff --git a/utils/hwstub/include/hwstub_uri.hpp b/utils/hwstub/include/hwstub_uri.hpp
new file mode 100644
index 0000000000..d461764cd9
--- /dev/null
+++ b/utils/hwstub/include/hwstub_uri.hpp
@@ -0,0 +1,131 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2016 by Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef __HWSTUB_URI_HPP__
22#define __HWSTUB_URI_HPP__
23
24#include "hwstub.hpp"
25#include "hwstub_net.hpp"
26
27namespace hwstub {
28namespace uri {
29
30/** HWSTUB URIs
31 *
32 * They are of the form:
33 *
34 * scheme:[//domain[:port]][/][path[?query]]
35 *
36 * The scheme is mandatory and controls the type of context that is created.
37 * The following scheme are recognized:
38 * usb USB context
39 * tcp TCP context
40 * unix Unix domain context
41 * virt Virtual context (Testing and debugging)
42 * default Default context (This is the default)
43 *
44 * When creating a USB context, the domain and port must be empty:
45 * usb:
46 *
47 * When creating a TCP context, the domain and port are given as argument to
48 * the context:
49 * tcp://localhost:6666
50 *
51 * When creating a Unix context, the domain is given as argument to
52 * the context, it is invalid to specify a port. There are two types of
53 * unix contexts: the one specified by a filesystem path, or (Linux-only) by
54 * an abstract domain. Abstract names are specified as a domain starting with a '#',
55 * whereas standard path can be any path:
56 * unix:///path/to/socket
57 * unix://#hwstub
58 *
59 * When creating a virtual context, the domain will contain a specification of
60 * the device to create. The device list is of the type(param);type(param);...
61 * where the only supported type at the moment is 'dummy' with a single parameter
62 * which is the device name:
63 * virt://dummy(Device A);dummy(Device B);dummy(Super device C)
64 *
65 *
66 * HWSTUB SERVER URIs
67 *
68 * The same scheme can be used to spawn servers. Server URIs are a subset of
69 * context URIs and only support tcp and unix schemes.
70 */
71
72/** URI
73 *
74 * Represents an URI and allows queries on it */
75class uri
76{
77public:
78 uri(const std::string& uri);
79 /** Return whether the URI is syntactically correct */
80 bool valid() const;
81 /** Return error description if URI is invalid */
82 std::string error() const;
83 /** Return the original URI */
84 std::string full_uri() const;
85 /** Return the scheme */
86 std::string scheme() const;
87 /** Return the domain, or empty is none */
88 std::string domain() const;
89 /** Return the port, or empty is none */
90 std::string port() const;
91 /** Return the path, or empty is none */
92 std::string path() const;
93
94protected:
95 void parse();
96 bool validate_scheme();
97 bool validate_domain();
98 bool validate_port();
99
100 std::string m_uri; /* original uri */
101 bool m_valid; /* did it parse correctly ? */
102 std::string m_scheme; /* scheme (extracted from URI) */
103 std::string m_domain; /* domain (extracted from URI) */
104 std::string m_port; /* port (extracted from URI) */
105 std::string m_path; /* path (extracted from URI) */
106 std::string m_error; /* error string (for invalid URIs) */
107};
108
109/** Create a context based on a URI. This function only uses the scheme/domain/port
110 * parts of the URI. This function may fail and return a empty pointer. An optional
111 * string can receive a description of the error */
112std::shared_ptr<context> create_context(const uri& uri, std::string *error = nullptr);
113/** Return a safe default for a URI */
114uri default_uri();
115/** Special case function for the default function */
116std::shared_ptr<context> create_default_context(std::string *error = nullptr);
117/** Create a server based on a URI. This function only uses the scheme/domain/port
118 * parts of the URI. This function may fail and return a empty pointer. An optional
119 * string can receive a description of the error */
120std::shared_ptr<net::server> create_server(std::shared_ptr<context> ctx,
121 const uri& uri, std::string *error);
122/** Return a safe default for a server URI */
123uri default_server_uri();
124/** Print help for the format of a URI, typically for a command-line help.
125 * The help can be client-only, server-only, or both. */
126void print_usage(FILE *f, bool client, bool server);
127
128} // namespace uri
129} // namespace hwstub
130
131#endif /* __HWSTUB_URI_HPP__ */