gattlib/dbus/gattlib_stream.c

98 lines
2.5 KiB
C
Raw Normal View History

2019-07-09 21:56:51 +02:00
/*
2021-09-01 00:00:36 +02:00
* SPDX-License-Identifier: BSD-3-Clause
2019-07-09 21:56:51 +02:00
*
2024-02-20 23:53:45 +01:00
* Copyright (c) 2016-2024, Olivier Martin <olivier@labapart.org>
2019-07-09 21:56:51 +02:00
*/
2024-02-20 23:53:45 +01:00
#include <errno.h>
2019-07-09 21:56:51 +02:00
#include <gio/gunixfdlist.h>
#include "gattlib_internal.h"
#if BLUEZ_VERSION < BLUEZ_VERSIONS(5, 48)
int gattlib_write_char_by_uuid_stream_open(gattlib_connection_t* connection, uuid_t* uuid, gattlib_stream_t **stream, uint16_t *mtu)
2019-07-09 21:56:51 +02:00
{
return GATTLIB_NOT_SUPPORTED;
}
int gattlib_write_char_stream_write(gattlib_stream_t *stream, const void *buffer, size_t buffer_len)
2019-07-09 21:56:51 +02:00
{
return GATTLIB_NOT_SUPPORTED;
}
int gattlib_write_char_stream_close(gattlib_stream_t *stream)
2019-07-09 21:56:51 +02:00
{
return GATTLIB_NOT_SUPPORTED;
}
#else
int gattlib_write_char_by_uuid_stream_open(gattlib_connection_t* connection, uuid_t* uuid, gattlib_stream_t **stream, uint16_t *mtu)
2019-07-09 21:56:51 +02:00
{
GError *error = NULL;
GUnixFDList *fd_list;
GVariant *out_fd;
int ret;
2019-07-09 21:56:51 +02:00
int fd;
2024-04-05 13:25:51 +02:00
//
// No need of locking the gattlib mutex. get_characteristic_from_uuid() is taking care of the gattlib
// object coherency. And 'dbus_characteristic' is not linked to gattlib object
//
struct dbus_characteristic dbus_characteristic = get_characteristic_from_uuid(connection, uuid);
2019-07-09 21:56:51 +02:00
GVariantBuilder *variant_options = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
org_bluez_gatt_characteristic1_call_acquire_write_sync(
dbus_characteristic.gatt,
g_variant_builder_end(variant_options),
NULL /* fd_list */,
&out_fd, mtu,
&fd_list,
NULL /* cancellable */, &error);
2019-07-18 20:26:16 +02:00
g_variant_builder_unref(variant_options);
2019-07-09 21:56:51 +02:00
if (error != NULL) {
ret = GATTLIB_ERROR_DBUS_WITH_ERROR(error);
2021-10-18 01:25:29 +02:00
GATTLIB_LOG(GATTLIB_ERROR, "Failed to acquired write DBus GATT characteristic: %s", error->message);
2019-07-09 21:56:51 +02:00
g_error_free(error);
return ret;
2019-07-09 21:56:51 +02:00
}
error = NULL;
fd = g_unix_fd_list_get(fd_list, g_variant_get_handle(out_fd), &error);
if (error != NULL) {
ret = GATTLIB_ERROR_DBUS_WITH_ERROR(error);
2021-10-18 01:25:29 +02:00
GATTLIB_LOG(GATTLIB_ERROR, "Failed to retrieve Unix File Descriptor: %s", error->message);
2019-07-09 21:56:51 +02:00
g_error_free(error);
return ret;
2019-07-09 21:56:51 +02:00
}
// We abuse the pointer 'stream' to pass the 'File Descriptor'
*stream = (gattlib_stream_t*)(unsigned long)fd;
2019-07-09 21:56:51 +02:00
return GATTLIB_SUCCESS;
}
int gattlib_write_char_stream_write(gattlib_stream_t *stream, const void *buffer, size_t buffer_len)
2019-07-09 21:56:51 +02:00
{
2024-02-20 23:53:45 +01:00
ssize_t ret = write((unsigned long)stream, buffer, buffer_len);
if (ret < 0) {
return GATTLIB_ERROR_UNIX_WITH_ERROR(errno);
} else {
return GATTLIB_SUCCESS;
}
2019-07-09 21:56:51 +02:00
}
int gattlib_write_char_stream_close(gattlib_stream_t *stream)
2019-07-09 21:56:51 +02:00
{
close((unsigned long)stream);
return GATTLIB_SUCCESS;
}
#endif /* #if BLUEZ_VERSION < BLUEZ_VERSIONS(5, 48) */