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