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