diff options
author | Johan Hedberg <johan.hedberg@nokia.com> | 2007-04-12 14:26:55 +0000 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@nokia.com> | 2007-04-12 14:26:55 +0000 |
commit | a07ea139e52ab9733f6d1e9f0ab2e0bf232059ef (patch) | |
tree | efd7dcf423c38397b38f1b97f4bf02051ce92ab0 /audio | |
parent | e41abf4a7c4f97c679ce748ef7eabbd11bcff3d9 (diff) |
A little more refactoring
Diffstat (limited to 'audio')
-rw-r--r-- | audio/headset.c | 94 | ||||
-rw-r--r-- | audio/headset.h | 7 | ||||
-rw-r--r-- | audio/main.c | 14 | ||||
-rw-r--r-- | audio/manager.c | 93 |
4 files changed, 102 insertions, 106 deletions
diff --git a/audio/headset.c b/audio/headset.c index c08489ff..a6581df8 100644 --- a/audio/headset.c +++ b/audio/headset.c @@ -54,6 +54,8 @@ #include "manager.h" #include "headset.h" +#define DEFAULT_HS_AG_CHANNEL 12 + #define RING_INTERVAL 3000 typedef enum { @@ -102,6 +104,10 @@ struct headset { static DBusHandlerResult hs_disconnect(struct headset *hs, DBusMessage *msg); +static GIOChannel *hs_server = NULL; + +static uint32_t hs_record_id = 0; + static GSList *headsets = NULL; static DBusConnection *connection = NULL; @@ -496,7 +502,7 @@ static void auth_callback(DBusPendingCall *call, void *data) dbus_message_unref(reply); } -gboolean headset_server_io_cb(GIOChannel *chan, GIOCondition cond, void *data) +static gboolean headset_server_io_cb(GIOChannel *chan, GIOCondition cond, void *data) { int srv_sk, cli_sk; struct sockaddr_rc addr; @@ -952,7 +958,7 @@ static int create_ag_record(sdp_buf_t *buf, uint8_t ch) return ret; } -uint32_t headset_add_ag_record(uint8_t channel) +static uint32_t headset_add_ag_record(uint8_t channel) { DBusMessage *msg, *reply; DBusError derr; @@ -1668,13 +1674,95 @@ void headset_remove(char *path) g_free(path); } -void headset_init(DBusConnection *conn) +static GIOChannel *server_socket(uint8_t *channel) +{ + int sock, lm; + struct sockaddr_rc addr; + socklen_t sa_len; + GIOChannel *io; + + sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + if (sock < 0) { + error("server socket: %s (%d)", strerror(errno), errno); + return NULL; + } + + lm = RFCOMM_LM_SECURE; + if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm)) < 0) { + error("server setsockopt: %s (%d)", strerror(errno), errno); + close(sock); + return NULL; + } + + memset(&addr, 0, sizeof(addr)); + addr.rc_family = AF_BLUETOOTH; + bacpy(&addr.rc_bdaddr, BDADDR_ANY); + addr.rc_channel = 0; + + if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + error("server bind: %s", strerror(errno), errno); + close(sock); + return NULL; + } + + if (listen(sock, 1) < 0) { + error("server listen: %s", strerror(errno), errno); + close(sock); + return NULL; + } + + sa_len = sizeof(struct sockaddr_rc); + getsockname(sock, (struct sockaddr *) &addr, &sa_len); + *channel = addr.rc_channel; + + io = g_io_channel_unix_new(sock); + if (!io) { + error("Unable to allocate new io channel"); + close(sock); + return NULL; + } + + return io; +} + +int headset_init(DBusConnection *conn) { + uint8_t chan = DEFAULT_HS_AG_CHANNEL; + connection = dbus_connection_ref(conn); + + hs_server = server_socket(&chan); + if (!hs_server) + return -1; + + if (!hs_record_id) + hs_record_id = headset_add_ag_record(chan); + + if (!hs_record_id) { + error("Unable to register service record"); + g_io_channel_unref(hs_server); + hs_server = NULL; + return -1; + } + + g_io_add_watch(hs_server, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, + (GIOFunc) headset_server_io_cb, NULL); + + return 0; } void headset_exit(void) { + if (hs_record_id) { + headset_remove_ag_record(hs_record_id); + hs_record_id = 0; + } + + if (hs_server) { + g_io_channel_unref(hs_server); + hs_server = NULL; + } + dbus_connection_unref(connection); connection = NULL; } diff --git a/audio/headset.h b/audio/headset.h index b8582cef..888b53fe 100644 --- a/audio/headset.h +++ b/audio/headset.h @@ -33,13 +33,8 @@ char *headset_add(const bdaddr_t *bda); void headset_remove(char *path); -uint32_t headset_add_ag_record(uint8_t channel); +int headset_init(DBusConnection *conn); -int headset_remove_ag_record(uint32_t rec_id); - -gboolean headset_server_io_cb(GIOChannel *chan, GIOCondition cond, void *data); - -void headset_init(DBusConnection *conn); void headset_exit(void); #endif /* __AUDIO_HEADSET_H_ */ diff --git a/audio/main.c b/audio/main.c index 3cf609f6..470b26d5 100644 --- a/audio/main.c +++ b/audio/main.c @@ -73,19 +73,25 @@ int main(int argc, char *argv[]) exit(1); } - headset_init(conn); + if (audio_init(conn) < 0) { + error("Audio init failed!"); + exit(1); + } - audio_init(conn); + if (headset_init(conn) < 0) { + error("Headset initialization failed!"); + exit(1); + } if (argc > 1 && !strcmp(argv[1], "-s")) register_external_service(conn, "audio", "Audio service", ""); g_main_loop_run(main_loop); - audio_exit(); - headset_exit(); + audio_exit(); + dbus_connection_unref(conn); g_main_loop_unref(main_loop); diff --git a/audio/manager.c b/audio/manager.c index 3265c89e..07aa7b60 100644 --- a/audio/manager.c +++ b/audio/manager.c @@ -55,9 +55,6 @@ static DBusConnection *connection = NULL; -static GIOChannel *hs_server = NULL; - -static uint32_t hs_record_id; static char *default_hs = NULL; static GSList *headsets = NULL; @@ -137,84 +134,6 @@ static gboolean unix_event(GIOChannel *chan, GIOCondition cond, gpointer data) return TRUE; } -static GIOChannel *server_socket(uint8_t *channel) -{ - int sock, lm; - struct sockaddr_rc addr; - socklen_t sa_len; - GIOChannel *io; - - sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - if (sock < 0) { - error("server socket: %s (%d)", strerror(errno), errno); - return NULL; - } - - lm = RFCOMM_LM_SECURE; - if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm)) < 0) { - error("server setsockopt: %s (%d)", strerror(errno), errno); - close(sock); - return NULL; - } - - memset(&addr, 0, sizeof(addr)); - addr.rc_family = AF_BLUETOOTH; - bacpy(&addr.rc_bdaddr, BDADDR_ANY); - addr.rc_channel = 0; - - if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { - error("server bind: %s", strerror(errno), errno); - close(sock); - return NULL; - } - - if (listen(sock, 1) < 0) { - error("server listen: %s", strerror(errno), errno); - close(sock); - return NULL; - } - - sa_len = sizeof(struct sockaddr_rc); - getsockname(sock, (struct sockaddr *) &addr, &sa_len); - *channel = addr.rc_channel; - - io = g_io_channel_unix_new(sock); - if (!io) { - error("Unable to allocate new io channel"); - close(sock); - return NULL; - } - - return io; -} - -static gboolean manager_create_headset_server(uint8_t chan) -{ - if (hs_server) { - error("Server socket already created"); - return FALSE; - } - - hs_server = server_socket(&chan); - if (!hs_server) - return FALSE; - - if (!hs_record_id) - hs_record_id = headset_add_ag_record(chan); - - if (!hs_record_id) { - error("Unable to register service record"); - g_io_channel_unref(hs_server); - hs_server = NULL; - return FALSE; - } - - g_io_add_watch(hs_server, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, - (GIOFunc) headset_server_io_cb, NULL); - - return TRUE; -} - void manager_add_headset(char *path) { if (g_slist_find_custom(headsets, path, (GCompareFunc) strcmp)) @@ -478,8 +397,6 @@ int audio_init(DBusConnection *conn) connection = dbus_connection_ref(conn); - manager_create_headset_server(12); - return 0; } @@ -489,16 +406,6 @@ void audio_exit(void) unix_sock = -1; - if (hs_record_id) { - headset_remove_ag_record(hs_record_id); - hs_record_id = 0; - } - - if (hs_server) { - g_io_channel_unref(hs_server); - hs_server = NULL; - } - if (headsets) { g_slist_foreach(headsets, (GFunc) headset_remove, NULL); g_slist_free(headsets); |