IoTivity-Lite
Server side

Optional group of functions OCF server support. More...

Modules

 Collection Support
 Optional group of functions to support OCF compliant collections.
 

Typedefs

typedef void(* oc_con_write_cb_t) (size_t device_index, oc_rep_t *rep)
 Callback for change notifications from the oic.wk.con resource. More...
 

Functions

bool oc_add_resource (oc_resource_t *resource)
 Add a resource to the IoTivity stack. More...
 
bool oc_delete_resource (oc_resource_t *resource)
 Remove a resource from the IoTivity stack and delete the resource. More...
 
void oc_device_bind_resource_type (size_t device, const char *type)
 Add a Resource Type "rt" property to the an /oic/d resource. More...
 
int oc_get_query_value (oc_request_t *request, const char *key, char **value)
 Get a pointer to the start of the value in a URL query parameter key=value pair. More...
 
void oc_ignore_request (oc_request_t *request)
 Ignore the request. More...
 
void oc_indicate_separate_response (oc_request_t *request, oc_separate_response_t *response)
 Respond to an incoming request asynchronously. More...
 
void oc_init_query_iterator (void)
 This resets the query iterator to the start of the URI query parameter. More...
 
int oc_iterate_query (oc_request_t *request, char **key, size_t *key_len, char **value, size_t *value_len)
 Iterate through the URI query parameters and get each key=value pair. More...
 
bool oc_iterate_query_get_values (oc_request_t *request, const char *key, char **value, int *value_len)
 Iterate though the URI query parameters for a specific key. More...
 
oc_resource_t * oc_new_resource (const char *name, const char *uri, uint8_t num_resource_types, size_t device)
 Allocate and populate a new oc_resource_t. More...
 
int oc_notify_observers (oc_resource_t *resource)
 Notify all observers of a change to a given resource's property. More...
 
void oc_process_baseline_interface (oc_resource_t *resource)
 Helper function used when responding to a GET request to add Common Properties to a GET response. More...
 
void oc_resource_bind_resource_interface (oc_resource_t *resource, oc_interface_mask_t iface_mask)
 Add the supported interface(s) to the resource. More...
 
void oc_resource_bind_resource_type (oc_resource_t *resource, const char *type)
 Add a Resource Type "rt" property to the resource. More...
 
void oc_resource_make_public (oc_resource_t *resource)
 Expose unsecured coap:// endpoints (in addition to secured coaps:// endpoints) for this resource in /oic/res. More...
 
void oc_resource_set_default_interface (oc_resource_t *resource, oc_interface_mask_t iface_mask)
 Select the default interface. More...
 
void oc_resource_set_discoverable (oc_resource_t *resource, bool state)
 Specify if a resource can be found using OCF discover mechanisms. More...
 
void oc_resource_set_observable (oc_resource_t *resource, bool state)
 Specify that a resource should notify clients when a property has been modified. More...
 
void oc_resource_set_periodic_observable (oc_resource_t *resource, uint16_t seconds)
 The resource will periodically notify observing clients of is property values. More...
 
void oc_resource_set_properties_cbs (oc_resource_t *resource, oc_get_properties_cb_t get_properties, void *get_propr_user_data, oc_set_properties_cb_t set_properties, void *set_props_user_data)
 
void oc_resource_set_request_handler (oc_resource_t *resource, oc_method_t method, oc_request_callback_t callback, void *user_data)
 Specify a request_callback for GET, PUT, POST, and DELETE methods. More...
 
void oc_send_response (oc_request_t *request, oc_status_t response_code)
 Called after the response to a GET, PUT, POST or DELETE call has been prepared completed. More...
 
void oc_send_separate_response (oc_separate_response_t *handle, oc_status_t response_code)
 Called to send the deferred response to a GET, PUT, POST or DELETE request. More...
 
void oc_set_con_write_cb (oc_con_write_cb_t callback)
 Sets the callback to receive change notifications for the oic.wk.con resource. More...
 
void oc_set_separate_response_buffer (oc_separate_response_t *handle)
 Set a response buffer for holding the response payload. More...
 

Detailed Description

Optional group of functions OCF server support.

Typedef Documentation

◆ oc_con_write_cb_t

typedef void(* oc_con_write_cb_t) (size_t device_index, oc_rep_t *rep)

Callback for change notifications from the oic.wk.con resource.

This callback is invoked to notify a change of one or more properties on the oic.wk.con resource. The rep parameter contains all properties, the function is not invoked for each property.

When the function is invoked, all properties handled by the stack are already updated. The callee can use the invocation to optionally store the new values persistently.

Once the callback returns, the response will be sent to the client and observers will be notified.

Note
As of now only the attribute "n" is supported.
The callee shall not block for too long as the stack is blocked during the invocation.
Parameters
device_indexindex of the device to which the change was applied, 0 is the first device
replist of properties and their new values

Function Documentation

◆ oc_add_resource()

bool oc_add_resource ( oc_resource_t *  resource)

Add a resource to the IoTivity stack.

The resource will be validated then added to the stack.

Parameters
[in]resourcethe resource to add to the stack
Returns
  • true: the resource was successfully added to the stack.
  • false: the resource can not be added to the stack.

◆ oc_delete_resource()

bool oc_delete_resource ( oc_resource_t *  resource)

Remove a resource from the IoTivity stack and delete the resource.

Any resource observers will automatically be removed.

This will free the memory associated with the resource.

Parameters
[in]resourcethe resource to delete
Returns
  • true: when the resource has been deleted and memory freed.
  • false: there was an issue deleting the resource.

◆ oc_device_bind_resource_type()

void oc_device_bind_resource_type ( size_t  device,
const char *  type 
)

Add a Resource Type "rt" property to the an /oic/d resource.

This function can be used to bind a new Resource Type to a logical device's /oic/d resource.

Parameters
[in]deviceindex of a logical device
[in]typethe Resource type to add to the Resource Type "rt" property

◆ oc_get_query_value()

int oc_get_query_value ( oc_request_t *  request,
const char *  key,
char **  value 
)

Get a pointer to the start of the value in a URL query parameter key=value pair.

Note
The char pointer returned is pointing to the string location in the query string. Do not rely on a nul terminator to find the end of the string since there may be additional query parameters.
Parameters
[in]requestthe oc_request_t that contains the query parameters
[in]keythe key being searched for
[out]valuepointer to the value string assigned to the key
Returns
  • The position in the query string of the next key=value string pair
  • -1 if there are no additional query parameters

◆ oc_ignore_request()

void oc_ignore_request ( oc_request_t *  request)

Ignore the request.

The GET, PUT, POST or DELETE requests can be ignored. For example a oc_request_callback_t may only want to respond to multicast requests. Thus any request that is not over multicast endpoint could be ignored.

Using oc_ignore(request) is preferred over oc_send_response(request, OC_IGNORE) since it does not attempt to fill the response buffer before sending the response.

Parameters
[in]requestthe request being responded to
See also
oc_request_callback_t
oc_send_response

◆ oc_indicate_separate_response()

void oc_indicate_separate_response ( oc_request_t *  request,
oc_separate_response_t *  response 
)

Respond to an incoming request asynchronously.

If for some reason the response to a request would take a long time or is not immediately available, then this function may be used defer responding to the request.

Example:

static oc_separate_response_t sep_response;
static oc_event_callback_retval_t
handle_separate_response(void *data)
{
if (sep_response.active) {
printf("Handle separate response for GET handler:\n");
oc_rep_set_boolean(root, value, true);
oc_rep_set_int(root, dimmingSetting, 75);
oc_send_separate_response(&sep_response, OC_STATUS_OK);
}
return OC_EVENT_DONE;
}
static void
get_handler(oc_request_t *request, oc_interface_mask_t iface_mask,
void *user_data)
{
printf("GET handler:\n");
oc_indicate_separate_response(request, &sep_response);
oc_set_delayed_callback(NULL, &handle_separate_response, 10);
}
Parameters
[in]requestthe request that will be responded to as a separate response
[in]responseinstance of an internal struct that is used to track the state of the separate response.
See also
oc_set_separate_response_buffer
oc_send_separate_response

◆ oc_init_query_iterator()

void oc_init_query_iterator ( void  )

This resets the query iterator to the start of the URI query parameter.

This is used together with oc_iterate_query_get_values() or oc_iterate_query() to iterate through query parameter of a URI that are part of an oc_request_t

◆ oc_iterate_query()

int oc_iterate_query ( oc_request_t *  request,
char **  key,
size_t *  key_len,
char **  value,
size_t *  value_len 
)

Iterate through the URI query parameters and get each key=value pair.

Before calling oc_iterate_query() the first time oc_init_query_iterator() must be called to reset the query iterator to the first query parameter.

Note
the char pointers returned are pointing to the string location in the query string. Do not rely on a nul terminator to find the end of the string since there may be additional query parameters.

Example:

char *value = NULL;
int value_len = -1;
char *key
while (oc_iterate_query(request, &key, &key_len, &value, &value_len) > 0) {
printf("%.*s = %.*s\n", key_len, key, query_value_len, query_value);
}
Parameters
[in]requestthe oc_request_t that contains the query parameters
[out]keypointer to the location of the the key of the key=value pair
[out]key_lenthe lenght of the key string
[out]valuepointer the location of the value string assigned to the key=value pair
[out]value_lenthe length of the value string
Returns
  • The position in the query string of the next key=value string pair
  • -1 if there are no additional query parameters

◆ oc_iterate_query_get_values()

bool oc_iterate_query_get_values ( oc_request_t *  request,
const char *  key,
char **  value,
int *  value_len 
)

Iterate though the URI query parameters for a specific key.

Before calling oc_iterate_query_get_values() the first time oc_init_query_iterator() must be called to reset the query iterator to the first query parameter.

Note
The char pointer returned is pointing to the string location in the query string. Do not rely on a nul terminator to find the end of the string since there may be additional query parameters.

Example:

bool more_query_params = false;
const char* expected_value = "world"
char *value = NULL;
int value_len = -1;
do {
more_query_params = oc_iterate_query_get_values(request, "hello",
&value, &value_len);
if (rt_len > 0) {
printf("Found %s = %.*s\n", "hello", value_len, value);
}
} while (more_query_params);
Parameters
[in]requestthe oc_request_t that contains the query parameters
[in]keythe key being searched for
[out]valuepointer to the value string for to the key=value pair
[out]value_lenthe length of the value string
Returns
True if there are more query parameters to iterate through

◆ oc_new_resource()

oc_resource_t* oc_new_resource ( const char *  name,
const char *  uri,
uint8_t  num_resource_types,
size_t  device 
)

Allocate and populate a new oc_resource_t.

Resources are the primary interface between code and real world devices.

Each resource has a Uniform Resource Identifier (URI) that identifies it. All resources must specify one or more Resource Types to be considered a valid resource. The number of Resource Types is specified by the num_resource_types the actual Resource Types are added later using the oc_resource_bind_resource_type() function.

The resource is populated with a default interface OC_IF_BASELINE.

Many properties associated with a resource are set or modified after the new resource has been created.

The resource is not added to the device till oc_add_resource() is called.

Example:

static void register_resources(void)
{
oc_resource_t *bswitch = oc_new_resource("light switch", "/switch", 1, 0);
oc_resource_bind_resource_type(bswitch, "oic.r.switch.binary");
oc_resource_set_request_handler(bswitch, OC_GET, get_switch, NULL);
oc_resource_set_request_handler(bswitch, OC_POST, post_switch, NULL);
oc_resource_set_request_handler(bswitch, OC_PUT, put_switch, NULL);
oc_add_resource(bswitch);
}
Parameters
[in]namethe name of the new resource this will set the property n
[in]urithe Uniform Resource Identifier for the resource
[in]num_resource_typesthe number of Resource Types that will be added/bound to the resource
[in]deviceindex of the logical device the resource will be added to
See also
oc_resource_bind_resource_interface
oc_resource_set_default_interface
oc_resource_bind_resource_type
oc_process_baseline_interface
oc_resource_set_discoverable
oc_resource_set_periodic_observable
oc_resource_set_request_handler

◆ oc_notify_observers()

int oc_notify_observers ( oc_resource_t *  resource)

Notify all observers of a change to a given resource's property.

Note
no need to call oc_notify_observers about resource changes that result from a PUT, or POST oc_request_callback_t.
Parameters
[in]resourcethe oc_resource_t that has a modified property
Returns
  • the number observers notified on success
  • 0 on failure could also mean no registered observers

◆ oc_process_baseline_interface()

void oc_process_baseline_interface ( oc_resource_t *  resource)

Helper function used when responding to a GET request to add Common Properties to a GET response.

This add Common Properties name ("n"), Interface ("if"), and Resource Type ("rt") to a GET response.

Example:

bool bswitch_state = false;
void get_bswitch(oc_resource_t *resource, oc_interface_mask_t iface_mask,
void *data)
{
switch (iface_mask) {
case OC_IF_BASELINE:
// fall through
case OC_IF_A:
oc_rep_set_boolean(root, value, bswitch_state);
break;
default:
break;
}
oc_send_response(request, OC_STATUS_OK);
}
Parameters
[in]resourcethe resource the baseline Common Properties will be read from to respond to the GET request

◆ oc_resource_bind_resource_interface()

void oc_resource_bind_resource_interface ( oc_resource_t *  resource,
oc_interface_mask_t  iface_mask 
)

Add the supported interface(s) to the resource.

Resource interfaces specify how the code is able to interact with the resource

The iface_mask is bitwise OR of the following interfaces:

  • OC_IF_BASELINE ("oic.if.baseline") baseline interface allow GET, PUT/POST, and notify/observe operations.
  • OC_IF_LL ("oic.if.ll") The links list interface is a specifically designed to provide a list of links pointing to other resources. Links list interfaces allow GET, and notify/observe operations.
  • OC_IF_B ("oic.if.b") batch interface. The batch interface is used to interact with a collection of resources at the same time.
  • OC_IF_R ("oic.if.r") a read-only interface. A read-only interface allows GET, and notify/observe operations.
  • OC_IF_RW ("oir.if.rw") a read-write interface. A read-write interface allows GET, PUT/POST, and notify/observe operations.
  • OC_IF_A ("oic.if.a") an actuator interface. An actuator interface allows GET, PUT/POST, and notify/observe operations.
  • OC_IF_S ("oic.if.s") a sensor interface. A sensor interface allows GET, and notify/observe operations.
  • OC_IC_CREATE ("oic.if.create") used to create new resources in a collection.

The read-write and actuator interfaces are very similar and sometimes hard to differentiate when one should be used over another. In general an actuator interface is used when it modifies the real world value. e.g. turn on light, increase temperature, open vent.

The read-only and sensor are also very similar in general a sensor value is read directly or indirectly from a real world sensor.

Parameters
[in]resourcethe resource that the interface(s) will be added to
[in]iface_maska bitwise ORed list of all interfaces supported by the resource.
See also
oc_interface_mask_t
oc_resource_set_default_interface

◆ oc_resource_bind_resource_type()

void oc_resource_bind_resource_type ( oc_resource_t *  resource,
const char *  type 
)

Add a Resource Type "rt" property to the resource.

All resources require at least one Resource Type. The number of Resource Types the resource contains is declared when the resource it created using oc_new_resource() function.

Resource Types use a dot "." naming scheme e.g. oic.r.switch.binary. Resource Types starting with oic are reserved for a OCF defined Resource Types. Developers are strongly encouraged to try and use an OCF defined Resource Type vs. creating their own. A repository of OCF defined resources can be found on oneiota.org.

Multi-value "rt" Resource means a resource with multiple Resource Types. i.e. oc_resource_bind_resource_type() is called multiple times for a single resource. When using a Mulit-value Resource the different resources properties must not conflict.

Parameters
[in]resourcethe resource that the Resource Type will be set on
[in]typethe Resource Type to add to the Resource Type "rt" property
See also
oc_new_resource
oc_device_bind_resource_type

◆ oc_resource_make_public()

void oc_resource_make_public ( oc_resource_t *  resource)

Expose unsecured coap:// endpoints (in addition to secured coaps:// endpoints) for this resource in /oic/res.

Note
While the resource may advertise unsecured endpoints, the resource shall remain inaccessible until the hosting device is configured with an anon-clear Access Control Entry (ACE).
Parameters
[in]resourcethe resource to make public
See also
oc_new_resource

◆ oc_resource_set_default_interface()

void oc_resource_set_default_interface ( oc_resource_t *  resource,
oc_interface_mask_t  iface_mask 
)

Select the default interface.

The default interface must be one of the resources specified in the oc_resource_bind_resource_interface() function.

If a request to the resource comes in and the interface is not specified then the default interface will be used to service the request.

If the default interface is not set then the OC_IF_BASELINE will be used by the stack.

Parameters
[in]resourcethe resource that the default interface will be set on
[in]iface_maska single interface that will will be used as the default interface

◆ oc_resource_set_discoverable()

void oc_resource_set_discoverable ( oc_resource_t *  resource,
bool  state 
)

Specify if a resource can be found using OCF discover mechanisms.

Parameters
[in]resourceto specify as discoverable or non-discoverable
[in]stateif true the resource will be discoverable if false the resource will be non-discoverable
See also
oc_new_resource for example code using this function

◆ oc_resource_set_observable()

void oc_resource_set_observable ( oc_resource_t *  resource,
bool  state 
)

Specify that a resource should notify clients when a property has been modified.

Note
this function can be used to make a periodic observable resource unobservable.
Parameters
[in]resourcethe resource to specify the observability
[in]statetrue to make resource observable, false to make resource unobservable
See also
oc_new_resource to see example code using this function
oc_resource_set_periodic_observable

◆ oc_resource_set_periodic_observable()

void oc_resource_set_periodic_observable ( oc_resource_t *  resource,
uint16_t  seconds 
)

The resource will periodically notify observing clients of is property values.

The oc_resource_set_observable() function can be used to turn off a periodic observable resource.

Setting a seconds frequency of zero 0 is invalid and will result in an invalid resource.

Parameters
[in]resourcethe resource to specify the periodic observability
[in]secondsthe frequency in seconds that the resource will send out an notification of is property values.

◆ oc_resource_set_request_handler()

void oc_resource_set_request_handler ( oc_resource_t *  resource,
oc_method_t  method,
oc_request_callback_t  callback,
void *  user_data 
)

Specify a request_callback for GET, PUT, POST, and DELETE methods.

All resources must provide at least one request handler to be a valid resource.

method types:

  • OC_GET the oc_request_callback_t is responsible for returning the current value of all of the resource properties.
  • OC_PUT the oc_request_callback_t is responsible for updating one or more of the resource properties.
  • OC_POST the oc_request_callback_t is responsible for updating one or more of the resource properties. The callback may also be responsible for creating new resources.
  • OC_DELETE the oc_request_callback_t is responsible for deleting a resource
Note
Some methods may never by invoked based on the resources Interface as well as the provisioning permissions of the client.
Parameters
[in]resourcethe resource the callback handler will be registered to
[in]methodspecify if type method the callback is responsible for handling
[in]callbackthe callback handler that will be invoked when a the method is called on the resource.
[in]user_datacontext pointer that is passed to the oc_request_callback_t. The pointer must remain valid as long as the resource exists.
See also
oc_new_resource to see example code using this function

◆ oc_send_response()

void oc_send_response ( oc_request_t *  request,
oc_status_t  response_code 
)

Called after the response to a GET, PUT, POST or DELETE call has been prepared completed.

The function oc_send_response is called at the end of a oc_request_callback_t to inform the caller about the status of the requested action.

Parameters
[in]requestthe request being responded to
[in]response_codethe status of the response
See also
oc_request_callback_t
oc_ignore_request
oc_indicate_separate_response

◆ oc_send_separate_response()

void oc_send_separate_response ( oc_separate_response_t *  handle,
oc_status_t  response_code 
)

Called to send the deferred response to a GET, PUT, POST or DELETE request.

The function oc_send_separate_response is called to initiate transfer of the response.

Parameters
[in]handleinstance of the internal struct that was passed to oc_indicate_separate_response()
[in]response_codethe status of the response
See also
oc_indicate_separate_response
oc_send_separate_response
oc_send_response
oc_ignore_request

◆ oc_set_con_write_cb()

void oc_set_con_write_cb ( oc_con_write_cb_t  callback)

Sets the callback to receive change notifications for the oic.wk.con resource.

The function can be used to set or unset the callback. Whenever an attribute of the oic.wk.con resource is changed, the callback will be invoked.

Parameters
callbackThe callback to register or NULL to unset it. If the function is invoked a second time, then the previously set callback is simply replaced.

◆ oc_set_separate_response_buffer()

void oc_set_separate_response_buffer ( oc_separate_response_t *  handle)

Set a response buffer for holding the response payload.

When a deferred response is ready, pass in the same oc_separate_response_t that was handed to oc_indicate_separate_response() for delaying the initial response.

Parameters
[in]handleinstance of the oc_separate_response_t that was passed to the oc_indicate_separate_response() function
See also
oc_indicate_separate_response
oc_send_separate_response