Add a void *cb_data parameter to datafeed callbacks.

This commit is contained in:
Martin Ling 2013-04-15 21:08:55 +01:00 committed by Bert Vermeulen
parent bd6fbf628a
commit 2726474a61
3 changed files with 23 additions and 9 deletions

View File

@ -761,7 +761,7 @@ struct sr_dev_driver {
struct sr_session { struct sr_session {
/** List of struct sr_dev pointers. */ /** List of struct sr_dev pointers. */
GSList *devs; GSList *devs;
/** List of sr_receive_data_callback_t items. */ /** List of struct datafeed_callback pointers. */
GSList *datafeed_callbacks; GSList *datafeed_callbacks;
GTimeVal starttime; GTimeVal starttime;

View File

@ -80,7 +80,7 @@ SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname)
/*--- session.c -------------------------------------------------------------*/ /*--- session.c -------------------------------------------------------------*/
typedef void (*sr_datafeed_callback_t)(const struct sr_dev_inst *sdi, typedef void (*sr_datafeed_callback_t)(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *packet); const struct sr_datafeed_packet *packet, void *cb_data);
/* Session setup */ /* Session setup */
SR_API int sr_session_load(const char *filename); SR_API int sr_session_load(const char *filename);
@ -91,7 +91,8 @@ SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi);
/* Datafeed setup */ /* Datafeed setup */
SR_API int sr_session_datafeed_callback_remove_all(void); SR_API int sr_session_datafeed_callback_remove_all(void);
SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb); SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb,
void *cb_data);
/* Session control */ /* Session control */
SR_API int sr_session_start(void); SR_API int sr_session_start(void);

View File

@ -59,6 +59,11 @@ struct source {
gintptr poll_object; gintptr poll_object;
}; };
struct datafeed_callback {
sr_datafeed_callback_t cb;
void *cb_data;
};
/* There can only be one session at a time. */ /* There can only be one session at a time. */
/* 'session' is not static, it's used elsewhere (via 'extern'). */ /* 'session' is not static, it's used elsewhere (via 'extern'). */
struct sr_session *session; struct sr_session *session;
@ -234,7 +239,7 @@ SR_API int sr_session_datafeed_callback_remove_all(void)
return SR_ERR_BUG; return SR_ERR_BUG;
} }
g_slist_free(session->datafeed_callbacks); g_slist_free_full(session->datafeed_callbacks, g_free);
session->datafeed_callbacks = NULL; session->datafeed_callbacks = NULL;
return SR_OK; return SR_OK;
@ -248,8 +253,10 @@ SR_API int sr_session_datafeed_callback_remove_all(void)
* *
* @return SR_OK upon success, SR_ERR_BUG if no session exists. * @return SR_OK upon success, SR_ERR_BUG if no session exists.
*/ */
SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb) SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb, void *cb_data)
{ {
struct datafeed_callback *cb_struct;
if (!session) { if (!session) {
sr_err("%s: session was NULL", __func__); sr_err("%s: session was NULL", __func__);
return SR_ERR_BUG; return SR_ERR_BUG;
@ -260,8 +267,14 @@ SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
return SR_ERR_ARG; return SR_ERR_ARG;
} }
if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback))))
return SR_ERR_MALLOC;
cb_struct->cb = cb;
cb_struct->cb_data = cb_data;
session->datafeed_callbacks = session->datafeed_callbacks =
g_slist_append(session->datafeed_callbacks, cb); g_slist_append(session->datafeed_callbacks, cb_struct);
return SR_OK; return SR_OK;
} }
@ -511,7 +524,7 @@ SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
const struct sr_datafeed_packet *packet) const struct sr_datafeed_packet *packet)
{ {
GSList *l; GSList *l;
sr_datafeed_callback_t cb; struct datafeed_callback *cb_struct;
if (!sdi) { if (!sdi) {
sr_err("%s: sdi was NULL", __func__); sr_err("%s: sdi was NULL", __func__);
@ -526,8 +539,8 @@ SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
for (l = session->datafeed_callbacks; l; l = l->next) { for (l = session->datafeed_callbacks; l; l = l->next) {
if (sr_log_loglevel_get() >= SR_LOG_DBG) if (sr_log_loglevel_get() >= SR_LOG_DBG)
datafeed_dump(packet); datafeed_dump(packet);
cb = l->data; cb_struct = l->data;
cb(sdi, packet); cb_struct->cb(sdi, packet, cb_struct->cb_data);
} }
return SR_OK; return SR_OK;