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.
26 #include "vom/cmd.hpp"
27 #include "vom/connection.hpp"
28 #include "vom/types.hpp"
37 * A HW::item is data that is either to be written to or read from
39 * The item is a pair of the data written/read and the result of that
59 , item_rc(rc_t::UNSET)
75 item(const T& data, rc_t rc)
89 bool operator==(const item<T>& i) const
91 return (item_data == i.item_data);
97 item& operator=(const item& other)
99 item_data = other.item_data;
100 item_rc = other.item_rc;
106 * Return the data read/written
108 T& data() { return (item_data); }
111 * Const reference to the data
113 const T& data() const { return (item_data); }
116 * Get the HW return code
118 rc_t rc() const { return (item_rc); }
121 * Set the HW return code - should only be called from the
122 * family of Command objects
124 void set(const rc_t& rc) { item_rc = rc; }
127 * Return true if the HW item is configred in HW
129 operator bool() const { return (rc_t::OK == item_rc); }
132 * update the item to the desired state.
133 * return true if a HW update is required
135 bool update(const item& desired)
137 bool need_hw_update = false;
140 * if the deisred set is unset (i.e. defaulted, we've
143 if (rc_t::UNSET == desired.rc()) {
147 * A HW update is needed if thestate is different
148 * or the state is not yet in HW
150 need_hw_update = (item_data != desired.data() || rc_t::OK != rc());
152 item_data = desired.data();
154 return (need_hw_update);
158 * convert to string format for debug purposes
160 std::string to_string() const
162 std::ostringstream os;
165 << "rc:" << item_rc.to_string() << " data:" << item_data.to_string()
178 * The result when the item was written
184 * The pipe to VPP into which we write the commands
199 * Copy assignement - only used in UT
201 cmd_q& operator=(const cmd_q& f);
204 * Enqueue a command into the Q.
206 virtual void enqueue(cmd* c);
208 * Enqueue a command into the Q.
210 virtual void enqueue(std::shared_ptr<cmd> c);
213 * Enqueue a set of commands
215 virtual void enqueue(std::queue<cmd*>& c);
218 * Write all the commands to HW
220 virtual rc_t write();
223 * Blocking Connect to VPP - call once at bootup
225 virtual void connect();
228 * Disable the passing of commands to VPP. Whilst disabled all
229 * writes will be discarded. Use this during the reset phase.
234 * Enable the passing of commands to VPP - undoes the disable.
235 * The Q is enabled by default.
241 * A queue of enqueued commands, ready to be written
243 std::deque<std::shared_ptr<cmd>> m_queue;
246 * A map of issued, but uncompleted, commands.
247 * i.e. those that we are waiting, async stylee,
248 * for VPP to complete
250 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
253 * VPP Q poll function
258 * The thread object running the poll/dispatch/connect thread
260 std::unique_ptr<std::thread> m_rx_thread;
263 * A flag indicating the client has disabled the cmd Q.
268 * A flag for the thread to poll to see if the queue is still alive
273 * The connection to VPP
279 * Initialise the HW connection to VPP - the UT version passing
282 static void init(cmd_q* f);
290 * Enqueue A command for execution
292 static void enqueue(cmd* f);
295 * Enqueue A command for execution
297 static void enqueue(std::shared_ptr<cmd> c);
300 * Enqueue A set of commands for execution
302 static void enqueue(std::queue<cmd*>& c);
305 * Write/Execute all commands hitherto enqueued.
310 * Blocking Connect to VPP
312 static void connect();
315 * Blocking pool of the HW connection
321 * The command Q toward HW
323 static cmd_q* m_cmdQ;
326 * HW::item representing the connection state as determined by polling
328 static HW::item<bool> m_poll_state;
331 * Disable the passing of commands to VPP. Whilst disabled all writes
332 * will be discarded. Use this during the reset phase.
334 static void disable();
337 * Enable the passing of commands to VPP - undoes the disable.
338 * The Q is enabled by default.
340 static void enable();
343 * Only the OM can enable/disable HW
349 * bool Specialisation for HW::item to_string
352 std::string HW::item<bool>::to_string() const;
355 * uint Specialisation for HW::item to_string
358 std::string HW::item<unsigned int>::to_string() const;
362 * fd.io coding-style-patch-verification: ON
365 * eval: (c-set-style "mozilla")