summaryrefslogtreecommitdiffstats
path: root/lib/device_addr.cpp
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2010-01-13 19:35:38 -0800
committerJosh Blum <josh@joshknows.com>2010-01-13 19:35:38 -0800
commitb52880892d1a85203e58cdb049877e7ae4c16663 (patch)
tree5ef463525c9633bfbe7eadb4b3da5b1188bb5142 /lib/device_addr.cpp
parent24d95c3f2cea168d1d314af29839d88cc16d3c7f (diff)
downloaduhd-b52880892d1a85203e58cdb049877e7ae4c16663.tar.gz
uhd-b52880892d1a85203e58cdb049877e7ae4c16663.tar.bz2
uhd-b52880892d1a85203e58cdb049877e7ae4c16663.zip
Added strict compiler flags.
Made changes to meet compilation. Added test to verify ip and mac addr code.
Diffstat (limited to 'lib/device_addr.cpp')
-rw-r--r--lib/device_addr.cpp55
1 files changed, 34 insertions, 21 deletions
diff --git a/lib/device_addr.cpp b/lib/device_addr.cpp
index ac66c0d25..34c61ede8 100644
--- a/lib/device_addr.cpp
+++ b/lib/device_addr.cpp
@@ -5,36 +5,49 @@
#include <usrp_uhd/device_addr.hpp>
#include <sstream>
#include <cstring>
-#include <cstdio>
#include <stdexcept>
+#include <boost/format.hpp>
+#include <boost/algorithm/string.hpp>
//----------------------- u2 mac addr wrapper ------------------------//
-usrp_uhd::mac_addr_t::mac_addr_t(const std::string &str){
+usrp_uhd::mac_addr_t::mac_addr_t(const std::string &mac_addr_str){
//ether_aton_r(str.c_str(), &mac_addr);
- bool good = false;
- char p[6] = {0x00, 0x50, 0xC2, 0x85, 0x30, 0x00}; // Matt's IAB
+ uint8_t p[6] = {0x00, 0x50, 0xC2, 0x85, 0x30, 0x00}; // Matt's IAB
- switch (str.size()){
- case 5:
- good = sscanf(str.c_str(), "%hhx:%hhx", &p[4], &p[5]) == 2;
- break;
- case 17:
- good = sscanf(str.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
- &p[0], &p[1], &p[2], &p[3], &p[4], &p[5]) == 6;
- break;
+ try{
+ //only allow patterns of xx:xx or xx:xx:xx:xx:xx:xx
+ //the IAB above will fill in for the shorter pattern
+ if (mac_addr_str.size() != 5 and mac_addr_str.size() != 17)
+ throw std::runtime_error("expected exactly 5 or 17 characters");
+
+ //split the mac addr hex string at the colons
+ std::vector<std::string> hex_strs;
+ boost::split(hex_strs, mac_addr_str, boost::is_any_of(":"));
+ for (size_t i = 0; i < hex_strs.size(); i++){
+ int hex_num;
+ std::istringstream iss(hex_strs[i]);
+ iss >> std::hex >> hex_num;
+ p[i] = uint8_t(hex_num);
+ }
+
+ }
+ catch(std::exception const& e){
+ throw std::runtime_error(str(
+ boost::format("Invalid mac address: %s\n\t%s") % mac_addr_str % e.what()
+ ));
}
- if (not good) throw std::runtime_error("Invalid mac address: " + str);
memcpy(&mac_addr, p, sizeof(mac_addr));
}
std::string usrp_uhd::mac_addr_t::to_string(void) const{
- char addr_buf[128];
//ether_ntoa_r(&mac_addr, addr_buf);
const uint8_t *p = reinterpret_cast<const uint8_t *>(&mac_addr);
- sprintf(addr_buf, "%02x:%02x:%02x:%02x:%02x:%02x",
- p[0], p[1], p[2], p[3], p[4], p[5]);
- return std::string(addr_buf);
+ return str(
+ boost::format("%02x:%02x:%02x:%02x:%02x:%02x")
+ % int(p[0]) % int(p[1]) % int(p[2])
+ % int(p[3]) % int(p[4]) % int(p[5])
+ );
}
std::ostream& operator<<(std::ostream &os, const usrp_uhd::mac_addr_t &x){
@@ -43,9 +56,9 @@ std::ostream& operator<<(std::ostream &os, const usrp_uhd::mac_addr_t &x){
}
//----------------------- u2 ipv4 wrapper ----------------------------//
-usrp_uhd::ip_addr_t::ip_addr_t(const std::string &str){
- int ret = inet_pton(AF_INET, str.c_str(), &ip_addr);
- if (ret == 0) throw std::runtime_error("Invalid ip address: " + str);
+usrp_uhd::ip_addr_t::ip_addr_t(const std::string &ip_addr_str){
+ int ret = inet_pton(AF_INET, ip_addr_str.c_str(), &ip_addr);
+ if (ret == 0) throw std::runtime_error("Invalid ip address: " + ip_addr_str);
}
std::string usrp_uhd::ip_addr_t::to_string(void) const{
@@ -73,7 +86,7 @@ usrp_uhd::device_addr_t::device_addr_t(device_addr_type_t device_addr_type){
}
std::string usrp_uhd::device_addr_t::to_string(void) const{
- std::stringstream out;
+ std::ostringstream out;
out << "USRP Type: ";
switch(type){
case DEVICE_ADDR_TYPE_AUTO: