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