From 6534b58d3b9643f9e15f1c45bec50ff57d3b21fa Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Wed, 10 Jul 2024 16:55:10 +0300 Subject: [PATCH] bluez5: telephony: add user_data space on the AG & Call structures This is mostly useful to store the spa_hook for the event listener --- spa/plugins/bluez5/telephony.c | 32 ++++++++++++++++++++++++++++---- spa/plugins/bluez5/telephony.h | 12 ++++++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/spa/plugins/bluez5/telephony.c b/spa/plugins/bluez5/telephony.c index 649522321..af1603b7f 100644 --- a/spa/plugins/bluez5/telephony.c +++ b/spa/plugins/bluez5/telephony.c @@ -155,6 +155,7 @@ struct agimpl { char *path; struct spa_list call_list; struct spa_hook_list listener_list; + void *user_data; bool dial_in_progress; struct callimpl *dial_return; @@ -166,6 +167,7 @@ struct callimpl { int id; char *path; struct spa_hook_list listener_list; + void *user_data; }; #define ag_emit(ag,m,v,...) spa_hook_list_call(&ag->listener_list, struct spa_bt_telephony_ag_events, m, v, ##__VA_ARGS__) @@ -576,12 +578,14 @@ static DBusHandlerResult ag_handler(DBusConnection *c, DBusMessage *m, void *use } struct spa_bt_telephony_ag * -telephony_ag_new(struct spa_bt_telephony *telephony) +telephony_ag_new(struct spa_bt_telephony *telephony, size_t user_data_size) { struct impl *impl = SPA_CONTAINER_OF(telephony, struct impl, this); struct agimpl *agimpl; - agimpl = calloc(1, sizeof(*agimpl)); + spa_assert(user_data_size < SIZE_MAX - sizeof(*agimpl)); + + agimpl = calloc(1, sizeof(*agimpl) + user_data_size); if (agimpl == NULL) return NULL; @@ -592,6 +596,9 @@ telephony_ag_new(struct spa_bt_telephony *telephony) spa_list_append(&impl->ag_list, &agimpl->link); + if (user_data_size > 0) + agimpl->user_data = SPA_PTROFF(agimpl, sizeof(struct agimpl), void); + return &agimpl->this; } @@ -611,6 +618,12 @@ void telephony_ag_destroy(struct spa_bt_telephony_ag *ag) free(agimpl); } +void *telephony_ag_get_user_data(struct spa_bt_telephony_ag *ag) +{ + struct agimpl *agimpl = SPA_CONTAINER_OF(ag, struct agimpl, this); + return agimpl->user_data; +} + int telephony_ag_register(struct spa_bt_telephony_ag *ag) { struct agimpl *agimpl = SPA_CONTAINER_OF(ag, struct agimpl, this); @@ -706,12 +719,14 @@ void telephony_ag_add_listener(struct spa_bt_telephony_ag *ag, } struct spa_bt_telephony_call * -telephony_call_new(struct spa_bt_telephony_ag *ag) +telephony_call_new(struct spa_bt_telephony_ag *ag, size_t user_data_size) { struct agimpl *agimpl = SPA_CONTAINER_OF(ag, struct agimpl, this); struct callimpl *callimpl; - callimpl = calloc(1, sizeof(*callimpl)); + spa_assert(user_data_size < SIZE_MAX - sizeof(*callimpl)); + + callimpl = calloc(1, sizeof(*callimpl) + user_data_size); if (callimpl == NULL) return NULL; @@ -721,6 +736,9 @@ telephony_call_new(struct spa_bt_telephony_ag *ag) spa_list_append(&agimpl->call_list, &callimpl->link); + if (user_data_size > 0) + callimpl->user_data = SPA_PTROFF(callimpl, sizeof(struct callimpl), void); + /* mark this object as the return value of the Dial method */ if (agimpl->dial_in_progress) agimpl->dial_return = callimpl; @@ -743,6 +761,12 @@ void telephony_call_destroy(struct spa_bt_telephony_call *call) free(callimpl); } +void *telephony_call_get_user_data(struct spa_bt_telephony_call *call) +{ + struct callimpl *callimpl = SPA_CONTAINER_OF(call, struct callimpl, this); + return callimpl->user_data; +} + static const char * const call_state_to_string[] = { "active", "held", diff --git a/spa/plugins/bluez5/telephony.h b/spa/plugins/bluez5/telephony.h index 11b252370..73bda4314 100644 --- a/spa/plugins/bluez5/telephony.h +++ b/spa/plugins/bluez5/telephony.h @@ -63,9 +63,13 @@ struct spa_bt_telephony *telephony_new(struct spa_log *log, struct spa_dbus *dbu void telephony_free(struct spa_bt_telephony *telephony); /* create/destroy the ag object */ -struct spa_bt_telephony_ag * telephony_ag_new(struct spa_bt_telephony *telephony); +struct spa_bt_telephony_ag * telephony_ag_new(struct spa_bt_telephony *telephony, + size_t user_data_size); void telephony_ag_destroy(struct spa_bt_telephony_ag *ag); +/* get the user data structure; struct size is set when creating the AG */ +void *telephony_ag_get_user_data(struct spa_bt_telephony_ag *ag); + void telephony_ag_add_listener(struct spa_bt_telephony_ag *ag, struct spa_hook *listener, const struct spa_bt_telephony_ag_events *events, @@ -80,9 +84,13 @@ void telephony_ag_unregister(struct spa_bt_telephony_ag *ag); //void telephony_ag_iterate_calls(struct spa_bt_telephony_ag *ag, callback, userdata); /* create/destroy the call object */ -struct spa_bt_telephony_call * telephony_call_new(struct spa_bt_telephony_ag *ag); +struct spa_bt_telephony_call * telephony_call_new(struct spa_bt_telephony_ag *ag, + size_t user_data_size); void telephony_call_destroy(struct spa_bt_telephony_call *call); +/* get the user data structure; struct size is set when creating the Call */ +void *telephony_call_get_user_data(struct spa_bt_telephony_call *call); + /* register/unregister Call object on the bus */ int telephony_call_register(struct spa_bt_telephony_call *call); void telephony_call_unregister(struct spa_bt_telephony_call *call);