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