Skip to content

Commit

Permalink
Fix glib source watch removal in BLE module
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Apr 4, 2023
1 parent 1053da7 commit 38aafdb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
41 changes: 15 additions & 26 deletions src/platform/Linux/bluez/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,6 @@ static gboolean BluezCharacteristicWriteFD(GIOChannel * aChannel, GIOCondition a
isSuccess = true;

exit:
if (!isSuccess && (conn != nullptr))
{
// Returning G_SOURCE_REMOVE from the source callback removes the source object
// from the context. Unset self source ID tag, so we will not call g_source_remove()
// in BluezOTConnectionDestroy() on already removed source.
//
// TODO: Investigate whether there is a batter way to handle this.
conn->mC1Channel.mWatch = 0;
}

return isSuccess ? G_SOURCE_CONTINUE : G_SOURCE_REMOVE;
}

Expand All @@ -425,15 +415,8 @@ static void Bluez_gatt_characteristic1_complete_acquire_write_with_fd(GDBusMetho
fd_list);
}

static gboolean bluezCharacteristicDestroyFD(GIOChannel * aChannel, GIOCondition aCond, gpointer apClosure)
static gboolean bluezCharacteristicDestroyFD(GIOChannel * aChannel, GIOCondition aCond, gpointer apEndpoint)
{
BluezConnection * conn = static_cast<BluezConnection *>(apClosure);
// Returning G_SOURCE_REMOVE from the source callback removes the source object
// from the context. Unset self source ID tag, so we will not call g_source_remove()
// in BluezOTConnectionDestroy() on already removed source.
//
// TODO: Investigate whether there is a batter way to handle this.
conn->mC2Channel.mWatch = 0;
return G_SOURCE_REMOVE;
}

Expand Down Expand Up @@ -490,8 +473,8 @@ static gboolean BluezCharacteristicAcquireWrite(BluezGattCharacteristic1 * aChar

watchSource = g_io_create_watch(channel, static_cast<GIOCondition>(G_IO_HUP | G_IO_IN | G_IO_ERR | G_IO_NVAL));
g_source_set_callback(watchSource, G_SOURCE_FUNC(BluezCharacteristicWriteFD), conn, nullptr);
conn->mC1Channel.mWatch = PlatformMgrImpl().GLibMatterContextAttachSource(watchSource);
g_source_unref(watchSource);
PlatformMgrImpl().GLibMatterContextAttachSource(watchSource);
conn->mC1Channel.mWatchSource = watchSource;

bluez_gatt_characteristic1_set_write_acquired(aChar, TRUE);

Expand Down Expand Up @@ -561,8 +544,8 @@ static gboolean BluezCharacteristicAcquireNotify(BluezGattCharacteristic1 * aCha

watchSource = g_io_create_watch(channel, static_cast<GIOCondition>(G_IO_HUP | G_IO_ERR | G_IO_NVAL));
g_source_set_callback(watchSource, G_SOURCE_FUNC(bluezCharacteristicDestroyFD), conn, nullptr);
conn->mC2Channel.mWatch = PlatformMgrImpl().GLibMatterContextAttachSource(watchSource);
g_source_unref(watchSource);
PlatformMgrImpl().GLibMatterContextAttachSource(watchSource);
conn->mC1Channel.mWatchSource = watchSource;

bluez_gatt_characteristic1_set_notify_acquired(aChar, TRUE);

Expand Down Expand Up @@ -795,12 +778,18 @@ static void BluezOTConnectionDestroy(BluezConnection * aConn)
g_object_unref(aConn->mpC2);
if (aConn->mpPeerAddress)
g_free(aConn->mpPeerAddress);
if (aConn->mC1Channel.mWatch > 0)
g_source_remove(aConn->mC1Channel.mWatch);
if (aConn->mC1Channel.mWatchSource)
{
g_source_destroy(aConn->mC1Channel.mWatchSource);
g_source_unref(aConn->mC1Channel.mWatchSource);
}
if (aConn->mC1Channel.mpChannel)
g_io_channel_unref(aConn->mC1Channel.mpChannel);
if (aConn->mC2Channel.mWatch > 0)
g_source_remove(aConn->mC2Channel.mWatch);
if (aConn->mC2Channel.mWatchSource)
{
g_source_destroy(aConn->mC2Channel.mWatchSource);
g_source_unref(aConn->mC2Channel.mWatchSource);
}
if (aConn->mC2Channel.mpChannel)
g_io_channel_unref(aConn->mC2Channel.mpChannel);

Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/bluez/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct BluezAddress
struct IOChannel
{
GIOChannel * mpChannel;
guint mWatch;
GSource * mWatchSource;
};

struct BluezEndpoint
Expand Down

0 comments on commit 38aafdb

Please sign in to comment.