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