API: Change ip4_address and ip6_address to use type alias.
[vpp.git] / extras / vom / vom / hw.cpp
1 /*
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"
17 #include "vom/hw_cmds.hpp"
18 #include "vom/logger.hpp"
19
20 namespace VOM {
21 HW::cmd_q::cmd_q()
22   : m_enabled(true)
23   , m_connected(false)
24   , m_conn()
25 {
26 }
27
28 HW::cmd_q::~cmd_q()
29 {
30 }
31
32 HW::cmd_q&
33 HW::cmd_q::operator=(const HW::cmd_q& f)
34 {
35   return (*this);
36 }
37
38 /**
39  * Run the connect/dispatch thread.
40  */
41 void
42 HW::cmd_q::rx_run()
43 {
44   while (m_connected) {
45     m_conn.ctx().dispatch();
46   }
47 }
48
49 void
50 HW::cmd_q::enqueue(cmd* c)
51 {
52   std::shared_ptr<cmd> sp(c);
53
54   m_queue.push_back(sp);
55 }
56
57 void
58 HW::cmd_q::enqueue(std::shared_ptr<cmd> c)
59 {
60   m_queue.push_back(c);
61 }
62
63 void
64 HW::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
74 bool
75 HW::cmd_q::connect()
76 {
77   if (m_connected)
78     return m_connected;
79
80   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);
85 }
86
87 void
88 HW::cmd_q::disconnect()
89 {
90
91   if (!m_connected)
92     return;
93
94   m_connected = false;
95
96   if (m_rx_thread && m_rx_thread->joinable()) {
97     m_rx_thread->join();
98   }
99
100   m_conn.disconnect();
101 }
102
103 void
104 HW::cmd_q::enable()
105 {
106   m_enabled = true;
107 }
108
109 void
110 HW::cmd_q::disable()
111 {
112   m_enabled = false;
113 }
114
115 rc_t
116 HW::cmd_q::write()
117 {
118   rc_t rc = rc_t::OK;
119
120   /*
121    * The queue is enabled, Execute each command in the queue.
122    * If one execution fails, abort the rest
123    */
124   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       /*
133        * 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        */
138       rc = c->issue(m_conn);
139
140       if (rc_t::OK == rc) {
141         /*
142          * move to the next
143          */
144       } else {
145         /*
146          * barf out without issuing the rest
147          */
148         VOM_LOG(log_level_t::ERROR) << "Failed to execute: " << c->to_string();
149         break;
150       }
151     } else {
152       /*
153        * The HW is disabled, so set each command as succeeded
154        */
155       c->succeeded();
156     }
157
158     ++it;
159   }
160
161   /*
162    * erase all objects in the queue
163    */
164   m_queue.erase(m_queue.begin(), m_queue.end());
165
166   return (rc);
167 }
168
169 /*
170  * The single Command Queue
171  */
172 HW::cmd_q* HW::m_cmdQ;
173 HW::item<bool> HW::m_poll_state;
174
175 /**
176  * Initialse the connection to VPP
177  */
178 void
179 HW::init(HW::cmd_q* f)
180 {
181   m_cmdQ = f;
182 }
183
184 /**
185  * Initialse the connection to VPP
186  */
187 void
188 HW::init()
189 {
190   m_cmdQ = new cmd_q();
191 }
192
193 void
194 HW::enqueue(cmd* cmd)
195 {
196   m_cmdQ->enqueue(cmd);
197 }
198
199 void
200 HW::enqueue(std::shared_ptr<cmd> cmd)
201 {
202   m_cmdQ->enqueue(cmd);
203 }
204
205 void
206 HW::enqueue(std::queue<cmd*>& cmds)
207 {
208   m_cmdQ->enqueue(cmds);
209 }
210
211 bool
212 HW::connect()
213 {
214   return m_cmdQ->connect();
215 }
216
217 void
218 HW::disconnect()
219 {
220   m_cmdQ->disconnect();
221 }
222
223 void
224 HW::enable()
225 {
226   m_cmdQ->enable();
227 }
228
229 void
230 HW::disable()
231 {
232   m_cmdQ->disable();
233 }
234
235 rc_t
236 HW::write()
237 {
238   return (m_cmdQ->write());
239 }
240
241 bool
242 HW::poll()
243 {
244   std::shared_ptr<cmd> poll(new hw_cmds::poll(m_poll_state));
245
246   HW::enqueue(poll);
247   HW::write();
248
249   return (m_poll_state);
250 }
251
252 template <>
253 std::string
254 HW::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
263 template <>
264 std::string
265 HW::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 }
273 }
274
275 /*
276  * fd.io coding-style-patch-verification: ON
277  *
278  * Local Variables:
279  * eval: (c-set-style "mozilla")
280  * End:
281  */