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