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