Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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 | |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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; |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 110 | result[name] = static_pointer_cast<Driver>( |
| 111 | driver->get_shared_pointer(this)); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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; |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 123 | result[name] = static_pointer_cast<InputFormat>( |
| 124 | input_format->get_shared_pointer(this)); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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; |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 136 | result[name] = static_pointer_cast<OutputFormat>( |
| 137 | output_format->get_shared_pointer(this)); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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) |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 296 | result.push_back(static_pointer_cast<HardwareDevice>( |
| 297 | device->get_shared_pointer(parent))); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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; |
Martin Ling | 4178d97 | 2014-07-24 03:22:44 +0100 | [diff] [blame] | 347 | channels[channel] = new Channel(channel); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 348 | } |
Martin Ling | 6be7a7f | 2014-07-24 12:41:49 +0100 | [diff] [blame^] | 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 | } |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | Device::~Device() |
| 358 | { |
Martin Ling | 4178d97 | 2014-07-24 03:22:44 +0100 | [diff] [blame] | 359 | for (auto entry : channels) |
| 360 | delete entry.second; |
Martin Ling | 6be7a7f | 2014-07-24 12:41:49 +0100 | [diff] [blame^] | 361 | for (auto entry : channel_groups) |
| 362 | delete entry.second; |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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; |
Martin Ling | 4178d97 | 2014-07-24 03:22:44 +0100 | [diff] [blame] | 383 | for (auto entry : channels) |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 384 | result.push_back(static_pointer_cast<Channel>( |
Martin Ling | 4178d97 | 2014-07-24 03:22:44 +0100 | [diff] [blame] | 385 | entry.second->get_shared_pointer(this))); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 386 | return result; |
| 387 | } |
| 388 | |
Martin Ling | 4178d97 | 2014-07-24 03:22:44 +0100 | [diff] [blame] | 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 | |
Martin Ling | 6be7a7f | 2014-07-24 12:41:49 +0100 | [diff] [blame^] | 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 | |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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 | { |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | HardwareDevice::~HardwareDevice() |
| 426 | { |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | shared_ptr<Driver> HardwareDevice::get_driver() |
| 430 | { |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 431 | return static_pointer_cast<Driver>(driver->get_shared_pointer(parent)); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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 | |
Martin Ling | 6be7a7f | 2014-07-24 12:41:49 +0100 | [diff] [blame^] | 469 | ChannelGroup::ChannelGroup(Device *device, |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 470 | struct sr_channel_group *structure) : |
Martin Ling | 6be7a7f | 2014-07-24 12:41:49 +0100 | [diff] [blame^] | 471 | StructureWrapper<Device, struct sr_channel_group>(structure), |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 472 | Configurable(device->structure->driver, device->structure, structure) |
| 473 | { |
| 474 | for (GSList *entry = structure->channels; entry; entry = entry->next) |
Martin Ling | 4178d97 | 2014-07-24 03:22:44 +0100 | [diff] [blame] | 475 | channels.push_back(device->channels[(struct sr_channel *)entry->data]); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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) |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 491 | result.push_back(static_pointer_cast<Channel>( |
| 492 | channel->get_shared_pointer(parent))); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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) |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 520 | result.push_back(static_pointer_cast<TriggerStage>( |
| 521 | stage->get_shared_pointer(this))); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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); |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 529 | return static_pointer_cast<TriggerStage>( |
| 530 | stage->get_shared_pointer(this)); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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) |
Martin Ling | 7649683 | 2014-07-24 03:12:40 +0100 | [diff] [blame] | 553 | result.push_back(static_pointer_cast<TriggerMatch>( |
| 554 | match->get_shared_pointer(this))); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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]; |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 604 | auto packet = shared_ptr<Packet>(new Packet(device, pkt), Packet::Deleter()); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 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 | |
| 732 | void Session::append(shared_ptr<Device> device, shared_ptr<Packet> packet) |
| 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 | |
| 759 | check(sr_config_get(device->structure->driver, |
| 760 | device->structure, NULL, SR_CONF_SAMPLERATE, &samplerate)); |
| 761 | |
| 762 | save_samplerate = g_variant_get_uint64(samplerate); |
| 763 | |
| 764 | g_variant_unref(samplerate); |
| 765 | } |
| 766 | |
| 767 | if (!save_initialized) |
| 768 | { |
| 769 | vector<shared_ptr<Channel>> save_channels; |
| 770 | |
| 771 | for (auto channel : device->get_channels()) |
| 772 | if (channel->structure->enabled && |
| 773 | channel->structure->type == SR_CHANNEL_LOGIC) |
| 774 | save_channels.push_back(channel); |
| 775 | |
| 776 | auto channels = g_new(char *, save_channels.size()); |
| 777 | |
| 778 | int i = 0; |
| 779 | for (auto channel : save_channels) |
| 780 | channels[i++] = channel->structure->name; |
| 781 | channels[i] = NULL; |
| 782 | |
| 783 | int ret = sr_session_save_init(structure, save_filename.c_str(), |
| 784 | save_samplerate, channels); |
| 785 | |
| 786 | g_free(channels); |
| 787 | |
| 788 | if (ret != SR_OK) |
| 789 | throw Error(ret); |
| 790 | |
| 791 | save_initialized = true; |
| 792 | } |
| 793 | |
| 794 | auto logic = (const struct sr_datafeed_logic *) |
| 795 | packet->structure->payload; |
| 796 | |
| 797 | check(sr_session_append(structure, save_filename.c_str(), |
| 798 | (uint8_t *) logic->data, logic->unitsize, |
| 799 | logic->length / logic->unitsize)); |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | static void datafeed_callback(const struct sr_dev_inst *sdi, |
| 805 | const struct sr_datafeed_packet *pkt, void *cb_data) |
| 806 | { |
| 807 | auto callback = static_cast<DatafeedCallbackData *>(cb_data); |
| 808 | callback->run(sdi, pkt); |
| 809 | } |
| 810 | |
| 811 | void Session::add_datafeed_callback(DatafeedCallbackFunction callback) |
| 812 | { |
| 813 | auto cb_data = new DatafeedCallbackData(this, callback); |
| 814 | check(sr_session_datafeed_callback_add(structure, datafeed_callback, cb_data)); |
| 815 | datafeed_callbacks.push_back(cb_data); |
| 816 | } |
| 817 | |
| 818 | void Session::remove_datafeed_callbacks(void) |
| 819 | { |
| 820 | check(sr_session_datafeed_callback_remove_all(structure)); |
| 821 | for (auto callback : datafeed_callbacks) |
| 822 | delete callback; |
| 823 | datafeed_callbacks.clear(); |
| 824 | } |
| 825 | |
| 826 | static int source_callback(int fd, int revents, void *cb_data) |
| 827 | { |
| 828 | (void) fd; |
| 829 | auto callback = (SourceCallbackData *) cb_data; |
| 830 | return callback->run(revents); |
| 831 | } |
| 832 | |
| 833 | void Session::add_source(shared_ptr<EventSource> source) |
| 834 | { |
| 835 | if (source_callbacks.count(source) == 1) |
| 836 | throw Error(SR_ERR_ARG); |
| 837 | |
| 838 | auto cb_data = new SourceCallbackData(source); |
| 839 | |
| 840 | switch (source->type) |
| 841 | { |
| 842 | case EventSource::SOURCE_FD: |
| 843 | check(sr_session_source_add(structure, source->fd, source->events, |
| 844 | source->timeout, source_callback, cb_data)); |
| 845 | break; |
| 846 | case EventSource::SOURCE_POLLFD: |
| 847 | check(sr_session_source_add_pollfd(structure, |
| 848 | source->pollfd.gobj(), source->timeout, source_callback, |
| 849 | cb_data)); |
| 850 | break; |
| 851 | case EventSource::SOURCE_IOCHANNEL: |
| 852 | check(sr_session_source_add_channel(structure, |
| 853 | source->channel->gobj(), source->events, source->timeout, |
| 854 | source_callback, cb_data)); |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | source_callbacks[source] = cb_data; |
| 859 | } |
| 860 | |
| 861 | void Session::remove_source(shared_ptr<EventSource> source) |
| 862 | { |
| 863 | if (source_callbacks.count(source) == 0) |
| 864 | throw Error(SR_ERR_ARG); |
| 865 | |
| 866 | switch (source->type) |
| 867 | { |
| 868 | case EventSource::SOURCE_FD: |
| 869 | check(sr_session_source_remove(structure, source->fd)); |
| 870 | break; |
| 871 | case EventSource::SOURCE_POLLFD: |
| 872 | check(sr_session_source_remove_pollfd(structure, |
| 873 | source->pollfd.gobj())); |
| 874 | break; |
| 875 | case EventSource::SOURCE_IOCHANNEL: |
| 876 | check(sr_session_source_remove_channel(structure, |
| 877 | source->channel->gobj())); |
| 878 | break; |
| 879 | } |
| 880 | |
| 881 | delete source_callbacks[source]; |
| 882 | |
| 883 | source_callbacks.erase(source); |
| 884 | } |
| 885 | |
Martin Ling | 6fa0eb8 | 2014-07-19 22:45:35 +0100 | [diff] [blame] | 886 | shared_ptr<Trigger> Session::get_trigger() |
| 887 | { |
| 888 | return trigger; |
| 889 | } |
| 890 | |
| 891 | void Session::set_trigger(shared_ptr<Trigger> trigger) |
| 892 | { |
| 893 | check(sr_session_trigger_set(structure, trigger->structure)); |
| 894 | this->trigger = trigger; |
| 895 | } |
| 896 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 897 | Packet::Packet(shared_ptr<Device> device, |
| 898 | const struct sr_datafeed_packet *structure) : |
| 899 | structure(structure), |
| 900 | device(device) |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 901 | { |
| 902 | switch (structure->type) |
| 903 | { |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 904 | case SR_DF_HEADER: |
| 905 | payload = new Header( |
| 906 | static_cast<const struct sr_datafeed_header *>( |
| 907 | structure->payload)); |
| 908 | break; |
| 909 | case SR_DF_META: |
| 910 | payload = new Meta( |
| 911 | static_cast<const struct sr_datafeed_meta *>( |
| 912 | structure->payload)); |
| 913 | break; |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 914 | case SR_DF_LOGIC: |
| 915 | payload = new Logic( |
| 916 | static_cast<const struct sr_datafeed_logic *>( |
| 917 | structure->payload)); |
| 918 | break; |
| 919 | case SR_DF_ANALOG: |
| 920 | payload = new Analog( |
| 921 | static_cast<const struct sr_datafeed_analog *>( |
| 922 | structure->payload)); |
| 923 | break; |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | Packet::~Packet() |
| 928 | { |
Martin Ling | 1797a88 | 2014-07-24 18:10:33 +0100 | [diff] [blame] | 929 | if (payload) |
| 930 | delete payload; |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Martin Ling | 90ba83f | 2014-07-20 01:38:31 +0100 | [diff] [blame] | 933 | const PacketType *Packet::get_type() |
| 934 | { |
| 935 | return PacketType::get(structure->type); |
| 936 | } |
| 937 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 938 | shared_ptr<PacketPayload> Packet::get_payload() |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 939 | { |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 940 | return payload->get_shared_pointer(this); |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | PacketPayload::PacketPayload() |
| 944 | { |
| 945 | } |
| 946 | |
| 947 | PacketPayload::~PacketPayload() |
| 948 | { |
| 949 | } |
| 950 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 951 | Header::Header(const struct sr_datafeed_header *structure) : |
| 952 | PacketPayload(), |
| 953 | StructureWrapper<Packet, const struct sr_datafeed_header>(structure) |
| 954 | { |
| 955 | } |
| 956 | |
| 957 | Header::~Header() |
| 958 | { |
| 959 | } |
| 960 | |
| 961 | int Header::get_feed_version() |
| 962 | { |
| 963 | return structure->feed_version; |
| 964 | } |
| 965 | |
| 966 | Glib::TimeVal Header::get_start_time() |
| 967 | { |
| 968 | return Glib::TimeVal( |
| 969 | structure->starttime.tv_sec, |
| 970 | structure->starttime.tv_usec); |
| 971 | } |
| 972 | |
| 973 | Meta::Meta(const struct sr_datafeed_meta *structure) : |
| 974 | PacketPayload(), |
| 975 | StructureWrapper<Packet, const struct sr_datafeed_meta>(structure) |
| 976 | { |
| 977 | } |
| 978 | |
| 979 | Meta::~Meta() |
| 980 | { |
| 981 | } |
| 982 | |
| 983 | map<const ConfigKey *, Glib::VariantBase> Meta::get_config() |
| 984 | { |
| 985 | map<const ConfigKey *, Glib::VariantBase> result; |
| 986 | for (auto l = structure->config; l; l = l->next) |
| 987 | { |
| 988 | auto config = (struct sr_config *) l->data; |
| 989 | result[ConfigKey::get(config->key)] = Glib::VariantBase(config->data); |
| 990 | } |
| 991 | return result; |
| 992 | } |
| 993 | |
| 994 | Logic::Logic(const struct sr_datafeed_logic *structure) : |
| 995 | PacketPayload(), |
| 996 | StructureWrapper<Packet, const struct sr_datafeed_logic>(structure) |
| 997 | { |
| 998 | } |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 999 | |
| 1000 | Logic::~Logic() |
| 1001 | { |
| 1002 | } |
| 1003 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 1004 | void *Logic::get_data_pointer() |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 1005 | { |
| 1006 | return structure->data; |
| 1007 | } |
| 1008 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 1009 | size_t Logic::get_data_length() |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 1010 | { |
| 1011 | return structure->length; |
| 1012 | } |
| 1013 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 1014 | unsigned int Logic::get_unit_size() |
| 1015 | { |
| 1016 | return structure->unitsize; |
| 1017 | } |
| 1018 | |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 1019 | Analog::Analog(const struct sr_datafeed_analog *structure) : |
| 1020 | PacketPayload(), |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 1021 | StructureWrapper<Packet, const struct sr_datafeed_analog>(structure) |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 1022 | { |
| 1023 | } |
| 1024 | |
| 1025 | Analog::~Analog() |
| 1026 | { |
| 1027 | } |
| 1028 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 1029 | float *Analog::get_data_pointer() |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 1030 | { |
| 1031 | return structure->data; |
| 1032 | } |
| 1033 | |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 1034 | unsigned int Analog::get_num_samples() |
| 1035 | { |
| 1036 | return structure->num_samples; |
| 1037 | } |
| 1038 | |
Martin Ling | 2928f47 | 2014-07-20 02:20:13 +0100 | [diff] [blame] | 1039 | vector<shared_ptr<Channel>> Analog::get_channels() |
| 1040 | { |
| 1041 | vector<shared_ptr<Channel>> result; |
| 1042 | for (auto l = structure->channels; l; l = l->next) |
| 1043 | result.push_back(parent->device->get_channel( |
| 1044 | (struct sr_channel *)l->data)); |
| 1045 | return result; |
| 1046 | } |
| 1047 | |
Martin Ling | c23c865 | 2014-07-17 18:39:49 +0100 | [diff] [blame] | 1048 | const Quantity *Analog::get_mq() |
| 1049 | { |
| 1050 | return Quantity::get(structure->mq); |
| 1051 | } |
| 1052 | |
| 1053 | const Unit *Analog::get_unit() |
| 1054 | { |
| 1055 | return Unit::get(structure->unit); |
| 1056 | } |
| 1057 | |
| 1058 | vector<const QuantityFlag *> Analog::get_mq_flags() |
| 1059 | { |
| 1060 | return QuantityFlag::flags_from_mask(structure->mqflags); |
| 1061 | } |
| 1062 | |
| 1063 | InputFormat::InputFormat(struct sr_input_format *structure) : |
| 1064 | StructureWrapper<Context, struct sr_input_format>(structure) |
| 1065 | { |
| 1066 | } |
| 1067 | |
| 1068 | InputFormat::~InputFormat() |
| 1069 | { |
| 1070 | } |
| 1071 | |
| 1072 | string InputFormat::get_name() |
| 1073 | { |
| 1074 | return valid_string(structure->id); |
| 1075 | } |
| 1076 | |
| 1077 | string InputFormat::get_description() |
| 1078 | { |
| 1079 | return valid_string(structure->description); |
| 1080 | } |
| 1081 | |
| 1082 | bool InputFormat::format_match(string filename) |
| 1083 | { |
| 1084 | return structure->format_match(filename.c_str()); |
| 1085 | } |
| 1086 | |
| 1087 | shared_ptr<InputFileDevice> InputFormat::open_file(string filename, |
| 1088 | map<string, string> options) |
| 1089 | { |
| 1090 | auto input = g_new(struct sr_input, 1); |
| 1091 | input->param = map_to_hash(options); |
| 1092 | |
| 1093 | /** Run initialisation. */ |
| 1094 | check(structure->init(input, filename.c_str())); |
| 1095 | |
| 1096 | /** Create virtual device. */ |
| 1097 | return shared_ptr<InputFileDevice>(new InputFileDevice( |
| 1098 | static_pointer_cast<InputFormat>(shared_from_this()), input, filename), |
| 1099 | InputFileDevice::Deleter()); |
| 1100 | } |
| 1101 | |
| 1102 | InputFileDevice::InputFileDevice(shared_ptr<InputFormat> format, |
| 1103 | struct sr_input *input, string filename) : |
| 1104 | Device(input->sdi), |
| 1105 | input(input), |
| 1106 | format(format), |
| 1107 | filename(filename) |
| 1108 | { |
| 1109 | } |
| 1110 | |
| 1111 | InputFileDevice::~InputFileDevice() |
| 1112 | { |
| 1113 | g_hash_table_unref(input->param); |
| 1114 | g_free(input); |
| 1115 | } |
| 1116 | |
| 1117 | void InputFileDevice::load() |
| 1118 | { |
| 1119 | check(format->structure->loadfile(input, filename.c_str())); |
| 1120 | } |
| 1121 | |
| 1122 | OutputFormat::OutputFormat(struct sr_output_format *structure) : |
| 1123 | StructureWrapper<Context, struct sr_output_format>(structure) |
| 1124 | { |
| 1125 | } |
| 1126 | |
| 1127 | OutputFormat::~OutputFormat() |
| 1128 | { |
| 1129 | } |
| 1130 | |
| 1131 | string OutputFormat::get_name() |
| 1132 | { |
| 1133 | return valid_string(structure->id); |
| 1134 | } |
| 1135 | |
| 1136 | string OutputFormat::get_description() |
| 1137 | { |
| 1138 | return valid_string(structure->description); |
| 1139 | } |
| 1140 | |
| 1141 | shared_ptr<Output> OutputFormat::create_output( |
| 1142 | shared_ptr<Device> device, map<string, string> options) |
| 1143 | { |
| 1144 | return shared_ptr<Output>( |
| 1145 | new Output( |
| 1146 | static_pointer_cast<OutputFormat>(shared_from_this()), |
| 1147 | device, options), |
| 1148 | Output::Deleter()); |
| 1149 | } |
| 1150 | |
| 1151 | Output::Output(shared_ptr<OutputFormat> format, |
| 1152 | shared_ptr<Device> device, map<string, string> options) : |
| 1153 | structure(sr_output_new(format->structure, |
| 1154 | map_to_hash(options), device->structure)), |
| 1155 | format(format), device(device), options(options) |
| 1156 | { |
| 1157 | } |
| 1158 | |
| 1159 | Output::~Output() |
| 1160 | { |
| 1161 | g_hash_table_unref(structure->params); |
| 1162 | check(sr_output_free(structure)); |
| 1163 | } |
| 1164 | |
| 1165 | string Output::receive(shared_ptr<Packet> packet) |
| 1166 | { |
| 1167 | GString *out; |
| 1168 | check(sr_output_send(structure, packet->structure, &out)); |
| 1169 | if (out) |
| 1170 | { |
| 1171 | auto result = string(out->str, out->str + out->len); |
| 1172 | g_string_free(out, true); |
| 1173 | return result; |
| 1174 | } |
| 1175 | else |
| 1176 | { |
| 1177 | return string(); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | #include "enums.cpp" |
| 1182 | |
| 1183 | } |