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"
38 * A HW::item is data that is either to be written to or read from
40 * The item is a pair of the data written/read and the result of that
60 , item_rc(rc_t::UNSET)
76 item(const T& data, rc_t rc)
90 bool operator==(const item<T>& i) const
92 return (item_data == i.item_data);
98 item& operator=(const item& other)
100 item_data = other.item_data;
101 item_rc = other.item_rc;
107 * Return the data read/written
109 T& data() { return (item_data); }
112 * Const reference to the data
114 const T& data() const { return (item_data); }
117 * Get the HW return code
119 rc_t rc() const { return (item_rc); }
122 * Set the HW return code - should only be called from the
123 * family of Command objects
125 void set(const rc_t& rc) { item_rc = rc; }
128 * Return true if the HW item is configred in HW
130 operator bool() const { return (rc_t::OK == item_rc); }
133 * update the item to the desired state.
134 * return true if a HW update is required
136 bool update(const item& desired)
138 bool need_hw_update = false;
141 * if the deisred set is unset (i.e. defaulted, we've
144 if (rc_t::UNSET == desired.rc()) {
148 * A HW update is needed if thestate is different
149 * or the state is not yet in HW
151 need_hw_update = (item_data != desired.data() || rc_t::OK != rc());
153 item_data = desired.data();
155 return (need_hw_update);
159 * convert to string format for debug purposes
161 std::string to_string() const
163 std::ostringstream os;
166 << "rc:" << item_rc.to_string() << " data:" << item_data.to_string()
179 * The result when the item was written
185 * The pipe to VPP into which we write the commands
200 * Copy assignement - only used in UT
202 cmd_q& operator=(const cmd_q& f);
205 * Enqueue a command into the Q.
207 virtual void enqueue(cmd* c);
209 * Enqueue a command into the Q.
211 virtual void enqueue(std::shared_ptr<cmd> c);
214 * Enqueue a set of commands
216 virtual void enqueue(std::queue<cmd*>& c);
219 * Write all the commands to HW
221 virtual rc_t write();
224 * Blocking Connect to VPP - call once at bootup
226 virtual bool connect();
231 virtual void disconnect();
234 * Disable the passing of commands to VPP. Whilst disabled all
235 * writes will be discarded. Use this during the reset phase.
240 * Enable the passing of commands to VPP - undoes the disable.
241 * The Q is enabled by default.
247 * A queue of enqueued commands, ready to be written
249 std::deque<std::shared_ptr<cmd>> m_queue;
252 * A map of issued, but uncompleted, commands.
253 * i.e. those that we are waiting, async stylee,
254 * for VPP to complete
256 std::map<cmd*, std::shared_ptr<cmd>> m_pending;
259 * VPP Q poll function
264 * The thread object running the poll/dispatch/connect thread
266 std::unique_ptr<std::thread> m_rx_thread;
269 * A flag indicating the client has disabled the cmd Q.
274 * A flag for the thread to poll to see if the queue is still alive
279 * The connection to VPP
285 * Initialise the HW connection to VPP - the UT version passing
288 static void init(cmd_q* f);
291 * Initialise the HW connection to VPP - the UT version passing
294 static void init(cmd_q* f, stat_reader* s);
302 * Enqueue A command for execution
304 static void enqueue(cmd* f);
307 * Enqueue A command for execution
309 static void enqueue(std::shared_ptr<cmd> c);
312 * Enqueue A set of commands for execution
314 static void enqueue(std::queue<cmd*>& c);
317 * Write/Execute all commands hitherto enqueued.
322 * Blocking Connect to VPP
324 static bool connect();
329 static void disconnect();
332 * Blocking pool of the HW connection
337 * read stats from stat segment
339 static void read_stats();
343 * The command Q toward HW
345 static cmd_q* m_cmdQ;
348 * The stat reader toward HW
350 static stat_reader* m_statReader;
353 * HW::item representing the connection state as determined by polling
355 static HW::item<bool> m_poll_state;
358 * Disable the passing of commands to VPP. Whilst disabled all writes
359 * will be discarded. Use this during the reset phase.
361 static void disable();
364 * Enable the passing of commands to VPP - undoes the disable.
365 * The Q is enabled by default.
367 static void enable();
370 * Only the OM can enable/disable HW
376 * bool Specialisation for HW::item to_string
379 std::string HW::item<bool>::to_string() const;
382 * uint Specialisation for HW::item to_string
385 std::string HW::item<unsigned int>::to_string() const;
389 * fd.io coding-style-patch-verification: ON
392 * eval: (c-set-style "mozilla")