Device Data Hub C API
Macros | Typedefs | Functions
subscriber.h File Reference

subscription API for Device Data Hub Client More...

#include "datahub/common.h"
Include dependency graph for subscriber.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DH_TOPIC_CREATED   ((DHTopicUpdateType)0)
 Topic was created.
 
#define DH_TOPIC_DELETED   ((DHTopicUpdateType)1)
 Topic was deleted.
 
#define DH_TOPIC_INSTANCE_CREATED   ((DHTopicInstanceUpdateType)0)
 Topic instance was created.
 
#define DH_TOPIC_INSTANCE_DELETED   ((DHTopicInstanceUpdateType)1)
 Topic instance was deleted.
 
#define DH_START_FROM_NOW    ((DHStartFrom)0)
 Start receiving only new data updates from the time of subscription.
 
#define DH_START_FROM_OLDEST    ((DHStartFrom)1)
 Start receiving data updates from the oldest available data.
 

Typedefs

typedef uint32_t DHTopicUpdateType
 Topic update type enumeration. More...
 
typedef uint32_t DHTopicInstanceUpdateType
 Topic instance update type enumeration. More...
 
typedef struct DHSubscriber_t DHSubscriber
 Opaque subscriber handle. More...
 
typedef void(* DHOnTopicUpdateCallback) (const char *topic_name, DHTopicUpdateType update_type, void *user_data)
 Topic update callback function type. More...
 
typedef void(* DHOnInstanceUpdateCallback) (const DHTopicInstance *topic_instance, DHTopicInstanceUpdateType update_type, void *user_data)
 Instance update callback function type. More...
 
typedef void(* DHOnDataCallback) (const DHTopicSample *topic_sample, void *user_data)
 Data update callback function type. More...
 
typedef struct DHFilter_t DHFilter
 Opaque subscription filter. More...
 
typedef uint32_t DHStartFrom
 
typedef struct DHSubscribeOptions_t DHSubscribeOptions
 Opaque subscription options. More...
 

Functions

DHFilter * dh_filter_create (void)
 Creates an empty filter. More...
 
void dh_filter_destroy (DHFilter *filter)
 Destroys a filter. More...
 
bool dh_filter_add_topic_name (DHFilter *filter, const char *topic_name, DHError **error)
 Adds a topic name to the filter. A sample matches if its topic name matches any of the added topic names (logical OR). More...
 
bool dh_filter_add_instance (DHFilter *filter, const DHInstanceKeys *instance_keys, DHError **error)
 Adds an instance key filter. A sample matches if it matches any of the added instance key filters (logical OR). Each instance key filter can contain multiple key-value pairs that must all match (logical AND). More...
 
bool dh_filter_add_data_expression (DHFilter *filter, const char *data_expression, DHError **error)
 Adds a data expression to the filter. A sample matches if it matches any of the data expressions. The data expression is an expression string applied to each incoming sample to filter based on sample data content. Multiple expressions are logically OR-ed: a sample passes if it satisfies any of the added expressions. The expression is a limited subset of operators. The following operators are supported: (), ==, !=, >, >=, <=, <, and, or, not, has(). Data fields can be accessed using . notation, e.g. .field.subfield. More...
 
DHSubscribeOptions * dh_subscribe_options_create (void)
 Creates a subscription options object with default settings. More...
 
void dh_subscribe_options_destroy (DHSubscribeOptions *options)
 Destroys a subscription options object. More...
 
bool dh_subscribe_options_add_filter (DHSubscribeOptions *options, const DHFilter *filter, DHError **error)
 Adds a filter to the subscription options. Each filter is evaluated independently and matches if any filter matches (logical OR). More...
 
void dh_subscribe_options_set_enable_topic_updates (DHSubscribeOptions *options, bool enable)
 Enables or disables topic creation/deletion notifications. More...
 
void dh_subscribe_options_set_enable_instance_updates (DHSubscribeOptions *options, bool enable)
 Enables or disables instance creation/deletion notifications. More...
 
void dh_subscribe_options_set_enable_data_updates (DHSubscribeOptions *options, bool enable)
 Enables or disables data update notifications. More...
 
void dh_subscribe_options_set_start_from (DHSubscribeOptions *options, DHStartFrom start_from)
 Sets the starting point for data delivery. More...
 
void dh_subscriber_destroy (DHSubscriber *subscriber)
 Destroys a subscriber. More...
 
const char * dh_subscriber_get_name (const DHSubscriber *subscriber)
 Gets the name of a subscriber. More...
 
bool dh_subscriber_set_topic_update_callback (DHSubscriber *subscriber, DHOnTopicUpdateCallback on_topic_update, void *user_data, DHError **error)
 Sets the topic update callback. More...
 
bool dh_subscriber_set_instance_update_callback (DHSubscriber *subscriber, DHOnInstanceUpdateCallback on_instance_update, void *user_data, DHError **error)
 Sets the instance update callback. More...
 
bool dh_subscriber_set_data_callback (DHSubscriber *subscriber, DHOnDataCallback on_data, void *user_data, DHError **error)
 Sets the data callback. More...
 
bool dh_subscriber_subscribe (DHSubscriber *subscriber, const DHSubscribeOptions *options, DHError **error)
 Subscribes to one or more topics with optional instance and data filtering. More...
 
bool dh_subscriber_unsubscribe (DHSubscriber *subscriber, DHError **error)
 Unsubscribes from all topics. More...
 

Detailed Description

subscription API for Device Data Hub Client

This file provides the subscription functionality for the Device Data Hub Client library. It enables applications to receive real-time data updates from topics without polling.

The subscription system provides the following subscriber type:

DHSubscriber uses a callback-based approach where the application registers listener functions that are invoked when matching data is received. Callbacks are executed on internal library threads — keep them lightweight and thread-safe.

Each subscription can be configured to receive any combination of:

Content filtering can be applied to receive only data matching specific criteria.

Typedef Documentation

◆ DHFilter

typedef struct DHFilter_t DHFilter

Opaque subscription filter.

Represents a single filter used to match incoming samples. A filter carries three independent criteria — topic names, instance key sets, and data expressions — that are each logically OR-ed within the filter: a sample passes if its topic name matches any added topic name, its instance matches any added instance key set, or its data satisfies any added data expression.

Create with dh_filter_create() and destroy with dh_filter_destroy().

◆ DHOnDataCallback

typedef void(* DHOnDataCallback) (const DHTopicSample *topic_sample, void *user_data)

Data update callback function type.

Called when new data matching the subscription filters is received. This is the primary callback for receiving topic data.

Parameters
topic_sampleThe received topic sample (must not be NULL)
user_dataUser-provided context pointer

◆ DHOnInstanceUpdateCallback

typedef void(* DHOnInstanceUpdateCallback) (const DHTopicInstance *topic_instance, DHTopicInstanceUpdateType update_type, void *user_data)

Instance update callback function type.

Called when a topic instance matching the subscription filters is created or deleted.

Parameters
topic_instanceThe topic instance that was updated (must not be NULL)
update_typeWhether the instance was created or deleted
user_dataUser-provided context pointer

◆ DHOnTopicUpdateCallback

typedef void(* DHOnTopicUpdateCallback) (const char *topic_name, DHTopicUpdateType update_type, void *user_data)

Topic update callback function type.

Called when a topic matching the subscription filters is created or deleted.

Parameters
topic_nameName of the topic that was updated (must not be NULL)
update_typeWhether the topic was created or deleted
user_dataUser-provided context pointer

◆ DHSubscribeOptions

typedef struct DHSubscribeOptions_t DHSubscribeOptions

Opaque subscription options.

Aggregates one or more DHFilter objects and update-enable flags into a single configuration passed to a subscribe call. Multiple filters are logically OR-ed: a sample matches if any filter matches.

Create with dh_subscribe_options_create() and destroy with dh_subscribe_options_destroy().

◆ DHSubscriber

typedef struct DHSubscriber_t DHSubscriber

Opaque subscriber handle.

Represents a subscriber for receiving data from one or more topics simultaneously.

◆ DHTopicInstanceUpdateType

typedef uint32_t DHTopicInstanceUpdateType

Topic instance update type enumeration.

Indicates whether a topic instance was created or deleted. Used in topic instance update callbacks.

◆ DHTopicUpdateType

typedef uint32_t DHTopicUpdateType

Topic update type enumeration.

Indicates whether a topic was created or deleted. Used in topic update callbacks.

Function Documentation

◆ dh_filter_add_data_expression()

bool dh_filter_add_data_expression ( DHFilter *  filter,
const char *  data_expression,
DHError **  error 
)

Adds a data expression to the filter. A sample matches if it matches any of the data expressions. The data expression is an expression string applied to each incoming sample to filter based on sample data content. Multiple expressions are logically OR-ed: a sample passes if it satisfies any of the added expressions. The expression is a limited subset of operators. The following operators are supported: (), ==, !=, >, >=, <=, <, and, or, not, has(). Data fields can be accessed using . notation, e.g. .field.subfield.

Parameters
filterThe filter to modify (must not be NULL)
data_expressionThe data expression (must not be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

◆ dh_filter_add_instance()

bool dh_filter_add_instance ( DHFilter *  filter,
const DHInstanceKeys *  instance_keys,
DHError **  error 
)

Adds an instance key filter. A sample matches if it matches any of the added instance key filters (logical OR). Each instance key filter can contain multiple key-value pairs that must all match (logical AND).

Parameters
filterThe filter to modify (must not be NULL)
instance_keysThe instance keys to add as a filter (must not be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

◆ dh_filter_add_topic_name()

bool dh_filter_add_topic_name ( DHFilter *  filter,
const char *  topic_name,
DHError **  error 
)

Adds a topic name to the filter. A sample matches if its topic name matches any of the added topic names (logical OR).

Parameters
filterThe filter to modify (must not be NULL)
topic_nameThe topic name to add (must not be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

◆ dh_filter_create()

DHFilter* dh_filter_create ( void  )

Creates an empty filter.

Returns
New DHFilter with no criteria set, or NULL on memory allocation failure

◆ dh_filter_destroy()

void dh_filter_destroy ( DHFilter *  filter)

Destroys a filter.

Frees all resources associated with the filter.

Parameters
filterThe filter to destroy (can be NULL)

◆ dh_subscribe_options_add_filter()

bool dh_subscribe_options_add_filter ( DHSubscribeOptions *  options,
const DHFilter *  filter,
DHError **  error 
)

Adds a filter to the subscription options. Each filter is evaluated independently and matches if any filter matches (logical OR).

Parameters
optionsThe subscription options (must not be NULL)
filterThe filter to add (must not be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

◆ dh_subscribe_options_create()

DHSubscribeOptions* dh_subscribe_options_create ( void  )

Creates a subscription options object with default settings.

Default: no filters, all update types disabled, start from now.

Returns
New DHSubscribeOptions, or NULL on memory allocation failure

◆ dh_subscribe_options_destroy()

void dh_subscribe_options_destroy ( DHSubscribeOptions *  options)

Destroys a subscription options object.

Parameters
optionsThe options to destroy (can be NULL)

◆ dh_subscribe_options_set_enable_data_updates()

void dh_subscribe_options_set_enable_data_updates ( DHSubscribeOptions *  options,
bool  enable 
)

Enables or disables data update notifications.

Parameters
optionsThe options to modify (must not be NULL)
enabletrue to enable, false to disable

◆ dh_subscribe_options_set_enable_instance_updates()

void dh_subscribe_options_set_enable_instance_updates ( DHSubscribeOptions *  options,
bool  enable 
)

Enables or disables instance creation/deletion notifications.

Parameters
optionsThe options to modify (must not be NULL)
enabletrue to enable, false to disable

◆ dh_subscribe_options_set_enable_topic_updates()

void dh_subscribe_options_set_enable_topic_updates ( DHSubscribeOptions *  options,
bool  enable 
)

Enables or disables topic creation/deletion notifications.

Parameters
optionsThe options to modify (must not be NULL)
enabletrue to enable, false to disable

◆ dh_subscribe_options_set_start_from()

void dh_subscribe_options_set_start_from ( DHSubscribeOptions *  options,
DHStartFrom  start_from 
)

Sets the starting point for data delivery.

Parameters
optionsThe options to modify (must not be NULL)
start_fromDH_START_FROM_NOW or DH_START_FROM_OLDEST

◆ dh_subscriber_destroy()

void dh_subscriber_destroy ( DHSubscriber *  subscriber)

Destroys a subscriber.

Automatically unsubscribes if still subscribed and frees all resources associated with the subscriber.

Parameters
subscriberThe subscriber to destroy (can be NULL)

◆ dh_subscriber_get_name()

const char* dh_subscriber_get_name ( const DHSubscriber *  subscriber)

Gets the name of a subscriber.

Returns the name that was specified when the subscriber was created.

Parameters
subscriberThe subscriber (must not be NULL)
Returns
The subscriber name, or NULL if invalid

◆ dh_subscriber_set_data_callback()

bool dh_subscriber_set_data_callback ( DHSubscriber *  subscriber,
DHOnDataCallback  on_data,
void *  user_data,
DHError **  error 
)

Sets the data callback.

Registers a callback to be invoked when a data sample matching the subscription is received. Pass NULL to remove the callback.

Parameters
subscriberThe subscriber (must not be NULL)
on_dataCallback function (can be NULL to remove)
user_dataOpaque pointer passed to the callback on each invocation (can be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

◆ dh_subscriber_set_instance_update_callback()

bool dh_subscriber_set_instance_update_callback ( DHSubscriber *  subscriber,
DHOnInstanceUpdateCallback  on_instance_update,
void *  user_data,
DHError **  error 
)

Sets the instance update callback.

Registers a callback to be invoked when a topic instance matching the subscription is created or deleted. Pass NULL to remove the callback.

Parameters
subscriberThe subscriber (must not be NULL)
on_instance_updateCallback function (can be NULL to remove)
user_dataOpaque pointer passed to the callback on each invocation (can be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

◆ dh_subscriber_set_topic_update_callback()

bool dh_subscriber_set_topic_update_callback ( DHSubscriber *  subscriber,
DHOnTopicUpdateCallback  on_topic_update,
void *  user_data,
DHError **  error 
)

Sets the topic update callback.

Registers a callback to be invoked when a topic matching the subscription is created or deleted. Pass NULL to remove the callback.

Parameters
subscriberThe subscriber (must not be NULL)
on_topic_updateCallback function (can be NULL to remove)
user_dataOpaque pointer passed to the callback on each invocation (can be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

◆ dh_subscriber_subscribe()

bool dh_subscriber_subscribe ( DHSubscriber *  subscriber,
const DHSubscribeOptions *  options,
DHError **  error 
)

Subscribes to one or more topics with optional instance and data filtering.

Establishes a subscription to receive data from one or more topics, with optional filtering by instance keys and/or a data filter expression.

Parameters
subscriberThe subscriber (must not be NULL)
optionsSubscription options specifying filters and update types (must not be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

Possible error codes:

  • DH_ERR_INVALID_PARAMS
  • DH_ERR_INVALID_DATA
  • DH_ERR_INVALID_REQUEST
  • DH_ERR_NOT_CONNECTED
  • DH_ERR_MAX_SUBSCRIPTIONS
  • DH_ERROR_DATA_TOO_BIG
  • DH_ERR_INTERNAL_ERROR

◆ dh_subscriber_unsubscribe()

bool dh_subscriber_unsubscribe ( DHSubscriber *  subscriber,
DHError **  error 
)

Unsubscribes from all topics.

Terminates all active subscriptions for this subscriber. No further callbacks will be received after this call completes successfully.

Parameters
subscriberThe subscriber (must not be NULL)
errorOutput parameter for error details on failure (can be NULL to ignore)
Returns
true on success, false on failure

Possible error codes:

  • DH_ERR_INVALID_REQUEST
  • DH_ERR_NOT_CONNECTED
  • DH_ERR_INVALID_SUBSCRIPTION
  • DH_ERR_INTERNAL_ERROR