]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | /* Needed for isascii(), as used in the GNU libstdc++ headers */ | |
21 | /* Needed in strutil.c for POSIX.1-2008 locale functions */ | |
22 | #ifndef _XOPEN_SOURCE | |
23 | #define _XOPEN_SOURCE 700 | |
24 | #endif | |
25 | ||
26 | #include <config.h> | |
27 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
28 | ||
29 | #include <sstream> | |
30 | #include <cmath> | |
31 | ||
32 | namespace sigrok | |
33 | { | |
34 | ||
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. */ | |
43 | static inline const char *valid_string(const char *input) | |
44 | { | |
45 | return (input) ? input : ""; | |
46 | } | |
47 | ||
48 | /** Helper function to convert between map<string, VariantBase> and GHashTable */ | |
49 | static GHashTable *map_to_hash_variant(const map<string, Glib::VariantBase> &input) | |
50 | { | |
51 | auto *const output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
52 | reinterpret_cast<GDestroyNotify>(&g_variant_unref)); | |
53 | for (const auto &entry : input) | |
54 | g_hash_table_insert(output, | |
55 | g_strdup(entry.first.c_str()), | |
56 | entry.second.gobj_copy()); | |
57 | return output; | |
58 | } | |
59 | ||
60 | Error::Error(int result) : result(result) | |
61 | { | |
62 | } | |
63 | ||
64 | const char *Error::what() const noexcept | |
65 | { | |
66 | return sr_strerror(result); | |
67 | } | |
68 | ||
69 | Error::~Error() noexcept | |
70 | { | |
71 | } | |
72 | ||
73 | ResourceReader::~ResourceReader() | |
74 | { | |
75 | } | |
76 | ||
77 | SR_PRIV int ResourceReader::open_callback(struct sr_resource *res, | |
78 | const char *name, void *cb_data) noexcept | |
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 | ||
91 | SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, | |
92 | void *cb_data) noexcept | |
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 | ||
105 | SR_PRIV gssize ResourceReader::read_callback(const struct sr_resource *res, | |
106 | void *buf, size_t count, void *cb_data) noexcept | |
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 | ||
118 | shared_ptr<Context> Context::create() | |
119 | { | |
120 | return shared_ptr<Context>{new Context{}, default_delete<Context>{}}; | |
121 | } | |
122 | ||
123 | Context::Context() : | |
124 | _structure(nullptr), | |
125 | _session(nullptr) | |
126 | { | |
127 | check(sr_init(&_structure)); | |
128 | ||
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]}}; | |
132 | _drivers.emplace(driver->name(), move(driver)); | |
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]}}; | |
138 | _input_formats.emplace(input->name(), move(input)); | |
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]}}; | |
144 | _output_formats.emplace(output->name(), move(output)); | |
145 | } | |
146 | } | |
147 | ||
148 | string Context::package_version() | |
149 | { | |
150 | return sr_package_version_string_get(); | |
151 | } | |
152 | ||
153 | string Context::lib_version() | |
154 | { | |
155 | return sr_lib_version_string_get(); | |
156 | } | |
157 | ||
158 | map<string, shared_ptr<Driver>> Context::drivers() | |
159 | { | |
160 | map<string, shared_ptr<Driver>> result; | |
161 | for (const auto &entry: _drivers) { | |
162 | const auto &name = entry.first; | |
163 | const auto &driver = entry.second; | |
164 | result.emplace(name, driver->share_owned_by(shared_from_this())); | |
165 | } | |
166 | return result; | |
167 | } | |
168 | ||
169 | map<string, shared_ptr<InputFormat>> Context::input_formats() | |
170 | { | |
171 | map<string, shared_ptr<InputFormat>> result; | |
172 | for (const auto &entry: _input_formats) { | |
173 | const auto &name = entry.first; | |
174 | const auto &input_format = entry.second; | |
175 | result.emplace(name, input_format->share_owned_by(shared_from_this())); | |
176 | } | |
177 | return result; | |
178 | } | |
179 | ||
180 | map<string, shared_ptr<OutputFormat>> Context::output_formats() | |
181 | { | |
182 | map<string, shared_ptr<OutputFormat>> result; | |
183 | for (const auto &entry: _output_formats) { | |
184 | const auto &name = entry.first; | |
185 | const auto &output_format = entry.second; | |
186 | result.emplace(name, output_format->share_owned_by(shared_from_this())); | |
187 | } | |
188 | return result; | |
189 | } | |
190 | ||
191 | Context::~Context() | |
192 | { | |
193 | check(sr_exit(_structure)); | |
194 | } | |
195 | ||
196 | const LogLevel *Context::log_level() const | |
197 | { | |
198 | return LogLevel::get(sr_log_loglevel_get()); | |
199 | } | |
200 | ||
201 | void Context::set_log_level(const LogLevel *level) | |
202 | { | |
203 | check(sr_log_loglevel_set(level->id())); | |
204 | } | |
205 | ||
206 | static int call_log_callback(void *cb_data, int loglevel, | |
207 | const char *format, va_list args) noexcept | |
208 | { | |
209 | const unique_ptr<char, decltype(&g_free)> | |
210 | message {g_strdup_vprintf(format, args), &g_free}; | |
211 | ||
212 | auto *const callback = static_cast<LogCallbackFunction *>(cb_data); | |
213 | ||
214 | try { | |
215 | (*callback)(LogLevel::get(loglevel), message.get()); | |
216 | } catch (Error e) { | |
217 | return e.result; | |
218 | } | |
219 | ||
220 | return SR_OK; | |
221 | } | |
222 | ||
223 | void Context::set_log_callback(LogCallbackFunction callback) | |
224 | { | |
225 | _log_callback = move(callback); | |
226 | check(sr_log_callback_set(call_log_callback, &_log_callback)); | |
227 | } | |
228 | ||
229 | void Context::set_log_callback_default() | |
230 | { | |
231 | check(sr_log_callback_set_default()); | |
232 | _log_callback = nullptr; | |
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 | } | |
247 | ||
248 | shared_ptr<Session> Context::create_session() | |
249 | { | |
250 | return shared_ptr<Session>{new Session{shared_from_this()}, | |
251 | default_delete<Session>{}}; | |
252 | } | |
253 | ||
254 | shared_ptr<UserDevice> Context::create_user_device( | |
255 | string vendor, string model, string version) | |
256 | { | |
257 | return shared_ptr<UserDevice>{ | |
258 | new UserDevice{move(vendor), move(model), move(version)}, | |
259 | default_delete<UserDevice>{}}; | |
260 | } | |
261 | ||
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; | |
271 | return shared_ptr<Packet>{new Packet{nullptr, packet}, | |
272 | default_delete<Packet>{}}; | |
273 | } | |
274 | ||
275 | shared_ptr<Packet> Context::create_meta_packet( | |
276 | map<const ConfigKey *, Glib::VariantBase> config) | |
277 | { | |
278 | auto meta = g_new0(struct sr_datafeed_meta, 1); | |
279 | for (const auto &input : config) { | |
280 | const auto &key = input.first; | |
281 | const auto &value = input.second; | |
282 | auto *const output = g_new(struct sr_config, 1); | |
283 | output->key = key->id(); | |
284 | output->data = value.gobj_copy(); | |
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; | |
290 | return shared_ptr<Packet>{new Packet{nullptr, packet}, | |
291 | default_delete<Packet>{}}; | |
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; | |
304 | return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}}; | |
305 | } | |
306 | ||
307 | shared_ptr<Packet> Context::create_analog_packet( | |
308 | vector<shared_ptr<Channel> > channels, | |
309 | const float *data_pointer, unsigned int num_samples, const Quantity *mq, | |
310 | const Unit *unit, vector<const QuantityFlag *> mqflags) | |
311 | { | |
312 | auto analog = g_new0(struct sr_datafeed_analog, 1); | |
313 | auto meaning = g_new0(struct sr_analog_meaning, 1); | |
314 | auto encoding = g_new0(struct sr_analog_encoding, 1); | |
315 | auto spec = g_new0(struct sr_analog_spec, 1); | |
316 | ||
317 | analog->meaning = meaning; | |
318 | ||
319 | for (const auto &channel : channels) | |
320 | meaning->channels = g_slist_append(meaning->channels, channel->_structure); | |
321 | meaning->mq = static_cast<sr_mq>(mq->id()); | |
322 | meaning->unit = static_cast<sr_unit>(unit->id()); | |
323 | meaning->mqflags = static_cast<sr_mqflag>(QuantityFlag::mask_from_flags(move(mqflags))); | |
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; | |
347 | analog->data = (float*)data_pointer; | |
348 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
349 | packet->type = SR_DF_ANALOG; | |
350 | packet->payload = analog; | |
351 | return shared_ptr<Packet>{new Packet{nullptr, packet}, default_delete<Packet>{}}; | |
352 | } | |
353 | ||
354 | shared_ptr<Session> Context::load_session(string filename) | |
355 | { | |
356 | return shared_ptr<Session>{ | |
357 | new Session{shared_from_this(), move(filename)}, | |
358 | default_delete<Session>{}}; | |
359 | } | |
360 | ||
361 | shared_ptr<Trigger> Context::create_trigger(string name) | |
362 | { | |
363 | return shared_ptr<Trigger>{ | |
364 | new Trigger{shared_from_this(), move(name)}, | |
365 | default_delete<Trigger>{}}; | |
366 | } | |
367 | ||
368 | shared_ptr<Input> Context::open_file(string filename) | |
369 | { | |
370 | const struct sr_input *input; | |
371 | ||
372 | check(sr_input_scan_file(filename.c_str(), &input)); | |
373 | return shared_ptr<Input>{ | |
374 | new Input{shared_from_this(), input}, | |
375 | default_delete<Input>{}}; | |
376 | } | |
377 | ||
378 | shared_ptr<Input> Context::open_stream(string header) | |
379 | { | |
380 | const struct sr_input *input; | |
381 | ||
382 | auto gstr = g_string_new(header.c_str()); | |
383 | auto ret = sr_input_scan_buffer(gstr, &input); | |
384 | g_string_free(gstr, true); | |
385 | check(ret); | |
386 | return shared_ptr<Input>{ | |
387 | new Input{shared_from_this(), input}, | |
388 | default_delete<Input>{}}; | |
389 | } | |
390 | ||
391 | map<string, string> Context::serials(shared_ptr<Driver> driver) const | |
392 | { | |
393 | GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr); | |
394 | map<string, string> serials; | |
395 | ||
396 | for (GSList *serial = serial_list; serial; serial = serial->next) { | |
397 | auto *const port = static_cast<sr_serial_port *>(serial->data); | |
398 | serials[string(port->name)] = string(port->description); | |
399 | } | |
400 | ||
401 | g_slist_free_full(serial_list, | |
402 | reinterpret_cast<GDestroyNotify>(&sr_serial_free)); | |
403 | return serials; | |
404 | } | |
405 | ||
406 | Driver::Driver(struct sr_dev_driver *structure) : | |
407 | Configurable(structure, nullptr, nullptr), | |
408 | _structure(structure), | |
409 | _initialized(false) | |
410 | { | |
411 | } | |
412 | ||
413 | Driver::~Driver() | |
414 | { | |
415 | } | |
416 | ||
417 | string Driver::name() const | |
418 | { | |
419 | return valid_string(_structure->name); | |
420 | } | |
421 | ||
422 | string Driver::long_name() const | |
423 | { | |
424 | return valid_string(_structure->longname); | |
425 | } | |
426 | ||
427 | set<const ConfigKey *> Driver::scan_options() const | |
428 | { | |
429 | GArray *opts = sr_driver_scan_options_list(_structure); | |
430 | set<const ConfigKey *> result; | |
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 | } | |
436 | return result; | |
437 | } | |
438 | ||
439 | vector<shared_ptr<HardwareDevice>> Driver::scan( | |
440 | map<const ConfigKey *, Glib::VariantBase> options) | |
441 | { | |
442 | /* Initialise the driver if not yet done. */ | |
443 | if (!_initialized) { | |
444 | check(sr_driver_init(_parent->_structure, _structure)); | |
445 | _initialized = true; | |
446 | } | |
447 | ||
448 | /* Translate scan options to GSList of struct sr_config pointers. */ | |
449 | GSList *option_list = nullptr; | |
450 | for (const auto &entry : options) { | |
451 | const auto &key = entry.first; | |
452 | const auto &value = entry.second; | |
453 | auto *const config = g_new(struct sr_config, 1); | |
454 | config->key = key->id(); | |
455 | config->data = const_cast<GVariant*>(value.gobj()); | |
456 | option_list = g_slist_append(option_list, config); | |
457 | } | |
458 | ||
459 | /* Run scan. */ | |
460 | GSList *device_list = sr_driver_scan(_structure, option_list); | |
461 | ||
462 | /* Free option list. */ | |
463 | g_slist_free_full(option_list, g_free); | |
464 | ||
465 | ||
466 | /* Create device objects. */ | |
467 | vector<shared_ptr<HardwareDevice>> result; | |
468 | for (GSList *device = device_list; device; device = device->next) { | |
469 | auto *const sdi = static_cast<struct sr_dev_inst *>(device->data); | |
470 | shared_ptr<HardwareDevice> hwdev { | |
471 | new HardwareDevice{shared_from_this(), sdi}, | |
472 | default_delete<HardwareDevice>{}}; | |
473 | result.push_back(move(hwdev)); | |
474 | } | |
475 | ||
476 | /* Free GSList returned from scan. */ | |
477 | g_slist_free(device_list); | |
478 | ||
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 | ||
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 | ||
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 | } | |
508 | ||
509 | return result; | |
510 | } | |
511 | ||
512 | Glib::VariantBase Configurable::config_get(const ConfigKey *key) const | |
513 | { | |
514 | GVariant *data; | |
515 | check(sr_config_get( | |
516 | config_driver, config_sdi, config_channel_group, | |
517 | key->id(), &data)); | |
518 | return Glib::VariantBase(data); | |
519 | } | |
520 | ||
521 | void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value) | |
522 | { | |
523 | check(sr_config_set( | |
524 | config_sdi, config_channel_group, | |
525 | key->id(), const_cast<GVariant*>(value.gobj()))); | |
526 | } | |
527 | ||
528 | set<const Capability *> Configurable::config_capabilities(const ConfigKey *key) const | |
529 | { | |
530 | int caps = sr_dev_config_capabilities_list(config_sdi, | |
531 | config_channel_group, key->id()); | |
532 | ||
533 | set<const Capability *> result; | |
534 | ||
535 | for (auto cap: Capability::values()) | |
536 | if (caps & cap->id()) | |
537 | result.insert(cap); | |
538 | ||
539 | return result; | |
540 | } | |
541 | ||
542 | bool Configurable::config_check(const ConfigKey *key, | |
543 | const Capability *capability) const | |
544 | { | |
545 | int caps = sr_dev_config_capabilities_list(config_sdi, | |
546 | config_channel_group, key->id()); | |
547 | ||
548 | return (caps & capability->id()); | |
549 | } | |
550 | ||
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); | |
558 | } | |
559 | ||
560 | Device::Device(struct sr_dev_inst *structure) : | |
561 | Configurable(sr_dev_inst_driver_get(structure), structure, nullptr), | |
562 | _structure(structure) | |
563 | { | |
564 | for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) { | |
565 | auto *const ch = static_cast<struct sr_channel *>(entry->data); | |
566 | unique_ptr<Channel> channel {new Channel{ch}}; | |
567 | _channels.emplace(ch, move(channel)); | |
568 | } | |
569 | ||
570 | for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) { | |
571 | auto *const cg = static_cast<struct sr_channel_group *>(entry->data); | |
572 | unique_ptr<ChannelGroup> group {new ChannelGroup{this, cg}}; | |
573 | _channel_groups.emplace(group->name(), move(group)); | |
574 | } | |
575 | } | |
576 | ||
577 | Device::~Device() | |
578 | { | |
579 | } | |
580 | ||
581 | string Device::vendor() const | |
582 | { | |
583 | return valid_string(sr_dev_inst_vendor_get(_structure)); | |
584 | } | |
585 | ||
586 | string Device::model() const | |
587 | { | |
588 | return valid_string(sr_dev_inst_model_get(_structure)); | |
589 | } | |
590 | ||
591 | string Device::version() const | |
592 | { | |
593 | return valid_string(sr_dev_inst_version_get(_structure)); | |
594 | } | |
595 | ||
596 | string Device::serial_number() const | |
597 | { | |
598 | return valid_string(sr_dev_inst_sernum_get(_structure)); | |
599 | } | |
600 | ||
601 | string Device::connection_id() const | |
602 | { | |
603 | return valid_string(sr_dev_inst_connid_get(_structure)); | |
604 | } | |
605 | ||
606 | vector<shared_ptr<Channel>> Device::channels() | |
607 | { | |
608 | vector<shared_ptr<Channel>> result; | |
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); | |
611 | result.push_back(_channels[ch]->share_owned_by(get_shared_from_this())); | |
612 | } | |
613 | return result; | |
614 | } | |
615 | ||
616 | shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr) | |
617 | { | |
618 | return _channels[ptr]->share_owned_by(get_shared_from_this()); | |
619 | } | |
620 | ||
621 | map<string, shared_ptr<ChannelGroup>> | |
622 | Device::channel_groups() | |
623 | { | |
624 | map<string, shared_ptr<ChannelGroup>> result; | |
625 | for (const auto &entry: _channel_groups) { | |
626 | const auto &name = entry.first; | |
627 | const auto &channel_group = entry.second; | |
628 | result.emplace(name, channel_group->share_owned_by(get_shared_from_this())); | |
629 | } | |
630 | return result; | |
631 | } | |
632 | ||
633 | void Device::open() | |
634 | { | |
635 | check(sr_dev_open(_structure)); | |
636 | } | |
637 | ||
638 | void Device::close() | |
639 | { | |
640 | check(sr_dev_close(_structure)); | |
641 | } | |
642 | ||
643 | HardwareDevice::HardwareDevice(shared_ptr<Driver> driver, | |
644 | struct sr_dev_inst *structure) : | |
645 | Device(structure), | |
646 | _driver(move(driver)) | |
647 | { | |
648 | } | |
649 | ||
650 | HardwareDevice::~HardwareDevice() | |
651 | { | |
652 | } | |
653 | ||
654 | shared_ptr<Device> HardwareDevice::get_shared_from_this() | |
655 | { | |
656 | return static_pointer_cast<Device>(shared_from_this()); | |
657 | } | |
658 | ||
659 | shared_ptr<Driver> HardwareDevice::driver() | |
660 | { | |
661 | return _driver; | |
662 | } | |
663 | ||
664 | UserDevice::UserDevice(string vendor, string model, string version) : | |
665 | Device(sr_dev_inst_user_new( | |
666 | vendor.c_str(), model.c_str(), version.c_str())) | |
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())); | |
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); | |
686 | unique_ptr<Channel> channel {new Channel{ch}}; | |
687 | _channels.emplace(ch, move(channel)); | |
688 | return get_channel(ch); | |
689 | } | |
690 | ||
691 | Channel::Channel(struct sr_channel *structure) : | |
692 | _structure(structure), | |
693 | _type(ChannelType::get(_structure->type)) | |
694 | { | |
695 | } | |
696 | ||
697 | Channel::~Channel() | |
698 | { | |
699 | } | |
700 | ||
701 | string Channel::name() const | |
702 | { | |
703 | return valid_string(_structure->name); | |
704 | } | |
705 | ||
706 | void Channel::set_name(string name) | |
707 | { | |
708 | check(sr_dev_channel_name_set(_structure, name.c_str())); | |
709 | } | |
710 | ||
711 | const ChannelType *Channel::type() const | |
712 | { | |
713 | return ChannelType::get(_structure->type); | |
714 | } | |
715 | ||
716 | bool Channel::enabled() const | |
717 | { | |
718 | return _structure->enabled; | |
719 | } | |
720 | ||
721 | void Channel::set_enabled(bool value) | |
722 | { | |
723 | check(sr_dev_channel_enable(_structure, value)); | |
724 | } | |
725 | ||
726 | unsigned int Channel::index() const | |
727 | { | |
728 | return _structure->index; | |
729 | } | |
730 | ||
731 | ChannelGroup::ChannelGroup(const Device *device, | |
732 | struct sr_channel_group *structure) : | |
733 | Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure) | |
734 | { | |
735 | for (GSList *entry = config_channel_group->channels; entry; entry = entry->next) { | |
736 | auto *const ch = static_cast<struct sr_channel *>(entry->data); | |
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()); | |
740 | } | |
741 | } | |
742 | ||
743 | ChannelGroup::~ChannelGroup() | |
744 | { | |
745 | } | |
746 | ||
747 | string ChannelGroup::name() const | |
748 | { | |
749 | return valid_string(config_channel_group->name); | |
750 | } | |
751 | ||
752 | vector<shared_ptr<Channel>> ChannelGroup::channels() | |
753 | { | |
754 | vector<shared_ptr<Channel>> result; | |
755 | for (const auto &channel : _channels) | |
756 | result.push_back(channel->share_owned_by(_parent)); | |
757 | return result; | |
758 | } | |
759 | ||
760 | Trigger::Trigger(shared_ptr<Context> context, string name) : | |
761 | _structure(sr_trigger_new(name.c_str())), | |
762 | _context(move(context)) | |
763 | { | |
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 | } | |
769 | } | |
770 | ||
771 | Trigger::~Trigger() | |
772 | { | |
773 | sr_trigger_free(_structure); | |
774 | } | |
775 | ||
776 | string Trigger::name() const | |
777 | { | |
778 | return _structure->name; | |
779 | } | |
780 | ||
781 | vector<shared_ptr<TriggerStage>> Trigger::stages() | |
782 | { | |
783 | vector<shared_ptr<TriggerStage>> result; | |
784 | for (const auto &stage : _stages) | |
785 | result.push_back(stage->share_owned_by(shared_from_this())); | |
786 | return result; | |
787 | } | |
788 | ||
789 | shared_ptr<TriggerStage> Trigger::add_stage() | |
790 | { | |
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()); | |
794 | } | |
795 | ||
796 | TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : | |
797 | _structure(structure) | |
798 | { | |
799 | } | |
800 | ||
801 | TriggerStage::~TriggerStage() | |
802 | { | |
803 | } | |
804 | ||
805 | int TriggerStage::number() const | |
806 | { | |
807 | return _structure->stage; | |
808 | } | |
809 | ||
810 | vector<shared_ptr<TriggerMatch>> TriggerStage::matches() | |
811 | { | |
812 | vector<shared_ptr<TriggerMatch>> result; | |
813 | for (const auto &match : _matches) | |
814 | result.push_back(match->share_owned_by(shared_from_this())); | |
815 | return result; | |
816 | } | |
817 | ||
818 | void TriggerStage::add_match(shared_ptr<Channel> channel, | |
819 | const TriggerMatchType *type, float value) | |
820 | { | |
821 | check(sr_trigger_match_add(_structure, | |
822 | channel->_structure, type->id(), value)); | |
823 | GSList *const last = g_slist_last(_structure->matches); | |
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)); | |
828 | } | |
829 | ||
830 | void TriggerStage::add_match(shared_ptr<Channel> channel, | |
831 | const TriggerMatchType *type) | |
832 | { | |
833 | add_match(move(channel), type, NAN); | |
834 | } | |
835 | ||
836 | TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, | |
837 | shared_ptr<Channel> channel) : | |
838 | _structure(structure), | |
839 | _channel(move(channel)) | |
840 | { | |
841 | } | |
842 | ||
843 | TriggerMatch::~TriggerMatch() | |
844 | { | |
845 | } | |
846 | ||
847 | shared_ptr<Channel> TriggerMatch::channel() | |
848 | { | |
849 | return _channel; | |
850 | } | |
851 | ||
852 | const TriggerMatchType *TriggerMatch::type() const | |
853 | { | |
854 | return TriggerMatchType::get(_structure->match); | |
855 | } | |
856 | ||
857 | float TriggerMatch::value() const | |
858 | { | |
859 | return _structure->value; | |
860 | } | |
861 | ||
862 | DatafeedCallbackData::DatafeedCallbackData(Session *session, | |
863 | DatafeedCallbackFunction callback) : | |
864 | _callback(move(callback)), | |
865 | _session(session) | |
866 | { | |
867 | } | |
868 | ||
869 | void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, | |
870 | const struct sr_datafeed_packet *pkt) | |
871 | { | |
872 | auto device = _session->get_device(sdi); | |
873 | shared_ptr<Packet> packet {new Packet{device, pkt}, default_delete<Packet>{}}; | |
874 | _callback(move(device), move(packet)); | |
875 | } | |
876 | ||
877 | SessionDevice::SessionDevice(struct sr_dev_inst *structure) : | |
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 | ||
891 | Session::Session(shared_ptr<Context> context) : | |
892 | _structure(nullptr), | |
893 | _context(move(context)) | |
894 | { | |
895 | check(sr_session_new(_context->_structure, &_structure)); | |
896 | _context->_session = this; | |
897 | } | |
898 | ||
899 | Session::Session(shared_ptr<Context> context, string filename) : | |
900 | _structure(nullptr), | |
901 | _context(move(context)), | |
902 | _filename(move(filename)) | |
903 | { | |
904 | check(sr_session_load(_context->_structure, _filename.c_str(), &_structure)); | |
905 | GSList *dev_list; | |
906 | check(sr_session_dev_list(_structure, &dev_list)); | |
907 | for (GSList *dev = dev_list; dev; dev = dev->next) { | |
908 | auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data); | |
909 | unique_ptr<SessionDevice> device {new SessionDevice{sdi}}; | |
910 | _owned_devices.emplace(sdi, move(device)); | |
911 | } | |
912 | _context->_session = this; | |
913 | } | |
914 | ||
915 | Session::~Session() | |
916 | { | |
917 | check(sr_session_destroy(_structure)); | |
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>( | |
924 | _owned_devices[sdi]->share_owned_by(shared_from_this())); | |
925 | else if (_other_devices.count(sdi)) | |
926 | return _other_devices[sdi]; | |
927 | else | |
928 | throw Error(SR_ERR_BUG); | |
929 | } | |
930 | ||
931 | void Session::add_device(shared_ptr<Device> device) | |
932 | { | |
933 | const auto dev_struct = device->_structure; | |
934 | check(sr_session_dev_add(_structure, dev_struct)); | |
935 | _other_devices[dev_struct] = move(device); | |
936 | } | |
937 | ||
938 | vector<shared_ptr<Device>> Session::devices() | |
939 | { | |
940 | GSList *dev_list; | |
941 | check(sr_session_dev_list(_structure, &dev_list)); | |
942 | vector<shared_ptr<Device>> result; | |
943 | for (GSList *dev = dev_list; dev; dev = dev->next) { | |
944 | auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data); | |
945 | result.push_back(get_device(sdi)); | |
946 | } | |
947 | return result; | |
948 | } | |
949 | ||
950 | void Session::remove_devices() | |
951 | { | |
952 | _other_devices.clear(); | |
953 | check(sr_session_dev_remove_all(_structure)); | |
954 | } | |
955 | ||
956 | void Session::start() | |
957 | { | |
958 | check(sr_session_start(_structure)); | |
959 | } | |
960 | ||
961 | void Session::run() | |
962 | { | |
963 | check(sr_session_run(_structure)); | |
964 | } | |
965 | ||
966 | void Session::stop() | |
967 | { | |
968 | check(sr_session_stop(_structure)); | |
969 | } | |
970 | ||
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 | ||
979 | static void session_stopped_callback(void *data) noexcept | |
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 | ||
996 | static void datafeed_callback(const struct sr_dev_inst *sdi, | |
997 | const struct sr_datafeed_packet *pkt, void *cb_data) noexcept | |
998 | { | |
999 | auto callback = static_cast<DatafeedCallbackData *>(cb_data); | |
1000 | callback->run(sdi, pkt); | |
1001 | } | |
1002 | ||
1003 | void Session::add_datafeed_callback(DatafeedCallbackFunction callback) | |
1004 | { | |
1005 | unique_ptr<DatafeedCallbackData> cb_data | |
1006 | {new DatafeedCallbackData{this, move(callback)}}; | |
1007 | check(sr_session_datafeed_callback_add(_structure, | |
1008 | &datafeed_callback, cb_data.get())); | |
1009 | _datafeed_callbacks.push_back(move(cb_data)); | |
1010 | } | |
1011 | ||
1012 | void Session::remove_datafeed_callbacks() | |
1013 | { | |
1014 | check(sr_session_datafeed_callback_remove_all(_structure)); | |
1015 | _datafeed_callbacks.clear(); | |
1016 | } | |
1017 | ||
1018 | shared_ptr<Trigger> Session::trigger() | |
1019 | { | |
1020 | return _trigger; | |
1021 | } | |
1022 | ||
1023 | void Session::set_trigger(shared_ptr<Trigger> trigger) | |
1024 | { | |
1025 | if (!trigger) | |
1026 | // Set NULL trigger, i.e. remove any trigger from the session. | |
1027 | check(sr_session_trigger_set(_structure, nullptr)); | |
1028 | else | |
1029 | check(sr_session_trigger_set(_structure, trigger->_structure)); | |
1030 | _trigger = move(trigger); | |
1031 | } | |
1032 | ||
1033 | string Session::filename() const | |
1034 | { | |
1035 | return _filename; | |
1036 | } | |
1037 | ||
1038 | shared_ptr<Context> Session::context() | |
1039 | { | |
1040 | return _context; | |
1041 | } | |
1042 | ||
1043 | Packet::Packet(shared_ptr<Device> device, | |
1044 | const struct sr_datafeed_packet *structure) : | |
1045 | _structure(structure), | |
1046 | _device(move(device)) | |
1047 | { | |
1048 | switch (structure->type) | |
1049 | { | |
1050 | case SR_DF_HEADER: | |
1051 | _payload.reset(new Header{ | |
1052 | static_cast<const struct sr_datafeed_header *>( | |
1053 | structure->payload)}); | |
1054 | break; | |
1055 | case SR_DF_META: | |
1056 | _payload.reset(new Meta{ | |
1057 | static_cast<const struct sr_datafeed_meta *>( | |
1058 | structure->payload)}); | |
1059 | break; | |
1060 | case SR_DF_LOGIC: | |
1061 | _payload.reset(new Logic{ | |
1062 | static_cast<const struct sr_datafeed_logic *>( | |
1063 | structure->payload)}); | |
1064 | break; | |
1065 | case SR_DF_ANALOG: | |
1066 | _payload.reset(new Analog{ | |
1067 | static_cast<const struct sr_datafeed_analog *>( | |
1068 | structure->payload)}); | |
1069 | break; | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | Packet::~Packet() | |
1074 | { | |
1075 | } | |
1076 | ||
1077 | const PacketType *Packet::type() const | |
1078 | { | |
1079 | return PacketType::get(_structure->type); | |
1080 | } | |
1081 | ||
1082 | shared_ptr<PacketPayload> Packet::payload() | |
1083 | { | |
1084 | if (_payload) | |
1085 | return _payload->share_owned_by(shared_from_this()); | |
1086 | else | |
1087 | throw Error(SR_ERR_NA); | |
1088 | } | |
1089 | ||
1090 | PacketPayload::PacketPayload() | |
1091 | { | |
1092 | } | |
1093 | ||
1094 | PacketPayload::~PacketPayload() | |
1095 | { | |
1096 | } | |
1097 | ||
1098 | Header::Header(const struct sr_datafeed_header *structure) : | |
1099 | PacketPayload(), | |
1100 | _structure(structure) | |
1101 | { | |
1102 | } | |
1103 | ||
1104 | Header::~Header() | |
1105 | { | |
1106 | } | |
1107 | ||
1108 | shared_ptr<PacketPayload> Header::share_owned_by(shared_ptr<Packet> _parent) | |
1109 | { | |
1110 | return static_pointer_cast<PacketPayload>( | |
1111 | ParentOwned::share_owned_by(_parent)); | |
1112 | } | |
1113 | ||
1114 | int Header::feed_version() const | |
1115 | { | |
1116 | return _structure->feed_version; | |
1117 | } | |
1118 | ||
1119 | Glib::TimeVal Header::start_time() const | |
1120 | { | |
1121 | return Glib::TimeVal( | |
1122 | _structure->starttime.tv_sec, | |
1123 | _structure->starttime.tv_usec); | |
1124 | } | |
1125 | ||
1126 | Meta::Meta(const struct sr_datafeed_meta *structure) : | |
1127 | PacketPayload(), | |
1128 | _structure(structure) | |
1129 | { | |
1130 | } | |
1131 | ||
1132 | Meta::~Meta() | |
1133 | { | |
1134 | } | |
1135 | ||
1136 | shared_ptr<PacketPayload> Meta::share_owned_by(shared_ptr<Packet> _parent) | |
1137 | { | |
1138 | return static_pointer_cast<PacketPayload>( | |
1139 | ParentOwned::share_owned_by(_parent)); | |
1140 | } | |
1141 | ||
1142 | map<const ConfigKey *, Glib::VariantBase> Meta::config() const | |
1143 | { | |
1144 | map<const ConfigKey *, Glib::VariantBase> result; | |
1145 | for (auto l = _structure->config; l; l = l->next) { | |
1146 | auto *const config = static_cast<struct sr_config *>(l->data); | |
1147 | result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data, true); | |
1148 | } | |
1149 | return result; | |
1150 | } | |
1151 | ||
1152 | Logic::Logic(const struct sr_datafeed_logic *structure) : | |
1153 | PacketPayload(), | |
1154 | _structure(structure) | |
1155 | { | |
1156 | } | |
1157 | ||
1158 | Logic::~Logic() | |
1159 | { | |
1160 | } | |
1161 | ||
1162 | shared_ptr<PacketPayload> Logic::share_owned_by(shared_ptr<Packet> _parent) | |
1163 | { | |
1164 | return static_pointer_cast<PacketPayload>( | |
1165 | ParentOwned::share_owned_by(_parent)); | |
1166 | } | |
1167 | ||
1168 | void *Logic::data_pointer() | |
1169 | { | |
1170 | return _structure->data; | |
1171 | } | |
1172 | ||
1173 | size_t Logic::data_length() const | |
1174 | { | |
1175 | return _structure->length; | |
1176 | } | |
1177 | ||
1178 | unsigned int Logic::unit_size() const | |
1179 | { | |
1180 | return _structure->unitsize; | |
1181 | } | |
1182 | ||
1183 | Analog::Analog(const struct sr_datafeed_analog *structure) : | |
1184 | PacketPayload(), | |
1185 | _structure(structure) | |
1186 | { | |
1187 | } | |
1188 | ||
1189 | Analog::~Analog() | |
1190 | { | |
1191 | } | |
1192 | ||
1193 | shared_ptr<PacketPayload> Analog::share_owned_by(shared_ptr<Packet> _parent) | |
1194 | { | |
1195 | return static_pointer_cast<PacketPayload>( | |
1196 | ParentOwned::share_owned_by(_parent)); | |
1197 | } | |
1198 | ||
1199 | void *Analog::data_pointer() | |
1200 | { | |
1201 | return _structure->data; | |
1202 | } | |
1203 | ||
1204 | void Analog::get_data_as_float(float *dest) | |
1205 | { | |
1206 | check(sr_analog_to_float(_structure, dest)); | |
1207 | } | |
1208 | ||
1209 | unsigned int Analog::num_samples() const | |
1210 | { | |
1211 | return _structure->num_samples; | |
1212 | } | |
1213 | ||
1214 | vector<shared_ptr<Channel>> Analog::channels() | |
1215 | { | |
1216 | vector<shared_ptr<Channel>> result; | |
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 | } | |
1221 | return result; | |
1222 | } | |
1223 | ||
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 | ||
1276 | const Quantity *Analog::mq() const | |
1277 | { | |
1278 | return Quantity::get(_structure->meaning->mq); | |
1279 | } | |
1280 | ||
1281 | const Unit *Analog::unit() const | |
1282 | { | |
1283 | return Unit::get(_structure->meaning->unit); | |
1284 | } | |
1285 | ||
1286 | vector<const QuantityFlag *> Analog::mq_flags() const | |
1287 | { | |
1288 | return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); | |
1289 | } | |
1290 | ||
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 | ||
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 | ||
1363 | InputFormat::InputFormat(const struct sr_input_module *structure) : | |
1364 | _structure(structure) | |
1365 | { | |
1366 | } | |
1367 | ||
1368 | InputFormat::~InputFormat() | |
1369 | { | |
1370 | } | |
1371 | ||
1372 | string InputFormat::name() const | |
1373 | { | |
1374 | return valid_string(sr_input_id_get(_structure)); | |
1375 | } | |
1376 | ||
1377 | string InputFormat::description() const | |
1378 | { | |
1379 | return valid_string(sr_input_description_get(_structure)); | |
1380 | } | |
1381 | ||
1382 | vector<string> InputFormat::extensions() const | |
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 | ||
1391 | map<string, shared_ptr<Option>> InputFormat::options() | |
1392 | { | |
1393 | map<string, shared_ptr<Option>> result; | |
1394 | ||
1395 | if (const struct sr_option **options = sr_input_options_get(_structure)) { | |
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>{}}; | |
1402 | result.emplace(opt->id(), move(opt)); | |
1403 | } | |
1404 | } | |
1405 | return result; | |
1406 | } | |
1407 | ||
1408 | shared_ptr<Input> InputFormat::create_input( | |
1409 | map<string, Glib::VariantBase> options) | |
1410 | { | |
1411 | auto input = sr_input_new(_structure, map_to_hash_variant(options)); | |
1412 | if (!input) | |
1413 | throw Error(SR_ERR_ARG); | |
1414 | return shared_ptr<Input>{new Input{_parent, input}, default_delete<Input>{}}; | |
1415 | } | |
1416 | ||
1417 | Input::Input(shared_ptr<Context> context, const struct sr_input *structure) : | |
1418 | _structure(structure), | |
1419 | _context(move(context)) | |
1420 | { | |
1421 | } | |
1422 | ||
1423 | shared_ptr<InputDevice> Input::device() | |
1424 | { | |
1425 | if (!_device) { | |
1426 | auto sdi = sr_input_dev_inst_get(_structure); | |
1427 | if (!sdi) | |
1428 | throw Error(SR_ERR_NA); | |
1429 | _device.reset(new InputDevice{shared_from_this(), sdi}); | |
1430 | } | |
1431 | ||
1432 | return _device->share_owned_by(shared_from_this()); | |
1433 | } | |
1434 | ||
1435 | void Input::send(void *data, size_t length) | |
1436 | { | |
1437 | auto gstr = g_string_new_len(static_cast<char *>(data), length); | |
1438 | auto ret = sr_input_send(_structure, gstr); | |
1439 | g_string_free(gstr, true); | |
1440 | check(ret); | |
1441 | } | |
1442 | ||
1443 | void Input::end() | |
1444 | { | |
1445 | check(sr_input_end(_structure)); | |
1446 | } | |
1447 | ||
1448 | void Input::reset() | |
1449 | { | |
1450 | check(sr_input_reset(_structure)); | |
1451 | } | |
1452 | ||
1453 | Input::~Input() | |
1454 | { | |
1455 | sr_input_free(_structure); | |
1456 | } | |
1457 | ||
1458 | InputDevice::InputDevice(shared_ptr<Input> input, | |
1459 | struct sr_dev_inst *structure) : | |
1460 | Device(structure), | |
1461 | _input(move(input)) | |
1462 | { | |
1463 | } | |
1464 | ||
1465 | InputDevice::~InputDevice() | |
1466 | { | |
1467 | } | |
1468 | ||
1469 | shared_ptr<Device> InputDevice::get_shared_from_this() | |
1470 | { | |
1471 | return static_pointer_cast<Device>(shared_from_this()); | |
1472 | } | |
1473 | ||
1474 | Option::Option(const struct sr_option *structure, | |
1475 | shared_ptr<const struct sr_option *> structure_array) : | |
1476 | _structure(structure), | |
1477 | _structure_array(move(structure_array)) | |
1478 | { | |
1479 | } | |
1480 | ||
1481 | Option::~Option() | |
1482 | { | |
1483 | } | |
1484 | ||
1485 | string Option::id() const | |
1486 | { | |
1487 | return valid_string(_structure->id); | |
1488 | } | |
1489 | ||
1490 | string Option::name() const | |
1491 | { | |
1492 | return valid_string(_structure->name); | |
1493 | } | |
1494 | ||
1495 | string Option::description() const | |
1496 | { | |
1497 | return valid_string(_structure->desc); | |
1498 | } | |
1499 | ||
1500 | Glib::VariantBase Option::default_value() const | |
1501 | { | |
1502 | return Glib::VariantBase(_structure->def, true); | |
1503 | } | |
1504 | ||
1505 | vector<Glib::VariantBase> Option::values() const | |
1506 | { | |
1507 | vector<Glib::VariantBase> result; | |
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 | } | |
1512 | return result; | |
1513 | } | |
1514 | ||
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 | ||
1537 | OutputFormat::OutputFormat(const struct sr_output_module *structure) : | |
1538 | _structure(structure) | |
1539 | { | |
1540 | } | |
1541 | ||
1542 | OutputFormat::~OutputFormat() | |
1543 | { | |
1544 | } | |
1545 | ||
1546 | string OutputFormat::name() const | |
1547 | { | |
1548 | return valid_string(sr_output_id_get(_structure)); | |
1549 | } | |
1550 | ||
1551 | string OutputFormat::description() const | |
1552 | { | |
1553 | return valid_string(sr_output_description_get(_structure)); | |
1554 | } | |
1555 | ||
1556 | vector<string> OutputFormat::extensions() const | |
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 | ||
1565 | map<string, shared_ptr<Option>> OutputFormat::options() | |
1566 | { | |
1567 | map<string, shared_ptr<Option>> result; | |
1568 | ||
1569 | if (const struct sr_option **options = sr_output_options_get(_structure)) { | |
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>{}}; | |
1576 | result.emplace(opt->id(), move(opt)); | |
1577 | } | |
1578 | } | |
1579 | return result; | |
1580 | } | |
1581 | ||
1582 | shared_ptr<Output> OutputFormat::create_output( | |
1583 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) | |
1584 | { | |
1585 | return shared_ptr<Output>{ | |
1586 | new Output{shared_from_this(), move(device), move(options)}, | |
1587 | default_delete<Output>{}}; | |
1588 | } | |
1589 | ||
1590 | shared_ptr<Output> OutputFormat::create_output(string filename, | |
1591 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) | |
1592 | { | |
1593 | return shared_ptr<Output>{ | |
1594 | new Output{move(filename), shared_from_this(), move(device), move(options)}, | |
1595 | default_delete<Output>{}}; | |
1596 | } | |
1597 | ||
1598 | bool OutputFormat::test_flag(const OutputFlag *flag) const | |
1599 | { | |
1600 | return sr_output_test_flag(_structure, flag->id()); | |
1601 | } | |
1602 | ||
1603 | Output::Output(shared_ptr<OutputFormat> format, | |
1604 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) : | |
1605 | _structure(sr_output_new(format->_structure, | |
1606 | map_to_hash_variant(options), device->_structure, nullptr)), | |
1607 | _format(move(format)), | |
1608 | _device(move(device)), | |
1609 | _options(move(options)) | |
1610 | { | |
1611 | } | |
1612 | ||
1613 | Output::Output(string filename, shared_ptr<OutputFormat> format, | |
1614 | shared_ptr<Device> device, map<string, Glib::VariantBase> options) : | |
1615 | _structure(sr_output_new(format->_structure, | |
1616 | map_to_hash_variant(options), device->_structure, filename.c_str())), | |
1617 | _format(move(format)), | |
1618 | _device(move(device)), | |
1619 | _options(move(options)) | |
1620 | { | |
1621 | } | |
1622 | ||
1623 | Output::~Output() | |
1624 | { | |
1625 | check(sr_output_free(_structure)); | |
1626 | } | |
1627 | ||
1628 | string Output::receive(shared_ptr<Packet> packet) | |
1629 | { | |
1630 | GString *out; | |
1631 | check(sr_output_send(_structure, packet->_structure, &out)); | |
1632 | if (out) { | |
1633 | auto result = string(out->str, out->str + out->len); | |
1634 | g_string_free(out, true); | |
1635 | return result; | |
1636 | } else { | |
1637 | return string(); | |
1638 | } | |
1639 | } | |
1640 | ||
1641 | #include <enums.cpp> | |
1642 | ||
1643 | } |