diff options
author | Martin Braun <martin.braun@ettus.com> | 2020-01-23 10:32:59 -0800 |
---|---|---|
committer | atrnati <54334261+atrnati@users.noreply.github.com> | 2020-02-04 08:53:01 -0600 |
commit | 22db12c4b2b55225801ec1efb2465c7a06295b9e (patch) | |
tree | cc60264fffe91465df9c6cbe764bf60931934a56 /host/lib/usrp_clock | |
parent | defcb174ebb274d4621a2b958a78ed6cb6cd6aed (diff) | |
download | uhd-22db12c4b2b55225801ec1efb2465c7a06295b9e.tar.gz uhd-22db12c4b2b55225801ec1efb2465c7a06295b9e.tar.bz2 uhd-22db12c4b2b55225801ec1efb2465c7a06295b9e.zip |
octoclock: Apply clang-format
Diffstat (limited to 'host/lib/usrp_clock')
-rw-r--r-- | host/lib/usrp_clock/multi_usrp_clock.cpp | 47 | ||||
-rw-r--r-- | host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp | 125 | ||||
-rw-r--r-- | host/lib/usrp_clock/octoclock/octoclock_image_loader.cpp | 327 | ||||
-rw-r--r-- | host/lib/usrp_clock/octoclock/octoclock_impl.cpp | 418 | ||||
-rw-r--r-- | host/lib/usrp_clock/octoclock/octoclock_impl.hpp | 49 | ||||
-rw-r--r-- | host/lib/usrp_clock/octoclock/octoclock_uart.cpp | 268 | ||||
-rw-r--r-- | host/lib/usrp_clock/octoclock/octoclock_uart.hpp | 22 | ||||
-rw-r--r-- | host/lib/usrp_clock/usrp_clock_c.cpp | 156 |
8 files changed, 753 insertions, 659 deletions
diff --git a/host/lib/usrp_clock/multi_usrp_clock.cpp b/host/lib/usrp_clock/multi_usrp_clock.cpp index 42dc1eb0b..c4bf081cc 100644 --- a/host/lib/usrp_clock/multi_usrp_clock.cpp +++ b/host/lib/usrp_clock/multi_usrp_clock.cpp @@ -5,12 +5,10 @@ // SPDX-License-Identifier: GPL-3.0-or-later // +#include <uhd/exception.hpp> #include <uhd/property_tree.hpp> #include <uhd/usrp_clock/multi_usrp_clock.hpp> #include <uhd/usrp_clock/octoclock_eeprom.hpp> - - -#include <uhd/exception.hpp> #include <uhd/utils/log.hpp> #include <boost/assign/list_of.hpp> #include <boost/format.hpp> @@ -21,48 +19,54 @@ using namespace uhd::usrp_clock; /*********************************************************************** * Multi USRP Clock implementation **********************************************************************/ -class multi_usrp_clock_impl : public multi_usrp_clock{ +class multi_usrp_clock_impl : public multi_usrp_clock +{ public: - multi_usrp_clock_impl(const device_addr_t &addr){ - _dev = device::make(addr, device::CLOCK); + multi_usrp_clock_impl(const device_addr_t& addr) + { + _dev = device::make(addr, device::CLOCK); _tree = _dev->get_tree(); } - device::sptr get_device(void){ + device::sptr get_device(void) + { return _dev; } - std::string get_pp_string(void){ + std::string get_pp_string(void) + { std::string buff = str(boost::format("%s USRP Clock Device\n") - % ((get_num_boards() > 1) ? "Multi" : "Single") - ); - for(size_t i = 0; i < get_num_boards(); i++){ + % ((get_num_boards() > 1) ? "Multi" : "Single")); + for (size_t i = 0; i < get_num_boards(); i++) { buff += str(boost::format(" Board %s\n") % i); buff += str(boost::format(" Reference: %s\n") - % (get_sensor("using_ref", i).value) - ); + % (get_sensor("using_ref", i).value)); } return buff; } - size_t get_num_boards(void){ + size_t get_num_boards(void) + { return _tree->list("/mboards").size(); } - uint32_t get_time(size_t board){ + uint32_t get_time(size_t board) + { std::string board_str = str(boost::format("/mboards/%d") % board); return _tree->access<uint32_t>(board_str / "time").get(); } - sensor_value_t get_sensor(const std::string &name, size_t board){ + sensor_value_t get_sensor(const std::string& name, size_t board) + { std::string board_str = str(boost::format("/mboards/%d") % board); return _tree->access<sensor_value_t>(board_str / "sensors" / name).get(); } - std::vector<std::string> get_sensor_names(size_t board){ + std::vector<std::string> get_sensor_names(size_t board) + { std::string board_str = str(boost::format("/mboards/%d") % board); return _tree->list(board_str / "sensors"); @@ -73,14 +77,17 @@ private: property_tree::sptr _tree; }; -multi_usrp_clock::~multi_usrp_clock(void){ +multi_usrp_clock::~multi_usrp_clock(void) +{ /* NOP */ } /*********************************************************************** * Multi USRP Clock factory function **********************************************************************/ -multi_usrp_clock::sptr multi_usrp_clock::make(const device_addr_t &dev_addr){ - UHD_LOGGER_TRACE("OCTOCLOCK") << "multi_usrp_clock::make with args " << dev_addr.to_pp_string() ; +multi_usrp_clock::sptr multi_usrp_clock::make(const device_addr_t& dev_addr) +{ + UHD_LOGGER_TRACE("OCTOCLOCK") + << "multi_usrp_clock::make with args " << dev_addr.to_pp_string(); return sptr(new multi_usrp_clock_impl(dev_addr)); } diff --git a/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp b/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp index a9f91897e..349d704f3 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp +++ b/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp @@ -5,20 +5,18 @@ // SPDX-License-Identifier: GPL-3.0-or-later // +#include "common.h" #include <uhd/exception.hpp> -#include <uhd/usrp_clock/octoclock_eeprom.hpp> #include <uhd/transport/udp_simple.hpp> -#include <uhd/usrp/mboard_eeprom.hpp> #include <uhd/types/byte_vector.hpp> #include <uhd/types/mac_addr.hpp> +#include <uhd/usrp/mboard_eeprom.hpp> +#include <uhd/usrp_clock/octoclock_eeprom.hpp> #include <uhd/utils/byteswap.hpp> -#include <boost/assign/list_of.hpp> #include <boost/asio.hpp> - +#include <boost/assign/list_of.hpp> #include <iostream> -#include "common.h" - typedef boost::asio::ip::address_v4 ip_v4; using namespace uhd; @@ -28,129 +26,146 @@ using namespace uhd::transport; /*********************************************************************** * Implementation **********************************************************************/ -void octoclock_eeprom_t::_load(){ +void octoclock_eeprom_t::_load() +{ uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<const octoclock_packet_t*>(octoclock_data); - const octoclock_fw_eeprom_t *eeprom_in = reinterpret_cast<const octoclock_fw_eeprom_t*>(pkt_in->data); + const octoclock_packet_t* pkt_in = + reinterpret_cast<const octoclock_packet_t*>(octoclock_data); + const octoclock_fw_eeprom_t* eeprom_in = + reinterpret_cast<const octoclock_fw_eeprom_t*>(pkt_in->data); octoclock_packet_t pkt_out; // To avoid replicating sequence numbers between sessions pkt_out.sequence = uint32_t(std::rand()); - size_t len = 0; + size_t len = 0; - UHD_OCTOCLOCK_SEND_AND_RECV(xport, _proto_ver, SEND_EEPROM_CMD, pkt_out, len, octoclock_data); - if(UHD_OCTOCLOCK_PACKET_MATCHES(SEND_EEPROM_ACK, pkt_out, pkt_in, len)){ - //MAC address - byte_vector_t mac_bytes(eeprom_in->mac_addr, eeprom_in->mac_addr+6); + UHD_OCTOCLOCK_SEND_AND_RECV( + xport, _proto_ver, SEND_EEPROM_CMD, pkt_out, len, octoclock_data); + if (UHD_OCTOCLOCK_PACKET_MATCHES(SEND_EEPROM_ACK, pkt_out, pkt_in, len)) { + // MAC address + byte_vector_t mac_bytes(eeprom_in->mac_addr, eeprom_in->mac_addr + 6); (*this)["mac-addr"] = mac_addr_t::from_bytes(mac_bytes).to_string(); - //IP address + // IP address uint32_t ip_addr = uhd::htonx<uint32_t>(eeprom_in->ip_addr); ip_v4::bytes_type ip_addr_bytes; memcpy(&ip_addr_bytes, &ip_addr, 4); (*this)["ip-addr"] = ip_v4(ip_addr_bytes).to_string(); - //Default router + // Default router uint32_t dr_addr = uhd::htonx<uint32_t>(eeprom_in->dr_addr); ip_v4::bytes_type dr_addr_bytes; memcpy(&dr_addr_bytes, &dr_addr, 4); (*this)["gateway"] = ip_v4(dr_addr_bytes).to_string(); - //Netmask + // Netmask uint32_t netmask = uhd::htonx<uint32_t>(eeprom_in->netmask); ip_v4::bytes_type netmask_bytes; memcpy(&netmask_bytes, &netmask, 4); (*this)["netmask"] = ip_v4(netmask_bytes).to_string(); - //Serial + // Serial std::string raw_serial((char*)eeprom_in->serial, 10); byte_vector_t serial_bytes(raw_serial.begin(), raw_serial.end()); (*this)["serial"] = bytes_to_string(serial_bytes); - //Name + // Name std::string raw_name((char*)eeprom_in->name, 10); byte_vector_t name_bytes(raw_name.begin(), raw_name.end()); (*this)["name"] = bytes_to_string(name_bytes); - //Revision + // Revision (*this)["revision"] = std::to_string(int(eeprom_in->revision)); - } - else throw uhd::runtime_error("Error loading OctoClock EEPROM."); + } else + throw uhd::runtime_error("Error loading OctoClock EEPROM."); } -void octoclock_eeprom_t::_store() const { +void octoclock_eeprom_t::_store() const +{ uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<const octoclock_packet_t *>(octoclock_data); + const octoclock_packet_t* pkt_in = + reinterpret_cast<const octoclock_packet_t*>(octoclock_data); octoclock_packet_t pkt_out; // To avoid replicating sequence numbers between sessions pkt_out.sequence = uint32_t(std::rand()); - pkt_out.len = sizeof(octoclock_fw_eeprom_t); - size_t len = 0; + pkt_out.len = sizeof(octoclock_fw_eeprom_t); + size_t len = 0; - octoclock_fw_eeprom_t *eeprom_out = reinterpret_cast<octoclock_fw_eeprom_t *>(&pkt_out.data); + octoclock_fw_eeprom_t* eeprom_out = + reinterpret_cast<octoclock_fw_eeprom_t*>(&pkt_out.data); memset(eeprom_out, 0xFF, sizeof(octoclock_fw_eeprom_t)); - //MAC address - if((*this).has_key("mac-addr")){ - byte_copy(mac_addr_t::from_string((*this)["mac-addr"]).to_bytes(), eeprom_out->mac_addr); + // MAC address + if ((*this).has_key("mac-addr")) { + byte_copy(mac_addr_t::from_string((*this)["mac-addr"]).to_bytes(), + eeprom_out->mac_addr); } - //IP address - if((*this).has_key("ip-addr")){ - ip_v4::bytes_type ip_addr_bytes = ip_v4::from_string((*this)["ip-addr"]).to_bytes(); + // IP address + if ((*this).has_key("ip-addr")) { + ip_v4::bytes_type ip_addr_bytes = + ip_v4::from_string((*this)["ip-addr"]).to_bytes(); memcpy(&eeprom_out->ip_addr, &ip_addr_bytes, 4); eeprom_out->ip_addr = uhd::htonx<uint32_t>(eeprom_out->ip_addr); } - //Default router - if((*this).has_key("gateway")){ - ip_v4::bytes_type dr_addr_bytes = ip_v4::from_string((*this)["gateway"]).to_bytes(); + // Default router + if ((*this).has_key("gateway")) { + ip_v4::bytes_type dr_addr_bytes = + ip_v4::from_string((*this)["gateway"]).to_bytes(); memcpy(&eeprom_out->dr_addr, &dr_addr_bytes, 4); eeprom_out->dr_addr = uhd::htonx<uint32_t>(eeprom_out->dr_addr); } - //Netmask - if((*this).has_key("netmask")){ - ip_v4::bytes_type netmask_bytes = ip_v4::from_string((*this)["netmask"]).to_bytes(); + // Netmask + if ((*this).has_key("netmask")) { + ip_v4::bytes_type netmask_bytes = + ip_v4::from_string((*this)["netmask"]).to_bytes(); memcpy(&eeprom_out->netmask, &netmask_bytes, 4); eeprom_out->netmask = uhd::htonx<uint32_t>(eeprom_out->netmask); } - //Serial - if((*this).has_key("serial")){ - byte_copy(byte_vector_t((*this)["serial"].begin(), (*this)["serial"].end()), eeprom_out->serial); + // Serial + if ((*this).has_key("serial")) { + byte_copy(byte_vector_t((*this)["serial"].begin(), (*this)["serial"].end()), + eeprom_out->serial); } - //Name - if((*this).has_key("name")){ - byte_copy(byte_vector_t((*this)["name"].begin(), (*this)["name"].end()), eeprom_out->name); + // Name + if ((*this).has_key("name")) { + byte_copy(byte_vector_t((*this)["name"].begin(), (*this)["name"].end()), + eeprom_out->name); } - //Revision - if((*this).has_key("revision")){ - eeprom_out->revision = (*this)["revision"][0]-'0'; + // Revision + if ((*this).has_key("revision")) { + eeprom_out->revision = (*this)["revision"][0] - '0'; } - UHD_OCTOCLOCK_SEND_AND_RECV(xport, _proto_ver, BURN_EEPROM_CMD, pkt_out, len, octoclock_data); - if(not UHD_OCTOCLOCK_PACKET_MATCHES(BURN_EEPROM_SUCCESS_ACK, pkt_out, pkt_in, len)) + UHD_OCTOCLOCK_SEND_AND_RECV( + xport, _proto_ver, BURN_EEPROM_CMD, pkt_out, len, octoclock_data); + if (not UHD_OCTOCLOCK_PACKET_MATCHES(BURN_EEPROM_SUCCESS_ACK, pkt_out, pkt_in, len)) throw uhd::runtime_error("Error writing to OctoClock EEPROM."); } /*********************************************************************** * Implementation of OctoClock EEPROM **********************************************************************/ -octoclock_eeprom_t::octoclock_eeprom_t(void){ +octoclock_eeprom_t::octoclock_eeprom_t(void) +{ /* NOP */ } -octoclock_eeprom_t::octoclock_eeprom_t(udp_simple::sptr transport, uint32_t proto_ver) : - xport(transport), _proto_ver(proto_ver) +octoclock_eeprom_t::octoclock_eeprom_t(udp_simple::sptr transport, uint32_t proto_ver) + : xport(transport), _proto_ver(proto_ver) { _load(); } -void octoclock_eeprom_t::commit() const{ - if(!xport) throw uhd::runtime_error("There is no set device communication."); +void octoclock_eeprom_t::commit() const +{ + if (!xport) + throw uhd::runtime_error("There is no set device communication."); _store(); } diff --git a/host/lib/usrp_clock/octoclock/octoclock_image_loader.cpp b/host/lib/usrp_clock/octoclock/octoclock_image_loader.cpp index e030e6106..02a243b70 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_image_loader.cpp +++ b/host/lib/usrp_clock/octoclock/octoclock_image_loader.cpp @@ -5,9 +5,8 @@ // SPDX-License-Identifier: GPL-3.0-or-later // -#include "octoclock_impl.hpp" #include "common.h" - +#include "octoclock_impl.hpp" #include <uhd/device.hpp> #include <uhd/image_loader.hpp> #include <uhd/transport/udp_simple.hpp> @@ -17,61 +16,63 @@ #include <uhd/utils/paths.hpp> #include <uhd/utils/static.hpp> #include <uhdlib/utils/ihex.hpp> - +#include <stdint.h> #include <boost/filesystem.hpp> #include <boost/format.hpp> - #include <algorithm> -#include <iterator> +#include <chrono> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> +#include <iterator> #include <string> -#include <chrono> #include <thread> -#include <stdint.h> namespace fs = boost::filesystem; using namespace uhd; using namespace uhd::usrp_clock; using namespace uhd::transport; -#define OCTOCLOCK_FIRMWARE_MAX_SIZE_BYTES (1024*120) // Last 8 MB are for bootloader -#define OCTOCLOCK_BLOCK_SIZE 256 +#define OCTOCLOCK_FIRMWARE_MAX_SIZE_BYTES (1024 * 120) // Last 8 MB are for bootloader +#define OCTOCLOCK_BLOCK_SIZE 256 /* * OctoClock burn session */ -typedef struct { - bool found; - uhd::device_addr_t dev_addr; - std::string image_filepath; - uint16_t crc; - uint16_t num_blocks; - uint32_t sequence; - udp_simple::sptr ctrl_xport; - udp_simple::sptr fw_xport; - uint8_t data_in[udp_simple::mtu]; - uint32_t starting_firmware_version; +typedef struct +{ + bool found; + uhd::device_addr_t dev_addr; + std::string image_filepath; + uint16_t crc; + uint16_t num_blocks; + uint32_t sequence; + udp_simple::sptr ctrl_xport; + udp_simple::sptr fw_xport; + uint8_t data_in[udp_simple::mtu]; + uint32_t starting_firmware_version; std::vector<uint8_t> image; } octoclock_session_t; -static void octoclock_calculate_crc(octoclock_session_t &session){ +static void octoclock_calculate_crc(octoclock_session_t& session) +{ session.crc = 0xFFFF; - for(size_t i = 0; i < session.image.size(); i++) - { + for (size_t i = 0; i < session.image.size(); i++) { session.crc ^= session.image[i]; - for(uint8_t j = 0; j < 8; ++j){ - if(session.crc & 1) session.crc = (session.crc >> 1) ^ 0xA001; - else session.crc = (session.crc >> 1); + for (uint8_t j = 0; j < 8; ++j) { + if (session.crc & 1) + session.crc = (session.crc >> 1) ^ 0xA001; + else + session.crc = (session.crc >> 1); } } } -static void octoclock_read_bin(octoclock_session_t &session) +static void octoclock_read_bin(octoclock_session_t& session) { - std::ifstream bin_file(session.image_filepath.c_str(), std::ios::in | std::ios::binary); + std::ifstream bin_file( + session.image_filepath.c_str(), std::ios::in | std::ios::binary); if (not bin_file.is_open()) { throw uhd::io_error("Could not read image file."); } @@ -80,183 +81,197 @@ static void octoclock_read_bin(octoclock_session_t &session) session.image.clear(); session.image.resize(filesize); bin_file.read((char*)&session.image[0], filesize); - if(size_t(bin_file.gcount()) != filesize) { + if (size_t(bin_file.gcount()) != filesize) { throw uhd::io_error("Failed to read firmware image."); } bin_file.close(); } -static void octoclock_validate_firmware_image(octoclock_session_t &session){ - if(not fs::exists(session.image_filepath)){ +static void octoclock_validate_firmware_image(octoclock_session_t& session) +{ + if (not fs::exists(session.image_filepath)) { throw uhd::runtime_error(str(boost::format("Could not find image at path \"%s\"") % session.image_filepath)); } std::string extension = fs::extension(session.image_filepath); - if(extension == ".bin"){ + if (extension == ".bin") { octoclock_read_bin(session); - } - else if(extension == ".hex"){ + } else if (extension == ".hex") { ihex_reader hex_reader(session.image_filepath); session.image = hex_reader.to_vector(OCTOCLOCK_FIRMWARE_MAX_SIZE_BYTES); - } - else throw uhd::runtime_error(str(boost::format("Invalid extension \"%s\". Extension must be .hex or .bin.") - % extension)); - - if(session.image.size() > OCTOCLOCK_FIRMWARE_MAX_SIZE_BYTES){ - throw uhd::runtime_error(str(boost::format("The specified firmware image is too large: %d vs. %d") - % session.image.size() % OCTOCLOCK_FIRMWARE_MAX_SIZE_BYTES)); + } else + throw uhd::runtime_error( + str(boost::format("Invalid extension \"%s\". Extension must be .hex or .bin.") + % extension)); + + if (session.image.size() > OCTOCLOCK_FIRMWARE_MAX_SIZE_BYTES) { + throw uhd::runtime_error( + str(boost::format("The specified firmware image is too large: %d vs. %d") + % session.image.size() % OCTOCLOCK_FIRMWARE_MAX_SIZE_BYTES)); } session.num_blocks = (session.image.size() % OCTOCLOCK_BLOCK_SIZE) - ? ((session.image.size() / OCTOCLOCK_BLOCK_SIZE) + 1) - : (session.image.size() / OCTOCLOCK_BLOCK_SIZE); + ? ((session.image.size() / OCTOCLOCK_BLOCK_SIZE) + 1) + : (session.image.size() / OCTOCLOCK_BLOCK_SIZE); octoclock_calculate_crc(session); } -static void octoclock_setup_session(octoclock_session_t &session, - const uhd::device_addr_t &args, - const std::string &filepath){ - +static void octoclock_setup_session(octoclock_session_t& session, + const uhd::device_addr_t& args, + const std::string& filepath) +{ // See if we can find an OctoClock with the given args device_addrs_t devs = octoclock_find(args); - if(devs.size() == 0){ + if (devs.size() == 0) { session.found = false; return; - } - else if(devs.size() > 1){ - std::string err_msg = "Could not resolve given args to a single OctoClock device.\n" - "Applicable devices:\n"; - - for(const uhd::device_addr_t &dev: devs){ - std::string name = (dev["type"] == "octoclock") ? str(boost::format("OctoClock r%d") - % dev.get("revision","4")) - : "OctoClock Bootloader"; - err_msg += str(boost::format(" * %s (addr=%s)\n") - % name - % dev.get("addr")); + } else if (devs.size() > 1) { + std::string err_msg = + "Could not resolve given args to a single OctoClock device.\n" + "Applicable devices:\n"; + + for (const uhd::device_addr_t& dev : devs) { + std::string name = + (dev["type"] == "octoclock") + ? str(boost::format("OctoClock r%d") % dev.get("revision", "4")) + : "OctoClock Bootloader"; + err_msg += str(boost::format(" * %s (addr=%s)\n") % name % dev.get("addr")); } - err_msg += "\nSpecify one of these devices with the given args to load an image onto it."; + err_msg += "\nSpecify one of these devices with the given args to load an image " + "onto it."; throw uhd::runtime_error(err_msg); } session.dev_addr = devs[0]; - session.found = true; + session.found = true; // If no filepath is given, use the default - if(filepath == ""){ - session.image_filepath = find_image_path(str(boost::format("octoclock_r%s_fw.hex") - % session.dev_addr.get("revision","4") - )); - } - else session.image_filepath = filepath; + if (filepath == "") { + session.image_filepath = + find_image_path(str(boost::format("octoclock_r%s_fw.hex") + % session.dev_addr.get("revision", "4"))); + } else + session.image_filepath = filepath; octoclock_validate_firmware_image(session); - session.ctrl_xport = udp_simple::make_connected(session.dev_addr["addr"], - BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT)); - session.fw_xport = udp_simple::make_connected(session.dev_addr["addr"], - BOOST_STRINGIZE(OCTOCLOCK_UDP_FW_PORT)); + session.ctrl_xport = udp_simple::make_connected( + session.dev_addr["addr"], BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT)); + session.fw_xport = udp_simple::make_connected( + session.dev_addr["addr"], BOOST_STRINGIZE(OCTOCLOCK_UDP_FW_PORT)); // To avoid replicating sequence numbers between sessions - session.sequence = uint32_t(std::rand()); + session.sequence = uint32_t(std::rand()); // Query OctoClock again to get compat number octoclock_packet_t pkt_out; - const octoclock_packet_t* pkt_in = reinterpret_cast<const octoclock_packet_t*>(session.data_in); + const octoclock_packet_t* pkt_in = + reinterpret_cast<const octoclock_packet_t*>(session.data_in); size_t len = 0; - UHD_OCTOCLOCK_SEND_AND_RECV(session.ctrl_xport, OCTOCLOCK_FW_COMPAT_NUM, OCTOCLOCK_QUERY_CMD, pkt_out, len, session.data_in); - if(UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)){ + UHD_OCTOCLOCK_SEND_AND_RECV(session.ctrl_xport, + OCTOCLOCK_FW_COMPAT_NUM, + OCTOCLOCK_QUERY_CMD, + pkt_out, + len, + session.data_in); + if (UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)) { session.starting_firmware_version = uhd::htonx<uint32_t>(pkt_in->proto_ver); } else { throw uhd::runtime_error("Failed to communicate with OctoClock."); } } -static void octoclock_reset_into_bootloader(octoclock_session_t &session){ - +static void octoclock_reset_into_bootloader(octoclock_session_t& session) +{ // Already in bootloader - if(session.dev_addr["type"] == "octoclock-bootloader") + if (session.dev_addr["type"] == "octoclock-bootloader") return; // Force compat num to device's current, works around old firmware bug octoclock_packet_t pkt_out; - pkt_out.sequence = uhd::htonx<uint32_t>(++session.sequence); + pkt_out.sequence = uhd::htonx<uint32_t>(++session.sequence); pkt_out.proto_ver = uhd::htonx<uint32_t>(session.starting_firmware_version); - pkt_out.code = RESET_CMD; + pkt_out.code = RESET_CMD; std::cout << " -- Resetting into bootloader..." << std::flush; - session.ctrl_xport->send(boost::asio::buffer(&pkt_out, sizeof(octoclock_packet_t))); \ - size_t len = session.ctrl_xport->recv(boost::asio::buffer(session.data_in), 2);\ - const octoclock_packet_t* pkt_in = reinterpret_cast<const octoclock_packet_t*>(session.data_in); + session.ctrl_xport->send(boost::asio::buffer(&pkt_out, sizeof(octoclock_packet_t))); + size_t len = session.ctrl_xport->recv(boost::asio::buffer(session.data_in), 2); + const octoclock_packet_t* pkt_in = + reinterpret_cast<const octoclock_packet_t*>(session.data_in); - if(UHD_OCTOCLOCK_PACKET_MATCHES(RESET_ACK, pkt_out, pkt_in, len)){ + if (UHD_OCTOCLOCK_PACKET_MATCHES(RESET_ACK, pkt_out, pkt_in, len)) { // Make sure this device is now in its bootloader std::this_thread::sleep_for(std::chrono::milliseconds(500)); uhd::device_addrs_t octoclocks = uhd::device::find( - uhd::device_addr_t(str(boost::format("addr=%s") - % session.dev_addr["addr"] - ))); - if(octoclocks.size() == 0){ + uhd::device_addr_t(str(boost::format("addr=%s") % session.dev_addr["addr"]))); + if (octoclocks.size() == 0) { std::cout << "failed." << std::endl; throw uhd::runtime_error("Failed to reset OctoClock."); - } - else if(octoclocks[0]["type"] != "octoclock-bootloader"){ + } else if (octoclocks[0]["type"] != "octoclock-bootloader") { std::cout << "failed." << std::endl; throw uhd::runtime_error("Failed to reset OctoClock."); - } - else{ + } else { std::cout << "successful." << std::endl; session.dev_addr = octoclocks[0]; } - } - else{ + } else { std::cout << "failed." << std::endl; throw uhd::runtime_error("Failed to reset OctoClock."); } } -static void octoclock_burn(octoclock_session_t &session){ - +static void octoclock_burn(octoclock_session_t& session) +{ // Make sure we're in the bootloader for this octoclock_reset_into_bootloader(session); octoclock_packet_t pkt_out; pkt_out.sequence = uhd::htonx<uint32_t>(++session.sequence); - const octoclock_packet_t* pkt_in = reinterpret_cast<const octoclock_packet_t*>(session.data_in); + const octoclock_packet_t* pkt_in = + reinterpret_cast<const octoclock_packet_t*>(session.data_in); // Tell OctoClock to prepare for burn pkt_out.len = htonx<uint16_t>(session.image.size()); - size_t len = 0; + size_t len = 0; std::cout << " -- Preparing OctoClock for firmware load..." << std::flush; pkt_out.len = session.image.size(); pkt_out.crc = session.crc; - UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, OCTOCLOCK_FW_COMPAT_NUM, PREPARE_FW_BURN_CMD, pkt_out, len, session.data_in); - if(UHD_OCTOCLOCK_PACKET_MATCHES(FW_BURN_READY_ACK, pkt_out, pkt_in, len)){ + UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, + OCTOCLOCK_FW_COMPAT_NUM, + PREPARE_FW_BURN_CMD, + pkt_out, + len, + session.data_in); + if (UHD_OCTOCLOCK_PACKET_MATCHES(FW_BURN_READY_ACK, pkt_out, pkt_in, len)) { std::cout << "successful." << std::endl; - } - else{ + } else { std::cout << "failed." << std::endl; throw uhd::runtime_error("Failed to prepare OctoClock for firmware load."); } // Start burning - for(size_t i = 0; i < session.num_blocks; i++){ + for (size_t i = 0; i < session.num_blocks; i++) { pkt_out.sequence = uhd::htonx<uint32_t>(++session.sequence); - pkt_out.addr = i * OCTOCLOCK_BLOCK_SIZE; + pkt_out.addr = i * OCTOCLOCK_BLOCK_SIZE; std::cout << str(boost::format("\r -- Loading firmware: %d%% (%d/%d blocks)") - % int((double(i)/double(session.num_blocks))*100) - % i % session.num_blocks) + % int((double(i) / double(session.num_blocks)) * 100) % i + % session.num_blocks) << std::flush; memset(pkt_out.data, 0, OCTOCLOCK_BLOCK_SIZE); memcpy((char*)pkt_out.data, &session.image[pkt_out.addr], OCTOCLOCK_BLOCK_SIZE); - UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, OCTOCLOCK_FW_COMPAT_NUM, FILE_TRANSFER_CMD, pkt_out, len, session.data_in); - if(not UHD_OCTOCLOCK_PACKET_MATCHES(FILE_TRANSFER_ACK, pkt_out, pkt_in, len)){ + UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, + OCTOCLOCK_FW_COMPAT_NUM, + FILE_TRANSFER_CMD, + pkt_out, + len, + session.data_in); + if (not UHD_OCTOCLOCK_PACKET_MATCHES(FILE_TRANSFER_ACK, pkt_out, pkt_in, len)) { std::cout << std::endl; throw uhd::runtime_error("Failed to load firmware."); } @@ -267,36 +282,43 @@ static void octoclock_burn(octoclock_session_t &session){ << std::endl; } -static void octoclock_verify(octoclock_session_t &session){ - +static void octoclock_verify(octoclock_session_t& session) +{ octoclock_packet_t pkt_out; pkt_out.sequence = uhd::htonx<uint32_t>(++session.sequence); - const octoclock_packet_t* pkt_in = reinterpret_cast<const octoclock_packet_t*>(session.data_in); + const octoclock_packet_t* pkt_in = + reinterpret_cast<const octoclock_packet_t*>(session.data_in); size_t len = 0; uint8_t image_part[OCTOCLOCK_BLOCK_SIZE]; uint16_t cmp_len = 0; - for(size_t i = 0; i < session.num_blocks; i++){ + for (size_t i = 0; i < session.num_blocks; i++) { pkt_out.sequence = uhd::htonx<uint32_t>(++session.sequence); - pkt_out.addr = i * OCTOCLOCK_BLOCK_SIZE; + pkt_out.addr = i * OCTOCLOCK_BLOCK_SIZE; - std::cout << str(boost::format("\r -- Verifying firmware load: %d%% (%d/%d blocks)") - % int((double(i)/double(session.num_blocks))*100) - % i % session.num_blocks) + std::cout << str(boost::format( + "\r -- Verifying firmware load: %d%% (%d/%d blocks)") + % int((double(i) / double(session.num_blocks)) * 100) % i + % session.num_blocks) << std::flush; memset(image_part, 0, OCTOCLOCK_BLOCK_SIZE); memcpy((char*)image_part, &session.image[pkt_out.addr], OCTOCLOCK_BLOCK_SIZE); - cmp_len = std::min<size_t>(OCTOCLOCK_BLOCK_SIZE, session.image.size() - size_t(pkt_out.addr)); - - UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, OCTOCLOCK_FW_COMPAT_NUM, READ_FW_CMD, pkt_out, len, session.data_in); - if(UHD_OCTOCLOCK_PACKET_MATCHES(READ_FW_ACK, pkt_out, pkt_in, len)){ - if(memcmp(pkt_in->data, image_part, cmp_len)){ + cmp_len = std::min<size_t>( + OCTOCLOCK_BLOCK_SIZE, session.image.size() - size_t(pkt_out.addr)); + + UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, + OCTOCLOCK_FW_COMPAT_NUM, + READ_FW_CMD, + pkt_out, + len, + session.data_in); + if (UHD_OCTOCLOCK_PACKET_MATCHES(READ_FW_ACK, pkt_out, pkt_in, len)) { + if (memcmp(pkt_in->data, image_part, cmp_len)) { std::cout << std::endl; throw uhd::runtime_error("Failed to verify OctoClock firmware."); } - } - else{ + } else { std::cout << std::endl; throw uhd::runtime_error("Failed to verify OctoClock firmware."); } @@ -307,34 +329,38 @@ static void octoclock_verify(octoclock_session_t &session){ << std::endl; } -static void octoclock_finalize(octoclock_session_t &session){ - +static void octoclock_finalize(octoclock_session_t& session) +{ octoclock_packet_t pkt_out; pkt_out.sequence = uhd::htonx<uint32_t>(++session.sequence); - const octoclock_packet_t* pkt_in = reinterpret_cast<const octoclock_packet_t*>(session.data_in); + const octoclock_packet_t* pkt_in = + reinterpret_cast<const octoclock_packet_t*>(session.data_in); size_t len = 0; std::cout << " -- Finalizing firmware load..." << std::flush; - UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, OCTOCLOCK_FW_COMPAT_NUM, FINALIZE_BURNING_CMD, pkt_out, len, session.data_in); - if(UHD_OCTOCLOCK_PACKET_MATCHES(FINALIZE_BURNING_ACK, pkt_out, pkt_in, len)){ + UHD_OCTOCLOCK_SEND_AND_RECV(session.fw_xport, + OCTOCLOCK_FW_COMPAT_NUM, + FINALIZE_BURNING_CMD, + pkt_out, + len, + session.data_in); + if (UHD_OCTOCLOCK_PACKET_MATCHES(FINALIZE_BURNING_ACK, pkt_out, pkt_in, len)) { std::cout << "successful." << std::endl; - } - else{ + } else { std::cout << "failed." << std::endl; throw uhd::runtime_error("Failed to finalize OctoClock firmware load."); } } -bool octoclock_image_loader(const image_loader::image_loader_args_t &image_loader_args){ +bool octoclock_image_loader(const image_loader::image_loader_args_t& image_loader_args) +{ octoclock_session_t session; - octoclock_setup_session(session, - image_loader_args.args, - image_loader_args.firmware_path - ); - if(!session.found or !image_loader_args.load_firmware) return false; - - std::cout << boost::format("Unit: OctoClock (%s)") - % session.dev_addr["addr"] + octoclock_setup_session( + session, image_loader_args.args, image_loader_args.firmware_path); + if (!session.found or !image_loader_args.load_firmware) + return false; + + std::cout << boost::format("Unit: OctoClock (%s)") % session.dev_addr["addr"] << std::endl; std::cout << "Firmware: " << session.image_filepath << std::endl; @@ -345,14 +371,15 @@ bool octoclock_image_loader(const image_loader::image_loader_args_t &image_loade return true; } -UHD_STATIC_BLOCK(register_octoclock_image_loader){ - std::string recovery_instructions = "Aborting. Your OctoClock firmware is now corrupt. The bootloader\n" - "is functional, but the device will not have functional clock distribution.\n" - "Run this utility again to restore functionality or refer to:\n\n" - "http://files.ettus.com/manual/page_octoclock.html\n\n" - "for alternative setups."; - - image_loader::register_image_loader("octoclock", - octoclock_image_loader, - recovery_instructions); +UHD_STATIC_BLOCK(register_octoclock_image_loader) +{ + std::string recovery_instructions = + "Aborting. Your OctoClock firmware is now corrupt. The bootloader\n" + "is functional, but the device will not have functional clock distribution.\n" + "Run this utility again to restore functionality or refer to:\n\n" + "http://files.ettus.com/manual/page_octoclock.html\n\n" + "for alternative setups."; + + image_loader::register_image_loader( + "octoclock", octoclock_image_loader, recovery_instructions); } diff --git a/host/lib/usrp_clock/octoclock/octoclock_impl.cpp b/host/lib/usrp_clock/octoclock/octoclock_impl.cpp index a1891c7bd..b7ebc3473 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_impl.cpp +++ b/host/lib/usrp_clock/octoclock/octoclock_impl.cpp @@ -24,154 +24,168 @@ #include <boost/assign.hpp> #include <boost/filesystem.hpp> #include <boost/format.hpp> +#include <boost/thread.hpp> using namespace uhd; using namespace uhd::usrp_clock; using namespace uhd::transport; namespace asio = boost::asio; -namespace fs = boost::filesystem; +namespace fs = boost::filesystem; /*********************************************************************** * Discovery **********************************************************************/ -device_addrs_t octoclock_find(const device_addr_t &hint){ - //Handle the multi-device discovery +device_addrs_t octoclock_find(const device_addr_t& hint) +{ + // Handle the multi-device discovery device_addrs_t hints = separate_device_addr(hint); - if (hints.size() > 1){ + if (hints.size() > 1) { device_addrs_t found_devices; std::string error_msg; - for(const device_addr_t &hint_i: hints){ + for (const device_addr_t& hint_i : hints) { device_addrs_t found_devices_i = octoclock_find(hint_i); - if (found_devices_i.size() != 1) error_msg += str(boost::format( - "Could not resolve device hint \"%s\" to a single device." - ) % hint_i.to_string()); - else found_devices.push_back(found_devices_i[0]); + if (found_devices_i.size() != 1) + error_msg += + str(boost::format( + "Could not resolve device hint \"%s\" to a single device.") + % hint_i.to_string()); + else + found_devices.push_back(found_devices_i[0]); } - if (found_devices.empty()) return device_addrs_t(); - if (not error_msg.empty()) throw uhd::value_error(error_msg); + if (found_devices.empty()) + return device_addrs_t(); + if (not error_msg.empty()) + throw uhd::value_error(error_msg); return device_addrs_t(1, combine_device_addrs(found_devices)); } - //Initialize the hint for a single device case + // Initialize the hint for a single device case UHD_ASSERT_THROW(hints.size() <= 1); - hints.resize(1); //In case it was empty + hints.resize(1); // In case it was empty device_addr_t _hint = hints[0]; device_addrs_t octoclock_addrs; - //return an empty list of addresses when type is set to non-OctoClock - if (hint.has_key("type") and hint["type"].find("octoclock") == std::string::npos) return octoclock_addrs; + // return an empty list of addresses when type is set to non-OctoClock + if (hint.has_key("type") and hint["type"].find("octoclock") == std::string::npos) + return octoclock_addrs; - //Return an empty list of addresses when a resource is specified, - //since a resource is intended for a different, non-USB, device. - if (hint.has_key("resource")) return octoclock_addrs; + // Return an empty list of addresses when a resource is specified, + // since a resource is intended for a different, non-USB, device. + if (hint.has_key("resource")) + return octoclock_addrs; - //If no address was specified, send a broadcast on each interface - if (not _hint.has_key("addr")){ - for(const if_addrs_t &if_addrs: get_if_addrs()){ - //avoid the loopback device - if (if_addrs.inet == asio::ip::address_v4::loopback().to_string()) continue; + // If no address was specified, send a broadcast on each interface + if (not _hint.has_key("addr")) { + for (const if_addrs_t& if_addrs : get_if_addrs()) { + // avoid the loopback device + if (if_addrs.inet == asio::ip::address_v4::loopback().to_string()) + continue; - //create a new hint with this broadcast address + // create a new hint with this broadcast address device_addr_t new_hint = hint; - new_hint["addr"] = if_addrs.bcast; + new_hint["addr"] = if_addrs.bcast; - //call discover with the new hint and append results + // call discover with the new hint and append results device_addrs_t new_octoclock_addrs = octoclock_find(new_hint); octoclock_addrs.insert(octoclock_addrs.begin(), - new_octoclock_addrs.begin(), new_octoclock_addrs.end() - ); + new_octoclock_addrs.begin(), + new_octoclock_addrs.end()); } return octoclock_addrs; } - //Create a UDP transport to communicate + // Create a UDP transport to communicate udp_simple::sptr udp_transport = udp_simple::make_broadcast( - _hint["addr"], - BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT) - ); + _hint["addr"], BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT)); - //Send a query packet + // Send a query packet octoclock_packet_t pkt_out; pkt_out.proto_ver = OCTOCLOCK_FW_COMPAT_NUM; // To avoid replicating sequence numbers between sessions pkt_out.sequence = uint32_t(std::rand()); - pkt_out.len = 0; - pkt_out.code = OCTOCLOCK_QUERY_CMD; - try{ + pkt_out.len = 0; + pkt_out.code = OCTOCLOCK_QUERY_CMD; + try { udp_transport->send(boost::asio::buffer(&pkt_out, sizeof(pkt_out))); - } - catch(const std::exception &ex){ - UHD_LOGGER_ERROR("OCTOCLOCK") << "OctoClock network discovery error - " << ex.what() ; - } - catch(...){ - UHD_LOGGER_ERROR("OCTOCLOCK") << "OctoClock network discovery unknown error" ; + } catch (const std::exception& ex) { + UHD_LOGGER_ERROR("OCTOCLOCK") + << "OctoClock network discovery error - " << ex.what(); + } catch (...) { + UHD_LOGGER_ERROR("OCTOCLOCK") << "OctoClock network discovery unknown error"; } uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<octoclock_packet_t*>(octoclock_data); + const octoclock_packet_t* pkt_in = + reinterpret_cast<octoclock_packet_t*>(octoclock_data); - while(true){ + while (true) { size_t len = udp_transport->recv(asio::buffer(octoclock_data)); - if(UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)){ + if (UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)) { device_addr_t new_addr; new_addr["addr"] = udp_transport->get_recv_addr(); - //Attempt direct communication with OctoClock + // Attempt direct communication with OctoClock udp_simple::sptr ctrl_xport = udp_simple::make_connected( - new_addr["addr"], - BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT) - ); - UHD_OCTOCLOCK_SEND_AND_RECV(ctrl_xport, OCTOCLOCK_FW_COMPAT_NUM, OCTOCLOCK_QUERY_CMD, pkt_out, len, octoclock_data); - if(UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)){ - //If the OctoClock is in its bootloader, don't ask for details - if(pkt_in->proto_ver == OCTOCLOCK_BOOTLOADER_PROTO_VER){ + new_addr["addr"], BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT)); + UHD_OCTOCLOCK_SEND_AND_RECV(ctrl_xport, + OCTOCLOCK_FW_COMPAT_NUM, + OCTOCLOCK_QUERY_CMD, + pkt_out, + len, + octoclock_data); + if (UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)) { + // If the OctoClock is in its bootloader, don't ask for details + if (pkt_in->proto_ver == OCTOCLOCK_BOOTLOADER_PROTO_VER) { new_addr["type"] = "octoclock-bootloader"; octoclock_addrs.push_back(new_addr); - } - else { + } else { new_addr["type"] = "octoclock"; - if(pkt_in->proto_ver >= OCTOCLOCK_FW_MIN_COMPAT_NUM and pkt_in->proto_ver <= OCTOCLOCK_FW_COMPAT_NUM) { + if (pkt_in->proto_ver >= OCTOCLOCK_FW_MIN_COMPAT_NUM + and pkt_in->proto_ver <= OCTOCLOCK_FW_COMPAT_NUM) { octoclock_eeprom_t oc_eeprom(ctrl_xport, pkt_in->proto_ver); - new_addr["name"] = oc_eeprom["name"]; + new_addr["name"] = oc_eeprom["name"]; new_addr["serial"] = oc_eeprom["serial"]; } else { - new_addr["name"] = ""; + new_addr["name"] = ""; new_addr["serial"] = ""; } - //Filter based on optional keys (if any) - if( - (not _hint.has_key("name") or (_hint["name"] == new_addr["name"])) and - (not _hint.has_key("serial") or (_hint["serial"] == new_addr["serial"])) - ){ + // Filter based on optional keys (if any) + if ((not _hint.has_key("name") or (_hint["name"] == new_addr["name"])) + and (not _hint.has_key("serial") + or (_hint["serial"] == new_addr["serial"]))) { octoclock_addrs.push_back(new_addr); } } - } - else continue; + } else + continue; } - if(len == 0) break; + if (len == 0) + break; } return octoclock_addrs; } -device::sptr octoclock_make(const device_addr_t &device_addr){ +device::sptr octoclock_make(const device_addr_t& device_addr) +{ return device::sptr(new octoclock_impl(device_addr)); } -UHD_STATIC_BLOCK(register_octoclock_device){ +UHD_STATIC_BLOCK(register_octoclock_device) +{ device::register_device(&octoclock_find, &octoclock_make, device::CLOCK); } /*********************************************************************** * Structors **********************************************************************/ -octoclock_impl::octoclock_impl(const device_addr_t &_device_addr){ +octoclock_impl::octoclock_impl(const device_addr_t& _device_addr) +{ UHD_LOGGER_INFO("OCTOCLOCK") << "Opening an OctoClock device..."; - _type = device::CLOCK; + _type = device::CLOCK; device_addrs_t device_args = separate_device_addr(_device_addr); // To avoid replicating sequence numbers between sessions _sequence = uint32_t(std::rand()); @@ -182,17 +196,19 @@ octoclock_impl::octoclock_impl(const device_addr_t &_device_addr){ _tree = property_tree::make(); _tree->create<std::string>("/name").set("OctoClock Device"); - for(size_t oci = 0; oci < device_args.size(); oci++){ + for (size_t oci = 0; oci < device_args.size(); oci++) { const device_addr_t device_args_i = device_args[oci]; - const std::string addr = device_args_i["addr"]; - //Can't make a device out of an OctoClock in bootloader state - if(device_args_i["type"] == "octoclock-bootloader"){ - throw uhd::runtime_error(str(boost::format( - "\n\nThis device is in its bootloader state and cannot be used by UHD.\n" - "This may mean the firmware on the device has been corrupted and will\n" - "need to be burned again.\n\n" - "%s\n" - ) % _get_images_help_message(addr))); + const std::string addr = device_args_i["addr"]; + // Can't make a device out of an OctoClock in bootloader state + if (device_args_i["type"] == "octoclock-bootloader") { + throw uhd::runtime_error( + str(boost::format("\n\nThis device is in its bootloader state and cannot " + "be used by UHD.\n" + "This may mean the firmware on the device has been " + "corrupted and will\n" + "need to be burned again.\n\n" + "%s\n") + % _get_images_help_message(addr))); } const std::string oc = std::to_string(oci); @@ -200,8 +216,10 @@ octoclock_impl::octoclock_impl(const device_addr_t &_device_addr){ //////////////////////////////////////////////////////////////////// // Set up UDP transports //////////////////////////////////////////////////////////////////// - _oc_dict[oc].ctrl_xport = udp_simple::make_connected(addr, BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT)); - _oc_dict[oc].gpsdo_xport = udp_simple::make_connected(addr, BOOST_STRINGIZE(OCTOCLOCK_UDP_GPSDO_PORT)); + _oc_dict[oc].ctrl_xport = + udp_simple::make_connected(addr, BOOST_STRINGIZE(OCTOCLOCK_UDP_CTRL_PORT)); + _oc_dict[oc].gpsdo_xport = + udp_simple::make_connected(addr, BOOST_STRINGIZE(OCTOCLOCK_UDP_GPSDO_PORT)); const fs_path oc_path = "/mboards/" + oc; _tree->create<std::string>(oc_path / "name").set("OctoClock"); @@ -210,15 +228,19 @@ octoclock_impl::octoclock_impl(const device_addr_t &_device_addr){ // Check the firmware compatibility number //////////////////////////////////////////////////////////////////// _proto_ver = _get_fw_version(oc); - if(_proto_ver < OCTOCLOCK_FW_MIN_COMPAT_NUM or _proto_ver > OCTOCLOCK_FW_COMPAT_NUM){ - throw uhd::runtime_error(str(boost::format( + if (_proto_ver < OCTOCLOCK_FW_MIN_COMPAT_NUM + or _proto_ver > OCTOCLOCK_FW_COMPAT_NUM) { + throw uhd::runtime_error(str( + boost::format( "\n\nPlease update your OctoClock's firmware.\n" "Expected firmware compatibility number %d, but got %d:\n" "The firmware build is not compatible with the host code build.\n\n" - "%s\n" - ) % int(OCTOCLOCK_FW_COMPAT_NUM) % int(_proto_ver) % _get_images_help_message(addr))); + "%s\n") + % int(OCTOCLOCK_FW_COMPAT_NUM) % int(_proto_ver) + % _get_images_help_message(addr))); } - _tree->create<std::string>(oc_path / "fw_version").set(std::to_string(int(_proto_ver))); + _tree->create<std::string>(oc_path / "fw_version") + .set(std::to_string(int(_proto_ver))); //////////////////////////////////////////////////////////////////// // Set up EEPROM @@ -226,7 +248,8 @@ octoclock_impl::octoclock_impl(const device_addr_t &_device_addr){ _oc_dict[oc].eeprom = octoclock_eeprom_t(_oc_dict[oc].ctrl_xport, _proto_ver); _tree->create<octoclock_eeprom_t>(oc_path / "eeprom") .set(_oc_dict[oc].eeprom) - .add_coerced_subscriber(std::bind(&octoclock_impl::_set_eeprom, this, oc, std::placeholders::_1)); + .add_coerced_subscriber( + std::bind(&octoclock_impl::_set_eeprom, this, oc, std::placeholders::_1)); //////////////////////////////////////////////////////////////////// // Initialize non-GPSDO sensors @@ -247,178 +270,215 @@ octoclock_impl::octoclock_impl(const device_addr_t &_device_addr){ //////////////////////////////////////////////////////////////////// std::string asterisk = (device_args.size() > 1) ? " * " : ""; - if(device_args.size() > 1){ + if (device_args.size() > 1) { UHD_LOGGER_INFO("OCTOCLOCK") << "Checking status of " << addr; } - UHD_LOGGER_INFO("OCTOCLOCK") << boost::format("%sDetecting internal GPSDO...") % asterisk; + UHD_LOGGER_INFO("OCTOCLOCK") + << boost::format("%sDetecting internal GPSDO...") % asterisk; _get_state(oc); - if(_oc_dict[oc].state.gps_detected){ - try{ - _oc_dict[oc].gps = gps_ctrl::make(octoclock_make_uart_iface(_oc_dict[oc].gpsdo_xport, _proto_ver)); + if (_oc_dict[oc].state.gps_detected) { + try { + _oc_dict[oc].gps = gps_ctrl::make( + octoclock_make_uart_iface(_oc_dict[oc].gpsdo_xport, _proto_ver)); - if(_oc_dict[oc].gps and _oc_dict[oc].gps->gps_detected()){ - for(const std::string &name: _oc_dict[oc].gps->get_sensors()){ + if (_oc_dict[oc].gps and _oc_dict[oc].gps->gps_detected()) { + for (const std::string& name : _oc_dict[oc].gps->get_sensors()) { _tree->create<sensor_value_t>(oc_path / "sensors" / name) - .set_publisher(std::bind(&gps_ctrl::get_sensor, _oc_dict[oc].gps, name)); + .set_publisher( + std::bind(&gps_ctrl::get_sensor, _oc_dict[oc].gps, name)); } - } - else{ - //If GPSDO communication failed, set gps_detected to false + } else { + // If GPSDO communication failed, set gps_detected to false _oc_dict[oc].state.gps_detected = 0; - UHD_LOGGER_WARNING("OCTOCLOCK") << "Device reports that it has a GPSDO, but we cannot communicate with it."; + UHD_LOGGER_WARNING("OCTOCLOCK") + << "Device reports that it has a GPSDO, but we cannot " + "communicate with it."; } + } catch (std::exception& e) { + UHD_LOGGER_ERROR("OCTOCLOCK") + << "An error occurred making GPSDO control: " << e.what(); } - catch(std::exception &e){ - UHD_LOGGER_ERROR("OCTOCLOCK") << "An error occurred making GPSDO control: " << e.what(); - } - } - else UHD_LOGGER_INFO("OCTOCLOCK") << "No GPSDO found"; - UHD_LOGGER_INFO("OCTOCLOCK") << boost::format("%sDetecting external reference...%s") % asterisk - % _ext_ref_detected(oc).value; - UHD_LOGGER_INFO("OCTOCLOCK") << boost::format("%sDetecting switch position...%s") % asterisk - % _switch_pos(oc).value; + } else + UHD_LOGGER_INFO("OCTOCLOCK") << "No GPSDO found"; + UHD_LOGGER_INFO("OCTOCLOCK") + << boost::format("%sDetecting external reference...%s") % asterisk + % _ext_ref_detected(oc).value; + UHD_LOGGER_INFO("OCTOCLOCK") << boost::format("%sDetecting switch position...%s") + % asterisk % _switch_pos(oc).value; std::string ref = _which_ref(oc).value; - if(ref == "none") UHD_LOGGER_INFO("OCTOCLOCK") << boost::format("%sDevice is not using any reference") % asterisk; - else UHD_LOGGER_INFO("OCTOCLOCK") << boost::format("%sDevice is using %s reference") % asterisk - % _which_ref(oc).value - ; + if (ref == "none") + UHD_LOGGER_INFO("OCTOCLOCK") + << boost::format("%sDevice is not using any reference") % asterisk; + else + UHD_LOGGER_INFO("OCTOCLOCK") + << boost::format("%sDevice is using %s reference") % asterisk + % _which_ref(oc).value; } } -rx_streamer::sptr octoclock_impl::get_rx_stream(UHD_UNUSED(const stream_args_t &args)){ +rx_streamer::sptr octoclock_impl::get_rx_stream(UHD_UNUSED(const stream_args_t& args)) +{ throw uhd::not_implemented_error("This function is incompatible with this device."); } -tx_streamer::sptr octoclock_impl::get_tx_stream(UHD_UNUSED(const stream_args_t &args)){ +tx_streamer::sptr octoclock_impl::get_tx_stream(UHD_UNUSED(const stream_args_t& args)) +{ throw uhd::not_implemented_error("This function is incompatible with this device."); } -bool octoclock_impl::recv_async_msg(UHD_UNUSED(uhd::async_metadata_t&), UHD_UNUSED(double)){ +bool octoclock_impl::recv_async_msg( + UHD_UNUSED(uhd::async_metadata_t&), UHD_UNUSED(double)) +{ throw uhd::not_implemented_error("This function is incompatible with this device."); } -void octoclock_impl::_set_eeprom(const std::string &oc, const octoclock_eeprom_t &oc_eeprom){ +void octoclock_impl::_set_eeprom( + const std::string& oc, const octoclock_eeprom_t& oc_eeprom) +{ /* * The OctoClock needs a full octoclock_eeprom_t so as to not erase * what it currently has in the EEPROM, so store the relevant values * from the user's input and send that instead. */ - for(const std::string &key: oc_eeprom.keys()){ - if(_oc_dict[oc].eeprom.has_key(key)) _oc_dict[oc].eeprom[key] = oc_eeprom[key]; + for (const std::string& key : oc_eeprom.keys()) { + if (_oc_dict[oc].eeprom.has_key(key)) + _oc_dict[oc].eeprom[key] = oc_eeprom[key]; } _oc_dict[oc].eeprom.commit(); } -uint32_t octoclock_impl::_get_fw_version(const std::string &oc){ +uint32_t octoclock_impl::_get_fw_version(const std::string& oc) +{ octoclock_packet_t pkt_out; pkt_out.sequence = uhd::htonx<uint32_t>(++_sequence); - pkt_out.len = 0; + pkt_out.len = 0; size_t len; uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<octoclock_packet_t*>(octoclock_data); - - UHD_OCTOCLOCK_SEND_AND_RECV(_oc_dict[oc].ctrl_xport, OCTOCLOCK_FW_COMPAT_NUM, OCTOCLOCK_QUERY_CMD, pkt_out, len, octoclock_data); - if(UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)){ + const octoclock_packet_t* pkt_in = + reinterpret_cast<octoclock_packet_t*>(octoclock_data); + + UHD_OCTOCLOCK_SEND_AND_RECV(_oc_dict[oc].ctrl_xport, + OCTOCLOCK_FW_COMPAT_NUM, + OCTOCLOCK_QUERY_CMD, + pkt_out, + len, + octoclock_data); + if (UHD_OCTOCLOCK_PACKET_MATCHES(OCTOCLOCK_QUERY_ACK, pkt_out, pkt_in, len)) { return pkt_in->proto_ver; - } - else throw uhd::runtime_error("Failed to retrieve firmware version from OctoClock."); + } else + throw uhd::runtime_error("Failed to retrieve firmware version from OctoClock."); } -void octoclock_impl::_get_state(const std::string &oc){ +void octoclock_impl::_get_state(const std::string& oc) +{ octoclock_packet_t pkt_out; pkt_out.sequence = uhd::htonx<uint32_t>(++_sequence); - pkt_out.len = 0; - size_t len = 0; + pkt_out.len = 0; + size_t len = 0; uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<octoclock_packet_t*>(octoclock_data); - - UHD_OCTOCLOCK_SEND_AND_RECV(_oc_dict[oc].ctrl_xport, _proto_ver, SEND_STATE_CMD, pkt_out, len, octoclock_data); - if(UHD_OCTOCLOCK_PACKET_MATCHES(SEND_STATE_ACK, pkt_out, pkt_in, len)){ - const octoclock_state_t *state = reinterpret_cast<const octoclock_state_t*>(pkt_in->data); + const octoclock_packet_t* pkt_in = + reinterpret_cast<octoclock_packet_t*>(octoclock_data); + + UHD_OCTOCLOCK_SEND_AND_RECV(_oc_dict[oc].ctrl_xport, + _proto_ver, + SEND_STATE_CMD, + pkt_out, + len, + octoclock_data); + if (UHD_OCTOCLOCK_PACKET_MATCHES(SEND_STATE_ACK, pkt_out, pkt_in, len)) { + const octoclock_state_t* state = + reinterpret_cast<const octoclock_state_t*>(pkt_in->data); _oc_dict[oc].state = *state; - } - else throw uhd::runtime_error("Failed to retrieve state information from OctoClock."); + } else + throw uhd::runtime_error("Failed to retrieve state information from OctoClock."); } -uhd::dict<ref_t, std::string> _ref_strings = boost::assign::map_list_of - (NO_REF, "none") - (INTERNAL, "internal") - (EXTERNAL, "external") -; +uhd::dict<ref_t, std::string> _ref_strings = boost::assign::map_list_of(NO_REF, "none")( + INTERNAL, "internal")(EXTERNAL, "external"); -uhd::dict<switch_pos_t, std::string> _switch_pos_strings = boost::assign::map_list_of - (PREFER_INTERNAL, "Prefer internal") - (PREFER_EXTERNAL, "Prefer external") -; +uhd::dict<switch_pos_t, std::string> _switch_pos_strings = boost::assign::map_list_of( + PREFER_INTERNAL, "Prefer internal")(PREFER_EXTERNAL, "Prefer external"); -sensor_value_t octoclock_impl::_ext_ref_detected(const std::string &oc){ +sensor_value_t octoclock_impl::_ext_ref_detected(const std::string& oc) +{ _get_state(oc); - return sensor_value_t("External reference detected", (_oc_dict[oc].state.external_detected > 0), - "true", "false"); + return sensor_value_t("External reference detected", + (_oc_dict[oc].state.external_detected > 0), + "true", + "false"); } -sensor_value_t octoclock_impl::_gps_detected(const std::string &oc){ - //Don't check, this shouldn't change once device is turned on +sensor_value_t octoclock_impl::_gps_detected(const std::string& oc) +{ + // Don't check, this shouldn't change once device is turned on - return sensor_value_t("GPSDO detected", (_oc_dict[oc].state.gps_detected > 0), - "true", "false"); + return sensor_value_t( + "GPSDO detected", (_oc_dict[oc].state.gps_detected > 0), "true", "false"); } -sensor_value_t octoclock_impl::_which_ref(const std::string &oc){ +sensor_value_t octoclock_impl::_which_ref(const std::string& oc) +{ _get_state(oc); - if(not _ref_strings.has_key(ref_t(_oc_dict[oc].state.which_ref))){ + if (not _ref_strings.has_key(ref_t(_oc_dict[oc].state.which_ref))) { throw uhd::runtime_error("Invalid reference detected."); } - return sensor_value_t("Using reference", _ref_strings[ref_t(_oc_dict[oc].state.which_ref)], ""); + return sensor_value_t( + "Using reference", _ref_strings[ref_t(_oc_dict[oc].state.which_ref)], ""); } -sensor_value_t octoclock_impl::_switch_pos(const std::string &oc){ +sensor_value_t octoclock_impl::_switch_pos(const std::string& oc) +{ _get_state(oc); - if(not _switch_pos_strings.has_key(switch_pos_t(_oc_dict[oc].state.switch_pos))){ + if (not _switch_pos_strings.has_key(switch_pos_t(_oc_dict[oc].state.switch_pos))) { throw uhd::runtime_error("Invalid switch position detected."); } - return sensor_value_t("Switch position", _switch_pos_strings[switch_pos_t(_oc_dict[oc].state.switch_pos)], ""); + return sensor_value_t("Switch position", + _switch_pos_strings[switch_pos_t(_oc_dict[oc].state.switch_pos)], + ""); } -uint32_t octoclock_impl::_get_time(const std::string &oc){ - if(_oc_dict[oc].state.gps_detected){ +uint32_t octoclock_impl::_get_time(const std::string& oc) +{ + if (_oc_dict[oc].state.gps_detected) { std::string time_str = _oc_dict[oc].gps->get_sensor("gps_time").value; return boost::lexical_cast<uint32_t>(time_str); - } - else throw uhd::runtime_error("This device cannot return a time."); + } else + throw uhd::runtime_error("This device cannot return a time."); } -std::string octoclock_impl::_get_images_help_message(const std::string &addr){ +std::string octoclock_impl::_get_images_help_message(const std::string& addr) +{ const std::string image_name = "octoclock_r4_fw.hex"; - //Check to see if image is in default location + // Check to see if image is in default location std::string image_location; - try{ + try { image_location = uhd::find_image_path(image_name); - } - catch(const std::exception &){ + } catch (const std::exception&) { return str(boost::format("Could not find %s in your images path.\n%s") - % image_name - % uhd::print_utility_error("uhd_images_downloader.py")); + % image_name % uhd::print_utility_error("uhd_images_downloader.py")); } - //Get escape character - #ifdef UHD_PLATFORM_WIN32 +// Get escape character +#ifdef UHD_PLATFORM_WIN32 const std::string ml = "^\n "; - #else +#else const std::string ml = "\\\n "; - #endif - - //Get burner command - const std::string burner_path = (fs::path(uhd::get_pkg_path()) / "bin" / "uhd_image_loader").string(); - const std::string burner_cmd = str(boost::format("%s %s--addr=\"%s\"") % burner_path % ml % addr); - return str(boost::format("%s\n%s") % uhd::print_utility_error("uhd_images_downloader.py") % burner_cmd); +#endif + + // Get burner command + const std::string burner_path = + (fs::path(uhd::get_pkg_path()) / "bin" / "uhd_image_loader").string(); + const std::string burner_cmd = + str(boost::format("%s %s--addr=\"%s\"") % burner_path % ml % addr); + return str(boost::format("%s\n%s") + % uhd::print_utility_error("uhd_images_downloader.py") % burner_cmd); } diff --git a/host/lib/usrp_clock/octoclock/octoclock_impl.hpp b/host/lib/usrp_clock/octoclock/octoclock_impl.hpp index 8fcda1999..d293ce3e5 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_impl.hpp +++ b/host/lib/usrp_clock/octoclock/octoclock_impl.hpp @@ -8,37 +8,37 @@ #ifndef INCLUDED_OCTOCLOCK_IMPL_HPP #define INCLUDED_OCTOCLOCK_IMPL_HPP -#include <memory> -#include <boost/thread.hpp> - +#include "common.h" #include <uhd/device.hpp> #include <uhd/stream.hpp> -#include <uhd/usrp/gps_ctrl.hpp> -#include <uhd/usrp_clock/octoclock_eeprom.hpp> #include <uhd/types/device_addr.hpp> #include <uhd/types/dict.hpp> #include <uhd/types/sensors.hpp> +#include <uhd/usrp/gps_ctrl.hpp> +#include <uhd/usrp_clock/octoclock_eeprom.hpp> +#include <boost/thread.hpp> +#include <memory> -#include "common.h" - -uhd::device_addrs_t octoclock_find(const uhd::device_addr_t &hint); +uhd::device_addrs_t octoclock_find(const uhd::device_addr_t& hint); /*! * OctoClock implementation guts */ -class octoclock_impl : public uhd::device{ +class octoclock_impl : public uhd::device +{ public: - octoclock_impl(const uhd::device_addr_t &); - ~octoclock_impl(void) {}; + octoclock_impl(const uhd::device_addr_t&); + ~octoclock_impl(void){}; - uhd::rx_streamer::sptr get_rx_stream(const uhd::stream_args_t &args); + uhd::rx_streamer::sptr get_rx_stream(const uhd::stream_args_t& args); - uhd::tx_streamer::sptr get_tx_stream(const uhd::stream_args_t &args); + uhd::tx_streamer::sptr get_tx_stream(const uhd::stream_args_t& args); bool recv_async_msg(uhd::async_metadata_t&, double); private: - struct oc_container_type{ + struct oc_container_type + { uhd::usrp_clock::octoclock_eeprom_t eeprom; octoclock_state_t state; uhd::transport::udp_simple::sptr ctrl_xport; @@ -47,25 +47,26 @@ private: }; uhd::dict<std::string, oc_container_type> _oc_dict; uint32_t _sequence; - uint32_t _proto_ver; + uint32_t _proto_ver; - void _set_eeprom(const std::string &oc, const uhd::usrp_clock::octoclock_eeprom_t &oc_eeprom); + void _set_eeprom( + const std::string& oc, const uhd::usrp_clock::octoclock_eeprom_t& oc_eeprom); - uint32_t _get_fw_version(const std::string &oc); + uint32_t _get_fw_version(const std::string& oc); - void _get_state(const std::string &oc); + void _get_state(const std::string& oc); - uhd::sensor_value_t _ext_ref_detected(const std::string &oc); + uhd::sensor_value_t _ext_ref_detected(const std::string& oc); - uhd::sensor_value_t _gps_detected(const std::string &oc); + uhd::sensor_value_t _gps_detected(const std::string& oc); - uhd::sensor_value_t _which_ref(const std::string &oc); + uhd::sensor_value_t _which_ref(const std::string& oc); - uhd::sensor_value_t _switch_pos(const std::string &oc); + uhd::sensor_value_t _switch_pos(const std::string& oc); - uint32_t _get_time(const std::string &oc); + uint32_t _get_time(const std::string& oc); - std::string _get_images_help_message(const std::string &addr); + std::string _get_images_help_message(const std::string& addr); boost::mutex _device_mutex; }; diff --git a/host/lib/usrp_clock/octoclock/octoclock_uart.cpp b/host/lib/usrp_clock/octoclock/octoclock_uart.cpp index 7fc455dda..15c503888 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_uart.cpp +++ b/host/lib/usrp_clock/octoclock/octoclock_uart.cpp @@ -5,163 +5,173 @@ // SPDX-License-Identifier: GPL-3.0-or-later // -#include <iostream> -#include <string> -#include <chrono> -#include <thread> -#include <string.h> +#include "octoclock_uart.hpp" +#include "common.h" +#include <uhd/exception.hpp> +#include <uhd/utils/byteswap.hpp> #include <stdint.h> - +#include <string.h> #include <boost/algorithm/string.hpp> #include <boost/asio.hpp> #include <boost/format.hpp> #include <boost/thread/thread.hpp> - -#include <uhd/exception.hpp> -#include <uhd/utils/byteswap.hpp> - -#include "common.h" -#include "octoclock_uart.hpp" +#include <chrono> +#include <string> +#include <thread> namespace asio = boost::asio; using namespace uhd::transport; -#define NUM_WRAPS_EQUAL (_state.num_wraps == _device_state.num_wraps) -#define POS_EQUAL (_state.pos == _device_state.pos) -#define STATES_EQUAL (NUM_WRAPS_EQUAL && POS_EQUAL) -#define MAX_CACHE_AGE 256 //seconds - -namespace uhd{ - octoclock_uart_iface::octoclock_uart_iface(udp_simple::sptr udp, uint32_t proto_ver): uart_iface(){ - _udp = udp; - _state.num_wraps = 0; - _state.pos = 0; - _device_state.num_wraps = 0; - _device_state.pos = 0; - _proto_ver = proto_ver; - // To avoid replicating sequence numbers between sessions - _sequence = uint32_t(std::rand()); - size_t len = 0; - - //Get pool size from device - octoclock_packet_t pkt_out; - pkt_out.sequence = uhd::htonx<uint32_t>(_sequence); - pkt_out.len = 0; - - uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<octoclock_packet_t*>(octoclock_data); - - UHD_OCTOCLOCK_SEND_AND_RECV(_udp, _proto_ver, SEND_POOLSIZE_CMD, pkt_out, len, octoclock_data); - if(UHD_OCTOCLOCK_PACKET_MATCHES(SEND_POOLSIZE_ACK, pkt_out, pkt_in, len)){ - _poolsize = pkt_in->poolsize; - _cache.resize(_poolsize); - } - else throw uhd::runtime_error("Failed to communicate with GPSDO."); - } +#define NUM_WRAPS_EQUAL (_state.num_wraps == _device_state.num_wraps) +#define POS_EQUAL (_state.pos == _device_state.pos) +#define STATES_EQUAL (NUM_WRAPS_EQUAL && POS_EQUAL) +#define MAX_CACHE_AGE 256 // seconds + +namespace uhd { +octoclock_uart_iface::octoclock_uart_iface(udp_simple::sptr udp, uint32_t proto_ver) + : uart_iface() +{ + _udp = udp; + _state.num_wraps = 0; + _state.pos = 0; + _device_state.num_wraps = 0; + _device_state.pos = 0; + _proto_ver = proto_ver; + // To avoid replicating sequence numbers between sessions + _sequence = uint32_t(std::rand()); + size_t len = 0; + + // Get pool size from device + octoclock_packet_t pkt_out; + pkt_out.sequence = uhd::htonx<uint32_t>(_sequence); + pkt_out.len = 0; + + uint8_t octoclock_data[udp_simple::mtu]; + const octoclock_packet_t* pkt_in = + reinterpret_cast<octoclock_packet_t*>(octoclock_data); + + UHD_OCTOCLOCK_SEND_AND_RECV( + _udp, _proto_ver, SEND_POOLSIZE_CMD, pkt_out, len, octoclock_data); + if (UHD_OCTOCLOCK_PACKET_MATCHES(SEND_POOLSIZE_ACK, pkt_out, pkt_in, len)) { + _poolsize = pkt_in->poolsize; + _cache.resize(_poolsize); + } else + throw uhd::runtime_error("Failed to communicate with GPSDO."); +} - void octoclock_uart_iface::write_uart(const std::string &buf){ - size_t len = 0; +void octoclock_uart_iface::write_uart(const std::string& buf) +{ + size_t len = 0; - octoclock_packet_t pkt_out; - pkt_out.sequence = uhd::htonx<uint32_t>(++_sequence); - pkt_out.len = buf.size(); - memcpy(pkt_out.data, buf.c_str(), buf.size()); + octoclock_packet_t pkt_out; + pkt_out.sequence = uhd::htonx<uint32_t>(++_sequence); + pkt_out.len = buf.size(); + memcpy(pkt_out.data, buf.c_str(), buf.size()); - uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<octoclock_packet_t*>(octoclock_data); + uint8_t octoclock_data[udp_simple::mtu]; + const octoclock_packet_t* pkt_in = + reinterpret_cast<octoclock_packet_t*>(octoclock_data); - UHD_OCTOCLOCK_SEND_AND_RECV(_udp, _proto_ver, HOST_SEND_TO_GPSDO_CMD, pkt_out, len, octoclock_data); - if(not UHD_OCTOCLOCK_PACKET_MATCHES(HOST_SEND_TO_GPSDO_ACK, pkt_out, pkt_in, len)){ - throw uhd::runtime_error("Failed to send commands to GPSDO."); - } + UHD_OCTOCLOCK_SEND_AND_RECV( + _udp, _proto_ver, HOST_SEND_TO_GPSDO_CMD, pkt_out, len, octoclock_data); + if (not UHD_OCTOCLOCK_PACKET_MATCHES(HOST_SEND_TO_GPSDO_ACK, pkt_out, pkt_in, len)) { + throw uhd::runtime_error("Failed to send commands to GPSDO."); } +} - std::string octoclock_uart_iface::read_uart(double timeout){ - std::string result; - const auto exit_time = - std::chrono::steady_clock::now() - + std::chrono::milliseconds(int64_t(timeout*1e3)); +std::string octoclock_uart_iface::read_uart(double timeout) +{ + std::string result; + const auto exit_time = std::chrono::steady_clock::now() + + std::chrono::milliseconds(int64_t(timeout * 1e3)); - while(true) - { - _update_cache(); + while (true) { + _update_cache(); - for(char ch = _getchar(); ch != 0; ch = _getchar()){ - _rxbuff += ch; + for (char ch = _getchar(); ch != 0; ch = _getchar()) { + _rxbuff += ch; - //If newline found, return string - if(ch == '\n'){ - result.swap(_rxbuff); - return result; - } + // If newline found, return string + if (ch == '\n') { + result.swap(_rxbuff); + return result; } - if (std::chrono::steady_clock::now() > exit_time) { - break; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1)); } - - return result; + if (std::chrono::steady_clock::now() > exit_time) { + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } - void octoclock_uart_iface::_update_cache(){ - octoclock_packet_t pkt_out; - pkt_out.len = 0; - size_t len = 0; - - uint8_t octoclock_data[udp_simple::mtu]; - const octoclock_packet_t *pkt_in = reinterpret_cast<octoclock_packet_t*>(octoclock_data); - - if(STATES_EQUAL){ - boost::system_time time = boost::get_system_time(); - boost::posix_time::time_duration age = time - _last_cache_update; - bool cache_expired = (age > boost::posix_time::seconds(MAX_CACHE_AGE)); - - pkt_out.sequence = uhd::htonx<uint32_t>(++_sequence); - UHD_OCTOCLOCK_SEND_AND_RECV(_udp, _proto_ver, SEND_GPSDO_CACHE_CMD, pkt_out, len, octoclock_data); - if(UHD_OCTOCLOCK_PACKET_MATCHES(SEND_GPSDO_CACHE_ACK, pkt_out, pkt_in, len)){ - memcpy(&_cache[0], pkt_in->data, _poolsize); - _device_state = pkt_in->state; - _last_cache_update = time; - } + return result; +} + +void octoclock_uart_iface::_update_cache() +{ + octoclock_packet_t pkt_out; + pkt_out.len = 0; + size_t len = 0; + + uint8_t octoclock_data[udp_simple::mtu]; + const octoclock_packet_t* pkt_in = + reinterpret_cast<octoclock_packet_t*>(octoclock_data); + + if (STATES_EQUAL) { + boost::system_time time = boost::get_system_time(); + boost::posix_time::time_duration age = time - _last_cache_update; + bool cache_expired = (age > boost::posix_time::seconds(MAX_CACHE_AGE)); - uint8_t delta_wraps = (_device_state.num_wraps - _state.num_wraps); - if(cache_expired or delta_wraps > 1 or - ((delta_wraps == 1) and (_device_state.pos > _state.pos))){ - - _state.pos = _device_state.pos; - _state.num_wraps = (_device_state.num_wraps-1); - _rxbuff.clear(); - - while((_cache[_state.pos] != '\n')){ - _state.pos = (_state.pos+1) % _poolsize; - //We may have wrapped around locally - if(_state.pos == 0) _state.num_wraps++; - if(STATES_EQUAL) break; - } - if (_cache[_state.pos] == '\n'){ - _state.pos = (_state.pos+1) % _poolsize; - //We may have wrapped around locally - if(_state.pos == 0) _state.num_wraps++; - } + pkt_out.sequence = uhd::htonx<uint32_t>(++_sequence); + UHD_OCTOCLOCK_SEND_AND_RECV( + _udp, _proto_ver, SEND_GPSDO_CACHE_CMD, pkt_out, len, octoclock_data); + if (UHD_OCTOCLOCK_PACKET_MATCHES(SEND_GPSDO_CACHE_ACK, pkt_out, pkt_in, len)) { + memcpy(&_cache[0], pkt_in->data, _poolsize); + _device_state = pkt_in->state; + _last_cache_update = time; + } + + uint8_t delta_wraps = (_device_state.num_wraps - _state.num_wraps); + if (cache_expired or delta_wraps > 1 + or ((delta_wraps == 1) and (_device_state.pos > _state.pos))) { + _state.pos = _device_state.pos; + _state.num_wraps = (_device_state.num_wraps - 1); + _rxbuff.clear(); + + while ((_cache[_state.pos] != '\n')) { + _state.pos = (_state.pos + 1) % _poolsize; + // We may have wrapped around locally + if (_state.pos == 0) + _state.num_wraps++; + if (STATES_EQUAL) + break; + } + if (_cache[_state.pos] == '\n') { + _state.pos = (_state.pos + 1) % _poolsize; + // We may have wrapped around locally + if (_state.pos == 0) + _state.num_wraps++; } } } +} - char octoclock_uart_iface::_getchar(){ - if(STATES_EQUAL){ - return 0; - } +char octoclock_uart_iface::_getchar() +{ + if (STATES_EQUAL) { + return 0; + } - char ch = _cache[_state.pos]; - _state.pos = ((_state.pos+1) % _poolsize); - //We may have wrapped around locally - if(_state.pos == 0) _state.num_wraps++; + char ch = _cache[_state.pos]; + _state.pos = ((_state.pos + 1) % _poolsize); + // We may have wrapped around locally + if (_state.pos == 0) + _state.num_wraps++; - return ch; - } + return ch; +} - uart_iface::sptr octoclock_make_uart_iface(udp_simple::sptr udp, uint32_t proto_ver){ - return uart_iface::sptr(new octoclock_uart_iface(udp, proto_ver)); - } +uart_iface::sptr octoclock_make_uart_iface(udp_simple::sptr udp, uint32_t proto_ver) +{ + return uart_iface::sptr(new octoclock_uart_iface(udp, proto_ver)); } +} // namespace uhd diff --git a/host/lib/usrp_clock/octoclock/octoclock_uart.hpp b/host/lib/usrp_clock/octoclock/octoclock_uart.hpp index cb0f58591..02fcf25e3 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_uart.hpp +++ b/host/lib/usrp_clock/octoclock/octoclock_uart.hpp @@ -8,23 +8,26 @@ #ifndef INCLUDED_OCTOCLOCK_UART_HPP #define INCLUDED_OCTOCLOCK_UART_HPP -#include <vector> - +#include "common.h" #include <uhd/transport/udp_simple.hpp> #include <uhd/types/serial.hpp> +#include <boost/thread.hpp> +#include <string> +#include <vector> /*! * The OctoClock doesn't take UART input per se but reads a specific * packet type and sends the string from there through its own serial * functions. */ -namespace uhd{ -class octoclock_uart_iface : public uhd::uart_iface{ +namespace uhd { +class octoclock_uart_iface : public uhd::uart_iface +{ public: octoclock_uart_iface(uhd::transport::udp_simple::sptr udp, uint32_t proto_ver); - ~octoclock_uart_iface(void) {}; + ~octoclock_uart_iface(void){}; - void write_uart(const std::string &buf); + void write_uart(const std::string& buf); std::string read_uart(double timeout); private: @@ -36,15 +39,16 @@ private: std::vector<uint8_t> _cache; std::string _rxbuff; uint32_t _sequence; - uint32_t _proto_ver; + uint32_t _proto_ver; boost::system_time _last_cache_update; void _update_cache(); char _getchar(); }; -uart_iface::sptr octoclock_make_uart_iface(uhd::transport::udp_simple::sptr udp, uint32_t proto_ver); +uart_iface::sptr octoclock_make_uart_iface( + uhd::transport::udp_simple::sptr udp, uint32_t proto_ver); -} +} // namespace uhd #endif /* INCLUDED_OCTOCLOCK_UART_HPP */ diff --git a/host/lib/usrp_clock/usrp_clock_c.cpp b/host/lib/usrp_clock/usrp_clock_c.cpp index 56a9a8740..ea67c7e6e 100644 --- a/host/lib/usrp_clock/usrp_clock_c.cpp +++ b/host/lib/usrp_clock/usrp_clock_c.cpp @@ -7,27 +7,26 @@ /* C-Interface for multi_usrp_clock */ -#include <uhd/utils/static.hpp> #include <uhd/usrp_clock/multi_usrp_clock.hpp> - #include <uhd/usrp_clock/usrp_clock.h> - -#include <boost/thread/mutex.hpp> - +#include <uhd/utils/static.hpp> #include <string.h> +#include <boost/thread/mutex.hpp> #include <map> /**************************************************************************** * Registry / Pointer Management ***************************************************************************/ /* Public structs */ -struct uhd_usrp_clock { +struct uhd_usrp_clock +{ size_t usrp_clock_index; std::string last_error; }; /* Not public: We use this for our internal registry */ -struct usrp_clock_ptr { +struct usrp_clock_ptr +{ uhd::usrp_clock::multi_usrp_clock::sptr ptr; static size_t usrp_clock_counter; }; @@ -44,122 +43,93 @@ UHD_SINGLETON_FCN(usrp_clock_ptrs, get_usrp_clock_ptrs); * Generate / Destroy API calls ***************************************************************************/ static boost::mutex _usrp_clock_find_mutex; -uhd_error uhd_usrp_clock_find( - const char* args, - uhd_string_vector_t *devices_out -){ +uhd_error uhd_usrp_clock_find(const char* args, uhd_string_vector_t* devices_out) +{ UHD_SAFE_C( boost::mutex::scoped_lock lock(_usrp_clock_find_mutex); - uhd::device_addrs_t devs = uhd::device::find(std::string(args), uhd::device::CLOCK); + uhd::device_addrs_t devs = + uhd::device::find(std::string(args), uhd::device::CLOCK); devices_out->string_vector_cpp.clear(); - for(const uhd::device_addr_t &dev: devs){ - devices_out->string_vector_cpp.push_back(dev.to_string()); - } - ) + for (const uhd::device_addr_t& dev + : devs) { devices_out->string_vector_cpp.push_back(dev.to_string()); }) } static boost::mutex _usrp_clock_make_mutex; -uhd_error uhd_usrp_clock_make( - uhd_usrp_clock_handle *h, - const char *args -){ - UHD_SAFE_C( - boost::mutex::scoped_lock lock(_usrp_clock_make_mutex); +uhd_error uhd_usrp_clock_make(uhd_usrp_clock_handle* h, const char* args) +{ + UHD_SAFE_C(boost::mutex::scoped_lock lock(_usrp_clock_make_mutex); - size_t usrp_clock_count = usrp_clock_ptr::usrp_clock_counter; - usrp_clock_ptr::usrp_clock_counter++; + size_t usrp_clock_count = usrp_clock_ptr::usrp_clock_counter; + usrp_clock_ptr::usrp_clock_counter++; - // Initialize USRP Clock - uhd::device_addr_t device_addr(args); - usrp_clock_ptr P; - P.ptr = uhd::usrp_clock::multi_usrp_clock::make(device_addr); + // Initialize USRP Clock + uhd::device_addr_t device_addr(args); + usrp_clock_ptr P; + P.ptr = uhd::usrp_clock::multi_usrp_clock::make(device_addr); - // Dump into registry - get_usrp_clock_ptrs()[usrp_clock_count] = P; + // Dump into registry + get_usrp_clock_ptrs()[usrp_clock_count] = P; - // Update handle - (*h) = new uhd_usrp_clock; - (*h)->usrp_clock_index = usrp_clock_count; - ) + // Update handle + (*h) = new uhd_usrp_clock; + (*h)->usrp_clock_index = usrp_clock_count;) } static boost::mutex _usrp_clock_free_mutex; -uhd_error uhd_usrp_clock_free( - uhd_usrp_clock_handle *h -){ - UHD_SAFE_C( - boost::mutex::scoped_lock lock(_usrp_clock_free_mutex); - - if(!get_usrp_clock_ptrs().count((*h)->usrp_clock_index)){ - return UHD_ERROR_INVALID_DEVICE; - } - - get_usrp_clock_ptrs().erase((*h)->usrp_clock_index); - delete *h; - *h = NULL; - ) +uhd_error uhd_usrp_clock_free(uhd_usrp_clock_handle* h) +{ + UHD_SAFE_C(boost::mutex::scoped_lock lock(_usrp_clock_free_mutex); + + if (!get_usrp_clock_ptrs().count((*h)->usrp_clock_index)) { + return UHD_ERROR_INVALID_DEVICE; + } + + get_usrp_clock_ptrs() + .erase((*h)->usrp_clock_index); + delete *h; + *h = NULL;) } uhd_error uhd_usrp_clock_last_error( - uhd_usrp_clock_handle h, - char* error_out, - size_t strbuffer_len -){ - UHD_SAFE_C( - memset(error_out, '\0', strbuffer_len); - strncpy(error_out, h->last_error.c_str(), strbuffer_len); - ) + uhd_usrp_clock_handle h, char* error_out, size_t strbuffer_len) +{ + UHD_SAFE_C(memset(error_out, '\0', strbuffer_len); + strncpy(error_out, h->last_error.c_str(), strbuffer_len);) } uhd_error uhd_usrp_clock_get_pp_string( - uhd_usrp_clock_handle h, - char* pp_string_out, - size_t strbuffer_len -){ - UHD_SAFE_C_SAVE_ERROR(h, - memset(pp_string_out, '\0', strbuffer_len); - strncpy(pp_string_out, USRP_CLOCK(h)->get_pp_string().c_str(), strbuffer_len); - ) + uhd_usrp_clock_handle h, char* pp_string_out, size_t strbuffer_len) +{ + UHD_SAFE_C_SAVE_ERROR(h, memset(pp_string_out, '\0', strbuffer_len); strncpy( + pp_string_out, USRP_CLOCK(h)->get_pp_string().c_str(), strbuffer_len);) } -uhd_error uhd_usrp_clock_get_num_boards( - uhd_usrp_clock_handle h, - size_t *num_boards_out -){ - UHD_SAFE_C_SAVE_ERROR(h, - *num_boards_out = USRP_CLOCK(h)->get_num_boards(); - ) +uhd_error uhd_usrp_clock_get_num_boards(uhd_usrp_clock_handle h, size_t* num_boards_out) +{ + UHD_SAFE_C_SAVE_ERROR(h, *num_boards_out = USRP_CLOCK(h)->get_num_boards();) } uhd_error uhd_usrp_clock_get_time( - uhd_usrp_clock_handle h, - size_t board, - uint32_t *clock_time_out -){ - UHD_SAFE_C_SAVE_ERROR(h, - *clock_time_out = USRP_CLOCK(h)->get_time(board); - ) + uhd_usrp_clock_handle h, size_t board, uint32_t* clock_time_out) +{ + UHD_SAFE_C_SAVE_ERROR(h, *clock_time_out = USRP_CLOCK(h)->get_time(board);) } -uhd_error uhd_usrp_clock_get_sensor( - uhd_usrp_clock_handle h, +uhd_error uhd_usrp_clock_get_sensor(uhd_usrp_clock_handle h, const char* name, size_t board, - uhd_sensor_value_handle *sensor_value_out -){ - UHD_SAFE_C_SAVE_ERROR(h, - delete (*sensor_value_out)->sensor_value_cpp; - (*sensor_value_out)->sensor_value_cpp = new uhd::sensor_value_t(USRP_CLOCK(h)->get_sensor(name, board)); - ) + uhd_sensor_value_handle* sensor_value_out) +{ + UHD_SAFE_C_SAVE_ERROR(h, delete (*sensor_value_out)->sensor_value_cpp; + (*sensor_value_out)->sensor_value_cpp = new uhd::sensor_value_t( + USRP_CLOCK(h)->get_sensor(name, board));) } uhd_error uhd_usrp_clock_get_sensor_names( - uhd_usrp_clock_handle h, - size_t board, - uhd_string_vector_handle *sensor_names_out -){ - UHD_SAFE_C_SAVE_ERROR(h, - (*sensor_names_out)->string_vector_cpp = USRP_CLOCK(h)->get_sensor_names(board); - ) + uhd_usrp_clock_handle h, size_t board, uhd_string_vector_handle* sensor_names_out) +{ + UHD_SAFE_C_SAVE_ERROR( + h, + (*sensor_names_out)->string_vector_cpp = USRP_CLOCK(h)->get_sensor_names(board);) } |