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 * dequeue a command from the Q.
220 virtual void dequeue(cmd* c);
223 * dequeue a command from the Q.
225 virtual void dequeue(std::shared_ptr<cmd> c);
228 * Write all the commands to HW
230 virtual rc_t write();
233 * Blocking Connect to VPP - call once at bootup
235 virtual void connect();
238 * Disable the passing of commands to VPP. Whilst disabled all
239 * writes will be discarded. Use this during the reset phase.
244 * Enable the passing of commands to VPP - undoes the disable.
245 * The Q is enabled by default.
251 * A queue of enqueued commands, ready to be written
253 std::deque<std::shared_ptr<cmd>> m_queue;
256 * A map of issued, but uncompleted, commands.
257 * i.e. those that we are waiting, async stylee,
258 * for VPP to complete
260 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
263 * VPP Q poll function
268 * The thread object running the poll/dispatch/connect thread
270 std::unique_ptr<std::thread> m_rx_thread;
273 * A flag indicating the client has disabled the cmd Q.
278 * A flag for the thread to poll to see if the queue is still alive
283 * The connection to VPP
289 * Initialise the HW connection to VPP - the UT version passing
292 static void init(cmd_q* f);
300 * Enqueue A command for execution
302 static void enqueue(cmd* f);
305 * Enqueue A command for execution
307 static void enqueue(std::shared_ptr<cmd> c);
310 * Enqueue A set of commands for execution
312 static void enqueue(std::queue<cmd*>& c);
315 * dequeue A command for execution
317 static void dequeue(cmd* f);
320 * dequeue A command for execution
322 static void dequeue(std::shared_ptr<cmd> c);
325 * Write/Execute all commands hitherto enqueued.
330 * Blocking Connect to VPP
332 static void connect();
335 * Blocking pool of the HW connection
341 * The command Q toward HW
343 static cmd_q* m_cmdQ;
346 * HW::item representing the connection state as determined by polling
348 static HW::item<bool> m_poll_state;
351 * Disable the passing of commands to VPP. Whilst disabled all writes
352 * will be discarded. Use this during the reset phase.
354 static void disable();
357 * Enable the passing of commands to VPP - undoes the disable.
358 * The Q is enabled by default.
360 static void enable();
363 * Only the OM can enable/disable HW
369 * bool Specialisation for HW::item to_string
372 std::string HW::item<bool>::to_string() const;
375 * uint Specialisation for HW::item to_string
378 std::string HW::item<unsigned int>::to_string() const;
382 * fd.io coding-style-patch-verification: ON
385 * eval: (c-set-style "mozilla")