Changeset 9424

Show
Ignore:
Timestamp:
08/26/08 20:02:31
Author:
jcorgan
Message:

(Re)implement MAC address normalization, use per-MAC address cache for USRP2 instances.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • usrp2/branches/developers/jcorgan/wip/host-ng/apps/rx_streaming_samples.cc

    r9215 r9424  
    356356 
    357357  if (verbose){ 
     358    printf("USRP2 MAC address: %s\n\n", u2->mac_addr().c_str()); 
    358359    printf("Daughterboard configuration:\n"); 
    359360    printf("  baseband_freq=%f\n", tr.baseband_freq); 
  • usrp2/branches/developers/jcorgan/wip/host-ng/include/usrp2/usrp2.h

    r9422 r9424  
    7575     * \param ifc   Network interface name, e.g., "eth0" 
    7676     * \param addr  Network mac address, e.g., "01:02:03:04:05:06", "05:06" or "". 
     77     *              If \p s is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH 
    7778     *              "" will autoselect a USRP2 if there is only a single one on the local ethernet. 
    7879     */ 
     
    8788     * Returns the MAC address associated with this USRP 
    8889     */ 
    89     const std::string &mac_addr(); 
     90    const std::string &mac_addr(); 
    9091 
    9192    /* 
     
    258259    bool config_mimo(int flags); 
    259260 
    260  
    261  
    262261    class impl;         // implementation details 
    263262 
    264263  private: 
    265     // Only usrp2::make factory function can instantiate this class 
    266     usrp2(const std::string &ifc, const std::string &addr); 
     264    // Static function to retrieve or create usrp2 instance 
     265    static sptr find_existing_or_make_new(const std::string &ifc, props *p); 
     266 
     267    // Only class members can instantiate this class 
     268    usrp2(const std::string &ifc, props *p); 
    267269   
    268270    // All private state is held in opaque pointer 
  • usrp2/branches/developers/jcorgan/wip/host-ng/lib/usrp2.cc

    r9422 r9424  
    4949  static usrp_table s_table; 
    5050 
    51   static usrp2::sptr 
    52   find_existing_or_make_new(const std::string &ifc, const std::string &mac_addr) 
    53   { 
    54     // FIXME normalize addr 
    55  
    56     if (mac_addr.size() != 17) 
    57       throw std::invalid_argument("invalid mac_addr: " + mac_addr); 
    58  
    59     std::string key = ifc + ":" + mac_addr; 
     51  usrp2::sptr 
     52  usrp2::find_existing_or_make_new(const std::string &ifc, props *pr) 
     53  { 
     54    std::string key = ifc + ":" + pr->addr; 
    6055 
    6156    boost::mutex::scoped_lock   guard(s_table_mutex); 
     
    7570 
    7671    // create a new one and stick it in the table. 
    77     usrp2::sptr r = usrp2::make(ifc, mac_addr); 
     72    usrp2::sptr r(new usrp2::usrp2(ifc, pr)); 
    7873    usrp_table_entry t(key, r); 
    7974    s_table.push_back(t); 
     
    8479  // --- end of table code --- 
    8580 
    86  
    87   // Shared pointer factory function, wraps constructor call 
     81  static bool 
     82  parse_mac_addr(const std::string &s, std::string &ns) 
     83  { 
     84    u2_mac_addr_t p; 
     85 
     86    p.addr[0] = 0x00;           // Matt's IAB 
     87    p.addr[1] = 0x50; 
     88    p.addr[2] = 0xC2; 
     89    p.addr[3] = 0x85; 
     90    p.addr[4] = 0x30; 
     91    p.addr[5] = 0x00; 
     92     
     93    int len = s.size(); 
     94    switch (len) { 
     95       
     96    case 5: 
     97      if (sscanf(s.c_str(), "%hhx:%hhx", &p.addr[4], &p.addr[5]) != 2) 
     98        return false; 
     99      break; 
     100       
     101    case 17: 
     102      if (sscanf(s.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 
     103                 &p.addr[0], &p.addr[1], &p.addr[2], 
     104                 &p.addr[3], &p.addr[4], &p.addr[5]) != 6) 
     105        return false; 
     106      break; 
     107 
     108    default: 
     109      return false; 
     110    } 
     111     
     112    char buf[128]; 
     113    snprintf(buf, sizeof(buf), 
     114             "%02x:%02x:%02x:%02x:%02x:%02x", 
     115             p.addr[0],p.addr[1],p.addr[2], 
     116             p.addr[3],p.addr[4],p.addr[5]); 
     117    ns = std::string(buf); 
     118    return true; 
     119  } 
     120 
    88121  usrp2::sptr 
    89122  usrp2::make(const std::string &ifc, const std::string &addr) 
    90123  { 
    91     return usrp2::sptr(new usrp2(ifc, addr)); 
     124    std::string naddr = ""; 
     125    if (addr != "" && !parse_mac_addr(addr, naddr)) 
     126      throw std::runtime_error("Invalid MAC address"); 
     127 
     128    props_vector_t u2s = find(ifc, naddr); 
     129    if (u2s.size() != 1) 
     130      throw std::runtime_error("Unable to find requested USRP2."); 
     131 
     132    return find_existing_or_make_new(ifc, &u2s[0]); 
    92133  } 
    93134 
    94135  // Private constructor.  Sole function is to create an impl. 
    95   usrp2::usrp2(const std::string &ifc, const std::string &addr
    96     : d_impl(new usrp2::impl(ifc, addr)) 
     136  usrp2::usrp2(const std::string &ifc, props *p
     137    : d_impl(new usrp2::impl(ifc, p->addr)) 
    97138  { 
    98139    // NOP