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