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