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