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