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