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