Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include "vom/hw.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 17 | #include "vom/hw_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 18 | #include "vom/logger.hpp" |
| 19 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 20 | namespace VOM { |
| 21 | HW::cmd_q::cmd_q() |
| 22 | : m_enabled(true) |
| 23 | , m_connected(false) |
| 24 | , m_conn() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | HW::cmd_q::~cmd_q() |
| 29 | { |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | HW::cmd_q& |
| 33 | HW::cmd_q::operator=(const HW::cmd_q& f) |
| 34 | { |
| 35 | return (*this); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Run the connect/dispatch thread. |
| 40 | */ |
| 41 | void |
| 42 | HW::cmd_q::rx_run() |
| 43 | { |
| 44 | while (m_connected) { |
| 45 | m_conn.ctx().dispatch(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | HW::cmd_q::enqueue(cmd* c) |
| 51 | { |
| 52 | std::shared_ptr<cmd> sp(c); |
| 53 | |
| 54 | m_queue.push_back(sp); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | HW::cmd_q::enqueue(std::shared_ptr<cmd> c) |
| 59 | { |
| 60 | m_queue.push_back(c); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | HW::cmd_q::enqueue(std::queue<cmd*>& cmds) |
| 65 | { |
| 66 | while (cmds.size()) { |
| 67 | std::shared_ptr<cmd> sp(cmds.front()); |
| 68 | |
| 69 | m_queue.push_back(sp); |
| 70 | cmds.pop(); |
| 71 | } |
| 72 | } |
| 73 | |
Mohsin Kazmi | 18e955e | 2018-01-08 12:58:32 +0100 | [diff] [blame] | 74 | bool |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 75 | HW::cmd_q::connect() |
| 76 | { |
Mohsin Kazmi | 18e955e | 2018-01-08 12:58:32 +0100 | [diff] [blame] | 77 | int rv; |
| 78 | |
| 79 | if (m_connected) |
| 80 | return m_connected; |
| 81 | |
| 82 | rv = m_conn.connect(); |
| 83 | |
| 84 | m_connected = true; |
| 85 | m_rx_thread.reset(new std::thread(&HW::cmd_q::rx_run, this)); |
| 86 | return (rv == 0); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | HW::cmd_q::disconnect() |
| 91 | { |
| 92 | |
| 93 | if (!m_connected) |
| 94 | return; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 95 | |
| 96 | m_connected = false; |
| 97 | |
| 98 | if (m_rx_thread && m_rx_thread->joinable()) { |
| 99 | m_rx_thread->join(); |
| 100 | } |
| 101 | |
Mohsin Kazmi | 18e955e | 2018-01-08 12:58:32 +0100 | [diff] [blame] | 102 | m_conn.disconnect(); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
| 106 | HW::cmd_q::enable() |
| 107 | { |
| 108 | m_enabled = true; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | HW::cmd_q::disable() |
| 113 | { |
| 114 | m_enabled = false; |
| 115 | } |
| 116 | |
| 117 | rc_t |
| 118 | HW::cmd_q::write() |
| 119 | { |
| 120 | rc_t rc = rc_t::OK; |
| 121 | |
| 122 | /* |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 123 | * The queue is enabled, Execute each command in the queue. |
| 124 | * If one execution fails, abort the rest |
| 125 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 126 | auto it = m_queue.begin(); |
| 127 | |
| 128 | while (it != m_queue.end()) { |
| 129 | std::shared_ptr<cmd> c = *it; |
| 130 | |
| 131 | VOM_LOG(log_level_t::DEBUG) << *c; |
| 132 | |
| 133 | if (m_enabled) { |
| 134 | /* |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 135 | * before we issue the command we must move it to the pending |
| 136 | * store |
| 137 | * ince a async event can be recieved before the command |
| 138 | * completes |
| 139 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 140 | rc = c->issue(m_conn); |
| 141 | |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 142 | if (rc_t::OK == rc) { |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 143 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 144 | * move to the next |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 145 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 146 | } else { |
| 147 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 148 | * barf out without issuing the rest |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 149 | */ |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 150 | VOM_LOG(log_level_t::ERROR) << "Failed to execute: " << c->to_string(); |
| 151 | break; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 152 | } |
| 153 | } else { |
| 154 | /* |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 155 | * The HW is disabled, so set each command as succeeded |
| 156 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 157 | c->succeeded(); |
| 158 | } |
| 159 | |
| 160 | ++it; |
| 161 | } |
| 162 | |
| 163 | /* |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 164 | * erase all objects in the queue |
| 165 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 166 | m_queue.erase(m_queue.begin(), m_queue.end()); |
| 167 | |
| 168 | return (rc); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * The single Command Queue |
| 173 | */ |
| 174 | HW::cmd_q* HW::m_cmdQ; |
| 175 | HW::item<bool> HW::m_poll_state; |
| 176 | |
| 177 | /** |
| 178 | * Initialse the connection to VPP |
| 179 | */ |
| 180 | void |
| 181 | HW::init(HW::cmd_q* f) |
| 182 | { |
| 183 | m_cmdQ = f; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Initialse the connection to VPP |
| 188 | */ |
| 189 | void |
| 190 | HW::init() |
| 191 | { |
| 192 | m_cmdQ = new cmd_q(); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | HW::enqueue(cmd* cmd) |
| 197 | { |
| 198 | m_cmdQ->enqueue(cmd); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | HW::enqueue(std::shared_ptr<cmd> cmd) |
| 203 | { |
| 204 | m_cmdQ->enqueue(cmd); |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | HW::enqueue(std::queue<cmd*>& cmds) |
| 209 | { |
| 210 | m_cmdQ->enqueue(cmds); |
| 211 | } |
| 212 | |
Mohsin Kazmi | 18e955e | 2018-01-08 12:58:32 +0100 | [diff] [blame] | 213 | bool |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 214 | HW::connect() |
| 215 | { |
Mohsin Kazmi | 18e955e | 2018-01-08 12:58:32 +0100 | [diff] [blame] | 216 | return m_cmdQ->connect(); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | HW::disconnect() |
| 221 | { |
| 222 | m_cmdQ->disconnect(); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void |
| 226 | HW::enable() |
| 227 | { |
| 228 | m_cmdQ->enable(); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | HW::disable() |
| 233 | { |
| 234 | m_cmdQ->disable(); |
| 235 | } |
| 236 | |
| 237 | rc_t |
| 238 | HW::write() |
| 239 | { |
| 240 | return (m_cmdQ->write()); |
| 241 | } |
| 242 | |
| 243 | bool |
| 244 | HW::poll() |
| 245 | { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 246 | std::shared_ptr<cmd> poll(new hw_cmds::poll(m_poll_state)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 247 | |
| 248 | HW::enqueue(poll); |
| 249 | HW::write(); |
| 250 | |
| 251 | return (m_poll_state); |
| 252 | } |
| 253 | |
| 254 | template <> |
| 255 | std::string |
| 256 | HW::item<bool>::to_string() const |
| 257 | { |
| 258 | std::ostringstream os; |
| 259 | |
| 260 | os << "hw-item:[" |
| 261 | << "rc:" << item_rc.to_string() << " data:" << item_data << "]"; |
| 262 | return (os.str()); |
| 263 | } |
| 264 | |
| 265 | template <> |
| 266 | std::string |
| 267 | HW::item<unsigned int>::to_string() const |
| 268 | { |
| 269 | std::ostringstream os; |
| 270 | |
| 271 | os << "hw-item:[" |
| 272 | << "rc:" << item_rc.to_string() << " data:" << item_data << "]"; |
| 273 | return (os.str()); |
| 274 | } |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * fd.io coding-style-patch-verification: ON |
| 279 | * |
| 280 | * Local Variables: |
| 281 | * eval: (c-set-style "mozilla") |
| 282 | * End: |
| 283 | */ |