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)
87 bool operator==(const item<T>& i) const
89 return (item_data == i.item_data);
95 item& operator=(const item& other)
97 item_data = other.item_data;
98 item_rc = other.item_rc;
104 * Return the data read/written
106 T& data() { return (item_data); }
109 * Const reference to the data
111 const T& data() const { return (item_data); }
114 * Get the HW return code
116 rc_t rc() const { return (item_rc); }
119 * Set the HW return code - should only be called from the
120 * family of Command objects
122 void set(const rc_t& rc) { item_rc = rc; }
125 * Return true if the HW item is configred in HW
127 operator bool() const { return (rc_t::OK == item_rc); }
130 * update the item to the desired state.
131 * return true if a HW update is required
133 bool update(const item& desired)
135 bool need_hw_update = false;
138 * if the deisred set is unset (i.e. defaulted, we've
141 if (rc_t::UNSET == desired.rc()) {
145 * A HW update is needed if thestate is different
146 * or the state is not yet in HW
148 need_hw_update = (item_data != desired.data() || rc_t::OK != rc());
150 item_data = desired.data();
152 return (need_hw_update);
156 * convert to string format for debug purposes
158 std::string to_string() const
160 std::ostringstream os;
163 << "rc:" << item_rc.to_string() << " data:" << item_data.to_string()
176 * The result when the item was written
182 * The pipe to VPP into which we write the commands
197 * Copy assignement - only used in UT
199 cmd_q& operator=(const cmd_q& f);
202 * Enqueue a command into the Q.
204 virtual void enqueue(cmd* c);
206 * Enqueue a command into the Q.
208 virtual void enqueue(std::shared_ptr<cmd> c);
211 * Enqueue a set of commands
213 virtual void enqueue(std::queue<cmd*>& c);
216 * dequeue a command from the Q.
218 virtual void dequeue(cmd* c);
221 * dequeue a command from the Q.
223 virtual void dequeue(std::shared_ptr<cmd> c);
226 * Write all the commands to HW
228 virtual rc_t write();
231 * Blocking Connect to VPP - call once at bootup
236 * Disable the passing of commands to VPP. Whilst disabled all
237 * writes will be discarded. Use this during the reset phase.
242 * Enable the passing of commands to VPP - undoes the disable.
243 * The Q is enabled by default.
249 * A queue of enqueued commands, ready to be written
251 std::deque<std::shared_ptr<cmd>> m_queue;
254 * A map of issued, but uncompleted, commands.
255 * i.e. those that we are waiting, async stylee,
256 * for VPP to complete
258 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
261 * VPP Q poll function
266 * The thread object running the poll/dispatch/connect thread
268 std::unique_ptr<std::thread> m_rx_thread;
271 * A flag indicating the client has disabled the cmd Q.
276 * A flag for the thread to poll to see if the queue is still alive
281 * The connection to VPP
287 * Initialise the HW connection to VPP - the UT version passing
290 static void init(cmd_q* f);
298 * Enqueue A command for execution
300 static void enqueue(cmd* f);
303 * Enqueue A command for execution
305 static void enqueue(std::shared_ptr<cmd> c);
308 * Enqueue A set of commands for execution
310 static void enqueue(std::queue<cmd*>& c);
313 * dequeue A command for execution
315 static void dequeue(cmd* f);
318 * dequeue A command for execution
320 static void dequeue(std::shared_ptr<cmd> c);
323 * Write/Execute all commands hitherto enqueued.
328 * Blocking Connect to VPP
330 static void connect();
333 * Blocking pool of the HW connection
339 * The command Q toward HW
341 static cmd_q* m_cmdQ;
344 * HW::item representing the connection state as determined by polling
346 static HW::item<bool> m_poll_state;
349 * Disable the passing of commands to VPP. Whilst disabled all writes
350 * will be discarded. Use this during the reset phase.
352 static void disable();
355 * Enable the passing of commands to VPP - undoes the disable.
356 * The Q is enabled by default.
358 static void enable();
361 * Only the OM can enable/disable HW
367 * bool Specialisation for HW::item to_string
370 std::string HW::item<bool>::to_string() const;
373 * uint Specialisation for HW::item to_string
376 std::string HW::item<unsigned int>::to_string() const;
380 * fd.io coding-style-patch-verification: ON
383 * eval: (c-set-style "mozilla")