* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "libsigrok/libsigrok.hpp"
+#include <libsigrokcxx/libsigrokcxx.hpp>
#include <sstream>
#include <cmath>
{
check(sr_init(&_structure));
- struct sr_dev_driver **driver_list = sr_driver_list();
+ struct sr_dev_driver **driver_list = sr_driver_list(_structure);
if (driver_list)
for (int i = 0; driver_list[i]; i++)
_drivers[driver_list[i]->name] =
new Session(shared_from_this()), Session::Deleter());
}
+shared_ptr<UserDevice> Context::create_user_device(
+ string vendor, string model, string version)
+{
+ return shared_ptr<UserDevice>(
+ new UserDevice(vendor, model, version), UserDevice::Deleter());
+}
+
+shared_ptr<Packet> Context::create_header_packet(Glib::TimeVal start_time)
+{
+ auto header = g_new(struct sr_datafeed_header, 1);
+ header->feed_version = 1;
+ header->starttime.tv_sec = start_time.tv_sec;
+ header->starttime.tv_usec = start_time.tv_usec;
+ auto packet = g_new(struct sr_datafeed_packet, 1);
+ packet->type = SR_DF_HEADER;
+ packet->payload = header;
+ return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+}
+
+shared_ptr<Packet> Context::create_meta_packet(
+ map<const ConfigKey *, Glib::VariantBase> config)
+{
+ auto meta = g_new0(struct sr_datafeed_meta, 1);
+ for (auto input : config)
+ {
+ auto key = input.first;
+ auto value = input.second;
+ auto output = g_new(struct sr_config, 1);
+ output->key = key->id();
+ output->data = value.gobj();
+ g_variant_ref(output->data);
+ meta->config = g_slist_append(meta->config, output);
+ }
+ auto packet = g_new(struct sr_datafeed_packet, 1);
+ packet->type = SR_DF_META;
+ packet->payload = meta;
+ return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+}
+
+shared_ptr<Packet> Context::create_logic_packet(
+ void *data_pointer, size_t data_length, unsigned int unit_size)
+{
+ auto logic = g_new(struct sr_datafeed_logic, 1);
+ logic->length = data_length;
+ logic->unitsize = unit_size;
+ logic->data = data_pointer;
+ auto packet = g_new(struct sr_datafeed_packet, 1);
+ packet->type = SR_DF_LOGIC;
+ packet->payload = logic;
+ return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+}
+
+shared_ptr<Packet> Context::create_analog_packet(
+ vector<shared_ptr<Channel> > channels,
+ float *data_pointer, unsigned int num_samples, const Quantity *mq,
+ const Unit *unit, vector<const QuantityFlag *> mqflags)
+{
+ auto analog = g_new0(struct sr_datafeed_analog, 1);
+ for (auto channel : channels)
+ analog->channels = g_slist_append(analog->channels, channel->_structure);
+ analog->num_samples = num_samples;
+ analog->mq = mq->id();
+ analog->unit = unit->id();
+ analog->mqflags = QuantityFlag::mask_from_flags(mqflags);
+ analog->data = data_pointer;
+ auto packet = g_new(struct sr_datafeed_packet, 1);
+ packet->type = SR_DF_ANALOG;
+ packet->payload = analog;
+ return shared_ptr<Packet>(new Packet(nullptr, packet), Packet::Deleter());
+}
+
shared_ptr<Session> Context::load_session(string filename)
{
return shared_ptr<Session>(
new Input(shared_from_this(), input), Input::Deleter());
}
+map<string, string> Context::serials(shared_ptr<Driver> driver)
+{
+ GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL);
+ map<string, string> serials;
+
+ for (GSList *serial = serial_list; serial; serial = serial->next) {
+ struct sr_serial_port *port = (sr_serial_port *) serial->data;
+ serials[string(port->name)] = string(port->description);
+ }
+
+ g_slist_free_full(serial_list, (GDestroyNotify)sr_serial_free);
+ return serials;
+}
+
Driver::Driver(struct sr_dev_driver *structure) :
ParentOwned(structure),
Configurable(structure, NULL, NULL),
for (gsize i = 0; i < num_opts; i++)
{
- if ((opts[i] & SR_CONF_MASK) == key->id())
+ if ((opts[i] & SR_CONF_MASK) == (uint32_t) key->id())
{
g_variant_unref(gvar_opts);
return true;
}
Device::Device(struct sr_dev_inst *structure) :
- Configurable(structure->driver, structure, NULL),
+ Configurable(sr_dev_inst_driver_get(structure), structure, NULL),
_structure(structure)
{
- for (GSList *entry = structure->channels; entry; entry = entry->next)
+ for (GSList *entry = sr_dev_inst_channels_get(structure); entry; entry = entry->next)
{
auto channel = (struct sr_channel *) entry->data;
_channels[channel] = new Channel(channel);
}
- for (GSList *entry = structure->channel_groups; entry; entry = entry->next)
+ for (GSList *entry = sr_dev_inst_channel_groups_get(structure); entry; entry = entry->next)
{
auto group = (struct sr_channel_group *) entry->data;
_channel_groups[group->name] = new ChannelGroup(this, group);
delete entry.second;
}
-string Device::description()
-{
- ostringstream s;
-
- vector<string> parts =
- {vendor(), model(), version(), serial_number()};
-
- for (size_t i = 0; i < parts.size(); i++)
- {
- if (parts[i].length() > 0)
- {
- if (i != 0)
- s << " ";
- s << parts[i];
- }
- }
-
- if (serial_number().length() == 0 && connection_id().length() > 0)
- s << " " << connection_id();
-
- return s.str();
-}
-
string Device::vendor()
{
- return valid_string(_structure->vendor);
+ return valid_string(sr_dev_inst_vendor_get(_structure));
}
string Device::model()
{
- return valid_string(_structure->model);
+ return valid_string(sr_dev_inst_model_get(_structure));
}
string Device::version()
{
- return valid_string(_structure->version);
+ return valid_string(sr_dev_inst_version_get(_structure));
}
string Device::serial_number()
{
- return valid_string(_structure->serial_num);
+ return valid_string(sr_dev_inst_sernum_get(_structure));
}
string Device::connection_id()
{
- return valid_string(_structure->connection_id);
+ return valid_string(sr_dev_inst_connid_get(_structure));
}
vector<shared_ptr<Channel>> Device::channels()
{
vector<shared_ptr<Channel>> result;
- for (auto channel = _structure->channels; channel; channel = channel->next)
+ for (auto channel = sr_dev_inst_channels_get(_structure); channel; channel = channel->next)
result.push_back(
_channels[(struct sr_channel *) channel->data]->get_shared_pointer(
get_shared_from_this()));
return _driver;
}
+UserDevice::UserDevice(string vendor, string model, string version) :
+ UserOwned(sr_dev_inst_user_new(
+ vendor.c_str(), model.c_str(), version.c_str())),
+ Device(UserOwned::_structure)
+{
+}
+
+UserDevice::~UserDevice()
+{
+}
+
+shared_ptr<Device> UserDevice::get_shared_from_this()
+{
+ return static_pointer_cast<Device>(shared_from_this());
+}
+
+shared_ptr<Channel> UserDevice::add_channel(unsigned int index,
+ const ChannelType *type, string name)
+{
+ check(sr_dev_inst_channel_add(Device::_structure,
+ index, type->id(), name.c_str()));
+ struct sr_channel *structure = (struct sr_channel *)
+ g_slist_last(sr_dev_inst_channels_get(Device::_structure))->data;
+ Channel *channel = new Channel(structure);
+ _channels[structure] = channel;
+ return get_channel(structure);
+}
+
Channel::Channel(struct sr_channel *structure) :
ParentOwned(structure),
_type(ChannelType::get(_structure->type))
void Channel::set_name(string name)
{
- check(sr_dev_channel_name_set(_parent->_structure,
- _structure->index, name.c_str()));
+ check(sr_dev_channel_name_set(_structure, name.c_str()));
}
const ChannelType *Channel::type()
void Channel::set_enabled(bool value)
{
- check(sr_dev_channel_enable(_parent->_structure, _structure->index, value));
+ check(sr_dev_channel_enable(_structure, value));
}
unsigned int Channel::index()
ChannelGroup::ChannelGroup(Device *device,
struct sr_channel_group *structure) :
ParentOwned(structure),
- Configurable(device->_structure->driver, device->_structure, structure)
+ Configurable(sr_dev_inst_driver_get(device->_structure), device->_structure, structure)
{
for (GSList *entry = structure->channels; entry; entry = entry->next)
_channels.push_back(device->_channels[(struct sr_channel *)entry->data]);
{
}
-string SessionDevice::description()
-{
- return _parent->_filename;
-}
-
shared_ptr<Device> SessionDevice::get_shared_from_this()
{
return static_pointer_cast<Device>(shared_from_this());
_context(context),
_saving(false)
{
- check(sr_session_new(&_structure));
+ check(sr_session_new(context->_structure, &_structure));
_context->_session = this;
}
_filename(filename),
_saving(false)
{
- check(sr_session_load(filename.c_str(), &_structure));
+ check(sr_session_load(context->_structure, filename.c_str(), &_structure));
GSList *dev_list;
check(sr_session_dev_list(_structure, &dev_list));
for (GSList *dev = dev_list; dev; dev = dev->next)
void Session::remove_devices()
{
- for (auto entry : _owned_devices)
- {
- // We own this device. Make sure it's not referenced.
- auto device = entry.second;
- auto ptr = device->get_shared_pointer(this);
- if (ptr.use_count() > 1)
- throw Error(SR_ERR_BUG);
- delete device;
- }
- _owned_devices.clear();
_other_devices.clear();
check(sr_session_dev_remove_all(_structure));
}
{
GVariant *samplerate;
- check(sr_config_get(packet->_device->_structure->driver,
+ check(sr_config_get(sr_dev_inst_driver_get(packet->_device->_structure),
packet->_device->_structure, NULL, SR_CONF_SAMPLERATE,
&samplerate));
void Session::set_trigger(shared_ptr<Trigger> trigger)
{
- check(sr_session_trigger_set(_structure, trigger->_structure));
+ if (!trigger)
+ // Set NULL trigger, i.e. remove any trigger from the session.
+ check(sr_session_trigger_set(_structure, NULL));
+ else
+ check(sr_session_trigger_set(_structure, trigger->_structure));
_trigger = trigger;
}
+string Session::filename()
+{
+ return _filename;
+}
+
+shared_ptr<Context> Session::context()
+{
+ return _context;
+}
+
Packet::Packet(shared_ptr<Device> device,
const struct sr_datafeed_packet *structure) :
UserOwned(structure),
return valid_string(sr_input_description_get(_structure));
}
+vector<string> InputFormat::extensions()
+{
+ vector<string> exts;
+ for (const char *const *e = sr_input_extensions_get(_structure);
+ e && *e; e++)
+ exts.push_back(*e);
+ return exts;
+}
+
map<string, shared_ptr<Option>> InputFormat::options()
{
const struct sr_option **options = sr_input_options_get(_structure);
- auto option_array = shared_ptr<const struct sr_option *>(
- options, sr_input_options_free);
map<string, shared_ptr<Option>> result;
- for (int i = 0; options[i]; i++)
- result[options[i]->id] = shared_ptr<Option>(
- new Option(options[i], option_array), Option::Deleter());
+ if (options)
+ {
+ auto option_array = shared_ptr<const struct sr_option *>(
+ options, sr_input_options_free);
+ for (int i = 0; options[i]; i++)
+ result[options[i]->id] = shared_ptr<Option>(
+ new Option(options[i], option_array), Option::Deleter());
+ }
return result;
}
return _device->get_shared_pointer(shared_from_this());
}
-void Input::send(string data)
+void Input::send(void *data, size_t length)
{
- auto gstr = g_string_new(data.c_str());
+ auto gstr = g_string_new_len((gchar *)data, length);
auto ret = sr_input_send(_structure, gstr);
g_string_free(gstr, false);
check(ret);
{
}
-string InputDevice::description()
-{
- return "<input data>";
-}
-
shared_ptr<Device> InputDevice::get_shared_from_this()
{
return static_pointer_cast<Device>(shared_from_this());
return valid_string(sr_output_description_get(_structure));
}
+vector<string> OutputFormat::extensions()
+{
+ vector<string> exts;
+ for (const char *const *e = sr_output_extensions_get(_structure);
+ e && *e; e++)
+ exts.push_back(*e);
+ return exts;
+}
+
map<string, shared_ptr<Option>> OutputFormat::options()
{
const struct sr_option **options = sr_output_options_get(_structure);
- auto option_array = shared_ptr<const struct sr_option *>(
- options, sr_output_options_free);
map<string, shared_ptr<Option>> result;
- for (int i = 0; options[i]; i++)
- result[options[i]->id] = shared_ptr<Option>(
- new Option(options[i], option_array), Option::Deleter());
+ if (options)
+ {
+ auto option_array = shared_ptr<const struct sr_option *>(
+ options, sr_output_options_free);
+ for (int i = 0; options[i]; i++)
+ result[options[i]->id] = shared_ptr<Option>(
+ new Option(options[i], option_array), Option::Deleter());
+ }
return result;
}
Output::Deleter());
}
+shared_ptr<Output> OutputFormat::create_output(string filename,
+ shared_ptr<Device> device, map<string, Glib::VariantBase> options)
+{
+ return shared_ptr<Output>(
+ new Output(filename, shared_from_this(), device, options),
+ Output::Deleter());
+}
+
+bool OutputFormat::test_flag(const OutputFlag *flag)
+{
+ return sr_output_test_flag(_structure, flag->id());
+}
+
Output::Output(shared_ptr<OutputFormat> format,
shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
UserOwned(sr_output_new(format->_structure,
- map_to_hash_variant(options), device->_structure)),
+ map_to_hash_variant(options), device->_structure, NULL)),
+ _format(format),
+ _device(device),
+ _options(options)
+{
+}
+
+Output::Output(string filename, shared_ptr<OutputFormat> format,
+ shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
+ UserOwned(sr_output_new(format->_structure,
+ map_to_hash_variant(options), device->_structure, filename.c_str())),
_format(format),
_device(device),
_options(options)
}
}
-#include "enums.cpp"
+#include <enums.cpp>
}