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