OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED
} OCStackState;
+#ifdef WITH_PRESENCE
+typedef enum {
+ OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED
+} OCPresenceState;
+#endif
+
//-----------------------------------------------------------------------------
// Private variables
//-----------------------------------------------------------------------------
static OCStackState stackState = OC_STACK_UNINITIALIZED;
+
OCResource *headResource = NULL;
#ifdef WITH_PRESENCE
+static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
static PresenceResource presenceResource;
uint8_t PresenceTimeOutSize = 4;
uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
OC_LOG(INFO, TAG, PCF("Calling into application address space"));
result = response->cbNode->callBack(response->cbNode->context,
response->cbNode->handle, response->clientResponse);
- #ifdef WITH_PRESENCE
- if (result == OC_STACK_DELETE_TRANSACTION ||
- response->clientResponse->result == OC_STACK_COMM_ERROR ||
- response->clientResponse->result == OC_STACK_PRESENCE_STOPPED)
- #else
if (result == OC_STACK_DELETE_TRANSACTION ||
response->clientResponse->result == OC_STACK_COMM_ERROR)
- #endif
{
FindAndDeleteClientCB(response->cbNode);
}
* presence notifications to clients via multicast. Once this API has been called with a success,
* clients may query for this server's presence and this server's stack will respond via multicast.
*
- * @param ttl (Time To Live in seconds) - Used to set the time a server
+ * Server can call this function when it comes online for the first time, or when it comes back
+ * online from offline mode, or when it re enters network.
+ *
+ * @param ttl - Time To Live in seconds
* Note: If ttl is '0', then the default stack value will be used (60 Seconds).
*
* @return
*/
OCStackResult OCStartPresence(const uint32_t ttl)
{
- if(((OCResource *)presenceResource.handle)->resourceProperties & OC_ACTIVE)
- {
- return OC_STACK_ERROR;
- }
OCChangeResourceProperty(
&(((OCResource *)presenceResource.handle)->resourceProperties),
OC_ACTIVE, 1);
+
if(ttl > 0)
{
presenceResource.presenceTTL = ttl;
}
- OCDevAddr multiCastAddr = {0};
- OCCoAPToken * token = OCGenerateCoAPToken();
- OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
- //add the presence observer
- AddObserver(OC_PRESENCE_URI, NULL, token, &multiCastAddr,
+
+ if(OC_PRESENCE_UNINITIALIZED == presenceState)
+ {
+ presenceState = OC_PRESENCE_INITIALIZED;
+
+ OCDevAddr multiCastAddr = {0};
+ OCCoAPToken * token = OCGenerateCoAPToken();
+ OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
+ //add the presence observer
+ AddObserver(OC_PRESENCE_URI, NULL, token, &multiCastAddr,
(OCResource *)presenceResource.handle, OC_NON_CONFIRMABLE);
+ }
return OCNotifyObservers(presenceResource.handle);
}
* presence notifications to clients via multicast. Once this API has been called with a success,
* this server's stack will not respond to clients querying for this server's presence.
*
+ * Server can call this function when it is terminating, going offline, or when going
+ * away from network.
+ *
* @return
* OC_STACK_OK - No errors; Success
* OC_STACK_ERROR - @ref OCStartPresence has never been called or @ref OCStopPresence has
int m_power;
std::string m_lightUri;
std::string m_lightUri2;
+ std::string m_lightUri3;
OCResourceHandle m_resourceHandle;
OCResourceHandle m_resourceHandle2;
+ OCResourceHandle m_resourceHandle3;
public:
/// Constructor
- LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"), m_lightUri2("/a/light2") {}
+ LightResource(): m_state(false), m_power(0), m_lightUri("/a/light"),
+ m_lightUri2("/a/light2"),m_lightUri3("/a/light3") {}
/* Note that this does not need to be a member function: for classes you do not have
access to, you can accomplish this with a free function: */
}
}
+ void createResource3(OC::OCPlatform& platform)
+ {
+ std::string resourceURI = m_lightUri3; // URI of the resource
+ std::string resourceTypeName = "core.light";
+ std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
+
+ // OCResourceProperty is defined ocstack.h
+ uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+
+ // This will internally create and register the resource.
+ OCStackResult result = platform.registerResource(
+ m_resourceHandle3, resourceURI, resourceTypeName,
+ resourceInterface, &entityHandler, resourceProperty);
+
+ if (OC_STACK_OK != result)
+ {
+ cout << "Resource creation was unsuccessful\n";
+ }
+ }
+
OCResourceHandle getHandle()
{
return m_resourceHandle;
{
OCPlatform platform(cfg);
- // Time to Live is 100
- platform.startPresence(100);
+ // Time to Live is 30 seconds
+ platform.startPresence(30);
// Invoke createResource function of class light.
myLightResource.createResource(platform);
- printf("Enter a key to create the second resource\n");
+ printf("\nEnter a key to create the second resource\n");
getchar();
myLightResource.createResource2(platform);
+ printf("\nEnter a key to stop the presence\n");
+ getchar();
+ platform.stopPresence();
+
+ printf("\nEnter a key to restart the presence\n");
+ getchar();
+
+ platform.startPresence(30);
+
+ printf("\nEnter a key to create the third resource\n");
+ getchar();
+
+ myLightResource.createResource3(platform);
+
// Perform app tasks
while(true)
{
public:
/**
- * Start or stop Presence announcements.
- *
- * @param announceDuration - Duration to keep presence duration active.
+ * Start Presence announcements.
+ *
+ * @param ttl - time to live
* @return OCStackResult - Returns OCSTACK_OK if success <br>
*
- * These apply only if a server instance is active.
+ * Server can call this function when it comes online for the
+ * first time, or when it comes back online from offline mode,
+ * or when it re enters network.
+ *
+ */
+
+ OCStackResult startPresence(const unsigned int ttl);
+
+ /**
+ * Stop Presence announcements.
+ *
+ * @return OCStackResult - Returns OCSTACK_OK if success <br>
+ *
+ * Server can call this function when it is terminating,
+ * going offline, or when going away from network.
+ *
*/
- OCStackResult startPresence(const unsigned int announceDurationSeconds);
OCStackResult stopPresence();