Site Tools


Resource Presence

Presence refers to active or advertisement discovery that happens at the device level. Although an application triggers the operation, the stack sends the actual notification. It operates in a server/client model where the server notifies its presence so the client can subscribe to or unsubscribe from a device's presence events. The resource to enable discovery is hosted locally on the client that initiates the discovery request. The publishing device (server) is responsible for the published information.

Call Flow

  • (1) OCPlatform::startPresence(30)
  • (2) OCStartPresence(30)
  • (3) Presence Notification
  • (4) OCPlatform::subscribePresence(presenceHandle, hostAddress, connectivityType, presenceHandler)
  • (5) OCDoResource(ocdoHandle, OC_REST_PRESENCE,host, resourceTpype, connectivityType, &cbdata, presenceHandler);
  • (6) OCPlatform::registerResource(m_resourceHandle, resourceURI, resourceTypeName, resourceInterface, &entityHandler, resourceProperty);
  • (7) OCDoResource(…)
  • (8) Presence Notification


APIs Functionalities Examples Client/Server API
OCStackResult startPresence(const unsigned int announceDurationSeconds) Start Presence announcements. OCPlatform::startPresence(30) Server
OCStackResult stopPresence() Stop Presence announcements. OCPlatform::stopPresence() Server
OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host, OCConnectivityType connectivityType, SubscribeCallback presenceHandler) subscribe OCPlatform::subscribePresence(presenceHandle, hostAddress, connectivityType, presenceHandle) Client
OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host, const std::string& resourceType, OCConnectivityType connectivityType, SubscribeCallback presenceHandle) subscribe OCPlatform::subscribePresence(presenceHandle, hostaddress, resourceType, connectivityType, presenceHandler) Client
OCStackResult unsubscribePresence(OCPresenceHandle presenceHandle) unsubscribe OCPlatform::unsubscribePresence(presenceHandle) Client

Presence Resource

The presence resource is modelled as a collection with URI of “/oic/ad', and each resource of type “”. Here are some examples of presence resource:

    std::array<std::string, numPresenceResources> resourceURI { {
        "/a/led" } };
    std::array<std::string, numPresenceResources> resourceTypeName { {
        "core.led" } };

Sample Application :

Presence samples provide examples to use presence APIs. They are also good examples showing the call flows described above.


The server starts the presence advertisement with TTL (time to live) in seconds. e.g:


The server can stop it by calling stopPresence()


While registering/creating presence resources, the server needs to go through each resource in the collection. e.g.

for(int i=0; i<numPresenceResources; i++)
        result = OCPlatform::registerResource(handle,
      ,, resourceInterface,
                &entityHandler, resourceProperty);
        if (result != OC_STACK_OK)
            cout << "Resource creation was unsuccessful with resource URI "


The client can choose to subscribe to the presence notification via unicast by giving a specific host address, or by multicast, e.g.

OCPlatform::subscribePresence(presenceHandle, hostAddress,
                            connectivityType, &presenceHandler);

Where presenceHandler is the callback function for presence, e.g.

void presenceHandler(OCStackResult result, const unsigned int nonce, const std::string& hostAddress)
    //function body

Next Steps

Once you're finished with this page, the next page in this guide covers common resource properties.

resource_presence.txt · Last modified: 2017/05/03 12:35 by poiema72