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