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