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