diff --git a/include/libdivecomputer/custom.h b/include/libdivecomputer/custom.h index 4e3f9b0..bccc882 100644 --- a/include/libdivecomputer/custom.h +++ b/include/libdivecomputer/custom.h @@ -45,6 +45,7 @@ typedef struct dc_custom_cbs_t { dc_status_t (*purge) (void *userdata, dc_direction_t direction); dc_status_t (*sleep) (void *userdata, unsigned int milliseconds); dc_status_t (*close) (void *userdata); + const char *(*get_name) (void *userdata); } dc_custom_cbs_t; /** diff --git a/include/libdivecomputer/iostream.h b/include/libdivecomputer/iostream.h index fe3b673..3b25460 100644 --- a/include/libdivecomputer/iostream.h +++ b/include/libdivecomputer/iostream.h @@ -283,6 +283,19 @@ dc_iostream_sleep (dc_iostream_t *iostream, unsigned int milliseconds); dc_status_t dc_iostream_close (dc_iostream_t *iostream); +/** + * Get the name of the device + * + * @param[in] iostream A valid I/O stream. + * @returns a NUL-terminated constant C string that has a lifetime + * until the close of the iostream, or NUL if no name exists. + * + * The name depends on the iostream transport. For a BLE device, it + * is the name of the device during device discovery. + */ +const char * +dc_iostream_get_name (dc_iostream_t *iostream); + dc_status_t dc_usb_storage_open (dc_iostream_t **out, dc_context_t *context, const char *name); diff --git a/src/custom.c b/src/custom.c index 8db276b..dc24347 100644 --- a/src/custom.c +++ b/src/custom.c @@ -41,6 +41,7 @@ static dc_status_t dc_custom_flush (dc_iostream_t *abstract); static dc_status_t dc_custom_purge (dc_iostream_t *abstract, dc_direction_t direction); static dc_status_t dc_custom_sleep (dc_iostream_t *abstract, unsigned int milliseconds); static dc_status_t dc_custom_close (dc_iostream_t *abstract); +static const char *dc_custom_get_name (dc_iostream_t *abstract); typedef struct dc_custom_t { /* Base class. */ @@ -66,6 +67,7 @@ static const dc_iostream_vtable_t dc_custom_vtable = { dc_custom_purge, /* purge */ dc_custom_sleep, /* sleep */ dc_custom_close, /* close */ + dc_custom_get_name, /* get_name */ }; dc_status_t @@ -246,3 +248,14 @@ dc_custom_close (dc_iostream_t *abstract) return custom->callbacks.close (custom->userdata); } + +static const char * +dc_custom_get_name (dc_iostream_t *abstract) +{ + dc_custom_t *custom = (dc_custom_t *) abstract; + + if (custom->callbacks.get_name == NULL) + return NULL; + + return custom->callbacks.get_name (custom->userdata); +} diff --git a/src/iostream-private.h b/src/iostream-private.h index f9c0883..42c5d5f 100644 --- a/src/iostream-private.h +++ b/src/iostream-private.h @@ -68,6 +68,8 @@ struct dc_iostream_vtable_t { dc_status_t (*sleep) (dc_iostream_t *iostream, unsigned int milliseconds); dc_status_t (*close) (dc_iostream_t *iostream); + + const char *(*get_name) (dc_iostream_t *iostream); }; dc_iostream_t * diff --git a/src/iostream.c b/src/iostream.c index e901341..9588f0e 100644 --- a/src/iostream.c +++ b/src/iostream.c @@ -305,3 +305,15 @@ dc_iostream_close (dc_iostream_t *iostream) return status; } + +const char * +dc_iostream_get_name (dc_iostream_t *iostream) +{ + if (iostream == NULL) + return NULL; + + if (iostream->vtable->get_name) + return iostream->vtable->get_name (iostream); + + return NULL; +}