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