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