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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "vom/connection.hpp"
26 #include "vom/types.hpp"
35 * A HW::item is data that is either to be written to or read from
37 * The item is a pair of the data written/read and the result of that
57 , item_rc(rc_t::UNSET)
73 item(const T& data, rc_t rc)
82 bool operator==(const item<T>& i) const
84 return (item_data == i.item_data);
90 item& operator=(const item& other)
92 item_data = other.item_data;
93 item_rc = other.item_rc;
99 * Return the data read/written
101 T& data() { return (item_data); }
104 * Const reference to the data
106 const T& data() const { return (item_data); }
109 * Get the HW return code
111 rc_t rc() const { return (item_rc); }
114 * Set the HW return code - should only be called from the
115 * family of Command objects
117 void set(const rc_t& rc) { item_rc = rc; }
120 * Return true if the HW item is configred in HW
122 operator bool() const { return (rc_t::OK == item_rc); }
125 * update the item to the desired state.
126 * return true if a HW update is required
128 bool update(const item& desired)
130 bool need_hw_update = false;
133 * if the deisred set is unset (i.e. defaulted, we've
136 if (rc_t::UNSET == desired.rc()) {
140 * A HW update is needed if thestate is different
141 * or the state is not yet in HW
143 need_hw_update = (item_data != desired.data() || rc_t::OK != rc());
145 item_data = desired.data();
147 return (need_hw_update);
151 * convert to string format for debug purposes
153 std::string to_string() const
155 std::ostringstream os;
158 << "rc:" << item_rc.to_string() << " data:" << item_data.to_string()
171 * The result when the item was written
177 * The pipe to VPP into which we write the commands
192 * Copy assignement - only used in UT
194 cmd_q& operator=(const cmd_q& f);
197 * Enqueue a command into the Q.
199 virtual void enqueue(cmd* c);
201 * Enqueue a command into the Q.
203 virtual void enqueue(std::shared_ptr<cmd> c);
206 * Enqueue a set of commands
208 virtual void enqueue(std::queue<cmd*>& c);
211 * dequeue a command from the Q.
213 virtual void dequeue(cmd* c);
216 * dequeue a command from the Q.
218 virtual void dequeue(std::shared_ptr<cmd> c);
221 * Write all the commands to HW
223 virtual rc_t write();
226 * Blocking Connect to VPP - call once at bootup
231 * Disable the passing of commands to VPP. Whilst disabled all
232 * writes will be discarded. Use this during the reset phase.
237 * Enable the passing of commands to VPP - undoes the disable.
238 * The Q is enabled by default.
244 * A queue of enqueued commands, ready to be written
246 std::deque<std::shared_ptr<cmd>> m_queue;
249 * A map of issued, but uncompleted, commands.
250 * i.e. those that we are waiting, async stylee,
251 * for VPP to complete
253 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
256 * VPP Q poll function
261 * The thread object running the poll/dispatch/connect thread
263 std::unique_ptr<std::thread> m_rx_thread;
266 * A flag indicating the client has disabled the cmd Q.
271 * A flag for the thread to poll to see if the queue is still alive
276 * The connection to VPP
282 * Initialise the HW connection to VPP - the UT version passing
285 static void init(cmd_q* f);
293 * Enqueue A command for execution
295 static void enqueue(cmd* f);
298 * Enqueue A command for execution
300 static void enqueue(std::shared_ptr<cmd> c);
303 * Enqueue A set of commands for execution
305 static void enqueue(std::queue<cmd*>& c);
308 * dequeue A command for execution
310 static void dequeue(cmd* f);
313 * dequeue A command for execution
315 static void dequeue(std::shared_ptr<cmd> c);
318 * Write/Execute all commands hitherto enqueued.
323 * Blocking Connect to VPP
325 static void connect();
328 * Blocking pool of the HW connection
334 * The command Q toward HW
336 static cmd_q* m_cmdQ;
339 * HW::item representing the connection state as determined by polling
341 static HW::item<bool> m_poll_state;
344 * Disable the passing of commands to VPP. Whilst disabled all writes
345 * will be discarded. Use this during the reset phase.
347 static void disable();
350 * Enable the passing of commands to VPP - undoes the disable.
351 * The Q is enabled by default.
353 static void enable();
356 * Only the OM can enable/disable HW
362 * bool Specialisation for HW::item to_string
365 std::string HW::item<bool>::to_string() const;
368 * uint Specialisation for HW::item to_string
371 std::string HW::item<unsigned int>::to_string() const;
375 * fd.io coding-style-patch-verification: ON
378 * eval: (c-set-style "mozilla")