Skip to content

Agent API

The agent library provides some utility routines and cloud helper functions.

Most of the Ioto API is provided by the following component libraries:

Extensions

Agent Agent control structure.
Aws AWS Support.

Functions

intaws(Url *up, cchar *region, cchar *service, cchar *target, cchar *body, ssize bodyLen, cchar *headers, ...)
 Invoke an AWS API request.
intawsPutFileToS3(cchar *region, cchar *bucket, cchar *key, cchar *filename)
 Put a file to AWS S3.
intawsPutToS3(cchar *region, cchar *bucket, cchar *key, cchar *data, ssize dataLen)
 Put a data block to AWS S3.
char*awsSign(cchar *region, cchar *service, cchar *target, cchar *method, cchar *path, cchar *query, cchar *body, ssize bodyLen, cchar *headers, ...)
 Create a set of signed headers to use with AWS SigV4 REST APIs.
cchar*iaGetShadow(cchar *store, cchar *key, cchar *defaultValue)
 Get a value from the shadow state.
WebHost*iaGetWebHost(void)
 Get the WebHost instance singleton.
voidiaInit(cchar *profile)
 Initialize Agent.
voidiaSaveShadow(void)
 Save the shadow state immediately.
voidiaSetShadow(cchar *store, cchar *key, cchar *value, bool save)
 Set a key value in a store.
intiaStart(void)
 User start entry point.
voidiaStop(void)
 User stop entry point.
voidiaTerm(void)
 Terminate Agent.

Typedefs

Defines

#defineIA_AUTH_FILE   "auth.json5"
 Name of the authentication control config file.
#defineIA_CONFIG_FILE   "config.json5"
 Name of the primary control config file.
#defineIA_DEVICE_FILE   "device.json5"
 Name of the device identification config file.
#defineIA_LOCAL_FILE   "local.json5"
 Development overrides.
#defineIA_LOG_FILE   "agent.log"
 Name of the agent log file.
#defineIA_LOG_GROUP   "ioto"
 AWS log group name.
#defineIA_LOG_LINGER   5000
 Delay before flushing log events to the cloud.
#defineIA_LOG_MAX_EVENTS   1000
 Max log events to buffer.
#defineIA_LOG_MAX_SIZE   32767
 Max size of log events to buffer.
#defineIA_LOG_STREAM   "agent"
 AWS log stream name.
#defineIA_MAX_URL   256
 Sanity length of a URL.
#defineIA_MESSAGE_SIZE   128 * 1024 * 1024
 Maximum MQTT message size.
#defineIA_PROVISION_FILE   "provision.json5"
 Name of the device provisioning config file.
#defineIA_ROOT_CERTS   "certs/roots.ca"
 Name of the root certificates bundle for use by Url.
#defineIA_SAVE_DELAY   5000
 Delay before saving updated shadow state.
#defineIA_SHADOW_FILE   "shadow.json5"
 Name of the persisted AWS shadow state file.
#defineIA_WEB_FILE   "web.json5"
 Name of the web server config file.

Agent

Agent

Agent control structure.

API Stability:
Evolving MOB - rename Agent.
Fields:
cchar *admin Admin API endpoint.
cchar *api Device cloud API endpoint.
cchar *apiToken Device cloud API authentication token.
Json *auth Auth configuraiton.
cchar *awsAccess AWS temp creds.
TimeawsExpires AWS cred expiry.
cchar *awsRegion Default AWS region.
cchar *awsSecret AWS cred secret.
cchar *awsToken AWS cred tokenn.
Json *config Configuration.
Db *db Structured state database.
RBuf *dbChanges Buffer of database change records.
cchar *deviceId Device ID from device.json5.id.
TicksfirstSyncChange When the first database state change happend.
char *instance Device claimed and provisioned by device cloud. EC2 instance.
boolkeyService Manufacturing serialization.
struct AgentLog *log Cloud Watch Log object.
RList *logs Log file ingestion list.
boollogsService AWS IAM key generation.
Mqtt *mqtt Mqtt object.
boolmqttService Log file ingest to CloudWatch logs.
char *profile Run profile. Defaults to "prod".
boolprovisioned Web server.
boolprovisionService MQTT service.
boolserializeService AWS IoT core service.
Json *shadow Shadow state.
cchar *shadowName AWS IoT shadow name.
boolshadowService Device registration service.
char *shadowTopic AWS IoT shadow topic.
TickssyncDelay Delay to flush database changes to the cloud.
REventsyncEvent Schedule synchronization event.
ssizesyncLimit Limit of buffered database changes.
boolsyncService AWS IoT core shadows.
cchar *syncToken Authentication token when sending database changes to the cloud.
cchar *thingName AWS IoT thing name.
boolwebService Sync device state to AWS.

cchar * iaGetShadow (cchar *store, cchar *key, cchar *defaultValue)

Get a value from the shadow state.

Parameters:
storeName of the state store. Set to "shadow".
keyProperty key value. May contain dots.
defaultValueDefault value to return if the key is not found.
API Stability:
Evolving.
See Also:

WebHost * iaGetWebHost (void )

Get the WebHost instance singleton.

Returns:
The WebHost object.
API Stability:
Evolving.
See Also:

void iaInit (cchar *profile)

Initialize Agent.

Parameters:
profileConfiguration profile. Defaults to "prod".
API Stability:
Evolving.
See Also:

void iaSaveShadow (void )

Save the shadow state immediately.

API Stability:
Evolving.
See Also:

void iaSetShadow (cchar *store, cchar *key, cchar *value, bool save)

Set a key value in a store.

Parameters:
storeName of the state store. Set to "shadow".
keyProperty key value. May contain dots.
valueValue to set.
saveSet to true to persist immediately.
API Stability:
Evolving.
See Also:

int iaStart (void )

User start entry point.

Description:
The iaStart function is invoked when Agent is fully initialized and ready to start. Users can provide their own iaStart and iaStop functions and link with the Agent library. Agent will then invoke the user's iaStart for custom initialization.
API Stability:
Evolving.
See Also:
iaStop

void iaStop (void )

User stop entry point.

Description:
The iaStop function is invoked when Agent is shutting down. Users can provide their own iaStart function and link with the Agent library. Agent will then invoke the user's iaStop for custom shutdown cleanup.
API Stability:
Evolving.
See Also:
iaStart

void iaTerm (void )

Terminate Agent.

API Stability:
Evolving.
See Also:

Aws

Aws

AWS Support.

Description:
This is suite of AWS helper routines that implement AWS SigV4 signed REST API requests.
API Stability:
Evolving.
See Also:
aws, awsPutFileToS3, awsPutToS3, awsSign
Fields:

int aws (Url *up, cchar *region, cchar *service, cchar *target, cchar *body, ssize bodyLen, cchar *headers, ...)

Invoke an AWS API request.

Parameters:
upUrl object allocated via urlAlloc.
regionAWS Region to target.
serviceAWS Service name. E.g. "s3".
targetAWS Target name. If not supplied, this is deduced from the service name and the "x-amz-target" header.
bodyRequest body data.
bodyLenLength of the request body.
headersFormat string containing HTTP headers. This format string can use printf embedded tokens that will be expanded to form the HTTP headers. The header format is of the form: "Key:Value\n..." with an extra new line at the end.
...Headers arguments.
API Stability:
Evolving.
See Also:
awsPutFileToS3, awsPutToS3, awsSign

int awsPutFileToS3 (cchar *region, cchar *bucket, cchar *key, cchar *filename)

Put a file to AWS S3.

Parameters:
regionAWS Region to target.
bucketAWS S3 bucket name.
keyAWS S3 bucket key (filename). If set to null, the key is set to the filename.
filenameFile name to put to S3.
API Stability:
Evolving.
See Also:
aws, awsPutToS3, awsSign

int awsPutToS3 (cchar *region, cchar *bucket, cchar *key, cchar *data, ssize dataLen)

Put a data block to AWS S3.

Parameters:
regionAWS Region to target.
bucketAWS S3 bucket name.
keyAWS S3 bucket key (filename).
dataData block to write to S3.
dataLenLength of the data block.
API Stability:
Evolving.
See Also:
aws, awsPutFileToS3, awsSign

char * awsSign (cchar *region, cchar *service, cchar *target, cchar *method, cchar *path, cchar *query, cchar *body, ssize bodyLen, cchar *headers, ...)

Create a set of signed headers to use with AWS SigV4 REST APIs.

Description:
The AWS SDK is big and relatively slow. If you only need a few AWS APIs, you can use the AWS REST APIs and avoid the AWS SDK. This routine processes HTTP request parameters to create a set of signed HTTP headers that can be used with the URL HTTP client.
Parameters:
regionAWS Region to target.
serviceAWS Service name. E.g. "s3".
targetAWS Target name. If not supplied, this is deduced from the service name and the "x-amz-target" header.
methodHTTP method to utilize.
pathURL request path.
queryURL request query.
bodyRequest body data.
bodyLenLength of the request body.
headersFormat string containing HTTP headers. This format string can use printf embedded tokens that will be expanded to form the HTTP headers. The header format is of the form: "Key:Value\n..." with an extra new line at the end.
...Headers arguments.
Returns:
The HTTP headers to use with a URL HTTP client request. Caller must free.
API Stability:
Evolving.
See Also:
aws, awsPutFileToS3, awsPutToS3

Typedefs