2013-04-17 13:30:31 +00:00
|
|
|
##
|
2013-04-23 20:24:30 +00:00
|
|
|
## This file is part of the libsigrok project.
|
2013-04-17 13:30:31 +00:00
|
|
|
##
|
|
|
|
## Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
|
|
|
|
##
|
|
|
|
## This program is free software: you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published by
|
|
|
|
## the Free Software Foundation, either version 3 of the License, or
|
|
|
|
## (at your option) any later version.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
|
|
|
from functools import partial
|
|
|
|
from fractions import Fraction
|
2013-12-17 16:25:13 +00:00
|
|
|
from collections import OrderedDict
|
2013-04-17 14:37:58 +00:00
|
|
|
from .lowlevel import *
|
|
|
|
from . import lowlevel
|
2013-04-17 13:30:31 +00:00
|
|
|
import itertools
|
|
|
|
|
2013-04-19 15:57:13 +00:00
|
|
|
__all__ = ['Error', 'Context', 'Driver', 'Device', 'Session', 'Packet', 'Log',
|
2013-04-21 14:46:48 +00:00
|
|
|
'LogLevel', 'PacketType', 'Quantity', 'Unit', 'QuantityFlag', 'ConfigKey',
|
2014-03-14 20:09:37 +00:00
|
|
|
'ProbeType', 'Probe', 'ChannelGroup', 'InputFormat', 'OutputFormat',
|
2013-12-17 00:42:33 +00:00
|
|
|
'InputFile', 'Output']
|
2013-04-17 13:30:31 +00:00
|
|
|
|
|
|
|
class Error(Exception):
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return sr_strerror(self.args[0])
|
|
|
|
|
|
|
|
def check(result):
|
|
|
|
if result != SR_OK:
|
|
|
|
raise Error(result)
|
|
|
|
|
|
|
|
def gvariant_to_python(value):
|
|
|
|
type_string = g_variant_get_type_string(value)
|
|
|
|
if type_string == 't':
|
|
|
|
return g_variant_get_uint64(value)
|
|
|
|
if type_string == 'b':
|
|
|
|
return g_variant_get_bool(value)
|
|
|
|
if type_string == 'd':
|
|
|
|
return g_variant_get_double(value)
|
|
|
|
if type_string == 's':
|
|
|
|
return g_variant_get_string(value, None)
|
|
|
|
if type_string == '(tt)':
|
|
|
|
return Fraction(
|
|
|
|
g_variant_get_uint64(g_variant_get_child_value(value, 0)),
|
|
|
|
g_variant_get_uint64(g_variant_get_child_value(value, 1)))
|
2013-04-17 14:37:58 +00:00
|
|
|
raise NotImplementedError(
|
2013-04-17 13:30:31 +00:00
|
|
|
"Can't convert GVariant type '%s' to a Python type." % type_string)
|
|
|
|
|
|
|
|
def python_to_gvariant(value):
|
|
|
|
if isinstance(value, int):
|
|
|
|
return g_variant_new_uint64(value)
|
|
|
|
if isinstance(value, bool):
|
|
|
|
return g_variant_new_boolean(value)
|
|
|
|
if isinstance(value, float):
|
|
|
|
return g_variant_new_double(value)
|
|
|
|
if isinstance(value, str):
|
|
|
|
return g_variant_new_string(value)
|
|
|
|
if isinstance(value, Fraction):
|
|
|
|
array = new_gvariant_ptr_array(2)
|
2013-04-21 18:52:09 +00:00
|
|
|
gvariant_ptr_array_setitem(array, 0,
|
|
|
|
g_variant_new_uint64(value.numerator))
|
|
|
|
gvariant_ptr_array_setitem(array, 1,
|
|
|
|
g_variant_new_uint64(value.denominator))
|
2013-04-17 13:30:31 +00:00
|
|
|
result = g_variant_new_tuple(array, 2)
|
|
|
|
delete_gvariant_ptr_array(array)
|
|
|
|
return result
|
2013-04-17 14:37:58 +00:00
|
|
|
raise NotImplementedError(
|
2013-04-17 13:30:31 +00:00
|
|
|
"Can't convert Python '%s' to a GVariant." % type(value))
|
|
|
|
|
|
|
|
def callback_wrapper(session, callback, device_ptr, packet_ptr):
|
|
|
|
device = session.context._devices[int(device_ptr.this)]
|
|
|
|
packet = Packet(session, packet_ptr)
|
|
|
|
callback(device, packet)
|
|
|
|
|
|
|
|
class Context(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
context_ptr_ptr = new_sr_context_ptr_ptr()
|
|
|
|
check(sr_init(context_ptr_ptr))
|
|
|
|
self.struct = sr_context_ptr_ptr_value(context_ptr_ptr)
|
|
|
|
self._drivers = None
|
|
|
|
self._devices = {}
|
2013-12-16 01:21:39 +00:00
|
|
|
self._input_formats = None
|
|
|
|
self._output_formats = None
|
2013-04-17 13:30:31 +00:00
|
|
|
self.session = None
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
sr_exit(self.struct)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def drivers(self):
|
|
|
|
if not self._drivers:
|
|
|
|
self._drivers = {}
|
|
|
|
driver_list = sr_driver_list()
|
|
|
|
for i in itertools.count():
|
|
|
|
driver_ptr = sr_dev_driver_ptr_array_getitem(driver_list, i)
|
|
|
|
if driver_ptr:
|
|
|
|
self._drivers[driver_ptr.name] = Driver(self, driver_ptr)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return self._drivers
|
|
|
|
|
2013-12-16 01:21:39 +00:00
|
|
|
@property
|
|
|
|
def input_formats(self):
|
|
|
|
if not self._input_formats:
|
2013-12-17 16:25:13 +00:00
|
|
|
self._input_formats = OrderedDict()
|
2013-12-16 01:21:39 +00:00
|
|
|
input_list = sr_input_list()
|
|
|
|
for i in itertools.count():
|
|
|
|
input_ptr = sr_input_format_ptr_array_getitem(input_list, i)
|
|
|
|
if input_ptr:
|
|
|
|
self._input_formats[input_ptr.id] = InputFormat(self, input_ptr)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return self._input_formats
|
|
|
|
|
|
|
|
@property
|
|
|
|
def output_formats(self):
|
|
|
|
if not self._output_formats:
|
|
|
|
self._output_formats = {}
|
|
|
|
output_list = sr_output_list()
|
|
|
|
for i in itertools.count():
|
|
|
|
output_ptr = sr_output_format_ptr_array_getitem(output_list, i)
|
|
|
|
if output_ptr:
|
|
|
|
self._output_formats[output_ptr.id] = OutputFormat(self, output_ptr)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return self._output_formats
|
|
|
|
|
2013-04-17 13:30:31 +00:00
|
|
|
class Driver(object):
|
|
|
|
|
|
|
|
def __init__(self, context, struct):
|
|
|
|
self.context = context
|
|
|
|
self.struct = struct
|
|
|
|
self._initialized = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.struct.name
|
|
|
|
|
2013-04-24 16:48:04 +00:00
|
|
|
def scan(self, **kwargs):
|
2013-04-17 13:30:31 +00:00
|
|
|
if not self._initialized:
|
|
|
|
check(sr_driver_init(self.context.struct, self.struct))
|
|
|
|
self._initialized = True
|
2013-04-24 16:48:04 +00:00
|
|
|
options = []
|
|
|
|
for name, value in kwargs.items():
|
2013-12-17 13:11:43 +00:00
|
|
|
key = getattr(ConfigKey, name)
|
2013-04-24 16:48:04 +00:00
|
|
|
src = sr_config()
|
|
|
|
src.key = key.id
|
2013-04-24 21:43:56 +00:00
|
|
|
src.data = python_to_gvariant(value)
|
2013-04-24 16:48:04 +00:00
|
|
|
options.append(src.this)
|
|
|
|
option_list = python_to_gslist(options)
|
|
|
|
device_list = sr_driver_scan(self.struct, option_list)
|
|
|
|
g_slist_free(option_list)
|
2013-12-17 16:10:08 +00:00
|
|
|
devices = [HardwareDevice(self, gpointer_to_sr_dev_inst_ptr(ptr))
|
2013-04-24 13:47:07 +00:00
|
|
|
for ptr in gslist_to_python(device_list)]
|
2013-04-17 13:30:31 +00:00
|
|
|
g_slist_free(device_list)
|
|
|
|
return devices
|
|
|
|
|
|
|
|
class Device(object):
|
|
|
|
|
2013-12-17 16:10:08 +00:00
|
|
|
def __new__(cls, struct, context):
|
2013-04-17 13:30:31 +00:00
|
|
|
address = int(struct.this)
|
2013-12-17 16:10:08 +00:00
|
|
|
if address not in context._devices:
|
|
|
|
device = super(Device, cls).__new__(cls)
|
|
|
|
device.struct = struct
|
|
|
|
device.context = context
|
|
|
|
device._probes = None
|
2014-03-14 20:09:37 +00:00
|
|
|
device._channel_groups = None
|
2013-12-17 16:10:08 +00:00
|
|
|
context._devices[address] = device
|
|
|
|
return context._devices[address]
|
2013-04-17 13:30:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def vendor(self):
|
|
|
|
return self.struct.vendor
|
|
|
|
|
|
|
|
@property
|
|
|
|
def model(self):
|
|
|
|
return self.struct.model
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self):
|
|
|
|
return self.struct.version
|
|
|
|
|
2013-04-21 14:46:48 +00:00
|
|
|
@property
|
|
|
|
def probes(self):
|
|
|
|
if self._probes is None:
|
|
|
|
self._probes = {}
|
|
|
|
probe_list = self.struct.probes
|
|
|
|
while (probe_list):
|
2014-03-20 20:58:57 +00:00
|
|
|
probe_ptr = void_ptr_to_sr_channel_ptr(probe_list.data)
|
2013-04-21 14:46:48 +00:00
|
|
|
self._probes[probe_ptr.name] = Probe(self, probe_ptr)
|
|
|
|
probe_list = probe_list.next
|
|
|
|
return self._probes
|
|
|
|
|
|
|
|
@property
|
2014-03-14 20:09:37 +00:00
|
|
|
def channel_groups(self):
|
|
|
|
if self._channel_groups is None:
|
|
|
|
self._channel_groups = {}
|
|
|
|
channel_group_list = self.struct.channel_groups
|
|
|
|
while (channel_group_list):
|
|
|
|
channel_group_ptr = void_ptr_to_sr_channel_group_ptr(
|
|
|
|
channel_group_list.data)
|
|
|
|
self._channel_groups[channel_group_ptr.name] = ChannelGroup(self,
|
|
|
|
channel_group_ptr)
|
|
|
|
channel_group_list = channel_group_list.next
|
|
|
|
return self._channel_groups
|
2013-04-21 14:46:48 +00:00
|
|
|
|
2013-12-17 16:10:08 +00:00
|
|
|
class HardwareDevice(Device):
|
|
|
|
|
|
|
|
def __new__(cls, driver, struct):
|
|
|
|
device = Device.__new__(cls, struct, driver.context)
|
|
|
|
device.driver = driver
|
|
|
|
return device
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
key = getattr(ConfigKey, name)
|
|
|
|
data = new_gvariant_ptr_ptr()
|
|
|
|
try:
|
|
|
|
check(sr_config_get(self.driver.struct, self.struct, None,
|
|
|
|
key.id, data))
|
|
|
|
except Error as error:
|
|
|
|
if error.errno == SR_ERR_NA:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Device does not implement %s" % name)
|
|
|
|
else:
|
|
|
|
raise AttributeError
|
|
|
|
value = gvariant_ptr_ptr_value(data)
|
|
|
|
return gvariant_to_python(value)
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
try:
|
|
|
|
key = getattr(ConfigKey, name)
|
|
|
|
except AttributeError:
|
|
|
|
super(Device, self).__setattr__(name, value)
|
|
|
|
return
|
|
|
|
check(sr_config_set(self.struct, None, key.id, python_to_gvariant(value)))
|
|
|
|
|
2013-04-21 14:46:48 +00:00
|
|
|
class Probe(object):
|
|
|
|
|
|
|
|
def __init__(self, device, struct):
|
|
|
|
self.device = device
|
|
|
|
self.struct = struct
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
return ProbeType(self.struct.type)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def enabled(self):
|
|
|
|
return self.struct.enabled
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.struct.name
|
|
|
|
|
2014-03-14 20:09:37 +00:00
|
|
|
class ChannelGroup(object):
|
2013-04-21 14:46:48 +00:00
|
|
|
|
|
|
|
def __init__(self, device, struct):
|
|
|
|
self.device = device
|
|
|
|
self.struct = struct
|
2014-03-14 20:09:37 +00:00
|
|
|
self._channels = None
|
2013-04-21 14:46:48 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
2014-03-14 20:09:37 +00:00
|
|
|
return iter(self.channels)
|
2013-04-21 14:46:48 +00:00
|
|
|
|
2013-04-21 15:02:12 +00:00
|
|
|
def __getattr__(self, name):
|
|
|
|
key = config_key(name)
|
|
|
|
data = new_gvariant_ptr_ptr()
|
|
|
|
try:
|
|
|
|
check(sr_config_get(self.device.driver.struct, self.device.struct,
|
2013-12-16 18:09:57 +00:00
|
|
|
self.struct, key.id, data))
|
2013-04-21 15:02:12 +00:00
|
|
|
except Error as error:
|
|
|
|
if error.errno == SR_ERR_NA:
|
|
|
|
raise NotImplementedError(
|
2014-03-14 20:09:37 +00:00
|
|
|
"Channel group does not implement %s" % name)
|
2013-04-21 15:02:12 +00:00
|
|
|
else:
|
|
|
|
raise AttributeError
|
|
|
|
value = gvariant_ptr_ptr_value(data)
|
|
|
|
return gvariant_to_python(value)
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
try:
|
|
|
|
key = config_key(name)
|
|
|
|
except AttributeError:
|
2014-03-14 20:09:37 +00:00
|
|
|
super(ChannelGroup, self).__setattr__(name, value)
|
2013-04-21 15:02:12 +00:00
|
|
|
return
|
|
|
|
check(sr_config_set(self.device.struct, self.struct,
|
2013-12-16 18:09:57 +00:00
|
|
|
key.id, python_to_gvariant(value)))
|
2013-04-21 15:02:12 +00:00
|
|
|
|
2013-04-21 14:46:48 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.struct.name
|
|
|
|
|
|
|
|
@property
|
2014-03-14 20:09:37 +00:00
|
|
|
def channels(self):
|
|
|
|
if self._channels is None:
|
|
|
|
self._channels = []
|
|
|
|
channel_list = self.struct.channels
|
|
|
|
while (channel_list):
|
2014-03-20 20:58:57 +00:00
|
|
|
channel_ptr = void_ptr_to_sr_channel_ptr(channel_list.data)
|
2014-03-14 20:09:37 +00:00
|
|
|
self._channels.append(Probe(self, probe_ptr))
|
|
|
|
channel_list = channel_list.next
|
|
|
|
return self._channels
|
2013-04-21 14:46:48 +00:00
|
|
|
|
2013-04-17 13:30:31 +00:00
|
|
|
class Session(object):
|
|
|
|
|
|
|
|
def __init__(self, context):
|
|
|
|
assert context.session is None
|
|
|
|
self.context = context
|
|
|
|
self.struct = sr_session_new()
|
|
|
|
context.session = self
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
check(sr_session_destroy())
|
|
|
|
|
|
|
|
def add_device(self, device):
|
|
|
|
check(sr_session_dev_add(device.struct))
|
|
|
|
|
2013-04-24 22:15:49 +00:00
|
|
|
def open_device(self, device):
|
|
|
|
check(sr_dev_open(device.struct))
|
|
|
|
|
2013-04-17 13:30:31 +00:00
|
|
|
def add_callback(self, callback):
|
|
|
|
wrapper = partial(callback_wrapper, self, callback)
|
|
|
|
check(sr_session_datafeed_python_callback_add(wrapper))
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
check(sr_session_start())
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
check(sr_session_run())
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
check(sr_session_stop())
|
|
|
|
|
|
|
|
class Packet(object):
|
|
|
|
|
|
|
|
def __init__(self, session, struct):
|
|
|
|
self.session = session
|
|
|
|
self.struct = struct
|
|
|
|
self._payload = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
2013-04-19 15:57:13 +00:00
|
|
|
return PacketType(self.struct.type)
|
2013-04-17 13:30:31 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def payload(self):
|
|
|
|
if self._payload is None:
|
|
|
|
pointer = self.struct.payload
|
2013-04-19 15:57:13 +00:00
|
|
|
if self.type == PacketType.LOGIC:
|
2013-04-17 13:30:31 +00:00
|
|
|
self._payload = Logic(self,
|
|
|
|
void_ptr_to_sr_datafeed_logic_ptr(pointer))
|
2013-04-19 15:57:13 +00:00
|
|
|
elif self.type == PacketType.ANALOG:
|
2013-04-17 16:48:39 +00:00
|
|
|
self._payload = Analog(self,
|
|
|
|
void_ptr_to_sr_datafeed_analog_ptr(pointer))
|
2013-04-17 13:30:31 +00:00
|
|
|
else:
|
2013-04-17 14:37:58 +00:00
|
|
|
raise NotImplementedError(
|
2013-04-21 14:46:48 +00:00
|
|
|
"No Python mapping for packet type %s" % self.struct.type)
|
2013-04-17 13:30:31 +00:00
|
|
|
return self._payload
|
|
|
|
|
|
|
|
class Logic(object):
|
|
|
|
|
|
|
|
def __init__(self, packet, struct):
|
|
|
|
self.packet = packet
|
|
|
|
self.struct = struct
|
|
|
|
self._data = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self):
|
|
|
|
if self._data is None:
|
|
|
|
self._data = cdata(self.struct.data, self.struct.length)
|
|
|
|
return self._data
|
|
|
|
|
2013-04-17 16:48:39 +00:00
|
|
|
class Analog(object):
|
|
|
|
|
|
|
|
def __init__(self, packet, struct):
|
|
|
|
self.packet = packet
|
|
|
|
self.struct = struct
|
|
|
|
self._data = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def num_samples(self):
|
|
|
|
return self.struct.num_samples
|
|
|
|
|
2013-04-18 15:45:17 +00:00
|
|
|
@property
|
|
|
|
def mq(self):
|
2013-04-19 15:57:13 +00:00
|
|
|
return Quantity(self.struct.mq)
|
2013-04-18 15:45:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit(self):
|
2013-04-19 15:57:13 +00:00
|
|
|
return Unit(self.struct.unit)
|
2013-04-18 15:45:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def mqflags(self):
|
2013-04-19 15:57:13 +00:00
|
|
|
return QuantityFlag.set_from_mask(self.struct.mqflags)
|
2013-04-18 15:45:17 +00:00
|
|
|
|
2013-04-17 16:48:39 +00:00
|
|
|
@property
|
|
|
|
def data(self):
|
|
|
|
if self._data is None:
|
|
|
|
self._data = float_array.frompointer(self.struct.data)
|
|
|
|
return self._data
|
|
|
|
|
2013-04-19 11:15:51 +00:00
|
|
|
class Log(object):
|
|
|
|
|
|
|
|
@property
|
|
|
|
def level(self):
|
2013-04-19 15:57:13 +00:00
|
|
|
return LogLevel(sr_log_loglevel_get())
|
2013-04-19 11:15:51 +00:00
|
|
|
|
|
|
|
@level.setter
|
|
|
|
def level(self, l):
|
2013-04-19 15:57:13 +00:00
|
|
|
check(sr_log_loglevel_set(l.id))
|
2013-04-19 11:15:51 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def domain(self):
|
|
|
|
return sr_log_logdomain_get()
|
|
|
|
|
|
|
|
@domain.setter
|
|
|
|
def domain(self, d):
|
|
|
|
check(sr_log_logdomain_set(d))
|
|
|
|
|
2013-12-16 01:21:39 +00:00
|
|
|
class InputFormat(object):
|
|
|
|
|
|
|
|
def __init__(self, context, struct):
|
|
|
|
self.context = context
|
|
|
|
self.struct = struct
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.struct.id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def description(self):
|
|
|
|
return self.struct.description
|
|
|
|
|
2013-12-16 02:11:42 +00:00
|
|
|
def format_match(self, filename):
|
|
|
|
return bool(self.struct.call_format_match(filename))
|
|
|
|
|
|
|
|
class InputFile(object):
|
|
|
|
|
|
|
|
def __init__(self, format, filename, **kwargs):
|
|
|
|
self.format = format
|
|
|
|
self.filename = filename
|
|
|
|
self.struct = sr_input()
|
|
|
|
self.struct.format = self.format.struct
|
|
|
|
self.struct.param = g_hash_table_new_full(
|
2013-12-17 00:42:33 +00:00
|
|
|
g_str_hash_ptr, g_str_equal_ptr, g_free_ptr, g_free_ptr)
|
2013-12-16 02:11:42 +00:00
|
|
|
for key, value in kwargs.items():
|
|
|
|
g_hash_table_insert(self.struct.param, g_strdup(key), g_strdup(str(value)))
|
|
|
|
check(self.format.struct.call_init(self.struct, self.filename))
|
2013-12-17 16:10:08 +00:00
|
|
|
self.device = InputFileDevice(self)
|
2013-12-16 02:11:42 +00:00
|
|
|
|
|
|
|
def load(self):
|
|
|
|
check(self.format.struct.call_loadfile(self.struct, self.filename))
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
g_hash_table_destroy(self.struct.param)
|
|
|
|
|
2013-12-17 16:10:08 +00:00
|
|
|
class InputFileDevice(Device):
|
|
|
|
|
|
|
|
def __new__(cls, file):
|
|
|
|
device = Device.__new__(cls, file.struct.sdi, file.format.context)
|
|
|
|
device.file = file
|
|
|
|
return device
|
|
|
|
|
2013-12-16 01:21:39 +00:00
|
|
|
class OutputFormat(object):
|
|
|
|
|
|
|
|
def __init__(self, context, struct):
|
|
|
|
self.context = context
|
|
|
|
self.struct = struct
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.struct.id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def description(self):
|
|
|
|
return self.struct.description
|
|
|
|
|
2013-12-17 00:42:33 +00:00
|
|
|
class Output(object):
|
|
|
|
|
|
|
|
def __init__(self, format, device, param=None):
|
|
|
|
self.format = format
|
|
|
|
self.device = device
|
|
|
|
self.param = param
|
|
|
|
self.struct = sr_output()
|
|
|
|
self.struct.format = self.format.struct
|
|
|
|
self.struct.sdi = self.device.struct
|
|
|
|
self.struct.param = param
|
|
|
|
check(self.format.struct.call_init(self.struct))
|
|
|
|
|
|
|
|
def receive(self, packet):
|
|
|
|
|
|
|
|
output_buf_ptr = new_uint8_ptr_ptr()
|
|
|
|
output_len_ptr = new_uint64_ptr()
|
|
|
|
using_obsolete_api = False
|
|
|
|
|
|
|
|
if self.format.struct.event and packet.type in (
|
|
|
|
PacketType.TRIGGER, PacketType.FRAME_BEGIN,
|
|
|
|
PacketType.FRAME_END, PacketType.END):
|
|
|
|
check(self.format.struct.call_event(self.struct, packet.type.id,
|
|
|
|
output_buf_ptr, output_len_ptr))
|
|
|
|
using_obsolete_api = True
|
|
|
|
elif self.format.struct.data and packet.type.id == self.format.struct.df_type:
|
|
|
|
check(self.format.struct.call_data(self.struct,
|
|
|
|
packet.payload.struct.data, packet.payload.struct.length,
|
|
|
|
output_buf_ptr, output_len_ptr))
|
|
|
|
using_obsolete_api = True
|
|
|
|
|
|
|
|
if using_obsolete_api:
|
|
|
|
output_buf = uint8_ptr_ptr_value(output_buf_ptr)
|
|
|
|
output_len = uint64_ptr_value(output_len_ptr)
|
|
|
|
result = cdata(output_buf, output_len)
|
|
|
|
g_free(output_buf)
|
|
|
|
return result
|
|
|
|
|
|
|
|
if self.format.struct.receive:
|
|
|
|
out_ptr = new_gstring_ptr_ptr()
|
|
|
|
check(self.format.struct.call_receive(self.struct, self.device.struct,
|
|
|
|
packet.struct, out_ptr))
|
|
|
|
out = gstring_ptr_ptr_value(out_ptr)
|
|
|
|
if out:
|
|
|
|
result = out.str
|
|
|
|
g_string_free(out, True)
|
|
|
|
return result
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
check(self.format.struct.call_cleanup(self.struct))
|
|
|
|
|
2013-12-17 12:47:49 +00:00
|
|
|
class ConfigInfo(object):
|
|
|
|
|
2013-12-17 13:01:11 +00:00
|
|
|
def __new__(cls, key):
|
|
|
|
struct = sr_config_info_get(key.id)
|
|
|
|
if not struct:
|
|
|
|
return None
|
|
|
|
obj = super(ConfigInfo, cls).__new__(cls)
|
|
|
|
obj.key = key
|
|
|
|
obj.struct = struct
|
|
|
|
return obj
|
2013-12-17 12:47:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def datatype(self):
|
|
|
|
return DataType(self.struct.datatype)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.struct.id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.struct.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def description(self):
|
|
|
|
return self.struct.description
|
|
|
|
|
2013-04-19 15:57:13 +00:00
|
|
|
class EnumValue(object):
|
|
|
|
|
|
|
|
_enum_values = {}
|
|
|
|
|
|
|
|
def __new__(cls, id):
|
|
|
|
if cls not in cls._enum_values:
|
|
|
|
cls._enum_values[cls] = {}
|
|
|
|
if id not in cls._enum_values[cls]:
|
|
|
|
value = super(EnumValue, cls).__new__(cls)
|
|
|
|
value.id = id
|
|
|
|
cls._enum_values[cls][id] = value
|
|
|
|
return cls._enum_values[cls][id]
|
|
|
|
|
|
|
|
class LogLevel(EnumValue):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PacketType(EnumValue):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Quantity(EnumValue):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Unit(EnumValue):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class QuantityFlag(EnumValue):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_from_mask(cls, mask):
|
|
|
|
result = set()
|
|
|
|
while mask:
|
|
|
|
new_mask = mask & (mask - 1)
|
|
|
|
result.add(cls(mask ^ new_mask))
|
|
|
|
mask = new_mask
|
|
|
|
return result
|
|
|
|
|
2013-04-24 16:47:40 +00:00
|
|
|
class ConfigKey(EnumValue):
|
|
|
|
pass
|
|
|
|
|
2013-12-17 12:47:49 +00:00
|
|
|
class DataType(EnumValue):
|
|
|
|
pass
|
|
|
|
|
2013-04-21 14:46:48 +00:00
|
|
|
class ProbeType(EnumValue):
|
|
|
|
pass
|
|
|
|
|
2013-04-17 13:30:31 +00:00
|
|
|
for symbol_name in dir(lowlevel):
|
2013-04-19 15:57:13 +00:00
|
|
|
for prefix, cls in [
|
|
|
|
('SR_LOG_', LogLevel),
|
|
|
|
('SR_DF_', PacketType),
|
|
|
|
('SR_MQ_', Quantity),
|
|
|
|
('SR_UNIT_', Unit),
|
2013-04-24 16:47:40 +00:00
|
|
|
('SR_MQFLAG_', QuantityFlag),
|
2013-04-21 14:46:48 +00:00
|
|
|
('SR_CONF_', ConfigKey),
|
2013-12-17 12:47:49 +00:00
|
|
|
('SR_T_', DataType),
|
2013-04-21 14:46:48 +00:00
|
|
|
('SR_PROBE_', ProbeType)]:
|
2013-04-19 15:57:13 +00:00
|
|
|
if symbol_name.startswith(prefix):
|
|
|
|
name = symbol_name[len(prefix):]
|
|
|
|
value = getattr(lowlevel, symbol_name)
|
2013-12-17 13:01:35 +00:00
|
|
|
obj = cls(value)
|
|
|
|
setattr(cls, name, obj)
|
|
|
|
if cls is ConfigKey:
|
|
|
|
obj.info = ConfigInfo(obj)
|
|
|
|
if obj.info:
|
|
|
|
setattr(cls, obj.info.id, obj)
|
2013-12-17 13:11:43 +00:00
|
|
|
else:
|
|
|
|
setattr(cls, name.lower(), obj)
|