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