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 bool connect();
230 virtual void disconnect();
233 * Disable the passing of commands to VPP. Whilst disabled all
234 * writes will be discarded. Use this during the reset phase.
239 * Enable the passing of commands to VPP - undoes the disable.
240 * The Q is enabled by default.
246 * A queue of enqueued commands, ready to be written
248 std::deque<std::shared_ptr<cmd>> m_queue;
251 * A map of issued, but uncompleted, commands.
252 * i.e. those that we are waiting, async stylee,
253 * for VPP to complete
255 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
258 * VPP Q poll function
263 * The thread object running the poll/dispatch/connect thread
265 std::unique_ptr<std::thread> m_rx_thread;
268 * A flag indicating the client has disabled the cmd Q.
273 * A flag for the thread to poll to see if the queue is still alive
278 * The connection to VPP
284 * Initialise the HW connection to VPP - the UT version passing
287 static void init(cmd_q* f);
295 * Enqueue A command for execution
297 static void enqueue(cmd* f);
300 * Enqueue A command for execution
302 static void enqueue(std::shared_ptr<cmd> c);
305 * Enqueue A set of commands for execution
307 static void enqueue(std::queue<cmd*>& c);
310 * Write/Execute all commands hitherto enqueued.
315 * Blocking Connect to VPP
317 static bool connect();
322 static void disconnect();
325 * Blocking pool of the HW connection
331 * The command Q toward HW
333 static cmd_q* m_cmdQ;
336 * HW::item representing the connection state as determined by polling
338 static HW::item<bool> m_poll_state;
341 * Disable the passing of commands to VPP. Whilst disabled all writes
342 * will be discarded. Use this during the reset phase.
344 static void disable();
347 * Enable the passing of commands to VPP - undoes the disable.
348 * The Q is enabled by default.
350 static void enable();
353 * Only the OM can enable/disable HW
359 * bool Specialisation for HW::item to_string
362 std::string HW::item<bool>::to_string() const;
365 * uint Specialisation for HW::item to_string
368 std::string HW::item<unsigned int>::to_string() const;
372 * fd.io coding-style-patch-verification: ON
375 * eval: (c-set-style "mozilla")