]>
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 | ||
e194c011 | 331 | Glib::VariantContainerBase Configurable::config_list(const ConfigKey *key) |
c23c8659 ML |
332 | { |
333 | GVariant *data; | |
334 | check(sr_config_list( | |
335 | config_driver, config_sdi, config_channel_group, | |
336 | key->get_id(), &data)); | |
e194c011 | 337 | return Glib::VariantContainerBase(data); |
c23c8659 ML |
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; | |
4178d971 | 347 | channels[channel] = new Channel(channel); |
c23c8659 | 348 | } |
6be7a7f2 ML |
349 | |
350 | for (GSList *entry = structure->channel_groups; entry; entry = entry->next) | |
351 | { | |
352 | auto group = (struct sr_channel_group *) entry->data; | |
353 | channel_groups[group->name] = new ChannelGroup(this, group); | |
354 | } | |
c23c8659 ML |
355 | } |
356 | ||
357 | Device::~Device() | |
358 | { | |
4178d971 ML |
359 | for (auto entry : channels) |
360 | delete entry.second; | |
6be7a7f2 ML |
361 | for (auto entry : channel_groups) |
362 | delete entry.second; | |
c23c8659 ML |
363 | } |
364 | ||
365 | string Device::get_vendor() | |
366 | { | |
367 | return valid_string(structure->vendor); | |
368 | } | |
369 | ||
370 | string Device::get_model() | |
371 | { | |
372 | return valid_string(structure->model); | |
373 | } | |
374 | ||
375 | string Device::get_version() | |
376 | { | |
377 | return valid_string(structure->version); | |
378 | } | |
379 | ||
380 | vector<shared_ptr<Channel>> Device::get_channels() | |
381 | { | |
382 | vector<shared_ptr<Channel>> result; | |
4178d971 | 383 | for (auto entry : channels) |
7649683c | 384 | result.push_back(static_pointer_cast<Channel>( |
4178d971 | 385 | entry.second->get_shared_pointer(this))); |
c23c8659 ML |
386 | return result; |
387 | } | |
388 | ||
4178d971 ML |
389 | shared_ptr<Channel> Device::get_channel(struct sr_channel *ptr) |
390 | { | |
391 | return static_pointer_cast<Channel>( | |
392 | channels[ptr]->get_shared_pointer(this)); | |
393 | } | |
394 | ||
6be7a7f2 ML |
395 | map<string, shared_ptr<ChannelGroup>> |
396 | Device::get_channel_groups() | |
397 | { | |
398 | map<string, shared_ptr<ChannelGroup>> result; | |
399 | for (auto entry: channel_groups) | |
400 | { | |
401 | auto name = entry.first; | |
402 | auto channel_group = entry.second; | |
403 | result[name] = static_pointer_cast<ChannelGroup>( | |
404 | channel_group->get_shared_pointer(this)); | |
405 | } | |
406 | return result; | |
407 | } | |
408 | ||
c23c8659 ML |
409 | void Device::open() |
410 | { | |
411 | check(sr_dev_open(structure)); | |
412 | } | |
413 | ||
414 | void Device::close() | |
415 | { | |
416 | check(sr_dev_close(structure)); | |
417 | } | |
418 | ||
419 | HardwareDevice::HardwareDevice(Driver *driver, struct sr_dev_inst *structure) : | |
420 | Device(structure), | |
421 | driver(driver) | |
422 | { | |
c23c8659 ML |
423 | } |
424 | ||
425 | HardwareDevice::~HardwareDevice() | |
426 | { | |
c23c8659 ML |
427 | } |
428 | ||
429 | shared_ptr<Driver> HardwareDevice::get_driver() | |
430 | { | |
7649683c | 431 | return static_pointer_cast<Driver>(driver->get_shared_pointer(parent)); |
c23c8659 ML |
432 | } |
433 | ||
c23c8659 ML |
434 | Channel::Channel(struct sr_channel *structure) : |
435 | StructureWrapper<Device, struct sr_channel>(structure), | |
436 | type(ChannelType::get(structure->type)) | |
437 | { | |
438 | } | |
439 | ||
440 | Channel::~Channel() | |
441 | { | |
442 | } | |
443 | ||
444 | string Channel::get_name() | |
445 | { | |
446 | return valid_string(structure->name); | |
447 | } | |
448 | ||
449 | void Channel::set_name(string name) | |
450 | { | |
451 | check(sr_dev_channel_name_set(parent->structure, structure->index, name.c_str())); | |
452 | } | |
453 | ||
454 | const ChannelType *Channel::get_type() | |
455 | { | |
456 | return ChannelType::get(structure->type); | |
457 | } | |
458 | ||
459 | bool Channel::get_enabled() | |
460 | { | |
461 | return structure->enabled; | |
462 | } | |
463 | ||
464 | void Channel::set_enabled(bool value) | |
465 | { | |
466 | check(sr_dev_channel_enable(parent->structure, structure->index, value)); | |
467 | } | |
468 | ||
6be7a7f2 | 469 | ChannelGroup::ChannelGroup(Device *device, |
c23c8659 | 470 | struct sr_channel_group *structure) : |
6be7a7f2 | 471 | StructureWrapper<Device, struct sr_channel_group>(structure), |
c23c8659 ML |
472 | Configurable(device->structure->driver, device->structure, structure) |
473 | { | |
474 | for (GSList *entry = structure->channels; entry; entry = entry->next) | |
4178d971 | 475 | channels.push_back(device->channels[(struct sr_channel *)entry->data]); |
c23c8659 ML |
476 | } |
477 | ||
478 | ChannelGroup::~ChannelGroup() | |
479 | { | |
480 | } | |
481 | ||
482 | string ChannelGroup::get_name() | |
483 | { | |
484 | return valid_string(structure->name); | |
485 | } | |
486 | ||
487 | vector<shared_ptr<Channel>> ChannelGroup::get_channels() | |
488 | { | |
489 | vector<shared_ptr<Channel>> result; | |
490 | for (auto channel : channels) | |
7649683c ML |
491 | result.push_back(static_pointer_cast<Channel>( |
492 | channel->get_shared_pointer(parent))); | |
c23c8659 ML |
493 | return result; |
494 | } | |
495 | ||
496 | Trigger::Trigger(shared_ptr<Context> context, string name) : | |
497 | structure(sr_trigger_new(name.c_str())), context(context) | |
498 | { | |
499 | for (auto stage = structure->stages; stage; stage = stage->next) | |
500 | stages.push_back(new TriggerStage((struct sr_trigger_stage *) stage->data)); | |
501 | } | |
502 | ||
503 | Trigger::~Trigger() | |
504 | { | |
505 | for (auto stage: stages) | |
506 | delete stage; | |
507 | ||
508 | sr_trigger_free(structure); | |
509 | } | |
510 | ||
511 | string Trigger::get_name() | |
512 | { | |
513 | return structure->name; | |
514 | } | |
515 | ||
516 | vector<shared_ptr<TriggerStage>> Trigger::get_stages() | |
517 | { | |
518 | vector<shared_ptr<TriggerStage>> result; | |
519 | for (auto stage : stages) | |
7649683c ML |
520 | result.push_back(static_pointer_cast<TriggerStage>( |
521 | stage->get_shared_pointer(this))); | |
c23c8659 ML |
522 | return result; |
523 | } | |
524 | ||
525 | shared_ptr<TriggerStage> Trigger::add_stage() | |
526 | { | |
527 | auto stage = new TriggerStage(sr_trigger_stage_add(structure)); | |
528 | stages.push_back(stage); | |
7649683c ML |
529 | return static_pointer_cast<TriggerStage>( |
530 | stage->get_shared_pointer(this)); | |
c23c8659 ML |
531 | } |
532 | ||
533 | TriggerStage::TriggerStage(struct sr_trigger_stage *structure) : | |
534 | StructureWrapper<Trigger, struct sr_trigger_stage>(structure) | |
535 | { | |
536 | } | |
537 | ||
538 | TriggerStage::~TriggerStage() | |
539 | { | |
540 | for (auto match : matches) | |
541 | delete match; | |
542 | } | |
543 | ||
544 | int TriggerStage::get_number() | |
545 | { | |
546 | return structure->stage; | |
547 | } | |
548 | ||
549 | vector<shared_ptr<TriggerMatch>> TriggerStage::get_matches() | |
550 | { | |
551 | vector<shared_ptr<TriggerMatch>> result; | |
552 | for (auto match : matches) | |
7649683c ML |
553 | result.push_back(static_pointer_cast<TriggerMatch>( |
554 | match->get_shared_pointer(this))); | |
c23c8659 ML |
555 | return result; |
556 | } | |
557 | ||
558 | void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type, float value) | |
559 | { | |
560 | check(sr_trigger_match_add(structure, channel->structure, type->get_id(), value)); | |
561 | matches.push_back(new TriggerMatch( | |
562 | (struct sr_trigger_match *) g_slist_last(structure->matches)->data, channel)); | |
563 | } | |
564 | ||
565 | void TriggerStage::add_match(shared_ptr<Channel> channel, const TriggerMatchType *type) | |
566 | { | |
567 | add_match(channel, type, NAN); | |
568 | } | |
569 | ||
570 | TriggerMatch::TriggerMatch(struct sr_trigger_match *structure, shared_ptr<Channel> channel) : | |
571 | StructureWrapper<TriggerStage, struct sr_trigger_match>(structure), channel(channel) | |
572 | { | |
573 | } | |
574 | ||
575 | TriggerMatch::~TriggerMatch() | |
576 | { | |
577 | } | |
578 | ||
579 | shared_ptr<Channel> TriggerMatch::get_channel() | |
580 | { | |
581 | return channel; | |
582 | } | |
583 | ||
584 | const TriggerMatchType *TriggerMatch::get_type() | |
585 | { | |
586 | return TriggerMatchType::get(structure->match); | |
587 | } | |
588 | ||
589 | float TriggerMatch::get_value() | |
590 | { | |
591 | return structure->value; | |
592 | } | |
593 | ||
594 | DatafeedCallbackData::DatafeedCallbackData(Session *session, | |
595 | DatafeedCallbackFunction callback) : | |
596 | callback(callback), session(session) | |
597 | { | |
598 | } | |
599 | ||
600 | void DatafeedCallbackData::run(const struct sr_dev_inst *sdi, | |
601 | const struct sr_datafeed_packet *pkt) | |
602 | { | |
603 | auto device = session->devices[sdi]; | |
2928f47d | 604 | auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter()); |
c23c8659 ML |
605 | callback(device, packet); |
606 | } | |
607 | ||
608 | SourceCallbackData::SourceCallbackData(shared_ptr<EventSource> source) : | |
609 | source(source) | |
610 | { | |
611 | } | |
612 | ||
613 | bool SourceCallbackData::run(int revents) | |
614 | { | |
615 | return source->callback((Glib::IOCondition) revents); | |
616 | } | |
617 | ||
618 | shared_ptr<EventSource> EventSource::create(int fd, Glib::IOCondition events, | |
619 | int timeout, SourceCallbackFunction callback) | |
620 | { | |
621 | auto result = new EventSource(timeout, callback); | |
622 | result->type = EventSource::SOURCE_FD; | |
623 | result->fd = fd; | |
624 | result->events = events; | |
625 | return shared_ptr<EventSource>(result, EventSource::Deleter()); | |
626 | } | |
627 | ||
628 | shared_ptr<EventSource> EventSource::create(Glib::PollFD pollfd, int timeout, | |
629 | SourceCallbackFunction callback) | |
630 | { | |
631 | auto result = new EventSource(timeout, callback); | |
632 | result->type = EventSource::SOURCE_POLLFD; | |
633 | result->pollfd = pollfd; | |
634 | return shared_ptr<EventSource>(result, EventSource::Deleter()); | |
635 | } | |
636 | ||
637 | shared_ptr<EventSource> EventSource::create(Glib::RefPtr<Glib::IOChannel> channel, | |
638 | Glib::IOCondition events, int timeout, SourceCallbackFunction callback) | |
639 | { | |
640 | auto result = new EventSource(timeout, callback); | |
641 | result->type = EventSource::SOURCE_IOCHANNEL; | |
642 | result->channel = channel; | |
643 | result->events = events; | |
644 | return shared_ptr<EventSource>(result, EventSource::Deleter()); | |
645 | } | |
646 | ||
647 | EventSource::EventSource(int timeout, SourceCallbackFunction callback) : | |
648 | timeout(timeout), callback(callback) | |
649 | { | |
650 | } | |
651 | ||
652 | EventSource::~EventSource() | |
653 | { | |
654 | } | |
655 | ||
656 | Session::Session(shared_ptr<Context> context) : | |
657 | context(context), saving(false) | |
658 | { | |
659 | check(sr_session_new(&structure)); | |
660 | context->session = this; | |
661 | } | |
662 | ||
663 | Session::Session(shared_ptr<Context> context, string filename) : | |
664 | context(context), saving(false) | |
665 | { | |
666 | check(sr_session_load(filename.c_str(), &structure)); | |
667 | context->session = this; | |
668 | } | |
669 | ||
670 | Session::~Session() | |
671 | { | |
672 | check(sr_session_destroy(structure)); | |
673 | ||
674 | for (auto callback : datafeed_callbacks) | |
675 | delete callback; | |
676 | ||
677 | for (auto entry : source_callbacks) | |
678 | delete entry.second; | |
679 | } | |
680 | ||
681 | void Session::add_device(shared_ptr<Device> device) | |
682 | { | |
683 | check(sr_session_dev_add(structure, device->structure)); | |
684 | devices[device->structure] = device; | |
685 | } | |
686 | ||
687 | vector<shared_ptr<Device>> Session::get_devices() | |
688 | { | |
689 | GSList *dev_list; | |
690 | check(sr_session_dev_list(structure, &dev_list)); | |
691 | vector<shared_ptr<Device>> result; | |
692 | for (GSList *dev = dev_list; dev; dev = dev->next) | |
693 | { | |
694 | auto sdi = (struct sr_dev_inst *) dev->data; | |
695 | if (devices.count(sdi) == 0) | |
696 | devices[sdi] = shared_ptr<Device>( | |
697 | new Device(sdi), Device::Deleter()); | |
698 | result.push_back(devices[sdi]); | |
699 | } | |
700 | return result; | |
701 | } | |
702 | ||
703 | void Session::remove_devices() | |
704 | { | |
705 | devices.clear(); | |
706 | check(sr_session_dev_remove_all(structure)); | |
707 | } | |
708 | ||
709 | void Session::start() | |
710 | { | |
711 | check(sr_session_start(structure)); | |
712 | } | |
713 | ||
714 | void Session::run() | |
715 | { | |
716 | check(sr_session_run(structure)); | |
717 | } | |
718 | ||
719 | void Session::stop() | |
720 | { | |
721 | check(sr_session_stop(structure)); | |
722 | } | |
723 | ||
724 | void Session::begin_save(string filename) | |
725 | { | |
726 | saving = true; | |
727 | save_initialized = false; | |
728 | save_filename = filename; | |
729 | save_samplerate = 0; | |
730 | } | |
731 | ||
1d67cfb4 | 732 | void Session::append(shared_ptr<Packet> packet) |
c23c8659 ML |
733 | { |
734 | if (!saving) | |
735 | throw Error(SR_ERR); | |
736 | ||
737 | switch (packet->structure->type) | |
738 | { | |
739 | case SR_DF_META: | |
740 | { | |
741 | auto meta = (const struct sr_datafeed_meta *) | |
742 | packet->structure->payload; | |
743 | ||
744 | for (auto l = meta->config; l; l = l->next) | |
745 | { | |
746 | auto config = (struct sr_config *) l->data; | |
747 | if (config->key == SR_CONF_SAMPLERATE) | |
748 | save_samplerate = g_variant_get_uint64(config->data); | |
749 | } | |
750 | ||
751 | break; | |
752 | } | |
753 | case SR_DF_LOGIC: | |
754 | { | |
755 | if (save_samplerate == 0) | |
756 | { | |
757 | GVariant *samplerate; | |
758 | ||
1d67cfb4 ML |
759 | check(sr_config_get(packet->device->structure->driver, |
760 | packet->device->structure, NULL, SR_CONF_SAMPLERATE, | |
761 | &samplerate)); | |
c23c8659 ML |
762 | |
763 | save_samplerate = g_variant_get_uint64(samplerate); | |
764 | ||
765 | g_variant_unref(samplerate); | |
766 | } | |
767 | ||
768 | if (!save_initialized) | |
769 | { | |
770 | vector<shared_ptr<Channel>> save_channels; | |
771 | ||
1d67cfb4 | 772 | for (auto channel : packet->device->get_channels()) |
c23c8659 ML |
773 | if (channel->structure->enabled && |
774 | channel->structure->type == SR_CHANNEL_LOGIC) | |
775 | save_channels.push_back(channel); | |
776 | ||
777 | auto channels = g_new(char *, save_channels.size()); | |
778 | ||
779 | int i = 0; | |
780 | for (auto channel : save_channels) | |
781 | channels[i++] = channel->structure->name; | |
782 | channels[i] = NULL; | |
783 | ||
784 | int ret = sr_session_save_init(structure, save_filename.c_str(), | |
785 | save_samplerate, channels); | |
786 | ||
787 | g_free(channels); | |
788 | ||
789 | if (ret != SR_OK) | |
790 | throw Error(ret); | |
791 | ||
792 | save_initialized = true; | |
793 | } | |
794 | ||
795 | auto logic = (const struct sr_datafeed_logic *) | |
796 | packet->structure->payload; | |
797 | ||
798 | check(sr_session_append(structure, save_filename.c_str(), | |
799 | (uint8_t *) logic->data, logic->unitsize, | |
800 | logic->length / logic->unitsize)); | |
801 | } | |
802 | } | |
803 | } | |
804 | ||
1d67cfb4 ML |
805 | void Session::append(void *data, size_t length, unsigned int unit_size) |
806 | { | |
807 | check(sr_session_append(structure, save_filename.c_str(), | |
808 | (uint8_t *) data, unit_size, length)); | |
809 | } | |
810 | ||
c23c8659 ML |
811 | static void datafeed_callback(const struct sr_dev_inst *sdi, |
812 | const struct sr_datafeed_packet *pkt, void *cb_data) | |
813 | { | |
814 | auto callback = static_cast<DatafeedCallbackData *>(cb_data); | |
815 | callback->run(sdi, pkt); | |
816 | } | |
817 | ||
818 | void Session::add_datafeed_callback(DatafeedCallbackFunction callback) | |
819 | { | |
820 | auto cb_data = new DatafeedCallbackData(this, callback); | |
821 | check(sr_session_datafeed_callback_add(structure, datafeed_callback, cb_data)); | |
822 | datafeed_callbacks.push_back(cb_data); | |
823 | } | |
824 | ||
825 | void Session::remove_datafeed_callbacks(void) | |
826 | { | |
827 | check(sr_session_datafeed_callback_remove_all(structure)); | |
828 | for (auto callback : datafeed_callbacks) | |
829 | delete callback; | |
830 | datafeed_callbacks.clear(); | |
831 | } | |
832 | ||
833 | static int source_callback(int fd, int revents, void *cb_data) | |
834 | { | |
835 | (void) fd; | |
836 | auto callback = (SourceCallbackData *) cb_data; | |
837 | return callback->run(revents); | |
838 | } | |
839 | ||
840 | void Session::add_source(shared_ptr<EventSource> source) | |
841 | { | |
842 | if (source_callbacks.count(source) == 1) | |
843 | throw Error(SR_ERR_ARG); | |
844 | ||
845 | auto cb_data = new SourceCallbackData(source); | |
846 | ||
847 | switch (source->type) | |
848 | { | |
849 | case EventSource::SOURCE_FD: | |
850 | check(sr_session_source_add(structure, source->fd, source->events, | |
851 | source->timeout, source_callback, cb_data)); | |
852 | break; | |
853 | case EventSource::SOURCE_POLLFD: | |
854 | check(sr_session_source_add_pollfd(structure, | |
855 | source->pollfd.gobj(), source->timeout, source_callback, | |
856 | cb_data)); | |
857 | break; | |
858 | case EventSource::SOURCE_IOCHANNEL: | |
859 | check(sr_session_source_add_channel(structure, | |
860 | source->channel->gobj(), source->events, source->timeout, | |
861 | source_callback, cb_data)); | |
862 | break; | |
863 | } | |
864 | ||
865 | source_callbacks[source] = cb_data; | |
866 | } | |
867 | ||
868 | void Session::remove_source(shared_ptr<EventSource> source) | |
869 | { | |
870 | if (source_callbacks.count(source) == 0) | |
871 | throw Error(SR_ERR_ARG); | |
872 | ||
873 | switch (source->type) | |
874 | { | |
875 | case EventSource::SOURCE_FD: | |
876 | check(sr_session_source_remove(structure, source->fd)); | |
877 | break; | |
878 | case EventSource::SOURCE_POLLFD: | |
879 | check(sr_session_source_remove_pollfd(structure, | |
880 | source->pollfd.gobj())); | |
881 | break; | |
882 | case EventSource::SOURCE_IOCHANNEL: | |
883 | check(sr_session_source_remove_channel(structure, | |
884 | source->channel->gobj())); | |
885 | break; | |
886 | } | |
887 | ||
888 | delete source_callbacks[source]; | |
889 | ||
890 | source_callbacks.erase(source); | |
891 | } | |
892 | ||
6fa0eb86 ML |
893 | shared_ptr<Trigger> Session::get_trigger() |
894 | { | |
895 | return trigger; | |
896 | } | |
897 | ||
898 | void Session::set_trigger(shared_ptr<Trigger> trigger) | |
899 | { | |
900 | check(sr_session_trigger_set(structure, trigger->structure)); | |
901 | this->trigger = trigger; | |
902 | } | |
903 | ||
2928f47d ML |
904 | Packet::Packet(shared_ptr<Device> device, |
905 | const struct sr_datafeed_packet *structure) : | |
906 | structure(structure), | |
907 | device(device) | |
c23c8659 ML |
908 | { |
909 | switch (structure->type) | |
910 | { | |
2928f47d ML |
911 | case SR_DF_HEADER: |
912 | payload = new Header( | |
913 | static_cast<const struct sr_datafeed_header *>( | |
914 | structure->payload)); | |
915 | break; | |
916 | case SR_DF_META: | |
917 | payload = new Meta( | |
918 | static_cast<const struct sr_datafeed_meta *>( | |
919 | structure->payload)); | |
920 | break; | |
c23c8659 ML |
921 | case SR_DF_LOGIC: |
922 | payload = new Logic( | |
923 | static_cast<const struct sr_datafeed_logic *>( | |
924 | structure->payload)); | |
925 | break; | |
926 | case SR_DF_ANALOG: | |
927 | payload = new Analog( | |
928 | static_cast<const struct sr_datafeed_analog *>( | |
929 | structure->payload)); | |
930 | break; | |
c23c8659 ML |
931 | } |
932 | } | |
933 | ||
934 | Packet::~Packet() | |
935 | { | |
1797a887 ML |
936 | if (payload) |
937 | delete payload; | |
c23c8659 ML |
938 | } |
939 | ||
90ba83f2 ML |
940 | const PacketType *Packet::get_type() |
941 | { | |
942 | return PacketType::get(structure->type); | |
943 | } | |
944 | ||
2928f47d | 945 | shared_ptr<PacketPayload> Packet::get_payload() |
c23c8659 | 946 | { |
2928f47d | 947 | return payload->get_shared_pointer(this); |
c23c8659 ML |
948 | } |
949 | ||
950 | PacketPayload::PacketPayload() | |
951 | { | |
952 | } | |
953 | ||
954 | PacketPayload::~PacketPayload() | |
955 | { | |
956 | } | |
957 | ||
2928f47d ML |
958 | Header::Header(const struct sr_datafeed_header *structure) : |
959 | PacketPayload(), | |
960 | StructureWrapper<Packet, const struct sr_datafeed_header>(structure) | |
961 | { | |
962 | } | |
963 | ||
964 | Header::~Header() | |
965 | { | |
966 | } | |
967 | ||
968 | int Header::get_feed_version() | |
969 | { | |
970 | return structure->feed_version; | |
971 | } | |
972 | ||
973 | Glib::TimeVal Header::get_start_time() | |
974 | { | |
975 | return Glib::TimeVal( | |
976 | structure->starttime.tv_sec, | |
977 | structure->starttime.tv_usec); | |
978 | } | |
979 | ||
980 | Meta::Meta(const struct sr_datafeed_meta *structure) : | |
981 | PacketPayload(), | |
982 | StructureWrapper<Packet, const struct sr_datafeed_meta>(structure) | |
983 | { | |
984 | } | |
985 | ||
986 | Meta::~Meta() | |
987 | { | |
988 | } | |
989 | ||
990 | map<const ConfigKey *, Glib::VariantBase> Meta::get_config() | |
991 | { | |
992 | map<const ConfigKey *, Glib::VariantBase> result; | |
993 | for (auto l = structure->config; l; l = l->next) | |
994 | { | |
995 | auto config = (struct sr_config *) l->data; | |
996 | result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data); | |
997 | } | |
998 | return result; | |
999 | } | |
1000 | ||
1001 | Logic::Logic(const struct sr_datafeed_logic *structure) : | |
1002 | PacketPayload(), | |
1003 | StructureWrapper<Packet, const struct sr_datafeed_logic>(structure) | |
1004 | { | |
1005 | } | |
c23c8659 ML |
1006 | |
1007 | Logic::~Logic() | |
1008 | { | |
1009 | } | |
1010 | ||
2928f47d | 1011 | void *Logic::get_data_pointer() |
c23c8659 ML |
1012 | { |
1013 | return structure->data; | |
1014 | } | |
1015 | ||
2928f47d | 1016 | size_t Logic::get_data_length() |
c23c8659 ML |
1017 | { |
1018 | return structure->length; | |
1019 | } | |
1020 | ||
2928f47d ML |
1021 | unsigned int Logic::get_unit_size() |
1022 | { | |
1023 | return structure->unitsize; | |
1024 | } | |
1025 | ||
c23c8659 ML |
1026 | Analog::Analog(const struct sr_datafeed_analog *structure) : |
1027 | PacketPayload(), | |
2928f47d | 1028 | StructureWrapper<Packet, const struct sr_datafeed_analog>(structure) |
c23c8659 ML |
1029 | { |
1030 | } | |
1031 | ||
1032 | Analog::~Analog() | |
1033 | { | |
1034 | } | |
1035 | ||
2928f47d | 1036 | float *Analog::get_data_pointer() |
c23c8659 ML |
1037 | { |
1038 | return structure->data; | |
1039 | } | |
1040 | ||
2928f47d | 1041 | unsigned int Analog::get_num_samples() |
c23c8659 | 1042 | { |
2928f47d | 1043 | return structure->num_samples; |
c23c8659 ML |
1044 | } |
1045 | ||
2928f47d | 1046 | vector<shared_ptr<Channel>> Analog::get_channels() |
c23c8659 | 1047 | { |
2928f47d ML |
1048 | vector<shared_ptr<Channel>> result; |
1049 | for (auto l = structure->channels; l; l = l->next) | |
1050 | result.push_back(parent->device->get_channel( | |
1051 | (struct sr_channel *)l->data)); | |
1052 | return result; | |
c23c8659 ML |
1053 | } |
1054 | ||
1055 | const Quantity *Analog::get_mq() | |
1056 | { | |
1057 | return Quantity::get(structure->mq); | |
1058 | } | |
1059 | ||
1060 | const Unit *Analog::get_unit() | |
1061 | { | |
1062 | return Unit::get(structure->unit); | |
1063 | } | |
1064 | ||
1065 | vector<const QuantityFlag *> Analog::get_mq_flags() | |
1066 | { | |
1067 | return QuantityFlag::flags_from_mask(structure->mqflags); | |
1068 | } | |
1069 | ||
1070 | InputFormat::InputFormat(struct sr_input_format *structure) : | |
1071 | StructureWrapper<Context, struct sr_input_format>(structure) | |
1072 | { | |
1073 | } | |
1074 | ||
1075 | InputFormat::~InputFormat() | |
1076 | { | |
1077 | } | |
1078 | ||
1079 | string InputFormat::get_name() | |
1080 | { | |
1081 | return valid_string(structure->id); | |
1082 | } | |
1083 | ||
1084 | string InputFormat::get_description() | |
1085 | { | |
1086 | return valid_string(structure->description); | |
1087 | } | |
1088 | ||
1089 | bool InputFormat::format_match(string filename) | |
1090 | { | |
1091 | return structure->format_match(filename.c_str()); | |
1092 | } | |
1093 | ||
1094 | shared_ptr<InputFileDevice> InputFormat::open_file(string filename, | |
1095 | map<string, string> options) | |
1096 | { | |
1097 | auto input = g_new(struct sr_input, 1); | |
1098 | input->param = map_to_hash(options); | |
1099 | ||
1100 | /** Run initialisation. */ | |
1101 | check(structure->init(input, filename.c_str())); | |
1102 | ||
1103 | /** Create virtual device. */ | |
1104 | return shared_ptr<InputFileDevice>(new InputFileDevice( | |
1105 | static_pointer_cast<InputFormat>(shared_from_this()), input, filename), | |
1106 | InputFileDevice::Deleter()); | |
1107 | } | |
1108 | ||
1109 | InputFileDevice::InputFileDevice(shared_ptr<InputFormat> format, | |
1110 | struct sr_input *input, string filename) : | |
1111 | Device(input->sdi), | |
1112 | input(input), | |
1113 | format(format), | |
1114 | filename(filename) | |
1115 | { | |
1116 | } | |
1117 | ||
1118 | InputFileDevice::~InputFileDevice() | |
1119 | { | |
1120 | g_hash_table_unref(input->param); | |
1121 | g_free(input); | |
1122 | } | |
1123 | ||
1124 | void InputFileDevice::load() | |
1125 | { | |
1126 | check(format->structure->loadfile(input, filename.c_str())); | |
1127 | } | |
1128 | ||
1129 | OutputFormat::OutputFormat(struct sr_output_format *structure) : | |
1130 | StructureWrapper<Context, struct sr_output_format>(structure) | |
1131 | { | |
1132 | } | |
1133 | ||
1134 | OutputFormat::~OutputFormat() | |
1135 | { | |
1136 | } | |
1137 | ||
1138 | string OutputFormat::get_name() | |
1139 | { | |
1140 | return valid_string(structure->id); | |
1141 | } | |
1142 | ||
1143 | string OutputFormat::get_description() | |
1144 | { | |
1145 | return valid_string(structure->description); | |
1146 | } | |
1147 | ||
1148 | shared_ptr<Output> OutputFormat::create_output( | |
1149 | shared_ptr<Device> device, map<string, string> options) | |
1150 | { | |
1151 | return shared_ptr<Output>( | |
1152 | new Output( | |
1153 | static_pointer_cast<OutputFormat>(shared_from_this()), | |
1154 | device, options), | |
1155 | Output::Deleter()); | |
1156 | } | |
1157 | ||
1158 | Output::Output(shared_ptr<OutputFormat> format, | |
1159 | shared_ptr<Device> device, map<string, string> options) : | |
1160 | structure(sr_output_new(format->structure, | |
1161 | map_to_hash(options), device->structure)), | |
1162 | format(format), device(device), options(options) | |
1163 | { | |
1164 | } | |
1165 | ||
1166 | Output::~Output() | |
1167 | { | |
1168 | g_hash_table_unref(structure->params); | |
1169 | check(sr_output_free(structure)); | |
1170 | } | |
1171 | ||
1172 | string Output::receive(shared_ptr<Packet> packet) | |
1173 | { | |
1174 | GString *out; | |
1175 | check(sr_output_send(structure, packet->structure, &out)); | |
1176 | if (out) | |
1177 | { | |
1178 | auto result = string(out->str, out->str + out->len); | |
1179 | g_string_free(out, true); | |
1180 | return result; | |
1181 | } | |
1182 | else | |
1183 | { | |
1184 | return string(); | |
1185 | } | |
1186 | } | |
1187 | ||
1188 | #include "enums.cpp" | |
1189 | ||
1190 | } |