SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
deviceplutosdrscan.cpp
Go to the documentation of this file.
1 // Copyright (C) 2017 Edouard Griffiths, F4EXB //
3 // //
4 // This program is free software; you can redistribute it and/or modify //
5 // it under the terms of the GNU General Public License as published by //
6 // the Free Software Foundation as version 3 of the License, or //
7 // (at your option) any later version. //
8 // //
9 // This program is distributed in the hope that it will be useful, //
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
12 // GNU General Public License V3 for more details. //
13 // //
14 // You should have received a copy of the GNU General Public License //
15 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
17 
18 #include <iostream>
19 #include <cstdio>
20 #include <cstring>
21 #include <regex>
22 #include <iio.h>
23 
24 #include <QtGlobal>
25 
26 #include "deviceplutosdrbox.h"
27 #include "deviceplutosdrscan.h"
28 
30 {
31  int i, num_contexts;
32  struct iio_scan_context *scan_ctx;
33  struct iio_context_info **info;
34 
35  scan_ctx = iio_create_scan_context(0, 0);
36 
37  if (!scan_ctx)
38  {
39  qCritical("PlutoSDRScan::scan: could not create scan context");
40  return;
41  }
42 
43  num_contexts = iio_scan_context_get_info_list(scan_ctx, &info);
44 
45  if (num_contexts < 0)
46  {
47  qCritical("PlutoSDRScan::scan: could not get contexts");
48  return;
49  }
50 
51  m_scans.clear();
52 
53  if (num_contexts == 0)
54  {
55  struct iio_context *ctx = iio_create_network_context("pluto.local");
56  if(!ctx) {
57  return;
58  }
59  m_scans.push_back({std::string("PlutoSDR"), std::string("networked"), std::string("ip:pluto.local")});
60  m_serialMap[m_scans.back().m_serial] = &m_scans.back();
61  m_urilMap[m_scans.back().m_uri] = &m_scans.back();
62  iio_context_destroy(ctx);
63  }
64 
65  for (i = 0; i < num_contexts; i++)
66  {
67  const char *description = iio_context_info_get_description(info[i]);
68  const char *uri = iio_context_info_get_uri(info[i]);
69 
70  if (!DevicePlutoSDRBox::probeURI(std::string(uri))) { // continue if not accessible
71  continue;
72  }
73 
74  qDebug("PlutoSDRScan::scan: %d: %s [%s]", i, description, uri);
75  char *pch = strstr(const_cast<char*>(description), "PlutoSDR");
76 
77  if (pch)
78  {
79  m_scans.push_back({std::string(description), std::string("TBD"), std::string(uri)});
80  m_urilMap[m_scans.back().m_uri] = &m_scans.back();
81 
82  std::regex desc_regex(".*serial=(.+)");
83  std::smatch desc_match;
84  std::regex_search(m_scans.back().m_name, desc_match, desc_regex);
85 
86  if (desc_match.size() == 2)
87  {
88  m_scans.back().m_serial = desc_match[1];
89  m_serialMap[m_scans.back().m_serial] = &m_scans.back();
90  }
91  }
92  }
93 
94  iio_context_info_list_free(info);
95  iio_scan_context_destroy(scan_ctx);
96 }
97 
98 const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const
99 {
100  if (index < m_scans.size()) {
101  return &(m_scans[index].m_uri);
102  } else {
103  return 0;
104  }
105 }
106 
107 const std::string* DevicePlutoSDRScan::getSerialAt(unsigned int index) const
108 {
109  if (index < m_scans.size()) {
110  return &(m_scans[index].m_serial);
111  } else {
112  return 0;
113  }
114 }
115 
117  const std::string& serial) const
118 {
119  std::map<std::string, DeviceScan*>::const_iterator it = m_serialMap.find(serial);
120  if (it == m_serialMap.end()) {
121  return 0;
122  } else {
123  return &((it->second)->m_uri);
124  }
125 }
126 
127 void DevicePlutoSDRScan::getSerials(std::vector<std::string>& serials) const
128 {
129  std::vector<DeviceScan>::const_iterator it = m_scans.begin();
130  serials.clear();
131 
132  for (; it != m_scans.end(); ++it) {
133  serials.push_back(it->m_serial);
134  }
135 }
136 
std::vector< DeviceScan > m_scans
std::map< std::string, DeviceScan * > m_urilMap
std::map< std::string, DeviceScan * > m_serialMap
int32_t i
Definition: decimators.h:244
const std::string * getSerialAt(unsigned int index) const
void getSerials(std::vector< std::string > &serials) const
const std::string * getURIFromSerial(const std::string &serial) const
static bool probeURI(const std::string &uri)
const std::string * getURIAt(unsigned int index) const