]>
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 | ||
000f504f DE |
20 | /* Needed for isascii(), as used in the GNU libstdc++ headers */ |
21 | #ifndef _XOPEN_SOURCE | |
22 | #define _XOPEN_SOURCE 600 | |
23 | #endif | |
24 | ||
6ec6c43b | 25 | #include <config.h> |
b5f07319 | 26 | #include <libsigrokcxx/libsigrokcxx.hpp> |
c23c8659 | 27 | |
f36ca889 | 28 | #include <sstream> |
35114c33 | 29 | #include <cmath> |
f36ca889 | 30 | |
c23c8659 ML |
31 | namespace sigrok |
32 | { | |
33 | ||
c23c8659 ML |
34 | /** Helper function to translate C errors to C++ exceptions. */ |
35 | static void check(int result) | |
36 | { | |
37 | if (result != SR_OK) | |
38 | throw Error(result); | |
39 | } | |
40 | ||
41 | /** Helper function to obtain valid strings from possibly null input. */ | |
58e21229 | 42 | static inline const char *valid_string(const char *input) |
c23c8659 | 43 | { |
58e21229 | 44 | return (input) ? input : ""; |
c23c8659 ML |
45 | } |
46 | ||
58aa1f83 | 47 | /** Helper function to convert between map<string, VariantBase> and GHashTable */ |
d370545d | 48 | static GHashTable *map_to_hash_variant(const map<string, Glib::VariantBase> &input) |
58aa1f83 | 49 | { |
f17b4546 | 50 | auto *const output = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
ce3e1e61 | 51 | reinterpret_cast<GDestroyNotify>(&g_variant_unref)); |
f17b4546 | 52 | for (const auto &entry : input) |
58aa1f83 ML |
53 | g_hash_table_insert(output, |
54 | g_strdup(entry.first.c_str()), | |
55 | entry.second.gobj_copy()); | |
d370545d | 56 | return output; |
58aa1f83 ML |
57 | } |
58 | ||
c23c8659 ML |
59 | Error::Error(int result) : result(result) |
60 | { | |
61 | } | |
62 | ||
15914cdb | 63 | const char *Error::what() const noexcept |
c23c8659 ML |
64 | { |
65 | return sr_strerror(result); | |
66 | } | |
67 | ||
15914cdb | 68 | Error::~Error() noexcept |
c23c8659 ML |
69 | { |
70 | } | |
71 | ||
e2eaf858 DE |
72 | ResourceReader::~ResourceReader() |
73 | { | |
74 | } | |
75 | ||
76 | SR_PRIV int ResourceReader::open_callback(struct sr_resource *res, | |
0ab8e5d2 | 77 | const char *name, void *cb_data) noexcept |
e2eaf858 DE |
78 | { |
79 | try { | |
80 | auto *const reader = static_cast<ResourceReader*>(cb_data); | |
81 | reader->open(res, name); | |
82 | } catch (const Error &err) { | |
83 | return err.result; | |
84 | } catch (...) { | |
85 | return SR_ERR; | |
86 | } | |
87 | return SR_OK; | |
88 | } | |
89 | ||
0ab8e5d2 DE |
90 | SR_PRIV int ResourceReader::close_callback(struct sr_resource *res, |
91 | void *cb_data) noexcept | |
e2eaf858 DE |
92 | { |
93 | try { | |
94 | auto *const reader = static_cast<ResourceReader*>(cb_data); | |
95 | reader->close(res); | |
96 | } catch (const Error &err) { | |
97 | return err.result; | |
98 | } catch (...) { | |
99 | return SR_ERR; | |
100 | } | |
101 | return SR_OK; | |
102 | } | |
103 | ||
104 | SR_PRIV ssize_t ResourceReader::read_callback(const struct sr_resource *res, | |
0ab8e5d2 | 105 | void *buf, size_t count, void *cb_data) noexcept |
e2eaf858 DE |
106 | { |
107 | try { | |
108 | auto *const reader = static_cast<ResourceReader*>(cb_data); | |
109 | return reader->read(res, buf, count); | |
110 | } catch (const Error &err) { | |
111 | return err.result; | |
112 | } catch (...) { | |
113 | return SR_ERR; | |
114 | } | |
115 | } | |
116 | ||
c23c8659 ML |
117 | shared_ptr<Context> Context::create() |
118 | { | |
119 | return shared_ptr<Context>(new Context(), Context::Deleter()); | |
120 | } | |
121 | ||
122 | Context::Context() : | |
d5d7b09e | 123 | _structure(nullptr), |
58e21229 | 124 | _session(nullptr) |
c23c8659 | 125 | { |
3b161085 | 126 | check(sr_init(&_structure)); |
90e89c2a | 127 | |
f17b4546 DE |
128 | if (struct sr_dev_driver **driver_list = sr_driver_list(_structure)) |
129 | for (int i = 0; driver_list[i]; i++) { | |
130 | unique_ptr<Driver> driver {new Driver{driver_list[i]}}; | |
131 | _drivers.emplace(driver->name(), move(driver)); | |
132 | } | |
133 | ||
134 | if (const struct sr_input_module **input_list = sr_input_list()) | |
135 | for (int i = 0; input_list[i]; i++) { | |
136 | unique_ptr<InputFormat> input {new InputFormat{input_list[i]}}; | |
137 | _input_formats.emplace(input->name(), move(input)); | |
138 | } | |
139 | ||
140 | if (const struct sr_output_module **output_list = sr_output_list()) | |
141 | for (int i = 0; output_list[i]; i++) { | |
142 | unique_ptr<OutputFormat> output {new OutputFormat{output_list[i]}}; | |
143 | _output_formats.emplace(output->name(), move(output)); | |
144 | } | |
c23c8659 ML |
145 | } |
146 | ||
3b161085 | 147 | string Context::package_version() |
c23c8659 ML |
148 | { |
149 | return sr_package_version_string_get(); | |
150 | } | |
151 | ||
3b161085 | 152 | string Context::lib_version() |
c23c8659 ML |
153 | { |
154 | return sr_lib_version_string_get(); | |
155 | } | |
156 | ||
3b161085 | 157 | map<string, shared_ptr<Driver>> Context::drivers() |
c23c8659 ML |
158 | { |
159 | map<string, shared_ptr<Driver>> result; | |
f17b4546 | 160 | for (const auto &entry: _drivers) |
c23c8659 | 161 | { |
f17b4546 DE |
162 | const auto &name = entry.first; |
163 | const auto &driver = entry.second; | |
164 | result.emplace(name, driver->share_owned_by(shared_from_this())); | |
c23c8659 ML |
165 | } |
166 | return result; | |
167 | } | |
168 | ||
3b161085 | 169 | map<string, shared_ptr<InputFormat>> Context::input_formats() |
c23c8659 ML |
170 | { |
171 | map<string, shared_ptr<InputFormat>> result; | |
f17b4546 | 172 | for (const auto &entry: _input_formats) |
c23c8659 | 173 | { |
f17b4546 DE |
174 | const auto &name = entry.first; |
175 | const auto &input_format = entry.second; | |
176 | result.emplace(name, input_format->share_owned_by(shared_from_this())); | |
c23c8659 ML |
177 | } |
178 | return result; | |
179 | } | |
180 | ||
3b161085 | 181 | map<string, shared_ptr<OutputFormat>> Context::output_formats() |
c23c8659 ML |
182 | { |
183 | map<string, shared_ptr<OutputFormat>> result; | |
f17b4546 | 184 | for (const auto &entry: _output_formats) |
c23c8659 | 185 | { |
f17b4546 DE |
186 | const auto &name = entry.first; |
187 | const auto &output_format = entry.second; | |
188 | result.emplace(name, output_format->share_owned_by(shared_from_this())); | |
c23c8659 ML |
189 | } |
190 | return result; | |
191 | } | |
192 | ||
193 | Context::~Context() | |
194 | { | |
3b161085 | 195 | check(sr_exit(_structure)); |
c23c8659 ML |
196 | } |
197 | ||
15bebf57 | 198 | const LogLevel *Context::log_level() const |
c23c8659 ML |
199 | { |
200 | return LogLevel::get(sr_log_loglevel_get()); | |
201 | } | |
202 | ||
203 | void Context::set_log_level(const LogLevel *level) | |
204 | { | |
3b161085 | 205 | check(sr_log_loglevel_set(level->id())); |
c23c8659 ML |
206 | } |
207 | ||
0ab8e5d2 DE |
208 | static int call_log_callback(void *cb_data, int loglevel, |
209 | const char *format, va_list args) noexcept | |
c23c8659 | 210 | { |
58e21229 DE |
211 | const unique_ptr<char, decltype(&g_free)> |
212 | message {g_strdup_vprintf(format, args), &g_free}; | |
c23c8659 | 213 | |
ce3e1e61 | 214 | auto *const callback = static_cast<LogCallbackFunction *>(cb_data); |
c23c8659 ML |
215 | |
216 | try | |
217 | { | |
ce3e1e61 | 218 | (*callback)(LogLevel::get(loglevel), message.get()); |
c23c8659 ML |
219 | } |
220 | catch (Error e) | |
221 | { | |
222 | return e.result; | |
223 | } | |
224 | ||
225 | return SR_OK; | |
226 | } | |
227 | ||
228 | void Context::set_log_callback(LogCallbackFunction callback) | |
229 | { | |
d370545d | 230 | _log_callback = move(callback); |
3b161085 | 231 | check(sr_log_callback_set(call_log_callback, &_log_callback)); |
e2eaf858 | 232 | } |
c23c8659 ML |
233 | |
234 | void Context::set_log_callback_default() | |
235 | { | |
236 | check(sr_log_callback_set_default()); | |
3b161085 | 237 | _log_callback = nullptr; |
e2eaf858 DE |
238 | } |
239 | ||
240 | void Context::set_resource_reader(ResourceReader *reader) | |
241 | { | |
242 | if (reader) { | |
243 | check(sr_resource_set_hooks(_structure, | |
244 | &ResourceReader::open_callback, | |
245 | &ResourceReader::close_callback, | |
246 | &ResourceReader::read_callback, reader)); | |
247 | } else { | |
248 | check(sr_resource_set_hooks(_structure, | |
249 | nullptr, nullptr, nullptr, nullptr)); | |
250 | } | |
251 | } | |
c23c8659 ML |
252 | |
253 | shared_ptr<Session> Context::create_session() | |
254 | { | |
255 | return shared_ptr<Session>( | |
256 | new Session(shared_from_this()), Session::Deleter()); | |
257 | } | |
258 | ||
9fa5b426 ML |
259 | shared_ptr<UserDevice> Context::create_user_device( |
260 | string vendor, string model, string version) | |
261 | { | |
262 | return shared_ptr<UserDevice>( | |
263 | new UserDevice(vendor, model, version), UserDevice::Deleter()); | |
264 | } | |
265 | ||
304be4a7 ML |
266 | shared_ptr<Packet> Context::create_header_packet(Glib::TimeVal start_time) |
267 | { | |
268 | auto header = g_new(struct sr_datafeed_header, 1); | |
269 | header->feed_version = 1; | |
270 | header->starttime.tv_sec = start_time.tv_sec; | |
271 | header->starttime.tv_usec = start_time.tv_usec; | |
272 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
273 | packet->type = SR_DF_HEADER; | |
274 | packet->payload = header; | |
275 | return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter()); | |
276 | } | |
277 | ||
278 | shared_ptr<Packet> Context::create_meta_packet( | |
d370545d | 279 | const map<const ConfigKey *, Glib::VariantBase> &config) |
304be4a7 ML |
280 | { |
281 | auto meta = g_new0(struct sr_datafeed_meta, 1); | |
f17b4546 | 282 | for (const auto &input : config) |
304be4a7 | 283 | { |
f17b4546 DE |
284 | const auto &key = input.first; |
285 | const auto &value = input.second; | |
286 | auto *const output = g_new(struct sr_config, 1); | |
304be4a7 | 287 | output->key = key->id(); |
f17b4546 | 288 | output->data = value.gobj_copy(); |
304be4a7 ML |
289 | meta->config = g_slist_append(meta->config, output); |
290 | } | |
291 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
292 | packet->type = SR_DF_META; | |
293 | packet->payload = meta; | |
294 | return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter()); | |
295 | } | |
296 | ||
297 | shared_ptr<Packet> Context::create_logic_packet( | |
298 | void *data_pointer, size_t data_length, unsigned int unit_size) | |
299 | { | |
300 | auto logic = g_new(struct sr_datafeed_logic, 1); | |
301 | logic->length = data_length; | |
302 | logic->unitsize = unit_size; | |
303 | logic->data = data_pointer; | |
304 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
305 | packet->type = SR_DF_LOGIC; | |
306 | packet->payload = logic; | |
307 | return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter()); | |
308 | } | |
309 | ||
dd13d47a | 310 | shared_ptr<Packet> Context::create_analog_packet( |
d370545d | 311 | const vector<shared_ptr<Channel> > &channels, |
304be4a7 | 312 | float *data_pointer, unsigned int num_samples, const Quantity *mq, |
d370545d | 313 | const Unit *unit, const vector<const QuantityFlag *> &mqflags) |
304be4a7 | 314 | { |
dd13d47a UH |
315 | auto analog = g_new0(struct sr_datafeed_analog, 1); |
316 | auto meaning = g_new0(struct sr_analog_meaning, 1); | |
317 | ||
318 | analog->meaning = meaning; | |
319 | ||
f17b4546 | 320 | for (const auto &channel : channels) |
dd13d47a | 321 | meaning->channels = g_slist_append(meaning->channels, channel->_structure); |
304be4a7 | 322 | analog->num_samples = num_samples; |
ce3e1e61 DE |
323 | meaning->mq = static_cast<sr_mq>(mq->id()); |
324 | meaning->unit = static_cast<sr_unit>(unit->id()); | |
325 | meaning->mqflags = static_cast<sr_mqflag>(QuantityFlag::mask_from_flags(mqflags)); | |
304be4a7 ML |
326 | analog->data = data_pointer; |
327 | auto packet = g_new(struct sr_datafeed_packet, 1); | |
dd13d47a | 328 | packet->type = SR_DF_ANALOG; |
304be4a7 ML |
329 | packet->payload = analog; |
330 | return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter()); | |
331 | } | |
332 | ||
c23c8659 ML |
333 | shared_ptr<Session> Context::load_session(string filename) |
334 | { | |
335 | return shared_ptr<Session>( | |
d370545d | 336 | new Session(shared_from_this(), move(filename)), Session::Deleter()); |
c23c8659 ML |
337 | } |
338 | ||
339 | shared_ptr<Trigger> Context::create_trigger(string name) | |
340 | { | |
341 | return shared_ptr<Trigger>( | |
d370545d | 342 | new Trigger(shared_from_this(), move(name)), Trigger::Deleter()); |
c23c8659 ML |
343 | } |
344 | ||
ca3291e3 ML |
345 | shared_ptr<Input> Context::open_file(string filename) |
346 | { | |
f88c7373 BV |
347 | const struct sr_input *input; |
348 | ||
d4cf45e5 | 349 | check(sr_input_scan_file(filename.c_str(), &input)); |
ca3291e3 ML |
350 | return shared_ptr<Input>( |
351 | new Input(shared_from_this(), input), Input::Deleter()); | |
352 | } | |
353 | ||
354 | shared_ptr<Input> Context::open_stream(string header) | |
355 | { | |
f88c7373 BV |
356 | const struct sr_input *input; |
357 | ||
ca3291e3 | 358 | auto gstr = g_string_new(header.c_str()); |
f88c7373 BV |
359 | auto ret = sr_input_scan_buffer(gstr, &input); |
360 | g_string_free(gstr, true); | |
361 | check(ret); | |
ca3291e3 ML |
362 | return shared_ptr<Input>( |
363 | new Input(shared_from_this(), input), Input::Deleter()); | |
364 | } | |
365 | ||
15bebf57 | 366 | map<string, string> Context::serials(shared_ptr<Driver> driver) const |
24287ea9 | 367 | { |
58e21229 | 368 | GSList *serial_list = sr_serial_list(driver ? driver->_structure : nullptr); |
24287ea9 AJ |
369 | map<string, string> serials; |
370 | ||
371 | for (GSList *serial = serial_list; serial; serial = serial->next) { | |
ce3e1e61 | 372 | auto *const port = static_cast<sr_serial_port *>(serial->data); |
24287ea9 AJ |
373 | serials[string(port->name)] = string(port->description); |
374 | } | |
375 | ||
ce3e1e61 DE |
376 | g_slist_free_full(serial_list, |
377 | reinterpret_cast<GDestroyNotify>(&sr_serial_free)); | |
24287ea9 AJ |
378 | return serials; |
379 | } | |
380 | ||
c23c8659 | 381 | Driver::Driver(struct sr_dev_driver *structure) : |
58e21229 | 382 | Configurable(structure, nullptr, nullptr), |
d5d7b09e | 383 | _structure(structure), |
3b161085 | 384 | _initialized(false) |
c23c8659 ML |
385 | { |
386 | } | |
387 | ||
388 | Driver::~Driver() | |
389 | { | |
c23c8659 ML |
390 | } |
391 | ||
15bebf57 | 392 | string Driver::name() const |
c23c8659 | 393 | { |
3b161085 | 394 | return valid_string(_structure->name); |
c23c8659 ML |
395 | } |
396 | ||
15bebf57 | 397 | string Driver::long_name() const |
c23c8659 | 398 | { |
3b161085 | 399 | return valid_string(_structure->longname); |
c23c8659 ML |
400 | } |
401 | ||
402 | vector<shared_ptr<HardwareDevice>> Driver::scan( | |
d370545d | 403 | const map<const ConfigKey *, Glib::VariantBase> &options) |
c23c8659 ML |
404 | { |
405 | /* Initialise the driver if not yet done. */ | |
3b161085 | 406 | if (!_initialized) |
c23c8659 | 407 | { |
3b161085 ML |
408 | check(sr_driver_init(_parent->_structure, _structure)); |
409 | _initialized = true; | |
c23c8659 ML |
410 | } |
411 | ||
c23c8659 | 412 | /* Translate scan options to GSList of struct sr_config pointers. */ |
58e21229 | 413 | GSList *option_list = nullptr; |
f17b4546 | 414 | for (const auto &entry : options) |
c23c8659 | 415 | { |
f17b4546 DE |
416 | const auto &key = entry.first; |
417 | const auto &value = entry.second; | |
418 | auto *const config = g_new(struct sr_config, 1); | |
3b161085 | 419 | config->key = key->id(); |
f17b4546 | 420 | config->data = const_cast<GVariant*>(value.gobj()); |
c23c8659 ML |
421 | option_list = g_slist_append(option_list, config); |
422 | } | |
423 | ||
424 | /* Run scan. */ | |
3b161085 | 425 | GSList *device_list = sr_driver_scan(_structure, option_list); |
c23c8659 ML |
426 | |
427 | /* Free option list. */ | |
428 | g_slist_free_full(option_list, g_free); | |
429 | ||
a4e47454 | 430 | |
c23c8659 | 431 | /* Create device objects. */ |
a4e47454 | 432 | vector<shared_ptr<HardwareDevice>> result; |
c23c8659 ML |
433 | for (GSList *device = device_list; device; device = device->next) |
434 | { | |
ce3e1e61 | 435 | auto *const sdi = static_cast<struct sr_dev_inst *>(device->data); |
a4e47454 ML |
436 | result.push_back(shared_ptr<HardwareDevice>( |
437 | new HardwareDevice(shared_from_this(), sdi), | |
438 | HardwareDevice::Deleter())); | |
c23c8659 ML |
439 | } |
440 | ||
441 | /* Free GSList returned from scan. */ | |
442 | g_slist_free(device_list); | |
443 | ||
c23c8659 ML |
444 | return result; |
445 | } | |
446 | ||
447 | Configurable::Configurable( | |
448 | struct sr_dev_driver *driver, | |
449 | struct sr_dev_inst *sdi, | |
450 | struct sr_channel_group *cg) : | |
451 | config_driver(driver), | |
452 | config_sdi(sdi), | |
453 | config_channel_group(cg) | |
454 | { | |
455 | } | |
456 | ||
457 | Configurable::~Configurable() | |
458 | { | |
459 | } | |
460 | ||
15bebf57 | 461 | Glib::VariantBase Configurable::config_get(const ConfigKey *key) const |
c23c8659 ML |
462 | { |
463 | GVariant *data; | |
464 | check(sr_config_get( | |
465 | config_driver, config_sdi, config_channel_group, | |
3b161085 | 466 | key->id(), &data)); |
c23c8659 ML |
467 | return Glib::VariantBase(data); |
468 | } | |
469 | ||
d370545d | 470 | void Configurable::config_set(const ConfigKey *key, const Glib::VariantBase &value) |
c23c8659 ML |
471 | { |
472 | check(sr_config_set( | |
473 | config_sdi, config_channel_group, | |
d370545d | 474 | key->id(), const_cast<GVariant*>(value.gobj()))); |
c23c8659 ML |
475 | } |
476 | ||
15bebf57 | 477 | Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) const |
c23c8659 ML |
478 | { |
479 | GVariant *data; | |
480 | check(sr_config_list( | |
481 | config_driver, config_sdi, config_channel_group, | |
3b161085 | 482 | key->id(), &data)); |
e194c011 | 483 | return Glib::VariantContainerBase(data); |
c23c8659 ML |
484 | } |
485 | ||
4c7c4194 | 486 | map<const ConfigKey *, set<Capability>> Configurable::config_keys(const ConfigKey *key) |
d54190a3 ML |
487 | { |
488 | GVariant *gvar_opts; | |
489 | gsize num_opts; | |
03a4c07a | 490 | const uint32_t *opts; |
4c7c4194 | 491 | map<const ConfigKey *, set<Capability>> result; |
d54190a3 ML |
492 | |
493 | check(sr_config_list( | |
494 | config_driver, config_sdi, config_channel_group, | |
3b161085 | 495 | key->id(), &gvar_opts)); |
d54190a3 | 496 | |
ce3e1e61 DE |
497 | opts = static_cast<const uint32_t *>(g_variant_get_fixed_array( |
498 | gvar_opts, &num_opts, sizeof(uint32_t))); | |
d54190a3 ML |
499 | |
500 | for (gsize i = 0; i < num_opts; i++) | |
4c7c4194 ML |
501 | { |
502 | auto key = ConfigKey::get(opts[i] & SR_CONF_MASK); | |
503 | set<Capability> capabilities; | |
504 | if (opts[i] & SR_CONF_GET) | |
505 | capabilities.insert(GET); | |
506 | if (opts[i] & SR_CONF_SET) | |
507 | capabilities.insert(SET); | |
508 | if (opts[i] & SR_CONF_LIST) | |
509 | capabilities.insert(LIST); | |
510 | result[key] = capabilities; | |
511 | } | |
d54190a3 ML |
512 | |
513 | g_variant_unref(gvar_opts); | |
514 | ||
515 | return result; | |
516 | } | |
517 | ||
d9eed47d | 518 | bool Configurable::config_check(const ConfigKey *key, |
15bebf57 | 519 | const ConfigKey *index_key) const |
d9eed47d ML |
520 | { |
521 | GVariant *gvar_opts; | |
522 | gsize num_opts; | |
03a4c07a | 523 | const uint32_t *opts; |
d9eed47d ML |
524 | |
525 | if (sr_config_list(config_driver, config_sdi, config_channel_group, | |
3b161085 | 526 | index_key->id(), &gvar_opts) != SR_OK) |
d9eed47d ML |
527 | return false; |
528 | ||
ce3e1e61 DE |
529 | opts = static_cast<const uint32_t *>(g_variant_get_fixed_array( |
530 | gvar_opts, &num_opts, sizeof(uint32_t))); | |
d9eed47d ML |
531 | |
532 | for (gsize i = 0; i < num_opts; i++) | |
533 | { | |
ce3e1e61 | 534 | if ((opts[i] & SR_CONF_MASK) == unsigned(key->id())) |
d9eed47d ML |
535 | { |
536 | g_variant_unref(gvar_opts); | |
537 | return true; | |
538 | } | |
539 | } | |
540 | ||
541 | g_variant_unref(gvar_opts); | |
542 | ||
543 | return false; | |
544 | } | |
545 | ||
c23c8659 | 546 | Device::Device(struct sr_dev_inst *structure) : |
58e21229 | 547 | Configurable(sr_dev_inst_driver_get(structure), structure, nullptr), |
3b161085 | 548 | _structure(structure) |
c23c8659 | 549 | { |
80fe5247 | 550 | for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next) |
c23c8659 | 551 | { |
f17b4546 DE |
552 | auto *const ch = static_cast<struct sr_channel *>(entry->data); |
553 | unique_ptr<Channel> channel {new Channel{ch}}; | |
554 | _channels.emplace(ch, move(channel)); | |
c23c8659 | 555 | } |
6be7a7f2 | 556 | |
80fe5247 | 557 | for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next) |
6be7a7f2 | 558 | { |
f17b4546 DE |
559 | auto *const cg = static_cast<struct sr_channel_group *>(entry->data); |
560 | unique_ptr<ChannelGroup> group {new ChannelGroup{this, cg}}; | |
561 | _channel_groups.emplace(group->name(), move(group)); | |
6be7a7f2 | 562 | } |
c23c8659 ML |
563 | } |
564 | ||
565 | Device::~Device() | |
f17b4546 | 566 | {} |
c23c8659 | 567 | |
15bebf57 | 568 | string Device::vendor() const |
c23c8659 | 569 | { |
80fe5247 | 570 | return valid_string(sr_dev_inst_vendor_get(_structure)); |
c23c8659 ML |
571 | } |
572 | ||
15bebf57 | 573 | string Device::model() const |
c23c8659 | 574 | { |
80fe5247 | 575 | return valid_string(sr_dev_inst_model_get(_structure)); |
c23c8659 ML |
576 | } |
577 | ||
15bebf57 | 578 | string Device::version() const |
c23c8659 | 579 | { |
80fe5247 | 580 | return valid_string(sr_dev_inst_version_get(_structure)); |
c23c8659 ML |
581 | } |
582 | ||
15bebf57 | 583 | string Device::serial_number() const |
d1075e5a | 584 | { |
80fe5247 | 585 | return valid_string(sr_dev_inst_sernum_get(_structure)); |
d1075e5a ML |
586 | } |
587 | ||
15bebf57 | 588 | string Device::connection_id() const |
d1075e5a | 589 | { |
80fe5247 | 590 | return valid_string(sr_dev_inst_connid_get(_structure)); |
d1075e5a ML |
591 | } |
592 | ||
3b161085 | 593 | vector<shared_ptr<Channel>> Device::channels() |
c23c8659 ML |
594 | { |
595 | vector<shared_ptr<Channel>> result; | |
ce3e1e61 DE |
596 | for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next) { |
597 | auto *const ch = static_cast<struct sr_channel *>(channel->data); | |
b6ab954d | 598 | result.push_back(_channels[ch]->share_owned_by(get_shared_from_this())); |
ce3e1e61 | 599 | } |
c23c8659 ML |
600 | return result; |
601 | } | |
602 | ||
4178d971 ML |
603 | shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr) |
604 | { | |
b6ab954d | 605 | return _channels[ptr]->share_owned_by(get_shared_from_this()); |
4178d971 ML |
606 | } |
607 | ||
6be7a7f2 | 608 | map<string, shared_ptr<ChannelGroup>> |
3b161085 | 609 | Device::channel_groups() |
6be7a7f2 ML |
610 | { |
611 | map<string, shared_ptr<ChannelGroup>> result; | |
f17b4546 | 612 | for (const auto &entry: _channel_groups) |
6be7a7f2 | 613 | { |
f17b4546 DE |
614 | const auto &name = entry.first; |
615 | const auto &channel_group = entry.second; | |
616 | result.emplace(name, channel_group->share_owned_by(get_shared_from_this())); | |
6be7a7f2 ML |
617 | } |
618 | return result; | |
619 | } | |
620 | ||
c23c8659 ML |
621 | void Device::open() |
622 | { | |
3b161085 | 623 | check(sr_dev_open(_structure)); |
c23c8659 ML |
624 | } |
625 | ||
626 | void Device::close() | |
627 | { | |
3b161085 | 628 | check(sr_dev_close(_structure)); |
c23c8659 ML |
629 | } |
630 | ||
a4e47454 ML |
631 | HardwareDevice::HardwareDevice(shared_ptr<Driver> driver, |
632 | struct sr_dev_inst *structure) : | |
c23c8659 | 633 | Device(structure), |
d370545d | 634 | _driver(move(driver)) |
c23c8659 | 635 | { |
c23c8659 ML |
636 | } |
637 | ||
638 | HardwareDevice::~HardwareDevice() | |
639 | { | |
c23c8659 ML |
640 | } |
641 | ||
d01d2314 ML |
642 | shared_ptr<Device> HardwareDevice::get_shared_from_this() |
643 | { | |
bf52cc8c | 644 | return static_pointer_cast<Device>(shared_from_this()); |
d01d2314 ML |
645 | } |
646 | ||
3b161085 | 647 | shared_ptr<Driver> HardwareDevice::driver() |
c23c8659 | 648 | { |
a4e47454 | 649 | return _driver; |
c23c8659 ML |
650 | } |
651 | ||
9fa5b426 | 652 | UserDevice::UserDevice(string vendor, string model, string version) : |
d5d7b09e DE |
653 | Device(sr_dev_inst_user_new( |
654 | vendor.c_str(), model.c_str(), version.c_str())) | |
9fa5b426 ML |
655 | { |
656 | } | |
657 | ||
658 | UserDevice::~UserDevice() | |
659 | { | |
660 | } | |
661 | ||
662 | shared_ptr<Device> UserDevice::get_shared_from_this() | |
663 | { | |
664 | return static_pointer_cast<Device>(shared_from_this()); | |
665 | } | |
666 | ||
667 | shared_ptr<Channel> UserDevice::add_channel(unsigned int index, | |
668 | const ChannelType *type, string name) | |
669 | { | |
670 | check(sr_dev_inst_channel_add(Device::_structure, | |
671 | index, type->id(), name.c_str())); | |
ce3e1e61 DE |
672 | GSList *const last = g_slist_last(sr_dev_inst_channels_get(Device::_structure)); |
673 | auto *const ch = static_cast<struct sr_channel *>(last->data); | |
f17b4546 DE |
674 | unique_ptr<Channel> channel {new Channel{ch}}; |
675 | _channels.emplace(ch, move(channel)); | |
ce3e1e61 | 676 | return get_channel(ch); |
9fa5b426 ML |
677 | } |
678 | ||
c23c8659 | 679 | Channel::Channel(struct sr_channel *structure) : |
d5d7b09e | 680 | _structure(structure), |
3b161085 | 681 | _type(ChannelType::get(_structure->type)) |
c23c8659 ML |
682 | { |
683 | } | |
684 | ||
685 | Channel::~Channel() | |
686 | { | |
687 | } | |
688 | ||
15bebf57 | 689 | string Channel::name() const |
c23c8659 | 690 | { |
3b161085 | 691 | return valid_string(_structure->name); |
c23c8659 ML |
692 | } |
693 | ||
694 | void Channel::set_name(string name) | |
695 | { | |
6f1346fb | 696 | check(sr_dev_channel_name_set(_structure, name.c_str())); |
c23c8659 ML |
697 | } |
698 | ||
15bebf57 | 699 | const ChannelType *Channel::type() const |
c23c8659 | 700 | { |
3b161085 | 701 | return ChannelType::get(_structure->type); |
c23c8659 ML |
702 | } |
703 | ||
15bebf57 | 704 | bool Channel::enabled() const |
c23c8659 | 705 | { |
3b161085 | 706 | return _structure->enabled; |
c23c8659 ML |
707 | } |
708 | ||
709 | void Channel::set_enabled(bool value) | |
710 | { | |
6f1346fb | 711 | check(sr_dev_channel_enable(_structure, value)); |
c23c8659 ML |
712 | } |
713 | ||
15bebf57 | 714 | unsigned int Channel::index() const |
06bd935e | 715 | { |
3b161085 | 716 | return _structure->index; |
06bd935e ML |
717 | } |
718 | ||
f17b4546 | 719 | ChannelGroup::ChannelGroup(const Device *device, |
c23c8659 | 720 | struct sr_channel_group *structure) : |
80fe5247 | 721 | Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure) |
c23c8659 | 722 | { |
d5d7b09e | 723 | for (GSList *entry = config_channel_group->channels; entry; entry = entry->next) { |
ce3e1e61 | 724 | auto *const ch = static_cast<struct sr_channel *>(entry->data); |
f17b4546 DE |
725 | /* Note: This relies on Device::_channels to keep the Channel |
726 | * objects around over the lifetime of the ChannelGroup. */ | |
727 | _channels.push_back(device->_channels.find(ch)->second.get()); | |
ce3e1e61 | 728 | } |
c23c8659 ML |
729 | } |
730 | ||
731 | ChannelGroup::~ChannelGroup() | |
732 | { | |
733 | } | |
734 | ||
15bebf57 | 735 | string ChannelGroup::name() const |
c23c8659 | 736 | { |
d5d7b09e | 737 | return valid_string(config_channel_group->name); |
c23c8659 ML |
738 | } |
739 | ||
3b161085 | 740 | vector<shared_ptr<Channel>> ChannelGroup::channels() |
c23c8659 ML |
741 | { |
742 | vector<shared_ptr<Channel>> result; | |
f17b4546 | 743 | for (const auto &channel : _channels) |
b6ab954d | 744 | result.push_back(channel->share_owned_by(_parent)); |
c23c8659 ML |
745 | return result; |
746 | } | |
747 | ||
748 | Trigger::Trigger(shared_ptr<Context> context, string name) : | |
d5d7b09e | 749 | _structure(sr_trigger_new(name.c_str())), |
d370545d | 750 | _context(move(context)) |
c23c8659 | 751 | { |
f17b4546 DE |
752 | for (auto *stage = _structure->stages; stage; stage = stage->next) { |
753 | unique_ptr<TriggerStage> ts {new TriggerStage{ | |
754 | static_cast<struct sr_trigger_stage *>(stage->data)}}; | |
755 | _stages.push_back(move(ts)); | |
756 | } | |
c23c8659 ML |
757 | } |
758 | ||
759 | Trigger::~Trigger() | |
760 | { | |
3b161085 | 761 | sr_trigger_free(_structure); |
c23c8659 ML |
762 | } |
763 | ||
15bebf57 | 764 | string Trigger::name() const |
c23c8659 | 765 | { |
3b161085 | 766 | return _structure->name; |
c23c8659 ML |
767 | } |
768 | ||
3b161085 | 769 | vector<shared_ptr<TriggerStage>> Trigger::stages() |
c23c8659 ML |
770 | { |
771 | vector<shared_ptr<TriggerStage>> result; | |
f17b4546 | 772 | for (const auto &stage : _stages) |
b6ab954d | 773 | result.push_back(stage->share_owned_by(shared_from_this())); |
c23c8659 ML |
774 | return result; |
775 | } | |
776 | ||
777 | shared_ptr<TriggerStage> Trigger::add_stage() | |
778 | { | |
f17b4546 DE |
779 | unique_ptr<TriggerStage> stage {new TriggerStage{sr_trigger_stage_add(_structure)}}; |
780 | _stages.push_back(move(stage)); | |
781 | return _stages.back()->share_owned_by(shared_from_this()); | |
c23c8659 ML |
782 | } |
783 | ||
d5d7b09e DE |
784 | TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : |
785 | _structure(structure) | |
c23c8659 ML |
786 | { |
787 | } | |
788 | ||
789 | TriggerStage::~TriggerStage() | |
790 | { | |
c23c8659 ML |
791 | } |
792 | ||
15bebf57 | 793 | int TriggerStage::number() const |
c23c8659 | 794 | { |
3b161085 | 795 | return _structure->stage; |
c23c8659 ML |
796 | } |
797 | ||
3b161085 | 798 | vector<shared_ptr<TriggerMatch>> TriggerStage::matches() |
c23c8659 ML |
799 | { |
800 | vector<shared_ptr<TriggerMatch>> result; | |
f17b4546 | 801 | for (const auto &match : _matches) |
b6ab954d | 802 | result.push_back(match->share_owned_by(shared_from_this())); |
c23c8659 ML |
803 | return result; |
804 | } | |
805 | ||
3b161085 ML |
806 | void TriggerStage::add_match(shared_ptr<Channel> channel, |
807 | const TriggerMatchType *type, float value) | |
c23c8659 | 808 | { |
3b161085 ML |
809 | check(sr_trigger_match_add(_structure, |
810 | channel->_structure, type->id(), value)); | |
ce3e1e61 | 811 | GSList *const last = g_slist_last(_structure->matches); |
f17b4546 DE |
812 | unique_ptr<TriggerMatch> match {new TriggerMatch{ |
813 | static_cast<struct sr_trigger_match *>(last->data), | |
814 | move(channel)}}; | |
815 | _matches.push_back(move(match)); | |
c23c8659 ML |
816 | } |
817 | ||
3b161085 ML |
818 | void TriggerStage::add_match(shared_ptr<Channel> channel, |
819 | const TriggerMatchType *type) | |
c23c8659 | 820 | { |
d370545d | 821 | add_match(move(channel), type, NAN); |
c23c8659 ML |
822 | } |
823 | ||
3b161085 ML |
824 | TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, |
825 | shared_ptr<Channel> channel) : | |
d5d7b09e | 826 | _structure(structure), |
d370545d | 827 | _channel(move(channel)) |
c23c8659 ML |
828 | { |
829 | } | |
830 | ||
831 | TriggerMatch::~TriggerMatch() | |
832 | { | |
833 | } | |
834 | ||
3b161085 | 835 | shared_ptr<Channel> TriggerMatch::channel() |
c23c8659 | 836 | { |
3b161085 | 837 | return _channel; |
c23c8659 ML |
838 | } |
839 | ||
15bebf57 | 840 | const TriggerMatchType *TriggerMatch::type() const |
c23c8659 | 841 | { |
3b161085 | 842 | return TriggerMatchType::get(_structure->match); |
c23c8659 ML |
843 | } |
844 | ||
15bebf57 | 845 | float TriggerMatch::value() const |
c23c8659 | 846 | { |
3b161085 | 847 | return _structure->value; |
c23c8659 ML |
848 | } |
849 | ||
850 | DatafeedCallbackData::DatafeedCallbackData(Session *session, | |
851 | DatafeedCallbackFunction callback) : | |
d370545d | 852 | _callback(move(callback)), |
3b161085 | 853 | _session(session) |
c23c8659 ML |
854 | { |
855 | } | |
856 | ||
857 | void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, | |
858 | const struct sr_datafeed_packet *pkt) | |
859 | { | |
ca4e307a | 860 | auto device = _session->get_device(sdi); |
2928f47d | 861 | auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter()); |
d370545d | 862 | _callback(move(device), move(packet)); |
c23c8659 ML |
863 | } |
864 | ||
cac58676 | 865 | SessionDevice::SessionDevice(struct sr_dev_inst *structure) : |
cac58676 ML |
866 | Device(structure) |
867 | { | |
868 | } | |
869 | ||
870 | SessionDevice::~SessionDevice() | |
871 | { | |
872 | } | |
873 | ||
874 | shared_ptr<Device> SessionDevice::get_shared_from_this() | |
875 | { | |
876 | return static_pointer_cast<Device>(shared_from_this()); | |
877 | } | |
878 | ||
c23c8659 | 879 | Session::Session(shared_ptr<Context> context) : |
d5d7b09e | 880 | _structure(nullptr), |
d370545d | 881 | _context(move(context)) |
c23c8659 | 882 | { |
d370545d | 883 | check(sr_session_new(_context->_structure, &_structure)); |
3b161085 | 884 | _context->_session = this; |
c23c8659 ML |
885 | } |
886 | ||
887 | Session::Session(shared_ptr<Context> context, string filename) : | |
d5d7b09e | 888 | _structure(nullptr), |
d370545d DE |
889 | _context(move(context)), |
890 | _filename(move(filename)) | |
c23c8659 | 891 | { |
d370545d | 892 | check(sr_session_load(_context->_structure, _filename.c_str(), &_structure)); |
cac58676 | 893 | GSList *dev_list; |
3b161085 | 894 | check(sr_session_dev_list(_structure, &dev_list)); |
ce3e1e61 DE |
895 | for (GSList *dev = dev_list; dev; dev = dev->next) { |
896 | auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data); | |
f17b4546 DE |
897 | unique_ptr<SessionDevice> device {new SessionDevice{sdi}}; |
898 | _owned_devices.emplace(sdi, move(device)); | |
cac58676 | 899 | } |
3b161085 | 900 | _context->_session = this; |
c23c8659 ML |
901 | } |
902 | ||
903 | Session::~Session() | |
904 | { | |
3b161085 | 905 | check(sr_session_destroy(_structure)); |
ca4e307a ML |
906 | } |
907 | ||
908 | shared_ptr<Device> Session::get_device(const struct sr_dev_inst *sdi) | |
909 | { | |
910 | if (_owned_devices.count(sdi)) | |
911 | return static_pointer_cast<Device>( | |
b6ab954d | 912 | _owned_devices[sdi]->share_owned_by(shared_from_this())); |
ca4e307a ML |
913 | else if (_other_devices.count(sdi)) |
914 | return _other_devices[sdi]; | |
915 | else | |
916 | throw Error(SR_ERR_BUG); | |
c23c8659 ML |
917 | } |
918 | ||
919 | void Session::add_device(shared_ptr<Device> device) | |
920 | { | |
d370545d DE |
921 | const auto dev_struct = device->_structure; |
922 | check(sr_session_dev_add(_structure, dev_struct)); | |
923 | _other_devices[dev_struct] = move(device); | |
c23c8659 ML |
924 | } |
925 | ||
3b161085 | 926 | vector<shared_ptr<Device>> Session::devices() |
c23c8659 ML |
927 | { |
928 | GSList *dev_list; | |
3b161085 | 929 | check(sr_session_dev_list(_structure, &dev_list)); |
c23c8659 | 930 | vector<shared_ptr<Device>> result; |
ce3e1e61 DE |
931 | for (GSList *dev = dev_list; dev; dev = dev->next) { |
932 | auto *const sdi = static_cast<struct sr_dev_inst *>(dev->data); | |
ca4e307a | 933 | result.push_back(get_device(sdi)); |
c23c8659 ML |
934 | } |
935 | return result; | |
936 | } | |
937 | ||
938 | void Session::remove_devices() | |
939 | { | |
ca4e307a | 940 | _other_devices.clear(); |
3b161085 | 941 | check(sr_session_dev_remove_all(_structure)); |
c23c8659 ML |
942 | } |
943 | ||
944 | void Session::start() | |
945 | { | |
3b161085 | 946 | check(sr_session_start(_structure)); |
c23c8659 ML |
947 | } |
948 | ||
949 | void Session::run() | |
950 | { | |
3b161085 | 951 | check(sr_session_run(_structure)); |
c23c8659 ML |
952 | } |
953 | ||
954 | void Session::stop() | |
955 | { | |
3b161085 | 956 | check(sr_session_stop(_structure)); |
c23c8659 ML |
957 | } |
958 | ||
f91cf612 DE |
959 | bool Session::is_running() const |
960 | { | |
961 | const int ret = sr_session_is_running(_structure); | |
962 | if (ret < 0) | |
963 | throw Error{ret}; | |
964 | return (ret != 0); | |
965 | } | |
966 | ||
0ab8e5d2 | 967 | static void session_stopped_callback(void *data) noexcept |
f91cf612 DE |
968 | { |
969 | auto *const callback = static_cast<SessionStoppedCallback*>(data); | |
970 | (*callback)(); | |
971 | } | |
972 | ||
973 | void Session::set_stopped_callback(SessionStoppedCallback callback) | |
974 | { | |
975 | _stopped_callback = move(callback); | |
976 | if (_stopped_callback) | |
977 | check(sr_session_stopped_callback_set(_structure, | |
978 | &session_stopped_callback, &_stopped_callback)); | |
979 | else | |
980 | check(sr_session_stopped_callback_set(_structure, | |
981 | nullptr, nullptr)); | |
982 | } | |
983 | ||
c23c8659 | 984 | static void datafeed_callback(const struct sr_dev_inst *sdi, |
0ab8e5d2 | 985 | const struct sr_datafeed_packet *pkt, void *cb_data) noexcept |
c23c8659 ML |
986 | { |
987 | auto callback = static_cast<DatafeedCallbackData *>(cb_data); | |
988 | callback->run(sdi, pkt); | |
989 | } | |
ce3e1e61 | 990 | |
c23c8659 ML |
991 | void Session::add_datafeed_callback(DatafeedCallbackFunction callback) |
992 | { | |
f17b4546 DE |
993 | unique_ptr<DatafeedCallbackData> cb_data |
994 | {new DatafeedCallbackData{this, move(callback)}}; | |
3b161085 | 995 | check(sr_session_datafeed_callback_add(_structure, |
f17b4546 DE |
996 | &datafeed_callback, cb_data.get())); |
997 | _datafeed_callbacks.push_back(move(cb_data)); | |
c23c8659 ML |
998 | } |
999 | ||
d370545d | 1000 | void Session::remove_datafeed_callbacks() |
c23c8659 | 1001 | { |
3b161085 | 1002 | check(sr_session_datafeed_callback_remove_all(_structure)); |
3b161085 | 1003 | _datafeed_callbacks.clear(); |
c23c8659 ML |
1004 | } |
1005 | ||
3b161085 | 1006 | shared_ptr<Trigger> Session::trigger() |
6fa0eb86 | 1007 | { |
3b161085 | 1008 | return _trigger; |
6fa0eb86 ML |
1009 | } |
1010 | ||
1011 | void Session::set_trigger(shared_ptr<Trigger> trigger) | |
1012 | { | |
e835e808 UH |
1013 | if (!trigger) |
1014 | // Set NULL trigger, i.e. remove any trigger from the session. | |
58e21229 | 1015 | check(sr_session_trigger_set(_structure, nullptr)); |
e835e808 UH |
1016 | else |
1017 | check(sr_session_trigger_set(_structure, trigger->_structure)); | |
d370545d | 1018 | _trigger = move(trigger); |
6fa0eb86 ML |
1019 | } |
1020 | ||
15bebf57 | 1021 | string Session::filename() const |
1411f7d8 ML |
1022 | { |
1023 | return _filename; | |
1024 | } | |
1025 | ||
624d1610 UH |
1026 | shared_ptr<Context> Session::context() |
1027 | { | |
1028 | return _context; | |
1029 | } | |
1030 | ||
2928f47d ML |
1031 | Packet::Packet(shared_ptr<Device> device, |
1032 | const struct sr_datafeed_packet *structure) : | |
d5d7b09e | 1033 | _structure(structure), |
d370545d | 1034 | _device(move(device)) |
c23c8659 ML |
1035 | { |
1036 | switch (structure->type) | |
1037 | { | |
2928f47d | 1038 | case SR_DF_HEADER: |
f17b4546 | 1039 | _payload.reset(new Header{ |
2928f47d | 1040 | static_cast<const struct sr_datafeed_header *>( |
f17b4546 | 1041 | structure->payload)}); |
2928f47d ML |
1042 | break; |
1043 | case SR_DF_META: | |
f17b4546 | 1044 | _payload.reset(new Meta{ |
2928f47d | 1045 | static_cast<const struct sr_datafeed_meta *>( |
f17b4546 | 1046 | structure->payload)}); |
2928f47d | 1047 | break; |
c23c8659 | 1048 | case SR_DF_LOGIC: |
f17b4546 | 1049 | _payload.reset(new Logic{ |
c23c8659 | 1050 | static_cast<const struct sr_datafeed_logic *>( |
f17b4546 | 1051 | structure->payload)}); |
c23c8659 | 1052 | break; |
dd13d47a | 1053 | case SR_DF_ANALOG: |
f17b4546 | 1054 | _payload.reset(new Analog{ |
dd13d47a | 1055 | static_cast<const struct sr_datafeed_analog *>( |
f17b4546 | 1056 | structure->payload)}); |
4cd883a7 | 1057 | break; |
c23c8659 ML |
1058 | } |
1059 | } | |
1060 | ||
1061 | Packet::~Packet() | |
1062 | { | |
c23c8659 ML |
1063 | } |
1064 | ||
15bebf57 | 1065 | const PacketType *Packet::type() const |
90ba83f2 | 1066 | { |
3b161085 | 1067 | return PacketType::get(_structure->type); |
90ba83f2 ML |
1068 | } |
1069 | ||
3b161085 | 1070 | shared_ptr<PacketPayload> Packet::payload() |
c23c8659 | 1071 | { |
3b161085 | 1072 | if (_payload) |
b6ab954d | 1073 | return _payload->share_owned_by(shared_from_this()); |
4cd883a7 ML |
1074 | else |
1075 | throw Error(SR_ERR_NA); | |
c23c8659 ML |
1076 | } |
1077 | ||
1078 | PacketPayload::PacketPayload() | |
1079 | { | |
1080 | } | |
1081 | ||
1082 | PacketPayload::~PacketPayload() | |
1083 | { | |
1084 | } | |
1085 | ||
2928f47d | 1086 | Header::Header(const struct sr_datafeed_header *structure) : |
d5d7b09e DE |
1087 | PacketPayload(), |
1088 | _structure(structure) | |
2928f47d ML |
1089 | { |
1090 | } | |
1091 | ||
1092 | Header::~Header() | |
1093 | { | |
1094 | } | |
1095 | ||
b6ab954d | 1096 | shared_ptr<PacketPayload> Header::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1097 | { |
4f7bcf0e | 1098 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1099 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1100 | } |
1101 | ||
15bebf57 | 1102 | int Header::feed_version() const |
2928f47d | 1103 | { |
3b161085 | 1104 | return _structure->feed_version; |
2928f47d ML |
1105 | } |
1106 | ||
15bebf57 | 1107 | Glib::TimeVal Header::start_time() const |
2928f47d ML |
1108 | { |
1109 | return Glib::TimeVal( | |
3b161085 ML |
1110 | _structure->starttime.tv_sec, |
1111 | _structure->starttime.tv_usec); | |
2928f47d ML |
1112 | } |
1113 | ||
1114 | Meta::Meta(const struct sr_datafeed_meta *structure) : | |
d5d7b09e DE |
1115 | PacketPayload(), |
1116 | _structure(structure) | |
2928f47d ML |
1117 | { |
1118 | } | |
1119 | ||
1120 | Meta::~Meta() | |
1121 | { | |
1122 | } | |
1123 | ||
b6ab954d | 1124 | shared_ptr<PacketPayload> Meta::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1125 | { |
4f7bcf0e | 1126 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1127 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1128 | } |
1129 | ||
15bebf57 | 1130 | map<const ConfigKey *, Glib::VariantBase> Meta::config() const |
2928f47d ML |
1131 | { |
1132 | map<const ConfigKey *, Glib::VariantBase> result; | |
ce3e1e61 DE |
1133 | for (auto l = _structure->config; l; l = l->next) { |
1134 | auto *const config = static_cast<struct sr_config *>(l->data); | |
2928f47d ML |
1135 | result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data); |
1136 | } | |
1137 | return result; | |
1138 | } | |
1139 | ||
1140 | Logic::Logic(const struct sr_datafeed_logic *structure) : | |
d5d7b09e DE |
1141 | PacketPayload(), |
1142 | _structure(structure) | |
2928f47d ML |
1143 | { |
1144 | } | |
c23c8659 ML |
1145 | |
1146 | Logic::~Logic() | |
1147 | { | |
1148 | } | |
1149 | ||
b6ab954d | 1150 | shared_ptr<PacketPayload> Logic::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1151 | { |
4f7bcf0e | 1152 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1153 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1154 | } |
1155 | ||
3b161085 | 1156 | void *Logic::data_pointer() |
c23c8659 | 1157 | { |
3b161085 | 1158 | return _structure->data; |
c23c8659 ML |
1159 | } |
1160 | ||
15bebf57 | 1161 | size_t Logic::data_length() const |
c23c8659 | 1162 | { |
3b161085 | 1163 | return _structure->length; |
c23c8659 ML |
1164 | } |
1165 | ||
15bebf57 | 1166 | unsigned int Logic::unit_size() const |
2928f47d | 1167 | { |
3b161085 | 1168 | return _structure->unitsize; |
2928f47d ML |
1169 | } |
1170 | ||
dd13d47a | 1171 | Analog::Analog(const struct sr_datafeed_analog *structure) : |
d5d7b09e DE |
1172 | PacketPayload(), |
1173 | _structure(structure) | |
c23c8659 ML |
1174 | { |
1175 | } | |
1176 | ||
dd13d47a | 1177 | Analog::~Analog() |
c23c8659 ML |
1178 | { |
1179 | } | |
1180 | ||
b6ab954d | 1181 | shared_ptr<PacketPayload> Analog::share_owned_by(shared_ptr<Packet> _parent) |
4cd883a7 | 1182 | { |
4f7bcf0e | 1183 | return static_pointer_cast<PacketPayload>( |
b6ab954d | 1184 | ParentOwned::share_owned_by(_parent)); |
4cd883a7 ML |
1185 | } |
1186 | ||
dd13d47a | 1187 | void *Analog::data_pointer() |
c23c8659 | 1188 | { |
3b161085 | 1189 | return _structure->data; |
c23c8659 ML |
1190 | } |
1191 | ||
15bebf57 | 1192 | unsigned int Analog::num_samples() const |
c23c8659 | 1193 | { |
3b161085 | 1194 | return _structure->num_samples; |
c23c8659 ML |
1195 | } |
1196 | ||
dd13d47a | 1197 | vector<shared_ptr<Channel>> Analog::channels() |
c23c8659 | 1198 | { |
2928f47d | 1199 | vector<shared_ptr<Channel>> result; |
ce3e1e61 DE |
1200 | for (auto l = _structure->meaning->channels; l; l = l->next) { |
1201 | auto *const ch = static_cast<struct sr_channel *>(l->data); | |
1202 | result.push_back(_parent->_device->get_channel(ch)); | |
1203 | } | |
2928f47d | 1204 | return result; |
c23c8659 ML |
1205 | } |
1206 | ||
15bebf57 | 1207 | const Quantity *Analog::mq() const |
c23c8659 | 1208 | { |
dd13d47a | 1209 | return Quantity::get(_structure->meaning->mq); |
c23c8659 ML |
1210 | } |
1211 | ||
15bebf57 | 1212 | const Unit *Analog::unit() const |
c23c8659 | 1213 | { |
dd13d47a | 1214 | return Unit::get(_structure->meaning->unit); |
c23c8659 ML |
1215 | } |
1216 | ||
15bebf57 | 1217 | vector<const QuantityFlag *> Analog::mq_flags() const |
c23c8659 | 1218 | { |
dd13d47a | 1219 | return QuantityFlag::flags_from_mask(_structure->meaning->mqflags); |
c23c8659 ML |
1220 | } |
1221 | ||
ca3291e3 | 1222 | InputFormat::InputFormat(const struct sr_input_module *structure) : |
d5d7b09e | 1223 | _structure(structure) |
c23c8659 ML |
1224 | { |
1225 | } | |
1226 | ||
1227 | InputFormat::~InputFormat() | |
1228 | { | |
1229 | } | |
1230 | ||
15bebf57 | 1231 | string InputFormat::name() const |
c23c8659 | 1232 | { |
3b161085 | 1233 | return valid_string(sr_input_id_get(_structure)); |
c23c8659 ML |
1234 | } |
1235 | ||
15bebf57 | 1236 | string InputFormat::description() const |
c23c8659 | 1237 | { |
3b161085 | 1238 | return valid_string(sr_input_description_get(_structure)); |
ca3291e3 ML |
1239 | } |
1240 | ||
15bebf57 | 1241 | vector<string> InputFormat::extensions() const |
c7bc82ff JH |
1242 | { |
1243 | vector<string> exts; | |
1244 | for (const char *const *e = sr_input_extensions_get(_structure); | |
1245 | e && *e; e++) | |
1246 | exts.push_back(*e); | |
1247 | return exts; | |
1248 | } | |
1249 | ||
3b161085 | 1250 | map<string, shared_ptr<Option>> InputFormat::options() |
43942280 | 1251 | { |
3b161085 | 1252 | const struct sr_option **options = sr_input_options_get(_structure); |
43942280 | 1253 | map<string, shared_ptr<Option>> result; |
48d92e2c ML |
1254 | if (options) |
1255 | { | |
1256 | auto option_array = shared_ptr<const struct sr_option *>( | |
1257 | options, sr_input_options_free); | |
1258 | for (int i = 0; options[i]; i++) | |
1259 | result[options[i]->id] = shared_ptr<Option>( | |
1260 | new Option(options[i], option_array), Option::Deleter()); | |
1261 | } | |
43942280 ML |
1262 | return result; |
1263 | } | |
1264 | ||
ca3291e3 | 1265 | shared_ptr<Input> InputFormat::create_input( |
d370545d | 1266 | const map<string, Glib::VariantBase> &options) |
ca3291e3 | 1267 | { |
3b161085 | 1268 | auto input = sr_input_new(_structure, map_to_hash_variant(options)); |
ca3291e3 ML |
1269 | if (!input) |
1270 | throw Error(SR_ERR_ARG); | |
67b82fc9 | 1271 | return shared_ptr<Input>(new Input(_parent, input), Input::Deleter()); |
c23c8659 ML |
1272 | } |
1273 | ||
ca3291e3 | 1274 | Input::Input(shared_ptr<Context> context, const struct sr_input *structure) : |
d5d7b09e | 1275 | _structure(structure), |
f17b4546 | 1276 | _context(move(context)) |
c23c8659 | 1277 | { |
c23c8659 ML |
1278 | } |
1279 | ||
3b161085 | 1280 | shared_ptr<InputDevice> Input::device() |
c23c8659 | 1281 | { |
3b161085 | 1282 | if (!_device) |
ca3291e3 | 1283 | { |
3b161085 | 1284 | auto sdi = sr_input_dev_inst_get(_structure); |
ca3291e3 ML |
1285 | if (!sdi) |
1286 | throw Error(SR_ERR_NA); | |
f17b4546 | 1287 | _device.reset(new InputDevice{shared_from_this(), sdi}); |
ca3291e3 | 1288 | } |
c23c8659 | 1289 | |
b6ab954d | 1290 | return _device->share_owned_by(shared_from_this()); |
ca3291e3 | 1291 | } |
c23c8659 | 1292 | |
2b51d48b | 1293 | void Input::send(void *data, size_t length) |
ca3291e3 | 1294 | { |
ce3e1e61 | 1295 | auto gstr = g_string_new_len(static_cast<char *>(data), length); |
3b161085 | 1296 | auto ret = sr_input_send(_structure, gstr); |
ca3291e3 ML |
1297 | g_string_free(gstr, false); |
1298 | check(ret); | |
c23c8659 ML |
1299 | } |
1300 | ||
9c51e8ec ML |
1301 | void Input::end() |
1302 | { | |
1303 | check(sr_input_end(_structure)); | |
1304 | } | |
1305 | ||
ca3291e3 | 1306 | Input::~Input() |
c23c8659 | 1307 | { |
9c51e8ec | 1308 | sr_input_free(_structure); |
c23c8659 ML |
1309 | } |
1310 | ||
6e5240f4 ML |
1311 | InputDevice::InputDevice(shared_ptr<Input> input, |
1312 | struct sr_dev_inst *structure) : | |
6e5240f4 | 1313 | Device(structure), |
d370545d | 1314 | _input(move(input)) |
c23c8659 | 1315 | { |
c23c8659 ML |
1316 | } |
1317 | ||
ca3291e3 | 1318 | InputDevice::~InputDevice() |
c23c8659 | 1319 | { |
c23c8659 ML |
1320 | } |
1321 | ||
d01d2314 ML |
1322 | shared_ptr<Device> InputDevice::get_shared_from_this() |
1323 | { | |
bf52cc8c | 1324 | return static_pointer_cast<Device>(shared_from_this()); |
d01d2314 ML |
1325 | } |
1326 | ||
58aa1f83 | 1327 | Option::Option(const struct sr_option *structure, |
70d3b20b | 1328 | shared_ptr<const struct sr_option *> structure_array) : |
d5d7b09e | 1329 | _structure(structure), |
d370545d | 1330 | _structure_array(move(structure_array)) |
58aa1f83 ML |
1331 | { |
1332 | } | |
1333 | ||
1334 | Option::~Option() | |
1335 | { | |
1336 | } | |
1337 | ||
15bebf57 | 1338 | string Option::id() const |
58aa1f83 | 1339 | { |
3b161085 | 1340 | return valid_string(_structure->id); |
58aa1f83 ML |
1341 | } |
1342 | ||
15bebf57 | 1343 | string Option::name() const |
58aa1f83 | 1344 | { |
3b161085 | 1345 | return valid_string(_structure->name); |
58aa1f83 ML |
1346 | } |
1347 | ||
15bebf57 | 1348 | string Option::description() const |
58aa1f83 | 1349 | { |
3b161085 | 1350 | return valid_string(_structure->desc); |
58aa1f83 ML |
1351 | } |
1352 | ||
15bebf57 | 1353 | Glib::VariantBase Option::default_value() const |
58aa1f83 | 1354 | { |
3b161085 | 1355 | return Glib::VariantBase(_structure->def, true); |
58aa1f83 ML |
1356 | } |
1357 | ||
15bebf57 | 1358 | vector<Glib::VariantBase> Option::values() const |
58aa1f83 ML |
1359 | { |
1360 | vector<Glib::VariantBase> result; | |
ce3e1e61 DE |
1361 | for (auto l = _structure->values; l; l = l->next) { |
1362 | auto *const var = static_cast<GVariant *>(l->data); | |
1363 | result.push_back(Glib::VariantBase(var, true)); | |
1364 | } | |
58aa1f83 ML |
1365 | return result; |
1366 | } | |
1367 | ||
1368 | OutputFormat::OutputFormat(const struct sr_output_module *structure) : | |
d5d7b09e | 1369 | _structure(structure) |
c23c8659 ML |
1370 | { |
1371 | } | |
1372 | ||
1373 | OutputFormat::~OutputFormat() | |
1374 | { | |
1375 | } | |
1376 | ||
15bebf57 | 1377 | string OutputFormat::name() const |
c23c8659 | 1378 | { |
3b161085 | 1379 | return valid_string(sr_output_id_get(_structure)); |
c23c8659 ML |
1380 | } |
1381 | ||
15bebf57 | 1382 | string OutputFormat::description() const |
c23c8659 | 1383 | { |
3b161085 | 1384 | return valid_string(sr_output_description_get(_structure)); |
58aa1f83 ML |
1385 | } |
1386 | ||
15bebf57 | 1387 | vector<string> OutputFormat::extensions() const |
8a174d23 JH |
1388 | { |
1389 | vector<string> exts; | |
1390 | for (const char *const *e = sr_output_extensions_get(_structure); | |
1391 | e && *e; e++) | |
1392 | exts.push_back(*e); | |
1393 | return exts; | |
1394 | } | |
1395 | ||
3b161085 | 1396 | map<string, shared_ptr<Option>> OutputFormat::options() |
58aa1f83 | 1397 | { |
3b161085 | 1398 | const struct sr_option **options = sr_output_options_get(_structure); |
58aa1f83 | 1399 | map<string, shared_ptr<Option>> result; |
48d92e2c ML |
1400 | if (options) |
1401 | { | |
1402 | auto option_array = shared_ptr<const struct sr_option *>( | |
1403 | options, sr_output_options_free); | |
1404 | for (int i = 0; options[i]; i++) | |
1405 | result[options[i]->id] = shared_ptr<Option>( | |
1406 | new Option(options[i], option_array), Option::Deleter()); | |
1407 | } | |
58aa1f83 | 1408 | return result; |
c23c8659 ML |
1409 | } |
1410 | ||
1411 | shared_ptr<Output> OutputFormat::create_output( | |
d370545d | 1412 | shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) |
c23c8659 ML |
1413 | { |
1414 | return shared_ptr<Output>( | |
d370545d | 1415 | new Output(shared_from_this(), move(device), options), |
c23c8659 ML |
1416 | Output::Deleter()); |
1417 | } | |
1418 | ||
81b3ce37 | 1419 | shared_ptr<Output> OutputFormat::create_output(string filename, |
d370545d | 1420 | shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) |
81b3ce37 SA |
1421 | { |
1422 | return shared_ptr<Output>( | |
d370545d | 1423 | new Output(move(filename), shared_from_this(), move(device), options), |
81b3ce37 SA |
1424 | Output::Deleter()); |
1425 | } | |
1426 | ||
15bebf57 | 1427 | bool OutputFormat::test_flag(const OutputFlag *flag) const |
3cd4b381 SA |
1428 | { |
1429 | return sr_output_test_flag(_structure, flag->id()); | |
1430 | } | |
1431 | ||
c23c8659 | 1432 | Output::Output(shared_ptr<OutputFormat> format, |
d370545d | 1433 | shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) : |
d5d7b09e | 1434 | _structure(sr_output_new(format->_structure, |
58e21229 | 1435 | map_to_hash_variant(options), device->_structure, nullptr)), |
d370545d DE |
1436 | _format(move(format)), |
1437 | _device(move(device)), | |
81b3ce37 SA |
1438 | _options(options) |
1439 | { | |
1440 | } | |
1441 | ||
1442 | Output::Output(string filename, shared_ptr<OutputFormat> format, | |
d370545d | 1443 | shared_ptr<Device> device, const map<string, Glib::VariantBase> &options) : |
d5d7b09e | 1444 | _structure(sr_output_new(format->_structure, |
81b3ce37 | 1445 | map_to_hash_variant(options), device->_structure, filename.c_str())), |
d370545d DE |
1446 | _format(move(format)), |
1447 | _device(move(device)), | |
3b161085 | 1448 | _options(options) |
c23c8659 ML |
1449 | { |
1450 | } | |
1451 | ||
1452 | Output::~Output() | |
1453 | { | |
3b161085 | 1454 | check(sr_output_free(_structure)); |
c23c8659 ML |
1455 | } |
1456 | ||
1457 | string Output::receive(shared_ptr<Packet> packet) | |
1458 | { | |
1459 | GString *out; | |
3b161085 | 1460 | check(sr_output_send(_structure, packet->_structure, &out)); |
c23c8659 ML |
1461 | if (out) |
1462 | { | |
1463 | auto result = string(out->str, out->str + out->len); | |
1464 | g_string_free(out, true); | |
1465 | return result; | |
1466 | } | |
1467 | else | |
1468 | { | |
1469 | return string(); | |
1470 | } | |
1471 | } | |
1472 | ||
b5f07319 | 1473 | #include <enums.cpp> |
c23c8659 ML |
1474 | |
1475 | } |