]>
Commit | Line | Data |
---|---|---|
c23c8659 ML |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013-2014 Martin Ling <martin-sigrok@earth.li> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "libsigrok/libsigrok.hpp" | |
21 | ||
f36ca889 ML |
22 | #include <sstream> |
23 | ||
c23c8659 ML |
24 | namespace sigrok |
25 | { | |
26 | ||
c23c8659 ML |
27 | /** Helper function to translate C errors to C++ exceptions. */ |
28 | static void check(int result) | |
29 | { | |
30 | if (result != SR_OK) | |
31 | throw Error(result); | |
32 | } | |
33 | ||
34 | /** Helper function to obtain valid strings from possibly null input. */ | |
35 | static const char *valid_string(const char *input) | |
36 | { | |
37 | if (input != NULL) | |
38 | return input; | |
39 | else | |
40 | return ""; | |
41 | } | |
42 | ||
43 | /** Helper function to convert between map<string, string> and GHashTable */ | |
44 | ||
45 | static GHashTable *map_to_hash(map<string, string> input) | |
46 | { | |
47 | auto output = g_hash_table_new_full( | |
48 | g_str_hash, g_str_equal, g_free, g_free); | |
49 | for (auto entry : input) | |
50 | g_hash_table_insert(output, | |
51 | g_strdup(entry.first.c_str()), | |
52 | g_strdup(entry.second.c_str())); | |
53 | return output; | |
54 | } | |
55 | ||
56 | Error::Error(int result) : result(result) | |
57 | { | |
58 | } | |
59 | ||
60 | const char *Error::what() const throw() | |
61 | { | |
62 | return sr_strerror(result); | |
63 | } | |
64 | ||
65 | Error::~Error() throw() | |
66 | { | |
67 | } | |
68 | ||
69 | shared_ptr<Context> Context::create() | |
70 | { | |
71 | return shared_ptr<Context>(new Context(), Context::Deleter()); | |
72 | } | |
73 | ||
74 | Context::Context() : | |
75 | session(NULL) | |
76 | { | |
77 | check(sr_init(&structure)); | |
78 | struct sr_dev_driver **driver_list = sr_driver_list(); | |
79 | if (driver_list) | |
80 | for (int i = 0; driver_list[i]; i++) | |
81 | drivers[driver_list[i]->name] = | |
82 | new Driver(driver_list[i]); | |
83 | struct sr_input_format **input_list = sr_input_list(); | |
84 | if (input_list) | |
85 | for (int i = 0; input_list[i]; i++) | |
86 | input_formats[input_list[i]->id] = | |
87 | new InputFormat(input_list[i]); | |
88 | struct sr_output_format **output_list = sr_output_list(); | |
89 | if (output_list) | |
90 | for (int i = 0; output_list[i]; i++) | |
91 | output_formats[output_list[i]->id] = | |
92 | new OutputFormat(output_list[i]); | |
93 | } | |
94 | ||
95 | string Context::get_package_version() | |
96 | { | |
97 | return sr_package_version_string_get(); | |
98 | } | |
99 | ||
100 | string Context::get_lib_version() | |
101 | { | |
102 | return sr_lib_version_string_get(); | |
103 | } | |
104 | ||
105 | map<string, shared_ptr<Driver>> Context::get_drivers() | |
106 | { | |
107 | map<string, shared_ptr<Driver>> result; | |
108 | for (auto entry: drivers) | |
109 | { | |
110 | auto name = entry.first; | |
111 | auto driver = entry.second; | |
7649683c ML |
112 | result[name] = static_pointer_cast<Driver>( |
113 | driver->get_shared_pointer(this)); | |
c23c8659 ML |
114 | } |
115 | return result; | |
116 | } | |
117 | ||
118 | map<string, shared_ptr<InputFormat>> Context::get_input_formats() | |
119 | { | |
120 | map<string, shared_ptr<InputFormat>> result; | |
121 | for (auto entry: input_formats) | |
122 | { | |
123 | auto name = entry.first; | |
124 | auto input_format = entry.second; | |
7649683c ML |
125 | result[name] = static_pointer_cast<InputFormat>( |
126 | input_format->get_shared_pointer(this)); | |
c23c8659 ML |
127 | } |
128 | return result; | |
129 | } | |
130 | ||
131 | map<string, shared_ptr<OutputFormat>> Context::get_output_formats() | |
132 | { | |
133 | map<string, shared_ptr<OutputFormat>> result; | |
134 | for (auto entry: output_formats) | |
135 | { | |
136 | auto name = entry.first; | |
137 | auto output_format = entry.second; | |
7649683c ML |
138 | result[name] = static_pointer_cast<OutputFormat>( |
139 | output_format->get_shared_pointer(this)); | |
c23c8659 ML |
140 | } |
141 | return result; | |
142 | } | |
143 | ||
144 | Context::~Context() | |
145 | { | |
146 | for (auto entry : drivers) | |
147 | delete entry.second; | |
148 | for (auto entry : input_formats) | |
149 | delete entry.second; | |
150 | for (auto entry : output_formats) | |
151 | delete entry.second; | |
152 | check(sr_exit(structure)); | |
153 | } | |
154 | ||
155 | const LogLevel *Context::get_log_level() | |
156 | { | |
157 | return LogLevel::get(sr_log_loglevel_get()); | |
158 | } | |
159 | ||
160 | void Context::set_log_level(const LogLevel *level) | |
161 | { | |
162 | check(sr_log_loglevel_set(level->get_id())); | |
163 | } | |
164 | ||
165 | string Context::get_log_domain() | |
166 | { | |
167 | return valid_string(sr_log_logdomain_get()); | |
168 | } | |
169 | ||
170 | void Context::set_log_domain(string value) | |
171 | { | |
172 | check(sr_log_logdomain_set(value.c_str())); | |
173 | } | |
174 | ||
175 | static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args) | |
176 | { | |
177 | va_list args_copy; | |
178 | va_copy(args_copy, args); | |
179 | int length = vsnprintf(NULL, 0, format, args_copy); | |
180 | va_end(args_copy); | |
181 | char *buf = (char *) g_malloc(length + 1); | |
182 | vsprintf(buf, format, args); | |
183 | string message(buf, length); | |
184 | g_free(buf); | |
185 | ||
186 | LogCallbackFunction callback = *((LogCallbackFunction *) cb_data); | |
187 | ||
188 | try | |
189 | { | |
190 | callback(LogLevel::get(loglevel), message); | |
191 | } | |
192 | catch (Error e) | |
193 | { | |
194 | return e.result; | |
195 | } | |
196 | ||
197 | return SR_OK; | |
198 | } | |
199 | ||
200 | void Context::set_log_callback(LogCallbackFunction callback) | |
201 | { | |
202 | log_callback = callback; | |
203 | check(sr_log_callback_set(call_log_callback, &log_callback)); | |
204 | } | |
205 | ||
206 | void Context::set_log_callback_default() | |
207 | { | |
208 | check(sr_log_callback_set_default()); | |
209 | log_callback = nullptr; | |
210 | } | |
211 | ||
212 | shared_ptr<Session> Context::create_session() | |
213 | { | |
214 | return shared_ptr<Session>( | |
215 | new Session(shared_from_this()), Session::Deleter()); | |
216 | } | |
217 | ||
218 | shared_ptr<Session> Context::load_session(string filename) | |
219 | { | |
220 | return shared_ptr<Session>( | |
221 | new Session(shared_from_this(), filename), Session::Deleter()); | |
222 | } | |
223 | ||
224 | shared_ptr<Trigger> Context::create_trigger(string name) | |
225 | { | |
226 | return shared_ptr<Trigger>( | |
227 | new Trigger(shared_from_this(), name), Trigger::Deleter()); | |
228 | } | |
229 | ||
230 | Driver::Driver(struct sr_dev_driver *structure) : | |
231 | StructureWrapper<Context, struct sr_dev_driver>(structure), | |
232 | initialized(false) | |
233 | { | |
234 | } | |
235 | ||
236 | Driver::~Driver() | |
237 | { | |
238 | for (auto device : devices) | |
239 | delete device; | |
240 | } | |
241 | ||
242 | string Driver::get_name() | |
243 | { | |
244 | return valid_string(structure->name); | |
245 | } | |
246 | ||
247 | string Driver::get_long_name() | |
248 | { | |
249 | return valid_string(structure->longname); | |
250 | } | |
251 | ||
252 | vector<shared_ptr<HardwareDevice>> Driver::scan( | |
253 | map<const ConfigKey *, Glib::VariantBase> options) | |
254 | { | |
255 | /* Initialise the driver if not yet done. */ | |
256 | if (!initialized) | |
257 | { | |
258 | check(sr_driver_init(parent->structure, structure)); | |
259 | initialized = true; | |
260 | } | |
261 | ||
262 | /* Clear all existing instances. */ | |
263 | for (auto device : devices) | |
264 | delete device; | |
265 | devices.clear(); | |
266 | ||
267 | /* Translate scan options to GSList of struct sr_config pointers. */ | |
268 | GSList *option_list = NULL; | |
269 | for (auto entry : options) | |
270 | { | |
271 | auto key = entry.first; | |
272 | auto value = entry.second; | |
273 | auto config = g_new(struct sr_config, 1); | |
274 | config->key = key->get_id(); | |
275 | config->data = value.gobj(); | |
276 | option_list = g_slist_append(option_list, config); | |
277 | } | |
278 | ||
279 | /* Run scan. */ | |
280 | GSList *device_list = sr_driver_scan(structure, option_list); | |
281 | ||
282 | /* Free option list. */ | |
283 | g_slist_free_full(option_list, g_free); | |
284 | ||
285 | /* Create device objects. */ | |
286 | for (GSList *device = device_list; device; device = device->next) | |
287 | { | |
288 | auto sdi = (struct sr_dev_inst *) device->data; | |
289 | devices.push_back(new HardwareDevice(this, sdi)); | |
290 | } | |
291 | ||
292 | /* Free GSList returned from scan. */ | |
293 | g_slist_free(device_list); | |
294 | ||
295 | /* Create list of shared pointers to device instances for return. */ | |
296 | vector<shared_ptr<HardwareDevice>> result; | |
297 | for (auto device : devices) | |
7649683c ML |
298 | result.push_back(static_pointer_cast<HardwareDevice>( |
299 | device->get_shared_pointer(parent))); | |
c23c8659 ML |
300 | return result; |
301 | } | |
302 | ||
303 | Configurable::Configurable( | |
304 | struct sr_dev_driver *driver, | |
305 | struct sr_dev_inst *sdi, | |
306 | struct sr_channel_group *cg) : | |
307 | config_driver(driver), | |
308 | config_sdi(sdi), | |
309 | config_channel_group(cg) | |
310 | { | |
311 | } | |
312 | ||
313 | Configurable::~Configurable() | |
314 | { | |
315 | } | |
316 | ||
317 | Glib::VariantBase Configurable::config_get(const ConfigKey *key) | |
318 | { | |
319 | GVariant *data; | |
320 | check(sr_config_get( | |
321 | config_driver, config_sdi, config_channel_group, | |
322 | key->get_id(), &data)); | |
323 | return Glib::VariantBase(data); | |
324 | } | |
325 | ||
326 | void Configurable::config_set(const ConfigKey *key, Glib::VariantBase value) | |
327 | { | |
328 | check(sr_config_set( | |
329 | config_sdi, config_channel_group, | |
330 | key->get_id(), value.gobj())); | |
331 | } | |
332 | ||
e194c011 | 333 | Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) |
c23c8659 ML |
334 | { |
335 | GVariant *data; | |
336 | check(sr_config_list( | |
337 | config_driver, config_sdi, config_channel_group, | |
338 | key->get_id(), &data)); | |
e194c011 | 339 | return Glib::VariantContainerBase(data); |
c23c8659 ML |
340 | } |
341 | ||
342 | Device::Device(struct sr_dev_inst *structure) : | |
343 | Configurable(structure->driver, structure, NULL), | |
344 | StructureWrapper<Context, struct sr_dev_inst>(structure) | |
345 | { | |
346 | for (GSList *entry = structure->channels; entry; entry = entry->next) | |
347 | { | |
348 | auto channel = (struct sr_channel *) entry->data; | |
4178d971 | 349 | channels[channel] = new Channel(channel); |
c23c8659 | 350 | } |
6be7a7f2 ML |
351 | |
352 | for (GSList *entry = structure->channel_groups; entry; entry = entry->next) | |
353 | { | |
354 | auto group = (struct sr_channel_group *) entry->data; | |
355 | channel_groups[group->name] = new ChannelGroup(this, group); | |
356 | } | |
c23c8659 ML |
357 | } |
358 | ||
359 | Device::~Device() | |
360 | { | |
4178d971 ML |
361 | for (auto entry : channels) |
362 | delete entry.second; | |
6be7a7f2 ML |
363 | for (auto entry : channel_groups) |
364 | delete entry.second; | |
c23c8659 ML |
365 | } |
366 | ||
f36ca889 ML |
367 | string Device::get_description() |
368 | { | |
369 | ostringstream s; | |
370 | ||
371 | vector<string> parts = | |
372 | {get_vendor(), get_model(), get_version()}; | |
373 | ||
374 | for (string part : parts) | |
375 | if (part.length() > 0) | |
376 | s << part; | |
377 | ||
378 | return s.str(); | |
379 | } | |
380 | ||
c23c8659 ML |
381 | string Device::get_vendor() |
382 | { | |
383 | return valid_string(structure->vendor); | |
384 | } | |
385 | ||
386 | string Device::get_model() | |
387 | { | |
388 | return valid_string(structure->model); | |
389 | } | |
390 | ||
391 | string Device::get_version() | |
392 | { | |
393 | return valid_string(structure->version); | |
394 | } | |
395 | ||
396 | vector<shared_ptr<Channel>> Device::get_channels() | |
397 | { | |
398 | vector<shared_ptr<Channel>> result; | |
4178d971 | 399 | for (auto entry : channels) |
7649683c | 400 | result.push_back(static_pointer_cast<Channel>( |
4178d971 | 401 | entry.second->get_shared_pointer(this))); |
c23c8659 ML |
402 | return result; |
403 | } | |
404 | ||
4178d971 ML |
405 | shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr) |
406 | { | |
407 | return static_pointer_cast<Channel>( | |
408 | channels[ptr]->get_shared_pointer(this)); | |
409 | } | |
410 | ||
6be7a7f2 ML |
411 | map<string, shared_ptr<ChannelGroup>> |
412 | Device::get_channel_groups() | |
413 | { | |
414 | map<string, shared_ptr<ChannelGroup>> result; | |
415 | for (auto entry: channel_groups) | |
416 | { | |
417 | auto name = entry.first; | |
418 | auto channel_group = entry.second; | |
419 | result[name] = static_pointer_cast<ChannelGroup>( | |
420 | channel_group->get_shared_pointer(this)); | |
421 | } | |
422 | return result; | |
423 | } | |
424 | ||
c23c8659 ML |
425 | void Device::open() |
426 | { | |
427 | check(sr_dev_open(structure)); | |
428 | } | |
429 | ||
430 | void Device::close() | |
431 | { | |
432 | check(sr_dev_close(structure)); | |
433 | } | |
434 | ||
435 | HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) : | |
436 | Device(structure), | |
437 | driver(driver) | |
438 | { | |
c23c8659 ML |
439 | } |
440 | ||
441 | HardwareDevice::~HardwareDevice() | |
442 | { | |
c23c8659 ML |
443 | } |
444 | ||
445 | shared_ptr<Driver> HardwareDevice::get_driver() | |
446 | { | |
7649683c | 447 | return static_pointer_cast<Driver>(driver->get_shared_pointer(parent)); |
c23c8659 ML |
448 | } |
449 | ||
c23c8659 ML |
450 | Channel::Channel(struct sr_channel *structure) : |
451 | StructureWrapper<Device, struct sr_channel>(structure), | |
452 | type(ChannelType::get(structure->type)) | |
453 | { | |
454 | } | |
455 | ||
456 | Channel::~Channel() | |
457 | { | |
458 | } | |
459 | ||
460 | string Channel::get_name() | |
461 | { | |
462 | return valid_string(structure->name); | |
463 | } | |
464 | ||
465 | void Channel::set_name(string name) | |
466 | { | |
467 | check(sr_dev_channel_name_set(parent->structure, structure->index, name.c_str())); | |
468 | } | |
469 | ||
470 | const ChannelType *Channel::get_type() | |
471 | { | |
472 | return ChannelType::get(structure->type); | |
473 | } | |
474 | ||
475 | bool Channel::get_enabled() | |
476 | { | |
477 | return structure->enabled; | |
478 | } | |
479 | ||
480 | void Channel::set_enabled(bool value) | |
481 | { | |
482 | check(sr_dev_channel_enable(parent->structure, structure->index, value)); | |
483 | } | |
484 | ||
06bd935e ML |
485 | unsigned int Channel::get_index() |
486 | { | |
487 | return structure->index; | |
488 | } | |
489 | ||
6be7a7f2 | 490 | ChannelGroup::ChannelGroup(Device *device, |
c23c8659 | 491 | struct sr_channel_group *structure) : |
6be7a7f2 | 492 | StructureWrapper<Device, struct sr_channel_group>(structure), |
c23c8659 ML |
493 | Configurable(device->structure->driver, device->structure, structure) |
494 | { | |
495 | for (GSList *entry = structure->channels; entry; entry = entry->next) | |
4178d971 | 496 | channels.push_back(device->channels[(struct sr_channel *)entry->data]); |
c23c8659 ML |
497 | } |
498 | ||
499 | ChannelGroup::~ChannelGroup() | |
500 | { | |
501 | } | |
502 | ||
503 | string ChannelGroup::get_name() | |
504 | { | |
505 | return valid_string(structure->name); | |
506 | } | |
507 | ||
508 | vector<shared_ptr<Channel>> ChannelGroup::get_channels() | |
509 | { | |
510 | vector<shared_ptr<Channel>> result; | |
511 | for (auto channel : channels) | |
7649683c ML |
512 | result.push_back(static_pointer_cast<Channel>( |
513 | channel->get_shared_pointer(parent))); | |
c23c8659 ML |
514 | return result; |
515 | } | |
516 | ||
517 | Trigger::Trigger(shared_ptr<Context> context, string name) : | |
518 | structure(sr_trigger_new(name.c_str())), context(context) | |
519 | { | |
520 | for (auto stage = structure->stages; stage; stage = stage->next) | |
521 | stages.push_back(new TriggerStage((struct sr_trigger_stage *) stage->data)); | |
522 | } | |
523 | ||
524 | Trigger::~Trigger() | |
525 | { | |
526 | for (auto stage: stages) | |
527 | delete stage; | |
528 | ||
529 | sr_trigger_free(structure); | |
530 | } | |
531 | ||
532 | string Trigger::get_name() | |
533 | { | |
534 | return structure->name; | |
535 | } | |
536 | ||
537 | vector<shared_ptr<TriggerStage>> Trigger::get_stages() | |
538 | { | |
539 | vector<shared_ptr<TriggerStage>> result; | |
540 | for (auto stage : stages) | |
7649683c ML |
541 | result.push_back(static_pointer_cast<TriggerStage>( |
542 | stage->get_shared_pointer(this))); | |
c23c8659 ML |
543 | return result; |
544 | } | |
545 | ||
546 | shared_ptr<TriggerStage> Trigger::add_stage() | |
547 | { | |
548 | auto stage = new TriggerStage(sr_trigger_stage_add(structure)); | |
549 | stages.push_back(stage); | |
7649683c ML |
550 | return static_pointer_cast<TriggerStage>( |
551 | stage->get_shared_pointer(this)); | |
c23c8659 ML |
552 | } |
553 | ||
554 | TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : | |
555 | StructureWrapper<Trigger, struct sr_trigger_stage>(structure) | |
556 | { | |
557 | } | |
558 | ||
559 | TriggerStage::~TriggerStage() | |
560 | { | |
561 | for (auto match : matches) | |
562 | delete match; | |
563 | } | |
564 | ||
565 | int TriggerStage::get_number() | |
566 | { | |
567 | return structure->stage; | |
568 | } | |
569 | ||
570 | vector<shared_ptr<TriggerMatch>> TriggerStage::get_matches() | |
571 | { | |
572 | vector<shared_ptr<TriggerMatch>> result; | |
573 | for (auto match : matches) | |
7649683c ML |
574 | result.push_back(static_pointer_cast<TriggerMatch>( |
575 | match->get_shared_pointer(this))); | |
c23c8659 ML |
576 | return result; |
577 | } | |
578 | ||
579 | void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value) | |
580 | { | |
581 | check(sr_trigger_match_add(structure, channel->structure, type->get_id(), value)); | |
582 | matches.push_back(new TriggerMatch( | |
583 | (struct sr_trigger_match *) g_slist_last(structure->matches)->data, channel)); | |
584 | } | |
585 | ||
586 | void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type) | |
587 | { | |
588 | add_match(channel, type, NAN); | |
589 | } | |
590 | ||
591 | TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel) : | |
592 | StructureWrapper<TriggerStage, struct sr_trigger_match>(structure), channel(channel) | |
593 | { | |
594 | } | |
595 | ||
596 | TriggerMatch::~TriggerMatch() | |
597 | { | |
598 | } | |
599 | ||
600 | shared_ptr<Channel> TriggerMatch::get_channel() | |
601 | { | |
602 | return channel; | |
603 | } | |
604 | ||
605 | const TriggerMatchType *TriggerMatch::get_type() | |
606 | { | |
607 | return TriggerMatchType::get(structure->match); | |
608 | } | |
609 | ||
610 | float TriggerMatch::get_value() | |
611 | { | |
612 | return structure->value; | |
613 | } | |
614 | ||
615 | DatafeedCallbackData::DatafeedCallbackData(Session *session, | |
616 | DatafeedCallbackFunction callback) : | |
617 | callback(callback), session(session) | |
618 | { | |
619 | } | |
620 | ||
621 | void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, | |
622 | const struct sr_datafeed_packet *pkt) | |
623 | { | |
624 | auto device = session->devices[sdi]; | |
2928f47d | 625 | auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter()); |
c23c8659 ML |
626 | callback(device, packet); |
627 | } | |
628 | ||
629 | SourceCallbackData::SourceCallbackData(shared_ptr<EventSource> source) : | |
630 | source(source) | |
631 | { | |
632 | } | |
633 | ||
634 | bool SourceCallbackData::run(int revents) | |
635 | { | |
636 | return source->callback((Glib::IOCondition) revents); | |
637 | } | |
638 | ||
639 | shared_ptr<EventSource> EventSource::create(int fd, Glib::IOCondition events, | |
640 | int timeout, SourceCallbackFunction callback) | |
641 | { | |
642 | auto result = new EventSource(timeout, callback); | |
643 | result->type = EventSource::SOURCE_FD; | |
644 | result->fd = fd; | |
645 | result->events = events; | |
646 | return shared_ptr<EventSource>(result, EventSource::Deleter()); | |
647 | } | |
648 | ||
649 | shared_ptr<EventSource> EventSource::create(Glib::PollFD pollfd, int timeout, | |
650 | SourceCallbackFunction callback) | |
651 | { | |
652 | auto result = new EventSource(timeout, callback); | |
653 | result->type = EventSource::SOURCE_POLLFD; | |
654 | result->pollfd = pollfd; | |
655 | return shared_ptr<EventSource>(result, EventSource::Deleter()); | |
656 | } | |
657 | ||
658 | shared_ptr<EventSource> EventSource::create(Glib::RefPtr<Glib::IOChannel> channel, | |
659 | Glib::IOCondition events, int timeout, SourceCallbackFunction callback) | |
660 | { | |
661 | auto result = new EventSource(timeout, callback); | |
662 | result->type = EventSource::SOURCE_IOCHANNEL; | |
663 | result->channel = channel; | |
664 | result->events = events; | |
665 | return shared_ptr<EventSource>(result, EventSource::Deleter()); | |
666 | } | |
667 | ||
668 | EventSource::EventSource(int timeout, SourceCallbackFunction callback) : | |
669 | timeout(timeout), callback(callback) | |
670 | { | |
671 | } | |
672 | ||
673 | EventSource::~EventSource() | |
674 | { | |
675 | } | |
676 | ||
677 | Session::Session(shared_ptr<Context> context) : | |
678 | context(context), saving(false) | |
679 | { | |
680 | check(sr_session_new(&structure)); | |
681 | context->session = this; | |
682 | } | |
683 | ||
684 | Session::Session(shared_ptr<Context> context, string filename) : | |
685 | context(context), saving(false) | |
686 | { | |
687 | check(sr_session_load(filename.c_str(), &structure)); | |
688 | context->session = this; | |
689 | } | |
690 | ||
691 | Session::~Session() | |
692 | { | |
693 | check(sr_session_destroy(structure)); | |
694 | ||
695 | for (auto callback : datafeed_callbacks) | |
696 | delete callback; | |
697 | ||
698 | for (auto entry : source_callbacks) | |
699 | delete entry.second; | |
700 | } | |
701 | ||
702 | void Session::add_device(shared_ptr<Device> device) | |
703 | { | |
704 | check(sr_session_dev_add(structure, device->structure)); | |
705 | devices[device->structure] = device; | |
706 | } | |
707 | ||
708 | vector<shared_ptr<Device>> Session::get_devices() | |
709 | { | |
710 | GSList *dev_list; | |
711 | check(sr_session_dev_list(structure, &dev_list)); | |
712 | vector<shared_ptr<Device>> result; | |
713 | for (GSList *dev = dev_list; dev; dev = dev->next) | |
714 | { | |
715 | auto sdi = (struct sr_dev_inst *) dev->data; | |
716 | if (devices.count(sdi) == 0) | |
717 | devices[sdi] = shared_ptr<Device>( | |
718 | new Device(sdi), Device::Deleter()); | |
719 | result.push_back(devices[sdi]); | |
720 | } | |
721 | return result; | |
722 | } | |
723 | ||
724 | void Session::remove_devices() | |
725 | { | |
726 | devices.clear(); | |
727 | check(sr_session_dev_remove_all(structure)); | |
728 | } | |
729 | ||
730 | void Session::start() | |
731 | { | |
732 | check(sr_session_start(structure)); | |
733 | } | |
734 | ||
735 | void Session::run() | |
736 | { | |
737 | check(sr_session_run(structure)); | |
738 | } | |
739 | ||
740 | void Session::stop() | |
741 | { | |
742 | check(sr_session_stop(structure)); | |
743 | } | |
744 | ||
745 | void Session::begin_save(string filename) | |
746 | { | |
747 | saving = true; | |
748 | save_initialized = false; | |
749 | save_filename = filename; | |
750 | save_samplerate = 0; | |
751 | } | |
752 | ||
1d67cfb4 | 753 | void Session::append(shared_ptr<Packet> packet) |
c23c8659 ML |
754 | { |
755 | if (!saving) | |
756 | throw Error(SR_ERR); | |
757 | ||
758 | switch (packet->structure->type) | |
759 | { | |
760 | case SR_DF_META: | |
761 | { | |
762 | auto meta = (const struct sr_datafeed_meta *) | |
763 | packet->structure->payload; | |
764 | ||
765 | for (auto l = meta->config; l; l = l->next) | |
766 | { | |
767 | auto config = (struct sr_config *) l->data; | |
768 | if (config->key == SR_CONF_SAMPLERATE) | |
769 | save_samplerate = g_variant_get_uint64(config->data); | |
770 | } | |
771 | ||
772 | break; | |
773 | } | |
774 | case SR_DF_LOGIC: | |
775 | { | |
776 | if (save_samplerate == 0) | |
777 | { | |
778 | GVariant *samplerate; | |
779 | ||
1d67cfb4 ML |
780 | check(sr_config_get(packet->device->structure->driver, |
781 | packet->device->structure, NULL, SR_CONF_SAMPLERATE, | |
782 | &samplerate)); | |
c23c8659 ML |
783 | |
784 | save_samplerate = g_variant_get_uint64(samplerate); | |
785 | ||
786 | g_variant_unref(samplerate); | |
787 | } | |
788 | ||
789 | if (!save_initialized) | |
790 | { | |
791 | vector<shared_ptr<Channel>> save_channels; | |
792 | ||
1d67cfb4 | 793 | for (auto channel : packet->device->get_channels()) |
c23c8659 ML |
794 | if (channel->structure->enabled && |
795 | channel->structure->type == SR_CHANNEL_LOGIC) | |
796 | save_channels.push_back(channel); | |
797 | ||
798 | auto channels = g_new(char *, save_channels.size()); | |
799 | ||
800 | int i = 0; | |
801 | for (auto channel : save_channels) | |
802 | channels[i++] = channel->structure->name; | |
803 | channels[i] = NULL; | |
804 | ||
805 | int ret = sr_session_save_init(structure, save_filename.c_str(), | |
806 | save_samplerate, channels); | |
807 | ||
808 | g_free(channels); | |
809 | ||
810 | if (ret != SR_OK) | |
811 | throw Error(ret); | |
812 | ||
813 | save_initialized = true; | |
814 | } | |
815 | ||
816 | auto logic = (const struct sr_datafeed_logic *) | |
817 | packet->structure->payload; | |
818 | ||
819 | check(sr_session_append(structure, save_filename.c_str(), | |
820 | (uint8_t *) logic->data, logic->unitsize, | |
821 | logic->length / logic->unitsize)); | |
822 | } | |
823 | } | |
824 | } | |
825 | ||
1d67cfb4 ML |
826 | void Session::append(void *data, size_t length, unsigned int unit_size) |
827 | { | |
828 | check(sr_session_append(structure, save_filename.c_str(), | |
829 | (uint8_t *) data, unit_size, length)); | |
830 | } | |
831 | ||
c23c8659 ML |
832 | static void datafeed_callback(const struct sr_dev_inst *sdi, |
833 | const struct sr_datafeed_packet *pkt, void *cb_data) | |
834 | { | |
835 | auto callback = static_cast<DatafeedCallbackData *>(cb_data); | |
836 | callback->run(sdi, pkt); | |
837 | } | |
838 | ||
839 | void Session::add_datafeed_callback(DatafeedCallbackFunction callback) | |
840 | { | |
841 | auto cb_data = new DatafeedCallbackData(this, callback); | |
842 | check(sr_session_datafeed_callback_add(structure, datafeed_callback, cb_data)); | |
843 | datafeed_callbacks.push_back(cb_data); | |
844 | } | |
845 | ||
846 | void Session::remove_datafeed_callbacks(void) | |
847 | { | |
848 | check(sr_session_datafeed_callback_remove_all(structure)); | |
849 | for (auto callback : datafeed_callbacks) | |
850 | delete callback; | |
851 | datafeed_callbacks.clear(); | |
852 | } | |
853 | ||
854 | static int source_callback(int fd, int revents, void *cb_data) | |
855 | { | |
856 | (void) fd; | |
857 | auto callback = (SourceCallbackData *) cb_data; | |
858 | return callback->run(revents); | |
859 | } | |
860 | ||
861 | void Session::add_source(shared_ptr<EventSource> source) | |
862 | { | |
863 | if (source_callbacks.count(source) == 1) | |
864 | throw Error(SR_ERR_ARG); | |
865 | ||
866 | auto cb_data = new SourceCallbackData(source); | |
867 | ||
868 | switch (source->type) | |
869 | { | |
870 | case EventSource::SOURCE_FD: | |
871 | check(sr_session_source_add(structure, source->fd, source->events, | |
872 | source->timeout, source_callback, cb_data)); | |
873 | break; | |
874 | case EventSource::SOURCE_POLLFD: | |
875 | check(sr_session_source_add_pollfd(structure, | |
876 | source->pollfd.gobj(), source->timeout, source_callback, | |
877 | cb_data)); | |
878 | break; | |
879 | case EventSource::SOURCE_IOCHANNEL: | |
880 | check(sr_session_source_add_channel(structure, | |
881 | source->channel->gobj(), source->events, source->timeout, | |
882 | source_callback, cb_data)); | |
883 | break; | |
884 | } | |
885 | ||
886 | source_callbacks[source] = cb_data; | |
887 | } | |
888 | ||
889 | void Session::remove_source(shared_ptr<EventSource> source) | |
890 | { | |
891 | if (source_callbacks.count(source) == 0) | |
892 | throw Error(SR_ERR_ARG); | |
893 | ||
894 | switch (source->type) | |
895 | { | |
896 | case EventSource::SOURCE_FD: | |
897 | check(sr_session_source_remove(structure, source->fd)); | |
898 | break; | |
899 | case EventSource::SOURCE_POLLFD: | |
900 | check(sr_session_source_remove_pollfd(structure, | |
901 | source->pollfd.gobj())); | |
902 | break; | |
903 | case EventSource::SOURCE_IOCHANNEL: | |
904 | check(sr_session_source_remove_channel(structure, | |
905 | source->channel->gobj())); | |
906 | break; | |
907 | } | |
908 | ||
909 | delete source_callbacks[source]; | |
910 | ||
911 | source_callbacks.erase(source); | |
912 | } | |
913 | ||
6fa0eb86 ML |
914 | shared_ptr<Trigger> Session::get_trigger() |
915 | { | |
916 | return trigger; | |
917 | } | |
918 | ||
919 | void Session::set_trigger(shared_ptr<Trigger> trigger) | |
920 | { | |
921 | check(sr_session_trigger_set(structure, trigger->structure)); | |
922 | this->trigger = trigger; | |
923 | } | |
924 | ||
2928f47d ML |
925 | Packet::Packet(shared_ptr<Device> device, |
926 | const struct sr_datafeed_packet *structure) : | |
927 | structure(structure), | |
928 | device(device) | |
c23c8659 ML |
929 | { |
930 | switch (structure->type) | |
931 | { | |
2928f47d ML |
932 | case SR_DF_HEADER: |
933 | payload = new Header( | |
934 | static_cast<const struct sr_datafeed_header *>( | |
935 | structure->payload)); | |
936 | break; | |
937 | case SR_DF_META: | |
938 | payload = new Meta( | |
939 | static_cast<const struct sr_datafeed_meta *>( | |
940 | structure->payload)); | |
941 | break; | |
c23c8659 ML |
942 | case SR_DF_LOGIC: |
943 | payload = new Logic( | |
944 | static_cast<const struct sr_datafeed_logic *>( | |
945 | structure->payload)); | |
946 | break; | |
947 | case SR_DF_ANALOG: | |
948 | payload = new Analog( | |
949 | static_cast<const struct sr_datafeed_analog *>( | |
950 | structure->payload)); | |
951 | break; | |
c23c8659 ML |
952 | } |
953 | } | |
954 | ||
955 | Packet::~Packet() | |
956 | { | |
1797a887 ML |
957 | if (payload) |
958 | delete payload; | |
c23c8659 ML |
959 | } |
960 | ||
90ba83f2 ML |
961 | const PacketType *Packet::get_type() |
962 | { | |
963 | return PacketType::get(structure->type); | |
964 | } | |
965 | ||
2928f47d | 966 | shared_ptr<PacketPayload> Packet::get_payload() |
c23c8659 | 967 | { |
2928f47d | 968 | return payload->get_shared_pointer(this); |
c23c8659 ML |
969 | } |
970 | ||
971 | PacketPayload::PacketPayload() | |
972 | { | |
973 | } | |
974 | ||
975 | PacketPayload::~PacketPayload() | |
976 | { | |
977 | } | |
978 | ||
2928f47d ML |
979 | Header::Header(const struct sr_datafeed_header *structure) : |
980 | PacketPayload(), | |
981 | StructureWrapper<Packet, const struct sr_datafeed_header>(structure) | |
982 | { | |
983 | } | |
984 | ||
985 | Header::~Header() | |
986 | { | |
987 | } | |
988 | ||
989 | int Header::get_feed_version() | |
990 | { | |
991 | return structure->feed_version; | |
992 | } | |
993 | ||
994 | Glib::TimeVal Header::get_start_time() | |
995 | { | |
996 | return Glib::TimeVal( | |
997 | structure->starttime.tv_sec, | |
998 | structure->starttime.tv_usec); | |
999 | } | |
1000 | ||
1001 | Meta::Meta(const struct sr_datafeed_meta *structure) : | |
1002 | PacketPayload(), | |
1003 | StructureWrapper<Packet, const struct sr_datafeed_meta>(structure) | |
1004 | { | |
1005 | } | |
1006 | ||
1007 | Meta::~Meta() | |
1008 | { | |
1009 | } | |
1010 | ||
1011 | map<const ConfigKey *, Glib::VariantBase> Meta::get_config() | |
1012 | { | |
1013 | map<const ConfigKey *, Glib::VariantBase> result; | |
1014 | for (auto l = structure->config; l; l = l->next) | |
1015 | { | |
1016 | auto config = (struct sr_config *) l->data; | |
1017 | result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data); | |
1018 | } | |
1019 | return result; | |
1020 | } | |
1021 | ||
1022 | Logic::Logic(const struct sr_datafeed_logic *structure) : | |
1023 | PacketPayload(), | |
1024 | StructureWrapper<Packet, const struct sr_datafeed_logic>(structure) | |
1025 | { | |
1026 | } | |
c23c8659 ML |
1027 | |
1028 | Logic::~Logic() | |
1029 | { | |
1030 | } | |
1031 | ||
2928f47d | 1032 | void *Logic::get_data_pointer() |
c23c8659 ML |
1033 | { |
1034 | return structure->data; | |
1035 | } | |
1036 | ||
2928f47d | 1037 | size_t Logic::get_data_length() |
c23c8659 ML |
1038 | { |
1039 | return structure->length; | |
1040 | } | |
1041 | ||
2928f47d ML |
1042 | unsigned int Logic::get_unit_size() |
1043 | { | |
1044 | return structure->unitsize; | |
1045 | } | |
1046 | ||
c23c8659 ML |
1047 | Analog::Analog(const struct sr_datafeed_analog *structure) : |
1048 | PacketPayload(), | |
2928f47d | 1049 | StructureWrapper<Packet, const struct sr_datafeed_analog>(structure) |
c23c8659 ML |
1050 | { |
1051 | } | |
1052 | ||
1053 | Analog::~Analog() | |
1054 | { | |
1055 | } | |
1056 | ||
2928f47d | 1057 | float *Analog::get_data_pointer() |
c23c8659 ML |
1058 | { |
1059 | return structure->data; | |
1060 | } | |
1061 | ||
2928f47d | 1062 | unsigned int Analog::get_num_samples() |
c23c8659 | 1063 | { |
2928f47d | 1064 | return structure->num_samples; |
c23c8659 ML |
1065 | } |
1066 | ||
2928f47d | 1067 | vector<shared_ptr<Channel>> Analog::get_channels() |
c23c8659 | 1068 | { |
2928f47d ML |
1069 | vector<shared_ptr<Channel>> result; |
1070 | for (auto l = structure->channels; l; l = l->next) | |
1071 | result.push_back(parent->device->get_channel( | |
1072 | (struct sr_channel *)l->data)); | |
1073 | return result; | |
c23c8659 ML |
1074 | } |
1075 | ||
1076 | const Quantity *Analog::get_mq() | |
1077 | { | |
1078 | return Quantity::get(structure->mq); | |
1079 | } | |
1080 | ||
1081 | const Unit *Analog::get_unit() | |
1082 | { | |
1083 | return Unit::get(structure->unit); | |
1084 | } | |
1085 | ||
1086 | vector<const QuantityFlag *> Analog::get_mq_flags() | |
1087 | { | |
1088 | return QuantityFlag::flags_from_mask(structure->mqflags); | |
1089 | } | |
1090 | ||
1091 | InputFormat::InputFormat(struct sr_input_format *structure) : | |
1092 | StructureWrapper<Context, struct sr_input_format>(structure) | |
1093 | { | |
1094 | } | |
1095 | ||
1096 | InputFormat::~InputFormat() | |
1097 | { | |
1098 | } | |
1099 | ||
1100 | string InputFormat::get_name() | |
1101 | { | |
1102 | return valid_string(structure->id); | |
1103 | } | |
1104 | ||
1105 | string InputFormat::get_description() | |
1106 | { | |
1107 | return valid_string(structure->description); | |
1108 | } | |
1109 | ||
1110 | bool InputFormat::format_match(string filename) | |
1111 | { | |
1112 | return structure->format_match(filename.c_str()); | |
1113 | } | |
1114 | ||
1115 | shared_ptr<InputFileDevice> InputFormat::open_file(string filename, | |
1116 | map<string, string> options) | |
1117 | { | |
1118 | auto input = g_new(struct sr_input, 1); | |
1119 | input->param = map_to_hash(options); | |
1120 | ||
1121 | /** Run initialisation. */ | |
1122 | check(structure->init(input, filename.c_str())); | |
1123 | ||
1124 | /** Create virtual device. */ | |
1125 | return shared_ptr<InputFileDevice>(new InputFileDevice( | |
1126 | static_pointer_cast<InputFormat>(shared_from_this()), input, filename), | |
1127 | InputFileDevice::Deleter()); | |
1128 | } | |
1129 | ||
1130 | InputFileDevice::InputFileDevice(shared_ptr<InputFormat> format, | |
1131 | struct sr_input *input, string filename) : | |
1132 | Device(input->sdi), | |
1133 | input(input), | |
1134 | format(format), | |
1135 | filename(filename) | |
1136 | { | |
1137 | } | |
1138 | ||
1139 | InputFileDevice::~InputFileDevice() | |
1140 | { | |
1141 | g_hash_table_unref(input->param); | |
1142 | g_free(input); | |
1143 | } | |
1144 | ||
1145 | void InputFileDevice::load() | |
1146 | { | |
1147 | check(format->structure->loadfile(input, filename.c_str())); | |
1148 | } | |
1149 | ||
1150 | OutputFormat::OutputFormat(struct sr_output_format *structure) : | |
1151 | StructureWrapper<Context, struct sr_output_format>(structure) | |
1152 | { | |
1153 | } | |
1154 | ||
1155 | OutputFormat::~OutputFormat() | |
1156 | { | |
1157 | } | |
1158 | ||
1159 | string OutputFormat::get_name() | |
1160 | { | |
1161 | return valid_string(structure->id); | |
1162 | } | |
1163 | ||
1164 | string OutputFormat::get_description() | |
1165 | { | |
1166 | return valid_string(structure->description); | |
1167 | } | |
1168 | ||
1169 | shared_ptr<Output> OutputFormat::create_output( | |
1170 | shared_ptr<Device> device, map<string, string> options) | |
1171 | { | |
1172 | return shared_ptr<Output>( | |
1173 | new Output( | |
1174 | static_pointer_cast<OutputFormat>(shared_from_this()), | |
1175 | device, options), | |
1176 | Output::Deleter()); | |
1177 | } | |
1178 | ||
1179 | Output::Output(shared_ptr<OutputFormat> format, | |
1180 | shared_ptr<Device> device, map<string, string> options) : | |
1181 | structure(sr_output_new(format->structure, | |
1182 | map_to_hash(options), device->structure)), | |
1183 | format(format), device(device), options(options) | |
1184 | { | |
1185 | } | |
1186 | ||
1187 | Output::~Output() | |
1188 | { | |
1189 | g_hash_table_unref(structure->params); | |
1190 | check(sr_output_free(structure)); | |
1191 | } | |
1192 | ||
1193 | string Output::receive(shared_ptr<Packet> packet) | |
1194 | { | |
1195 | GString *out; | |
1196 | check(sr_output_send(structure, packet->structure, &out)); | |
1197 | if (out) | |
1198 | { | |
1199 | auto result = string(out->str, out->str + out->len); | |
1200 | g_string_free(out, true); | |
1201 | return result; | |
1202 | } | |
1203 | else | |
1204 | { | |
1205 | return string(); | |
1206 | } | |
1207 | } | |
1208 | ||
1209 | #include "enums.cpp" | |
1210 | ||
1211 | } |