]>
Commit | Line | Data |
---|---|---|
c23c8659 ML |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013-2014 Martin Ling <martin-sigrok@earth.li> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
000f504f | 20 | /* Needed for isascii(), as used in the GNU libstdc++ headers */ |
79034d4f | 21 | /* Needed in strutil.c for POSIX.1-2008 locale functions */ |
000f504f | 22 | #ifndef _XOPEN_SOURCE |
79034d4f | 23 | #define _XOPEN_SOURCE 700 |
000f504f DE |
24 | #endif |
25 | ||
6ec6c43b | 26 | #include <config.h> |
b5f07319 | 27 | #include <libsigrokcxx/libsigrokcxx.hpp> |
c23c8659 | 28 | |
f36ca889 | 29 | #include <sstream> |
35114c33 | 30 | #include <cmath> |
f36ca889 | 31 | |
c23c8659 ML |
32 | namespace sigrok |
33 | { | |
34 | ||
c23c8659 ML |
35 | /** Helper function to translate C errors to C++ exceptions. */ |
36 | static void check(int result) | |
37 | { | |
38 | if (result != SR_OK) | |
39 | throw Error(result); | |
40 | } | |
41 | ||
42 | /** Helper function to obtain valid strings from possibly null input. */ | |
58e21229 | 43 | static inline const char *valid_string(const char *input) |
c23c8659 | 44 | { |
58e21229 | 45 | return (input) ? input : ""; |
c23c8659 ML |
46 | } |
47 | ||
58aa1f83 | 48 | /** Helper function to convert between map<string, VariantBase> and GHashTable */ |
d370545d | 49 | static GHashTable *map_to_hash_variant(const map<string, Glib::VariantBase> &input) |
58aa1f83 | 50 | { |
f17b4546 | 51 | auto *const output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
ce3e1e61 | 52 | reinterpret_cast<GDestroyNotify>(&g_variant_unref)); |
f17b4546 | 53 | for (const auto &entry : input) |
58aa1f83 ML |
54 | g_hash_table_insert(output, |
55 | g_strdup(entry.first.c_str()), | |
56 | entry.second.gobj_copy()); | |
d370545d | 57 | return output; |
58aa1f83 ML |
58 | } |
59 | ||
c23c8659 ML |
60 | Error::Error(int result) : result(result) |
61 | { | |
62 | } | |
63 | ||
15914cdb | 64 | const char *Error::what() const noexcept |
c23c8659 ML |
65 | { |
66 | return sr_strerror(result); | |
67 | } | |
68 | ||
15914cdb | 69 | Error::~Error() noexcept |
c23c8659 ML |
70 | { |
71 | } | |
72 | ||
e2eaf858 DE |
73 | ResourceReader::~ResourceReader() |
74 | { | |
75 | } | |
76 | ||
77 | SR_PRIV int ResourceReader::open_callback(struct sr_resource *res, | |
0ab8e5d2 | 78 | const char *name, void *cb_data) noexcept |
e2eaf858 DE |
79 | { |
80 | try { | |
81 | auto *const reader = static_cast<ResourceReader*>(cb_data); | |
82 | reader->open(res, name); | |
83 | } catch (const Error &err) { | |
84 | return err.result; | |
85 | } catch (...) { | |
86 | return SR_ERR; | |
87 | } | |
88 | return SR_OK; | |
89 | } | |
90 | ||
0ab8e5d2 DE |
91 | SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, |
92 | void *cb_data) noexcept | |
e2eaf858 DE |
93 | { |
94 | try { | |
95 | auto *const reader = static_cast<ResourceReader*>(cb_data); | |
96 | reader->close(res); | |
97 | } catch (const Error &err) { | |
98 | return err.result; | |
99 | } catch (...) { | |
100 | return SR_ERR; | |
101 | } | |
102 | return SR_OK; | |
103 | } | |
104 | ||
32ba0d80 | 105 | SR_PRIV gssize ResourceReader::read_callback(const struct sr_resource *res, |
0ab8e5d2 | 106 | void *buf, size_t count, void *cb_data) noexcept |
e2eaf858 DE |
107 | { |
108 | try { | |
109 | auto *const reader = static_cast<ResourceReader*>(cb_data); | |
110 | return reader->read(res, buf, count); | |
111 | } catch (const Error &err) { | |
112 | return err.result; | |
113 | } catch (...) { | |
114 | return SR_ERR; | |
115 | } | |
116 | } | |
117 | ||
c23c8659 ML |
118 | shared_ptr<Context> Context::create() |
119 | { | |
a98729a7 | 120 | return shared_ptr<Context>{new Context{}, default_delete<Context>{}}; |
c23c8659 ML |
121 | } |
122 | ||
123 | Context::Context() : | |
d5d7b09e | 124 | _structure(nullptr), |
58e21229 | 125 | _session(nullptr) |
c23c8659 | 126 | { |
3b161085 | 127 | check(sr_init(&_structure)); |
90e89c2a | 128 | |
f17b4546 DE |
129 | if (struct sr_dev_driver **driver_list = sr_driver_list(_structure)) |
130 | for (int i = 0; driver_list[i]; i++) { | |
131 | unique_ptr<Driver> driver {new Driver{driver_list[i]}}; | |
b605edf3 | 132 | _drivers.emplace(driver->name(), move(driver)); |
f17b4546 DE |
133 | } |
134 | ||
135 | if (const struct sr_input_module **input_list = sr_input_list()) | |
136 | for (int i = 0; input_list[i]; i++) { | |
137 | unique_ptr<InputFormat> input {new InputFormat{input_list[i]}}; | |
b605edf3 | 138 | _input_formats.emplace(input->name(), move(input)); |
f17b4546 DE |
139 | } |
140 | ||
141 | if (const struct sr_output_module **output_list = sr_output_list()) | |
142 | for (int i = 0; output_list[i]; i++) { | |
143 | unique_ptr<OutputFormat> output {new OutputFormat{output_list[i]}}; | |
b605edf3 | 144 | _output_formats.emplace(output->name(), move(output)); |
f17b4546 | 145 | } |
c23c8659 ML |
146 | } |
147 | ||
3b161085 | 148 | string Context::package_version() |
c23c8659 ML |
149 | { |
150 | return sr_package_version_string_get(); | |
151 | } | |
152 | ||
3b161085 | 153 | string Context::lib_version() |
c23c8659 ML |
154 | { |
155 | return sr_lib_version_string_get(); | |
156 | } | |
157 | ||
3b161085 | 158 | map<string, shared_ptr<Driver>> Context::drivers() |
c23c8659 ML |
159 | { |
160 | map<string, shared_ptr<Driver>> result; | |
e7eb2968 | 161 | for (const auto &entry: _drivers) { |
f17b4546 DE |
162 | const auto &name = entry.first; |
163 | const auto &driver = entry.second; | |
b605edf3 | 164 | result.emplace(name, driver->share_owned_by(shared_from_this())); |
c23c8659 ML |
165 | } |
166 | return result; | |
167 | } | |
168 | ||
3b161085 | 169 | map<string, shared_ptr<InputFormat>> Context::input_formats() |
c23c8659 ML |
170 | { |
171 | map<string, shared_ptr<InputFormat>> result; | |
e7eb2968 | 172 | for (const auto &entry: _input_formats) { |
f17b4546 DE |
173 | const auto &name = entry.first; |
174 | const auto &input_format = entry.second; | |
b605edf3 | 175 | result.emplace(name, input_format->share_owned_by(shared_from_this())); |
c23c8659 ML |
176 | } |
177 | return result; | |
178 | } | |
179 | ||
092843eb GS |
180 | shared_ptr<InputFormat> Context::input_format_match(string filename) |
181 | { | |
182 | const struct sr_input *input; | |
183 | const struct sr_input_module *imod; | |
184 | int rc; | |
185 | ||
186 | /* | |
187 | * Have the input module looked up for the specified file. | |
188 | * Failed lookup (or "successful lookup" with an empty result) | |
189 | * are non-fatal. Free the sr_input that was created by the | |
190 | * lookup routine, but grab the input module kind and return an | |
191 | * InputFormat instance to the application. This works because | |
192 | * the application passes a filename, no input data got buffered | |
193 | * in the sr_input that we release. | |
194 | */ | |
195 | input = NULL; | |
196 | rc = sr_input_scan_file(filename.c_str(), &input); | |
197 | if (rc != SR_OK) | |
198 | return nullptr; | |
199 | if (!input) | |
200 | return nullptr; | |
201 | imod = sr_input_module_get(input); | |
202 | sr_input_free(input); | |
203 | return shared_ptr<InputFormat>{new InputFormat{imod}, default_delete<InputFormat>{}}; | |
204 | } | |
205 | ||
3b161085 | 206 | map<string, shared_ptr<OutputFormat>> Context::output_formats() |
c23c8659 ML |
207 | { |
208 | map<string, shared_ptr<OutputFormat>> result; | |
e7eb2968 | 209 | for (const auto &entry: _output_formats) { |
f17b4546 DE |
210 | const auto &name = entry.first; |
211 | const auto &output_format = entry.second; | |
b605edf3 | 212 | result.emplace(name, output_format->share_owned_by(shared_from_this())); |
c23c8659 ML |
213 | } |
214 | return result; | |
215 | } | |
216 | ||
217 | Context::~Context() | |
218 | { | |
3b161085 | 219 | check(sr_exit(_structure)); |
c23c8659 ML |
220 | } |
221 | ||
15bebf57 | 222 | const LogLevel *Context::log_level() const |
c23c8659 ML |
223 | { |
224 | return LogLevel::get(sr_log_loglevel_get()); | |
225 | } | |
226 | ||
227 | void Context::set_log_level(const LogLevel *level) | |
228 | { | |
3b161085 | 229 | check(sr_log_loglevel_set(level->id())); |
c23c8659 ML |
230 | } |
231 | ||
0ab8e5d2 DE |
232 | static int call_log_callback(void *cb_data, int loglevel, |
233 | const char *format, va_list args) noexcept | |
c23c8659 | 234 | { |
58e21229 DE |
235 | const unique_ptr<char, decltype(&g_free)> |
236 | message {g_strdup_vprintf(format, args), &g_free}; | |
c23c8659 | 237 | |
ce3e1e61 | 238 | auto *const callback = static_cast<LogCallbackFunction *>(cb_data); |
c23c8659 | 239 | |
e7eb2968 | 240 | try { |
ce3e1e61 | 241 | (*callback)(LogLevel::get(loglevel), message.get()); |
0875f11d | 242 | } catch (Error &e) { |
c23c8659 ML |
243 | return e.result; |
244 | } | |
245 | ||
246 | return SR_OK; | |
247 | } | |
248 | ||
249 | void Context::set_log_callback(LogCallbackFunction callback) | |
250 | { | |
d370545d | 251 | _log_callback = move(callback); |
3b161085 | 252 | check(sr_log_callback_set(call_log_callback, &_log_callback)); |
e2eaf858 | 253 | } |
c23c8659 ML |
254 | |
255 | void Context::set_log_callback_default() | |
256 | { | |
257 | check(sr_log_callback_set_default()); | |
3b161085 | 258 | _log_callback = nullptr; |
e2eaf858 DE |
259 | } |
260 | ||
261 | void Context::set_resource_reader(ResourceReader *reader) | |
262 | { | |
263 | if (reader) { | |
264 | check(sr_resource_set_hooks(_structure, | |
265 | &ResourceReader::open_callback, | |
266 | &ResourceReader::close_callback, | |
267 | &ResourceReader::read_callback, reader)); | |
268 | } else { | |
269 | check(sr_resource_set_hooks(_structure, | |
270 | nullptr, nullptr, nullptr, nullptr)); | |
271 | } | |
272 | } | |
c23c8659 ML |
273 | |
274 | shared_ptr<Session> Context::create_session() | |
275 | { | |
a98729a7 DE |
276 | return shared_ptr<Session>{new Session{shared_from_this()}, |
277 | default_delete<Session>{}}; | |
c23c8659 ML |
278 | } |
279 | ||
9fa5b426 ML |
280 | shared_ptr<UserDevice> Context::create_user_device( |
281 | string vendor, string model, string version) | |
282 | { | |
a98729a7 DE |
283 | return shared_ptr<UserDevice>{ |
284 | new UserDevice{move(vendor), move(model), move(version)}, | |
285 | default_delete<UserDevice>{}}; | |
9fa5b426 ML |
286 | } |
287 | ||
304be4a7 ML |
288 | shared_ptr<Packet> Context::create_header_packet(Glib::TimeVal start_time) |
289 | { | |
290 | auto header = g_new(struct sr_datafeed_header, 1); | |
291 | header->feed_version = 1; | |
292 | header->starttime.tv_sec = start_time.tv_sec; | |
293 | header->starttime.tv_usec = start_time.tv_usec; | |
294 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
295 | packet->type = SR_DF_HEADER; | |
296 | packet->payload = header; | |
a98729a7 DE |
297 | return shared_ptr<Packet>{new Packet{nullptr, packet}, |
298 | default_delete<Packet>{}}; | |
304be4a7 ML |
299 | } |
300 | ||
301 | shared_ptr<Packet> Context::create_meta_packet( | |
9e7176bd | 302 | map<const ConfigKey *, Glib::VariantBase> config) |
304be4a7 ML |
303 | { |
304 | auto meta = g_new0(struct sr_datafeed_meta, 1); | |
e7eb2968 | 305 | for (const auto &input : config) { |
f17b4546 DE |
306 | const auto &key = input.first; |
307 | const auto &value = input.second; | |
308 | auto *const output = g_new(struct sr_config, 1); | |
304be4a7 | 309 | output->key = key->id(); |
f17b4546 | 310 | output->data = value.gobj_copy(); |
304be4a7 ML |
311 | meta->config = g_slist_append(meta->config, output); |
312 | } | |
313 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
314 | packet->type = SR_DF_META; | |
315 | packet->payload = meta; | |
a98729a7 DE |
316 | return shared_ptr<Packet>{new Packet{nullptr, packet}, |
317 | default_delete<Packet>{}}; | |
304be4a7 ML |
318 | } |
319 | ||
320 | shared_ptr<Packet> Context::create_logic_packet( | |
321 | void *data_pointer, size_t data_length, unsigned int unit_size) | |
322 | { | |
323 | auto logic = g_new(struct sr_datafeed_logic, 1); | |
324 | logic->length = data_length; | |
325 | logic->unitsize = unit_size; | |
326 | logic->data = data_pointer; | |
327 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
328 | packet->type = SR_DF_LOGIC; | |
329 | packet->payload = logic; | |
a98729a7 | 330 | return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}}; |
304be4a7 ML |
331 | } |
332 | ||
dd13d47a | 333 | shared_ptr<Packet> Context::create_analog_packet( |
9e7176bd | 334 | vector<shared_ptr<Channel> > channels, |
895cbcdd | 335 | const float *data_pointer, unsigned int num_samples, const Quantity *mq, |
9e7176bd | 336 | const Unit *unit, vector<const QuantityFlag *> mqflags) |
304be4a7 | 337 | { |
dd13d47a UH |
338 | auto analog = g_new0(struct sr_datafeed_analog, 1); |
339 | auto meaning = g_new0(struct sr_analog_meaning, 1); | |
d9d57ceb SA |
340 | auto encoding = g_new0(struct sr_analog_encoding, 1); |
341 | auto spec = g_new0(struct sr_analog_spec, 1); | |
dd13d47a UH |
342 | |
343 | analog->meaning = meaning; | |
344 | ||
f17b4546 | 345 | for (const auto &channel : channels) |
dd13d47a | 346 | meaning->channels = g_slist_append(meaning->channels, channel->_structure); |
ce3e1e61 DE |
347 | meaning->mq = static_cast<sr_mq>(mq->id()); |
348 | meaning->unit = static_cast<sr_unit>(unit->id()); | |
9e7176bd | 349 | meaning->mqflags = static_cast<sr_mqflag>(QuantityFlag::mask_from_flags(move(mqflags))); |
d9d57ceb SA |
350 | |
351 | analog->encoding = encoding; | |
352 | ||
353 | encoding->unitsize = sizeof(float); | |
354 | encoding->is_signed = TRUE; | |
355 | encoding->is_float = TRUE; | |
356 | #ifdef WORDS_BIGENDIAN | |
357 | encoding->is_bigendian = TRUE; | |
358 | #else | |
359 | encoding->is_bigendian = FALSE; | |
360 | #endif | |
361 | encoding->digits = 0; | |
362 | encoding->is_digits_decimal = FALSE; | |
363 | encoding->scale.p = 1; | |
364 | encoding->scale.q = 1; | |
365 | encoding->offset.p = 0; | |
366 | encoding->offset.q = 1; | |
367 | ||
368 | analog->spec = spec; | |
369 | ||
370 | spec->spec_digits = 0; | |
371 | ||
372 | analog->num_samples = num_samples; | |
895cbcdd | 373 | analog->data = (float*)data_pointer; |
304be4a7 | 374 | auto packet = g_new(struct sr_datafeed_packet, 1); |
dd13d47a | 375 | packet->type = SR_DF_ANALOG; |
304be4a7 | 376 | packet->payload = analog; |
a98729a7 | 377 | return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}}; |
304be4a7 ML |
378 | } |
379 | ||
a9ed2eb0 ML |
380 | shared_ptr<Packet> Context::create_end_packet() |
381 | { | |
382 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
383 | packet->type = SR_DF_END; | |
384 | return shared_ptr<Packet>{new Packet{nullptr, packet}, | |
385 | default_delete<Packet>{}}; | |
386 | } | |
387 | ||
c23c8659 ML |
388 | shared_ptr<Session> Context::load_session(string filename) |
389 | { | |
a98729a7 DE |
390 | return shared_ptr<Session>{ |
391 | new Session{shared_from_this(), move(filename)}, | |
392 | default_delete<Session>{}}; | |
c23c8659 ML |
393 | } |
394 | ||
395 | shared_ptr<Trigger> Context::create_trigger(string name) | |
396 | { | |
a98729a7 DE |
397 | return shared_ptr<Trigger>{ |
398 | new Trigger{shared_from_this(), move(name)}, | |
399 | default_delete<Trigger>{}}; | |
c23c8659 ML |
400 | } |
401 | ||
ca3291e3 ML |
402 | shared_ptr<Input> Context::open_file(string filename) |
403 | { | |
f88c7373 BV |
404 | const struct sr_input *input; |
405 | ||
d4cf45e5 | 406 | check(sr_input_scan_file(filename.c_str(), &input)); |
a98729a7 DE |
407 | return shared_ptr<Input>{ |
408 | new Input{shared_from_this(), input}, | |
409 | default_delete<Input>{}}; | |
ca3291e3 ML |
410 | } |
411 | ||
412 | shared_ptr<Input> Context::open_stream(string header) | |
413 | { | |
f88c7373 BV |
414 | const struct sr_input *input; |
415 | ||
ca3291e3 | 416 | auto gstr = g_string_new(header.c_str()); |
f88c7373 BV |
417 | auto ret = sr_input_scan_buffer(gstr, &input); |
418 | g_string_free(gstr, true); | |
419 | check(ret); | |
a98729a7 DE |
420 | return shared_ptr<Input>{ |
421 | new Input{shared_from_this(), input}, | |
422 | default_delete<Input>{}}; | |
ca3291e3 ML |
423 | } |
424 | ||
15bebf57 | 425 | map<string, string> Context::serials(shared_ptr<Driver> driver) const |
24287ea9 | 426 | { |
58e21229 | 427 | GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr); |
24287ea9 AJ |
428 | map<string, string> serials; |
429 | ||
430 | for (GSList *serial = serial_list; serial; serial = serial->next) { | |
ce3e1e61 | 431 | auto *const port = static_cast<sr_serial_port *>(serial->data); |
24287ea9 AJ |
432 | serials[string(port->name)] = string(port->description); |
433 | } | |
434 | ||
ce3e1e61 DE |
435 | g_slist_free_full(serial_list, |
436 | reinterpret_cast<GDestroyNotify>(&sr_serial_free)); | |
24287ea9 AJ |
437 | return serials; |
438 | } | |
439 | ||
c23c8659 | 440 | Driver::Driver(struct sr_dev_driver *structure) : |
58e21229 | 441 | Configurable(structure, nullptr, nullptr), |
d5d7b09e | 442 | _structure(structure), |
3b161085 | 443 | _initialized(false) |
c23c8659 ML |
444 | { |
445 | } | |
446 | ||
447 | Driver::~Driver() | |
448 | { | |
c23c8659 ML |
449 | } |
450 | ||
15bebf57 | 451 | string Driver::name() const |
c23c8659 | 452 | { |
3b161085 | 453 | return valid_string(_structure->name); |
c23c8659 ML |
454 | } |
455 | ||
15bebf57 | 456 | string Driver::long_name() const |
c23c8659 | 457 | { |
3b161085 | 458 | return valid_string(_structure->longname); |
c23c8659 ML |
459 | } |
460 | ||
36bb818d ML |
461 | set<const ConfigKey *> Driver::scan_options() const |
462 | { | |
8141a032 | 463 | GArray *opts = sr_driver_scan_options_list(_structure); |
36bb818d | 464 | set<const ConfigKey *> result; |
116670b1 E |
465 | if (opts) { |
466 | for (guint i = 0; i < opts->len; i++) | |
467 | result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i))); | |
468 | g_array_free(opts, TRUE); | |
469 | } | |
36bb818d ML |
470 | return result; |
471 | } | |
472 | ||
c23c8659 | 473 | vector<shared_ptr<HardwareDevice>> Driver::scan( |
9e7176bd | 474 | map<const ConfigKey *, Glib::VariantBase> options) |
c23c8659 ML |
475 | { |
476 | /* Initialise the driver if not yet done. */ | |
e7eb2968 | 477 | if (!_initialized) { |
3b161085 ML |
478 | check(sr_driver_init(_parent->_structure, _structure)); |
479 | _initialized = true; | |
c23c8659 ML |
480 | } |
481 | ||
c23c8659 | 482 | /* Translate scan options to GSList of struct sr_config pointers. */ |
58e21229 | 483 | GSList *option_list = nullptr; |
e7eb2968 | 484 | for (const auto &entry : options) { |
f17b4546 DE |
485 | const auto &key = entry.first; |
486 | const auto &value = entry.second; | |
487 | auto *const config = g_new(struct sr_config, 1); | |
3b161085 | 488 | config->key = key->id(); |
f17b4546 | 489 | config->data = const_cast<GVariant*>(value.gobj()); |
c23c8659 ML |
490 | option_list = g_slist_append(option_list, config); |
491 | } | |
492 | ||
493 | /* Run scan. */ | |
3b161085 | 494 | GSList *device_list = sr_driver_scan(_structure, option_list); |
c23c8659 ML |
495 | |
496 | /* Free option list. */ | |
497 | g_slist_free_full(option_list, g_free); | |
498 | ||
a4e47454 | 499 | |
c23c8659 | 500 | /* Create device objects. */ |
a4e47454 | 501 | vector<shared_ptr<HardwareDevice>> result; |
e7eb2968 | 502 | for (GSList *device = device_list; device; device = device->next) { |
ce3e1e61 | 503 | auto *const sdi = static_cast<struct sr_dev_inst *>(device->data); |
a98729a7 DE |
504 | shared_ptr<HardwareDevice> hwdev { |
505 | new HardwareDevice{shared_from_this(), sdi}, | |
506 | default_delete<HardwareDevice>{}}; | |
507 | result.push_back(move(hwdev)); | |
c23c8659 ML |
508 | } |
509 | ||
510 | /* Free GSList returned from scan. */ | |
511 | g_slist_free(device_list); | |
512 | ||
c23c8659 ML |
513 | return result; |
514 | } | |
515 | ||
516 | Configurable::Configurable( | |
517 | struct sr_dev_driver *driver, | |
518 | struct sr_dev_inst *sdi, | |
519 | struct sr_channel_group *cg) : | |
520 | config_driver(driver), | |
521 | config_sdi(sdi), | |
522 | config_channel_group(cg) | |
523 | { | |
524 | } | |
525 | ||
526 | Configurable::~Configurable() | |
527 | { | |
528 | } | |
529 | ||
36bb818d ML |
530 | set<const ConfigKey *> Configurable::config_keys() const |
531 | { | |
532 | GArray *opts; | |
533 | set<const ConfigKey *> result; | |
534 | ||
535 | opts = sr_dev_options(config_driver, config_sdi, config_channel_group); | |
536 | ||
0221cbdd SA |
537 | if (opts) { |
538 | for (guint i = 0; i < opts->len; i++) | |
539 | result.insert(ConfigKey::get(g_array_index(opts, uint32_t, i))); | |
540 | g_array_free(opts, TRUE); | |
541 | } | |
36bb818d ML |
542 | |
543 | return result; | |
544 | } | |
545 | ||
15bebf57 | 546 | Glib::VariantBase Configurable::config_get(const ConfigKey *key) const |
c23c8659 ML |
547 | { |
548 | GVariant *data; | |
549 | check(sr_config_get( | |
550 | config_driver, config_sdi, config_channel_group, | |
3b161085 | 551 | key->id(), &data)); |
c23c8659 ML |
552 | return Glib::VariantBase(data); |
553 | } | |
554 | ||
d370545d | 555 | void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value) |
c23c8659 ML |
556 | { |
557 | check(sr_config_set( | |
558 | config_sdi, config_channel_group, | |
d370545d | 559 | key->id(), const_cast<GVariant*>(value.gobj()))); |
c23c8659 ML |
560 | } |
561 | ||
36bb818d | 562 | set<const Capability *> Configurable::config_capabilities(const ConfigKey *key) const |
c23c8659 | 563 | { |
0c697a4b UH |
564 | int caps = sr_dev_config_capabilities_list(config_sdi, |
565 | config_channel_group, key->id()); | |
c23c8659 | 566 | |
36bb818d | 567 | set<const Capability *> result; |
d54190a3 | 568 | |
36bb818d ML |
569 | for (auto cap: Capability::values()) |
570 | if (caps & cap->id()) | |
571 | result.insert(cap); | |
d54190a3 ML |
572 | |
573 | return result; | |
574 | } | |
575 | ||
d9eed47d | 576 | bool Configurable::config_check(const ConfigKey *key, |
36bb818d | 577 | const Capability *capability) const |
d9eed47d | 578 | { |
0c697a4b UH |
579 | int caps = sr_dev_config_capabilities_list(config_sdi, |
580 | config_channel_group, key->id()); | |
d9eed47d | 581 | |
36bb818d ML |
582 | return (caps & capability->id()); |
583 | } | |
d9eed47d | 584 | |
36bb818d ML |
585 | Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) const |
586 | { | |
587 | GVariant *data; | |
588 | check(sr_config_list( | |
589 | config_driver, config_sdi, config_channel_group, | |
590 | key->id(), &data)); | |
591 | return Glib::VariantContainerBase(data); | |
d9eed47d ML |
592 | } |
593 | ||
c23c8659 | 594 | Device::Device(struct sr_dev_inst *structure) : |
58e21229 | 595 | Configurable(sr_dev_inst_driver_get(structure), structure, nullptr), |
3b161085 | 596 | _structure(structure) |
c23c8659 | 597 | { |
e7eb2968 | 598 | for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) { |
f17b4546 DE |
599 | auto *const ch = static_cast<struct sr_channel *>(entry->data); |
600 | unique_ptr<Channel> channel {new Channel{ch}}; | |
b605edf3 | 601 | _channels.emplace(ch, move(channel)); |
c23c8659 | 602 | } |
6be7a7f2 | 603 | |
e7eb2968 | 604 | for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) { |
f17b4546 DE |
605 | auto *const cg = static_cast<struct sr_channel_group *>(entry->data); |
606 | unique_ptr<ChannelGroup> group {new ChannelGroup{this, cg}}; | |
b605edf3 | 607 | _channel_groups.emplace(group->name(), move(group)); |
6be7a7f2 | 608 | } |
c23c8659 ML |
609 | } |
610 | ||
611 | Device::~Device() | |
e7eb2968 GS |
612 | { |
613 | } | |
c23c8659 | 614 | |
15bebf57 | 615 | string Device::vendor() const |
c23c8659 | 616 | { |
80fe5247 | 617 | return valid_string(sr_dev_inst_vendor_get(_structure)); |
c23c8659 ML |
618 | } |
619 | ||
15bebf57 | 620 | string Device::model() const |
c23c8659 | 621 | { |
80fe5247 | 622 | return valid_string(sr_dev_inst_model_get(_structure)); |
c23c8659 ML |
623 | } |
624 | ||
15bebf57 | 625 | string Device::version() const |
c23c8659 | 626 | { |
80fe5247 | 627 | return valid_string(sr_dev_inst_version_get(_structure)); |
c23c8659 ML |
628 | } |
629 | ||
15bebf57 | 630 | string Device::serial_number() const |
d1075e5a | 631 | { |
80fe5247 | 632 | return valid_string(sr_dev_inst_sernum_get(_structure)); |
d1075e5a ML |
633 | } |
634 | ||
15bebf57 | 635 | string Device::connection_id() const |
d1075e5a | 636 | { |
80fe5247 | 637 | return valid_string(sr_dev_inst_connid_get(_structure)); |
d1075e5a ML |
638 | } |
639 | ||
3b161085 | 640 | vector<shared_ptr<Channel>> Device::channels() |
c23c8659 ML |
641 | { |
642 | vector<shared_ptr<Channel>> result; | |
ce3e1e61 DE |
643 | for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) { |
644 | auto *const ch = static_cast<struct sr_channel *>(channel->data); | |
b6ab954d | 645 | result.push_back(_channels[ch]->share_owned_by(get_shared_from_this())); |
ce3e1e61 | 646 | } |
c23c8659 ML |
647 | return result; |
648 | } | |
649 | ||
4178d971 ML |
650 | shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr) |
651 | { | |
b6ab954d | 652 | return _channels[ptr]->share_owned_by(get_shared_from_this()); |
4178d971 ML |
653 | } |
654 | ||
6be7a7f2 | 655 | map<string, shared_ptr<ChannelGroup>> |
3b161085 | 656 | Device::channel_groups() |
6be7a7f2 ML |
657 | { |
658 | map<string, shared_ptr<ChannelGroup>> result; | |
e7eb2968 | 659 | for (const auto &entry: _channel_groups) { |
f17b4546 DE |
660 | const auto &name = entry.first; |
661 | const auto &channel_group = entry.second; | |
b605edf3 | 662 | result.emplace(name, channel_group->share_owned_by(get_shared_from_this())); |
6be7a7f2 ML |
663 | } |
664 | return result; | |
665 | } | |
666 | ||
c23c8659 ML |
667 | void Device::open() |
668 | { | |
3b161085 | 669 | check(sr_dev_open(_structure)); |
c23c8659 ML |
670 | } |
671 | ||
672 | void Device::close() | |
673 | { | |
3b161085 | 674 | check(sr_dev_close(_structure)); |
c23c8659 ML |
675 | } |
676 | ||
a4e47454 ML |
677 | HardwareDevice::HardwareDevice(shared_ptr<Driver> driver, |
678 | struct sr_dev_inst *structure) : | |
c23c8659 | 679 | Device(structure), |
d370545d | 680 | _driver(move(driver)) |
c23c8659 | 681 | { |
c23c8659 ML |
682 | } |
683 | ||
684 | HardwareDevice::~HardwareDevice() | |
685 | { | |
c23c8659 ML |
686 | } |
687 | ||
d01d2314 ML |
688 | shared_ptr<Device> HardwareDevice::get_shared_from_this() |
689 | { | |
bf52cc8c | 690 | return static_pointer_cast<Device>(shared_from_this()); |
d01d2314 ML |
691 | } |
692 | ||
3b161085 | 693 | shared_ptr<Driver> HardwareDevice::driver() |
c23c8659 | 694 | { |
a4e47454 | 695 | return _driver; |
c23c8659 ML |
696 | } |
697 | ||
9fa5b426 | 698 | UserDevice::UserDevice(string vendor, string model, string version) : |
d5d7b09e DE |
699 | Device(sr_dev_inst_user_new( |
700 | vendor.c_str(), model.c_str(), version.c_str())) | |
9fa5b426 ML |
701 | { |
702 | } | |
703 | ||
704 | UserDevice::~UserDevice() | |
705 | { | |
706 | } | |
707 | ||
708 | shared_ptr<Device> UserDevice::get_shared_from_this() | |
709 | { | |
710 | return static_pointer_cast<Device>(shared_from_this()); | |
711 | } | |
712 | ||
713 | shared_ptr<Channel> UserDevice::add_channel(unsigned int index, | |
714 | const ChannelType *type, string name) | |
715 | { | |
716 | check(sr_dev_inst_channel_add(Device::_structure, | |
717 | index, type->id(), name.c_str())); | |
ce3e1e61 DE |
718 | GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure)); |
719 | auto *const ch = static_cast<struct sr_channel *>(last->data); | |
f17b4546 | 720 | unique_ptr<Channel> channel {new Channel{ch}}; |
b605edf3 | 721 | _channels.emplace(ch, move(channel)); |
ce3e1e61 | 722 | return get_channel(ch); |
9fa5b426 ML |
723 | } |
724 | ||
c23c8659 | 725 | Channel::Channel(struct sr_channel *structure) : |
d5d7b09e | 726 | _structure(structure), |
3b161085 | 727 | _type(ChannelType::get(_structure->type)) |
c23c8659 ML |
728 | { |
729 | } | |
730 | ||
731 | Channel::~Channel() | |
732 | { | |
733 | } | |
734 | ||
15bebf57 | 735 | string Channel::name() const |
c23c8659 | 736 | { |
3b161085 | 737 | return valid_string(_structure->name); |
c23c8659 ML |
738 | } |
739 | ||
740 | void Channel::set_name(string name) | |
741 | { | |
6f1346fb | 742 | check(sr_dev_channel_name_set(_structure, name.c_str())); |
c23c8659 ML |
743 | } |
744 | ||
15bebf57 | 745 | const ChannelType *Channel::type() const |
c23c8659 | 746 | { |
3b161085 | 747 | return ChannelType::get(_structure->type); |
c23c8659 ML |
748 | } |
749 | ||
15bebf57 | 750 | bool Channel::enabled() const |
c23c8659 | 751 | { |
3b161085 | 752 | return _structure->enabled; |
c23c8659 ML |
753 | } |
754 | ||
755 | void Channel::set_enabled(bool value) | |
756 | { | |
6f1346fb | 757 | check(sr_dev_channel_enable(_structure, value)); |
c23c8659 ML |
758 | } |
759 | ||
15bebf57 | 760 | unsigned int Channel::index() const |
06bd935e | 761 | { |
3b161085 | 762 | return _structure->index; |
06bd935e ML |
763 | } |
764 | ||
f17b4546 | 765 | ChannelGroup::ChannelGroup(const Device *device, |
c23c8659 | 766 | struct sr_channel_group *structure) : |
80fe5247 | 767 | Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure) |
c23c8659 | 768 | { |
d5d7b09e | 769 | for (GSList *entry = config_channel_group->channels; entry; entry = entry->next) { |
ce3e1e61 | 770 | auto *const ch = static_cast<struct sr_channel *>(entry->data); |
f17b4546 DE |
771 | /* Note: This relies on Device::_channels to keep the Channel |
772 | * objects around over the lifetime of the ChannelGroup. */ | |
773 | _channels.push_back(device->_channels.find(ch)->second.get()); | |
ce3e1e61 | 774 | } |
c23c8659 ML |
775 | } |
776 | ||
777 | ChannelGroup::~ChannelGroup() | |
778 | { | |
779 | } | |
780 | ||
15bebf57 | 781 | string ChannelGroup::name() const |
c23c8659 | 782 | { |
d5d7b09e | 783 | return valid_string(config_channel_group->name); |
c23c8659 ML |
784 | } |
785 | ||
3b161085 | 786 | vector<shared_ptr<Channel>> ChannelGroup::channels() |
c23c8659 ML |
787 | { |
788 | vector<shared_ptr<Channel>> result; | |
f17b4546 | 789 | for (const auto &channel : _channels) |
b6ab954d | 790 | result.push_back(channel->share_owned_by(_parent)); |
c23c8659 ML |
791 | return result; |
792 | } | |
793 | ||
176d785d | 794 | Trigger::Trigger(shared_ptr<Context> context, string name) : |
d5d7b09e | 795 | _structure(sr_trigger_new(name.c_str())), |
d370545d | 796 | _context(move(context)) |
c23c8659 | 797 | { |
f17b4546 DE |
798 | for (auto *stage = _structure->stages; stage; stage = stage->next) { |
799 | unique_ptr<TriggerStage> ts {new TriggerStage{ | |
800 | static_cast<struct sr_trigger_stage *>(stage->data)}}; | |
801 | _stages.push_back(move(ts)); | |
802 | } | |
c23c8659 ML |
803 | } |
804 | ||
805 | Trigger::~Trigger() | |
806 | { | |
3b161085 | 807 | sr_trigger_free(_structure); |
c23c8659 ML |
808 | } |
809 | ||
15bebf57 | 810 | string Trigger::name() const |
c23c8659 | 811 | { |
3b161085 | 812 | return _structure->name; |
c23c8659 ML |
813 | } |
814 | ||
3b161085 | 815 | vector<shared_ptr<TriggerStage>> Trigger::stages() |
c23c8659 ML |
816 | { |
817 | vector<shared_ptr<TriggerStage>> result; | |
f17b4546 | 818 | for (const auto &stage : _stages) |
b6ab954d | 819 | result.push_back(stage->share_owned_by(shared_from_this())); |
c23c8659 ML |
820 | return result; |
821 | } | |
822 | ||
823 | shared_ptr<TriggerStage> Trigger::add_stage() | |
824 | { | |
f17b4546 DE |
825 | unique_ptr<TriggerStage> stage {new TriggerStage{sr_trigger_stage_add(_structure)}}; |
826 | _stages.push_back(move(stage)); | |
827 | return _stages.back()->share_owned_by(shared_from_this()); | |
c23c8659 ML |
828 | } |
829 | ||
d5d7b09e DE |
830 | TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : |
831 | _structure(structure) | |
c23c8659 ML |
832 | { |
833 | } | |
834 | ||
835 | TriggerStage::~TriggerStage() | |
836 | { | |
c23c8659 | 837 | } |
176d785d | 838 | |
15bebf57 | 839 | int TriggerStage::number() const |
c23c8659 | 840 | { |
3b161085 | 841 | return _structure->stage; |
c23c8659 ML |
842 | } |
843 | ||
3b161085 | 844 | vector<shared_ptr<TriggerMatch>> TriggerStage::matches() |
c23c8659 ML |
845 | { |
846 | vector<shared_ptr<TriggerMatch>> result; | |
f17b4546 | 847 | for (const auto &match : _matches) |
b6ab954d | 848 | result.push_back(match->share_owned_by(shared_from_this())); |
c23c8659 ML |
849 | return result; |
850 | } | |
851 | ||
3b161085 ML |
852 | void TriggerStage::add_match(shared_ptr<Channel> channel, |
853 | const TriggerMatchType *type, float value) | |
c23c8659 | 854 | { |
3b161085 ML |
855 | check(sr_trigger_match_add(_structure, |
856 | channel->_structure, type->id(), value)); | |
ce3e1e61 | 857 | GSList *const last = g_slist_last(_structure->matches); |
f17b4546 DE |
858 | unique_ptr<TriggerMatch> match {new TriggerMatch{ |
859 | static_cast<struct sr_trigger_match *>(last->data), | |
860 | move(channel)}}; | |
861 | _matches.push_back(move(match)); | |
c23c8659 ML |
862 | } |
863 | ||
3b161085 ML |
864 | void TriggerStage::add_match(shared_ptr<Channel> channel, |
865 | const TriggerMatchType *type) | |
c23c8659 | 866 | { |
d370545d | 867 | add_match(move(channel), type, NAN); |
c23c8659 ML |
868 | } |
869 | ||
3b161085 ML |
870 | TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, |
871 | shared_ptr<Channel> channel) : | |
d5d7b09e | 872 | _structure(structure), |
d370545d | 873 | _channel(move(channel)) |
c23c8659 ML |
874 | { |
875 | } | |
876 | ||
877 | TriggerMatch::~TriggerMatch() | |
878 | { | |
879 | } | |
880 | ||
3b161085 | 881 | shared_ptr<Channel> TriggerMatch::channel() |
c23c8659 | 882 | { |
3b161085 | 883 | return _channel; |
c23c8659 ML |
884 | } |
885 | ||
15bebf57 | 886 | const TriggerMatchType *TriggerMatch::type() const |
c23c8659 | 887 | { |
3b161085 | 888 | return TriggerMatchType::get(_structure->match); |
c23c8659 ML |
889 | } |
890 | ||
15bebf57 | 891 | float TriggerMatch::value() const |
c23c8659 | 892 | { |
3b161085 | 893 | return _structure->value; |
c23c8659 ML |
894 | } |
895 | ||
896 | DatafeedCallbackData::DatafeedCallbackData(Session *session, | |
897 | DatafeedCallbackFunction callback) : | |
d370545d | 898 | _callback(move(callback)), |
3b161085 | 899 | _session(session) |
c23c8659 ML |
900 | { |
901 | } | |
902 | ||
903 | void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, | |
904 | const struct sr_datafeed_packet *pkt) | |
905 | { | |
ca4e307a | 906 | auto device = _session->get_device(sdi); |
a98729a7 | 907 | shared_ptr<Packet> packet {new Packet{device, pkt}, default_delete<Packet>{}}; |
d370545d | 908 | _callback(move(device), move(packet)); |
c23c8659 ML |
909 | } |
910 | ||
cac58676 | 911 | SessionDevice::SessionDevice(struct sr_dev_inst *structure) : |
cac58676 ML |
912 | Device(structure) |
913 | { | |
914 | } | |
915 | ||
916 | SessionDevice::~SessionDevice() | |
917 | { | |
918 | } | |
919 | ||
920 | shared_ptr<Device> SessionDevice::get_shared_from_this() | |
921 | { | |
922 | return static_pointer_cast<Device>(shared_from_this()); | |
923 | } | |
924 | ||
c23c8659 | 925 | Session::Session(shared_ptr<Context> context) : |
d5d7b09e | 926 | _structure(nullptr), |
d370545d | 927 | _context(move(context)) |
c23c8659 | 928 | { |
d370545d | 929 | check(sr_session_new(_context->_structure, &_structure)); |
3b161085 | 930 | _context->_session = this; |
c23c8659 ML |
931 | } |
932 | ||
933 | Session::Session(shared_ptr<Context> context, string filename) : | |
d5d7b09e | 934 | _structure(nullptr), |
d370545d DE |
935 | _context(move(context)), |
936 | _filename(move(filename)) | |
c23c8659 | 937 | { |
d370545d | 938 | check(sr_session_load(_context->_structure, _filename.c_str(), &_structure)); |
cac58676 | 939 | GSList *dev_list; |
3b161085 | 940 | check(sr_session_dev_list(_structure, &dev_list)); |
ce3e1e61 DE |
941 | for (GSList *dev = dev_list; dev; dev = dev->next) { |
942 | auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data); | |
f17b4546 | 943 | unique_ptr<SessionDevice> device {new SessionDevice{sdi}}; |
b605edf3 | 944 | _owned_devices.emplace(sdi, move(device)); |
cac58676 | 945 | } |
3b161085 | 946 | _context->_session = this; |
3940abcb | 947 | g_slist_free(dev_list); |
c23c8659 ML |
948 | } |
949 | ||
950 | Session::~Session() | |
951 | { | |
3b161085 | 952 | check(sr_session_destroy(_structure)); |
ca4e307a ML |
953 | } |
954 | ||
955 | shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi) | |
956 | { | |
957 | if (_owned_devices.count(sdi)) | |
958 | return static_pointer_cast<Device>( | |
b6ab954d | 959 | _owned_devices[sdi]->share_owned_by(shared_from_this())); |
ca4e307a ML |
960 | else if (_other_devices.count(sdi)) |
961 | return _other_devices[sdi]; | |
962 | else | |
963 | throw Error(SR_ERR_BUG); | |
c23c8659 ML |
964 | } |
965 | ||
966 | void Session::add_device(shared_ptr<Device> device) | |
967 | { | |
d370545d DE |
968 | const auto dev_struct = device->_structure; |
969 | check(sr_session_dev_add(_structure, dev_struct)); | |
970 | _other_devices[dev_struct] = move(device); | |
c23c8659 ML |
971 | } |
972 | ||
3b161085 | 973 | vector<shared_ptr<Device>> Session::devices() |
c23c8659 ML |
974 | { |
975 | GSList *dev_list; | |
3b161085 | 976 | check(sr_session_dev_list(_structure, &dev_list)); |
c23c8659 | 977 | vector<shared_ptr<Device>> result; |
ce3e1e61 DE |
978 | for (GSList *dev = dev_list; dev; dev = dev->next) { |
979 | auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data); | |
ca4e307a | 980 | result.push_back(get_device(sdi)); |
c23c8659 | 981 | } |
3940abcb | 982 | g_slist_free(dev_list); |
c23c8659 ML |
983 | return result; |
984 | } | |
985 | ||
986 | void Session::remove_devices() | |
987 | { | |
ca4e307a | 988 | _other_devices.clear(); |
3b161085 | 989 | check(sr_session_dev_remove_all(_structure)); |
c23c8659 ML |
990 | } |
991 | ||
992 | void Session::start() | |
993 | { | |
3b161085 | 994 | check(sr_session_start(_structure)); |
c23c8659 ML |
995 | } |
996 | ||
997 | void Session::run() | |
998 | { | |
3b161085 | 999 | check(sr_session_run(_structure)); |
c23c8659 ML |
1000 | } |
1001 | ||
1002 | void Session::stop() | |
1003 | { | |
3b161085 | 1004 | check(sr_session_stop(_structure)); |
c23c8659 ML |
1005 | } |
1006 | ||
f91cf612 DE |
1007 | bool Session::is_running() const |
1008 | { | |
1009 | const int ret = sr_session_is_running(_structure); | |
1010 | if (ret < 0) | |
1011 | throw Error{ret}; | |
1012 | return (ret != 0); | |
1013 | } | |
1014 | ||
0ab8e5d2 | 1015 | static void session_stopped_callback(void *data) noexcept |
f91cf612 DE |
1016 | { |
1017 | auto *const callback = static_cast<SessionStoppedCallback*>(data); | |
1018 | (*callback)(); | |
1019 | } | |
1020 | ||
1021 | void Session::set_stopped_callback(SessionStoppedCallback callback) | |
1022 | { | |
1023 | _stopped_callback = move(callback); | |
1024 | if (_stopped_callback) | |
1025 | check(sr_session_stopped_callback_set(_structure, | |
1026 | &session_stopped_callback, &_stopped_callback)); | |
1027 | else | |
1028 | check(sr_session_stopped_callback_set(_structure, | |
1029 | nullptr, nullptr)); | |
1030 | } | |
1031 | ||
c23c8659 | 1032 | static void datafeed_callback(const struct sr_dev_inst *sdi, |
0ab8e5d2 | 1033 | const struct sr_datafeed_packet *pkt, void *cb_data) noexcept |
c23c8659 ML |
1034 | { |
1035 | auto callback = static_cast<DatafeedCallbackData *>(cb_data); | |
1036 | callback->run(sdi, pkt); | |
1037 | } | |
ce3e1e61 | 1038 | |
c23c8659 ML |
1039 | void Session::add_datafeed_callback(DatafeedCallbackFunction callback) |
1040 | { | |
f17b4546 DE |
1041 | unique_ptr<DatafeedCallbackData> cb_data |
1042 | {new DatafeedCallbackData{this, move(callback)}}; | |
3b161085 | 1043 | check(sr_session_datafeed_callback_add(_structure, |
f17b4546 DE |
1044 | &datafeed_callback, cb_data.get())); |
1045 | _datafeed_callbacks.push_back(move(cb_data)); | |
c23c8659 ML |
1046 | } |
1047 | ||
d370545d | 1048 | void Session::remove_datafeed_callbacks() |
c23c8659 | 1049 | { |
3b161085 | 1050 | check(sr_session_datafeed_callback_remove_all(_structure)); |
3b161085 | 1051 | _datafeed_callbacks.clear(); |
c23c8659 ML |
1052 | } |
1053 | ||
3b161085 | 1054 | shared_ptr<Trigger> Session::trigger() |
6fa0eb86 | 1055 | { |
3b161085 | 1056 | return _trigger; |
6fa0eb86 ML |
1057 | } |
1058 | ||
1059 | void Session::set_trigger(shared_ptr<Trigger> trigger) | |
1060 | { | |
e835e808 UH |
1061 | if (!trigger) |
1062 | // Set NULL trigger, i.e. remove any trigger from the session. | |
58e21229 | 1063 | check(sr_session_trigger_set(_structure, nullptr)); |
e835e808 UH |
1064 | else |
1065 | check(sr_session_trigger_set(_structure, trigger->_structure)); | |
d370545d | 1066 | _trigger = move(trigger); |
6fa0eb86 ML |
1067 | } |
1068 | ||
15bebf57 | 1069 | string Session::filename() const |
1411f7d8 ML |
1070 | { |
1071 | return _filename; | |
1072 | } | |
1073 | ||
624d1610 UH |
1074 | shared_ptr<Context> Session::context() |
1075 | { | |
1076 | return _context; | |
1077 | } | |
1078 | ||
2928f47d ML |
1079 | Packet::Packet(shared_ptr<Device> device, |
1080 | const struct sr_datafeed_packet *structure) : | |
d5d7b09e | 1081 | _structure(structure), |
d370545d | 1082 | _device(move(device)) |
c23c8659 ML |
1083 | { |
1084 | switch (structure->type) | |
1085 | { | |
2928f47d | 1086 | case SR_DF_HEADER: |
f17b4546 | 1087 | _payload.reset(new Header{ |
2928f47d | 1088 | static_cast<const struct sr_datafeed_header *>( |
f17b4546 | 1089 | structure->payload)}); |
2928f47d ML |
1090 | break; |
1091 | case SR_DF_META: | |
f17b4546 | 1092 | _payload.reset(new Meta{ |
2928f47d | 1093 | static_cast<const struct sr_datafeed_meta *>( |
f17b4546 | 1094 | structure->payload)}); |
2928f47d | 1095 | break; |
c23c8659 | 1096 | case SR_DF_LOGIC: |
f17b4546 | 1097 | _payload.reset(new Logic{ |
c23c8659 | 1098 | static_cast<const struct sr_datafeed_logic *>( |
f17b4546 | 1099 | structure->payload)}); |
c23c8659 | 1100 | break; |
dd13d47a | 1101 | case SR_DF_ANALOG: |
f17b4546 | 1102 | _payload.reset(new Analog{ |
dd13d47a | 1103 | static_cast<const struct sr_datafeed_analog *>( |
f17b4546 | 1104 | structure->payload)}); |
4cd883a7 | 1105 | break; |
c23c8659 ML |
1106 | } |
1107 | } | |
1108 | ||
1109 | Packet::~Packet() | |
1110 | { | |
c23c8659 ML |
1111 | } |
1112 | ||
15bebf57 | 1113 | const PacketType *Packet::type() const |
90ba83f2 | 1114 | { |
3b161085 | 1115 | return PacketType::get(_structure->type); |
90ba83f2 ML |
1116 | } |
1117 | ||
3b161085 | 1118 | shared_ptr<PacketPayload> Packet::payload() |
c23c8659 | 1119 | { |
3b161085 | 1120 | if (_payload) |
b6ab954d | 1121 | return _payload->share_owned_by(shared_from_this()); |
4cd883a7 ML |
1122 | else |
1123 | throw Error(SR_ERR_NA); | |
c23c8659 ML |
1124 | } |
1125 | ||
1126 | PacketPayload::PacketPayload() | |
1127 | { | |
1128 | } | |
1129 | ||
1130 | PacketPayload::~PacketPayload() | |
1131 | { | |
1132 | } | |
1133 | ||
2928f47d | 1134 | Header::Header(const struct sr_datafeed_header *structure) : |
d5d7b09e DE |
1135 | PacketPayload(), |
1136 | _structure(structure) | |
2928f47d ML |
1137 | { |
1138 | } | |
1139 | ||
1140 | Header::~Header() | |
1141 | { | |
1142 | } | |
1143 | ||
b6ab954d | 1144 | shared_ptr<PacketPayload> Header::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1145 | { |
4f7bcf0e | 1146 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1147 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1148 | } |
1149 | ||
15bebf57 | 1150 | int Header::feed_version() const |
2928f47d | 1151 | { |
3b161085 | 1152 | return _structure->feed_version; |
2928f47d ML |
1153 | } |
1154 | ||
15bebf57 | 1155 | Glib::TimeVal Header::start_time() const |
2928f47d ML |
1156 | { |
1157 | return Glib::TimeVal( | |
3b161085 ML |
1158 | _structure->starttime.tv_sec, |
1159 | _structure->starttime.tv_usec); | |
2928f47d ML |
1160 | } |
1161 | ||
1162 | Meta::Meta(const struct sr_datafeed_meta *structure) : | |
d5d7b09e DE |
1163 | PacketPayload(), |
1164 | _structure(structure) | |
2928f47d ML |
1165 | { |
1166 | } | |
1167 | ||
1168 | Meta::~Meta() | |
1169 | { | |
1170 | } | |
1171 | ||
b6ab954d | 1172 | shared_ptr<PacketPayload> Meta::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1173 | { |
4f7bcf0e | 1174 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1175 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1176 | } |
1177 | ||
15bebf57 | 1178 | map<const ConfigKey *, Glib::VariantBase> Meta::config() const |
2928f47d ML |
1179 | { |
1180 | map<const ConfigKey *, Glib::VariantBase> result; | |
ce3e1e61 DE |
1181 | for (auto l = _structure->config; l; l = l->next) { |
1182 | auto *const config = static_cast<struct sr_config *>(l->data); | |
7cccc915 | 1183 | result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data, true); |
2928f47d ML |
1184 | } |
1185 | return result; | |
1186 | } | |
1187 | ||
1188 | Logic::Logic(const struct sr_datafeed_logic *structure) : | |
d5d7b09e DE |
1189 | PacketPayload(), |
1190 | _structure(structure) | |
2928f47d ML |
1191 | { |
1192 | } | |
c23c8659 ML |
1193 | |
1194 | Logic::~Logic() | |
1195 | { | |
1196 | } | |
1197 | ||
b6ab954d | 1198 | shared_ptr<PacketPayload> Logic::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1199 | { |
4f7bcf0e | 1200 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1201 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1202 | } |
1203 | ||
3b161085 | 1204 | void *Logic::data_pointer() |
c23c8659 | 1205 | { |
3b161085 | 1206 | return _structure->data; |
c23c8659 ML |
1207 | } |
1208 | ||
15bebf57 | 1209 | size_t Logic::data_length() const |
c23c8659 | 1210 | { |
3b161085 | 1211 | return _structure->length; |
c23c8659 ML |
1212 | } |
1213 | ||
15bebf57 | 1214 | unsigned int Logic::unit_size() const |
2928f47d | 1215 | { |
3b161085 | 1216 | return _structure->unitsize; |
2928f47d ML |
1217 | } |
1218 | ||
dd13d47a | 1219 | Analog::Analog(const struct sr_datafeed_analog *structure) : |
d5d7b09e DE |
1220 | PacketPayload(), |
1221 | _structure(structure) | |
c23c8659 ML |
1222 | { |
1223 | } | |
1224 | ||
dd13d47a | 1225 | Analog::~Analog() |
c23c8659 ML |
1226 | { |
1227 | } | |
1228 | ||
b6ab954d | 1229 | shared_ptr<PacketPayload> Analog::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1230 | { |
4f7bcf0e | 1231 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1232 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1233 | } |
1234 | ||
dd13d47a | 1235 | void *Analog::data_pointer() |
c23c8659 | 1236 | { |
3b161085 | 1237 | return _structure->data; |
c23c8659 ML |
1238 | } |
1239 | ||
c5d081f7 SA |
1240 | void Analog::get_data_as_float(float *dest) |
1241 | { | |
1242 | check(sr_analog_to_float(_structure, dest)); | |
1243 | } | |
1244 | ||
15bebf57 | 1245 | unsigned int Analog::num_samples() const |
c23c8659 | 1246 | { |
3b161085 | 1247 | return _structure->num_samples; |
c23c8659 ML |
1248 | } |
1249 | ||
dd13d47a | 1250 | vector<shared_ptr<Channel>> Analog::channels() |
c23c8659 | 1251 | { |
2928f47d | 1252 | vector<shared_ptr<Channel>> result; |
ce3e1e61 DE |
1253 | for (auto l = _structure->meaning->channels; l; l = l->next) { |
1254 | auto *const ch = static_cast<struct sr_channel *>(l->data); | |
1255 | result.push_back(_parent->_device->get_channel(ch)); | |
1256 | } | |
2928f47d | 1257 | return result; |
c23c8659 ML |
1258 | } |
1259 | ||
0cee3a3e SA |
1260 | unsigned int Analog::unitsize() const |
1261 | { | |
1262 | return _structure->encoding->unitsize; | |
1263 | } | |
1264 | ||
1265 | bool Analog::is_signed() const | |
1266 | { | |
1267 | return _structure->encoding->is_signed; | |
1268 | } | |
1269 | ||
1270 | bool Analog::is_float() const | |
1271 | { | |
1272 | return _structure->encoding->is_float; | |
1273 | } | |
1274 | ||
1275 | bool Analog::is_bigendian() const | |
1276 | { | |
1277 | return _structure->encoding->is_bigendian; | |
1278 | } | |
1279 | ||
1280 | int Analog::digits() const | |
1281 | { | |
1282 | return _structure->encoding->digits; | |
1283 | } | |
1284 | ||
1285 | bool Analog::is_digits_decimal() const | |
1286 | { | |
1287 | return _structure->encoding->is_digits_decimal; | |
1288 | } | |
1289 | ||
1290 | shared_ptr<Rational> Analog::scale() | |
1291 | { | |
1292 | unique_ptr<Rational> scale; | |
1293 | scale.reset(new Rational(&(_structure->encoding->scale))); | |
1294 | ||
1295 | if (scale) | |
1296 | return scale->share_owned_by(shared_from_this()); | |
1297 | else | |
1298 | throw Error(SR_ERR_NA); | |
1299 | } | |
1300 | ||
1301 | shared_ptr<Rational> Analog::offset() | |
1302 | { | |
1303 | unique_ptr<Rational> offset; | |
1304 | offset.reset(new Rational(&(_structure->encoding->offset))); | |
1305 | ||
1306 | if (offset) | |
1307 | return offset->share_owned_by(shared_from_this()); | |
1308 | else | |
1309 | throw Error(SR_ERR_NA); | |
1310 | } | |
1311 | ||
15bebf57 | 1312 | const Quantity *Analog::mq() const |
c23c8659 | 1313 | { |
dd13d47a | 1314 | return Quantity::get(_structure->meaning->mq); |
c23c8659 ML |
1315 | } |
1316 | ||
15bebf57 | 1317 | const Unit *Analog::unit() const |
c23c8659 | 1318 | { |
dd13d47a | 1319 | return Unit::get(_structure->meaning->unit); |
c23c8659 ML |
1320 | } |
1321 | ||
15bebf57 | 1322 | vector<const QuantityFlag *> Analog::mq_flags() const |
c23c8659 | 1323 | { |
dd13d47a | 1324 | return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); |
c23c8659 ML |
1325 | } |
1326 | ||
6ad2fbaa SA |
1327 | shared_ptr<Logic> Analog::get_logic_via_threshold(float threshold, |
1328 | uint8_t *data_ptr) const | |
1329 | { | |
1330 | auto datafeed = g_new(struct sr_datafeed_logic, 1); | |
1331 | datafeed->length = num_samples(); | |
1332 | datafeed->unitsize = 1; | |
1333 | ||
1334 | if (data_ptr) | |
1335 | datafeed->data = data_ptr; | |
1336 | else | |
1337 | datafeed->data = g_malloc(datafeed->length); | |
1338 | ||
1339 | shared_ptr<Logic> logic = | |
1340 | shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}}; | |
1341 | ||
1342 | check(sr_a2l_threshold(_structure, threshold, | |
1343 | (uint8_t*)datafeed->data, datafeed->length)); | |
1344 | ||
1345 | return logic; | |
1346 | } | |
1347 | ||
1348 | shared_ptr<Logic> Analog::get_logic_via_schmitt_trigger(float lo_thr, | |
1349 | float hi_thr, uint8_t *state, uint8_t *data_ptr) const | |
1350 | { | |
1351 | auto datafeed = g_new(struct sr_datafeed_logic, 1); | |
1352 | datafeed->length = num_samples(); | |
1353 | datafeed->unitsize = 1; | |
1354 | ||
1355 | if (data_ptr) | |
1356 | datafeed->data = data_ptr; | |
1357 | else | |
1358 | datafeed->data = g_malloc(datafeed->length); | |
1359 | ||
1360 | shared_ptr<Logic> logic = | |
1361 | shared_ptr<Logic>{new Logic{datafeed}, default_delete<Logic>{}}; | |
1362 | ||
1363 | check(sr_a2l_schmitt_trigger(_structure, lo_thr, hi_thr, state, | |
1364 | (uint8_t*)datafeed->data, datafeed->length)); | |
1365 | ||
1366 | return logic; | |
1367 | } | |
1368 | ||
0cee3a3e SA |
1369 | Rational::Rational(const struct sr_rational *structure) : |
1370 | _structure(structure) | |
1371 | { | |
1372 | } | |
1373 | ||
1374 | Rational::~Rational() | |
1375 | { | |
1376 | } | |
1377 | ||
1378 | shared_ptr<Rational> Rational::share_owned_by(shared_ptr<Analog> _parent) | |
1379 | { | |
1380 | return static_pointer_cast<Rational>( | |
1381 | ParentOwned::share_owned_by(_parent)); | |
1382 | } | |
1383 | ||
1384 | int64_t Rational::numerator() const | |
1385 | { | |
1386 | return _structure->p; | |
1387 | } | |
1388 | ||
1389 | uint64_t Rational::denominator() const | |
1390 | { | |
1391 | return _structure->q; | |
1392 | } | |
1393 | ||
1394 | float Rational::value() const | |
1395 | { | |
1396 | return (float)(_structure->p) / (float)(_structure->q); | |
1397 | } | |
1398 | ||
ca3291e3 | 1399 | InputFormat::InputFormat(const struct sr_input_module *structure) : |
d5d7b09e | 1400 | _structure(structure) |
c23c8659 ML |
1401 | { |
1402 | } | |
1403 | ||
1404 | InputFormat::~InputFormat() | |
1405 | { | |
1406 | } | |
1407 | ||
15bebf57 | 1408 | string InputFormat::name() const |
c23c8659 | 1409 | { |
3b161085 | 1410 | return valid_string(sr_input_id_get(_structure)); |
c23c8659 ML |
1411 | } |
1412 | ||
15bebf57 | 1413 | string InputFormat::description() const |
c23c8659 | 1414 | { |
3b161085 | 1415 | return valid_string(sr_input_description_get(_structure)); |
ca3291e3 ML |
1416 | } |
1417 | ||
15bebf57 | 1418 | vector<string> InputFormat::extensions() const |
c7bc82ff JH |
1419 | { |
1420 | vector<string> exts; | |
1421 | for (const char *const *e = sr_input_extensions_get(_structure); | |
1422 | e && *e; e++) | |
1423 | exts.push_back(*e); | |
1424 | return exts; | |
1425 | } | |
1426 | ||
3b161085 | 1427 | map<string, shared_ptr<Option>> InputFormat::options() |
43942280 | 1428 | { |
43942280 | 1429 | map<string, shared_ptr<Option>> result; |
a98729a7 | 1430 | |
e7eb2968 | 1431 | if (const struct sr_option **options = sr_input_options_get(_structure)) { |
a98729a7 DE |
1432 | shared_ptr<const struct sr_option *> option_array |
1433 | {options, &sr_input_options_free}; | |
1434 | for (int i = 0; options[i]; i++) { | |
1435 | shared_ptr<Option> opt { | |
1436 | new Option{options[i], option_array}, | |
1437 | default_delete<Option>{}}; | |
b605edf3 | 1438 | result.emplace(opt->id(), move(opt)); |
a98729a7 | 1439 | } |
48d92e2c | 1440 | } |
43942280 ML |
1441 | return result; |
1442 | } | |
1443 | ||
ca3291e3 | 1444 | shared_ptr<Input> InputFormat::create_input( |
9e7176bd | 1445 | map<string, Glib::VariantBase> options) |
ca3291e3 | 1446 | { |
3b161085 | 1447 | auto input = sr_input_new(_structure, map_to_hash_variant(options)); |
ca3291e3 ML |
1448 | if (!input) |
1449 | throw Error(SR_ERR_ARG); | |
a98729a7 | 1450 | return shared_ptr<Input>{new Input{_parent, input}, default_delete<Input>{}}; |
c23c8659 ML |
1451 | } |
1452 | ||
ca3291e3 | 1453 | Input::Input(shared_ptr<Context> context, const struct sr_input *structure) : |
d5d7b09e | 1454 | _structure(structure), |
f17b4546 | 1455 | _context(move(context)) |
c23c8659 | 1456 | { |
c23c8659 ML |
1457 | } |
1458 | ||
3b161085 | 1459 | shared_ptr<InputDevice> Input::device() |
c23c8659 | 1460 | { |
e7eb2968 | 1461 | if (!_device) { |
3b161085 | 1462 | auto sdi = sr_input_dev_inst_get(_structure); |
ca3291e3 ML |
1463 | if (!sdi) |
1464 | throw Error(SR_ERR_NA); | |
f17b4546 | 1465 | _device.reset(new InputDevice{shared_from_this(), sdi}); |
ca3291e3 | 1466 | } |
c23c8659 | 1467 | |
b6ab954d | 1468 | return _device->share_owned_by(shared_from_this()); |
ca3291e3 | 1469 | } |
c23c8659 | 1470 | |
2b51d48b | 1471 | void Input::send(void *data, size_t length) |
ca3291e3 | 1472 | { |
ce3e1e61 | 1473 | auto gstr = g_string_new_len(static_cast<char *>(data), length); |
3b161085 | 1474 | auto ret = sr_input_send(_structure, gstr); |
e3e1f20c | 1475 | g_string_free(gstr, true); |
ca3291e3 | 1476 | check(ret); |
c23c8659 ML |
1477 | } |
1478 | ||
9c51e8ec ML |
1479 | void Input::end() |
1480 | { | |
1481 | check(sr_input_end(_structure)); | |
1482 | } | |
1483 | ||
b6b4f03e SA |
1484 | void Input::reset() |
1485 | { | |
1486 | check(sr_input_reset(_structure)); | |
1487 | } | |
1488 | ||
ca3291e3 | 1489 | Input::~Input() |
c23c8659 | 1490 | { |
9c51e8ec | 1491 | sr_input_free(_structure); |
c23c8659 ML |
1492 | } |
1493 | ||
6e5240f4 ML |
1494 | InputDevice::InputDevice(shared_ptr<Input> input, |
1495 | struct sr_dev_inst *structure) : | |
6e5240f4 | 1496 | Device(structure), |
d370545d | 1497 | _input(move(input)) |
c23c8659 | 1498 | { |
c23c8659 ML |
1499 | } |
1500 | ||
ca3291e3 | 1501 | InputDevice::~InputDevice() |
c23c8659 | 1502 | { |
c23c8659 ML |
1503 | } |
1504 | ||
d01d2314 ML |
1505 | shared_ptr<Device> InputDevice::get_shared_from_this() |
1506 | { | |
bf52cc8c | 1507 | return static_pointer_cast<Device>(shared_from_this()); |
d01d2314 ML |
1508 | } |
1509 | ||
58aa1f83 | 1510 | Option::Option(const struct sr_option *structure, |
70d3b20b | 1511 | shared_ptr<const struct sr_option *> structure_array) : |
d5d7b09e | 1512 | _structure(structure), |
d370545d | 1513 | _structure_array(move(structure_array)) |
58aa1f83 ML |
1514 | { |
1515 | } | |
1516 | ||
1517 | Option::~Option() | |
1518 | { | |
1519 | } | |
1520 | ||
15bebf57 | 1521 | string Option::id() const |
58aa1f83 | 1522 | { |
3b161085 | 1523 | return valid_string(_structure->id); |
58aa1f83 ML |
1524 | } |
1525 | ||
15bebf57 | 1526 | string Option::name() const |
58aa1f83 | 1527 | { |
3b161085 | 1528 | return valid_string(_structure->name); |
58aa1f83 ML |
1529 | } |
1530 | ||
15bebf57 | 1531 | string Option::description() const |
58aa1f83 | 1532 | { |
3b161085 | 1533 | return valid_string(_structure->desc); |
58aa1f83 ML |
1534 | } |
1535 | ||
15bebf57 | 1536 | Glib::VariantBase Option::default_value() const |
58aa1f83 | 1537 | { |
3b161085 | 1538 | return Glib::VariantBase(_structure->def, true); |
58aa1f83 ML |
1539 | } |
1540 | ||
15bebf57 | 1541 | vector<Glib::VariantBase> Option::values() const |
58aa1f83 ML |
1542 | { |
1543 | vector<Glib::VariantBase> result; | |
ce3e1e61 DE |
1544 | for (auto l = _structure->values; l; l = l->next) { |
1545 | auto *const var = static_cast<GVariant *>(l->data); | |
1546 | result.push_back(Glib::VariantBase(var, true)); | |
1547 | } | |
58aa1f83 ML |
1548 | return result; |
1549 | } | |
1550 | ||
61a6d983 GS |
1551 | Glib::VariantBase Option::parse_string(string value) |
1552 | { | |
1553 | enum sr_datatype dt; | |
1554 | Glib::VariantBase dflt = default_value(); | |
1555 | GVariant *tmpl = dflt.gobj(); | |
1556 | ||
1557 | if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_UINT64)) { | |
1558 | dt = SR_T_UINT64; | |
1559 | } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_STRING)) { | |
1560 | dt = SR_T_STRING; | |
1561 | } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_BOOLEAN)) { | |
1562 | dt = SR_T_BOOL; | |
1563 | } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_DOUBLE)) { | |
1564 | dt = SR_T_FLOAT; | |
1565 | } else if (g_variant_is_of_type(tmpl, G_VARIANT_TYPE_INT32)) { | |
1566 | dt = SR_T_INT32; | |
1567 | } else { | |
1568 | throw Error(SR_ERR_BUG); | |
1569 | } | |
1570 | return ConfigKey::parse_string(value, dt); | |
1571 | } | |
1572 | ||
58aa1f83 | 1573 | OutputFormat::OutputFormat(const struct sr_output_module *structure) : |
d5d7b09e | 1574 | _structure(structure) |
c23c8659 ML |
1575 | { |
1576 | } | |
1577 | ||
1578 | OutputFormat::~OutputFormat() | |
1579 | { | |
1580 | } | |
1581 | ||
15bebf57 | 1582 | string OutputFormat::name() const |
c23c8659 | 1583 | { |
3b161085 | 1584 | return valid_string(sr_output_id_get(_structure)); |
c23c8659 ML |
1585 | } |
1586 | ||
15bebf57 | 1587 | string OutputFormat::description() const |
c23c8659 | 1588 | { |
3b161085 | 1589 | return valid_string(sr_output_description_get(_structure)); |
58aa1f83 ML |
1590 | } |
1591 | ||
15bebf57 | 1592 | vector<string> OutputFormat::extensions() const |
8a174d23 JH |
1593 | { |
1594 | vector<string> exts; | |
1595 | for (const char *const *e = sr_output_extensions_get(_structure); | |
1596 | e && *e; e++) | |
1597 | exts.push_back(*e); | |
1598 | return exts; | |
1599 | } | |
1600 | ||
3b161085 | 1601 | map<string, shared_ptr<Option>> OutputFormat::options() |
58aa1f83 | 1602 | { |
58aa1f83 | 1603 | map<string, shared_ptr<Option>> result; |
a98729a7 | 1604 | |
e7eb2968 | 1605 | if (const struct sr_option **options = sr_output_options_get(_structure)) { |
a98729a7 DE |
1606 | shared_ptr<const struct sr_option *> option_array |
1607 | {options, &sr_output_options_free}; | |
1608 | for (int i = 0; options[i]; i++) { | |
1609 | shared_ptr<Option> opt { | |
1610 | new Option{options[i], option_array}, | |
1611 | default_delete<Option>{}}; | |
b605edf3 | 1612 | result.emplace(opt->id(), move(opt)); |
a98729a7 | 1613 | } |
48d92e2c | 1614 | } |
58aa1f83 | 1615 | return result; |
c23c8659 ML |
1616 | } |
1617 | ||
1618 | shared_ptr<Output> OutputFormat::create_output( | |
9e7176bd | 1619 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) |
c23c8659 | 1620 | { |
a98729a7 | 1621 | return shared_ptr<Output>{ |
9e7176bd | 1622 | new Output{shared_from_this(), move(device), move(options)}, |
a98729a7 | 1623 | default_delete<Output>{}}; |
c23c8659 ML |
1624 | } |
1625 | ||
81b3ce37 | 1626 | shared_ptr<Output> OutputFormat::create_output(string filename, |
9e7176bd | 1627 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) |
81b3ce37 | 1628 | { |
a98729a7 | 1629 | return shared_ptr<Output>{ |
9e7176bd | 1630 | new Output{move(filename), shared_from_this(), move(device), move(options)}, |
a98729a7 | 1631 | default_delete<Output>{}}; |
81b3ce37 SA |
1632 | } |
1633 | ||
15bebf57 | 1634 | bool OutputFormat::test_flag(const OutputFlag *flag) const |
3cd4b381 SA |
1635 | { |
1636 | return sr_output_test_flag(_structure, flag->id()); | |
1637 | } | |
1638 | ||
c23c8659 | 1639 | Output::Output(shared_ptr<OutputFormat> format, |
9e7176bd | 1640 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) : |
d5d7b09e | 1641 | _structure(sr_output_new(format->_structure, |
58e21229 | 1642 | map_to_hash_variant(options), device->_structure, nullptr)), |
d370545d DE |
1643 | _format(move(format)), |
1644 | _device(move(device)), | |
9e7176bd | 1645 | _options(move(options)) |
81b3ce37 SA |
1646 | { |
1647 | } | |
1648 | ||
1649 | Output::Output(string filename, shared_ptr<OutputFormat> format, | |
9e7176bd | 1650 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) : |
d5d7b09e | 1651 | _structure(sr_output_new(format->_structure, |
81b3ce37 | 1652 | map_to_hash_variant(options), device->_structure, filename.c_str())), |
d370545d DE |
1653 | _format(move(format)), |
1654 | _device(move(device)), | |
9e7176bd | 1655 | _options(move(options)) |
c23c8659 ML |
1656 | { |
1657 | } | |
1658 | ||
1659 | Output::~Output() | |
1660 | { | |
3b161085 | 1661 | check(sr_output_free(_structure)); |
c23c8659 ML |
1662 | } |
1663 | ||
ea7a83a4 ML |
1664 | shared_ptr<OutputFormat> Output::format() |
1665 | { | |
1666 | return _format; | |
1667 | } | |
1668 | ||
c23c8659 ML |
1669 | string Output::receive(shared_ptr<Packet> packet) |
1670 | { | |
1671 | GString *out; | |
3b161085 | 1672 | check(sr_output_send(_structure, packet->_structure, &out)); |
e7eb2968 | 1673 | if (out) { |
c23c8659 ML |
1674 | auto result = string(out->str, out->str + out->len); |
1675 | g_string_free(out, true); | |
1676 | return result; | |
e7eb2968 | 1677 | } else { |
c23c8659 ML |
1678 | return string(); |
1679 | } | |
1680 | } | |
1681 | ||
b5f07319 | 1682 | #include <enums.cpp> |
c23c8659 ML |
1683 | |
1684 | } |