forked from paulh002/sdrberry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.cpp
155 lines (129 loc) · 4.45 KB
/
device.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstdio> //stdandard output
#include <cstdlib>
#include "devices.h"
#include <string> // std::string
#include <vector> // std::vector<...>
#include <map> // std::map< ... , ... >
#include <algorithm> //sort, min, max
#include <unistd.h>
#include <ios>
#include <fstream>
#include <iostream>
int num_devices = 0;
struct device_structure soapy_devices[MAX_NUM_DEVICES];
std::string SoapySDRDeviceProbe(struct device_structure *sdev);
/***********************************************************************
* Find devices and print args
**********************************************************************/
static int findDevices()
{
string information[MAX_NUM_RANGES];
const auto results = SoapySDR::Device::enumerate("");
for (size_t i = 0; i < results.size(); i++)
{
std::cout << "Found device " << i << std::endl;
int ii = 0;
for (const auto &it : results[i])
{
information[ii++] = std::string((char *)it.first.c_str());
information[ii++] = std::string((char *)it.second.c_str());
std::cout << " " << it.first << " = " << it.second << std::endl;
}
std::cout << std::endl;
}
num_devices = results.size();
if (results.empty()) std::cerr << "No devices found! " << std::endl;
else std::cout << std::endl;
return results.empty() ? EXIT_FAILURE : EXIT_SUCCESS;
}
/***********************************************************************
* Make device and print hardware info
**********************************************************************/
static int makeDevice(const std::string &argStr)
{
std::cout << "Make device " << argStr << std::endl;
try
{
auto device = SoapySDR::Device::make(argStr);
std::cout << " driver=" << device->getDriverKey() << std::endl;
std::cout << " hardware=" << device->getHardwareKey() << std::endl;
for (const auto &it : device->getHardwareInfo())
{
std::cout << " " << it.first << "=" << it.second << std::endl;
}
SoapySDR::Device::unmake(device);
}
catch (const std::exception &ex)
{
std::cerr << "Error making device: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
/***********************************************************************
* Make device and print detailed info
**********************************************************************/
static int probeDevice(const std::string &argStr)
{
std::cout << "Probe device " << argStr << std::endl;
try
{
auto device = SoapySDR::Device::make(argStr);
soapy_devices[0].sdr = device;
std::cout << SoapySDRDeviceProbe(&soapy_devices[0]) << std::endl;
// SoapySDR::Device::unmake(device);
}
catch (const std::exception &ex)
{
soapy_devices[0].sdr = NULL;
std::cerr << "Error probing device: " << ex.what() << std::endl;
return EXIT_FAILURE;
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
int discover_devices(std::string driver)
{ int i;
//findDevices();
SoapySDR_setLogLevel(SOAPY_SDR_DEBUG);
i = probeDevice((char *)driver.c_str());
// Channel information is collected in SoapySDRDeviceProbe
return i;
}
//////////////////////////////////////////////////////////////////////////////
//
// process_mem_usage(double &, double &) - takes two doubles by reference,
// attempts to read the system-dependent data for a process' virtual memory
// size and resident set size, and return the results in KB.
//
// On failure, returns 0.0, 0.0
void process_mem_usage(double& vm_usage, double& resident_set)
{
using std::ios_base;
using std::ifstream;
using std::string;
vm_usage = 0.0;
resident_set = 0.0;
// 'file' stat seems to give the most reliable results
//
ifstream stat_stream("/proc/self/stat", ios_base::in);
// dummy vars for leading entries in stat that we don't care about
//
string pid, comm, state, ppid, pgrp, session, tty_nr;
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
string utime, stime, cutime, cstime, priority, nice;
string O, itrealvalue, starttime;
// the two fields we want
//
unsigned long vsize;
long rss;
stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
>> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
>> utime >> stime >> cutime >> cstime >> priority >> nice
>> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
stat_stream.close();
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
vm_usage = vsize / 1024.0;
resident_set = rss * page_size_kb;
}