-
Notifications
You must be signed in to change notification settings - Fork 25
/
interface.h
148 lines (110 loc) · 3.62 KB
/
interface.h
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
/**
* bcm2-utils
* Copyright (C) 2016 Joseph Lehner <[email protected]>
*
* bcm2-utils 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 3 of the License, or
* (at your option) any later version.
*
* bcm2-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bcm2-utils. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef BCM2DUMP_INTERFACE_H
#define BCM2DUMP_INTERFACE_H
#include <functional>
#include <csignal>
#include <memory>
#include <string>
#include <map>
#include "profile.h"
#include "util.h"
#include "io.h"
#undef interface
namespace bcm2dump {
class interface : public std::enable_shared_from_this<interface>
{
typedef bcm2dump::profile profile_type;
typedef bcm2dump::version version_type;
public:
typedef std::shared_ptr<interface> sp;
virtual ~interface() {}
virtual std::string name() const = 0;
virtual void set_profile(const profile::sp& profile, const version& version)
{
set_profile(profile);
m_version = version;
}
virtual void set_profile(const profile::sp& profile)
{ m_profile = profile; }
virtual profile_type::sp profile() const
{ return m_profile; }
virtual const version_type& version() const
{ return m_version; }
virtual bool has_version() const
{ return !m_version.name().empty(); }
virtual bool is_privileged() const
{ return true; }
virtual void elevate_privileges() {}
static interface::sp detect(const io::sp& io, const profile::sp& sp = nullptr);
static interface::sp create(const std::string& specl, const std::string& profile = "");
virtual bcm2_interface id() const = 0;
protected:
void initialize(const profile::sp& profile);
virtual void initialize_impl()
{}
virtual void detect_profile()
{}
virtual void detect_version()
{}
protected:
profile::sp m_profile;
version_type m_version;
};
class cmdline_interface : public interface
{
public:
std::vector<std::string> run(const std::string& cmd, unsigned timeout = 0);
std::vector<std::string> run_raw(const std::string& cmd, unsigned timeout = 0);
bool run(const std::string& cmd, const std::string& expect, bool stop_on_match = false);
virtual bool is_ready(bool passive = false) = 0;
virtual bool wait_ready(unsigned timeout = 5000);
virtual bool wait_quiet(unsigned timeout = 500) const;
virtual bool is_active()
{ return is_ready(false); }
bool is_active(const std::shared_ptr<io>& io)
{
m_io = io;
if (!is_active()) {
m_io.reset();
return false;
}
return true;
}
virtual void writeln(const std::string& str = "")
{ m_io->writeln(str); }
virtual void write(const std::string& str)
{ m_io->write(str); }
bool foreach_line_raw(std::function<bool(const std::string&)> f, unsigned timeout = 0, bool restart = false) const;
bool foreach_line(std::function<bool(const std::string&)> f, unsigned timeout = 0) const;
virtual std::string readln(unsigned timeout = 0) const;
virtual bool pending(unsigned timeout = 0) const
{ return m_io->pending(timeout ? timeout : this->timeout()); }
protected:
virtual void call(const std::string& cmd)
{ writeln(cmd); }
virtual bool is_crash_line(const std::string& line) const
{ return false; }
virtual bool check_for_prompt(const std::string& line) const = 0;
virtual uint32_t timeout() const
{ return 50; }
std::shared_ptr<io> m_io;
};
}
#endif