Also remove the corresponding functionality from the bindings.
_callback(device, packet);
}
-SourceCallbackData::SourceCallbackData(shared_ptr<EventSource> source) :
- _source(source)
-{
-}
-
-bool SourceCallbackData::run(int revents)
-{
- return _source->_callback((Glib::IOCondition) revents);
-}
-
-shared_ptr<EventSource> EventSource::create(int fd, Glib::IOCondition events,
- int timeout, SourceCallbackFunction callback)
-{
- auto result = new EventSource(timeout, callback);
- result->_type = EventSource::SOURCE_FD;
- result->_fd = fd;
- result->_events = events;
- return shared_ptr<EventSource>(result, EventSource::Deleter());
-}
-
-shared_ptr<EventSource> EventSource::create(Glib::PollFD pollfd, int timeout,
- SourceCallbackFunction callback)
-{
- auto result = new EventSource(timeout, callback);
- result->_type = EventSource::SOURCE_POLLFD;
- result->_pollfd = pollfd;
- return shared_ptr<EventSource>(result, EventSource::Deleter());
-}
-
-shared_ptr<EventSource> EventSource::create(Glib::RefPtr<Glib::IOChannel> channel,
- Glib::IOCondition events, int timeout, SourceCallbackFunction callback)
-{
- auto result = new EventSource(timeout, callback);
- result->_type = EventSource::SOURCE_IOCHANNEL;
- result->_channel = channel;
- result->_events = events;
- return shared_ptr<EventSource>(result, EventSource::Deleter());
-}
-
-EventSource::EventSource(int timeout, SourceCallbackFunction callback) :
- _timeout(timeout),
- _callback(callback)
-{
-}
-
-EventSource::~EventSource()
-{
-}
-
SessionDevice::SessionDevice(struct sr_dev_inst *structure) :
ParentOwned(structure),
Device(structure)
for (auto callback : _datafeed_callbacks)
delete callback;
- for (auto entry : _source_callbacks)
- delete entry.second;
-
for (auto entry : _owned_devices)
delete entry.second;
}
_datafeed_callbacks.clear();
}
-static int source_callback(int fd, int revents, void *cb_data)
-{
- (void) fd;
- auto callback = (SourceCallbackData *) cb_data;
- return callback->run(revents);
-}
-
-void Session::add_source(shared_ptr<EventSource> source)
-{
- if (_source_callbacks.count(source) == 1)
- throw Error(SR_ERR_ARG);
-
- auto cb_data = new SourceCallbackData(source);
-
- switch (source->_type)
- {
- case EventSource::SOURCE_FD:
- check(sr_session_source_add(_structure, source->_fd, source->_events,
- source->_timeout, source_callback, cb_data));
- break;
- case EventSource::SOURCE_POLLFD:
- check(sr_session_source_add_pollfd(_structure,
- source->_pollfd.gobj(), source->_timeout, source_callback,
- cb_data));
- break;
- case EventSource::SOURCE_IOCHANNEL:
- check(sr_session_source_add_channel(_structure,
- source->_channel->gobj(), source->_events, source->_timeout,
- source_callback, cb_data));
- break;
- }
-
- _source_callbacks[source] = cb_data;
-}
-
-void Session::remove_source(shared_ptr<EventSource> source)
-{
- if (_source_callbacks.count(source) == 0)
- throw Error(SR_ERR_ARG);
-
- switch (source->_type)
- {
- case EventSource::SOURCE_FD:
- check(sr_session_source_remove(_structure, source->_fd));
- break;
- case EventSource::SOURCE_POLLFD:
- check(sr_session_source_remove_pollfd(_structure,
- source->_pollfd.gobj()));
- break;
- case EventSource::SOURCE_IOCHANNEL:
- check(sr_session_source_remove_channel(_structure,
- source->_channel->gobj()));
- break;
- }
-
- delete _source_callbacks[source];
-
- _source_callbacks.erase(source);
-}
-
shared_ptr<Trigger> Session::trigger()
{
return _trigger;
class SR_API Device;
class SR_API HardwareDevice;
class SR_API Channel;
-class SR_API EventSource;
class SR_API Session;
class SR_API ConfigKey;
class SR_API InputFormat;
friend class Session;
};
-/** Type of source callback */
-typedef function<bool(Glib::IOCondition)>
- SourceCallbackFunction;
-
-/* Data required for C callback function to call a C++ source callback */
-class SR_PRIV SourceCallbackData
-{
-public:
- bool run(int revents);
-protected:
- SourceCallbackData(shared_ptr<EventSource> source);
- shared_ptr<EventSource> _source;
- friend class Session;
-};
-
-/** An I/O event source */
-class SR_API EventSource
-{
-public:
- /** Create an event source from a file descriptor.
- * @param fd File descriptor.
- * @param events GLib IOCondition event mask.
- * @param timeout Timeout in milliseconds.
- * @param callback Callback of the form callback(events) */
- static shared_ptr<EventSource> create(int fd, Glib::IOCondition events,
- int timeout, SourceCallbackFunction callback);
- /** Create an event source from a GLib PollFD
- * @param pollfd GLib PollFD
- * @param timeout Timeout in milliseconds.
- * @param callback Callback of the form callback(events) */
- static shared_ptr<EventSource> create(Glib::PollFD pollfd, int timeout,
- SourceCallbackFunction callback);
- /** Create an event source from a GLib IOChannel
- * @param channel GLib IOChannel.
- * @param events GLib IOCondition event mask.
- * @param timeout Timeout in milliseconds.
- * @param callback Callback of the form callback(events) */
- static shared_ptr<EventSource> create(
- Glib::RefPtr<Glib::IOChannel> channel, Glib::IOCondition events,
- int timeout, SourceCallbackFunction callback);
-protected:
- EventSource(int timeout, SourceCallbackFunction callback);
- ~EventSource();
- enum source_type {
- SOURCE_FD,
- SOURCE_POLLFD,
- SOURCE_IOCHANNEL
- } _type;
- int _fd;
- Glib::PollFD _pollfd;
- Glib::RefPtr<Glib::IOChannel> _channel;
- Glib::IOCondition _events;
- int _timeout;
- SourceCallbackFunction _callback;
- /** Deleter needed to allow shared_ptr use with protected destructor. */
- class Deleter
- {
- public:
- void operator()(EventSource *source) { delete source; }
- };
- friend class Deleter;
- friend class Session;
- friend class SourceCallbackData;
-};
-
/** A virtual device associated with a stored session */
class SR_API SessionDevice :
public ParentOwned<SessionDevice, Session, struct sr_dev_inst>,
void add_datafeed_callback(DatafeedCallbackFunction callback);
/** Remove all datafeed callbacks from this session. */
void remove_datafeed_callbacks();
- /** Add an I/O event source.
- * @param source EventSource to add. */
- void add_source(shared_ptr<EventSource> source);
- /** Remove an event source.
- * @param source EventSource to remove. */
- void remove_source(shared_ptr<EventSource> source);
/** Start the session. */
void start();
/** Run the session event loop. */
map<const struct sr_dev_inst *, shared_ptr<Device> > _other_devices;
vector<DatafeedCallbackData *> _datafeed_callbacks;
SessionStoppedCallback _stopped_callback;
- map<shared_ptr<EventSource>, SourceCallbackData *> _source_callbacks;
string _filename;
shared_ptr<Trigger> _trigger;
friend class Deleter;
import org.sigrok.core.interfaces.LogCallback;
import org.sigrok.core.interfaces.DatafeedCallback;
-import org.sigrok.core.interfaces.SourceCallback;
%}
-/* Map Java FileDescriptor objects to int fds */
-%typemap(jni) int fd "jobject"
-%typemap(jtype) int fd "java.io.FileDescriptor"
-%typemap(jstype) int fd "java.io.FileDescriptor"
-%typemap(javain) int fd "$javainput"
-
-%typemap(in) int fd {
- jclass FileDescriptor = jenv->FindClass("java/io/FileDescriptor");
- jfieldID fd = jenv->GetFieldID(FileDescriptor, "fd", "I");
- $1 = jenv->GetIntField($input, fd);
-}
-
/* Map Glib::VariantBase to a Variant class in Java */
%rename(Variant) VariantBase;
namespace Glib {
}
}
-/* Support Java event source callbacks. */
-
-%typemap(javaimports) sigrok::EventSource
- "import org.sigrok.core.interfaces.SourceCallback;"
-
-%inline {
-typedef jobject jsourcecallback;
-}
-
-%typemap(jni) jsourcecallback "jsourcecallback"
-%typemap(jtype) jsourcecallback "SourceCallback"
-%typemap(jstype) jsourcecallback "SourceCallback"
-%typemap(javain) jsourcecallback "$javainput"
-
-%extend sigrok::EventSource
-{
- std::shared_ptr<sigrok::EventSource> create(
- int fd, Glib::IOCondition events, int timeout,
- JNIEnv *env, jsourcecallback obj)
- {
- (void) $self;
- jclass obj_class = env->GetObjectClass(obj);
- jmethodID method = env->GetMethodID(obj_class, "run", "(I)V");
- jobject obj_ref = env->NewGlobalRef(obj);
-
- return sigrok::EventSource::create(fd, events, timeout, [=] (int revents)
- {
- bool result = env->CallBooleanMethod(obj_ref, method, revents);
- if (env->ExceptionCheck())
- throw sigrok::Error(SR_ERR);
- return result;
- });
- }
-}
-
%include "doc.i"
%define %attributevector(Class, Type, Name, Get)
PyObject *PyGObject_lib;
PyObject *GLib;
-PyTypeObject *IOChannel;
-PyTypeObject *PollFD;
#include "config.h"
return;
#endif
}
- IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel");
- PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD");
import_array();
%}
$1 = (PyObject_AsFileDescriptor($input) != -1);
}
-%typemap(in) int fd {
- int fd = PyObject_AsFileDescriptor($input);
- if (fd == -1)
- SWIG_exception(SWIG_TypeError,
- "Expected file object or integer file descriptor");
- else
- $1 = fd;
-}
-
/* Map from Glib::Variant to native Python types. */
%typemap(out) Glib::VariantBase {
GValue *value = g_new0(GValue, 1);
g_free(value);
}
-/* Map from Glib::IOCondition to GLib.IOCondition. */
-%typecheck(SWIG_TYPECHECK_POINTER) Glib::IOCondition {
- pyg_flags_type flags;
- $1 = pygobject_check($input, &PyGFlags_Type) &&
- (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) != -1);
-}
-
-%typemap(in) Glib::IOCondition {
- if (!pygobject_check($input, &PyGFlags_Type))
- SWIG_exception(SWIG_TypeError, "Expected GLib.IOCondition value");
- pyg_flags_type flags;
- if (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) == -1)
- SWIG_exception(SWIG_TypeError, "Not a valid Glib.IOCondition value");
- $1 = (Glib::IOCondition) flags;
-}
-
-/* And back */
-%typemap(out) Glib::IOCondition {
- GValue *value = g_new0(GValue, 1);
- g_value_init(value, G_TYPE_IO_CONDITION);
- g_value_set_flags(value, &$1);
- $result = pyg_value_as_pyobject(value, true);
- g_free(value);
-}
-
-/* Map from GLib.PollFD to Glib::PollFD *. */
-%typecheck(SWIG_TYPECHECK_POINTER) Glib::PollFD {
- $1 = pygobject_check($input, PollFD);
-}
-
-%typemap(in) Glib::PollFD {
- if (!pygobject_check($input, PollFD))
- SWIG_exception(SWIG_TypeError, "Expected GLib.PollFD");
- PyObject *fd_obj = PyObject_GetAttrString($input, "fd");
- PyObject *events_obj = PyObject_GetAttrString($input, "events");
- pyg_flags_type flags;
- pyg_flags_get_value(G_TYPE_IO_CONDITION, events_obj, &flags);
- int fd = PyInt_AsLong(fd_obj);
- Glib::IOCondition events = (Glib::IOCondition) flags;
- $1 = Glib::PollFD(fd, events);
-}
-
-/* Map from GLib.IOChannel to Glib::IOChannel *. */
-%typecheck(SWIG_TYPECHECK_POINTER) Glib::RefPtr<Glib::IOChannel> {
- $1 = pygobject_check($input, IOChannel);
-}
-
-%typemap(in) Glib::RefPtr<Glib::IOChannel> {
- if (!pygobject_check($input, IOChannel))
- SWIG_exception(SWIG_TypeError, "Expected GLib.IOChannel");
- $1 = Glib::wrap((GIOChannel *) PyObject_Hash($input), true);
-}
-
-/* Map from callable PyObject to SourceCallbackFunction. */
-%typecheck(SWIG_TYPECHECK_POINTER) sigrok::SourceCallbackFunction {
- $1 = PyCallable_Check($input);
-}
-
-%typemap(in) sigrok::SourceCallbackFunction {
- if (!PyCallable_Check($input))
- SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
-
- $1 = [=] (Glib::IOCondition revents) {
- auto gstate = PyGILState_Ensure();
-
- GValue *value = g_new0(GValue, 1);
- g_value_init(value, G_TYPE_IO_CONDITION);
- g_value_set_flags(value, revents);
- auto revents_obj = pyg_value_as_pyobject(value, true);
- g_free(value);
-
- auto arglist = Py_BuildValue("(O)", revents_obj);
-
- auto result = PyEval_CallObject($input, arglist);
-
- Py_XDECREF(arglist);
- Py_XDECREF(revents_obj);
-
- bool completed = !PyErr_Occurred();
-
- if (!completed)
- PyErr_Print();
-
- bool valid_result = (completed && PyBool_Check(result));
-
- if (completed && !valid_result)
- {
- PyErr_SetString(PyExc_TypeError,
- "EventSource callback did not return a boolean");
- PyErr_Print();
- }
-
- bool retval = (valid_result && result == Py_True);
-
- Py_XDECREF(result);
-
- PyGILState_Release(gstate);
-
- if (!valid_result)
- throw sigrok::Error(SR_ERR);
-
- return retval;
- };
-
- Py_XINCREF($input);
-}
-
/* Map from callable PyObject to LogCallbackFunction */
%typecheck(SWIG_TYPECHECK_POINTER) sigrok::LogCallbackFunction {
$1 = PyCallable_Check($input);
%shared_ptr(sigrok::HardwareDevice);
%shared_ptr(sigrok::Channel);
%shared_ptr(sigrok::ChannelGroup);
-%shared_ptr(sigrok::EventSource);
%shared_ptr(sigrok::Session);
%shared_ptr(sigrok::SessionDevice);
%shared_ptr(sigrok::Packet);
#define SR_PRIV
%ignore sigrok::DatafeedCallbackData;
-%ignore sigrok::SourceCallbackData;
#define SWIG_ATTRIBUTE_TEMPLATE
SR_API int sr_session_stopped_callback_set(struct sr_session *session,
sr_session_stopped_callback cb, void *cb_data);
-SR_API int sr_session_source_add(struct sr_session *session, int fd,
- int events, int timeout, sr_receive_data_callback cb, void *cb_data);
-SR_API int sr_session_source_add_pollfd(struct sr_session *session,
- GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
- void *cb_data);
-SR_API int sr_session_source_add_channel(struct sr_session *session,
- GIOChannel *channel, int events, int timeout,
- sr_receive_data_callback cb, void *cb_data);
-SR_API int sr_session_source_remove(struct sr_session *session, int fd);
-SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
- GPollFD *pollfd);
-SR_API int sr_session_source_remove_channel(struct sr_session *session,
- GIOChannel *channel);
-
/*--- input/input.c ---------------------------------------------------------*/
SR_API const struct sr_input_module **sr_input_list(void);
SR_PRIV int sr_session_fd_source_add(struct sr_session *session,
void *key, gintptr fd, int events, int timeout,
sr_receive_data_callback cb, void *cb_data);
+
+SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
+ int events, int timeout, sr_receive_data_callback cb, void *cb_data);
+SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
+ GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
+ void *cb_data);
+SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
+ GIOChannel *channel, int events, int timeout,
+ sr_receive_data_callback cb, void *cb_data);
+SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd);
+SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
+ GPollFD *pollfd);
+SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
+ GIOChannel *channel);
+
SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *packet);
SR_PRIV int sr_sessionfile_check(const char *filename);
* @param session The session to use. Must not be NULL.
* @param key The key which identifies the event source.
* @param source An event source object. Must not be NULL.
+ *
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument.
* @retval SR_ERR_BUG Event source with @a key already installed.
* @retval SR_ERR Other error.
+ *
+ * @private
*/
SR_PRIV int sr_session_source_add_internal(struct sr_session *session,
void *key, GSource *source)
* @retval SR_ERR_ARG Invalid argument.
*
* @since 0.3.0
+ * @private
*/
-SR_API int sr_session_source_add(struct sr_session *session, int fd,
+SR_PRIV int sr_session_source_add(struct sr_session *session, int fd,
int events, int timeout, sr_receive_data_callback cb, void *cb_data)
{
if (fd < 0 && timeout < 0) {
* @retval SR_ERR_ARG Invalid argument.
*
* @since 0.3.0
+ * @private
*/
-SR_API int sr_session_source_add_pollfd(struct sr_session *session,
+SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session,
GPollFD *pollfd, int timeout, sr_receive_data_callback cb,
void *cb_data)
{
* @retval SR_ERR_ARG Invalid argument.
*
* @since 0.3.0
+ * @private
*/
-SR_API int sr_session_source_add_channel(struct sr_session *session,
+SR_PRIV int sr_session_source_add_channel(struct sr_session *session,
GIOChannel *channel, int events, int timeout,
sr_receive_data_callback cb, void *cb_data)
{
*
* @retval SR_OK Success
* @retval SR_ERR_BUG No event source for poll_object found.
+ *
+ * @private
*/
SR_PRIV int sr_session_source_remove_internal(struct sr_session *session,
void *key)
* @retval SR_ERR_BUG Internal error.
*
* @since 0.3.0
+ * @private
*/
-SR_API int sr_session_source_remove(struct sr_session *session, int fd)
+SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd)
{
return sr_session_source_remove_internal(session, GINT_TO_POINTER(fd));
}
* internal errors.
*
* @since 0.2.0
+ * @private
*/
-SR_API int sr_session_source_remove_pollfd(struct sr_session *session,
+SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session,
GPollFD *pollfd)
{
if (!pollfd) {
* @return SR_ERR_BUG Internal error.
*
* @since 0.2.0
+ * @private
*/
-SR_API int sr_session_source_remove_channel(struct sr_session *session,
+SR_PRIV int sr_session_source_remove_channel(struct sr_session *session,
GIOChannel *channel)
{
if (!channel) {
* @retval SR_OK Success.
* @retval SR_ERR_BUG Event source for @a key does not match @a source.
* @retval SR_ERR Other error.
+ *
+ * @private
*/
SR_PRIV int sr_session_source_destroyed(struct sr_session *session,
void *key, GSource *source)