blob: 0952b60f9d93402e70ac6fb0d068a1ee4b46fd42 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "vom/hw.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/hw_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018#include "vom/logger.hpp"
19
Neale Ranns812ed392017-10-16 04:20:13 -070020namespace VOM {
21HW::cmd_q::cmd_q()
22 : m_enabled(true)
23 , m_connected(false)
24 , m_conn()
25{
26}
27
28HW::cmd_q::~cmd_q()
29{
Neale Ranns812ed392017-10-16 04:20:13 -070030}
31
32HW::cmd_q&
33HW::cmd_q::operator=(const HW::cmd_q& f)
34{
35 return (*this);
36}
37
38/**
39 * Run the connect/dispatch thread.
40 */
41void
42HW::cmd_q::rx_run()
43{
44 while (m_connected) {
45 m_conn.ctx().dispatch();
46 }
47}
48
49void
50HW::cmd_q::enqueue(cmd* c)
51{
52 std::shared_ptr<cmd> sp(c);
53
54 m_queue.push_back(sp);
55}
56
57void
58HW::cmd_q::enqueue(std::shared_ptr<cmd> c)
59{
60 m_queue.push_back(c);
61}
62
63void
64HW::cmd_q::enqueue(std::queue<cmd*>& cmds)
65{
66 while (cmds.size()) {
67 std::shared_ptr<cmd> sp(cmds.front());
68
69 m_queue.push_back(sp);
70 cmds.pop();
71 }
72}
73
Mohsin Kazmi18e955e2018-01-08 12:58:32 +010074bool
Neale Ranns812ed392017-10-16 04:20:13 -070075HW::cmd_q::connect()
76{
Mohsin Kazmi18e955e2018-01-08 12:58:32 +010077 if (m_connected)
78 return m_connected;
79
Neale Rannsf7564012018-03-20 16:30:51 -070080 if (0 == m_conn.connect()) {
81 m_connected = true;
82 m_rx_thread.reset(new std::thread(&HW::cmd_q::rx_run, this));
83 }
84 return (m_connected);
Mohsin Kazmi18e955e2018-01-08 12:58:32 +010085}
86
87void
88HW::cmd_q::disconnect()
89{
90
91 if (!m_connected)
92 return;
Neale Ranns812ed392017-10-16 04:20:13 -070093
94 m_connected = false;
95
96 if (m_rx_thread && m_rx_thread->joinable()) {
97 m_rx_thread->join();
98 }
99
Mohsin Kazmi18e955e2018-01-08 12:58:32 +0100100 m_conn.disconnect();
Neale Ranns812ed392017-10-16 04:20:13 -0700101}
102
103void
104HW::cmd_q::enable()
105{
106 m_enabled = true;
107}
108
109void
110HW::cmd_q::disable()
111{
112 m_enabled = false;
113}
114
115rc_t
116HW::cmd_q::write()
117{
118 rc_t rc = rc_t::OK;
119
120 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800121 * The queue is enabled, Execute each command in the queue.
122 * If one execution fails, abort the rest
123 */
Neale Ranns812ed392017-10-16 04:20:13 -0700124 auto it = m_queue.begin();
125
126 while (it != m_queue.end()) {
127 std::shared_ptr<cmd> c = *it;
128
129 VOM_LOG(log_level_t::DEBUG) << *c;
130
131 if (m_enabled) {
132 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800133 * before we issue the command we must move it to the pending
134 * store
135 * ince a async event can be recieved before the command
136 * completes
137 */
Neale Ranns812ed392017-10-16 04:20:13 -0700138 rc = c->issue(m_conn);
139
Neale Rannsa2ee0292017-11-28 22:29:13 -0800140 if (rc_t::OK == rc) {
Neale Ranns812ed392017-10-16 04:20:13 -0700141 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -0800142 * move to the next
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800143 */
Neale Ranns812ed392017-10-16 04:20:13 -0700144 } else {
145 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -0800146 * barf out without issuing the rest
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800147 */
Neale Rannsa2ee0292017-11-28 22:29:13 -0800148 VOM_LOG(log_level_t::ERROR) << "Failed to execute: " << c->to_string();
149 break;
Neale Ranns812ed392017-10-16 04:20:13 -0700150 }
151 } else {
152 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800153 * The HW is disabled, so set each command as succeeded
154 */
Neale Ranns812ed392017-10-16 04:20:13 -0700155 c->succeeded();
156 }
157
158 ++it;
159 }
160
161 /*
Neale Ranns8ac4ce82017-11-17 05:08:55 -0800162 * erase all objects in the queue
163 */
Neale Ranns812ed392017-10-16 04:20:13 -0700164 m_queue.erase(m_queue.begin(), m_queue.end());
165
166 return (rc);
167}
168
169/*
170 * The single Command Queue
171 */
172HW::cmd_q* HW::m_cmdQ;
173HW::item<bool> HW::m_poll_state;
174
175/**
176 * Initialse the connection to VPP
177 */
178void
179HW::init(HW::cmd_q* f)
180{
181 m_cmdQ = f;
182}
183
184/**
185 * Initialse the connection to VPP
186 */
187void
188HW::init()
189{
190 m_cmdQ = new cmd_q();
191}
192
193void
194HW::enqueue(cmd* cmd)
195{
196 m_cmdQ->enqueue(cmd);
197}
198
199void
200HW::enqueue(std::shared_ptr<cmd> cmd)
201{
202 m_cmdQ->enqueue(cmd);
203}
204
205void
206HW::enqueue(std::queue<cmd*>& cmds)
207{
208 m_cmdQ->enqueue(cmds);
209}
210
Mohsin Kazmi18e955e2018-01-08 12:58:32 +0100211bool
Neale Ranns812ed392017-10-16 04:20:13 -0700212HW::connect()
213{
Mohsin Kazmi18e955e2018-01-08 12:58:32 +0100214 return m_cmdQ->connect();
215}
216
217void
218HW::disconnect()
219{
220 m_cmdQ->disconnect();
Neale Ranns812ed392017-10-16 04:20:13 -0700221}
222
223void
224HW::enable()
225{
226 m_cmdQ->enable();
227}
228
229void
230HW::disable()
231{
232 m_cmdQ->disable();
233}
234
235rc_t
236HW::write()
237{
238 return (m_cmdQ->write());
239}
240
241bool
242HW::poll()
243{
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700244 std::shared_ptr<cmd> poll(new hw_cmds::poll(m_poll_state));
Neale Ranns812ed392017-10-16 04:20:13 -0700245
246 HW::enqueue(poll);
247 HW::write();
248
249 return (m_poll_state);
250}
251
252template <>
253std::string
254HW::item<bool>::to_string() const
255{
256 std::ostringstream os;
257
258 os << "hw-item:["
259 << "rc:" << item_rc.to_string() << " data:" << item_data << "]";
260 return (os.str());
261}
262
263template <>
264std::string
265HW::item<unsigned int>::to_string() const
266{
267 std::ostringstream os;
268
269 os << "hw-item:["
270 << "rc:" << item_rc.to_string() << " data:" << item_data << "]";
271 return (os.str());
272}
Neale Ranns812ed392017-10-16 04:20:13 -0700273}
274
275/*
276 * fd.io coding-style-patch-verification: ON
277 *
278 * Local Variables:
279 * eval: (c-set-style "mozilla")
280 * End:
281 */