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