Google Cloud IoT Device SDK for Embedded C API
Functions | Variables
iotc.h File Reference
#include <stdint.h>
#include <stdlib.h>
#include <iotc_connection_data.h>
#include <iotc_mqtt.h>
#include <iotc_time.h>
#include <iotc_types.h>

Go to the source code of this file.

Functions

iotc_state_t iotc_initialize ()
 
iotc_state_t iotc_shutdown ()
 
iotc_context_handle_t iotc_create_context ()
 
iotc_state_t iotc_delete_context (iotc_context_handle_t context_handle)
 
uint8_t iotc_is_context_connected (iotc_context_handle_t context_handle)
 
void iotc_events_process_blocking ()
 
iotc_state_t iotc_events_process_tick ()
 
void iotc_events_stop ()
 
iotc_state_t iotc_connect (iotc_context_handle_t iotc_h, const char *username, const char *password, const char *client_id, uint16_t connection_timeout, uint16_t keepalive_timeout, iotc_user_callback_t *client_callback)
 
iotc_state_t iotc_connect_to (iotc_context_handle_t iotc_h, const char *host, uint16_t port, const char *username, const char *password, const char *client_id, uint16_t connection_timeout, uint16_t keepalive_timeout, iotc_user_callback_t *client_callback)
 
iotc_state_t iotc_publish (iotc_context_handle_t iotc_h, const char *topic, const char *msg, const iotc_mqtt_qos_t qos, iotc_user_callback_t *callback, void *user_data)
 
iotc_state_t iotc_publish_data (iotc_context_handle_t iotc_h, const char *topic, const uint8_t *data, size_t data_len, const iotc_mqtt_qos_t qos, iotc_user_callback_t *callback, void *user_data)
 
iotc_state_t iotc_subscribe (iotc_context_handle_t iotc_h, const char *topic, const iotc_mqtt_qos_t qos, iotc_user_subscription_callback_t *callback, void *user_data)
 
iotc_state_t iotc_shutdown_connection (iotc_context_handle_t iotc_h)
 
iotc_timed_task_handle_t iotc_schedule_timed_task (iotc_context_handle_t iotc_h, iotc_user_task_callback_t *callback, const iotc_time_t seconds_from_now, const uint8_t repeats_forever, void *data)
 
void iotc_cancel_timed_task (iotc_timed_task_handle_t timed_task_handle)
 
void iotc_set_network_timeout (uint32_t timeout)
 
uint32_t iotc_get_network_timeout (void)
 
iotc_state_t iotc_set_maximum_heap_usage (const size_t max_bytes)
 
iotc_state_t iotc_get_heap_usage (size_t *const heap_usage)
 

Variables

const uint16_t iotc_major
 
const uint16_t iotc_minor
 
const uint16_t iotc_revision
 
const char iotc_cilent_version_str []
 

Detailed Description

The main API for compiling your Application against the the Google Cloud IoT Device SDK for Embedded C (IoTC).

Definition in file iotc.h.

Function Documentation

◆ iotc_cancel_timed_task()

void iotc_cancel_timed_task ( iotc_timed_task_handle_t  timed_task_handle)

Cancel a timed task This function cancels the timed execution of the task specified by the given handle.

Parameters
[in]timed_task_handlea handle returned by iotc_schedule_timed_task.
See also
iotc_create_context
iotc_schedule_timed_task

◆ iotc_connect()

iotc_state_t iotc_connect ( iotc_context_handle_t  iotc_h,
const char *  username,
const char *  password,
const char *  client_id,
uint16_t  connection_timeout,
uint16_t  keepalive_timeout,
iotc_user_callback_t client_callback 
)

Opens a connection to the Google Cloud IoT Core service using the provided context.

Includes a callback. Using the provided context, this function requests that a connection be made to the Google Cloud IoT Core service given the provided credentials. The callback method is a connection state monitor callback, and will be invoked when this connection attempt completes successfully, has failed, or after disconnection if a connection was established.

The callback function is type defined by iotc_user_callback_t, which has the following signature: void foo( iotc_context_handle_t in_context_handle , void* data , iotc_state_t state )

where the callback parameters are:

  • in_context_handle is the context handle that you provided to iotc_connect
  • data is a multifunctional structure. Please see the IoTC User Guide or examples/ for More Information.
  • state should be IOTC_STATE_OK if the connection succeeded. For other error codes please see the IoTC User Guide or Examples.
Parameters
[in]iotc_ha context handle created by invoking iotc_create_context.
[in]usernamethe MQTT Connect Username. For IoT Core, this parameter is unused.
[in]passwordthe MQTT Connect Password. For IoT Core, this should be the IoT Core Connecting JWT created by the function iotc_create_iotcore_jwt.
[in]client_idthe MQTT connect client identifier. For IoT Core, this must be the device_path of your device. Please see https://cloud.google.com/iot/docs/how-tos/mqtt-bridge which details how to construct the device path string.
[in]connection_timeoutNumber of seconds that the socket will be kept before CONNACK without data coming from the server. In case of 0, the TCP timeout will be used.
[in]keepalive_timeoutNumber of seconds that the socket will be kept connected while being unused.
See also
iotc_create_context
iotc_create_iotcore_jwt
Return values
IOTC_STATE_OKIf the connection request was formatted correctly and the client is processing the request with IoT Core.

◆ iotc_connect_to()

iotc_state_t iotc_connect_to ( iotc_context_handle_t  iotc_h,
const char *  host,
uint16_t  port,
const char *  username,
const char *  password,
const char *  client_id,
uint16_t  connection_timeout,
uint16_t  keepalive_timeout,
iotc_user_callback_t client_callback 
)

Opens a MQTT connection to a custom service endpoint using the provided context, host and port.

Parameters
[in]hostclient will connect to this address
[in]portclient will connect to the host on this port

This function's behavior and other parameters are identical to function iotc_connect.

See also
iotc_connect

◆ iotc_create_context()

iotc_context_handle_t iotc_create_context ( )

Creates a connection context for subscriptions and publications This should by invoked following a successful libiotc initialization.

This creates a specific context that can be passed to connection, subscription, and publish functions.

See also
iotc_initialize
iotc_delete_context
Return values
nonnegativenumber A valid context handle.
negativenumber A negated error code, defined in iotc_error.h.

◆ iotc_delete_context()

iotc_state_t iotc_delete_context ( iotc_context_handle_t  context_handle)

Frees the provided connection context This should by invoked to free up memory when your applicaiton has disconnected from the Google Cloud IoT Core service through the given context.

You may reuse a disconnected context to attempt to reconnect to the service; you do not need to destroy a disconnected context and create a new one.

NOTE: Please do not delete the context in the disconnection callback itself. Doing so many cause data corruption or a crash after the connect callback returns. Instead, delete the context either on the next tick of the client event loop in iotc_process_events_tick, or on POSIX, after iotc_process_blocking returns due to a call to iotc_events_stop.

Parameters
[in]contexthandle that should be freed.
See also
iotc_create_context
Return values
IOTC_STATE_OKStatus OK
IOTC_INVALID_PARAMETERIf the provided context handle is invalid.

◆ iotc_events_process_blocking()

void iotc_events_process_blocking ( )

Invokes the IoTC Client Event Processing loop.

This function is meant to be execute the IoTC Client as the main application process and therefore it does not return until iotc_events_stop is invoked.

If you're executing on a platform that cannot block indefinitely, then please use iotc_events_process_tick instead.

NOTE: if the client is in a state of IOTC_EVENT_PROCESS_STOPPED, then the client will need to be destroyed and reiniitalized before the event engine will properly process events again. Therefore, stopping the event system should be used sparringly, most likely in the standard process of a reboot operation, or on POSIX, the standard process of client application shutdown.

See also
iotc_events_process_tick
iotc_events_stop

◆ iotc_events_process_tick()

iotc_state_t iotc_events_process_tick ( )

Invokes the IOTC Client Event Processing loop.

This function will begin to process any pending tasks but then return control to the client application.

This function should be used on Embedded devices only. It is meant for RTOS or No OS devices which require that control be yielded to the OS for standard tick operations.

NOTE: This function WILL BLOCK on standard UNIX devices like LINUX or MAC OSX.

NOTE: if the client is in a state of IOTC_EVENT_PROCESS_STOPPED, then the client will need to be destroyed and reiniitalized before the event engine will properly process events again. Therefore, stopping the event system should be used sparringly, most likely in the standard process of a reboot operation, or on POSIX, the standard process of client application shutdown.

IOTC_STATE_OK will be returned if the event system is ongoing and can continue to operate

IOTC_EVENT_PROCESS_STOPPED will be returned if iotc_events_stop has been invoked by the client application or if the event processor has been stopped due to an unrecoverable error.

See also
iotc_events_process_blocking
iotc_events_stop
Return values
IOTC_STATE_OKStatus OK.
IOTC_EVENT_PROCESS_STOPPEDIf the event processor has been stopped.

◆ iotc_events_stop()

void iotc_events_stop ( )

Causes the IoTC Client event loop to exit.

Used to exit the blocking event loop, or to signal that an irrecoverable event has occurred.

See also
iotc_events_process_blocking
iotc_events_process_tick

◆ iotc_get_heap_usage()

iotc_state_t iotc_get_heap_usage ( size_t *const  heap_usage)

Fetches the IoTC Client's current amount of heap usage This function is part of an optional configuration of the IoTC Client called the Memory Limiter.

If enabled, you can use this function to determine the current heap usage of the IoTC Client. Depending on the TLS implementation, this might also include the TLS buffers used for encoding / decoding and certificate parsing.

Return values
IOTC_NOT_SUPPORTEDIf the Memory Limiter module has not been compiled into this version of the IoTC Client Library
IOTC_INVALID_PARAMETERIf the provided parameter is NULL
IOTC_STATE_OKif the parameter has been filled-in with the IoTC Client's current heap usage.

◆ iotc_get_network_timeout()

uint32_t iotc_get_network_timeout ( void  )

Returns the timeout value for socket connections.

Queries the current configuration of network timeout values that will be fed to that native network implementation when sockets are initialized.

See also
iotc_set_network_timeout

◆ iotc_initialize()

iotc_state_t iotc_initialize ( )

Required before use of the IoTC Client library.

This should be the first function that you call on a new runtime.

Return values
IOTC_STATE_OKStatus OK

◆ iotc_is_context_connected()

uint8_t iotc_is_context_connected ( iotc_context_handle_t  context_handle)

Used to determine the state of a IoTC Client context's connection to the remote service.

Parameters
[in]contexthandle to determine the connection status of.
See also
iotc_create_context
iotc_connect
iotc_connect_to
Return values
1if the context is currently connected
0if the context is invalid, or the connection is currently any one of the other following states: Unitialized, connecting, closing or closed.

◆ iotc_publish()

iotc_state_t iotc_publish ( iotc_context_handle_t  iotc_h,
const char *  topic,
const char *  msg,
const iotc_mqtt_qos_t  qos,
iotc_user_callback_t callback,
void *  user_data 
)

◆ iotc_publish_data()

iotc_state_t iotc_publish_data ( iotc_context_handle_t  iotc_h,
const char *  topic,
const uint8_t *  data,
size_t  data_len,
const iotc_mqtt_qos_t  qos,
iotc_user_callback_t callback,
void *  user_data 
)

Publishes binary data to the Google Cloud IoT service on the given topic.

Using the provided context, this function requests that binary data be delivered to the IoT Core service on the given topic. This requires that a connection already be made to the IoT Core service via a iotc_connect call.

Note: This function is idential to iotc_publish except that it doesn't assume a null terminated string. Instead a pointer and data length are provided.

Parameters
[in]datathe payload to send to the IoT Core service.
[in]data_lenthe size in bytes of the data buf to send.
See also
iotc_create_context
iotc_publish
Return values
IOTC_STATE_OKIf the publication request was formatted correctly.
IOTC_OUT_OF_MEMORYIf the platform did not have enough free memory to fulfill the request.
IOTC_INTERNAL_ERRORIf an unforseen and unrecoverable error has occurred.

◆ iotc_schedule_timed_task()

iotc_timed_task_handle_t iotc_schedule_timed_task ( iotc_context_handle_t  iotc_h,
iotc_user_task_callback_t callback,
const iotc_time_t  seconds_from_now,
const uint8_t  repeats_forever,
void *  data 
)

Schedule a task for timed execution Using the provided context, this function adds a task to the internal event system.

The callback method will be invoked when the given time has passed.

The callback function should have the following siguature: typedef void ( iotc_user_task_callback_t ) ( const iotc_context_handle_t context_handle, const iotc_timed_task_handle_t, timed_task_handle, void* user_data );

where:

  • in_context_handle the context handle you provided to iotc_schedule_timed_task
  • timed_task_handle is the handle that iotc_schedule_timed_task call returned, this can be used to cancel the task if the task is no longer needed from within the callback
  • user_data is the data you provided to iotc_schedule_timed_task
Parameters
[in]iotc_ha context handle created by invoking iotc_create_context.
[in]iotc_user_task_callback_t*a function pointer to be invoked when given time has passed.
[in]seconds_from_nownumber of seconds to wait before task invocation.
[in]repeats_foreverif zero is passed the callback will be called only once, otherwise the callback will be called continuously with the seconds_from_now delay until iotc_cancel_timed_task() called.
See also
iotc_create_context
iotc_cancel_timed_task
Return values
iotc_time_task_handle_tif bigger than 0 it's the unique identifier of the task, if smaller, it's an error code multiplied by -1, the possible error codes are:
  • IOTC_STATE_OK If the request was formatted correctly.
  • IOTC_OUT_OF_MEMORY If the platform did not have enough free memory to fulfill the request
  • IOTC_INTERNAL_ERROR If an unforseen and unrecoverable error has occurred.

◆ iotc_set_maximum_heap_usage()

iotc_state_t iotc_set_maximum_heap_usage ( const size_t  max_bytes)

Sets Maximum Amount of Heap Allocated Memory the IoTC Client may use.

This function is part of an optional configuration of the IoTC Client called the Memory Limiter.

This system can be used to guarantee that the IoTC Client will use only a certain amount of the heapspace during its standard execution. On many platforms this can also include the heap usage of the TLS implementation if the TLS implementation can be written to use the IoTC malloc and free functions.

Parameters
[in]max_bytesthe upper bounds of memory that the IoTC Client will use in its serialization, parsing and encryption of mqtt packets, and for the facilitation of Client Application callbacks and events.
Return values
IOTC_NOT_SUPPORTEDIf the Memory Limiter module has not been compiled into this version of the IoTC Client Library
IOTC_OUT_OF_MEMORYIf the new memory limit is too small to support the current IoTC Client heapspace footprint.
IOTC_STATE_OKthe new memory limit has been set to the value specified by max_bytes.

◆ iotc_set_network_timeout()

void iotc_set_network_timeout ( uint32_t  timeout)

Timeout value that's passed to the networking implementation This function configures how long a socket will live while there is no data sent on the socket.

Note that the MQTT Keep Alive value configured in the iotc_connect will cause the IoTC Client to periodically create network traffic, as part of the MQTT specification.

This value is only observed when constructing new connections, so invoking this will not have any affect on on-going connections.

Parameters
[in]timeouta timeout value that will be passed to the implementing networking layer during socket initialization.
See also
iotc_connect
iotc_connect_to
iotc_get_network_timeout

◆ iotc_shutdown()

iotc_state_t iotc_shutdown ( )

Signals the IoTC Client library to cleanup any internal memory.

This should by the last function called while shutting down your application. Any resources that were created during initialization will be cleaned up and freed.

Note that you should always clean up IoTC contexts individually with iotc_delete_context

See also
iotc_initialize
iotc_create_context
iotc_delete_context
iotc_shutdown_connection
Return values
IOTC_STATE_OKStatus OK
IOTC_FAILED_INITIALIZATIONAn urecoverable error occured

◆ iotc_shutdown_connection()

iotc_state_t iotc_shutdown_connection ( iotc_context_handle_t  iotc_h)

Closes the connection associated with the provide context.

Closes the connection to the Google Cloud IoT Core Service. This will happen asynchronously. The callback is defined by iotc_connect.

Parameters
[in]iotc_ha context handle created by invoking iotc_create_context.
Return values
IOTC_SOCKET_NO_ACTIVE_CONNECTION_ERRORIf there is no connection for this context.
IOTC_STATE_OKIf the disconnection request has been queued for process.
See also
iotc_create_context
iotc_connect

◆ iotc_subscribe()

iotc_state_t iotc_subscribe ( iotc_context_handle_t  iotc_h,
const char *  topic,
const iotc_mqtt_qos_t  qos,
iotc_user_subscription_callback_t callback,
void *  user_data 
)

Subscribes to notifications if a message from the Google Cloud IoT Core service is posted to the given topic.

Using the provided context, this function formats a request to subscribe to a topic in the IoT Core service. If the subscription is successful then incoming messages will be provided to the callback function. Subscription falures will also invoke the callback function with parameters that denote the error.

The callback function is type defined by iotc_user_callback_t, which has the following siguature: void foo( iotc_context_handle_t in_context_handle, iotc_sub_call_type_t call_type, const iotc_sub_call_params_t* const params, iotc_state_t state, void* user_data )

where:

  • context_handle is the context handle you provided to iotc_subscribe.
  • call_type each callback invocation may be related to subscription confirmation or a new message, this enum points to the type of the invocation.
  • params is a structure that holds the details about the subscription confirmation or new message. The data in this structure will vary depending on the call_type value
  • state should be IOTC_STATE_OK if the message reception succeeded or IOTC_MQTT_SUBSCRIPTION_SUCCESSFULL/IOTC_MQTT_SUBSCRIPTION_FAILED in case of successfull / failed subscription, values different than described should be treated as errors.
  • user_data is the pointer you provided to user parameter in iotc_subscribe
Parameters
[in]iotc_ha context handle created by invoking iotc_create_context
[in]topica string based topic name that you have created for messaging via the IoT Core webservice.
[in]qosQuality of Service MQTT level. 0, 1, or 2. Please see MQTT specification or iotc_mqtt_qos_e in iotc_mqtt_message.h for constant values.
[in]callbacka function pointer to be invoked when a message arrives, as described above
[in]usera pointer that will be returned back during the callback invocation
See also
iotc_create_context
iotc_connect
iotc_publish
iotc_publish_data
Return values
IOTC_STATE_OKIf the publication request was formatted correctly.
IOTC_OUT_OF_MEMORYIf the platform did not have enough free memory to fulfill the request.
IOTC_INTERNAL_ERRORIf an unforseen and unrecoverable error has occurred.

Variable Documentation

◆ iotc_cilent_version_str

const char iotc_cilent_version_str[]

String representation of the Major.Minor.Revision version of the IoTC Client library.

library.

◆ iotc_major

const uint16_t iotc_major

Contains the major version number of the IoTC Client library.

◆ iotc_minor

const uint16_t iotc_minor

Contains the minor version number of the IoTC Client library.

◆ iotc_revision

const uint16_t iotc_revision

Contains the revsion number of the IoTC Client library.