From 951e03dfeadf4d4594c886c17792b7d47034d4de Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Thu, 21 Feb 2013 08:27:55 -0600 Subject: [PATCH] handsfree-audio: Add ref / unref support --- src/handsfree-audio.c | 46 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/handsfree-audio.c b/src/handsfree-audio.c index dd398549..4e2cf936 100644 --- a/src/handsfree-audio.c +++ b/src/handsfree-audio.c @@ -29,6 +29,8 @@ #include +#include + #include "ofono.h" #define HFP_AUDIO_MANAGER_INTERFACE OFONO_SERVICE ".HandsfreeAudioManager" @@ -49,6 +51,7 @@ struct agent { }; static struct agent *agent = NULL; +static int ref_count = 0; static void agent_free(struct agent *agent) { @@ -168,19 +171,31 @@ static const GDBusMethodTable am_methods[] = { { } }; -int __ofono_handsfree_audio_manager_init(void) +void ofono_handsfree_audio_ref(void) { + ref_count += 1; + + if (ref_count != 1) + return; + if (!g_dbus_register_interface(ofono_dbus_get_connection(), "/", HFP_AUDIO_MANAGER_INTERFACE, - am_methods, NULL, NULL, NULL, NULL)) { - return -EIO; - } - - return 0; + am_methods, NULL, NULL, NULL, NULL)) + ofono_error("Unable to register Handsfree Audio Manager"); } -void __ofono_handsfree_audio_manager_cleanup(void) +void ofono_handsfree_audio_unref(void) { + if (ref_count == 0) { + ofono_error("Error in handsfree audio manager ref counting"); + return; + } + + ref_count -= 1; + + if (ref_count > 0) + return; + g_dbus_unregister_interface(ofono_dbus_get_connection(), "/", HFP_AUDIO_MANAGER_INTERFACE); @@ -189,3 +204,20 @@ void __ofono_handsfree_audio_manager_cleanup(void) agent_free(agent); } } + +int __ofono_handsfree_audio_manager_init(void) +{ + return 0; +} + +void __ofono_handsfree_audio_manager_cleanup(void) +{ + if (ref_count == 0) + return; + + ofono_error("Handsfree Audio manager not cleaned up properly," + "fixing..."); + + ref_count = 1; + ofono_handsfree_audio_unref(); +}