SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
USRPPropTree.hxx
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, Matthew H. Reilly (kb1vc)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#ifndef USRP_PROP_TREE_HDR
30#define USRP_PROP_TREE_HDR
31
32#include <string>
33#include <uhd/usrp/multi_usrp.hpp>
34#include <uhd/property_tree.hpp>
35#include <SoDa/Format.hxx>
36#include <iostream>
37
38namespace SoDa
39{
45{
46public:
55 USRPPropTree(const uhd::usrp::multi_usrp::sptr usrp, const std::string &requester, int mb_index = 0)
56 {
57 client_name = requester;
58 try
59 {
60 mb0_name = usrp->get_device()->get_tree()->list("/mboards").at(mb_index);
61 }
62 catch (std::runtime_error e)
63 {
64 complain((SoDa::Format("Exception looking for motherboard %0").addI(mb_index)).str(), e, false);
65 }
66
67 // the object is to create a tree rooted at the first motherboard.
68 fqpn = "/mboards/" + mb0_name;
69 try
70 {
71 tree = usrp->get_device()->get_tree()->subtree(fqpn);
72 }
73 catch (std::runtime_error e)
74 {
75 complain("Exception while creating initial motherboard property tree", fqpn, e, false);
76 }
77 }
78
86 USRPPropTree(USRPPropTree *ptree, const std::string &path)
87 {
88 init(ptree, path);
89 }
90
98 USRPPropTree(USRPPropTree &ptree, const std::string &path)
99 {
100 init(&ptree, path);
101 }
102
108 bool hasProperty(const std::string &propname)
109 {
110 bool ret = false;
111 try
112 {
113 std::vector<std::string> pslis = tree->list("");
114 ret = std::find(pslis.begin(), pslis.end(), propname) != pslis.end();
115 }
116 catch (std::runtime_error e)
117 {
118 complain("Exception while looking for property", fqpn + "/" + propname, e, false);
119 }
120 return ret;
121 }
122
123 std::vector<std::string> getPropNames(const std::string &path = std::string(""))
124 {
125 return tree->list(path);
126 }
127
128 std::string getStringProp(const std::string &propname, const std::string defval = std::string("None"))
129 {
130 return getProperty<std::string>(propname, defval);
131 }
132
133 int getIntProp(const std::string &propname, const int defval = 0)
134 {
135 return getProperty<int>(propname, defval);
136 }
137
138 int getDoubleProp(const std::string &propname, const double defval = 0.0)
139 {
140 return getProperty<double>(propname, defval);
141 }
142
143 bool getBoolProp(const std::string &propname, const bool defval = false)
144 {
145 return getProperty<bool>(propname, defval);
146 }
147
148 void setStringProp(const std::string &propname, const std::string val = std::string("None"))
149 {
150 setProperty<std::string>(propname, val);
151 }
152
153 void setIntProp(const std::string &propname, const int val = 0)
154 {
155 setProperty<int>(propname, val);
156 }
157
158 void setDoubleProp(const std::string &propname, const double val = 0.0)
159 {
160 setProperty<double>(propname, val);
161 }
162
163 void setBoolProp(const std::string &propname, const bool val = false)
164 {
165 setProperty<bool>(propname, val);
166 }
167
168 template <typename T>
169 T getProperty(const std::string &propname, const T defval)
170 {
171 T ret = defval;
172 std::string path = fqpn + "/" + propname;
173 try
174 {
175 ret = tree->access<T>(propname).get();
176 }
177 catch (std::runtime_error e)
178 {
179 complain("getProperty Unknown exception -- unknown property", path, e);
180 }
181 return ret;
182 }
183
184 template <typename T>
185 T getProperty(const std::string &propname)
186 {
187 T ret;
188 std::string path = fqpn + "/" + propname;
189 try
190 {
191 ret = tree->access<T>(propname).get();
192 }
193 catch (std::runtime_error e)
194 {
195 complain("getProperty Unknown exception -- unknown property", path, e, false);
196 }
197 return ret;
198 }
199
200 template <typename T>
201 void setProperty(const std::string &propname, const T val)
202 {
203 std::string path = fqpn + "/" + propname;
204 try
205 {
206 tree->access<T>(propname).set(val);
207 }
208 catch (std::runtime_error e)
209 {
210 complain("setProperty Unknown exception -- unknown property", path, e);
211 }
212 }
213
214private:
215 void init(USRPPropTree *ptree, const std::string &path)
216 {
217 client_name = ptree->client_name;
218 try
219 {
220 tree = ptree->tree->subtree(path);
221 }
222 catch (std::runtime_error e)
223 {
224 complain("Exception while creating a subtree", path, e, false);
225 }
226
227 fqpn = ptree->fqpn + "/" + path;
228 }
229
230 void complain(const std::string &explain_string,
231 std::runtime_error &e,
232 bool continue_p = true)
233 {
234 std::cerr << SoDa::Format("USRPPropTree encountered %0\n\tRequester: %1\n\tException: [%2]\n\t%3\n")
235 .addS(explain_string)
236 .addS(client_name)
237 .addS(e.what())
238 .addS((continue_p ? "Thread will continue" : "Thread will terminate"));
239 if (!continue_p)
240 exit(-1);
241 }
242
243 void complain(const std::string &explain_string,
244 const std::string &path,
245 std::runtime_error &e,
246 bool continue_p = true)
247 {
248 std::cerr << SoDa::Format("USRPPropTree encountered %0\n\twhile looking for path [%1]\n\tRequester: %2\n\tException: [%3]\n\t%4\n")
249 .addS(explain_string)
250 .addS(path)
251 .addS(client_name)
252 .addS(e.what())
253 .addS((continue_p ? "Thread will continue" : "Thread will terminate"));
254
255 if (!continue_p)
256 exit(-1);
257 }
258
259 std::string client_name;
260 std::string fqpn;
261 uhd::property_tree::sptr tree;
262 std::string mb0_name;
263};
264} // namespace SoDa
265#endif
void setDoubleProp(const std::string &propname, const double val=0.0)
uhd::property_tree::sptr tree
void setProperty(const std::string &propname, const T val)
USRPPropTree(USRPPropTree *ptree, const std::string &path)
Constructor – build a property tree widget that is a subtree of the mboard property tree.
void complain(const std::string &explain_string, std::runtime_error &e, bool continue_p=true)
T getProperty(const std::string &propname, const T defval)
int getIntProp(const std::string &propname, const int defval=0)
void init(USRPPropTree *ptree, const std::string &path)
std::string client_name
the name of the runtime object/thread that created this tree.
void setBoolProp(const std::string &propname, const bool val=false)
std::string getStringProp(const std::string &propname, const std::string defval=std::string("None"))
bool getBoolProp(const std::string &propname, const bool defval=false)
void setIntProp(const std::string &propname, const int val=0)
void setStringProp(const std::string &propname, const std::string val=std::string("None"))
int getDoubleProp(const std::string &propname, const double defval=0.0)
std::vector< std::string > getPropNames(const std::string &path=std::string(""))
void complain(const std::string &explain_string, const std::string &path, std::runtime_error &e, bool continue_p=true)
USRPPropTree(USRPPropTree &ptree, const std::string &path)
Constructor – build a property tree widget that is a subtree of the mboard property tree.
USRPPropTree(const uhd::usrp::multi_usrp::sptr usrp, const std::string &requester, int mb_index=0)
Constructor – build a property tree widget rooted at the first motherboard in this multi-usrp.
bool hasProperty(const std::string &propname)
does the property tree have this property name as a child?
T getProperty(const std::string &propname)